* Make-lang.in (GFORTRAN_TARGET_INSTALL_NAME): Define.
[official-gcc.git] / gcc / c-pretty-print.c
blobbbc19be9fd49788675821ee93d7dce2c17c47d8c
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002, 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, 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 /* ??? This node is now in GENERIC and so shouldn't be here. But
265 we'll fix that later. */
266 case DECL_EXPR:
267 pp_declaration (pp, DECL_EXPR_DECL (t));
268 pp_needs_newline (pp) = true;
269 break;
271 default:
272 pp_unsupported_tree (pp, t);
276 /* type-specifier:
277 void
278 char
279 short
281 long
282 float
283 double
284 signed
285 unsigned
286 _Bool -- C99
287 _Complex -- C99
288 _Imaginary -- C99
289 struct-or-union-specifier
290 enum-specifier
291 typedef-name.
293 GNU extensions.
294 simple-type-specifier:
295 __complex__
296 __vector__ */
298 void
299 pp_c_type_specifier (c_pretty_printer *pp, tree t)
301 const enum tree_code code = TREE_CODE (t);
302 switch (code)
304 case ERROR_MARK:
305 pp_c_identifier (pp, "<type-error>");
306 break;
308 case IDENTIFIER_NODE:
309 pp_c_tree_decl_identifier (pp, t);
310 break;
312 case VOID_TYPE:
313 case BOOLEAN_TYPE:
314 case CHAR_TYPE:
315 case INTEGER_TYPE:
316 case REAL_TYPE:
317 if (TYPE_NAME (t))
319 t = TYPE_NAME (t);
320 pp_c_type_specifier (pp, t);
322 else
324 int prec = TYPE_PRECISION (t);
325 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
326 pp_c_type_specifier (pp, t);
327 if (TYPE_PRECISION (t) != prec)
329 pp_string (pp, ":");
330 pp_decimal_int (pp, prec);
333 break;
335 case TYPE_DECL:
336 if (DECL_NAME (t))
337 pp_id_expression (pp, t);
338 else
339 pp_c_identifier (pp, "<typedef-error>");
340 break;
342 case UNION_TYPE:
343 case RECORD_TYPE:
344 case ENUMERAL_TYPE:
345 if (code == UNION_TYPE)
346 pp_c_identifier (pp, "union");
347 else if (code == RECORD_TYPE)
348 pp_c_identifier (pp, "struct");
349 else if (code == ENUMERAL_TYPE)
350 pp_c_identifier (pp, "enum");
351 else
352 pp_c_identifier (pp, "<tag-error>");
354 if (TYPE_NAME (t))
355 pp_id_expression (pp, TYPE_NAME (t));
356 else
357 pp_c_identifier (pp, "<anonymous>");
358 break;
360 default:
361 pp_unsupported_tree (pp, t);
362 break;
366 /* specifier-qualifier-list:
367 type-specifier specifier-qualifier-list-opt
368 type-qualifier specifier-qualifier-list-opt
371 Implementation note: Because of the non-linearities in array or
372 function declarations, this routine prints not just the
373 specifier-qualifier-list of such entities or types of such entities,
374 but also the 'pointer' production part of their declarators. The
375 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
377 void
378 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
380 const enum tree_code code = TREE_CODE (t);
382 if (TREE_CODE (t) != POINTER_TYPE)
383 pp_c_type_qualifier_list (pp, t);
384 switch (code)
386 case REFERENCE_TYPE:
387 case POINTER_TYPE:
389 /* Get the types-specifier of this type. */
390 tree pointee = strip_pointer_operator (TREE_TYPE (t));
391 pp_c_specifier_qualifier_list (pp, pointee);
392 if (TREE_CODE (pointee) == ARRAY_TYPE
393 || TREE_CODE (pointee) == FUNCTION_TYPE)
395 pp_c_whitespace (pp);
396 pp_c_left_paren (pp);
398 else if (!c_dialect_cxx ())
399 pp_c_whitespace (pp);
400 pp_ptr_operator (pp, t);
402 break;
404 case FUNCTION_TYPE:
405 case ARRAY_TYPE:
406 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
407 break;
409 case VECTOR_TYPE:
410 case COMPLEX_TYPE:
411 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
412 if (code == COMPLEX_TYPE)
413 pp_c_identifier (pp, flag_isoc99 ? "_Complex" : "__complex__");
414 else if (code == VECTOR_TYPE)
415 pp_c_identifier (pp, "__vector__");
416 break;
418 default:
419 pp_simple_type_specifier (pp, t);
420 break;
424 /* parameter-type-list:
425 parameter-list
426 parameter-list , ...
428 parameter-list:
429 parameter-declaration
430 parameter-list , parameter-declaration
432 parameter-declaration:
433 declaration-specifiers declarator
434 declaration-specifiers abstract-declarator(opt) */
436 void
437 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
439 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
440 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
441 pp_c_left_paren (pp);
442 if (parms == void_list_node)
443 pp_c_identifier (pp, "void");
444 else
446 bool first = true;
447 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
449 if (!first)
450 pp_separate_with (pp, ',');
451 first = false;
452 pp_declaration_specifiers
453 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
454 if (want_parm_decl)
455 pp_declarator (pp, parms);
456 else
457 pp_abstract_declarator (pp, TREE_VALUE (parms));
460 pp_c_right_paren (pp);
463 /* abstract-declarator:
464 pointer
465 pointer(opt) direct-abstract-declarator */
467 static void
468 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
470 if (TREE_CODE (t) == POINTER_TYPE)
472 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
473 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
474 pp_c_right_paren (pp);
475 t = TREE_TYPE (t);
478 pp_direct_abstract_declarator (pp, t);
481 /* direct-abstract-declarator:
482 ( abstract-declarator )
483 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
484 direct-abstract-declarator(opt) [ * ]
485 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
487 void
488 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
490 switch (TREE_CODE (t))
492 case POINTER_TYPE:
493 pp_abstract_declarator (pp, t);
494 break;
496 case FUNCTION_TYPE:
497 pp_c_parameter_type_list (pp, t);
498 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
499 break;
501 case ARRAY_TYPE:
502 pp_c_left_bracket (pp);
503 if (TYPE_DOMAIN (t) && TYPE_MAX_VALUE (TYPE_DOMAIN (t)))
504 pp_expression (pp, TYPE_MAX_VALUE (TYPE_DOMAIN (t)));
505 pp_c_right_bracket (pp);
506 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
507 break;
509 case IDENTIFIER_NODE:
510 case VOID_TYPE:
511 case BOOLEAN_TYPE:
512 case INTEGER_TYPE:
513 case REAL_TYPE:
514 case ENUMERAL_TYPE:
515 case RECORD_TYPE:
516 case UNION_TYPE:
517 case VECTOR_TYPE:
518 case COMPLEX_TYPE:
519 case TYPE_DECL:
520 break;
522 default:
523 pp_unsupported_tree (pp, t);
524 break;
528 /* type-name:
529 specifier-qualifier-list abstract-declarator(opt) */
531 void
532 pp_c_type_id (c_pretty_printer *pp, tree t)
534 pp_c_specifier_qualifier_list (pp, t);
535 pp_abstract_declarator (pp, t);
538 /* storage-class-specifier:
539 typedef
540 extern
541 static
542 auto
543 register */
545 void
546 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
548 if (TREE_CODE (t) == TYPE_DECL)
549 pp_c_identifier (pp, "typedef");
550 else if (DECL_P (t))
552 if (DECL_REGISTER (t))
553 pp_c_identifier (pp, "register");
554 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
555 pp_c_identifier (pp, "static");
559 /* function-specifier:
560 inline */
562 void
563 pp_c_function_specifier (c_pretty_printer *pp, tree t)
565 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
566 pp_c_identifier (pp, "inline");
569 /* declaration-specifiers:
570 storage-class-specifier declaration-specifiers(opt)
571 type-specifier declaration-specifiers(opt)
572 type-qualifier declaration-specifiers(opt)
573 function-specifier declaration-specifiers(opt) */
575 void
576 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
578 pp_storage_class_specifier (pp, t);
579 pp_function_specifier (pp, t);
580 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
583 /* direct-declarator
584 identifier
585 ( declarator )
586 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
587 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
588 direct-declarator [ type-qualifier-list static assignment-expression ]
589 direct-declarator [ type-qualifier-list * ]
590 direct-declarator ( parameter-type-list )
591 direct-declarator ( identifier-list(opt) ) */
593 void
594 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
596 switch (TREE_CODE (t))
598 case VAR_DECL:
599 case PARM_DECL:
600 case TYPE_DECL:
601 case FIELD_DECL:
602 case LABEL_DECL:
603 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
604 pp_c_tree_decl_identifier (pp, t);
605 break;
607 case ARRAY_TYPE:
608 case POINTER_TYPE:
609 pp_abstract_declarator (pp, TREE_TYPE (t));
610 break;
612 case FUNCTION_TYPE:
613 pp_parameter_list (pp, t);
614 pp_abstract_declarator (pp, TREE_TYPE (t));
615 break;
617 case FUNCTION_DECL:
618 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
619 pp_c_tree_decl_identifier (pp, t);
620 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
621 pp_abstract_declarator (pp, TREE_TYPE (t));
622 else
624 pp_parameter_list (pp, t);
625 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
627 break;
629 case INTEGER_TYPE:
630 case REAL_TYPE:
631 case ENUMERAL_TYPE:
632 case UNION_TYPE:
633 case RECORD_TYPE:
634 break;
636 default:
637 pp_unsupported_tree (pp, t);
638 break;
643 /* declarator:
644 pointer(opt) direct-declarator */
646 void
647 pp_c_declarator (c_pretty_printer *pp, tree t)
649 switch (TREE_CODE (t))
651 case INTEGER_TYPE:
652 case REAL_TYPE:
653 case ENUMERAL_TYPE:
654 case UNION_TYPE:
655 case RECORD_TYPE:
656 break;
658 case VAR_DECL:
659 case PARM_DECL:
660 case FIELD_DECL:
661 case ARRAY_TYPE:
662 case FUNCTION_TYPE:
663 case FUNCTION_DECL:
664 case TYPE_DECL:
665 pp_direct_declarator (pp, t);
666 break;
669 default:
670 pp_unsupported_tree (pp, t);
671 break;
675 /* declaration:
676 declaration-specifiers init-declarator-list(opt) ; */
678 void
679 pp_c_declaration (c_pretty_printer *pp, tree t)
681 pp_declaration_specifiers (pp, t);
682 pp_c_init_declarator (pp, t);
685 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
687 void
688 pp_c_attributes (c_pretty_printer *pp, tree attributes)
690 if (attributes == NULL_TREE)
691 return;
693 pp_c_identifier (pp, "__attribute__");
694 pp_c_left_paren (pp);
695 pp_c_left_paren (pp);
696 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
698 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
699 if (TREE_VALUE (attributes))
700 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
702 if (TREE_CHAIN (attributes))
703 pp_separate_with (pp, ',');
705 pp_c_right_paren (pp);
706 pp_c_right_paren (pp);
709 /* function-definition:
710 declaration-specifiers declarator compound-statement */
712 void
713 pp_c_function_definition (c_pretty_printer *pp, tree t)
715 pp_declaration_specifiers (pp, t);
716 pp_declarator (pp, t);
717 pp_needs_newline (pp) = true;
718 pp_statement (pp, DECL_SAVED_TREE (t));
719 pp_newline (pp);
720 pp_flush (pp);
724 /* Expressions. */
726 /* Print out a c-char. This is called solely for characters which are
727 in the *target* execution character set. We ought to convert them
728 back to the *host* execution character set before printing, but we
729 have no way to do this at present. A decent compromise is to print
730 all characters as if they were in the host execution character set,
731 and not attempt to recover any named escape characters, but render
732 all unprintables as octal escapes. If the host and target character
733 sets are the same, this produces relatively readable output. If they
734 are not the same, strings may appear as gibberish, but that's okay
735 (in fact, it may well be what the reader wants, e.g. if they are looking
736 to see if conversion to the target character set happened correctly).
738 A special case: we need to prefix \, ", and ' with backslashes. It is
739 correct to do so for the *host*'s \, ", and ', because the rest of the
740 file appears in the host character set. */
742 static void
743 pp_c_char (c_pretty_printer *pp, int c)
745 if (ISPRINT (c))
747 switch (c)
749 case '\\': pp_string (pp, "\\\\"); break;
750 case '\'': pp_string (pp, "\\\'"); break;
751 case '\"': pp_string (pp, "\\\""); break;
752 default: pp_character (pp, c);
755 else
756 pp_scalar (pp, "\\%03o", (unsigned) c);
759 /* Print out a STRING literal. */
761 void
762 pp_c_string_literal (c_pretty_printer *pp, tree s)
764 const char *p = TREE_STRING_POINTER (s);
765 int n = TREE_STRING_LENGTH (s) - 1;
766 int i;
767 pp_doublequote (pp);
768 for (i = 0; i < n; ++i)
769 pp_c_char (pp, p[i]);
770 pp_doublequote (pp);
773 /* Pretty-print an INTEGER literal. */
775 static void
776 pp_c_integer_constant (c_pretty_printer *pp, tree i)
778 tree type = TREE_TYPE (i);
780 if (TREE_INT_CST_HIGH (i) == 0)
781 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
782 else
784 if (tree_int_cst_sgn (i) < 0)
786 pp_character (pp, '-');
787 i = build_int_cst_wide (NULL_TREE,
788 -TREE_INT_CST_LOW (i),
789 ~TREE_INT_CST_HIGH (i)
790 + !TREE_INT_CST_LOW (i));
792 sprintf (pp_buffer (pp)->digit_buffer,
793 HOST_WIDE_INT_PRINT_DOUBLE_HEX,
794 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
795 pp_string (pp, pp_buffer (pp)->digit_buffer);
797 if (TYPE_UNSIGNED (type))
798 pp_character (pp, 'u');
799 if (type == long_integer_type_node || type == long_unsigned_type_node)
800 pp_character (pp, 'l');
801 else if (type == long_long_integer_type_node
802 || type == long_long_unsigned_type_node)
803 pp_string (pp, "ll");
806 /* Print out a CHARACTER literal. */
808 static void
809 pp_c_character_constant (c_pretty_printer *pp, tree c)
811 tree type = TREE_TYPE (c);
812 if (type == wchar_type_node)
813 pp_character (pp, 'L');
814 pp_quote (pp);
815 if (host_integerp (c, TYPE_UNSIGNED (type)))
816 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
817 else
818 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
819 pp_quote (pp);
822 /* Print out a BOOLEAN literal. */
824 static void
825 pp_c_bool_constant (c_pretty_printer *pp, tree b)
827 if (b == boolean_false_node)
829 if (c_dialect_cxx ())
830 pp_c_identifier (pp, "false");
831 else if (flag_isoc99)
832 pp_c_identifier (pp, "_False");
833 else
834 pp_unsupported_tree (pp, b);
836 else if (b == boolean_true_node)
838 if (c_dialect_cxx ())
839 pp_c_identifier (pp, "true");
840 else if (flag_isoc99)
841 pp_c_identifier (pp, "_True");
842 else
843 pp_unsupported_tree (pp, b);
845 else if (TREE_CODE (b) == INTEGER_CST)
846 pp_c_integer_constant (pp, b);
847 else
848 pp_unsupported_tree (pp, b);
851 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
852 false; that means the value was obtained by a cast, in which case
853 print out the type-id part of the cast-expression -- the casted value
854 is then printed by pp_c_integer_literal. */
856 static bool
857 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
859 bool value_is_named = true;
860 tree type = TREE_TYPE (e);
861 tree value;
863 /* Find the name of this constant. */
864 for (value = TYPE_VALUES (type);
865 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
866 value = TREE_CHAIN (value))
869 if (value != NULL_TREE)
870 pp_id_expression (pp, TREE_PURPOSE (value));
871 else
873 /* Value must have been cast. */
874 pp_c_type_cast (pp, type);
875 value_is_named = false;
878 return value_is_named;
881 /* Print out a REAL value as a decimal-floating-constant. */
883 static void
884 pp_c_floating_constant (c_pretty_printer *pp, tree r)
886 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
887 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
888 pp_string (pp, pp_buffer(pp)->digit_buffer);
889 if (TREE_TYPE (r) == float_type_node)
890 pp_character (pp, 'f');
891 else if (TREE_TYPE (r) == long_double_type_node)
892 pp_character (pp, 'l');
895 /* Pretty-print a compound literal expression. GNU extensions include
896 vector constants. */
898 static void
899 pp_c_compound_literal (c_pretty_printer *pp, tree e)
901 tree type = TREE_TYPE (e);
902 pp_c_type_cast (pp, type);
904 switch (TREE_CODE (type))
906 case RECORD_TYPE:
907 case UNION_TYPE:
908 case ARRAY_TYPE:
909 case VECTOR_TYPE:
910 case COMPLEX_TYPE:
911 pp_c_brace_enclosed_initializer_list (pp, e);
912 break;
914 default:
915 pp_unsupported_tree (pp, e);
916 break;
920 /* constant:
921 integer-constant
922 floating-constant
923 enumeration-constant
924 character-constant */
926 void
927 pp_c_constant (c_pretty_printer *pp, tree e)
929 const enum tree_code code = TREE_CODE (e);
931 switch (code)
933 case INTEGER_CST:
935 tree type = TREE_TYPE (e);
936 if (type == boolean_type_node)
937 pp_c_bool_constant (pp, e);
938 else if (type == char_type_node)
939 pp_c_character_constant (pp, e);
940 else if (TREE_CODE (type) == ENUMERAL_TYPE
941 && pp_c_enumeration_constant (pp, e))
943 else
944 pp_c_integer_constant (pp, e);
946 break;
948 case REAL_CST:
949 pp_c_floating_constant (pp, e);
950 break;
952 case STRING_CST:
953 pp_c_string_literal (pp, e);
954 break;
956 default:
957 pp_unsupported_tree (pp, e);
958 break;
962 /* Pretty-print an IDENTIFIER_NODE, preceded by whitespace is necessary. */
964 void
965 pp_c_identifier (c_pretty_printer *pp, const char *id)
967 pp_c_maybe_whitespace (pp);
968 pp_identifier (pp, id);
969 pp_base (pp)->padding = pp_before;
972 /* Pretty-print a C primary-expression.
973 primary-expression:
974 identifier
975 constant
976 string-literal
977 ( expression ) */
979 void
980 pp_c_primary_expression (c_pretty_printer *pp, tree e)
982 switch (TREE_CODE (e))
984 case VAR_DECL:
985 case PARM_DECL:
986 case FIELD_DECL:
987 case CONST_DECL:
988 case FUNCTION_DECL:
989 case LABEL_DECL:
990 pp_c_tree_decl_identifier (pp, e);
991 break;
993 case IDENTIFIER_NODE:
994 pp_c_tree_identifier (pp, e);
995 break;
997 case ERROR_MARK:
998 pp_c_identifier (pp, "<erroneous-expression>");
999 break;
1001 case RESULT_DECL:
1002 pp_c_identifier (pp, "<return-value>");
1003 break;
1005 case INTEGER_CST:
1006 case REAL_CST:
1007 case STRING_CST:
1008 pp_c_constant (pp, e);
1009 break;
1011 case TARGET_EXPR:
1012 pp_c_identifier (pp, "__builtin_memcpy");
1013 pp_c_left_paren (pp);
1014 pp_ampersand (pp);
1015 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1016 pp_separate_with (pp, ',');
1017 pp_ampersand (pp);
1018 pp_initializer (pp, TREE_OPERAND (e, 1));
1019 if (TREE_OPERAND (e, 2))
1021 pp_separate_with (pp, ',');
1022 pp_c_expression (pp, TREE_OPERAND (e, 2));
1024 pp_c_right_paren (pp);
1025 break;
1027 default:
1028 /* FIXME: Make sure we won't get into an infinie loop. */
1029 pp_c_left_paren (pp);
1030 pp_expression (pp, e);
1031 pp_c_right_paren (pp);
1032 break;
1036 /* Print out a C initializer -- also support C compound-literals.
1037 initializer:
1038 assignment-expression:
1039 { initializer-list }
1040 { initializer-list , } */
1042 static void
1043 pp_c_initializer (c_pretty_printer *pp, tree e)
1045 if (TREE_CODE (e) == CONSTRUCTOR)
1046 pp_c_brace_enclosed_initializer_list (pp, e);
1047 else
1048 pp_expression (pp, e);
1051 /* init-declarator:
1052 declarator:
1053 declarator = initializer */
1055 void
1056 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1058 pp_declarator (pp, t);
1059 /* We don't want to output function definitions here. There are handled
1060 elsewhere (and the syntactic form is bogus anyway). */
1061 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1063 tree init = DECL_INITIAL (t);
1064 /* This C++ bit is handled here because it is easier to do so.
1065 In templates, the C++ parser builds a TREE_LIST for a
1066 direct-initialization; the TREE_PURPOSE is the variable to
1067 initialize and the TREE_VALUE is the initializer. */
1068 if (TREE_CODE (init) == TREE_LIST)
1070 pp_c_left_paren (pp);
1071 pp_expression (pp, TREE_VALUE (init));
1072 pp_right_paren (pp);
1074 else
1076 pp_space (pp);
1077 pp_equal (pp);
1078 pp_space (pp);
1079 pp_c_initializer (pp, init);
1084 /* initializer-list:
1085 designation(opt) initializer
1086 initializer-list , designation(opt) initializer
1088 designation:
1089 designator-list =
1091 designator-list:
1092 designator
1093 designator-list designator
1095 designator:
1096 [ constant-expression ]
1097 identifier */
1099 static void
1100 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1102 tree type = TREE_TYPE (e);
1103 const enum tree_code code = TREE_CODE (type);
1105 switch (code)
1107 case RECORD_TYPE:
1108 case UNION_TYPE:
1109 case ARRAY_TYPE:
1111 tree init = TREE_OPERAND (e, 0);
1112 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1114 if (code == RECORD_TYPE || code == UNION_TYPE)
1116 pp_c_dot (pp);
1117 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1119 else
1121 pp_c_left_bracket (pp);
1122 if (TREE_PURPOSE (init))
1123 pp_c_constant (pp, TREE_PURPOSE (init));
1124 pp_c_right_bracket (pp);
1126 pp_c_whitespace (pp);
1127 pp_equal (pp);
1128 pp_c_whitespace (pp);
1129 pp_initializer (pp, TREE_VALUE (init));
1130 if (TREE_CHAIN (init))
1131 pp_separate_with (pp, ',');
1134 return;
1136 case VECTOR_TYPE:
1137 if (TREE_CODE (e) == VECTOR_CST)
1138 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1139 else if (TREE_CODE (e) == CONSTRUCTOR)
1140 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1141 else
1142 break;
1143 return;
1145 case COMPLEX_TYPE:
1146 if (TREE_CODE (e) == CONSTRUCTOR)
1147 pp_c_constructor_elts (pp, CONSTRUCTOR_ELTS (e));
1148 else if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1150 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1151 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1152 pp_separate_with (pp, ',');
1153 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1155 else
1156 break;
1157 return;
1159 default:
1160 break;
1163 pp_unsupported_tree (pp, type);
1166 /* Pretty-print a brace-enclosed initializer-list. */
1168 static void
1169 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1171 pp_c_left_brace (pp);
1172 pp_c_initializer_list (pp, l);
1173 pp_c_right_brace (pp);
1177 /* This is a convenient function, used to bridge gap between C and C++
1178 grammars.
1180 id-expression:
1181 identifier */
1183 void
1184 pp_c_id_expression (c_pretty_printer *pp, tree t)
1186 switch (TREE_CODE (t))
1188 case VAR_DECL:
1189 case PARM_DECL:
1190 case CONST_DECL:
1191 case TYPE_DECL:
1192 case FUNCTION_DECL:
1193 case FIELD_DECL:
1194 case LABEL_DECL:
1195 pp_c_tree_decl_identifier (pp, t);
1196 break;
1198 case IDENTIFIER_NODE:
1199 pp_c_tree_identifier (pp, t);
1200 break;
1202 default:
1203 pp_unsupported_tree (pp, t);
1204 break;
1208 /* postfix-expression:
1209 primary-expression
1210 postfix-expression [ expression ]
1211 postfix-expression ( argument-expression-list(opt) )
1212 postfix-expression . identifier
1213 postfix-expression -> identifier
1214 postfix-expression ++
1215 postfix-expression --
1216 ( type-name ) { initializer-list }
1217 ( type-name ) { initializer-list , } */
1219 void
1220 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1222 enum tree_code code = TREE_CODE (e);
1223 switch (code)
1225 case POSTINCREMENT_EXPR:
1226 case POSTDECREMENT_EXPR:
1227 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1228 pp_identifier (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1229 break;
1231 case ARRAY_REF:
1232 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1233 pp_c_left_bracket (pp);
1234 pp_expression (pp, TREE_OPERAND (e, 1));
1235 pp_c_right_bracket (pp);
1236 break;
1238 case CALL_EXPR:
1239 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1240 pp_c_call_argument_list (pp, TREE_OPERAND (e, 1));
1241 break;
1243 case UNORDERED_EXPR:
1244 pp_c_identifier (pp, flag_isoc99
1245 ? "isunordered"
1246 : "__builtin_isunordered");
1247 goto two_args_fun;
1249 case ORDERED_EXPR:
1250 pp_c_identifier (pp, flag_isoc99
1251 ? "!isunordered"
1252 : "!__builtin_isunordered");
1253 goto two_args_fun;
1255 case UNLT_EXPR:
1256 pp_c_identifier (pp, flag_isoc99
1257 ? "!isgreaterequal"
1258 : "!__builtin_isgreaterequal");
1259 goto two_args_fun;
1261 case UNLE_EXPR:
1262 pp_c_identifier (pp, flag_isoc99
1263 ? "!isgreater"
1264 : "!__builtin_isgreater");
1265 goto two_args_fun;
1267 case UNGT_EXPR:
1268 pp_c_identifier (pp, flag_isoc99
1269 ? "!islessequal"
1270 : "!__builtin_islessequal");
1271 goto two_args_fun;
1273 case UNGE_EXPR:
1274 pp_c_identifier (pp, flag_isoc99
1275 ? "!isless"
1276 : "!__builtin_isless");
1277 goto two_args_fun;
1279 case UNEQ_EXPR:
1280 pp_c_identifier (pp, flag_isoc99
1281 ? "!islessgreater"
1282 : "!__builtin_islessgreater");
1283 goto two_args_fun;
1285 case LTGT_EXPR:
1286 pp_c_identifier (pp, flag_isoc99
1287 ? "islessgreater"
1288 : "__builtin_islessgreater");
1289 goto two_args_fun;
1291 two_args_fun:
1292 pp_c_left_paren (pp);
1293 pp_expression (pp, TREE_OPERAND (e, 0));
1294 pp_separate_with (pp, ',');
1295 pp_expression (pp, TREE_OPERAND (e, 1));
1296 pp_c_right_paren (pp);
1297 break;
1299 case ABS_EXPR:
1300 pp_c_identifier (pp, "__builtin_abs");
1301 pp_c_left_paren (pp);
1302 pp_expression (pp, TREE_OPERAND (e, 0));
1303 pp_c_right_paren (pp);
1304 break;
1306 case COMPONENT_REF:
1308 tree object = TREE_OPERAND (e, 0);
1309 if (TREE_CODE (object) == INDIRECT_REF)
1311 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1312 pp_c_arrow (pp);
1314 else
1316 pp_postfix_expression (pp, object);
1317 pp_c_dot (pp);
1319 pp_expression (pp, TREE_OPERAND (e, 1));
1321 break;
1323 case COMPLEX_CST:
1324 case VECTOR_CST:
1325 case COMPLEX_EXPR:
1326 pp_c_compound_literal (pp, e);
1327 break;
1329 case COMPOUND_LITERAL_EXPR:
1330 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1331 /* Fall through. */
1332 case CONSTRUCTOR:
1333 pp_initializer (pp, e);
1334 break;
1336 case VA_ARG_EXPR:
1337 pp_c_identifier (pp, "__builtin_va_arg");
1338 pp_c_left_paren (pp);
1339 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1340 pp_separate_with (pp, ',');
1341 pp_type_id (pp, TREE_TYPE (e));
1342 pp_c_right_paren (pp);
1343 break;
1345 case ADDR_EXPR:
1346 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1348 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1349 break;
1351 /* else fall through. */
1353 default:
1354 pp_primary_expression (pp, e);
1355 break;
1359 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1361 void
1362 pp_c_expression_list (c_pretty_printer *pp, tree e)
1364 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1366 pp_expression (pp, TREE_VALUE (e));
1367 if (TREE_CHAIN (e))
1368 pp_separate_with (pp, ',');
1372 /* Print out V, which contains the elements of a constructor. */
1374 void
1375 pp_c_constructor_elts (c_pretty_printer *pp, VEC(constructor_elt,gc) *v)
1377 unsigned HOST_WIDE_INT ix;
1378 tree value;
1380 FOR_EACH_CONSTRUCTOR_VALUE (v, ix, value)
1382 pp_expression (pp, value);
1383 if (ix != VEC_length (constructor_elt, v) - 1)
1384 pp_separate_with (pp, ',');
1388 /* Print out an expression-list in parens, as in a function call. */
1390 void
1391 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1393 pp_c_left_paren (pp);
1394 if (t && TREE_CODE (t) == TREE_LIST)
1395 pp_c_expression_list (pp, t);
1396 pp_c_right_paren (pp);
1399 /* unary-expression:
1400 postfix-expression
1401 ++ cast-expression
1402 -- cast-expression
1403 unary-operator cast-expression
1404 sizeof unary-expression
1405 sizeof ( type-id )
1407 unary-operator: one of
1408 * & + - ! ~
1410 GNU extensions.
1411 unary-expression:
1412 __alignof__ unary-expression
1413 __alignof__ ( type-id )
1414 __real__ unary-expression
1415 __imag__ unary-expression */
1417 void
1418 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1420 enum tree_code code = TREE_CODE (e);
1421 switch (code)
1423 case PREINCREMENT_EXPR:
1424 case PREDECREMENT_EXPR:
1425 pp_identifier (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1426 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1427 break;
1429 case ADDR_EXPR:
1430 case INDIRECT_REF:
1431 case NEGATE_EXPR:
1432 case BIT_NOT_EXPR:
1433 case TRUTH_NOT_EXPR:
1434 case CONJ_EXPR:
1435 /* String literal are used by address. */
1436 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1437 pp_ampersand (pp);
1438 else if (code == INDIRECT_REF)
1439 pp_c_star (pp);
1440 else if (code == NEGATE_EXPR)
1441 pp_minus (pp);
1442 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1443 pp_complement (pp);
1444 else if (code == TRUTH_NOT_EXPR)
1445 pp_exclamation (pp);
1446 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1447 break;
1449 case REALPART_EXPR:
1450 case IMAGPART_EXPR:
1451 pp_c_identifier (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1452 pp_c_whitespace (pp);
1453 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1454 break;
1456 default:
1457 pp_postfix_expression (pp, e);
1458 break;
1462 /* cast-expression:
1463 unary-expression
1464 ( type-name ) cast-expression */
1466 void
1467 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1469 switch (TREE_CODE (e))
1471 case FLOAT_EXPR:
1472 case FIX_TRUNC_EXPR:
1473 case CONVERT_EXPR:
1474 pp_c_type_cast (pp, TREE_TYPE (e));
1475 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1476 break;
1478 default:
1479 pp_unary_expression (pp, e);
1483 /* multiplicative-expression:
1484 cast-expression
1485 multiplicative-expression * cast-expression
1486 multiplicative-expression / cast-expression
1487 multiplicative-expression % cast-expression */
1489 static void
1490 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1492 enum tree_code code = TREE_CODE (e);
1493 switch (code)
1495 case MULT_EXPR:
1496 case TRUNC_DIV_EXPR:
1497 case TRUNC_MOD_EXPR:
1498 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1499 pp_c_whitespace (pp);
1500 if (code == MULT_EXPR)
1501 pp_c_star (pp);
1502 else if (code == TRUNC_DIV_EXPR)
1503 pp_slash (pp);
1504 else
1505 pp_modulo (pp);
1506 pp_c_whitespace (pp);
1507 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1508 break;
1510 default:
1511 pp_c_cast_expression (pp, e);
1512 break;
1516 /* additive-expression:
1517 multiplicative-expression
1518 additive-expression + multiplicative-expression
1519 additive-expression - multiplicative-expression */
1521 static void
1522 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1524 enum tree_code code = TREE_CODE (e);
1525 switch (code)
1527 case PLUS_EXPR:
1528 case MINUS_EXPR:
1529 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1530 pp_c_whitespace (pp);
1531 if (code == PLUS_EXPR)
1532 pp_plus (pp);
1533 else
1534 pp_minus (pp);
1535 pp_c_whitespace (pp);
1536 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1537 break;
1539 default:
1540 pp_multiplicative_expression (pp, e);
1541 break;
1545 /* additive-expression:
1546 additive-expression
1547 shift-expression << additive-expression
1548 shift-expression >> additive-expression */
1550 static void
1551 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1553 enum tree_code code = TREE_CODE (e);
1554 switch (code)
1556 case LSHIFT_EXPR:
1557 case RSHIFT_EXPR:
1558 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1559 pp_c_whitespace (pp);
1560 pp_identifier (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1561 pp_c_whitespace (pp);
1562 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1563 break;
1565 default:
1566 pp_c_additive_expression (pp, e);
1570 /* relational-expression:
1571 shift-expression
1572 relational-expression < shift-expression
1573 relational-expression > shift-expression
1574 relational-expression <= shift-expression
1575 relational-expression >= shift-expression */
1577 static void
1578 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1580 enum tree_code code = TREE_CODE (e);
1581 switch (code)
1583 case LT_EXPR:
1584 case GT_EXPR:
1585 case LE_EXPR:
1586 case GE_EXPR:
1587 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1588 pp_c_whitespace (pp);
1589 if (code == LT_EXPR)
1590 pp_less (pp);
1591 else if (code == GT_EXPR)
1592 pp_greater (pp);
1593 else if (code == LE_EXPR)
1594 pp_identifier (pp, "<=");
1595 else if (code == GE_EXPR)
1596 pp_identifier (pp, ">=");
1597 pp_c_whitespace (pp);
1598 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1599 break;
1601 default:
1602 pp_c_shift_expression (pp, e);
1603 break;
1607 /* equality-expression:
1608 relational-expression
1609 equality-expression == relational-expression
1610 equality-equality != relational-expression */
1612 static void
1613 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1615 enum tree_code code = TREE_CODE (e);
1616 switch (code)
1618 case EQ_EXPR:
1619 case NE_EXPR:
1620 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1621 pp_c_whitespace (pp);
1622 pp_identifier (pp, code == EQ_EXPR ? "==" : "!=");
1623 pp_c_whitespace (pp);
1624 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1625 break;
1627 default:
1628 pp_c_relational_expression (pp, e);
1629 break;
1633 /* AND-expression:
1634 equality-expression
1635 AND-expression & equality-equality */
1637 static void
1638 pp_c_and_expression (c_pretty_printer *pp, tree e)
1640 if (TREE_CODE (e) == BIT_AND_EXPR)
1642 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1643 pp_c_whitespace (pp);
1644 pp_ampersand (pp);
1645 pp_c_whitespace (pp);
1646 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1648 else
1649 pp_c_equality_expression (pp, e);
1652 /* exclusive-OR-expression:
1653 AND-expression
1654 exclusive-OR-expression ^ AND-expression */
1656 static void
1657 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1659 if (TREE_CODE (e) == BIT_XOR_EXPR)
1661 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1662 pp_c_maybe_whitespace (pp);
1663 pp_carret (pp);
1664 pp_c_whitespace (pp);
1665 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1667 else
1668 pp_c_and_expression (pp, e);
1671 /* inclusive-OR-expression:
1672 exclusive-OR-expression
1673 inclusive-OR-expression | exclusive-OR-expression */
1675 static void
1676 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1678 if (TREE_CODE (e) == BIT_IOR_EXPR)
1680 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1681 pp_c_whitespace (pp);
1682 pp_bar (pp);
1683 pp_c_whitespace (pp);
1684 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1686 else
1687 pp_c_exclusive_or_expression (pp, e);
1690 /* logical-AND-expression:
1691 inclusive-OR-expression
1692 logical-AND-expression && inclusive-OR-expression */
1694 static void
1695 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1697 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
1699 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1700 pp_c_whitespace (pp);
1701 pp_identifier (pp, "&&");
1702 pp_c_whitespace (pp);
1703 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1705 else
1706 pp_c_inclusive_or_expression (pp, e);
1709 /* logical-OR-expression:
1710 logical-AND-expression
1711 logical-OR-expression || logical-AND-expression */
1713 void
1714 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1716 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1718 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1719 pp_c_whitespace (pp);
1720 pp_identifier (pp, "||");
1721 pp_c_whitespace (pp);
1722 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1724 else
1725 pp_c_logical_and_expression (pp, e);
1728 /* conditional-expression:
1729 logical-OR-expression
1730 logical-OR-expression ? expression : conditional-expression */
1732 static void
1733 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1735 if (TREE_CODE (e) == COND_EXPR)
1737 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1738 pp_c_whitespace (pp);
1739 pp_question (pp);
1740 pp_c_whitespace (pp);
1741 pp_expression (pp, TREE_OPERAND (e, 1));
1742 pp_c_whitespace (pp);
1743 pp_colon (pp);
1744 pp_c_whitespace (pp);
1745 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1747 else
1748 pp_c_logical_or_expression (pp, e);
1752 /* assignment-expression:
1753 conditional-expression
1754 unary-expression assignment-operator assignment-expression
1756 assignment-expression: one of
1757 = *= /= %= += -= >>= <<= &= ^= |= */
1759 static void
1760 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1762 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1764 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1765 pp_c_whitespace (pp);
1766 pp_equal (pp);
1767 pp_space (pp);
1768 pp_c_expression (pp, TREE_OPERAND (e, 1));
1770 else
1771 pp_c_conditional_expression (pp, e);
1774 /* expression:
1775 assignment-expression
1776 expression , assignment-expression
1778 Implementation note: instead of going through the usual recursion
1779 chain, I take the liberty of dispatching nodes to the appropriate
1780 functions. This makes some redundancy, but it worths it. That also
1781 prevents a possible infinite recursion between pp_c_primary_expression ()
1782 and pp_c_expression (). */
1784 void
1785 pp_c_expression (c_pretty_printer *pp, tree e)
1787 switch (TREE_CODE (e))
1789 case INTEGER_CST:
1790 pp_c_integer_constant (pp, e);
1791 break;
1793 case REAL_CST:
1794 pp_c_floating_constant (pp, e);
1795 break;
1797 case STRING_CST:
1798 pp_c_string_literal (pp, e);
1799 break;
1801 case IDENTIFIER_NODE:
1802 case FUNCTION_DECL:
1803 case VAR_DECL:
1804 case CONST_DECL:
1805 case PARM_DECL:
1806 case RESULT_DECL:
1807 case FIELD_DECL:
1808 case LABEL_DECL:
1809 case ERROR_MARK:
1810 pp_primary_expression (pp, e);
1811 break;
1813 case POSTINCREMENT_EXPR:
1814 case POSTDECREMENT_EXPR:
1815 case ARRAY_REF:
1816 case CALL_EXPR:
1817 case COMPONENT_REF:
1818 case COMPLEX_CST:
1819 case COMPLEX_EXPR:
1820 case VECTOR_CST:
1821 case ORDERED_EXPR:
1822 case UNORDERED_EXPR:
1823 case LTGT_EXPR:
1824 case UNEQ_EXPR:
1825 case UNLE_EXPR:
1826 case UNLT_EXPR:
1827 case UNGE_EXPR:
1828 case UNGT_EXPR:
1829 case ABS_EXPR:
1830 case CONSTRUCTOR:
1831 case COMPOUND_LITERAL_EXPR:
1832 case VA_ARG_EXPR:
1833 pp_postfix_expression (pp, e);
1834 break;
1836 case CONJ_EXPR:
1837 case ADDR_EXPR:
1838 case INDIRECT_REF:
1839 case NEGATE_EXPR:
1840 case BIT_NOT_EXPR:
1841 case TRUTH_NOT_EXPR:
1842 case PREINCREMENT_EXPR:
1843 case PREDECREMENT_EXPR:
1844 case REALPART_EXPR:
1845 case IMAGPART_EXPR:
1846 pp_c_unary_expression (pp, e);
1847 break;
1849 case FLOAT_EXPR:
1850 case FIX_TRUNC_EXPR:
1851 case CONVERT_EXPR:
1852 pp_c_cast_expression (pp, e);
1853 break;
1855 case MULT_EXPR:
1856 case TRUNC_MOD_EXPR:
1857 case TRUNC_DIV_EXPR:
1858 pp_multiplicative_expression (pp, e);
1859 break;
1861 case LSHIFT_EXPR:
1862 case RSHIFT_EXPR:
1863 pp_c_shift_expression (pp, e);
1864 break;
1866 case LT_EXPR:
1867 case GT_EXPR:
1868 case LE_EXPR:
1869 case GE_EXPR:
1870 pp_c_relational_expression (pp, e);
1871 break;
1873 case BIT_AND_EXPR:
1874 pp_c_and_expression (pp, e);
1875 break;
1877 case BIT_XOR_EXPR:
1878 pp_c_exclusive_or_expression (pp, e);
1879 break;
1881 case BIT_IOR_EXPR:
1882 pp_c_inclusive_or_expression (pp, e);
1883 break;
1885 case TRUTH_ANDIF_EXPR:
1886 pp_c_logical_and_expression (pp, e);
1887 break;
1889 case TRUTH_ORIF_EXPR:
1890 pp_c_logical_or_expression (pp, e);
1891 break;
1893 case EQ_EXPR:
1894 case NE_EXPR:
1895 pp_c_equality_expression (pp, e);
1896 break;
1898 case COND_EXPR:
1899 pp_conditional_expression (pp, e);
1900 break;
1902 case PLUS_EXPR:
1903 case MINUS_EXPR:
1904 pp_c_additive_expression (pp, e);
1905 break;
1907 case MODIFY_EXPR:
1908 case INIT_EXPR:
1909 pp_assignment_expression (pp, e);
1910 break;
1912 case COMPOUND_EXPR:
1913 pp_c_left_paren (pp);
1914 pp_expression (pp, TREE_OPERAND (e, 0));
1915 pp_separate_with (pp, ',');
1916 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
1917 pp_c_right_paren (pp);
1918 break;
1920 case NOP_EXPR:
1921 case NON_LVALUE_EXPR:
1922 case SAVE_EXPR:
1923 pp_expression (pp, TREE_OPERAND (e, 0));
1924 break;
1926 case TARGET_EXPR:
1927 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
1928 break;
1930 default:
1931 pp_unsupported_tree (pp, e);
1932 break;
1938 /* Statements. */
1940 void
1941 pp_c_statement (c_pretty_printer *pp, tree stmt)
1943 if (stmt == NULL)
1944 return;
1946 if (pp_needs_newline (pp))
1947 pp_newline_and_indent (pp, 0);
1949 dump_generic_node (pp_base (pp), stmt, pp_indentation (pp), 0, true);
1953 /* Initialize the PRETTY-PRINTER for handling C codes. */
1955 void
1956 pp_c_pretty_printer_init (c_pretty_printer *pp)
1958 pp->offset_list = 0;
1960 pp->declaration = pp_c_declaration;
1961 pp->declaration_specifiers = pp_c_declaration_specifiers;
1962 pp->declarator = pp_c_declarator;
1963 pp->direct_declarator = pp_c_direct_declarator;
1964 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
1965 pp->abstract_declarator = pp_c_abstract_declarator;
1966 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
1967 pp->ptr_operator = pp_c_pointer;
1968 pp->parameter_list = pp_c_parameter_type_list;
1969 pp->type_id = pp_c_type_id;
1970 pp->simple_type_specifier = pp_c_type_specifier;
1971 pp->function_specifier = pp_c_function_specifier;
1972 pp->storage_class_specifier = pp_c_storage_class_specifier;
1974 pp->statement = pp_c_statement;
1976 pp->id_expression = pp_c_id_expression;
1977 pp->primary_expression = pp_c_primary_expression;
1978 pp->postfix_expression = pp_c_postfix_expression;
1979 pp->unary_expression = pp_c_unary_expression;
1980 pp->initializer = pp_c_initializer;
1981 pp->multiplicative_expression = pp_c_multiplicative_expression;
1982 pp->conditional_expression = pp_c_conditional_expression;
1983 pp->assignment_expression = pp_c_assignment_expression;
1984 pp->expression = pp_c_expression;
1988 /* Print the tree T in full, on file FILE. */
1990 void
1991 print_c_tree (FILE *file, tree t)
1993 static c_pretty_printer pp_rec;
1994 static bool initialized = 0;
1995 c_pretty_printer *pp = &pp_rec;
1997 if (!initialized)
1999 initialized = 1;
2000 pp_construct (pp_base (pp), NULL, 0);
2001 pp_c_pretty_printer_init (pp);
2002 pp_needs_newline (pp) = true;
2004 pp_base (pp)->buffer->stream = file;
2006 pp_statement (pp, t);
2008 pp_newline (pp);
2009 pp_flush (pp);
2012 /* Print the tree T in full, on stderr. */
2014 void
2015 debug_c_tree (tree t)
2017 print_c_tree (stderr, t);
2018 fputc ('\n', stderr);
2021 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2022 up of T's memory address. */
2024 void
2025 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2027 const char *name;
2029 gcc_assert (DECL_P (t));
2031 if (DECL_NAME (t))
2032 name = IDENTIFIER_POINTER (DECL_NAME (t));
2033 else
2035 static char xname[8];
2036 sprintf (xname, "<U%4x>", ((unsigned)((unsigned long)(t) & 0xffff)));
2037 name = xname;
2040 pp_c_identifier (pp, name);