* config/i386/i386.md (x86_shrd): Add athlon_decode and
[official-gcc/alias-decl.git] / gcc / c-pretty-print.c
blob12f1841dd16860f4bf2592e3154fdcdc3907917c
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 "real.h"
27 #include "fixed-value.h"
28 #include "intl.h"
29 #include "c-pretty-print.h"
30 #include "c-tree.h"
31 #include "tree-iterator.h"
32 #include "diagnostic.h"
34 /* Translate if being used for diagnostics, but not for dump files or
35 __PRETTY_FUNCTION. */
36 #define M_(msgid) (pp_translate_identifiers (pp) ? _(msgid) : (msgid))
38 /* The pretty-printer code is primarily designed to closely follow
39 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
40 codes we used to have in the past. Following a structured
41 approach (preferably the official grammars) is believed to make it
42 much easier to add extensions and nifty pretty-printing effects that
43 takes expression or declaration contexts into account. */
46 #define pp_c_maybe_whitespace(PP) \
47 do { \
48 if (pp_base (PP)->padding == pp_before) \
49 pp_c_whitespace (PP); \
50 } while (0)
52 /* literal */
53 static void pp_c_char (c_pretty_printer *, int);
55 /* postfix-expression */
56 static void pp_c_initializer_list (c_pretty_printer *, tree);
57 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
59 static void pp_c_multiplicative_expression (c_pretty_printer *, tree);
60 static void pp_c_additive_expression (c_pretty_printer *, tree);
61 static void pp_c_shift_expression (c_pretty_printer *, tree);
62 static void pp_c_relational_expression (c_pretty_printer *, tree);
63 static void pp_c_equality_expression (c_pretty_printer *, tree);
64 static void pp_c_and_expression (c_pretty_printer *, tree);
65 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
66 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
67 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
68 static void pp_c_conditional_expression (c_pretty_printer *, tree);
69 static void pp_c_assignment_expression (c_pretty_printer *, tree);
71 /* declarations. */
74 /* Helper functions. */
76 void
77 pp_c_whitespace (c_pretty_printer *pp)
79 pp_space (pp);
80 pp_base (pp)->padding = pp_none;
83 void
84 pp_c_left_paren (c_pretty_printer *pp)
86 pp_left_paren (pp);
87 pp_base (pp)->padding = pp_none;
90 void
91 pp_c_right_paren (c_pretty_printer *pp)
93 pp_right_paren (pp);
94 pp_base (pp)->padding = pp_none;
97 void
98 pp_c_left_brace (c_pretty_printer *pp)
100 pp_left_brace (pp);
101 pp_base (pp)->padding = pp_none;
104 void
105 pp_c_right_brace (c_pretty_printer *pp)
107 pp_right_brace (pp);
108 pp_base (pp)->padding = pp_none;
111 void
112 pp_c_left_bracket (c_pretty_printer *pp)
114 pp_left_bracket (pp);
115 pp_base (pp)->padding = pp_none;
118 void
119 pp_c_right_bracket (c_pretty_printer *pp)
121 pp_right_bracket (pp);
122 pp_base (pp)->padding = pp_none;
125 void
126 pp_c_dot (c_pretty_printer *pp)
128 pp_dot (pp);
129 pp_base (pp)->padding = pp_none;
132 void
133 pp_c_ampersand (c_pretty_printer *pp)
135 pp_ampersand (pp);
136 pp_base (pp)->padding = pp_none;
139 void
140 pp_c_star (c_pretty_printer *pp)
142 pp_star (pp);
143 pp_base (pp)->padding = pp_none;
146 void
147 pp_c_arrow (c_pretty_printer *pp)
149 pp_arrow (pp);
150 pp_base (pp)->padding = pp_none;
153 void
154 pp_c_semicolon (c_pretty_printer *pp)
156 pp_semicolon (pp);
157 pp_base (pp)->padding = pp_none;
160 void
161 pp_c_complement (c_pretty_printer *pp)
163 pp_complement (pp);
164 pp_base (pp)->padding = pp_none;
167 void
168 pp_c_exclamation (c_pretty_printer *pp)
170 pp_exclamation (pp);
171 pp_base (pp)->padding = pp_none;
174 /* Print out the external representation of CV-QUALIFIER. */
176 static void
177 pp_c_cv_qualifier (c_pretty_printer *pp, const char *cv)
179 const char *p = pp_last_position_in_text (pp);
180 /* The C programming language does not have references, but it is much
181 simpler to handle those here rather than going through the same
182 logic in the C++ pretty-printer. */
183 if (p != NULL && (*p == '*' || *p == '&'))
184 pp_c_whitespace (pp);
185 pp_c_ws_string (pp, cv);
188 /* Pretty-print T using the type-cast notation '( type-name )'. */
190 static void
191 pp_c_type_cast (c_pretty_printer *pp, tree t)
193 pp_c_left_paren (pp);
194 pp_type_id (pp, t);
195 pp_c_right_paren (pp);
198 /* We're about to pretty-print a pointer type as indicated by T.
199 Output a whitespace, if needed, preparing for subsequent output. */
201 void
202 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
204 if (POINTER_TYPE_P (t))
206 tree pointee = strip_pointer_operator (TREE_TYPE (t));
207 if (TREE_CODE (pointee) != ARRAY_TYPE
208 && TREE_CODE (pointee) != FUNCTION_TYPE)
209 pp_c_whitespace (pp);
214 /* Declarations. */
216 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
217 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
218 of its type. Take care of possible extensions.
220 type-qualifier-list:
221 type-qualifier
222 type-qualifier-list type-qualifier
224 type-qualifier:
225 const
226 restrict -- C99
227 __restrict__ -- GNU C
228 address-space-qualifier -- GNU C
229 volatile
231 address-space-qualifier:
232 identifier -- GNU C */
234 void
235 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
237 int qualifiers;
239 if (!t || t == error_mark_node)
240 return;
242 if (!TYPE_P (t))
243 t = TREE_TYPE (t);
245 qualifiers = TYPE_QUALS (t);
246 if (qualifiers & TYPE_QUAL_CONST)
247 pp_c_cv_qualifier (pp, "const");
248 if (qualifiers & TYPE_QUAL_VOLATILE)
249 pp_c_cv_qualifier (pp, "volatile");
250 if (qualifiers & TYPE_QUAL_RESTRICT)
251 pp_c_cv_qualifier (pp, flag_isoc99 ? "restrict" : "__restrict__");
253 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
255 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
256 pp_c_identifier (pp, as);
260 /* pointer:
261 * type-qualifier-list(opt)
262 * type-qualifier-list(opt) pointer */
264 static void
265 pp_c_pointer (c_pretty_printer *pp, tree t)
267 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
268 t = TREE_TYPE (t);
269 switch (TREE_CODE (t))
271 case POINTER_TYPE:
272 /* It is easier to handle C++ reference types here. */
273 case REFERENCE_TYPE:
274 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
275 pp_c_pointer (pp, TREE_TYPE (t));
276 if (TREE_CODE (t) == POINTER_TYPE)
277 pp_c_star (pp);
278 else
279 pp_c_ampersand (pp);
280 pp_c_type_qualifier_list (pp, t);
281 break;
283 /* ??? This node is now in GENERIC and so shouldn't be here. But
284 we'll fix that later. */
285 case DECL_EXPR:
286 pp_declaration (pp, DECL_EXPR_DECL (t));
287 pp_needs_newline (pp) = true;
288 break;
290 default:
291 pp_unsupported_tree (pp, t);
295 /* type-specifier:
296 void
297 char
298 short
300 long
301 float
302 double
303 signed
304 unsigned
305 _Bool -- C99
306 _Complex -- C99
307 _Imaginary -- C99
308 struct-or-union-specifier
309 enum-specifier
310 typedef-name.
312 GNU extensions.
313 simple-type-specifier:
314 __complex__
315 __vector__ */
317 void
318 pp_c_type_specifier (c_pretty_printer *pp, tree t)
320 const enum tree_code code = TREE_CODE (t);
321 switch (code)
323 case ERROR_MARK:
324 pp_c_ws_string (pp, M_("<type-error>"));
325 break;
327 case IDENTIFIER_NODE:
328 pp_c_tree_decl_identifier (pp, t);
329 break;
331 case VOID_TYPE:
332 case BOOLEAN_TYPE:
333 case INTEGER_TYPE:
334 case REAL_TYPE:
335 case FIXED_POINT_TYPE:
336 if (TYPE_NAME (t))
338 t = TYPE_NAME (t);
339 pp_c_type_specifier (pp, t);
341 else
343 int prec = TYPE_PRECISION (t);
344 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
345 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_SATURATING (t));
346 else
347 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
348 if (TYPE_NAME (t))
350 pp_c_type_specifier (pp, t);
351 if (TYPE_PRECISION (t) != prec)
353 pp_string (pp, ":");
354 pp_decimal_int (pp, prec);
357 else
359 switch (code)
361 case INTEGER_TYPE:
362 pp_string (pp, (TYPE_UNSIGNED (t)
363 ? M_("<unnamed-unsigned:")
364 : M_("<unnamed-signed:")));
365 break;
366 case REAL_TYPE:
367 pp_string (pp, M_("<unnamed-float:"));
368 break;
369 case FIXED_POINT_TYPE:
370 pp_string (pp, M_("<unnamed-fixed:"));
371 break;
372 default:
373 gcc_unreachable ();
375 pp_decimal_int (pp, prec);
376 pp_string (pp, ">");
379 break;
381 case TYPE_DECL:
382 if (DECL_NAME (t))
383 pp_id_expression (pp, t);
384 else
385 pp_c_ws_string (pp, M_("<typedef-error>"));
386 break;
388 case UNION_TYPE:
389 case RECORD_TYPE:
390 case ENUMERAL_TYPE:
391 if (code == UNION_TYPE)
392 pp_c_ws_string (pp, "union");
393 else if (code == RECORD_TYPE)
394 pp_c_ws_string (pp, "struct");
395 else if (code == ENUMERAL_TYPE)
396 pp_c_ws_string (pp, "enum");
397 else
398 pp_c_ws_string (pp, M_("<tag-error>"));
400 if (TYPE_NAME (t))
401 pp_id_expression (pp, TYPE_NAME (t));
402 else
403 pp_c_ws_string (pp, M_("<anonymous>"));
404 break;
406 default:
407 pp_unsupported_tree (pp, t);
408 break;
412 /* specifier-qualifier-list:
413 type-specifier specifier-qualifier-list-opt
414 type-qualifier specifier-qualifier-list-opt
417 Implementation note: Because of the non-linearities in array or
418 function declarations, this routine prints not just the
419 specifier-qualifier-list of such entities or types of such entities,
420 but also the 'pointer' production part of their declarators. The
421 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
423 void
424 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
426 const enum tree_code code = TREE_CODE (t);
428 if (TREE_CODE (t) != POINTER_TYPE)
429 pp_c_type_qualifier_list (pp, t);
430 switch (code)
432 case REFERENCE_TYPE:
433 case POINTER_TYPE:
435 /* Get the types-specifier of this type. */
436 tree pointee = strip_pointer_operator (TREE_TYPE (t));
437 pp_c_specifier_qualifier_list (pp, pointee);
438 if (TREE_CODE (pointee) == ARRAY_TYPE
439 || TREE_CODE (pointee) == FUNCTION_TYPE)
441 pp_c_whitespace (pp);
442 pp_c_left_paren (pp);
444 else if (!c_dialect_cxx ())
445 pp_c_whitespace (pp);
446 pp_ptr_operator (pp, t);
448 break;
450 case FUNCTION_TYPE:
451 case ARRAY_TYPE:
452 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
453 break;
455 case VECTOR_TYPE:
456 case COMPLEX_TYPE:
457 if (code == COMPLEX_TYPE)
458 pp_c_ws_string (pp, flag_isoc99 ? "_Complex" : "__complex__");
459 else if (code == VECTOR_TYPE)
461 pp_c_ws_string (pp, "__vector");
462 pp_c_left_paren (pp);
463 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
464 pp_c_right_paren (pp);
465 pp_c_whitespace (pp);
467 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
468 break;
470 default:
471 pp_simple_type_specifier (pp, t);
472 break;
476 /* parameter-type-list:
477 parameter-list
478 parameter-list , ...
480 parameter-list:
481 parameter-declaration
482 parameter-list , parameter-declaration
484 parameter-declaration:
485 declaration-specifiers declarator
486 declaration-specifiers abstract-declarator(opt) */
488 void
489 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
491 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
492 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
493 pp_c_left_paren (pp);
494 if (parms == void_list_node)
495 pp_c_ws_string (pp, "void");
496 else
498 bool first = true;
499 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
501 if (!first)
502 pp_separate_with (pp, ',');
503 first = false;
504 pp_declaration_specifiers
505 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
506 if (want_parm_decl)
507 pp_declarator (pp, parms);
508 else
509 pp_abstract_declarator (pp, TREE_VALUE (parms));
512 pp_c_right_paren (pp);
515 /* abstract-declarator:
516 pointer
517 pointer(opt) direct-abstract-declarator */
519 static void
520 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
522 if (TREE_CODE (t) == POINTER_TYPE)
524 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
525 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
526 pp_c_right_paren (pp);
527 t = TREE_TYPE (t);
530 pp_direct_abstract_declarator (pp, t);
533 /* direct-abstract-declarator:
534 ( abstract-declarator )
535 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
536 direct-abstract-declarator(opt) [ * ]
537 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
539 void
540 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
542 switch (TREE_CODE (t))
544 case POINTER_TYPE:
545 pp_abstract_declarator (pp, t);
546 break;
548 case FUNCTION_TYPE:
549 pp_c_parameter_type_list (pp, t);
550 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
551 break;
553 case ARRAY_TYPE:
554 pp_c_left_bracket (pp);
555 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
557 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
558 tree type = TREE_TYPE (maxval);
560 if (host_integerp (maxval, 0))
561 pp_wide_integer (pp, tree_low_cst (maxval, 0) + 1);
562 else
563 pp_expression (pp, fold_build2 (PLUS_EXPR, type, maxval,
564 build_int_cst (type, 1)));
566 pp_c_right_bracket (pp);
567 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
568 break;
570 case IDENTIFIER_NODE:
571 case VOID_TYPE:
572 case BOOLEAN_TYPE:
573 case INTEGER_TYPE:
574 case REAL_TYPE:
575 case FIXED_POINT_TYPE:
576 case ENUMERAL_TYPE:
577 case RECORD_TYPE:
578 case UNION_TYPE:
579 case VECTOR_TYPE:
580 case COMPLEX_TYPE:
581 case TYPE_DECL:
582 break;
584 default:
585 pp_unsupported_tree (pp, t);
586 break;
590 /* type-name:
591 specifier-qualifier-list abstract-declarator(opt) */
593 void
594 pp_c_type_id (c_pretty_printer *pp, tree t)
596 pp_c_specifier_qualifier_list (pp, t);
597 pp_abstract_declarator (pp, t);
600 /* storage-class-specifier:
601 typedef
602 extern
603 static
604 auto
605 register */
607 void
608 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
610 if (TREE_CODE (t) == TYPE_DECL)
611 pp_c_ws_string (pp, "typedef");
612 else if (DECL_P (t))
614 if (DECL_REGISTER (t))
615 pp_c_ws_string (pp, "register");
616 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
617 pp_c_ws_string (pp, "static");
621 /* function-specifier:
622 inline */
624 void
625 pp_c_function_specifier (c_pretty_printer *pp, tree t)
627 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
628 pp_c_ws_string (pp, "inline");
631 /* declaration-specifiers:
632 storage-class-specifier declaration-specifiers(opt)
633 type-specifier declaration-specifiers(opt)
634 type-qualifier declaration-specifiers(opt)
635 function-specifier declaration-specifiers(opt) */
637 void
638 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
640 pp_storage_class_specifier (pp, t);
641 pp_function_specifier (pp, t);
642 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
645 /* direct-declarator
646 identifier
647 ( declarator )
648 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
649 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
650 direct-declarator [ type-qualifier-list static assignment-expression ]
651 direct-declarator [ type-qualifier-list * ]
652 direct-declarator ( parameter-type-list )
653 direct-declarator ( identifier-list(opt) ) */
655 void
656 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
658 switch (TREE_CODE (t))
660 case VAR_DECL:
661 case PARM_DECL:
662 case TYPE_DECL:
663 case FIELD_DECL:
664 case LABEL_DECL:
665 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
666 pp_c_tree_decl_identifier (pp, t);
667 break;
669 case ARRAY_TYPE:
670 case POINTER_TYPE:
671 pp_abstract_declarator (pp, TREE_TYPE (t));
672 break;
674 case FUNCTION_TYPE:
675 pp_parameter_list (pp, t);
676 pp_abstract_declarator (pp, TREE_TYPE (t));
677 break;
679 case FUNCTION_DECL:
680 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
681 pp_c_tree_decl_identifier (pp, t);
682 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
683 pp_abstract_declarator (pp, TREE_TYPE (t));
684 else
686 pp_parameter_list (pp, t);
687 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
689 break;
691 case INTEGER_TYPE:
692 case REAL_TYPE:
693 case FIXED_POINT_TYPE:
694 case ENUMERAL_TYPE:
695 case UNION_TYPE:
696 case RECORD_TYPE:
697 break;
699 default:
700 pp_unsupported_tree (pp, t);
701 break;
706 /* declarator:
707 pointer(opt) direct-declarator */
709 void
710 pp_c_declarator (c_pretty_printer *pp, tree t)
712 switch (TREE_CODE (t))
714 case INTEGER_TYPE:
715 case REAL_TYPE:
716 case FIXED_POINT_TYPE:
717 case ENUMERAL_TYPE:
718 case UNION_TYPE:
719 case RECORD_TYPE:
720 break;
722 case VAR_DECL:
723 case PARM_DECL:
724 case FIELD_DECL:
725 case ARRAY_TYPE:
726 case FUNCTION_TYPE:
727 case FUNCTION_DECL:
728 case TYPE_DECL:
729 pp_direct_declarator (pp, t);
730 break;
733 default:
734 pp_unsupported_tree (pp, t);
735 break;
739 /* declaration:
740 declaration-specifiers init-declarator-list(opt) ; */
742 void
743 pp_c_declaration (c_pretty_printer *pp, tree t)
745 pp_declaration_specifiers (pp, t);
746 pp_c_init_declarator (pp, t);
749 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
751 void
752 pp_c_attributes (c_pretty_printer *pp, tree attributes)
754 if (attributes == NULL_TREE)
755 return;
757 pp_c_ws_string (pp, "__attribute__");
758 pp_c_left_paren (pp);
759 pp_c_left_paren (pp);
760 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
762 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
763 if (TREE_VALUE (attributes))
764 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
766 if (TREE_CHAIN (attributes))
767 pp_separate_with (pp, ',');
769 pp_c_right_paren (pp);
770 pp_c_right_paren (pp);
773 /* function-definition:
774 declaration-specifiers declarator compound-statement */
776 void
777 pp_c_function_definition (c_pretty_printer *pp, tree t)
779 pp_declaration_specifiers (pp, t);
780 pp_declarator (pp, t);
781 pp_needs_newline (pp) = true;
782 pp_statement (pp, DECL_SAVED_TREE (t));
783 pp_newline (pp);
784 pp_flush (pp);
788 /* Expressions. */
790 /* Print out a c-char. This is called solely for characters which are
791 in the *target* execution character set. We ought to convert them
792 back to the *host* execution character set before printing, but we
793 have no way to do this at present. A decent compromise is to print
794 all characters as if they were in the host execution character set,
795 and not attempt to recover any named escape characters, but render
796 all unprintables as octal escapes. If the host and target character
797 sets are the same, this produces relatively readable output. If they
798 are not the same, strings may appear as gibberish, but that's okay
799 (in fact, it may well be what the reader wants, e.g. if they are looking
800 to see if conversion to the target character set happened correctly).
802 A special case: we need to prefix \, ", and ' with backslashes. It is
803 correct to do so for the *host*'s \, ", and ', because the rest of the
804 file appears in the host character set. */
806 static void
807 pp_c_char (c_pretty_printer *pp, int c)
809 if (ISPRINT (c))
811 switch (c)
813 case '\\': pp_string (pp, "\\\\"); break;
814 case '\'': pp_string (pp, "\\\'"); break;
815 case '\"': pp_string (pp, "\\\""); break;
816 default: pp_character (pp, c);
819 else
820 pp_scalar (pp, "\\%03o", (unsigned) c);
823 /* Print out a STRING literal. */
825 void
826 pp_c_string_literal (c_pretty_printer *pp, tree s)
828 const char *p = TREE_STRING_POINTER (s);
829 int n = TREE_STRING_LENGTH (s) - 1;
830 int i;
831 pp_doublequote (pp);
832 for (i = 0; i < n; ++i)
833 pp_c_char (pp, p[i]);
834 pp_doublequote (pp);
837 /* Pretty-print an INTEGER literal. */
839 static void
840 pp_c_integer_constant (c_pretty_printer *pp, tree i)
842 tree type = TREE_TYPE (i);
844 if (TREE_INT_CST_HIGH (i) == 0)
845 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
846 else
848 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (i);
849 HOST_WIDE_INT high = TREE_INT_CST_HIGH (i);
850 if (tree_int_cst_sgn (i) < 0)
852 pp_character (pp, '-');
853 high = ~high + !low;
854 low = -low;
856 sprintf (pp_buffer (pp)->digit_buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
857 (unsigned HOST_WIDE_INT) high, (unsigned HOST_WIDE_INT) low);
858 pp_string (pp, pp_buffer (pp)->digit_buffer);
860 if (TYPE_UNSIGNED (type))
861 pp_character (pp, 'u');
862 if (type == long_integer_type_node || type == long_unsigned_type_node)
863 pp_character (pp, 'l');
864 else if (type == long_long_integer_type_node
865 || type == long_long_unsigned_type_node)
866 pp_string (pp, "ll");
869 /* Print out a CHARACTER literal. */
871 static void
872 pp_c_character_constant (c_pretty_printer *pp, tree c)
874 tree type = TREE_TYPE (c);
875 if (type == wchar_type_node)
876 pp_character (pp, 'L');
877 pp_quote (pp);
878 if (host_integerp (c, TYPE_UNSIGNED (type)))
879 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
880 else
881 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
882 pp_quote (pp);
885 /* Print out a BOOLEAN literal. */
887 static void
888 pp_c_bool_constant (c_pretty_printer *pp, tree b)
890 if (b == boolean_false_node)
892 if (c_dialect_cxx ())
893 pp_c_ws_string (pp, "false");
894 else if (flag_isoc99)
895 pp_c_ws_string (pp, "_False");
896 else
897 pp_unsupported_tree (pp, b);
899 else if (b == boolean_true_node)
901 if (c_dialect_cxx ())
902 pp_c_ws_string (pp, "true");
903 else if (flag_isoc99)
904 pp_c_ws_string (pp, "_True");
905 else
906 pp_unsupported_tree (pp, b);
908 else if (TREE_CODE (b) == INTEGER_CST)
909 pp_c_integer_constant (pp, b);
910 else
911 pp_unsupported_tree (pp, b);
914 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
915 false; that means the value was obtained by a cast, in which case
916 print out the type-id part of the cast-expression -- the casted value
917 is then printed by pp_c_integer_literal. */
919 static bool
920 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
922 bool value_is_named = true;
923 tree type = TREE_TYPE (e);
924 tree value;
926 /* Find the name of this constant. */
927 for (value = TYPE_VALUES (type);
928 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
929 value = TREE_CHAIN (value))
932 if (value != NULL_TREE)
933 pp_id_expression (pp, TREE_PURPOSE (value));
934 else
936 /* Value must have been cast. */
937 pp_c_type_cast (pp, type);
938 value_is_named = false;
941 return value_is_named;
944 /* Print out a REAL value as a decimal-floating-constant. */
946 static void
947 pp_c_floating_constant (c_pretty_printer *pp, tree r)
949 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
950 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
951 pp_string (pp, pp_buffer(pp)->digit_buffer);
952 if (TREE_TYPE (r) == float_type_node)
953 pp_character (pp, 'f');
954 else if (TREE_TYPE (r) == long_double_type_node)
955 pp_character (pp, 'l');
956 else if (TREE_TYPE (r) == dfloat128_type_node)
957 pp_string (pp, "dl");
958 else if (TREE_TYPE (r) == dfloat64_type_node)
959 pp_string (pp, "dd");
960 else if (TREE_TYPE (r) == dfloat32_type_node)
961 pp_string (pp, "df");
964 /* Print out a FIXED value as a decimal-floating-constant. */
966 static void
967 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
969 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
970 sizeof (pp_buffer (pp)->digit_buffer));
971 pp_string (pp, pp_buffer(pp)->digit_buffer);
974 /* Pretty-print a compound literal expression. GNU extensions include
975 vector constants. */
977 static void
978 pp_c_compound_literal (c_pretty_printer *pp, tree e)
980 tree type = TREE_TYPE (e);
981 pp_c_type_cast (pp, type);
983 switch (TREE_CODE (type))
985 case RECORD_TYPE:
986 case UNION_TYPE:
987 case ARRAY_TYPE:
988 case VECTOR_TYPE:
989 case COMPLEX_TYPE:
990 pp_c_brace_enclosed_initializer_list (pp, e);
991 break;
993 default:
994 pp_unsupported_tree (pp, e);
995 break;
999 /* Pretty-print a COMPLEX_EXPR expression. */
1001 static void
1002 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1004 /* Handle a few common special cases, otherwise fallback
1005 to printing it as compound literal. */
1006 tree type = TREE_TYPE (e);
1007 tree realexpr = TREE_OPERAND (e, 0);
1008 tree imagexpr = TREE_OPERAND (e, 1);
1010 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1011 if (TREE_CODE (realexpr) == NOP_EXPR
1012 && TREE_CODE (imagexpr) == NOP_EXPR
1013 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1014 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1015 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1016 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1017 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1018 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1020 pp_c_type_cast (pp, type);
1021 pp_expression (pp, TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1022 return;
1025 /* Cast of an scalar expression to COMPLEX_TYPE. */
1026 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1027 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1029 pp_c_type_cast (pp, type);
1030 if (TREE_CODE (realexpr) == NOP_EXPR)
1031 realexpr = TREE_OPERAND (realexpr, 0);
1032 pp_expression (pp, realexpr);
1033 return;
1036 pp_c_compound_literal (pp, e);
1039 /* constant:
1040 integer-constant
1041 floating-constant
1042 fixed-point-constant
1043 enumeration-constant
1044 character-constant */
1046 void
1047 pp_c_constant (c_pretty_printer *pp, tree e)
1049 const enum tree_code code = TREE_CODE (e);
1051 switch (code)
1053 case INTEGER_CST:
1055 tree type = TREE_TYPE (e);
1056 if (type == boolean_type_node)
1057 pp_c_bool_constant (pp, e);
1058 else if (type == char_type_node)
1059 pp_c_character_constant (pp, e);
1060 else if (TREE_CODE (type) == ENUMERAL_TYPE
1061 && pp_c_enumeration_constant (pp, e))
1063 else
1064 pp_c_integer_constant (pp, e);
1066 break;
1068 case REAL_CST:
1069 pp_c_floating_constant (pp, e);
1070 break;
1072 case FIXED_CST:
1073 pp_c_fixed_constant (pp, e);
1074 break;
1076 case STRING_CST:
1077 pp_c_string_literal (pp, e);
1078 break;
1080 case COMPLEX_CST:
1081 /* Sometimes, we are confused and we think a complex literal
1082 is a constant. Such thing is a compound literal which
1083 grammatically belongs to postfix-expr production. */
1084 pp_c_compound_literal (pp, e);
1085 break;
1087 default:
1088 pp_unsupported_tree (pp, e);
1089 break;
1093 /* Pretty-print a string such as an identifier, without changing its
1094 encoding, preceded by whitespace is necessary. */
1096 void
1097 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1099 pp_c_maybe_whitespace (pp);
1100 pp_string (pp, str);
1101 pp_base (pp)->padding = pp_before;
1104 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1105 that need converting to the locale encoding, preceded by whitespace
1106 is necessary. */
1108 void
1109 pp_c_identifier (c_pretty_printer *pp, const char *id)
1111 pp_c_maybe_whitespace (pp);
1112 pp_identifier (pp, id);
1113 pp_base (pp)->padding = pp_before;
1116 /* Pretty-print a C primary-expression.
1117 primary-expression:
1118 identifier
1119 constant
1120 string-literal
1121 ( expression ) */
1123 void
1124 pp_c_primary_expression (c_pretty_printer *pp, tree e)
1126 switch (TREE_CODE (e))
1128 case VAR_DECL:
1129 case PARM_DECL:
1130 case FIELD_DECL:
1131 case CONST_DECL:
1132 case FUNCTION_DECL:
1133 case LABEL_DECL:
1134 pp_c_tree_decl_identifier (pp, e);
1135 break;
1137 case IDENTIFIER_NODE:
1138 pp_c_tree_identifier (pp, e);
1139 break;
1141 case ERROR_MARK:
1142 pp_c_ws_string (pp, M_("<erroneous-expression>"));
1143 break;
1145 case RESULT_DECL:
1146 pp_c_ws_string (pp, M_("<return-value>"));
1147 break;
1149 case INTEGER_CST:
1150 case REAL_CST:
1151 case FIXED_CST:
1152 case STRING_CST:
1153 pp_c_constant (pp, e);
1154 break;
1156 case TARGET_EXPR:
1157 pp_c_ws_string (pp, "__builtin_memcpy");
1158 pp_c_left_paren (pp);
1159 pp_ampersand (pp);
1160 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1161 pp_separate_with (pp, ',');
1162 pp_ampersand (pp);
1163 pp_initializer (pp, TREE_OPERAND (e, 1));
1164 if (TREE_OPERAND (e, 2))
1166 pp_separate_with (pp, ',');
1167 pp_c_expression (pp, TREE_OPERAND (e, 2));
1169 pp_c_right_paren (pp);
1170 break;
1172 default:
1173 /* FIXME: Make sure we won't get into an infinite loop. */
1174 pp_c_left_paren (pp);
1175 pp_expression (pp, e);
1176 pp_c_right_paren (pp);
1177 break;
1181 /* Print out a C initializer -- also support C compound-literals.
1182 initializer:
1183 assignment-expression:
1184 { initializer-list }
1185 { initializer-list , } */
1187 static void
1188 pp_c_initializer (c_pretty_printer *pp, tree e)
1190 if (TREE_CODE (e) == CONSTRUCTOR)
1191 pp_c_brace_enclosed_initializer_list (pp, e);
1192 else
1193 pp_expression (pp, e);
1196 /* init-declarator:
1197 declarator:
1198 declarator = initializer */
1200 void
1201 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1203 pp_declarator (pp, t);
1204 /* We don't want to output function definitions here. There are handled
1205 elsewhere (and the syntactic form is bogus anyway). */
1206 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1208 tree init = DECL_INITIAL (t);
1209 /* This C++ bit is handled here because it is easier to do so.
1210 In templates, the C++ parser builds a TREE_LIST for a
1211 direct-initialization; the TREE_PURPOSE is the variable to
1212 initialize and the TREE_VALUE is the initializer. */
1213 if (TREE_CODE (init) == TREE_LIST)
1215 pp_c_left_paren (pp);
1216 pp_expression (pp, TREE_VALUE (init));
1217 pp_right_paren (pp);
1219 else
1221 pp_space (pp);
1222 pp_equal (pp);
1223 pp_space (pp);
1224 pp_c_initializer (pp, init);
1229 /* initializer-list:
1230 designation(opt) initializer
1231 initializer-list , designation(opt) initializer
1233 designation:
1234 designator-list =
1236 designator-list:
1237 designator
1238 designator-list designator
1240 designator:
1241 [ constant-expression ]
1242 identifier */
1244 static void
1245 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1247 tree type = TREE_TYPE (e);
1248 const enum tree_code code = TREE_CODE (type);
1250 if (TREE_CODE (e) == CONSTRUCTOR)
1252 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1253 return;
1256 switch (code)
1258 case RECORD_TYPE:
1259 case UNION_TYPE:
1260 case ARRAY_TYPE:
1262 tree init = TREE_OPERAND (e, 0);
1263 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1265 if (code == RECORD_TYPE || code == UNION_TYPE)
1267 pp_c_dot (pp);
1268 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1270 else
1272 pp_c_left_bracket (pp);
1273 if (TREE_PURPOSE (init))
1274 pp_c_constant (pp, TREE_PURPOSE (init));
1275 pp_c_right_bracket (pp);
1277 pp_c_whitespace (pp);
1278 pp_equal (pp);
1279 pp_c_whitespace (pp);
1280 pp_initializer (pp, TREE_VALUE (init));
1281 if (TREE_CHAIN (init))
1282 pp_separate_with (pp, ',');
1285 return;
1287 case VECTOR_TYPE:
1288 if (TREE_CODE (e) == VECTOR_CST)
1289 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1290 else
1291 break;
1292 return;
1294 case COMPLEX_TYPE:
1295 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1297 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1298 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1299 pp_separate_with (pp, ',');
1300 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1302 else
1303 break;
1304 return;
1306 default:
1307 break;
1310 pp_unsupported_tree (pp, type);
1313 /* Pretty-print a brace-enclosed initializer-list. */
1315 static void
1316 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1318 pp_c_left_brace (pp);
1319 pp_c_initializer_list (pp, l);
1320 pp_c_right_brace (pp);
1324 /* This is a convenient function, used to bridge gap between C and C++
1325 grammars.
1327 id-expression:
1328 identifier */
1330 void
1331 pp_c_id_expression (c_pretty_printer *pp, tree t)
1333 switch (TREE_CODE (t))
1335 case VAR_DECL:
1336 case PARM_DECL:
1337 case CONST_DECL:
1338 case TYPE_DECL:
1339 case FUNCTION_DECL:
1340 case FIELD_DECL:
1341 case LABEL_DECL:
1342 pp_c_tree_decl_identifier (pp, t);
1343 break;
1345 case IDENTIFIER_NODE:
1346 pp_c_tree_identifier (pp, t);
1347 break;
1349 default:
1350 pp_unsupported_tree (pp, t);
1351 break;
1355 /* postfix-expression:
1356 primary-expression
1357 postfix-expression [ expression ]
1358 postfix-expression ( argument-expression-list(opt) )
1359 postfix-expression . identifier
1360 postfix-expression -> identifier
1361 postfix-expression ++
1362 postfix-expression --
1363 ( type-name ) { initializer-list }
1364 ( type-name ) { initializer-list , } */
1366 void
1367 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1369 enum tree_code code = TREE_CODE (e);
1370 switch (code)
1372 case POSTINCREMENT_EXPR:
1373 case POSTDECREMENT_EXPR:
1374 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1375 pp_string (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1376 break;
1378 case ARRAY_REF:
1379 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1380 pp_c_left_bracket (pp);
1381 pp_expression (pp, TREE_OPERAND (e, 1));
1382 pp_c_right_bracket (pp);
1383 break;
1385 case CALL_EXPR:
1387 call_expr_arg_iterator iter;
1388 tree arg;
1389 pp_postfix_expression (pp, CALL_EXPR_FN (e));
1390 pp_c_left_paren (pp);
1391 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1393 pp_expression (pp, arg);
1394 if (more_call_expr_args_p (&iter))
1395 pp_separate_with (pp, ',');
1397 pp_c_right_paren (pp);
1398 break;
1401 case UNORDERED_EXPR:
1402 pp_c_ws_string (pp, flag_isoc99
1403 ? "isunordered"
1404 : "__builtin_isunordered");
1405 goto two_args_fun;
1407 case ORDERED_EXPR:
1408 pp_c_ws_string (pp, flag_isoc99
1409 ? "!isunordered"
1410 : "!__builtin_isunordered");
1411 goto two_args_fun;
1413 case UNLT_EXPR:
1414 pp_c_ws_string (pp, flag_isoc99
1415 ? "!isgreaterequal"
1416 : "!__builtin_isgreaterequal");
1417 goto two_args_fun;
1419 case UNLE_EXPR:
1420 pp_c_ws_string (pp, flag_isoc99
1421 ? "!isgreater"
1422 : "!__builtin_isgreater");
1423 goto two_args_fun;
1425 case UNGT_EXPR:
1426 pp_c_ws_string (pp, flag_isoc99
1427 ? "!islessequal"
1428 : "!__builtin_islessequal");
1429 goto two_args_fun;
1431 case UNGE_EXPR:
1432 pp_c_ws_string (pp, flag_isoc99
1433 ? "!isless"
1434 : "!__builtin_isless");
1435 goto two_args_fun;
1437 case UNEQ_EXPR:
1438 pp_c_ws_string (pp, flag_isoc99
1439 ? "!islessgreater"
1440 : "!__builtin_islessgreater");
1441 goto two_args_fun;
1443 case LTGT_EXPR:
1444 pp_c_ws_string (pp, flag_isoc99
1445 ? "islessgreater"
1446 : "__builtin_islessgreater");
1447 goto two_args_fun;
1449 two_args_fun:
1450 pp_c_left_paren (pp);
1451 pp_expression (pp, TREE_OPERAND (e, 0));
1452 pp_separate_with (pp, ',');
1453 pp_expression (pp, TREE_OPERAND (e, 1));
1454 pp_c_right_paren (pp);
1455 break;
1457 case ABS_EXPR:
1458 pp_c_ws_string (pp, "__builtin_abs");
1459 pp_c_left_paren (pp);
1460 pp_expression (pp, TREE_OPERAND (e, 0));
1461 pp_c_right_paren (pp);
1462 break;
1464 case COMPONENT_REF:
1466 tree object = TREE_OPERAND (e, 0);
1467 if (TREE_CODE (object) == INDIRECT_REF)
1469 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1470 pp_c_arrow (pp);
1472 else
1474 pp_postfix_expression (pp, object);
1475 pp_c_dot (pp);
1477 pp_expression (pp, TREE_OPERAND (e, 1));
1479 break;
1481 case BIT_FIELD_REF:
1483 tree type = TREE_TYPE (e);
1485 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1486 if (type
1487 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1489 HOST_WIDE_INT bitpos = tree_low_cst (TREE_OPERAND (e, 2), 0);
1490 HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 0);
1491 if ((bitpos % size) == 0)
1493 pp_c_left_paren (pp);
1494 pp_c_left_paren (pp);
1495 pp_type_id (pp, type);
1496 pp_c_star (pp);
1497 pp_c_right_paren (pp);
1498 pp_c_ampersand (pp);
1499 pp_expression (pp, TREE_OPERAND (e, 0));
1500 pp_c_right_paren (pp);
1501 pp_c_left_bracket (pp);
1502 pp_wide_integer (pp, bitpos / size);
1503 pp_c_right_bracket (pp);
1504 break;
1507 pp_unsupported_tree (pp, e);
1509 break;
1511 case COMPLEX_CST:
1512 case VECTOR_CST:
1513 pp_c_compound_literal (pp, e);
1514 break;
1516 case COMPLEX_EXPR:
1517 pp_c_complex_expr (pp, e);
1518 break;
1520 case COMPOUND_LITERAL_EXPR:
1521 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1522 /* Fall through. */
1523 case CONSTRUCTOR:
1524 pp_initializer (pp, e);
1525 break;
1527 case VA_ARG_EXPR:
1528 pp_c_ws_string (pp, "__builtin_va_arg");
1529 pp_c_left_paren (pp);
1530 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1531 pp_separate_with (pp, ',');
1532 pp_type_id (pp, TREE_TYPE (e));
1533 pp_c_right_paren (pp);
1534 break;
1536 case ADDR_EXPR:
1537 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1539 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1540 break;
1542 /* else fall through. */
1544 default:
1545 pp_primary_expression (pp, e);
1546 break;
1550 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1552 void
1553 pp_c_expression_list (c_pretty_printer *pp, tree e)
1555 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1557 pp_expression (pp, TREE_VALUE (e));
1558 if (TREE_CHAIN (e))
1559 pp_separate_with (pp, ',');
1563 /* Print out V, which contains the elements of a constructor. */
1565 void
1566 pp_c_constructor_elts (c_pretty_printer *pp, VEC(constructor_elt,gc) *v)
1568 unsigned HOST_WIDE_INT ix;
1569 tree value;
1571 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1573 pp_expression (pp, value);
1574 if (ix != VEC_length (constructor_elt, v) - 1)
1575 pp_separate_with (pp, ',');
1579 /* Print out an expression-list in parens, as if it were the argument
1580 list to a function. */
1582 void
1583 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1585 pp_c_left_paren (pp);
1586 if (t && TREE_CODE (t) == TREE_LIST)
1587 pp_c_expression_list (pp, t);
1588 pp_c_right_paren (pp);
1591 /* unary-expression:
1592 postfix-expression
1593 ++ cast-expression
1594 -- cast-expression
1595 unary-operator cast-expression
1596 sizeof unary-expression
1597 sizeof ( type-id )
1599 unary-operator: one of
1600 * & + - ! ~
1602 GNU extensions.
1603 unary-expression:
1604 __alignof__ unary-expression
1605 __alignof__ ( type-id )
1606 __real__ unary-expression
1607 __imag__ unary-expression */
1609 void
1610 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1612 enum tree_code code = TREE_CODE (e);
1613 switch (code)
1615 case PREINCREMENT_EXPR:
1616 case PREDECREMENT_EXPR:
1617 pp_string (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1618 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1619 break;
1621 case ADDR_EXPR:
1622 case INDIRECT_REF:
1623 case NEGATE_EXPR:
1624 case BIT_NOT_EXPR:
1625 case TRUTH_NOT_EXPR:
1626 case CONJ_EXPR:
1627 /* String literal are used by address. */
1628 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1629 pp_ampersand (pp);
1630 else if (code == INDIRECT_REF)
1631 pp_c_star (pp);
1632 else if (code == NEGATE_EXPR)
1633 pp_minus (pp);
1634 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1635 pp_complement (pp);
1636 else if (code == TRUTH_NOT_EXPR)
1637 pp_exclamation (pp);
1638 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1639 break;
1641 case REALPART_EXPR:
1642 case IMAGPART_EXPR:
1643 pp_c_ws_string (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1644 pp_c_whitespace (pp);
1645 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1646 break;
1648 default:
1649 pp_postfix_expression (pp, e);
1650 break;
1654 /* cast-expression:
1655 unary-expression
1656 ( type-name ) cast-expression */
1658 void
1659 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1661 switch (TREE_CODE (e))
1663 case FLOAT_EXPR:
1664 case FIX_TRUNC_EXPR:
1665 CASE_CONVERT:
1666 case VIEW_CONVERT_EXPR:
1667 pp_c_type_cast (pp, TREE_TYPE (e));
1668 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1669 break;
1671 default:
1672 pp_unary_expression (pp, e);
1676 /* multiplicative-expression:
1677 cast-expression
1678 multiplicative-expression * cast-expression
1679 multiplicative-expression / cast-expression
1680 multiplicative-expression % cast-expression */
1682 static void
1683 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1685 enum tree_code code = TREE_CODE (e);
1686 switch (code)
1688 case MULT_EXPR:
1689 case TRUNC_DIV_EXPR:
1690 case TRUNC_MOD_EXPR:
1691 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1692 pp_c_whitespace (pp);
1693 if (code == MULT_EXPR)
1694 pp_c_star (pp);
1695 else if (code == TRUNC_DIV_EXPR)
1696 pp_slash (pp);
1697 else
1698 pp_modulo (pp);
1699 pp_c_whitespace (pp);
1700 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1701 break;
1703 default:
1704 pp_c_cast_expression (pp, e);
1705 break;
1709 /* additive-expression:
1710 multiplicative-expression
1711 additive-expression + multiplicative-expression
1712 additive-expression - multiplicative-expression */
1714 static void
1715 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1717 enum tree_code code = TREE_CODE (e);
1718 switch (code)
1720 case POINTER_PLUS_EXPR:
1721 case PLUS_EXPR:
1722 case MINUS_EXPR:
1723 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1724 pp_c_whitespace (pp);
1725 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1726 pp_plus (pp);
1727 else
1728 pp_minus (pp);
1729 pp_c_whitespace (pp);
1730 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1731 break;
1733 default:
1734 pp_multiplicative_expression (pp, e);
1735 break;
1739 /* additive-expression:
1740 additive-expression
1741 shift-expression << additive-expression
1742 shift-expression >> additive-expression */
1744 static void
1745 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1747 enum tree_code code = TREE_CODE (e);
1748 switch (code)
1750 case LSHIFT_EXPR:
1751 case RSHIFT_EXPR:
1752 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1753 pp_c_whitespace (pp);
1754 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1755 pp_c_whitespace (pp);
1756 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1757 break;
1759 default:
1760 pp_c_additive_expression (pp, e);
1764 /* relational-expression:
1765 shift-expression
1766 relational-expression < shift-expression
1767 relational-expression > shift-expression
1768 relational-expression <= shift-expression
1769 relational-expression >= shift-expression */
1771 static void
1772 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1774 enum tree_code code = TREE_CODE (e);
1775 switch (code)
1777 case LT_EXPR:
1778 case GT_EXPR:
1779 case LE_EXPR:
1780 case GE_EXPR:
1781 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1782 pp_c_whitespace (pp);
1783 if (code == LT_EXPR)
1784 pp_less (pp);
1785 else if (code == GT_EXPR)
1786 pp_greater (pp);
1787 else if (code == LE_EXPR)
1788 pp_string (pp, "<=");
1789 else if (code == GE_EXPR)
1790 pp_string (pp, ">=");
1791 pp_c_whitespace (pp);
1792 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1793 break;
1795 default:
1796 pp_c_shift_expression (pp, e);
1797 break;
1801 /* equality-expression:
1802 relational-expression
1803 equality-expression == relational-expression
1804 equality-equality != relational-expression */
1806 static void
1807 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1809 enum tree_code code = TREE_CODE (e);
1810 switch (code)
1812 case EQ_EXPR:
1813 case NE_EXPR:
1814 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1815 pp_c_whitespace (pp);
1816 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1817 pp_c_whitespace (pp);
1818 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1819 break;
1821 default:
1822 pp_c_relational_expression (pp, e);
1823 break;
1827 /* AND-expression:
1828 equality-expression
1829 AND-expression & equality-equality */
1831 static void
1832 pp_c_and_expression (c_pretty_printer *pp, tree e)
1834 if (TREE_CODE (e) == BIT_AND_EXPR)
1836 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1837 pp_c_whitespace (pp);
1838 pp_ampersand (pp);
1839 pp_c_whitespace (pp);
1840 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1842 else
1843 pp_c_equality_expression (pp, e);
1846 /* exclusive-OR-expression:
1847 AND-expression
1848 exclusive-OR-expression ^ AND-expression */
1850 static void
1851 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1853 if (TREE_CODE (e) == BIT_XOR_EXPR
1854 || TREE_CODE (e) == TRUTH_XOR_EXPR)
1856 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1857 if (TREE_CODE (e) == BIT_XOR_EXPR)
1858 pp_c_maybe_whitespace (pp);
1859 else
1860 pp_c_whitespace (pp);
1861 pp_carret (pp);
1862 pp_c_whitespace (pp);
1863 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1865 else
1866 pp_c_and_expression (pp, e);
1869 /* inclusive-OR-expression:
1870 exclusive-OR-expression
1871 inclusive-OR-expression | exclusive-OR-expression */
1873 static void
1874 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1876 if (TREE_CODE (e) == BIT_IOR_EXPR)
1878 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1879 pp_c_whitespace (pp);
1880 pp_bar (pp);
1881 pp_c_whitespace (pp);
1882 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1884 else
1885 pp_c_exclusive_or_expression (pp, e);
1888 /* logical-AND-expression:
1889 inclusive-OR-expression
1890 logical-AND-expression && inclusive-OR-expression */
1892 static void
1893 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1895 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
1896 || TREE_CODE (e) == TRUTH_AND_EXPR)
1898 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1899 pp_c_whitespace (pp);
1900 pp_string (pp, "&&");
1901 pp_c_whitespace (pp);
1902 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1904 else
1905 pp_c_inclusive_or_expression (pp, e);
1908 /* logical-OR-expression:
1909 logical-AND-expression
1910 logical-OR-expression || logical-AND-expression */
1912 void
1913 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1915 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
1916 || TREE_CODE (e) == TRUTH_OR_EXPR)
1918 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1919 pp_c_whitespace (pp);
1920 pp_string (pp, "||");
1921 pp_c_whitespace (pp);
1922 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1924 else
1925 pp_c_logical_and_expression (pp, e);
1928 /* conditional-expression:
1929 logical-OR-expression
1930 logical-OR-expression ? expression : conditional-expression */
1932 static void
1933 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1935 if (TREE_CODE (e) == COND_EXPR)
1937 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1938 pp_c_whitespace (pp);
1939 pp_question (pp);
1940 pp_c_whitespace (pp);
1941 pp_expression (pp, TREE_OPERAND (e, 1));
1942 pp_c_whitespace (pp);
1943 pp_colon (pp);
1944 pp_c_whitespace (pp);
1945 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1947 else
1948 pp_c_logical_or_expression (pp, e);
1952 /* assignment-expression:
1953 conditional-expression
1954 unary-expression assignment-operator assignment-expression
1956 assignment-expression: one of
1957 = *= /= %= += -= >>= <<= &= ^= |= */
1959 static void
1960 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1962 if (TREE_CODE (e) == MODIFY_EXPR
1963 || TREE_CODE (e) == INIT_EXPR)
1965 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1966 pp_c_whitespace (pp);
1967 pp_equal (pp);
1968 pp_space (pp);
1969 pp_c_expression (pp, TREE_OPERAND (e, 1));
1971 else
1972 pp_c_conditional_expression (pp, e);
1975 /* expression:
1976 assignment-expression
1977 expression , assignment-expression
1979 Implementation note: instead of going through the usual recursion
1980 chain, I take the liberty of dispatching nodes to the appropriate
1981 functions. This makes some redundancy, but it worths it. That also
1982 prevents a possible infinite recursion between pp_c_primary_expression ()
1983 and pp_c_expression (). */
1985 void
1986 pp_c_expression (c_pretty_printer *pp, tree e)
1988 switch (TREE_CODE (e))
1990 case INTEGER_CST:
1991 pp_c_integer_constant (pp, e);
1992 break;
1994 case REAL_CST:
1995 pp_c_floating_constant (pp, e);
1996 break;
1998 case FIXED_CST:
1999 pp_c_fixed_constant (pp, e);
2000 break;
2002 case STRING_CST:
2003 pp_c_string_literal (pp, e);
2004 break;
2006 case IDENTIFIER_NODE:
2007 case FUNCTION_DECL:
2008 case VAR_DECL:
2009 case CONST_DECL:
2010 case PARM_DECL:
2011 case RESULT_DECL:
2012 case FIELD_DECL:
2013 case LABEL_DECL:
2014 case ERROR_MARK:
2015 pp_primary_expression (pp, e);
2016 break;
2018 case POSTINCREMENT_EXPR:
2019 case POSTDECREMENT_EXPR:
2020 case ARRAY_REF:
2021 case CALL_EXPR:
2022 case COMPONENT_REF:
2023 case BIT_FIELD_REF:
2024 case COMPLEX_CST:
2025 case COMPLEX_EXPR:
2026 case VECTOR_CST:
2027 case ORDERED_EXPR:
2028 case UNORDERED_EXPR:
2029 case LTGT_EXPR:
2030 case UNEQ_EXPR:
2031 case UNLE_EXPR:
2032 case UNLT_EXPR:
2033 case UNGE_EXPR:
2034 case UNGT_EXPR:
2035 case ABS_EXPR:
2036 case CONSTRUCTOR:
2037 case COMPOUND_LITERAL_EXPR:
2038 case VA_ARG_EXPR:
2039 pp_postfix_expression (pp, e);
2040 break;
2042 case CONJ_EXPR:
2043 case ADDR_EXPR:
2044 case INDIRECT_REF:
2045 case NEGATE_EXPR:
2046 case BIT_NOT_EXPR:
2047 case TRUTH_NOT_EXPR:
2048 case PREINCREMENT_EXPR:
2049 case PREDECREMENT_EXPR:
2050 case REALPART_EXPR:
2051 case IMAGPART_EXPR:
2052 pp_c_unary_expression (pp, e);
2053 break;
2055 case FLOAT_EXPR:
2056 case FIX_TRUNC_EXPR:
2057 CASE_CONVERT:
2058 case VIEW_CONVERT_EXPR:
2059 pp_c_cast_expression (pp, e);
2060 break;
2062 case MULT_EXPR:
2063 case TRUNC_MOD_EXPR:
2064 case TRUNC_DIV_EXPR:
2065 pp_multiplicative_expression (pp, e);
2066 break;
2068 case LSHIFT_EXPR:
2069 case RSHIFT_EXPR:
2070 pp_c_shift_expression (pp, e);
2071 break;
2073 case LT_EXPR:
2074 case GT_EXPR:
2075 case LE_EXPR:
2076 case GE_EXPR:
2077 pp_c_relational_expression (pp, e);
2078 break;
2080 case BIT_AND_EXPR:
2081 pp_c_and_expression (pp, e);
2082 break;
2084 case BIT_XOR_EXPR:
2085 case TRUTH_XOR_EXPR:
2086 pp_c_exclusive_or_expression (pp, e);
2087 break;
2089 case BIT_IOR_EXPR:
2090 pp_c_inclusive_or_expression (pp, e);
2091 break;
2093 case TRUTH_ANDIF_EXPR:
2094 case TRUTH_AND_EXPR:
2095 pp_c_logical_and_expression (pp, e);
2096 break;
2098 case TRUTH_ORIF_EXPR:
2099 case TRUTH_OR_EXPR:
2100 pp_c_logical_or_expression (pp, e);
2101 break;
2103 case EQ_EXPR:
2104 case NE_EXPR:
2105 pp_c_equality_expression (pp, e);
2106 break;
2108 case COND_EXPR:
2109 pp_conditional_expression (pp, e);
2110 break;
2112 case POINTER_PLUS_EXPR:
2113 case PLUS_EXPR:
2114 case MINUS_EXPR:
2115 pp_c_additive_expression (pp, e);
2116 break;
2118 case MODIFY_EXPR:
2119 case INIT_EXPR:
2120 pp_assignment_expression (pp, e);
2121 break;
2123 case COMPOUND_EXPR:
2124 pp_c_left_paren (pp);
2125 pp_expression (pp, TREE_OPERAND (e, 0));
2126 pp_separate_with (pp, ',');
2127 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
2128 pp_c_right_paren (pp);
2129 break;
2131 case NON_LVALUE_EXPR:
2132 case SAVE_EXPR:
2133 pp_expression (pp, TREE_OPERAND (e, 0));
2134 break;
2136 case TARGET_EXPR:
2137 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
2138 break;
2140 case BIND_EXPR:
2141 case GOTO_EXPR:
2142 /* We don't yet have a way of dumping statements in a
2143 human-readable format. */
2144 pp_string (pp, "({...})");
2145 break;
2147 default:
2148 pp_unsupported_tree (pp, e);
2149 break;
2155 /* Statements. */
2157 void
2158 pp_c_statement (c_pretty_printer *pp, tree stmt)
2160 if (stmt == NULL)
2161 return;
2163 if (pp_needs_newline (pp))
2164 pp_newline_and_indent (pp, 0);
2166 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
2170 /* Initialize the PRETTY-PRINTER for handling C codes. */
2172 void
2173 pp_c_pretty_printer_init (c_pretty_printer *pp)
2175 pp->offset_list = 0;
2177 pp->declaration = pp_c_declaration;
2178 pp->declaration_specifiers = pp_c_declaration_specifiers;
2179 pp->declarator = pp_c_declarator;
2180 pp->direct_declarator = pp_c_direct_declarator;
2181 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
2182 pp->abstract_declarator = pp_c_abstract_declarator;
2183 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
2184 pp->ptr_operator = pp_c_pointer;
2185 pp->parameter_list = pp_c_parameter_type_list;
2186 pp->type_id = pp_c_type_id;
2187 pp->simple_type_specifier = pp_c_type_specifier;
2188 pp->function_specifier = pp_c_function_specifier;
2189 pp->storage_class_specifier = pp_c_storage_class_specifier;
2191 pp->statement = pp_c_statement;
2193 pp->constant = pp_c_constant;
2194 pp->id_expression = pp_c_id_expression;
2195 pp->primary_expression = pp_c_primary_expression;
2196 pp->postfix_expression = pp_c_postfix_expression;
2197 pp->unary_expression = pp_c_unary_expression;
2198 pp->initializer = pp_c_initializer;
2199 pp->multiplicative_expression = pp_c_multiplicative_expression;
2200 pp->conditional_expression = pp_c_conditional_expression;
2201 pp->assignment_expression = pp_c_assignment_expression;
2202 pp->expression = pp_c_expression;
2206 /* Print the tree T in full, on file FILE. */
2208 void
2209 print_c_tree (FILE *file, tree t)
2211 static c_pretty_printer pp_rec;
2212 static bool initialized = 0;
2213 c_pretty_printer *pp = &pp_rec;
2215 if (!initialized)
2217 initialized = 1;
2218 pp_construct (pp_base (pp), NULL, 0);
2219 pp_c_pretty_printer_init (pp);
2220 pp_needs_newline (pp) = true;
2222 pp_base (pp)->buffer->stream = file;
2224 pp_statement (pp, t);
2226 pp_newline (pp);
2227 pp_flush (pp);
2230 /* Print the tree T in full, on stderr. */
2232 void
2233 debug_c_tree (tree t)
2235 print_c_tree (stderr, t);
2236 fputc ('\n', stderr);
2239 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2240 up of T's memory address. */
2242 void
2243 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2245 const char *name;
2247 gcc_assert (DECL_P (t));
2249 if (DECL_NAME (t))
2250 name = IDENTIFIER_POINTER (DECL_NAME (t));
2251 else
2253 static char xname[8];
2254 sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
2255 name = xname;
2258 pp_c_identifier (pp, name);