Daily bump.
[official-gcc.git] / gcc / c-family / c-pretty-print.c
bloba987da46d6da3da1c614e01d037fe741a05690f2
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2021 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 "c-pretty-print.h"
25 #include "gimple-pretty-print.h"
26 #include "diagnostic.h"
27 #include "stor-layout.h"
28 #include "stringpool.h"
29 #include "attribs.h"
30 #include "intl.h"
31 #include "tree-pretty-print.h"
32 #include "selftest.h"
33 #include "langhooks.h"
34 #include "options.h"
36 /* The pretty-printer code is primarily designed to closely follow
37 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
38 codes we used to have in the past. Following a structured
39 approach (preferably the official grammars) is believed to make it
40 much easier to add extensions and nifty pretty-printing effects that
41 takes expression or declaration contexts into account. */
44 #define pp_c_maybe_whitespace(PP) \
45 do { \
46 if ((PP)->padding == pp_before) \
47 pp_c_whitespace (PP); \
48 } while (0)
50 /* literal */
51 static void pp_c_char (c_pretty_printer *, int);
53 /* postfix-expression */
54 static void pp_c_initializer_list (c_pretty_printer *, tree);
55 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
57 static void pp_c_additive_expression (c_pretty_printer *, tree);
58 static void pp_c_shift_expression (c_pretty_printer *, tree);
59 static void pp_c_relational_expression (c_pretty_printer *, tree);
60 static void pp_c_equality_expression (c_pretty_printer *, tree);
61 static void pp_c_and_expression (c_pretty_printer *, tree);
62 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
63 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
64 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
66 /* declarations. */
69 /* Helper functions. */
71 void
72 pp_c_whitespace (c_pretty_printer *pp)
74 pp_space (pp);
75 pp->padding = pp_none;
78 void
79 pp_c_left_paren (c_pretty_printer *pp)
81 pp_left_paren (pp);
82 pp->padding = pp_none;
85 void
86 pp_c_right_paren (c_pretty_printer *pp)
88 pp_right_paren (pp);
89 pp->padding = pp_none;
92 void
93 pp_c_left_brace (c_pretty_printer *pp)
95 pp_left_brace (pp);
96 pp->padding = pp_none;
99 void
100 pp_c_right_brace (c_pretty_printer *pp)
102 pp_right_brace (pp);
103 pp->padding = pp_none;
106 void
107 pp_c_left_bracket (c_pretty_printer *pp)
109 pp_left_bracket (pp);
110 pp->padding = pp_none;
113 void
114 pp_c_right_bracket (c_pretty_printer *pp)
116 pp_right_bracket (pp);
117 pp->padding = pp_none;
120 void
121 pp_c_dot (c_pretty_printer *pp)
123 pp_dot (pp);
124 pp->padding = pp_none;
127 void
128 pp_c_ampersand (c_pretty_printer *pp)
130 pp_ampersand (pp);
131 pp->padding = pp_none;
134 void
135 pp_c_star (c_pretty_printer *pp)
137 pp_star (pp);
138 pp->padding = pp_none;
141 void
142 pp_c_arrow (c_pretty_printer *pp)
144 pp_arrow (pp);
145 pp->padding = pp_none;
148 void
149 pp_c_semicolon (c_pretty_printer *pp)
151 pp_semicolon (pp);
152 pp->padding = pp_none;
155 void
156 pp_c_complement (c_pretty_printer *pp)
158 pp_complement (pp);
159 pp->padding = pp_none;
162 void
163 pp_c_exclamation (c_pretty_printer *pp)
165 pp_exclamation (pp);
166 pp->padding = pp_none;
169 /* Print out the external representation of QUALIFIERS. */
171 void
172 pp_c_cv_qualifiers (c_pretty_printer *pp, int qualifiers, bool func_type)
174 const char *p = pp_last_position_in_text (pp);
176 if (!qualifiers)
177 return;
179 /* The C programming language does not have references, but it is much
180 simpler to handle those here rather than going through the same
181 logic in the C++ pretty-printer. */
182 if (p != NULL && (*p == '*' || *p == '&'))
183 pp_c_whitespace (pp);
185 if (qualifiers & TYPE_QUAL_ATOMIC)
186 pp_c_ws_string (pp, "_Atomic");
187 if (qualifiers & TYPE_QUAL_CONST)
188 pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const");
189 if (qualifiers & TYPE_QUAL_VOLATILE)
190 pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile");
191 if (qualifiers & TYPE_QUAL_RESTRICT)
192 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
193 ? "restrict" : "__restrict__"));
196 /* Pretty-print T using the type-cast notation '( type-name )'. */
198 void
199 pp_c_type_cast (c_pretty_printer *pp, tree t)
201 pp_c_left_paren (pp);
202 pp->type_id (t);
203 pp_c_right_paren (pp);
206 /* We're about to pretty-print a pointer type as indicated by T.
207 Output a whitespace, if needed, preparing for subsequent output. */
209 void
210 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
212 if (POINTER_TYPE_P (t))
214 tree pointee = strip_pointer_operator (TREE_TYPE (t));
215 if (TREE_CODE (pointee) != ARRAY_TYPE
216 && TREE_CODE (pointee) != FUNCTION_TYPE)
217 pp_c_whitespace (pp);
222 /* Declarations. */
224 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
225 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
226 of its type. Take care of possible extensions.
228 type-qualifier-list:
229 type-qualifier
230 type-qualifier-list type-qualifier
232 type-qualifier:
233 const
234 restrict -- C99
235 __restrict__ -- GNU C
236 address-space-qualifier -- GNU C
237 volatile
238 _Atomic -- C11
240 address-space-qualifier:
241 identifier -- GNU C */
243 void
244 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
246 int qualifiers;
248 if (!t || t == error_mark_node)
249 return;
251 if (!TYPE_P (t))
252 t = TREE_TYPE (t);
254 if (TREE_CODE (t) != ARRAY_TYPE)
256 qualifiers = TYPE_QUALS (t);
257 pp_c_cv_qualifiers (pp, qualifiers,
258 TREE_CODE (t) == FUNCTION_TYPE);
261 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
263 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
264 pp_c_identifier (pp, as);
268 /* pointer:
269 * type-qualifier-list(opt)
270 * type-qualifier-list(opt) pointer */
272 static void
273 pp_c_pointer (c_pretty_printer *pp, tree t)
275 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
276 t = TREE_TYPE (t);
277 switch (TREE_CODE (t))
279 case POINTER_TYPE:
280 /* It is easier to handle C++ reference types here. */
281 case REFERENCE_TYPE:
282 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
283 pp_c_pointer (pp, TREE_TYPE (t));
284 if (TREE_CODE (t) == POINTER_TYPE)
285 pp_c_star (pp);
286 else
288 pp_c_ampersand (pp);
289 if (TYPE_REF_IS_RVALUE (t))
290 pp_c_ampersand (pp);
292 pp_c_type_qualifier_list (pp, t);
293 break;
295 /* ??? This node is now in GENERIC and so shouldn't be here. But
296 we'll fix that later. */
297 case DECL_EXPR:
298 pp->declaration (DECL_EXPR_DECL (t));
299 pp_needs_newline (pp) = true;
300 break;
302 default:
303 pp_unsupported_tree (pp, t);
307 /* simple-type-specifier:
308 type-specifier
310 type-specifier:
311 void
312 char
313 short
315 long
316 float
317 double
318 signed
319 unsigned
320 _Bool -- C99
321 _Complex -- C99
322 _Imaginary -- C99
323 struct-or-union-specifier
324 enum-specifier
325 typedef-name.
327 GNU extensions.
328 simple-type-specifier:
329 __complex__
330 __vector__ */
332 void
333 c_pretty_printer::simple_type_specifier (tree t)
335 const enum tree_code code = TREE_CODE (t);
336 switch (code)
338 case ERROR_MARK:
339 translate_string ("<type-error>");
340 break;
342 case IDENTIFIER_NODE:
343 pp_c_identifier (this, IDENTIFIER_POINTER (t));
344 break;
346 case VOID_TYPE:
347 case OPAQUE_TYPE:
348 case BOOLEAN_TYPE:
349 case INTEGER_TYPE:
350 case REAL_TYPE:
351 case FIXED_POINT_TYPE:
352 if (TYPE_NAME (t))
354 t = TYPE_NAME (t);
355 simple_type_specifier (t);
357 else
359 int prec = TYPE_PRECISION (t);
360 tree common_t;
361 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
362 common_t = c_common_type_for_mode (TYPE_MODE (t),
363 TYPE_SATURATING (t));
364 else
365 common_t = c_common_type_for_mode (TYPE_MODE (t),
366 TYPE_UNSIGNED (t));
367 if (common_t && TYPE_NAME (common_t))
369 simple_type_specifier (common_t);
370 if (TYPE_PRECISION (common_t) != prec)
372 pp_colon (this);
373 pp_decimal_int (this, prec);
376 else
378 switch (code)
380 case INTEGER_TYPE:
381 translate_string (TYPE_UNSIGNED (t)
382 ? "<unnamed-unsigned:"
383 : "<unnamed-signed:");
384 break;
385 case REAL_TYPE:
386 translate_string ("<unnamed-float:");
387 break;
388 case FIXED_POINT_TYPE:
389 translate_string ("<unnamed-fixed:");
390 break;
391 default:
392 gcc_unreachable ();
394 pp_decimal_int (this, prec);
395 pp_greater (this);
398 break;
400 case TYPE_DECL:
401 if (DECL_NAME (t))
402 id_expression (t);
403 else
404 translate_string ("<typedef-error>");
405 break;
407 case UNION_TYPE:
408 case RECORD_TYPE:
409 case ENUMERAL_TYPE:
410 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
411 /* Don't decorate the type if this is a typedef name. */;
412 else if (code == UNION_TYPE)
413 pp_c_ws_string (this, "union");
414 else if (code == RECORD_TYPE)
415 pp_c_ws_string (this, "struct");
416 else if (code == ENUMERAL_TYPE)
417 pp_c_ws_string (this, "enum");
418 else
419 translate_string ("<tag-error>");
421 if (TYPE_NAME (t))
422 id_expression (TYPE_NAME (t));
423 else
424 translate_string ("<anonymous>");
425 break;
427 default:
428 pp_unsupported_tree (this, t);
429 break;
433 /* specifier-qualifier-list:
434 type-specifier specifier-qualifier-list-opt
435 type-qualifier specifier-qualifier-list-opt
438 Implementation note: Because of the non-linearities in array or
439 function declarations, this routine prints not just the
440 specifier-qualifier-list of such entities or types of such entities,
441 but also the 'pointer' production part of their declarators. The
442 remaining part is done by declarator() or abstract_declarator(). */
444 void
445 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
447 const enum tree_code code = TREE_CODE (t);
449 if (!(pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
450 pp_c_type_qualifier_list (pp, t);
451 switch (code)
453 case REFERENCE_TYPE:
454 case POINTER_TYPE:
456 /* Get the types-specifier of this type. */
457 tree pointee = strip_pointer_operator (TREE_TYPE (t));
458 pp_c_specifier_qualifier_list (pp, pointee);
459 if (TREE_CODE (pointee) == ARRAY_TYPE
460 || TREE_CODE (pointee) == FUNCTION_TYPE)
462 pp_c_whitespace (pp);
463 pp_c_left_paren (pp);
464 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
466 else if (!c_dialect_cxx ())
467 pp_c_whitespace (pp);
468 pp_ptr_operator (pp, t);
470 break;
472 case FUNCTION_TYPE:
473 case ARRAY_TYPE:
474 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
475 break;
477 case VECTOR_TYPE:
478 case COMPLEX_TYPE:
479 if (code == COMPLEX_TYPE)
480 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
481 ? "_Complex" : "__complex__"));
482 else if (code == VECTOR_TYPE)
484 /* The syntax we print for vector types isn't real C or C++ syntax,
485 so it's better to print the type name if we have one. */
486 tree name = TYPE_NAME (t);
487 if (!(pp->flags & pp_c_flag_gnu_v3)
488 && name
489 && TREE_CODE (name) == TYPE_DECL)
491 pp->id_expression (name);
492 break;
494 pp_c_ws_string (pp, "__vector");
495 pp_c_left_paren (pp);
496 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
497 pp_c_right_paren (pp);
498 pp_c_whitespace (pp);
500 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
501 break;
503 default:
504 pp->simple_type_specifier (t);
505 break;
507 if ((pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
508 pp_c_type_qualifier_list (pp, t);
511 /* parameter-type-list:
512 parameter-list
513 parameter-list , ...
515 parameter-list:
516 parameter-declaration
517 parameter-list , parameter-declaration
519 parameter-declaration:
520 declaration-specifiers declarator
521 declaration-specifiers abstract-declarator(opt) */
523 void
524 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
526 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
527 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
528 pp_c_left_paren (pp);
529 if (parms == void_list_node)
530 pp_c_ws_string (pp, "void");
531 else
533 bool first = true;
534 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
536 if (!first)
537 pp_separate_with (pp, ',');
538 first = false;
539 pp->declaration_specifiers
540 (want_parm_decl ? parms : TREE_VALUE (parms));
541 if (want_parm_decl)
542 pp->declarator (parms);
543 else
544 pp->abstract_declarator (TREE_VALUE (parms));
546 if (!first && !parms)
548 pp_separate_with (pp, ',');
549 pp_string (pp, "...");
552 pp_c_right_paren (pp);
555 /* abstract-declarator:
556 pointer
557 pointer(opt) direct-abstract-declarator */
559 void
560 c_pretty_printer::abstract_declarator (tree t)
562 if (TREE_CODE (t) == POINTER_TYPE)
564 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
565 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
566 pp_c_right_paren (this);
567 t = TREE_TYPE (t);
570 direct_abstract_declarator (t);
573 /* direct-abstract-declarator:
574 ( abstract-declarator )
575 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
576 direct-abstract-declarator(opt) [ * ]
577 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
579 void
580 c_pretty_printer::direct_abstract_declarator (tree t)
582 bool add_space = false;
584 switch (TREE_CODE (t))
586 case POINTER_TYPE:
587 abstract_declarator (t);
588 break;
590 case FUNCTION_TYPE:
591 pp_c_parameter_type_list (this, t);
592 direct_abstract_declarator (TREE_TYPE (t));
593 break;
595 case ARRAY_TYPE:
596 pp_c_left_bracket (this);
598 if (int quals = TYPE_QUALS (t))
600 /* Print the array qualifiers such as in "T[const restrict 3]". */
601 pp_c_cv_qualifiers (this, quals, false);
602 add_space = true;
605 if (tree arr = lookup_attribute ("array", TYPE_ATTRIBUTES (t)))
607 if (TREE_VALUE (arr))
609 /* Print the specifier as in "T[static 3]" that's not actually
610 part of the type but may be added by the front end. */
611 pp_c_ws_string (this, "static");
612 add_space = true;
614 else if (!TYPE_DOMAIN (t))
615 /* For arrays of unspecified bound using the [*] notation. */
616 pp_character (this, '*');
619 if (tree dom = TYPE_DOMAIN (t))
621 if (tree maxval = TYPE_MAX_VALUE (dom))
623 if (add_space)
624 pp_space (this);
626 tree type = TREE_TYPE (maxval);
628 if (tree_fits_shwi_p (maxval))
629 pp_wide_integer (this, tree_to_shwi (maxval) + 1);
630 else if (TREE_CODE (maxval) == INTEGER_CST)
631 expression (fold_build2 (PLUS_EXPR, type, maxval,
632 build_int_cst (type, 1)));
633 else
635 /* Strip the expressions from around a VLA bound added
636 internally to make it fit the domain mold, including
637 any casts. */
638 if (TREE_CODE (maxval) == NOP_EXPR)
639 maxval = TREE_OPERAND (maxval, 0);
640 if (TREE_CODE (maxval) == PLUS_EXPR
641 && integer_all_onesp (TREE_OPERAND (maxval, 1)))
643 maxval = TREE_OPERAND (maxval, 0);
644 if (TREE_CODE (maxval) == NOP_EXPR)
645 maxval = TREE_OPERAND (maxval, 0);
647 if (TREE_CODE (maxval) == SAVE_EXPR)
649 maxval = TREE_OPERAND (maxval, 0);
650 if (TREE_CODE (maxval) == NOP_EXPR)
651 maxval = TREE_OPERAND (maxval, 0);
654 expression (maxval);
657 else if (TYPE_SIZE (t))
658 /* Print zero for zero-length arrays but not for flexible
659 array members whose TYPE_SIZE is null. */
660 pp_string (this, "0");
662 pp_c_right_bracket (this);
663 direct_abstract_declarator (TREE_TYPE (t));
664 break;
666 case IDENTIFIER_NODE:
667 case VOID_TYPE:
668 case OPAQUE_TYPE:
669 case BOOLEAN_TYPE:
670 case INTEGER_TYPE:
671 case REAL_TYPE:
672 case FIXED_POINT_TYPE:
673 case ENUMERAL_TYPE:
674 case RECORD_TYPE:
675 case UNION_TYPE:
676 case VECTOR_TYPE:
677 case COMPLEX_TYPE:
678 case TYPE_DECL:
679 case ERROR_MARK:
680 break;
682 default:
683 pp_unsupported_tree (this, t);
684 break;
688 /* type-name:
689 specifier-qualifier-list abstract-declarator(opt) */
691 void
692 c_pretty_printer::type_id (tree t)
694 pp_c_specifier_qualifier_list (this, t);
695 abstract_declarator (t);
698 /* storage-class-specifier:
699 typedef
700 extern
701 static
702 auto
703 register */
705 void
706 c_pretty_printer::storage_class_specifier (tree t)
708 if (TREE_CODE (t) == TYPE_DECL)
709 pp_c_ws_string (this, "typedef");
710 else if (DECL_P (t))
712 if (DECL_REGISTER (t))
713 pp_c_ws_string (this, "register");
714 else if (TREE_STATIC (t) && VAR_P (t))
715 pp_c_ws_string (this, "static");
719 /* function-specifier:
720 inline */
722 void
723 c_pretty_printer::function_specifier (tree t)
725 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
726 pp_c_ws_string (this, "inline");
729 /* declaration-specifiers:
730 storage-class-specifier declaration-specifiers(opt)
731 type-specifier declaration-specifiers(opt)
732 type-qualifier declaration-specifiers(opt)
733 function-specifier declaration-specifiers(opt) */
735 void
736 c_pretty_printer::declaration_specifiers (tree t)
738 storage_class_specifier (t);
739 function_specifier (t);
740 pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t);
743 /* direct-declarator
744 identifier
745 ( declarator )
746 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
747 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
748 direct-declarator [ type-qualifier-list static assignment-expression ]
749 direct-declarator [ type-qualifier-list * ]
750 direct-declarator ( parameter-type-list )
751 direct-declarator ( identifier-list(opt) ) */
753 void
754 c_pretty_printer::direct_declarator (tree t)
756 switch (TREE_CODE (t))
758 case VAR_DECL:
759 case PARM_DECL:
760 case TYPE_DECL:
761 case FIELD_DECL:
762 case LABEL_DECL:
763 pp_c_space_for_pointer_operator (this, TREE_TYPE (t));
764 pp_c_tree_decl_identifier (this, t);
765 break;
767 case ARRAY_TYPE:
768 case POINTER_TYPE:
769 abstract_declarator (TREE_TYPE (t));
770 break;
772 case FUNCTION_TYPE:
773 pp_parameter_list (this, t);
774 abstract_declarator (TREE_TYPE (t));
775 break;
777 case FUNCTION_DECL:
778 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
779 pp_c_tree_decl_identifier (this, t);
780 if (flags & pp_c_flag_abstract)
781 abstract_declarator (TREE_TYPE (t));
782 else
784 pp_parameter_list (this, t);
785 abstract_declarator (TREE_TYPE (TREE_TYPE (t)));
787 break;
789 case INTEGER_TYPE:
790 case REAL_TYPE:
791 case FIXED_POINT_TYPE:
792 case ENUMERAL_TYPE:
793 case UNION_TYPE:
794 case RECORD_TYPE:
795 break;
797 default:
798 pp_unsupported_tree (this, t);
799 break;
804 /* declarator:
805 pointer(opt) direct-declarator */
807 void
808 c_pretty_printer::declarator (tree t)
810 switch (TREE_CODE (t))
812 case INTEGER_TYPE:
813 case REAL_TYPE:
814 case FIXED_POINT_TYPE:
815 case ENUMERAL_TYPE:
816 case UNION_TYPE:
817 case RECORD_TYPE:
818 break;
820 case VAR_DECL:
821 case PARM_DECL:
822 case FIELD_DECL:
823 case ARRAY_TYPE:
824 case FUNCTION_TYPE:
825 case FUNCTION_DECL:
826 case TYPE_DECL:
827 direct_declarator (t);
828 break;
831 default:
832 pp_unsupported_tree (this, t);
833 break;
837 /* declaration:
838 declaration-specifiers init-declarator-list(opt) ; */
840 void
841 c_pretty_printer::declaration (tree t)
843 declaration_specifiers (t);
844 pp_c_init_declarator (this, t);
847 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
849 void
850 pp_c_attributes (c_pretty_printer *pp, tree attributes)
852 if (attributes == NULL_TREE)
853 return;
855 pp_c_ws_string (pp, "__attribute__");
856 pp_c_left_paren (pp);
857 pp_c_left_paren (pp);
858 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
860 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
861 if (TREE_VALUE (attributes))
862 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
864 if (TREE_CHAIN (attributes))
865 pp_separate_with (pp, ',');
867 pp_c_right_paren (pp);
868 pp_c_right_paren (pp);
871 /* Pretty-print ATTRIBUTES using GNU C extension syntax for attributes
872 marked to be displayed on disgnostic. */
874 void
875 pp_c_attributes_display (c_pretty_printer *pp, tree a)
877 bool is_first = true;
879 if (a == NULL_TREE)
880 return;
882 for (; a != NULL_TREE; a = TREE_CHAIN (a))
884 const struct attribute_spec *as;
885 as = lookup_attribute_spec (TREE_PURPOSE (a));
886 if (!as || as->affects_type_identity == false)
887 continue;
888 if (c_dialect_cxx ()
889 && !strcmp ("transaction_safe", as->name))
890 /* In C++ transaction_safe is printed at the end of the declarator. */
891 continue;
892 if (is_first)
894 pp_c_ws_string (pp, "__attribute__");
895 pp_c_left_paren (pp);
896 pp_c_left_paren (pp);
897 is_first = false;
899 else
901 pp_separate_with (pp, ',');
903 pp_tree_identifier (pp, TREE_PURPOSE (a));
904 if (TREE_VALUE (a))
905 pp_c_call_argument_list (pp, TREE_VALUE (a));
908 if (!is_first)
910 pp_c_right_paren (pp);
911 pp_c_right_paren (pp);
912 pp_c_whitespace (pp);
916 /* function-definition:
917 declaration-specifiers declarator compound-statement */
919 void
920 pp_c_function_definition (c_pretty_printer *pp, tree t)
922 pp->declaration_specifiers (t);
923 pp->declarator (t);
924 pp_needs_newline (pp) = true;
925 pp->statement (DECL_SAVED_TREE (t));
926 pp_newline_and_flush (pp);
930 /* Expressions. */
932 /* Print out a c-char. This is called solely for characters which are
933 in the *target* execution character set. We ought to convert them
934 back to the *host* execution character set before printing, but we
935 have no way to do this at present. A decent compromise is to print
936 all characters as if they were in the host execution character set,
937 and not attempt to recover any named escape characters, but render
938 all unprintables as octal escapes. If the host and target character
939 sets are the same, this produces relatively readable output. If they
940 are not the same, strings may appear as gibberish, but that's okay
941 (in fact, it may well be what the reader wants, e.g. if they are looking
942 to see if conversion to the target character set happened correctly).
944 A special case: we need to prefix \, ", and ' with backslashes. It is
945 correct to do so for the *host*'s \, ", and ', because the rest of the
946 file appears in the host character set. */
948 static void
949 pp_c_char (c_pretty_printer *pp, int c)
951 if (ISPRINT (c))
953 switch (c)
955 case '\\': pp_string (pp, "\\\\"); break;
956 case '\'': pp_string (pp, "\\\'"); break;
957 case '\"': pp_string (pp, "\\\""); break;
958 default: pp_character (pp, c);
961 else
962 pp_scalar (pp, "\\%03o", (unsigned) c);
965 /* Print out a STRING literal. */
967 void
968 pp_c_string_literal (c_pretty_printer *pp, tree s)
970 const char *p = TREE_STRING_POINTER (s);
971 int n = TREE_STRING_LENGTH (s) - 1;
972 int i;
973 pp_doublequote (pp);
974 for (i = 0; i < n; ++i)
975 pp_c_char (pp, p[i]);
976 pp_doublequote (pp);
979 /* Pretty-print a VOID_CST (void_node). */
981 static void
982 pp_c_void_constant (c_pretty_printer *pp)
984 pp_c_type_cast (pp, void_type_node);
985 pp_string (pp, "0");
988 /* Pretty-print an INTEGER literal. */
990 void
991 pp_c_integer_constant (c_pretty_printer *pp, tree i)
993 if (tree_fits_shwi_p (i))
994 pp_wide_integer (pp, tree_to_shwi (i));
995 else if (tree_fits_uhwi_p (i))
996 pp_unsigned_wide_integer (pp, tree_to_uhwi (i));
997 else
999 wide_int wi = wi::to_wide (i);
1001 if (wi::lt_p (wi::to_wide (i), 0, TYPE_SIGN (TREE_TYPE (i))))
1003 pp_minus (pp);
1004 wi = -wi;
1006 print_hex (wi, pp_buffer (pp)->digit_buffer);
1007 pp_string (pp, pp_buffer (pp)->digit_buffer);
1011 /* Print out a CHARACTER literal. */
1013 static void
1014 pp_c_character_constant (c_pretty_printer *pp, tree c)
1016 pp_quote (pp);
1017 pp_c_char (pp, (unsigned) TREE_INT_CST_LOW (c));
1018 pp_quote (pp);
1021 /* Print out a BOOLEAN literal. */
1023 static void
1024 pp_c_bool_constant (c_pretty_printer *pp, tree b)
1026 if (b == boolean_false_node)
1028 if (c_dialect_cxx ())
1029 pp_c_ws_string (pp, "false");
1030 else if (flag_isoc99)
1031 pp_c_ws_string (pp, "_False");
1032 else
1033 pp_unsupported_tree (pp, b);
1035 else if (b == boolean_true_node)
1037 if (c_dialect_cxx ())
1038 pp_c_ws_string (pp, "true");
1039 else if (flag_isoc99)
1040 pp_c_ws_string (pp, "_True");
1041 else
1042 pp_unsupported_tree (pp, b);
1044 else if (TREE_CODE (b) == INTEGER_CST)
1045 pp_c_integer_constant (pp, b);
1046 else
1047 pp_unsupported_tree (pp, b);
1050 /* Given a value e of ENUMERAL_TYPE:
1051 Print out the first ENUMERATOR id with value e, if one is found,
1052 else print out the value as a C-style cast (type-id)value. */
1054 static void
1055 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
1057 tree type = TREE_TYPE (e);
1058 tree value = NULL_TREE;
1060 /* Find the name of this constant. */
1061 if ((pp->flags & pp_c_flag_gnu_v3) == 0)
1062 for (value = TYPE_VALUES (type); value != NULL_TREE;
1063 value = TREE_CHAIN (value))
1064 if (tree_int_cst_equal (DECL_INITIAL (TREE_VALUE (value)), e))
1065 break;
1067 if (value != NULL_TREE)
1068 pp->id_expression (TREE_PURPOSE (value));
1069 else
1071 /* Value must have been cast. */
1072 pp_c_type_cast (pp, type);
1073 pp_c_integer_constant (pp, e);
1077 /* Print out a REAL value as a decimal-floating-constant. */
1079 static void
1080 pp_c_floating_constant (c_pretty_printer *pp, tree r)
1082 const struct real_format *fmt
1083 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
1085 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
1086 bool is_decimal = floating_cst.decimal;
1088 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1089 log10(2) to 7 significant digits. */
1090 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
1092 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
1093 sizeof (pp_buffer (pp)->digit_buffer),
1094 max_digits10, 1);
1096 pp_string (pp, pp_buffer(pp)->digit_buffer);
1097 if (TREE_TYPE (r) == float_type_node)
1098 pp_character (pp, 'f');
1099 else if (TREE_TYPE (r) == long_double_type_node)
1100 pp_character (pp, 'l');
1101 else if (TREE_TYPE (r) == dfloat128_type_node)
1102 pp_string (pp, "dl");
1103 else if (TREE_TYPE (r) == dfloat64_type_node)
1104 pp_string (pp, "dd");
1105 else if (TREE_TYPE (r) == dfloat32_type_node)
1106 pp_string (pp, "df");
1107 else if (TREE_TYPE (r) != double_type_node)
1108 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
1109 if (TREE_TYPE (r) == FLOATN_NX_TYPE_NODE (i))
1111 pp_character (pp, 'f');
1112 pp_decimal_int (pp, floatn_nx_types[i].n);
1113 if (floatn_nx_types[i].extended)
1114 pp_character (pp, 'x');
1115 break;
1119 /* Print out a FIXED value as a decimal-floating-constant. */
1121 static void
1122 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1124 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1125 sizeof (pp_buffer (pp)->digit_buffer));
1126 pp_string (pp, pp_buffer(pp)->digit_buffer);
1129 /* Pretty-print a compound literal expression. GNU extensions include
1130 vector constants. */
1132 static void
1133 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1135 tree type = TREE_TYPE (e);
1136 pp_c_type_cast (pp, type);
1138 switch (TREE_CODE (type))
1140 case RECORD_TYPE:
1141 case UNION_TYPE:
1142 case ARRAY_TYPE:
1143 case VECTOR_TYPE:
1144 case COMPLEX_TYPE:
1145 pp_c_brace_enclosed_initializer_list (pp, e);
1146 break;
1148 default:
1149 pp_unsupported_tree (pp, e);
1150 break;
1154 /* Pretty-print a COMPLEX_EXPR expression. */
1156 static void
1157 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1159 /* Handle a few common special cases, otherwise fallback
1160 to printing it as compound literal. */
1161 tree type = TREE_TYPE (e);
1162 tree realexpr = TREE_OPERAND (e, 0);
1163 tree imagexpr = TREE_OPERAND (e, 1);
1165 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1166 if (TREE_CODE (realexpr) == NOP_EXPR
1167 && TREE_CODE (imagexpr) == NOP_EXPR
1168 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1169 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1170 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1171 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1172 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1173 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1175 pp_c_type_cast (pp, type);
1176 pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1177 return;
1180 /* Cast of an scalar expression to COMPLEX_TYPE. */
1181 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1182 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1184 pp_c_type_cast (pp, type);
1185 if (TREE_CODE (realexpr) == NOP_EXPR)
1186 realexpr = TREE_OPERAND (realexpr, 0);
1187 pp->expression (realexpr);
1188 return;
1191 pp_c_compound_literal (pp, e);
1194 /* constant:
1195 integer-constant
1196 floating-constant
1197 fixed-point-constant
1198 enumeration-constant
1199 character-constant */
1201 void
1202 c_pretty_printer::constant (tree e)
1204 const enum tree_code code = TREE_CODE (e);
1206 switch (code)
1208 case VOID_CST:
1209 pp_c_void_constant (this);
1210 break;
1212 case INTEGER_CST:
1214 tree type = TREE_TYPE (e);
1215 if (type == boolean_type_node)
1216 pp_c_bool_constant (this, e);
1217 else if (type == char_type_node)
1218 pp_c_character_constant (this, e);
1219 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1220 pp_c_enumeration_constant (this, e);
1221 else
1222 pp_c_integer_constant (this, e);
1224 break;
1226 case REAL_CST:
1227 pp_c_floating_constant (this, e);
1228 break;
1230 case FIXED_CST:
1231 pp_c_fixed_constant (this, e);
1232 break;
1234 case STRING_CST:
1235 pp_c_string_literal (this, e);
1236 break;
1238 case COMPLEX_CST:
1239 /* Sometimes, we are confused and we think a complex literal
1240 is a constant. Such thing is a compound literal which
1241 grammatically belongs to postfix-expr production. */
1242 pp_c_compound_literal (this, e);
1243 break;
1245 default:
1246 pp_unsupported_tree (this, e);
1247 break;
1251 /* Pretty-print a string such as an identifier, without changing its
1252 encoding, preceded by whitespace is necessary. */
1254 void
1255 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1257 pp_c_maybe_whitespace (pp);
1258 pp_string (pp, str);
1259 pp->padding = pp_before;
1262 void
1263 c_pretty_printer::translate_string (const char *gmsgid)
1265 if (pp_translate_identifiers (this))
1266 pp_c_ws_string (this, _(gmsgid));
1267 else
1268 pp_c_ws_string (this, gmsgid);
1271 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1272 that need converting to the locale encoding, preceded by whitespace
1273 is necessary. */
1275 void
1276 pp_c_identifier (c_pretty_printer *pp, const char *id)
1278 pp_c_maybe_whitespace (pp);
1279 pp_identifier (pp, id);
1280 pp->padding = pp_before;
1283 /* Pretty-print a C primary-expression.
1284 primary-expression:
1285 identifier
1286 constant
1287 string-literal
1288 ( expression ) */
1290 void
1291 c_pretty_printer::primary_expression (tree e)
1293 switch (TREE_CODE (e))
1295 case VAR_DECL:
1296 case PARM_DECL:
1297 case FIELD_DECL:
1298 case CONST_DECL:
1299 case FUNCTION_DECL:
1300 case LABEL_DECL:
1301 pp_c_tree_decl_identifier (this, e);
1302 break;
1304 case IDENTIFIER_NODE:
1305 pp_c_tree_identifier (this, e);
1306 break;
1308 case ERROR_MARK:
1309 translate_string ("<erroneous-expression>");
1310 break;
1312 case RESULT_DECL:
1313 translate_string ("<return-value>");
1314 break;
1316 case VOID_CST:
1317 case INTEGER_CST:
1318 case REAL_CST:
1319 case FIXED_CST:
1320 case STRING_CST:
1321 constant (e);
1322 break;
1324 case TARGET_EXPR:
1325 pp_c_ws_string (this, "__builtin_memcpy");
1326 pp_c_left_paren (this);
1327 pp_ampersand (this);
1328 primary_expression (TREE_OPERAND (e, 0));
1329 pp_separate_with (this, ',');
1330 pp_ampersand (this);
1331 initializer (TREE_OPERAND (e, 1));
1332 if (TREE_OPERAND (e, 2))
1334 pp_separate_with (this, ',');
1335 expression (TREE_OPERAND (e, 2));
1337 pp_c_right_paren (this);
1338 break;
1340 case SSA_NAME:
1341 if (SSA_NAME_VAR (e))
1343 tree var = SSA_NAME_VAR (e);
1344 if (tree id = SSA_NAME_IDENTIFIER (e))
1346 const char *name = IDENTIFIER_POINTER (id);
1347 const char *dot;
1348 if (DECL_ARTIFICIAL (var) && (dot = strchr (name, '.')))
1350 /* Print the name without the . suffix (such as in VLAs).
1351 Use pp_c_identifier so that it can be converted into
1352 the appropriate encoding. */
1353 size_t size = dot - name;
1354 char *ident = XALLOCAVEC (char, size + 1);
1355 memcpy (ident, name, size);
1356 ident[size] = '\0';
1357 pp_c_identifier (this, ident);
1359 else
1360 primary_expression (var);
1362 else
1363 primary_expression (var);
1365 else
1367 /* Print only the right side of the GIMPLE assignment. */
1368 gimple *def_stmt = SSA_NAME_DEF_STMT (e);
1369 pp_gimple_stmt_1 (this, def_stmt, 0, TDF_RHS_ONLY);
1371 break;
1373 default:
1374 /* FIXME: Make sure we won't get into an infinite loop. */
1375 if (location_wrapper_p (e))
1376 expression (e);
1377 else
1379 pp_c_left_paren (this);
1380 expression (e);
1381 pp_c_right_paren (this);
1383 break;
1387 /* Print out a C initializer -- also support C compound-literals.
1388 initializer:
1389 assignment-expression:
1390 { initializer-list }
1391 { initializer-list , } */
1393 void
1394 c_pretty_printer::initializer (tree e)
1396 if (TREE_CODE (e) == CONSTRUCTOR)
1397 pp_c_brace_enclosed_initializer_list (this, e);
1398 else
1399 expression (e);
1402 /* init-declarator:
1403 declarator:
1404 declarator = initializer */
1406 void
1407 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1409 pp->declarator (t);
1410 /* We don't want to output function definitions here. There are handled
1411 elsewhere (and the syntactic form is bogus anyway). */
1412 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1414 tree init = DECL_INITIAL (t);
1415 /* This C++ bit is handled here because it is easier to do so.
1416 In templates, the C++ parser builds a TREE_LIST for a
1417 direct-initialization; the TREE_PURPOSE is the variable to
1418 initialize and the TREE_VALUE is the initializer. */
1419 if (TREE_CODE (init) == TREE_LIST)
1421 pp_c_left_paren (pp);
1422 pp->expression (TREE_VALUE (init));
1423 pp_right_paren (pp);
1425 else
1427 pp_space (pp);
1428 pp_equal (pp);
1429 pp_space (pp);
1430 pp->initializer (init);
1435 /* initializer-list:
1436 designation(opt) initializer
1437 initializer-list , designation(opt) initializer
1439 designation:
1440 designator-list =
1442 designator-list:
1443 designator
1444 designator-list designator
1446 designator:
1447 [ constant-expression ]
1448 identifier */
1450 static void
1451 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1453 tree type = TREE_TYPE (e);
1454 const enum tree_code code = TREE_CODE (type);
1456 if (TREE_CODE (e) == CONSTRUCTOR)
1458 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1459 return;
1462 switch (code)
1464 case RECORD_TYPE:
1465 case UNION_TYPE:
1466 case ARRAY_TYPE:
1468 tree init = TREE_OPERAND (e, 0);
1469 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1471 if (code == RECORD_TYPE || code == UNION_TYPE)
1473 pp_c_dot (pp);
1474 pp->primary_expression (TREE_PURPOSE (init));
1476 else
1478 pp_c_left_bracket (pp);
1479 if (TREE_PURPOSE (init))
1480 pp->constant (TREE_PURPOSE (init));
1481 pp_c_right_bracket (pp);
1483 pp_c_whitespace (pp);
1484 pp_equal (pp);
1485 pp_c_whitespace (pp);
1486 pp->initializer (TREE_VALUE (init));
1487 if (TREE_CHAIN (init))
1488 pp_separate_with (pp, ',');
1491 return;
1493 case VECTOR_TYPE:
1494 if (TREE_CODE (e) == VECTOR_CST)
1496 /* We don't create variable-length VECTOR_CSTs. */
1497 unsigned int nunits = VECTOR_CST_NELTS (e).to_constant ();
1498 for (unsigned int i = 0; i < nunits; ++i)
1500 if (i > 0)
1501 pp_separate_with (pp, ',');
1502 pp->expression (VECTOR_CST_ELT (e, i));
1505 else
1506 break;
1507 return;
1509 case COMPLEX_TYPE:
1510 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1512 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1513 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1514 pp_separate_with (pp, ',');
1515 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1517 else
1518 break;
1519 return;
1521 default:
1522 break;
1525 pp_unsupported_tree (pp, type);
1528 /* Pretty-print a brace-enclosed initializer-list. */
1530 static void
1531 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1533 pp_c_left_brace (pp);
1534 pp_c_initializer_list (pp, l);
1535 pp_c_right_brace (pp);
1539 /* This is a convenient function, used to bridge gap between C and C++
1540 grammars.
1542 id-expression:
1543 identifier */
1545 void
1546 c_pretty_printer::id_expression (tree t)
1548 switch (TREE_CODE (t))
1550 case VAR_DECL:
1551 case PARM_DECL:
1552 case CONST_DECL:
1553 case TYPE_DECL:
1554 case FUNCTION_DECL:
1555 case FIELD_DECL:
1556 case LABEL_DECL:
1557 pp_c_tree_decl_identifier (this, t);
1558 break;
1560 case IDENTIFIER_NODE:
1561 pp_c_tree_identifier (this, t);
1562 break;
1564 default:
1565 pp_unsupported_tree (this, t);
1566 break;
1570 /* postfix-expression:
1571 primary-expression
1572 postfix-expression [ expression ]
1573 postfix-expression ( argument-expression-list(opt) )
1574 postfix-expression . identifier
1575 postfix-expression -> identifier
1576 postfix-expression ++
1577 postfix-expression --
1578 ( type-name ) { initializer-list }
1579 ( type-name ) { initializer-list , } */
1581 void
1582 c_pretty_printer::postfix_expression (tree e)
1584 enum tree_code code = TREE_CODE (e);
1585 switch (code)
1587 case POSTINCREMENT_EXPR:
1588 case POSTDECREMENT_EXPR:
1589 postfix_expression (TREE_OPERAND (e, 0));
1590 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1591 break;
1593 case ARRAY_REF:
1594 postfix_expression (TREE_OPERAND (e, 0));
1595 pp_c_left_bracket (this);
1596 expression (TREE_OPERAND (e, 1));
1597 pp_c_right_bracket (this);
1598 break;
1600 case CALL_EXPR:
1602 call_expr_arg_iterator iter;
1603 tree arg;
1604 postfix_expression (CALL_EXPR_FN (e));
1605 pp_c_left_paren (this);
1606 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1608 expression (arg);
1609 if (more_call_expr_args_p (&iter))
1610 pp_separate_with (this, ',');
1612 pp_c_right_paren (this);
1613 break;
1616 case UNORDERED_EXPR:
1617 pp_c_ws_string (this, flag_isoc99
1618 ? "isunordered"
1619 : "__builtin_isunordered");
1620 goto two_args_fun;
1622 case ORDERED_EXPR:
1623 pp_c_ws_string (this, flag_isoc99
1624 ? "!isunordered"
1625 : "!__builtin_isunordered");
1626 goto two_args_fun;
1628 case UNLT_EXPR:
1629 pp_c_ws_string (this, flag_isoc99
1630 ? "!isgreaterequal"
1631 : "!__builtin_isgreaterequal");
1632 goto two_args_fun;
1634 case UNLE_EXPR:
1635 pp_c_ws_string (this, flag_isoc99
1636 ? "!isgreater"
1637 : "!__builtin_isgreater");
1638 goto two_args_fun;
1640 case UNGT_EXPR:
1641 pp_c_ws_string (this, flag_isoc99
1642 ? "!islessequal"
1643 : "!__builtin_islessequal");
1644 goto two_args_fun;
1646 case UNGE_EXPR:
1647 pp_c_ws_string (this, flag_isoc99
1648 ? "!isless"
1649 : "!__builtin_isless");
1650 goto two_args_fun;
1652 case UNEQ_EXPR:
1653 pp_c_ws_string (this, flag_isoc99
1654 ? "!islessgreater"
1655 : "!__builtin_islessgreater");
1656 goto two_args_fun;
1658 case LTGT_EXPR:
1659 pp_c_ws_string (this, flag_isoc99
1660 ? "islessgreater"
1661 : "__builtin_islessgreater");
1662 goto two_args_fun;
1664 case MAX_EXPR:
1665 pp_c_ws_string (this, "max");
1666 goto two_args_fun;
1668 case MIN_EXPR:
1669 pp_c_ws_string (this, "min");
1670 goto two_args_fun;
1672 two_args_fun:
1673 pp_c_left_paren (this);
1674 expression (TREE_OPERAND (e, 0));
1675 pp_separate_with (this, ',');
1676 expression (TREE_OPERAND (e, 1));
1677 pp_c_right_paren (this);
1678 break;
1680 case ABS_EXPR:
1681 pp_c_ws_string (this, "__builtin_abs");
1682 pp_c_left_paren (this);
1683 expression (TREE_OPERAND (e, 0));
1684 pp_c_right_paren (this);
1685 break;
1687 case COMPONENT_REF:
1689 tree object = TREE_OPERAND (e, 0);
1690 if (INDIRECT_REF_P (object))
1692 postfix_expression (TREE_OPERAND (object, 0));
1693 pp_c_arrow (this);
1695 else
1697 postfix_expression (object);
1698 pp_c_dot (this);
1700 expression (TREE_OPERAND (e, 1));
1702 break;
1704 case BIT_FIELD_REF:
1706 tree type = TREE_TYPE (e);
1708 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1709 if (type
1710 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1712 HOST_WIDE_INT bitpos = tree_to_shwi (TREE_OPERAND (e, 2));
1713 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (type));
1714 if ((bitpos % size) == 0)
1716 pp_c_left_paren (this);
1717 pp_c_left_paren (this);
1718 type_id (type);
1719 pp_c_star (this);
1720 pp_c_right_paren (this);
1721 pp_c_ampersand (this);
1722 expression (TREE_OPERAND (e, 0));
1723 pp_c_right_paren (this);
1724 pp_c_left_bracket (this);
1725 pp_wide_integer (this, bitpos / size);
1726 pp_c_right_bracket (this);
1727 break;
1730 pp_unsupported_tree (this, e);
1732 break;
1734 case MEM_REF:
1735 case TARGET_MEM_REF:
1736 expression (e);
1737 break;
1739 case COMPLEX_CST:
1740 case VECTOR_CST:
1741 pp_c_compound_literal (this, e);
1742 break;
1744 case COMPLEX_EXPR:
1745 pp_c_complex_expr (this, e);
1746 break;
1748 case COMPOUND_LITERAL_EXPR:
1749 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1750 /* Fall through. */
1751 case CONSTRUCTOR:
1752 initializer (e);
1753 break;
1755 case VA_ARG_EXPR:
1756 pp_c_ws_string (this, "__builtin_va_arg");
1757 pp_c_left_paren (this);
1758 assignment_expression (TREE_OPERAND (e, 0));
1759 pp_separate_with (this, ',');
1760 type_id (TREE_TYPE (e));
1761 pp_c_right_paren (this);
1762 break;
1764 case ADDR_EXPR:
1765 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1767 id_expression (TREE_OPERAND (e, 0));
1768 break;
1770 /* fall through. */
1772 default:
1773 primary_expression (e);
1774 break;
1778 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1780 void
1781 pp_c_expression_list (c_pretty_printer *pp, tree e)
1783 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1785 pp->expression (TREE_VALUE (e));
1786 if (TREE_CHAIN (e))
1787 pp_separate_with (pp, ',');
1791 /* Print out V, which contains the elements of a constructor. */
1793 void
1794 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1796 unsigned HOST_WIDE_INT ix;
1797 tree value;
1799 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1801 pp->expression (value);
1802 if (ix != vec_safe_length (v) - 1)
1803 pp_separate_with (pp, ',');
1807 /* Print out an expression-list in parens, as if it were the argument
1808 list to a function. */
1810 void
1811 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1813 pp_c_left_paren (pp);
1814 if (t && TREE_CODE (t) == TREE_LIST)
1815 pp_c_expression_list (pp, t);
1816 pp_c_right_paren (pp);
1819 /* Try to fold *(type *)&op into op.fld.fld2[1] if possible.
1820 Only used for printing expressions. Should punt if ambiguous
1821 (e.g. in unions). */
1823 static tree
1824 c_fold_indirect_ref_for_warn (location_t loc, tree type, tree op,
1825 offset_int &off)
1827 tree optype = TREE_TYPE (op);
1828 if (off == 0)
1830 if (lang_hooks.types_compatible_p (optype, type))
1831 return op;
1832 /* *(foo *)&complexfoo => __real__ complexfoo */
1833 else if (TREE_CODE (optype) == COMPLEX_TYPE
1834 && lang_hooks.types_compatible_p (type, TREE_TYPE (optype)))
1835 return build1_loc (loc, REALPART_EXPR, type, op);
1837 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
1838 else if (TREE_CODE (optype) == COMPLEX_TYPE
1839 && lang_hooks.types_compatible_p (type, TREE_TYPE (optype))
1840 && tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
1842 off = 0;
1843 return build1_loc (loc, IMAGPART_EXPR, type, op);
1845 /* ((foo *)&fooarray)[x] => fooarray[x] */
1846 if (TREE_CODE (optype) == ARRAY_TYPE
1847 && TYPE_SIZE_UNIT (TREE_TYPE (optype))
1848 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (optype))) == INTEGER_CST
1849 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
1851 tree type_domain = TYPE_DOMAIN (optype);
1852 tree min_val = size_zero_node;
1853 if (type_domain && TYPE_MIN_VALUE (type_domain))
1854 min_val = TYPE_MIN_VALUE (type_domain);
1855 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
1856 offset_int idx = off / el_sz;
1857 offset_int rem = off % el_sz;
1858 if (TREE_CODE (min_val) == INTEGER_CST)
1860 tree index
1861 = wide_int_to_tree (sizetype, idx + wi::to_offset (min_val));
1862 op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
1863 NULL_TREE, NULL_TREE);
1864 off = rem;
1865 if (tree ret = c_fold_indirect_ref_for_warn (loc, type, op, off))
1866 return ret;
1867 return op;
1870 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
1871 else if (TREE_CODE (optype) == RECORD_TYPE)
1873 for (tree field = TYPE_FIELDS (optype);
1874 field; field = DECL_CHAIN (field))
1875 if (TREE_CODE (field) == FIELD_DECL
1876 && TREE_TYPE (field) != error_mark_node
1877 && TYPE_SIZE_UNIT (TREE_TYPE (field))
1878 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (field))) == INTEGER_CST)
1880 tree pos = byte_position (field);
1881 if (TREE_CODE (pos) != INTEGER_CST)
1882 continue;
1883 offset_int upos = wi::to_offset (pos);
1884 offset_int el_sz
1885 = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (field)));
1886 if (upos <= off && off < upos + el_sz)
1888 tree cop = build3_loc (loc, COMPONENT_REF, TREE_TYPE (field),
1889 op, field, NULL_TREE);
1890 off = off - upos;
1891 if (tree ret = c_fold_indirect_ref_for_warn (loc, type, cop,
1892 off))
1893 return ret;
1894 return cop;
1898 /* Similarly for unions, but in this case try to be very conservative,
1899 only match if some field has type compatible with type and it is the
1900 only such field. */
1901 else if (TREE_CODE (optype) == UNION_TYPE)
1903 tree fld = NULL_TREE;
1904 for (tree field = TYPE_FIELDS (optype);
1905 field; field = DECL_CHAIN (field))
1906 if (TREE_CODE (field) == FIELD_DECL
1907 && TREE_TYPE (field) != error_mark_node
1908 && lang_hooks.types_compatible_p (TREE_TYPE (field), type))
1910 if (fld)
1911 return NULL_TREE;
1912 else
1913 fld = field;
1915 if (fld)
1917 off = 0;
1918 return build3_loc (loc, COMPONENT_REF, TREE_TYPE (fld), op, fld,
1919 NULL_TREE);
1923 return NULL_TREE;
1926 /* Print the MEM_REF expression REF, including its type and offset.
1927 Apply casts as necessary if the type of the access is different
1928 from the type of the accessed object. Produce compact output
1929 designed to include both the element index as well as any
1930 misalignment by preferring
1931 ((int*)((char*)p + 1))[2]
1932 over
1933 *(int*)((char*)p + 9)
1934 The former is more verbose but makes it clearer that the access
1935 to the third element of the array is misaligned by one byte. */
1937 static void
1938 print_mem_ref (c_pretty_printer *pp, tree e)
1940 tree arg = TREE_OPERAND (e, 0);
1942 /* The byte offset. Initially equal to the MEM_REF offset, then
1943 adjusted to the remainder of the division by the byte size of
1944 the access. */
1945 offset_int byte_off = wi::to_offset (TREE_OPERAND (e, 1));
1946 /* The result of dividing BYTE_OFF by the size of the access. */
1947 offset_int elt_idx = 0;
1948 /* True to include a cast to char* (for a nonzero final BYTE_OFF). */
1949 bool char_cast = false;
1950 tree op = NULL_TREE;
1951 bool array_ref_only = false;
1952 if (TREE_CODE (arg) == ADDR_EXPR)
1954 op = c_fold_indirect_ref_for_warn (EXPR_LOCATION (e), TREE_TYPE (e),
1955 TREE_OPERAND (arg, 0), byte_off);
1956 /* Try to fold it back to component, array ref or their combination,
1957 but print it only if the types and TBAA types are compatible. */
1958 if (op
1959 && byte_off == 0
1960 && lang_hooks.types_compatible_p (TREE_TYPE (e), TREE_TYPE (op))
1961 && (!flag_strict_aliasing
1962 || (get_deref_alias_set (TREE_OPERAND (e, 1))
1963 == get_alias_set (op))))
1965 pp->expression (op);
1966 return;
1968 if (op == NULL_TREE)
1969 op = TREE_OPERAND (arg, 0);
1970 /* If the types or TBAA types are incompatible, undo the
1971 UNION_TYPE handling from c_fold_indirect_ref_for_warn, and similarly
1972 undo __real__/__imag__ the code below doesn't try to handle. */
1973 if (op != TREE_OPERAND (arg, 0)
1974 && ((TREE_CODE (op) == COMPONENT_REF
1975 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == UNION_TYPE)
1976 || TREE_CODE (op) == REALPART_EXPR
1977 || TREE_CODE (op) == IMAGPART_EXPR))
1978 op = TREE_OPERAND (op, 0);
1979 if (op != TREE_OPERAND (arg, 0))
1981 array_ref_only = true;
1982 for (tree ref = op; ref != TREE_OPERAND (arg, 0);
1983 ref = TREE_OPERAND (ref, 0))
1984 if (TREE_CODE (ref) != ARRAY_REF)
1986 array_ref_only = false;
1987 break;
1992 tree access_type = TREE_TYPE (e);
1993 tree arg_type = TREE_TYPE (TREE_TYPE (arg));
1994 if (tree access_size = TYPE_SIZE_UNIT (access_type))
1995 if (byte_off != 0
1996 && TREE_CODE (access_size) == INTEGER_CST
1997 && !integer_zerop (access_size))
1999 offset_int asize = wi::to_offset (access_size);
2000 elt_idx = byte_off / asize;
2001 byte_off = byte_off % asize;
2004 /* True to include a cast to the accessed type. */
2005 const bool access_cast
2006 = ((op && op != TREE_OPERAND (arg, 0))
2007 || VOID_TYPE_P (arg_type)
2008 || !lang_hooks.types_compatible_p (access_type, arg_type));
2009 const bool has_off = byte_off != 0 || (op && op != TREE_OPERAND (arg, 0));
2011 if (has_off && (byte_off != 0 || !array_ref_only))
2013 /* When printing the byte offset for a pointer to a type of
2014 a different size than char, include a cast to char* first,
2015 before printing the cast to a pointer to the accessed type. */
2016 tree size = TYPE_SIZE (arg_type);
2017 if (size == NULL_TREE
2018 || TREE_CODE (size) != INTEGER_CST
2019 || wi::to_wide (size) != BITS_PER_UNIT)
2020 char_cast = true;
2023 if (elt_idx == 0)
2024 pp_c_star (pp);
2025 else if (access_cast || char_cast)
2026 pp_c_left_paren (pp);
2028 if (access_cast)
2030 /* Include a cast to the accessed type if it isn't compatible
2031 with the type of the referenced object (or if the object
2032 is typeless). */
2033 pp_c_left_paren (pp);
2034 pp->type_id (build_pointer_type (access_type));
2035 pp_c_right_paren (pp);
2038 if (has_off)
2039 pp_c_left_paren (pp);
2041 if (char_cast)
2043 /* Include a cast to char *. */
2044 pp_c_left_paren (pp);
2045 pp->type_id (string_type_node);
2046 pp_c_right_paren (pp);
2049 pp->unary_expression (arg);
2051 if (op && op != TREE_OPERAND (arg, 0))
2053 auto_vec<tree, 16> refs;
2054 tree ref;
2055 unsigned i;
2056 bool array_refs = true;
2057 for (ref = op; ref != TREE_OPERAND (arg, 0); ref = TREE_OPERAND (ref, 0))
2058 refs.safe_push (ref);
2059 FOR_EACH_VEC_ELT_REVERSE (refs, i, ref)
2060 if (array_refs && TREE_CODE (ref) == ARRAY_REF)
2062 pp_c_left_bracket (pp);
2063 pp->expression (TREE_OPERAND (ref, 1));
2064 pp_c_right_bracket (pp);
2066 else
2068 if (array_refs)
2070 array_refs = false;
2071 pp_string (pp, " + offsetof");
2072 pp_c_left_paren (pp);
2073 pp->type_id (TREE_TYPE (TREE_OPERAND (ref, 0)));
2074 pp_comma (pp);
2076 else if (TREE_CODE (ref) == COMPONENT_REF)
2077 pp_c_dot (pp);
2078 if (TREE_CODE (ref) == COMPONENT_REF)
2079 pp->expression (TREE_OPERAND (ref, 1));
2080 else
2082 pp_c_left_bracket (pp);
2083 pp->expression (TREE_OPERAND (ref, 1));
2084 pp_c_right_bracket (pp);
2087 if (!array_refs)
2088 pp_c_right_paren (pp);
2091 if (byte_off != 0)
2093 pp_space (pp);
2094 pp_plus (pp);
2095 pp_space (pp);
2096 tree off = wide_int_to_tree (ssizetype, byte_off);
2097 pp->constant (off);
2100 if (has_off)
2101 pp_c_right_paren (pp);
2103 if (elt_idx != 0)
2105 if (access_cast || char_cast)
2106 pp_c_right_paren (pp);
2108 pp_c_left_bracket (pp);
2109 tree idx = wide_int_to_tree (ssizetype, elt_idx);
2110 pp->constant (idx);
2111 pp_c_right_bracket (pp);
2115 /* unary-expression:
2116 postfix-expression
2117 ++ cast-expression
2118 -- cast-expression
2119 unary-operator cast-expression
2120 sizeof unary-expression
2121 sizeof ( type-id )
2123 unary-operator: one of
2124 * & + - ! ~
2126 GNU extensions.
2127 unary-expression:
2128 __alignof__ unary-expression
2129 __alignof__ ( type-id )
2130 __real__ unary-expression
2131 __imag__ unary-expression */
2133 void
2134 c_pretty_printer::unary_expression (tree e)
2136 enum tree_code code = TREE_CODE (e);
2137 switch (code)
2139 case PREINCREMENT_EXPR:
2140 case PREDECREMENT_EXPR:
2141 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
2142 unary_expression (TREE_OPERAND (e, 0));
2143 break;
2145 case ADDR_EXPR:
2146 case INDIRECT_REF:
2147 case NEGATE_EXPR:
2148 case BIT_NOT_EXPR:
2149 case TRUTH_NOT_EXPR:
2150 case CONJ_EXPR:
2151 /* String literal are used by address. */
2152 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
2153 pp_ampersand (this);
2154 else if (code == INDIRECT_REF)
2156 tree type = TREE_TYPE (TREE_OPERAND (e, 0));
2157 if (type && TREE_CODE (type) == REFERENCE_TYPE)
2158 /* Reference decay is implicit, don't print anything. */;
2159 else
2160 pp_c_star (this);
2162 else if (code == NEGATE_EXPR)
2163 pp_minus (this);
2164 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
2165 pp_complement (this);
2166 else if (code == TRUTH_NOT_EXPR)
2167 pp_exclamation (this);
2168 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
2169 break;
2171 case MEM_REF:
2172 print_mem_ref (this, e);
2173 break;
2175 case TARGET_MEM_REF:
2176 /* TARGET_MEM_REF can't appear directly from source, but can appear
2177 during late GIMPLE optimizations and through late diagnostic we might
2178 need to support it. Print it as dereferencing of a pointer after
2179 cast to the TARGET_MEM_REF type, with pointer arithmetics on some
2180 pointer to single byte types, so
2181 *(type *)((char *) ptr + step * index + index2) if all the operands
2182 are present and the casts are needed. */
2183 pp_c_star (this);
2184 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TMR_BASE (e)))) == NULL_TREE
2185 || !integer_onep (TYPE_SIZE_UNIT
2186 (TREE_TYPE (TREE_TYPE (TMR_BASE (e))))))
2188 if (TYPE_SIZE_UNIT (TREE_TYPE (e))
2189 && integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (e))))
2191 pp_c_left_paren (this);
2192 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e)));
2194 else
2196 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e)));
2197 pp_c_left_paren (this);
2198 pp_c_type_cast (this, build_pointer_type (char_type_node));
2201 else if (!lang_hooks.types_compatible_p
2202 (TREE_TYPE (e), TREE_TYPE (TREE_TYPE (TMR_BASE (e)))))
2204 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e)));
2205 pp_c_left_paren (this);
2207 else
2208 pp_c_left_paren (this);
2209 pp_c_cast_expression (this, TMR_BASE (e));
2210 if (TMR_STEP (e) && TMR_INDEX (e))
2212 pp_plus (this);
2213 pp_c_cast_expression (this, TMR_INDEX (e));
2214 pp_c_star (this);
2215 pp_c_cast_expression (this, TMR_STEP (e));
2217 if (TMR_INDEX2 (e))
2219 pp_plus (this);
2220 pp_c_cast_expression (this, TMR_INDEX2 (e));
2222 if (!integer_zerop (TMR_OFFSET (e)))
2224 pp_plus (this);
2225 pp_c_integer_constant (this,
2226 fold_convert (ssizetype, TMR_OFFSET (e)));
2228 pp_c_right_paren (this);
2229 break;
2231 case REALPART_EXPR:
2232 case IMAGPART_EXPR:
2233 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
2234 pp_c_whitespace (this);
2235 unary_expression (TREE_OPERAND (e, 0));
2236 break;
2238 default:
2239 postfix_expression (e);
2240 break;
2244 /* cast-expression:
2245 unary-expression
2246 ( type-name ) cast-expression */
2248 void
2249 pp_c_cast_expression (c_pretty_printer *pp, tree e)
2251 switch (TREE_CODE (e))
2253 case FLOAT_EXPR:
2254 case FIX_TRUNC_EXPR:
2255 CASE_CONVERT:
2256 case VIEW_CONVERT_EXPR:
2257 if (!location_wrapper_p (e))
2258 pp_c_type_cast (pp, TREE_TYPE (e));
2259 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
2260 break;
2262 default:
2263 pp->unary_expression (e);
2267 /* multiplicative-expression:
2268 cast-expression
2269 multiplicative-expression * cast-expression
2270 multiplicative-expression / cast-expression
2271 multiplicative-expression % cast-expression */
2273 void
2274 c_pretty_printer::multiplicative_expression (tree e)
2276 enum tree_code code = TREE_CODE (e);
2277 switch (code)
2279 case MULT_EXPR:
2280 case TRUNC_DIV_EXPR:
2281 case TRUNC_MOD_EXPR:
2282 case EXACT_DIV_EXPR:
2283 case RDIV_EXPR:
2284 multiplicative_expression (TREE_OPERAND (e, 0));
2285 pp_c_whitespace (this);
2286 if (code == MULT_EXPR)
2287 pp_c_star (this);
2288 else if (code != TRUNC_MOD_EXPR)
2289 pp_slash (this);
2290 else
2291 pp_modulo (this);
2292 pp_c_whitespace (this);
2293 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
2294 break;
2296 default:
2297 pp_c_cast_expression (this, e);
2298 break;
2302 /* additive-expression:
2303 multiplicative-expression
2304 additive-expression + multiplicative-expression
2305 additive-expression - multiplicative-expression */
2307 static void
2308 pp_c_additive_expression (c_pretty_printer *pp, tree e)
2310 enum tree_code code = TREE_CODE (e);
2311 switch (code)
2313 case POINTER_PLUS_EXPR:
2314 case PLUS_EXPR:
2315 case POINTER_DIFF_EXPR:
2316 case MINUS_EXPR:
2317 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
2318 pp_c_whitespace (pp);
2319 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
2320 pp_plus (pp);
2321 else
2322 pp_minus (pp);
2323 pp_c_whitespace (pp);
2325 tree op1 = TREE_OPERAND (e, 1);
2326 if (code == POINTER_PLUS_EXPR
2327 && TREE_CODE (op1) == INTEGER_CST
2328 && tree_int_cst_sign_bit (op1))
2329 /* A pointer minus an integer is represented internally as plus a very
2330 large number, don't expose that to users. */
2331 op1 = convert (ssizetype, op1);
2332 pp->multiplicative_expression (op1);
2334 break;
2336 default:
2337 pp->multiplicative_expression (e);
2338 break;
2342 /* additive-expression:
2343 additive-expression
2344 shift-expression << additive-expression
2345 shift-expression >> additive-expression */
2347 static void
2348 pp_c_shift_expression (c_pretty_printer *pp, tree e)
2350 enum tree_code code = TREE_CODE (e);
2351 switch (code)
2353 case LSHIFT_EXPR:
2354 case RSHIFT_EXPR:
2355 case LROTATE_EXPR:
2356 case RROTATE_EXPR:
2357 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
2358 pp_c_whitespace (pp);
2359 pp_string (pp, code == LSHIFT_EXPR ? "<<" :
2360 code == RSHIFT_EXPR ? ">>" :
2361 code == LROTATE_EXPR ? "<<<" : ">>>");
2362 pp_c_whitespace (pp);
2363 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
2364 break;
2366 default:
2367 pp_c_additive_expression (pp, e);
2371 /* relational-expression:
2372 shift-expression
2373 relational-expression < shift-expression
2374 relational-expression > shift-expression
2375 relational-expression <= shift-expression
2376 relational-expression >= shift-expression */
2378 static void
2379 pp_c_relational_expression (c_pretty_printer *pp, tree e)
2381 enum tree_code code = TREE_CODE (e);
2382 switch (code)
2384 case LT_EXPR:
2385 case GT_EXPR:
2386 case LE_EXPR:
2387 case GE_EXPR:
2388 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
2389 pp_c_whitespace (pp);
2390 if (code == LT_EXPR)
2391 pp_less (pp);
2392 else if (code == GT_EXPR)
2393 pp_greater (pp);
2394 else if (code == LE_EXPR)
2395 pp_less_equal (pp);
2396 else if (code == GE_EXPR)
2397 pp_greater_equal (pp);
2398 pp_c_whitespace (pp);
2399 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
2400 break;
2402 default:
2403 pp_c_shift_expression (pp, e);
2404 break;
2408 /* equality-expression:
2409 relational-expression
2410 equality-expression == relational-expression
2411 equality-equality != relational-expression */
2413 static void
2414 pp_c_equality_expression (c_pretty_printer *pp, tree e)
2416 enum tree_code code = TREE_CODE (e);
2417 switch (code)
2419 case EQ_EXPR:
2420 case NE_EXPR:
2421 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
2422 pp_c_whitespace (pp);
2423 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
2424 pp_c_whitespace (pp);
2425 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
2426 break;
2428 default:
2429 pp_c_relational_expression (pp, e);
2430 break;
2434 /* AND-expression:
2435 equality-expression
2436 AND-expression & equality-equality */
2438 static void
2439 pp_c_and_expression (c_pretty_printer *pp, tree e)
2441 if (TREE_CODE (e) == BIT_AND_EXPR)
2443 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
2444 pp_c_whitespace (pp);
2445 pp_ampersand (pp);
2446 pp_c_whitespace (pp);
2447 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
2449 else
2450 pp_c_equality_expression (pp, e);
2453 /* exclusive-OR-expression:
2454 AND-expression
2455 exclusive-OR-expression ^ AND-expression */
2457 static void
2458 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
2460 if (TREE_CODE (e) == BIT_XOR_EXPR
2461 || TREE_CODE (e) == TRUTH_XOR_EXPR)
2463 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2464 if (TREE_CODE (e) == BIT_XOR_EXPR)
2465 pp_c_maybe_whitespace (pp);
2466 else
2467 pp_c_whitespace (pp);
2468 pp_carret (pp);
2469 pp_c_whitespace (pp);
2470 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2472 else
2473 pp_c_and_expression (pp, e);
2476 /* inclusive-OR-expression:
2477 exclusive-OR-expression
2478 inclusive-OR-expression | exclusive-OR-expression */
2480 static void
2481 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2483 if (TREE_CODE (e) == BIT_IOR_EXPR)
2485 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2486 pp_c_whitespace (pp);
2487 pp_bar (pp);
2488 pp_c_whitespace (pp);
2489 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2491 else
2492 pp_c_exclusive_or_expression (pp, e);
2495 /* logical-AND-expression:
2496 inclusive-OR-expression
2497 logical-AND-expression && inclusive-OR-expression */
2499 static void
2500 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2502 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2503 || TREE_CODE (e) == TRUTH_AND_EXPR)
2505 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2506 pp_c_whitespace (pp);
2507 pp_ampersand_ampersand (pp);
2508 pp_c_whitespace (pp);
2509 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2511 else
2512 pp_c_inclusive_or_expression (pp, e);
2515 /* logical-OR-expression:
2516 logical-AND-expression
2517 logical-OR-expression || logical-AND-expression */
2519 void
2520 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2522 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2523 || TREE_CODE (e) == TRUTH_OR_EXPR)
2525 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2526 pp_c_whitespace (pp);
2527 pp_bar_bar (pp);
2528 pp_c_whitespace (pp);
2529 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2531 else
2532 pp_c_logical_and_expression (pp, e);
2535 /* conditional-expression:
2536 logical-OR-expression
2537 logical-OR-expression ? expression : conditional-expression */
2539 void
2540 c_pretty_printer::conditional_expression (tree e)
2542 if (TREE_CODE (e) == COND_EXPR)
2544 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2545 pp_c_whitespace (this);
2546 pp_question (this);
2547 pp_c_whitespace (this);
2548 expression (TREE_OPERAND (e, 1));
2549 pp_c_whitespace (this);
2550 pp_colon (this);
2551 pp_c_whitespace (this);
2552 conditional_expression (TREE_OPERAND (e, 2));
2554 else
2555 pp_c_logical_or_expression (this, e);
2559 /* assignment-expression:
2560 conditional-expression
2561 unary-expression assignment-operator assignment-expression
2563 assignment-expression: one of
2564 = *= /= %= += -= >>= <<= &= ^= |= */
2566 void
2567 c_pretty_printer::assignment_expression (tree e)
2569 if (TREE_CODE (e) == MODIFY_EXPR
2570 || TREE_CODE (e) == INIT_EXPR)
2572 unary_expression (TREE_OPERAND (e, 0));
2573 pp_c_whitespace (this);
2574 pp_equal (this);
2575 pp_space (this);
2576 expression (TREE_OPERAND (e, 1));
2578 else
2579 conditional_expression (e);
2582 /* expression:
2583 assignment-expression
2584 expression , assignment-expression
2586 Implementation note: instead of going through the usual recursion
2587 chain, I take the liberty of dispatching nodes to the appropriate
2588 functions. This makes some redundancy, but it worths it. That also
2589 prevents a possible infinite recursion between primary_expression ()
2590 and expression (). */
2592 void
2593 c_pretty_printer::expression (tree e)
2595 switch (TREE_CODE (e))
2597 case VOID_CST:
2598 pp_c_void_constant (this);
2599 break;
2601 case INTEGER_CST:
2602 pp_c_integer_constant (this, e);
2603 break;
2605 case REAL_CST:
2606 pp_c_floating_constant (this, e);
2607 break;
2609 case FIXED_CST:
2610 pp_c_fixed_constant (this, e);
2611 break;
2613 case STRING_CST:
2614 pp_c_string_literal (this, e);
2615 break;
2617 case IDENTIFIER_NODE:
2618 case FUNCTION_DECL:
2619 case VAR_DECL:
2620 case CONST_DECL:
2621 case PARM_DECL:
2622 case RESULT_DECL:
2623 case FIELD_DECL:
2624 case LABEL_DECL:
2625 case ERROR_MARK:
2626 primary_expression (e);
2627 break;
2629 case SSA_NAME:
2630 if (SSA_NAME_VAR (e)
2631 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2632 expression (SSA_NAME_VAR (e));
2633 else
2634 translate_string ("<unknown>");
2635 break;
2637 case POSTINCREMENT_EXPR:
2638 case POSTDECREMENT_EXPR:
2639 case ARRAY_REF:
2640 case CALL_EXPR:
2641 case COMPONENT_REF:
2642 case BIT_FIELD_REF:
2643 case COMPLEX_CST:
2644 case COMPLEX_EXPR:
2645 case VECTOR_CST:
2646 case ORDERED_EXPR:
2647 case UNORDERED_EXPR:
2648 case LTGT_EXPR:
2649 case UNEQ_EXPR:
2650 case UNLE_EXPR:
2651 case UNLT_EXPR:
2652 case UNGE_EXPR:
2653 case UNGT_EXPR:
2654 case MAX_EXPR:
2655 case MIN_EXPR:
2656 case ABS_EXPR:
2657 case CONSTRUCTOR:
2658 case COMPOUND_LITERAL_EXPR:
2659 case VA_ARG_EXPR:
2660 postfix_expression (e);
2661 break;
2663 case CONJ_EXPR:
2664 case ADDR_EXPR:
2665 case INDIRECT_REF:
2666 case MEM_REF:
2667 case TARGET_MEM_REF:
2668 case NEGATE_EXPR:
2669 case BIT_NOT_EXPR:
2670 case TRUTH_NOT_EXPR:
2671 case PREINCREMENT_EXPR:
2672 case PREDECREMENT_EXPR:
2673 case REALPART_EXPR:
2674 case IMAGPART_EXPR:
2675 unary_expression (e);
2676 break;
2678 case FLOAT_EXPR:
2679 case FIX_TRUNC_EXPR:
2680 CASE_CONVERT:
2681 case VIEW_CONVERT_EXPR:
2682 pp_c_cast_expression (this, e);
2683 break;
2685 case MULT_EXPR:
2686 case TRUNC_MOD_EXPR:
2687 case TRUNC_DIV_EXPR:
2688 case EXACT_DIV_EXPR:
2689 case RDIV_EXPR:
2690 multiplicative_expression (e);
2691 break;
2693 case LSHIFT_EXPR:
2694 case RSHIFT_EXPR:
2695 case LROTATE_EXPR:
2696 case RROTATE_EXPR:
2697 pp_c_shift_expression (this, e);
2698 break;
2700 case LT_EXPR:
2701 case GT_EXPR:
2702 case LE_EXPR:
2703 case GE_EXPR:
2704 pp_c_relational_expression (this, e);
2705 break;
2707 case BIT_AND_EXPR:
2708 pp_c_and_expression (this, e);
2709 break;
2711 case BIT_XOR_EXPR:
2712 case TRUTH_XOR_EXPR:
2713 pp_c_exclusive_or_expression (this, e);
2714 break;
2716 case BIT_IOR_EXPR:
2717 pp_c_inclusive_or_expression (this, e);
2718 break;
2720 case TRUTH_ANDIF_EXPR:
2721 case TRUTH_AND_EXPR:
2722 pp_c_logical_and_expression (this, e);
2723 break;
2725 case TRUTH_ORIF_EXPR:
2726 case TRUTH_OR_EXPR:
2727 pp_c_logical_or_expression (this, e);
2728 break;
2730 case EQ_EXPR:
2731 case NE_EXPR:
2732 pp_c_equality_expression (this, e);
2733 break;
2735 case COND_EXPR:
2736 conditional_expression (e);
2737 break;
2739 case POINTER_PLUS_EXPR:
2740 case PLUS_EXPR:
2741 case POINTER_DIFF_EXPR:
2742 case MINUS_EXPR:
2743 pp_c_additive_expression (this, e);
2744 break;
2746 case MODIFY_EXPR:
2747 case INIT_EXPR:
2748 assignment_expression (e);
2749 break;
2751 case COMPOUND_EXPR:
2752 pp_c_left_paren (this);
2753 expression (TREE_OPERAND (e, 0));
2754 pp_separate_with (this, ',');
2755 assignment_expression (TREE_OPERAND (e, 1));
2756 pp_c_right_paren (this);
2757 break;
2759 case NON_LVALUE_EXPR:
2760 case SAVE_EXPR:
2761 expression (TREE_OPERAND (e, 0));
2762 break;
2764 case TARGET_EXPR:
2765 postfix_expression (TREE_OPERAND (e, 1));
2766 break;
2768 case BIND_EXPR:
2769 case GOTO_EXPR:
2770 /* We don't yet have a way of dumping statements in a
2771 human-readable format. */
2772 pp_string (this, "({...})");
2773 break;
2775 case C_MAYBE_CONST_EXPR:
2776 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2777 break;
2779 default:
2780 pp_unsupported_tree (this, e);
2781 break;
2787 /* Statements. */
2789 void
2790 c_pretty_printer::statement (tree t)
2792 if (t == NULL)
2793 return;
2795 switch (TREE_CODE (t))
2798 case SWITCH_STMT:
2799 pp_c_ws_string (this, "switch");
2800 pp_space (this);
2801 pp_c_left_paren (this);
2802 expression (SWITCH_STMT_COND (t));
2803 pp_c_right_paren (this);
2804 pp_indentation (this) += 3;
2805 pp_needs_newline (this) = true;
2806 statement (SWITCH_STMT_BODY (t));
2807 pp_newline_and_indent (this, -3);
2808 break;
2810 /* iteration-statement:
2811 while ( expression ) statement
2812 do statement while ( expression ) ;
2813 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
2814 for ( declaration expression(opt) ; expression(opt) ) statement */
2815 case WHILE_STMT:
2816 pp_c_ws_string (this, "while");
2817 pp_space (this);
2818 pp_c_left_paren (this);
2819 expression (WHILE_COND (t));
2820 pp_c_right_paren (this);
2821 pp_newline_and_indent (this, 3);
2822 statement (WHILE_BODY (t));
2823 pp_indentation (this) -= 3;
2824 pp_needs_newline (this) = true;
2825 break;
2827 case DO_STMT:
2828 pp_c_ws_string (this, "do");
2829 pp_newline_and_indent (this, 3);
2830 statement (DO_BODY (t));
2831 pp_newline_and_indent (this, -3);
2832 pp_c_ws_string (this, "while");
2833 pp_space (this);
2834 pp_c_left_paren (this);
2835 expression (DO_COND (t));
2836 pp_c_right_paren (this);
2837 pp_c_semicolon (this);
2838 pp_needs_newline (this) = true;
2839 break;
2841 case FOR_STMT:
2842 pp_c_ws_string (this, "for");
2843 pp_space (this);
2844 pp_c_left_paren (this);
2845 if (FOR_INIT_STMT (t))
2846 statement (FOR_INIT_STMT (t));
2847 else
2848 pp_c_semicolon (this);
2849 pp_needs_newline (this) = false;
2850 pp_c_whitespace (this);
2851 if (FOR_COND (t))
2852 expression (FOR_COND (t));
2853 pp_c_semicolon (this);
2854 pp_needs_newline (this) = false;
2855 pp_c_whitespace (this);
2856 if (FOR_EXPR (t))
2857 expression (FOR_EXPR (t));
2858 pp_c_right_paren (this);
2859 pp_newline_and_indent (this, 3);
2860 statement (FOR_BODY (t));
2861 pp_indentation (this) -= 3;
2862 pp_needs_newline (this) = true;
2863 break;
2865 /* jump-statement:
2866 goto identifier;
2867 continue ;
2868 return expression(opt) ; */
2869 case BREAK_STMT:
2870 case CONTINUE_STMT:
2871 pp_string (this, TREE_CODE (t) == BREAK_STMT ? "break" : "continue");
2872 pp_c_semicolon (this);
2873 pp_needs_newline (this) = true;
2874 break;
2876 default:
2877 if (pp_needs_newline (this))
2878 pp_newline_and_indent (this, 0);
2879 dump_generic_node (this, t, pp_indentation (this), TDF_NONE, true);
2884 /* Initialize the PRETTY-PRINTER for handling C codes. */
2886 c_pretty_printer::c_pretty_printer ()
2887 : pretty_printer (),
2888 offset_list (),
2889 flags ()
2891 type_specifier_seq = pp_c_specifier_qualifier_list;
2892 ptr_operator = pp_c_pointer;
2893 parameter_list = pp_c_parameter_type_list;
2896 /* c_pretty_printer's implementation of pretty_printer::clone vfunc. */
2898 pretty_printer *
2899 c_pretty_printer::clone () const
2901 return new c_pretty_printer (*this);
2904 /* Print the tree T in full, on file FILE. */
2906 void
2907 print_c_tree (FILE *file, tree t)
2909 c_pretty_printer pp;
2911 pp_needs_newline (&pp) = true;
2912 pp.buffer->stream = file;
2913 pp.statement (t);
2914 pp_newline_and_flush (&pp);
2917 /* Print the tree T in full, on stderr. */
2919 DEBUG_FUNCTION void
2920 debug_c_tree (tree t)
2922 print_c_tree (stderr, t);
2923 fputc ('\n', stderr);
2926 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2927 up of T's memory address. */
2929 void
2930 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2932 const char *name;
2934 gcc_assert (DECL_P (t));
2936 if (DECL_NAME (t))
2937 name = IDENTIFIER_POINTER (DECL_NAME (t));
2938 else
2940 static char xname[8];
2941 sprintf (xname, "<U%4hx>", ((unsigned short) ((uintptr_t) (t)
2942 & 0xffff)));
2943 name = xname;
2946 pp_c_identifier (pp, name);
2949 #if CHECKING_P
2951 namespace selftest {
2953 /* Selftests for pretty-printing trees. */
2955 /* Verify that EXPR printed by c_pretty_printer is EXPECTED, using
2956 LOC as the effective location for any failures. */
2958 static void
2959 assert_c_pretty_printer_output (const location &loc, const char *expected,
2960 tree expr)
2962 c_pretty_printer pp;
2963 pp.expression (expr);
2964 ASSERT_STREQ_AT (loc, expected, pp_formatted_text (&pp));
2967 /* Helper function for calling assert_c_pretty_printer_output.
2968 This is to avoid having to write SELFTEST_LOCATION. */
2970 #define ASSERT_C_PRETTY_PRINTER_OUTPUT(EXPECTED, EXPR) \
2971 SELFTEST_BEGIN_STMT \
2972 assert_c_pretty_printer_output ((SELFTEST_LOCATION), \
2973 (EXPECTED), \
2974 (EXPR)); \
2975 SELFTEST_END_STMT
2977 /* Verify that location wrappers don't show up in pretty-printed output. */
2979 static void
2980 test_location_wrappers ()
2982 /* VAR_DECL. */
2983 tree id = get_identifier ("foo");
2984 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, id,
2985 integer_type_node);
2986 tree wrapped_decl = maybe_wrap_with_location (decl, BUILTINS_LOCATION);
2987 ASSERT_NE (wrapped_decl, decl);
2988 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", decl);
2989 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", wrapped_decl);
2991 /* INTEGER_CST. */
2992 tree int_cst = build_int_cst (integer_type_node, 42);
2993 tree wrapped_cst = maybe_wrap_with_location (int_cst, BUILTINS_LOCATION);
2994 ASSERT_NE (wrapped_cst, int_cst);
2995 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", int_cst);
2996 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", wrapped_cst);
2999 /* Run all of the selftests within this file. */
3001 void
3002 c_pretty_print_c_tests ()
3004 test_location_wrappers ();
3007 } // namespace selftest
3009 #endif /* CHECKING_P */