c-pretty-print.h (c_pretty_printer::declaration): Now a virtual member function.
[official-gcc.git] / gcc / c-family / c-pretty-print.c
blobc50b068c6277dab3cad7611103b76f7079f0492f
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "intl.h"
27 #include "c-pretty-print.h"
28 #include "tree-pretty-print.h"
29 #include "tree-iterator.h"
30 #include "diagnostic.h"
32 /* The pretty-printer code is primarily designed to closely follow
33 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
34 codes we used to have in the past. Following a structured
35 approach (preferably the official grammars) is believed to make it
36 much easier to add extensions and nifty pretty-printing effects that
37 takes expression or declaration contexts into account. */
40 #define pp_c_maybe_whitespace(PP) \
41 do { \
42 if ((PP)->padding == pp_before) \
43 pp_c_whitespace (PP); \
44 } while (0)
46 /* literal */
47 static void pp_c_char (c_pretty_printer *, int);
49 /* postfix-expression */
50 static void pp_c_initializer_list (c_pretty_printer *, tree);
51 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
53 static void pp_c_additive_expression (c_pretty_printer *, tree);
54 static void pp_c_shift_expression (c_pretty_printer *, tree);
55 static void pp_c_relational_expression (c_pretty_printer *, tree);
56 static void pp_c_equality_expression (c_pretty_printer *, tree);
57 static void pp_c_and_expression (c_pretty_printer *, tree);
58 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
59 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
60 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
62 /* declarations. */
65 /* Helper functions. */
67 void
68 pp_c_whitespace (c_pretty_printer *pp)
70 pp_space (pp);
71 pp->padding = pp_none;
74 void
75 pp_c_left_paren (c_pretty_printer *pp)
77 pp_left_paren (pp);
78 pp->padding = pp_none;
81 void
82 pp_c_right_paren (c_pretty_printer *pp)
84 pp_right_paren (pp);
85 pp->padding = pp_none;
88 void
89 pp_c_left_brace (c_pretty_printer *pp)
91 pp_left_brace (pp);
92 pp->padding = pp_none;
95 void
96 pp_c_right_brace (c_pretty_printer *pp)
98 pp_right_brace (pp);
99 pp->padding = pp_none;
102 void
103 pp_c_left_bracket (c_pretty_printer *pp)
105 pp_left_bracket (pp);
106 pp->padding = pp_none;
109 void
110 pp_c_right_bracket (c_pretty_printer *pp)
112 pp_right_bracket (pp);
113 pp->padding = pp_none;
116 void
117 pp_c_dot (c_pretty_printer *pp)
119 pp_dot (pp);
120 pp->padding = pp_none;
123 void
124 pp_c_ampersand (c_pretty_printer *pp)
126 pp_ampersand (pp);
127 pp->padding = pp_none;
130 void
131 pp_c_star (c_pretty_printer *pp)
133 pp_star (pp);
134 pp->padding = pp_none;
137 void
138 pp_c_arrow (c_pretty_printer *pp)
140 pp_arrow (pp);
141 pp->padding = pp_none;
144 void
145 pp_c_semicolon (c_pretty_printer *pp)
147 pp_semicolon (pp);
148 pp->padding = pp_none;
151 void
152 pp_c_complement (c_pretty_printer *pp)
154 pp_complement (pp);
155 pp->padding = pp_none;
158 void
159 pp_c_exclamation (c_pretty_printer *pp)
161 pp_exclamation (pp);
162 pp->padding = pp_none;
165 /* Print out the external representation of QUALIFIERS. */
167 void
168 pp_c_cv_qualifiers (c_pretty_printer *pp, int qualifiers, bool func_type)
170 const char *p = pp_last_position_in_text (pp);
171 bool previous = false;
173 if (!qualifiers)
174 return;
176 /* The C programming language does not have references, but it is much
177 simpler to handle those here rather than going through the same
178 logic in the C++ pretty-printer. */
179 if (p != NULL && (*p == '*' || *p == '&'))
180 pp_c_whitespace (pp);
182 if (qualifiers & TYPE_QUAL_CONST)
184 pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const");
185 previous = true;
188 if (qualifiers & TYPE_QUAL_VOLATILE)
190 if (previous)
191 pp_c_whitespace (pp);
192 pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile");
193 previous = true;
196 if (qualifiers & TYPE_QUAL_RESTRICT)
198 if (previous)
199 pp_c_whitespace (pp);
200 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
201 ? "restrict" : "__restrict__"));
205 /* Pretty-print T using the type-cast notation '( type-name )'. */
207 static void
208 pp_c_type_cast (c_pretty_printer *pp, tree t)
210 pp_c_left_paren (pp);
211 pp_type_id (pp, t);
212 pp_c_right_paren (pp);
215 /* We're about to pretty-print a pointer type as indicated by T.
216 Output a whitespace, if needed, preparing for subsequent output. */
218 void
219 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
221 if (POINTER_TYPE_P (t))
223 tree pointee = strip_pointer_operator (TREE_TYPE (t));
224 if (TREE_CODE (pointee) != ARRAY_TYPE
225 && TREE_CODE (pointee) != FUNCTION_TYPE)
226 pp_c_whitespace (pp);
231 /* Declarations. */
233 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
234 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
235 of its type. Take care of possible extensions.
237 type-qualifier-list:
238 type-qualifier
239 type-qualifier-list type-qualifier
241 type-qualifier:
242 const
243 restrict -- C99
244 __restrict__ -- GNU C
245 address-space-qualifier -- GNU C
246 volatile
248 address-space-qualifier:
249 identifier -- GNU C */
251 void
252 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
254 int qualifiers;
256 if (!t || t == error_mark_node)
257 return;
259 if (!TYPE_P (t))
260 t = TREE_TYPE (t);
262 qualifiers = TYPE_QUALS (t);
263 pp_c_cv_qualifiers (pp, qualifiers,
264 TREE_CODE (t) == FUNCTION_TYPE);
266 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
268 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
269 pp_c_identifier (pp, as);
273 /* pointer:
274 * type-qualifier-list(opt)
275 * type-qualifier-list(opt) pointer */
277 static void
278 pp_c_pointer (c_pretty_printer *pp, tree t)
280 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
281 t = TREE_TYPE (t);
282 switch (TREE_CODE (t))
284 case POINTER_TYPE:
285 /* It is easier to handle C++ reference types here. */
286 case REFERENCE_TYPE:
287 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
288 pp_c_pointer (pp, TREE_TYPE (t));
289 if (TREE_CODE (t) == POINTER_TYPE)
290 pp_c_star (pp);
291 else
292 pp_c_ampersand (pp);
293 pp_c_type_qualifier_list (pp, t);
294 break;
296 /* ??? This node is now in GENERIC and so shouldn't be here. But
297 we'll fix that later. */
298 case DECL_EXPR:
299 pp_declaration (pp, DECL_EXPR_DECL (t));
300 pp_needs_newline (pp) = true;
301 break;
303 default:
304 pp_unsupported_tree (pp, t);
308 /* type-specifier:
309 void
310 char
311 short
313 long
314 float
315 double
316 signed
317 unsigned
318 _Bool -- C99
319 _Complex -- C99
320 _Imaginary -- C99
321 struct-or-union-specifier
322 enum-specifier
323 typedef-name.
325 GNU extensions.
326 simple-type-specifier:
327 __complex__
328 __vector__ */
330 void
331 pp_c_type_specifier (c_pretty_printer *pp, tree t)
333 const enum tree_code code = TREE_CODE (t);
334 switch (code)
336 case ERROR_MARK:
337 pp->translate_string ("<type-error>");
338 break;
340 case IDENTIFIER_NODE:
341 pp_c_identifier (pp, IDENTIFIER_POINTER (t));
342 break;
344 case VOID_TYPE:
345 case BOOLEAN_TYPE:
346 case INTEGER_TYPE:
347 case REAL_TYPE:
348 case FIXED_POINT_TYPE:
349 if (TYPE_NAME (t))
351 t = TYPE_NAME (t);
352 pp_c_type_specifier (pp, t);
354 else
356 int prec = TYPE_PRECISION (t);
357 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
358 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_SATURATING (t));
359 else
360 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
361 if (TYPE_NAME (t))
363 pp_c_type_specifier (pp, t);
364 if (TYPE_PRECISION (t) != prec)
366 pp_colon (pp);
367 pp_decimal_int (pp, prec);
370 else
372 switch (code)
374 case INTEGER_TYPE:
375 pp->translate_string (TYPE_UNSIGNED (t)
376 ? "<unnamed-unsigned:"
377 : "<unnamed-signed:");
378 break;
379 case REAL_TYPE:
380 pp->translate_string ("<unnamed-float:");
381 break;
382 case FIXED_POINT_TYPE:
383 pp->translate_string ("<unnamed-fixed:");
384 break;
385 default:
386 gcc_unreachable ();
388 pp_decimal_int (pp, prec);
389 pp_greater (pp);
392 break;
394 case TYPE_DECL:
395 if (DECL_NAME (t))
396 pp_id_expression (pp, t);
397 else
398 pp->translate_string ("<typedef-error>");
399 break;
401 case UNION_TYPE:
402 case RECORD_TYPE:
403 case ENUMERAL_TYPE:
404 if (code == UNION_TYPE)
405 pp_c_ws_string (pp, "union");
406 else if (code == RECORD_TYPE)
407 pp_c_ws_string (pp, "struct");
408 else if (code == ENUMERAL_TYPE)
409 pp_c_ws_string (pp, "enum");
410 else
411 pp->translate_string ("<tag-error>");
413 if (TYPE_NAME (t))
414 pp_id_expression (pp, TYPE_NAME (t));
415 else
416 pp->translate_string ("<anonymous>");
417 break;
419 default:
420 pp_unsupported_tree (pp, t);
421 break;
425 /* specifier-qualifier-list:
426 type-specifier specifier-qualifier-list-opt
427 type-qualifier specifier-qualifier-list-opt
430 Implementation note: Because of the non-linearities in array or
431 function declarations, this routine prints not just the
432 specifier-qualifier-list of such entities or types of such entities,
433 but also the 'pointer' production part of their declarators. The
434 remaining part is done by pp_declarator or pp_abstract_declarator. */
436 void
437 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
439 const enum tree_code code = TREE_CODE (t);
441 if (!(pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
442 pp_c_type_qualifier_list (pp, t);
443 switch (code)
445 case REFERENCE_TYPE:
446 case POINTER_TYPE:
448 /* Get the types-specifier of this type. */
449 tree pointee = strip_pointer_operator (TREE_TYPE (t));
450 pp_c_specifier_qualifier_list (pp, pointee);
451 if (TREE_CODE (pointee) == ARRAY_TYPE
452 || TREE_CODE (pointee) == FUNCTION_TYPE)
454 pp_c_whitespace (pp);
455 pp_c_left_paren (pp);
456 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
458 else if (!c_dialect_cxx ())
459 pp_c_whitespace (pp);
460 pp_ptr_operator (pp, t);
462 break;
464 case FUNCTION_TYPE:
465 case ARRAY_TYPE:
466 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
467 break;
469 case VECTOR_TYPE:
470 case COMPLEX_TYPE:
471 if (code == COMPLEX_TYPE)
472 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
473 ? "_Complex" : "__complex__"));
474 else if (code == VECTOR_TYPE)
476 pp_c_ws_string (pp, "__vector");
477 pp_c_left_paren (pp);
478 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
479 pp_c_right_paren (pp);
480 pp_c_whitespace (pp);
482 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
483 break;
485 default:
486 pp_simple_type_specifier (pp, t);
487 break;
489 if ((pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
490 pp_c_type_qualifier_list (pp, t);
493 /* parameter-type-list:
494 parameter-list
495 parameter-list , ...
497 parameter-list:
498 parameter-declaration
499 parameter-list , parameter-declaration
501 parameter-declaration:
502 declaration-specifiers declarator
503 declaration-specifiers abstract-declarator(opt) */
505 void
506 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
508 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
509 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
510 pp_c_left_paren (pp);
511 if (parms == void_list_node)
512 pp_c_ws_string (pp, "void");
513 else
515 bool first = true;
516 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
518 if (!first)
519 pp_separate_with (pp, ',');
520 first = false;
521 pp_declaration_specifiers
522 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
523 if (want_parm_decl)
524 pp_declarator (pp, parms);
525 else
526 pp_abstract_declarator (pp, TREE_VALUE (parms));
529 pp_c_right_paren (pp);
532 /* abstract-declarator:
533 pointer
534 pointer(opt) direct-abstract-declarator */
536 void
537 c_pretty_printer::abstract_declarator (tree t)
539 if (TREE_CODE (t) == POINTER_TYPE)
541 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
542 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
543 pp_c_right_paren (this);
544 t = TREE_TYPE (t);
547 direct_abstract_declarator (t);
550 /* direct-abstract-declarator:
551 ( abstract-declarator )
552 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
553 direct-abstract-declarator(opt) [ * ]
554 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
556 void
557 c_pretty_printer::direct_abstract_declarator (tree t)
559 switch (TREE_CODE (t))
561 case POINTER_TYPE:
562 abstract_declarator (t);
563 break;
565 case FUNCTION_TYPE:
566 pp_c_parameter_type_list (this, t);
567 direct_abstract_declarator (TREE_TYPE (t));
568 break;
570 case ARRAY_TYPE:
571 pp_c_left_bracket (this);
572 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
574 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
575 tree type = TREE_TYPE (maxval);
577 if (host_integerp (maxval, 0))
578 pp_wide_integer (this, tree_low_cst (maxval, 0) + 1);
579 else
580 pp_expression (this, fold_build2 (PLUS_EXPR, type, maxval,
581 build_int_cst (type, 1)));
583 pp_c_right_bracket (this);
584 direct_abstract_declarator (TREE_TYPE (t));
585 break;
587 case IDENTIFIER_NODE:
588 case VOID_TYPE:
589 case BOOLEAN_TYPE:
590 case INTEGER_TYPE:
591 case REAL_TYPE:
592 case FIXED_POINT_TYPE:
593 case ENUMERAL_TYPE:
594 case RECORD_TYPE:
595 case UNION_TYPE:
596 case VECTOR_TYPE:
597 case COMPLEX_TYPE:
598 case TYPE_DECL:
599 break;
601 default:
602 pp_unsupported_tree (this, t);
603 break;
607 /* type-name:
608 specifier-qualifier-list abstract-declarator(opt) */
610 void
611 pp_c_type_id (c_pretty_printer *pp, tree t)
613 pp_c_specifier_qualifier_list (pp, t);
614 pp_abstract_declarator (pp, t);
617 /* storage-class-specifier:
618 typedef
619 extern
620 static
621 auto
622 register */
624 void
625 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
627 if (TREE_CODE (t) == TYPE_DECL)
628 pp_c_ws_string (pp, "typedef");
629 else if (DECL_P (t))
631 if (DECL_REGISTER (t))
632 pp_c_ws_string (pp, "register");
633 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
634 pp_c_ws_string (pp, "static");
638 /* function-specifier:
639 inline */
641 void
642 c_pretty_printer::function_specifier (tree t)
644 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
645 pp_c_ws_string (this, "inline");
648 /* declaration-specifiers:
649 storage-class-specifier declaration-specifiers(opt)
650 type-specifier declaration-specifiers(opt)
651 type-qualifier declaration-specifiers(opt)
652 function-specifier declaration-specifiers(opt) */
654 void
655 c_pretty_printer::declaration_specifiers (tree t)
657 pp_storage_class_specifier (this, t);
658 pp_function_specifier (this, t);
659 pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t);
662 /* direct-declarator
663 identifier
664 ( declarator )
665 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
666 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
667 direct-declarator [ type-qualifier-list static assignment-expression ]
668 direct-declarator [ type-qualifier-list * ]
669 direct-declarator ( parameter-type-list )
670 direct-declarator ( identifier-list(opt) ) */
672 void
673 c_pretty_printer::direct_declarator (tree t)
675 switch (TREE_CODE (t))
677 case VAR_DECL:
678 case PARM_DECL:
679 case TYPE_DECL:
680 case FIELD_DECL:
681 case LABEL_DECL:
682 pp_c_space_for_pointer_operator (this, TREE_TYPE (t));
683 pp_c_tree_decl_identifier (this, t);
684 break;
686 case ARRAY_TYPE:
687 case POINTER_TYPE:
688 abstract_declarator (TREE_TYPE (t));
689 break;
691 case FUNCTION_TYPE:
692 pp_parameter_list (this, t);
693 abstract_declarator (TREE_TYPE (t));
694 break;
696 case FUNCTION_DECL:
697 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
698 pp_c_tree_decl_identifier (this, t);
699 if (flags & pp_c_flag_abstract)
700 abstract_declarator (TREE_TYPE (t));
701 else
703 pp_parameter_list (this, t);
704 abstract_declarator (TREE_TYPE (TREE_TYPE (t)));
706 break;
708 case INTEGER_TYPE:
709 case REAL_TYPE:
710 case FIXED_POINT_TYPE:
711 case ENUMERAL_TYPE:
712 case UNION_TYPE:
713 case RECORD_TYPE:
714 break;
716 default:
717 pp_unsupported_tree (this, t);
718 break;
723 /* declarator:
724 pointer(opt) direct-declarator */
726 void
727 c_pretty_printer::declarator (tree t)
729 switch (TREE_CODE (t))
731 case INTEGER_TYPE:
732 case REAL_TYPE:
733 case FIXED_POINT_TYPE:
734 case ENUMERAL_TYPE:
735 case UNION_TYPE:
736 case RECORD_TYPE:
737 break;
739 case VAR_DECL:
740 case PARM_DECL:
741 case FIELD_DECL:
742 case ARRAY_TYPE:
743 case FUNCTION_TYPE:
744 case FUNCTION_DECL:
745 case TYPE_DECL:
746 pp_direct_declarator (this, t);
747 break;
750 default:
751 pp_unsupported_tree (this, t);
752 break;
756 /* declaration:
757 declaration-specifiers init-declarator-list(opt) ; */
759 void
760 c_pretty_printer::declaration (tree t)
762 declaration_specifiers (t);
763 pp_c_init_declarator (this, t);
766 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
768 void
769 pp_c_attributes (c_pretty_printer *pp, tree attributes)
771 if (attributes == NULL_TREE)
772 return;
774 pp_c_ws_string (pp, "__attribute__");
775 pp_c_left_paren (pp);
776 pp_c_left_paren (pp);
777 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
779 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
780 if (TREE_VALUE (attributes))
781 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
783 if (TREE_CHAIN (attributes))
784 pp_separate_with (pp, ',');
786 pp_c_right_paren (pp);
787 pp_c_right_paren (pp);
790 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
791 marked to be displayed on disgnostic. */
793 void
794 pp_c_attributes_display (c_pretty_printer *pp, tree a)
796 bool is_first = true;
798 if (a == NULL_TREE)
799 return;
801 for (; a != NULL_TREE; a = TREE_CHAIN (a))
803 const struct attribute_spec *as;
804 as = lookup_attribute_spec (TREE_PURPOSE (a));
805 if (!as || as->affects_type_identity == false)
806 continue;
807 if (is_first)
809 pp_c_ws_string (pp, "__attribute__");
810 pp_c_left_paren (pp);
811 pp_c_left_paren (pp);
812 is_first = false;
814 else
816 pp_separate_with (pp, ',');
818 pp_tree_identifier (pp, TREE_PURPOSE (a));
819 if (TREE_VALUE (a))
820 pp_c_call_argument_list (pp, TREE_VALUE (a));
823 if (!is_first)
825 pp_c_right_paren (pp);
826 pp_c_right_paren (pp);
827 pp_c_whitespace (pp);
831 /* function-definition:
832 declaration-specifiers declarator compound-statement */
834 void
835 pp_c_function_definition (c_pretty_printer *pp, tree t)
837 pp_declaration_specifiers (pp, t);
838 pp_declarator (pp, t);
839 pp_needs_newline (pp) = true;
840 pp->statement (DECL_SAVED_TREE (t));
841 pp_newline_and_flush (pp);
845 /* Expressions. */
847 /* Print out a c-char. This is called solely for characters which are
848 in the *target* execution character set. We ought to convert them
849 back to the *host* execution character set before printing, but we
850 have no way to do this at present. A decent compromise is to print
851 all characters as if they were in the host execution character set,
852 and not attempt to recover any named escape characters, but render
853 all unprintables as octal escapes. If the host and target character
854 sets are the same, this produces relatively readable output. If they
855 are not the same, strings may appear as gibberish, but that's okay
856 (in fact, it may well be what the reader wants, e.g. if they are looking
857 to see if conversion to the target character set happened correctly).
859 A special case: we need to prefix \, ", and ' with backslashes. It is
860 correct to do so for the *host*'s \, ", and ', because the rest of the
861 file appears in the host character set. */
863 static void
864 pp_c_char (c_pretty_printer *pp, int c)
866 if (ISPRINT (c))
868 switch (c)
870 case '\\': pp_string (pp, "\\\\"); break;
871 case '\'': pp_string (pp, "\\\'"); break;
872 case '\"': pp_string (pp, "\\\""); break;
873 default: pp_character (pp, c);
876 else
877 pp_scalar (pp, "\\%03o", (unsigned) c);
880 /* Print out a STRING literal. */
882 void
883 pp_c_string_literal (c_pretty_printer *pp, tree s)
885 const char *p = TREE_STRING_POINTER (s);
886 int n = TREE_STRING_LENGTH (s) - 1;
887 int i;
888 pp_doublequote (pp);
889 for (i = 0; i < n; ++i)
890 pp_c_char (pp, p[i]);
891 pp_doublequote (pp);
894 /* Pretty-print an INTEGER literal. */
896 static void
897 pp_c_integer_constant (c_pretty_printer *pp, tree i)
899 /* We are going to compare the type of I to other types using
900 pointer comparison so we need to use its canonical type. */
901 tree type =
902 TYPE_CANONICAL (TREE_TYPE (i))
903 ? TYPE_CANONICAL (TREE_TYPE (i))
904 : TREE_TYPE (i);
906 if (host_integerp (i, 0))
907 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
908 else if (host_integerp (i, 1))
909 pp_unsigned_wide_integer (pp, TREE_INT_CST_LOW (i));
910 else
912 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (i);
913 HOST_WIDE_INT high = TREE_INT_CST_HIGH (i);
914 if (tree_int_cst_sgn (i) < 0)
916 pp_minus (pp);
917 high = ~high + !low;
918 low = -low;
920 sprintf (pp_buffer (pp)->digit_buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
921 (unsigned HOST_WIDE_INT) high, (unsigned HOST_WIDE_INT) low);
922 pp_string (pp, pp_buffer (pp)->digit_buffer);
924 if (TYPE_UNSIGNED (type))
925 pp_character (pp, 'u');
926 if (type == long_integer_type_node || type == long_unsigned_type_node)
927 pp_character (pp, 'l');
928 else if (type == long_long_integer_type_node
929 || type == long_long_unsigned_type_node)
930 pp_string (pp, "ll");
931 else if (type == int128_integer_type_node
932 || type == int128_unsigned_type_node)
933 pp_string (pp, "I128");
936 /* Print out a CHARACTER literal. */
938 static void
939 pp_c_character_constant (c_pretty_printer *pp, tree c)
941 tree type = TREE_TYPE (c);
942 if (type == wchar_type_node)
943 pp_character (pp, 'L');
944 pp_quote (pp);
945 if (host_integerp (c, TYPE_UNSIGNED (type)))
946 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
947 else
948 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
949 pp_quote (pp);
952 /* Print out a BOOLEAN literal. */
954 static void
955 pp_c_bool_constant (c_pretty_printer *pp, tree b)
957 if (b == boolean_false_node)
959 if (c_dialect_cxx ())
960 pp_c_ws_string (pp, "false");
961 else if (flag_isoc99)
962 pp_c_ws_string (pp, "_False");
963 else
964 pp_unsupported_tree (pp, b);
966 else if (b == boolean_true_node)
968 if (c_dialect_cxx ())
969 pp_c_ws_string (pp, "true");
970 else if (flag_isoc99)
971 pp_c_ws_string (pp, "_True");
972 else
973 pp_unsupported_tree (pp, b);
975 else if (TREE_CODE (b) == INTEGER_CST)
976 pp_c_integer_constant (pp, b);
977 else
978 pp_unsupported_tree (pp, b);
981 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
982 false; that means the value was obtained by a cast, in which case
983 print out the type-id part of the cast-expression -- the casted value
984 is then printed by pp_c_integer_literal. */
986 static bool
987 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
989 bool value_is_named = true;
990 tree type = TREE_TYPE (e);
991 tree value;
993 /* Find the name of this constant. */
994 for (value = TYPE_VALUES (type);
995 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
996 value = TREE_CHAIN (value))
999 if (value != NULL_TREE)
1000 pp_id_expression (pp, TREE_PURPOSE (value));
1001 else
1003 /* Value must have been cast. */
1004 pp_c_type_cast (pp, type);
1005 value_is_named = false;
1008 return value_is_named;
1011 /* Print out a REAL value as a decimal-floating-constant. */
1013 static void
1014 pp_c_floating_constant (c_pretty_printer *pp, tree r)
1016 const struct real_format *fmt
1017 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
1019 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
1020 bool is_decimal = floating_cst.decimal;
1022 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1023 log10(2) to 7 significant digits. */
1024 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
1026 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
1027 sizeof (pp_buffer (pp)->digit_buffer),
1028 max_digits10, 1);
1030 pp_string (pp, pp_buffer(pp)->digit_buffer);
1031 if (TREE_TYPE (r) == float_type_node)
1032 pp_character (pp, 'f');
1033 else if (TREE_TYPE (r) == long_double_type_node)
1034 pp_character (pp, 'l');
1035 else if (TREE_TYPE (r) == dfloat128_type_node)
1036 pp_string (pp, "dl");
1037 else if (TREE_TYPE (r) == dfloat64_type_node)
1038 pp_string (pp, "dd");
1039 else if (TREE_TYPE (r) == dfloat32_type_node)
1040 pp_string (pp, "df");
1043 /* Print out a FIXED value as a decimal-floating-constant. */
1045 static void
1046 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1048 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1049 sizeof (pp_buffer (pp)->digit_buffer));
1050 pp_string (pp, pp_buffer(pp)->digit_buffer);
1053 /* Pretty-print a compound literal expression. GNU extensions include
1054 vector constants. */
1056 static void
1057 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1059 tree type = TREE_TYPE (e);
1060 pp_c_type_cast (pp, type);
1062 switch (TREE_CODE (type))
1064 case RECORD_TYPE:
1065 case UNION_TYPE:
1066 case ARRAY_TYPE:
1067 case VECTOR_TYPE:
1068 case COMPLEX_TYPE:
1069 pp_c_brace_enclosed_initializer_list (pp, e);
1070 break;
1072 default:
1073 pp_unsupported_tree (pp, e);
1074 break;
1078 /* Pretty-print a COMPLEX_EXPR expression. */
1080 static void
1081 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1083 /* Handle a few common special cases, otherwise fallback
1084 to printing it as compound literal. */
1085 tree type = TREE_TYPE (e);
1086 tree realexpr = TREE_OPERAND (e, 0);
1087 tree imagexpr = TREE_OPERAND (e, 1);
1089 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1090 if (TREE_CODE (realexpr) == NOP_EXPR
1091 && TREE_CODE (imagexpr) == NOP_EXPR
1092 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1093 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1094 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1095 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1096 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1097 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1099 pp_c_type_cast (pp, type);
1100 pp_expression (pp, TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1101 return;
1104 /* Cast of an scalar expression to COMPLEX_TYPE. */
1105 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1106 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1108 pp_c_type_cast (pp, type);
1109 if (TREE_CODE (realexpr) == NOP_EXPR)
1110 realexpr = TREE_OPERAND (realexpr, 0);
1111 pp_expression (pp, realexpr);
1112 return;
1115 pp_c_compound_literal (pp, e);
1118 /* constant:
1119 integer-constant
1120 floating-constant
1121 fixed-point-constant
1122 enumeration-constant
1123 character-constant */
1125 void
1126 c_pretty_printer::constant (tree e)
1128 const enum tree_code code = TREE_CODE (e);
1130 switch (code)
1132 case INTEGER_CST:
1134 tree type = TREE_TYPE (e);
1135 if (type == boolean_type_node)
1136 pp_c_bool_constant (this, e);
1137 else if (type == char_type_node)
1138 pp_c_character_constant (this, e);
1139 else if (TREE_CODE (type) == ENUMERAL_TYPE
1140 && pp_c_enumeration_constant (this, e))
1142 else
1143 pp_c_integer_constant (this, e);
1145 break;
1147 case REAL_CST:
1148 pp_c_floating_constant (this, e);
1149 break;
1151 case FIXED_CST:
1152 pp_c_fixed_constant (this, e);
1153 break;
1155 case STRING_CST:
1156 pp_c_string_literal (this, e);
1157 break;
1159 case COMPLEX_CST:
1160 /* Sometimes, we are confused and we think a complex literal
1161 is a constant. Such thing is a compound literal which
1162 grammatically belongs to postfix-expr production. */
1163 pp_c_compound_literal (this, e);
1164 break;
1166 default:
1167 pp_unsupported_tree (this, e);
1168 break;
1172 /* Pretty-print a string such as an identifier, without changing its
1173 encoding, preceded by whitespace is necessary. */
1175 void
1176 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1178 pp_c_maybe_whitespace (pp);
1179 pp_string (pp, str);
1180 pp->padding = pp_before;
1183 void
1184 c_pretty_printer::translate_string (const char *gmsgid)
1186 if (pp_translate_identifiers (this))
1187 pp_c_ws_string (this, _(gmsgid));
1188 else
1189 pp_c_ws_string (this, gmsgid);
1192 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1193 that need converting to the locale encoding, preceded by whitespace
1194 is necessary. */
1196 void
1197 pp_c_identifier (c_pretty_printer *pp, const char *id)
1199 pp_c_maybe_whitespace (pp);
1200 pp_identifier (pp, id);
1201 pp->padding = pp_before;
1204 /* Pretty-print a C primary-expression.
1205 primary-expression:
1206 identifier
1207 constant
1208 string-literal
1209 ( expression ) */
1211 void
1212 c_pretty_printer::primary_expression (tree e)
1214 switch (TREE_CODE (e))
1216 case VAR_DECL:
1217 case PARM_DECL:
1218 case FIELD_DECL:
1219 case CONST_DECL:
1220 case FUNCTION_DECL:
1221 case LABEL_DECL:
1222 pp_c_tree_decl_identifier (this, e);
1223 break;
1225 case IDENTIFIER_NODE:
1226 pp_c_tree_identifier (this, e);
1227 break;
1229 case ERROR_MARK:
1230 translate_string ("<erroneous-expression>");
1231 break;
1233 case RESULT_DECL:
1234 translate_string ("<return-value>");
1235 break;
1237 case INTEGER_CST:
1238 case REAL_CST:
1239 case FIXED_CST:
1240 case STRING_CST:
1241 constant (e);
1242 break;
1244 case TARGET_EXPR:
1245 pp_c_ws_string (this, "__builtin_memcpy");
1246 pp_c_left_paren (this);
1247 pp_ampersand (this);
1248 primary_expression (TREE_OPERAND (e, 0));
1249 pp_separate_with (this, ',');
1250 pp_ampersand (this);
1251 pp_initializer (this, TREE_OPERAND (e, 1));
1252 if (TREE_OPERAND (e, 2))
1254 pp_separate_with (this, ',');
1255 expression (TREE_OPERAND (e, 2));
1257 pp_c_right_paren (this);
1258 break;
1260 default:
1261 /* FIXME: Make sure we won't get into an infinite loop. */
1262 pp_c_left_paren (this);
1263 pp_expression (this, e);
1264 pp_c_right_paren (this);
1265 break;
1269 /* Print out a C initializer -- also support C compound-literals.
1270 initializer:
1271 assignment-expression:
1272 { initializer-list }
1273 { initializer-list , } */
1275 static void
1276 pp_c_initializer (c_pretty_printer *pp, tree e)
1278 if (TREE_CODE (e) == CONSTRUCTOR)
1279 pp_c_brace_enclosed_initializer_list (pp, e);
1280 else
1281 pp_expression (pp, e);
1284 /* init-declarator:
1285 declarator:
1286 declarator = initializer */
1288 void
1289 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1291 pp_declarator (pp, t);
1292 /* We don't want to output function definitions here. There are handled
1293 elsewhere (and the syntactic form is bogus anyway). */
1294 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1296 tree init = DECL_INITIAL (t);
1297 /* This C++ bit is handled here because it is easier to do so.
1298 In templates, the C++ parser builds a TREE_LIST for a
1299 direct-initialization; the TREE_PURPOSE is the variable to
1300 initialize and the TREE_VALUE is the initializer. */
1301 if (TREE_CODE (init) == TREE_LIST)
1303 pp_c_left_paren (pp);
1304 pp_expression (pp, TREE_VALUE (init));
1305 pp_right_paren (pp);
1307 else
1309 pp_space (pp);
1310 pp_equal (pp);
1311 pp_space (pp);
1312 pp_c_initializer (pp, init);
1317 /* initializer-list:
1318 designation(opt) initializer
1319 initializer-list , designation(opt) initializer
1321 designation:
1322 designator-list =
1324 designator-list:
1325 designator
1326 designator-list designator
1328 designator:
1329 [ constant-expression ]
1330 identifier */
1332 static void
1333 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1335 tree type = TREE_TYPE (e);
1336 const enum tree_code code = TREE_CODE (type);
1338 if (TREE_CODE (e) == CONSTRUCTOR)
1340 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1341 return;
1344 switch (code)
1346 case RECORD_TYPE:
1347 case UNION_TYPE:
1348 case ARRAY_TYPE:
1350 tree init = TREE_OPERAND (e, 0);
1351 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1353 if (code == RECORD_TYPE || code == UNION_TYPE)
1355 pp_c_dot (pp);
1356 pp_primary_expression (pp, TREE_PURPOSE (init));
1358 else
1360 pp_c_left_bracket (pp);
1361 if (TREE_PURPOSE (init))
1362 pp_constant (pp, TREE_PURPOSE (init));
1363 pp_c_right_bracket (pp);
1365 pp_c_whitespace (pp);
1366 pp_equal (pp);
1367 pp_c_whitespace (pp);
1368 pp_initializer (pp, TREE_VALUE (init));
1369 if (TREE_CHAIN (init))
1370 pp_separate_with (pp, ',');
1373 return;
1375 case VECTOR_TYPE:
1376 if (TREE_CODE (e) == VECTOR_CST)
1378 unsigned i;
1379 for (i = 0; i < VECTOR_CST_NELTS (e); ++i)
1381 if (i > 0)
1382 pp_separate_with (pp, ',');
1383 pp_expression (pp, VECTOR_CST_ELT (e, i));
1386 else
1387 break;
1388 return;
1390 case COMPLEX_TYPE:
1391 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1393 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1394 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1395 pp_separate_with (pp, ',');
1396 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1398 else
1399 break;
1400 return;
1402 default:
1403 break;
1406 pp_unsupported_tree (pp, type);
1409 /* Pretty-print a brace-enclosed initializer-list. */
1411 static void
1412 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1414 pp_c_left_brace (pp);
1415 pp_c_initializer_list (pp, l);
1416 pp_c_right_brace (pp);
1420 /* This is a convenient function, used to bridge gap between C and C++
1421 grammars.
1423 id-expression:
1424 identifier */
1426 void
1427 c_pretty_printer::id_expression (tree t)
1429 switch (TREE_CODE (t))
1431 case VAR_DECL:
1432 case PARM_DECL:
1433 case CONST_DECL:
1434 case TYPE_DECL:
1435 case FUNCTION_DECL:
1436 case FIELD_DECL:
1437 case LABEL_DECL:
1438 pp_c_tree_decl_identifier (this, t);
1439 break;
1441 case IDENTIFIER_NODE:
1442 pp_c_tree_identifier (this, t);
1443 break;
1445 default:
1446 pp_unsupported_tree (this, t);
1447 break;
1451 /* postfix-expression:
1452 primary-expression
1453 postfix-expression [ expression ]
1454 postfix-expression ( argument-expression-list(opt) )
1455 postfix-expression . identifier
1456 postfix-expression -> identifier
1457 postfix-expression ++
1458 postfix-expression --
1459 ( type-name ) { initializer-list }
1460 ( type-name ) { initializer-list , } */
1462 void
1463 c_pretty_printer::postfix_expression (tree e)
1465 enum tree_code code = TREE_CODE (e);
1466 switch (code)
1468 case POSTINCREMENT_EXPR:
1469 case POSTDECREMENT_EXPR:
1470 postfix_expression (TREE_OPERAND (e, 0));
1471 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1472 break;
1474 case ARRAY_REF:
1475 postfix_expression (TREE_OPERAND (e, 0));
1476 pp_c_left_bracket (this);
1477 pp_expression (this, TREE_OPERAND (e, 1));
1478 pp_c_right_bracket (this);
1479 break;
1481 case ARRAY_NOTATION_REF:
1482 postfix_expression (ARRAY_NOTATION_ARRAY (e));
1483 pp_c_left_bracket (this);
1484 pp_expression (this, ARRAY_NOTATION_START (e));
1485 pp_colon (this);
1486 pp_expression (this, ARRAY_NOTATION_LENGTH (e));
1487 pp_colon (this);
1488 pp_expression (this, ARRAY_NOTATION_STRIDE (e));
1489 pp_c_right_bracket (this);
1490 break;
1492 case CALL_EXPR:
1494 call_expr_arg_iterator iter;
1495 tree arg;
1496 postfix_expression (CALL_EXPR_FN (e));
1497 pp_c_left_paren (this);
1498 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1500 pp_expression (this, arg);
1501 if (more_call_expr_args_p (&iter))
1502 pp_separate_with (this, ',');
1504 pp_c_right_paren (this);
1505 break;
1508 case UNORDERED_EXPR:
1509 pp_c_ws_string (this, flag_isoc99
1510 ? "isunordered"
1511 : "__builtin_isunordered");
1512 goto two_args_fun;
1514 case ORDERED_EXPR:
1515 pp_c_ws_string (this, flag_isoc99
1516 ? "!isunordered"
1517 : "!__builtin_isunordered");
1518 goto two_args_fun;
1520 case UNLT_EXPR:
1521 pp_c_ws_string (this, flag_isoc99
1522 ? "!isgreaterequal"
1523 : "!__builtin_isgreaterequal");
1524 goto two_args_fun;
1526 case UNLE_EXPR:
1527 pp_c_ws_string (this, flag_isoc99
1528 ? "!isgreater"
1529 : "!__builtin_isgreater");
1530 goto two_args_fun;
1532 case UNGT_EXPR:
1533 pp_c_ws_string (this, flag_isoc99
1534 ? "!islessequal"
1535 : "!__builtin_islessequal");
1536 goto two_args_fun;
1538 case UNGE_EXPR:
1539 pp_c_ws_string (this, flag_isoc99
1540 ? "!isless"
1541 : "!__builtin_isless");
1542 goto two_args_fun;
1544 case UNEQ_EXPR:
1545 pp_c_ws_string (this, flag_isoc99
1546 ? "!islessgreater"
1547 : "!__builtin_islessgreater");
1548 goto two_args_fun;
1550 case LTGT_EXPR:
1551 pp_c_ws_string (this, flag_isoc99
1552 ? "islessgreater"
1553 : "__builtin_islessgreater");
1554 goto two_args_fun;
1556 two_args_fun:
1557 pp_c_left_paren (this);
1558 pp_expression (this, TREE_OPERAND (e, 0));
1559 pp_separate_with (this, ',');
1560 pp_expression (this, TREE_OPERAND (e, 1));
1561 pp_c_right_paren (this);
1562 break;
1564 case ABS_EXPR:
1565 pp_c_ws_string (this, "__builtin_abs");
1566 pp_c_left_paren (this);
1567 pp_expression (this, TREE_OPERAND (e, 0));
1568 pp_c_right_paren (this);
1569 break;
1571 case COMPONENT_REF:
1573 tree object = TREE_OPERAND (e, 0);
1574 if (TREE_CODE (object) == INDIRECT_REF)
1576 postfix_expression (TREE_OPERAND (object, 0));
1577 pp_c_arrow (this);
1579 else
1581 postfix_expression (object);
1582 pp_c_dot (this);
1584 pp_expression (this, TREE_OPERAND (e, 1));
1586 break;
1588 case BIT_FIELD_REF:
1590 tree type = TREE_TYPE (e);
1592 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1593 if (type
1594 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1596 HOST_WIDE_INT bitpos = tree_low_cst (TREE_OPERAND (e, 2), 0);
1597 HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 0);
1598 if ((bitpos % size) == 0)
1600 pp_c_left_paren (this);
1601 pp_c_left_paren (this);
1602 pp_type_id (this, type);
1603 pp_c_star (this);
1604 pp_c_right_paren (this);
1605 pp_c_ampersand (this);
1606 pp_expression (this, TREE_OPERAND (e, 0));
1607 pp_c_right_paren (this);
1608 pp_c_left_bracket (this);
1609 pp_wide_integer (this, bitpos / size);
1610 pp_c_right_bracket (this);
1611 break;
1614 pp_unsupported_tree (this, e);
1616 break;
1618 case MEM_REF:
1619 expression (e);
1620 break;
1622 case COMPLEX_CST:
1623 case VECTOR_CST:
1624 pp_c_compound_literal (this, e);
1625 break;
1627 case COMPLEX_EXPR:
1628 pp_c_complex_expr (this, e);
1629 break;
1631 case COMPOUND_LITERAL_EXPR:
1632 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1633 /* Fall through. */
1634 case CONSTRUCTOR:
1635 pp_initializer (this, e);
1636 break;
1638 case VA_ARG_EXPR:
1639 pp_c_ws_string (this, "__builtin_va_arg");
1640 pp_c_left_paren (this);
1641 assignment_expression (TREE_OPERAND (e, 0));
1642 pp_separate_with (this, ',');
1643 pp_type_id (this, TREE_TYPE (e));
1644 pp_c_right_paren (this);
1645 break;
1647 case ADDR_EXPR:
1648 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1650 id_expression (TREE_OPERAND (e, 0));
1651 break;
1653 /* else fall through. */
1655 default:
1656 primary_expression (e);
1657 break;
1661 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1663 void
1664 pp_c_expression_list (c_pretty_printer *pp, tree e)
1666 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1668 pp_expression (pp, TREE_VALUE (e));
1669 if (TREE_CHAIN (e))
1670 pp_separate_with (pp, ',');
1674 /* Print out V, which contains the elements of a constructor. */
1676 void
1677 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1679 unsigned HOST_WIDE_INT ix;
1680 tree value;
1682 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1684 pp_expression (pp, value);
1685 if (ix != vec_safe_length (v) - 1)
1686 pp_separate_with (pp, ',');
1690 /* Print out an expression-list in parens, as if it were the argument
1691 list to a function. */
1693 void
1694 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1696 pp_c_left_paren (pp);
1697 if (t && TREE_CODE (t) == TREE_LIST)
1698 pp_c_expression_list (pp, t);
1699 pp_c_right_paren (pp);
1702 /* unary-expression:
1703 postfix-expression
1704 ++ cast-expression
1705 -- cast-expression
1706 unary-operator cast-expression
1707 sizeof unary-expression
1708 sizeof ( type-id )
1710 unary-operator: one of
1711 * & + - ! ~
1713 GNU extensions.
1714 unary-expression:
1715 __alignof__ unary-expression
1716 __alignof__ ( type-id )
1717 __real__ unary-expression
1718 __imag__ unary-expression */
1720 void
1721 c_pretty_printer::unary_expression (tree e)
1723 enum tree_code code = TREE_CODE (e);
1724 switch (code)
1726 case PREINCREMENT_EXPR:
1727 case PREDECREMENT_EXPR:
1728 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
1729 unary_expression (TREE_OPERAND (e, 0));
1730 break;
1732 case ADDR_EXPR:
1733 case INDIRECT_REF:
1734 case NEGATE_EXPR:
1735 case BIT_NOT_EXPR:
1736 case TRUTH_NOT_EXPR:
1737 case CONJ_EXPR:
1738 /* String literal are used by address. */
1739 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1740 pp_ampersand (this);
1741 else if (code == INDIRECT_REF)
1742 pp_c_star (this);
1743 else if (code == NEGATE_EXPR)
1744 pp_minus (this);
1745 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1746 pp_complement (this);
1747 else if (code == TRUTH_NOT_EXPR)
1748 pp_exclamation (this);
1749 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1750 break;
1752 case MEM_REF:
1753 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1754 && integer_zerop (TREE_OPERAND (e, 1)))
1755 expression (TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1756 else
1758 pp_c_star (this);
1759 if (!integer_zerop (TREE_OPERAND (e, 1)))
1761 pp_c_left_paren (this);
1762 if (!integer_onep (TYPE_SIZE_UNIT
1763 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1764 pp_c_type_cast (this, ptr_type_node);
1766 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1767 if (!integer_zerop (TREE_OPERAND (e, 1)))
1769 pp_plus (this);
1770 pp_c_integer_constant (this,
1771 fold_convert (ssizetype,
1772 TREE_OPERAND (e, 1)));
1773 pp_c_right_paren (this);
1776 break;
1778 case REALPART_EXPR:
1779 case IMAGPART_EXPR:
1780 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
1781 pp_c_whitespace (this);
1782 unary_expression (TREE_OPERAND (e, 0));
1783 break;
1785 default:
1786 postfix_expression (e);
1787 break;
1791 /* cast-expression:
1792 unary-expression
1793 ( type-name ) cast-expression */
1795 void
1796 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1798 switch (TREE_CODE (e))
1800 case FLOAT_EXPR:
1801 case FIX_TRUNC_EXPR:
1802 CASE_CONVERT:
1803 case VIEW_CONVERT_EXPR:
1804 pp_c_type_cast (pp, TREE_TYPE (e));
1805 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1806 break;
1808 default:
1809 pp_unary_expression (pp, e);
1813 /* multiplicative-expression:
1814 cast-expression
1815 multiplicative-expression * cast-expression
1816 multiplicative-expression / cast-expression
1817 multiplicative-expression % cast-expression */
1819 void
1820 c_pretty_printer::multiplicative_expression (tree e)
1822 enum tree_code code = TREE_CODE (e);
1823 switch (code)
1825 case MULT_EXPR:
1826 case TRUNC_DIV_EXPR:
1827 case TRUNC_MOD_EXPR:
1828 multiplicative_expression (TREE_OPERAND (e, 0));
1829 pp_c_whitespace (this);
1830 if (code == MULT_EXPR)
1831 pp_c_star (this);
1832 else if (code == TRUNC_DIV_EXPR)
1833 pp_slash (this);
1834 else
1835 pp_modulo (this);
1836 pp_c_whitespace (this);
1837 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
1838 break;
1840 default:
1841 pp_c_cast_expression (this, e);
1842 break;
1846 /* additive-expression:
1847 multiplicative-expression
1848 additive-expression + multiplicative-expression
1849 additive-expression - multiplicative-expression */
1851 static void
1852 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1854 enum tree_code code = TREE_CODE (e);
1855 switch (code)
1857 case POINTER_PLUS_EXPR:
1858 case PLUS_EXPR:
1859 case MINUS_EXPR:
1860 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1861 pp_c_whitespace (pp);
1862 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1863 pp_plus (pp);
1864 else
1865 pp_minus (pp);
1866 pp_c_whitespace (pp);
1867 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1868 break;
1870 default:
1871 pp_multiplicative_expression (pp, e);
1872 break;
1876 /* additive-expression:
1877 additive-expression
1878 shift-expression << additive-expression
1879 shift-expression >> additive-expression */
1881 static void
1882 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1884 enum tree_code code = TREE_CODE (e);
1885 switch (code)
1887 case LSHIFT_EXPR:
1888 case RSHIFT_EXPR:
1889 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1890 pp_c_whitespace (pp);
1891 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1892 pp_c_whitespace (pp);
1893 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1894 break;
1896 default:
1897 pp_c_additive_expression (pp, e);
1901 /* relational-expression:
1902 shift-expression
1903 relational-expression < shift-expression
1904 relational-expression > shift-expression
1905 relational-expression <= shift-expression
1906 relational-expression >= shift-expression */
1908 static void
1909 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1911 enum tree_code code = TREE_CODE (e);
1912 switch (code)
1914 case LT_EXPR:
1915 case GT_EXPR:
1916 case LE_EXPR:
1917 case GE_EXPR:
1918 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1919 pp_c_whitespace (pp);
1920 if (code == LT_EXPR)
1921 pp_less (pp);
1922 else if (code == GT_EXPR)
1923 pp_greater (pp);
1924 else if (code == LE_EXPR)
1925 pp_less_equal (pp);
1926 else if (code == GE_EXPR)
1927 pp_greater_equal (pp);
1928 pp_c_whitespace (pp);
1929 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1930 break;
1932 default:
1933 pp_c_shift_expression (pp, e);
1934 break;
1938 /* equality-expression:
1939 relational-expression
1940 equality-expression == relational-expression
1941 equality-equality != relational-expression */
1943 static void
1944 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1946 enum tree_code code = TREE_CODE (e);
1947 switch (code)
1949 case EQ_EXPR:
1950 case NE_EXPR:
1951 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1952 pp_c_whitespace (pp);
1953 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1954 pp_c_whitespace (pp);
1955 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1956 break;
1958 default:
1959 pp_c_relational_expression (pp, e);
1960 break;
1964 /* AND-expression:
1965 equality-expression
1966 AND-expression & equality-equality */
1968 static void
1969 pp_c_and_expression (c_pretty_printer *pp, tree e)
1971 if (TREE_CODE (e) == BIT_AND_EXPR)
1973 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1974 pp_c_whitespace (pp);
1975 pp_ampersand (pp);
1976 pp_c_whitespace (pp);
1977 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1979 else
1980 pp_c_equality_expression (pp, e);
1983 /* exclusive-OR-expression:
1984 AND-expression
1985 exclusive-OR-expression ^ AND-expression */
1987 static void
1988 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1990 if (TREE_CODE (e) == BIT_XOR_EXPR
1991 || TREE_CODE (e) == TRUTH_XOR_EXPR)
1993 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1994 if (TREE_CODE (e) == BIT_XOR_EXPR)
1995 pp_c_maybe_whitespace (pp);
1996 else
1997 pp_c_whitespace (pp);
1998 pp_carret (pp);
1999 pp_c_whitespace (pp);
2000 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2002 else
2003 pp_c_and_expression (pp, e);
2006 /* inclusive-OR-expression:
2007 exclusive-OR-expression
2008 inclusive-OR-expression | exclusive-OR-expression */
2010 static void
2011 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2013 if (TREE_CODE (e) == BIT_IOR_EXPR)
2015 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2016 pp_c_whitespace (pp);
2017 pp_bar (pp);
2018 pp_c_whitespace (pp);
2019 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2021 else
2022 pp_c_exclusive_or_expression (pp, e);
2025 /* logical-AND-expression:
2026 inclusive-OR-expression
2027 logical-AND-expression && inclusive-OR-expression */
2029 static void
2030 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2032 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2033 || TREE_CODE (e) == TRUTH_AND_EXPR)
2035 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2036 pp_c_whitespace (pp);
2037 pp_ampersand_ampersand (pp);
2038 pp_c_whitespace (pp);
2039 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2041 else
2042 pp_c_inclusive_or_expression (pp, e);
2045 /* logical-OR-expression:
2046 logical-AND-expression
2047 logical-OR-expression || logical-AND-expression */
2049 void
2050 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2052 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2053 || TREE_CODE (e) == TRUTH_OR_EXPR)
2055 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2056 pp_c_whitespace (pp);
2057 pp_bar_bar (pp);
2058 pp_c_whitespace (pp);
2059 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2061 else
2062 pp_c_logical_and_expression (pp, e);
2065 /* conditional-expression:
2066 logical-OR-expression
2067 logical-OR-expression ? expression : conditional-expression */
2069 void
2070 c_pretty_printer::conditional_expression (tree e)
2072 if (TREE_CODE (e) == COND_EXPR)
2074 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2075 pp_c_whitespace (this);
2076 pp_question (this);
2077 pp_c_whitespace (this);
2078 pp_expression (this, TREE_OPERAND (e, 1));
2079 pp_c_whitespace (this);
2080 pp_colon (this);
2081 pp_c_whitespace (this);
2082 conditional_expression (TREE_OPERAND (e, 2));
2084 else
2085 pp_c_logical_or_expression (this, e);
2089 /* assignment-expression:
2090 conditional-expression
2091 unary-expression assignment-operator assignment-expression
2093 assignment-expression: one of
2094 = *= /= %= += -= >>= <<= &= ^= |= */
2096 void
2097 c_pretty_printer::assignment_expression (tree e)
2099 if (TREE_CODE (e) == MODIFY_EXPR
2100 || TREE_CODE (e) == INIT_EXPR)
2102 unary_expression (TREE_OPERAND (e, 0));
2103 pp_c_whitespace (this);
2104 pp_equal (this);
2105 pp_space (this);
2106 pp_expression (this, TREE_OPERAND (e, 1));
2108 else
2109 conditional_expression (e);
2112 /* expression:
2113 assignment-expression
2114 expression , assignment-expression
2116 Implementation note: instead of going through the usual recursion
2117 chain, I take the liberty of dispatching nodes to the appropriate
2118 functions. This makes some redundancy, but it worths it. That also
2119 prevents a possible infinite recursion between pp_primary_expression ()
2120 and pp_expression (). */
2122 void
2123 c_pretty_printer::expression (tree e)
2125 switch (TREE_CODE (e))
2127 case INTEGER_CST:
2128 pp_c_integer_constant (this, e);
2129 break;
2131 case REAL_CST:
2132 pp_c_floating_constant (this, e);
2133 break;
2135 case FIXED_CST:
2136 pp_c_fixed_constant (this, e);
2137 break;
2139 case STRING_CST:
2140 pp_c_string_literal (this, e);
2141 break;
2143 case IDENTIFIER_NODE:
2144 case FUNCTION_DECL:
2145 case VAR_DECL:
2146 case CONST_DECL:
2147 case PARM_DECL:
2148 case RESULT_DECL:
2149 case FIELD_DECL:
2150 case LABEL_DECL:
2151 case ERROR_MARK:
2152 primary_expression (e);
2153 break;
2155 case SSA_NAME:
2156 if (SSA_NAME_VAR (e)
2157 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2158 expression (SSA_NAME_VAR (e));
2159 else
2160 translate_string ("<unknown>");
2161 break;
2163 case POSTINCREMENT_EXPR:
2164 case POSTDECREMENT_EXPR:
2165 case ARRAY_REF:
2166 case ARRAY_NOTATION_REF:
2167 case CALL_EXPR:
2168 case COMPONENT_REF:
2169 case BIT_FIELD_REF:
2170 case COMPLEX_CST:
2171 case COMPLEX_EXPR:
2172 case VECTOR_CST:
2173 case ORDERED_EXPR:
2174 case UNORDERED_EXPR:
2175 case LTGT_EXPR:
2176 case UNEQ_EXPR:
2177 case UNLE_EXPR:
2178 case UNLT_EXPR:
2179 case UNGE_EXPR:
2180 case UNGT_EXPR:
2181 case ABS_EXPR:
2182 case CONSTRUCTOR:
2183 case COMPOUND_LITERAL_EXPR:
2184 case VA_ARG_EXPR:
2185 postfix_expression (e);
2186 break;
2188 case CONJ_EXPR:
2189 case ADDR_EXPR:
2190 case INDIRECT_REF:
2191 case MEM_REF:
2192 case NEGATE_EXPR:
2193 case BIT_NOT_EXPR:
2194 case TRUTH_NOT_EXPR:
2195 case PREINCREMENT_EXPR:
2196 case PREDECREMENT_EXPR:
2197 case REALPART_EXPR:
2198 case IMAGPART_EXPR:
2199 unary_expression (e);
2200 break;
2202 case FLOAT_EXPR:
2203 case FIX_TRUNC_EXPR:
2204 CASE_CONVERT:
2205 case VIEW_CONVERT_EXPR:
2206 pp_c_cast_expression (this, e);
2207 break;
2209 case MULT_EXPR:
2210 case TRUNC_MOD_EXPR:
2211 case TRUNC_DIV_EXPR:
2212 multiplicative_expression (e);
2213 break;
2215 case LSHIFT_EXPR:
2216 case RSHIFT_EXPR:
2217 pp_c_shift_expression (this, e);
2218 break;
2220 case LT_EXPR:
2221 case GT_EXPR:
2222 case LE_EXPR:
2223 case GE_EXPR:
2224 pp_c_relational_expression (this, e);
2225 break;
2227 case BIT_AND_EXPR:
2228 pp_c_and_expression (this, e);
2229 break;
2231 case BIT_XOR_EXPR:
2232 case TRUTH_XOR_EXPR:
2233 pp_c_exclusive_or_expression (this, e);
2234 break;
2236 case BIT_IOR_EXPR:
2237 pp_c_inclusive_or_expression (this, e);
2238 break;
2240 case TRUTH_ANDIF_EXPR:
2241 case TRUTH_AND_EXPR:
2242 pp_c_logical_and_expression (this, e);
2243 break;
2245 case TRUTH_ORIF_EXPR:
2246 case TRUTH_OR_EXPR:
2247 pp_c_logical_or_expression (this, e);
2248 break;
2250 case EQ_EXPR:
2251 case NE_EXPR:
2252 pp_c_equality_expression (this, e);
2253 break;
2255 case COND_EXPR:
2256 conditional_expression (e);
2257 break;
2259 case POINTER_PLUS_EXPR:
2260 case PLUS_EXPR:
2261 case MINUS_EXPR:
2262 pp_c_additive_expression (this, e);
2263 break;
2265 case MODIFY_EXPR:
2266 case INIT_EXPR:
2267 assignment_expression (e);
2268 break;
2270 case COMPOUND_EXPR:
2271 pp_c_left_paren (this);
2272 expression (TREE_OPERAND (e, 0));
2273 pp_separate_with (this, ',');
2274 assignment_expression (TREE_OPERAND (e, 1));
2275 pp_c_right_paren (this);
2276 break;
2278 case NON_LVALUE_EXPR:
2279 case SAVE_EXPR:
2280 expression (TREE_OPERAND (e, 0));
2281 break;
2283 case TARGET_EXPR:
2284 postfix_expression (TREE_OPERAND (e, 1));
2285 break;
2287 case BIND_EXPR:
2288 case GOTO_EXPR:
2289 /* We don't yet have a way of dumping statements in a
2290 human-readable format. */
2291 pp_string (this, "({...})");
2292 break;
2294 case C_MAYBE_CONST_EXPR:
2295 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2296 break;
2298 default:
2299 pp_unsupported_tree (this, e);
2300 break;
2306 /* Statements. */
2308 void
2309 c_pretty_printer::statement (tree stmt)
2311 if (stmt == NULL)
2312 return;
2314 if (pp_needs_newline (this))
2315 pp_newline_and_indent (this, 0);
2317 dump_generic_node (this, stmt, pp_indentation (this), 0, true);
2321 /* Initialize the PRETTY-PRINTER for handling C codes. */
2323 c_pretty_printer::c_pretty_printer ()
2324 : pretty_printer ()
2326 offset_list = 0;
2327 flags = 0;
2328 type_specifier_seq = pp_c_specifier_qualifier_list;
2329 ptr_operator = pp_c_pointer;
2330 parameter_list = pp_c_parameter_type_list;
2331 type_id = pp_c_type_id;
2332 simple_type_specifier = pp_c_type_specifier;
2333 storage_class_specifier = pp_c_storage_class_specifier;
2335 initializer = pp_c_initializer;
2339 /* Print the tree T in full, on file FILE. */
2341 void
2342 print_c_tree (FILE *file, tree t)
2344 c_pretty_printer pp;
2346 pp_needs_newline (&pp) = true;
2347 pp.buffer->stream = file;
2348 pp.statement (t);
2349 pp_newline_and_flush (&pp);
2352 /* Print the tree T in full, on stderr. */
2354 DEBUG_FUNCTION void
2355 debug_c_tree (tree t)
2357 print_c_tree (stderr, t);
2358 fputc ('\n', stderr);
2361 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2362 up of T's memory address. */
2364 void
2365 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2367 const char *name;
2369 gcc_assert (DECL_P (t));
2371 if (DECL_NAME (t))
2372 name = IDENTIFIER_POINTER (DECL_NAME (t));
2373 else
2375 static char xname[8];
2376 sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
2377 name = xname;
2380 pp_c_identifier (pp, name);