* function.c (walk_fixup_memory_subreg): New parameter 'var'.
[official-gcc.git] / gcc / c-pretty-print.c
blob4b5dd51e578f5d120ff541ea46e61e318b6ee415
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"
29 #include "diagnostic.h"
31 /* The pretty-printer code is primarily designed to closely follow
32 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
33 codes we used to have in the past. Following a structured
34 approach (preferably the official grammars) is believed to make it
35 much easier to add extensions and nifty pretty-printing effects that
36 takes expression or declaration contexts into account. */
39 #define pp_c_maybe_whitespace(PP) \
40 do { \
41 if (pp_base (PP)->padding == pp_before) \
42 pp_c_whitespace (PP); \
43 } while (0)
45 /* literal */
46 static void pp_c_char (c_pretty_printer *, int);
48 /* postfix-expression */
49 static void pp_c_initializer_list (c_pretty_printer *, tree);
50 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
52 static void pp_c_multiplicative_expression (c_pretty_printer *, tree);
53 static void pp_c_additive_expression (c_pretty_printer *, tree);
54 static void pp_c_shift_expression (c_pretty_printer *, tree);
55 static void pp_c_relational_expression (c_pretty_printer *, tree);
56 static void pp_c_equality_expression (c_pretty_printer *, tree);
57 static void pp_c_and_expression (c_pretty_printer *, tree);
58 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
59 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
60 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
61 static void pp_c_conditional_expression (c_pretty_printer *, tree);
62 static void pp_c_assignment_expression (c_pretty_printer *, tree);
64 /* declarations. */
67 /* Helper functions. */
69 void
70 pp_c_whitespace (c_pretty_printer *pp)
72 pp_space (pp);
73 pp_base (pp)->padding = pp_none;
76 void
77 pp_c_left_paren (c_pretty_printer *pp)
79 pp_left_paren (pp);
80 pp_base (pp)->padding = pp_none;
83 void
84 pp_c_right_paren (c_pretty_printer *pp)
86 pp_right_paren (pp);
87 pp_base (pp)->padding = pp_none;
90 void
91 pp_c_left_brace (c_pretty_printer *pp)
93 pp_left_brace (pp);
94 pp_base (pp)->padding = pp_none;
97 void
98 pp_c_right_brace (c_pretty_printer *pp)
100 pp_right_brace (pp);
101 pp_base (pp)->padding = pp_none;
104 void
105 pp_c_left_bracket (c_pretty_printer *pp)
107 pp_left_bracket (pp);
108 pp_base (pp)->padding = pp_none;
111 void
112 pp_c_right_bracket (c_pretty_printer *pp)
114 pp_right_bracket (pp);
115 pp_base (pp)->padding = pp_none;
118 void
119 pp_c_dot (c_pretty_printer *pp)
121 pp_dot (pp);
122 pp_base (pp)->padding = pp_none;
125 void
126 pp_c_ampersand (c_pretty_printer *pp)
128 pp_ampersand (pp);
129 pp_base (pp)->padding = pp_none;
132 void
133 pp_c_star (c_pretty_printer *pp)
135 pp_star (pp);
136 pp_base (pp)->padding = pp_none;
139 void
140 pp_c_arrow (c_pretty_printer *pp)
142 pp_arrow (pp);
143 pp_base (pp)->padding = pp_none;
146 void
147 pp_c_semicolon (c_pretty_printer *pp)
149 pp_semicolon (pp);
150 pp_base (pp)->padding = pp_none;
153 void
154 pp_c_complement (c_pretty_printer *pp)
156 pp_complement (pp);
157 pp_base (pp)->padding = pp_none;
160 void
161 pp_c_exclamation (c_pretty_printer *pp)
163 pp_exclamation (pp);
164 pp_base (pp)->padding = pp_none;
167 /* Print out the external representation of CV-QUALIFIER. */
169 static void
170 pp_c_cv_qualifier (c_pretty_printer *pp, const char *cv)
172 const char *p = pp_last_position_in_text (pp);
173 /* The C programming language does not have references, but it is much
174 simpler to handle those here rather than going through the same
175 logic in the C++ pretty-printer. */
176 if (p != NULL && (*p == '*' || *p == '&'))
177 pp_c_whitespace (pp);
178 pp_c_identifier (pp, cv);
181 /* Pretty-print T using the type-cast notation '( type-name )'. */
183 static void
184 pp_c_type_cast (c_pretty_printer *pp, tree t)
186 pp_c_left_paren (pp);
187 pp_type_id (pp, t);
188 pp_c_right_paren (pp);
191 /* We're about to pretty-print a pointer type as indicated by T.
192 Output a whitespace, if needed, preparing for subsequent output. */
194 void
195 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
197 if (POINTER_TYPE_P (t))
199 tree pointee = strip_pointer_operator (TREE_TYPE (t));
200 if (TREE_CODE (pointee) != ARRAY_TYPE
201 && TREE_CODE (pointee) != FUNCTION_TYPE)
202 pp_c_whitespace (pp);
207 /* Declarations. */
209 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
210 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
211 of its type. Take care of possible extensions.
213 type-qualifier-list:
214 type-qualifier
215 type-qualifier-list type-qualifier
217 type-qualifier:
218 const
219 restrict -- C99
220 __restrict__ -- GNU C
221 volatile */
223 void
224 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
226 int qualifiers;
228 if (!TYPE_P (t))
229 t = TREE_TYPE (t);
231 qualifiers = TYPE_QUALS (t);
232 if (qualifiers & TYPE_QUAL_CONST)
233 pp_c_cv_qualifier (pp, "const");
234 if (qualifiers & TYPE_QUAL_VOLATILE)
235 pp_c_cv_qualifier (pp, "volatile");
236 if (qualifiers & TYPE_QUAL_RESTRICT)
237 pp_c_cv_qualifier (pp, flag_isoc99 ? "restrict" : "__restrict__");
240 /* pointer:
241 * type-qualifier-list(opt)
242 * type-qualifier-list(opt) pointer */
244 static void
245 pp_c_pointer (c_pretty_printer *pp, tree t)
247 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
248 t = TREE_TYPE (t);
249 switch (TREE_CODE (t))
251 case POINTER_TYPE:
252 /* It is easier to handle C++ reference types here. */
253 case REFERENCE_TYPE:
254 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
255 pp_c_pointer (pp, TREE_TYPE (t));
256 if (TREE_CODE (t) == POINTER_TYPE)
257 pp_c_star (pp);
258 else
259 pp_c_ampersand (pp);
260 pp_c_type_qualifier_list (pp, t);
261 break;
263 default:
264 pp_unsupported_tree (pp, t);
268 /* type-specifier:
269 void
270 char
271 short
273 long
274 float
275 double
276 signed
277 unsigned
278 _Bool -- C99
279 _Complex -- C99
280 _Imaginary -- C99
281 struct-or-union-specifier
282 enum-specifier
283 typedef-name.
285 GNU extensions.
286 simple-type-specifier:
287 __complex__
288 __vector__ */
290 void
291 pp_c_type_specifier (c_pretty_printer *pp, tree t)
293 const enum tree_code code = TREE_CODE (t);
294 switch (code)
296 case ERROR_MARK:
297 pp_c_identifier (pp, "<type-error>");
298 break;
300 case IDENTIFIER_NODE:
301 pp_c_tree_decl_identifier (pp, t);
302 break;
304 case VOID_TYPE:
305 case BOOLEAN_TYPE:
306 case CHAR_TYPE:
307 case INTEGER_TYPE:
308 case REAL_TYPE:
309 if (TYPE_NAME (t))
310 t = TYPE_NAME (t);
311 else
312 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
313 pp_c_type_specifier (pp, t);
314 break;
316 case TYPE_DECL:
317 if (DECL_NAME (t))
318 pp_id_expression (pp, t);
319 else
320 pp_c_identifier (pp, "<typedef-error>");
321 break;
323 case UNION_TYPE:
324 case RECORD_TYPE:
325 case ENUMERAL_TYPE:
326 if (code == UNION_TYPE)
327 pp_c_identifier (pp, "union");
328 else if (code == RECORD_TYPE)
329 pp_c_identifier (pp, "struct");
330 else if (code == ENUMERAL_TYPE)
331 pp_c_identifier (pp, "enum");
332 else
333 pp_c_identifier (pp, "<tag-error>");
335 if (TYPE_NAME (t))
336 pp_id_expression (pp, TYPE_NAME (t));
337 else
338 pp_c_identifier (pp, "<anonymous>");
339 break;
341 default:
342 pp_unsupported_tree (pp, t);
343 break;
347 /* specifier-qualifier-list:
348 type-specifier specifier-qualifier-list-opt
349 type-qualifier specifier-qualifier-list-opt
352 Implementation note: Because of the non-linearities in array or
353 function declarations, this routine prints not just the
354 specifier-qualifier-list of such entities or types of such entities,
355 but also the 'pointer' production part of their declarators. The
356 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
358 void
359 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
361 const enum tree_code code = TREE_CODE (t);
363 if (TREE_CODE (t) != POINTER_TYPE)
364 pp_c_type_qualifier_list (pp, t);
365 switch (code)
367 case REFERENCE_TYPE:
368 case POINTER_TYPE:
370 /* Get the types-specifier of this type. */
371 tree pointee = strip_pointer_operator (TREE_TYPE (t));
372 pp_c_specifier_qualifier_list (pp, pointee);
373 if (TREE_CODE (pointee) == ARRAY_TYPE
374 || TREE_CODE (pointee) == FUNCTION_TYPE)
376 pp_c_whitespace (pp);
377 pp_c_left_paren (pp);
379 pp_ptr_operator (pp, t);
381 break;
383 case FUNCTION_TYPE:
384 case ARRAY_TYPE:
385 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
386 break;
388 case VECTOR_TYPE:
389 case COMPLEX_TYPE:
390 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
391 if (code == COMPLEX_TYPE)
392 pp_c_identifier (pp, flag_isoc99 ? "_Complex" : "__complex__");
393 else if (code == VECTOR_TYPE)
394 pp_c_identifier (pp, "__vector__");
395 break;
397 default:
398 pp_simple_type_specifier (pp, t);
399 break;
403 /* parameter-type-list:
404 parameter-list
405 parameter-list , ...
407 parameter-list:
408 parameter-declaration
409 parameter-list , parameter-declaration
411 parameter-declaration:
412 declaration-specifiers declarator
413 declaration-specifiers abstract-declarator(opt) */
415 void
416 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
418 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
419 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
420 pp_c_left_paren (pp);
421 if (parms == void_list_node)
422 pp_c_identifier (pp, "void");
423 else
425 bool first = true;
426 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
428 if (!first)
429 pp_separate_with (pp, ',');
430 first = false;
431 pp_declaration_specifiers
432 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
433 if (want_parm_decl)
434 pp_declarator (pp, parms);
435 else
436 pp_abstract_declarator (pp, TREE_VALUE (parms));
439 pp_c_right_paren (pp);
442 /* abstract-declarator:
443 pointer
444 pointer(opt) direct-abstract-declarator */
446 static void
447 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
449 if (TREE_CODE (t) == POINTER_TYPE)
451 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
452 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
453 pp_c_right_paren (pp);
454 t = TREE_TYPE (t);
457 pp_direct_abstract_declarator (pp, t);
460 /* direct-abstract-declarator:
461 ( abstract-declarator )
462 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
463 direct-abstract-declarator(opt) [ * ]
464 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
466 void
467 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
469 switch (TREE_CODE (t))
471 case POINTER_TYPE:
472 pp_abstract_declarator (pp, t);
473 break;
475 case FUNCTION_TYPE:
476 pp_c_parameter_type_list (pp, t);
477 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
478 break;
480 case ARRAY_TYPE:
481 pp_c_left_bracket (pp);
482 if (TYPE_DOMAIN (t))
483 pp_expression (pp, TYPE_MAX_VALUE (TYPE_DOMAIN (t)));
484 pp_c_right_bracket (pp);
485 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
486 break;
488 case IDENTIFIER_NODE:
489 case VOID_TYPE:
490 case BOOLEAN_TYPE:
491 case INTEGER_TYPE:
492 case REAL_TYPE:
493 case ENUMERAL_TYPE:
494 case RECORD_TYPE:
495 case UNION_TYPE:
496 case VECTOR_TYPE:
497 case COMPLEX_TYPE:
498 case TYPE_DECL:
499 break;
501 default:
502 pp_unsupported_tree (pp, t);
503 break;
507 /* type-name:
508 specifier-qualifier-list abstract-declarator(opt) */
510 void
511 pp_c_type_id (c_pretty_printer *pp, tree t)
513 pp_c_specifier_qualifier_list (pp, t);
514 pp_abstract_declarator (pp, t);
517 /* storage-class-specifier:
518 typedef
519 extern
520 static
521 auto
522 register */
524 void
525 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
527 if (TREE_CODE (t) == TYPE_DECL)
528 pp_c_identifier (pp, "typedef");
529 else if (DECL_P (t))
531 if (DECL_REGISTER (t))
532 pp_c_identifier (pp, "register");
533 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
534 pp_c_identifier (pp, "static");
538 /* function-specifier:
539 inline */
541 void
542 pp_c_function_specifier (c_pretty_printer *pp, tree t)
544 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
545 pp_c_identifier (pp, "inline");
548 /* declaration-specifiers:
549 storage-class-specifier declaration-specifiers(opt)
550 type-specifier declaration-specifiers(opt)
551 type-qualifier declaration-specifiers(opt)
552 function-specifier declaration-specifiers(opt) */
554 void
555 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
557 pp_storage_class_specifier (pp, t);
558 pp_function_specifier (pp, t);
559 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
562 /* direct-declarator
563 identifier
564 ( declarator )
565 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
566 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
567 direct-declarator [ type-qualifier-list static assignment-expression ]
568 direct-declarator [ type-qualifier-list * ]
569 direct-declarator ( parameter-type-list )
570 direct-declarator ( identifier-list(opt) ) */
572 void
573 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
575 switch (TREE_CODE (t))
577 case VAR_DECL:
578 case PARM_DECL:
579 case TYPE_DECL:
580 case FIELD_DECL:
581 case LABEL_DECL:
582 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
583 pp_c_tree_decl_identifier (pp, t);
584 break;
586 case ARRAY_TYPE:
587 case POINTER_TYPE:
588 pp_abstract_declarator (pp, TREE_TYPE (t));
589 break;
591 case FUNCTION_TYPE:
592 pp_parameter_list (pp, t);
593 pp_abstract_declarator (pp, TREE_TYPE (t));
594 break;
596 case FUNCTION_DECL:
597 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
598 pp_c_tree_decl_identifier (pp, t);
599 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
600 pp_abstract_declarator (pp, TREE_TYPE (t));
601 else
603 pp_parameter_list (pp, t);
604 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
606 break;
608 case INTEGER_TYPE:
609 case REAL_TYPE:
610 case ENUMERAL_TYPE:
611 case UNION_TYPE:
612 case RECORD_TYPE:
613 break;
615 default:
616 pp_unsupported_tree (pp, t);
617 break;
622 /* declarator:
623 pointer(opt) direct-declarator */
625 void
626 pp_c_declarator (c_pretty_printer *pp, tree t)
628 switch (TREE_CODE (t))
630 case INTEGER_TYPE:
631 case REAL_TYPE:
632 case ENUMERAL_TYPE:
633 case UNION_TYPE:
634 case RECORD_TYPE:
635 break;
637 case VAR_DECL:
638 case PARM_DECL:
639 case FIELD_DECL:
640 case ARRAY_TYPE:
641 case FUNCTION_TYPE:
642 case FUNCTION_DECL:
643 case TYPE_DECL:
644 pp_direct_declarator (pp, t);
645 break;
648 default:
649 pp_unsupported_tree (pp, t);
650 break;
654 /* declaration:
655 declaration-specifiers init-declarator-list(opt) ; */
657 void
658 pp_c_declaration (c_pretty_printer *pp, tree t)
660 pp_declaration_specifiers (pp, t);
661 pp_c_init_declarator (pp, t);
664 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
666 void
667 pp_c_attributes (c_pretty_printer *pp, tree attributes)
669 if (attributes == NULL_TREE)
670 return;
672 pp_c_identifier (pp, "__attribute__");
673 pp_c_left_paren (pp);
674 pp_c_left_paren (pp);
675 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
677 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
678 if (TREE_VALUE (attributes))
679 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
681 if (TREE_CHAIN (attributes))
682 pp_separate_with (pp, ',');
684 pp_c_right_paren (pp);
685 pp_c_right_paren (pp);
688 /* function-definition:
689 declaration-specifiers declarator compound-statement */
691 void
692 pp_c_function_definition (c_pretty_printer *pp, tree t)
694 pp_declaration_specifiers (pp, t);
695 pp_declarator (pp, t);
696 pp_needs_newline (pp) = true;
697 pp_statement (pp, DECL_SAVED_TREE (t));
698 pp_newline (pp);
699 pp_flush (pp);
703 /* Expressions. */
705 /* Print out a c-char. */
707 static void
708 pp_c_char (c_pretty_printer *pp, int c)
710 switch (c)
712 case TARGET_NEWLINE:
713 pp_string (pp, "\\n");
714 break;
715 case TARGET_TAB:
716 pp_string (pp, "\\t");
717 break;
718 case TARGET_VT:
719 pp_string (pp, "\\v");
720 break;
721 case TARGET_BS:
722 pp_string (pp, "\\b");
723 break;
724 case TARGET_CR:
725 pp_string (pp, "\\r");
726 break;
727 case TARGET_FF:
728 pp_string (pp, "\\f");
729 break;
730 case TARGET_BELL:
731 pp_string (pp, "\\a");
732 break;
733 case '\\':
734 pp_string (pp, "\\\\");
735 break;
736 case '\'':
737 pp_string (pp, "\\'");
738 break;
739 case '\"':
740 pp_string (pp, "\\\"");
741 break;
742 default:
743 if (ISPRINT (c))
744 pp_character (pp, c);
745 else
746 pp_scalar (pp, "\\%03o", (unsigned) c);
747 break;
751 /* Print out a STRING literal. */
753 void
754 pp_c_string_literal (c_pretty_printer *pp, tree s)
756 const char *p = TREE_STRING_POINTER (s);
757 int n = TREE_STRING_LENGTH (s) - 1;
758 int i;
759 pp_doublequote (pp);
760 for (i = 0; i < n; ++i)
761 pp_c_char (pp, p[i]);
762 pp_doublequote (pp);
765 /* Pretty-print an INTEGER literal. */
767 static void
768 pp_c_integer_constant (c_pretty_printer *pp, tree i)
770 tree type = TREE_TYPE (i);
772 if (TREE_INT_CST_HIGH (i) == 0)
773 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
774 else
776 if (tree_int_cst_sgn (i) < 0)
778 pp_c_char (pp, '-');
779 i = build_int_2 (-TREE_INT_CST_LOW (i),
780 ~TREE_INT_CST_HIGH (i) + !TREE_INT_CST_LOW (i));
782 sprintf (pp_buffer (pp)->digit_buffer,
783 HOST_WIDE_INT_PRINT_DOUBLE_HEX,
784 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
785 pp_string (pp, pp_buffer (pp)->digit_buffer);
787 if (TYPE_UNSIGNED (type))
788 pp_character (pp, 'u');
789 if (type == long_integer_type_node || type == long_unsigned_type_node)
790 pp_character (pp, 'l');
791 else if (type == long_long_integer_type_node
792 || type == long_long_unsigned_type_node)
793 pp_string (pp, "ll");
796 /* Print out a CHARACTER literal. */
798 static void
799 pp_c_character_constant (c_pretty_printer *pp, tree c)
801 tree type = TREE_TYPE (c);
802 if (type == wchar_type_node)
803 pp_character (pp, 'L');
804 pp_quote (pp);
805 if (host_integerp (c, TYPE_UNSIGNED (type)))
806 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
807 else
808 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
809 pp_quote (pp);
812 /* Print out a BOOLEAN literal. */
814 static void
815 pp_c_bool_constant (c_pretty_printer *pp, tree b)
817 if (b == boolean_false_node)
819 if (c_dialect_cxx ())
820 pp_c_identifier (pp, "false");
821 else if (flag_isoc99)
822 pp_c_identifier (pp, "_False");
823 else
824 pp_unsupported_tree (pp, b);
826 else if (b == boolean_true_node)
828 if (c_dialect_cxx ())
829 pp_c_identifier (pp, "true");
830 else if (flag_isoc99)
831 pp_c_identifier (pp, "_True");
832 else
833 pp_unsupported_tree (pp, b);
835 else if (TREE_CODE (b) == INTEGER_CST)
836 pp_c_integer_constant (pp, b);
837 else
838 pp_unsupported_tree (pp, b);
841 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
842 false; that means the value was obtained by a cast, in which case
843 print out the type-id part of the cast-expression -- the casted value
844 is then printed by pp_c_integer_literal. */
846 static bool
847 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
849 bool value_is_named = true;
850 tree type = TREE_TYPE (e);
851 tree value;
853 /* Find the name of this constant. */
854 for (value = TYPE_VALUES (type);
855 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
856 value = TREE_CHAIN (value))
859 if (value != NULL_TREE)
860 pp_id_expression (pp, TREE_PURPOSE (value));
861 else
863 /* Value must have been cast. */
864 pp_c_type_cast (pp, type);
865 value_is_named = false;
868 return value_is_named;
871 /* Print out a REAL value as a decimal-floating-constant. */
873 static void
874 pp_c_floating_constant (c_pretty_printer *pp, tree r)
876 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
877 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
878 pp_string (pp, pp_buffer(pp)->digit_buffer);
879 if (TREE_TYPE (r) == float_type_node)
880 pp_character (pp, 'f');
881 else if (TREE_TYPE (r) == long_double_type_node)
882 pp_character (pp, 'l');
885 /* Pretty-print a compound literal expression. GNU extensions include
886 vector constants. */
888 static void
889 pp_c_compound_literal (c_pretty_printer *pp, tree e)
891 tree type = TREE_TYPE (e);
892 pp_c_type_cast (pp, type);
894 switch (TREE_CODE (type))
896 case RECORD_TYPE:
897 case UNION_TYPE:
898 case ARRAY_TYPE:
899 case VECTOR_TYPE:
900 case COMPLEX_TYPE:
901 pp_c_brace_enclosed_initializer_list (pp, e);
902 break;
904 default:
905 pp_unsupported_tree (pp, e);
906 break;
910 /* constant:
911 integer-constant
912 floating-constant
913 enumeration-constant
914 character-constant */
916 void
917 pp_c_constant (c_pretty_printer *pp, tree e)
919 const enum tree_code code = TREE_CODE (e);
921 switch (code)
923 case INTEGER_CST:
925 tree type = TREE_TYPE (e);
926 if (type == boolean_type_node)
927 pp_c_bool_constant (pp, e);
928 else if (type == char_type_node)
929 pp_c_character_constant (pp, e);
930 else if (TREE_CODE (type) == ENUMERAL_TYPE
931 && pp_c_enumeration_constant (pp, e))
933 else
934 pp_c_integer_constant (pp, e);
936 break;
938 case REAL_CST:
939 pp_c_floating_constant (pp, e);
940 break;
942 case STRING_CST:
943 pp_c_string_literal (pp, e);
944 break;
946 default:
947 pp_unsupported_tree (pp, e);
948 break;
952 /* Pretty-print an IDENTIFIER_NODE, preceded by whitespace is necessary. */
954 void
955 pp_c_identifier (c_pretty_printer *pp, const char *id)
957 pp_c_maybe_whitespace (pp);
958 pp_identifier (pp, id);
959 pp_base (pp)->padding = pp_before;
962 /* Pretty-print a C primary-expression.
963 primary-expression:
964 identifier
965 constant
966 string-literal
967 ( expression ) */
969 void
970 pp_c_primary_expression (c_pretty_printer *pp, tree e)
972 switch (TREE_CODE (e))
974 case VAR_DECL:
975 case PARM_DECL:
976 case FIELD_DECL:
977 case CONST_DECL:
978 case FUNCTION_DECL:
979 case LABEL_DECL:
980 pp_c_tree_decl_identifier (pp, e);
981 break;
983 case IDENTIFIER_NODE:
984 pp_c_tree_identifier (pp, e);
985 break;
987 case ERROR_MARK:
988 pp_c_identifier (pp, "<erroneous-expression>");
989 break;
991 case RESULT_DECL:
992 pp_c_identifier (pp, "<return-value>");
993 break;
995 case INTEGER_CST:
996 case REAL_CST:
997 case STRING_CST:
998 pp_c_constant (pp, e);
999 break;
1001 case TARGET_EXPR:
1002 pp_c_identifier (pp, "__builtin_memcpy");
1003 pp_c_left_paren (pp);
1004 pp_ampersand (pp);
1005 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1006 pp_separate_with (pp, ',');
1007 pp_ampersand (pp);
1008 pp_initializer (pp, TREE_OPERAND (e, 1));
1009 if (TREE_OPERAND (e, 2))
1011 pp_separate_with (pp, ',');
1012 pp_c_expression (pp, TREE_OPERAND (e, 2));
1014 pp_c_right_paren (pp);
1015 break;
1017 case STMT_EXPR:
1018 pp_c_left_paren (pp);
1019 pp_statement (pp, STMT_EXPR_STMT (e));
1020 pp_c_right_paren (pp);
1021 break;
1023 default:
1024 /* FIXME: Make sure we won't get into an infinie loop. */
1025 pp_c_left_paren (pp);
1026 pp_expression (pp, e);
1027 pp_c_right_paren (pp);
1028 break;
1032 /* Print out a C initializer -- also support C compound-literals.
1033 initializer:
1034 assignment-expression:
1035 { initializer-list }
1036 { initializer-list , } */
1038 static void
1039 pp_c_initializer (c_pretty_printer *pp, tree e)
1041 if (TREE_CODE (e) == CONSTRUCTOR)
1042 pp_c_brace_enclosed_initializer_list (pp, e);
1043 else
1044 pp_expression (pp, e);
1047 /* init-declarator:
1048 declarator:
1049 declarator = initializer */
1051 void
1052 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1054 pp_declarator (pp, t);
1055 /* We don't want to output function definitions here. There are handled
1056 elsewhere (and the syntactic form is bogus anyway). */
1057 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1059 tree init = DECL_INITIAL (t);
1060 /* This C++ bit is handled here because it is easier to do so.
1061 In templates, the C++ parser builds a TREE_LIST for a
1062 direct-initialization; the TREE_PURPOSE is the variable to
1063 initialize and the TREE_VALUE is the initializer. */
1064 if (TREE_CODE (init) == TREE_LIST)
1066 pp_c_left_paren (pp);
1067 pp_expression (pp, TREE_VALUE (init));
1068 pp_right_paren (pp);
1070 else
1072 pp_space (pp);
1073 pp_equal (pp);
1074 pp_space (pp);
1075 pp_c_initializer (pp, init);
1080 /* initializer-list:
1081 designation(opt) initializer
1082 initializer-list , designation(opt) initializer
1084 designation:
1085 designator-list =
1087 designator-list:
1088 designator
1089 designator-list designator
1091 designator:
1092 [ constant-expression ]
1093 identifier */
1095 static void
1096 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1098 tree type = TREE_TYPE (e);
1099 const enum tree_code code = TREE_CODE (type);
1101 switch (code)
1103 case RECORD_TYPE:
1104 case UNION_TYPE:
1105 case ARRAY_TYPE:
1107 tree init = TREE_OPERAND (e, 0);
1108 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1110 if (code == RECORD_TYPE || code == UNION_TYPE)
1112 pp_c_dot (pp);
1113 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1115 else
1117 pp_c_left_bracket (pp);
1118 if (TREE_PURPOSE (init))
1119 pp_c_constant (pp, TREE_PURPOSE (init));
1120 pp_c_right_bracket (pp);
1122 pp_c_whitespace (pp);
1123 pp_equal (pp);
1124 pp_c_whitespace (pp);
1125 pp_initializer (pp, TREE_VALUE (init));
1126 if (TREE_CHAIN (init))
1127 pp_separate_with (pp, ',');
1130 return;
1132 case VECTOR_TYPE:
1133 if (TREE_CODE (e) == VECTOR_CST)
1134 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1135 else if (TREE_CODE (e) == CONSTRUCTOR)
1136 pp_c_expression_list (pp, CONSTRUCTOR_ELTS (e));
1137 else
1138 break;
1139 return;
1141 case COMPLEX_TYPE:
1142 if (TREE_CODE (e) == CONSTRUCTOR)
1143 pp_c_expression_list (pp, CONSTRUCTOR_ELTS (e));
1144 else if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1146 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1147 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1148 pp_separate_with (pp, ',');
1149 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1151 else
1152 break;
1153 return;
1155 default:
1156 break;
1159 pp_unsupported_tree (pp, type);
1162 /* Pretty-print a brace-enclosed initializer-list. */
1164 static void
1165 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1167 pp_c_left_brace (pp);
1168 pp_c_initializer_list (pp, l);
1169 pp_c_right_brace (pp);
1173 /* This is a convenient function, used to bridge gap between C and C++
1174 grammars.
1176 id-expression:
1177 identifier */
1179 void
1180 pp_c_id_expression (c_pretty_printer *pp, tree t)
1182 switch (TREE_CODE (t))
1184 case VAR_DECL:
1185 case PARM_DECL:
1186 case CONST_DECL:
1187 case TYPE_DECL:
1188 case FUNCTION_DECL:
1189 case FIELD_DECL:
1190 case LABEL_DECL:
1191 pp_c_tree_decl_identifier (pp, t);
1192 break;
1194 case IDENTIFIER_NODE:
1195 pp_c_tree_identifier (pp, t);
1196 break;
1198 default:
1199 pp_unsupported_tree (pp, t);
1200 break;
1204 /* postfix-expression:
1205 primary-expression
1206 postfix-expression [ expression ]
1207 postfix-expression ( argument-expression-list(opt) )
1208 postfix-expression . identifier
1209 postfix-expression -> identifier
1210 postfix-expression ++
1211 postfix-expression --
1212 ( type-name ) { initializer-list }
1213 ( type-name ) { initializer-list , } */
1215 void
1216 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1218 enum tree_code code = TREE_CODE (e);
1219 switch (code)
1221 case POSTINCREMENT_EXPR:
1222 case POSTDECREMENT_EXPR:
1223 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1224 pp_identifier (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1225 break;
1227 case ARROW_EXPR:
1228 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1229 pp_c_arrow (pp);
1230 break;
1232 case ARRAY_REF:
1233 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1234 pp_c_left_bracket (pp);
1235 pp_expression (pp, TREE_OPERAND (e, 1));
1236 pp_c_right_bracket (pp);
1237 break;
1239 case CALL_EXPR:
1240 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1241 pp_c_call_argument_list (pp, TREE_OPERAND (e, 1));
1242 break;
1244 case ABS_EXPR:
1245 pp_c_identifier (pp, "__builtin_abs");
1246 pp_c_left_paren (pp);
1247 pp_expression (pp, TREE_OPERAND (e, 0));
1248 pp_c_right_paren (pp);
1249 break;
1251 case COMPONENT_REF:
1253 tree object = TREE_OPERAND (e, 0);
1254 if (TREE_CODE (object) == INDIRECT_REF)
1256 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1257 pp_c_arrow (pp);
1259 else
1261 pp_postfix_expression (pp, object);
1262 pp_c_dot (pp);
1264 pp_expression (pp, TREE_OPERAND (e, 1));
1266 break;
1268 case COMPLEX_CST:
1269 case VECTOR_CST:
1270 case COMPLEX_EXPR:
1271 pp_c_compound_literal (pp, e);
1272 break;
1274 case COMPOUND_LITERAL_EXPR:
1275 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1276 /* Fall through. */
1277 case CONSTRUCTOR:
1278 pp_initializer (pp, e);
1279 break;
1281 case VA_ARG_EXPR:
1282 pp_c_identifier (pp, "__builtin_va_arg");
1283 pp_c_left_paren (pp);
1284 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1285 pp_separate_with (pp, ',');
1286 pp_type_id (pp, TREE_TYPE (e));
1287 pp_c_right_paren (pp);
1288 break;
1290 case ADDR_EXPR:
1291 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1293 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1294 break;
1296 /* else fall through. */
1298 default:
1299 pp_primary_expression (pp, e);
1300 break;
1304 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1306 void
1307 pp_c_expression_list (c_pretty_printer *pp, tree e)
1309 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1311 pp_expression (pp, TREE_VALUE (e));
1312 if (TREE_CHAIN (e))
1313 pp_separate_with (pp, ',');
1317 /* Print out an expression-list in parens, as in a function call. */
1319 void
1320 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1322 pp_c_left_paren (pp);
1323 if (t && TREE_CODE (t) == TREE_LIST)
1324 pp_c_expression_list (pp, t);
1325 pp_c_right_paren (pp);
1328 /* unary-expression:
1329 postfix-expression
1330 ++ cast-expression
1331 -- cast-expression
1332 unary-operator cast-expression
1333 sizeof unary-expression
1334 sizeof ( type-id )
1336 unary-operator: one of
1337 * & + - ! ~
1339 GNU extensions.
1340 unary-expression:
1341 __alignof__ unary-expression
1342 __alignof__ ( type-id )
1343 __real__ unary-expression
1344 __imag__ unary-expression */
1346 void
1347 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1349 enum tree_code code = TREE_CODE (e);
1350 switch (code)
1352 case PREINCREMENT_EXPR:
1353 case PREDECREMENT_EXPR:
1354 pp_identifier (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1355 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1356 break;
1358 case ADDR_EXPR:
1359 case INDIRECT_REF:
1360 case NEGATE_EXPR:
1361 case BIT_NOT_EXPR:
1362 case TRUTH_NOT_EXPR:
1363 case CONJ_EXPR:
1364 /* String literal are used by address. */
1365 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1366 pp_ampersand (pp);
1367 else if (code == INDIRECT_REF)
1368 pp_c_star (pp);
1369 else if (code == NEGATE_EXPR)
1370 pp_minus (pp);
1371 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1372 pp_complement (pp);
1373 else if (code == TRUTH_NOT_EXPR)
1374 pp_exclamation (pp);
1375 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1376 break;
1378 case SIZEOF_EXPR:
1379 case ALIGNOF_EXPR:
1380 pp_c_identifier (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
1381 pp_c_whitespace (pp);
1382 if (TYPE_P (TREE_OPERAND (e, 0)))
1383 pp_c_type_cast (pp, TREE_OPERAND (e, 0));
1384 else
1385 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1386 break;
1388 case REALPART_EXPR:
1389 case IMAGPART_EXPR:
1390 pp_c_identifier (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1391 pp_c_whitespace (pp);
1392 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1393 break;
1395 default:
1396 pp_postfix_expression (pp, e);
1397 break;
1401 /* cast-expression:
1402 unary-expression
1403 ( type-name ) cast-expression */
1405 void
1406 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1408 switch (TREE_CODE (e))
1410 case FLOAT_EXPR:
1411 case FIX_TRUNC_EXPR:
1412 case CONVERT_EXPR:
1413 pp_c_type_cast (pp, TREE_TYPE (e));
1414 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1415 break;
1417 default:
1418 pp_unary_expression (pp, e);
1422 /* multiplicative-expression:
1423 cast-expression
1424 multiplicative-expression * cast-expression
1425 multiplicative-expression / cast-expression
1426 multiplicative-expression % cast-expression */
1428 static void
1429 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1431 enum tree_code code = TREE_CODE (e);
1432 switch (code)
1434 case MULT_EXPR:
1435 case TRUNC_DIV_EXPR:
1436 case TRUNC_MOD_EXPR:
1437 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1438 pp_c_whitespace (pp);
1439 if (code == MULT_EXPR)
1440 pp_c_star (pp);
1441 else if (code == TRUNC_DIV_EXPR)
1442 pp_slash (pp);
1443 else
1444 pp_modulo (pp);
1445 pp_c_whitespace (pp);
1446 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1447 break;
1449 default:
1450 pp_c_cast_expression (pp, e);
1451 break;
1455 /* additive-expression:
1456 multiplicative-expression
1457 additive-expression + multiplicative-expression
1458 additive-expression - multiplicative-expression */
1460 static void
1461 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1463 enum tree_code code = TREE_CODE (e);
1464 switch (code)
1466 case PLUS_EXPR:
1467 case MINUS_EXPR:
1468 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1469 pp_c_whitespace (pp);
1470 if (code == PLUS_EXPR)
1471 pp_plus (pp);
1472 else
1473 pp_minus (pp);
1474 pp_c_whitespace (pp);
1475 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1476 break;
1478 default:
1479 pp_multiplicative_expression (pp, e);
1480 break;
1484 /* additive-expression:
1485 additive-expression
1486 shift-expression << additive-expression
1487 shift-expression >> additive-expression */
1489 static void
1490 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1492 enum tree_code code = TREE_CODE (e);
1493 switch (code)
1495 case LSHIFT_EXPR:
1496 case RSHIFT_EXPR:
1497 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1498 pp_c_whitespace (pp);
1499 pp_identifier (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1500 pp_c_whitespace (pp);
1501 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1502 break;
1504 default:
1505 pp_c_additive_expression (pp, e);
1509 /* relational-expression:
1510 shift-expression
1511 relational-expression < shift-expression
1512 relational-expression > shift-expression
1513 relational-expression <= shift-expression
1514 relational-expression >= shift-expression */
1516 static void
1517 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1519 enum tree_code code = TREE_CODE (e);
1520 switch (code)
1522 case LT_EXPR:
1523 case GT_EXPR:
1524 case LE_EXPR:
1525 case GE_EXPR:
1526 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1527 pp_c_whitespace (pp);
1528 if (code == LT_EXPR)
1529 pp_less (pp);
1530 else if (code == GT_EXPR)
1531 pp_greater (pp);
1532 else if (code == LE_EXPR)
1533 pp_identifier (pp, "<=");
1534 else if (code == GE_EXPR)
1535 pp_identifier (pp, ">=");
1536 pp_c_whitespace (pp);
1537 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1538 break;
1540 default:
1541 pp_c_shift_expression (pp, e);
1542 break;
1546 /* equality-expression:
1547 relational-expression
1548 equality-expression == relational-expression
1549 equality-equality != relational-expression */
1551 static void
1552 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1554 enum tree_code code = TREE_CODE (e);
1555 switch (code)
1557 case EQ_EXPR:
1558 case NE_EXPR:
1559 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1560 pp_c_whitespace (pp);
1561 pp_identifier (pp, code == EQ_EXPR ? "==" : "!=");
1562 pp_c_whitespace (pp);
1563 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1564 break;
1566 default:
1567 pp_c_relational_expression (pp, e);
1568 break;
1572 /* AND-expression:
1573 equality-expression
1574 AND-expression & equality-equality */
1576 static void
1577 pp_c_and_expression (c_pretty_printer *pp, tree e)
1579 if (TREE_CODE (e) == BIT_AND_EXPR)
1581 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1582 pp_c_whitespace (pp);
1583 pp_ampersand (pp);
1584 pp_c_whitespace (pp);
1585 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1587 else
1588 pp_c_equality_expression (pp, e);
1591 /* exclusive-OR-expression:
1592 AND-expression
1593 exclusive-OR-expression ^ AND-expression */
1595 static void
1596 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1598 if (TREE_CODE (e) == BIT_XOR_EXPR)
1600 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1601 pp_c_maybe_whitespace (pp);
1602 pp_carret (pp);
1603 pp_c_whitespace (pp);
1604 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1606 else
1607 pp_c_and_expression (pp, e);
1610 /* inclusive-OR-expression:
1611 exclusive-OR-expression
1612 inclusive-OR-expression | exclusive-OR-expression */
1614 static void
1615 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1617 if (TREE_CODE (e) == BIT_IOR_EXPR)
1619 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1620 pp_c_whitespace (pp);
1621 pp_bar (pp);
1622 pp_c_whitespace (pp);
1623 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1625 else
1626 pp_c_exclusive_or_expression (pp, e);
1629 /* logical-AND-expression:
1630 inclusive-OR-expression
1631 logical-AND-expression && inclusive-OR-expression */
1633 static void
1634 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1636 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
1638 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1639 pp_c_whitespace (pp);
1640 pp_identifier (pp, "&&");
1641 pp_c_whitespace (pp);
1642 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1644 else
1645 pp_c_inclusive_or_expression (pp, e);
1648 /* logical-OR-expression:
1649 logical-AND-expression
1650 logical-OR-expression || logical-AND-expression */
1652 void
1653 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1655 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1657 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1658 pp_c_whitespace (pp);
1659 pp_identifier (pp, "||");
1660 pp_c_whitespace (pp);
1661 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1663 else
1664 pp_c_logical_and_expression (pp, e);
1667 /* conditional-expression:
1668 logical-OR-expression
1669 logical-OR-expression ? expression : conditional-expression */
1671 static void
1672 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1674 if (TREE_CODE (e) == COND_EXPR)
1676 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1677 pp_c_whitespace (pp);
1678 pp_question (pp);
1679 pp_c_whitespace (pp);
1680 pp_expression (pp, TREE_OPERAND (e, 1));
1681 pp_c_whitespace (pp);
1682 pp_colon (pp);
1683 pp_c_whitespace (pp);
1684 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1686 else
1687 pp_c_logical_or_expression (pp, e);
1691 /* assignment-expression:
1692 conditional-expression
1693 unary-expression assignment-operator assignment-expression
1695 assignment-expression: one of
1696 = *= /= %= += -= >>= <<= &= ^= |= */
1698 static void
1699 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1701 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1703 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1704 pp_c_whitespace (pp);
1705 pp_equal (pp);
1706 pp_space (pp);
1707 pp_c_expression (pp, TREE_OPERAND (e, 1));
1709 else
1710 pp_c_conditional_expression (pp, e);
1713 /* expression:
1714 assignment-expression
1715 expression , assignment-expression
1717 Implementation note: instead of going through the usual recursion
1718 chain, I take the liberty of dispatching nodes to the appropriate
1719 functions. This makes some redundancy, but it worths it. That also
1720 prevents a possible infinite recursion between pp_c_primary_expression ()
1721 and pp_c_expression (). */
1723 void
1724 pp_c_expression (c_pretty_printer *pp, tree e)
1726 switch (TREE_CODE (e))
1728 case INTEGER_CST:
1729 pp_c_integer_constant (pp, e);
1730 break;
1732 case REAL_CST:
1733 pp_c_floating_constant (pp, e);
1734 break;
1736 case STRING_CST:
1737 pp_c_string_literal (pp, e);
1738 break;
1740 case IDENTIFIER_NODE:
1741 case FUNCTION_DECL:
1742 case VAR_DECL:
1743 case CONST_DECL:
1744 case PARM_DECL:
1745 case RESULT_DECL:
1746 case FIELD_DECL:
1747 case LABEL_DECL:
1748 case ERROR_MARK:
1749 case STMT_EXPR:
1750 pp_primary_expression (pp, e);
1751 break;
1753 case POSTINCREMENT_EXPR:
1754 case POSTDECREMENT_EXPR:
1755 case ARROW_EXPR:
1756 case ARRAY_REF:
1757 case CALL_EXPR:
1758 case COMPONENT_REF:
1759 case COMPLEX_CST:
1760 case COMPLEX_EXPR:
1761 case VECTOR_CST:
1762 case ABS_EXPR:
1763 case CONSTRUCTOR:
1764 case COMPOUND_LITERAL_EXPR:
1765 case VA_ARG_EXPR:
1766 pp_postfix_expression (pp, e);
1767 break;
1769 case CONJ_EXPR:
1770 case ADDR_EXPR:
1771 case INDIRECT_REF:
1772 case NEGATE_EXPR:
1773 case BIT_NOT_EXPR:
1774 case TRUTH_NOT_EXPR:
1775 case PREINCREMENT_EXPR:
1776 case PREDECREMENT_EXPR:
1777 case SIZEOF_EXPR:
1778 case ALIGNOF_EXPR:
1779 case REALPART_EXPR:
1780 case IMAGPART_EXPR:
1781 pp_c_unary_expression (pp, e);
1782 break;
1784 case FLOAT_EXPR:
1785 case FIX_TRUNC_EXPR:
1786 case CONVERT_EXPR:
1787 pp_c_cast_expression (pp, e);
1788 break;
1790 case MULT_EXPR:
1791 case TRUNC_MOD_EXPR:
1792 case TRUNC_DIV_EXPR:
1793 pp_multiplicative_expression (pp, e);
1794 break;
1796 case LSHIFT_EXPR:
1797 case RSHIFT_EXPR:
1798 pp_c_shift_expression (pp, e);
1799 break;
1801 case LT_EXPR:
1802 case GT_EXPR:
1803 case LE_EXPR:
1804 case GE_EXPR:
1805 pp_c_relational_expression (pp, e);
1806 break;
1808 case BIT_AND_EXPR:
1809 pp_c_and_expression (pp, e);
1810 break;
1812 case BIT_XOR_EXPR:
1813 pp_c_exclusive_or_expression (pp, e);
1814 break;
1816 case BIT_IOR_EXPR:
1817 pp_c_inclusive_or_expression (pp, e);
1818 break;
1820 case TRUTH_ANDIF_EXPR:
1821 pp_c_logical_and_expression (pp, e);
1822 break;
1824 case TRUTH_ORIF_EXPR:
1825 pp_c_logical_or_expression (pp, e);
1826 break;
1828 case EQ_EXPR:
1829 case NE_EXPR:
1830 pp_c_equality_expression (pp, e);
1831 break;
1833 case COND_EXPR:
1834 pp_conditional_expression (pp, e);
1835 break;
1837 case PLUS_EXPR:
1838 case MINUS_EXPR:
1839 pp_c_additive_expression (pp, e);
1840 break;
1842 case MODIFY_EXPR:
1843 case INIT_EXPR:
1844 pp_assignment_expression (pp, e);
1845 break;
1847 case COMPOUND_EXPR:
1848 pp_c_left_paren (pp);
1849 pp_expression (pp, TREE_OPERAND (e, 0));
1850 pp_separate_with (pp, ',');
1851 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
1852 pp_c_right_paren (pp);
1853 break;
1855 case NOP_EXPR:
1856 case NON_LVALUE_EXPR:
1857 case SAVE_EXPR:
1858 case UNSAVE_EXPR:
1859 pp_expression (pp, TREE_OPERAND (e, 0));
1860 break;
1862 case TARGET_EXPR:
1863 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
1864 break;
1866 default:
1867 pp_unsupported_tree (pp, e);
1868 break;
1874 /* Statements. */
1876 /* statement:
1877 labeled-statement
1878 compound-statement
1879 expression-statement
1880 selection-statement
1881 iteration-statement
1882 jump-statement */
1884 void
1885 pp_c_statement (c_pretty_printer *pp, tree stmt)
1887 enum tree_code code;
1889 if (stmt == NULL)
1890 return;
1892 code = TREE_CODE (stmt);
1893 switch (code)
1895 /* labeled-statement:
1896 identifier : statement
1897 case constant-expression : statement
1898 default : statement */
1899 case LABEL_STMT:
1900 case CASE_LABEL:
1901 if (pp_needs_newline (pp))
1902 pp_newline_and_indent (pp, -3);
1903 else
1904 pp_indentation (pp) -= 3;
1905 if (code == LABEL_STMT)
1906 pp_c_tree_decl_identifier (pp, LABEL_STMT_LABEL (stmt));
1907 else if (code == CASE_LABEL)
1909 if (CASE_LOW (stmt) == NULL_TREE)
1910 pp_identifier (pp, "default");
1911 else
1913 pp_c_identifier (pp, "case");
1914 pp_c_whitespace (pp);
1915 pp_conditional_expression (pp, CASE_LOW (stmt));
1916 if (CASE_HIGH (stmt))
1918 pp_identifier (pp, "...");
1919 pp_conditional_expression (pp, CASE_HIGH (stmt));
1923 pp_colon (pp);
1924 pp_indentation (pp) += 3;
1925 pp_needs_newline (pp) = true;
1926 break;
1928 /* compound-statement:
1929 { block-item-list(opt) }
1931 block-item-list:
1932 block-item
1933 block-item-list block-item
1935 block-item:
1936 declaration
1937 statement */
1938 case COMPOUND_STMT:
1939 if (pp_needs_newline (pp))
1940 pp_newline_and_indent (pp, 0);
1941 pp_c_left_brace (pp);
1942 pp_newline_and_indent (pp, 3);
1943 for (stmt = COMPOUND_BODY (stmt); stmt; stmt = TREE_CHAIN (stmt))
1944 pp_statement (pp, stmt);
1945 pp_newline_and_indent (pp, -3);
1946 pp_c_right_brace (pp);
1947 pp_needs_newline (pp) = true;
1948 break;
1950 /* expression-statement:
1951 expression(opt) ; */
1952 case EXPR_STMT:
1953 case CLEANUP_STMT:
1954 if (pp_needs_newline (pp))
1955 pp_newline_and_indent (pp, 0);
1957 tree e = code == EXPR_STMT
1958 ? EXPR_STMT_EXPR (stmt)
1959 : CLEANUP_EXPR (stmt);
1960 if (e)
1961 pp_expression (pp, e);
1963 pp_c_semicolon (pp);
1964 pp_needs_newline (pp) = true;
1965 break;
1967 /* selection-statement:
1968 if ( expression ) statement
1969 if ( expression ) statement else statement
1970 switch ( expression ) statement */
1971 case IF_STMT:
1972 if (pp_needs_newline (pp))
1973 pp_newline_and_indent (pp, 0);
1974 pp_c_identifier (pp, "if");
1975 pp_c_whitespace (pp);
1976 pp_c_left_paren (pp);
1977 pp_expression (pp, IF_COND (stmt));
1978 pp_c_right_paren (pp);
1979 pp_newline_and_indent (pp, 3);
1980 pp_statement (pp, THEN_CLAUSE (stmt));
1981 pp_newline_and_indent (pp, -3);
1982 if (ELSE_CLAUSE (stmt))
1984 tree else_clause = ELSE_CLAUSE (stmt);
1985 pp_c_identifier (pp, "else");
1986 if (TREE_CODE (else_clause) == IF_STMT)
1987 pp_c_whitespace (pp);
1988 else
1989 pp_newline_and_indent (pp, 3);
1990 pp_statement (pp, else_clause);
1991 if (TREE_CODE (else_clause) != IF_STMT)
1992 pp_newline_and_indent (pp, -3);
1994 break;
1996 case SWITCH_STMT:
1997 if (pp_needs_newline (pp))
1998 pp_newline_and_indent (pp, 0);
1999 pp_c_identifier (pp, "switch");
2000 pp_space (pp);
2001 pp_c_left_paren (pp);
2002 pp_expression (pp, SWITCH_COND (stmt));
2003 pp_c_right_paren (pp);
2004 pp_indentation (pp) += 3;
2005 pp_needs_newline (pp) = true;
2006 pp_statement (pp, SWITCH_BODY (stmt));
2007 pp_newline_and_indent (pp, -3);
2008 break;
2010 /* iteration-statement:
2011 while ( expression ) statement
2012 do statement while ( expression ) ;
2013 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
2014 for ( declaration expression(opt) ; expression(opt) ) statement */
2015 case WHILE_STMT:
2016 if (pp_needs_newline (pp))
2017 pp_newline_and_indent (pp, 0);
2018 pp_c_identifier (pp, "while");
2019 pp_space (pp);
2020 pp_c_left_paren (pp);
2021 pp_expression (pp, WHILE_COND (stmt));
2022 pp_c_right_paren (pp);
2023 pp_newline_and_indent (pp, 3);
2024 pp_statement (pp, WHILE_BODY (stmt));
2025 pp_indentation (pp) -= 3;
2026 pp_needs_newline (pp) = true;
2027 break;
2029 case DO_STMT:
2030 if (pp_needs_newline (pp))
2031 pp_newline_and_indent (pp, 0);
2032 pp_c_identifier (pp, "do");
2033 pp_newline_and_indent (pp, 3);
2034 pp_statement (pp, DO_BODY (stmt));
2035 pp_newline_and_indent (pp, -3);
2036 pp_c_identifier (pp, "while");
2037 pp_space (pp);
2038 pp_c_left_paren (pp);
2039 pp_expression (pp, DO_COND (stmt));
2040 pp_c_right_paren (pp);
2041 pp_c_semicolon (pp);
2042 pp_needs_newline (pp) = true;
2043 break;
2045 case FOR_STMT:
2046 if (pp_needs_newline (pp))
2047 pp_newline_and_indent (pp, 0);
2048 pp_c_identifier (pp, "for");
2049 pp_space (pp);
2050 pp_c_left_paren (pp);
2051 if (FOR_INIT_STMT (stmt))
2052 pp_statement (pp, FOR_INIT_STMT (stmt));
2053 else
2054 pp_c_semicolon (pp);
2055 pp_needs_newline (pp) = false;
2056 pp_c_whitespace (pp);
2057 if (FOR_COND (stmt))
2058 pp_expression (pp, FOR_COND (stmt));
2059 pp_c_semicolon (pp);
2060 pp_needs_newline (pp) = false;
2061 pp_c_whitespace (pp);
2062 if (FOR_EXPR (stmt))
2063 pp_expression (pp, FOR_EXPR (stmt));
2064 pp_c_right_paren (pp);
2065 pp_newline_and_indent (pp, 3);
2066 pp_statement (pp, FOR_BODY (stmt));
2067 pp_indentation (pp) -= 3;
2068 pp_needs_newline (pp) = true;
2069 break;
2071 /* jump-statement:
2072 goto identifier;
2073 continue ;
2074 return expression(opt) ; */
2075 case BREAK_STMT:
2076 case CONTINUE_STMT:
2077 if (pp_needs_newline (pp))
2078 pp_newline_and_indent (pp, 0);
2079 pp_identifier (pp, code == BREAK_STMT ? "break" : "continue");
2080 pp_c_semicolon (pp);
2081 pp_needs_newline (pp) = true;
2082 break;
2084 case RETURN_STMT:
2085 case GOTO_STMT:
2087 tree e = code == RETURN_STMT
2088 ? RETURN_STMT_EXPR (stmt)
2089 : GOTO_DESTINATION (stmt);
2090 if (pp_needs_newline (pp))
2091 pp_newline_and_indent (pp, 0);
2092 pp_c_identifier (pp, code == RETURN_STMT ? "return" : "goto");
2093 pp_c_whitespace (pp);
2094 if (e)
2096 if (TREE_CODE (e) == INIT_EXPR
2097 && TREE_CODE (TREE_OPERAND (e, 0)) == RESULT_DECL)
2098 e = TREE_OPERAND (e, 1);
2099 pp_expression (pp, e);
2101 pp_c_semicolon (pp);
2102 pp_needs_newline (pp) = true;
2104 break;
2106 case SCOPE_STMT:
2107 if (!SCOPE_NULLIFIED_P (stmt) && SCOPE_NO_CLEANUPS_P (stmt))
2109 int i = 0;
2110 if (pp_needs_newline (pp))
2111 pp_newline_and_indent (pp, 0);
2112 if (SCOPE_BEGIN_P (stmt))
2114 pp_left_brace (pp);
2115 i = 3;
2117 else if (SCOPE_END_P (stmt))
2119 pp_right_brace (pp);
2120 i = -3;
2122 pp_indentation (pp) += i;
2123 pp_needs_newline (pp) = true;
2125 break;
2127 case DECL_STMT:
2128 if (pp_needs_newline (pp))
2129 pp_newline_and_indent (pp, 0);
2130 pp_declaration (pp, DECL_STMT_DECL (stmt));
2131 pp_needs_newline (pp) = true;
2132 break;
2134 case ASM_STMT:
2136 bool has_volatile_p = ASM_VOLATILE_P (stmt);
2137 bool is_extended = has_volatile_p || ASM_INPUTS (stmt)
2138 || ASM_OUTPUTS (stmt) || ASM_CLOBBERS (stmt);
2139 pp_c_identifier (pp, is_extended ? "__asm__" : "asm");
2140 if (has_volatile_p)
2141 pp_c_identifier (pp, "__volatile__");
2142 pp_space (pp);
2143 pp_c_left_paren (pp);
2144 pp_c_string_literal (pp, ASM_STRING (stmt));
2145 if (is_extended)
2147 pp_space (pp);
2148 pp_separate_with (pp, ':');
2149 if (ASM_OUTPUTS (stmt))
2150 pp_expression (pp, ASM_OUTPUTS (stmt));
2151 pp_space (pp);
2152 pp_separate_with (pp, ':');
2153 if (ASM_INPUTS (stmt))
2154 pp_expression (pp, ASM_INPUTS (stmt));
2155 pp_space (pp);
2156 pp_separate_with (pp, ':');
2157 if (ASM_CLOBBERS (stmt))
2158 pp_expression (pp, ASM_CLOBBERS (stmt));
2160 pp_c_right_paren (pp);
2161 pp_newline (pp);
2163 break;
2165 default:
2166 pp_unsupported_tree (pp, stmt);
2171 /* Initialize the PRETTY-PRINTER for handling C codes. */
2173 void
2174 pp_c_pretty_printer_init (c_pretty_printer *pp)
2176 pp->offset_list = 0;
2178 pp->declaration = pp_c_declaration;
2179 pp->declaration_specifiers = pp_c_declaration_specifiers;
2180 pp->declarator = pp_c_declarator;
2181 pp->direct_declarator = pp_c_direct_declarator;
2182 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
2183 pp->abstract_declarator = pp_c_abstract_declarator;
2184 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
2185 pp->ptr_operator = pp_c_pointer;
2186 pp->parameter_list = pp_c_parameter_type_list;
2187 pp->type_id = pp_c_type_id;
2188 pp->simple_type_specifier = pp_c_type_specifier;
2189 pp->function_specifier = pp_c_function_specifier;
2190 pp->storage_class_specifier = pp_c_storage_class_specifier;
2192 pp->statement = pp_c_statement;
2194 pp->id_expression = pp_c_id_expression;
2195 pp->primary_expression = pp_c_primary_expression;
2196 pp->postfix_expression = pp_c_postfix_expression;
2197 pp->unary_expression = pp_c_unary_expression;
2198 pp->initializer = pp_c_initializer;
2199 pp->multiplicative_expression = pp_c_multiplicative_expression;
2200 pp->conditional_expression = pp_c_conditional_expression;
2201 pp->assignment_expression = pp_c_assignment_expression;
2202 pp->expression = pp_c_expression;
2206 /* Print the tree T in full, on file FILE. */
2208 void
2209 print_c_tree (FILE *file, tree t)
2211 static c_pretty_printer pp_rec;
2212 static bool initialized = 0;
2213 c_pretty_printer *pp = &pp_rec;
2215 if (!initialized)
2217 initialized = 1;
2218 pp_construct (pp_base (pp), NULL, 0);
2219 pp_c_pretty_printer_init (pp);
2220 pp_needs_newline (pp) = true;
2222 pp_base (pp)->buffer->stream = file;
2224 pp_statement (pp, t);
2226 pp_newline (pp);
2227 pp_flush (pp);
2230 /* Print the tree T in full, on stderr. */
2232 void
2233 debug_c_tree (tree t)
2235 print_c_tree (stderr, t);
2236 fputc ('\n', stderr);
2239 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2240 up of T's memory address. */
2242 void
2243 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2245 const char *name;
2247 if (!DECL_P (t))
2248 abort ();
2250 if (DECL_NAME (t))
2251 name = IDENTIFIER_POINTER (DECL_NAME (t));
2252 else
2254 static char xname[8];
2255 sprintf (xname, "<U%4x>", ((unsigned)((unsigned long)(t) & 0xffff)));
2256 name = xname;
2259 pp_c_identifier (pp, name);