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