gcc/ChangeLog:
[official-gcc.git] / gcc / c-family / c-pretty-print.c
blob7f4b2388f43e1fb53cbccadcf1cbfae7202b4cf4
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "intl.h"
28 #include "c-pretty-print.h"
29 #include "tree-pretty-print.h"
30 #include "tree-iterator.h"
31 #include "diagnostic.h"
33 /* Translate if being used for diagnostics, but not for dump files or
34 __PRETTY_FUNCTION. */
35 #define M_(msgid) (pp_translate_identifiers (pp) ? _(msgid) : (msgid))
37 /* The pretty-printer code is primarily designed to closely follow
38 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
39 codes we used to have in the past. Following a structured
40 approach (preferably the official grammars) is believed to make it
41 much easier to add extensions and nifty pretty-printing effects that
42 takes expression or declaration contexts into account. */
45 #define pp_c_maybe_whitespace(PP) \
46 do { \
47 if (pp_base (PP)->padding == pp_before) \
48 pp_c_whitespace (PP); \
49 } while (0)
51 /* literal */
52 static void pp_c_char (c_pretty_printer *, int);
54 /* postfix-expression */
55 static void pp_c_initializer_list (c_pretty_printer *, tree);
56 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
58 static void pp_c_multiplicative_expression (c_pretty_printer *, tree);
59 static void pp_c_additive_expression (c_pretty_printer *, tree);
60 static void pp_c_shift_expression (c_pretty_printer *, tree);
61 static void pp_c_relational_expression (c_pretty_printer *, tree);
62 static void pp_c_equality_expression (c_pretty_printer *, tree);
63 static void pp_c_and_expression (c_pretty_printer *, tree);
64 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
65 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
66 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
67 static void pp_c_conditional_expression (c_pretty_printer *, tree);
68 static void pp_c_assignment_expression (c_pretty_printer *, tree);
70 /* declarations. */
73 /* Helper functions. */
75 void
76 pp_c_whitespace (c_pretty_printer *pp)
78 pp_space (pp);
79 pp_base (pp)->padding = pp_none;
82 void
83 pp_c_left_paren (c_pretty_printer *pp)
85 pp_left_paren (pp);
86 pp_base (pp)->padding = pp_none;
89 void
90 pp_c_right_paren (c_pretty_printer *pp)
92 pp_right_paren (pp);
93 pp_base (pp)->padding = pp_none;
96 void
97 pp_c_left_brace (c_pretty_printer *pp)
99 pp_left_brace (pp);
100 pp_base (pp)->padding = pp_none;
103 void
104 pp_c_right_brace (c_pretty_printer *pp)
106 pp_right_brace (pp);
107 pp_base (pp)->padding = pp_none;
110 void
111 pp_c_left_bracket (c_pretty_printer *pp)
113 pp_left_bracket (pp);
114 pp_base (pp)->padding = pp_none;
117 void
118 pp_c_right_bracket (c_pretty_printer *pp)
120 pp_right_bracket (pp);
121 pp_base (pp)->padding = pp_none;
124 void
125 pp_c_dot (c_pretty_printer *pp)
127 pp_dot (pp);
128 pp_base (pp)->padding = pp_none;
131 void
132 pp_c_ampersand (c_pretty_printer *pp)
134 pp_ampersand (pp);
135 pp_base (pp)->padding = pp_none;
138 void
139 pp_c_star (c_pretty_printer *pp)
141 pp_star (pp);
142 pp_base (pp)->padding = pp_none;
145 void
146 pp_c_arrow (c_pretty_printer *pp)
148 pp_arrow (pp);
149 pp_base (pp)->padding = pp_none;
152 void
153 pp_c_semicolon (c_pretty_printer *pp)
155 pp_semicolon (pp);
156 pp_base (pp)->padding = pp_none;
159 void
160 pp_c_complement (c_pretty_printer *pp)
162 pp_complement (pp);
163 pp_base (pp)->padding = pp_none;
166 void
167 pp_c_exclamation (c_pretty_printer *pp)
169 pp_exclamation (pp);
170 pp_base (pp)->padding = pp_none;
173 /* Print out the external representation of QUALIFIERS. */
175 void
176 pp_c_cv_qualifiers (c_pretty_printer *pp, int qualifiers, bool func_type)
178 const char *p = pp_last_position_in_text (pp);
179 bool previous = false;
181 if (!qualifiers)
182 return;
184 /* The C programming language does not have references, but it is much
185 simpler to handle those here rather than going through the same
186 logic in the C++ pretty-printer. */
187 if (p != NULL && (*p == '*' || *p == '&'))
188 pp_c_whitespace (pp);
190 if (qualifiers & TYPE_QUAL_CONST)
192 pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const");
193 previous = true;
196 if (qualifiers & TYPE_QUAL_VOLATILE)
198 if (previous)
199 pp_c_whitespace (pp);
200 pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile");
201 previous = true;
204 if (qualifiers & TYPE_QUAL_RESTRICT)
206 if (previous)
207 pp_c_whitespace (pp);
208 pp_c_ws_string (pp, flag_isoc99 ? "restrict" : "__restrict__");
212 /* Pretty-print T using the type-cast notation '( type-name )'. */
214 static void
215 pp_c_type_cast (c_pretty_printer *pp, tree t)
217 pp_c_left_paren (pp);
218 pp_type_id (pp, t);
219 pp_c_right_paren (pp);
222 /* We're about to pretty-print a pointer type as indicated by T.
223 Output a whitespace, if needed, preparing for subsequent output. */
225 void
226 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
228 if (POINTER_TYPE_P (t))
230 tree pointee = strip_pointer_operator (TREE_TYPE (t));
231 if (TREE_CODE (pointee) != ARRAY_TYPE
232 && TREE_CODE (pointee) != FUNCTION_TYPE)
233 pp_c_whitespace (pp);
238 /* Declarations. */
240 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
241 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
242 of its type. Take care of possible extensions.
244 type-qualifier-list:
245 type-qualifier
246 type-qualifier-list type-qualifier
248 type-qualifier:
249 const
250 restrict -- C99
251 __restrict__ -- GNU C
252 address-space-qualifier -- GNU C
253 volatile
255 address-space-qualifier:
256 identifier -- GNU C */
258 void
259 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
261 int qualifiers;
263 if (!t || t == error_mark_node)
264 return;
266 if (!TYPE_P (t))
267 t = TREE_TYPE (t);
269 qualifiers = TYPE_QUALS (t);
270 pp_c_cv_qualifiers (pp, qualifiers,
271 TREE_CODE (t) == FUNCTION_TYPE);
273 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
275 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
276 pp_c_identifier (pp, as);
280 /* pointer:
281 * type-qualifier-list(opt)
282 * type-qualifier-list(opt) pointer */
284 static void
285 pp_c_pointer (c_pretty_printer *pp, tree t)
287 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
288 t = TREE_TYPE (t);
289 switch (TREE_CODE (t))
291 case POINTER_TYPE:
292 /* It is easier to handle C++ reference types here. */
293 case REFERENCE_TYPE:
294 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
295 pp_c_pointer (pp, TREE_TYPE (t));
296 if (TREE_CODE (t) == POINTER_TYPE)
297 pp_c_star (pp);
298 else
299 pp_c_ampersand (pp);
300 pp_c_type_qualifier_list (pp, t);
301 break;
303 /* ??? This node is now in GENERIC and so shouldn't be here. But
304 we'll fix that later. */
305 case DECL_EXPR:
306 pp_declaration (pp, DECL_EXPR_DECL (t));
307 pp_needs_newline (pp) = true;
308 break;
310 default:
311 pp_unsupported_tree (pp, t);
315 /* type-specifier:
316 void
317 char
318 short
320 long
321 float
322 double
323 signed
324 unsigned
325 _Bool -- C99
326 _Complex -- C99
327 _Imaginary -- C99
328 struct-or-union-specifier
329 enum-specifier
330 typedef-name.
332 GNU extensions.
333 simple-type-specifier:
334 __complex__
335 __vector__ */
337 void
338 pp_c_type_specifier (c_pretty_printer *pp, tree t)
340 const enum tree_code code = TREE_CODE (t);
341 switch (code)
343 case ERROR_MARK:
344 pp_c_ws_string (pp, M_("<type-error>"));
345 break;
347 case IDENTIFIER_NODE:
348 pp_c_tree_decl_identifier (pp, t);
349 break;
351 case VOID_TYPE:
352 case BOOLEAN_TYPE:
353 case INTEGER_TYPE:
354 case REAL_TYPE:
355 case FIXED_POINT_TYPE:
356 if (TYPE_NAME (t))
358 t = TYPE_NAME (t);
359 pp_c_type_specifier (pp, t);
361 else
363 int prec = TYPE_PRECISION (t);
364 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
365 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_SATURATING (t));
366 else
367 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
368 if (TYPE_NAME (t))
370 pp_c_type_specifier (pp, t);
371 if (TYPE_PRECISION (t) != prec)
373 pp_string (pp, ":");
374 pp_decimal_int (pp, prec);
377 else
379 switch (code)
381 case INTEGER_TYPE:
382 pp_string (pp, (TYPE_UNSIGNED (t)
383 ? M_("<unnamed-unsigned:")
384 : M_("<unnamed-signed:")));
385 break;
386 case REAL_TYPE:
387 pp_string (pp, M_("<unnamed-float:"));
388 break;
389 case FIXED_POINT_TYPE:
390 pp_string (pp, M_("<unnamed-fixed:"));
391 break;
392 default:
393 gcc_unreachable ();
395 pp_decimal_int (pp, prec);
396 pp_string (pp, ">");
399 break;
401 case TYPE_DECL:
402 if (DECL_NAME (t))
403 pp_id_expression (pp, t);
404 else
405 pp_c_ws_string (pp, M_("<typedef-error>"));
406 break;
408 case UNION_TYPE:
409 case RECORD_TYPE:
410 case ENUMERAL_TYPE:
411 if (code == UNION_TYPE)
412 pp_c_ws_string (pp, "union");
413 else if (code == RECORD_TYPE)
414 pp_c_ws_string (pp, "struct");
415 else if (code == ENUMERAL_TYPE)
416 pp_c_ws_string (pp, "enum");
417 else
418 pp_c_ws_string (pp, M_("<tag-error>"));
420 if (TYPE_NAME (t))
421 pp_id_expression (pp, TYPE_NAME (t));
422 else
423 pp_c_ws_string (pp, M_("<anonymous>"));
424 break;
426 default:
427 pp_unsupported_tree (pp, t);
428 break;
432 /* specifier-qualifier-list:
433 type-specifier specifier-qualifier-list-opt
434 type-qualifier specifier-qualifier-list-opt
437 Implementation note: Because of the non-linearities in array or
438 function declarations, this routine prints not just the
439 specifier-qualifier-list of such entities or types of such entities,
440 but also the 'pointer' production part of their declarators. The
441 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
443 void
444 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
446 const enum tree_code code = TREE_CODE (t);
448 if (TREE_CODE (t) != POINTER_TYPE)
449 pp_c_type_qualifier_list (pp, t);
450 switch (code)
452 case REFERENCE_TYPE:
453 case POINTER_TYPE:
455 /* Get the types-specifier of this type. */
456 tree pointee = strip_pointer_operator (TREE_TYPE (t));
457 pp_c_specifier_qualifier_list (pp, pointee);
458 if (TREE_CODE (pointee) == ARRAY_TYPE
459 || TREE_CODE (pointee) == FUNCTION_TYPE)
461 pp_c_whitespace (pp);
462 pp_c_left_paren (pp);
464 else if (!c_dialect_cxx ())
465 pp_c_whitespace (pp);
466 pp_ptr_operator (pp, t);
468 break;
470 case FUNCTION_TYPE:
471 case ARRAY_TYPE:
472 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
473 break;
475 case VECTOR_TYPE:
476 case COMPLEX_TYPE:
477 if (code == COMPLEX_TYPE)
478 pp_c_ws_string (pp, flag_isoc99 ? "_Complex" : "__complex__");
479 else if (code == VECTOR_TYPE)
481 pp_c_ws_string (pp, "__vector");
482 pp_c_left_paren (pp);
483 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
484 pp_c_right_paren (pp);
485 pp_c_whitespace (pp);
487 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
488 break;
490 default:
491 pp_simple_type_specifier (pp, t);
492 break;
496 /* parameter-type-list:
497 parameter-list
498 parameter-list , ...
500 parameter-list:
501 parameter-declaration
502 parameter-list , parameter-declaration
504 parameter-declaration:
505 declaration-specifiers declarator
506 declaration-specifiers abstract-declarator(opt) */
508 void
509 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
511 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
512 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
513 pp_c_left_paren (pp);
514 if (parms == void_list_node)
515 pp_c_ws_string (pp, "void");
516 else
518 bool first = true;
519 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
521 if (!first)
522 pp_separate_with (pp, ',');
523 first = false;
524 pp_declaration_specifiers
525 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
526 if (want_parm_decl)
527 pp_declarator (pp, parms);
528 else
529 pp_abstract_declarator (pp, TREE_VALUE (parms));
532 pp_c_right_paren (pp);
535 /* abstract-declarator:
536 pointer
537 pointer(opt) direct-abstract-declarator */
539 static void
540 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
542 if (TREE_CODE (t) == POINTER_TYPE)
544 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
545 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
546 pp_c_right_paren (pp);
547 t = TREE_TYPE (t);
550 pp_direct_abstract_declarator (pp, t);
553 /* direct-abstract-declarator:
554 ( abstract-declarator )
555 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
556 direct-abstract-declarator(opt) [ * ]
557 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
559 void
560 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
562 switch (TREE_CODE (t))
564 case POINTER_TYPE:
565 pp_abstract_declarator (pp, t);
566 break;
568 case FUNCTION_TYPE:
569 pp_c_parameter_type_list (pp, t);
570 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
571 break;
573 case ARRAY_TYPE:
574 pp_c_left_bracket (pp);
575 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
577 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
578 tree type = TREE_TYPE (maxval);
580 if (host_integerp (maxval, 0))
581 pp_wide_integer (pp, tree_low_cst (maxval, 0) + 1);
582 else
583 pp_expression (pp, fold_build2 (PLUS_EXPR, type, maxval,
584 build_int_cst (type, 1)));
586 pp_c_right_bracket (pp);
587 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
588 break;
590 case IDENTIFIER_NODE:
591 case VOID_TYPE:
592 case BOOLEAN_TYPE:
593 case INTEGER_TYPE:
594 case REAL_TYPE:
595 case FIXED_POINT_TYPE:
596 case ENUMERAL_TYPE:
597 case RECORD_TYPE:
598 case UNION_TYPE:
599 case VECTOR_TYPE:
600 case COMPLEX_TYPE:
601 case TYPE_DECL:
602 break;
604 default:
605 pp_unsupported_tree (pp, t);
606 break;
610 /* type-name:
611 specifier-qualifier-list abstract-declarator(opt) */
613 void
614 pp_c_type_id (c_pretty_printer *pp, tree t)
616 pp_c_specifier_qualifier_list (pp, t);
617 pp_abstract_declarator (pp, t);
620 /* storage-class-specifier:
621 typedef
622 extern
623 static
624 auto
625 register */
627 void
628 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
630 if (TREE_CODE (t) == TYPE_DECL)
631 pp_c_ws_string (pp, "typedef");
632 else if (DECL_P (t))
634 if (DECL_REGISTER (t))
635 pp_c_ws_string (pp, "register");
636 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
637 pp_c_ws_string (pp, "static");
641 /* function-specifier:
642 inline */
644 void
645 pp_c_function_specifier (c_pretty_printer *pp, tree t)
647 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
648 pp_c_ws_string (pp, "inline");
651 /* declaration-specifiers:
652 storage-class-specifier declaration-specifiers(opt)
653 type-specifier declaration-specifiers(opt)
654 type-qualifier declaration-specifiers(opt)
655 function-specifier declaration-specifiers(opt) */
657 void
658 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
660 pp_storage_class_specifier (pp, t);
661 pp_function_specifier (pp, t);
662 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
665 /* direct-declarator
666 identifier
667 ( declarator )
668 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
669 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
670 direct-declarator [ type-qualifier-list static assignment-expression ]
671 direct-declarator [ type-qualifier-list * ]
672 direct-declarator ( parameter-type-list )
673 direct-declarator ( identifier-list(opt) ) */
675 void
676 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
678 switch (TREE_CODE (t))
680 case VAR_DECL:
681 case PARM_DECL:
682 case TYPE_DECL:
683 case FIELD_DECL:
684 case LABEL_DECL:
685 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
686 pp_c_tree_decl_identifier (pp, t);
687 break;
689 case ARRAY_TYPE:
690 case POINTER_TYPE:
691 pp_abstract_declarator (pp, TREE_TYPE (t));
692 break;
694 case FUNCTION_TYPE:
695 pp_parameter_list (pp, t);
696 pp_abstract_declarator (pp, TREE_TYPE (t));
697 break;
699 case FUNCTION_DECL:
700 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
701 pp_c_tree_decl_identifier (pp, t);
702 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
703 pp_abstract_declarator (pp, TREE_TYPE (t));
704 else
706 pp_parameter_list (pp, t);
707 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
709 break;
711 case INTEGER_TYPE:
712 case REAL_TYPE:
713 case FIXED_POINT_TYPE:
714 case ENUMERAL_TYPE:
715 case UNION_TYPE:
716 case RECORD_TYPE:
717 break;
719 default:
720 pp_unsupported_tree (pp, t);
721 break;
726 /* declarator:
727 pointer(opt) direct-declarator */
729 void
730 pp_c_declarator (c_pretty_printer *pp, tree t)
732 switch (TREE_CODE (t))
734 case INTEGER_TYPE:
735 case REAL_TYPE:
736 case FIXED_POINT_TYPE:
737 case ENUMERAL_TYPE:
738 case UNION_TYPE:
739 case RECORD_TYPE:
740 break;
742 case VAR_DECL:
743 case PARM_DECL:
744 case FIELD_DECL:
745 case ARRAY_TYPE:
746 case FUNCTION_TYPE:
747 case FUNCTION_DECL:
748 case TYPE_DECL:
749 pp_direct_declarator (pp, t);
750 break;
753 default:
754 pp_unsupported_tree (pp, t);
755 break;
759 /* declaration:
760 declaration-specifiers init-declarator-list(opt) ; */
762 void
763 pp_c_declaration (c_pretty_printer *pp, tree t)
765 pp_declaration_specifiers (pp, t);
766 pp_c_init_declarator (pp, t);
769 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
771 void
772 pp_c_attributes (c_pretty_printer *pp, tree attributes)
774 if (attributes == NULL_TREE)
775 return;
777 pp_c_ws_string (pp, "__attribute__");
778 pp_c_left_paren (pp);
779 pp_c_left_paren (pp);
780 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
782 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
783 if (TREE_VALUE (attributes))
784 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
786 if (TREE_CHAIN (attributes))
787 pp_separate_with (pp, ',');
789 pp_c_right_paren (pp);
790 pp_c_right_paren (pp);
793 /* function-definition:
794 declaration-specifiers declarator compound-statement */
796 void
797 pp_c_function_definition (c_pretty_printer *pp, tree t)
799 pp_declaration_specifiers (pp, t);
800 pp_declarator (pp, t);
801 pp_needs_newline (pp) = true;
802 pp_statement (pp, DECL_SAVED_TREE (t));
803 pp_newline (pp);
804 pp_flush (pp);
808 /* Expressions. */
810 /* Print out a c-char. This is called solely for characters which are
811 in the *target* execution character set. We ought to convert them
812 back to the *host* execution character set before printing, but we
813 have no way to do this at present. A decent compromise is to print
814 all characters as if they were in the host execution character set,
815 and not attempt to recover any named escape characters, but render
816 all unprintables as octal escapes. If the host and target character
817 sets are the same, this produces relatively readable output. If they
818 are not the same, strings may appear as gibberish, but that's okay
819 (in fact, it may well be what the reader wants, e.g. if they are looking
820 to see if conversion to the target character set happened correctly).
822 A special case: we need to prefix \, ", and ' with backslashes. It is
823 correct to do so for the *host*'s \, ", and ', because the rest of the
824 file appears in the host character set. */
826 static void
827 pp_c_char (c_pretty_printer *pp, int c)
829 if (ISPRINT (c))
831 switch (c)
833 case '\\': pp_string (pp, "\\\\"); break;
834 case '\'': pp_string (pp, "\\\'"); break;
835 case '\"': pp_string (pp, "\\\""); break;
836 default: pp_character (pp, c);
839 else
840 pp_scalar (pp, "\\%03o", (unsigned) c);
843 /* Print out a STRING literal. */
845 void
846 pp_c_string_literal (c_pretty_printer *pp, tree s)
848 const char *p = TREE_STRING_POINTER (s);
849 int n = TREE_STRING_LENGTH (s) - 1;
850 int i;
851 pp_doublequote (pp);
852 for (i = 0; i < n; ++i)
853 pp_c_char (pp, p[i]);
854 pp_doublequote (pp);
857 /* Pretty-print an INTEGER literal. */
859 static void
860 pp_c_integer_constant (c_pretty_printer *pp, tree i)
862 tree type = TREE_TYPE (i);
864 if (TREE_INT_CST_HIGH (i) == 0)
865 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
866 else
868 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (i);
869 HOST_WIDE_INT high = TREE_INT_CST_HIGH (i);
870 if (tree_int_cst_sgn (i) < 0)
872 pp_character (pp, '-');
873 high = ~high + !low;
874 low = -low;
876 sprintf (pp_buffer (pp)->digit_buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
877 (unsigned HOST_WIDE_INT) high, (unsigned HOST_WIDE_INT) low);
878 pp_string (pp, pp_buffer (pp)->digit_buffer);
880 if (TYPE_UNSIGNED (type))
881 pp_character (pp, 'u');
882 if (type == long_integer_type_node || type == long_unsigned_type_node)
883 pp_character (pp, 'l');
884 else if (type == long_long_integer_type_node
885 || type == long_long_unsigned_type_node)
886 pp_string (pp, "ll");
887 else if (type == int128_integer_type_node
888 || type == int128_unsigned_type_node)
889 pp_string (pp, "I128");
892 /* Print out a CHARACTER literal. */
894 static void
895 pp_c_character_constant (c_pretty_printer *pp, tree c)
897 tree type = TREE_TYPE (c);
898 if (type == wchar_type_node)
899 pp_character (pp, 'L');
900 pp_quote (pp);
901 if (host_integerp (c, TYPE_UNSIGNED (type)))
902 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
903 else
904 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
905 pp_quote (pp);
908 /* Print out a BOOLEAN literal. */
910 static void
911 pp_c_bool_constant (c_pretty_printer *pp, tree b)
913 if (b == boolean_false_node)
915 if (c_dialect_cxx ())
916 pp_c_ws_string (pp, "false");
917 else if (flag_isoc99)
918 pp_c_ws_string (pp, "_False");
919 else
920 pp_unsupported_tree (pp, b);
922 else if (b == boolean_true_node)
924 if (c_dialect_cxx ())
925 pp_c_ws_string (pp, "true");
926 else if (flag_isoc99)
927 pp_c_ws_string (pp, "_True");
928 else
929 pp_unsupported_tree (pp, b);
931 else if (TREE_CODE (b) == INTEGER_CST)
932 pp_c_integer_constant (pp, b);
933 else
934 pp_unsupported_tree (pp, b);
937 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
938 false; that means the value was obtained by a cast, in which case
939 print out the type-id part of the cast-expression -- the casted value
940 is then printed by pp_c_integer_literal. */
942 static bool
943 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
945 bool value_is_named = true;
946 tree type = TREE_TYPE (e);
947 tree value;
949 /* Find the name of this constant. */
950 for (value = TYPE_VALUES (type);
951 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
952 value = TREE_CHAIN (value))
955 if (value != NULL_TREE)
956 pp_id_expression (pp, TREE_PURPOSE (value));
957 else
959 /* Value must have been cast. */
960 pp_c_type_cast (pp, type);
961 value_is_named = false;
964 return value_is_named;
967 /* Print out a REAL value as a decimal-floating-constant. */
969 static void
970 pp_c_floating_constant (c_pretty_printer *pp, tree r)
972 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
973 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
974 pp_string (pp, pp_buffer(pp)->digit_buffer);
975 if (TREE_TYPE (r) == float_type_node)
976 pp_character (pp, 'f');
977 else if (TREE_TYPE (r) == long_double_type_node)
978 pp_character (pp, 'l');
979 else if (TREE_TYPE (r) == dfloat128_type_node)
980 pp_string (pp, "dl");
981 else if (TREE_TYPE (r) == dfloat64_type_node)
982 pp_string (pp, "dd");
983 else if (TREE_TYPE (r) == dfloat32_type_node)
984 pp_string (pp, "df");
987 /* Print out a FIXED value as a decimal-floating-constant. */
989 static void
990 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
992 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
993 sizeof (pp_buffer (pp)->digit_buffer));
994 pp_string (pp, pp_buffer(pp)->digit_buffer);
997 /* Pretty-print a compound literal expression. GNU extensions include
998 vector constants. */
1000 static void
1001 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1003 tree type = TREE_TYPE (e);
1004 pp_c_type_cast (pp, type);
1006 switch (TREE_CODE (type))
1008 case RECORD_TYPE:
1009 case UNION_TYPE:
1010 case ARRAY_TYPE:
1011 case VECTOR_TYPE:
1012 case COMPLEX_TYPE:
1013 pp_c_brace_enclosed_initializer_list (pp, e);
1014 break;
1016 default:
1017 pp_unsupported_tree (pp, e);
1018 break;
1022 /* Pretty-print a COMPLEX_EXPR expression. */
1024 static void
1025 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1027 /* Handle a few common special cases, otherwise fallback
1028 to printing it as compound literal. */
1029 tree type = TREE_TYPE (e);
1030 tree realexpr = TREE_OPERAND (e, 0);
1031 tree imagexpr = TREE_OPERAND (e, 1);
1033 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1034 if (TREE_CODE (realexpr) == NOP_EXPR
1035 && TREE_CODE (imagexpr) == NOP_EXPR
1036 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1037 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1038 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1039 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1040 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1041 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1043 pp_c_type_cast (pp, type);
1044 pp_expression (pp, TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1045 return;
1048 /* Cast of an scalar expression to COMPLEX_TYPE. */
1049 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1050 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1052 pp_c_type_cast (pp, type);
1053 if (TREE_CODE (realexpr) == NOP_EXPR)
1054 realexpr = TREE_OPERAND (realexpr, 0);
1055 pp_expression (pp, realexpr);
1056 return;
1059 pp_c_compound_literal (pp, e);
1062 /* constant:
1063 integer-constant
1064 floating-constant
1065 fixed-point-constant
1066 enumeration-constant
1067 character-constant */
1069 void
1070 pp_c_constant (c_pretty_printer *pp, tree e)
1072 const enum tree_code code = TREE_CODE (e);
1074 switch (code)
1076 case INTEGER_CST:
1078 tree type = TREE_TYPE (e);
1079 if (type == boolean_type_node)
1080 pp_c_bool_constant (pp, e);
1081 else if (type == char_type_node)
1082 pp_c_character_constant (pp, e);
1083 else if (TREE_CODE (type) == ENUMERAL_TYPE
1084 && pp_c_enumeration_constant (pp, e))
1086 else
1087 pp_c_integer_constant (pp, e);
1089 break;
1091 case REAL_CST:
1092 pp_c_floating_constant (pp, e);
1093 break;
1095 case FIXED_CST:
1096 pp_c_fixed_constant (pp, e);
1097 break;
1099 case STRING_CST:
1100 pp_c_string_literal (pp, e);
1101 break;
1103 case COMPLEX_CST:
1104 /* Sometimes, we are confused and we think a complex literal
1105 is a constant. Such thing is a compound literal which
1106 grammatically belongs to postfix-expr production. */
1107 pp_c_compound_literal (pp, e);
1108 break;
1110 default:
1111 pp_unsupported_tree (pp, e);
1112 break;
1116 /* Pretty-print a string such as an identifier, without changing its
1117 encoding, preceded by whitespace is necessary. */
1119 void
1120 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1122 pp_c_maybe_whitespace (pp);
1123 pp_string (pp, str);
1124 pp_base (pp)->padding = pp_before;
1127 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1128 that need converting to the locale encoding, preceded by whitespace
1129 is necessary. */
1131 void
1132 pp_c_identifier (c_pretty_printer *pp, const char *id)
1134 pp_c_maybe_whitespace (pp);
1135 pp_identifier (pp, id);
1136 pp_base (pp)->padding = pp_before;
1139 /* Pretty-print a C primary-expression.
1140 primary-expression:
1141 identifier
1142 constant
1143 string-literal
1144 ( expression ) */
1146 void
1147 pp_c_primary_expression (c_pretty_printer *pp, tree e)
1149 switch (TREE_CODE (e))
1151 case VAR_DECL:
1152 case PARM_DECL:
1153 case FIELD_DECL:
1154 case CONST_DECL:
1155 case FUNCTION_DECL:
1156 case LABEL_DECL:
1157 pp_c_tree_decl_identifier (pp, e);
1158 break;
1160 case IDENTIFIER_NODE:
1161 pp_c_tree_identifier (pp, e);
1162 break;
1164 case ERROR_MARK:
1165 pp_c_ws_string (pp, M_("<erroneous-expression>"));
1166 break;
1168 case RESULT_DECL:
1169 pp_c_ws_string (pp, M_("<return-value>"));
1170 break;
1172 case INTEGER_CST:
1173 case REAL_CST:
1174 case FIXED_CST:
1175 case STRING_CST:
1176 pp_c_constant (pp, e);
1177 break;
1179 case TARGET_EXPR:
1180 pp_c_ws_string (pp, "__builtin_memcpy");
1181 pp_c_left_paren (pp);
1182 pp_ampersand (pp);
1183 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1184 pp_separate_with (pp, ',');
1185 pp_ampersand (pp);
1186 pp_initializer (pp, TREE_OPERAND (e, 1));
1187 if (TREE_OPERAND (e, 2))
1189 pp_separate_with (pp, ',');
1190 pp_c_expression (pp, TREE_OPERAND (e, 2));
1192 pp_c_right_paren (pp);
1193 break;
1195 default:
1196 /* FIXME: Make sure we won't get into an infinite loop. */
1197 pp_c_left_paren (pp);
1198 pp_expression (pp, e);
1199 pp_c_right_paren (pp);
1200 break;
1204 /* Print out a C initializer -- also support C compound-literals.
1205 initializer:
1206 assignment-expression:
1207 { initializer-list }
1208 { initializer-list , } */
1210 static void
1211 pp_c_initializer (c_pretty_printer *pp, tree e)
1213 if (TREE_CODE (e) == CONSTRUCTOR)
1214 pp_c_brace_enclosed_initializer_list (pp, e);
1215 else
1216 pp_expression (pp, e);
1219 /* init-declarator:
1220 declarator:
1221 declarator = initializer */
1223 void
1224 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1226 pp_declarator (pp, t);
1227 /* We don't want to output function definitions here. There are handled
1228 elsewhere (and the syntactic form is bogus anyway). */
1229 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1231 tree init = DECL_INITIAL (t);
1232 /* This C++ bit is handled here because it is easier to do so.
1233 In templates, the C++ parser builds a TREE_LIST for a
1234 direct-initialization; the TREE_PURPOSE is the variable to
1235 initialize and the TREE_VALUE is the initializer. */
1236 if (TREE_CODE (init) == TREE_LIST)
1238 pp_c_left_paren (pp);
1239 pp_expression (pp, TREE_VALUE (init));
1240 pp_right_paren (pp);
1242 else
1244 pp_space (pp);
1245 pp_equal (pp);
1246 pp_space (pp);
1247 pp_c_initializer (pp, init);
1252 /* initializer-list:
1253 designation(opt) initializer
1254 initializer-list , designation(opt) initializer
1256 designation:
1257 designator-list =
1259 designator-list:
1260 designator
1261 designator-list designator
1263 designator:
1264 [ constant-expression ]
1265 identifier */
1267 static void
1268 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1270 tree type = TREE_TYPE (e);
1271 const enum tree_code code = TREE_CODE (type);
1273 if (TREE_CODE (e) == CONSTRUCTOR)
1275 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1276 return;
1279 switch (code)
1281 case RECORD_TYPE:
1282 case UNION_TYPE:
1283 case ARRAY_TYPE:
1285 tree init = TREE_OPERAND (e, 0);
1286 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1288 if (code == RECORD_TYPE || code == UNION_TYPE)
1290 pp_c_dot (pp);
1291 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1293 else
1295 pp_c_left_bracket (pp);
1296 if (TREE_PURPOSE (init))
1297 pp_c_constant (pp, TREE_PURPOSE (init));
1298 pp_c_right_bracket (pp);
1300 pp_c_whitespace (pp);
1301 pp_equal (pp);
1302 pp_c_whitespace (pp);
1303 pp_initializer (pp, TREE_VALUE (init));
1304 if (TREE_CHAIN (init))
1305 pp_separate_with (pp, ',');
1308 return;
1310 case VECTOR_TYPE:
1311 if (TREE_CODE (e) == VECTOR_CST)
1312 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1313 else
1314 break;
1315 return;
1317 case COMPLEX_TYPE:
1318 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1320 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1321 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1322 pp_separate_with (pp, ',');
1323 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1325 else
1326 break;
1327 return;
1329 default:
1330 break;
1333 pp_unsupported_tree (pp, type);
1336 /* Pretty-print a brace-enclosed initializer-list. */
1338 static void
1339 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1341 pp_c_left_brace (pp);
1342 pp_c_initializer_list (pp, l);
1343 pp_c_right_brace (pp);
1347 /* This is a convenient function, used to bridge gap between C and C++
1348 grammars.
1350 id-expression:
1351 identifier */
1353 void
1354 pp_c_id_expression (c_pretty_printer *pp, tree t)
1356 switch (TREE_CODE (t))
1358 case VAR_DECL:
1359 case PARM_DECL:
1360 case CONST_DECL:
1361 case TYPE_DECL:
1362 case FUNCTION_DECL:
1363 case FIELD_DECL:
1364 case LABEL_DECL:
1365 pp_c_tree_decl_identifier (pp, t);
1366 break;
1368 case IDENTIFIER_NODE:
1369 pp_c_tree_identifier (pp, t);
1370 break;
1372 default:
1373 pp_unsupported_tree (pp, t);
1374 break;
1378 /* postfix-expression:
1379 primary-expression
1380 postfix-expression [ expression ]
1381 postfix-expression ( argument-expression-list(opt) )
1382 postfix-expression . identifier
1383 postfix-expression -> identifier
1384 postfix-expression ++
1385 postfix-expression --
1386 ( type-name ) { initializer-list }
1387 ( type-name ) { initializer-list , } */
1389 void
1390 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1392 enum tree_code code = TREE_CODE (e);
1393 switch (code)
1395 case POSTINCREMENT_EXPR:
1396 case POSTDECREMENT_EXPR:
1397 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1398 pp_string (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1399 break;
1401 case ARRAY_REF:
1402 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1403 pp_c_left_bracket (pp);
1404 pp_expression (pp, TREE_OPERAND (e, 1));
1405 pp_c_right_bracket (pp);
1406 break;
1408 case CALL_EXPR:
1410 call_expr_arg_iterator iter;
1411 tree arg;
1412 pp_postfix_expression (pp, CALL_EXPR_FN (e));
1413 pp_c_left_paren (pp);
1414 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1416 pp_expression (pp, arg);
1417 if (more_call_expr_args_p (&iter))
1418 pp_separate_with (pp, ',');
1420 pp_c_right_paren (pp);
1421 break;
1424 case UNORDERED_EXPR:
1425 pp_c_ws_string (pp, flag_isoc99
1426 ? "isunordered"
1427 : "__builtin_isunordered");
1428 goto two_args_fun;
1430 case ORDERED_EXPR:
1431 pp_c_ws_string (pp, flag_isoc99
1432 ? "!isunordered"
1433 : "!__builtin_isunordered");
1434 goto two_args_fun;
1436 case UNLT_EXPR:
1437 pp_c_ws_string (pp, flag_isoc99
1438 ? "!isgreaterequal"
1439 : "!__builtin_isgreaterequal");
1440 goto two_args_fun;
1442 case UNLE_EXPR:
1443 pp_c_ws_string (pp, flag_isoc99
1444 ? "!isgreater"
1445 : "!__builtin_isgreater");
1446 goto two_args_fun;
1448 case UNGT_EXPR:
1449 pp_c_ws_string (pp, flag_isoc99
1450 ? "!islessequal"
1451 : "!__builtin_islessequal");
1452 goto two_args_fun;
1454 case UNGE_EXPR:
1455 pp_c_ws_string (pp, flag_isoc99
1456 ? "!isless"
1457 : "!__builtin_isless");
1458 goto two_args_fun;
1460 case UNEQ_EXPR:
1461 pp_c_ws_string (pp, flag_isoc99
1462 ? "!islessgreater"
1463 : "!__builtin_islessgreater");
1464 goto two_args_fun;
1466 case LTGT_EXPR:
1467 pp_c_ws_string (pp, flag_isoc99
1468 ? "islessgreater"
1469 : "__builtin_islessgreater");
1470 goto two_args_fun;
1472 two_args_fun:
1473 pp_c_left_paren (pp);
1474 pp_expression (pp, TREE_OPERAND (e, 0));
1475 pp_separate_with (pp, ',');
1476 pp_expression (pp, TREE_OPERAND (e, 1));
1477 pp_c_right_paren (pp);
1478 break;
1480 case ABS_EXPR:
1481 pp_c_ws_string (pp, "__builtin_abs");
1482 pp_c_left_paren (pp);
1483 pp_expression (pp, TREE_OPERAND (e, 0));
1484 pp_c_right_paren (pp);
1485 break;
1487 case COMPONENT_REF:
1489 tree object = TREE_OPERAND (e, 0);
1490 if (TREE_CODE (object) == INDIRECT_REF)
1492 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1493 pp_c_arrow (pp);
1495 else
1497 pp_postfix_expression (pp, object);
1498 pp_c_dot (pp);
1500 pp_expression (pp, TREE_OPERAND (e, 1));
1502 break;
1504 case BIT_FIELD_REF:
1506 tree type = TREE_TYPE (e);
1508 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1509 if (type
1510 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1512 HOST_WIDE_INT bitpos = tree_low_cst (TREE_OPERAND (e, 2), 0);
1513 HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 0);
1514 if ((bitpos % size) == 0)
1516 pp_c_left_paren (pp);
1517 pp_c_left_paren (pp);
1518 pp_type_id (pp, type);
1519 pp_c_star (pp);
1520 pp_c_right_paren (pp);
1521 pp_c_ampersand (pp);
1522 pp_expression (pp, TREE_OPERAND (e, 0));
1523 pp_c_right_paren (pp);
1524 pp_c_left_bracket (pp);
1525 pp_wide_integer (pp, bitpos / size);
1526 pp_c_right_bracket (pp);
1527 break;
1530 pp_unsupported_tree (pp, e);
1532 break;
1534 case COMPLEX_CST:
1535 case VECTOR_CST:
1536 pp_c_compound_literal (pp, e);
1537 break;
1539 case COMPLEX_EXPR:
1540 pp_c_complex_expr (pp, e);
1541 break;
1543 case COMPOUND_LITERAL_EXPR:
1544 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1545 /* Fall through. */
1546 case CONSTRUCTOR:
1547 pp_initializer (pp, e);
1548 break;
1550 case VA_ARG_EXPR:
1551 pp_c_ws_string (pp, "__builtin_va_arg");
1552 pp_c_left_paren (pp);
1553 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1554 pp_separate_with (pp, ',');
1555 pp_type_id (pp, TREE_TYPE (e));
1556 pp_c_right_paren (pp);
1557 break;
1559 case ADDR_EXPR:
1560 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1562 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1563 break;
1565 /* else fall through. */
1567 default:
1568 pp_primary_expression (pp, e);
1569 break;
1573 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1575 void
1576 pp_c_expression_list (c_pretty_printer *pp, tree e)
1578 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1580 pp_expression (pp, TREE_VALUE (e));
1581 if (TREE_CHAIN (e))
1582 pp_separate_with (pp, ',');
1586 /* Print out V, which contains the elements of a constructor. */
1588 void
1589 pp_c_constructor_elts (c_pretty_printer *pp, VEC(constructor_elt,gc) *v)
1591 unsigned HOST_WIDE_INT ix;
1592 tree value;
1594 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1596 pp_expression (pp, value);
1597 if (ix != VEC_length (constructor_elt, v) - 1)
1598 pp_separate_with (pp, ',');
1602 /* Print out an expression-list in parens, as if it were the argument
1603 list to a function. */
1605 void
1606 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1608 pp_c_left_paren (pp);
1609 if (t && TREE_CODE (t) == TREE_LIST)
1610 pp_c_expression_list (pp, t);
1611 pp_c_right_paren (pp);
1614 /* unary-expression:
1615 postfix-expression
1616 ++ cast-expression
1617 -- cast-expression
1618 unary-operator cast-expression
1619 sizeof unary-expression
1620 sizeof ( type-id )
1622 unary-operator: one of
1623 * & + - ! ~
1625 GNU extensions.
1626 unary-expression:
1627 __alignof__ unary-expression
1628 __alignof__ ( type-id )
1629 __real__ unary-expression
1630 __imag__ unary-expression */
1632 void
1633 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1635 enum tree_code code = TREE_CODE (e);
1636 switch (code)
1638 case PREINCREMENT_EXPR:
1639 case PREDECREMENT_EXPR:
1640 pp_string (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1641 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1642 break;
1644 case ADDR_EXPR:
1645 case INDIRECT_REF:
1646 case NEGATE_EXPR:
1647 case BIT_NOT_EXPR:
1648 case TRUTH_NOT_EXPR:
1649 case CONJ_EXPR:
1650 /* String literal are used by address. */
1651 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1652 pp_ampersand (pp);
1653 else if (code == INDIRECT_REF)
1654 pp_c_star (pp);
1655 else if (code == NEGATE_EXPR)
1656 pp_minus (pp);
1657 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1658 pp_complement (pp);
1659 else if (code == TRUTH_NOT_EXPR)
1660 pp_exclamation (pp);
1661 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1662 break;
1664 case REALPART_EXPR:
1665 case IMAGPART_EXPR:
1666 pp_c_ws_string (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1667 pp_c_whitespace (pp);
1668 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1669 break;
1671 default:
1672 pp_postfix_expression (pp, e);
1673 break;
1677 /* cast-expression:
1678 unary-expression
1679 ( type-name ) cast-expression */
1681 void
1682 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1684 switch (TREE_CODE (e))
1686 case FLOAT_EXPR:
1687 case FIX_TRUNC_EXPR:
1688 CASE_CONVERT:
1689 case VIEW_CONVERT_EXPR:
1690 pp_c_type_cast (pp, TREE_TYPE (e));
1691 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1692 break;
1694 default:
1695 pp_unary_expression (pp, e);
1699 /* multiplicative-expression:
1700 cast-expression
1701 multiplicative-expression * cast-expression
1702 multiplicative-expression / cast-expression
1703 multiplicative-expression % cast-expression */
1705 static void
1706 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1708 enum tree_code code = TREE_CODE (e);
1709 switch (code)
1711 case MULT_EXPR:
1712 case TRUNC_DIV_EXPR:
1713 case TRUNC_MOD_EXPR:
1714 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1715 pp_c_whitespace (pp);
1716 if (code == MULT_EXPR)
1717 pp_c_star (pp);
1718 else if (code == TRUNC_DIV_EXPR)
1719 pp_slash (pp);
1720 else
1721 pp_modulo (pp);
1722 pp_c_whitespace (pp);
1723 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1724 break;
1726 default:
1727 pp_c_cast_expression (pp, e);
1728 break;
1732 /* additive-expression:
1733 multiplicative-expression
1734 additive-expression + multiplicative-expression
1735 additive-expression - multiplicative-expression */
1737 static void
1738 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1740 enum tree_code code = TREE_CODE (e);
1741 switch (code)
1743 case POINTER_PLUS_EXPR:
1744 case PLUS_EXPR:
1745 case MINUS_EXPR:
1746 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1747 pp_c_whitespace (pp);
1748 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1749 pp_plus (pp);
1750 else
1751 pp_minus (pp);
1752 pp_c_whitespace (pp);
1753 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1754 break;
1756 default:
1757 pp_multiplicative_expression (pp, e);
1758 break;
1762 /* additive-expression:
1763 additive-expression
1764 shift-expression << additive-expression
1765 shift-expression >> additive-expression */
1767 static void
1768 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1770 enum tree_code code = TREE_CODE (e);
1771 switch (code)
1773 case LSHIFT_EXPR:
1774 case RSHIFT_EXPR:
1775 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1776 pp_c_whitespace (pp);
1777 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1778 pp_c_whitespace (pp);
1779 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1780 break;
1782 default:
1783 pp_c_additive_expression (pp, e);
1787 /* relational-expression:
1788 shift-expression
1789 relational-expression < shift-expression
1790 relational-expression > shift-expression
1791 relational-expression <= shift-expression
1792 relational-expression >= shift-expression */
1794 static void
1795 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1797 enum tree_code code = TREE_CODE (e);
1798 switch (code)
1800 case LT_EXPR:
1801 case GT_EXPR:
1802 case LE_EXPR:
1803 case GE_EXPR:
1804 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1805 pp_c_whitespace (pp);
1806 if (code == LT_EXPR)
1807 pp_less (pp);
1808 else if (code == GT_EXPR)
1809 pp_greater (pp);
1810 else if (code == LE_EXPR)
1811 pp_string (pp, "<=");
1812 else if (code == GE_EXPR)
1813 pp_string (pp, ">=");
1814 pp_c_whitespace (pp);
1815 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1816 break;
1818 default:
1819 pp_c_shift_expression (pp, e);
1820 break;
1824 /* equality-expression:
1825 relational-expression
1826 equality-expression == relational-expression
1827 equality-equality != relational-expression */
1829 static void
1830 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1832 enum tree_code code = TREE_CODE (e);
1833 switch (code)
1835 case EQ_EXPR:
1836 case NE_EXPR:
1837 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1838 pp_c_whitespace (pp);
1839 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1840 pp_c_whitespace (pp);
1841 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1842 break;
1844 default:
1845 pp_c_relational_expression (pp, e);
1846 break;
1850 /* AND-expression:
1851 equality-expression
1852 AND-expression & equality-equality */
1854 static void
1855 pp_c_and_expression (c_pretty_printer *pp, tree e)
1857 if (TREE_CODE (e) == BIT_AND_EXPR)
1859 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1860 pp_c_whitespace (pp);
1861 pp_ampersand (pp);
1862 pp_c_whitespace (pp);
1863 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1865 else
1866 pp_c_equality_expression (pp, e);
1869 /* exclusive-OR-expression:
1870 AND-expression
1871 exclusive-OR-expression ^ AND-expression */
1873 static void
1874 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1876 if (TREE_CODE (e) == BIT_XOR_EXPR
1877 || TREE_CODE (e) == TRUTH_XOR_EXPR)
1879 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1880 if (TREE_CODE (e) == BIT_XOR_EXPR)
1881 pp_c_maybe_whitespace (pp);
1882 else
1883 pp_c_whitespace (pp);
1884 pp_carret (pp);
1885 pp_c_whitespace (pp);
1886 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1888 else
1889 pp_c_and_expression (pp, e);
1892 /* inclusive-OR-expression:
1893 exclusive-OR-expression
1894 inclusive-OR-expression | exclusive-OR-expression */
1896 static void
1897 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1899 if (TREE_CODE (e) == BIT_IOR_EXPR)
1901 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1902 pp_c_whitespace (pp);
1903 pp_bar (pp);
1904 pp_c_whitespace (pp);
1905 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1907 else
1908 pp_c_exclusive_or_expression (pp, e);
1911 /* logical-AND-expression:
1912 inclusive-OR-expression
1913 logical-AND-expression && inclusive-OR-expression */
1915 static void
1916 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1918 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
1919 || TREE_CODE (e) == TRUTH_AND_EXPR)
1921 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1922 pp_c_whitespace (pp);
1923 pp_string (pp, "&&");
1924 pp_c_whitespace (pp);
1925 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1927 else
1928 pp_c_inclusive_or_expression (pp, e);
1931 /* logical-OR-expression:
1932 logical-AND-expression
1933 logical-OR-expression || logical-AND-expression */
1935 void
1936 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1938 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
1939 || TREE_CODE (e) == TRUTH_OR_EXPR)
1941 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1942 pp_c_whitespace (pp);
1943 pp_string (pp, "||");
1944 pp_c_whitespace (pp);
1945 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1947 else
1948 pp_c_logical_and_expression (pp, e);
1951 /* conditional-expression:
1952 logical-OR-expression
1953 logical-OR-expression ? expression : conditional-expression */
1955 static void
1956 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1958 if (TREE_CODE (e) == COND_EXPR)
1960 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1961 pp_c_whitespace (pp);
1962 pp_question (pp);
1963 pp_c_whitespace (pp);
1964 pp_expression (pp, TREE_OPERAND (e, 1));
1965 pp_c_whitespace (pp);
1966 pp_colon (pp);
1967 pp_c_whitespace (pp);
1968 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1970 else
1971 pp_c_logical_or_expression (pp, e);
1975 /* assignment-expression:
1976 conditional-expression
1977 unary-expression assignment-operator assignment-expression
1979 assignment-expression: one of
1980 = *= /= %= += -= >>= <<= &= ^= |= */
1982 static void
1983 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1985 if (TREE_CODE (e) == MODIFY_EXPR
1986 || TREE_CODE (e) == INIT_EXPR)
1988 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1989 pp_c_whitespace (pp);
1990 pp_equal (pp);
1991 pp_space (pp);
1992 pp_c_expression (pp, TREE_OPERAND (e, 1));
1994 else
1995 pp_c_conditional_expression (pp, e);
1998 /* expression:
1999 assignment-expression
2000 expression , assignment-expression
2002 Implementation note: instead of going through the usual recursion
2003 chain, I take the liberty of dispatching nodes to the appropriate
2004 functions. This makes some redundancy, but it worths it. That also
2005 prevents a possible infinite recursion between pp_c_primary_expression ()
2006 and pp_c_expression (). */
2008 void
2009 pp_c_expression (c_pretty_printer *pp, tree e)
2011 switch (TREE_CODE (e))
2013 case INTEGER_CST:
2014 pp_c_integer_constant (pp, e);
2015 break;
2017 case REAL_CST:
2018 pp_c_floating_constant (pp, e);
2019 break;
2021 case FIXED_CST:
2022 pp_c_fixed_constant (pp, e);
2023 break;
2025 case STRING_CST:
2026 pp_c_string_literal (pp, e);
2027 break;
2029 case IDENTIFIER_NODE:
2030 case FUNCTION_DECL:
2031 case VAR_DECL:
2032 case CONST_DECL:
2033 case PARM_DECL:
2034 case RESULT_DECL:
2035 case FIELD_DECL:
2036 case LABEL_DECL:
2037 case ERROR_MARK:
2038 pp_primary_expression (pp, e);
2039 break;
2041 case POSTINCREMENT_EXPR:
2042 case POSTDECREMENT_EXPR:
2043 case ARRAY_REF:
2044 case CALL_EXPR:
2045 case COMPONENT_REF:
2046 case BIT_FIELD_REF:
2047 case COMPLEX_CST:
2048 case COMPLEX_EXPR:
2049 case VECTOR_CST:
2050 case ORDERED_EXPR:
2051 case UNORDERED_EXPR:
2052 case LTGT_EXPR:
2053 case UNEQ_EXPR:
2054 case UNLE_EXPR:
2055 case UNLT_EXPR:
2056 case UNGE_EXPR:
2057 case UNGT_EXPR:
2058 case ABS_EXPR:
2059 case CONSTRUCTOR:
2060 case COMPOUND_LITERAL_EXPR:
2061 case VA_ARG_EXPR:
2062 pp_postfix_expression (pp, e);
2063 break;
2065 case CONJ_EXPR:
2066 case ADDR_EXPR:
2067 case INDIRECT_REF:
2068 case NEGATE_EXPR:
2069 case BIT_NOT_EXPR:
2070 case TRUTH_NOT_EXPR:
2071 case PREINCREMENT_EXPR:
2072 case PREDECREMENT_EXPR:
2073 case REALPART_EXPR:
2074 case IMAGPART_EXPR:
2075 pp_c_unary_expression (pp, e);
2076 break;
2078 case FLOAT_EXPR:
2079 case FIX_TRUNC_EXPR:
2080 CASE_CONVERT:
2081 case VIEW_CONVERT_EXPR:
2082 pp_c_cast_expression (pp, e);
2083 break;
2085 case MULT_EXPR:
2086 case TRUNC_MOD_EXPR:
2087 case TRUNC_DIV_EXPR:
2088 pp_multiplicative_expression (pp, e);
2089 break;
2091 case LSHIFT_EXPR:
2092 case RSHIFT_EXPR:
2093 pp_c_shift_expression (pp, e);
2094 break;
2096 case LT_EXPR:
2097 case GT_EXPR:
2098 case LE_EXPR:
2099 case GE_EXPR:
2100 pp_c_relational_expression (pp, e);
2101 break;
2103 case BIT_AND_EXPR:
2104 pp_c_and_expression (pp, e);
2105 break;
2107 case BIT_XOR_EXPR:
2108 case TRUTH_XOR_EXPR:
2109 pp_c_exclusive_or_expression (pp, e);
2110 break;
2112 case BIT_IOR_EXPR:
2113 pp_c_inclusive_or_expression (pp, e);
2114 break;
2116 case TRUTH_ANDIF_EXPR:
2117 case TRUTH_AND_EXPR:
2118 pp_c_logical_and_expression (pp, e);
2119 break;
2121 case TRUTH_ORIF_EXPR:
2122 case TRUTH_OR_EXPR:
2123 pp_c_logical_or_expression (pp, e);
2124 break;
2126 case EQ_EXPR:
2127 case NE_EXPR:
2128 pp_c_equality_expression (pp, e);
2129 break;
2131 case COND_EXPR:
2132 pp_conditional_expression (pp, e);
2133 break;
2135 case POINTER_PLUS_EXPR:
2136 case PLUS_EXPR:
2137 case MINUS_EXPR:
2138 pp_c_additive_expression (pp, e);
2139 break;
2141 case MODIFY_EXPR:
2142 case INIT_EXPR:
2143 pp_assignment_expression (pp, e);
2144 break;
2146 case COMPOUND_EXPR:
2147 pp_c_left_paren (pp);
2148 pp_expression (pp, TREE_OPERAND (e, 0));
2149 pp_separate_with (pp, ',');
2150 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
2151 pp_c_right_paren (pp);
2152 break;
2154 case NON_LVALUE_EXPR:
2155 case SAVE_EXPR:
2156 pp_expression (pp, TREE_OPERAND (e, 0));
2157 break;
2159 case TARGET_EXPR:
2160 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
2161 break;
2163 case BIND_EXPR:
2164 case GOTO_EXPR:
2165 /* We don't yet have a way of dumping statements in a
2166 human-readable format. */
2167 pp_string (pp, "({...})");
2168 break;
2170 default:
2171 pp_unsupported_tree (pp, e);
2172 break;
2178 /* Statements. */
2180 void
2181 pp_c_statement (c_pretty_printer *pp, tree stmt)
2183 if (stmt == NULL)
2184 return;
2186 if (pp_needs_newline (pp))
2187 pp_newline_and_indent (pp, 0);
2189 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
2193 /* Initialize the PRETTY-PRINTER for handling C codes. */
2195 void
2196 pp_c_pretty_printer_init (c_pretty_printer *pp)
2198 pp->offset_list = 0;
2200 pp->declaration = pp_c_declaration;
2201 pp->declaration_specifiers = pp_c_declaration_specifiers;
2202 pp->declarator = pp_c_declarator;
2203 pp->direct_declarator = pp_c_direct_declarator;
2204 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
2205 pp->abstract_declarator = pp_c_abstract_declarator;
2206 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
2207 pp->ptr_operator = pp_c_pointer;
2208 pp->parameter_list = pp_c_parameter_type_list;
2209 pp->type_id = pp_c_type_id;
2210 pp->simple_type_specifier = pp_c_type_specifier;
2211 pp->function_specifier = pp_c_function_specifier;
2212 pp->storage_class_specifier = pp_c_storage_class_specifier;
2214 pp->statement = pp_c_statement;
2216 pp->constant = pp_c_constant;
2217 pp->id_expression = pp_c_id_expression;
2218 pp->primary_expression = pp_c_primary_expression;
2219 pp->postfix_expression = pp_c_postfix_expression;
2220 pp->unary_expression = pp_c_unary_expression;
2221 pp->initializer = pp_c_initializer;
2222 pp->multiplicative_expression = pp_c_multiplicative_expression;
2223 pp->conditional_expression = pp_c_conditional_expression;
2224 pp->assignment_expression = pp_c_assignment_expression;
2225 pp->expression = pp_c_expression;
2229 /* Print the tree T in full, on file FILE. */
2231 void
2232 print_c_tree (FILE *file, tree t)
2234 static c_pretty_printer pp_rec;
2235 static bool initialized = 0;
2236 c_pretty_printer *pp = &pp_rec;
2238 if (!initialized)
2240 initialized = 1;
2241 pp_construct (pp_base (pp), NULL, 0);
2242 pp_c_pretty_printer_init (pp);
2243 pp_needs_newline (pp) = true;
2245 pp_base (pp)->buffer->stream = file;
2247 pp_statement (pp, t);
2249 pp_newline (pp);
2250 pp_flush (pp);
2253 /* Print the tree T in full, on stderr. */
2255 DEBUG_FUNCTION void
2256 debug_c_tree (tree t)
2258 print_c_tree (stderr, t);
2259 fputc ('\n', stderr);
2262 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2263 up of T's memory address. */
2265 void
2266 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2268 const char *name;
2270 gcc_assert (DECL_P (t));
2272 if (DECL_NAME (t))
2273 name = IDENTIFIER_POINTER (DECL_NAME (t));
2274 else
2276 static char xname[8];
2277 sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
2278 name = xname;
2281 pp_c_identifier (pp, name);