gcc/c-family/
[official-gcc.git] / gcc / c-family / c-pretty-print.c
blob54ed5512cd4292a2cf4300874088bc8085838ae0
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_ATOMIC)
184 pp_c_ws_string (pp, "_Atomic");
185 previous = true;
188 if (qualifiers & TYPE_QUAL_CONST)
190 if (previous)
191 pp_c_whitespace (pp);
192 pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const");
193 previous = true;
196 if (qualifiers & TYPE_QUAL_VOLATILE)
198 if (previous)
199 pp_c_whitespace (pp);
200 pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile");
201 previous = true;
204 if (qualifiers & TYPE_QUAL_RESTRICT)
206 if (previous)
207 pp_c_whitespace (pp);
208 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
209 ? "restrict" : "__restrict__"));
213 /* Pretty-print T using the type-cast notation '( type-name )'. */
215 static void
216 pp_c_type_cast (c_pretty_printer *pp, tree t)
218 pp_c_left_paren (pp);
219 pp->type_id (t);
220 pp_c_right_paren (pp);
223 /* We're about to pretty-print a pointer type as indicated by T.
224 Output a whitespace, if needed, preparing for subsequent output. */
226 void
227 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
229 if (POINTER_TYPE_P (t))
231 tree pointee = strip_pointer_operator (TREE_TYPE (t));
232 if (TREE_CODE (pointee) != ARRAY_TYPE
233 && TREE_CODE (pointee) != FUNCTION_TYPE)
234 pp_c_whitespace (pp);
239 /* Declarations. */
241 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
242 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
243 of its type. Take care of possible extensions.
245 type-qualifier-list:
246 type-qualifier
247 type-qualifier-list type-qualifier
249 type-qualifier:
250 const
251 restrict -- C99
252 __restrict__ -- GNU C
253 address-space-qualifier -- GNU C
254 volatile
255 _Atomic -- C11
257 address-space-qualifier:
258 identifier -- GNU C */
260 void
261 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
263 int qualifiers;
265 if (!t || t == error_mark_node)
266 return;
268 if (!TYPE_P (t))
269 t = TREE_TYPE (t);
271 qualifiers = TYPE_QUALS (t);
272 pp_c_cv_qualifiers (pp, qualifiers,
273 TREE_CODE (t) == FUNCTION_TYPE);
275 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
277 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
278 pp_c_identifier (pp, as);
282 /* pointer:
283 * type-qualifier-list(opt)
284 * type-qualifier-list(opt) pointer */
286 static void
287 pp_c_pointer (c_pretty_printer *pp, tree t)
289 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
290 t = TREE_TYPE (t);
291 switch (TREE_CODE (t))
293 case POINTER_TYPE:
294 /* It is easier to handle C++ reference types here. */
295 case REFERENCE_TYPE:
296 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
297 pp_c_pointer (pp, TREE_TYPE (t));
298 if (TREE_CODE (t) == POINTER_TYPE)
299 pp_c_star (pp);
300 else
301 pp_c_ampersand (pp);
302 pp_c_type_qualifier_list (pp, t);
303 break;
305 /* ??? This node is now in GENERIC and so shouldn't be here. But
306 we'll fix that later. */
307 case DECL_EXPR:
308 pp->declaration (DECL_EXPR_DECL (t));
309 pp_needs_newline (pp) = true;
310 break;
312 default:
313 pp_unsupported_tree (pp, t);
317 /* simple-type-specifier:
318 type-specifier
320 type-specifier:
321 void
322 char
323 short
325 long
326 float
327 double
328 signed
329 unsigned
330 _Bool -- C99
331 _Complex -- C99
332 _Imaginary -- C99
333 struct-or-union-specifier
334 enum-specifier
335 typedef-name.
337 GNU extensions.
338 simple-type-specifier:
339 __complex__
340 __vector__ */
342 void
343 c_pretty_printer::simple_type_specifier (tree t)
345 const enum tree_code code = TREE_CODE (t);
346 switch (code)
348 case ERROR_MARK:
349 translate_string ("<type-error>");
350 break;
352 case IDENTIFIER_NODE:
353 pp_c_identifier (this, IDENTIFIER_POINTER (t));
354 break;
356 case VOID_TYPE:
357 case BOOLEAN_TYPE:
358 case INTEGER_TYPE:
359 case REAL_TYPE:
360 case FIXED_POINT_TYPE:
361 if (TYPE_NAME (t))
363 t = TYPE_NAME (t);
364 simple_type_specifier (t);
366 else
368 int prec = TYPE_PRECISION (t);
369 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
370 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_SATURATING (t));
371 else
372 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
373 if (TYPE_NAME (t))
375 simple_type_specifier (t);
376 if (TYPE_PRECISION (t) != prec)
378 pp_colon (this);
379 pp_decimal_int (this, prec);
382 else
384 switch (code)
386 case INTEGER_TYPE:
387 translate_string (TYPE_UNSIGNED (t)
388 ? "<unnamed-unsigned:"
389 : "<unnamed-signed:");
390 break;
391 case REAL_TYPE:
392 translate_string ("<unnamed-float:");
393 break;
394 case FIXED_POINT_TYPE:
395 translate_string ("<unnamed-fixed:");
396 break;
397 default:
398 gcc_unreachable ();
400 pp_decimal_int (this, prec);
401 pp_greater (this);
404 break;
406 case TYPE_DECL:
407 if (DECL_NAME (t))
408 id_expression (t);
409 else
410 translate_string ("<typedef-error>");
411 break;
413 case UNION_TYPE:
414 case RECORD_TYPE:
415 case ENUMERAL_TYPE:
416 if (code == UNION_TYPE)
417 pp_c_ws_string (this, "union");
418 else if (code == RECORD_TYPE)
419 pp_c_ws_string (this, "struct");
420 else if (code == ENUMERAL_TYPE)
421 pp_c_ws_string (this, "enum");
422 else
423 translate_string ("<tag-error>");
425 if (TYPE_NAME (t))
426 id_expression (TYPE_NAME (t));
427 else
428 translate_string ("<anonymous>");
429 break;
431 default:
432 pp_unsupported_tree (this, t);
433 break;
437 /* specifier-qualifier-list:
438 type-specifier specifier-qualifier-list-opt
439 type-qualifier specifier-qualifier-list-opt
442 Implementation note: Because of the non-linearities in array or
443 function declarations, this routine prints not just the
444 specifier-qualifier-list of such entities or types of such entities,
445 but also the 'pointer' production part of their declarators. The
446 remaining part is done by declarator() or abstract_declarator(). */
448 void
449 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
451 const enum tree_code code = TREE_CODE (t);
453 if (!(pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
454 pp_c_type_qualifier_list (pp, t);
455 switch (code)
457 case REFERENCE_TYPE:
458 case POINTER_TYPE:
460 /* Get the types-specifier of this type. */
461 tree pointee = strip_pointer_operator (TREE_TYPE (t));
462 pp_c_specifier_qualifier_list (pp, pointee);
463 if (TREE_CODE (pointee) == ARRAY_TYPE
464 || TREE_CODE (pointee) == FUNCTION_TYPE)
466 pp_c_whitespace (pp);
467 pp_c_left_paren (pp);
468 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
470 else if (!c_dialect_cxx ())
471 pp_c_whitespace (pp);
472 pp_ptr_operator (pp, t);
474 break;
476 case FUNCTION_TYPE:
477 case ARRAY_TYPE:
478 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
479 break;
481 case VECTOR_TYPE:
482 case COMPLEX_TYPE:
483 if (code == COMPLEX_TYPE)
484 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
485 ? "_Complex" : "__complex__"));
486 else if (code == VECTOR_TYPE)
488 pp_c_ws_string (pp, "__vector");
489 pp_c_left_paren (pp);
490 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
491 pp_c_right_paren (pp);
492 pp_c_whitespace (pp);
494 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
495 break;
497 default:
498 pp->simple_type_specifier (t);
499 break;
501 if ((pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
502 pp_c_type_qualifier_list (pp, t);
505 /* parameter-type-list:
506 parameter-list
507 parameter-list , ...
509 parameter-list:
510 parameter-declaration
511 parameter-list , parameter-declaration
513 parameter-declaration:
514 declaration-specifiers declarator
515 declaration-specifiers abstract-declarator(opt) */
517 void
518 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
520 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
521 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
522 pp_c_left_paren (pp);
523 if (parms == void_list_node)
524 pp_c_ws_string (pp, "void");
525 else
527 bool first = true;
528 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
530 if (!first)
531 pp_separate_with (pp, ',');
532 first = false;
533 pp->declaration_specifiers
534 (want_parm_decl ? parms : TREE_VALUE (parms));
535 if (want_parm_decl)
536 pp->declarator (parms);
537 else
538 pp->abstract_declarator (TREE_VALUE (parms));
541 pp_c_right_paren (pp);
544 /* abstract-declarator:
545 pointer
546 pointer(opt) direct-abstract-declarator */
548 void
549 c_pretty_printer::abstract_declarator (tree t)
551 if (TREE_CODE (t) == POINTER_TYPE)
553 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
554 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
555 pp_c_right_paren (this);
556 t = TREE_TYPE (t);
559 direct_abstract_declarator (t);
562 /* direct-abstract-declarator:
563 ( abstract-declarator )
564 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
565 direct-abstract-declarator(opt) [ * ]
566 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
568 void
569 c_pretty_printer::direct_abstract_declarator (tree t)
571 switch (TREE_CODE (t))
573 case POINTER_TYPE:
574 abstract_declarator (t);
575 break;
577 case FUNCTION_TYPE:
578 pp_c_parameter_type_list (this, t);
579 direct_abstract_declarator (TREE_TYPE (t));
580 break;
582 case ARRAY_TYPE:
583 pp_c_left_bracket (this);
584 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
586 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
587 tree type = TREE_TYPE (maxval);
589 if (host_integerp (maxval, 0))
590 pp_wide_integer (this, tree_low_cst (maxval, 0) + 1);
591 else
592 expression (fold_build2 (PLUS_EXPR, type, maxval,
593 build_int_cst (type, 1)));
595 pp_c_right_bracket (this);
596 direct_abstract_declarator (TREE_TYPE (t));
597 break;
599 case IDENTIFIER_NODE:
600 case VOID_TYPE:
601 case BOOLEAN_TYPE:
602 case INTEGER_TYPE:
603 case REAL_TYPE:
604 case FIXED_POINT_TYPE:
605 case ENUMERAL_TYPE:
606 case RECORD_TYPE:
607 case UNION_TYPE:
608 case VECTOR_TYPE:
609 case COMPLEX_TYPE:
610 case TYPE_DECL:
611 break;
613 default:
614 pp_unsupported_tree (this, t);
615 break;
619 /* type-name:
620 specifier-qualifier-list abstract-declarator(opt) */
622 void
623 c_pretty_printer::type_id (tree t)
625 pp_c_specifier_qualifier_list (this, t);
626 abstract_declarator (t);
629 /* storage-class-specifier:
630 typedef
631 extern
632 static
633 auto
634 register */
636 void
637 c_pretty_printer::storage_class_specifier (tree t)
639 if (TREE_CODE (t) == TYPE_DECL)
640 pp_c_ws_string (this, "typedef");
641 else if (DECL_P (t))
643 if (DECL_REGISTER (t))
644 pp_c_ws_string (this, "register");
645 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
646 pp_c_ws_string (this, "static");
650 /* function-specifier:
651 inline */
653 void
654 c_pretty_printer::function_specifier (tree t)
656 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
657 pp_c_ws_string (this, "inline");
660 /* declaration-specifiers:
661 storage-class-specifier declaration-specifiers(opt)
662 type-specifier declaration-specifiers(opt)
663 type-qualifier declaration-specifiers(opt)
664 function-specifier declaration-specifiers(opt) */
666 void
667 c_pretty_printer::declaration_specifiers (tree t)
669 storage_class_specifier (t);
670 function_specifier (t);
671 pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t);
674 /* direct-declarator
675 identifier
676 ( declarator )
677 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
678 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
679 direct-declarator [ type-qualifier-list static assignment-expression ]
680 direct-declarator [ type-qualifier-list * ]
681 direct-declarator ( parameter-type-list )
682 direct-declarator ( identifier-list(opt) ) */
684 void
685 c_pretty_printer::direct_declarator (tree t)
687 switch (TREE_CODE (t))
689 case VAR_DECL:
690 case PARM_DECL:
691 case TYPE_DECL:
692 case FIELD_DECL:
693 case LABEL_DECL:
694 pp_c_space_for_pointer_operator (this, TREE_TYPE (t));
695 pp_c_tree_decl_identifier (this, t);
696 break;
698 case ARRAY_TYPE:
699 case POINTER_TYPE:
700 abstract_declarator (TREE_TYPE (t));
701 break;
703 case FUNCTION_TYPE:
704 pp_parameter_list (this, t);
705 abstract_declarator (TREE_TYPE (t));
706 break;
708 case FUNCTION_DECL:
709 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
710 pp_c_tree_decl_identifier (this, t);
711 if (flags & pp_c_flag_abstract)
712 abstract_declarator (TREE_TYPE (t));
713 else
715 pp_parameter_list (this, t);
716 abstract_declarator (TREE_TYPE (TREE_TYPE (t)));
718 break;
720 case INTEGER_TYPE:
721 case REAL_TYPE:
722 case FIXED_POINT_TYPE:
723 case ENUMERAL_TYPE:
724 case UNION_TYPE:
725 case RECORD_TYPE:
726 break;
728 default:
729 pp_unsupported_tree (this, t);
730 break;
735 /* declarator:
736 pointer(opt) direct-declarator */
738 void
739 c_pretty_printer::declarator (tree t)
741 switch (TREE_CODE (t))
743 case INTEGER_TYPE:
744 case REAL_TYPE:
745 case FIXED_POINT_TYPE:
746 case ENUMERAL_TYPE:
747 case UNION_TYPE:
748 case RECORD_TYPE:
749 break;
751 case VAR_DECL:
752 case PARM_DECL:
753 case FIELD_DECL:
754 case ARRAY_TYPE:
755 case FUNCTION_TYPE:
756 case FUNCTION_DECL:
757 case TYPE_DECL:
758 direct_declarator (t);
759 break;
762 default:
763 pp_unsupported_tree (this, t);
764 break;
768 /* declaration:
769 declaration-specifiers init-declarator-list(opt) ; */
771 void
772 c_pretty_printer::declaration (tree t)
774 declaration_specifiers (t);
775 pp_c_init_declarator (this, t);
778 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
780 void
781 pp_c_attributes (c_pretty_printer *pp, tree attributes)
783 if (attributes == NULL_TREE)
784 return;
786 pp_c_ws_string (pp, "__attribute__");
787 pp_c_left_paren (pp);
788 pp_c_left_paren (pp);
789 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
791 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
792 if (TREE_VALUE (attributes))
793 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
795 if (TREE_CHAIN (attributes))
796 pp_separate_with (pp, ',');
798 pp_c_right_paren (pp);
799 pp_c_right_paren (pp);
802 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
803 marked to be displayed on disgnostic. */
805 void
806 pp_c_attributes_display (c_pretty_printer *pp, tree a)
808 bool is_first = true;
810 if (a == NULL_TREE)
811 return;
813 for (; a != NULL_TREE; a = TREE_CHAIN (a))
815 const struct attribute_spec *as;
816 as = lookup_attribute_spec (TREE_PURPOSE (a));
817 if (!as || as->affects_type_identity == false)
818 continue;
819 if (is_first)
821 pp_c_ws_string (pp, "__attribute__");
822 pp_c_left_paren (pp);
823 pp_c_left_paren (pp);
824 is_first = false;
826 else
828 pp_separate_with (pp, ',');
830 pp_tree_identifier (pp, TREE_PURPOSE (a));
831 if (TREE_VALUE (a))
832 pp_c_call_argument_list (pp, TREE_VALUE (a));
835 if (!is_first)
837 pp_c_right_paren (pp);
838 pp_c_right_paren (pp);
839 pp_c_whitespace (pp);
843 /* function-definition:
844 declaration-specifiers declarator compound-statement */
846 void
847 pp_c_function_definition (c_pretty_printer *pp, tree t)
849 pp->declaration_specifiers (t);
850 pp->declarator (t);
851 pp_needs_newline (pp) = true;
852 pp->statement (DECL_SAVED_TREE (t));
853 pp_newline_and_flush (pp);
857 /* Expressions. */
859 /* Print out a c-char. This is called solely for characters which are
860 in the *target* execution character set. We ought to convert them
861 back to the *host* execution character set before printing, but we
862 have no way to do this at present. A decent compromise is to print
863 all characters as if they were in the host execution character set,
864 and not attempt to recover any named escape characters, but render
865 all unprintables as octal escapes. If the host and target character
866 sets are the same, this produces relatively readable output. If they
867 are not the same, strings may appear as gibberish, but that's okay
868 (in fact, it may well be what the reader wants, e.g. if they are looking
869 to see if conversion to the target character set happened correctly).
871 A special case: we need to prefix \, ", and ' with backslashes. It is
872 correct to do so for the *host*'s \, ", and ', because the rest of the
873 file appears in the host character set. */
875 static void
876 pp_c_char (c_pretty_printer *pp, int c)
878 if (ISPRINT (c))
880 switch (c)
882 case '\\': pp_string (pp, "\\\\"); break;
883 case '\'': pp_string (pp, "\\\'"); break;
884 case '\"': pp_string (pp, "\\\""); break;
885 default: pp_character (pp, c);
888 else
889 pp_scalar (pp, "\\%03o", (unsigned) c);
892 /* Print out a STRING literal. */
894 void
895 pp_c_string_literal (c_pretty_printer *pp, tree s)
897 const char *p = TREE_STRING_POINTER (s);
898 int n = TREE_STRING_LENGTH (s) - 1;
899 int i;
900 pp_doublequote (pp);
901 for (i = 0; i < n; ++i)
902 pp_c_char (pp, p[i]);
903 pp_doublequote (pp);
906 /* Pretty-print an INTEGER literal. */
908 static void
909 pp_c_integer_constant (c_pretty_printer *pp, tree i)
911 /* We are going to compare the type of I to other types using
912 pointer comparison so we need to use its canonical type. */
913 tree type =
914 TYPE_CANONICAL (TREE_TYPE (i))
915 ? TYPE_CANONICAL (TREE_TYPE (i))
916 : TREE_TYPE (i);
918 if (host_integerp (i, 0))
919 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
920 else if (host_integerp (i, 1))
921 pp_unsigned_wide_integer (pp, TREE_INT_CST_LOW (i));
922 else
924 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (i);
925 HOST_WIDE_INT high = TREE_INT_CST_HIGH (i);
926 if (tree_int_cst_sgn (i) < 0)
928 pp_minus (pp);
929 high = ~high + !low;
930 low = -low;
932 sprintf (pp_buffer (pp)->digit_buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
933 (unsigned HOST_WIDE_INT) high, (unsigned HOST_WIDE_INT) low);
934 pp_string (pp, pp_buffer (pp)->digit_buffer);
936 if (TYPE_UNSIGNED (type))
937 pp_character (pp, 'u');
938 if (type == long_integer_type_node || type == long_unsigned_type_node)
939 pp_character (pp, 'l');
940 else if (type == long_long_integer_type_node
941 || type == long_long_unsigned_type_node)
942 pp_string (pp, "ll");
943 else if (type == int128_integer_type_node
944 || type == int128_unsigned_type_node)
945 pp_string (pp, "I128");
948 /* Print out a CHARACTER literal. */
950 static void
951 pp_c_character_constant (c_pretty_printer *pp, tree c)
953 pp_quote (pp);
954 pp_c_char (pp, (unsigned) TREE_INT_CST_LOW (c));
955 pp_quote (pp);
958 /* Print out a BOOLEAN literal. */
960 static void
961 pp_c_bool_constant (c_pretty_printer *pp, tree b)
963 if (b == boolean_false_node)
965 if (c_dialect_cxx ())
966 pp_c_ws_string (pp, "false");
967 else if (flag_isoc99)
968 pp_c_ws_string (pp, "_False");
969 else
970 pp_unsupported_tree (pp, b);
972 else if (b == boolean_true_node)
974 if (c_dialect_cxx ())
975 pp_c_ws_string (pp, "true");
976 else if (flag_isoc99)
977 pp_c_ws_string (pp, "_True");
978 else
979 pp_unsupported_tree (pp, b);
981 else if (TREE_CODE (b) == INTEGER_CST)
982 pp_c_integer_constant (pp, b);
983 else
984 pp_unsupported_tree (pp, b);
987 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
988 false; that means the value was obtained by a cast, in which case
989 print out the type-id part of the cast-expression -- the casted value
990 is then printed by pp_c_integer_literal. */
992 static bool
993 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
995 bool value_is_named = true;
996 tree type = TREE_TYPE (e);
997 tree value;
999 /* Find the name of this constant. */
1000 for (value = TYPE_VALUES (type);
1001 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
1002 value = TREE_CHAIN (value))
1005 if (value != NULL_TREE)
1006 pp->id_expression (TREE_PURPOSE (value));
1007 else
1009 /* Value must have been cast. */
1010 pp_c_type_cast (pp, type);
1011 value_is_named = false;
1014 return value_is_named;
1017 /* Print out a REAL value as a decimal-floating-constant. */
1019 static void
1020 pp_c_floating_constant (c_pretty_printer *pp, tree r)
1022 const struct real_format *fmt
1023 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
1025 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
1026 bool is_decimal = floating_cst.decimal;
1028 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1029 log10(2) to 7 significant digits. */
1030 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
1032 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
1033 sizeof (pp_buffer (pp)->digit_buffer),
1034 max_digits10, 1);
1036 pp_string (pp, pp_buffer(pp)->digit_buffer);
1037 if (TREE_TYPE (r) == float_type_node)
1038 pp_character (pp, 'f');
1039 else if (TREE_TYPE (r) == long_double_type_node)
1040 pp_character (pp, 'l');
1041 else if (TREE_TYPE (r) == dfloat128_type_node)
1042 pp_string (pp, "dl");
1043 else if (TREE_TYPE (r) == dfloat64_type_node)
1044 pp_string (pp, "dd");
1045 else if (TREE_TYPE (r) == dfloat32_type_node)
1046 pp_string (pp, "df");
1049 /* Print out a FIXED value as a decimal-floating-constant. */
1051 static void
1052 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1054 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1055 sizeof (pp_buffer (pp)->digit_buffer));
1056 pp_string (pp, pp_buffer(pp)->digit_buffer);
1059 /* Pretty-print a compound literal expression. GNU extensions include
1060 vector constants. */
1062 static void
1063 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1065 tree type = TREE_TYPE (e);
1066 pp_c_type_cast (pp, type);
1068 switch (TREE_CODE (type))
1070 case RECORD_TYPE:
1071 case UNION_TYPE:
1072 case ARRAY_TYPE:
1073 case VECTOR_TYPE:
1074 case COMPLEX_TYPE:
1075 pp_c_brace_enclosed_initializer_list (pp, e);
1076 break;
1078 default:
1079 pp_unsupported_tree (pp, e);
1080 break;
1084 /* Pretty-print a COMPLEX_EXPR expression. */
1086 static void
1087 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1089 /* Handle a few common special cases, otherwise fallback
1090 to printing it as compound literal. */
1091 tree type = TREE_TYPE (e);
1092 tree realexpr = TREE_OPERAND (e, 0);
1093 tree imagexpr = TREE_OPERAND (e, 1);
1095 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1096 if (TREE_CODE (realexpr) == NOP_EXPR
1097 && TREE_CODE (imagexpr) == NOP_EXPR
1098 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1099 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1100 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1101 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1102 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1103 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1105 pp_c_type_cast (pp, type);
1106 pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1107 return;
1110 /* Cast of an scalar expression to COMPLEX_TYPE. */
1111 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1112 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1114 pp_c_type_cast (pp, type);
1115 if (TREE_CODE (realexpr) == NOP_EXPR)
1116 realexpr = TREE_OPERAND (realexpr, 0);
1117 pp->expression (realexpr);
1118 return;
1121 pp_c_compound_literal (pp, e);
1124 /* constant:
1125 integer-constant
1126 floating-constant
1127 fixed-point-constant
1128 enumeration-constant
1129 character-constant */
1131 void
1132 c_pretty_printer::constant (tree e)
1134 const enum tree_code code = TREE_CODE (e);
1136 switch (code)
1138 case INTEGER_CST:
1140 tree type = TREE_TYPE (e);
1141 if (type == boolean_type_node)
1142 pp_c_bool_constant (this, e);
1143 else if (type == char_type_node)
1144 pp_c_character_constant (this, e);
1145 else if (TREE_CODE (type) == ENUMERAL_TYPE
1146 && pp_c_enumeration_constant (this, e))
1148 else
1149 pp_c_integer_constant (this, e);
1151 break;
1153 case REAL_CST:
1154 pp_c_floating_constant (this, e);
1155 break;
1157 case FIXED_CST:
1158 pp_c_fixed_constant (this, e);
1159 break;
1161 case STRING_CST:
1162 pp_c_string_literal (this, e);
1163 break;
1165 case COMPLEX_CST:
1166 /* Sometimes, we are confused and we think a complex literal
1167 is a constant. Such thing is a compound literal which
1168 grammatically belongs to postfix-expr production. */
1169 pp_c_compound_literal (this, e);
1170 break;
1172 default:
1173 pp_unsupported_tree (this, e);
1174 break;
1178 /* Pretty-print a string such as an identifier, without changing its
1179 encoding, preceded by whitespace is necessary. */
1181 void
1182 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1184 pp_c_maybe_whitespace (pp);
1185 pp_string (pp, str);
1186 pp->padding = pp_before;
1189 void
1190 c_pretty_printer::translate_string (const char *gmsgid)
1192 if (pp_translate_identifiers (this))
1193 pp_c_ws_string (this, _(gmsgid));
1194 else
1195 pp_c_ws_string (this, gmsgid);
1198 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1199 that need converting to the locale encoding, preceded by whitespace
1200 is necessary. */
1202 void
1203 pp_c_identifier (c_pretty_printer *pp, const char *id)
1205 pp_c_maybe_whitespace (pp);
1206 pp_identifier (pp, id);
1207 pp->padding = pp_before;
1210 /* Pretty-print a C primary-expression.
1211 primary-expression:
1212 identifier
1213 constant
1214 string-literal
1215 ( expression ) */
1217 void
1218 c_pretty_printer::primary_expression (tree e)
1220 switch (TREE_CODE (e))
1222 case VAR_DECL:
1223 case PARM_DECL:
1224 case FIELD_DECL:
1225 case CONST_DECL:
1226 case FUNCTION_DECL:
1227 case LABEL_DECL:
1228 pp_c_tree_decl_identifier (this, e);
1229 break;
1231 case IDENTIFIER_NODE:
1232 pp_c_tree_identifier (this, e);
1233 break;
1235 case ERROR_MARK:
1236 translate_string ("<erroneous-expression>");
1237 break;
1239 case RESULT_DECL:
1240 translate_string ("<return-value>");
1241 break;
1243 case INTEGER_CST:
1244 case REAL_CST:
1245 case FIXED_CST:
1246 case STRING_CST:
1247 constant (e);
1248 break;
1250 case TARGET_EXPR:
1251 pp_c_ws_string (this, "__builtin_memcpy");
1252 pp_c_left_paren (this);
1253 pp_ampersand (this);
1254 primary_expression (TREE_OPERAND (e, 0));
1255 pp_separate_with (this, ',');
1256 pp_ampersand (this);
1257 initializer (TREE_OPERAND (e, 1));
1258 if (TREE_OPERAND (e, 2))
1260 pp_separate_with (this, ',');
1261 expression (TREE_OPERAND (e, 2));
1263 pp_c_right_paren (this);
1264 break;
1266 default:
1267 /* FIXME: Make sure we won't get into an infinite loop. */
1268 pp_c_left_paren (this);
1269 expression (e);
1270 pp_c_right_paren (this);
1271 break;
1275 /* Print out a C initializer -- also support C compound-literals.
1276 initializer:
1277 assignment-expression:
1278 { initializer-list }
1279 { initializer-list , } */
1281 void
1282 c_pretty_printer::initializer (tree e)
1284 if (TREE_CODE (e) == CONSTRUCTOR)
1285 pp_c_brace_enclosed_initializer_list (this, e);
1286 else
1287 expression (e);
1290 /* init-declarator:
1291 declarator:
1292 declarator = initializer */
1294 void
1295 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1297 pp->declarator (t);
1298 /* We don't want to output function definitions here. There are handled
1299 elsewhere (and the syntactic form is bogus anyway). */
1300 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1302 tree init = DECL_INITIAL (t);
1303 /* This C++ bit is handled here because it is easier to do so.
1304 In templates, the C++ parser builds a TREE_LIST for a
1305 direct-initialization; the TREE_PURPOSE is the variable to
1306 initialize and the TREE_VALUE is the initializer. */
1307 if (TREE_CODE (init) == TREE_LIST)
1309 pp_c_left_paren (pp);
1310 pp->expression (TREE_VALUE (init));
1311 pp_right_paren (pp);
1313 else
1315 pp_space (pp);
1316 pp_equal (pp);
1317 pp_space (pp);
1318 pp->initializer (init);
1323 /* initializer-list:
1324 designation(opt) initializer
1325 initializer-list , designation(opt) initializer
1327 designation:
1328 designator-list =
1330 designator-list:
1331 designator
1332 designator-list designator
1334 designator:
1335 [ constant-expression ]
1336 identifier */
1338 static void
1339 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1341 tree type = TREE_TYPE (e);
1342 const enum tree_code code = TREE_CODE (type);
1344 if (TREE_CODE (e) == CONSTRUCTOR)
1346 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1347 return;
1350 switch (code)
1352 case RECORD_TYPE:
1353 case UNION_TYPE:
1354 case ARRAY_TYPE:
1356 tree init = TREE_OPERAND (e, 0);
1357 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1359 if (code == RECORD_TYPE || code == UNION_TYPE)
1361 pp_c_dot (pp);
1362 pp->primary_expression (TREE_PURPOSE (init));
1364 else
1366 pp_c_left_bracket (pp);
1367 if (TREE_PURPOSE (init))
1368 pp->constant (TREE_PURPOSE (init));
1369 pp_c_right_bracket (pp);
1371 pp_c_whitespace (pp);
1372 pp_equal (pp);
1373 pp_c_whitespace (pp);
1374 pp->initializer (TREE_VALUE (init));
1375 if (TREE_CHAIN (init))
1376 pp_separate_with (pp, ',');
1379 return;
1381 case VECTOR_TYPE:
1382 if (TREE_CODE (e) == VECTOR_CST)
1384 unsigned i;
1385 for (i = 0; i < VECTOR_CST_NELTS (e); ++i)
1387 if (i > 0)
1388 pp_separate_with (pp, ',');
1389 pp->expression (VECTOR_CST_ELT (e, i));
1392 else
1393 break;
1394 return;
1396 case COMPLEX_TYPE:
1397 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1399 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1400 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1401 pp_separate_with (pp, ',');
1402 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1404 else
1405 break;
1406 return;
1408 default:
1409 break;
1412 pp_unsupported_tree (pp, type);
1415 /* Pretty-print a brace-enclosed initializer-list. */
1417 static void
1418 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1420 pp_c_left_brace (pp);
1421 pp_c_initializer_list (pp, l);
1422 pp_c_right_brace (pp);
1426 /* This is a convenient function, used to bridge gap between C and C++
1427 grammars.
1429 id-expression:
1430 identifier */
1432 void
1433 c_pretty_printer::id_expression (tree t)
1435 switch (TREE_CODE (t))
1437 case VAR_DECL:
1438 case PARM_DECL:
1439 case CONST_DECL:
1440 case TYPE_DECL:
1441 case FUNCTION_DECL:
1442 case FIELD_DECL:
1443 case LABEL_DECL:
1444 pp_c_tree_decl_identifier (this, t);
1445 break;
1447 case IDENTIFIER_NODE:
1448 pp_c_tree_identifier (this, t);
1449 break;
1451 default:
1452 pp_unsupported_tree (this, t);
1453 break;
1457 /* postfix-expression:
1458 primary-expression
1459 postfix-expression [ expression ]
1460 postfix-expression ( argument-expression-list(opt) )
1461 postfix-expression . identifier
1462 postfix-expression -> identifier
1463 postfix-expression ++
1464 postfix-expression --
1465 ( type-name ) { initializer-list }
1466 ( type-name ) { initializer-list , } */
1468 void
1469 c_pretty_printer::postfix_expression (tree e)
1471 enum tree_code code = TREE_CODE (e);
1472 switch (code)
1474 case POSTINCREMENT_EXPR:
1475 case POSTDECREMENT_EXPR:
1476 postfix_expression (TREE_OPERAND (e, 0));
1477 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1478 break;
1480 case ARRAY_REF:
1481 postfix_expression (TREE_OPERAND (e, 0));
1482 pp_c_left_bracket (this);
1483 expression (TREE_OPERAND (e, 1));
1484 pp_c_right_bracket (this);
1485 break;
1487 case ARRAY_NOTATION_REF:
1488 postfix_expression (ARRAY_NOTATION_ARRAY (e));
1489 pp_c_left_bracket (this);
1490 expression (ARRAY_NOTATION_START (e));
1491 pp_colon (this);
1492 expression (ARRAY_NOTATION_LENGTH (e));
1493 pp_colon (this);
1494 expression (ARRAY_NOTATION_STRIDE (e));
1495 pp_c_right_bracket (this);
1496 break;
1498 case CALL_EXPR:
1500 call_expr_arg_iterator iter;
1501 tree arg;
1502 postfix_expression (CALL_EXPR_FN (e));
1503 pp_c_left_paren (this);
1504 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1506 expression (arg);
1507 if (more_call_expr_args_p (&iter))
1508 pp_separate_with (this, ',');
1510 pp_c_right_paren (this);
1511 break;
1514 case UNORDERED_EXPR:
1515 pp_c_ws_string (this, flag_isoc99
1516 ? "isunordered"
1517 : "__builtin_isunordered");
1518 goto two_args_fun;
1520 case ORDERED_EXPR:
1521 pp_c_ws_string (this, flag_isoc99
1522 ? "!isunordered"
1523 : "!__builtin_isunordered");
1524 goto two_args_fun;
1526 case UNLT_EXPR:
1527 pp_c_ws_string (this, flag_isoc99
1528 ? "!isgreaterequal"
1529 : "!__builtin_isgreaterequal");
1530 goto two_args_fun;
1532 case UNLE_EXPR:
1533 pp_c_ws_string (this, flag_isoc99
1534 ? "!isgreater"
1535 : "!__builtin_isgreater");
1536 goto two_args_fun;
1538 case UNGT_EXPR:
1539 pp_c_ws_string (this, flag_isoc99
1540 ? "!islessequal"
1541 : "!__builtin_islessequal");
1542 goto two_args_fun;
1544 case UNGE_EXPR:
1545 pp_c_ws_string (this, flag_isoc99
1546 ? "!isless"
1547 : "!__builtin_isless");
1548 goto two_args_fun;
1550 case UNEQ_EXPR:
1551 pp_c_ws_string (this, flag_isoc99
1552 ? "!islessgreater"
1553 : "!__builtin_islessgreater");
1554 goto two_args_fun;
1556 case LTGT_EXPR:
1557 pp_c_ws_string (this, flag_isoc99
1558 ? "islessgreater"
1559 : "__builtin_islessgreater");
1560 goto two_args_fun;
1562 two_args_fun:
1563 pp_c_left_paren (this);
1564 expression (TREE_OPERAND (e, 0));
1565 pp_separate_with (this, ',');
1566 expression (TREE_OPERAND (e, 1));
1567 pp_c_right_paren (this);
1568 break;
1570 case ABS_EXPR:
1571 pp_c_ws_string (this, "__builtin_abs");
1572 pp_c_left_paren (this);
1573 expression (TREE_OPERAND (e, 0));
1574 pp_c_right_paren (this);
1575 break;
1577 case COMPONENT_REF:
1579 tree object = TREE_OPERAND (e, 0);
1580 if (TREE_CODE (object) == INDIRECT_REF)
1582 postfix_expression (TREE_OPERAND (object, 0));
1583 pp_c_arrow (this);
1585 else
1587 postfix_expression (object);
1588 pp_c_dot (this);
1590 expression (TREE_OPERAND (e, 1));
1592 break;
1594 case BIT_FIELD_REF:
1596 tree type = TREE_TYPE (e);
1598 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1599 if (type
1600 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1602 HOST_WIDE_INT bitpos = tree_low_cst (TREE_OPERAND (e, 2), 0);
1603 HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 0);
1604 if ((bitpos % size) == 0)
1606 pp_c_left_paren (this);
1607 pp_c_left_paren (this);
1608 type_id (type);
1609 pp_c_star (this);
1610 pp_c_right_paren (this);
1611 pp_c_ampersand (this);
1612 expression (TREE_OPERAND (e, 0));
1613 pp_c_right_paren (this);
1614 pp_c_left_bracket (this);
1615 pp_wide_integer (this, bitpos / size);
1616 pp_c_right_bracket (this);
1617 break;
1620 pp_unsupported_tree (this, e);
1622 break;
1624 case MEM_REF:
1625 expression (e);
1626 break;
1628 case COMPLEX_CST:
1629 case VECTOR_CST:
1630 pp_c_compound_literal (this, e);
1631 break;
1633 case COMPLEX_EXPR:
1634 pp_c_complex_expr (this, e);
1635 break;
1637 case COMPOUND_LITERAL_EXPR:
1638 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1639 /* Fall through. */
1640 case CONSTRUCTOR:
1641 initializer (e);
1642 break;
1644 case VA_ARG_EXPR:
1645 pp_c_ws_string (this, "__builtin_va_arg");
1646 pp_c_left_paren (this);
1647 assignment_expression (TREE_OPERAND (e, 0));
1648 pp_separate_with (this, ',');
1649 type_id (TREE_TYPE (e));
1650 pp_c_right_paren (this);
1651 break;
1653 case ADDR_EXPR:
1654 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1656 id_expression (TREE_OPERAND (e, 0));
1657 break;
1659 /* else fall through. */
1661 default:
1662 primary_expression (e);
1663 break;
1667 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1669 void
1670 pp_c_expression_list (c_pretty_printer *pp, tree e)
1672 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1674 pp->expression (TREE_VALUE (e));
1675 if (TREE_CHAIN (e))
1676 pp_separate_with (pp, ',');
1680 /* Print out V, which contains the elements of a constructor. */
1682 void
1683 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1685 unsigned HOST_WIDE_INT ix;
1686 tree value;
1688 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1690 pp->expression (value);
1691 if (ix != vec_safe_length (v) - 1)
1692 pp_separate_with (pp, ',');
1696 /* Print out an expression-list in parens, as if it were the argument
1697 list to a function. */
1699 void
1700 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1702 pp_c_left_paren (pp);
1703 if (t && TREE_CODE (t) == TREE_LIST)
1704 pp_c_expression_list (pp, t);
1705 pp_c_right_paren (pp);
1708 /* unary-expression:
1709 postfix-expression
1710 ++ cast-expression
1711 -- cast-expression
1712 unary-operator cast-expression
1713 sizeof unary-expression
1714 sizeof ( type-id )
1716 unary-operator: one of
1717 * & + - ! ~
1719 GNU extensions.
1720 unary-expression:
1721 __alignof__ unary-expression
1722 __alignof__ ( type-id )
1723 __real__ unary-expression
1724 __imag__ unary-expression */
1726 void
1727 c_pretty_printer::unary_expression (tree e)
1729 enum tree_code code = TREE_CODE (e);
1730 switch (code)
1732 case PREINCREMENT_EXPR:
1733 case PREDECREMENT_EXPR:
1734 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
1735 unary_expression (TREE_OPERAND (e, 0));
1736 break;
1738 case ADDR_EXPR:
1739 case INDIRECT_REF:
1740 case NEGATE_EXPR:
1741 case BIT_NOT_EXPR:
1742 case TRUTH_NOT_EXPR:
1743 case CONJ_EXPR:
1744 /* String literal are used by address. */
1745 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1746 pp_ampersand (this);
1747 else if (code == INDIRECT_REF)
1748 pp_c_star (this);
1749 else if (code == NEGATE_EXPR)
1750 pp_minus (this);
1751 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1752 pp_complement (this);
1753 else if (code == TRUTH_NOT_EXPR)
1754 pp_exclamation (this);
1755 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1756 break;
1758 case MEM_REF:
1759 if (TREE_CODE (TREE_OPERAND (e, 0)) == ADDR_EXPR
1760 && integer_zerop (TREE_OPERAND (e, 1)))
1761 expression (TREE_OPERAND (TREE_OPERAND (e, 0), 0));
1762 else
1764 pp_c_star (this);
1765 if (!integer_zerop (TREE_OPERAND (e, 1)))
1767 pp_c_left_paren (this);
1768 if (!integer_onep (TYPE_SIZE_UNIT
1769 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (e, 0))))))
1770 pp_c_type_cast (this, ptr_type_node);
1772 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
1773 if (!integer_zerop (TREE_OPERAND (e, 1)))
1775 pp_plus (this);
1776 pp_c_integer_constant (this,
1777 fold_convert (ssizetype,
1778 TREE_OPERAND (e, 1)));
1779 pp_c_right_paren (this);
1782 break;
1784 case REALPART_EXPR:
1785 case IMAGPART_EXPR:
1786 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
1787 pp_c_whitespace (this);
1788 unary_expression (TREE_OPERAND (e, 0));
1789 break;
1791 default:
1792 postfix_expression (e);
1793 break;
1797 /* cast-expression:
1798 unary-expression
1799 ( type-name ) cast-expression */
1801 void
1802 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1804 switch (TREE_CODE (e))
1806 case FLOAT_EXPR:
1807 case FIX_TRUNC_EXPR:
1808 CASE_CONVERT:
1809 case VIEW_CONVERT_EXPR:
1810 pp_c_type_cast (pp, TREE_TYPE (e));
1811 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1812 break;
1814 default:
1815 pp->unary_expression (e);
1819 /* multiplicative-expression:
1820 cast-expression
1821 multiplicative-expression * cast-expression
1822 multiplicative-expression / cast-expression
1823 multiplicative-expression % cast-expression */
1825 void
1826 c_pretty_printer::multiplicative_expression (tree e)
1828 enum tree_code code = TREE_CODE (e);
1829 switch (code)
1831 case MULT_EXPR:
1832 case TRUNC_DIV_EXPR:
1833 case TRUNC_MOD_EXPR:
1834 multiplicative_expression (TREE_OPERAND (e, 0));
1835 pp_c_whitespace (this);
1836 if (code == MULT_EXPR)
1837 pp_c_star (this);
1838 else if (code == TRUNC_DIV_EXPR)
1839 pp_slash (this);
1840 else
1841 pp_modulo (this);
1842 pp_c_whitespace (this);
1843 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
1844 break;
1846 default:
1847 pp_c_cast_expression (this, e);
1848 break;
1852 /* additive-expression:
1853 multiplicative-expression
1854 additive-expression + multiplicative-expression
1855 additive-expression - multiplicative-expression */
1857 static void
1858 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1860 enum tree_code code = TREE_CODE (e);
1861 switch (code)
1863 case POINTER_PLUS_EXPR:
1864 case PLUS_EXPR:
1865 case MINUS_EXPR:
1866 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1867 pp_c_whitespace (pp);
1868 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1869 pp_plus (pp);
1870 else
1871 pp_minus (pp);
1872 pp_c_whitespace (pp);
1873 pp->multiplicative_expression (TREE_OPERAND (e, 1));
1874 break;
1876 default:
1877 pp->multiplicative_expression (e);
1878 break;
1882 /* additive-expression:
1883 additive-expression
1884 shift-expression << additive-expression
1885 shift-expression >> additive-expression */
1887 static void
1888 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1890 enum tree_code code = TREE_CODE (e);
1891 switch (code)
1893 case LSHIFT_EXPR:
1894 case RSHIFT_EXPR:
1895 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1896 pp_c_whitespace (pp);
1897 pp_string (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1898 pp_c_whitespace (pp);
1899 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1900 break;
1902 default:
1903 pp_c_additive_expression (pp, e);
1907 /* relational-expression:
1908 shift-expression
1909 relational-expression < shift-expression
1910 relational-expression > shift-expression
1911 relational-expression <= shift-expression
1912 relational-expression >= shift-expression */
1914 static void
1915 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1917 enum tree_code code = TREE_CODE (e);
1918 switch (code)
1920 case LT_EXPR:
1921 case GT_EXPR:
1922 case LE_EXPR:
1923 case GE_EXPR:
1924 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1925 pp_c_whitespace (pp);
1926 if (code == LT_EXPR)
1927 pp_less (pp);
1928 else if (code == GT_EXPR)
1929 pp_greater (pp);
1930 else if (code == LE_EXPR)
1931 pp_less_equal (pp);
1932 else if (code == GE_EXPR)
1933 pp_greater_equal (pp);
1934 pp_c_whitespace (pp);
1935 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1936 break;
1938 default:
1939 pp_c_shift_expression (pp, e);
1940 break;
1944 /* equality-expression:
1945 relational-expression
1946 equality-expression == relational-expression
1947 equality-equality != relational-expression */
1949 static void
1950 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1952 enum tree_code code = TREE_CODE (e);
1953 switch (code)
1955 case EQ_EXPR:
1956 case NE_EXPR:
1957 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1958 pp_c_whitespace (pp);
1959 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
1960 pp_c_whitespace (pp);
1961 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1962 break;
1964 default:
1965 pp_c_relational_expression (pp, e);
1966 break;
1970 /* AND-expression:
1971 equality-expression
1972 AND-expression & equality-equality */
1974 static void
1975 pp_c_and_expression (c_pretty_printer *pp, tree e)
1977 if (TREE_CODE (e) == BIT_AND_EXPR)
1979 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1980 pp_c_whitespace (pp);
1981 pp_ampersand (pp);
1982 pp_c_whitespace (pp);
1983 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1985 else
1986 pp_c_equality_expression (pp, e);
1989 /* exclusive-OR-expression:
1990 AND-expression
1991 exclusive-OR-expression ^ AND-expression */
1993 static void
1994 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1996 if (TREE_CODE (e) == BIT_XOR_EXPR
1997 || TREE_CODE (e) == TRUTH_XOR_EXPR)
1999 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2000 if (TREE_CODE (e) == BIT_XOR_EXPR)
2001 pp_c_maybe_whitespace (pp);
2002 else
2003 pp_c_whitespace (pp);
2004 pp_carret (pp);
2005 pp_c_whitespace (pp);
2006 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2008 else
2009 pp_c_and_expression (pp, e);
2012 /* inclusive-OR-expression:
2013 exclusive-OR-expression
2014 inclusive-OR-expression | exclusive-OR-expression */
2016 static void
2017 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2019 if (TREE_CODE (e) == BIT_IOR_EXPR)
2021 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2022 pp_c_whitespace (pp);
2023 pp_bar (pp);
2024 pp_c_whitespace (pp);
2025 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2027 else
2028 pp_c_exclusive_or_expression (pp, e);
2031 /* logical-AND-expression:
2032 inclusive-OR-expression
2033 logical-AND-expression && inclusive-OR-expression */
2035 static void
2036 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2038 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2039 || TREE_CODE (e) == TRUTH_AND_EXPR)
2041 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2042 pp_c_whitespace (pp);
2043 pp_ampersand_ampersand (pp);
2044 pp_c_whitespace (pp);
2045 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2047 else
2048 pp_c_inclusive_or_expression (pp, e);
2051 /* logical-OR-expression:
2052 logical-AND-expression
2053 logical-OR-expression || logical-AND-expression */
2055 void
2056 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2058 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2059 || TREE_CODE (e) == TRUTH_OR_EXPR)
2061 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2062 pp_c_whitespace (pp);
2063 pp_bar_bar (pp);
2064 pp_c_whitespace (pp);
2065 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2067 else
2068 pp_c_logical_and_expression (pp, e);
2071 /* conditional-expression:
2072 logical-OR-expression
2073 logical-OR-expression ? expression : conditional-expression */
2075 void
2076 c_pretty_printer::conditional_expression (tree e)
2078 if (TREE_CODE (e) == COND_EXPR)
2080 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2081 pp_c_whitespace (this);
2082 pp_question (this);
2083 pp_c_whitespace (this);
2084 expression (TREE_OPERAND (e, 1));
2085 pp_c_whitespace (this);
2086 pp_colon (this);
2087 pp_c_whitespace (this);
2088 conditional_expression (TREE_OPERAND (e, 2));
2090 else
2091 pp_c_logical_or_expression (this, e);
2095 /* assignment-expression:
2096 conditional-expression
2097 unary-expression assignment-operator assignment-expression
2099 assignment-expression: one of
2100 = *= /= %= += -= >>= <<= &= ^= |= */
2102 void
2103 c_pretty_printer::assignment_expression (tree e)
2105 if (TREE_CODE (e) == MODIFY_EXPR
2106 || TREE_CODE (e) == INIT_EXPR)
2108 unary_expression (TREE_OPERAND (e, 0));
2109 pp_c_whitespace (this);
2110 pp_equal (this);
2111 pp_space (this);
2112 expression (TREE_OPERAND (e, 1));
2114 else
2115 conditional_expression (e);
2118 /* expression:
2119 assignment-expression
2120 expression , assignment-expression
2122 Implementation note: instead of going through the usual recursion
2123 chain, I take the liberty of dispatching nodes to the appropriate
2124 functions. This makes some redundancy, but it worths it. That also
2125 prevents a possible infinite recursion between primary_expression ()
2126 and expression (). */
2128 void
2129 c_pretty_printer::expression (tree e)
2131 switch (TREE_CODE (e))
2133 case INTEGER_CST:
2134 pp_c_integer_constant (this, e);
2135 break;
2137 case REAL_CST:
2138 pp_c_floating_constant (this, e);
2139 break;
2141 case FIXED_CST:
2142 pp_c_fixed_constant (this, e);
2143 break;
2145 case STRING_CST:
2146 pp_c_string_literal (this, e);
2147 break;
2149 case IDENTIFIER_NODE:
2150 case FUNCTION_DECL:
2151 case VAR_DECL:
2152 case CONST_DECL:
2153 case PARM_DECL:
2154 case RESULT_DECL:
2155 case FIELD_DECL:
2156 case LABEL_DECL:
2157 case ERROR_MARK:
2158 primary_expression (e);
2159 break;
2161 case SSA_NAME:
2162 if (SSA_NAME_VAR (e)
2163 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2164 expression (SSA_NAME_VAR (e));
2165 else
2166 translate_string ("<unknown>");
2167 break;
2169 case POSTINCREMENT_EXPR:
2170 case POSTDECREMENT_EXPR:
2171 case ARRAY_REF:
2172 case ARRAY_NOTATION_REF:
2173 case CALL_EXPR:
2174 case COMPONENT_REF:
2175 case BIT_FIELD_REF:
2176 case COMPLEX_CST:
2177 case COMPLEX_EXPR:
2178 case VECTOR_CST:
2179 case ORDERED_EXPR:
2180 case UNORDERED_EXPR:
2181 case LTGT_EXPR:
2182 case UNEQ_EXPR:
2183 case UNLE_EXPR:
2184 case UNLT_EXPR:
2185 case UNGE_EXPR:
2186 case UNGT_EXPR:
2187 case ABS_EXPR:
2188 case CONSTRUCTOR:
2189 case COMPOUND_LITERAL_EXPR:
2190 case VA_ARG_EXPR:
2191 postfix_expression (e);
2192 break;
2194 case CONJ_EXPR:
2195 case ADDR_EXPR:
2196 case INDIRECT_REF:
2197 case MEM_REF:
2198 case NEGATE_EXPR:
2199 case BIT_NOT_EXPR:
2200 case TRUTH_NOT_EXPR:
2201 case PREINCREMENT_EXPR:
2202 case PREDECREMENT_EXPR:
2203 case REALPART_EXPR:
2204 case IMAGPART_EXPR:
2205 unary_expression (e);
2206 break;
2208 case FLOAT_EXPR:
2209 case FIX_TRUNC_EXPR:
2210 CASE_CONVERT:
2211 case VIEW_CONVERT_EXPR:
2212 pp_c_cast_expression (this, e);
2213 break;
2215 case MULT_EXPR:
2216 case TRUNC_MOD_EXPR:
2217 case TRUNC_DIV_EXPR:
2218 multiplicative_expression (e);
2219 break;
2221 case LSHIFT_EXPR:
2222 case RSHIFT_EXPR:
2223 pp_c_shift_expression (this, e);
2224 break;
2226 case LT_EXPR:
2227 case GT_EXPR:
2228 case LE_EXPR:
2229 case GE_EXPR:
2230 pp_c_relational_expression (this, e);
2231 break;
2233 case BIT_AND_EXPR:
2234 pp_c_and_expression (this, e);
2235 break;
2237 case BIT_XOR_EXPR:
2238 case TRUTH_XOR_EXPR:
2239 pp_c_exclusive_or_expression (this, e);
2240 break;
2242 case BIT_IOR_EXPR:
2243 pp_c_inclusive_or_expression (this, e);
2244 break;
2246 case TRUTH_ANDIF_EXPR:
2247 case TRUTH_AND_EXPR:
2248 pp_c_logical_and_expression (this, e);
2249 break;
2251 case TRUTH_ORIF_EXPR:
2252 case TRUTH_OR_EXPR:
2253 pp_c_logical_or_expression (this, e);
2254 break;
2256 case EQ_EXPR:
2257 case NE_EXPR:
2258 pp_c_equality_expression (this, e);
2259 break;
2261 case COND_EXPR:
2262 conditional_expression (e);
2263 break;
2265 case POINTER_PLUS_EXPR:
2266 case PLUS_EXPR:
2267 case MINUS_EXPR:
2268 pp_c_additive_expression (this, e);
2269 break;
2271 case MODIFY_EXPR:
2272 case INIT_EXPR:
2273 assignment_expression (e);
2274 break;
2276 case COMPOUND_EXPR:
2277 pp_c_left_paren (this);
2278 expression (TREE_OPERAND (e, 0));
2279 pp_separate_with (this, ',');
2280 assignment_expression (TREE_OPERAND (e, 1));
2281 pp_c_right_paren (this);
2282 break;
2284 case NON_LVALUE_EXPR:
2285 case SAVE_EXPR:
2286 expression (TREE_OPERAND (e, 0));
2287 break;
2289 case TARGET_EXPR:
2290 postfix_expression (TREE_OPERAND (e, 1));
2291 break;
2293 case BIND_EXPR:
2294 case GOTO_EXPR:
2295 /* We don't yet have a way of dumping statements in a
2296 human-readable format. */
2297 pp_string (this, "({...})");
2298 break;
2300 case C_MAYBE_CONST_EXPR:
2301 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2302 break;
2304 default:
2305 pp_unsupported_tree (this, e);
2306 break;
2312 /* Statements. */
2314 void
2315 c_pretty_printer::statement (tree stmt)
2317 if (stmt == NULL)
2318 return;
2320 if (pp_needs_newline (this))
2321 pp_newline_and_indent (this, 0);
2323 dump_generic_node (this, stmt, pp_indentation (this), 0, true);
2327 /* Initialize the PRETTY-PRINTER for handling C codes. */
2329 c_pretty_printer::c_pretty_printer ()
2330 : pretty_printer (),
2331 offset_list (),
2332 flags ()
2334 type_specifier_seq = pp_c_specifier_qualifier_list;
2335 ptr_operator = pp_c_pointer;
2336 parameter_list = pp_c_parameter_type_list;
2340 /* Print the tree T in full, on file FILE. */
2342 void
2343 print_c_tree (FILE *file, tree t)
2345 c_pretty_printer pp;
2347 pp_needs_newline (&pp) = true;
2348 pp.buffer->stream = file;
2349 pp.statement (t);
2350 pp_newline_and_flush (&pp);
2353 /* Print the tree T in full, on stderr. */
2355 DEBUG_FUNCTION void
2356 debug_c_tree (tree t)
2358 print_c_tree (stderr, t);
2359 fputc ('\n', stderr);
2362 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2363 up of T's memory address. */
2365 void
2366 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2368 const char *name;
2370 gcc_assert (DECL_P (t));
2372 if (DECL_NAME (t))
2373 name = IDENTIFIER_POINTER (DECL_NAME (t));
2374 else
2376 static char xname[8];
2377 sprintf (xname, "<U%4x>", ((unsigned)((uintptr_t)(t) & 0xffff)));
2378 name = xname;
2381 pp_c_identifier (pp, name);