http://gcc.gnu.org/ml/gcc-patches/2008-02/msg01094.html
[official-gcc.git] / gcc / c-pretty-print.c
blobc8443d31fcb1c9524fad82e5ac93e00e27038cea
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "real.h"
27 #include "fixed-value.h"
28 #include "c-pretty-print.h"
29 #include "c-tree.h"
30 #include "tree-iterator.h"
31 #include "diagnostic.h"
33 /* The pretty-printer code is primarily designed to closely follow
34 (GNU) C and C++ grammars. That is to be contrasted with spaghetti
35 codes we used to have in the past. Following a structured
36 approach (preferably the official grammars) is believed to make it
37 much easier to add extensions and nifty pretty-printing effects that
38 takes expression or declaration contexts into account. */
41 #define pp_c_maybe_whitespace(PP) \
42 do { \
43 if (pp_base (PP)->padding == pp_before) \
44 pp_c_whitespace (PP); \
45 } while (0)
47 /* literal */
48 static void pp_c_char (c_pretty_printer *, int);
50 /* postfix-expression */
51 static void pp_c_initializer_list (c_pretty_printer *, tree);
52 static void pp_c_brace_enclosed_initializer_list (c_pretty_printer *, tree);
54 static void pp_c_multiplicative_expression (c_pretty_printer *, tree);
55 static void pp_c_additive_expression (c_pretty_printer *, tree);
56 static void pp_c_shift_expression (c_pretty_printer *, tree);
57 static void pp_c_relational_expression (c_pretty_printer *, tree);
58 static void pp_c_equality_expression (c_pretty_printer *, tree);
59 static void pp_c_and_expression (c_pretty_printer *, tree);
60 static void pp_c_exclusive_or_expression (c_pretty_printer *, tree);
61 static void pp_c_inclusive_or_expression (c_pretty_printer *, tree);
62 static void pp_c_logical_and_expression (c_pretty_printer *, tree);
63 static void pp_c_conditional_expression (c_pretty_printer *, tree);
64 static void pp_c_assignment_expression (c_pretty_printer *, tree);
66 /* declarations. */
69 /* Helper functions. */
71 void
72 pp_c_whitespace (c_pretty_printer *pp)
74 pp_space (pp);
75 pp_base (pp)->padding = pp_none;
78 void
79 pp_c_left_paren (c_pretty_printer *pp)
81 pp_left_paren (pp);
82 pp_base (pp)->padding = pp_none;
85 void
86 pp_c_right_paren (c_pretty_printer *pp)
88 pp_right_paren (pp);
89 pp_base (pp)->padding = pp_none;
92 void
93 pp_c_left_brace (c_pretty_printer *pp)
95 pp_left_brace (pp);
96 pp_base (pp)->padding = pp_none;
99 void
100 pp_c_right_brace (c_pretty_printer *pp)
102 pp_right_brace (pp);
103 pp_base (pp)->padding = pp_none;
106 void
107 pp_c_left_bracket (c_pretty_printer *pp)
109 pp_left_bracket (pp);
110 pp_base (pp)->padding = pp_none;
113 void
114 pp_c_right_bracket (c_pretty_printer *pp)
116 pp_right_bracket (pp);
117 pp_base (pp)->padding = pp_none;
120 void
121 pp_c_dot (c_pretty_printer *pp)
123 pp_dot (pp);
124 pp_base (pp)->padding = pp_none;
127 void
128 pp_c_ampersand (c_pretty_printer *pp)
130 pp_ampersand (pp);
131 pp_base (pp)->padding = pp_none;
134 void
135 pp_c_star (c_pretty_printer *pp)
137 pp_star (pp);
138 pp_base (pp)->padding = pp_none;
141 void
142 pp_c_arrow (c_pretty_printer *pp)
144 pp_arrow (pp);
145 pp_base (pp)->padding = pp_none;
148 void
149 pp_c_semicolon (c_pretty_printer *pp)
151 pp_semicolon (pp);
152 pp_base (pp)->padding = pp_none;
155 void
156 pp_c_complement (c_pretty_printer *pp)
158 pp_complement (pp);
159 pp_base (pp)->padding = pp_none;
162 void
163 pp_c_exclamation (c_pretty_printer *pp)
165 pp_exclamation (pp);
166 pp_base (pp)->padding = pp_none;
169 /* Print out the external representation of CV-QUALIFIER. */
171 static void
172 pp_c_cv_qualifier (c_pretty_printer *pp, const char *cv)
174 const char *p = pp_last_position_in_text (pp);
175 /* The C programming language does not have references, but it is much
176 simpler to handle those here rather than going through the same
177 logic in the C++ pretty-printer. */
178 if (p != NULL && (*p == '*' || *p == '&'))
179 pp_c_whitespace (pp);
180 pp_c_identifier (pp, cv);
183 /* Pretty-print T using the type-cast notation '( type-name )'. */
185 static void
186 pp_c_type_cast (c_pretty_printer *pp, tree t)
188 pp_c_left_paren (pp);
189 pp_type_id (pp, t);
190 pp_c_right_paren (pp);
193 /* We're about to pretty-print a pointer type as indicated by T.
194 Output a whitespace, if needed, preparing for subsequent output. */
196 void
197 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
199 if (POINTER_TYPE_P (t))
201 tree pointee = strip_pointer_operator (TREE_TYPE (t));
202 if (TREE_CODE (pointee) != ARRAY_TYPE
203 && TREE_CODE (pointee) != FUNCTION_TYPE)
204 pp_c_whitespace (pp);
209 /* Declarations. */
211 /* C++ cv-qualifiers are called type-qualifiers in C. Print out the
212 cv-qualifiers of T. If T is a declaration then it is the cv-qualifier
213 of its type. Take care of possible extensions.
215 type-qualifier-list:
216 type-qualifier
217 type-qualifier-list type-qualifier
219 type-qualifier:
220 const
221 restrict -- C99
222 __restrict__ -- GNU C
223 volatile */
225 void
226 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
228 int qualifiers;
230 if (!t || t == error_mark_node)
231 return;
233 if (!TYPE_P (t))
234 t = TREE_TYPE (t);
236 qualifiers = TYPE_QUALS (t);
237 if (qualifiers & TYPE_QUAL_CONST)
238 pp_c_cv_qualifier (pp, "const");
239 if (qualifiers & TYPE_QUAL_VOLATILE)
240 pp_c_cv_qualifier (pp, "volatile");
241 if (qualifiers & TYPE_QUAL_RESTRICT)
242 pp_c_cv_qualifier (pp, flag_isoc99 ? "restrict" : "__restrict__");
245 /* pointer:
246 * type-qualifier-list(opt)
247 * type-qualifier-list(opt) pointer */
249 static void
250 pp_c_pointer (c_pretty_printer *pp, tree t)
252 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
253 t = TREE_TYPE (t);
254 switch (TREE_CODE (t))
256 case POINTER_TYPE:
257 /* It is easier to handle C++ reference types here. */
258 case REFERENCE_TYPE:
259 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
260 pp_c_pointer (pp, TREE_TYPE (t));
261 if (TREE_CODE (t) == POINTER_TYPE)
262 pp_c_star (pp);
263 else
264 pp_c_ampersand (pp);
265 pp_c_type_qualifier_list (pp, t);
266 break;
268 /* ??? This node is now in GENERIC and so shouldn't be here. But
269 we'll fix that later. */
270 case DECL_EXPR:
271 pp_declaration (pp, DECL_EXPR_DECL (t));
272 pp_needs_newline (pp) = true;
273 break;
275 default:
276 pp_unsupported_tree (pp, t);
280 /* type-specifier:
281 void
282 char
283 short
285 long
286 float
287 double
288 signed
289 unsigned
290 _Bool -- C99
291 _Complex -- C99
292 _Imaginary -- C99
293 struct-or-union-specifier
294 enum-specifier
295 typedef-name.
297 GNU extensions.
298 simple-type-specifier:
299 __complex__
300 __vector__ */
302 void
303 pp_c_type_specifier (c_pretty_printer *pp, tree t)
305 const enum tree_code code = TREE_CODE (t);
306 switch (code)
308 case ERROR_MARK:
309 pp_c_identifier (pp, "<type-error>");
310 break;
312 case IDENTIFIER_NODE:
313 pp_c_tree_decl_identifier (pp, t);
314 break;
316 case VOID_TYPE:
317 case BOOLEAN_TYPE:
318 case INTEGER_TYPE:
319 case REAL_TYPE:
320 case FIXED_POINT_TYPE:
321 if (TYPE_NAME (t))
323 t = TYPE_NAME (t);
324 pp_c_type_specifier (pp, t);
326 else
328 int prec = TYPE_PRECISION (t);
329 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (t)))
330 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_SATURATING (t));
331 else
332 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
333 if (TYPE_NAME (t))
335 pp_c_type_specifier (pp, t);
336 if (TYPE_PRECISION (t) != prec)
338 pp_string (pp, ":");
339 pp_decimal_int (pp, prec);
342 else
344 switch (code)
346 case INTEGER_TYPE:
347 pp_string (pp, (TYPE_UNSIGNED (t)
348 ? "<unnamed-unsigned:"
349 : "<unnamed-signed:"));
350 break;
351 case REAL_TYPE:
352 pp_string (pp, "<unnamed-float:");
353 break;
354 case FIXED_POINT_TYPE:
355 pp_string (pp, "<unnamed-fixed:");
356 break;
357 default:
358 gcc_unreachable ();
360 pp_decimal_int (pp, prec);
361 pp_string (pp, ">");
364 break;
366 case TYPE_DECL:
367 if (DECL_NAME (t))
368 pp_id_expression (pp, t);
369 else
370 pp_c_identifier (pp, "<typedef-error>");
371 break;
373 case UNION_TYPE:
374 case RECORD_TYPE:
375 case ENUMERAL_TYPE:
376 if (code == UNION_TYPE)
377 pp_c_identifier (pp, "union");
378 else if (code == RECORD_TYPE)
379 pp_c_identifier (pp, "struct");
380 else if (code == ENUMERAL_TYPE)
381 pp_c_identifier (pp, "enum");
382 else
383 pp_c_identifier (pp, "<tag-error>");
385 if (TYPE_NAME (t))
386 pp_id_expression (pp, TYPE_NAME (t));
387 else
388 pp_c_identifier (pp, "<anonymous>");
389 break;
391 default:
392 pp_unsupported_tree (pp, t);
393 break;
397 /* specifier-qualifier-list:
398 type-specifier specifier-qualifier-list-opt
399 type-qualifier specifier-qualifier-list-opt
402 Implementation note: Because of the non-linearities in array or
403 function declarations, this routine prints not just the
404 specifier-qualifier-list of such entities or types of such entities,
405 but also the 'pointer' production part of their declarators. The
406 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
408 void
409 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
411 const enum tree_code code = TREE_CODE (t);
413 if (TREE_CODE (t) != POINTER_TYPE)
414 pp_c_type_qualifier_list (pp, t);
415 switch (code)
417 case REFERENCE_TYPE:
418 case POINTER_TYPE:
420 /* Get the types-specifier of this type. */
421 tree pointee = strip_pointer_operator (TREE_TYPE (t));
422 pp_c_specifier_qualifier_list (pp, pointee);
423 if (TREE_CODE (pointee) == ARRAY_TYPE
424 || TREE_CODE (pointee) == FUNCTION_TYPE)
426 pp_c_whitespace (pp);
427 pp_c_left_paren (pp);
429 else if (!c_dialect_cxx ())
430 pp_c_whitespace (pp);
431 pp_ptr_operator (pp, t);
433 break;
435 case FUNCTION_TYPE:
436 case ARRAY_TYPE:
437 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
438 break;
440 case VECTOR_TYPE:
441 case COMPLEX_TYPE:
442 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
443 if (code == COMPLEX_TYPE)
444 pp_c_identifier (pp, flag_isoc99 ? "_Complex" : "__complex__");
445 else if (code == VECTOR_TYPE)
446 pp_c_identifier (pp, "__vector__");
447 break;
449 default:
450 pp_simple_type_specifier (pp, t);
451 break;
455 /* parameter-type-list:
456 parameter-list
457 parameter-list , ...
459 parameter-list:
460 parameter-declaration
461 parameter-list , parameter-declaration
463 parameter-declaration:
464 declaration-specifiers declarator
465 declaration-specifiers abstract-declarator(opt) */
467 void
468 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
470 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
471 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
472 pp_c_left_paren (pp);
473 if (parms == void_list_node)
474 pp_c_identifier (pp, "void");
475 else
477 bool first = true;
478 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
480 if (!first)
481 pp_separate_with (pp, ',');
482 first = false;
483 pp_declaration_specifiers
484 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
485 if (want_parm_decl)
486 pp_declarator (pp, parms);
487 else
488 pp_abstract_declarator (pp, TREE_VALUE (parms));
491 pp_c_right_paren (pp);
494 /* abstract-declarator:
495 pointer
496 pointer(opt) direct-abstract-declarator */
498 static void
499 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
501 if (TREE_CODE (t) == POINTER_TYPE)
503 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
504 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
505 pp_c_right_paren (pp);
506 t = TREE_TYPE (t);
509 pp_direct_abstract_declarator (pp, t);
512 /* direct-abstract-declarator:
513 ( abstract-declarator )
514 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
515 direct-abstract-declarator(opt) [ * ]
516 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
518 void
519 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
521 switch (TREE_CODE (t))
523 case POINTER_TYPE:
524 pp_abstract_declarator (pp, t);
525 break;
527 case FUNCTION_TYPE:
528 pp_c_parameter_type_list (pp, t);
529 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
530 break;
532 case ARRAY_TYPE:
533 pp_c_left_bracket (pp);
534 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
536 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (t));
537 tree type = TREE_TYPE (maxval);
539 if (host_integerp (maxval, 0))
540 pp_wide_integer (pp, tree_low_cst (maxval, 0) + 1);
541 else
542 pp_expression (pp, fold_build2 (PLUS_EXPR, type, maxval,
543 build_int_cst (type, 1)));
545 pp_c_right_bracket (pp);
546 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
547 break;
549 case IDENTIFIER_NODE:
550 case VOID_TYPE:
551 case BOOLEAN_TYPE:
552 case INTEGER_TYPE:
553 case REAL_TYPE:
554 case FIXED_POINT_TYPE:
555 case ENUMERAL_TYPE:
556 case RECORD_TYPE:
557 case UNION_TYPE:
558 case VECTOR_TYPE:
559 case COMPLEX_TYPE:
560 case TYPE_DECL:
561 break;
563 default:
564 pp_unsupported_tree (pp, t);
565 break;
569 /* type-name:
570 specifier-qualifier-list abstract-declarator(opt) */
572 void
573 pp_c_type_id (c_pretty_printer *pp, tree t)
575 pp_c_specifier_qualifier_list (pp, t);
576 pp_abstract_declarator (pp, t);
579 /* storage-class-specifier:
580 typedef
581 extern
582 static
583 auto
584 register */
586 void
587 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
589 if (TREE_CODE (t) == TYPE_DECL)
590 pp_c_identifier (pp, "typedef");
591 else if (DECL_P (t))
593 if (DECL_REGISTER (t))
594 pp_c_identifier (pp, "register");
595 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
596 pp_c_identifier (pp, "static");
600 /* function-specifier:
601 inline */
603 void
604 pp_c_function_specifier (c_pretty_printer *pp, tree t)
606 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
607 pp_c_identifier (pp, "inline");
610 /* declaration-specifiers:
611 storage-class-specifier declaration-specifiers(opt)
612 type-specifier declaration-specifiers(opt)
613 type-qualifier declaration-specifiers(opt)
614 function-specifier declaration-specifiers(opt) */
616 void
617 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
619 pp_storage_class_specifier (pp, t);
620 pp_function_specifier (pp, t);
621 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
624 /* direct-declarator
625 identifier
626 ( declarator )
627 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
628 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
629 direct-declarator [ type-qualifier-list static assignment-expression ]
630 direct-declarator [ type-qualifier-list * ]
631 direct-declarator ( parameter-type-list )
632 direct-declarator ( identifier-list(opt) ) */
634 void
635 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
637 switch (TREE_CODE (t))
639 case VAR_DECL:
640 case PARM_DECL:
641 case TYPE_DECL:
642 case FIELD_DECL:
643 case LABEL_DECL:
644 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
645 pp_c_tree_decl_identifier (pp, t);
646 break;
648 case ARRAY_TYPE:
649 case POINTER_TYPE:
650 pp_abstract_declarator (pp, TREE_TYPE (t));
651 break;
653 case FUNCTION_TYPE:
654 pp_parameter_list (pp, t);
655 pp_abstract_declarator (pp, TREE_TYPE (t));
656 break;
658 case FUNCTION_DECL:
659 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
660 pp_c_tree_decl_identifier (pp, t);
661 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
662 pp_abstract_declarator (pp, TREE_TYPE (t));
663 else
665 pp_parameter_list (pp, t);
666 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
668 break;
670 case INTEGER_TYPE:
671 case REAL_TYPE:
672 case FIXED_POINT_TYPE:
673 case ENUMERAL_TYPE:
674 case UNION_TYPE:
675 case RECORD_TYPE:
676 break;
678 default:
679 pp_unsupported_tree (pp, t);
680 break;
685 /* declarator:
686 pointer(opt) direct-declarator */
688 void
689 pp_c_declarator (c_pretty_printer *pp, tree t)
691 switch (TREE_CODE (t))
693 case INTEGER_TYPE:
694 case REAL_TYPE:
695 case FIXED_POINT_TYPE:
696 case ENUMERAL_TYPE:
697 case UNION_TYPE:
698 case RECORD_TYPE:
699 break;
701 case VAR_DECL:
702 case PARM_DECL:
703 case FIELD_DECL:
704 case ARRAY_TYPE:
705 case FUNCTION_TYPE:
706 case FUNCTION_DECL:
707 case TYPE_DECL:
708 pp_direct_declarator (pp, t);
709 break;
712 default:
713 pp_unsupported_tree (pp, t);
714 break;
718 /* declaration:
719 declaration-specifiers init-declarator-list(opt) ; */
721 void
722 pp_c_declaration (c_pretty_printer *pp, tree t)
724 pp_declaration_specifiers (pp, t);
725 pp_c_init_declarator (pp, t);
728 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
730 void
731 pp_c_attributes (c_pretty_printer *pp, tree attributes)
733 if (attributes == NULL_TREE)
734 return;
736 pp_c_identifier (pp, "__attribute__");
737 pp_c_left_paren (pp);
738 pp_c_left_paren (pp);
739 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
741 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
742 if (TREE_VALUE (attributes))
743 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
745 if (TREE_CHAIN (attributes))
746 pp_separate_with (pp, ',');
748 pp_c_right_paren (pp);
749 pp_c_right_paren (pp);
752 /* function-definition:
753 declaration-specifiers declarator compound-statement */
755 void
756 pp_c_function_definition (c_pretty_printer *pp, tree t)
758 pp_declaration_specifiers (pp, t);
759 pp_declarator (pp, t);
760 pp_needs_newline (pp) = true;
761 pp_statement (pp, DECL_SAVED_TREE (t));
762 pp_newline (pp);
763 pp_flush (pp);
767 /* Expressions. */
769 /* Print out a c-char. This is called solely for characters which are
770 in the *target* execution character set. We ought to convert them
771 back to the *host* execution character set before printing, but we
772 have no way to do this at present. A decent compromise is to print
773 all characters as if they were in the host execution character set,
774 and not attempt to recover any named escape characters, but render
775 all unprintables as octal escapes. If the host and target character
776 sets are the same, this produces relatively readable output. If they
777 are not the same, strings may appear as gibberish, but that's okay
778 (in fact, it may well be what the reader wants, e.g. if they are looking
779 to see if conversion to the target character set happened correctly).
781 A special case: we need to prefix \, ", and ' with backslashes. It is
782 correct to do so for the *host*'s \, ", and ', because the rest of the
783 file appears in the host character set. */
785 static void
786 pp_c_char (c_pretty_printer *pp, int c)
788 if (ISPRINT (c))
790 switch (c)
792 case '\\': pp_string (pp, "\\\\"); break;
793 case '\'': pp_string (pp, "\\\'"); break;
794 case '\"': pp_string (pp, "\\\""); break;
795 default: pp_character (pp, c);
798 else
799 pp_scalar (pp, "\\%03o", (unsigned) c);
802 /* Print out a STRING literal. */
804 void
805 pp_c_string_literal (c_pretty_printer *pp, tree s)
807 const char *p = TREE_STRING_POINTER (s);
808 int n = TREE_STRING_LENGTH (s) - 1;
809 int i;
810 pp_doublequote (pp);
811 for (i = 0; i < n; ++i)
812 pp_c_char (pp, p[i]);
813 pp_doublequote (pp);
816 /* Pretty-print an INTEGER literal. */
818 static void
819 pp_c_integer_constant (c_pretty_printer *pp, tree i)
821 tree type = TREE_TYPE (i);
823 if (TREE_INT_CST_HIGH (i) == 0)
824 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
825 else
827 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (i);
828 HOST_WIDE_INT high = TREE_INT_CST_HIGH (i);
829 if (tree_int_cst_sgn (i) < 0)
831 pp_character (pp, '-');
832 high = ~high + !low;
833 low = -low;
835 sprintf (pp_buffer (pp)->digit_buffer,
836 HOST_WIDE_INT_PRINT_DOUBLE_HEX, high, low);
837 pp_string (pp, pp_buffer (pp)->digit_buffer);
839 if (TYPE_UNSIGNED (type))
840 pp_character (pp, 'u');
841 if (type == long_integer_type_node || type == long_unsigned_type_node)
842 pp_character (pp, 'l');
843 else if (type == long_long_integer_type_node
844 || type == long_long_unsigned_type_node)
845 pp_string (pp, "ll");
848 /* Print out a CHARACTER literal. */
850 static void
851 pp_c_character_constant (c_pretty_printer *pp, tree c)
853 tree type = TREE_TYPE (c);
854 if (type == wchar_type_node)
855 pp_character (pp, 'L');
856 pp_quote (pp);
857 if (host_integerp (c, TYPE_UNSIGNED (type)))
858 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
859 else
860 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
861 pp_quote (pp);
864 /* Print out a BOOLEAN literal. */
866 static void
867 pp_c_bool_constant (c_pretty_printer *pp, tree b)
869 if (b == boolean_false_node)
871 if (c_dialect_cxx ())
872 pp_c_identifier (pp, "false");
873 else if (flag_isoc99)
874 pp_c_identifier (pp, "_False");
875 else
876 pp_unsupported_tree (pp, b);
878 else if (b == boolean_true_node)
880 if (c_dialect_cxx ())
881 pp_c_identifier (pp, "true");
882 else if (flag_isoc99)
883 pp_c_identifier (pp, "_True");
884 else
885 pp_unsupported_tree (pp, b);
887 else if (TREE_CODE (b) == INTEGER_CST)
888 pp_c_integer_constant (pp, b);
889 else
890 pp_unsupported_tree (pp, b);
893 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
894 false; that means the value was obtained by a cast, in which case
895 print out the type-id part of the cast-expression -- the casted value
896 is then printed by pp_c_integer_literal. */
898 static bool
899 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
901 bool value_is_named = true;
902 tree type = TREE_TYPE (e);
903 tree value;
905 /* Find the name of this constant. */
906 for (value = TYPE_VALUES (type);
907 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
908 value = TREE_CHAIN (value))
911 if (value != NULL_TREE)
912 pp_id_expression (pp, TREE_PURPOSE (value));
913 else
915 /* Value must have been cast. */
916 pp_c_type_cast (pp, type);
917 value_is_named = false;
920 return value_is_named;
923 /* Print out a REAL value as a decimal-floating-constant. */
925 static void
926 pp_c_floating_constant (c_pretty_printer *pp, tree r)
928 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
929 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
930 pp_string (pp, pp_buffer(pp)->digit_buffer);
931 if (TREE_TYPE (r) == float_type_node)
932 pp_character (pp, 'f');
933 else if (TREE_TYPE (r) == long_double_type_node)
934 pp_character (pp, 'l');
935 else if (TREE_TYPE (r) == dfloat128_type_node)
936 pp_string (pp, "dl");
937 else if (TREE_TYPE (r) == dfloat64_type_node)
938 pp_string (pp, "dd");
939 else if (TREE_TYPE (r) == dfloat32_type_node)
940 pp_string (pp, "df");
943 /* Print out a FIXED value as a decimal-floating-constant. */
945 static void
946 pp_c_fixed_constant (c_pretty_printer *pp, tree r)
948 fixed_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_FIXED_CST (r),
949 sizeof (pp_buffer (pp)->digit_buffer));
950 pp_string (pp, pp_buffer(pp)->digit_buffer);
953 /* Pretty-print a compound literal expression. GNU extensions include
954 vector constants. */
956 static void
957 pp_c_compound_literal (c_pretty_printer *pp, tree e)
959 tree type = TREE_TYPE (e);
960 pp_c_type_cast (pp, type);
962 switch (TREE_CODE (type))
964 case RECORD_TYPE:
965 case UNION_TYPE:
966 case ARRAY_TYPE:
967 case VECTOR_TYPE:
968 case COMPLEX_TYPE:
969 pp_c_brace_enclosed_initializer_list (pp, e);
970 break;
972 default:
973 pp_unsupported_tree (pp, e);
974 break;
978 /* constant:
979 integer-constant
980 floating-constant
981 fixed-point-constant
982 enumeration-constant
983 character-constant */
985 void
986 pp_c_constant (c_pretty_printer *pp, tree e)
988 const enum tree_code code = TREE_CODE (e);
990 switch (code)
992 case INTEGER_CST:
994 tree type = TREE_TYPE (e);
995 if (type == boolean_type_node)
996 pp_c_bool_constant (pp, e);
997 else if (type == char_type_node)
998 pp_c_character_constant (pp, e);
999 else if (TREE_CODE (type) == ENUMERAL_TYPE
1000 && pp_c_enumeration_constant (pp, e))
1002 else
1003 pp_c_integer_constant (pp, e);
1005 break;
1007 case REAL_CST:
1008 pp_c_floating_constant (pp, e);
1009 break;
1011 case FIXED_CST:
1012 pp_c_fixed_constant (pp, e);
1013 break;
1015 case STRING_CST:
1016 pp_c_string_literal (pp, e);
1017 break;
1019 case COMPLEX_CST:
1020 /* Sometimes, we are confused and we think a complex literal
1021 is a constant. Such thing is a compound literal which
1022 grammatically belongs to postifx-expr production. */
1023 pp_c_compound_literal (pp, e);
1024 break;
1026 default:
1027 pp_unsupported_tree (pp, e);
1028 break;
1032 /* Pretty-print an IDENTIFIER_NODE, preceded by whitespace is necessary. */
1034 void
1035 pp_c_identifier (c_pretty_printer *pp, const char *id)
1037 pp_c_maybe_whitespace (pp);
1038 pp_identifier (pp, id);
1039 pp_base (pp)->padding = pp_before;
1042 /* Pretty-print a C primary-expression.
1043 primary-expression:
1044 identifier
1045 constant
1046 string-literal
1047 ( expression ) */
1049 void
1050 pp_c_primary_expression (c_pretty_printer *pp, tree e)
1052 switch (TREE_CODE (e))
1054 case VAR_DECL:
1055 case PARM_DECL:
1056 case FIELD_DECL:
1057 case CONST_DECL:
1058 case FUNCTION_DECL:
1059 case LABEL_DECL:
1060 pp_c_tree_decl_identifier (pp, e);
1061 break;
1063 case IDENTIFIER_NODE:
1064 pp_c_tree_identifier (pp, e);
1065 break;
1067 case ERROR_MARK:
1068 pp_c_identifier (pp, "<erroneous-expression>");
1069 break;
1071 case RESULT_DECL:
1072 pp_c_identifier (pp, "<return-value>");
1073 break;
1075 case INTEGER_CST:
1076 case REAL_CST:
1077 case FIXED_CST:
1078 case STRING_CST:
1079 pp_c_constant (pp, e);
1080 break;
1082 case TARGET_EXPR:
1083 pp_c_identifier (pp, "__builtin_memcpy");
1084 pp_c_left_paren (pp);
1085 pp_ampersand (pp);
1086 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1087 pp_separate_with (pp, ',');
1088 pp_ampersand (pp);
1089 pp_initializer (pp, TREE_OPERAND (e, 1));
1090 if (TREE_OPERAND (e, 2))
1092 pp_separate_with (pp, ',');
1093 pp_c_expression (pp, TREE_OPERAND (e, 2));
1095 pp_c_right_paren (pp);
1096 break;
1098 default:
1099 /* FIXME: Make sure we won't get into an infinie loop. */
1100 pp_c_left_paren (pp);
1101 pp_expression (pp, e);
1102 pp_c_right_paren (pp);
1103 break;
1107 /* Print out a C initializer -- also support C compound-literals.
1108 initializer:
1109 assignment-expression:
1110 { initializer-list }
1111 { initializer-list , } */
1113 static void
1114 pp_c_initializer (c_pretty_printer *pp, tree e)
1116 if (TREE_CODE (e) == CONSTRUCTOR)
1117 pp_c_brace_enclosed_initializer_list (pp, e);
1118 else
1119 pp_expression (pp, e);
1122 /* init-declarator:
1123 declarator:
1124 declarator = initializer */
1126 void
1127 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1129 pp_declarator (pp, t);
1130 /* We don't want to output function definitions here. There are handled
1131 elsewhere (and the syntactic form is bogus anyway). */
1132 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1134 tree init = DECL_INITIAL (t);
1135 /* This C++ bit is handled here because it is easier to do so.
1136 In templates, the C++ parser builds a TREE_LIST for a
1137 direct-initialization; the TREE_PURPOSE is the variable to
1138 initialize and the TREE_VALUE is the initializer. */
1139 if (TREE_CODE (init) == TREE_LIST)
1141 pp_c_left_paren (pp);
1142 pp_expression (pp, TREE_VALUE (init));
1143 pp_right_paren (pp);
1145 else
1147 pp_space (pp);
1148 pp_equal (pp);
1149 pp_space (pp);
1150 pp_c_initializer (pp, init);
1155 /* initializer-list:
1156 designation(opt) initializer
1157 initializer-list , designation(opt) initializer
1159 designation:
1160 designator-list =
1162 designator-list:
1163 designator
1164 designator-list designator
1166 designator:
1167 [ constant-expression ]
1168 identifier */
1170 static void
1171 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1173 tree type = TREE_TYPE (e);
1174 const enum tree_code code = TREE_CODE (type);
1176 switch (code)
1178 case RECORD_TYPE:
1179 case UNION_TYPE:
1180 case ARRAY_TYPE:
1182 tree init = TREE_OPERAND (e, 0);
1183 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1185 if (code == RECORD_TYPE || code == UNION_TYPE)
1187 pp_c_dot (pp);
1188 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1190 else
1192 pp_c_left_bracket (pp);
1193 if (TREE_PURPOSE (init))
1194 pp_c_constant (pp, TREE_PURPOSE (init));
1195 pp_c_right_bracket (pp);
1197 pp_c_whitespace (pp);
1198 pp_equal (pp);
1199 pp_c_whitespace (pp);
1200 pp_initializer (pp, TREE_VALUE (init));
1201 if (TREE_CHAIN (init))
1202 pp_separate_with (pp, ',');
1205 return;
1207 case VECTOR_TYPE:
1208 if (TREE_CODE (e) == VECTOR_CST)
1209 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1210 else if (TREE_CODE (e) == CONSTRUCTOR)
1211 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1212 else
1213 break;
1214 return;
1216 case COMPLEX_TYPE:
1217 if (TREE_CODE (e) == CONSTRUCTOR)
1218 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1219 else if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1221 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1222 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1223 pp_separate_with (pp, ',');
1224 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1226 else
1227 break;
1228 return;
1230 default:
1231 break;
1234 pp_unsupported_tree (pp, type);
1237 /* Pretty-print a brace-enclosed initializer-list. */
1239 static void
1240 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1242 pp_c_left_brace (pp);
1243 pp_c_initializer_list (pp, l);
1244 pp_c_right_brace (pp);
1248 /* This is a convenient function, used to bridge gap between C and C++
1249 grammars.
1251 id-expression:
1252 identifier */
1254 void
1255 pp_c_id_expression (c_pretty_printer *pp, tree t)
1257 switch (TREE_CODE (t))
1259 case VAR_DECL:
1260 case PARM_DECL:
1261 case CONST_DECL:
1262 case TYPE_DECL:
1263 case FUNCTION_DECL:
1264 case FIELD_DECL:
1265 case LABEL_DECL:
1266 pp_c_tree_decl_identifier (pp, t);
1267 break;
1269 case IDENTIFIER_NODE:
1270 pp_c_tree_identifier (pp, t);
1271 break;
1273 default:
1274 pp_unsupported_tree (pp, t);
1275 break;
1279 /* postfix-expression:
1280 primary-expression
1281 postfix-expression [ expression ]
1282 postfix-expression ( argument-expression-list(opt) )
1283 postfix-expression . identifier
1284 postfix-expression -> identifier
1285 postfix-expression ++
1286 postfix-expression --
1287 ( type-name ) { initializer-list }
1288 ( type-name ) { initializer-list , } */
1290 void
1291 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1293 enum tree_code code = TREE_CODE (e);
1294 switch (code)
1296 case POSTINCREMENT_EXPR:
1297 case POSTDECREMENT_EXPR:
1298 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1299 pp_identifier (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1300 break;
1302 case ARRAY_REF:
1303 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1304 pp_c_left_bracket (pp);
1305 pp_expression (pp, TREE_OPERAND (e, 1));
1306 pp_c_right_bracket (pp);
1307 break;
1309 case CALL_EXPR:
1311 call_expr_arg_iterator iter;
1312 tree arg;
1313 pp_postfix_expression (pp, CALL_EXPR_FN (e));
1314 pp_c_left_paren (pp);
1315 FOR_EACH_CALL_EXPR_ARG (arg, iter, e)
1317 pp_expression (pp, arg);
1318 if (more_call_expr_args_p (&iter))
1319 pp_separate_with (pp, ',');
1321 pp_c_right_paren (pp);
1322 break;
1325 case UNORDERED_EXPR:
1326 pp_c_identifier (pp, flag_isoc99
1327 ? "isunordered"
1328 : "__builtin_isunordered");
1329 goto two_args_fun;
1331 case ORDERED_EXPR:
1332 pp_c_identifier (pp, flag_isoc99
1333 ? "!isunordered"
1334 : "!__builtin_isunordered");
1335 goto two_args_fun;
1337 case UNLT_EXPR:
1338 pp_c_identifier (pp, flag_isoc99
1339 ? "!isgreaterequal"
1340 : "!__builtin_isgreaterequal");
1341 goto two_args_fun;
1343 case UNLE_EXPR:
1344 pp_c_identifier (pp, flag_isoc99
1345 ? "!isgreater"
1346 : "!__builtin_isgreater");
1347 goto two_args_fun;
1349 case UNGT_EXPR:
1350 pp_c_identifier (pp, flag_isoc99
1351 ? "!islessequal"
1352 : "!__builtin_islessequal");
1353 goto two_args_fun;
1355 case UNGE_EXPR:
1356 pp_c_identifier (pp, flag_isoc99
1357 ? "!isless"
1358 : "!__builtin_isless");
1359 goto two_args_fun;
1361 case UNEQ_EXPR:
1362 pp_c_identifier (pp, flag_isoc99
1363 ? "!islessgreater"
1364 : "!__builtin_islessgreater");
1365 goto two_args_fun;
1367 case LTGT_EXPR:
1368 pp_c_identifier (pp, flag_isoc99
1369 ? "islessgreater"
1370 : "__builtin_islessgreater");
1371 goto two_args_fun;
1373 two_args_fun:
1374 pp_c_left_paren (pp);
1375 pp_expression (pp, TREE_OPERAND (e, 0));
1376 pp_separate_with (pp, ',');
1377 pp_expression (pp, TREE_OPERAND (e, 1));
1378 pp_c_right_paren (pp);
1379 break;
1381 case ABS_EXPR:
1382 pp_c_identifier (pp, "__builtin_abs");
1383 pp_c_left_paren (pp);
1384 pp_expression (pp, TREE_OPERAND (e, 0));
1385 pp_c_right_paren (pp);
1386 break;
1388 case COMPONENT_REF:
1390 tree object = TREE_OPERAND (e, 0);
1391 if (TREE_CODE (object) == INDIRECT_REF)
1393 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1394 pp_c_arrow (pp);
1396 else
1398 pp_postfix_expression (pp, object);
1399 pp_c_dot (pp);
1401 pp_expression (pp, TREE_OPERAND (e, 1));
1403 break;
1405 case COMPLEX_CST:
1406 case VECTOR_CST:
1407 case COMPLEX_EXPR:
1408 pp_c_compound_literal (pp, e);
1409 break;
1411 case COMPOUND_LITERAL_EXPR:
1412 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1413 /* Fall through. */
1414 case CONSTRUCTOR:
1415 pp_initializer (pp, e);
1416 break;
1418 case VA_ARG_EXPR:
1419 pp_c_identifier (pp, "__builtin_va_arg");
1420 pp_c_left_paren (pp);
1421 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1422 pp_separate_with (pp, ',');
1423 pp_type_id (pp, TREE_TYPE (e));
1424 pp_c_right_paren (pp);
1425 break;
1427 case ADDR_EXPR:
1428 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1430 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1431 break;
1433 /* else fall through. */
1435 default:
1436 pp_primary_expression (pp, e);
1437 break;
1441 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1443 void
1444 pp_c_expression_list (c_pretty_printer *pp, tree e)
1446 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1448 pp_expression (pp, TREE_VALUE (e));
1449 if (TREE_CHAIN (e))
1450 pp_separate_with (pp, ',');
1454 /* Print out V, which contains the elements of a constructor. */
1456 void
1457 pp_c_constructor_elts (c_pretty_printer *pp, VEC(constructor_elt,gc) *v)
1459 unsigned HOST_WIDE_INT ix;
1460 tree value;
1462 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1464 pp_expression (pp, value);
1465 if (ix != VEC_length (constructor_elt, v) - 1)
1466 pp_separate_with (pp, ',');
1470 /* Print out an expression-list in parens, as if it were the argument
1471 list to a function. */
1473 void
1474 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1476 pp_c_left_paren (pp);
1477 if (t && TREE_CODE (t) == TREE_LIST)
1478 pp_c_expression_list (pp, t);
1479 pp_c_right_paren (pp);
1482 /* unary-expression:
1483 postfix-expression
1484 ++ cast-expression
1485 -- cast-expression
1486 unary-operator cast-expression
1487 sizeof unary-expression
1488 sizeof ( type-id )
1490 unary-operator: one of
1491 * & + - ! ~
1493 GNU extensions.
1494 unary-expression:
1495 __alignof__ unary-expression
1496 __alignof__ ( type-id )
1497 __real__ unary-expression
1498 __imag__ unary-expression */
1500 void
1501 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1503 enum tree_code code = TREE_CODE (e);
1504 switch (code)
1506 case PREINCREMENT_EXPR:
1507 case PREDECREMENT_EXPR:
1508 pp_identifier (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1509 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1510 break;
1512 case ADDR_EXPR:
1513 case INDIRECT_REF:
1514 case NEGATE_EXPR:
1515 case BIT_NOT_EXPR:
1516 case TRUTH_NOT_EXPR:
1517 case CONJ_EXPR:
1518 /* String literal are used by address. */
1519 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1520 pp_ampersand (pp);
1521 else if (code == INDIRECT_REF)
1522 pp_c_star (pp);
1523 else if (code == NEGATE_EXPR)
1524 pp_minus (pp);
1525 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1526 pp_complement (pp);
1527 else if (code == TRUTH_NOT_EXPR)
1528 pp_exclamation (pp);
1529 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1530 break;
1532 case REALPART_EXPR:
1533 case IMAGPART_EXPR:
1534 pp_c_identifier (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1535 pp_c_whitespace (pp);
1536 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1537 break;
1539 default:
1540 pp_postfix_expression (pp, e);
1541 break;
1545 /* cast-expression:
1546 unary-expression
1547 ( type-name ) cast-expression */
1549 void
1550 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1552 switch (TREE_CODE (e))
1554 case FLOAT_EXPR:
1555 case FIX_TRUNC_EXPR:
1556 case CONVERT_EXPR:
1557 case NOP_EXPR:
1558 pp_c_type_cast (pp, TREE_TYPE (e));
1559 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1560 break;
1562 default:
1563 pp_unary_expression (pp, e);
1567 /* multiplicative-expression:
1568 cast-expression
1569 multiplicative-expression * cast-expression
1570 multiplicative-expression / cast-expression
1571 multiplicative-expression % cast-expression */
1573 static void
1574 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1576 enum tree_code code = TREE_CODE (e);
1577 switch (code)
1579 case MULT_EXPR:
1580 case TRUNC_DIV_EXPR:
1581 case TRUNC_MOD_EXPR:
1582 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1583 pp_c_whitespace (pp);
1584 if (code == MULT_EXPR)
1585 pp_c_star (pp);
1586 else if (code == TRUNC_DIV_EXPR)
1587 pp_slash (pp);
1588 else
1589 pp_modulo (pp);
1590 pp_c_whitespace (pp);
1591 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1592 break;
1594 default:
1595 pp_c_cast_expression (pp, e);
1596 break;
1600 /* additive-expression:
1601 multiplicative-expression
1602 additive-expression + multiplicative-expression
1603 additive-expression - multiplicative-expression */
1605 static void
1606 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1608 enum tree_code code = TREE_CODE (e);
1609 switch (code)
1611 case POINTER_PLUS_EXPR:
1612 case PLUS_EXPR:
1613 case MINUS_EXPR:
1614 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1615 pp_c_whitespace (pp);
1616 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1617 pp_plus (pp);
1618 else
1619 pp_minus (pp);
1620 pp_c_whitespace (pp);
1621 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1622 break;
1624 default:
1625 pp_multiplicative_expression (pp, e);
1626 break;
1630 /* additive-expression:
1631 additive-expression
1632 shift-expression << additive-expression
1633 shift-expression >> additive-expression */
1635 static void
1636 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1638 enum tree_code code = TREE_CODE (e);
1639 switch (code)
1641 case LSHIFT_EXPR:
1642 case RSHIFT_EXPR:
1643 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1644 pp_c_whitespace (pp);
1645 pp_identifier (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1646 pp_c_whitespace (pp);
1647 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1648 break;
1650 default:
1651 pp_c_additive_expression (pp, e);
1655 /* relational-expression:
1656 shift-expression
1657 relational-expression < shift-expression
1658 relational-expression > shift-expression
1659 relational-expression <= shift-expression
1660 relational-expression >= shift-expression */
1662 static void
1663 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1665 enum tree_code code = TREE_CODE (e);
1666 switch (code)
1668 case LT_EXPR:
1669 case GT_EXPR:
1670 case LE_EXPR:
1671 case GE_EXPR:
1672 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1673 pp_c_whitespace (pp);
1674 if (code == LT_EXPR)
1675 pp_less (pp);
1676 else if (code == GT_EXPR)
1677 pp_greater (pp);
1678 else if (code == LE_EXPR)
1679 pp_identifier (pp, "<=");
1680 else if (code == GE_EXPR)
1681 pp_identifier (pp, ">=");
1682 pp_c_whitespace (pp);
1683 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1684 break;
1686 default:
1687 pp_c_shift_expression (pp, e);
1688 break;
1692 /* equality-expression:
1693 relational-expression
1694 equality-expression == relational-expression
1695 equality-equality != relational-expression */
1697 static void
1698 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1700 enum tree_code code = TREE_CODE (e);
1701 switch (code)
1703 case EQ_EXPR:
1704 case NE_EXPR:
1705 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1706 pp_c_whitespace (pp);
1707 pp_identifier (pp, code == EQ_EXPR ? "==" : "!=");
1708 pp_c_whitespace (pp);
1709 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1710 break;
1712 default:
1713 pp_c_relational_expression (pp, e);
1714 break;
1718 /* AND-expression:
1719 equality-expression
1720 AND-expression & equality-equality */
1722 static void
1723 pp_c_and_expression (c_pretty_printer *pp, tree e)
1725 if (TREE_CODE (e) == BIT_AND_EXPR)
1727 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1728 pp_c_whitespace (pp);
1729 pp_ampersand (pp);
1730 pp_c_whitespace (pp);
1731 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1733 else
1734 pp_c_equality_expression (pp, e);
1737 /* exclusive-OR-expression:
1738 AND-expression
1739 exclusive-OR-expression ^ AND-expression */
1741 static void
1742 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1744 if (TREE_CODE (e) == BIT_XOR_EXPR
1745 || TREE_CODE (e) == TRUTH_XOR_EXPR)
1747 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1748 if (TREE_CODE (e) == BIT_XOR_EXPR)
1749 pp_c_maybe_whitespace (pp);
1750 else
1751 pp_c_whitespace (pp);
1752 pp_carret (pp);
1753 pp_c_whitespace (pp);
1754 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1756 else
1757 pp_c_and_expression (pp, e);
1760 /* inclusive-OR-expression:
1761 exclusive-OR-expression
1762 inclusive-OR-expression | exclusive-OR-expression */
1764 static void
1765 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1767 if (TREE_CODE (e) == BIT_IOR_EXPR)
1769 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1770 pp_c_whitespace (pp);
1771 pp_bar (pp);
1772 pp_c_whitespace (pp);
1773 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1775 else
1776 pp_c_exclusive_or_expression (pp, e);
1779 /* logical-AND-expression:
1780 inclusive-OR-expression
1781 logical-AND-expression && inclusive-OR-expression */
1783 static void
1784 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1786 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR
1787 || TREE_CODE (e) == TRUTH_AND_EXPR)
1789 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1790 pp_c_whitespace (pp);
1791 pp_identifier (pp, "&&");
1792 pp_c_whitespace (pp);
1793 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1795 else
1796 pp_c_inclusive_or_expression (pp, e);
1799 /* logical-OR-expression:
1800 logical-AND-expression
1801 logical-OR-expression || logical-AND-expression */
1803 void
1804 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1806 if (TREE_CODE (e) == TRUTH_ORIF_EXPR
1807 || TREE_CODE (e) == TRUTH_OR_EXPR)
1809 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1810 pp_c_whitespace (pp);
1811 pp_identifier (pp, "||");
1812 pp_c_whitespace (pp);
1813 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1815 else
1816 pp_c_logical_and_expression (pp, e);
1819 /* conditional-expression:
1820 logical-OR-expression
1821 logical-OR-expression ? expression : conditional-expression */
1823 static void
1824 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1826 if (TREE_CODE (e) == COND_EXPR)
1828 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1829 pp_c_whitespace (pp);
1830 pp_question (pp);
1831 pp_c_whitespace (pp);
1832 pp_expression (pp, TREE_OPERAND (e, 1));
1833 pp_c_whitespace (pp);
1834 pp_colon (pp);
1835 pp_c_whitespace (pp);
1836 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1838 else
1839 pp_c_logical_or_expression (pp, e);
1843 /* assignment-expression:
1844 conditional-expression
1845 unary-expression assignment-operator assignment-expression
1847 assignment-expression: one of
1848 = *= /= %= += -= >>= <<= &= ^= |= */
1850 static void
1851 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1853 if (TREE_CODE (e) == MODIFY_EXPR
1854 || TREE_CODE (e) == GIMPLE_MODIFY_STMT
1855 || TREE_CODE (e) == INIT_EXPR)
1857 pp_c_unary_expression (pp, GENERIC_TREE_OPERAND (e, 0));
1858 pp_c_whitespace (pp);
1859 pp_equal (pp);
1860 pp_space (pp);
1861 pp_c_expression (pp, GENERIC_TREE_OPERAND (e, 1));
1863 else
1864 pp_c_conditional_expression (pp, e);
1867 /* expression:
1868 assignment-expression
1869 expression , assignment-expression
1871 Implementation note: instead of going through the usual recursion
1872 chain, I take the liberty of dispatching nodes to the appropriate
1873 functions. This makes some redundancy, but it worths it. That also
1874 prevents a possible infinite recursion between pp_c_primary_expression ()
1875 and pp_c_expression (). */
1877 void
1878 pp_c_expression (c_pretty_printer *pp, tree e)
1880 switch (TREE_CODE (e))
1882 case INTEGER_CST:
1883 pp_c_integer_constant (pp, e);
1884 break;
1886 case REAL_CST:
1887 pp_c_floating_constant (pp, e);
1888 break;
1890 case FIXED_CST:
1891 pp_c_fixed_constant (pp, e);
1892 break;
1894 case STRING_CST:
1895 pp_c_string_literal (pp, e);
1896 break;
1898 case IDENTIFIER_NODE:
1899 case FUNCTION_DECL:
1900 case VAR_DECL:
1901 case CONST_DECL:
1902 case PARM_DECL:
1903 case RESULT_DECL:
1904 case FIELD_DECL:
1905 case LABEL_DECL:
1906 case ERROR_MARK:
1907 pp_primary_expression (pp, e);
1908 break;
1910 case POSTINCREMENT_EXPR:
1911 case POSTDECREMENT_EXPR:
1912 case ARRAY_REF:
1913 case CALL_EXPR:
1914 case COMPONENT_REF:
1915 case COMPLEX_CST:
1916 case COMPLEX_EXPR:
1917 case VECTOR_CST:
1918 case ORDERED_EXPR:
1919 case UNORDERED_EXPR:
1920 case LTGT_EXPR:
1921 case UNEQ_EXPR:
1922 case UNLE_EXPR:
1923 case UNLT_EXPR:
1924 case UNGE_EXPR:
1925 case UNGT_EXPR:
1926 case ABS_EXPR:
1927 case CONSTRUCTOR:
1928 case COMPOUND_LITERAL_EXPR:
1929 case VA_ARG_EXPR:
1930 pp_postfix_expression (pp, e);
1931 break;
1933 case CONJ_EXPR:
1934 case ADDR_EXPR:
1935 case INDIRECT_REF:
1936 case NEGATE_EXPR:
1937 case BIT_NOT_EXPR:
1938 case TRUTH_NOT_EXPR:
1939 case PREINCREMENT_EXPR:
1940 case PREDECREMENT_EXPR:
1941 case REALPART_EXPR:
1942 case IMAGPART_EXPR:
1943 pp_c_unary_expression (pp, e);
1944 break;
1946 case FLOAT_EXPR:
1947 case FIX_TRUNC_EXPR:
1948 case CONVERT_EXPR:
1949 case NOP_EXPR:
1950 pp_c_cast_expression (pp, e);
1951 break;
1953 case MULT_EXPR:
1954 case TRUNC_MOD_EXPR:
1955 case TRUNC_DIV_EXPR:
1956 pp_multiplicative_expression (pp, e);
1957 break;
1959 case LSHIFT_EXPR:
1960 case RSHIFT_EXPR:
1961 pp_c_shift_expression (pp, e);
1962 break;
1964 case LT_EXPR:
1965 case GT_EXPR:
1966 case LE_EXPR:
1967 case GE_EXPR:
1968 pp_c_relational_expression (pp, e);
1969 break;
1971 case BIT_AND_EXPR:
1972 pp_c_and_expression (pp, e);
1973 break;
1975 case BIT_XOR_EXPR:
1976 case TRUTH_XOR_EXPR:
1977 pp_c_exclusive_or_expression (pp, e);
1978 break;
1980 case BIT_IOR_EXPR:
1981 pp_c_inclusive_or_expression (pp, e);
1982 break;
1984 case TRUTH_ANDIF_EXPR:
1985 case TRUTH_AND_EXPR:
1986 pp_c_logical_and_expression (pp, e);
1987 break;
1989 case TRUTH_ORIF_EXPR:
1990 case TRUTH_OR_EXPR:
1991 pp_c_logical_or_expression (pp, e);
1992 break;
1994 case EQ_EXPR:
1995 case NE_EXPR:
1996 pp_c_equality_expression (pp, e);
1997 break;
1999 case COND_EXPR:
2000 pp_conditional_expression (pp, e);
2001 break;
2003 case POINTER_PLUS_EXPR:
2004 case PLUS_EXPR:
2005 case MINUS_EXPR:
2006 pp_c_additive_expression (pp, e);
2007 break;
2009 case MODIFY_EXPR:
2010 case GIMPLE_MODIFY_STMT:
2011 case INIT_EXPR:
2012 pp_assignment_expression (pp, e);
2013 break;
2015 case COMPOUND_EXPR:
2016 pp_c_left_paren (pp);
2017 pp_expression (pp, TREE_OPERAND (e, 0));
2018 pp_separate_with (pp, ',');
2019 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
2020 pp_c_right_paren (pp);
2021 break;
2023 case NON_LVALUE_EXPR:
2024 case SAVE_EXPR:
2025 pp_expression (pp, TREE_OPERAND (e, 0));
2026 break;
2028 case TARGET_EXPR:
2029 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
2030 break;
2032 default:
2033 pp_unsupported_tree (pp, e);
2034 break;
2040 /* Statements. */
2042 void
2043 pp_c_statement (c_pretty_printer *pp, tree stmt)
2045 if (stmt == NULL)
2046 return;
2048 if (pp_needs_newline (pp))
2049 pp_newline_and_indent (pp, 0);
2051 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
2055 /* Initialize the PRETTY-PRINTER for handling C codes. */
2057 void
2058 pp_c_pretty_printer_init (c_pretty_printer *pp)
2060 pp->offset_list = 0;
2062 pp->declaration = pp_c_declaration;
2063 pp->declaration_specifiers = pp_c_declaration_specifiers;
2064 pp->declarator = pp_c_declarator;
2065 pp->direct_declarator = pp_c_direct_declarator;
2066 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
2067 pp->abstract_declarator = pp_c_abstract_declarator;
2068 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
2069 pp->ptr_operator = pp_c_pointer;
2070 pp->parameter_list = pp_c_parameter_type_list;
2071 pp->type_id = pp_c_type_id;
2072 pp->simple_type_specifier = pp_c_type_specifier;
2073 pp->function_specifier = pp_c_function_specifier;
2074 pp->storage_class_specifier = pp_c_storage_class_specifier;
2076 pp->statement = pp_c_statement;
2078 pp->constant = pp_c_constant;
2079 pp->id_expression = pp_c_id_expression;
2080 pp->primary_expression = pp_c_primary_expression;
2081 pp->postfix_expression = pp_c_postfix_expression;
2082 pp->unary_expression = pp_c_unary_expression;
2083 pp->initializer = pp_c_initializer;
2084 pp->multiplicative_expression = pp_c_multiplicative_expression;
2085 pp->conditional_expression = pp_c_conditional_expression;
2086 pp->assignment_expression = pp_c_assignment_expression;
2087 pp->expression = pp_c_expression;
2091 /* Print the tree T in full, on file FILE. */
2093 void
2094 print_c_tree (FILE *file, tree t)
2096 static c_pretty_printer pp_rec;
2097 static bool initialized = 0;
2098 c_pretty_printer *pp = &pp_rec;
2100 if (!initialized)
2102 initialized = 1;
2103 pp_construct (pp_base (pp), NULL, 0);
2104 pp_c_pretty_printer_init (pp);
2105 pp_needs_newline (pp) = true;
2107 pp_base (pp)->buffer->stream = file;
2109 pp_statement (pp, t);
2111 pp_newline (pp);
2112 pp_flush (pp);
2115 /* Print the tree T in full, on stderr. */
2117 void
2118 debug_c_tree (tree t)
2120 print_c_tree (stderr, t);
2121 fputc ('\n', stderr);
2124 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2125 up of T's memory address. */
2127 void
2128 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2130 const char *name;
2132 gcc_assert (DECL_P (t));
2134 if (DECL_NAME (t))
2135 name = IDENTIFIER_POINTER (DECL_NAME (t));
2136 else
2138 static char xname[8];
2139 sprintf (xname, "<U%4x>", ((unsigned)((unsigned long)(t) & 0xffff)));
2140 name = xname;
2143 pp_c_identifier (pp, name);