2004-06-22 Eric Christopher <echristo@redhat.com>
[official-gcc.git] / gcc / c-pretty-print.c
blob4d0b06d5de9f5d379a4c027d410f2df4aff02f1a
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 "tree-iterator.h"
30 #include "diagnostic.h"
32 /* The pretty-printer code is primarily designed to closely follow
33 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
34 codes we used to have in the past. Following a structured
35 approach (preferably the official grammars) is believed to make it
36 much easier to add extensions and nifty pretty-printing effects that
37 takes expression or declaration contexts into account. */
40 #define pp_c_maybe_whitespace(PP) \
41 do { \
42 if (pp_base (PP)->padding == pp_before) \
43 pp_c_whitespace (PP); \
44 } while (0)
46 /* literal */
47 static void pp_c_char (c_pretty_printer *, int);
49 /* postfix-expression */
50 static void pp_c_initializer_list (c_pretty_printer *, tree);
51 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
53 static void pp_c_multiplicative_expression (c_pretty_printer *, tree);
54 static void pp_c_additive_expression (c_pretty_printer *, tree);
55 static void pp_c_shift_expression (c_pretty_printer *, tree);
56 static void pp_c_relational_expression (c_pretty_printer *, tree);
57 static void pp_c_equality_expression (c_pretty_printer *, tree);
58 static void pp_c_and_expression (c_pretty_printer *, tree);
59 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
60 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
61 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
62 static void pp_c_conditional_expression (c_pretty_printer *, tree);
63 static void pp_c_assignment_expression (c_pretty_printer *, tree);
65 /* declarations. */
68 /* Helper functions. */
70 void
71 pp_c_whitespace (c_pretty_printer *pp)
73 pp_space (pp);
74 pp_base (pp)->padding = pp_none;
77 void
78 pp_c_left_paren (c_pretty_printer *pp)
80 pp_left_paren (pp);
81 pp_base (pp)->padding = pp_none;
84 void
85 pp_c_right_paren (c_pretty_printer *pp)
87 pp_right_paren (pp);
88 pp_base (pp)->padding = pp_none;
91 void
92 pp_c_left_brace (c_pretty_printer *pp)
94 pp_left_brace (pp);
95 pp_base (pp)->padding = pp_none;
98 void
99 pp_c_right_brace (c_pretty_printer *pp)
101 pp_right_brace (pp);
102 pp_base (pp)->padding = pp_none;
105 void
106 pp_c_left_bracket (c_pretty_printer *pp)
108 pp_left_bracket (pp);
109 pp_base (pp)->padding = pp_none;
112 void
113 pp_c_right_bracket (c_pretty_printer *pp)
115 pp_right_bracket (pp);
116 pp_base (pp)->padding = pp_none;
119 void
120 pp_c_dot (c_pretty_printer *pp)
122 pp_dot (pp);
123 pp_base (pp)->padding = pp_none;
126 void
127 pp_c_ampersand (c_pretty_printer *pp)
129 pp_ampersand (pp);
130 pp_base (pp)->padding = pp_none;
133 void
134 pp_c_star (c_pretty_printer *pp)
136 pp_star (pp);
137 pp_base (pp)->padding = pp_none;
140 void
141 pp_c_arrow (c_pretty_printer *pp)
143 pp_arrow (pp);
144 pp_base (pp)->padding = pp_none;
147 void
148 pp_c_semicolon (c_pretty_printer *pp)
150 pp_semicolon (pp);
151 pp_base (pp)->padding = pp_none;
154 void
155 pp_c_complement (c_pretty_printer *pp)
157 pp_complement (pp);
158 pp_base (pp)->padding = pp_none;
161 void
162 pp_c_exclamation (c_pretty_printer *pp)
164 pp_exclamation (pp);
165 pp_base (pp)->padding = pp_none;
168 /* Print out the external representation of CV-QUALIFIER. */
170 static void
171 pp_c_cv_qualifier (c_pretty_printer *pp, const char *cv)
173 const char *p = pp_last_position_in_text (pp);
174 /* The C programming language does not have references, but it is much
175 simpler to handle those here rather than going through the same
176 logic in the C++ pretty-printer. */
177 if (p != NULL && (*p == '*' || *p == '&'))
178 pp_c_whitespace (pp);
179 pp_c_identifier (pp, cv);
182 /* Pretty-print T using the type-cast notation '( type-name )'. */
184 static void
185 pp_c_type_cast (c_pretty_printer *pp, tree t)
187 pp_c_left_paren (pp);
188 pp_type_id (pp, t);
189 pp_c_right_paren (pp);
192 /* We're about to pretty-print a pointer type as indicated by T.
193 Output a whitespace, if needed, preparing for subsequent output. */
195 void
196 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
198 if (POINTER_TYPE_P (t))
200 tree pointee = strip_pointer_operator (TREE_TYPE (t));
201 if (TREE_CODE (pointee) != ARRAY_TYPE
202 && TREE_CODE (pointee) != FUNCTION_TYPE)
203 pp_c_whitespace (pp);
208 /* Declarations. */
210 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
211 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
212 of its type. Take care of possible extensions.
214 type-qualifier-list:
215 type-qualifier
216 type-qualifier-list type-qualifier
218 type-qualifier:
219 const
220 restrict -- C99
221 __restrict__ -- GNU C
222 volatile */
224 void
225 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
227 int qualifiers;
229 if (!TYPE_P (t))
230 t = TREE_TYPE (t);
232 qualifiers = TYPE_QUALS (t);
233 if (qualifiers & TYPE_QUAL_CONST)
234 pp_c_cv_qualifier (pp, "const");
235 if (qualifiers & TYPE_QUAL_VOLATILE)
236 pp_c_cv_qualifier (pp, "volatile");
237 if (qualifiers & TYPE_QUAL_RESTRICT)
238 pp_c_cv_qualifier (pp, flag_isoc99 ? "restrict" : "__restrict__");
241 /* pointer:
242 * type-qualifier-list(opt)
243 * type-qualifier-list(opt) pointer */
245 static void
246 pp_c_pointer (c_pretty_printer *pp, tree t)
248 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
249 t = TREE_TYPE (t);
250 switch (TREE_CODE (t))
252 case POINTER_TYPE:
253 /* It is easier to handle C++ reference types here. */
254 case REFERENCE_TYPE:
255 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
256 pp_c_pointer (pp, TREE_TYPE (t));
257 if (TREE_CODE (t) == POINTER_TYPE)
258 pp_c_star (pp);
259 else
260 pp_c_ampersand (pp);
261 pp_c_type_qualifier_list (pp, t);
262 break;
264 default:
265 pp_unsupported_tree (pp, t);
269 /* type-specifier:
270 void
271 char
272 short
274 long
275 float
276 double
277 signed
278 unsigned
279 _Bool -- C99
280 _Complex -- C99
281 _Imaginary -- C99
282 struct-or-union-specifier
283 enum-specifier
284 typedef-name.
286 GNU extensions.
287 simple-type-specifier:
288 __complex__
289 __vector__ */
291 void
292 pp_c_type_specifier (c_pretty_printer *pp, tree t)
294 const enum tree_code code = TREE_CODE (t);
295 switch (code)
297 case ERROR_MARK:
298 pp_c_identifier (pp, "<type-error>");
299 break;
301 case IDENTIFIER_NODE:
302 pp_c_tree_decl_identifier (pp, t);
303 break;
305 case VOID_TYPE:
306 case BOOLEAN_TYPE:
307 case CHAR_TYPE:
308 case INTEGER_TYPE:
309 case REAL_TYPE:
310 if (TYPE_NAME (t))
311 t = TYPE_NAME (t);
312 else
313 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
314 pp_c_type_specifier (pp, t);
315 break;
317 case TYPE_DECL:
318 if (DECL_NAME (t))
319 pp_id_expression (pp, t);
320 else
321 pp_c_identifier (pp, "<typedef-error>");
322 break;
324 case UNION_TYPE:
325 case RECORD_TYPE:
326 case ENUMERAL_TYPE:
327 if (code == UNION_TYPE)
328 pp_c_identifier (pp, "union");
329 else if (code == RECORD_TYPE)
330 pp_c_identifier (pp, "struct");
331 else if (code == ENUMERAL_TYPE)
332 pp_c_identifier (pp, "enum");
333 else
334 pp_c_identifier (pp, "<tag-error>");
336 if (TYPE_NAME (t))
337 pp_id_expression (pp, TYPE_NAME (t));
338 else
339 pp_c_identifier (pp, "<anonymous>");
340 break;
342 default:
343 pp_unsupported_tree (pp, t);
344 break;
348 /* specifier-qualifier-list:
349 type-specifier specifier-qualifier-list-opt
350 type-qualifier specifier-qualifier-list-opt
353 Implementation note: Because of the non-linearities in array or
354 function declarations, this routine prints not just the
355 specifier-qualifier-list of such entities or types of such entities,
356 but also the 'pointer' production part of their declarators. The
357 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
359 void
360 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
362 const enum tree_code code = TREE_CODE (t);
364 if (TREE_CODE (t) != POINTER_TYPE)
365 pp_c_type_qualifier_list (pp, t);
366 switch (code)
368 case REFERENCE_TYPE:
369 case POINTER_TYPE:
371 /* Get the types-specifier of this type. */
372 tree pointee = strip_pointer_operator (TREE_TYPE (t));
373 pp_c_specifier_qualifier_list (pp, pointee);
374 if (TREE_CODE (pointee) == ARRAY_TYPE
375 || TREE_CODE (pointee) == FUNCTION_TYPE)
377 pp_c_whitespace (pp);
378 pp_c_left_paren (pp);
380 pp_ptr_operator (pp, t);
382 break;
384 case FUNCTION_TYPE:
385 case ARRAY_TYPE:
386 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
387 break;
389 case VECTOR_TYPE:
390 case COMPLEX_TYPE:
391 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
392 if (code == COMPLEX_TYPE)
393 pp_c_identifier (pp, flag_isoc99 ? "_Complex" : "__complex__");
394 else if (code == VECTOR_TYPE)
395 pp_c_identifier (pp, "__vector__");
396 break;
398 default:
399 pp_simple_type_specifier (pp, t);
400 break;
404 /* parameter-type-list:
405 parameter-list
406 parameter-list , ...
408 parameter-list:
409 parameter-declaration
410 parameter-list , parameter-declaration
412 parameter-declaration:
413 declaration-specifiers declarator
414 declaration-specifiers abstract-declarator(opt) */
416 void
417 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
419 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
420 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
421 pp_c_left_paren (pp);
422 if (parms == void_list_node)
423 pp_c_identifier (pp, "void");
424 else
426 bool first = true;
427 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
429 if (!first)
430 pp_separate_with (pp, ',');
431 first = false;
432 pp_declaration_specifiers
433 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
434 if (want_parm_decl)
435 pp_declarator (pp, parms);
436 else
437 pp_abstract_declarator (pp, TREE_VALUE (parms));
440 pp_c_right_paren (pp);
443 /* abstract-declarator:
444 pointer
445 pointer(opt) direct-abstract-declarator */
447 static void
448 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
450 if (TREE_CODE (t) == POINTER_TYPE)
452 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
453 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
454 pp_c_right_paren (pp);
455 t = TREE_TYPE (t);
458 pp_direct_abstract_declarator (pp, t);
461 /* direct-abstract-declarator:
462 ( abstract-declarator )
463 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
464 direct-abstract-declarator(opt) [ * ]
465 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
467 void
468 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
470 switch (TREE_CODE (t))
472 case POINTER_TYPE:
473 pp_abstract_declarator (pp, t);
474 break;
476 case FUNCTION_TYPE:
477 pp_c_parameter_type_list (pp, t);
478 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
479 break;
481 case ARRAY_TYPE:
482 pp_c_left_bracket (pp);
483 if (TYPE_DOMAIN (t))
484 pp_expression (pp, TYPE_MAX_VALUE (TYPE_DOMAIN (t)));
485 pp_c_right_bracket (pp);
486 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
487 break;
489 case IDENTIFIER_NODE:
490 case VOID_TYPE:
491 case BOOLEAN_TYPE:
492 case INTEGER_TYPE:
493 case REAL_TYPE:
494 case ENUMERAL_TYPE:
495 case RECORD_TYPE:
496 case UNION_TYPE:
497 case VECTOR_TYPE:
498 case COMPLEX_TYPE:
499 case TYPE_DECL:
500 break;
502 default:
503 pp_unsupported_tree (pp, t);
504 break;
508 /* type-name:
509 specifier-qualifier-list abstract-declarator(opt) */
511 void
512 pp_c_type_id (c_pretty_printer *pp, tree t)
514 pp_c_specifier_qualifier_list (pp, t);
515 pp_abstract_declarator (pp, t);
518 /* storage-class-specifier:
519 typedef
520 extern
521 static
522 auto
523 register */
525 void
526 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
528 if (TREE_CODE (t) == TYPE_DECL)
529 pp_c_identifier (pp, "typedef");
530 else if (DECL_P (t))
532 if (DECL_REGISTER (t))
533 pp_c_identifier (pp, "register");
534 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
535 pp_c_identifier (pp, "static");
539 /* function-specifier:
540 inline */
542 void
543 pp_c_function_specifier (c_pretty_printer *pp, tree t)
545 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
546 pp_c_identifier (pp, "inline");
549 /* declaration-specifiers:
550 storage-class-specifier declaration-specifiers(opt)
551 type-specifier declaration-specifiers(opt)
552 type-qualifier declaration-specifiers(opt)
553 function-specifier declaration-specifiers(opt) */
555 void
556 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
558 pp_storage_class_specifier (pp, t);
559 pp_function_specifier (pp, t);
560 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
563 /* direct-declarator
564 identifier
565 ( declarator )
566 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
567 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
568 direct-declarator [ type-qualifier-list static assignment-expression ]
569 direct-declarator [ type-qualifier-list * ]
570 direct-declarator ( parameter-type-list )
571 direct-declarator ( identifier-list(opt) ) */
573 void
574 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
576 switch (TREE_CODE (t))
578 case VAR_DECL:
579 case PARM_DECL:
580 case TYPE_DECL:
581 case FIELD_DECL:
582 case LABEL_DECL:
583 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
584 pp_c_tree_decl_identifier (pp, t);
585 break;
587 case ARRAY_TYPE:
588 case POINTER_TYPE:
589 pp_abstract_declarator (pp, TREE_TYPE (t));
590 break;
592 case FUNCTION_TYPE:
593 pp_parameter_list (pp, t);
594 pp_abstract_declarator (pp, TREE_TYPE (t));
595 break;
597 case FUNCTION_DECL:
598 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
599 pp_c_tree_decl_identifier (pp, t);
600 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
601 pp_abstract_declarator (pp, TREE_TYPE (t));
602 else
604 pp_parameter_list (pp, t);
605 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
607 break;
609 case INTEGER_TYPE:
610 case REAL_TYPE:
611 case ENUMERAL_TYPE:
612 case UNION_TYPE:
613 case RECORD_TYPE:
614 break;
616 default:
617 pp_unsupported_tree (pp, t);
618 break;
623 /* declarator:
624 pointer(opt) direct-declarator */
626 void
627 pp_c_declarator (c_pretty_printer *pp, tree t)
629 switch (TREE_CODE (t))
631 case INTEGER_TYPE:
632 case REAL_TYPE:
633 case ENUMERAL_TYPE:
634 case UNION_TYPE:
635 case RECORD_TYPE:
636 break;
638 case VAR_DECL:
639 case PARM_DECL:
640 case FIELD_DECL:
641 case ARRAY_TYPE:
642 case FUNCTION_TYPE:
643 case FUNCTION_DECL:
644 case TYPE_DECL:
645 pp_direct_declarator (pp, t);
646 break;
649 default:
650 pp_unsupported_tree (pp, t);
651 break;
655 /* declaration:
656 declaration-specifiers init-declarator-list(opt) ; */
658 void
659 pp_c_declaration (c_pretty_printer *pp, tree t)
661 pp_declaration_specifiers (pp, t);
662 pp_c_init_declarator (pp, t);
665 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
667 void
668 pp_c_attributes (c_pretty_printer *pp, tree attributes)
670 if (attributes == NULL_TREE)
671 return;
673 pp_c_identifier (pp, "__attribute__");
674 pp_c_left_paren (pp);
675 pp_c_left_paren (pp);
676 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
678 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
679 if (TREE_VALUE (attributes))
680 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
682 if (TREE_CHAIN (attributes))
683 pp_separate_with (pp, ',');
685 pp_c_right_paren (pp);
686 pp_c_right_paren (pp);
689 /* function-definition:
690 declaration-specifiers declarator compound-statement */
692 void
693 pp_c_function_definition (c_pretty_printer *pp, tree t)
695 pp_declaration_specifiers (pp, t);
696 pp_declarator (pp, t);
697 pp_needs_newline (pp) = true;
698 pp_statement (pp, DECL_SAVED_TREE (t));
699 pp_newline (pp);
700 pp_flush (pp);
704 /* Expressions. */
706 /* Print out a c-char. */
708 static void
709 pp_c_char (c_pretty_printer *pp, int c)
711 switch (c)
713 case TARGET_NEWLINE:
714 pp_string (pp, "\\n");
715 break;
716 case TARGET_TAB:
717 pp_string (pp, "\\t");
718 break;
719 case TARGET_VT:
720 pp_string (pp, "\\v");
721 break;
722 case TARGET_BS:
723 pp_string (pp, "\\b");
724 break;
725 case TARGET_CR:
726 pp_string (pp, "\\r");
727 break;
728 case TARGET_FF:
729 pp_string (pp, "\\f");
730 break;
731 case TARGET_BELL:
732 pp_string (pp, "\\a");
733 break;
734 case '\\':
735 pp_string (pp, "\\\\");
736 break;
737 case '\'':
738 pp_string (pp, "\\'");
739 break;
740 case '\"':
741 pp_string (pp, "\\\"");
742 break;
743 default:
744 if (ISPRINT (c))
745 pp_character (pp, c);
746 else
747 pp_scalar (pp, "\\%03o", (unsigned) c);
748 break;
752 /* Print out a STRING literal. */
754 void
755 pp_c_string_literal (c_pretty_printer *pp, tree s)
757 const char *p = TREE_STRING_POINTER (s);
758 int n = TREE_STRING_LENGTH (s) - 1;
759 int i;
760 pp_doublequote (pp);
761 for (i = 0; i < n; ++i)
762 pp_c_char (pp, p[i]);
763 pp_doublequote (pp);
766 /* Pretty-print an INTEGER literal. */
768 static void
769 pp_c_integer_constant (c_pretty_printer *pp, tree i)
771 tree type = TREE_TYPE (i);
773 if (TREE_INT_CST_HIGH (i) == 0)
774 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
775 else
777 if (tree_int_cst_sgn (i) < 0)
779 pp_c_char (pp, '-');
780 i = build_int_2 (-TREE_INT_CST_LOW (i),
781 ~TREE_INT_CST_HIGH (i) + !TREE_INT_CST_LOW (i));
783 sprintf (pp_buffer (pp)->digit_buffer,
784 HOST_WIDE_INT_PRINT_DOUBLE_HEX,
785 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
786 pp_string (pp, pp_buffer (pp)->digit_buffer);
788 if (TYPE_UNSIGNED (type))
789 pp_character (pp, 'u');
790 if (type == long_integer_type_node || type == long_unsigned_type_node)
791 pp_character (pp, 'l');
792 else if (type == long_long_integer_type_node
793 || type == long_long_unsigned_type_node)
794 pp_string (pp, "ll");
797 /* Print out a CHARACTER literal. */
799 static void
800 pp_c_character_constant (c_pretty_printer *pp, tree c)
802 tree type = TREE_TYPE (c);
803 if (type == wchar_type_node)
804 pp_character (pp, 'L');
805 pp_quote (pp);
806 if (host_integerp (c, TYPE_UNSIGNED (type)))
807 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
808 else
809 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
810 pp_quote (pp);
813 /* Print out a BOOLEAN literal. */
815 static void
816 pp_c_bool_constant (c_pretty_printer *pp, tree b)
818 if (b == boolean_false_node)
820 if (c_dialect_cxx ())
821 pp_c_identifier (pp, "false");
822 else if (flag_isoc99)
823 pp_c_identifier (pp, "_False");
824 else
825 pp_unsupported_tree (pp, b);
827 else if (b == boolean_true_node)
829 if (c_dialect_cxx ())
830 pp_c_identifier (pp, "true");
831 else if (flag_isoc99)
832 pp_c_identifier (pp, "_True");
833 else
834 pp_unsupported_tree (pp, b);
836 else if (TREE_CODE (b) == INTEGER_CST)
837 pp_c_integer_constant (pp, b);
838 else
839 pp_unsupported_tree (pp, b);
842 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
843 false; that means the value was obtained by a cast, in which case
844 print out the type-id part of the cast-expression -- the casted value
845 is then printed by pp_c_integer_literal. */
847 static bool
848 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
850 bool value_is_named = true;
851 tree type = TREE_TYPE (e);
852 tree value;
854 /* Find the name of this constant. */
855 for (value = TYPE_VALUES (type);
856 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
857 value = TREE_CHAIN (value))
860 if (value != NULL_TREE)
861 pp_id_expression (pp, TREE_PURPOSE (value));
862 else
864 /* Value must have been cast. */
865 pp_c_type_cast (pp, type);
866 value_is_named = false;
869 return value_is_named;
872 /* Print out a REAL value as a decimal-floating-constant. */
874 static void
875 pp_c_floating_constant (c_pretty_printer *pp, tree r)
877 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
878 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
879 pp_string (pp, pp_buffer(pp)->digit_buffer);
880 if (TREE_TYPE (r) == float_type_node)
881 pp_character (pp, 'f');
882 else if (TREE_TYPE (r) == long_double_type_node)
883 pp_character (pp, 'l');
886 /* Pretty-print a compound literal expression. GNU extensions include
887 vector constants. */
889 static void
890 pp_c_compound_literal (c_pretty_printer *pp, tree e)
892 tree type = TREE_TYPE (e);
893 pp_c_type_cast (pp, type);
895 switch (TREE_CODE (type))
897 case RECORD_TYPE:
898 case UNION_TYPE:
899 case ARRAY_TYPE:
900 case VECTOR_TYPE:
901 case COMPLEX_TYPE:
902 pp_c_brace_enclosed_initializer_list (pp, e);
903 break;
905 default:
906 pp_unsupported_tree (pp, e);
907 break;
911 /* constant:
912 integer-constant
913 floating-constant
914 enumeration-constant
915 character-constant */
917 void
918 pp_c_constant (c_pretty_printer *pp, tree e)
920 const enum tree_code code = TREE_CODE (e);
922 switch (code)
924 case INTEGER_CST:
926 tree type = TREE_TYPE (e);
927 if (type == boolean_type_node)
928 pp_c_bool_constant (pp, e);
929 else if (type == char_type_node)
930 pp_c_character_constant (pp, e);
931 else if (TREE_CODE (type) == ENUMERAL_TYPE
932 && pp_c_enumeration_constant (pp, e))
934 else
935 pp_c_integer_constant (pp, e);
937 break;
939 case REAL_CST:
940 pp_c_floating_constant (pp, e);
941 break;
943 case STRING_CST:
944 pp_c_string_literal (pp, e);
945 break;
947 default:
948 pp_unsupported_tree (pp, e);
949 break;
953 /* Pretty-print an IDENTIFIER_NODE, preceded by whitespace is necessary. */
955 void
956 pp_c_identifier (c_pretty_printer *pp, const char *id)
958 pp_c_maybe_whitespace (pp);
959 pp_identifier (pp, id);
960 pp_base (pp)->padding = pp_before;
963 /* Pretty-print a C primary-expression.
964 primary-expression:
965 identifier
966 constant
967 string-literal
968 ( expression ) */
970 void
971 pp_c_primary_expression (c_pretty_printer *pp, tree e)
973 switch (TREE_CODE (e))
975 case VAR_DECL:
976 case PARM_DECL:
977 case FIELD_DECL:
978 case CONST_DECL:
979 case FUNCTION_DECL:
980 case LABEL_DECL:
981 pp_c_tree_decl_identifier (pp, e);
982 break;
984 case IDENTIFIER_NODE:
985 pp_c_tree_identifier (pp, e);
986 break;
988 case ERROR_MARK:
989 pp_c_identifier (pp, "<erroneous-expression>");
990 break;
992 case RESULT_DECL:
993 pp_c_identifier (pp, "<return-value>");
994 break;
996 case INTEGER_CST:
997 case REAL_CST:
998 case STRING_CST:
999 pp_c_constant (pp, e);
1000 break;
1002 case TARGET_EXPR:
1003 pp_c_identifier (pp, "__builtin_memcpy");
1004 pp_c_left_paren (pp);
1005 pp_ampersand (pp);
1006 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1007 pp_separate_with (pp, ',');
1008 pp_ampersand (pp);
1009 pp_initializer (pp, TREE_OPERAND (e, 1));
1010 if (TREE_OPERAND (e, 2))
1012 pp_separate_with (pp, ',');
1013 pp_c_expression (pp, TREE_OPERAND (e, 2));
1015 pp_c_right_paren (pp);
1016 break;
1018 case STMT_EXPR:
1019 pp_c_left_paren (pp);
1020 pp_statement (pp, STMT_EXPR_STMT (e));
1021 pp_c_right_paren (pp);
1022 break;
1024 default:
1025 /* FIXME: Make sure we won't get into an infinie loop. */
1026 pp_c_left_paren (pp);
1027 pp_expression (pp, e);
1028 pp_c_right_paren (pp);
1029 break;
1033 /* Print out a C initializer -- also support C compound-literals.
1034 initializer:
1035 assignment-expression:
1036 { initializer-list }
1037 { initializer-list , } */
1039 static void
1040 pp_c_initializer (c_pretty_printer *pp, tree e)
1042 if (TREE_CODE (e) == CONSTRUCTOR)
1043 pp_c_brace_enclosed_initializer_list (pp, e);
1044 else
1045 pp_expression (pp, e);
1048 /* init-declarator:
1049 declarator:
1050 declarator = initializer */
1052 void
1053 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1055 pp_declarator (pp, t);
1056 /* We don't want to output function definitions here. There are handled
1057 elsewhere (and the syntactic form is bogus anyway). */
1058 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1060 tree init = DECL_INITIAL (t);
1061 /* This C++ bit is handled here because it is easier to do so.
1062 In templates, the C++ parser builds a TREE_LIST for a
1063 direct-initialization; the TREE_PURPOSE is the variable to
1064 initialize and the TREE_VALUE is the initializer. */
1065 if (TREE_CODE (init) == TREE_LIST)
1067 pp_c_left_paren (pp);
1068 pp_expression (pp, TREE_VALUE (init));
1069 pp_right_paren (pp);
1071 else
1073 pp_space (pp);
1074 pp_equal (pp);
1075 pp_space (pp);
1076 pp_c_initializer (pp, init);
1081 /* initializer-list:
1082 designation(opt) initializer
1083 initializer-list , designation(opt) initializer
1085 designation:
1086 designator-list =
1088 designator-list:
1089 designator
1090 designator-list designator
1092 designator:
1093 [ constant-expression ]
1094 identifier */
1096 static void
1097 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1099 tree type = TREE_TYPE (e);
1100 const enum tree_code code = TREE_CODE (type);
1102 switch (code)
1104 case RECORD_TYPE:
1105 case UNION_TYPE:
1106 case ARRAY_TYPE:
1108 tree init = TREE_OPERAND (e, 0);
1109 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1111 if (code == RECORD_TYPE || code == UNION_TYPE)
1113 pp_c_dot (pp);
1114 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1116 else
1118 pp_c_left_bracket (pp);
1119 if (TREE_PURPOSE (init))
1120 pp_c_constant (pp, TREE_PURPOSE (init));
1121 pp_c_right_bracket (pp);
1123 pp_c_whitespace (pp);
1124 pp_equal (pp);
1125 pp_c_whitespace (pp);
1126 pp_initializer (pp, TREE_VALUE (init));
1127 if (TREE_CHAIN (init))
1128 pp_separate_with (pp, ',');
1131 return;
1133 case VECTOR_TYPE:
1134 if (TREE_CODE (e) == VECTOR_CST)
1135 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1136 else if (TREE_CODE (e) == CONSTRUCTOR)
1137 pp_c_expression_list (pp, CONSTRUCTOR_ELTS (e));
1138 else
1139 break;
1140 return;
1142 case COMPLEX_TYPE:
1143 if (TREE_CODE (e) == CONSTRUCTOR)
1144 pp_c_expression_list (pp, CONSTRUCTOR_ELTS (e));
1145 else if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1147 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1148 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1149 pp_separate_with (pp, ',');
1150 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1152 else
1153 break;
1154 return;
1156 default:
1157 break;
1160 pp_unsupported_tree (pp, type);
1163 /* Pretty-print a brace-enclosed initializer-list. */
1165 static void
1166 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1168 pp_c_left_brace (pp);
1169 pp_c_initializer_list (pp, l);
1170 pp_c_right_brace (pp);
1174 /* This is a convenient function, used to bridge gap between C and C++
1175 grammars.
1177 id-expression:
1178 identifier */
1180 void
1181 pp_c_id_expression (c_pretty_printer *pp, tree t)
1183 switch (TREE_CODE (t))
1185 case VAR_DECL:
1186 case PARM_DECL:
1187 case CONST_DECL:
1188 case TYPE_DECL:
1189 case FUNCTION_DECL:
1190 case FIELD_DECL:
1191 case LABEL_DECL:
1192 pp_c_tree_decl_identifier (pp, t);
1193 break;
1195 case IDENTIFIER_NODE:
1196 pp_c_tree_identifier (pp, t);
1197 break;
1199 default:
1200 pp_unsupported_tree (pp, t);
1201 break;
1205 /* postfix-expression:
1206 primary-expression
1207 postfix-expression [ expression ]
1208 postfix-expression ( argument-expression-list(opt) )
1209 postfix-expression . identifier
1210 postfix-expression -> identifier
1211 postfix-expression ++
1212 postfix-expression --
1213 ( type-name ) { initializer-list }
1214 ( type-name ) { initializer-list , } */
1216 void
1217 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1219 enum tree_code code = TREE_CODE (e);
1220 switch (code)
1222 case POSTINCREMENT_EXPR:
1223 case POSTDECREMENT_EXPR:
1224 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1225 pp_identifier (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1226 break;
1228 case ARROW_EXPR:
1229 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1230 pp_c_arrow (pp);
1231 break;
1233 case ARRAY_REF:
1234 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1235 pp_c_left_bracket (pp);
1236 pp_expression (pp, TREE_OPERAND (e, 1));
1237 pp_c_right_bracket (pp);
1238 break;
1240 case CALL_EXPR:
1241 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1242 pp_c_call_argument_list (pp, TREE_OPERAND (e, 1));
1243 break;
1245 case ABS_EXPR:
1246 pp_c_identifier (pp, "__builtin_abs");
1247 pp_c_left_paren (pp);
1248 pp_expression (pp, TREE_OPERAND (e, 0));
1249 pp_c_right_paren (pp);
1250 break;
1252 case COMPONENT_REF:
1254 tree object = TREE_OPERAND (e, 0);
1255 if (TREE_CODE (object) == INDIRECT_REF)
1257 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1258 pp_c_arrow (pp);
1260 else
1262 pp_postfix_expression (pp, object);
1263 pp_c_dot (pp);
1265 pp_expression (pp, TREE_OPERAND (e, 1));
1267 break;
1269 case COMPLEX_CST:
1270 case VECTOR_CST:
1271 case COMPLEX_EXPR:
1272 pp_c_compound_literal (pp, e);
1273 break;
1275 case COMPOUND_LITERAL_EXPR:
1276 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1277 /* Fall through. */
1278 case CONSTRUCTOR:
1279 pp_initializer (pp, e);
1280 break;
1282 case VA_ARG_EXPR:
1283 pp_c_identifier (pp, "__builtin_va_arg");
1284 pp_c_left_paren (pp);
1285 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1286 pp_separate_with (pp, ',');
1287 pp_type_id (pp, TREE_TYPE (e));
1288 pp_c_right_paren (pp);
1289 break;
1291 case ADDR_EXPR:
1292 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1294 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1295 break;
1297 /* else fall through. */
1299 default:
1300 pp_primary_expression (pp, e);
1301 break;
1305 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1307 void
1308 pp_c_expression_list (c_pretty_printer *pp, tree e)
1310 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1312 pp_expression (pp, TREE_VALUE (e));
1313 if (TREE_CHAIN (e))
1314 pp_separate_with (pp, ',');
1318 /* Print out an expression-list in parens, as in a function call. */
1320 void
1321 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1323 pp_c_left_paren (pp);
1324 if (t && TREE_CODE (t) == TREE_LIST)
1325 pp_c_expression_list (pp, t);
1326 pp_c_right_paren (pp);
1329 /* unary-expression:
1330 postfix-expression
1331 ++ cast-expression
1332 -- cast-expression
1333 unary-operator cast-expression
1334 sizeof unary-expression
1335 sizeof ( type-id )
1337 unary-operator: one of
1338 * & + - ! ~
1340 GNU extensions.
1341 unary-expression:
1342 __alignof__ unary-expression
1343 __alignof__ ( type-id )
1344 __real__ unary-expression
1345 __imag__ unary-expression */
1347 void
1348 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1350 enum tree_code code = TREE_CODE (e);
1351 switch (code)
1353 case PREINCREMENT_EXPR:
1354 case PREDECREMENT_EXPR:
1355 pp_identifier (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1356 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1357 break;
1359 case ADDR_EXPR:
1360 case INDIRECT_REF:
1361 case NEGATE_EXPR:
1362 case BIT_NOT_EXPR:
1363 case TRUTH_NOT_EXPR:
1364 case CONJ_EXPR:
1365 /* String literal are used by address. */
1366 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1367 pp_ampersand (pp);
1368 else if (code == INDIRECT_REF)
1369 pp_c_star (pp);
1370 else if (code == NEGATE_EXPR)
1371 pp_minus (pp);
1372 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1373 pp_complement (pp);
1374 else if (code == TRUTH_NOT_EXPR)
1375 pp_exclamation (pp);
1376 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1377 break;
1379 case SIZEOF_EXPR:
1380 case ALIGNOF_EXPR:
1381 pp_c_identifier (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
1382 pp_c_whitespace (pp);
1383 if (TYPE_P (TREE_OPERAND (e, 0)))
1384 pp_c_type_cast (pp, TREE_OPERAND (e, 0));
1385 else
1386 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1387 break;
1389 case REALPART_EXPR:
1390 case IMAGPART_EXPR:
1391 pp_c_identifier (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1392 pp_c_whitespace (pp);
1393 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1394 break;
1396 default:
1397 pp_postfix_expression (pp, e);
1398 break;
1402 /* cast-expression:
1403 unary-expression
1404 ( type-name ) cast-expression */
1406 void
1407 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1409 switch (TREE_CODE (e))
1411 case FLOAT_EXPR:
1412 case FIX_TRUNC_EXPR:
1413 case CONVERT_EXPR:
1414 pp_c_type_cast (pp, TREE_TYPE (e));
1415 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1416 break;
1418 default:
1419 pp_unary_expression (pp, e);
1423 /* multiplicative-expression:
1424 cast-expression
1425 multiplicative-expression * cast-expression
1426 multiplicative-expression / cast-expression
1427 multiplicative-expression % cast-expression */
1429 static void
1430 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1432 enum tree_code code = TREE_CODE (e);
1433 switch (code)
1435 case MULT_EXPR:
1436 case TRUNC_DIV_EXPR:
1437 case TRUNC_MOD_EXPR:
1438 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1439 pp_c_whitespace (pp);
1440 if (code == MULT_EXPR)
1441 pp_c_star (pp);
1442 else if (code == TRUNC_DIV_EXPR)
1443 pp_slash (pp);
1444 else
1445 pp_modulo (pp);
1446 pp_c_whitespace (pp);
1447 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1448 break;
1450 default:
1451 pp_c_cast_expression (pp, e);
1452 break;
1456 /* additive-expression:
1457 multiplicative-expression
1458 additive-expression + multiplicative-expression
1459 additive-expression - multiplicative-expression */
1461 static void
1462 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1464 enum tree_code code = TREE_CODE (e);
1465 switch (code)
1467 case PLUS_EXPR:
1468 case MINUS_EXPR:
1469 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1470 pp_c_whitespace (pp);
1471 if (code == PLUS_EXPR)
1472 pp_plus (pp);
1473 else
1474 pp_minus (pp);
1475 pp_c_whitespace (pp);
1476 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1477 break;
1479 default:
1480 pp_multiplicative_expression (pp, e);
1481 break;
1485 /* additive-expression:
1486 additive-expression
1487 shift-expression << additive-expression
1488 shift-expression >> additive-expression */
1490 static void
1491 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1493 enum tree_code code = TREE_CODE (e);
1494 switch (code)
1496 case LSHIFT_EXPR:
1497 case RSHIFT_EXPR:
1498 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1499 pp_c_whitespace (pp);
1500 pp_identifier (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1501 pp_c_whitespace (pp);
1502 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1503 break;
1505 default:
1506 pp_c_additive_expression (pp, e);
1510 /* relational-expression:
1511 shift-expression
1512 relational-expression < shift-expression
1513 relational-expression > shift-expression
1514 relational-expression <= shift-expression
1515 relational-expression >= shift-expression */
1517 static void
1518 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1520 enum tree_code code = TREE_CODE (e);
1521 switch (code)
1523 case LT_EXPR:
1524 case GT_EXPR:
1525 case LE_EXPR:
1526 case GE_EXPR:
1527 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1528 pp_c_whitespace (pp);
1529 if (code == LT_EXPR)
1530 pp_less (pp);
1531 else if (code == GT_EXPR)
1532 pp_greater (pp);
1533 else if (code == LE_EXPR)
1534 pp_identifier (pp, "<=");
1535 else if (code == GE_EXPR)
1536 pp_identifier (pp, ">=");
1537 pp_c_whitespace (pp);
1538 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1539 break;
1541 default:
1542 pp_c_shift_expression (pp, e);
1543 break;
1547 /* equality-expression:
1548 relational-expression
1549 equality-expression == relational-expression
1550 equality-equality != relational-expression */
1552 static void
1553 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1555 enum tree_code code = TREE_CODE (e);
1556 switch (code)
1558 case EQ_EXPR:
1559 case NE_EXPR:
1560 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1561 pp_c_whitespace (pp);
1562 pp_identifier (pp, code == EQ_EXPR ? "==" : "!=");
1563 pp_c_whitespace (pp);
1564 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1565 break;
1567 default:
1568 pp_c_relational_expression (pp, e);
1569 break;
1573 /* AND-expression:
1574 equality-expression
1575 AND-expression & equality-equality */
1577 static void
1578 pp_c_and_expression (c_pretty_printer *pp, tree e)
1580 if (TREE_CODE (e) == BIT_AND_EXPR)
1582 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1583 pp_c_whitespace (pp);
1584 pp_ampersand (pp);
1585 pp_c_whitespace (pp);
1586 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1588 else
1589 pp_c_equality_expression (pp, e);
1592 /* exclusive-OR-expression:
1593 AND-expression
1594 exclusive-OR-expression ^ AND-expression */
1596 static void
1597 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1599 if (TREE_CODE (e) == BIT_XOR_EXPR)
1601 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1602 pp_c_maybe_whitespace (pp);
1603 pp_carret (pp);
1604 pp_c_whitespace (pp);
1605 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1607 else
1608 pp_c_and_expression (pp, e);
1611 /* inclusive-OR-expression:
1612 exclusive-OR-expression
1613 inclusive-OR-expression | exclusive-OR-expression */
1615 static void
1616 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1618 if (TREE_CODE (e) == BIT_IOR_EXPR)
1620 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1621 pp_c_whitespace (pp);
1622 pp_bar (pp);
1623 pp_c_whitespace (pp);
1624 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1626 else
1627 pp_c_exclusive_or_expression (pp, e);
1630 /* logical-AND-expression:
1631 inclusive-OR-expression
1632 logical-AND-expression && inclusive-OR-expression */
1634 static void
1635 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1637 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
1639 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1640 pp_c_whitespace (pp);
1641 pp_identifier (pp, "&&");
1642 pp_c_whitespace (pp);
1643 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1645 else
1646 pp_c_inclusive_or_expression (pp, e);
1649 /* logical-OR-expression:
1650 logical-AND-expression
1651 logical-OR-expression || logical-AND-expression */
1653 void
1654 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1656 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1658 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1659 pp_c_whitespace (pp);
1660 pp_identifier (pp, "||");
1661 pp_c_whitespace (pp);
1662 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1664 else
1665 pp_c_logical_and_expression (pp, e);
1668 /* conditional-expression:
1669 logical-OR-expression
1670 logical-OR-expression ? expression : conditional-expression */
1672 static void
1673 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1675 if (TREE_CODE (e) == COND_EXPR)
1677 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1678 pp_c_whitespace (pp);
1679 pp_question (pp);
1680 pp_c_whitespace (pp);
1681 pp_expression (pp, TREE_OPERAND (e, 1));
1682 pp_c_whitespace (pp);
1683 pp_colon (pp);
1684 pp_c_whitespace (pp);
1685 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1687 else
1688 pp_c_logical_or_expression (pp, e);
1692 /* assignment-expression:
1693 conditional-expression
1694 unary-expression assignment-operator assignment-expression
1696 assignment-expression: one of
1697 = *= /= %= += -= >>= <<= &= ^= |= */
1699 static void
1700 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1702 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1704 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1705 pp_c_whitespace (pp);
1706 pp_equal (pp);
1707 pp_space (pp);
1708 pp_c_expression (pp, TREE_OPERAND (e, 1));
1710 else
1711 pp_c_conditional_expression (pp, e);
1714 /* expression:
1715 assignment-expression
1716 expression , assignment-expression
1718 Implementation note: instead of going through the usual recursion
1719 chain, I take the liberty of dispatching nodes to the appropriate
1720 functions. This makes some redundancy, but it worths it. That also
1721 prevents a possible infinite recursion between pp_c_primary_expression ()
1722 and pp_c_expression (). */
1724 void
1725 pp_c_expression (c_pretty_printer *pp, tree e)
1727 switch (TREE_CODE (e))
1729 case INTEGER_CST:
1730 pp_c_integer_constant (pp, e);
1731 break;
1733 case REAL_CST:
1734 pp_c_floating_constant (pp, e);
1735 break;
1737 case STRING_CST:
1738 pp_c_string_literal (pp, e);
1739 break;
1741 case IDENTIFIER_NODE:
1742 case FUNCTION_DECL:
1743 case VAR_DECL:
1744 case CONST_DECL:
1745 case PARM_DECL:
1746 case RESULT_DECL:
1747 case FIELD_DECL:
1748 case LABEL_DECL:
1749 case ERROR_MARK:
1750 case STMT_EXPR:
1751 pp_primary_expression (pp, e);
1752 break;
1754 case POSTINCREMENT_EXPR:
1755 case POSTDECREMENT_EXPR:
1756 case ARROW_EXPR:
1757 case ARRAY_REF:
1758 case CALL_EXPR:
1759 case COMPONENT_REF:
1760 case COMPLEX_CST:
1761 case COMPLEX_EXPR:
1762 case VECTOR_CST:
1763 case ABS_EXPR:
1764 case CONSTRUCTOR:
1765 case COMPOUND_LITERAL_EXPR:
1766 case VA_ARG_EXPR:
1767 pp_postfix_expression (pp, e);
1768 break;
1770 case CONJ_EXPR:
1771 case ADDR_EXPR:
1772 case INDIRECT_REF:
1773 case NEGATE_EXPR:
1774 case BIT_NOT_EXPR:
1775 case TRUTH_NOT_EXPR:
1776 case PREINCREMENT_EXPR:
1777 case PREDECREMENT_EXPR:
1778 case SIZEOF_EXPR:
1779 case ALIGNOF_EXPR:
1780 case REALPART_EXPR:
1781 case IMAGPART_EXPR:
1782 pp_c_unary_expression (pp, e);
1783 break;
1785 case FLOAT_EXPR:
1786 case FIX_TRUNC_EXPR:
1787 case CONVERT_EXPR:
1788 pp_c_cast_expression (pp, e);
1789 break;
1791 case MULT_EXPR:
1792 case TRUNC_MOD_EXPR:
1793 case TRUNC_DIV_EXPR:
1794 pp_multiplicative_expression (pp, e);
1795 break;
1797 case LSHIFT_EXPR:
1798 case RSHIFT_EXPR:
1799 pp_c_shift_expression (pp, e);
1800 break;
1802 case LT_EXPR:
1803 case GT_EXPR:
1804 case LE_EXPR:
1805 case GE_EXPR:
1806 pp_c_relational_expression (pp, e);
1807 break;
1809 case BIT_AND_EXPR:
1810 pp_c_and_expression (pp, e);
1811 break;
1813 case BIT_XOR_EXPR:
1814 pp_c_exclusive_or_expression (pp, e);
1815 break;
1817 case BIT_IOR_EXPR:
1818 pp_c_inclusive_or_expression (pp, e);
1819 break;
1821 case TRUTH_ANDIF_EXPR:
1822 pp_c_logical_and_expression (pp, e);
1823 break;
1825 case TRUTH_ORIF_EXPR:
1826 pp_c_logical_or_expression (pp, e);
1827 break;
1829 case EQ_EXPR:
1830 case NE_EXPR:
1831 pp_c_equality_expression (pp, e);
1832 break;
1834 case COND_EXPR:
1835 pp_conditional_expression (pp, e);
1836 break;
1838 case PLUS_EXPR:
1839 case MINUS_EXPR:
1840 pp_c_additive_expression (pp, e);
1841 break;
1843 case MODIFY_EXPR:
1844 case INIT_EXPR:
1845 pp_assignment_expression (pp, e);
1846 break;
1848 case COMPOUND_EXPR:
1849 pp_c_left_paren (pp);
1850 pp_expression (pp, TREE_OPERAND (e, 0));
1851 pp_separate_with (pp, ',');
1852 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
1853 pp_c_right_paren (pp);
1854 break;
1856 case NOP_EXPR:
1857 case NON_LVALUE_EXPR:
1858 case SAVE_EXPR:
1859 case UNSAVE_EXPR:
1860 pp_expression (pp, TREE_OPERAND (e, 0));
1861 break;
1863 case TARGET_EXPR:
1864 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
1865 break;
1867 default:
1868 pp_unsupported_tree (pp, e);
1869 break;
1875 /* Statements. */
1877 /* statement:
1878 labeled-statement
1879 compound-statement
1880 expression-statement
1881 selection-statement
1882 iteration-statement
1883 jump-statement */
1885 void
1886 pp_c_statement (c_pretty_printer *pp, tree stmt)
1888 enum tree_code code;
1890 if (stmt == NULL)
1891 return;
1893 if (pp_needs_newline (pp))
1894 pp_newline_and_indent (pp, 0);
1896 code = TREE_CODE (stmt);
1897 switch (code)
1899 /* expression-statement:
1900 expression(opt) ; */
1901 case EXPR_STMT:
1902 pp_expression (pp, EXPR_STMT_EXPR (stmt));
1903 pp_c_semicolon (pp);
1904 pp_needs_newline (pp) = true;
1905 break;
1907 case SWITCH_STMT:
1908 pp_c_identifier (pp, "switch");
1909 pp_space (pp);
1910 pp_c_left_paren (pp);
1911 pp_expression (pp, SWITCH_COND (stmt));
1912 pp_c_right_paren (pp);
1913 pp_indentation (pp) += 3;
1914 pp_needs_newline (pp) = true;
1915 pp_statement (pp, SWITCH_BODY (stmt));
1916 pp_newline_and_indent (pp, -3);
1917 break;
1919 /* iteration-statement:
1920 while ( expression ) statement
1921 do statement while ( expression ) ;
1922 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1923 for ( declaration expression(opt) ; expression(opt) ) statement */
1924 case WHILE_STMT:
1925 pp_c_identifier (pp, "while");
1926 pp_space (pp);
1927 pp_c_left_paren (pp);
1928 pp_expression (pp, WHILE_COND (stmt));
1929 pp_c_right_paren (pp);
1930 pp_newline_and_indent (pp, 3);
1931 pp_statement (pp, WHILE_BODY (stmt));
1932 pp_indentation (pp) -= 3;
1933 pp_needs_newline (pp) = true;
1934 break;
1936 case DO_STMT:
1937 pp_c_identifier (pp, "do");
1938 pp_newline_and_indent (pp, 3);
1939 pp_statement (pp, DO_BODY (stmt));
1940 pp_newline_and_indent (pp, -3);
1941 pp_c_identifier (pp, "while");
1942 pp_space (pp);
1943 pp_c_left_paren (pp);
1944 pp_expression (pp, DO_COND (stmt));
1945 pp_c_right_paren (pp);
1946 pp_c_semicolon (pp);
1947 pp_needs_newline (pp) = true;
1948 break;
1950 case FOR_STMT:
1951 pp_c_identifier (pp, "for");
1952 pp_space (pp);
1953 pp_c_left_paren (pp);
1954 if (FOR_INIT_STMT (stmt))
1955 pp_statement (pp, FOR_INIT_STMT (stmt));
1956 else
1957 pp_c_semicolon (pp);
1958 pp_needs_newline (pp) = false;
1959 pp_c_whitespace (pp);
1960 if (FOR_COND (stmt))
1961 pp_expression (pp, FOR_COND (stmt));
1962 pp_c_semicolon (pp);
1963 pp_needs_newline (pp) = false;
1964 pp_c_whitespace (pp);
1965 if (FOR_EXPR (stmt))
1966 pp_expression (pp, FOR_EXPR (stmt));
1967 pp_c_right_paren (pp);
1968 pp_newline_and_indent (pp, 3);
1969 pp_statement (pp, FOR_BODY (stmt));
1970 pp_indentation (pp) -= 3;
1971 pp_needs_newline (pp) = true;
1972 break;
1974 /* jump-statement:
1975 goto identifier;
1976 continue ;
1977 return expression(opt) ; */
1978 case BREAK_STMT:
1979 case CONTINUE_STMT:
1980 pp_identifier (pp, code == BREAK_STMT ? "break" : "continue");
1981 pp_c_semicolon (pp);
1982 pp_needs_newline (pp) = true;
1983 break;
1985 case DECL_STMT:
1986 pp_declaration (pp, DECL_STMT_DECL (stmt));
1987 pp_needs_newline (pp) = true;
1988 break;
1990 default:
1991 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
1992 break;
1997 /* Initialize the PRETTY-PRINTER for handling C codes. */
1999 void
2000 pp_c_pretty_printer_init (c_pretty_printer *pp)
2002 pp->offset_list = 0;
2004 pp->declaration = pp_c_declaration;
2005 pp->declaration_specifiers = pp_c_declaration_specifiers;
2006 pp->declarator = pp_c_declarator;
2007 pp->direct_declarator = pp_c_direct_declarator;
2008 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
2009 pp->abstract_declarator = pp_c_abstract_declarator;
2010 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
2011 pp->ptr_operator = pp_c_pointer;
2012 pp->parameter_list = pp_c_parameter_type_list;
2013 pp->type_id = pp_c_type_id;
2014 pp->simple_type_specifier = pp_c_type_specifier;
2015 pp->function_specifier = pp_c_function_specifier;
2016 pp->storage_class_specifier = pp_c_storage_class_specifier;
2018 pp->statement = pp_c_statement;
2020 pp->id_expression = pp_c_id_expression;
2021 pp->primary_expression = pp_c_primary_expression;
2022 pp->postfix_expression = pp_c_postfix_expression;
2023 pp->unary_expression = pp_c_unary_expression;
2024 pp->initializer = pp_c_initializer;
2025 pp->multiplicative_expression = pp_c_multiplicative_expression;
2026 pp->conditional_expression = pp_c_conditional_expression;
2027 pp->assignment_expression = pp_c_assignment_expression;
2028 pp->expression = pp_c_expression;
2032 /* Print the tree T in full, on file FILE. */
2034 void
2035 print_c_tree (FILE *file, tree t)
2037 static c_pretty_printer pp_rec;
2038 static bool initialized = 0;
2039 c_pretty_printer *pp = &pp_rec;
2041 if (!initialized)
2043 initialized = 1;
2044 pp_construct (pp_base (pp), NULL, 0);
2045 pp_c_pretty_printer_init (pp);
2046 pp_needs_newline (pp) = true;
2048 pp_base (pp)->buffer->stream = file;
2050 pp_statement (pp, t);
2052 pp_newline (pp);
2053 pp_flush (pp);
2056 /* Print the tree T in full, on stderr. */
2058 void
2059 debug_c_tree (tree t)
2061 print_c_tree (stderr, t);
2062 fputc ('\n', stderr);
2065 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2066 up of T's memory address. */
2068 void
2069 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2071 const char *name;
2073 if (!DECL_P (t))
2074 abort ();
2076 if (DECL_NAME (t))
2077 name = IDENTIFIER_POINTER (DECL_NAME (t));
2078 else
2080 static char xname[8];
2081 sprintf (xname, "<U%4x>", ((unsigned)((unsigned long)(t) & 0xffff)));
2082 name = xname;
2085 pp_c_identifier (pp, name);