* tree.c (find_tree_t, find_tree): Remove.
[official-gcc.git] / gcc / c-pretty-print.c
blob516022340ca978669dbe544045b7b2982721a308
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "real.h"
27 #include "c-pretty-print.h"
28 #include "c-tree.h"
29 #include "tree-iterator.h"
30 #include "diagnostic.h"
32 /* The pretty-printer code is primarily designed to closely follow
33 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
34 codes we used to have in the past. Following a structured
35 approach (preferably the official grammars) is believed to make it
36 much easier to add extensions and nifty pretty-printing effects that
37 takes expression or declaration contexts into account. */
40 #define pp_c_maybe_whitespace(PP) \
41 do { \
42 if (pp_base (PP)->padding == pp_before) \
43 pp_c_whitespace (PP); \
44 } while (0)
46 /* literal */
47 static void pp_c_char (c_pretty_printer *, int);
49 /* postfix-expression */
50 static void pp_c_initializer_list (c_pretty_printer *, tree);
51 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
53 static void pp_c_multiplicative_expression (c_pretty_printer *, tree);
54 static void pp_c_additive_expression (c_pretty_printer *, tree);
55 static void pp_c_shift_expression (c_pretty_printer *, tree);
56 static void pp_c_relational_expression (c_pretty_printer *, tree);
57 static void pp_c_equality_expression (c_pretty_printer *, tree);
58 static void pp_c_and_expression (c_pretty_printer *, tree);
59 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
60 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
61 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
62 static void pp_c_conditional_expression (c_pretty_printer *, tree);
63 static void pp_c_assignment_expression (c_pretty_printer *, tree);
65 /* declarations. */
68 /* Helper functions. */
70 void
71 pp_c_whitespace (c_pretty_printer *pp)
73 pp_space (pp);
74 pp_base (pp)->padding = pp_none;
77 void
78 pp_c_left_paren (c_pretty_printer *pp)
80 pp_left_paren (pp);
81 pp_base (pp)->padding = pp_none;
84 void
85 pp_c_right_paren (c_pretty_printer *pp)
87 pp_right_paren (pp);
88 pp_base (pp)->padding = pp_none;
91 void
92 pp_c_left_brace (c_pretty_printer *pp)
94 pp_left_brace (pp);
95 pp_base (pp)->padding = pp_none;
98 void
99 pp_c_right_brace (c_pretty_printer *pp)
101 pp_right_brace (pp);
102 pp_base (pp)->padding = pp_none;
105 void
106 pp_c_left_bracket (c_pretty_printer *pp)
108 pp_left_bracket (pp);
109 pp_base (pp)->padding = pp_none;
112 void
113 pp_c_right_bracket (c_pretty_printer *pp)
115 pp_right_bracket (pp);
116 pp_base (pp)->padding = pp_none;
119 void
120 pp_c_dot (c_pretty_printer *pp)
122 pp_dot (pp);
123 pp_base (pp)->padding = pp_none;
126 void
127 pp_c_ampersand (c_pretty_printer *pp)
129 pp_ampersand (pp);
130 pp_base (pp)->padding = pp_none;
133 void
134 pp_c_star (c_pretty_printer *pp)
136 pp_star (pp);
137 pp_base (pp)->padding = pp_none;
140 void
141 pp_c_arrow (c_pretty_printer *pp)
143 pp_arrow (pp);
144 pp_base (pp)->padding = pp_none;
147 void
148 pp_c_semicolon (c_pretty_printer *pp)
150 pp_semicolon (pp);
151 pp_base (pp)->padding = pp_none;
154 void
155 pp_c_complement (c_pretty_printer *pp)
157 pp_complement (pp);
158 pp_base (pp)->padding = pp_none;
161 void
162 pp_c_exclamation (c_pretty_printer *pp)
164 pp_exclamation (pp);
165 pp_base (pp)->padding = pp_none;
168 /* Print out the external representation of CV-QUALIFIER. */
170 static void
171 pp_c_cv_qualifier (c_pretty_printer *pp, const char *cv)
173 const char *p = pp_last_position_in_text (pp);
174 /* The C programming language does not have references, but it is much
175 simpler to handle those here rather than going through the same
176 logic in the C++ pretty-printer. */
177 if (p != NULL && (*p == '*' || *p == '&'))
178 pp_c_whitespace (pp);
179 pp_c_identifier (pp, cv);
182 /* Pretty-print T using the type-cast notation '( type-name )'. */
184 static void
185 pp_c_type_cast (c_pretty_printer *pp, tree t)
187 pp_c_left_paren (pp);
188 pp_type_id (pp, t);
189 pp_c_right_paren (pp);
192 /* We're about to pretty-print a pointer type as indicated by T.
193 Output a whitespace, if needed, preparing for subsequent output. */
195 void
196 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
198 if (POINTER_TYPE_P (t))
200 tree pointee = strip_pointer_operator (TREE_TYPE (t));
201 if (TREE_CODE (pointee) != ARRAY_TYPE
202 && TREE_CODE (pointee) != FUNCTION_TYPE)
203 pp_c_whitespace (pp);
208 /* Declarations. */
210 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
211 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
212 of its type. Take care of possible extensions.
214 type-qualifier-list:
215 type-qualifier
216 type-qualifier-list type-qualifier
218 type-qualifier:
219 const
220 restrict -- C99
221 __restrict__ -- GNU C
222 volatile */
224 void
225 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
227 int qualifiers;
229 if (!TYPE_P (t))
230 t = TREE_TYPE (t);
232 qualifiers = TYPE_QUALS (t);
233 if (qualifiers & TYPE_QUAL_CONST)
234 pp_c_cv_qualifier (pp, "const");
235 if (qualifiers & TYPE_QUAL_VOLATILE)
236 pp_c_cv_qualifier (pp, "volatile");
237 if (qualifiers & TYPE_QUAL_RESTRICT)
238 pp_c_cv_qualifier (pp, flag_isoc99 ? "restrict" : "__restrict__");
241 /* pointer:
242 * type-qualifier-list(opt)
243 * type-qualifier-list(opt) pointer */
245 static void
246 pp_c_pointer (c_pretty_printer *pp, tree t)
248 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
249 t = TREE_TYPE (t);
250 switch (TREE_CODE (t))
252 case POINTER_TYPE:
253 /* It is easier to handle C++ reference types here. */
254 case REFERENCE_TYPE:
255 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
256 pp_c_pointer (pp, TREE_TYPE (t));
257 if (TREE_CODE (t) == POINTER_TYPE)
258 pp_c_star (pp);
259 else
260 pp_c_ampersand (pp);
261 pp_c_type_qualifier_list (pp, t);
262 break;
264 /* ??? This node is now in GENERIC and so shouldn't be here. But
265 we'll fix that later. */
266 case DECL_EXPR:
267 pp_declaration (pp, DECL_EXPR_DECL (t));
268 pp_needs_newline (pp) = true;
269 break;
271 default:
272 pp_unsupported_tree (pp, t);
276 /* type-specifier:
277 void
278 char
279 short
281 long
282 float
283 double
284 signed
285 unsigned
286 _Bool -- C99
287 _Complex -- C99
288 _Imaginary -- C99
289 struct-or-union-specifier
290 enum-specifier
291 typedef-name.
293 GNU extensions.
294 simple-type-specifier:
295 __complex__
296 __vector__ */
298 void
299 pp_c_type_specifier (c_pretty_printer *pp, tree t)
301 const enum tree_code code = TREE_CODE (t);
302 switch (code)
304 case ERROR_MARK:
305 pp_c_identifier (pp, "<type-error>");
306 break;
308 case IDENTIFIER_NODE:
309 pp_c_tree_decl_identifier (pp, t);
310 break;
312 case VOID_TYPE:
313 case BOOLEAN_TYPE:
314 case CHAR_TYPE:
315 case INTEGER_TYPE:
316 case REAL_TYPE:
317 if (TYPE_NAME (t))
319 t = TYPE_NAME (t);
320 pp_c_type_specifier (pp, t);
322 else
324 int prec = TYPE_PRECISION (t);
325 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
326 if (TYPE_NAME (t))
328 pp_c_type_specifier (pp, t);
329 if (TYPE_PRECISION (t) != prec)
331 pp_string (pp, ":");
332 pp_decimal_int (pp, prec);
335 else
337 switch (code)
339 case INTEGER_TYPE:
340 pp_string (pp, (TYPE_UNSIGNED (t)
341 ? "<unnamed-unsigned:"
342 : "<unnamed-signed:"));
343 break;
344 case REAL_TYPE:
345 pp_string (pp, "<unnamed-float:");
346 break;
347 default:
348 gcc_unreachable ();
350 pp_decimal_int (pp, prec);
351 pp_string (pp, ">");
354 break;
356 case TYPE_DECL:
357 if (DECL_NAME (t))
358 pp_id_expression (pp, t);
359 else
360 pp_c_identifier (pp, "<typedef-error>");
361 break;
363 case UNION_TYPE:
364 case RECORD_TYPE:
365 case ENUMERAL_TYPE:
366 if (code == UNION_TYPE)
367 pp_c_identifier (pp, "union");
368 else if (code == RECORD_TYPE)
369 pp_c_identifier (pp, "struct");
370 else if (code == ENUMERAL_TYPE)
371 pp_c_identifier (pp, "enum");
372 else
373 pp_c_identifier (pp, "<tag-error>");
375 if (TYPE_NAME (t))
376 pp_id_expression (pp, TYPE_NAME (t));
377 else
378 pp_c_identifier (pp, "<anonymous>");
379 break;
381 default:
382 pp_unsupported_tree (pp, t);
383 break;
387 /* specifier-qualifier-list:
388 type-specifier specifier-qualifier-list-opt
389 type-qualifier specifier-qualifier-list-opt
392 Implementation note: Because of the non-linearities in array or
393 function declarations, this routine prints not just the
394 specifier-qualifier-list of such entities or types of such entities,
395 but also the 'pointer' production part of their declarators. The
396 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
398 void
399 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
401 const enum tree_code code = TREE_CODE (t);
403 if (TREE_CODE (t) != POINTER_TYPE)
404 pp_c_type_qualifier_list (pp, t);
405 switch (code)
407 case REFERENCE_TYPE:
408 case POINTER_TYPE:
410 /* Get the types-specifier of this type. */
411 tree pointee = strip_pointer_operator (TREE_TYPE (t));
412 pp_c_specifier_qualifier_list (pp, pointee);
413 if (TREE_CODE (pointee) == ARRAY_TYPE
414 || TREE_CODE (pointee) == FUNCTION_TYPE)
416 pp_c_whitespace (pp);
417 pp_c_left_paren (pp);
419 else if (!c_dialect_cxx ())
420 pp_c_whitespace (pp);
421 pp_ptr_operator (pp, t);
423 break;
425 case FUNCTION_TYPE:
426 case ARRAY_TYPE:
427 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
428 break;
430 case VECTOR_TYPE:
431 case COMPLEX_TYPE:
432 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
433 if (code == COMPLEX_TYPE)
434 pp_c_identifier (pp, flag_isoc99 ? "_Complex" : "__complex__");
435 else if (code == VECTOR_TYPE)
436 pp_c_identifier (pp, "__vector__");
437 break;
439 default:
440 pp_simple_type_specifier (pp, t);
441 break;
445 /* parameter-type-list:
446 parameter-list
447 parameter-list , ...
449 parameter-list:
450 parameter-declaration
451 parameter-list , parameter-declaration
453 parameter-declaration:
454 declaration-specifiers declarator
455 declaration-specifiers abstract-declarator(opt) */
457 void
458 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
460 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
461 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
462 pp_c_left_paren (pp);
463 if (parms == void_list_node)
464 pp_c_identifier (pp, "void");
465 else
467 bool first = true;
468 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
470 if (!first)
471 pp_separate_with (pp, ',');
472 first = false;
473 pp_declaration_specifiers
474 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
475 if (want_parm_decl)
476 pp_declarator (pp, parms);
477 else
478 pp_abstract_declarator (pp, TREE_VALUE (parms));
481 pp_c_right_paren (pp);
484 /* abstract-declarator:
485 pointer
486 pointer(opt) direct-abstract-declarator */
488 static void
489 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
491 if (TREE_CODE (t) == POINTER_TYPE)
493 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
494 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
495 pp_c_right_paren (pp);
496 t = TREE_TYPE (t);
499 pp_direct_abstract_declarator (pp, t);
502 /* direct-abstract-declarator:
503 ( abstract-declarator )
504 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
505 direct-abstract-declarator(opt) [ * ]
506 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
508 void
509 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
511 switch (TREE_CODE (t))
513 case POINTER_TYPE:
514 pp_abstract_declarator (pp, t);
515 break;
517 case FUNCTION_TYPE:
518 pp_c_parameter_type_list (pp, t);
519 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
520 break;
522 case ARRAY_TYPE:
523 pp_c_left_bracket (pp);
524 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
525 pp_expression (pp, TYPE_MAX_VALUE (TYPE_DOMAIN (t)));
526 pp_c_right_bracket (pp);
527 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
528 break;
530 case IDENTIFIER_NODE:
531 case VOID_TYPE:
532 case BOOLEAN_TYPE:
533 case INTEGER_TYPE:
534 case REAL_TYPE:
535 case ENUMERAL_TYPE:
536 case RECORD_TYPE:
537 case UNION_TYPE:
538 case VECTOR_TYPE:
539 case COMPLEX_TYPE:
540 case TYPE_DECL:
541 break;
543 default:
544 pp_unsupported_tree (pp, t);
545 break;
549 /* type-name:
550 specifier-qualifier-list abstract-declarator(opt) */
552 void
553 pp_c_type_id (c_pretty_printer *pp, tree t)
555 pp_c_specifier_qualifier_list (pp, t);
556 pp_abstract_declarator (pp, t);
559 /* storage-class-specifier:
560 typedef
561 extern
562 static
563 auto
564 register */
566 void
567 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
569 if (TREE_CODE (t) == TYPE_DECL)
570 pp_c_identifier (pp, "typedef");
571 else if (DECL_P (t))
573 if (DECL_REGISTER (t))
574 pp_c_identifier (pp, "register");
575 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
576 pp_c_identifier (pp, "static");
580 /* function-specifier:
581 inline */
583 void
584 pp_c_function_specifier (c_pretty_printer *pp, tree t)
586 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
587 pp_c_identifier (pp, "inline");
590 /* declaration-specifiers:
591 storage-class-specifier declaration-specifiers(opt)
592 type-specifier declaration-specifiers(opt)
593 type-qualifier declaration-specifiers(opt)
594 function-specifier declaration-specifiers(opt) */
596 void
597 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
599 pp_storage_class_specifier (pp, t);
600 pp_function_specifier (pp, t);
601 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
604 /* direct-declarator
605 identifier
606 ( declarator )
607 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
608 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
609 direct-declarator [ type-qualifier-list static assignment-expression ]
610 direct-declarator [ type-qualifier-list * ]
611 direct-declarator ( parameter-type-list )
612 direct-declarator ( identifier-list(opt) ) */
614 void
615 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
617 switch (TREE_CODE (t))
619 case VAR_DECL:
620 case PARM_DECL:
621 case TYPE_DECL:
622 case FIELD_DECL:
623 case LABEL_DECL:
624 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
625 pp_c_tree_decl_identifier (pp, t);
626 break;
628 case ARRAY_TYPE:
629 case POINTER_TYPE:
630 pp_abstract_declarator (pp, TREE_TYPE (t));
631 break;
633 case FUNCTION_TYPE:
634 pp_parameter_list (pp, t);
635 pp_abstract_declarator (pp, TREE_TYPE (t));
636 break;
638 case FUNCTION_DECL:
639 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
640 pp_c_tree_decl_identifier (pp, t);
641 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
642 pp_abstract_declarator (pp, TREE_TYPE (t));
643 else
645 pp_parameter_list (pp, t);
646 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
648 break;
650 case INTEGER_TYPE:
651 case REAL_TYPE:
652 case ENUMERAL_TYPE:
653 case UNION_TYPE:
654 case RECORD_TYPE:
655 break;
657 default:
658 pp_unsupported_tree (pp, t);
659 break;
664 /* declarator:
665 pointer(opt) direct-declarator */
667 void
668 pp_c_declarator (c_pretty_printer *pp, tree t)
670 switch (TREE_CODE (t))
672 case INTEGER_TYPE:
673 case REAL_TYPE:
674 case ENUMERAL_TYPE:
675 case UNION_TYPE:
676 case RECORD_TYPE:
677 break;
679 case VAR_DECL:
680 case PARM_DECL:
681 case FIELD_DECL:
682 case ARRAY_TYPE:
683 case FUNCTION_TYPE:
684 case FUNCTION_DECL:
685 case TYPE_DECL:
686 pp_direct_declarator (pp, t);
687 break;
690 default:
691 pp_unsupported_tree (pp, t);
692 break;
696 /* declaration:
697 declaration-specifiers init-declarator-list(opt) ; */
699 void
700 pp_c_declaration (c_pretty_printer *pp, tree t)
702 pp_declaration_specifiers (pp, t);
703 pp_c_init_declarator (pp, t);
706 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
708 void
709 pp_c_attributes (c_pretty_printer *pp, tree attributes)
711 if (attributes == NULL_TREE)
712 return;
714 pp_c_identifier (pp, "__attribute__");
715 pp_c_left_paren (pp);
716 pp_c_left_paren (pp);
717 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
719 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
720 if (TREE_VALUE (attributes))
721 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
723 if (TREE_CHAIN (attributes))
724 pp_separate_with (pp, ',');
726 pp_c_right_paren (pp);
727 pp_c_right_paren (pp);
730 /* function-definition:
731 declaration-specifiers declarator compound-statement */
733 void
734 pp_c_function_definition (c_pretty_printer *pp, tree t)
736 pp_declaration_specifiers (pp, t);
737 pp_declarator (pp, t);
738 pp_needs_newline (pp) = true;
739 pp_statement (pp, DECL_SAVED_TREE (t));
740 pp_newline (pp);
741 pp_flush (pp);
745 /* Expressions. */
747 /* Print out a c-char. This is called solely for characters which are
748 in the *target* execution character set. We ought to convert them
749 back to the *host* execution character set before printing, but we
750 have no way to do this at present. A decent compromise is to print
751 all characters as if they were in the host execution character set,
752 and not attempt to recover any named escape characters, but render
753 all unprintables as octal escapes. If the host and target character
754 sets are the same, this produces relatively readable output. If they
755 are not the same, strings may appear as gibberish, but that's okay
756 (in fact, it may well be what the reader wants, e.g. if they are looking
757 to see if conversion to the target character set happened correctly).
759 A special case: we need to prefix \, ", and ' with backslashes. It is
760 correct to do so for the *host*'s \, ", and ', because the rest of the
761 file appears in the host character set. */
763 static void
764 pp_c_char (c_pretty_printer *pp, int c)
766 if (ISPRINT (c))
768 switch (c)
770 case '\\': pp_string (pp, "\\\\"); break;
771 case '\'': pp_string (pp, "\\\'"); break;
772 case '\"': pp_string (pp, "\\\""); break;
773 default: pp_character (pp, c);
776 else
777 pp_scalar (pp, "\\%03o", (unsigned) c);
780 /* Print out a STRING literal. */
782 void
783 pp_c_string_literal (c_pretty_printer *pp, tree s)
785 const char *p = TREE_STRING_POINTER (s);
786 int n = TREE_STRING_LENGTH (s) - 1;
787 int i;
788 pp_doublequote (pp);
789 for (i = 0; i < n; ++i)
790 pp_c_char (pp, p[i]);
791 pp_doublequote (pp);
794 /* Pretty-print an INTEGER literal. */
796 static void
797 pp_c_integer_constant (c_pretty_printer *pp, tree i)
799 tree type = TREE_TYPE (i);
801 if (TREE_INT_CST_HIGH (i) == 0)
802 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
803 else
805 if (tree_int_cst_sgn (i) < 0)
807 pp_character (pp, '-');
808 i = build_int_cst_wide (NULL_TREE,
809 -TREE_INT_CST_LOW (i),
810 ~TREE_INT_CST_HIGH (i)
811 + !TREE_INT_CST_LOW (i));
813 sprintf (pp_buffer (pp)->digit_buffer,
814 HOST_WIDE_INT_PRINT_DOUBLE_HEX,
815 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
816 pp_string (pp, pp_buffer (pp)->digit_buffer);
818 if (TYPE_UNSIGNED (type))
819 pp_character (pp, 'u');
820 if (type == long_integer_type_node || type == long_unsigned_type_node)
821 pp_character (pp, 'l');
822 else if (type == long_long_integer_type_node
823 || type == long_long_unsigned_type_node)
824 pp_string (pp, "ll");
827 /* Print out a CHARACTER literal. */
829 static void
830 pp_c_character_constant (c_pretty_printer *pp, tree c)
832 tree type = TREE_TYPE (c);
833 if (type == wchar_type_node)
834 pp_character (pp, 'L');
835 pp_quote (pp);
836 if (host_integerp (c, TYPE_UNSIGNED (type)))
837 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
838 else
839 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
840 pp_quote (pp);
843 /* Print out a BOOLEAN literal. */
845 static void
846 pp_c_bool_constant (c_pretty_printer *pp, tree b)
848 if (b == boolean_false_node)
850 if (c_dialect_cxx ())
851 pp_c_identifier (pp, "false");
852 else if (flag_isoc99)
853 pp_c_identifier (pp, "_False");
854 else
855 pp_unsupported_tree (pp, b);
857 else if (b == boolean_true_node)
859 if (c_dialect_cxx ())
860 pp_c_identifier (pp, "true");
861 else if (flag_isoc99)
862 pp_c_identifier (pp, "_True");
863 else
864 pp_unsupported_tree (pp, b);
866 else if (TREE_CODE (b) == INTEGER_CST)
867 pp_c_integer_constant (pp, b);
868 else
869 pp_unsupported_tree (pp, b);
872 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
873 false; that means the value was obtained by a cast, in which case
874 print out the type-id part of the cast-expression -- the casted value
875 is then printed by pp_c_integer_literal. */
877 static bool
878 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
880 bool value_is_named = true;
881 tree type = TREE_TYPE (e);
882 tree value;
884 /* Find the name of this constant. */
885 for (value = TYPE_VALUES (type);
886 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
887 value = TREE_CHAIN (value))
890 if (value != NULL_TREE)
891 pp_id_expression (pp, TREE_PURPOSE (value));
892 else
894 /* Value must have been cast. */
895 pp_c_type_cast (pp, type);
896 value_is_named = false;
899 return value_is_named;
902 /* Print out a REAL value as a decimal-floating-constant. */
904 static void
905 pp_c_floating_constant (c_pretty_printer *pp, tree r)
907 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
908 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
909 pp_string (pp, pp_buffer(pp)->digit_buffer);
910 if (TREE_TYPE (r) == float_type_node)
911 pp_character (pp, 'f');
912 else if (TREE_TYPE (r) == long_double_type_node)
913 pp_character (pp, 'l');
914 else if (TREE_TYPE (r) == dfloat128_type_node)
915 pp_string (pp, "dl");
916 else if (TREE_TYPE (r) == dfloat64_type_node)
917 pp_string (pp, "dd");
918 else if (TREE_TYPE (r) == dfloat32_type_node)
919 pp_string (pp, "df");
922 /* Pretty-print a compound literal expression. GNU extensions include
923 vector constants. */
925 static void
926 pp_c_compound_literal (c_pretty_printer *pp, tree e)
928 tree type = TREE_TYPE (e);
929 pp_c_type_cast (pp, type);
931 switch (TREE_CODE (type))
933 case RECORD_TYPE:
934 case UNION_TYPE:
935 case ARRAY_TYPE:
936 case VECTOR_TYPE:
937 case COMPLEX_TYPE:
938 pp_c_brace_enclosed_initializer_list (pp, e);
939 break;
941 default:
942 pp_unsupported_tree (pp, e);
943 break;
947 /* constant:
948 integer-constant
949 floating-constant
950 enumeration-constant
951 character-constant */
953 void
954 pp_c_constant (c_pretty_printer *pp, tree e)
956 const enum tree_code code = TREE_CODE (e);
958 switch (code)
960 case INTEGER_CST:
962 tree type = TREE_TYPE (e);
963 if (type == boolean_type_node)
964 pp_c_bool_constant (pp, e);
965 else if (type == char_type_node)
966 pp_c_character_constant (pp, e);
967 else if (TREE_CODE (type) == ENUMERAL_TYPE
968 && pp_c_enumeration_constant (pp, e))
970 else
971 pp_c_integer_constant (pp, e);
973 break;
975 case REAL_CST:
976 pp_c_floating_constant (pp, e);
977 break;
979 case STRING_CST:
980 pp_c_string_literal (pp, e);
981 break;
983 default:
984 pp_unsupported_tree (pp, e);
985 break;
989 /* Pretty-print an IDENTIFIER_NODE, preceded by whitespace is necessary. */
991 void
992 pp_c_identifier (c_pretty_printer *pp, const char *id)
994 pp_c_maybe_whitespace (pp);
995 pp_identifier (pp, id);
996 pp_base (pp)->padding = pp_before;
999 /* Pretty-print a C primary-expression.
1000 primary-expression:
1001 identifier
1002 constant
1003 string-literal
1004 ( expression ) */
1006 void
1007 pp_c_primary_expression (c_pretty_printer *pp, tree e)
1009 switch (TREE_CODE (e))
1011 case VAR_DECL:
1012 case PARM_DECL:
1013 case FIELD_DECL:
1014 case CONST_DECL:
1015 case FUNCTION_DECL:
1016 case LABEL_DECL:
1017 pp_c_tree_decl_identifier (pp, e);
1018 break;
1020 case IDENTIFIER_NODE:
1021 pp_c_tree_identifier (pp, e);
1022 break;
1024 case ERROR_MARK:
1025 pp_c_identifier (pp, "<erroneous-expression>");
1026 break;
1028 case RESULT_DECL:
1029 pp_c_identifier (pp, "<return-value>");
1030 break;
1032 case INTEGER_CST:
1033 case REAL_CST:
1034 case STRING_CST:
1035 pp_c_constant (pp, e);
1036 break;
1038 case TARGET_EXPR:
1039 pp_c_identifier (pp, "__builtin_memcpy");
1040 pp_c_left_paren (pp);
1041 pp_ampersand (pp);
1042 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1043 pp_separate_with (pp, ',');
1044 pp_ampersand (pp);
1045 pp_initializer (pp, TREE_OPERAND (e, 1));
1046 if (TREE_OPERAND (e, 2))
1048 pp_separate_with (pp, ',');
1049 pp_c_expression (pp, TREE_OPERAND (e, 2));
1051 pp_c_right_paren (pp);
1052 break;
1054 default:
1055 /* FIXME: Make sure we won't get into an infinie loop. */
1056 pp_c_left_paren (pp);
1057 pp_expression (pp, e);
1058 pp_c_right_paren (pp);
1059 break;
1063 /* Print out a C initializer -- also support C compound-literals.
1064 initializer:
1065 assignment-expression:
1066 { initializer-list }
1067 { initializer-list , } */
1069 static void
1070 pp_c_initializer (c_pretty_printer *pp, tree e)
1072 if (TREE_CODE (e) == CONSTRUCTOR)
1073 pp_c_brace_enclosed_initializer_list (pp, e);
1074 else
1075 pp_expression (pp, e);
1078 /* init-declarator:
1079 declarator:
1080 declarator = initializer */
1082 void
1083 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1085 pp_declarator (pp, t);
1086 /* We don't want to output function definitions here. There are handled
1087 elsewhere (and the syntactic form is bogus anyway). */
1088 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1090 tree init = DECL_INITIAL (t);
1091 /* This C++ bit is handled here because it is easier to do so.
1092 In templates, the C++ parser builds a TREE_LIST for a
1093 direct-initialization; the TREE_PURPOSE is the variable to
1094 initialize and the TREE_VALUE is the initializer. */
1095 if (TREE_CODE (init) == TREE_LIST)
1097 pp_c_left_paren (pp);
1098 pp_expression (pp, TREE_VALUE (init));
1099 pp_right_paren (pp);
1101 else
1103 pp_space (pp);
1104 pp_equal (pp);
1105 pp_space (pp);
1106 pp_c_initializer (pp, init);
1111 /* initializer-list:
1112 designation(opt) initializer
1113 initializer-list , designation(opt) initializer
1115 designation:
1116 designator-list =
1118 designator-list:
1119 designator
1120 designator-list designator
1122 designator:
1123 [ constant-expression ]
1124 identifier */
1126 static void
1127 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1129 tree type = TREE_TYPE (e);
1130 const enum tree_code code = TREE_CODE (type);
1132 switch (code)
1134 case RECORD_TYPE:
1135 case UNION_TYPE:
1136 case ARRAY_TYPE:
1138 tree init = TREE_OPERAND (e, 0);
1139 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1141 if (code == RECORD_TYPE || code == UNION_TYPE)
1143 pp_c_dot (pp);
1144 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1146 else
1148 pp_c_left_bracket (pp);
1149 if (TREE_PURPOSE (init))
1150 pp_c_constant (pp, TREE_PURPOSE (init));
1151 pp_c_right_bracket (pp);
1153 pp_c_whitespace (pp);
1154 pp_equal (pp);
1155 pp_c_whitespace (pp);
1156 pp_initializer (pp, TREE_VALUE (init));
1157 if (TREE_CHAIN (init))
1158 pp_separate_with (pp, ',');
1161 return;
1163 case VECTOR_TYPE:
1164 if (TREE_CODE (e) == VECTOR_CST)
1165 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1166 else if (TREE_CODE (e) == CONSTRUCTOR)
1167 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1168 else
1169 break;
1170 return;
1172 case COMPLEX_TYPE:
1173 if (TREE_CODE (e) == CONSTRUCTOR)
1174 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1175 else if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1177 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1178 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1179 pp_separate_with (pp, ',');
1180 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1182 else
1183 break;
1184 return;
1186 default:
1187 break;
1190 pp_unsupported_tree (pp, type);
1193 /* Pretty-print a brace-enclosed initializer-list. */
1195 static void
1196 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1198 pp_c_left_brace (pp);
1199 pp_c_initializer_list (pp, l);
1200 pp_c_right_brace (pp);
1204 /* This is a convenient function, used to bridge gap between C and C++
1205 grammars.
1207 id-expression:
1208 identifier */
1210 void
1211 pp_c_id_expression (c_pretty_printer *pp, tree t)
1213 switch (TREE_CODE (t))
1215 case VAR_DECL:
1216 case PARM_DECL:
1217 case CONST_DECL:
1218 case TYPE_DECL:
1219 case FUNCTION_DECL:
1220 case FIELD_DECL:
1221 case LABEL_DECL:
1222 pp_c_tree_decl_identifier (pp, t);
1223 break;
1225 case IDENTIFIER_NODE:
1226 pp_c_tree_identifier (pp, t);
1227 break;
1229 default:
1230 pp_unsupported_tree (pp, t);
1231 break;
1235 /* postfix-expression:
1236 primary-expression
1237 postfix-expression [ expression ]
1238 postfix-expression ( argument-expression-list(opt) )
1239 postfix-expression . identifier
1240 postfix-expression -> identifier
1241 postfix-expression ++
1242 postfix-expression --
1243 ( type-name ) { initializer-list }
1244 ( type-name ) { initializer-list , } */
1246 void
1247 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1249 enum tree_code code = TREE_CODE (e);
1250 switch (code)
1252 case POSTINCREMENT_EXPR:
1253 case POSTDECREMENT_EXPR:
1254 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1255 pp_identifier (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1256 break;
1258 case ARRAY_REF:
1259 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1260 pp_c_left_bracket (pp);
1261 pp_expression (pp, TREE_OPERAND (e, 1));
1262 pp_c_right_bracket (pp);
1263 break;
1265 case CALL_EXPR:
1266 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1267 pp_c_call_argument_list (pp, TREE_OPERAND (e, 1));
1268 break;
1270 case UNORDERED_EXPR:
1271 pp_c_identifier (pp, flag_isoc99
1272 ? "isunordered"
1273 : "__builtin_isunordered");
1274 goto two_args_fun;
1276 case ORDERED_EXPR:
1277 pp_c_identifier (pp, flag_isoc99
1278 ? "!isunordered"
1279 : "!__builtin_isunordered");
1280 goto two_args_fun;
1282 case UNLT_EXPR:
1283 pp_c_identifier (pp, flag_isoc99
1284 ? "!isgreaterequal"
1285 : "!__builtin_isgreaterequal");
1286 goto two_args_fun;
1288 case UNLE_EXPR:
1289 pp_c_identifier (pp, flag_isoc99
1290 ? "!isgreater"
1291 : "!__builtin_isgreater");
1292 goto two_args_fun;
1294 case UNGT_EXPR:
1295 pp_c_identifier (pp, flag_isoc99
1296 ? "!islessequal"
1297 : "!__builtin_islessequal");
1298 goto two_args_fun;
1300 case UNGE_EXPR:
1301 pp_c_identifier (pp, flag_isoc99
1302 ? "!isless"
1303 : "!__builtin_isless");
1304 goto two_args_fun;
1306 case UNEQ_EXPR:
1307 pp_c_identifier (pp, flag_isoc99
1308 ? "!islessgreater"
1309 : "!__builtin_islessgreater");
1310 goto two_args_fun;
1312 case LTGT_EXPR:
1313 pp_c_identifier (pp, flag_isoc99
1314 ? "islessgreater"
1315 : "__builtin_islessgreater");
1316 goto two_args_fun;
1318 two_args_fun:
1319 pp_c_left_paren (pp);
1320 pp_expression (pp, TREE_OPERAND (e, 0));
1321 pp_separate_with (pp, ',');
1322 pp_expression (pp, TREE_OPERAND (e, 1));
1323 pp_c_right_paren (pp);
1324 break;
1326 case ABS_EXPR:
1327 pp_c_identifier (pp, "__builtin_abs");
1328 pp_c_left_paren (pp);
1329 pp_expression (pp, TREE_OPERAND (e, 0));
1330 pp_c_right_paren (pp);
1331 break;
1333 case COMPONENT_REF:
1335 tree object = TREE_OPERAND (e, 0);
1336 if (TREE_CODE (object) == INDIRECT_REF)
1338 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1339 pp_c_arrow (pp);
1341 else
1343 pp_postfix_expression (pp, object);
1344 pp_c_dot (pp);
1346 pp_expression (pp, TREE_OPERAND (e, 1));
1348 break;
1350 case COMPLEX_CST:
1351 case VECTOR_CST:
1352 case COMPLEX_EXPR:
1353 pp_c_compound_literal (pp, e);
1354 break;
1356 case COMPOUND_LITERAL_EXPR:
1357 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1358 /* Fall through. */
1359 case CONSTRUCTOR:
1360 pp_initializer (pp, e);
1361 break;
1363 case VA_ARG_EXPR:
1364 pp_c_identifier (pp, "__builtin_va_arg");
1365 pp_c_left_paren (pp);
1366 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1367 pp_separate_with (pp, ',');
1368 pp_type_id (pp, TREE_TYPE (e));
1369 pp_c_right_paren (pp);
1370 break;
1372 case ADDR_EXPR:
1373 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1375 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1376 break;
1378 /* else fall through. */
1380 default:
1381 pp_primary_expression (pp, e);
1382 break;
1386 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1388 void
1389 pp_c_expression_list (c_pretty_printer *pp, tree e)
1391 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1393 pp_expression (pp, TREE_VALUE (e));
1394 if (TREE_CHAIN (e))
1395 pp_separate_with (pp, ',');
1399 /* Print out V, which contains the elements of a constructor. */
1401 void
1402 pp_c_constructor_elts (c_pretty_printer *pp, VEC(constructor_elt,gc) *v)
1404 unsigned HOST_WIDE_INT ix;
1405 tree value;
1407 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1409 pp_expression (pp, value);
1410 if (ix != VEC_length (constructor_elt, v) - 1)
1411 pp_separate_with (pp, ',');
1415 /* Print out an expression-list in parens, as in a function call. */
1417 void
1418 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1420 pp_c_left_paren (pp);
1421 if (t && TREE_CODE (t) == TREE_LIST)
1422 pp_c_expression_list (pp, t);
1423 pp_c_right_paren (pp);
1426 /* unary-expression:
1427 postfix-expression
1428 ++ cast-expression
1429 -- cast-expression
1430 unary-operator cast-expression
1431 sizeof unary-expression
1432 sizeof ( type-id )
1434 unary-operator: one of
1435 * & + - ! ~
1437 GNU extensions.
1438 unary-expression:
1439 __alignof__ unary-expression
1440 __alignof__ ( type-id )
1441 __real__ unary-expression
1442 __imag__ unary-expression */
1444 void
1445 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1447 enum tree_code code = TREE_CODE (e);
1448 switch (code)
1450 case PREINCREMENT_EXPR:
1451 case PREDECREMENT_EXPR:
1452 pp_identifier (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1453 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1454 break;
1456 case ADDR_EXPR:
1457 case INDIRECT_REF:
1458 case NEGATE_EXPR:
1459 case BIT_NOT_EXPR:
1460 case TRUTH_NOT_EXPR:
1461 case CONJ_EXPR:
1462 /* String literal are used by address. */
1463 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1464 pp_ampersand (pp);
1465 else if (code == INDIRECT_REF)
1466 pp_c_star (pp);
1467 else if (code == NEGATE_EXPR)
1468 pp_minus (pp);
1469 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1470 pp_complement (pp);
1471 else if (code == TRUTH_NOT_EXPR)
1472 pp_exclamation (pp);
1473 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1474 break;
1476 case REALPART_EXPR:
1477 case IMAGPART_EXPR:
1478 pp_c_identifier (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1479 pp_c_whitespace (pp);
1480 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1481 break;
1483 default:
1484 pp_postfix_expression (pp, e);
1485 break;
1489 /* cast-expression:
1490 unary-expression
1491 ( type-name ) cast-expression */
1493 void
1494 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1496 switch (TREE_CODE (e))
1498 case FLOAT_EXPR:
1499 case FIX_TRUNC_EXPR:
1500 case CONVERT_EXPR:
1501 case NOP_EXPR:
1502 pp_c_type_cast (pp, TREE_TYPE (e));
1503 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1504 break;
1506 default:
1507 pp_unary_expression (pp, e);
1511 /* multiplicative-expression:
1512 cast-expression
1513 multiplicative-expression * cast-expression
1514 multiplicative-expression / cast-expression
1515 multiplicative-expression % cast-expression */
1517 static void
1518 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1520 enum tree_code code = TREE_CODE (e);
1521 switch (code)
1523 case MULT_EXPR:
1524 case TRUNC_DIV_EXPR:
1525 case TRUNC_MOD_EXPR:
1526 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1527 pp_c_whitespace (pp);
1528 if (code == MULT_EXPR)
1529 pp_c_star (pp);
1530 else if (code == TRUNC_DIV_EXPR)
1531 pp_slash (pp);
1532 else
1533 pp_modulo (pp);
1534 pp_c_whitespace (pp);
1535 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1536 break;
1538 default:
1539 pp_c_cast_expression (pp, e);
1540 break;
1544 /* additive-expression:
1545 multiplicative-expression
1546 additive-expression + multiplicative-expression
1547 additive-expression - multiplicative-expression */
1549 static void
1550 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1552 enum tree_code code = TREE_CODE (e);
1553 switch (code)
1555 case PLUS_EXPR:
1556 case MINUS_EXPR:
1557 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1558 pp_c_whitespace (pp);
1559 if (code == PLUS_EXPR)
1560 pp_plus (pp);
1561 else
1562 pp_minus (pp);
1563 pp_c_whitespace (pp);
1564 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1565 break;
1567 default:
1568 pp_multiplicative_expression (pp, e);
1569 break;
1573 /* additive-expression:
1574 additive-expression
1575 shift-expression << additive-expression
1576 shift-expression >> additive-expression */
1578 static void
1579 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1581 enum tree_code code = TREE_CODE (e);
1582 switch (code)
1584 case LSHIFT_EXPR:
1585 case RSHIFT_EXPR:
1586 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1587 pp_c_whitespace (pp);
1588 pp_identifier (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1589 pp_c_whitespace (pp);
1590 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1591 break;
1593 default:
1594 pp_c_additive_expression (pp, e);
1598 /* relational-expression:
1599 shift-expression
1600 relational-expression < shift-expression
1601 relational-expression > shift-expression
1602 relational-expression <= shift-expression
1603 relational-expression >= shift-expression */
1605 static void
1606 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1608 enum tree_code code = TREE_CODE (e);
1609 switch (code)
1611 case LT_EXPR:
1612 case GT_EXPR:
1613 case LE_EXPR:
1614 case GE_EXPR:
1615 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1616 pp_c_whitespace (pp);
1617 if (code == LT_EXPR)
1618 pp_less (pp);
1619 else if (code == GT_EXPR)
1620 pp_greater (pp);
1621 else if (code == LE_EXPR)
1622 pp_identifier (pp, "<=");
1623 else if (code == GE_EXPR)
1624 pp_identifier (pp, ">=");
1625 pp_c_whitespace (pp);
1626 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1627 break;
1629 default:
1630 pp_c_shift_expression (pp, e);
1631 break;
1635 /* equality-expression:
1636 relational-expression
1637 equality-expression == relational-expression
1638 equality-equality != relational-expression */
1640 static void
1641 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1643 enum tree_code code = TREE_CODE (e);
1644 switch (code)
1646 case EQ_EXPR:
1647 case NE_EXPR:
1648 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1649 pp_c_whitespace (pp);
1650 pp_identifier (pp, code == EQ_EXPR ? "==" : "!=");
1651 pp_c_whitespace (pp);
1652 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1653 break;
1655 default:
1656 pp_c_relational_expression (pp, e);
1657 break;
1661 /* AND-expression:
1662 equality-expression
1663 AND-expression & equality-equality */
1665 static void
1666 pp_c_and_expression (c_pretty_printer *pp, tree e)
1668 if (TREE_CODE (e) == BIT_AND_EXPR)
1670 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1671 pp_c_whitespace (pp);
1672 pp_ampersand (pp);
1673 pp_c_whitespace (pp);
1674 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1676 else
1677 pp_c_equality_expression (pp, e);
1680 /* exclusive-OR-expression:
1681 AND-expression
1682 exclusive-OR-expression ^ AND-expression */
1684 static void
1685 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1687 if (TREE_CODE (e) == BIT_XOR_EXPR)
1689 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1690 pp_c_maybe_whitespace (pp);
1691 pp_carret (pp);
1692 pp_c_whitespace (pp);
1693 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1695 else
1696 pp_c_and_expression (pp, e);
1699 /* inclusive-OR-expression:
1700 exclusive-OR-expression
1701 inclusive-OR-expression | exclusive-OR-expression */
1703 static void
1704 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1706 if (TREE_CODE (e) == BIT_IOR_EXPR)
1708 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1709 pp_c_whitespace (pp);
1710 pp_bar (pp);
1711 pp_c_whitespace (pp);
1712 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1714 else
1715 pp_c_exclusive_or_expression (pp, e);
1718 /* logical-AND-expression:
1719 inclusive-OR-expression
1720 logical-AND-expression && inclusive-OR-expression */
1722 static void
1723 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1725 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
1727 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1728 pp_c_whitespace (pp);
1729 pp_identifier (pp, "&&");
1730 pp_c_whitespace (pp);
1731 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1733 else
1734 pp_c_inclusive_or_expression (pp, e);
1737 /* logical-OR-expression:
1738 logical-AND-expression
1739 logical-OR-expression || logical-AND-expression */
1741 void
1742 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1744 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1746 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1747 pp_c_whitespace (pp);
1748 pp_identifier (pp, "||");
1749 pp_c_whitespace (pp);
1750 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1752 else
1753 pp_c_logical_and_expression (pp, e);
1756 /* conditional-expression:
1757 logical-OR-expression
1758 logical-OR-expression ? expression : conditional-expression */
1760 static void
1761 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1763 if (TREE_CODE (e) == COND_EXPR)
1765 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1766 pp_c_whitespace (pp);
1767 pp_question (pp);
1768 pp_c_whitespace (pp);
1769 pp_expression (pp, TREE_OPERAND (e, 1));
1770 pp_c_whitespace (pp);
1771 pp_colon (pp);
1772 pp_c_whitespace (pp);
1773 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1775 else
1776 pp_c_logical_or_expression (pp, e);
1780 /* assignment-expression:
1781 conditional-expression
1782 unary-expression assignment-operator assignment-expression
1784 assignment-expression: one of
1785 = *= /= %= += -= >>= <<= &= ^= |= */
1787 static void
1788 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1790 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1792 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1793 pp_c_whitespace (pp);
1794 pp_equal (pp);
1795 pp_space (pp);
1796 pp_c_expression (pp, TREE_OPERAND (e, 1));
1798 else
1799 pp_c_conditional_expression (pp, e);
1802 /* expression:
1803 assignment-expression
1804 expression , assignment-expression
1806 Implementation note: instead of going through the usual recursion
1807 chain, I take the liberty of dispatching nodes to the appropriate
1808 functions. This makes some redundancy, but it worths it. That also
1809 prevents a possible infinite recursion between pp_c_primary_expression ()
1810 and pp_c_expression (). */
1812 void
1813 pp_c_expression (c_pretty_printer *pp, tree e)
1815 switch (TREE_CODE (e))
1817 case INTEGER_CST:
1818 pp_c_integer_constant (pp, e);
1819 break;
1821 case REAL_CST:
1822 pp_c_floating_constant (pp, e);
1823 break;
1825 case STRING_CST:
1826 pp_c_string_literal (pp, e);
1827 break;
1829 case IDENTIFIER_NODE:
1830 case FUNCTION_DECL:
1831 case VAR_DECL:
1832 case CONST_DECL:
1833 case PARM_DECL:
1834 case RESULT_DECL:
1835 case FIELD_DECL:
1836 case LABEL_DECL:
1837 case ERROR_MARK:
1838 pp_primary_expression (pp, e);
1839 break;
1841 case POSTINCREMENT_EXPR:
1842 case POSTDECREMENT_EXPR:
1843 case ARRAY_REF:
1844 case CALL_EXPR:
1845 case COMPONENT_REF:
1846 case COMPLEX_CST:
1847 case COMPLEX_EXPR:
1848 case VECTOR_CST:
1849 case ORDERED_EXPR:
1850 case UNORDERED_EXPR:
1851 case LTGT_EXPR:
1852 case UNEQ_EXPR:
1853 case UNLE_EXPR:
1854 case UNLT_EXPR:
1855 case UNGE_EXPR:
1856 case UNGT_EXPR:
1857 case ABS_EXPR:
1858 case CONSTRUCTOR:
1859 case COMPOUND_LITERAL_EXPR:
1860 case VA_ARG_EXPR:
1861 pp_postfix_expression (pp, e);
1862 break;
1864 case CONJ_EXPR:
1865 case ADDR_EXPR:
1866 case INDIRECT_REF:
1867 case NEGATE_EXPR:
1868 case BIT_NOT_EXPR:
1869 case TRUTH_NOT_EXPR:
1870 case PREINCREMENT_EXPR:
1871 case PREDECREMENT_EXPR:
1872 case REALPART_EXPR:
1873 case IMAGPART_EXPR:
1874 pp_c_unary_expression (pp, e);
1875 break;
1877 case FLOAT_EXPR:
1878 case FIX_TRUNC_EXPR:
1879 case CONVERT_EXPR:
1880 case NOP_EXPR:
1881 pp_c_cast_expression (pp, e);
1882 break;
1884 case MULT_EXPR:
1885 case TRUNC_MOD_EXPR:
1886 case TRUNC_DIV_EXPR:
1887 pp_multiplicative_expression (pp, e);
1888 break;
1890 case LSHIFT_EXPR:
1891 case RSHIFT_EXPR:
1892 pp_c_shift_expression (pp, e);
1893 break;
1895 case LT_EXPR:
1896 case GT_EXPR:
1897 case LE_EXPR:
1898 case GE_EXPR:
1899 pp_c_relational_expression (pp, e);
1900 break;
1902 case BIT_AND_EXPR:
1903 pp_c_and_expression (pp, e);
1904 break;
1906 case BIT_XOR_EXPR:
1907 pp_c_exclusive_or_expression (pp, e);
1908 break;
1910 case BIT_IOR_EXPR:
1911 pp_c_inclusive_or_expression (pp, e);
1912 break;
1914 case TRUTH_ANDIF_EXPR:
1915 pp_c_logical_and_expression (pp, e);
1916 break;
1918 case TRUTH_ORIF_EXPR:
1919 pp_c_logical_or_expression (pp, e);
1920 break;
1922 case EQ_EXPR:
1923 case NE_EXPR:
1924 pp_c_equality_expression (pp, e);
1925 break;
1927 case COND_EXPR:
1928 pp_conditional_expression (pp, e);
1929 break;
1931 case PLUS_EXPR:
1932 case MINUS_EXPR:
1933 pp_c_additive_expression (pp, e);
1934 break;
1936 case MODIFY_EXPR:
1937 case INIT_EXPR:
1938 pp_assignment_expression (pp, e);
1939 break;
1941 case COMPOUND_EXPR:
1942 pp_c_left_paren (pp);
1943 pp_expression (pp, TREE_OPERAND (e, 0));
1944 pp_separate_with (pp, ',');
1945 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
1946 pp_c_right_paren (pp);
1947 break;
1949 case NON_LVALUE_EXPR:
1950 case SAVE_EXPR:
1951 pp_expression (pp, TREE_OPERAND (e, 0));
1952 break;
1954 case TARGET_EXPR:
1955 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
1956 break;
1958 default:
1959 pp_unsupported_tree (pp, e);
1960 break;
1966 /* Statements. */
1968 void
1969 pp_c_statement (c_pretty_printer *pp, tree stmt)
1971 if (stmt == NULL)
1972 return;
1974 if (pp_needs_newline (pp))
1975 pp_newline_and_indent (pp, 0);
1977 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
1981 /* Initialize the PRETTY-PRINTER for handling C codes. */
1983 void
1984 pp_c_pretty_printer_init (c_pretty_printer *pp)
1986 pp->offset_list = 0;
1988 pp->declaration = pp_c_declaration;
1989 pp->declaration_specifiers = pp_c_declaration_specifiers;
1990 pp->declarator = pp_c_declarator;
1991 pp->direct_declarator = pp_c_direct_declarator;
1992 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
1993 pp->abstract_declarator = pp_c_abstract_declarator;
1994 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
1995 pp->ptr_operator = pp_c_pointer;
1996 pp->parameter_list = pp_c_parameter_type_list;
1997 pp->type_id = pp_c_type_id;
1998 pp->simple_type_specifier = pp_c_type_specifier;
1999 pp->function_specifier = pp_c_function_specifier;
2000 pp->storage_class_specifier = pp_c_storage_class_specifier;
2002 pp->statement = pp_c_statement;
2004 pp->constant = pp_c_constant;
2005 pp->id_expression = pp_c_id_expression;
2006 pp->primary_expression = pp_c_primary_expression;
2007 pp->postfix_expression = pp_c_postfix_expression;
2008 pp->unary_expression = pp_c_unary_expression;
2009 pp->initializer = pp_c_initializer;
2010 pp->multiplicative_expression = pp_c_multiplicative_expression;
2011 pp->conditional_expression = pp_c_conditional_expression;
2012 pp->assignment_expression = pp_c_assignment_expression;
2013 pp->expression = pp_c_expression;
2017 /* Print the tree T in full, on file FILE. */
2019 void
2020 print_c_tree (FILE *file, tree t)
2022 static c_pretty_printer pp_rec;
2023 static bool initialized = 0;
2024 c_pretty_printer *pp = &pp_rec;
2026 if (!initialized)
2028 initialized = 1;
2029 pp_construct (pp_base (pp), NULL, 0);
2030 pp_c_pretty_printer_init (pp);
2031 pp_needs_newline (pp) = true;
2033 pp_base (pp)->buffer->stream = file;
2035 pp_statement (pp, t);
2037 pp_newline (pp);
2038 pp_flush (pp);
2041 /* Print the tree T in full, on stderr. */
2043 void
2044 debug_c_tree (tree t)
2046 print_c_tree (stderr, t);
2047 fputc ('\n', stderr);
2050 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2051 up of T's memory address. */
2053 void
2054 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2056 const char *name;
2058 gcc_assert (DECL_P (t));
2060 if (DECL_NAME (t))
2061 name = IDENTIFIER_POINTER (DECL_NAME (t));
2062 else
2064 static char xname[8];
2065 sprintf (xname, "<U%4x>", ((unsigned)((unsigned long)(t) & 0xffff)));
2066 name = xname;
2069 pp_c_identifier (pp, name);