Mark ChangeLog
[official-gcc.git] / gcc / c-pretty-print.c
blob14c29dae413bed29120d3ae95152493586e1caa4
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "real.h"
26 #include "c-pretty-print.h"
27 #include "c-tree.h"
28 #include "tree-iterator.h"
29 #include "diagnostic.h"
31 /* The pretty-printer code is primarily designed to closely follow
32 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
33 codes we used to have in the past. Following a structured
34 approach (preferably the official grammars) is believed to make it
35 much easier to add extensions and nifty pretty-printing effects that
36 takes expression or declaration contexts into account. */
39 #define pp_c_maybe_whitespace(PP) \
40 do { \
41 if (pp_base (PP)->padding == pp_before) \
42 pp_c_whitespace (PP); \
43 } while (0)
45 /* literal */
46 static void pp_c_char (c_pretty_printer *, int);
48 /* postfix-expression */
49 static void pp_c_initializer_list (c_pretty_printer *, tree);
50 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
52 static void pp_c_multiplicative_expression (c_pretty_printer *, tree);
53 static void pp_c_additive_expression (c_pretty_printer *, tree);
54 static void pp_c_shift_expression (c_pretty_printer *, tree);
55 static void pp_c_relational_expression (c_pretty_printer *, tree);
56 static void pp_c_equality_expression (c_pretty_printer *, tree);
57 static void pp_c_and_expression (c_pretty_printer *, tree);
58 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
59 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
60 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
61 static void pp_c_conditional_expression (c_pretty_printer *, tree);
62 static void pp_c_assignment_expression (c_pretty_printer *, tree);
64 /* declarations. */
67 /* Helper functions. */
69 void
70 pp_c_whitespace (c_pretty_printer *pp)
72 pp_space (pp);
73 pp_base (pp)->padding = pp_none;
76 void
77 pp_c_left_paren (c_pretty_printer *pp)
79 pp_left_paren (pp);
80 pp_base (pp)->padding = pp_none;
83 void
84 pp_c_right_paren (c_pretty_printer *pp)
86 pp_right_paren (pp);
87 pp_base (pp)->padding = pp_none;
90 void
91 pp_c_left_brace (c_pretty_printer *pp)
93 pp_left_brace (pp);
94 pp_base (pp)->padding = pp_none;
97 void
98 pp_c_right_brace (c_pretty_printer *pp)
100 pp_right_brace (pp);
101 pp_base (pp)->padding = pp_none;
104 void
105 pp_c_left_bracket (c_pretty_printer *pp)
107 pp_left_bracket (pp);
108 pp_base (pp)->padding = pp_none;
111 void
112 pp_c_right_bracket (c_pretty_printer *pp)
114 pp_right_bracket (pp);
115 pp_base (pp)->padding = pp_none;
118 void
119 pp_c_dot (c_pretty_printer *pp)
121 pp_dot (pp);
122 pp_base (pp)->padding = pp_none;
125 void
126 pp_c_ampersand (c_pretty_printer *pp)
128 pp_ampersand (pp);
129 pp_base (pp)->padding = pp_none;
132 void
133 pp_c_star (c_pretty_printer *pp)
135 pp_star (pp);
136 pp_base (pp)->padding = pp_none;
139 void
140 pp_c_arrow (c_pretty_printer *pp)
142 pp_arrow (pp);
143 pp_base (pp)->padding = pp_none;
146 void
147 pp_c_semicolon (c_pretty_printer *pp)
149 pp_semicolon (pp);
150 pp_base (pp)->padding = pp_none;
153 void
154 pp_c_complement (c_pretty_printer *pp)
156 pp_complement (pp);
157 pp_base (pp)->padding = pp_none;
160 void
161 pp_c_exclamation (c_pretty_printer *pp)
163 pp_exclamation (pp);
164 pp_base (pp)->padding = pp_none;
167 /* Print out the external representation of CV-QUALIFIER. */
169 static void
170 pp_c_cv_qualifier (c_pretty_printer *pp, const char *cv)
172 const char *p = pp_last_position_in_text (pp);
173 /* The C programming language does not have references, but it is much
174 simpler to handle those here rather than going through the same
175 logic in the C++ pretty-printer. */
176 if (p != NULL && (*p == '*' || *p == '&'))
177 pp_c_whitespace (pp);
178 pp_c_identifier (pp, cv);
181 /* Pretty-print T using the type-cast notation '( type-name )'. */
183 static void
184 pp_c_type_cast (c_pretty_printer *pp, tree t)
186 pp_c_left_paren (pp);
187 pp_type_id (pp, t);
188 pp_c_right_paren (pp);
191 /* We're about to pretty-print a pointer type as indicated by T.
192 Output a whitespace, if needed, preparing for subsequent output. */
194 void
195 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
197 if (POINTER_TYPE_P (t))
199 tree pointee = strip_pointer_operator (TREE_TYPE (t));
200 if (TREE_CODE (pointee) != ARRAY_TYPE
201 && TREE_CODE (pointee) != FUNCTION_TYPE)
202 pp_c_whitespace (pp);
207 /* Declarations. */
209 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
210 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
211 of its type. Take care of possible extensions.
213 type-qualifier-list:
214 type-qualifier
215 type-qualifier-list type-qualifier
217 type-qualifier:
218 const
219 restrict -- C99
220 __restrict__ -- GNU C
221 volatile */
223 void
224 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
226 int qualifiers;
228 if (!TYPE_P (t))
229 t = TREE_TYPE (t);
231 qualifiers = TYPE_QUALS (t);
232 if (qualifiers & TYPE_QUAL_CONST)
233 pp_c_cv_qualifier (pp, "const");
234 if (qualifiers & TYPE_QUAL_VOLATILE)
235 pp_c_cv_qualifier (pp, "volatile");
236 if (qualifiers & TYPE_QUAL_RESTRICT)
237 pp_c_cv_qualifier (pp, flag_isoc99 ? "restrict" : "__restrict__");
240 /* pointer:
241 * type-qualifier-list(opt)
242 * type-qualifier-list(opt) pointer */
244 static void
245 pp_c_pointer (c_pretty_printer *pp, tree t)
247 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
248 t = TREE_TYPE (t);
249 switch (TREE_CODE (t))
251 case POINTER_TYPE:
252 /* It is easier to handle C++ reference types here. */
253 case REFERENCE_TYPE:
254 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
255 pp_c_pointer (pp, TREE_TYPE (t));
256 if (TREE_CODE (t) == POINTER_TYPE)
257 pp_c_star (pp);
258 else
259 pp_c_ampersand (pp);
260 pp_c_type_qualifier_list (pp, t);
261 break;
263 /* ??? This node is now in GENERIC and so shouldn't be here. But
264 we'll fix that later. */
265 case DECL_EXPR:
266 pp_declaration (pp, DECL_EXPR_DECL (t));
267 pp_needs_newline (pp) = true;
268 break;
270 default:
271 pp_unsupported_tree (pp, t);
275 /* type-specifier:
276 void
277 char
278 short
280 long
281 float
282 double
283 signed
284 unsigned
285 _Bool -- C99
286 _Complex -- C99
287 _Imaginary -- C99
288 struct-or-union-specifier
289 enum-specifier
290 typedef-name.
292 GNU extensions.
293 simple-type-specifier:
294 __complex__
295 __vector__ */
297 void
298 pp_c_type_specifier (c_pretty_printer *pp, tree t)
300 const enum tree_code code = TREE_CODE (t);
301 switch (code)
303 case ERROR_MARK:
304 pp_c_identifier (pp, "<type-error>");
305 break;
307 case IDENTIFIER_NODE:
308 pp_c_tree_decl_identifier (pp, t);
309 break;
311 case VOID_TYPE:
312 case BOOLEAN_TYPE:
313 case INTEGER_TYPE:
314 case REAL_TYPE:
315 if (TYPE_NAME (t))
317 t = TYPE_NAME (t);
318 pp_c_type_specifier (pp, t);
320 else
322 int prec = TYPE_PRECISION (t);
323 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
324 if (TYPE_NAME (t))
326 pp_c_type_specifier (pp, t);
327 if (TYPE_PRECISION (t) != prec)
329 pp_string (pp, ":");
330 pp_decimal_int (pp, prec);
333 else
335 switch (code)
337 case INTEGER_TYPE:
338 pp_string (pp, (TYPE_UNSIGNED (t)
339 ? "<unnamed-unsigned:"
340 : "<unnamed-signed:"));
341 break;
342 case REAL_TYPE:
343 pp_string (pp, "<unnamed-float:");
344 break;
345 default:
346 gcc_unreachable ();
348 pp_decimal_int (pp, prec);
349 pp_string (pp, ">");
352 break;
354 case TYPE_DECL:
355 if (DECL_NAME (t))
356 pp_id_expression (pp, t);
357 else
358 pp_c_identifier (pp, "<typedef-error>");
359 break;
361 case UNION_TYPE:
362 case RECORD_TYPE:
363 case ENUMERAL_TYPE:
364 if (code == UNION_TYPE)
365 pp_c_identifier (pp, "union");
366 else if (code == RECORD_TYPE)
367 pp_c_identifier (pp, "struct");
368 else if (code == ENUMERAL_TYPE)
369 pp_c_identifier (pp, "enum");
370 else
371 pp_c_identifier (pp, "<tag-error>");
373 if (TYPE_NAME (t))
374 pp_id_expression (pp, TYPE_NAME (t));
375 else
376 pp_c_identifier (pp, "<anonymous>");
377 break;
379 default:
380 pp_unsupported_tree (pp, t);
381 break;
385 /* specifier-qualifier-list:
386 type-specifier specifier-qualifier-list-opt
387 type-qualifier specifier-qualifier-list-opt
390 Implementation note: Because of the non-linearities in array or
391 function declarations, this routine prints not just the
392 specifier-qualifier-list of such entities or types of such entities,
393 but also the 'pointer' production part of their declarators. The
394 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
396 void
397 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
399 const enum tree_code code = TREE_CODE (t);
401 if (TREE_CODE (t) != POINTER_TYPE)
402 pp_c_type_qualifier_list (pp, t);
403 switch (code)
405 case REFERENCE_TYPE:
406 case POINTER_TYPE:
408 /* Get the types-specifier of this type. */
409 tree pointee = strip_pointer_operator (TREE_TYPE (t));
410 pp_c_specifier_qualifier_list (pp, pointee);
411 if (TREE_CODE (pointee) == ARRAY_TYPE
412 || TREE_CODE (pointee) == FUNCTION_TYPE)
414 pp_c_whitespace (pp);
415 pp_c_left_paren (pp);
417 else if (!c_dialect_cxx ())
418 pp_c_whitespace (pp);
419 pp_ptr_operator (pp, t);
421 break;
423 case FUNCTION_TYPE:
424 case ARRAY_TYPE:
425 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
426 break;
428 case VECTOR_TYPE:
429 case COMPLEX_TYPE:
430 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
431 if (code == COMPLEX_TYPE)
432 pp_c_identifier (pp, flag_isoc99 ? "_Complex" : "__complex__");
433 else if (code == VECTOR_TYPE)
434 pp_c_identifier (pp, "__vector__");
435 break;
437 default:
438 pp_simple_type_specifier (pp, t);
439 break;
443 /* parameter-type-list:
444 parameter-list
445 parameter-list , ...
447 parameter-list:
448 parameter-declaration
449 parameter-list , parameter-declaration
451 parameter-declaration:
452 declaration-specifiers declarator
453 declaration-specifiers abstract-declarator(opt) */
455 void
456 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
458 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
459 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
460 pp_c_left_paren (pp);
461 if (parms == void_list_node)
462 pp_c_identifier (pp, "void");
463 else
465 bool first = true;
466 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
468 if (!first)
469 pp_separate_with (pp, ',');
470 first = false;
471 pp_declaration_specifiers
472 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
473 if (want_parm_decl)
474 pp_declarator (pp, parms);
475 else
476 pp_abstract_declarator (pp, TREE_VALUE (parms));
479 pp_c_right_paren (pp);
482 /* abstract-declarator:
483 pointer
484 pointer(opt) direct-abstract-declarator */
486 static void
487 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
489 if (TREE_CODE (t) == POINTER_TYPE)
491 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
492 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
493 pp_c_right_paren (pp);
494 t = TREE_TYPE (t);
497 pp_direct_abstract_declarator (pp, t);
500 /* direct-abstract-declarator:
501 ( abstract-declarator )
502 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
503 direct-abstract-declarator(opt) [ * ]
504 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
506 void
507 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
509 switch (TREE_CODE (t))
511 case POINTER_TYPE:
512 pp_abstract_declarator (pp, t);
513 break;
515 case FUNCTION_TYPE:
516 pp_c_parameter_type_list (pp, t);
517 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
518 break;
520 case ARRAY_TYPE:
521 pp_c_left_bracket (pp);
522 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
524 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
525 tree type = TREE_TYPE (maxval);
527 if (host_integerp (maxval, 0))
528 pp_wide_integer (pp, tree_low_cst (maxval, 0) + 1);
529 else
530 pp_expression (pp, fold (build2 (PLUS_EXPR, type, maxval,
531 build_int_cst (type, 1))));
533 pp_c_right_bracket (pp);
534 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
535 break;
537 case IDENTIFIER_NODE:
538 case VOID_TYPE:
539 case BOOLEAN_TYPE:
540 case INTEGER_TYPE:
541 case REAL_TYPE:
542 case ENUMERAL_TYPE:
543 case RECORD_TYPE:
544 case UNION_TYPE:
545 case VECTOR_TYPE:
546 case COMPLEX_TYPE:
547 case TYPE_DECL:
548 break;
550 default:
551 pp_unsupported_tree (pp, t);
552 break;
556 /* type-name:
557 specifier-qualifier-list abstract-declarator(opt) */
559 void
560 pp_c_type_id (c_pretty_printer *pp, tree t)
562 pp_c_specifier_qualifier_list (pp, t);
563 pp_abstract_declarator (pp, t);
566 /* storage-class-specifier:
567 typedef
568 extern
569 static
570 auto
571 register */
573 void
574 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
576 if (TREE_CODE (t) == TYPE_DECL)
577 pp_c_identifier (pp, "typedef");
578 else if (DECL_P (t))
580 if (DECL_REGISTER (t))
581 pp_c_identifier (pp, "register");
582 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
583 pp_c_identifier (pp, "static");
587 /* function-specifier:
588 inline */
590 void
591 pp_c_function_specifier (c_pretty_printer *pp, tree t)
593 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
594 pp_c_identifier (pp, "inline");
597 /* declaration-specifiers:
598 storage-class-specifier declaration-specifiers(opt)
599 type-specifier declaration-specifiers(opt)
600 type-qualifier declaration-specifiers(opt)
601 function-specifier declaration-specifiers(opt) */
603 void
604 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
606 pp_storage_class_specifier (pp, t);
607 pp_function_specifier (pp, t);
608 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
611 /* direct-declarator
612 identifier
613 ( declarator )
614 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
615 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
616 direct-declarator [ type-qualifier-list static assignment-expression ]
617 direct-declarator [ type-qualifier-list * ]
618 direct-declarator ( parameter-type-list )
619 direct-declarator ( identifier-list(opt) ) */
621 void
622 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
624 switch (TREE_CODE (t))
626 case VAR_DECL:
627 case PARM_DECL:
628 case TYPE_DECL:
629 case FIELD_DECL:
630 case LABEL_DECL:
631 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
632 pp_c_tree_decl_identifier (pp, t);
633 break;
635 case ARRAY_TYPE:
636 case POINTER_TYPE:
637 pp_abstract_declarator (pp, TREE_TYPE (t));
638 break;
640 case FUNCTION_TYPE:
641 pp_parameter_list (pp, t);
642 pp_abstract_declarator (pp, TREE_TYPE (t));
643 break;
645 case FUNCTION_DECL:
646 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
647 pp_c_tree_decl_identifier (pp, t);
648 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
649 pp_abstract_declarator (pp, TREE_TYPE (t));
650 else
652 pp_parameter_list (pp, t);
653 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
655 break;
657 case INTEGER_TYPE:
658 case REAL_TYPE:
659 case ENUMERAL_TYPE:
660 case UNION_TYPE:
661 case RECORD_TYPE:
662 break;
664 default:
665 pp_unsupported_tree (pp, t);
666 break;
671 /* declarator:
672 pointer(opt) direct-declarator */
674 void
675 pp_c_declarator (c_pretty_printer *pp, tree t)
677 switch (TREE_CODE (t))
679 case INTEGER_TYPE:
680 case REAL_TYPE:
681 case ENUMERAL_TYPE:
682 case UNION_TYPE:
683 case RECORD_TYPE:
684 break;
686 case VAR_DECL:
687 case PARM_DECL:
688 case FIELD_DECL:
689 case ARRAY_TYPE:
690 case FUNCTION_TYPE:
691 case FUNCTION_DECL:
692 case TYPE_DECL:
693 pp_direct_declarator (pp, t);
694 break;
697 default:
698 pp_unsupported_tree (pp, t);
699 break;
703 /* declaration:
704 declaration-specifiers init-declarator-list(opt) ; */
706 void
707 pp_c_declaration (c_pretty_printer *pp, tree t)
709 pp_declaration_specifiers (pp, t);
710 pp_c_init_declarator (pp, t);
713 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
715 void
716 pp_c_attributes (c_pretty_printer *pp, tree attributes)
718 if (attributes == NULL_TREE)
719 return;
721 pp_c_identifier (pp, "__attribute__");
722 pp_c_left_paren (pp);
723 pp_c_left_paren (pp);
724 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
726 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
727 if (TREE_VALUE (attributes))
728 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
730 if (TREE_CHAIN (attributes))
731 pp_separate_with (pp, ',');
733 pp_c_right_paren (pp);
734 pp_c_right_paren (pp);
737 /* function-definition:
738 declaration-specifiers declarator compound-statement */
740 void
741 pp_c_function_definition (c_pretty_printer *pp, tree t)
743 pp_declaration_specifiers (pp, t);
744 pp_declarator (pp, t);
745 pp_needs_newline (pp) = true;
746 pp_statement (pp, DECL_SAVED_TREE (t));
747 pp_newline (pp);
748 pp_flush (pp);
752 /* Expressions. */
754 /* Print out a c-char. This is called solely for characters which are
755 in the *target* execution character set. We ought to convert them
756 back to the *host* execution character set before printing, but we
757 have no way to do this at present. A decent compromise is to print
758 all characters as if they were in the host execution character set,
759 and not attempt to recover any named escape characters, but render
760 all unprintables as octal escapes. If the host and target character
761 sets are the same, this produces relatively readable output. If they
762 are not the same, strings may appear as gibberish, but that's okay
763 (in fact, it may well be what the reader wants, e.g. if they are looking
764 to see if conversion to the target character set happened correctly).
766 A special case: we need to prefix \, ", and ' with backslashes. It is
767 correct to do so for the *host*'s \, ", and ', because the rest of the
768 file appears in the host character set. */
770 static void
771 pp_c_char (c_pretty_printer *pp, int c)
773 if (ISPRINT (c))
775 switch (c)
777 case '\\': pp_string (pp, "\\\\"); break;
778 case '\'': pp_string (pp, "\\\'"); break;
779 case '\"': pp_string (pp, "\\\""); break;
780 default: pp_character (pp, c);
783 else
784 pp_scalar (pp, "\\%03o", (unsigned) c);
787 /* Print out a STRING literal. */
789 void
790 pp_c_string_literal (c_pretty_printer *pp, tree s)
792 const char *p = TREE_STRING_POINTER (s);
793 int n = TREE_STRING_LENGTH (s) - 1;
794 int i;
795 pp_doublequote (pp);
796 for (i = 0; i < n; ++i)
797 pp_c_char (pp, p[i]);
798 pp_doublequote (pp);
801 /* Pretty-print an INTEGER literal. */
803 static void
804 pp_c_integer_constant (c_pretty_printer *pp, tree i)
806 tree type = TREE_TYPE (i);
808 if (TREE_INT_CST_HIGH (i) == 0)
809 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
810 else
812 if (tree_int_cst_sgn (i) < 0)
814 pp_character (pp, '-');
815 i = build_int_cst_wide (NULL_TREE,
816 -TREE_INT_CST_LOW (i),
817 ~TREE_INT_CST_HIGH (i)
818 + !TREE_INT_CST_LOW (i));
820 sprintf (pp_buffer (pp)->digit_buffer,
821 HOST_WIDE_INT_PRINT_DOUBLE_HEX,
822 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
823 pp_string (pp, pp_buffer (pp)->digit_buffer);
825 if (TYPE_UNSIGNED (type))
826 pp_character (pp, 'u');
827 if (type == long_integer_type_node || type == long_unsigned_type_node)
828 pp_character (pp, 'l');
829 else if (type == long_long_integer_type_node
830 || type == long_long_unsigned_type_node)
831 pp_string (pp, "ll");
834 /* Print out a CHARACTER literal. */
836 static void
837 pp_c_character_constant (c_pretty_printer *pp, tree c)
839 tree type = TREE_TYPE (c);
840 if (type == wchar_type_node)
841 pp_character (pp, 'L');
842 pp_quote (pp);
843 if (host_integerp (c, TYPE_UNSIGNED (type)))
844 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
845 else
846 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
847 pp_quote (pp);
850 /* Print out a BOOLEAN literal. */
852 static void
853 pp_c_bool_constant (c_pretty_printer *pp, tree b)
855 if (b == boolean_false_node)
857 if (c_dialect_cxx ())
858 pp_c_identifier (pp, "false");
859 else if (flag_isoc99)
860 pp_c_identifier (pp, "_False");
861 else
862 pp_unsupported_tree (pp, b);
864 else if (b == boolean_true_node)
866 if (c_dialect_cxx ())
867 pp_c_identifier (pp, "true");
868 else if (flag_isoc99)
869 pp_c_identifier (pp, "_True");
870 else
871 pp_unsupported_tree (pp, b);
873 else if (TREE_CODE (b) == INTEGER_CST)
874 pp_c_integer_constant (pp, b);
875 else
876 pp_unsupported_tree (pp, b);
879 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
880 false; that means the value was obtained by a cast, in which case
881 print out the type-id part of the cast-expression -- the casted value
882 is then printed by pp_c_integer_literal. */
884 static bool
885 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
887 bool value_is_named = true;
888 tree type = TREE_TYPE (e);
889 tree value;
891 /* Find the name of this constant. */
892 for (value = TYPE_VALUES (type);
893 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
894 value = TREE_CHAIN (value))
897 if (value != NULL_TREE)
898 pp_id_expression (pp, TREE_PURPOSE (value));
899 else
901 /* Value must have been cast. */
902 pp_c_type_cast (pp, type);
903 value_is_named = false;
906 return value_is_named;
909 /* Print out a REAL value as a decimal-floating-constant. */
911 static void
912 pp_c_floating_constant (c_pretty_printer *pp, tree r)
914 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
915 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
916 pp_string (pp, pp_buffer(pp)->digit_buffer);
917 if (TREE_TYPE (r) == float_type_node)
918 pp_character (pp, 'f');
919 else if (TREE_TYPE (r) == long_double_type_node)
920 pp_character (pp, 'l');
921 else if (TREE_TYPE (r) == dfloat128_type_node)
922 pp_string (pp, "dl");
923 else if (TREE_TYPE (r) == dfloat64_type_node)
924 pp_string (pp, "dd");
925 else if (TREE_TYPE (r) == dfloat32_type_node)
926 pp_string (pp, "df");
929 /* Pretty-print a compound literal expression. GNU extensions include
930 vector constants. */
932 static void
933 pp_c_compound_literal (c_pretty_printer *pp, tree e)
935 tree type = TREE_TYPE (e);
936 pp_c_type_cast (pp, type);
938 switch (TREE_CODE (type))
940 case RECORD_TYPE:
941 case UNION_TYPE:
942 case ARRAY_TYPE:
943 case VECTOR_TYPE:
944 case COMPLEX_TYPE:
945 pp_c_brace_enclosed_initializer_list (pp, e);
946 break;
948 default:
949 pp_unsupported_tree (pp, e);
950 break;
954 /* constant:
955 integer-constant
956 floating-constant
957 enumeration-constant
958 character-constant */
960 void
961 pp_c_constant (c_pretty_printer *pp, tree e)
963 const enum tree_code code = TREE_CODE (e);
965 switch (code)
967 case INTEGER_CST:
969 tree type = TREE_TYPE (e);
970 if (type == boolean_type_node)
971 pp_c_bool_constant (pp, e);
972 else if (type == char_type_node)
973 pp_c_character_constant (pp, e);
974 else if (TREE_CODE (type) == ENUMERAL_TYPE
975 && pp_c_enumeration_constant (pp, e))
977 else
978 pp_c_integer_constant (pp, e);
980 break;
982 case REAL_CST:
983 pp_c_floating_constant (pp, e);
984 break;
986 case STRING_CST:
987 pp_c_string_literal (pp, e);
988 break;
990 default:
991 pp_unsupported_tree (pp, e);
992 break;
996 /* Pretty-print an IDENTIFIER_NODE, preceded by whitespace is necessary. */
998 void
999 pp_c_identifier (c_pretty_printer *pp, const char *id)
1001 pp_c_maybe_whitespace (pp);
1002 pp_identifier (pp, id);
1003 pp_base (pp)->padding = pp_before;
1006 /* Pretty-print a C primary-expression.
1007 primary-expression:
1008 identifier
1009 constant
1010 string-literal
1011 ( expression ) */
1013 void
1014 pp_c_primary_expression (c_pretty_printer *pp, tree e)
1016 switch (TREE_CODE (e))
1018 case VAR_DECL:
1019 case PARM_DECL:
1020 case FIELD_DECL:
1021 case CONST_DECL:
1022 case FUNCTION_DECL:
1023 case LABEL_DECL:
1024 pp_c_tree_decl_identifier (pp, e);
1025 break;
1027 case IDENTIFIER_NODE:
1028 pp_c_tree_identifier (pp, e);
1029 break;
1031 case ERROR_MARK:
1032 pp_c_identifier (pp, "<erroneous-expression>");
1033 break;
1035 case RESULT_DECL:
1036 pp_c_identifier (pp, "<return-value>");
1037 break;
1039 case INTEGER_CST:
1040 case REAL_CST:
1041 case STRING_CST:
1042 pp_c_constant (pp, e);
1043 break;
1045 case TARGET_EXPR:
1046 pp_c_identifier (pp, "__builtin_memcpy");
1047 pp_c_left_paren (pp);
1048 pp_ampersand (pp);
1049 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1050 pp_separate_with (pp, ',');
1051 pp_ampersand (pp);
1052 pp_initializer (pp, TREE_OPERAND (e, 1));
1053 if (TREE_OPERAND (e, 2))
1055 pp_separate_with (pp, ',');
1056 pp_c_expression (pp, TREE_OPERAND (e, 2));
1058 pp_c_right_paren (pp);
1059 break;
1061 default:
1062 /* FIXME: Make sure we won't get into an infinie loop. */
1063 pp_c_left_paren (pp);
1064 pp_expression (pp, e);
1065 pp_c_right_paren (pp);
1066 break;
1070 /* Print out a C initializer -- also support C compound-literals.
1071 initializer:
1072 assignment-expression:
1073 { initializer-list }
1074 { initializer-list , } */
1076 static void
1077 pp_c_initializer (c_pretty_printer *pp, tree e)
1079 if (TREE_CODE (e) == CONSTRUCTOR)
1080 pp_c_brace_enclosed_initializer_list (pp, e);
1081 else
1082 pp_expression (pp, e);
1085 /* init-declarator:
1086 declarator:
1087 declarator = initializer */
1089 void
1090 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1092 pp_declarator (pp, t);
1093 /* We don't want to output function definitions here. There are handled
1094 elsewhere (and the syntactic form is bogus anyway). */
1095 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1097 tree init = DECL_INITIAL (t);
1098 /* This C++ bit is handled here because it is easier to do so.
1099 In templates, the C++ parser builds a TREE_LIST for a
1100 direct-initialization; the TREE_PURPOSE is the variable to
1101 initialize and the TREE_VALUE is the initializer. */
1102 if (TREE_CODE (init) == TREE_LIST)
1104 pp_c_left_paren (pp);
1105 pp_expression (pp, TREE_VALUE (init));
1106 pp_right_paren (pp);
1108 else
1110 pp_space (pp);
1111 pp_equal (pp);
1112 pp_space (pp);
1113 pp_c_initializer (pp, init);
1118 /* initializer-list:
1119 designation(opt) initializer
1120 initializer-list , designation(opt) initializer
1122 designation:
1123 designator-list =
1125 designator-list:
1126 designator
1127 designator-list designator
1129 designator:
1130 [ constant-expression ]
1131 identifier */
1133 static void
1134 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1136 tree type = TREE_TYPE (e);
1137 const enum tree_code code = TREE_CODE (type);
1139 switch (code)
1141 case RECORD_TYPE:
1142 case UNION_TYPE:
1143 case ARRAY_TYPE:
1145 tree init = TREE_OPERAND (e, 0);
1146 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1148 if (code == RECORD_TYPE || code == UNION_TYPE)
1150 pp_c_dot (pp);
1151 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1153 else
1155 pp_c_left_bracket (pp);
1156 if (TREE_PURPOSE (init))
1157 pp_c_constant (pp, TREE_PURPOSE (init));
1158 pp_c_right_bracket (pp);
1160 pp_c_whitespace (pp);
1161 pp_equal (pp);
1162 pp_c_whitespace (pp);
1163 pp_initializer (pp, TREE_VALUE (init));
1164 if (TREE_CHAIN (init))
1165 pp_separate_with (pp, ',');
1168 return;
1170 case VECTOR_TYPE:
1171 if (TREE_CODE (e) == VECTOR_CST)
1172 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1173 else if (TREE_CODE (e) == CONSTRUCTOR)
1174 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1175 else
1176 break;
1177 return;
1179 case COMPLEX_TYPE:
1180 if (TREE_CODE (e) == CONSTRUCTOR)
1181 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1182 else if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1184 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1185 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1186 pp_separate_with (pp, ',');
1187 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1189 else
1190 break;
1191 return;
1193 default:
1194 break;
1197 pp_unsupported_tree (pp, type);
1200 /* Pretty-print a brace-enclosed initializer-list. */
1202 static void
1203 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1205 pp_c_left_brace (pp);
1206 pp_c_initializer_list (pp, l);
1207 pp_c_right_brace (pp);
1211 /* This is a convenient function, used to bridge gap between C and C++
1212 grammars.
1214 id-expression:
1215 identifier */
1217 void
1218 pp_c_id_expression (c_pretty_printer *pp, tree t)
1220 switch (TREE_CODE (t))
1222 case VAR_DECL:
1223 case PARM_DECL:
1224 case CONST_DECL:
1225 case TYPE_DECL:
1226 case FUNCTION_DECL:
1227 case FIELD_DECL:
1228 case LABEL_DECL:
1229 pp_c_tree_decl_identifier (pp, t);
1230 break;
1232 case IDENTIFIER_NODE:
1233 pp_c_tree_identifier (pp, t);
1234 break;
1236 default:
1237 pp_unsupported_tree (pp, t);
1238 break;
1242 /* postfix-expression:
1243 primary-expression
1244 postfix-expression [ expression ]
1245 postfix-expression ( argument-expression-list(opt) )
1246 postfix-expression . identifier
1247 postfix-expression -> identifier
1248 postfix-expression ++
1249 postfix-expression --
1250 ( type-name ) { initializer-list }
1251 ( type-name ) { initializer-list , } */
1253 void
1254 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1256 enum tree_code code = TREE_CODE (e);
1257 switch (code)
1259 case POSTINCREMENT_EXPR:
1260 case POSTDECREMENT_EXPR:
1261 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1262 pp_identifier (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1263 break;
1265 case ARRAY_REF:
1266 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1267 pp_c_left_bracket (pp);
1268 pp_expression (pp, TREE_OPERAND (e, 1));
1269 pp_c_right_bracket (pp);
1270 break;
1272 case CALL_EXPR:
1273 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1274 pp_c_call_argument_list (pp, TREE_OPERAND (e, 1));
1275 break;
1277 case UNORDERED_EXPR:
1278 pp_c_identifier (pp, flag_isoc99
1279 ? "isunordered"
1280 : "__builtin_isunordered");
1281 goto two_args_fun;
1283 case ORDERED_EXPR:
1284 pp_c_identifier (pp, flag_isoc99
1285 ? "!isunordered"
1286 : "!__builtin_isunordered");
1287 goto two_args_fun;
1289 case UNLT_EXPR:
1290 pp_c_identifier (pp, flag_isoc99
1291 ? "!isgreaterequal"
1292 : "!__builtin_isgreaterequal");
1293 goto two_args_fun;
1295 case UNLE_EXPR:
1296 pp_c_identifier (pp, flag_isoc99
1297 ? "!isgreater"
1298 : "!__builtin_isgreater");
1299 goto two_args_fun;
1301 case UNGT_EXPR:
1302 pp_c_identifier (pp, flag_isoc99
1303 ? "!islessequal"
1304 : "!__builtin_islessequal");
1305 goto two_args_fun;
1307 case UNGE_EXPR:
1308 pp_c_identifier (pp, flag_isoc99
1309 ? "!isless"
1310 : "!__builtin_isless");
1311 goto two_args_fun;
1313 case UNEQ_EXPR:
1314 pp_c_identifier (pp, flag_isoc99
1315 ? "!islessgreater"
1316 : "!__builtin_islessgreater");
1317 goto two_args_fun;
1319 case LTGT_EXPR:
1320 pp_c_identifier (pp, flag_isoc99
1321 ? "islessgreater"
1322 : "__builtin_islessgreater");
1323 goto two_args_fun;
1325 two_args_fun:
1326 pp_c_left_paren (pp);
1327 pp_expression (pp, TREE_OPERAND (e, 0));
1328 pp_separate_with (pp, ',');
1329 pp_expression (pp, TREE_OPERAND (e, 1));
1330 pp_c_right_paren (pp);
1331 break;
1333 case ABS_EXPR:
1334 pp_c_identifier (pp, "__builtin_abs");
1335 pp_c_left_paren (pp);
1336 pp_expression (pp, TREE_OPERAND (e, 0));
1337 pp_c_right_paren (pp);
1338 break;
1340 case COMPONENT_REF:
1342 tree object = TREE_OPERAND (e, 0);
1343 if (TREE_CODE (object) == INDIRECT_REF)
1345 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1346 pp_c_arrow (pp);
1348 else
1350 pp_postfix_expression (pp, object);
1351 pp_c_dot (pp);
1353 pp_expression (pp, TREE_OPERAND (e, 1));
1355 break;
1357 case COMPLEX_CST:
1358 case VECTOR_CST:
1359 case COMPLEX_EXPR:
1360 pp_c_compound_literal (pp, e);
1361 break;
1363 case COMPOUND_LITERAL_EXPR:
1364 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1365 /* Fall through. */
1366 case CONSTRUCTOR:
1367 pp_initializer (pp, e);
1368 break;
1370 case VA_ARG_EXPR:
1371 pp_c_identifier (pp, "__builtin_va_arg");
1372 pp_c_left_paren (pp);
1373 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1374 pp_separate_with (pp, ',');
1375 pp_type_id (pp, TREE_TYPE (e));
1376 pp_c_right_paren (pp);
1377 break;
1379 case ADDR_EXPR:
1380 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1382 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1383 break;
1385 /* else fall through. */
1387 default:
1388 pp_primary_expression (pp, e);
1389 break;
1393 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1395 void
1396 pp_c_expression_list (c_pretty_printer *pp, tree e)
1398 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1400 pp_expression (pp, TREE_VALUE (e));
1401 if (TREE_CHAIN (e))
1402 pp_separate_with (pp, ',');
1406 /* Print out V, which contains the elements of a constructor. */
1408 void
1409 pp_c_constructor_elts (c_pretty_printer *pp, VEC(constructor_elt,gc) *v)
1411 unsigned HOST_WIDE_INT ix;
1412 tree value;
1414 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1416 pp_expression (pp, value);
1417 if (ix != VEC_length (constructor_elt, v) - 1)
1418 pp_separate_with (pp, ',');
1422 /* Print out an expression-list in parens, as in a function call. */
1424 void
1425 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1427 pp_c_left_paren (pp);
1428 if (t && TREE_CODE (t) == TREE_LIST)
1429 pp_c_expression_list (pp, t);
1430 pp_c_right_paren (pp);
1433 /* unary-expression:
1434 postfix-expression
1435 ++ cast-expression
1436 -- cast-expression
1437 unary-operator cast-expression
1438 sizeof unary-expression
1439 sizeof ( type-id )
1441 unary-operator: one of
1442 * & + - ! ~
1444 GNU extensions.
1445 unary-expression:
1446 __alignof__ unary-expression
1447 __alignof__ ( type-id )
1448 __real__ unary-expression
1449 __imag__ unary-expression */
1451 void
1452 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1454 enum tree_code code = TREE_CODE (e);
1455 switch (code)
1457 case PREINCREMENT_EXPR:
1458 case PREDECREMENT_EXPR:
1459 pp_identifier (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1460 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1461 break;
1463 case ADDR_EXPR:
1464 case INDIRECT_REF:
1465 case NEGATE_EXPR:
1466 case BIT_NOT_EXPR:
1467 case TRUTH_NOT_EXPR:
1468 case CONJ_EXPR:
1469 /* String literal are used by address. */
1470 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1471 pp_ampersand (pp);
1472 else if (code == INDIRECT_REF)
1473 pp_c_star (pp);
1474 else if (code == NEGATE_EXPR)
1475 pp_minus (pp);
1476 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1477 pp_complement (pp);
1478 else if (code == TRUTH_NOT_EXPR)
1479 pp_exclamation (pp);
1480 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1481 break;
1483 case REALPART_EXPR:
1484 case IMAGPART_EXPR:
1485 pp_c_identifier (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1486 pp_c_whitespace (pp);
1487 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1488 break;
1490 default:
1491 pp_postfix_expression (pp, e);
1492 break;
1496 /* cast-expression:
1497 unary-expression
1498 ( type-name ) cast-expression */
1500 void
1501 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1503 switch (TREE_CODE (e))
1505 case FLOAT_EXPR:
1506 case FIX_TRUNC_EXPR:
1507 case CONVERT_EXPR:
1508 case NOP_EXPR:
1509 pp_c_type_cast (pp, TREE_TYPE (e));
1510 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1511 break;
1513 default:
1514 pp_unary_expression (pp, e);
1518 /* multiplicative-expression:
1519 cast-expression
1520 multiplicative-expression * cast-expression
1521 multiplicative-expression / cast-expression
1522 multiplicative-expression % cast-expression */
1524 static void
1525 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1527 enum tree_code code = TREE_CODE (e);
1528 switch (code)
1530 case MULT_EXPR:
1531 case TRUNC_DIV_EXPR:
1532 case TRUNC_MOD_EXPR:
1533 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1534 pp_c_whitespace (pp);
1535 if (code == MULT_EXPR)
1536 pp_c_star (pp);
1537 else if (code == TRUNC_DIV_EXPR)
1538 pp_slash (pp);
1539 else
1540 pp_modulo (pp);
1541 pp_c_whitespace (pp);
1542 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1543 break;
1545 default:
1546 pp_c_cast_expression (pp, e);
1547 break;
1551 /* additive-expression:
1552 multiplicative-expression
1553 additive-expression + multiplicative-expression
1554 additive-expression - multiplicative-expression */
1556 static void
1557 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1559 enum tree_code code = TREE_CODE (e);
1560 switch (code)
1562 case PLUS_EXPR:
1563 case MINUS_EXPR:
1564 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1565 pp_c_whitespace (pp);
1566 if (code == PLUS_EXPR)
1567 pp_plus (pp);
1568 else
1569 pp_minus (pp);
1570 pp_c_whitespace (pp);
1571 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1572 break;
1574 default:
1575 pp_multiplicative_expression (pp, e);
1576 break;
1580 /* additive-expression:
1581 additive-expression
1582 shift-expression << additive-expression
1583 shift-expression >> additive-expression */
1585 static void
1586 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1588 enum tree_code code = TREE_CODE (e);
1589 switch (code)
1591 case LSHIFT_EXPR:
1592 case RSHIFT_EXPR:
1593 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1594 pp_c_whitespace (pp);
1595 pp_identifier (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1596 pp_c_whitespace (pp);
1597 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1598 break;
1600 default:
1601 pp_c_additive_expression (pp, e);
1605 /* relational-expression:
1606 shift-expression
1607 relational-expression < shift-expression
1608 relational-expression > shift-expression
1609 relational-expression <= shift-expression
1610 relational-expression >= shift-expression */
1612 static void
1613 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1615 enum tree_code code = TREE_CODE (e);
1616 switch (code)
1618 case LT_EXPR:
1619 case GT_EXPR:
1620 case LE_EXPR:
1621 case GE_EXPR:
1622 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1623 pp_c_whitespace (pp);
1624 if (code == LT_EXPR)
1625 pp_less (pp);
1626 else if (code == GT_EXPR)
1627 pp_greater (pp);
1628 else if (code == LE_EXPR)
1629 pp_identifier (pp, "<=");
1630 else if (code == GE_EXPR)
1631 pp_identifier (pp, ">=");
1632 pp_c_whitespace (pp);
1633 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1634 break;
1636 default:
1637 pp_c_shift_expression (pp, e);
1638 break;
1642 /* equality-expression:
1643 relational-expression
1644 equality-expression == relational-expression
1645 equality-equality != relational-expression */
1647 static void
1648 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1650 enum tree_code code = TREE_CODE (e);
1651 switch (code)
1653 case EQ_EXPR:
1654 case NE_EXPR:
1655 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1656 pp_c_whitespace (pp);
1657 pp_identifier (pp, code == EQ_EXPR ? "==" : "!=");
1658 pp_c_whitespace (pp);
1659 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1660 break;
1662 default:
1663 pp_c_relational_expression (pp, e);
1664 break;
1668 /* AND-expression:
1669 equality-expression
1670 AND-expression & equality-equality */
1672 static void
1673 pp_c_and_expression (c_pretty_printer *pp, tree e)
1675 if (TREE_CODE (e) == BIT_AND_EXPR)
1677 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1678 pp_c_whitespace (pp);
1679 pp_ampersand (pp);
1680 pp_c_whitespace (pp);
1681 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1683 else
1684 pp_c_equality_expression (pp, e);
1687 /* exclusive-OR-expression:
1688 AND-expression
1689 exclusive-OR-expression ^ AND-expression */
1691 static void
1692 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1694 if (TREE_CODE (e) == BIT_XOR_EXPR)
1696 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1697 pp_c_maybe_whitespace (pp);
1698 pp_carret (pp);
1699 pp_c_whitespace (pp);
1700 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1702 else
1703 pp_c_and_expression (pp, e);
1706 /* inclusive-OR-expression:
1707 exclusive-OR-expression
1708 inclusive-OR-expression | exclusive-OR-expression */
1710 static void
1711 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1713 if (TREE_CODE (e) == BIT_IOR_EXPR)
1715 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1716 pp_c_whitespace (pp);
1717 pp_bar (pp);
1718 pp_c_whitespace (pp);
1719 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1721 else
1722 pp_c_exclusive_or_expression (pp, e);
1725 /* logical-AND-expression:
1726 inclusive-OR-expression
1727 logical-AND-expression && inclusive-OR-expression */
1729 static void
1730 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1732 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
1734 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1735 pp_c_whitespace (pp);
1736 pp_identifier (pp, "&&");
1737 pp_c_whitespace (pp);
1738 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1740 else
1741 pp_c_inclusive_or_expression (pp, e);
1744 /* logical-OR-expression:
1745 logical-AND-expression
1746 logical-OR-expression || logical-AND-expression */
1748 void
1749 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1751 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1753 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1754 pp_c_whitespace (pp);
1755 pp_identifier (pp, "||");
1756 pp_c_whitespace (pp);
1757 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1759 else
1760 pp_c_logical_and_expression (pp, e);
1763 /* conditional-expression:
1764 logical-OR-expression
1765 logical-OR-expression ? expression : conditional-expression */
1767 static void
1768 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1770 if (TREE_CODE (e) == COND_EXPR)
1772 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1773 pp_c_whitespace (pp);
1774 pp_question (pp);
1775 pp_c_whitespace (pp);
1776 pp_expression (pp, TREE_OPERAND (e, 1));
1777 pp_c_whitespace (pp);
1778 pp_colon (pp);
1779 pp_c_whitespace (pp);
1780 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1782 else
1783 pp_c_logical_or_expression (pp, e);
1787 /* assignment-expression:
1788 conditional-expression
1789 unary-expression assignment-operator assignment-expression
1791 assignment-expression: one of
1792 = *= /= %= += -= >>= <<= &= ^= |= */
1794 static void
1795 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1797 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1799 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1800 pp_c_whitespace (pp);
1801 pp_equal (pp);
1802 pp_space (pp);
1803 pp_c_expression (pp, TREE_OPERAND (e, 1));
1805 else
1806 pp_c_conditional_expression (pp, e);
1809 /* expression:
1810 assignment-expression
1811 expression , assignment-expression
1813 Implementation note: instead of going through the usual recursion
1814 chain, I take the liberty of dispatching nodes to the appropriate
1815 functions. This makes some redundancy, but it worths it. That also
1816 prevents a possible infinite recursion between pp_c_primary_expression ()
1817 and pp_c_expression (). */
1819 void
1820 pp_c_expression (c_pretty_printer *pp, tree e)
1822 switch (TREE_CODE (e))
1824 case INTEGER_CST:
1825 pp_c_integer_constant (pp, e);
1826 break;
1828 case REAL_CST:
1829 pp_c_floating_constant (pp, e);
1830 break;
1832 case STRING_CST:
1833 pp_c_string_literal (pp, e);
1834 break;
1836 case IDENTIFIER_NODE:
1837 case FUNCTION_DECL:
1838 case VAR_DECL:
1839 case CONST_DECL:
1840 case PARM_DECL:
1841 case RESULT_DECL:
1842 case FIELD_DECL:
1843 case LABEL_DECL:
1844 case ERROR_MARK:
1845 pp_primary_expression (pp, e);
1846 break;
1848 case POSTINCREMENT_EXPR:
1849 case POSTDECREMENT_EXPR:
1850 case ARRAY_REF:
1851 case CALL_EXPR:
1852 case COMPONENT_REF:
1853 case COMPLEX_CST:
1854 case COMPLEX_EXPR:
1855 case VECTOR_CST:
1856 case ORDERED_EXPR:
1857 case UNORDERED_EXPR:
1858 case LTGT_EXPR:
1859 case UNEQ_EXPR:
1860 case UNLE_EXPR:
1861 case UNLT_EXPR:
1862 case UNGE_EXPR:
1863 case UNGT_EXPR:
1864 case ABS_EXPR:
1865 case CONSTRUCTOR:
1866 case COMPOUND_LITERAL_EXPR:
1867 case VA_ARG_EXPR:
1868 pp_postfix_expression (pp, e);
1869 break;
1871 case CONJ_EXPR:
1872 case ADDR_EXPR:
1873 case INDIRECT_REF:
1874 case NEGATE_EXPR:
1875 case BIT_NOT_EXPR:
1876 case TRUTH_NOT_EXPR:
1877 case PREINCREMENT_EXPR:
1878 case PREDECREMENT_EXPR:
1879 case REALPART_EXPR:
1880 case IMAGPART_EXPR:
1881 pp_c_unary_expression (pp, e);
1882 break;
1884 case FLOAT_EXPR:
1885 case FIX_TRUNC_EXPR:
1886 case CONVERT_EXPR:
1887 case NOP_EXPR:
1888 pp_c_cast_expression (pp, e);
1889 break;
1891 case MULT_EXPR:
1892 case TRUNC_MOD_EXPR:
1893 case TRUNC_DIV_EXPR:
1894 pp_multiplicative_expression (pp, e);
1895 break;
1897 case LSHIFT_EXPR:
1898 case RSHIFT_EXPR:
1899 pp_c_shift_expression (pp, e);
1900 break;
1902 case LT_EXPR:
1903 case GT_EXPR:
1904 case LE_EXPR:
1905 case GE_EXPR:
1906 pp_c_relational_expression (pp, e);
1907 break;
1909 case BIT_AND_EXPR:
1910 pp_c_and_expression (pp, e);
1911 break;
1913 case BIT_XOR_EXPR:
1914 pp_c_exclusive_or_expression (pp, e);
1915 break;
1917 case BIT_IOR_EXPR:
1918 pp_c_inclusive_or_expression (pp, e);
1919 break;
1921 case TRUTH_ANDIF_EXPR:
1922 pp_c_logical_and_expression (pp, e);
1923 break;
1925 case TRUTH_ORIF_EXPR:
1926 pp_c_logical_or_expression (pp, e);
1927 break;
1929 case EQ_EXPR:
1930 case NE_EXPR:
1931 pp_c_equality_expression (pp, e);
1932 break;
1934 case COND_EXPR:
1935 pp_conditional_expression (pp, e);
1936 break;
1938 case PLUS_EXPR:
1939 case MINUS_EXPR:
1940 pp_c_additive_expression (pp, e);
1941 break;
1943 case MODIFY_EXPR:
1944 case INIT_EXPR:
1945 pp_assignment_expression (pp, e);
1946 break;
1948 case COMPOUND_EXPR:
1949 pp_c_left_paren (pp);
1950 pp_expression (pp, TREE_OPERAND (e, 0));
1951 pp_separate_with (pp, ',');
1952 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
1953 pp_c_right_paren (pp);
1954 break;
1956 case NON_LVALUE_EXPR:
1957 case SAVE_EXPR:
1958 pp_expression (pp, TREE_OPERAND (e, 0));
1959 break;
1961 case TARGET_EXPR:
1962 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
1963 break;
1965 default:
1966 pp_unsupported_tree (pp, e);
1967 break;
1973 /* Statements. */
1975 void
1976 pp_c_statement (c_pretty_printer *pp, tree stmt)
1978 if (stmt == NULL)
1979 return;
1981 if (pp_needs_newline (pp))
1982 pp_newline_and_indent (pp, 0);
1984 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
1988 /* Initialize the PRETTY-PRINTER for handling C codes. */
1990 void
1991 pp_c_pretty_printer_init (c_pretty_printer *pp)
1993 pp->offset_list = 0;
1995 pp->declaration = pp_c_declaration;
1996 pp->declaration_specifiers = pp_c_declaration_specifiers;
1997 pp->declarator = pp_c_declarator;
1998 pp->direct_declarator = pp_c_direct_declarator;
1999 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
2000 pp->abstract_declarator = pp_c_abstract_declarator;
2001 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
2002 pp->ptr_operator = pp_c_pointer;
2003 pp->parameter_list = pp_c_parameter_type_list;
2004 pp->type_id = pp_c_type_id;
2005 pp->simple_type_specifier = pp_c_type_specifier;
2006 pp->function_specifier = pp_c_function_specifier;
2007 pp->storage_class_specifier = pp_c_storage_class_specifier;
2009 pp->statement = pp_c_statement;
2011 pp->constant = pp_c_constant;
2012 pp->id_expression = pp_c_id_expression;
2013 pp->primary_expression = pp_c_primary_expression;
2014 pp->postfix_expression = pp_c_postfix_expression;
2015 pp->unary_expression = pp_c_unary_expression;
2016 pp->initializer = pp_c_initializer;
2017 pp->multiplicative_expression = pp_c_multiplicative_expression;
2018 pp->conditional_expression = pp_c_conditional_expression;
2019 pp->assignment_expression = pp_c_assignment_expression;
2020 pp->expression = pp_c_expression;
2024 /* Print the tree T in full, on file FILE. */
2026 void
2027 print_c_tree (FILE *file, tree t)
2029 static c_pretty_printer pp_rec;
2030 static bool initialized = 0;
2031 c_pretty_printer *pp = &pp_rec;
2033 if (!initialized)
2035 initialized = 1;
2036 pp_construct (pp_base (pp), NULL, 0);
2037 pp_c_pretty_printer_init (pp);
2038 pp_needs_newline (pp) = true;
2040 pp_base (pp)->buffer->stream = file;
2042 pp_statement (pp, t);
2044 pp_newline (pp);
2045 pp_flush (pp);
2048 /* Print the tree T in full, on stderr. */
2050 void
2051 debug_c_tree (tree t)
2053 print_c_tree (stderr, t);
2054 fputc ('\n', stderr);
2057 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2058 up of T's memory address. */
2060 void
2061 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2063 const char *name;
2065 gcc_assert (DECL_P (t));
2067 if (DECL_NAME (t))
2068 name = IDENTIFIER_POINTER (DECL_NAME (t));
2069 else
2071 static char xname[8];
2072 sprintf (xname, "<U%4x>", ((unsigned)((unsigned long)(t) & 0xffff)));
2073 name = xname;
2076 pp_c_identifier (pp, name);