Debian Codegraph @

gcc/c-family/c-pretty-print.cc

raw
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2025 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <[email protected]>
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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"
36 #include "function.h"
37 #include "basic-block.h"
38 #include "gimple.h"
39 #include "make-unique.h"
40
41 /* The pretty-printer code is primarily designed to closely follow
42 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
43 codes we used to have in the past. Following a structured
44 approach (preferably the official grammars) is believed to make it
45 much easier to add extensions and nifty pretty-printing effects that
46 takes expression or declaration contexts into account. */
47
48
49 #define pp_c_maybe_whitespace(PP) \
50 do { \
51 if ((PP)->get_padding () == pp_before) \
52 pp_c_whitespace (PP); \
53 } while (0)
54
55 /* literal */
56 static void pp_c_char (c_pretty_printer *, int);
57
58 /* postfix-expression */
59 static void pp_c_initializer_list (c_pretty_printer *, tree);
60 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
61
62 static void pp_c_additive_expression (c_pretty_printer *, tree);
63 static void pp_c_shift_expression (c_pretty_printer *, tree);
64 static void pp_c_relational_expression (c_pretty_printer *, tree);
65 static void pp_c_equality_expression (c_pretty_printer *, tree);
66 static void pp_c_and_expression (c_pretty_printer *, tree);
67 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
68 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
69 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
70
71 /* declarations. */
72
73
74 /* Helper functions. */
75
76 void
77 pp_c_whitespace (c_pretty_printer *pp)
78 {
79 pp_space (pp);
80 pp->set_padding (pp_none);
81 }
82
83 void
84 pp_c_left_paren (c_pretty_printer *pp)
85 {
86 pp_left_paren (pp);
87 pp->set_padding (pp_none);
88 }
89
90 void
91 pp_c_right_paren (c_pretty_printer *pp)
92 {
93 pp_right_paren (pp);
94 pp->set_padding (pp_none);
95 }
96
97 void
98 pp_c_left_brace (c_pretty_printer *pp)
99 {
100 pp_left_brace (pp);
101 pp->set_padding (pp_none);
102 }
103
104 void
105 pp_c_right_brace (c_pretty_printer *pp)
106 {
107 pp_right_brace (pp);
108 pp->set_padding (pp_none);
109 }
110
111 void
112 pp_c_left_bracket (c_pretty_printer *pp)
113 {
114 pp_left_bracket (pp);
115 pp->set_padding (pp_none);
116 }
117
118 void
119 pp_c_right_bracket (c_pretty_printer *pp)
120 {
121 pp_right_bracket (pp);
122 pp->set_padding (pp_none);
123 }
124
125 void
126 pp_c_dot (c_pretty_printer *pp)
127 {
128 pp_dot (pp);
129 pp->set_padding (pp_none);
130 }
131
132 void
133 pp_c_ampersand (c_pretty_printer *pp)
134 {
135 pp_ampersand (pp);
136 pp->set_padding (pp_none);
137 }
138
139 void
140 pp_c_star (c_pretty_printer *pp)
141 {
142 pp_star (pp);
143 pp->set_padding (pp_none);
144 }
145
146 void
147 pp_c_arrow (c_pretty_printer *pp)
148 {
149 pp_arrow (pp);
150 pp->set_padding (pp_none);
151 }
152
153 void
154 pp_c_semicolon (c_pretty_printer *pp)
155 {
156 pp_semicolon (pp);
157 pp->set_padding (pp_none);
158 }
159
160 void
161 pp_c_complement (c_pretty_printer *pp)
162 {
163 pp_complement (pp);
164 pp->set_padding (pp_none);
165 }
166
167 void
168 pp_c_exclamation (c_pretty_printer *pp)
169 {
170 pp_exclamation (pp);
171 pp->set_padding (pp_none);
172 }
173
174 /* Print out the external representation of QUALIFIERS. */
175
176 void
177 pp_c_cv_qualifiers (c_pretty_printer *pp, int qualifiers, bool func_type)
178 {
179 const char *p = pp_last_position_in_text (pp);
180
181 if (!qualifiers)
182 return;
183
184 /* The C programming language does not have references, but it is much
185 simpler to handle those here rather than going through the same
186 logic in the C++ pretty-printer. */
187 if (p != NULL && (*p == '*' || *p == '&'))
188 pp_c_whitespace (pp);
189
190 if (qualifiers & TYPE_QUAL_ATOMIC)
191 pp_c_ws_string (pp, "_Atomic");
192 if (qualifiers & TYPE_QUAL_CONST)
193 pp_c_ws_string (pp, func_type ? "__attribute__((const))" : "const");
194 if (qualifiers & TYPE_QUAL_VOLATILE)
195 pp_c_ws_string (pp, func_type ? "__attribute__((noreturn))" : "volatile");
196 if (qualifiers & TYPE_QUAL_RESTRICT)
197 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
198 ? "restrict" : "__restrict__"));
199 }
200
201 /* Pretty-print T using the type-cast notation '( type-name )'. */
202
203 void
204 pp_c_type_cast (c_pretty_printer *pp, tree t)
205 {
206 pp_c_left_paren (pp);
207 pp->type_id (t);
208 pp_c_right_paren (pp);
209 }
210
211 /* We're about to pretty-print a pointer type as indicated by T.
212 Output a whitespace, if needed, preparing for subsequent output. */
213
214 void
215 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
216 {
217 if (POINTER_TYPE_P (t))
218 {
219 tree pointee = strip_pointer_operator (TREE_TYPE (t));
220 if (TREE_CODE (pointee) != ARRAY_TYPE
221 && TREE_CODE (pointee) != FUNCTION_TYPE)
222 pp_c_whitespace (pp);
223 }
224 }
225
226
227 /* Declarations. */
228
229 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
230 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
231 of its type. Take care of possible extensions.
232
233 type-qualifier-list:
234 type-qualifier
235 type-qualifier-list type-qualifier
236
237 type-qualifier:
238 const
239 restrict -- C99
240 __restrict__ -- GNU C
241 address-space-qualifier -- GNU C
242 volatile
243 _Atomic -- C11
244
245 address-space-qualifier:
246 identifier -- GNU C */
247
248 void
249 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
250 {
251 int qualifiers;
252
253 if (!t || t == error_mark_node)
254 return;
255
256 if (!TYPE_P (t))
257 t = TREE_TYPE (t);
258
259 if (TREE_CODE (t) != ARRAY_TYPE)
260 {
261 qualifiers = TYPE_QUALS (t);
262 pp_c_cv_qualifiers (pp, qualifiers,
263 TREE_CODE (t) == FUNCTION_TYPE);
264 }
265
266 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (t)))
267 {
268 const char *as = c_addr_space_name (TYPE_ADDR_SPACE (t));
269 pp_c_identifier (pp, as);
270 }
271 }
272
273 /* pointer:
274 * type-qualifier-list(opt)
275 * type-qualifier-list(opt) pointer */
276
277 static void
278 pp_c_pointer (c_pretty_printer *pp, tree t)
279 {
280 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
281 t = TREE_TYPE (t);
282 switch (TREE_CODE (t))
283 {
284 case POINTER_TYPE:
285 /* It is easier to handle C++ reference types here. */
286 case REFERENCE_TYPE:
287 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
288 pp_c_pointer (pp, TREE_TYPE (t));
289 if (TREE_CODE (t) == POINTER_TYPE)
290 pp_c_star (pp);
291 else
292 {
293 pp_c_ampersand (pp);
294 if (TYPE_REF_IS_RVALUE (t))
295 pp_c_ampersand (pp);
296 }
297 pp_c_type_qualifier_list (pp, t);
298 break;
299
300 /* ??? This node is now in GENERIC and so shouldn't be here. But
301 we'll fix that later. */
302 case DECL_EXPR:
303 pp->declaration (DECL_EXPR_DECL (t));
304 pp_needs_newline (pp) = true;
305 break;
306
307 default:
308 pp_unsupported_tree (pp, t);
309 }
310 }
311
312 /* simple-type-specifier:
313 type-specifier
314
315 type-specifier:
316 void
317 char
318 short
319 int
320 long
321 float
322 double
323 signed
324 unsigned
325 _Bool -- C99
326 _Complex -- C99
327 _Imaginary -- C99
328 nullptr_t -- C23
329 struct-or-union-specifier
330 enum-specifier
331 typedef-name.
332
333 GNU extensions.
334 simple-type-specifier:
335 __complex__
336 __vector__ */
337
338 void
339 c_pretty_printer::simple_type_specifier (tree t)
340 {
341 const enum tree_code code = TREE_CODE (t);
342 switch (code)
343 {
344 case ERROR_MARK:
345 translate_string ("<type-error>");
346 break;
347
348 case IDENTIFIER_NODE:
349 pp_c_identifier (this, IDENTIFIER_POINTER (t));
350 break;
351
352 case VOID_TYPE:
353 case OPAQUE_TYPE:
354 case BOOLEAN_TYPE:
355 case INTEGER_TYPE:
356 case REAL_TYPE:
357 case FIXED_POINT_TYPE:
358 if (TYPE_NAME (t))
359 {
360 t = TYPE_NAME (t);
361 simple_type_specifier (t);
362 }
363 else
364 {
365 int prec = TYPE_PRECISION (t);
366 tree common_t;
367 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
368 common_t = c_common_type_for_mode (TYPE_MODE (t),
369 TYPE_SATURATING (t));
370 else
371 common_t = c_common_type_for_mode (TYPE_MODE (t),
372 TYPE_UNSIGNED (t));
373 if (common_t && TYPE_NAME (common_t))
374 {
375 simple_type_specifier (common_t);
376 if (TYPE_PRECISION (common_t) != prec)
377 {
378 pp_colon (this);
379 pp_decimal_int (this, prec);
380 }
381 }
382 else
383 {
384 switch (code)
385 {
386 case INTEGER_TYPE:
387 translate_string (TYPE_UNSIGNED (t)
388 ? "<unnamed-unsigned:"
389 : "<unnamed-signed:");
390 break;
391 case REAL_TYPE:
392 translate_string ("<unnamed-float:");
393 break;
394 case FIXED_POINT_TYPE:
395 translate_string ("<unnamed-fixed:");
396 break;
397 default:
398 gcc_unreachable ();
399 }
400 pp_decimal_int (this, prec);
401 pp_greater (this);
402 }
403 }
404 break;
405
406 case BITINT_TYPE:
407 if (TYPE_NAME (t))
408 {
409 t = TYPE_NAME (t);
410 simple_type_specifier (t);
411 }
412 else
413 {
414 int prec = TYPE_PRECISION (t);
415 if (TYPE_UNSIGNED (t))
416 pp_c_ws_string (this, "unsigned");
417 pp_c_ws_string (this, "_BitInt(");;
418 pp_decimal_int (this, prec);
419 pp_right_paren (this);
420 }
421 break;
422
423 case TYPE_DECL:
424 if (DECL_NAME (t))
425 id_expression (t);
426 else
427 translate_string ("<typedef-error>");
428 break;
429
430 case UNION_TYPE:
431 case RECORD_TYPE:
432 case ENUMERAL_TYPE:
433 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
434 /* Don't decorate the type if this is a typedef name. */;
435 else if (code == UNION_TYPE)
436 pp_c_ws_string (this, "union");
437 else if (code == RECORD_TYPE)
438 pp_c_ws_string (this, "struct");
439 else if (code == ENUMERAL_TYPE)
440 pp_c_ws_string (this, "enum");
441 else
442 translate_string ("<tag-error>");
443
444 if (TYPE_NAME (t))
445 id_expression (TYPE_NAME (t));
446 else
447 translate_string ("<anonymous>");
448 break;
449 case NULLPTR_TYPE:
450 pp_c_ws_string (this, "nullptr_t");
451 break;
452
453 default:
454 pp_unsupported_tree (this, t);
455 break;
456 }
457 }
458
459 /* specifier-qualifier-list:
460 type-specifier specifier-qualifier-list-opt
461 type-qualifier specifier-qualifier-list-opt
462
463
464 Implementation note: Because of the non-linearities in array or
465 function declarations, this routine prints not just the
466 specifier-qualifier-list of such entities or types of such entities,
467 but also the 'pointer' production part of their declarators. The
468 remaining part is done by declarator() or abstract_declarator(). */
469
470 void
471 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
472 {
473 const enum tree_code code = TREE_CODE (t);
474
475 if (!(pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
476 pp_c_type_qualifier_list (pp, t);
477 switch (code)
478 {
479 case REFERENCE_TYPE:
480 case POINTER_TYPE:
481 {
482 /* Get the types-specifier of this type. */
483 tree pointee = strip_pointer_operator (TREE_TYPE (t));
484 pp_c_specifier_qualifier_list (pp, pointee);
485 if (TREE_CODE (pointee) == ARRAY_TYPE
486 || TREE_CODE (pointee) == FUNCTION_TYPE)
487 {
488 pp_c_whitespace (pp);
489 pp_c_left_paren (pp);
490 /* If we're dealing with the GNU form of attributes, print this:
491 void (__attribute__((noreturn)) *f) ();
492 If it is the standard [[]] attribute, we'll print the attribute
493 in c_pretty_printer::direct_abstract_declarator/FUNCTION_TYPE. */
494 if (!cxx11_attribute_p (TYPE_ATTRIBUTES (pointee)))
495 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (pointee));
496 }
497 else if (!c_dialect_cxx ())
498 pp_c_whitespace (pp);
499 pp_ptr_operator (pp, t);
500 }
501 break;
502
503 case FUNCTION_TYPE:
504 case ARRAY_TYPE:
505 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
506 break;
507
508 case VECTOR_TYPE:
509 case COMPLEX_TYPE:
510 if (code == COMPLEX_TYPE)
511 pp_c_ws_string (pp, (flag_isoc99 && !c_dialect_cxx ()
512 ? "_Complex" : "__complex__"));
513 else if (code == VECTOR_TYPE)
514 {
515 /* The syntax we print for vector types isn't real C or C++ syntax,
516 so it's better to print the type name if we have one. */
517 tree name = TYPE_NAME (t);
518 if (!(pp->flags & pp_c_flag_gnu_v3)
519 && name
520 && TREE_CODE (name) == TYPE_DECL)
521 {
522 pp->id_expression (name);
523 break;
524 }
525 pp_c_ws_string (pp, "__vector");
526 pp_c_left_paren (pp);
527 pp_wide_integer (pp, TYPE_VECTOR_SUBPARTS (t));
528 pp_c_right_paren (pp);
529 pp_c_whitespace (pp);
530 }
531 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
532 break;
533
534 default:
535 pp->simple_type_specifier (t);
536 break;
537 }
538 if ((pp->flags & pp_c_flag_gnu_v3) && code != POINTER_TYPE)
539 pp_c_type_qualifier_list (pp, t);
540 }
541
542 /* parameter-type-list:
543 parameter-list
544 parameter-list , ...
545
546 parameter-list:
547 parameter-declaration
548 parameter-list , parameter-declaration
549
550 parameter-declaration:
551 declaration-specifiers declarator
552 declaration-specifiers abstract-declarator(opt) */
553
554 void
555 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
556 {
557 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
558 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
559 pp_c_left_paren (pp);
560 if (parms == void_list_node)
561 pp_c_ws_string (pp, "void");
562 else
563 {
564 bool first = true;
565 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
566 {
567 if (!first)
568 pp_separate_with (pp, ',');
569 first = false;
570 pp->declaration_specifiers
571 (want_parm_decl ? parms : TREE_VALUE (parms));
572 if (want_parm_decl)
573 pp->declarator (parms);
574 else
575 pp->abstract_declarator (TREE_VALUE (parms));
576 }
577 if (!first && !parms)
578 {
579 pp_separate_with (pp, ',');
580 pp_string (pp, "...");
581 }
582 }
583 pp_c_right_paren (pp);
584 }
585
586 /* abstract-declarator:
587 pointer
588 pointer(opt) direct-abstract-declarator */
589
590 void
591 c_pretty_printer::abstract_declarator (tree t)
592 {
593 if (TREE_CODE (t) == POINTER_TYPE)
594 {
595 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
596 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
597 pp_c_right_paren (this);
598 t = TREE_TYPE (t);
599 }
600
601 direct_abstract_declarator (t);
602 }
603
604 /* direct-abstract-declarator:
605 ( abstract-declarator )
606 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
607 direct-abstract-declarator(opt) [ * ]
608 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
609
610 void
611 c_pretty_printer::direct_abstract_declarator (tree t)
612 {
613 bool add_space = false;
614
615 switch (TREE_CODE (t))
616 {
617 case POINTER_TYPE:
618 abstract_declarator (t);
619 break;
620
621 case FUNCTION_TYPE:
622 pp_c_parameter_type_list (this, t);
623 direct_abstract_declarator (TREE_TYPE (t));
624 /* If this is the standard [[]] attribute, print
625 void (*)() [[noreturn]]; */
626 if (cxx11_attribute_p (TYPE_ATTRIBUTES (t)))
627 {
628 pp_space (this);
629 pp_c_attributes_display (this, TYPE_ATTRIBUTES (t));
630 }
631 break;
632
633 case ARRAY_TYPE:
634 pp_c_left_bracket (this);
635
636 if (int quals = TYPE_QUALS (t))
637 {
638 /* Print the array qualifiers such as in "T[const restrict 3]". */
639 pp_c_cv_qualifiers (this, quals, false);
640 add_space = true;
641 }
642
643 if (tree arr = lookup_attribute ("array", TYPE_ATTRIBUTES (t)))
644 {
645 if (TREE_VALUE (arr))
646 {
647 /* Print the specifier as in "T[static 3]" that's not actually
648 part of the type but may be added by the front end. */
649 pp_c_ws_string (this, "static");
650 add_space = true;
651 }
652 else if (!TYPE_DOMAIN (t))
653 /* For arrays of unspecified bound using the [*] notation. */
654 pp_character (this, '*');
655 }
656
657 if (tree dom = TYPE_DOMAIN (t))
658 {
659 if (tree maxval = TYPE_MAX_VALUE (dom))
660 {
661 if (add_space)
662 pp_space (this);
663
664 tree type = TREE_TYPE (maxval);
665
666 if (tree_fits_shwi_p (maxval))
667 pp_wide_integer (this, tree_to_shwi (maxval) + 1);
668 else if (TREE_CODE (maxval) == INTEGER_CST)
669 expression (fold_build2 (PLUS_EXPR, type, maxval,
670 build_int_cst (type, 1)));
671 else
672 {
673 /* Strip the expressions from around a VLA bound added
674 internally to make it fit the domain mold, including
675 any casts. */
676 if (TREE_CODE (maxval) == NOP_EXPR)
677 maxval = TREE_OPERAND (maxval, 0);
678 if (TREE_CODE (maxval) == PLUS_EXPR
679 && integer_all_onesp (TREE_OPERAND (maxval, 1)))
680 {
681 maxval = TREE_OPERAND (maxval, 0);
682 if (TREE_CODE (maxval) == NOP_EXPR)
683 maxval = TREE_OPERAND (maxval, 0);
684 }
685 if (TREE_CODE (maxval) == SAVE_EXPR)
686 {
687 maxval = TREE_OPERAND (maxval, 0);
688 if (TREE_CODE (maxval) == NOP_EXPR)
689 maxval = TREE_OPERAND (maxval, 0);
690 }
691
692 /* This covers unspecified bounds. */
693 if (TREE_CODE (maxval) == COMPOUND_EXPR)
694 pp_string (this, "*");
695 else
696 expression (maxval);
697 }
698 }
699 else if (TYPE_SIZE (t))
700 /* Print zero for zero-length arrays but not for flexible
701 array members whose TYPE_SIZE is null. */
702 pp_string (this, "0");
703 }
704 pp_c_right_bracket (this);
705 direct_abstract_declarator (TREE_TYPE (t));
706 break;
707
708 case IDENTIFIER_NODE:
709 case VOID_TYPE:
710 case OPAQUE_TYPE:
711 case BOOLEAN_TYPE:
712 case INTEGER_TYPE:
713 case REAL_TYPE:
714 case FIXED_POINT_TYPE:
715 case ENUMERAL_TYPE:
716 case BITINT_TYPE:
717 case RECORD_TYPE:
718 case UNION_TYPE:
719 case VECTOR_TYPE:
720 case COMPLEX_TYPE:
721 case TYPE_DECL:
722 case ERROR_MARK:
723 case NULLPTR_TYPE:
724 break;
725
726 default:
727 pp_unsupported_tree (this, t);
728 break;
729 }
730 }
731
732 /* type-name:
733 specifier-qualifier-list abstract-declarator(opt) */
734
735 void
736 c_pretty_printer::type_id (tree t)
737 {
738 pp_c_specifier_qualifier_list (this, t);
739 abstract_declarator (t);
740 }
741
742 /* storage-class-specifier:
743 typedef
744 extern
745 static
746 auto
747 register */
748
749 void
750 c_pretty_printer::storage_class_specifier (tree t)
751 {
752 if (TREE_CODE (t) == TYPE_DECL)
753 pp_c_ws_string (this, "typedef");
754 else if (DECL_P (t))
755 {
756 if ((TREE_CODE (t) == PARM_DECL || VAR_P (t))
757 && DECL_REGISTER (t))
758 pp_c_ws_string (this, "register");
759 else if (TREE_STATIC (t) && VAR_P (t))
760 pp_c_ws_string (this, "static");
761 }
762 }
763
764 /* function-specifier:
765 inline */
766
767 void
768 c_pretty_printer::function_specifier (tree t)
769 {
770 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
771 pp_c_ws_string (this, "inline");
772 }
773
774 /* declaration-specifiers:
775 storage-class-specifier declaration-specifiers(opt)
776 type-specifier declaration-specifiers(opt)
777 type-qualifier declaration-specifiers(opt)
778 function-specifier declaration-specifiers(opt) */
779
780 void
781 c_pretty_printer::declaration_specifiers (tree t)
782 {
783 storage_class_specifier (t);
784 function_specifier (t);
785 pp_c_specifier_qualifier_list (this, DECL_P (t) ? TREE_TYPE (t) : t);
786 }
787
788 /* direct-declarator
789 identifier
790 ( declarator )
791 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
792 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
793 direct-declarator [ type-qualifier-list static assignment-expression ]
794 direct-declarator [ type-qualifier-list * ]
795 direct-declarator ( parameter-type-list )
796 direct-declarator ( identifier-list(opt) ) */
797
798 void
799 c_pretty_printer::direct_declarator (tree t)
800 {
801 switch (TREE_CODE (t))
802 {
803 case VAR_DECL:
804 case PARM_DECL:
805 case TYPE_DECL:
806 case FIELD_DECL:
807 case LABEL_DECL:
808 pp_c_space_for_pointer_operator (this, TREE_TYPE (t));
809 pp_c_tree_decl_identifier (this, t);
810 break;
811
812 case ARRAY_TYPE:
813 case POINTER_TYPE:
814 abstract_declarator (TREE_TYPE (t));
815 break;
816
817 case FUNCTION_TYPE:
818 pp_parameter_list (this, t);
819 abstract_declarator (TREE_TYPE (t));
820 break;
821
822 case FUNCTION_DECL:
823 pp_c_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
824 pp_c_tree_decl_identifier (this, t);
825 if (flags & pp_c_flag_abstract)
826 abstract_declarator (TREE_TYPE (t));
827 else
828 {
829 pp_parameter_list (this, t);
830 abstract_declarator (TREE_TYPE (TREE_TYPE (t)));
831 }
832 break;
833
834 case INTEGER_TYPE:
835 case REAL_TYPE:
836 case FIXED_POINT_TYPE:
837 case ENUMERAL_TYPE:
838 case BITINT_TYPE:
839 case UNION_TYPE:
840 case RECORD_TYPE:
841 break;
842
843 default:
844 pp_unsupported_tree (this, t);
845 break;
846 }
847 }
848
849
850 /* declarator:
851 pointer(opt) direct-declarator */
852
853 void
854 c_pretty_printer::declarator (tree t)
855 {
856 switch (TREE_CODE (t))
857 {
858 case INTEGER_TYPE:
859 case REAL_TYPE:
860 case FIXED_POINT_TYPE:
861 case ENUMERAL_TYPE:
862 case BITINT_TYPE:
863 case UNION_TYPE:
864 case RECORD_TYPE:
865 break;
866
867 case VAR_DECL:
868 case PARM_DECL:
869 case FIELD_DECL:
870 case ARRAY_TYPE:
871 case FUNCTION_TYPE:
872 case FUNCTION_DECL:
873 case TYPE_DECL:
874 direct_declarator (t);
875 break;
876
877
878 default:
879 pp_unsupported_tree (this, t);
880 break;
881 }
882 }
883
884 /* declaration:
885 declaration-specifiers init-declarator-list(opt) ; */
886
887 void
888 c_pretty_printer::declaration (tree t)
889 {
890 declaration_specifiers (t);
891 pp_c_init_declarator (this, t);
892 }
893
894 /* Pretty-print ATTRIBUTES marked to be displayed on diagnostic. */
895
896 void
897 pp_c_attributes_display (c_pretty_printer *pp, tree a)
898 {
899 bool is_first = true;
900
901 if (a == NULL_TREE)
902 return;
903
904 const bool std_p = cxx11_attribute_p (a);
905
906 for (; a != NULL_TREE; a = TREE_CHAIN (a))
907 {
908 const struct attribute_spec *as
909 = lookup_attribute_spec (get_attribute_name (a));
910 if (!as || as->affects_type_identity == false)
911 continue;
912 if (c_dialect_cxx ()
913 && !strcmp ("transaction_safe", as->name))
914 /* In C++ transaction_safe is printed at the end of the declarator. */
915 continue;
916 if (is_first)
917 {
918 if (std_p)
919 {
920 pp_c_left_bracket (pp);
921 pp_c_left_bracket (pp);
922 }
923 else
924 {
925 pp_c_ws_string (pp, "__attribute__");
926 pp_c_left_paren (pp);
927 pp_c_left_paren (pp);
928 }
929 is_first = false;
930 }
931 else
932 pp_separate_with (pp, ',');
933 tree ns;
934 if (std_p && (ns = get_attribute_namespace (a)))
935 {
936 pp_tree_identifier (pp, ns);
937 pp_colon (pp);
938 pp_colon (pp);
939 }
940 pp_tree_identifier (pp, get_attribute_name (a));
941 if (TREE_VALUE (a))
942 pp_c_call_argument_list (pp, TREE_VALUE (a));
943 }
944
945 if (!is_first)
946 {
947 if (std_p)
948 {
949 pp_c_right_bracket (pp);
950 pp_c_right_bracket (pp);
951 }
952 else
953 {
954 pp_c_right_paren (pp);
955 pp_c_right_paren (pp);
956 pp_c_whitespace (pp);
957 }
958 }
959 }
960
961 /* function-definition:
962 declaration-specifiers declarator compound-statement */
963
964 void
965 pp_c_function_definition (c_pretty_printer *pp, tree t)
966 {
967 pp->declaration_specifiers (t);
968 pp->declarator (t);
969 pp_needs_newline (pp) = true;
970 pp->statement (DECL_SAVED_TREE (t));
971 pp_newline_and_flush (pp);
972 }
973
974
975 /* Expressions. */
976
977 /* Print out a c-char. This is called solely for characters which are
978 in the *target* execution character set. We ought to convert them
979 back to the *host* execution character set before printing, but we
980 have no way to do this at present. A decent compromise is to print
981 all characters as if they were in the host execution character set,
982 and not attempt to recover any named escape characters, but render
983 all unprintables as octal escapes. If the host and target character
984 sets are the same, this produces relatively readable output. If they
985 are not the same, strings may appear as gibberish, but that's okay
986 (in fact, it may well be what the reader wants, e.g. if they are looking
987 to see if conversion to the target character set happened correctly).
988
989 A special case: we need to prefix \, ", and ' with backslashes. It is
990 correct to do so for the *host*'s \, ", and ', because the rest of the
991 file appears in the host character set. */
992
993 static void
994 pp_c_char (c_pretty_printer *pp, int c)
995 {
996 if (ISPRINT (c))
997 {
998 switch (c)
999 {
1000 case '\\': pp_string (pp, "\\\\"); break;
1001 case '\'': pp_string (pp, "\\\'"); break;
1002 case '\"': pp_string (pp, "\\\""); break;
1003 default: pp_character (pp, c);
1004 }
1005 }
1006 else
1007 pp_scalar (pp, "\\%03o", (unsigned) c);
1008 }
1009
1010 /* Print out a STRING literal. */
1011
1012 void
1013 pp_c_string_literal (c_pretty_printer *pp, tree s)
1014 {
1015 const char *p = TREE_STRING_POINTER (s);
1016 int n = TREE_STRING_LENGTH (s) - 1;
1017 int i;
1018 pp_doublequote (pp);
1019 for (i = 0; i < n; ++i)
1020 pp_c_char (pp, p[i]);
1021 pp_doublequote (pp);
1022 }
1023
1024 /* Pretty-print a VOID_CST (void_node). */
1025
1026 static void
1027 pp_c_void_constant (c_pretty_printer *pp)
1028 {
1029 pp_c_type_cast (pp, void_type_node);
1030 pp_string (pp, "0");
1031 }
1032
1033 /* Pretty-print an INTEGER literal. */
1034
1035 void
1036 pp_c_integer_constant (c_pretty_printer *pp, tree i)
1037 {
1038 if (tree_fits_shwi_p (i))
1039 pp_wide_integer (pp, tree_to_shwi (i));
1040 else if (tree_fits_uhwi_p (i))
1041 pp_unsigned_wide_integer (pp, tree_to_uhwi (i));
1042 else
1043 {
1044 wide_int wi = wi::to_wide (i);
1045
1046 if (wi::lt_p (wi::to_wide (i), 0, TYPE_SIGN (TREE_TYPE (i))))
1047 {
1048 pp_minus (pp);
1049 wi = -wi;
1050 }
1051 unsigned int prec = wi.get_precision ();
1052 if ((prec + 3) / 4 > sizeof (pp_buffer (pp)->m_digit_buffer) - 3)
1053 {
1054 char *buf = XALLOCAVEC (char, (prec + 3) / 4 + 3);
1055 print_hex (wi, buf);
1056 pp_string (pp, buf);
1057 }
1058 else
1059 {
1060 print_hex (wi, pp_buffer (pp)->m_digit_buffer);
1061 pp_string (pp, pp_buffer (pp)->m_digit_buffer);
1062 }
1063 }
1064 }
1065
1066 /* Print out a CHARACTER literal. */
1067
1068 static void
1069 pp_c_character_constant (c_pretty_printer *pp, tree c)
1070 {
1071 pp_quote (pp);
1072 pp_c_char (pp, (unsigned) TREE_INT_CST_LOW (c));
1073 pp_quote (pp);
1074 }
1075
1076 /* Print out a BOOLEAN literal. */
1077
1078 static void
1079 pp_c_bool_constant (c_pretty_printer *pp, tree b)
1080 {
1081 if (b == boolean_false_node)
1082 {
1083 if (c_dialect_cxx ())
1084 pp_c_ws_string (pp, "false");
1085 else if (flag_isoc99)
1086 pp_c_ws_string (pp, "_False");
1087 else
1088 pp_unsupported_tree (pp, b);
1089 }
1090 else if (b == boolean_true_node)
1091 {
1092 if (c_dialect_cxx ())
1093 pp_c_ws_string (pp, "true");
1094 else if (flag_isoc99)
1095 pp_c_ws_string (pp, "_True");
1096 else
1097 pp_unsupported_tree (pp, b);
1098 }
1099 else if (TREE_CODE (b) == INTEGER_CST)
1100 pp_c_integer_constant (pp, b);
1101 else
1102 pp_unsupported_tree (pp, b);
1103 }
1104
1105 /* Given a value e of ENUMERAL_TYPE:
1106 Print out the first ENUMERATOR id with value e, if one is found,
1107 else print out the value as a C-style cast (type-id)value. */
1108
1109 static void
1110 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
1111 {
1112 tree type = TREE_TYPE (e);
1113 tree value = NULL_TREE;
1114
1115 /* Find the name of this constant. */
1116 if ((pp->flags & pp_c_flag_gnu_v3) == 0)
1117 for (value = TYPE_VALUES (type); value != NULL_TREE;
1118 value = TREE_CHAIN (value))
1119 if (tree_int_cst_equal (DECL_INITIAL (TREE_VALUE (value)), e))
1120 break;
1121
1122 if (value != NULL_TREE)
1123 pp->id_expression (TREE_PURPOSE (value));
1124 else
1125 {
1126 /* Value must have been cast. */
1127 pp_c_type_cast (pp, type);
1128 pp_c_integer_constant (pp, e);
1129 }
1130 }
1131
1132 /* Print out a REAL value as a decimal-floating-constant. */
1133
1134 static void
1135 pp_c_floating_constant (c_pretty_printer *pp, tree r)
1136 {
1137 const struct real_format *fmt
1138 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
1139
1140 REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
1141 bool is_decimal = floating_cst.decimal;
1142
1143 /* See ISO C++ WG N1822. Note: The fraction 643/2136 approximates
1144 log10(2) to 7 significant digits. */
1145 int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
1146
1147 real_to_decimal (pp_buffer (pp)->m_digit_buffer, &TREE_REAL_CST (r),
1148 sizeof (pp_buffer (pp)->m_digit_buffer),
1149 max_digits10, 1);
1150
1151 pp_string (pp, pp_buffer(pp)->m_digit_buffer);
1152 if (TREE_TYPE (r) == float_type_node)
1153 pp_character (pp, 'f');
1154 else if (TREE_TYPE (r) == long_double_type_node)
1155 pp_character (pp, 'l');
1156 else if (TREE_TYPE (r) == dfloat128_type_node)
1157 pp_string (pp, "dl");
1158 else if (TREE_TYPE (r) == dfloat64_type_node)
1159 pp_string (pp, "dd");
1160 else if (TREE_TYPE (r) == dfloat32_type_node)
1161 pp_string (pp, "df");
1162 else if (TREE_TYPE (r) == dfloat64x_type_node)
1163 pp_string (pp, "d64x");
1164 else if (TREE_TYPE (r) != double_type_node)
1165 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
1166 if (TREE_TYPE (r) == FLOATN_NX_TYPE_NODE (i))
1167 {
1168 pp_character (pp, 'f');
1169 pp_decimal_int (pp, floatn_nx_types[i].n);
1170 if (floatn_nx_types[i].extended)
1171 pp_character (pp, 'x');
1172 break;
1173 }
1174 }
1175
1176 /* Print out a FIXED value as a decimal-floating-constant. */
1177
1178 static void
1179 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
1180 {
1181 fixed_to_decimal (pp_buffer (pp)->m_digit_buffer, &TREE_FIXED_CST (r),
1182 sizeof (pp_buffer (pp)->m_digit_buffer));
1183 pp_string (pp, pp_buffer(pp)->m_digit_buffer);
1184 }
1185
1186 /* Pretty-print a compound literal expression. GNU extensions include
1187 vector constants. */
1188
1189 static void
1190 pp_c_compound_literal (c_pretty_printer *pp, tree e)
1191 {
1192 tree type = TREE_TYPE (e);
1193 pp_c_type_cast (pp, type);
1194
1195 switch (TREE_CODE (type))
1196 {
1197 case RECORD_TYPE:
1198 case UNION_TYPE:
1199 case ARRAY_TYPE:
1200 case VECTOR_TYPE:
1201 case COMPLEX_TYPE:
1202 pp_c_brace_enclosed_initializer_list (pp, e);
1203 break;
1204
1205 default:
1206 pp_unsupported_tree (pp, e);
1207 break;
1208 }
1209 }
1210
1211 /* Pretty-print a COMPLEX_EXPR expression. */
1212
1213 static void
1214 pp_c_complex_expr (c_pretty_printer *pp, tree e)
1215 {
1216 /* Handle a few common special cases, otherwise fallback
1217 to printing it as compound literal. */
1218 tree type = TREE_TYPE (e);
1219 tree realexpr = TREE_OPERAND (e, 0);
1220 tree imagexpr = TREE_OPERAND (e, 1);
1221
1222 /* Cast of an COMPLEX_TYPE expression to a different COMPLEX_TYPE. */
1223 if (TREE_CODE (realexpr) == NOP_EXPR
1224 && TREE_CODE (imagexpr) == NOP_EXPR
1225 && TREE_TYPE (realexpr) == TREE_TYPE (type)
1226 && TREE_TYPE (imagexpr) == TREE_TYPE (type)
1227 && TREE_CODE (TREE_OPERAND (realexpr, 0)) == REALPART_EXPR
1228 && TREE_CODE (TREE_OPERAND (imagexpr, 0)) == IMAGPART_EXPR
1229 && TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0)
1230 == TREE_OPERAND (TREE_OPERAND (imagexpr, 0), 0))
1231 {
1232 pp_c_type_cast (pp, type);
1233 pp->expression (TREE_OPERAND (TREE_OPERAND (realexpr, 0), 0));
1234 return;
1235 }
1236
1237 /* Cast of an scalar expression to COMPLEX_TYPE. */
1238 if ((integer_zerop (imagexpr) || real_zerop (imagexpr))
1239 && TREE_TYPE (realexpr) == TREE_TYPE (type))
1240 {
1241 pp_c_type_cast (pp, type);
1242 if (TREE_CODE (realexpr) == NOP_EXPR)
1243 realexpr = TREE_OPERAND (realexpr, 0);
1244 pp->expression (realexpr);
1245 return;
1246 }
1247
1248 pp_c_compound_literal (pp, e);
1249 }
1250
1251 /* constant:
1252 integer-constant
1253 floating-constant
1254 fixed-point-constant
1255 enumeration-constant
1256 character-constant */
1257
1258 void
1259 c_pretty_printer::constant (tree e)
1260 {
1261 const enum tree_code code = TREE_CODE (e);
1262
1263 switch (code)
1264 {
1265 case VOID_CST:
1266 pp_c_void_constant (this);
1267 break;
1268
1269 case INTEGER_CST:
1270 {
1271 tree type = TREE_TYPE (e);
1272 if (type == boolean_type_node)
1273 pp_c_bool_constant (this, e);
1274 else if (type == char_type_node)
1275 pp_c_character_constant (this, e);
1276 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1277 pp_c_enumeration_constant (this, e);
1278 else if (NULLPTR_TYPE_P (type))
1279 pp_string (this, "nullptr");
1280 else
1281 pp_c_integer_constant (this, e);
1282 }
1283 break;
1284
1285 case REAL_CST:
1286 pp_c_floating_constant (this, e);
1287 break;
1288
1289 case FIXED_CST:
1290 pp_c_fixed_constant (this, e);
1291 break;
1292
1293 case STRING_CST:
1294 pp_c_string_literal (this, e);
1295 break;
1296
1297 case COMPLEX_CST:
1298 /* Sometimes, we are confused and we think a complex literal
1299 is a constant. Such thing is a compound literal which
1300 grammatically belongs to postfix-expr production. */
1301 pp_c_compound_literal (this, e);
1302 break;
1303
1304 default:
1305 pp_unsupported_tree (this, e);
1306 break;
1307 }
1308 }
1309
1310 /* Pretty-print a string such as an identifier, without changing its
1311 encoding, preceded by whitespace is necessary. */
1312
1313 void
1314 pp_c_ws_string (c_pretty_printer *pp, const char *str)
1315 {
1316 pp_c_maybe_whitespace (pp);
1317 pp_string (pp, str);
1318 pp->set_padding (pp_before);
1319 }
1320
1321 void
1322 c_pretty_printer::translate_string (const char *gmsgid)
1323 {
1324 if (pp_translate_identifiers (this))
1325 pp_c_ws_string (this, _(gmsgid));
1326 else
1327 pp_c_ws_string (this, gmsgid);
1328 }
1329
1330 /* Pretty-print an IDENTIFIER_NODE, which may contain UTF-8 sequences
1331 that need converting to the locale encoding, preceded by whitespace
1332 is necessary. */
1333
1334 void
1335 pp_c_identifier (c_pretty_printer *pp, const char *id)
1336 {
1337 pp_c_maybe_whitespace (pp);
1338 pp_identifier (pp, id);
1339 pp->set_padding (pp_before);
1340 }
1341
1342 /* Pretty-print a C primary-expression.
1343 primary-expression:
1344 identifier
1345 constant
1346 string-literal
1347 ( expression ) */
1348
1349 void
1350 c_pretty_printer::primary_expression (tree e)
1351 {
1352 switch (TREE_CODE (e))
1353 {
1354 case VAR_DECL:
1355 case PARM_DECL:
1356 case FIELD_DECL:
1357 case CONST_DECL:
1358 case FUNCTION_DECL:
1359 case LABEL_DECL:
1360 pp_c_tree_decl_identifier (this, e);
1361 break;
1362
1363 case IDENTIFIER_NODE:
1364 pp_c_tree_identifier (this, e);
1365 break;
1366
1367 case ERROR_MARK:
1368 translate_string ("<erroneous-expression>");
1369 break;
1370
1371 case RESULT_DECL:
1372 translate_string ("<return-value>");
1373 break;
1374
1375 case VOID_CST:
1376 case INTEGER_CST:
1377 case REAL_CST:
1378 case FIXED_CST:
1379 case STRING_CST:
1380 constant (e);
1381 break;
1382
1383 case TARGET_EXPR:
1384 pp_c_ws_string (this, "__builtin_memcpy");
1385 pp_c_left_paren (this);
1386 pp_ampersand (this);
1387 primary_expression (TARGET_EXPR_SLOT (e));
1388 pp_separate_with (this, ',');
1389 pp_ampersand (this);
1390 initializer (TARGET_EXPR_INITIAL (e));
1391 if (TARGET_EXPR_CLEANUP (e))
1392 {
1393 pp_separate_with (this, ',');
1394 expression (TARGET_EXPR_CLEANUP (e));
1395 }
1396 pp_c_right_paren (this);
1397 break;
1398
1399 case SSA_NAME:
1400 if (SSA_NAME_VAR (e))
1401 primary_expression (SSA_NAME_VAR (e));
1402 else if (gimple_assign_single_p (SSA_NAME_DEF_STMT (e)))
1403 {
1404 /* Print only the right side of the GIMPLE assignment. */
1405 gimple *def_stmt = SSA_NAME_DEF_STMT (e);
1406 pp_gimple_stmt_1 (this, def_stmt, 0, TDF_RHS_ONLY);
1407 }
1408 else
1409 expression (e);
1410 break;
1411
1412 default:
1413 /* FIXME: Make sure we won't get into an infinite loop. */
1414 if (location_wrapper_p (e))
1415 expression (e);
1416 else
1417 {
1418 pp_c_left_paren (this);
1419 expression (e);
1420 pp_c_right_paren (this);
1421 }
1422 break;
1423 }
1424 }
1425
1426 /* Print out a C initializer -- also support C compound-literals.
1427 initializer:
1428 assignment-expression:
1429 { initializer-list }
1430 { initializer-list , } */
1431
1432 void
1433 c_pretty_printer::initializer (tree e)
1434 {
1435 if (TREE_CODE (e) == CONSTRUCTOR)
1436 pp_c_brace_enclosed_initializer_list (this, e);
1437 else
1438 expression (e);
1439 }
1440
1441 /* init-declarator:
1442 declarator:
1443 declarator = initializer */
1444
1445 void
1446 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1447 {
1448 pp->declarator (t);
1449 /* We don't want to output function definitions here. There are handled
1450 elsewhere (and the syntactic form is bogus anyway). */
1451 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1452 {
1453 tree init = DECL_INITIAL (t);
1454 /* This C++ bit is handled here because it is easier to do so.
1455 In templates, the C++ parser builds a TREE_LIST for a
1456 direct-initialization; the TREE_PURPOSE is the variable to
1457 initialize and the TREE_VALUE is the initializer. */
1458 if (TREE_CODE (init) == TREE_LIST)
1459 {
1460 pp_c_left_paren (pp);
1461 pp->expression (TREE_VALUE (init));
1462 pp_right_paren (pp);
1463 }
1464 else
1465 {
1466 pp_space (pp);
1467 pp_equal (pp);
1468 pp_space (pp);
1469 pp->initializer (init);
1470 }
1471 }
1472 }
1473
1474 /* initializer-list:
1475 designation(opt) initializer
1476 initializer-list , designation(opt) initializer
1477
1478 designation:
1479 designator-list =
1480
1481 designator-list:
1482 designator
1483 designator-list designator
1484
1485 designator:
1486 [ constant-expression ]
1487 identifier */
1488
1489 static void
1490 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1491 {
1492 tree type = TREE_TYPE (e);
1493 const enum tree_code code = TREE_CODE (type);
1494
1495 if (TREE_CODE (e) == CONSTRUCTOR)
1496 {
1497 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1498 return;
1499 }
1500
1501 switch (code)
1502 {
1503 case RECORD_TYPE:
1504 case UNION_TYPE:
1505 case ARRAY_TYPE:
1506 {
1507 tree init = TREE_OPERAND (e, 0);
1508 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1509 {
1510 if (code == RECORD_TYPE || code == UNION_TYPE)
1511 {
1512 pp_c_dot (pp);
1513 pp->primary_expression (TREE_PURPOSE (init));
1514 }
1515 else
1516 {
1517 pp_c_left_bracket (pp);
1518 if (TREE_PURPOSE (init))
1519 pp->constant (TREE_PURPOSE (init));
1520 pp_c_right_bracket (pp);
1521 }
1522 pp_c_whitespace (pp);
1523 pp_equal (pp);
1524 pp_c_whitespace (pp);
1525 pp->initializer (TREE_VALUE (init));
1526 if (TREE_CHAIN (init))
1527 pp_separate_with (pp, ',');
1528 }
1529 }
1530 return;
1531
1532 case VECTOR_TYPE:
1533 if (TREE_CODE (e) == VECTOR_CST)
1534 {
1535 /* We don't create variable-length VECTOR_CSTs. */
1536 unsigned int nunits = VECTOR_CST_NELTS (e).to_constant ();
1537 for (unsigned int i = 0; i < nunits; ++i)
1538 {
1539 if (i > 0)
1540 pp_separate_with (pp, ',');
1541 pp->expression (VECTOR_CST_ELT (e, i));
1542 }
1543 }
1544 else
1545 break;
1546 return;
1547
1548 case COMPLEX_TYPE:
1549 if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1550 {
1551 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1552 pp->expression (cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1553 pp_separate_with (pp, ',');
1554 pp->expression (cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1555 }
1556 else
1557 break;
1558 return;
1559
1560 default:
1561 break;
1562 }
1563
1564 pp_unsupported_tree (pp, type);
1565 }
1566
1567 /* Pretty-print a brace-enclosed initializer-list. */
1568
1569 static void
1570 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1571 {
1572 pp_c_left_brace (pp);
1573 pp_c_initializer_list (pp, l);
1574 pp_c_right_brace (pp);
1575 }
1576
1577
1578 /* This is a convenient function, used to bridge gap between C and C++
1579 grammars.
1580
1581 id-expression:
1582 identifier */
1583
1584 void
1585 c_pretty_printer::id_expression (tree t)
1586 {
1587 switch (TREE_CODE (t))
1588 {
1589 case VAR_DECL:
1590 case PARM_DECL:
1591 case CONST_DECL:
1592 case TYPE_DECL:
1593 case FUNCTION_DECL:
1594 case FIELD_DECL:
1595 case LABEL_DECL:
1596 pp_c_tree_decl_identifier (this, t);
1597 break;
1598
1599 case IDENTIFIER_NODE:
1600 pp_c_tree_identifier (this, t);
1601 break;
1602
1603 default:
1604 pp_unsupported_tree (this, t);
1605 break;
1606 }
1607 }
1608
1609 /* postfix-expression:
1610 primary-expression
1611 postfix-expression [ expression ]
1612 postfix-expression ( argument-expression-list(opt) )
1613 postfix-expression . identifier
1614 postfix-expression -> identifier
1615 postfix-expression ++
1616 postfix-expression --
1617 ( type-name ) { initializer-list }
1618 ( type-name ) { initializer-list , } */
1619
1620 void
1621 c_pretty_printer::postfix_expression (tree e)
1622 {
1623 enum tree_code code = TREE_CODE (e);
1624 switch (code)
1625 {
1626 case POSTINCREMENT_EXPR:
1627 case POSTDECREMENT_EXPR:
1628 postfix_expression (TREE_OPERAND (e, 0));
1629 pp_string (this, code == POSTINCREMENT_EXPR ? "++" : "--");
1630 break;
1631
1632 case ARRAY_REF:
1633 postfix_expression (TREE_OPERAND (e, 0));
1634 pp_c_left_bracket (this);
1635 expression (TREE_OPERAND (e, 1));
1636 pp_c_right_bracket (this);
1637 break;
1638
1639 case OMP_ARRAY_SECTION:
1640 postfix_expression (TREE_OPERAND (e, 0));
1641 pp_c_left_bracket (this);
1642 if (TREE_OPERAND (e, 1))
1643 expression (TREE_OPERAND (e, 1));
1644 pp_colon (this);
1645 if (TREE_OPERAND (e, 2))
1646 expression (TREE_OPERAND (e, 2));
1647 pp_c_right_bracket (this);
1648 break;
1649
1650 case CALL_EXPR:
1651 {
1652 call_expr_arg_iterator iter;
1653 tree arg;
1654 if (CALL_EXPR_FN (e) != NULL_TREE)
1655 postfix_expression (CALL_EXPR_FN (e));
1656 else
1657 pp_string (this, internal_fn_name (CALL_EXPR_IFN (e)));
1658 pp_c_left_paren (this);
1659 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1660 {
1661 expression (arg);
1662 if (more_call_expr_args_p (&iter))
1663 pp_separate_with (this, ',');
1664 }
1665 pp_c_right_paren (this);
1666 break;
1667 }
1668
1669 case UNORDERED_EXPR:
1670 pp_c_ws_string (this, flag_isoc99
1671 ? "isunordered"
1672 : "__builtin_isunordered");
1673 goto two_args_fun;
1674
1675 case ORDERED_EXPR:
1676 pp_c_ws_string (this, flag_isoc99
1677 ? "!isunordered"
1678 : "!__builtin_isunordered");
1679 goto two_args_fun;
1680
1681 case UNLT_EXPR:
1682 pp_c_ws_string (this, flag_isoc99
1683 ? "!isgreaterequal"
1684 : "!__builtin_isgreaterequal");
1685 goto two_args_fun;
1686
1687 case UNLE_EXPR:
1688 pp_c_ws_string (this, flag_isoc99
1689 ? "!isgreater"
1690 : "!__builtin_isgreater");
1691 goto two_args_fun;
1692
1693 case UNGT_EXPR:
1694 pp_c_ws_string (this, flag_isoc99
1695 ? "!islessequal"
1696 : "!__builtin_islessequal");
1697 goto two_args_fun;
1698
1699 case UNGE_EXPR:
1700 pp_c_ws_string (this, flag_isoc99
1701 ? "!isless"
1702 : "!__builtin_isless");
1703 goto two_args_fun;
1704
1705 case UNEQ_EXPR:
1706 pp_c_ws_string (this, flag_isoc99
1707 ? "!islessgreater"
1708 : "!__builtin_islessgreater");
1709 goto two_args_fun;
1710
1711 case LTGT_EXPR:
1712 pp_c_ws_string (this, flag_isoc99
1713 ? "islessgreater"
1714 : "__builtin_islessgreater");
1715 goto two_args_fun;
1716
1717 case MAX_EXPR:
1718 pp_c_ws_string (this, "max");
1719 goto two_args_fun;
1720
1721 case MIN_EXPR:
1722 pp_c_ws_string (this, "min");
1723 goto two_args_fun;
1724
1725 two_args_fun:
1726 pp_c_left_paren (this);
1727 expression (TREE_OPERAND (e, 0));
1728 pp_separate_with (this, ',');
1729 expression (TREE_OPERAND (e, 1));
1730 pp_c_right_paren (this);
1731 break;
1732
1733 case ABS_EXPR:
1734 pp_c_ws_string (this, "__builtin_abs");
1735 pp_c_left_paren (this);
1736 expression (TREE_OPERAND (e, 0));
1737 pp_c_right_paren (this);
1738 break;
1739
1740 case COMPONENT_REF:
1741 {
1742 tree object = TREE_OPERAND (e, 0);
1743 if (INDIRECT_REF_P (object))
1744 {
1745 postfix_expression (TREE_OPERAND (object, 0));
1746 pp_c_arrow (this);
1747 }
1748 else
1749 {
1750 postfix_expression (object);
1751 pp_c_dot (this);
1752 }
1753 expression (TREE_OPERAND (e, 1));
1754 }
1755 break;
1756
1757 case BIT_FIELD_REF:
1758 {
1759 tree type = TREE_TYPE (e);
1760
1761 type = signed_or_unsigned_type_for (TYPE_UNSIGNED (type), type);
1762 if (type
1763 && tree_int_cst_equal (TYPE_SIZE (type), TREE_OPERAND (e, 1)))
1764 {
1765 HOST_WIDE_INT bitpos = tree_to_shwi (TREE_OPERAND (e, 2));
1766 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (type));
1767 if ((bitpos % size) == 0)
1768 {
1769 pp_c_left_paren (this);
1770 pp_c_left_paren (this);
1771 type_id (type);
1772 pp_c_star (this);
1773 pp_c_right_paren (this);
1774 pp_c_ampersand (this);
1775 expression (TREE_OPERAND (e, 0));
1776 pp_c_right_paren (this);
1777 pp_c_left_bracket (this);
1778 pp_wide_integer (this, bitpos / size);
1779 pp_c_right_bracket (this);
1780 break;
1781 }
1782 }
1783 pp_unsupported_tree (this, e);
1784 }
1785 break;
1786
1787 case MEM_REF:
1788 case TARGET_MEM_REF:
1789 expression (e);
1790 break;
1791
1792 case COMPLEX_CST:
1793 case VECTOR_CST:
1794 pp_c_compound_literal (this, e);
1795 break;
1796
1797 case COMPLEX_EXPR:
1798 pp_c_complex_expr (this, e);
1799 break;
1800
1801 case COMPOUND_LITERAL_EXPR:
1802 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1803 /* Fall through. */
1804 case CONSTRUCTOR:
1805 initializer (e);
1806 break;
1807
1808 case VA_ARG_EXPR:
1809 pp_c_ws_string (this, "__builtin_va_arg");
1810 pp_c_left_paren (this);
1811 assignment_expression (TREE_OPERAND (e, 0));
1812 pp_separate_with (this, ',');
1813 type_id (TREE_TYPE (e));
1814 pp_c_right_paren (this);
1815 break;
1816
1817 case ADDR_EXPR:
1818 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1819 {
1820 id_expression (TREE_OPERAND (e, 0));
1821 break;
1822 }
1823 /* fall through. */
1824
1825 default:
1826 primary_expression (e);
1827 break;
1828 }
1829 }
1830
1831 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1832
1833 void
1834 pp_c_expression_list (c_pretty_printer *pp, tree e)
1835 {
1836 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1837 {
1838 pp->expression (TREE_VALUE (e));
1839 if (TREE_CHAIN (e))
1840 pp_separate_with (pp, ',');
1841 }
1842 }
1843
1844 /* Print out V, which contains the elements of a constructor. */
1845
1846 void
1847 pp_c_constructor_elts (c_pretty_printer *pp, vec<constructor_elt, va_gc> *v)
1848 {
1849 unsigned HOST_WIDE_INT ix;
1850 tree value;
1851
1852 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1853 {
1854 pp->expression (value);
1855 if (ix != vec_safe_length (v) - 1)
1856 pp_separate_with (pp, ',');
1857 }
1858 }
1859
1860 /* Print out an expression-list in parens, as if it were the argument
1861 list to a function. */
1862
1863 void
1864 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1865 {
1866 pp_c_left_paren (pp);
1867 if (t && TREE_CODE (t) == TREE_LIST)
1868 pp_c_expression_list (pp, t);
1869 pp_c_right_paren (pp);
1870 }
1871
1872 /* Try to fold *(type *)&op into op.fld.fld2[1] if possible.
1873 Only used for printing expressions. Should punt if ambiguous
1874 (e.g. in unions). */
1875
1876 static tree
1877 c_fold_indirect_ref_for_warn (location_t loc, tree type, tree op,
1878 offset_int &off)
1879 {
1880 tree optype = TREE_TYPE (op);
1881 if (off == 0)
1882 {
1883 if (lang_hooks.types_compatible_p (optype, type))
1884 return op;
1885 /* *(foo *)&complexfoo => __real__ complexfoo */
1886 else if (TREE_CODE (optype) == COMPLEX_TYPE
1887 && lang_hooks.types_compatible_p (type, TREE_TYPE (optype)))
1888 return build1_loc (loc, REALPART_EXPR, type, op);
1889 }
1890 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
1891 else if (TREE_CODE (optype) == COMPLEX_TYPE
1892 && lang_hooks.types_compatible_p (type, TREE_TYPE (optype))
1893 && tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
1894 {
1895 off = 0;
1896 return build1_loc (loc, IMAGPART_EXPR, type, op);
1897 }
1898 /* ((foo *)&fooarray)[x] => fooarray[x] */
1899 if (TREE_CODE (optype) == ARRAY_TYPE
1900 && TYPE_SIZE_UNIT (TREE_TYPE (optype))
1901 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (optype))) == INTEGER_CST
1902 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
1903 {
1904 tree type_domain = TYPE_DOMAIN (optype);
1905 tree min_val = size_zero_node;
1906 if (type_domain && TYPE_MIN_VALUE (type_domain))
1907 min_val = TYPE_MIN_VALUE (type_domain);
1908 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
1909 offset_int idx = off / el_sz;
1910 offset_int rem = off % el_sz;
1911 if (TREE_CODE (min_val) == INTEGER_CST)
1912 {
1913 tree index
1914 = wide_int_to_tree (sizetype, idx + wi::to_offset (min_val));
1915 op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
1916 NULL_TREE, NULL_TREE);
1917 off = rem;
1918 if (tree ret = c_fold_indirect_ref_for_warn (loc, type, op, off))
1919 return ret;
1920 return op;
1921 }
1922 }
1923 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
1924 else if (TREE_CODE (optype) == RECORD_TYPE)
1925 {
1926 for (tree field = TYPE_FIELDS (optype);
1927 field; field = DECL_CHAIN (field))
1928 if (TREE_CODE (field) == FIELD_DECL
1929 && TREE_TYPE (field) != error_mark_node
1930 && TYPE_SIZE_UNIT (TREE_TYPE (field))
1931 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (field))) == INTEGER_CST)
1932 {
1933 tree pos = byte_position (field);
1934 if (TREE_CODE (pos) != INTEGER_CST)
1935 continue;
1936 offset_int upos = wi::to_offset (pos);
1937 offset_int el_sz
1938 = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (field)));
1939 if (upos <= off && off < upos + el_sz)
1940 {
1941 /* The C++ pretty printers print scope of the FIELD_DECLs,
1942 so punt if it is something that can't be printed. */
1943 if (c_dialect_cxx ())
1944 if (tree scope = get_containing_scope (field))
1945 if (TYPE_P (scope) && TYPE_NAME (scope) == NULL_TREE)
1946 break;
1947 tree cop = build3_loc (loc, COMPONENT_REF, TREE_TYPE (field),
1948 op, field, NULL_TREE);
1949 off = off - upos;
1950 if (tree ret = c_fold_indirect_ref_for_warn (loc, type, cop,
1951 off))
1952 return ret;
1953 return cop;
1954 }
1955 }
1956 }
1957 /* Similarly for unions, but in this case try to be very conservative,
1958 only match if some field has type compatible with type and it is the
1959 only such field. */
1960 else if (TREE_CODE (optype) == UNION_TYPE)
1961 {
1962 tree fld = NULL_TREE;
1963 for (tree field = TYPE_FIELDS (optype);
1964 field; field = DECL_CHAIN (field))
1965 if (TREE_CODE (field) == FIELD_DECL
1966 && TREE_TYPE (field) != error_mark_node
1967 && lang_hooks.types_compatible_p (TREE_TYPE (field), type))
1968 {
1969 if (fld)
1970 return NULL_TREE;
1971 else
1972 fld = field;
1973 }
1974 if (fld)
1975 {
1976 off = 0;
1977 return build3_loc (loc, COMPONENT_REF, TREE_TYPE (fld), op, fld,
1978 NULL_TREE);
1979 }
1980 }
1981
1982 return NULL_TREE;
1983 }
1984
1985 /* Print the MEM_REF expression REF, including its type and offset.
1986 Apply casts as necessary if the type of the access is different
1987 from the type of the accessed object. Produce compact output
1988 designed to include both the element index as well as any
1989 misalignment by preferring
1990 ((int*)((char*)p + 1))[2]
1991 over
1992 *(int*)((char*)p + 9)
1993 The former is more verbose but makes it clearer that the access
1994 to the third element of the array is misaligned by one byte. */
1995
1996 static void
1997 print_mem_ref (c_pretty_printer *pp, tree e)
1998 {
1999 tree arg = TREE_OPERAND (e, 0);
2000
2001 /* The byte offset. Initially equal to the MEM_REF offset, then
2002 adjusted to the remainder of the division by the byte size of
2003 the access. */
2004 offset_int byte_off = wi::to_offset (TREE_OPERAND (e, 1));
2005 /* The result of dividing BYTE_OFF by the size of the access. */
2006 offset_int elt_idx = 0;
2007 /* True to include a cast to char* (for a nonzero final BYTE_OFF). */
2008 bool char_cast = false;
2009 tree op = NULL_TREE;
2010 bool array_ref_only = false;
2011 if (TREE_CODE (arg) == ADDR_EXPR)
2012 {
2013 op = c_fold_indirect_ref_for_warn (EXPR_LOCATION (e), TREE_TYPE (e),
2014 TREE_OPERAND (arg, 0), byte_off);
2015 /* Try to fold it back to component, array ref or their combination,
2016 but print it only if the types and TBAA types are compatible. */
2017 if (op
2018 && byte_off == 0
2019 && lang_hooks.types_compatible_p (TREE_TYPE (e), TREE_TYPE (op))
2020 && (!flag_strict_aliasing
2021 || (get_deref_alias_set (TREE_OPERAND (e, 1))
2022 == get_alias_set (op))))
2023 {
2024 pp->expression (op);
2025 return;
2026 }
2027 if (op == NULL_TREE)
2028 op = TREE_OPERAND (arg, 0);
2029 /* If the types or TBAA types are incompatible, undo the
2030 UNION_TYPE handling from c_fold_indirect_ref_for_warn, and similarly
2031 undo __real__/__imag__ the code below doesn't try to handle. */
2032 if (op != TREE_OPERAND (arg, 0)
2033 && ((TREE_CODE (op) == COMPONENT_REF
2034 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == UNION_TYPE)
2035 || TREE_CODE (op) == REALPART_EXPR
2036 || TREE_CODE (op) == IMAGPART_EXPR))
2037 op = TREE_OPERAND (op, 0);
2038 if (op != TREE_OPERAND (arg, 0))
2039 {
2040 array_ref_only = true;
2041 for (tree ref = op; ref != TREE_OPERAND (arg, 0);
2042 ref = TREE_OPERAND (ref, 0))
2043 if (TREE_CODE (ref) != ARRAY_REF)
2044 {
2045 array_ref_only = false;
2046 break;
2047 }
2048 }
2049 }
2050
2051 tree access_type = TREE_TYPE (e);
2052 tree arg_type = TREE_TYPE (TREE_TYPE (arg));
2053 if (tree access_size = TYPE_SIZE_UNIT (access_type))
2054 if (byte_off != 0
2055 && TREE_CODE (access_size) == INTEGER_CST
2056 && !integer_zerop (access_size))
2057 {
2058 offset_int asize = wi::to_offset (access_size);
2059 elt_idx = byte_off / asize;
2060 byte_off = byte_off % asize;
2061 }
2062
2063 /* True to include a cast to the accessed type. */
2064 const bool access_cast
2065 = ((op && op != TREE_OPERAND (arg, 0))
2066 || VOID_TYPE_P (arg_type)
2067 || !lang_hooks.types_compatible_p (access_type, arg_type));
2068 const bool has_off = byte_off != 0 || (op && op != TREE_OPERAND (arg, 0));
2069
2070 if (has_off && (byte_off != 0 || !array_ref_only))
2071 {
2072 /* When printing the byte offset for a pointer to a type of
2073 a different size than char, include a cast to char* first,
2074 before printing the cast to a pointer to the accessed type. */
2075 tree size = TYPE_SIZE (arg_type);
2076 if (size == NULL_TREE
2077 || TREE_CODE (size) != INTEGER_CST
2078 || wi::to_wide (size) != BITS_PER_UNIT)
2079 char_cast = true;
2080 }
2081
2082 if (elt_idx == 0)
2083 pp_c_star (pp);
2084 else if (access_cast || char_cast)
2085 pp_c_left_paren (pp);
2086
2087 if (access_cast)
2088 {
2089 /* Include a cast to the accessed type if it isn't compatible
2090 with the type of the referenced object (or if the object
2091 is typeless). */
2092 pp_c_left_paren (pp);
2093 pp->type_id (build_pointer_type (access_type));
2094 pp_c_right_paren (pp);
2095 }
2096
2097 if (has_off)
2098 pp_c_left_paren (pp);
2099
2100 if (char_cast)
2101 {
2102 /* Include a cast to char *. */
2103 pp_c_left_paren (pp);
2104 pp->type_id (string_type_node);
2105 pp_c_right_paren (pp);
2106 }
2107
2108 pp->unary_expression (arg);
2109
2110 if (op && op != TREE_OPERAND (arg, 0))
2111 {
2112 auto_vec<tree, 16> refs;
2113 tree ref;
2114 unsigned i;
2115 bool array_refs = true;
2116 for (ref = op; ref != TREE_OPERAND (arg, 0); ref = TREE_OPERAND (ref, 0))
2117 refs.safe_push (ref);
2118 FOR_EACH_VEC_ELT_REVERSE (refs, i, ref)
2119 if (array_refs && TREE_CODE (ref) == ARRAY_REF)
2120 {
2121 pp_c_left_bracket (pp);
2122 pp->expression (TREE_OPERAND (ref, 1));
2123 pp_c_right_bracket (pp);
2124 }
2125 else
2126 {
2127 if (array_refs)
2128 {
2129 array_refs = false;
2130 pp_string (pp, " + offsetof");
2131 pp_c_left_paren (pp);
2132 pp->type_id (TREE_TYPE (TREE_OPERAND (ref, 0)));
2133 pp_comma (pp);
2134 }
2135 else if (TREE_CODE (ref) == COMPONENT_REF)
2136 pp_c_dot (pp);
2137 if (TREE_CODE (ref) == COMPONENT_REF)
2138 pp->expression (TREE_OPERAND (ref, 1));
2139 else
2140 {
2141 pp_c_left_bracket (pp);
2142 pp->expression (TREE_OPERAND (ref, 1));
2143 pp_c_right_bracket (pp);
2144 }
2145 }
2146 if (!array_refs)
2147 pp_c_right_paren (pp);
2148 }
2149
2150 if (byte_off != 0)
2151 {
2152 pp_space (pp);
2153 pp_plus (pp);
2154 pp_space (pp);
2155 tree off = wide_int_to_tree (ssizetype, byte_off);
2156 pp->constant (off);
2157 }
2158
2159 if (has_off)
2160 pp_c_right_paren (pp);
2161
2162 if (elt_idx != 0)
2163 {
2164 if (access_cast || char_cast)
2165 pp_c_right_paren (pp);
2166
2167 pp_c_left_bracket (pp);
2168 tree idx = wide_int_to_tree (ssizetype, elt_idx);
2169 pp->constant (idx);
2170 pp_c_right_bracket (pp);
2171 }
2172 }
2173
2174 /* unary-expression:
2175 postfix-expression
2176 ++ cast-expression
2177 -- cast-expression
2178 unary-operator cast-expression
2179 sizeof unary-expression
2180 sizeof ( type-id )
2181
2182 unary-operator: one of
2183 * & + - ! ~
2184
2185 GNU extensions.
2186 unary-expression:
2187 __alignof__ unary-expression
2188 __alignof__ ( type-id )
2189 __real__ unary-expression
2190 __imag__ unary-expression */
2191
2192 void
2193 c_pretty_printer::unary_expression (tree e)
2194 {
2195 enum tree_code code = TREE_CODE (e);
2196 switch (code)
2197 {
2198 case PREINCREMENT_EXPR:
2199 case PREDECREMENT_EXPR:
2200 pp_string (this, code == PREINCREMENT_EXPR ? "++" : "--");
2201 unary_expression (TREE_OPERAND (e, 0));
2202 break;
2203
2204 case ADDR_EXPR:
2205 case INDIRECT_REF:
2206 case NEGATE_EXPR:
2207 case BIT_NOT_EXPR:
2208 case TRUTH_NOT_EXPR:
2209 case CONJ_EXPR:
2210 /* String literal are used by address. */
2211 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
2212 pp_ampersand (this);
2213 else if (code == INDIRECT_REF)
2214 {
2215 tree type = TREE_TYPE (TREE_OPERAND (e, 0));
2216 if (type && TREE_CODE (type) == REFERENCE_TYPE)
2217 /* Reference decay is implicit, don't print anything. */;
2218 else
2219 pp_c_star (this);
2220 }
2221 else if (code == NEGATE_EXPR)
2222 pp_minus (this);
2223 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
2224 pp_complement (this);
2225 else if (code == TRUTH_NOT_EXPR)
2226 pp_exclamation (this);
2227 pp_c_cast_expression (this, TREE_OPERAND (e, 0));
2228 break;
2229
2230 case MEM_REF:
2231 print_mem_ref (this, e);
2232 break;
2233
2234 case TARGET_MEM_REF:
2235 /* TARGET_MEM_REF can't appear directly from source, but can appear
2236 during late GIMPLE optimizations and through late diagnostic we might
2237 need to support it. Print it as dereferencing of a pointer after
2238 cast to the TARGET_MEM_REF type, with pointer arithmetics on some
2239 pointer to single byte types, so
2240 *(type *)((char *) ptr + step * index + index2) if all the operands
2241 are present and the casts are needed. */
2242 pp_c_star (this);
2243 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TMR_BASE (e)))) == NULL_TREE
2244 || !integer_onep (TYPE_SIZE_UNIT
2245 (TREE_TYPE (TREE_TYPE (TMR_BASE (e))))))
2246 {
2247 if (TYPE_SIZE_UNIT (TREE_TYPE (e))
2248 && integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (e))))
2249 {
2250 pp_c_left_paren (this);
2251 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e)));
2252 }
2253 else
2254 {
2255 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e)));
2256 pp_c_left_paren (this);
2257 pp_c_type_cast (this, build_pointer_type (char_type_node));
2258 }
2259 }
2260 else if (!lang_hooks.types_compatible_p
2261 (TREE_TYPE (e), TREE_TYPE (TREE_TYPE (TMR_BASE (e)))))
2262 {
2263 pp_c_type_cast (this, build_pointer_type (TREE_TYPE (e)));
2264 pp_c_left_paren (this);
2265 }
2266 else
2267 pp_c_left_paren (this);
2268 pp_c_cast_expression (this, TMR_BASE (e));
2269 if (TMR_STEP (e) && TMR_INDEX (e))
2270 {
2271 pp_plus (this);
2272 pp_c_cast_expression (this, TMR_INDEX (e));
2273 pp_c_star (this);
2274 pp_c_cast_expression (this, TMR_STEP (e));
2275 }
2276 if (TMR_INDEX2 (e))
2277 {
2278 pp_plus (this);
2279 pp_c_cast_expression (this, TMR_INDEX2 (e));
2280 }
2281 if (!integer_zerop (TMR_OFFSET (e)))
2282 {
2283 pp_plus (this);
2284 pp_c_integer_constant (this,
2285 fold_convert (ssizetype, TMR_OFFSET (e)));
2286 }
2287 pp_c_right_paren (this);
2288 break;
2289
2290 case REALPART_EXPR:
2291 case IMAGPART_EXPR:
2292 pp_c_ws_string (this, code == REALPART_EXPR ? "__real__" : "__imag__");
2293 pp_c_whitespace (this);
2294 unary_expression (TREE_OPERAND (e, 0));
2295 break;
2296
2297 default:
2298 postfix_expression (e);
2299 break;
2300 }
2301 }
2302
2303 /* cast-expression:
2304 unary-expression
2305 ( type-name ) cast-expression */
2306
2307 void
2308 pp_c_cast_expression (c_pretty_printer *pp, tree e)
2309 {
2310 switch (TREE_CODE (e))
2311 {
2312 case FLOAT_EXPR:
2313 case FIX_TRUNC_EXPR:
2314 CASE_CONVERT:
2315 case VIEW_CONVERT_EXPR:
2316 case EXCESS_PRECISION_EXPR:
2317 if (!location_wrapper_p (e))
2318 pp_c_type_cast (pp, TREE_TYPE (e));
2319 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
2320 break;
2321
2322 default:
2323 pp->unary_expression (e);
2324 }
2325 }
2326
2327 /* multiplicative-expression:
2328 cast-expression
2329 multiplicative-expression * cast-expression
2330 multiplicative-expression / cast-expression
2331 multiplicative-expression % cast-expression */
2332
2333 void
2334 c_pretty_printer::multiplicative_expression (tree e)
2335 {
2336 enum tree_code code = TREE_CODE (e);
2337 switch (code)
2338 {
2339 case MULT_EXPR:
2340 case TRUNC_DIV_EXPR:
2341 case TRUNC_MOD_EXPR:
2342 case EXACT_DIV_EXPR:
2343 case RDIV_EXPR:
2344 multiplicative_expression (TREE_OPERAND (e, 0));
2345 pp_c_whitespace (this);
2346 if (code == MULT_EXPR)
2347 pp_c_star (this);
2348 else if (code != TRUNC_MOD_EXPR)
2349 pp_slash (this);
2350 else
2351 pp_modulo (this);
2352 pp_c_whitespace (this);
2353 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
2354 break;
2355
2356 default:
2357 pp_c_cast_expression (this, e);
2358 break;
2359 }
2360 }
2361
2362 /* additive-expression:
2363 multiplicative-expression
2364 additive-expression + multiplicative-expression
2365 additive-expression - multiplicative-expression */
2366
2367 static void
2368 pp_c_additive_expression (c_pretty_printer *pp, tree e)
2369 {
2370 enum tree_code code = TREE_CODE (e);
2371 switch (code)
2372 {
2373 case POINTER_PLUS_EXPR:
2374 case PLUS_EXPR:
2375 case POINTER_DIFF_EXPR:
2376 case MINUS_EXPR:
2377 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
2378 pp_c_whitespace (pp);
2379 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
2380 pp_plus (pp);
2381 else
2382 pp_minus (pp);
2383 pp_c_whitespace (pp);
2384 {
2385 tree op1 = TREE_OPERAND (e, 1);
2386 if (code == POINTER_PLUS_EXPR
2387 && TREE_CODE (op1) == INTEGER_CST
2388 && tree_int_cst_sign_bit (op1))
2389 /* A pointer minus an integer is represented internally as plus a very
2390 large number, don't expose that to users. */
2391 op1 = convert (ssizetype, op1);
2392 pp->multiplicative_expression (op1);
2393 }
2394 break;
2395
2396 default:
2397 pp->multiplicative_expression (e);
2398 break;
2399 }
2400 }
2401
2402 /* additive-expression:
2403 additive-expression
2404 shift-expression << additive-expression
2405 shift-expression >> additive-expression */
2406
2407 static void
2408 pp_c_shift_expression (c_pretty_printer *pp, tree e)
2409 {
2410 enum tree_code code = TREE_CODE (e);
2411 switch (code)
2412 {
2413 case LSHIFT_EXPR:
2414 case RSHIFT_EXPR:
2415 case LROTATE_EXPR:
2416 case RROTATE_EXPR:
2417 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
2418 pp_c_whitespace (pp);
2419 pp_string (pp, code == LSHIFT_EXPR ? "<<" :
2420 code == RSHIFT_EXPR ? ">>" :
2421 code == LROTATE_EXPR ? "<<<" : ">>>");
2422 pp_c_whitespace (pp);
2423 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
2424 break;
2425
2426 default:
2427 pp_c_additive_expression (pp, e);
2428 }
2429 }
2430
2431 /* relational-expression:
2432 shift-expression
2433 relational-expression < shift-expression
2434 relational-expression > shift-expression
2435 relational-expression <= shift-expression
2436 relational-expression >= shift-expression */
2437
2438 static void
2439 pp_c_relational_expression (c_pretty_printer *pp, tree e)
2440 {
2441 enum tree_code code = TREE_CODE (e);
2442 switch (code)
2443 {
2444 case LT_EXPR:
2445 case GT_EXPR:
2446 case LE_EXPR:
2447 case GE_EXPR:
2448 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
2449 pp_c_whitespace (pp);
2450 if (code == LT_EXPR)
2451 pp_less (pp);
2452 else if (code == GT_EXPR)
2453 pp_greater (pp);
2454 else if (code == LE_EXPR)
2455 pp_less_equal (pp);
2456 else if (code == GE_EXPR)
2457 pp_greater_equal (pp);
2458 pp_c_whitespace (pp);
2459 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
2460 break;
2461
2462 default:
2463 pp_c_shift_expression (pp, e);
2464 break;
2465 }
2466 }
2467
2468 /* equality-expression:
2469 relational-expression
2470 equality-expression == relational-expression
2471 equality-equality != relational-expression */
2472
2473 static void
2474 pp_c_equality_expression (c_pretty_printer *pp, tree e)
2475 {
2476 enum tree_code code = TREE_CODE (e);
2477 switch (code)
2478 {
2479 case EQ_EXPR:
2480 case NE_EXPR:
2481 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
2482 pp_c_whitespace (pp);
2483 pp_string (pp, code == EQ_EXPR ? "==" : "!=");
2484 pp_c_whitespace (pp);
2485 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
2486 break;
2487
2488 default:
2489 pp_c_relational_expression (pp, e);
2490 break;
2491 }
2492 }
2493
2494 /* AND-expression:
2495 equality-expression
2496 AND-expression & equality-equality */
2497
2498 static void
2499 pp_c_and_expression (c_pretty_printer *pp, tree e)
2500 {
2501 if (TREE_CODE (e) == BIT_AND_EXPR)
2502 {
2503 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
2504 pp_c_whitespace (pp);
2505 pp_ampersand (pp);
2506 pp_c_whitespace (pp);
2507 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
2508 }
2509 else
2510 pp_c_equality_expression (pp, e);
2511 }
2512
2513 /* exclusive-OR-expression:
2514 AND-expression
2515 exclusive-OR-expression ^ AND-expression */
2516
2517 static void
2518 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
2519 {
2520 if (TREE_CODE (e) == BIT_XOR_EXPR
2521 || TREE_CODE (e) == TRUTH_XOR_EXPR)
2522 {
2523 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2524 if (TREE_CODE (e) == BIT_XOR_EXPR)
2525 pp_c_maybe_whitespace (pp);
2526 else
2527 pp_c_whitespace (pp);
2528 pp_carret (pp);
2529 pp_c_whitespace (pp);
2530 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
2531 }
2532 else
2533 pp_c_and_expression (pp, e);
2534 }
2535
2536 /* inclusive-OR-expression:
2537 exclusive-OR-expression
2538 inclusive-OR-expression | exclusive-OR-expression */
2539
2540 static void
2541 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
2542 {
2543 if (TREE_CODE (e) == BIT_IOR_EXPR)
2544 {
2545 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
2546 pp_c_whitespace (pp);
2547 pp_bar (pp);
2548 pp_c_whitespace (pp);
2549 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
2550 }
2551 else
2552 pp_c_exclusive_or_expression (pp, e);
2553 }
2554
2555 /* logical-AND-expression:
2556 inclusive-OR-expression
2557 logical-AND-expression && inclusive-OR-expression */
2558
2559 static void
2560 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
2561 {
2562 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
2563 || TREE_CODE (e) == TRUTH_AND_EXPR)
2564 {
2565 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
2566 pp_c_whitespace (pp);
2567 pp_ampersand_ampersand (pp);
2568 pp_c_whitespace (pp);
2569 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
2570 }
2571 else
2572 pp_c_inclusive_or_expression (pp, e);
2573 }
2574
2575 /* logical-OR-expression:
2576 logical-AND-expression
2577 logical-OR-expression || logical-AND-expression */
2578
2579 void
2580 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
2581 {
2582 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
2583 || TREE_CODE (e) == TRUTH_OR_EXPR)
2584 {
2585 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
2586 pp_c_whitespace (pp);
2587 pp_bar_bar (pp);
2588 pp_c_whitespace (pp);
2589 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
2590 }
2591 else
2592 pp_c_logical_and_expression (pp, e);
2593 }
2594
2595 /* conditional-expression:
2596 logical-OR-expression
2597 logical-OR-expression ? expression : conditional-expression */
2598
2599 void
2600 c_pretty_printer::conditional_expression (tree e)
2601 {
2602 if (TREE_CODE (e) == COND_EXPR)
2603 {
2604 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
2605 pp_c_whitespace (this);
2606 pp_question (this);
2607 pp_c_whitespace (this);
2608 expression (TREE_OPERAND (e, 1));
2609 pp_c_whitespace (this);
2610 pp_colon (this);
2611 pp_c_whitespace (this);
2612 conditional_expression (TREE_OPERAND (e, 2));
2613 }
2614 else
2615 pp_c_logical_or_expression (this, e);
2616 }
2617
2618
2619 /* assignment-expression:
2620 conditional-expression
2621 unary-expression assignment-operator assignment-expression
2622
2623 assignment-expression: one of
2624 = *= /= %= += -= >>= <<= &= ^= |= */
2625
2626 void
2627 c_pretty_printer::assignment_expression (tree e)
2628 {
2629 if (TREE_CODE (e) == MODIFY_EXPR
2630 || TREE_CODE (e) == INIT_EXPR)
2631 {
2632 unary_expression (TREE_OPERAND (e, 0));
2633 pp_c_whitespace (this);
2634 pp_equal (this);
2635 pp_space (this);
2636 expression (TREE_OPERAND (e, 1));
2637 }
2638 else
2639 conditional_expression (e);
2640 }
2641
2642 /* expression:
2643 assignment-expression
2644 expression , assignment-expression
2645
2646 Implementation note: instead of going through the usual recursion
2647 chain, I take the liberty of dispatching nodes to the appropriate
2648 functions. This makes some redundancy, but it worths it. That also
2649 prevents a possible infinite recursion between primary_expression ()
2650 and expression (). */
2651
2652 void
2653 c_pretty_printer::expression (tree e)
2654 {
2655 switch (TREE_CODE (e))
2656 {
2657 case VOID_CST:
2658 pp_c_void_constant (this);
2659 break;
2660
2661 case INTEGER_CST:
2662 pp_c_integer_constant (this, e);
2663 break;
2664
2665 case REAL_CST:
2666 pp_c_floating_constant (this, e);
2667 break;
2668
2669 case FIXED_CST:
2670 pp_c_fixed_constant (this, e);
2671 break;
2672
2673 case STRING_CST:
2674 pp_c_string_literal (this, e);
2675 break;
2676
2677 case IDENTIFIER_NODE:
2678 case FUNCTION_DECL:
2679 case VAR_DECL:
2680 case CONST_DECL:
2681 case PARM_DECL:
2682 case RESULT_DECL:
2683 case FIELD_DECL:
2684 case LABEL_DECL:
2685 case ERROR_MARK:
2686 primary_expression (e);
2687 break;
2688
2689 case SSA_NAME:
2690 if (SSA_NAME_VAR (e)
2691 && !DECL_ARTIFICIAL (SSA_NAME_VAR (e)))
2692 expression (SSA_NAME_VAR (e));
2693 else
2694 translate_string ("<unknown>");
2695 break;
2696
2697 case POSTINCREMENT_EXPR:
2698 case POSTDECREMENT_EXPR:
2699 case ARRAY_REF:
2700 case OMP_ARRAY_SECTION:
2701 case CALL_EXPR:
2702 case COMPONENT_REF:
2703 case BIT_FIELD_REF:
2704 case COMPLEX_CST:
2705 case COMPLEX_EXPR:
2706 case VECTOR_CST:
2707 case ORDERED_EXPR:
2708 case UNORDERED_EXPR:
2709 case LTGT_EXPR:
2710 case UNEQ_EXPR:
2711 case UNLE_EXPR:
2712 case UNLT_EXPR:
2713 case UNGE_EXPR:
2714 case UNGT_EXPR:
2715 case MAX_EXPR:
2716 case MIN_EXPR:
2717 case ABS_EXPR:
2718 case CONSTRUCTOR:
2719 case COMPOUND_LITERAL_EXPR:
2720 case VA_ARG_EXPR:
2721 postfix_expression (e);
2722 break;
2723
2724 case CONJ_EXPR:
2725 case ADDR_EXPR:
2726 case INDIRECT_REF:
2727 case MEM_REF:
2728 case TARGET_MEM_REF:
2729 case NEGATE_EXPR:
2730 case BIT_NOT_EXPR:
2731 case TRUTH_NOT_EXPR:
2732 case PREINCREMENT_EXPR:
2733 case PREDECREMENT_EXPR:
2734 case REALPART_EXPR:
2735 case IMAGPART_EXPR:
2736 unary_expression (e);
2737 break;
2738
2739 case FLOAT_EXPR:
2740 case FIX_TRUNC_EXPR:
2741 CASE_CONVERT:
2742 case VIEW_CONVERT_EXPR:
2743 case EXCESS_PRECISION_EXPR:
2744 pp_c_cast_expression (this, e);
2745 break;
2746
2747 case MULT_EXPR:
2748 case TRUNC_MOD_EXPR:
2749 case TRUNC_DIV_EXPR:
2750 case EXACT_DIV_EXPR:
2751 case RDIV_EXPR:
2752 multiplicative_expression (e);
2753 break;
2754
2755 case LSHIFT_EXPR:
2756 case RSHIFT_EXPR:
2757 case LROTATE_EXPR:
2758 case RROTATE_EXPR:
2759 pp_c_shift_expression (this, e);
2760 break;
2761
2762 case LT_EXPR:
2763 case GT_EXPR:
2764 case LE_EXPR:
2765 case GE_EXPR:
2766 pp_c_relational_expression (this, e);
2767 break;
2768
2769 case BIT_AND_EXPR:
2770 pp_c_and_expression (this, e);
2771 break;
2772
2773 case BIT_XOR_EXPR:
2774 case TRUTH_XOR_EXPR:
2775 pp_c_exclusive_or_expression (this, e);
2776 break;
2777
2778 case BIT_IOR_EXPR:
2779 pp_c_inclusive_or_expression (this, e);
2780 break;
2781
2782 case TRUTH_ANDIF_EXPR:
2783 case TRUTH_AND_EXPR:
2784 pp_c_logical_and_expression (this, e);
2785 break;
2786
2787 case TRUTH_ORIF_EXPR:
2788 case TRUTH_OR_EXPR:
2789 pp_c_logical_or_expression (this, e);
2790 break;
2791
2792 case EQ_EXPR:
2793 case NE_EXPR:
2794 pp_c_equality_expression (this, e);
2795 break;
2796
2797 case COND_EXPR:
2798 conditional_expression (e);
2799 break;
2800
2801 case POINTER_PLUS_EXPR:
2802 case PLUS_EXPR:
2803 case POINTER_DIFF_EXPR:
2804 case MINUS_EXPR:
2805 pp_c_additive_expression (this, e);
2806 break;
2807
2808 case MODIFY_EXPR:
2809 case INIT_EXPR:
2810 assignment_expression (e);
2811 break;
2812
2813 case COMPOUND_EXPR:
2814 pp_c_left_paren (this);
2815 expression (TREE_OPERAND (e, 0));
2816 pp_separate_with (this, ',');
2817 assignment_expression (TREE_OPERAND (e, 1));
2818 pp_c_right_paren (this);
2819 break;
2820
2821 case NON_LVALUE_EXPR:
2822 case SAVE_EXPR:
2823 expression (TREE_OPERAND (e, 0));
2824 break;
2825
2826 case TARGET_EXPR:
2827 postfix_expression (TARGET_EXPR_INITIAL (e));
2828 break;
2829
2830 case BIND_EXPR:
2831 case GOTO_EXPR:
2832 /* We don't yet have a way of dumping statements in a
2833 human-readable format. */
2834 pp_string (this, "({...})");
2835 break;
2836
2837 case C_MAYBE_CONST_EXPR:
2838 expression (C_MAYBE_CONST_EXPR_EXPR (e));
2839 break;
2840
2841 default:
2842 pp_unsupported_tree (this, e);
2843 break;
2844 }
2845 }
2846
2847
2848
2849 /* Statements. */
2850
2851 void
2852 c_pretty_printer::statement (tree t)
2853 {
2854 if (t == NULL)
2855 return;
2856
2857 switch (TREE_CODE (t))
2858 {
2859
2860 case SWITCH_STMT:
2861 if (dump_flags != TDF_NONE)
2862 internal_error ("dump flags not handled here");
2863
2864 pp_c_ws_string (this, "switch");
2865 pp_space (this);
2866 pp_c_left_paren (this);
2867 expression (SWITCH_STMT_COND (t));
2868 pp_c_right_paren (this);
2869 pp_indentation (this) += 3;
2870 pp_needs_newline (this) = true;
2871 statement (SWITCH_STMT_BODY (t));
2872 pp_newline_and_indent (this, -3);
2873 break;
2874
2875 /* iteration-statement:
2876 while ( expression ) statement
2877 do statement while ( expression ) ;
2878 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
2879 for ( declaration expression(opt) ; expression(opt) ) statement */
2880 case WHILE_STMT:
2881 if (dump_flags != TDF_NONE)
2882 internal_error ("dump flags not handled here");
2883
2884 pp_c_ws_string (this, "while");
2885 pp_space (this);
2886 pp_c_left_paren (this);
2887 expression (WHILE_COND (t));
2888 pp_c_right_paren (this);
2889 pp_newline_and_indent (this, 3);
2890 statement (WHILE_BODY (t));
2891 pp_indentation (this) -= 3;
2892 pp_needs_newline (this) = true;
2893 break;
2894
2895 case DO_STMT:
2896 if (dump_flags != TDF_NONE)
2897 internal_error ("dump flags not handled here");
2898
2899 pp_c_ws_string (this, "do");
2900 pp_newline_and_indent (this, 3);
2901 statement (DO_BODY (t));
2902 pp_newline_and_indent (this, -3);
2903 pp_c_ws_string (this, "while");
2904 pp_space (this);
2905 pp_c_left_paren (this);
2906 expression (DO_COND (t));
2907 pp_c_right_paren (this);
2908 pp_c_semicolon (this);
2909 pp_needs_newline (this) = true;
2910 break;
2911
2912 case FOR_STMT:
2913 if (dump_flags != TDF_NONE)
2914 internal_error ("dump flags not handled here");
2915
2916 pp_c_ws_string (this, "for");
2917 pp_space (this);
2918 pp_c_left_paren (this);
2919 if (FOR_INIT_STMT (t))
2920 statement (FOR_INIT_STMT (t));
2921 else
2922 pp_c_semicolon (this);
2923 pp_needs_newline (this) = false;
2924 pp_c_whitespace (this);
2925 if (FOR_COND (t))
2926 expression (FOR_COND (t));
2927 pp_c_semicolon (this);
2928 pp_needs_newline (this) = false;
2929 pp_c_whitespace (this);
2930 if (FOR_EXPR (t))
2931 expression (FOR_EXPR (t));
2932 pp_c_right_paren (this);
2933 pp_newline_and_indent (this, 3);
2934 statement (FOR_BODY (t));
2935 pp_indentation (this) -= 3;
2936 pp_needs_newline (this) = true;
2937 break;
2938
2939 /* jump-statement:
2940 goto identifier;
2941 continue ;
2942 return expression(opt) ; */
2943 case BREAK_STMT:
2944 if (dump_flags != TDF_NONE)
2945 internal_error ("dump flags not handled here");
2946
2947 pp_string (this, "break");
2948 if (BREAK_NAME (t))
2949 {
2950 pp_space (this);
2951 pp_c_tree_decl_identifier (this, BREAK_NAME (t));
2952 }
2953 pp_c_semicolon (this);
2954 pp_needs_newline (this) = true;
2955 break;
2956
2957 case CONTINUE_STMT:
2958 if (dump_flags != TDF_NONE)
2959 internal_error ("dump flags not handled here");
2960
2961 pp_string (this, "continue");
2962 if (CONTINUE_NAME (t))
2963 {
2964 pp_space (this);
2965 pp_c_tree_decl_identifier (this, CONTINUE_NAME (t));
2966 }
2967 pp_c_semicolon (this);
2968 pp_needs_newline (this) = true;
2969 break;
2970
2971 default:
2972 if (pp_needs_newline (this))
2973 pp_newline_and_indent (this, 0);
2974 dump_generic_node (this, t, pp_indentation (this), dump_flags, true);
2975 }
2976 }
2977
2978
2979 /* Initialize the PRETTY-PRINTER for handling C codes. */
2980
2981 c_pretty_printer::c_pretty_printer (dump_flags_t dump_flags)
2982 : pretty_printer (),
2983 dump_flags (dump_flags),
2984 offset_list (),
2985 flags ()
2986 {
2987 type_specifier_seq = pp_c_specifier_qualifier_list;
2988 ptr_operator = pp_c_pointer;
2989 parameter_list = pp_c_parameter_type_list;
2990 }
2991
2992 /* c_pretty_printer's implementation of pretty_printer::clone vfunc. */
2993
2994 std::unique_ptr<pretty_printer>
2995 c_pretty_printer::clone () const
2996 {
2997 return ::make_unique<c_pretty_printer> (*this);
2998 }
2999
3000 /* Print the tree T in full, on file FILE. */
3001
3002 void
3003 print_c_tree (FILE *file, tree t, dump_flags_t dump_flags)
3004 {
3005 c_pretty_printer pp (dump_flags);
3006
3007 pp_needs_newline (&pp) = true;
3008 pp.set_output_stream (file);
3009 pp.statement (t);
3010 pp_newline_and_flush (&pp);
3011 }
3012
3013 /* Print the tree T in full, on stderr. */
3014
3015 DEBUG_FUNCTION void
3016 debug_c_tree (tree t)
3017 {
3018 print_c_tree (stderr, t, TDF_NONE);
3019 fputc ('\n', stderr);
3020 }
3021
3022 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
3023 up of T's memory address. */
3024
3025 void
3026 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
3027 {
3028 const char *name;
3029
3030 gcc_assert (DECL_P (t));
3031
3032 if (DECL_NAME (t))
3033 {
3034 const char *dot;
3035 name = IDENTIFIER_POINTER (DECL_NAME (t));
3036 if (DECL_ARTIFICIAL (t) && (dot = strchr (name, '.')))
3037 {
3038 /* Print the name without the . suffix (such as in VLAs and
3039 callee-copied parameters). */
3040 size_t size = dot - name;
3041 char *ident = XALLOCAVEC (char, size + 1);
3042 memcpy (ident, name, size);
3043 ident[size] = '\0';
3044 name = ident;
3045 }
3046 }
3047 else
3048 {
3049 static char xname[8];
3050 sprintf (xname, "<U%4hx>", ((unsigned short) ((uintptr_t) (t)
3051 & 0xffff)));
3052 name = xname;
3053 }
3054
3055 pp_c_identifier (pp, name);
3056 }
3057
3058 #if CHECKING_P
3059
3060 namespace selftest {
3061
3062 /* Selftests for pretty-printing trees. */
3063
3064 /* Verify that EXPR printed by c_pretty_printer is EXPECTED, using
3065 LOC as the effective location for any failures. */
3066
3067 static void
3068 assert_c_pretty_printer_output (const location &loc, const char *expected,
3069 tree expr)
3070 {
3071 c_pretty_printer pp;
3072 pp.expression (expr);
3073 ASSERT_STREQ_AT (loc, expected, pp_formatted_text (&pp));
3074 }
3075
3076 /* Helper function for calling assert_c_pretty_printer_output.
3077 This is to avoid having to write SELFTEST_LOCATION. */
3078
3079 #define ASSERT_C_PRETTY_PRINTER_OUTPUT(EXPECTED, EXPR) \
3080 SELFTEST_BEGIN_STMT \
3081 assert_c_pretty_printer_output ((SELFTEST_LOCATION), \
3082 (EXPECTED), \
3083 (EXPR)); \
3084 SELFTEST_END_STMT
3085
3086 /* Verify that location wrappers don't show up in pretty-printed output. */
3087
3088 static void
3089 test_location_wrappers ()
3090 {
3091 /* VAR_DECL. */
3092 tree id = get_identifier ("foo");
3093 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, id,
3094 integer_type_node);
3095 tree wrapped_decl = maybe_wrap_with_location (decl, BUILTINS_LOCATION);
3096 ASSERT_NE (wrapped_decl, decl);
3097 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", decl);
3098 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", wrapped_decl);
3099
3100 /* INTEGER_CST. */
3101 tree int_cst = build_int_cst (integer_type_node, 42);
3102 tree wrapped_cst = maybe_wrap_with_location (int_cst, BUILTINS_LOCATION);
3103 ASSERT_NE (wrapped_cst, int_cst);
3104 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", int_cst);
3105 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", wrapped_cst);
3106 }
3107
3108 /* Run all of the selftests within this file. */
3109
3110 void
3111 c_pretty_print_cc_tests ()
3112 {
3113 test_location_wrappers ();
3114 }
3115
3116 } // namespace selftest
3117
3118 #endif /* CHECKING_P */