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