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