Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / c-family / c-pretty-print.cc
blob7536a7c471ffe11df61b6acea1015ba752b89516
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2023 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"
35 #include "internal-fn.h"
37 /* The pretty-printer code is primarily designed to closely follow
38 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
39 codes we used to have in the past. Following a structured
40 approach (preferably the official grammars) is believed to make it
41 much easier to add extensions and nifty pretty-printing effects that
42 takes expression or declaration contexts into account. */
45 #define pp_c_maybe_whitespace(PP) \
46 do { \
47 if ((PP)->padding == pp_before) \
48 pp_c_whitespace (PP); \
49 } while (0)
51 /* literal */
52 static void pp_c_char (c_pretty_printer *, int);
54 /* postfix-expression */
55 static void pp_c_initializer_list (c_pretty_printer *, tree);
56 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
58 static void pp_c_additive_expression (c_pretty_printer *, tree);
59 static void pp_c_shift_expression (c_pretty_printer *, tree);
60 static void pp_c_relational_expression (c_pretty_printer *, tree);
61 static void pp_c_equality_expression (c_pretty_printer *, tree);
62 static void pp_c_and_expression (c_pretty_printer *, tree);
63 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
64 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
65 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
67 /* declarations. */
70 /* Helper functions. */
72 void
73 pp_c_whitespace (c_pretty_printer *pp)
75 pp_space (pp);
76 pp->padding = pp_none;
79 void
80 pp_c_left_paren (c_pretty_printer *pp)
82 pp_left_paren (pp);
83 pp->padding = pp_none;
86 void
87 pp_c_right_paren (c_pretty_printer *pp)
89 pp_right_paren (pp);
90 pp->padding = pp_none;
93 void
94 pp_c_left_brace (c_pretty_printer *pp)
96 pp_left_brace (pp);
97 pp->padding = pp_none;
100 void
101 pp_c_right_brace (c_pretty_printer *pp)
103 pp_right_brace (pp);
104 pp->padding = pp_none;
107 void
108 pp_c_left_bracket (c_pretty_printer *pp)
110 pp_left_bracket (pp);
111 pp->padding = pp_none;
114 void
115 pp_c_right_bracket (c_pretty_printer *pp)
117 pp_right_bracket (pp);
118 pp->padding = pp_none;
121 void
122 pp_c_dot (c_pretty_printer *pp)
124 pp_dot (pp);
125 pp->padding = pp_none;
128 void
129 pp_c_ampersand (c_pretty_printer *pp)
131 pp_ampersand (pp);
132 pp->padding = pp_none;
135 void
136 pp_c_star (c_pretty_printer *pp)
138 pp_star (pp);
139 pp->padding = pp_none;
142 void
143 pp_c_arrow (c_pretty_printer *pp)
145 pp_arrow (pp);
146 pp->padding = pp_none;
149 void
150 pp_c_semicolon (c_pretty_printer *pp)
152 pp_semicolon (pp);
153 pp->padding = pp_none;
156 void
157 pp_c_complement (c_pretty_printer *pp)
159 pp_complement (pp);
160 pp->padding = pp_none;
163 void
164 pp_c_exclamation (c_pretty_printer *pp)
166 pp_exclamation (pp);
167 pp->padding = pp_none;
170 /* Print out the external representation of QUALIFIERS. */
172 void
173 pp_c_cv_qualifiers (c_pretty_printer *pp, int qualifiers, bool func_type)
175 const char *p = pp_last_position_in_text (pp);
177 if (!qualifiers)
178 return;
180 /* The C programming language does not have references, but it is much
181 simpler to handle those here rather than going through the same
182 logic in the C++ pretty-printer. */
183 if (p != NULL && (*p == '*' || *p == '&'))
184 pp_c_whitespace (pp);
186 if (qualifiers & TYPE_QUAL_ATOMIC)
187 pp_c_ws_string (pp, "_Atomic");
188 if (qualifiers & TYPE_QUAL_CONST)
189 pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const");
190 if (qualifiers & TYPE_QUAL_VOLATILE)
191 pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile");
192 if (qualifiers & TYPE_QUAL_RESTRICT)
193 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
194 ? "restrict" : "__restrict__"));
197 /* Pretty-print T using the type-cast notation '( type-name )'. */
199 void
200 pp_c_type_cast (c_pretty_printer *pp, tree t)
202 pp_c_left_paren (pp);
203 pp->type_id (t);
204 pp_c_right_paren (pp);
207 /* We're about to pretty-print a pointer type as indicated by T.
208 Output a whitespace, if needed, preparing for subsequent output. */
210 void
211 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
213 if (POINTER_TYPE_P (t))
215 tree pointee = strip_pointer_operator (TREE_TYPE (t));
216 if (TREE_CODE (pointee) != ARRAY_TYPE
217 && TREE_CODE (pointee) != FUNCTION_TYPE)
218 pp_c_whitespace (pp);
223 /* Declarations. */
225 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
226 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
227 of its type. Take care of possible extensions.
229 type-qualifier-list:
230 type-qualifier
231 type-qualifier-list type-qualifier
233 type-qualifier:
234 const
235 restrict -- C99
236 __restrict__ -- GNU C
237 address-space-qualifier -- GNU C
238 volatile
239 _Atomic -- C11
241 address-space-qualifier:
242 identifier -- GNU C */
244 void
245 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
247 int qualifiers;
249 if (!t || t == error_mark_node)
250 return;
252 if (!TYPE_P (t))
253 t = TREE_TYPE (t);
255 if (TREE_CODE (t) != ARRAY_TYPE)
257 qualifiers = TYPE_QUALS (t);
258 pp_c_cv_qualifiers (pp, qualifiers,
259 TREE_CODE (t) == FUNCTION_TYPE);
262 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
264 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
265 pp_c_identifier (pp, as);
269 /* pointer:
270 * type-qualifier-list(opt)
271 * type-qualifier-list(opt) pointer */
273 static void
274 pp_c_pointer (c_pretty_printer *pp, tree t)
276 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
277 t = TREE_TYPE (t);
278 switch (TREE_CODE (t))
280 case POINTER_TYPE:
281 /* It is easier to handle C++ reference types here. */
282 case REFERENCE_TYPE:
283 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
284 pp_c_pointer (pp, TREE_TYPE (t));
285 if (TREE_CODE (t) == POINTER_TYPE)
286 pp_c_star (pp);
287 else
289 pp_c_ampersand (pp);
290 if (TYPE_REF_IS_RVALUE (t))
291 pp_c_ampersand (pp);
293 pp_c_type_qualifier_list (pp, t);
294 break;
296 /* ??? This node is now in GENERIC and so shouldn't be here. But
297 we'll fix that later. */
298 case DECL_EXPR:
299 pp->declaration (DECL_EXPR_DECL (t));
300 pp_needs_newline (pp) = true;
301 break;
303 default:
304 pp_unsupported_tree (pp, t);
308 /* simple-type-specifier:
309 type-specifier
311 type-specifier:
312 void
313 char
314 short
316 long
317 float
318 double
319 signed
320 unsigned
321 _Bool -- C99
322 _Complex -- C99
323 _Imaginary -- C99
324 nullptr_t -- C23
325 struct-or-union-specifier
326 enum-specifier
327 typedef-name.
329 GNU extensions.
330 simple-type-specifier:
331 __complex__
332 __vector__ */
334 void
335 c_pretty_printer::simple_type_specifier (tree t)
337 const enum tree_code code = TREE_CODE (t);
338 switch (code)
340 case ERROR_MARK:
341 translate_string ("<type-error>");
342 break;
344 case IDENTIFIER_NODE:
345 pp_c_identifier (this, IDENTIFIER_POINTER (t));
346 break;
348 case VOID_TYPE:
349 case OPAQUE_TYPE:
350 case BOOLEAN_TYPE:
351 case INTEGER_TYPE:
352 case REAL_TYPE:
353 case FIXED_POINT_TYPE:
354 if (TYPE_NAME (t))
356 t = TYPE_NAME (t);
357 simple_type_specifier (t);
359 else
361 int prec = TYPE_PRECISION (t);
362 tree common_t;
363 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
364 common_t = c_common_type_for_mode (TYPE_MODE (t),
365 TYPE_SATURATING (t));
366 else
367 common_t = c_common_type_for_mode (TYPE_MODE (t),
368 TYPE_UNSIGNED (t));
369 if (common_t && TYPE_NAME (common_t))
371 simple_type_specifier (common_t);
372 if (TYPE_PRECISION (common_t) != prec)
374 pp_colon (this);
375 pp_decimal_int (this, prec);
378 else
380 switch (code)
382 case INTEGER_TYPE:
383 translate_string (TYPE_UNSIGNED (t)
384 ? "<unnamed-unsigned:"
385 : "<unnamed-signed:");
386 break;
387 case REAL_TYPE:
388 translate_string ("<unnamed-float:");
389 break;
390 case FIXED_POINT_TYPE:
391 translate_string ("<unnamed-fixed:");
392 break;
393 default:
394 gcc_unreachable ();
396 pp_decimal_int (this, prec);
397 pp_greater (this);
400 break;
402 case TYPE_DECL:
403 if (DECL_NAME (t))
404 id_expression (t);
405 else
406 translate_string ("<typedef-error>");
407 break;
409 case UNION_TYPE:
410 case RECORD_TYPE:
411 case ENUMERAL_TYPE:
412 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
413 /* Don't decorate the type if this is a typedef name. */;
414 else if (code == UNION_TYPE)
415 pp_c_ws_string (this, "union");
416 else if (code == RECORD_TYPE)
417 pp_c_ws_string (this, "struct");
418 else if (code == ENUMERAL_TYPE)
419 pp_c_ws_string (this, "enum");
420 else
421 translate_string ("<tag-error>");
423 if (TYPE_NAME (t))
424 id_expression (TYPE_NAME (t));
425 else
426 translate_string ("<anonymous>");
427 break;
428 case NULLPTR_TYPE:
429 pp_c_ws_string (this, "nullptr_t");
430 break;
432 default:
433 pp_unsupported_tree (this, t);
434 break;
438 /* specifier-qualifier-list:
439 type-specifier specifier-qualifier-list-opt
440 type-qualifier specifier-qualifier-list-opt
443 Implementation note: Because of the non-linearities in array or
444 function declarations, this routine prints not just the
445 specifier-qualifier-list of such entities or types of such entities,
446 but also the 'pointer' production part of their declarators. The
447 remaining part is done by declarator() or abstract_declarator(). */
449 void
450 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
452 const enum tree_code code = TREE_CODE (t);
454 if (!(pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
455 pp_c_type_qualifier_list (pp, t);
456 switch (code)
458 case REFERENCE_TYPE:
459 case POINTER_TYPE:
461 /* Get the types-specifier of this type. */
462 tree pointee = strip_pointer_operator (TREE_TYPE (t));
463 pp_c_specifier_qualifier_list (pp, pointee);
464 if (TREE_CODE (pointee) == ARRAY_TYPE
465 || TREE_CODE (pointee) == FUNCTION_TYPE)
467 pp_c_whitespace (pp);
468 pp_c_left_paren (pp);
469 /* If we're dealing with the GNU form of attributes, print this:
470 void (__attribute__((noreturn)) *f) ();
471 If it is the standard [[]] attribute, we'll print the attribute
472 in c_pretty_printer::direct_abstract_declarator/FUNCTION_TYPE. */
473 if (!cxx11_attribute_p (TYPE_ATTRIBUTES (pointee)))
474 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
476 else if (!c_dialect_cxx ())
477 pp_c_whitespace (pp);
478 pp_ptr_operator (pp, t);
480 break;
482 case FUNCTION_TYPE:
483 case ARRAY_TYPE:
484 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
485 break;
487 case VECTOR_TYPE:
488 case COMPLEX_TYPE:
489 if (code == COMPLEX_TYPE)
490 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
491 ? "_Complex" : "__complex__"));
492 else if (code == VECTOR_TYPE)
494 /* The syntax we print for vector types isn't real C or C++ syntax,
495 so it's better to print the type name if we have one. */
496 tree name = TYPE_NAME (t);
497 if (!(pp->flags & pp_c_flag_gnu_v3)
498 && name
499 && TREE_CODE (name) == TYPE_DECL)
501 pp->id_expression (name);
502 break;
504 pp_c_ws_string (pp, "__vector");
505 pp_c_left_paren (pp);
506 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
507 pp_c_right_paren (pp);
508 pp_c_whitespace (pp);
510 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
511 break;
513 default:
514 pp->simple_type_specifier (t);
515 break;
517 if ((pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
518 pp_c_type_qualifier_list (pp, t);
521 /* parameter-type-list:
522 parameter-list
523 parameter-list , ...
525 parameter-list:
526 parameter-declaration
527 parameter-list , parameter-declaration
529 parameter-declaration:
530 declaration-specifiers declarator
531 declaration-specifiers abstract-declarator(opt) */
533 void
534 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
536 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
537 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
538 pp_c_left_paren (pp);
539 if (parms == void_list_node)
540 pp_c_ws_string (pp, "void");
541 else
543 bool first = true;
544 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
546 if (!first)
547 pp_separate_with (pp, ',');
548 first = false;
549 pp->declaration_specifiers
550 (want_parm_decl ? parms : TREE_VALUE (parms));
551 if (want_parm_decl)
552 pp->declarator (parms);
553 else
554 pp->abstract_declarator (TREE_VALUE (parms));
556 if (!first && !parms)
558 pp_separate_with (pp, ',');
559 pp_string (pp, "...");
562 pp_c_right_paren (pp);
565 /* abstract-declarator:
566 pointer
567 pointer(opt) direct-abstract-declarator */
569 void
570 c_pretty_printer::abstract_declarator (tree t)
572 if (TREE_CODE (t) == POINTER_TYPE)
574 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
575 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
576 pp_c_right_paren (this);
577 t = TREE_TYPE (t);
580 direct_abstract_declarator (t);
583 /* direct-abstract-declarator:
584 ( abstract-declarator )
585 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
586 direct-abstract-declarator(opt) [ * ]
587 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
589 void
590 c_pretty_printer::direct_abstract_declarator (tree t)
592 bool add_space = false;
594 switch (TREE_CODE (t))
596 case POINTER_TYPE:
597 abstract_declarator (t);
598 break;
600 case FUNCTION_TYPE:
601 pp_c_parameter_type_list (this, t);
602 direct_abstract_declarator (TREE_TYPE (t));
603 /* If this is the standard [[]] attribute, print
604 void (*)() [[noreturn]]; */
605 if (cxx11_attribute_p (TYPE_ATTRIBUTES (t)))
607 pp_space (this);
608 pp_c_attributes_display (this, TYPE_ATTRIBUTES (t));
610 break;
612 case ARRAY_TYPE:
613 pp_c_left_bracket (this);
615 if (int quals = TYPE_QUALS (t))
617 /* Print the array qualifiers such as in "T[const restrict 3]". */
618 pp_c_cv_qualifiers (this, quals, false);
619 add_space = true;
622 if (tree arr = lookup_attribute ("array", TYPE_ATTRIBUTES (t)))
624 if (TREE_VALUE (arr))
626 /* Print the specifier as in "T[static 3]" that's not actually
627 part of the type but may be added by the front end. */
628 pp_c_ws_string (this, "static");
629 add_space = true;
631 else if (!TYPE_DOMAIN (t))
632 /* For arrays of unspecified bound using the [*] notation. */
633 pp_character (this, '*');
636 if (tree dom = TYPE_DOMAIN (t))
638 if (tree maxval = TYPE_MAX_VALUE (dom))
640 if (add_space)
641 pp_space (this);
643 tree type = TREE_TYPE (maxval);
645 if (tree_fits_shwi_p (maxval))
646 pp_wide_integer (this, tree_to_shwi (maxval) + 1);
647 else if (TREE_CODE (maxval) == INTEGER_CST)
648 expression (fold_build2 (PLUS_EXPR, type, maxval,
649 build_int_cst (type, 1)));
650 else
652 /* Strip the expressions from around a VLA bound added
653 internally to make it fit the domain mold, including
654 any casts. */
655 if (TREE_CODE (maxval) == NOP_EXPR)
656 maxval = TREE_OPERAND (maxval, 0);
657 if (TREE_CODE (maxval) == PLUS_EXPR
658 && integer_all_onesp (TREE_OPERAND (maxval, 1)))
660 maxval = TREE_OPERAND (maxval, 0);
661 if (TREE_CODE (maxval) == NOP_EXPR)
662 maxval = TREE_OPERAND (maxval, 0);
664 if (TREE_CODE (maxval) == SAVE_EXPR)
666 maxval = TREE_OPERAND (maxval, 0);
667 if (TREE_CODE (maxval) == NOP_EXPR)
668 maxval = TREE_OPERAND (maxval, 0);
671 expression (maxval);
674 else if (TYPE_SIZE (t))
675 /* Print zero for zero-length arrays but not for flexible
676 array members whose TYPE_SIZE is null. */
677 pp_string (this, "0");
679 pp_c_right_bracket (this);
680 direct_abstract_declarator (TREE_TYPE (t));
681 break;
683 case IDENTIFIER_NODE:
684 case VOID_TYPE:
685 case OPAQUE_TYPE:
686 case BOOLEAN_TYPE:
687 case INTEGER_TYPE:
688 case REAL_TYPE:
689 case FIXED_POINT_TYPE:
690 case ENUMERAL_TYPE:
691 case RECORD_TYPE:
692 case UNION_TYPE:
693 case VECTOR_TYPE:
694 case COMPLEX_TYPE:
695 case TYPE_DECL:
696 case ERROR_MARK:
697 case NULLPTR_TYPE:
698 break;
700 default:
701 pp_unsupported_tree (this, t);
702 break;
706 /* type-name:
707 specifier-qualifier-list abstract-declarator(opt) */
709 void
710 c_pretty_printer::type_id (tree t)
712 pp_c_specifier_qualifier_list (this, t);
713 abstract_declarator (t);
716 /* storage-class-specifier:
717 typedef
718 extern
719 static
720 auto
721 register */
723 void
724 c_pretty_printer::storage_class_specifier (tree t)
726 if (TREE_CODE (t) == TYPE_DECL)
727 pp_c_ws_string (this, "typedef");
728 else if (DECL_P (t))
730 if (DECL_REGISTER (t))
731 pp_c_ws_string (this, "register");
732 else if (TREE_STATIC (t) && VAR_P (t))
733 pp_c_ws_string (this, "static");
737 /* function-specifier:
738 inline */
740 void
741 c_pretty_printer::function_specifier (tree t)
743 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
744 pp_c_ws_string (this, "inline");
747 /* declaration-specifiers:
748 storage-class-specifier declaration-specifiers(opt)
749 type-specifier declaration-specifiers(opt)
750 type-qualifier declaration-specifiers(opt)
751 function-specifier declaration-specifiers(opt) */
753 void
754 c_pretty_printer::declaration_specifiers (tree t)
756 storage_class_specifier (t);
757 function_specifier (t);
758 pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t);
761 /* direct-declarator
762 identifier
763 ( declarator )
764 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
765 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
766 direct-declarator [ type-qualifier-list static assignment-expression ]
767 direct-declarator [ type-qualifier-list * ]
768 direct-declarator ( parameter-type-list )
769 direct-declarator ( identifier-list(opt) ) */
771 void
772 c_pretty_printer::direct_declarator (tree t)
774 switch (TREE_CODE (t))
776 case VAR_DECL:
777 case PARM_DECL:
778 case TYPE_DECL:
779 case FIELD_DECL:
780 case LABEL_DECL:
781 pp_c_space_for_pointer_operator (this, TREE_TYPE (t));
782 pp_c_tree_decl_identifier (this, t);
783 break;
785 case ARRAY_TYPE:
786 case POINTER_TYPE:
787 abstract_declarator (TREE_TYPE (t));
788 break;
790 case FUNCTION_TYPE:
791 pp_parameter_list (this, t);
792 abstract_declarator (TREE_TYPE (t));
793 break;
795 case FUNCTION_DECL:
796 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
797 pp_c_tree_decl_identifier (this, t);
798 if (flags & pp_c_flag_abstract)
799 abstract_declarator (TREE_TYPE (t));
800 else
802 pp_parameter_list (this, t);
803 abstract_declarator (TREE_TYPE (TREE_TYPE (t)));
805 break;
807 case INTEGER_TYPE:
808 case REAL_TYPE:
809 case FIXED_POINT_TYPE:
810 case ENUMERAL_TYPE:
811 case UNION_TYPE:
812 case RECORD_TYPE:
813 break;
815 default:
816 pp_unsupported_tree (this, t);
817 break;
822 /* declarator:
823 pointer(opt) direct-declarator */
825 void
826 c_pretty_printer::declarator (tree t)
828 switch (TREE_CODE (t))
830 case INTEGER_TYPE:
831 case REAL_TYPE:
832 case FIXED_POINT_TYPE:
833 case ENUMERAL_TYPE:
834 case UNION_TYPE:
835 case RECORD_TYPE:
836 break;
838 case VAR_DECL:
839 case PARM_DECL:
840 case FIELD_DECL:
841 case ARRAY_TYPE:
842 case FUNCTION_TYPE:
843 case FUNCTION_DECL:
844 case TYPE_DECL:
845 direct_declarator (t);
846 break;
849 default:
850 pp_unsupported_tree (this, t);
851 break;
855 /* declaration:
856 declaration-specifiers init-declarator-list(opt) ; */
858 void
859 c_pretty_printer::declaration (tree t)
861 declaration_specifiers (t);
862 pp_c_init_declarator (this, t);
865 /* Pretty-print ATTRIBUTES marked to be displayed on diagnostic. */
867 void
868 pp_c_attributes_display (c_pretty_printer *pp, tree a)
870 bool is_first = true;
872 if (a == NULL_TREE)
873 return;
875 const bool std_p = cxx11_attribute_p (a);
877 for (; a != NULL_TREE; a = TREE_CHAIN (a))
879 const struct attribute_spec *as
880 = lookup_attribute_spec (get_attribute_name (a));
881 if (!as || as->affects_type_identity == false)
882 continue;
883 if (c_dialect_cxx ()
884 && !strcmp ("transaction_safe", as->name))
885 /* In C++ transaction_safe is printed at the end of the declarator. */
886 continue;
887 if (is_first)
889 if (std_p)
891 pp_c_left_bracket (pp);
892 pp_c_left_bracket (pp);
894 else
896 pp_c_ws_string (pp, "__attribute__");
897 pp_c_left_paren (pp);
898 pp_c_left_paren (pp);
900 is_first = false;
902 else
903 pp_separate_with (pp, ',');
904 tree ns;
905 if (std_p && (ns = get_attribute_namespace (a)))
907 pp_tree_identifier (pp, ns);
908 pp_colon (pp);
909 pp_colon (pp);
911 pp_tree_identifier (pp, get_attribute_name (a));
912 if (TREE_VALUE (a))
913 pp_c_call_argument_list (pp, TREE_VALUE (a));
916 if (!is_first)
918 if (std_p)
920 pp_c_right_bracket (pp);
921 pp_c_right_bracket (pp);
923 else
925 pp_c_right_paren (pp);
926 pp_c_right_paren (pp);
927 pp_c_whitespace (pp);
932 /* function-definition:
933 declaration-specifiers declarator compound-statement */
935 void
936 pp_c_function_definition (c_pretty_printer *pp, tree t)
938 pp->declaration_specifiers (t);
939 pp->declarator (t);
940 pp_needs_newline (pp) = true;
941 pp->statement (DECL_SAVED_TREE (t));
942 pp_newline_and_flush (pp);
946 /* Expressions. */
948 /* Print out a c-char. This is called solely for characters which are
949 in the *target* execution character set. We ought to convert them
950 back to the *host* execution character set before printing, but we
951 have no way to do this at present. A decent compromise is to print
952 all characters as if they were in the host execution character set,
953 and not attempt to recover any named escape characters, but render
954 all unprintables as octal escapes. If the host and target character
955 sets are the same, this produces relatively readable output. If they
956 are not the same, strings may appear as gibberish, but that's okay
957 (in fact, it may well be what the reader wants, e.g. if they are looking
958 to see if conversion to the target character set happened correctly).
960 A special case: we need to prefix \, ", and ' with backslashes. It is
961 correct to do so for the *host*'s \, ", and ', because the rest of the
962 file appears in the host character set. */
964 static void
965 pp_c_char (c_pretty_printer *pp, int c)
967 if (ISPRINT (c))
969 switch (c)
971 case '\\': pp_string (pp, "\\\\"); break;
972 case '\'': pp_string (pp, "\\\'"); break;
973 case '\"': pp_string (pp, "\\\""); break;
974 default: pp_character (pp, c);
977 else
978 pp_scalar (pp, "\\%03o", (unsigned) c);
981 /* Print out a STRING literal. */
983 void
984 pp_c_string_literal (c_pretty_printer *pp, tree s)
986 const char *p = TREE_STRING_POINTER (s);
987 int n = TREE_STRING_LENGTH (s) - 1;
988 int i;
989 pp_doublequote (pp);
990 for (i = 0; i < n; ++i)
991 pp_c_char (pp, p[i]);
992 pp_doublequote (pp);
995 /* Pretty-print a VOID_CST (void_node). */
997 static void
998 pp_c_void_constant (c_pretty_printer *pp)
1000 pp_c_type_cast (pp, void_type_node);
1001 pp_string (pp, "0");
1004 /* Pretty-print an INTEGER literal. */
1006 void
1007 pp_c_integer_constant (c_pretty_printer *pp, tree i)
1009 if (tree_fits_shwi_p (i))
1010 pp_wide_integer (pp, tree_to_shwi (i));
1011 else if (tree_fits_uhwi_p (i))
1012 pp_unsigned_wide_integer (pp, tree_to_uhwi (i));
1013 else
1015 wide_int wi = wi::to_wide (i);
1017 if (wi::lt_p (wi::to_wide (i), 0, TYPE_SIGN (TREE_TYPE (i))))
1019 pp_minus (pp);
1020 wi = -wi;
1022 print_hex (wi, pp_buffer (pp)->digit_buffer);
1023 pp_string (pp, pp_buffer (pp)->digit_buffer);
1027 /* Print out a CHARACTER literal. */
1029 static void
1030 pp_c_character_constant (c_pretty_printer *pp, tree c)
1032 pp_quote (pp);
1033 pp_c_char (pp, (unsigned) TREE_INT_CST_LOW (c));
1034 pp_quote (pp);
1037 /* Print out a BOOLEAN literal. */
1039 static void
1040 pp_c_bool_constant (c_pretty_printer *pp, tree b)
1042 if (b == boolean_false_node)
1044 if (c_dialect_cxx ())
1045 pp_c_ws_string (pp, "false");
1046 else if (flag_isoc99)
1047 pp_c_ws_string (pp, "_False");
1048 else
1049 pp_unsupported_tree (pp, b);
1051 else if (b == boolean_true_node)
1053 if (c_dialect_cxx ())
1054 pp_c_ws_string (pp, "true");
1055 else if (flag_isoc99)
1056 pp_c_ws_string (pp, "_True");
1057 else
1058 pp_unsupported_tree (pp, b);
1060 else if (TREE_CODE (b) == INTEGER_CST)
1061 pp_c_integer_constant (pp, b);
1062 else
1063 pp_unsupported_tree (pp, b);
1066 /* Given a value e of ENUMERAL_TYPE:
1067 Print out the first ENUMERATOR id with value e, if one is found,
1068 else print out the value as a C-style cast (type-id)value. */
1070 static void
1071 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
1073 tree type = TREE_TYPE (e);
1074 tree value = NULL_TREE;
1076 /* Find the name of this constant. */
1077 if ((pp->flags & pp_c_flag_gnu_v3) == 0)
1078 for (value = TYPE_VALUES (type); value != NULL_TREE;
1079 value = TREE_CHAIN (value))
1080 if (tree_int_cst_equal (DECL_INITIAL (TREE_VALUE (value)), e))
1081 break;
1083 if (value != NULL_TREE)
1084 pp->id_expression (TREE_PURPOSE (value));
1085 else
1087 /* Value must have been cast. */
1088 pp_c_type_cast (pp, type);
1089 pp_c_integer_constant (pp, e);
1093 /* Print out a REAL value as a decimal-floating-constant. */
1095 static void
1096 pp_c_floating_constant (c_pretty_printer *pp, tree r)
1098 const struct real_format *fmt
1099 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
1101 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
1102 bool is_decimal = floating_cst.decimal;
1104 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1105 log10(2) to 7 significant digits. */
1106 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
1108 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
1109 sizeof (pp_buffer (pp)->digit_buffer),
1110 max_digits10, 1);
1112 pp_string (pp, pp_buffer(pp)->digit_buffer);
1113 if (TREE_TYPE (r) == float_type_node)
1114 pp_character (pp, 'f');
1115 else if (TREE_TYPE (r) == long_double_type_node)
1116 pp_character (pp, 'l');
1117 else if (TREE_TYPE (r) == dfloat128_type_node)
1118 pp_string (pp, "dl");
1119 else if (TREE_TYPE (r) == dfloat64_type_node)
1120 pp_string (pp, "dd");
1121 else if (TREE_TYPE (r) == dfloat32_type_node)
1122 pp_string (pp, "df");
1123 else if (TREE_TYPE (r) != double_type_node)
1124 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
1125 if (TREE_TYPE (r) == FLOATN_NX_TYPE_NODE (i))
1127 pp_character (pp, 'f');
1128 pp_decimal_int (pp, floatn_nx_types[i].n);
1129 if (floatn_nx_types[i].extended)
1130 pp_character (pp, 'x');
1131 break;
1135 /* Print out a FIXED value as a decimal-floating-constant. */
1137 static void
1138 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1140 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
1141 sizeof (pp_buffer (pp)->digit_buffer));
1142 pp_string (pp, pp_buffer(pp)->digit_buffer);
1145 /* Pretty-print a compound literal expression. GNU extensions include
1146 vector constants. */
1148 static void
1149 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1151 tree type = TREE_TYPE (e);
1152 pp_c_type_cast (pp, type);
1154 switch (TREE_CODE (type))
1156 case RECORD_TYPE:
1157 case UNION_TYPE:
1158 case ARRAY_TYPE:
1159 case VECTOR_TYPE:
1160 case COMPLEX_TYPE:
1161 pp_c_brace_enclosed_initializer_list (pp, e);
1162 break;
1164 default:
1165 pp_unsupported_tree (pp, e);
1166 break;
1170 /* Pretty-print a COMPLEX_EXPR expression. */
1172 static void
1173 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1175 /* Handle a few common special cases, otherwise fallback
1176 to printing it as compound literal. */
1177 tree type = TREE_TYPE (e);
1178 tree realexpr = TREE_OPERAND (e, 0);
1179 tree imagexpr = TREE_OPERAND (e, 1);
1181 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1182 if (TREE_CODE (realexpr) == NOP_EXPR
1183 && TREE_CODE (imagexpr) == NOP_EXPR
1184 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1185 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1186 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1187 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1188 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1189 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1191 pp_c_type_cast (pp, type);
1192 pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1193 return;
1196 /* Cast of an scalar expression to COMPLEX_TYPE. */
1197 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1198 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1200 pp_c_type_cast (pp, type);
1201 if (TREE_CODE (realexpr) == NOP_EXPR)
1202 realexpr = TREE_OPERAND (realexpr, 0);
1203 pp->expression (realexpr);
1204 return;
1207 pp_c_compound_literal (pp, e);
1210 /* constant:
1211 integer-constant
1212 floating-constant
1213 fixed-point-constant
1214 enumeration-constant
1215 character-constant */
1217 void
1218 c_pretty_printer::constant (tree e)
1220 const enum tree_code code = TREE_CODE (e);
1222 switch (code)
1224 case VOID_CST:
1225 pp_c_void_constant (this);
1226 break;
1228 case INTEGER_CST:
1230 tree type = TREE_TYPE (e);
1231 if (type == boolean_type_node)
1232 pp_c_bool_constant (this, e);
1233 else if (type == char_type_node)
1234 pp_c_character_constant (this, e);
1235 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1236 pp_c_enumeration_constant (this, e);
1237 else if (NULLPTR_TYPE_P (type))
1238 pp_string (this, "nullptr");
1239 else
1240 pp_c_integer_constant (this, e);
1242 break;
1244 case REAL_CST:
1245 pp_c_floating_constant (this, e);
1246 break;
1248 case FIXED_CST:
1249 pp_c_fixed_constant (this, e);
1250 break;
1252 case STRING_CST:
1253 pp_c_string_literal (this, e);
1254 break;
1256 case COMPLEX_CST:
1257 /* Sometimes, we are confused and we think a complex literal
1258 is a constant. Such thing is a compound literal which
1259 grammatically belongs to postfix-expr production. */
1260 pp_c_compound_literal (this, e);
1261 break;
1263 default:
1264 pp_unsupported_tree (this, e);
1265 break;
1269 /* Pretty-print a string such as an identifier, without changing its
1270 encoding, preceded by whitespace is necessary. */
1272 void
1273 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1275 pp_c_maybe_whitespace (pp);
1276 pp_string (pp, str);
1277 pp->padding = pp_before;
1280 void
1281 c_pretty_printer::translate_string (const char *gmsgid)
1283 if (pp_translate_identifiers (this))
1284 pp_c_ws_string (this, _(gmsgid));
1285 else
1286 pp_c_ws_string (this, gmsgid);
1289 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1290 that need converting to the locale encoding, preceded by whitespace
1291 is necessary. */
1293 void
1294 pp_c_identifier (c_pretty_printer *pp, const char *id)
1296 pp_c_maybe_whitespace (pp);
1297 pp_identifier (pp, id);
1298 pp->padding = pp_before;
1301 /* Pretty-print a C primary-expression.
1302 primary-expression:
1303 identifier
1304 constant
1305 string-literal
1306 ( expression ) */
1308 void
1309 c_pretty_printer::primary_expression (tree e)
1311 switch (TREE_CODE (e))
1313 case VAR_DECL:
1314 case PARM_DECL:
1315 case FIELD_DECL:
1316 case CONST_DECL:
1317 case FUNCTION_DECL:
1318 case LABEL_DECL:
1319 pp_c_tree_decl_identifier (this, e);
1320 break;
1322 case IDENTIFIER_NODE:
1323 pp_c_tree_identifier (this, e);
1324 break;
1326 case ERROR_MARK:
1327 translate_string ("<erroneous-expression>");
1328 break;
1330 case RESULT_DECL:
1331 translate_string ("<return-value>");
1332 break;
1334 case VOID_CST:
1335 case INTEGER_CST:
1336 case REAL_CST:
1337 case FIXED_CST:
1338 case STRING_CST:
1339 constant (e);
1340 break;
1342 case TARGET_EXPR:
1343 pp_c_ws_string (this, "__builtin_memcpy");
1344 pp_c_left_paren (this);
1345 pp_ampersand (this);
1346 primary_expression (TREE_OPERAND (e, 0));
1347 pp_separate_with (this, ',');
1348 pp_ampersand (this);
1349 initializer (TREE_OPERAND (e, 1));
1350 if (TREE_OPERAND (e, 2))
1352 pp_separate_with (this, ',');
1353 expression (TREE_OPERAND (e, 2));
1355 pp_c_right_paren (this);
1356 break;
1358 case SSA_NAME:
1359 if (SSA_NAME_VAR (e))
1361 tree var = SSA_NAME_VAR (e);
1362 if (tree id = SSA_NAME_IDENTIFIER (e))
1364 const char *name = IDENTIFIER_POINTER (id);
1365 const char *dot;
1366 if (DECL_ARTIFICIAL (var) && (dot = strchr (name, '.')))
1368 /* Print the name without the . suffix (such as in VLAs).
1369 Use pp_c_identifier so that it can be converted into
1370 the appropriate encoding. */
1371 size_t size = dot - name;
1372 char *ident = XALLOCAVEC (char, size + 1);
1373 memcpy (ident, name, size);
1374 ident[size] = '\0';
1375 pp_c_identifier (this, ident);
1377 else
1378 primary_expression (var);
1380 else
1381 primary_expression (var);
1383 else
1385 /* Print only the right side of the GIMPLE assignment. */
1386 gimple *def_stmt = SSA_NAME_DEF_STMT (e);
1387 pp_gimple_stmt_1 (this, def_stmt, 0, TDF_RHS_ONLY);
1389 break;
1391 default:
1392 /* FIXME: Make sure we won't get into an infinite loop. */
1393 if (location_wrapper_p (e))
1394 expression (e);
1395 else
1397 pp_c_left_paren (this);
1398 expression (e);
1399 pp_c_right_paren (this);
1401 break;
1405 /* Print out a C initializer -- also support C compound-literals.
1406 initializer:
1407 assignment-expression:
1408 { initializer-list }
1409 { initializer-list , } */
1411 void
1412 c_pretty_printer::initializer (tree e)
1414 if (TREE_CODE (e) == CONSTRUCTOR)
1415 pp_c_brace_enclosed_initializer_list (this, e);
1416 else
1417 expression (e);
1420 /* init-declarator:
1421 declarator:
1422 declarator = initializer */
1424 void
1425 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1427 pp->declarator (t);
1428 /* We don't want to output function definitions here. There are handled
1429 elsewhere (and the syntactic form is bogus anyway). */
1430 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1432 tree init = DECL_INITIAL (t);
1433 /* This C++ bit is handled here because it is easier to do so.
1434 In templates, the C++ parser builds a TREE_LIST for a
1435 direct-initialization; the TREE_PURPOSE is the variable to
1436 initialize and the TREE_VALUE is the initializer. */
1437 if (TREE_CODE (init) == TREE_LIST)
1439 pp_c_left_paren (pp);
1440 pp->expression (TREE_VALUE (init));
1441 pp_right_paren (pp);
1443 else
1445 pp_space (pp);
1446 pp_equal (pp);
1447 pp_space (pp);
1448 pp->initializer (init);
1453 /* initializer-list:
1454 designation(opt) initializer
1455 initializer-list , designation(opt) initializer
1457 designation:
1458 designator-list =
1460 designator-list:
1461 designator
1462 designator-list designator
1464 designator:
1465 [ constant-expression ]
1466 identifier */
1468 static void
1469 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1471 tree type = TREE_TYPE (e);
1472 const enum tree_code code = TREE_CODE (type);
1474 if (TREE_CODE (e) == CONSTRUCTOR)
1476 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1477 return;
1480 switch (code)
1482 case RECORD_TYPE:
1483 case UNION_TYPE:
1484 case ARRAY_TYPE:
1486 tree init = TREE_OPERAND (e, 0);
1487 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1489 if (code == RECORD_TYPE || code == UNION_TYPE)
1491 pp_c_dot (pp);
1492 pp->primary_expression (TREE_PURPOSE (init));
1494 else
1496 pp_c_left_bracket (pp);
1497 if (TREE_PURPOSE (init))
1498 pp->constant (TREE_PURPOSE (init));
1499 pp_c_right_bracket (pp);
1501 pp_c_whitespace (pp);
1502 pp_equal (pp);
1503 pp_c_whitespace (pp);
1504 pp->initializer (TREE_VALUE (init));
1505 if (TREE_CHAIN (init))
1506 pp_separate_with (pp, ',');
1509 return;
1511 case VECTOR_TYPE:
1512 if (TREE_CODE (e) == VECTOR_CST)
1514 /* We don't create variable-length VECTOR_CSTs. */
1515 unsigned int nunits = VECTOR_CST_NELTS (e).to_constant ();
1516 for (unsigned int i = 0; i < nunits; ++i)
1518 if (i > 0)
1519 pp_separate_with (pp, ',');
1520 pp->expression (VECTOR_CST_ELT (e, i));
1523 else
1524 break;
1525 return;
1527 case COMPLEX_TYPE:
1528 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1530 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1531 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1532 pp_separate_with (pp, ',');
1533 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1535 else
1536 break;
1537 return;
1539 default:
1540 break;
1543 pp_unsupported_tree (pp, type);
1546 /* Pretty-print a brace-enclosed initializer-list. */
1548 static void
1549 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1551 pp_c_left_brace (pp);
1552 pp_c_initializer_list (pp, l);
1553 pp_c_right_brace (pp);
1557 /* This is a convenient function, used to bridge gap between C and C++
1558 grammars.
1560 id-expression:
1561 identifier */
1563 void
1564 c_pretty_printer::id_expression (tree t)
1566 switch (TREE_CODE (t))
1568 case VAR_DECL:
1569 case PARM_DECL:
1570 case CONST_DECL:
1571 case TYPE_DECL:
1572 case FUNCTION_DECL:
1573 case FIELD_DECL:
1574 case LABEL_DECL:
1575 pp_c_tree_decl_identifier (this, t);
1576 break;
1578 case IDENTIFIER_NODE:
1579 pp_c_tree_identifier (this, t);
1580 break;
1582 default:
1583 pp_unsupported_tree (this, t);
1584 break;
1588 /* postfix-expression:
1589 primary-expression
1590 postfix-expression [ expression ]
1591 postfix-expression ( argument-expression-list(opt) )
1592 postfix-expression . identifier
1593 postfix-expression -> identifier
1594 postfix-expression ++
1595 postfix-expression --
1596 ( type-name ) { initializer-list }
1597 ( type-name ) { initializer-list , } */
1599 void
1600 c_pretty_printer::postfix_expression (tree e)
1602 enum tree_code code = TREE_CODE (e);
1603 switch (code)
1605 case POSTINCREMENT_EXPR:
1606 case POSTDECREMENT_EXPR:
1607 postfix_expression (TREE_OPERAND (e, 0));
1608 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1609 break;
1611 case ARRAY_REF:
1612 postfix_expression (TREE_OPERAND (e, 0));
1613 pp_c_left_bracket (this);
1614 expression (TREE_OPERAND (e, 1));
1615 pp_c_right_bracket (this);
1616 break;
1618 case CALL_EXPR:
1620 call_expr_arg_iterator iter;
1621 tree arg;
1622 if (CALL_EXPR_FN (e) != NULL_TREE)
1623 postfix_expression (CALL_EXPR_FN (e));
1624 else
1625 pp_string (this, internal_fn_name (CALL_EXPR_IFN (e)));
1626 pp_c_left_paren (this);
1627 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1629 expression (arg);
1630 if (more_call_expr_args_p (&iter))
1631 pp_separate_with (this, ',');
1633 pp_c_right_paren (this);
1634 break;
1637 case UNORDERED_EXPR:
1638 pp_c_ws_string (this, flag_isoc99
1639 ? "isunordered"
1640 : "__builtin_isunordered");
1641 goto two_args_fun;
1643 case ORDERED_EXPR:
1644 pp_c_ws_string (this, flag_isoc99
1645 ? "!isunordered"
1646 : "!__builtin_isunordered");
1647 goto two_args_fun;
1649 case UNLT_EXPR:
1650 pp_c_ws_string (this, flag_isoc99
1651 ? "!isgreaterequal"
1652 : "!__builtin_isgreaterequal");
1653 goto two_args_fun;
1655 case UNLE_EXPR:
1656 pp_c_ws_string (this, flag_isoc99
1657 ? "!isgreater"
1658 : "!__builtin_isgreater");
1659 goto two_args_fun;
1661 case UNGT_EXPR:
1662 pp_c_ws_string (this, flag_isoc99
1663 ? "!islessequal"
1664 : "!__builtin_islessequal");
1665 goto two_args_fun;
1667 case UNGE_EXPR:
1668 pp_c_ws_string (this, flag_isoc99
1669 ? "!isless"
1670 : "!__builtin_isless");
1671 goto two_args_fun;
1673 case UNEQ_EXPR:
1674 pp_c_ws_string (this, flag_isoc99
1675 ? "!islessgreater"
1676 : "!__builtin_islessgreater");
1677 goto two_args_fun;
1679 case LTGT_EXPR:
1680 pp_c_ws_string (this, flag_isoc99
1681 ? "islessgreater"
1682 : "__builtin_islessgreater");
1683 goto two_args_fun;
1685 case MAX_EXPR:
1686 pp_c_ws_string (this, "max");
1687 goto two_args_fun;
1689 case MIN_EXPR:
1690 pp_c_ws_string (this, "min");
1691 goto two_args_fun;
1693 two_args_fun:
1694 pp_c_left_paren (this);
1695 expression (TREE_OPERAND (e, 0));
1696 pp_separate_with (this, ',');
1697 expression (TREE_OPERAND (e, 1));
1698 pp_c_right_paren (this);
1699 break;
1701 case ABS_EXPR:
1702 pp_c_ws_string (this, "__builtin_abs");
1703 pp_c_left_paren (this);
1704 expression (TREE_OPERAND (e, 0));
1705 pp_c_right_paren (this);
1706 break;
1708 case COMPONENT_REF:
1710 tree object = TREE_OPERAND (e, 0);
1711 if (INDIRECT_REF_P (object))
1713 postfix_expression (TREE_OPERAND (object, 0));
1714 pp_c_arrow (this);
1716 else
1718 postfix_expression (object);
1719 pp_c_dot (this);
1721 expression (TREE_OPERAND (e, 1));
1723 break;
1725 case BIT_FIELD_REF:
1727 tree type = TREE_TYPE (e);
1729 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1730 if (type
1731 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1733 HOST_WIDE_INT bitpos = tree_to_shwi (TREE_OPERAND (e, 2));
1734 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (type));
1735 if ((bitpos % size) == 0)
1737 pp_c_left_paren (this);
1738 pp_c_left_paren (this);
1739 type_id (type);
1740 pp_c_star (this);
1741 pp_c_right_paren (this);
1742 pp_c_ampersand (this);
1743 expression (TREE_OPERAND (e, 0));
1744 pp_c_right_paren (this);
1745 pp_c_left_bracket (this);
1746 pp_wide_integer (this, bitpos / size);
1747 pp_c_right_bracket (this);
1748 break;
1751 pp_unsupported_tree (this, e);
1753 break;
1755 case MEM_REF:
1756 case TARGET_MEM_REF:
1757 expression (e);
1758 break;
1760 case COMPLEX_CST:
1761 case VECTOR_CST:
1762 pp_c_compound_literal (this, e);
1763 break;
1765 case COMPLEX_EXPR:
1766 pp_c_complex_expr (this, e);
1767 break;
1769 case COMPOUND_LITERAL_EXPR:
1770 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1771 /* Fall through. */
1772 case CONSTRUCTOR:
1773 initializer (e);
1774 break;
1776 case VA_ARG_EXPR:
1777 pp_c_ws_string (this, "__builtin_va_arg");
1778 pp_c_left_paren (this);
1779 assignment_expression (TREE_OPERAND (e, 0));
1780 pp_separate_with (this, ',');
1781 type_id (TREE_TYPE (e));
1782 pp_c_right_paren (this);
1783 break;
1785 case ADDR_EXPR:
1786 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1788 id_expression (TREE_OPERAND (e, 0));
1789 break;
1791 /* fall through. */
1793 default:
1794 primary_expression (e);
1795 break;
1799 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1801 void
1802 pp_c_expression_list (c_pretty_printer *pp, tree e)
1804 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1806 pp->expression (TREE_VALUE (e));
1807 if (TREE_CHAIN (e))
1808 pp_separate_with (pp, ',');
1812 /* Print out V, which contains the elements of a constructor. */
1814 void
1815 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1817 unsigned HOST_WIDE_INT ix;
1818 tree value;
1820 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1822 pp->expression (value);
1823 if (ix != vec_safe_length (v) - 1)
1824 pp_separate_with (pp, ',');
1828 /* Print out an expression-list in parens, as if it were the argument
1829 list to a function. */
1831 void
1832 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1834 pp_c_left_paren (pp);
1835 if (t && TREE_CODE (t) == TREE_LIST)
1836 pp_c_expression_list (pp, t);
1837 pp_c_right_paren (pp);
1840 /* Try to fold *(type *)&op into op.fld.fld2[1] if possible.
1841 Only used for printing expressions. Should punt if ambiguous
1842 (e.g. in unions). */
1844 static tree
1845 c_fold_indirect_ref_for_warn (location_t loc, tree type, tree op,
1846 offset_int &off)
1848 tree optype = TREE_TYPE (op);
1849 if (off == 0)
1851 if (lang_hooks.types_compatible_p (optype, type))
1852 return op;
1853 /* *(foo *)&complexfoo => __real__ complexfoo */
1854 else if (TREE_CODE (optype) == COMPLEX_TYPE
1855 && lang_hooks.types_compatible_p (type, TREE_TYPE (optype)))
1856 return build1_loc (loc, REALPART_EXPR, type, op);
1858 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
1859 else if (TREE_CODE (optype) == COMPLEX_TYPE
1860 && lang_hooks.types_compatible_p (type, TREE_TYPE (optype))
1861 && tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
1863 off = 0;
1864 return build1_loc (loc, IMAGPART_EXPR, type, op);
1866 /* ((foo *)&fooarray)[x] => fooarray[x] */
1867 if (TREE_CODE (optype) == ARRAY_TYPE
1868 && TYPE_SIZE_UNIT (TREE_TYPE (optype))
1869 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (optype))) == INTEGER_CST
1870 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
1872 tree type_domain = TYPE_DOMAIN (optype);
1873 tree min_val = size_zero_node;
1874 if (type_domain && TYPE_MIN_VALUE (type_domain))
1875 min_val = TYPE_MIN_VALUE (type_domain);
1876 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
1877 offset_int idx = off / el_sz;
1878 offset_int rem = off % el_sz;
1879 if (TREE_CODE (min_val) == INTEGER_CST)
1881 tree index
1882 = wide_int_to_tree (sizetype, idx + wi::to_offset (min_val));
1883 op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
1884 NULL_TREE, NULL_TREE);
1885 off = rem;
1886 if (tree ret = c_fold_indirect_ref_for_warn (loc, type, op, off))
1887 return ret;
1888 return op;
1891 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
1892 else if (TREE_CODE (optype) == RECORD_TYPE)
1894 for (tree field = TYPE_FIELDS (optype);
1895 field; field = DECL_CHAIN (field))
1896 if (TREE_CODE (field) == FIELD_DECL
1897 && TREE_TYPE (field) != error_mark_node
1898 && TYPE_SIZE_UNIT (TREE_TYPE (field))
1899 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (field))) == INTEGER_CST)
1901 tree pos = byte_position (field);
1902 if (TREE_CODE (pos) != INTEGER_CST)
1903 continue;
1904 offset_int upos = wi::to_offset (pos);
1905 offset_int el_sz
1906 = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (field)));
1907 if (upos <= off && off < upos + el_sz)
1909 /* The C++ pretty printers print scope of the FIELD_DECLs,
1910 so punt if it is something that can't be printed. */
1911 if (c_dialect_cxx ())
1912 if (tree scope = get_containing_scope (field))
1913 if (TYPE_P (scope) && TYPE_NAME (scope) == NULL_TREE)
1914 break;
1915 tree cop = build3_loc (loc, COMPONENT_REF, TREE_TYPE (field),
1916 op, field, NULL_TREE);
1917 off = off - upos;
1918 if (tree ret = c_fold_indirect_ref_for_warn (loc, type, cop,
1919 off))
1920 return ret;
1921 return cop;
1925 /* Similarly for unions, but in this case try to be very conservative,
1926 only match if some field has type compatible with type and it is the
1927 only such field. */
1928 else if (TREE_CODE (optype) == UNION_TYPE)
1930 tree fld = NULL_TREE;
1931 for (tree field = TYPE_FIELDS (optype);
1932 field; field = DECL_CHAIN (field))
1933 if (TREE_CODE (field) == FIELD_DECL
1934 && TREE_TYPE (field) != error_mark_node
1935 && lang_hooks.types_compatible_p (TREE_TYPE (field), type))
1937 if (fld)
1938 return NULL_TREE;
1939 else
1940 fld = field;
1942 if (fld)
1944 off = 0;
1945 return build3_loc (loc, COMPONENT_REF, TREE_TYPE (fld), op, fld,
1946 NULL_TREE);
1950 return NULL_TREE;
1953 /* Print the MEM_REF expression REF, including its type and offset.
1954 Apply casts as necessary if the type of the access is different
1955 from the type of the accessed object. Produce compact output
1956 designed to include both the element index as well as any
1957 misalignment by preferring
1958 ((int*)((char*)p + 1))[2]
1959 over
1960 *(int*)((char*)p + 9)
1961 The former is more verbose but makes it clearer that the access
1962 to the third element of the array is misaligned by one byte. */
1964 static void
1965 print_mem_ref (c_pretty_printer *pp, tree e)
1967 tree arg = TREE_OPERAND (e, 0);
1969 /* The byte offset. Initially equal to the MEM_REF offset, then
1970 adjusted to the remainder of the division by the byte size of
1971 the access. */
1972 offset_int byte_off = wi::to_offset (TREE_OPERAND (e, 1));
1973 /* The result of dividing BYTE_OFF by the size of the access. */
1974 offset_int elt_idx = 0;
1975 /* True to include a cast to char* (for a nonzero final BYTE_OFF). */
1976 bool char_cast = false;
1977 tree op = NULL_TREE;
1978 bool array_ref_only = false;
1979 if (TREE_CODE (arg) == ADDR_EXPR)
1981 op = c_fold_indirect_ref_for_warn (EXPR_LOCATION (e), TREE_TYPE (e),
1982 TREE_OPERAND (arg, 0), byte_off);
1983 /* Try to fold it back to component, array ref or their combination,
1984 but print it only if the types and TBAA types are compatible. */
1985 if (op
1986 && byte_off == 0
1987 && lang_hooks.types_compatible_p (TREE_TYPE (e), TREE_TYPE (op))
1988 && (!flag_strict_aliasing
1989 || (get_deref_alias_set (TREE_OPERAND (e, 1))
1990 == get_alias_set (op))))
1992 pp->expression (op);
1993 return;
1995 if (op == NULL_TREE)
1996 op = TREE_OPERAND (arg, 0);
1997 /* If the types or TBAA types are incompatible, undo the
1998 UNION_TYPE handling from c_fold_indirect_ref_for_warn, and similarly
1999 undo __real__/__imag__ the code below doesn't try to handle. */
2000 if (op != TREE_OPERAND (arg, 0)
2001 && ((TREE_CODE (op) == COMPONENT_REF
2002 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == UNION_TYPE)
2003 || TREE_CODE (op) == REALPART_EXPR
2004 || TREE_CODE (op) == IMAGPART_EXPR))
2005 op = TREE_OPERAND (op, 0);
2006 if (op != TREE_OPERAND (arg, 0))
2008 array_ref_only = true;
2009 for (tree ref = op; ref != TREE_OPERAND (arg, 0);
2010 ref = TREE_OPERAND (ref, 0))
2011 if (TREE_CODE (ref) != ARRAY_REF)
2013 array_ref_only = false;
2014 break;
2019 tree access_type = TREE_TYPE (e);
2020 tree arg_type = TREE_TYPE (TREE_TYPE (arg));
2021 if (tree access_size = TYPE_SIZE_UNIT (access_type))
2022 if (byte_off != 0
2023 && TREE_CODE (access_size) == INTEGER_CST
2024 && !integer_zerop (access_size))
2026 offset_int asize = wi::to_offset (access_size);
2027 elt_idx = byte_off / asize;
2028 byte_off = byte_off % asize;
2031 /* True to include a cast to the accessed type. */
2032 const bool access_cast
2033 = ((op && op != TREE_OPERAND (arg, 0))
2034 || VOID_TYPE_P (arg_type)
2035 || !lang_hooks.types_compatible_p (access_type, arg_type));
2036 const bool has_off = byte_off != 0 || (op && op != TREE_OPERAND (arg, 0));
2038 if (has_off && (byte_off != 0 || !array_ref_only))
2040 /* When printing the byte offset for a pointer to a type of
2041 a different size than char, include a cast to char* first,
2042 before printing the cast to a pointer to the accessed type. */
2043 tree size = TYPE_SIZE (arg_type);
2044 if (size == NULL_TREE
2045 || TREE_CODE (size) != INTEGER_CST
2046 || wi::to_wide (size) != BITS_PER_UNIT)
2047 char_cast = true;
2050 if (elt_idx == 0)
2051 pp_c_star (pp);
2052 else if (access_cast || char_cast)
2053 pp_c_left_paren (pp);
2055 if (access_cast)
2057 /* Include a cast to the accessed type if it isn't compatible
2058 with the type of the referenced object (or if the object
2059 is typeless). */
2060 pp_c_left_paren (pp);
2061 pp->type_id (build_pointer_type (access_type));
2062 pp_c_right_paren (pp);
2065 if (has_off)
2066 pp_c_left_paren (pp);
2068 if (char_cast)
2070 /* Include a cast to char *. */
2071 pp_c_left_paren (pp);
2072 pp->type_id (string_type_node);
2073 pp_c_right_paren (pp);
2076 pp->unary_expression (arg);
2078 if (op && op != TREE_OPERAND (arg, 0))
2080 auto_vec<tree, 16> refs;
2081 tree ref;
2082 unsigned i;
2083 bool array_refs = true;
2084 for (ref = op; ref != TREE_OPERAND (arg, 0); ref = TREE_OPERAND (ref, 0))
2085 refs.safe_push (ref);
2086 FOR_EACH_VEC_ELT_REVERSE (refs, i, ref)
2087 if (array_refs && TREE_CODE (ref) == ARRAY_REF)
2089 pp_c_left_bracket (pp);
2090 pp->expression (TREE_OPERAND (ref, 1));
2091 pp_c_right_bracket (pp);
2093 else
2095 if (array_refs)
2097 array_refs = false;
2098 pp_string (pp, " + offsetof");
2099 pp_c_left_paren (pp);
2100 pp->type_id (TREE_TYPE (TREE_OPERAND (ref, 0)));
2101 pp_comma (pp);
2103 else if (TREE_CODE (ref) == COMPONENT_REF)
2104 pp_c_dot (pp);
2105 if (TREE_CODE (ref) == COMPONENT_REF)
2106 pp->expression (TREE_OPERAND (ref, 1));
2107 else
2109 pp_c_left_bracket (pp);
2110 pp->expression (TREE_OPERAND (ref, 1));
2111 pp_c_right_bracket (pp);
2114 if (!array_refs)
2115 pp_c_right_paren (pp);
2118 if (byte_off != 0)
2120 pp_space (pp);
2121 pp_plus (pp);
2122 pp_space (pp);
2123 tree off = wide_int_to_tree (ssizetype, byte_off);
2124 pp->constant (off);
2127 if (has_off)
2128 pp_c_right_paren (pp);
2130 if (elt_idx != 0)
2132 if (access_cast || char_cast)
2133 pp_c_right_paren (pp);
2135 pp_c_left_bracket (pp);
2136 tree idx = wide_int_to_tree (ssizetype, elt_idx);
2137 pp->constant (idx);
2138 pp_c_right_bracket (pp);
2142 /* unary-expression:
2143 postfix-expression
2144 ++ cast-expression
2145 -- cast-expression
2146 unary-operator cast-expression
2147 sizeof unary-expression
2148 sizeof ( type-id )
2150 unary-operator: one of
2151 * & + - ! ~
2153 GNU extensions.
2154 unary-expression:
2155 __alignof__ unary-expression
2156 __alignof__ ( type-id )
2157 __real__ unary-expression
2158 __imag__ unary-expression */
2160 void
2161 c_pretty_printer::unary_expression (tree e)
2163 enum tree_code code = TREE_CODE (e);
2164 switch (code)
2166 case PREINCREMENT_EXPR:
2167 case PREDECREMENT_EXPR:
2168 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
2169 unary_expression (TREE_OPERAND (e, 0));
2170 break;
2172 case ADDR_EXPR:
2173 case INDIRECT_REF:
2174 case NEGATE_EXPR:
2175 case BIT_NOT_EXPR:
2176 case TRUTH_NOT_EXPR:
2177 case CONJ_EXPR:
2178 /* String literal are used by address. */
2179 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
2180 pp_ampersand (this);
2181 else if (code == INDIRECT_REF)
2183 tree type = TREE_TYPE (TREE_OPERAND (e, 0));
2184 if (type && TREE_CODE (type) == REFERENCE_TYPE)
2185 /* Reference decay is implicit, don't print anything. */;
2186 else
2187 pp_c_star (this);
2189 else if (code == NEGATE_EXPR)
2190 pp_minus (this);
2191 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
2192 pp_complement (this);
2193 else if (code == TRUTH_NOT_EXPR)
2194 pp_exclamation (this);
2195 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
2196 break;
2198 case MEM_REF:
2199 print_mem_ref (this, e);
2200 break;
2202 case TARGET_MEM_REF:
2203 /* TARGET_MEM_REF can't appear directly from source, but can appear
2204 during late GIMPLE optimizations and through late diagnostic we might
2205 need to support it. Print it as dereferencing of a pointer after
2206 cast to the TARGET_MEM_REF type, with pointer arithmetics on some
2207 pointer to single byte types, so
2208 *(type *)((char *) ptr + step * index + index2) if all the operands
2209 are present and the casts are needed. */
2210 pp_c_star (this);
2211 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TMR_BASE (e)))) == NULL_TREE
2212 || !integer_onep (TYPE_SIZE_UNIT
2213 (TREE_TYPE (TREE_TYPE (TMR_BASE (e))))))
2215 if (TYPE_SIZE_UNIT (TREE_TYPE (e))
2216 && integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (e))))
2218 pp_c_left_paren (this);
2219 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e)));
2221 else
2223 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e)));
2224 pp_c_left_paren (this);
2225 pp_c_type_cast (this, build_pointer_type (char_type_node));
2228 else if (!lang_hooks.types_compatible_p
2229 (TREE_TYPE (e), TREE_TYPE (TREE_TYPE (TMR_BASE (e)))))
2231 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e)));
2232 pp_c_left_paren (this);
2234 else
2235 pp_c_left_paren (this);
2236 pp_c_cast_expression (this, TMR_BASE (e));
2237 if (TMR_STEP (e) && TMR_INDEX (e))
2239 pp_plus (this);
2240 pp_c_cast_expression (this, TMR_INDEX (e));
2241 pp_c_star (this);
2242 pp_c_cast_expression (this, TMR_STEP (e));
2244 if (TMR_INDEX2 (e))
2246 pp_plus (this);
2247 pp_c_cast_expression (this, TMR_INDEX2 (e));
2249 if (!integer_zerop (TMR_OFFSET (e)))
2251 pp_plus (this);
2252 pp_c_integer_constant (this,
2253 fold_convert (ssizetype, TMR_OFFSET (e)));
2255 pp_c_right_paren (this);
2256 break;
2258 case REALPART_EXPR:
2259 case IMAGPART_EXPR:
2260 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
2261 pp_c_whitespace (this);
2262 unary_expression (TREE_OPERAND (e, 0));
2263 break;
2265 default:
2266 postfix_expression (e);
2267 break;
2271 /* cast-expression:
2272 unary-expression
2273 ( type-name ) cast-expression */
2275 void
2276 pp_c_cast_expression (c_pretty_printer *pp, tree e)
2278 switch (TREE_CODE (e))
2280 case FLOAT_EXPR:
2281 case FIX_TRUNC_EXPR:
2282 CASE_CONVERT:
2283 case VIEW_CONVERT_EXPR:
2284 if (!location_wrapper_p (e))
2285 pp_c_type_cast (pp, TREE_TYPE (e));
2286 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
2287 break;
2289 default:
2290 pp->unary_expression (e);
2294 /* multiplicative-expression:
2295 cast-expression
2296 multiplicative-expression * cast-expression
2297 multiplicative-expression / cast-expression
2298 multiplicative-expression % cast-expression */
2300 void
2301 c_pretty_printer::multiplicative_expression (tree e)
2303 enum tree_code code = TREE_CODE (e);
2304 switch (code)
2306 case MULT_EXPR:
2307 case TRUNC_DIV_EXPR:
2308 case TRUNC_MOD_EXPR:
2309 case EXACT_DIV_EXPR:
2310 case RDIV_EXPR:
2311 multiplicative_expression (TREE_OPERAND (e, 0));
2312 pp_c_whitespace (this);
2313 if (code == MULT_EXPR)
2314 pp_c_star (this);
2315 else if (code != TRUNC_MOD_EXPR)
2316 pp_slash (this);
2317 else
2318 pp_modulo (this);
2319 pp_c_whitespace (this);
2320 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
2321 break;
2323 default:
2324 pp_c_cast_expression (this, e);
2325 break;
2329 /* additive-expression:
2330 multiplicative-expression
2331 additive-expression + multiplicative-expression
2332 additive-expression - multiplicative-expression */
2334 static void
2335 pp_c_additive_expression (c_pretty_printer *pp, tree e)
2337 enum tree_code code = TREE_CODE (e);
2338 switch (code)
2340 case POINTER_PLUS_EXPR:
2341 case PLUS_EXPR:
2342 case POINTER_DIFF_EXPR:
2343 case MINUS_EXPR:
2344 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
2345 pp_c_whitespace (pp);
2346 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
2347 pp_plus (pp);
2348 else
2349 pp_minus (pp);
2350 pp_c_whitespace (pp);
2352 tree op1 = TREE_OPERAND (e, 1);
2353 if (code == POINTER_PLUS_EXPR
2354 && TREE_CODE (op1) == INTEGER_CST
2355 && tree_int_cst_sign_bit (op1))
2356 /* A pointer minus an integer is represented internally as plus a very
2357 large number, don't expose that to users. */
2358 op1 = convert (ssizetype, op1);
2359 pp->multiplicative_expression (op1);
2361 break;
2363 default:
2364 pp->multiplicative_expression (e);
2365 break;
2369 /* additive-expression:
2370 additive-expression
2371 shift-expression << additive-expression
2372 shift-expression >> additive-expression */
2374 static void
2375 pp_c_shift_expression (c_pretty_printer *pp, tree e)
2377 enum tree_code code = TREE_CODE (e);
2378 switch (code)
2380 case LSHIFT_EXPR:
2381 case RSHIFT_EXPR:
2382 case LROTATE_EXPR:
2383 case RROTATE_EXPR:
2384 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
2385 pp_c_whitespace (pp);
2386 pp_string (pp, code == LSHIFT_EXPR ? "<<" :
2387 code == RSHIFT_EXPR ? ">>" :
2388 code == LROTATE_EXPR ? "<<<" : ">>>");
2389 pp_c_whitespace (pp);
2390 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
2391 break;
2393 default:
2394 pp_c_additive_expression (pp, e);
2398 /* relational-expression:
2399 shift-expression
2400 relational-expression < shift-expression
2401 relational-expression > shift-expression
2402 relational-expression <= shift-expression
2403 relational-expression >= shift-expression */
2405 static void
2406 pp_c_relational_expression (c_pretty_printer *pp, tree e)
2408 enum tree_code code = TREE_CODE (e);
2409 switch (code)
2411 case LT_EXPR:
2412 case GT_EXPR:
2413 case LE_EXPR:
2414 case GE_EXPR:
2415 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
2416 pp_c_whitespace (pp);
2417 if (code == LT_EXPR)
2418 pp_less (pp);
2419 else if (code == GT_EXPR)
2420 pp_greater (pp);
2421 else if (code == LE_EXPR)
2422 pp_less_equal (pp);
2423 else if (code == GE_EXPR)
2424 pp_greater_equal (pp);
2425 pp_c_whitespace (pp);
2426 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
2427 break;
2429 default:
2430 pp_c_shift_expression (pp, e);
2431 break;
2435 /* equality-expression:
2436 relational-expression
2437 equality-expression == relational-expression
2438 equality-equality != relational-expression */
2440 static void
2441 pp_c_equality_expression (c_pretty_printer *pp, tree e)
2443 enum tree_code code = TREE_CODE (e);
2444 switch (code)
2446 case EQ_EXPR:
2447 case NE_EXPR:
2448 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
2449 pp_c_whitespace (pp);
2450 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
2451 pp_c_whitespace (pp);
2452 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
2453 break;
2455 default:
2456 pp_c_relational_expression (pp, e);
2457 break;
2461 /* AND-expression:
2462 equality-expression
2463 AND-expression & equality-equality */
2465 static void
2466 pp_c_and_expression (c_pretty_printer *pp, tree e)
2468 if (TREE_CODE (e) == BIT_AND_EXPR)
2470 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
2471 pp_c_whitespace (pp);
2472 pp_ampersand (pp);
2473 pp_c_whitespace (pp);
2474 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
2476 else
2477 pp_c_equality_expression (pp, e);
2480 /* exclusive-OR-expression:
2481 AND-expression
2482 exclusive-OR-expression ^ AND-expression */
2484 static void
2485 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
2487 if (TREE_CODE (e) == BIT_XOR_EXPR
2488 || TREE_CODE (e) == TRUTH_XOR_EXPR)
2490 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2491 if (TREE_CODE (e) == BIT_XOR_EXPR)
2492 pp_c_maybe_whitespace (pp);
2493 else
2494 pp_c_whitespace (pp);
2495 pp_carret (pp);
2496 pp_c_whitespace (pp);
2497 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2499 else
2500 pp_c_and_expression (pp, e);
2503 /* inclusive-OR-expression:
2504 exclusive-OR-expression
2505 inclusive-OR-expression | exclusive-OR-expression */
2507 static void
2508 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2510 if (TREE_CODE (e) == BIT_IOR_EXPR)
2512 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2513 pp_c_whitespace (pp);
2514 pp_bar (pp);
2515 pp_c_whitespace (pp);
2516 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2518 else
2519 pp_c_exclusive_or_expression (pp, e);
2522 /* logical-AND-expression:
2523 inclusive-OR-expression
2524 logical-AND-expression && inclusive-OR-expression */
2526 static void
2527 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2529 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2530 || TREE_CODE (e) == TRUTH_AND_EXPR)
2532 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2533 pp_c_whitespace (pp);
2534 pp_ampersand_ampersand (pp);
2535 pp_c_whitespace (pp);
2536 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2538 else
2539 pp_c_inclusive_or_expression (pp, e);
2542 /* logical-OR-expression:
2543 logical-AND-expression
2544 logical-OR-expression || logical-AND-expression */
2546 void
2547 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2549 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2550 || TREE_CODE (e) == TRUTH_OR_EXPR)
2552 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2553 pp_c_whitespace (pp);
2554 pp_bar_bar (pp);
2555 pp_c_whitespace (pp);
2556 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2558 else
2559 pp_c_logical_and_expression (pp, e);
2562 /* conditional-expression:
2563 logical-OR-expression
2564 logical-OR-expression ? expression : conditional-expression */
2566 void
2567 c_pretty_printer::conditional_expression (tree e)
2569 if (TREE_CODE (e) == COND_EXPR)
2571 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2572 pp_c_whitespace (this);
2573 pp_question (this);
2574 pp_c_whitespace (this);
2575 expression (TREE_OPERAND (e, 1));
2576 pp_c_whitespace (this);
2577 pp_colon (this);
2578 pp_c_whitespace (this);
2579 conditional_expression (TREE_OPERAND (e, 2));
2581 else
2582 pp_c_logical_or_expression (this, e);
2586 /* assignment-expression:
2587 conditional-expression
2588 unary-expression assignment-operator assignment-expression
2590 assignment-expression: one of
2591 = *= /= %= += -= >>= <<= &= ^= |= */
2593 void
2594 c_pretty_printer::assignment_expression (tree e)
2596 if (TREE_CODE (e) == MODIFY_EXPR
2597 || TREE_CODE (e) == INIT_EXPR)
2599 unary_expression (TREE_OPERAND (e, 0));
2600 pp_c_whitespace (this);
2601 pp_equal (this);
2602 pp_space (this);
2603 expression (TREE_OPERAND (e, 1));
2605 else
2606 conditional_expression (e);
2609 /* expression:
2610 assignment-expression
2611 expression , assignment-expression
2613 Implementation note: instead of going through the usual recursion
2614 chain, I take the liberty of dispatching nodes to the appropriate
2615 functions. This makes some redundancy, but it worths it. That also
2616 prevents a possible infinite recursion between primary_expression ()
2617 and expression (). */
2619 void
2620 c_pretty_printer::expression (tree e)
2622 switch (TREE_CODE (e))
2624 case VOID_CST:
2625 pp_c_void_constant (this);
2626 break;
2628 case INTEGER_CST:
2629 pp_c_integer_constant (this, e);
2630 break;
2632 case REAL_CST:
2633 pp_c_floating_constant (this, e);
2634 break;
2636 case FIXED_CST:
2637 pp_c_fixed_constant (this, e);
2638 break;
2640 case STRING_CST:
2641 pp_c_string_literal (this, e);
2642 break;
2644 case IDENTIFIER_NODE:
2645 case FUNCTION_DECL:
2646 case VAR_DECL:
2647 case CONST_DECL:
2648 case PARM_DECL:
2649 case RESULT_DECL:
2650 case FIELD_DECL:
2651 case LABEL_DECL:
2652 case ERROR_MARK:
2653 primary_expression (e);
2654 break;
2656 case SSA_NAME:
2657 if (SSA_NAME_VAR (e)
2658 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2659 expression (SSA_NAME_VAR (e));
2660 else
2661 translate_string ("<unknown>");
2662 break;
2664 case POSTINCREMENT_EXPR:
2665 case POSTDECREMENT_EXPR:
2666 case ARRAY_REF:
2667 case CALL_EXPR:
2668 case COMPONENT_REF:
2669 case BIT_FIELD_REF:
2670 case COMPLEX_CST:
2671 case COMPLEX_EXPR:
2672 case VECTOR_CST:
2673 case ORDERED_EXPR:
2674 case UNORDERED_EXPR:
2675 case LTGT_EXPR:
2676 case UNEQ_EXPR:
2677 case UNLE_EXPR:
2678 case UNLT_EXPR:
2679 case UNGE_EXPR:
2680 case UNGT_EXPR:
2681 case MAX_EXPR:
2682 case MIN_EXPR:
2683 case ABS_EXPR:
2684 case CONSTRUCTOR:
2685 case COMPOUND_LITERAL_EXPR:
2686 case VA_ARG_EXPR:
2687 postfix_expression (e);
2688 break;
2690 case CONJ_EXPR:
2691 case ADDR_EXPR:
2692 case INDIRECT_REF:
2693 case MEM_REF:
2694 case TARGET_MEM_REF:
2695 case NEGATE_EXPR:
2696 case BIT_NOT_EXPR:
2697 case TRUTH_NOT_EXPR:
2698 case PREINCREMENT_EXPR:
2699 case PREDECREMENT_EXPR:
2700 case REALPART_EXPR:
2701 case IMAGPART_EXPR:
2702 unary_expression (e);
2703 break;
2705 case FLOAT_EXPR:
2706 case FIX_TRUNC_EXPR:
2707 CASE_CONVERT:
2708 case VIEW_CONVERT_EXPR:
2709 pp_c_cast_expression (this, e);
2710 break;
2712 case MULT_EXPR:
2713 case TRUNC_MOD_EXPR:
2714 case TRUNC_DIV_EXPR:
2715 case EXACT_DIV_EXPR:
2716 case RDIV_EXPR:
2717 multiplicative_expression (e);
2718 break;
2720 case LSHIFT_EXPR:
2721 case RSHIFT_EXPR:
2722 case LROTATE_EXPR:
2723 case RROTATE_EXPR:
2724 pp_c_shift_expression (this, e);
2725 break;
2727 case LT_EXPR:
2728 case GT_EXPR:
2729 case LE_EXPR:
2730 case GE_EXPR:
2731 pp_c_relational_expression (this, e);
2732 break;
2734 case BIT_AND_EXPR:
2735 pp_c_and_expression (this, e);
2736 break;
2738 case BIT_XOR_EXPR:
2739 case TRUTH_XOR_EXPR:
2740 pp_c_exclusive_or_expression (this, e);
2741 break;
2743 case BIT_IOR_EXPR:
2744 pp_c_inclusive_or_expression (this, e);
2745 break;
2747 case TRUTH_ANDIF_EXPR:
2748 case TRUTH_AND_EXPR:
2749 pp_c_logical_and_expression (this, e);
2750 break;
2752 case TRUTH_ORIF_EXPR:
2753 case TRUTH_OR_EXPR:
2754 pp_c_logical_or_expression (this, e);
2755 break;
2757 case EQ_EXPR:
2758 case NE_EXPR:
2759 pp_c_equality_expression (this, e);
2760 break;
2762 case COND_EXPR:
2763 conditional_expression (e);
2764 break;
2766 case POINTER_PLUS_EXPR:
2767 case PLUS_EXPR:
2768 case POINTER_DIFF_EXPR:
2769 case MINUS_EXPR:
2770 pp_c_additive_expression (this, e);
2771 break;
2773 case MODIFY_EXPR:
2774 case INIT_EXPR:
2775 assignment_expression (e);
2776 break;
2778 case COMPOUND_EXPR:
2779 pp_c_left_paren (this);
2780 expression (TREE_OPERAND (e, 0));
2781 pp_separate_with (this, ',');
2782 assignment_expression (TREE_OPERAND (e, 1));
2783 pp_c_right_paren (this);
2784 break;
2786 case NON_LVALUE_EXPR:
2787 case SAVE_EXPR:
2788 expression (TREE_OPERAND (e, 0));
2789 break;
2791 case TARGET_EXPR:
2792 postfix_expression (TREE_OPERAND (e, 1));
2793 break;
2795 case BIND_EXPR:
2796 case GOTO_EXPR:
2797 /* We don't yet have a way of dumping statements in a
2798 human-readable format. */
2799 pp_string (this, "({...})");
2800 break;
2802 case C_MAYBE_CONST_EXPR:
2803 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2804 break;
2806 default:
2807 pp_unsupported_tree (this, e);
2808 break;
2814 /* Statements. */
2816 void
2817 c_pretty_printer::statement (tree t)
2819 if (t == NULL)
2820 return;
2822 switch (TREE_CODE (t))
2825 case SWITCH_STMT:
2826 pp_c_ws_string (this, "switch");
2827 pp_space (this);
2828 pp_c_left_paren (this);
2829 expression (SWITCH_STMT_COND (t));
2830 pp_c_right_paren (this);
2831 pp_indentation (this) += 3;
2832 pp_needs_newline (this) = true;
2833 statement (SWITCH_STMT_BODY (t));
2834 pp_newline_and_indent (this, -3);
2835 break;
2837 /* iteration-statement:
2838 while ( expression ) statement
2839 do statement while ( expression ) ;
2840 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
2841 for ( declaration expression(opt) ; expression(opt) ) statement */
2842 case WHILE_STMT:
2843 pp_c_ws_string (this, "while");
2844 pp_space (this);
2845 pp_c_left_paren (this);
2846 expression (WHILE_COND (t));
2847 pp_c_right_paren (this);
2848 pp_newline_and_indent (this, 3);
2849 statement (WHILE_BODY (t));
2850 pp_indentation (this) -= 3;
2851 pp_needs_newline (this) = true;
2852 break;
2854 case DO_STMT:
2855 pp_c_ws_string (this, "do");
2856 pp_newline_and_indent (this, 3);
2857 statement (DO_BODY (t));
2858 pp_newline_and_indent (this, -3);
2859 pp_c_ws_string (this, "while");
2860 pp_space (this);
2861 pp_c_left_paren (this);
2862 expression (DO_COND (t));
2863 pp_c_right_paren (this);
2864 pp_c_semicolon (this);
2865 pp_needs_newline (this) = true;
2866 break;
2868 case FOR_STMT:
2869 pp_c_ws_string (this, "for");
2870 pp_space (this);
2871 pp_c_left_paren (this);
2872 if (FOR_INIT_STMT (t))
2873 statement (FOR_INIT_STMT (t));
2874 else
2875 pp_c_semicolon (this);
2876 pp_needs_newline (this) = false;
2877 pp_c_whitespace (this);
2878 if (FOR_COND (t))
2879 expression (FOR_COND (t));
2880 pp_c_semicolon (this);
2881 pp_needs_newline (this) = false;
2882 pp_c_whitespace (this);
2883 if (FOR_EXPR (t))
2884 expression (FOR_EXPR (t));
2885 pp_c_right_paren (this);
2886 pp_newline_and_indent (this, 3);
2887 statement (FOR_BODY (t));
2888 pp_indentation (this) -= 3;
2889 pp_needs_newline (this) = true;
2890 break;
2892 /* jump-statement:
2893 goto identifier;
2894 continue ;
2895 return expression(opt) ; */
2896 case BREAK_STMT:
2897 case CONTINUE_STMT:
2898 pp_string (this, TREE_CODE (t) == BREAK_STMT ? "break" : "continue");
2899 pp_c_semicolon (this);
2900 pp_needs_newline (this) = true;
2901 break;
2903 default:
2904 if (pp_needs_newline (this))
2905 pp_newline_and_indent (this, 0);
2906 dump_generic_node (this, t, pp_indentation (this), TDF_NONE, true);
2911 /* Initialize the PRETTY-PRINTER for handling C codes. */
2913 c_pretty_printer::c_pretty_printer ()
2914 : pretty_printer (),
2915 offset_list (),
2916 flags ()
2918 type_specifier_seq = pp_c_specifier_qualifier_list;
2919 ptr_operator = pp_c_pointer;
2920 parameter_list = pp_c_parameter_type_list;
2923 /* c_pretty_printer's implementation of pretty_printer::clone vfunc. */
2925 pretty_printer *
2926 c_pretty_printer::clone () const
2928 return new c_pretty_printer (*this);
2931 /* Print the tree T in full, on file FILE. */
2933 void
2934 print_c_tree (FILE *file, tree t)
2936 c_pretty_printer pp;
2938 pp_needs_newline (&pp) = true;
2939 pp.buffer->stream = file;
2940 pp.statement (t);
2941 pp_newline_and_flush (&pp);
2944 /* Print the tree T in full, on stderr. */
2946 DEBUG_FUNCTION void
2947 debug_c_tree (tree t)
2949 print_c_tree (stderr, t);
2950 fputc ('\n', stderr);
2953 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2954 up of T's memory address. */
2956 void
2957 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2959 const char *name;
2961 gcc_assert (DECL_P (t));
2963 if (DECL_NAME (t))
2964 name = IDENTIFIER_POINTER (DECL_NAME (t));
2965 else
2967 static char xname[8];
2968 sprintf (xname, "<U%4hx>", ((unsigned short) ((uintptr_t) (t)
2969 & 0xffff)));
2970 name = xname;
2973 pp_c_identifier (pp, name);
2976 #if CHECKING_P
2978 namespace selftest {
2980 /* Selftests for pretty-printing trees. */
2982 /* Verify that EXPR printed by c_pretty_printer is EXPECTED, using
2983 LOC as the effective location for any failures. */
2985 static void
2986 assert_c_pretty_printer_output (const location &loc, const char *expected,
2987 tree expr)
2989 c_pretty_printer pp;
2990 pp.expression (expr);
2991 ASSERT_STREQ_AT (loc, expected, pp_formatted_text (&pp));
2994 /* Helper function for calling assert_c_pretty_printer_output.
2995 This is to avoid having to write SELFTEST_LOCATION. */
2997 #define ASSERT_C_PRETTY_PRINTER_OUTPUT(EXPECTED, EXPR) \
2998 SELFTEST_BEGIN_STMT \
2999 assert_c_pretty_printer_output ((SELFTEST_LOCATION), \
3000 (EXPECTED), \
3001 (EXPR)); \
3002 SELFTEST_END_STMT
3004 /* Verify that location wrappers don't show up in pretty-printed output. */
3006 static void
3007 test_location_wrappers ()
3009 /* VAR_DECL. */
3010 tree id = get_identifier ("foo");
3011 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, id,
3012 integer_type_node);
3013 tree wrapped_decl = maybe_wrap_with_location (decl, BUILTINS_LOCATION);
3014 ASSERT_NE (wrapped_decl, decl);
3015 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", decl);
3016 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", wrapped_decl);
3018 /* INTEGER_CST. */
3019 tree int_cst = build_int_cst (integer_type_node, 42);
3020 tree wrapped_cst = maybe_wrap_with_location (int_cst, BUILTINS_LOCATION);
3021 ASSERT_NE (wrapped_cst, int_cst);
3022 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", int_cst);
3023 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", wrapped_cst);
3026 /* Run all of the selftests within this file. */
3028 void
3029 c_pretty_print_cc_tests ()
3031 test_location_wrappers ();
3034 } // namespace selftest
3036 #endif /* CHECKING_P */