* gcc.dg/const-elim-1.c: xfail for xtensa.
[official-gcc.git] / gcc / c-pretty-print.c
blobacb70cd327d180f4fa6f2b3e38c60d0a9231bd0f
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>
5 This file is part of GCC.
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.
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.
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. */
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"
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. */
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)
44 #define pp_c_left_bracket(PP) \
45 do { \
46 pp_left_bracket (PP); \
47 pp_base (PP)->padding = pp_none; \
48 } while (0)
50 #define pp_c_right_bracket(PP) \
51 do { \
52 pp_right_bracket (PP); \
53 pp_base (PP)->padding = pp_none; \
54 } while (0)
56 #define pp_c_star(PP) \
57 do { \
58 pp_star (PP); \
59 pp_base (PP)->padding = pp_none; \
60 } while (0)
62 /* literal */
63 static void pp_c_char (c_pretty_printer *, int);
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);
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);
81 /* declarations. */
84 /* Helper functions. */
86 void
87 pp_c_whitespace (c_pretty_printer *pp)
89 pp_space (pp);
90 pp_base (pp)->padding = pp_none;
93 void
94 pp_c_left_paren (c_pretty_printer *pp)
96 pp_left_paren (pp);
97 pp_base (pp)->padding = pp_none;
100 void
101 pp_c_right_paren (c_pretty_printer *pp)
103 pp_right_paren (pp);
104 pp_base (pp)->padding = pp_none;
107 void
108 pp_c_left_brace (c_pretty_printer *pp)
110 pp_left_brace (pp);
111 pp_base (pp)->padding = pp_none;
114 void
115 pp_c_right_brace (c_pretty_printer *pp)
117 pp_right_brace (pp);
118 pp_base (pp)->padding = pp_none;
121 void
122 pp_c_dot (c_pretty_printer *pp)
124 pp_dot (pp);
125 pp_base (pp)->padding = pp_none;
128 void
129 pp_c_ampersand (c_pretty_printer *pp)
131 pp_ampersand (pp);
132 pp_base (pp)->padding = pp_none;
135 void
136 pp_c_arrow (c_pretty_printer *pp)
138 pp_arrow (pp);
139 pp_base (pp)->padding = pp_none;
142 void
143 pp_c_semicolon(c_pretty_printer *pp)
145 pp_semicolon (pp);
146 pp_base (pp)->padding = pp_none;
149 static void
150 pp_c_cv_qualifier (c_pretty_printer *pp, const char *cv)
152 const char *p = pp_last_position_in_text (pp);
153 if (p != NULL && *p == '*')
154 pp_c_whitespace (pp);
155 pp_c_identifier (pp, cv);
158 /* Pretty-print T using the type-cast notation '( type-name )'. */
160 static void
161 pp_c_type_cast (c_pretty_printer *pp, tree t)
163 pp_c_left_paren (pp);
164 pp_type_id (pp, t);
165 pp_c_right_paren (pp);
168 void
169 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
171 if (POINTER_TYPE_P (t))
173 tree pointee = strip_pointer_operator (TREE_TYPE (t));
174 if (TREE_CODE (pointee) != ARRAY_TYPE
175 && TREE_CODE (pointee) != FUNCTION_TYPE)
176 pp_c_whitespace (pp);
181 /* Declarations. */
183 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
184 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
185 of its type. Take care of possible extensions.
187 type-qualifier-list:
188 type-qualifier
189 type-qualifier-list type-qualifier
191 type-qualifier:
192 const
193 restrict -- C99
194 __restrict__ -- GNU C
195 volatile */
197 void
198 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
200 int qualifiers;
202 if (!TYPE_P (t))
203 t = TREE_TYPE (t);
205 qualifiers = TYPE_QUALS (t);
206 if (qualifiers & TYPE_QUAL_CONST)
207 pp_c_cv_qualifier (pp, "const");
208 if (qualifiers & TYPE_QUAL_VOLATILE)
209 pp_c_cv_qualifier (pp, "volatile");
210 if (qualifiers & TYPE_QUAL_RESTRICT)
211 pp_c_cv_qualifier (pp, flag_isoc99 ? "restrict" : "__restrict__");
214 /* pointer:
215 * type-qualifier-list(opt)
216 * type-qualifier-list(opt) pointer */
218 static void
219 pp_c_pointer (c_pretty_printer *pp, tree t)
221 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
222 t = TREE_TYPE (t);
223 switch (TREE_CODE (t))
225 case POINTER_TYPE:
226 /* It is easier to handle C++ reference types here. */
227 case REFERENCE_TYPE:
228 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
229 pp_c_pointer (pp, TREE_TYPE (t));
230 if (TREE_CODE (t) == POINTER_TYPE)
231 pp_c_star (pp);
232 else
233 pp_c_ampersand (pp);
234 pp_c_type_qualifier_list (pp, t);
235 break;
237 default:
238 pp_unsupported_tree (pp, t);
242 /* type-specifier:
243 void
244 char
245 short
247 long
248 float
249 double
250 signed
251 unsigned
252 _Bool -- C99
253 _Complex -- C99
254 _Imaginary -- C99
255 struct-or-union-specifier
256 enum-specifier
257 typedef-name.
259 GNU extensions.
260 simple-type-specifier:
261 __complex__
262 __vector__ */
264 void
265 pp_c_type_specifier (c_pretty_printer *pp, tree t)
267 const enum tree_code code = TREE_CODE (t);
268 switch (code)
270 case ERROR_MARK:
271 pp_c_identifier (pp, "<type-error>");
272 break;
274 case IDENTIFIER_NODE:
275 pp_c_tree_identifier (pp, t);
276 break;
278 case VOID_TYPE:
279 case BOOLEAN_TYPE:
280 case CHAR_TYPE:
281 case INTEGER_TYPE:
282 case REAL_TYPE:
283 if (TYPE_NAME (t))
284 t = TYPE_NAME (t);
285 else
286 t = c_common_type_for_mode (TYPE_MODE (t), TREE_UNSIGNED (t));
287 pp_c_type_specifier (pp, t);
288 break;
290 case TYPE_DECL:
291 if (DECL_NAME (t))
292 pp_id_expression (pp, t);
293 else
294 pp_c_identifier (pp, "<typedef-error>");
295 break;
297 case UNION_TYPE:
298 case RECORD_TYPE:
299 case ENUMERAL_TYPE:
300 if (code == UNION_TYPE)
301 pp_c_identifier (pp, "union");
302 else if (code == RECORD_TYPE)
303 pp_c_identifier (pp, "struct");
304 else if (code == ENUMERAL_TYPE)
305 pp_c_identifier (pp, "enum");
306 else
307 pp_c_identifier (pp, "<tag-error>");
309 if (TYPE_NAME (t))
310 pp_id_expression (pp, TYPE_NAME (t));
311 else
312 pp_c_identifier (pp, "<anonymous>");
313 break;
315 default:
316 pp_unsupported_tree (pp, t);
317 break;
321 /* specifier-qualifier-list:
322 type-specifier specifier-qualifier-list-opt
323 type-qualifier specifier-qualifier-list-opt
326 Implementation note: Because of the non-linearities in array or
327 function declarations, this routine prints not just the
328 specifier-qualifier-list of such entities or types of such entities,
329 but also the 'pointer' production part of their declarators. The
330 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
332 void
333 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
335 const enum tree_code code = TREE_CODE (t);
337 if (TREE_CODE (t) != POINTER_TYPE)
338 pp_c_type_qualifier_list (pp, t);
339 switch (code)
341 case REFERENCE_TYPE:
342 case POINTER_TYPE:
344 /* Get the types-specifier of this type. */
345 tree pointee = strip_pointer_operator (TREE_TYPE (t));
346 pp_c_specifier_qualifier_list (pp, pointee);
347 if (TREE_CODE (pointee) == ARRAY_TYPE
348 || TREE_CODE (pointee) == FUNCTION_TYPE)
350 pp_c_whitespace (pp);
351 pp_c_left_paren (pp);
353 pp_ptr_operator (pp, t);
355 break;
357 case FUNCTION_TYPE:
358 case ARRAY_TYPE:
359 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
360 break;
362 case VECTOR_TYPE:
363 case COMPLEX_TYPE:
364 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
365 if (code == COMPLEX_TYPE)
366 pp_c_identifier (pp, flag_isoc99 ? "_Complex" : "__complex__");
367 else if (code == VECTOR_TYPE)
368 pp_c_identifier (pp, "__vector__");
369 break;
371 default:
372 pp_simple_type_specifier (pp, t);
373 break;
377 /* parameter-type-list:
378 parameter-list
379 parameter-list , ...
381 parameter-list:
382 parameter-declaration
383 parameter-list , parameter-declaration
385 parameter-declaration:
386 declaration-specifiers declarator
387 declaration-specifiers abstract-declarator(opt) */
389 void
390 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
392 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
393 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
394 pp_c_left_paren (pp);
395 if (parms == void_list_node)
396 pp_c_identifier (pp, "void");
397 else
399 bool first = true;
400 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
402 if (!first)
403 pp_separate_with (pp, ',');
404 first = false;
405 pp_declaration_specifiers
406 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
407 if (want_parm_decl)
408 pp_declarator (pp, parms);
409 else
410 pp_abstract_declarator (pp, TREE_VALUE (parms));
413 pp_c_right_paren (pp);
416 /* abstract-declarator:
417 pointer
418 pointer(opt) direct-abstract-declarator */
420 static void
421 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
423 if (TREE_CODE (t) == POINTER_TYPE)
425 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
426 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
427 pp_c_right_paren (pp);
428 t = TREE_TYPE (t);
431 pp_direct_abstract_declarator (pp, t);
434 /* direct-abstract-declarator:
435 ( abstract-declarator )
436 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
437 direct-abstract-declarator(opt) [ * ]
438 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
440 void
441 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
443 switch (TREE_CODE (t))
445 case POINTER_TYPE:
446 pp_abstract_declarator (pp, t);
447 break;
449 case FUNCTION_TYPE:
450 pp_c_parameter_type_list (pp, t);
451 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
452 break;
454 case ARRAY_TYPE:
455 pp_c_left_bracket (pp);
456 if (TYPE_DOMAIN (t))
457 pp_expression (pp, TYPE_MAX_VALUE (TYPE_DOMAIN (t)));
458 pp_c_right_bracket (pp);
459 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
460 break;
462 case IDENTIFIER_NODE:
463 case VOID_TYPE:
464 case BOOLEAN_TYPE:
465 case INTEGER_TYPE:
466 case REAL_TYPE:
467 case ENUMERAL_TYPE:
468 case RECORD_TYPE:
469 case UNION_TYPE:
470 case VECTOR_TYPE:
471 case COMPLEX_TYPE:
472 case TYPE_DECL:
473 break;
475 default:
476 pp_unsupported_tree (pp, t);
477 break;
481 /* type-name:
482 specifier-qualifier-list abstract-declarator(opt) */
484 void
485 pp_c_type_id (c_pretty_printer *pp, tree t)
487 pp_c_specifier_qualifier_list (pp, t);
488 pp_abstract_declarator (pp, t);
491 /* storage-class-specifier:
492 typedef
493 extern
494 static
495 auto
496 register */
498 void
499 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
501 if (TREE_CODE (t) == TYPE_DECL)
502 pp_c_identifier (pp, "typedef");
503 else if (DECL_P (t))
505 if (DECL_REGISTER (t))
506 pp_c_identifier (pp, "register");
507 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
508 pp_c_identifier (pp, "static");
512 /* function-specifier:
513 inline */
515 void
516 pp_c_function_specifier (c_pretty_printer *pp, tree t)
518 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
519 pp_c_identifier (pp, "inline");
522 /* declaration-specifiers:
523 storage-class-specifier declaration-specifiers(opt)
524 type-specifier declaration-specifiers(opt)
525 type-qualifier declaration-specifiers(opt)
526 function-specifier declaration-specifiers(opt) */
528 void
529 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
531 pp_storage_class_specifier (pp, t);
532 pp_function_specifier (pp, t);
533 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
536 /* direct-declarator
537 identifier
538 ( declarator )
539 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
540 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
541 direct-declarator [ type-qualifier-list static assignment-expression ]
542 direct-declarator [ type-qualifier-list * ]
543 direct-declarator ( parameter-type-list )
544 direct-declarator ( identifier-list(opt) ) */
546 void
547 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
549 switch (TREE_CODE (t))
551 case VAR_DECL:
552 case PARM_DECL:
553 case TYPE_DECL:
554 case FIELD_DECL:
555 case LABEL_DECL:
556 if (DECL_NAME (t))
558 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
559 pp_c_tree_identifier (pp, DECL_NAME (t));
561 case ARRAY_TYPE:
562 case POINTER_TYPE:
563 pp_abstract_declarator (pp, TREE_TYPE (t));
564 break;
566 case FUNCTION_TYPE:
567 pp_parameter_list (pp, t);
568 pp_abstract_declarator (pp, TREE_TYPE (t));
569 break;
571 case FUNCTION_DECL:
572 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
573 pp_c_tree_identifier (pp, DECL_NAME (t));
574 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
575 pp_abstract_declarator (pp, TREE_TYPE (t));
576 else
578 pp_parameter_list (pp, t);
579 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
581 break;
583 case INTEGER_TYPE:
584 case REAL_TYPE:
585 case ENUMERAL_TYPE:
586 case UNION_TYPE:
587 case RECORD_TYPE:
588 break;
590 default:
591 pp_unsupported_tree (pp, t);
592 break;
597 /* declarator:
598 pointer(opt) direct-declarator */
600 void
601 pp_c_declarator (c_pretty_printer *pp, tree t)
603 switch (TREE_CODE (t))
605 case INTEGER_TYPE:
606 case REAL_TYPE:
607 case ENUMERAL_TYPE:
608 case UNION_TYPE:
609 case RECORD_TYPE:
610 break;
612 case VAR_DECL:
613 case PARM_DECL:
614 case FIELD_DECL:
615 case ARRAY_TYPE:
616 case FUNCTION_TYPE:
617 case FUNCTION_DECL:
618 case TYPE_DECL:
619 pp_direct_declarator (pp, t);
620 break;
623 default:
624 pp_unsupported_tree (pp, t);
625 break;
629 /* declaration:
630 declaration-specifiers init-declarator-list(opt) ; */
632 void
633 pp_c_declaration (c_pretty_printer *pp, tree t)
635 pp_declaration_specifiers (pp, t);
636 pp_c_init_declarator (pp, t);
639 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
641 void
642 pp_c_attributes (c_pretty_printer *pp, tree attributes)
644 if (attributes == NULL_TREE)
645 return;
647 pp_c_identifier (pp, "__attribute__");
648 pp_c_left_paren (pp);
649 pp_c_left_paren (pp);
650 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
652 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
653 if (TREE_VALUE (attributes))
654 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
656 if (TREE_CHAIN (attributes))
657 pp_separate_with (pp, ',');
659 pp_c_right_paren (pp);
660 pp_c_right_paren (pp);
663 /* function-definition:
664 declaration-specifiers declarator compound-statement */
666 void
667 pp_c_function_definition (c_pretty_printer *pp, tree t)
669 pp_declaration_specifiers (pp, t);
670 pp_declarator (pp, t);
671 pp_needs_newline (pp) = true;
672 pp_statement (pp, DECL_SAVED_TREE (t));
673 pp_newline (pp);
674 pp_flush (pp);
678 /* Expressions. */
680 /* Print out a c-char. */
682 static void
683 pp_c_char (c_pretty_printer *pp, int c)
685 switch (c)
687 case TARGET_NEWLINE:
688 pp_string (pp, "\\n");
689 break;
690 case TARGET_TAB:
691 pp_string (pp, "\\t");
692 break;
693 case TARGET_VT:
694 pp_string (pp, "\\v");
695 break;
696 case TARGET_BS:
697 pp_string (pp, "\\b");
698 break;
699 case TARGET_CR:
700 pp_string (pp, "\\r");
701 break;
702 case TARGET_FF:
703 pp_string (pp, "\\f");
704 break;
705 case TARGET_BELL:
706 pp_string (pp, "\\a");
707 break;
708 case '\\':
709 pp_string (pp, "\\\\");
710 break;
711 case '\'':
712 pp_string (pp, "\\'");
713 break;
714 case '\"':
715 pp_string (pp, "\\\"");
716 break;
717 default:
718 if (ISPRINT (c))
719 pp_character (pp, c);
720 else
721 pp_scalar (pp, "\\%03o", (unsigned) c);
722 break;
726 /* Print out a STRING literal. */
728 void
729 pp_c_string_literal (c_pretty_printer *pp, tree s)
731 const char *p = TREE_STRING_POINTER (s);
732 int n = TREE_STRING_LENGTH (s) - 1;
733 int i;
734 pp_doublequote (pp);
735 for (i = 0; i < n; ++i)
736 pp_c_char (pp, p[i]);
737 pp_doublequote (pp);
740 static void
741 pp_c_integer_constant (c_pretty_printer *pp, tree i)
743 tree type = TREE_TYPE (i);
745 if (TREE_INT_CST_HIGH (i) == 0)
746 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
747 else
749 if (tree_int_cst_sgn (i) < 0)
751 pp_c_char (pp, '-');
752 i = build_int_2 (-TREE_INT_CST_LOW (i),
753 ~TREE_INT_CST_HIGH (i) + !TREE_INT_CST_LOW (i));
755 sprintf (pp_buffer (pp)->digit_buffer,
756 HOST_WIDE_INT_PRINT_DOUBLE_HEX,
757 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
758 pp_string (pp, pp_buffer (pp)->digit_buffer);
760 if (TREE_UNSIGNED (type))
761 pp_character (pp, 'u');
762 if (type == long_integer_type_node || type == long_unsigned_type_node)
763 pp_character (pp, 'l');
764 else if (type == long_long_integer_type_node
765 || type == long_long_unsigned_type_node)
766 pp_string (pp, "ll");
769 /* Print out a CHARACTER literal. */
771 static void
772 pp_c_character_constant (c_pretty_printer *pp, tree c)
774 tree type = TREE_TYPE (c);
775 if (type == wchar_type_node)
776 pp_character (pp, 'L');
777 pp_quote (pp);
778 if (host_integerp (c, TREE_UNSIGNED (type)))
779 pp_c_char (pp, tree_low_cst (c, TREE_UNSIGNED (type)));
780 else
781 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
782 pp_quote (pp);
785 /* Print out a BOOLEAN literal. */
787 static void
788 pp_c_bool_constant (c_pretty_printer *pp, tree b)
790 if (b == boolean_false_node)
792 if (c_dialect_cxx ())
793 pp_c_identifier (pp, "false");
794 else if (flag_isoc99)
795 pp_c_identifier (pp, "_False");
796 else
797 pp_unsupported_tree (pp, b);
799 else if (b == boolean_true_node)
801 if (c_dialect_cxx ())
802 pp_c_identifier (pp, "true");
803 else if (flag_isoc99)
804 pp_c_identifier (pp, "_True");
805 else
806 pp_unsupported_tree (pp, b);
808 else if (TREE_CODE (b) == INTEGER_CST)
809 pp_c_integer_constant (pp, b);
810 else
811 pp_unsupported_tree (pp, b);
814 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
815 false; that means the value was obtained by a cast, in which case
816 print out the type-id part of the cast-expression -- the casted value
817 is then printed by pp_c_integer_literal. */
819 static bool
820 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
822 bool value_is_named = true;
823 tree type = TREE_TYPE (e);
824 tree value;
826 /* Find the name of this constant. */
827 for (value = TYPE_VALUES (type);
828 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
829 value = TREE_CHAIN (value))
832 if (value != NULL_TREE)
833 pp_id_expression (pp, TREE_PURPOSE (value));
834 else
836 /* Value must have been cast. */
837 pp_c_type_cast (pp, type);
838 value_is_named = false;
841 return value_is_named;
844 /* Print out a REAL value as a decimal-floating-constant. */
846 static void
847 pp_c_floating_constant (c_pretty_printer *pp, tree r)
849 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
850 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
851 pp_string (pp, pp_buffer(pp)->digit_buffer);
852 if (TREE_TYPE (r) == float_type_node)
853 pp_character (pp, 'f');
854 else if (TREE_TYPE (r) == long_double_type_node)
855 pp_character (pp, 'l');
858 /* Pretty-print a compound literal expression. GNU extensions include
859 vector constants. */
861 static void
862 pp_c_compound_literal (c_pretty_printer *pp, tree e)
864 tree type = TREE_TYPE (e);
865 pp_c_type_cast (pp, type);
867 switch (TREE_CODE (type))
869 case RECORD_TYPE:
870 case UNION_TYPE:
871 case ARRAY_TYPE:
872 case VECTOR_TYPE:
873 case COMPLEX_TYPE:
874 pp_c_brace_enclosed_initializer_list (pp, e);
875 break;
877 default:
878 pp_unsupported_tree (pp, e);
879 break;
883 /* constant:
884 integer-constant
885 floating-constant
886 enumeration-constant
887 character-constant */
889 void
890 pp_c_constant (c_pretty_printer *pp, tree e)
892 const enum tree_code code = TREE_CODE (e);
894 switch (code)
896 case INTEGER_CST:
898 tree type = TREE_TYPE (e);
899 if (type == boolean_type_node)
900 pp_c_bool_constant (pp, e);
901 else if (type == char_type_node)
902 pp_c_character_constant (pp, e);
903 else if (TREE_CODE (type) == ENUMERAL_TYPE
904 && pp_c_enumeration_constant (pp, e))
906 else
907 pp_c_integer_constant (pp, e);
909 break;
911 case REAL_CST:
912 pp_c_floating_constant (pp, e);
913 break;
915 case STRING_CST:
916 pp_c_string_literal (pp, e);
917 break;
919 default:
920 pp_unsupported_tree (pp, e);
921 break;
925 void
926 pp_c_identifier (c_pretty_printer *pp, const char *id)
928 pp_c_maybe_whitespace (pp);
929 pp_identifier (pp, id);
930 pp_base (pp)->padding = pp_before;
933 /* Pretty-print a C primary-expression.
934 primary-expression:
935 identifier
936 constant
937 string-literal
938 ( expression ) */
940 void
941 pp_c_primary_expression (c_pretty_printer *pp, tree e)
943 switch (TREE_CODE (e))
945 case VAR_DECL:
946 case PARM_DECL:
947 case FIELD_DECL:
948 case CONST_DECL:
949 case FUNCTION_DECL:
950 case LABEL_DECL:
951 e = DECL_NAME (e);
952 /* Fall through. */
953 case IDENTIFIER_NODE:
954 pp_c_tree_identifier (pp, e);
955 break;
957 case ERROR_MARK:
958 pp_c_identifier (pp, "<erroneous-expression>");
959 break;
961 case RESULT_DECL:
962 pp_c_identifier (pp, "<return-value>");
963 break;
965 case INTEGER_CST:
966 case REAL_CST:
967 case STRING_CST:
968 pp_c_constant (pp, e);
969 break;
971 case STMT_EXPR:
972 pp_c_left_paren (pp);
973 pp_statement (pp, STMT_EXPR_STMT (e));
974 pp_c_right_paren (pp);
975 break;
977 default:
978 /* FIXME: Make sure we won't get into an infinie loop. */
979 pp_c_left_paren (pp);
980 pp_expression (pp, e);
981 pp_c_right_paren (pp);
982 break;
986 /* Print out a C initializer -- also support C compound-literals.
987 initializer:
988 assignment-expression:
989 { initializer-list }
990 { initializer-list , } */
992 static void
993 pp_c_initializer (c_pretty_printer *pp, tree e)
995 if (TREE_CODE (e) == CONSTRUCTOR)
997 enum tree_code code = TREE_CODE (TREE_TYPE (e));
998 if (code == RECORD_TYPE || code == UNION_TYPE || code == ARRAY_TYPE)
999 pp_c_brace_enclosed_initializer_list (pp, e);
1000 else
1001 pp_unsupported_tree (pp, TREE_OPERAND (e, 1));
1003 else
1004 pp_expression (pp, e);
1007 /* init-declarator:
1008 declarator:
1009 declarator = initializer */
1011 void
1012 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1014 pp_declarator (pp, t);
1015 if (DECL_INITIAL (t))
1017 tree init = DECL_INITIAL (t);
1018 /* This C++ bit is handled here because it is easier to do so.
1019 In templates, the C++ parser builds a TREE_LIST for a
1020 direct-initialization; the TREE_PURPOSE is the variable to
1021 initialize and the TREE_VALUE is the initializer. */
1022 if (TREE_CODE (init) == TREE_LIST)
1024 pp_c_left_paren (pp);
1025 pp_expression (pp, TREE_VALUE (init));
1026 pp_right_paren (pp);
1028 else
1030 pp_space (pp);
1031 pp_equal (pp);
1032 pp_space (pp);
1033 pp_c_initializer (pp, init);
1038 /* initializer-list:
1039 designation(opt) initializer
1040 initializer-list , designation(opt) initializer
1042 designation:
1043 designator-list =
1045 designator-list:
1046 designator
1047 designator-list designator
1049 designator:
1050 [ constant-expression ]
1051 identifier */
1053 static void
1054 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1056 tree type = TREE_TYPE (e);
1057 const enum tree_code code = TREE_CODE (type);
1059 switch (code)
1061 case RECORD_TYPE:
1062 case UNION_TYPE:
1063 case ARRAY_TYPE:
1065 tree init = TREE_OPERAND (e, 0);
1066 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1068 if (code == RECORD_TYPE || code == UNION_TYPE)
1070 pp_c_dot (pp);
1071 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1073 else
1075 pp_c_left_bracket (pp);
1076 if (TREE_PURPOSE (init))
1077 pp_c_constant (pp, TREE_PURPOSE (init));
1078 pp_c_right_bracket (pp);
1080 pp_c_whitespace (pp);
1081 pp_equal (pp);
1082 pp_c_whitespace (pp);
1083 pp_initializer (pp, TREE_VALUE (init));
1084 if (TREE_CHAIN (init))
1085 pp_separate_with (pp, ',');
1088 break;
1090 case VECTOR_TYPE:
1091 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1092 break;
1094 case COMPLEX_TYPE:
1096 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1097 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1098 pp_separate_with (pp, ',');
1099 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1101 break;
1103 default:
1104 pp_unsupported_tree (pp, type);
1105 break;
1109 /* Pretty-print a brace-enclosed initializer-list. */
1111 static void
1112 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1114 pp_c_left_brace (pp);
1115 pp_c_initializer_list (pp, l);
1116 pp_c_right_brace (pp);
1120 /* This is a convenient function, used to bridge gap between C and C++
1121 grammars.
1123 id-expression:
1124 identifier */
1126 void
1127 pp_c_id_expression (c_pretty_printer *pp, tree t)
1129 switch (TREE_CODE (t))
1131 case VAR_DECL:
1132 case PARM_DECL:
1133 case CONST_DECL:
1134 case TYPE_DECL:
1135 case FUNCTION_DECL:
1136 case FIELD_DECL:
1137 case LABEL_DECL:
1138 t = DECL_NAME (t);
1139 case IDENTIFIER_NODE:
1140 pp_c_tree_identifier (pp, t);
1141 break;
1143 default:
1144 pp_unsupported_tree (pp, t);
1145 break;
1149 /* postfix-expression:
1150 primary-expression
1151 postfix-expression [ expression ]
1152 postfix-expression ( argument-expression-list(opt) )
1153 postfix-expression . identifier
1154 postfix-expression -> identifier
1155 postfix-expression ++
1156 postfix-expression --
1157 ( type-name ) { initializer-list }
1158 ( type-name ) { initializer-list , } */
1160 void
1161 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1163 enum tree_code code = TREE_CODE (e);
1164 switch (code)
1166 case POSTINCREMENT_EXPR:
1167 case POSTDECREMENT_EXPR:
1168 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1169 pp_identifier (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1170 break;
1172 case ARROW_EXPR:
1173 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1174 pp_c_arrow (pp);
1175 break;
1177 case ARRAY_REF:
1178 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1179 pp_c_left_bracket (pp);
1180 pp_expression (pp, TREE_OPERAND (e, 1));
1181 pp_c_right_bracket (pp);
1182 break;
1184 case CALL_EXPR:
1185 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1186 pp_c_call_argument_list (pp, TREE_OPERAND (e, 1));
1187 break;
1189 case ABS_EXPR:
1190 pp_c_identifier (pp, "__builtin_abs");
1191 pp_c_left_paren (pp);
1192 pp_expression (pp, TREE_OPERAND (e, 0));
1193 pp_c_right_paren (pp);
1194 break;
1196 case COMPONENT_REF:
1198 tree object = TREE_OPERAND (e, 0);
1199 if (TREE_CODE (object) == INDIRECT_REF)
1201 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1202 pp_c_arrow (pp);
1204 else
1206 pp_postfix_expression (pp, object);
1207 pp_c_dot (pp);
1209 pp_expression (pp, TREE_OPERAND (e, 1));
1211 break;
1213 case COMPLEX_CST:
1214 case VECTOR_CST:
1215 case COMPLEX_EXPR:
1216 pp_c_compound_literal (pp, e);
1217 break;
1219 case COMPOUND_LITERAL_EXPR:
1220 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1221 /* Fall through. */
1222 case CONSTRUCTOR:
1223 pp_initializer (pp, e);
1224 break;
1226 case VA_ARG_EXPR:
1227 pp_c_identifier (pp, "__builtin_va_arg");
1228 pp_c_left_paren (pp);
1229 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1230 pp_separate_with (pp, ',');
1231 pp_type_id (pp, TREE_TYPE (e));
1232 pp_c_right_paren (pp);
1233 break;
1235 case ADDR_EXPR:
1236 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1238 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1239 break;
1241 /* else fall through. */
1243 default:
1244 pp_primary_expression (pp, e);
1245 break;
1249 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1251 void
1252 pp_c_expression_list (c_pretty_printer *pp, tree e)
1254 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1256 pp_expression (pp, TREE_VALUE (e));
1257 if (TREE_CHAIN (e))
1258 pp_separate_with (pp, ',');
1262 /* Print out an expression-list in parens, as in a function call. */
1264 void
1265 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1267 pp_c_left_paren (pp);
1268 if (t && TREE_CODE (t) == TREE_LIST)
1269 pp_c_expression_list (pp, t);
1270 pp_c_right_paren (pp);
1273 /* unary-expression:
1274 postfix-expression
1275 ++ cast-expression
1276 -- cast-expression
1277 unary-operator cast-expression
1278 sizeof unary-expression
1279 sizeof ( type-id )
1281 unary-operator: one of
1282 * & + - ! ~
1284 GNU extensions.
1285 unary-expression:
1286 __alignof__ unary-expression
1287 __alignof__ ( type-id )
1288 __real__ unary-expression
1289 __imag__ unary-expression */
1291 void
1292 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1294 enum tree_code code = TREE_CODE (e);
1295 switch (code)
1297 case PREINCREMENT_EXPR:
1298 case PREDECREMENT_EXPR:
1299 pp_identifier (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1300 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1301 break;
1303 case ADDR_EXPR:
1304 case INDIRECT_REF:
1305 case NEGATE_EXPR:
1306 case BIT_NOT_EXPR:
1307 case TRUTH_NOT_EXPR:
1308 case CONJ_EXPR:
1309 /* String literal are used by address. */
1310 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1311 pp_ampersand (pp);
1312 else if (code == INDIRECT_REF)
1313 pp_c_star (pp);
1314 else if (code == NEGATE_EXPR)
1315 pp_minus (pp);
1316 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1317 pp_complement (pp);
1318 else if (code == TRUTH_NOT_EXPR)
1319 pp_exclamation (pp);
1320 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1321 break;
1323 case SIZEOF_EXPR:
1324 case ALIGNOF_EXPR:
1325 pp_c_identifier (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
1326 pp_c_whitespace (pp);
1327 if (TYPE_P (TREE_OPERAND (e, 0)))
1328 pp_c_type_cast (pp, TREE_OPERAND (e, 0));
1329 else
1330 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1331 break;
1333 case REALPART_EXPR:
1334 case IMAGPART_EXPR:
1335 pp_c_identifier (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1336 pp_c_whitespace (pp);
1337 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1338 break;
1340 default:
1341 pp_postfix_expression (pp, e);
1342 break;
1346 /* cast-expression:
1347 unary-expression
1348 ( type-name ) cast-expression */
1350 void
1351 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1353 switch (TREE_CODE (e))
1355 case FLOAT_EXPR:
1356 case FIX_TRUNC_EXPR:
1357 case CONVERT_EXPR:
1358 pp_c_type_cast (pp, TREE_TYPE (e));
1359 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1360 break;
1362 default:
1363 pp_unary_expression (pp, e);
1367 /* multiplicative-expression:
1368 cast-expression
1369 multiplicative-expression * cast-expression
1370 multiplicative-expression / cast-expression
1371 multiplicative-expression % cast-expression */
1373 static void
1374 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1376 enum tree_code code = TREE_CODE (e);
1377 switch (code)
1379 case MULT_EXPR:
1380 case TRUNC_DIV_EXPR:
1381 case TRUNC_MOD_EXPR:
1382 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1383 pp_c_whitespace (pp);
1384 if (code == MULT_EXPR)
1385 pp_c_star (pp);
1386 else if (code == TRUNC_DIV_EXPR)
1387 pp_slash (pp);
1388 else
1389 pp_modulo (pp);
1390 pp_c_whitespace (pp);
1391 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1392 break;
1394 default:
1395 pp_c_cast_expression (pp, e);
1396 break;
1400 /* additive-expression:
1401 multiplicative-expression
1402 additive-expression + multiplicative-expression
1403 additive-expression - multiplicative-expression */
1405 static void
1406 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1408 enum tree_code code = TREE_CODE (e);
1409 switch (code)
1411 case PLUS_EXPR:
1412 case MINUS_EXPR:
1413 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1414 pp_c_whitespace (pp);
1415 if (code == PLUS_EXPR)
1416 pp_plus (pp);
1417 else
1418 pp_minus (pp);
1419 pp_c_whitespace (pp);
1420 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1421 break;
1423 default:
1424 pp_multiplicative_expression (pp, e);
1425 break;
1429 /* additive-expression:
1430 additive-expression
1431 shift-expression << additive-expression
1432 shift-expression >> additive-expression */
1434 static void
1435 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1437 enum tree_code code = TREE_CODE (e);
1438 switch (code)
1440 case LSHIFT_EXPR:
1441 case RSHIFT_EXPR:
1442 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1443 pp_c_whitespace (pp);
1444 pp_identifier (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1445 pp_c_whitespace (pp);
1446 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1447 break;
1449 default:
1450 pp_c_additive_expression (pp, e);
1454 /* relational-expression:
1455 shift-expression
1456 relational-expression < shift-expression
1457 relational-expression > shift-expression
1458 relational-expression <= shift-expression
1459 relational-expression >= shift-expression */
1461 static void
1462 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1464 enum tree_code code = TREE_CODE (e);
1465 switch (code)
1467 case LT_EXPR:
1468 case GT_EXPR:
1469 case LE_EXPR:
1470 case GE_EXPR:
1471 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1472 pp_c_whitespace (pp);
1473 if (code == LT_EXPR)
1474 pp_less (pp);
1475 else if (code == GT_EXPR)
1476 pp_greater (pp);
1477 else if (code == LE_EXPR)
1478 pp_identifier (pp, "<=");
1479 else if (code == GE_EXPR)
1480 pp_identifier (pp, ">=");
1481 pp_c_whitespace (pp);
1482 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1483 break;
1485 default:
1486 pp_c_shift_expression (pp, e);
1487 break;
1491 /* equality-expression:
1492 relational-expression
1493 equality-expression == relational-expression
1494 equality-equality != relational-expression */
1496 static void
1497 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1499 enum tree_code code = TREE_CODE (e);
1500 switch (code)
1502 case EQ_EXPR:
1503 case NE_EXPR:
1504 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1505 pp_c_whitespace (pp);
1506 pp_identifier (pp, code == EQ_EXPR ? "==" : "!=");
1507 pp_c_whitespace (pp);
1508 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1509 break;
1511 default:
1512 pp_c_relational_expression (pp, e);
1513 break;
1517 /* AND-expression:
1518 equality-expression
1519 AND-expression & equality-equality */
1521 static void
1522 pp_c_and_expression (c_pretty_printer *pp, tree e)
1524 if (TREE_CODE (e) == BIT_AND_EXPR)
1526 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1527 pp_c_whitespace (pp);
1528 pp_ampersand (pp);
1529 pp_c_whitespace (pp);
1530 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1532 else
1533 pp_c_equality_expression (pp, e);
1536 /* exclusive-OR-expression:
1537 AND-expression
1538 exclusive-OR-expression ^ AND-expression */
1540 static void
1541 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1543 if (TREE_CODE (e) == BIT_XOR_EXPR)
1545 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1546 pp_c_maybe_whitespace (pp);
1547 pp_carret (pp);
1548 pp_c_whitespace (pp);
1549 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1551 else
1552 pp_c_and_expression (pp, e);
1555 /* inclusive-OR-expression:
1556 exclusive-OR-expression
1557 inclusive-OR-expression | exclusive-OR-expression */
1559 static void
1560 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1562 if (TREE_CODE (e) == BIT_IOR_EXPR)
1564 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1565 pp_c_whitespace (pp);
1566 pp_bar (pp);
1567 pp_c_whitespace (pp);
1568 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1570 else
1571 pp_c_exclusive_or_expression (pp, e);
1574 /* logical-AND-expression:
1575 inclusive-OR-expression
1576 logical-AND-expression && inclusive-OR-expression */
1578 static void
1579 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1581 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
1583 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1584 pp_c_whitespace (pp);
1585 pp_identifier (pp, "&&");
1586 pp_c_whitespace (pp);
1587 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1589 else
1590 pp_c_inclusive_or_expression (pp, e);
1593 /* logical-OR-expression:
1594 logical-AND-expression
1595 logical-OR-expression || logical-AND-expression */
1597 void
1598 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1600 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1602 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1603 pp_c_whitespace (pp);
1604 pp_identifier (pp, "||");
1605 pp_c_whitespace (pp);
1606 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1608 else
1609 pp_c_logical_and_expression (pp, e);
1612 /* conditional-expression:
1613 logical-OR-expression
1614 logical-OR-expression ? expression : conditional-expression */
1616 static void
1617 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1619 if (TREE_CODE (e) == COND_EXPR)
1621 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1622 pp_c_whitespace (pp);
1623 pp_question (pp);
1624 pp_c_whitespace (pp);
1625 pp_expression (pp, TREE_OPERAND (e, 1));
1626 pp_c_whitespace (pp);
1627 pp_colon (pp);
1628 pp_c_whitespace (pp);
1629 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1631 else
1632 pp_c_logical_or_expression (pp, e);
1636 /* assignment-expression:
1637 conditional-expression
1638 unary-expression assignment-operator assignment-expression
1640 assignment-expression: one of
1641 = *= /= %= += -= >>= <<= &= ^= |= */
1643 static void
1644 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1646 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1648 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1649 pp_c_whitespace (pp);
1650 pp_equal (pp);
1651 pp_space (pp);
1652 pp_c_expression (pp, TREE_OPERAND (e, 1));
1654 else
1655 pp_c_conditional_expression (pp, e);
1658 /* expression:
1659 assignment-expression
1660 expression , assignment-expression
1662 Implementation note: instead of going through the usual recursion
1663 chain, I take the liberty of dispatching nodes to the appropriate
1664 functions. This makes some redundancy, but it worths it. That also
1665 prevents a possible infinite recursion between pp_c_primary_expression ()
1666 and pp_c_expression (). */
1668 void
1669 pp_c_expression (c_pretty_printer *pp, tree e)
1671 switch (TREE_CODE (e))
1673 case INTEGER_CST:
1674 pp_c_integer_constant (pp, e);
1675 break;
1677 case REAL_CST:
1678 pp_c_floating_constant (pp, e);
1679 break;
1681 case STRING_CST:
1682 pp_c_string_literal (pp, e);
1683 break;
1685 case IDENTIFIER_NODE:
1686 case FUNCTION_DECL:
1687 case VAR_DECL:
1688 case CONST_DECL:
1689 case PARM_DECL:
1690 case RESULT_DECL:
1691 case FIELD_DECL:
1692 case LABEL_DECL:
1693 case ERROR_MARK:
1694 case STMT_EXPR:
1695 pp_primary_expression (pp, e);
1696 break;
1698 case POSTINCREMENT_EXPR:
1699 case POSTDECREMENT_EXPR:
1700 case ARROW_EXPR:
1701 case ARRAY_REF:
1702 case CALL_EXPR:
1703 case COMPONENT_REF:
1704 case COMPLEX_CST:
1705 case COMPLEX_EXPR:
1706 case VECTOR_CST:
1707 case ABS_EXPR:
1708 case CONSTRUCTOR:
1709 case COMPOUND_LITERAL_EXPR:
1710 case VA_ARG_EXPR:
1711 pp_postfix_expression (pp, e);
1712 break;
1714 case CONJ_EXPR:
1715 case ADDR_EXPR:
1716 case INDIRECT_REF:
1717 case NEGATE_EXPR:
1718 case BIT_NOT_EXPR:
1719 case TRUTH_NOT_EXPR:
1720 case PREINCREMENT_EXPR:
1721 case PREDECREMENT_EXPR:
1722 case SIZEOF_EXPR:
1723 case ALIGNOF_EXPR:
1724 case REALPART_EXPR:
1725 case IMAGPART_EXPR:
1726 pp_c_unary_expression (pp, e);
1727 break;
1729 case FLOAT_EXPR:
1730 case FIX_TRUNC_EXPR:
1731 case CONVERT_EXPR:
1732 pp_c_cast_expression (pp, e);
1733 break;
1735 case MULT_EXPR:
1736 case TRUNC_MOD_EXPR:
1737 case TRUNC_DIV_EXPR:
1738 pp_multiplicative_expression (pp, e);
1739 break;
1741 case LSHIFT_EXPR:
1742 case RSHIFT_EXPR:
1743 pp_c_shift_expression (pp, e);
1744 break;
1746 case LT_EXPR:
1747 case GT_EXPR:
1748 case LE_EXPR:
1749 case GE_EXPR:
1750 pp_c_relational_expression (pp, e);
1751 break;
1753 case BIT_AND_EXPR:
1754 pp_c_and_expression (pp, e);
1755 break;
1757 case BIT_XOR_EXPR:
1758 pp_c_exclusive_or_expression (pp, e);
1759 break;
1761 case BIT_IOR_EXPR:
1762 pp_c_inclusive_or_expression (pp, e);
1763 break;
1765 case TRUTH_ANDIF_EXPR:
1766 pp_c_logical_and_expression (pp, e);
1767 break;
1769 case TRUTH_ORIF_EXPR:
1770 pp_c_logical_or_expression (pp, e);
1771 break;
1773 case EQ_EXPR:
1774 case NE_EXPR:
1775 pp_c_equality_expression (pp, e);
1776 break;
1778 case COND_EXPR:
1779 pp_conditional_expression (pp, e);
1780 break;
1782 case PLUS_EXPR:
1783 case MINUS_EXPR:
1784 pp_c_additive_expression (pp, e);
1785 break;
1787 case MODIFY_EXPR:
1788 case INIT_EXPR:
1789 pp_assignment_expression (pp, e);
1790 break;
1792 case COMPOUND_EXPR:
1793 pp_c_left_paren (pp);
1794 pp_expression (pp, TREE_OPERAND (e, 0));
1795 pp_separate_with (pp, ',');
1796 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
1797 pp_c_right_paren (pp);
1798 break;
1800 case NOP_EXPR:
1801 case NON_LVALUE_EXPR:
1802 case SAVE_EXPR:
1803 case UNSAVE_EXPR:
1804 pp_expression (pp, TREE_OPERAND (e, 0));
1805 break;
1807 case TARGET_EXPR:
1808 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
1809 break;
1811 default:
1812 pp_unsupported_tree (pp, e);
1813 break;
1819 /* Statements. */
1821 /* statement:
1822 labeled-statement
1823 compound-statement
1824 expression-statement
1825 selection-statement
1826 iteration-statement
1827 jump-statement */
1829 void
1830 pp_c_statement (c_pretty_printer *pp, tree stmt)
1832 enum tree_code code;
1834 if (stmt == NULL)
1835 return;
1837 code = TREE_CODE (stmt);
1838 switch (code)
1840 /* labeled-statement:
1841 identifier : statement
1842 case constant-expression : statement
1843 default : statement */
1844 case LABEL_STMT:
1845 case CASE_LABEL:
1846 if (pp_needs_newline (pp))
1847 pp_newline_and_indent (pp, -3);
1848 else
1849 pp_indentation (pp) -= 3;
1850 if (code == LABEL_STMT)
1851 pp_tree_identifier (pp, DECL_NAME (LABEL_STMT_LABEL (stmt)));
1852 else if (code == CASE_LABEL)
1854 if (CASE_LOW (stmt) == NULL_TREE)
1855 pp_identifier (pp, "default");
1856 else
1858 pp_c_identifier (pp, "case");
1859 pp_c_whitespace (pp);
1860 pp_conditional_expression (pp, CASE_LOW (stmt));
1861 if (CASE_HIGH (stmt))
1863 pp_identifier (pp, "...");
1864 pp_conditional_expression (pp, CASE_HIGH (stmt));
1868 pp_colon (pp);
1869 pp_indentation (pp) += 3;
1870 pp_needs_newline (pp) = true;
1871 break;
1873 /* compound-statement:
1874 { block-item-list(opt) }
1876 block-item-list:
1877 block-item
1878 block-item-list block-item
1880 block-item:
1881 declaration
1882 statement */
1883 case COMPOUND_STMT:
1884 if (pp_needs_newline (pp))
1885 pp_newline_and_indent (pp, 0);
1886 pp_c_left_brace (pp);
1887 pp_newline_and_indent (pp, 3);
1888 for (stmt = COMPOUND_BODY (stmt); stmt; stmt = TREE_CHAIN (stmt))
1889 pp_statement (pp, stmt);
1890 pp_newline_and_indent (pp, -3);
1891 pp_c_right_brace (pp);
1892 pp_needs_newline (pp) = true;
1893 break;
1895 /* expression-statement:
1896 expression(opt) ; */
1897 case EXPR_STMT:
1898 case CLEANUP_STMT:
1899 if (pp_needs_newline (pp))
1900 pp_newline_and_indent (pp, 0);
1902 tree e = code == EXPR_STMT
1903 ? EXPR_STMT_EXPR (stmt)
1904 : CLEANUP_EXPR (stmt);
1905 if (e)
1906 pp_expression (pp, e);
1908 pp_c_semicolon (pp);
1909 pp_needs_newline (pp) = true;
1910 break;
1912 /* selection-statement:
1913 if ( expression ) statement
1914 if ( expression ) statement else statement
1915 switch ( expression ) statement */
1916 case IF_STMT:
1917 if (pp_needs_newline (pp))
1918 pp_newline_and_indent (pp, 0);
1919 pp_c_identifier (pp, "if");
1920 pp_c_whitespace (pp);
1921 pp_c_left_paren (pp);
1922 pp_expression (pp, IF_COND (stmt));
1923 pp_c_right_paren (pp);
1924 pp_newline_and_indent (pp, 3);
1925 pp_statement (pp, THEN_CLAUSE (stmt));
1926 pp_newline_and_indent (pp, -3);
1927 if (ELSE_CLAUSE (stmt))
1929 tree else_clause = ELSE_CLAUSE (stmt);
1930 pp_c_identifier (pp, "else");
1931 if (TREE_CODE (else_clause) == IF_STMT)
1932 pp_c_whitespace (pp);
1933 else
1934 pp_newline_and_indent (pp, 3);
1935 pp_statement (pp, else_clause);
1936 if (TREE_CODE (else_clause) != IF_STMT)
1937 pp_newline_and_indent (pp, -3);
1939 break;
1941 case SWITCH_STMT:
1942 if (pp_needs_newline (pp))
1943 pp_newline_and_indent (pp, 0);
1944 pp_c_identifier (pp, "switch");
1945 pp_space (pp);
1946 pp_c_left_paren (pp);
1947 pp_expression (pp, SWITCH_COND (stmt));
1948 pp_c_right_paren (pp);
1949 pp_indentation (pp) += 3;
1950 pp_needs_newline (pp) = true;
1951 pp_statement (pp, SWITCH_BODY (stmt));
1952 pp_newline_and_indent (pp, -3);
1953 break;
1955 /* iteration-statement:
1956 while ( expression ) statement
1957 do statement while ( expression ) ;
1958 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1959 for ( declaration expression(opt) ; expression(opt) ) statement */
1960 case WHILE_STMT:
1961 if (pp_needs_newline (pp))
1962 pp_newline_and_indent (pp, 0);
1963 pp_c_identifier (pp, "while");
1964 pp_space (pp);
1965 pp_c_left_paren (pp);
1966 pp_expression (pp, WHILE_COND (stmt));
1967 pp_c_right_paren (pp);
1968 pp_newline_and_indent (pp, 3);
1969 pp_statement (pp, WHILE_BODY (stmt));
1970 pp_indentation (pp) -= 3;
1971 pp_needs_newline (pp) = true;
1972 break;
1974 case DO_STMT:
1975 if (pp_needs_newline (pp))
1976 pp_newline_and_indent (pp, 0);
1977 pp_c_identifier (pp, "do");
1978 pp_newline_and_indent (pp, 3);
1979 pp_statement (pp, DO_BODY (stmt));
1980 pp_newline_and_indent (pp, -3);
1981 pp_c_identifier (pp, "while");
1982 pp_space (pp);
1983 pp_c_left_paren (pp);
1984 pp_expression (pp, DO_COND (stmt));
1985 pp_c_right_paren (pp);
1986 pp_c_semicolon (pp);
1987 pp_needs_newline (pp) = true;
1988 break;
1990 case FOR_STMT:
1991 if (pp_needs_newline (pp))
1992 pp_newline_and_indent (pp, 0);
1993 pp_c_identifier (pp, "for");
1994 pp_space (pp);
1995 pp_c_left_paren (pp);
1996 if (FOR_INIT_STMT (stmt))
1997 pp_statement (pp, FOR_INIT_STMT (stmt));
1998 else
1999 pp_c_semicolon (pp);
2000 pp_needs_newline (pp) = false;
2001 pp_c_whitespace (pp);
2002 if (FOR_COND (stmt))
2003 pp_expression (pp, FOR_COND (stmt));
2004 pp_c_semicolon (pp);
2005 pp_needs_newline (pp) = false;
2006 pp_c_whitespace (pp);
2007 if (FOR_EXPR (stmt))
2008 pp_expression (pp, FOR_EXPR (stmt));
2009 pp_c_right_paren (pp);
2010 pp_newline_and_indent (pp, 3);
2011 pp_statement (pp, FOR_BODY (stmt));
2012 pp_indentation (pp) -= 3;
2013 pp_needs_newline (pp) = true;
2014 break;
2016 /* jump-statement:
2017 goto identifier;
2018 continue ;
2019 return expression(opt) ; */
2020 case BREAK_STMT:
2021 case CONTINUE_STMT:
2022 if (pp_needs_newline (pp))
2023 pp_newline_and_indent (pp, 0);
2024 pp_identifier (pp, code == BREAK_STMT ? "break" : "continue");
2025 pp_c_semicolon (pp);
2026 pp_needs_newline (pp) = true;
2027 break;
2029 case RETURN_STMT:
2030 case GOTO_STMT:
2032 tree e = code == RETURN_STMT
2033 ? RETURN_STMT_EXPR (stmt)
2034 : GOTO_DESTINATION (stmt);
2035 if (pp_needs_newline (pp))
2036 pp_newline_and_indent (pp, 0);
2037 pp_c_identifier (pp, code == RETURN_STMT ? "return" : "goto");
2038 pp_c_whitespace (pp);
2039 if (e)
2041 if (TREE_CODE (e) == INIT_EXPR
2042 && TREE_CODE (TREE_OPERAND (e, 0)) == RESULT_DECL)
2043 e = TREE_OPERAND (e, 1);
2044 pp_expression (pp, e);
2046 pp_c_semicolon (pp);
2047 pp_needs_newline (pp) = true;
2049 break;
2051 case SCOPE_STMT:
2052 if (!SCOPE_NULLIFIED_P (stmt) && SCOPE_NO_CLEANUPS_P (stmt))
2054 int i = 0;
2055 if (pp_needs_newline (pp))
2056 pp_newline_and_indent (pp, 0);
2057 if (SCOPE_BEGIN_P (stmt))
2059 pp_left_brace (pp);
2060 i = 3;
2062 else if (SCOPE_END_P (stmt))
2064 pp_right_brace (pp);
2065 i = -3;
2067 pp_indentation (pp) += i;
2068 pp_needs_newline (pp) = true;
2070 break;
2072 case DECL_STMT:
2073 if (pp_needs_newline (pp))
2074 pp_newline_and_indent (pp, 0);
2075 pp_declaration (pp, DECL_STMT_DECL (stmt));
2076 pp_needs_newline (pp) = true;
2077 break;
2079 case ASM_STMT:
2081 bool has_volatile_p = ASM_VOLATILE_P (stmt);
2082 bool is_extended = has_volatile_p || ASM_INPUTS (stmt)
2083 || ASM_OUTPUTS (stmt) || ASM_CLOBBERS (stmt);
2084 pp_c_identifier (pp, is_extended ? "__asm__" : "asm");
2085 if (has_volatile_p)
2086 pp_c_identifier (pp, "__volatile__");
2087 pp_space (pp);
2088 pp_c_left_paren (pp);
2089 pp_c_string_literal (pp, ASM_STRING (stmt));
2090 if (is_extended)
2092 pp_space (pp);
2093 pp_separate_with (pp, ':');
2094 if (ASM_OUTPUTS (stmt))
2095 pp_expression (pp, ASM_OUTPUTS (stmt));
2096 pp_space (pp);
2097 pp_separate_with (pp, ':');
2098 if (ASM_INPUTS (stmt))
2099 pp_expression (pp, ASM_INPUTS (stmt));
2100 pp_space (pp);
2101 pp_separate_with (pp, ':');
2102 if (ASM_CLOBBERS (stmt))
2103 pp_expression (pp, ASM_CLOBBERS (stmt));
2105 pp_c_right_paren (pp);
2106 pp_newline (pp);
2108 break;
2110 case FILE_STMT:
2111 pp_c_identifier (pp, "__FILE__");
2112 pp_space (pp);
2113 pp_equal (pp);
2114 pp_c_whitespace (pp);
2115 pp_c_identifier (pp, FILE_STMT_FILENAME (stmt));
2116 pp_c_semicolon (pp);
2117 pp_needs_newline (pp) = true;
2118 break;
2120 default:
2121 pp_unsupported_tree (pp, stmt);
2126 /* Initialize the PRETTY-PRINTER for handling C codes. */
2128 void
2129 pp_c_pretty_printer_init (c_pretty_printer *pp)
2131 pp->offset_list = 0;
2133 pp->declaration = pp_c_declaration;
2134 pp->declaration_specifiers = pp_c_declaration_specifiers;
2135 pp->declarator = pp_c_declarator;
2136 pp->direct_declarator = pp_c_direct_declarator;
2137 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
2138 pp->abstract_declarator = pp_c_abstract_declarator;
2139 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
2140 pp->ptr_operator = pp_c_pointer;
2141 pp->parameter_list = pp_c_parameter_type_list;
2142 pp->type_id = pp_c_type_id;
2143 pp->simple_type_specifier = pp_c_type_specifier;
2144 pp->function_specifier = pp_c_function_specifier;
2145 pp->storage_class_specifier = pp_c_storage_class_specifier;
2147 pp->statement = pp_c_statement;
2149 pp->id_expression = pp_c_id_expression;
2150 pp->primary_expression = pp_c_primary_expression;
2151 pp->postfix_expression = pp_c_postfix_expression;
2152 pp->unary_expression = pp_c_unary_expression;
2153 pp->initializer = pp_c_initializer;
2154 pp->multiplicative_expression = pp_c_multiplicative_expression;
2155 pp->conditional_expression = pp_c_conditional_expression;
2156 pp->assignment_expression = pp_c_assignment_expression;
2157 pp->expression = pp_c_expression;