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