a2ef2048874981c08b4b2bb369707ded991ad6eb
[gcc.git] / gcc / c-pretty-print.c
1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4
5 This file is part of GCC.
6
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.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
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"
31
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. */
38
39
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)
45
46 /* literal */
47 static void pp_c_char (c_pretty_printer *, int);
48
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);
52
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);
64
65 /* declarations. */
66
67 \f
68 /* Helper functions. */
69
70 void
71 pp_c_whitespace (c_pretty_printer *pp)
72 {
73 pp_space (pp);
74 pp_base (pp)->padding = pp_none;
75 }
76
77 void
78 pp_c_left_paren (c_pretty_printer *pp)
79 {
80 pp_left_paren (pp);
81 pp_base (pp)->padding = pp_none;
82 }
83
84 void
85 pp_c_right_paren (c_pretty_printer *pp)
86 {
87 pp_right_paren (pp);
88 pp_base (pp)->padding = pp_none;
89 }
90
91 void
92 pp_c_left_brace (c_pretty_printer *pp)
93 {
94 pp_left_brace (pp);
95 pp_base (pp)->padding = pp_none;
96 }
97
98 void
99 pp_c_right_brace (c_pretty_printer *pp)
100 {
101 pp_right_brace (pp);
102 pp_base (pp)->padding = pp_none;
103 }
104
105 void
106 pp_c_left_bracket (c_pretty_printer *pp)
107 {
108 pp_left_bracket (pp);
109 pp_base (pp)->padding = pp_none;
110 }
111
112 void
113 pp_c_right_bracket (c_pretty_printer *pp)
114 {
115 pp_right_bracket (pp);
116 pp_base (pp)->padding = pp_none;
117 }
118
119 void
120 pp_c_dot (c_pretty_printer *pp)
121 {
122 pp_dot (pp);
123 pp_base (pp)->padding = pp_none;
124 }
125
126 void
127 pp_c_ampersand (c_pretty_printer *pp)
128 {
129 pp_ampersand (pp);
130 pp_base (pp)->padding = pp_none;
131 }
132
133 void
134 pp_c_star (c_pretty_printer *pp)
135 {
136 pp_star (pp);
137 pp_base (pp)->padding = pp_none;
138 }
139
140 void
141 pp_c_arrow (c_pretty_printer *pp)
142 {
143 pp_arrow (pp);
144 pp_base (pp)->padding = pp_none;
145 }
146
147 void
148 pp_c_semicolon (c_pretty_printer *pp)
149 {
150 pp_semicolon (pp);
151 pp_base (pp)->padding = pp_none;
152 }
153
154 void
155 pp_c_complement (c_pretty_printer *pp)
156 {
157 pp_complement (pp);
158 pp_base (pp)->padding = pp_none;
159 }
160
161 void
162 pp_c_exclamation (c_pretty_printer *pp)
163 {
164 pp_exclamation (pp);
165 pp_base (pp)->padding = pp_none;
166 }
167
168 /* Print out the external representation of CV-QUALIFIER. */
169
170 static void
171 pp_c_cv_qualifier (c_pretty_printer *pp, const char *cv)
172 {
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);
180 }
181
182 /* Pretty-print T using the type-cast notation '( type-name )'. */
183
184 static void
185 pp_c_type_cast (c_pretty_printer *pp, tree t)
186 {
187 pp_c_left_paren (pp);
188 pp_type_id (pp, t);
189 pp_c_right_paren (pp);
190 }
191
192 /* We're about to pretty-print a pointer type as indicated by T.
193 Output a whitespace, if needed, preparing for subsequent output. */
194
195 void
196 pp_c_space_for_pointer_operator (c_pretty_printer *pp, tree t)
197 {
198 if (POINTER_TYPE_P (t))
199 {
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);
204 }
205 }
206
207 \f
208 /* Declarations. */
209
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.
213
214 type-qualifier-list:
215 type-qualifier
216 type-qualifier-list type-qualifier
217
218 type-qualifier:
219 const
220 restrict -- C99
221 __restrict__ -- GNU C
222 volatile */
223
224 void
225 pp_c_type_qualifier_list (c_pretty_printer *pp, tree t)
226 {
227 int qualifiers;
228
229 if (!TYPE_P (t))
230 t = TREE_TYPE (t);
231
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__");
239 }
240
241 /* pointer:
242 * type-qualifier-list(opt)
243 * type-qualifier-list(opt) pointer */
244
245 static void
246 pp_c_pointer (c_pretty_printer *pp, tree t)
247 {
248 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
249 t = TREE_TYPE (t);
250 switch (TREE_CODE (t))
251 {
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;
263
264 default:
265 pp_unsupported_tree (pp, t);
266 }
267 }
268
269 /* type-specifier:
270 void
271 char
272 short
273 int
274 long
275 float
276 double
277 signed
278 unsigned
279 _Bool -- C99
280 _Complex -- C99
281 _Imaginary -- C99
282 struct-or-union-specifier
283 enum-specifier
284 typedef-name.
285
286 GNU extensions.
287 simple-type-specifier:
288 __complex__
289 __vector__ */
290
291 void
292 pp_c_type_specifier (c_pretty_printer *pp, tree t)
293 {
294 const enum tree_code code = TREE_CODE (t);
295 switch (code)
296 {
297 case ERROR_MARK:
298 pp_c_identifier (pp, "<type-error>");
299 break;
300
301 case IDENTIFIER_NODE:
302 pp_c_tree_decl_identifier (pp, t);
303 break;
304
305 case VOID_TYPE:
306 case BOOLEAN_TYPE:
307 case CHAR_TYPE:
308 case INTEGER_TYPE:
309 case REAL_TYPE:
310 if (TYPE_NAME (t))
311 t = TYPE_NAME (t);
312 else
313 t = c_common_type_for_mode (TYPE_MODE (t), TYPE_UNSIGNED (t));
314 pp_c_type_specifier (pp, t);
315 break;
316
317 case TYPE_DECL:
318 if (DECL_NAME (t))
319 pp_id_expression (pp, t);
320 else
321 pp_c_identifier (pp, "<typedef-error>");
322 break;
323
324 case UNION_TYPE:
325 case RECORD_TYPE:
326 case ENUMERAL_TYPE:
327 if (code == UNION_TYPE)
328 pp_c_identifier (pp, "union");
329 else if (code == RECORD_TYPE)
330 pp_c_identifier (pp, "struct");
331 else if (code == ENUMERAL_TYPE)
332 pp_c_identifier (pp, "enum");
333 else
334 pp_c_identifier (pp, "<tag-error>");
335
336 if (TYPE_NAME (t))
337 pp_id_expression (pp, TYPE_NAME (t));
338 else
339 pp_c_identifier (pp, "<anonymous>");
340 break;
341
342 default:
343 pp_unsupported_tree (pp, t);
344 break;
345 }
346 }
347
348 /* specifier-qualifier-list:
349 type-specifier specifier-qualifier-list-opt
350 type-qualifier specifier-qualifier-list-opt
351
352
353 Implementation note: Because of the non-linearities in array or
354 function declarations, this routine prints not just the
355 specifier-qualifier-list of such entities or types of such entities,
356 but also the 'pointer' production part of their declarators. The
357 remaining part is done by pp_declarator or pp_c_abstract_declarator. */
358
359 void
360 pp_c_specifier_qualifier_list (c_pretty_printer *pp, tree t)
361 {
362 const enum tree_code code = TREE_CODE (t);
363
364 if (TREE_CODE (t) != POINTER_TYPE)
365 pp_c_type_qualifier_list (pp, t);
366 switch (code)
367 {
368 case REFERENCE_TYPE:
369 case POINTER_TYPE:
370 {
371 /* Get the types-specifier of this type. */
372 tree pointee = strip_pointer_operator (TREE_TYPE (t));
373 pp_c_specifier_qualifier_list (pp, pointee);
374 if (TREE_CODE (pointee) == ARRAY_TYPE
375 || TREE_CODE (pointee) == FUNCTION_TYPE)
376 {
377 pp_c_whitespace (pp);
378 pp_c_left_paren (pp);
379 }
380 pp_ptr_operator (pp, t);
381 }
382 break;
383
384 case FUNCTION_TYPE:
385 case ARRAY_TYPE:
386 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
387 break;
388
389 case VECTOR_TYPE:
390 case COMPLEX_TYPE:
391 pp_c_specifier_qualifier_list (pp, TREE_TYPE (t));
392 if (code == COMPLEX_TYPE)
393 pp_c_identifier (pp, flag_isoc99 ? "_Complex" : "__complex__");
394 else if (code == VECTOR_TYPE)
395 pp_c_identifier (pp, "__vector__");
396 break;
397
398 default:
399 pp_simple_type_specifier (pp, t);
400 break;
401 }
402 }
403
404 /* parameter-type-list:
405 parameter-list
406 parameter-list , ...
407
408 parameter-list:
409 parameter-declaration
410 parameter-list , parameter-declaration
411
412 parameter-declaration:
413 declaration-specifiers declarator
414 declaration-specifiers abstract-declarator(opt) */
415
416 void
417 pp_c_parameter_type_list (c_pretty_printer *pp, tree t)
418 {
419 bool want_parm_decl = DECL_P (t) && !(pp->flags & pp_c_flag_abstract);
420 tree parms = want_parm_decl ? DECL_ARGUMENTS (t) : TYPE_ARG_TYPES (t);
421 pp_c_left_paren (pp);
422 if (parms == void_list_node)
423 pp_c_identifier (pp, "void");
424 else
425 {
426 bool first = true;
427 for ( ; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
428 {
429 if (!first)
430 pp_separate_with (pp, ',');
431 first = false;
432 pp_declaration_specifiers
433 (pp, want_parm_decl ? parms : TREE_VALUE (parms));
434 if (want_parm_decl)
435 pp_declarator (pp, parms);
436 else
437 pp_abstract_declarator (pp, TREE_VALUE (parms));
438 }
439 }
440 pp_c_right_paren (pp);
441 }
442
443 /* abstract-declarator:
444 pointer
445 pointer(opt) direct-abstract-declarator */
446
447 static void
448 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
449 {
450 if (TREE_CODE (t) == POINTER_TYPE)
451 {
452 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
453 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
454 pp_c_right_paren (pp);
455 t = TREE_TYPE (t);
456 }
457
458 pp_direct_abstract_declarator (pp, t);
459 }
460
461 /* direct-abstract-declarator:
462 ( abstract-declarator )
463 direct-abstract-declarator(opt) [ assignment-expression(opt) ]
464 direct-abstract-declarator(opt) [ * ]
465 direct-abstract-declarator(opt) ( parameter-type-list(opt) ) */
466
467 void
468 pp_c_direct_abstract_declarator (c_pretty_printer *pp, tree t)
469 {
470 switch (TREE_CODE (t))
471 {
472 case POINTER_TYPE:
473 pp_abstract_declarator (pp, t);
474 break;
475
476 case FUNCTION_TYPE:
477 pp_c_parameter_type_list (pp, t);
478 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
479 break;
480
481 case ARRAY_TYPE:
482 pp_c_left_bracket (pp);
483 if (TYPE_DOMAIN (t))
484 pp_expression (pp, TYPE_MAX_VALUE (TYPE_DOMAIN (t)));
485 pp_c_right_bracket (pp);
486 pp_direct_abstract_declarator (pp, TREE_TYPE (t));
487 break;
488
489 case IDENTIFIER_NODE:
490 case VOID_TYPE:
491 case BOOLEAN_TYPE:
492 case INTEGER_TYPE:
493 case REAL_TYPE:
494 case ENUMERAL_TYPE:
495 case RECORD_TYPE:
496 case UNION_TYPE:
497 case VECTOR_TYPE:
498 case COMPLEX_TYPE:
499 case TYPE_DECL:
500 break;
501
502 default:
503 pp_unsupported_tree (pp, t);
504 break;
505 }
506 }
507
508 /* type-name:
509 specifier-qualifier-list abstract-declarator(opt) */
510
511 void
512 pp_c_type_id (c_pretty_printer *pp, tree t)
513 {
514 pp_c_specifier_qualifier_list (pp, t);
515 pp_abstract_declarator (pp, t);
516 }
517
518 /* storage-class-specifier:
519 typedef
520 extern
521 static
522 auto
523 register */
524
525 void
526 pp_c_storage_class_specifier (c_pretty_printer *pp, tree t)
527 {
528 if (TREE_CODE (t) == TYPE_DECL)
529 pp_c_identifier (pp, "typedef");
530 else if (DECL_P (t))
531 {
532 if (DECL_REGISTER (t))
533 pp_c_identifier (pp, "register");
534 else if (TREE_STATIC (t) && TREE_CODE (t) == VAR_DECL)
535 pp_c_identifier (pp, "static");
536 }
537 }
538
539 /* function-specifier:
540 inline */
541
542 void
543 pp_c_function_specifier (c_pretty_printer *pp, tree t)
544 {
545 if (TREE_CODE (t) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (t))
546 pp_c_identifier (pp, "inline");
547 }
548
549 /* declaration-specifiers:
550 storage-class-specifier declaration-specifiers(opt)
551 type-specifier declaration-specifiers(opt)
552 type-qualifier declaration-specifiers(opt)
553 function-specifier declaration-specifiers(opt) */
554
555 void
556 pp_c_declaration_specifiers (c_pretty_printer *pp, tree t)
557 {
558 pp_storage_class_specifier (pp, t);
559 pp_function_specifier (pp, t);
560 pp_c_specifier_qualifier_list (pp, DECL_P (t) ? TREE_TYPE (t) : t);
561 }
562
563 /* direct-declarator
564 identifier
565 ( declarator )
566 direct-declarator [ type-qualifier-list(opt) assignment-expression(opt) ]
567 direct-declarator [ static type-qualifier-list(opt) assignment-expression(opt)]
568 direct-declarator [ type-qualifier-list static assignment-expression ]
569 direct-declarator [ type-qualifier-list * ]
570 direct-declarator ( parameter-type-list )
571 direct-declarator ( identifier-list(opt) ) */
572
573 void
574 pp_c_direct_declarator (c_pretty_printer *pp, tree t)
575 {
576 switch (TREE_CODE (t))
577 {
578 case VAR_DECL:
579 case PARM_DECL:
580 case TYPE_DECL:
581 case FIELD_DECL:
582 case LABEL_DECL:
583 pp_c_space_for_pointer_operator (pp, TREE_TYPE (t));
584 pp_c_tree_decl_identifier (pp, t);
585 break;
586
587 case ARRAY_TYPE:
588 case POINTER_TYPE:
589 pp_abstract_declarator (pp, TREE_TYPE (t));
590 break;
591
592 case FUNCTION_TYPE:
593 pp_parameter_list (pp, t);
594 pp_abstract_declarator (pp, TREE_TYPE (t));
595 break;
596
597 case FUNCTION_DECL:
598 pp_c_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
599 pp_c_tree_decl_identifier (pp, t);
600 if (pp_c_base (pp)->flags & pp_c_flag_abstract)
601 pp_abstract_declarator (pp, TREE_TYPE (t));
602 else
603 {
604 pp_parameter_list (pp, t);
605 pp_abstract_declarator (pp, TREE_TYPE (TREE_TYPE (t)));
606 }
607 break;
608
609 case INTEGER_TYPE:
610 case REAL_TYPE:
611 case ENUMERAL_TYPE:
612 case UNION_TYPE:
613 case RECORD_TYPE:
614 break;
615
616 default:
617 pp_unsupported_tree (pp, t);
618 break;
619 }
620 }
621
622
623 /* declarator:
624 pointer(opt) direct-declarator */
625
626 void
627 pp_c_declarator (c_pretty_printer *pp, tree t)
628 {
629 switch (TREE_CODE (t))
630 {
631 case INTEGER_TYPE:
632 case REAL_TYPE:
633 case ENUMERAL_TYPE:
634 case UNION_TYPE:
635 case RECORD_TYPE:
636 break;
637
638 case VAR_DECL:
639 case PARM_DECL:
640 case FIELD_DECL:
641 case ARRAY_TYPE:
642 case FUNCTION_TYPE:
643 case FUNCTION_DECL:
644 case TYPE_DECL:
645 pp_direct_declarator (pp, t);
646 break;
647
648
649 default:
650 pp_unsupported_tree (pp, t);
651 break;
652 }
653 }
654
655 /* declaration:
656 declaration-specifiers init-declarator-list(opt) ; */
657
658 void
659 pp_c_declaration (c_pretty_printer *pp, tree t)
660 {
661 pp_declaration_specifiers (pp, t);
662 pp_c_init_declarator (pp, t);
663 }
664
665 /* Pretty-print ATTRIBUTES using GNU C extension syntax. */
666
667 void
668 pp_c_attributes (c_pretty_printer *pp, tree attributes)
669 {
670 if (attributes == NULL_TREE)
671 return;
672
673 pp_c_identifier (pp, "__attribute__");
674 pp_c_left_paren (pp);
675 pp_c_left_paren (pp);
676 for (; attributes != NULL_TREE; attributes = TREE_CHAIN (attributes))
677 {
678 pp_tree_identifier (pp, TREE_PURPOSE (attributes));
679 if (TREE_VALUE (attributes))
680 pp_c_call_argument_list (pp, TREE_VALUE (attributes));
681
682 if (TREE_CHAIN (attributes))
683 pp_separate_with (pp, ',');
684 }
685 pp_c_right_paren (pp);
686 pp_c_right_paren (pp);
687 }
688
689 /* function-definition:
690 declaration-specifiers declarator compound-statement */
691
692 void
693 pp_c_function_definition (c_pretty_printer *pp, tree t)
694 {
695 pp_declaration_specifiers (pp, t);
696 pp_declarator (pp, t);
697 pp_needs_newline (pp) = true;
698 pp_statement (pp, DECL_SAVED_TREE (t));
699 pp_newline (pp);
700 pp_flush (pp);
701 }
702
703 \f
704 /* Expressions. */
705
706 /* Print out a c-char. */
707
708 static void
709 pp_c_char (c_pretty_printer *pp, int c)
710 {
711 switch (c)
712 {
713 case TARGET_NEWLINE:
714 pp_string (pp, "\\n");
715 break;
716 case TARGET_TAB:
717 pp_string (pp, "\\t");
718 break;
719 case TARGET_VT:
720 pp_string (pp, "\\v");
721 break;
722 case TARGET_BS:
723 pp_string (pp, "\\b");
724 break;
725 case TARGET_CR:
726 pp_string (pp, "\\r");
727 break;
728 case TARGET_FF:
729 pp_string (pp, "\\f");
730 break;
731 case TARGET_BELL:
732 pp_string (pp, "\\a");
733 break;
734 case '\\':
735 pp_string (pp, "\\\\");
736 break;
737 case '\'':
738 pp_string (pp, "\\'");
739 break;
740 case '\"':
741 pp_string (pp, "\\\"");
742 break;
743 default:
744 if (ISPRINT (c))
745 pp_character (pp, c);
746 else
747 pp_scalar (pp, "\\%03o", (unsigned) c);
748 break;
749 }
750 }
751
752 /* Print out a STRING literal. */
753
754 void
755 pp_c_string_literal (c_pretty_printer *pp, tree s)
756 {
757 const char *p = TREE_STRING_POINTER (s);
758 int n = TREE_STRING_LENGTH (s) - 1;
759 int i;
760 pp_doublequote (pp);
761 for (i = 0; i < n; ++i)
762 pp_c_char (pp, p[i]);
763 pp_doublequote (pp);
764 }
765
766 /* Pretty-print an INTEGER literal. */
767
768 static void
769 pp_c_integer_constant (c_pretty_printer *pp, tree i)
770 {
771 tree type = TREE_TYPE (i);
772
773 if (TREE_INT_CST_HIGH (i) == 0)
774 pp_wide_integer (pp, TREE_INT_CST_LOW (i));
775 else
776 {
777 if (tree_int_cst_sgn (i) < 0)
778 {
779 pp_c_char (pp, '-');
780 i = build_int_2 (-TREE_INT_CST_LOW (i),
781 ~TREE_INT_CST_HIGH (i) + !TREE_INT_CST_LOW (i));
782 }
783 sprintf (pp_buffer (pp)->digit_buffer,
784 HOST_WIDE_INT_PRINT_DOUBLE_HEX,
785 TREE_INT_CST_HIGH (i), TREE_INT_CST_LOW (i));
786 pp_string (pp, pp_buffer (pp)->digit_buffer);
787 }
788 if (TYPE_UNSIGNED (type))
789 pp_character (pp, 'u');
790 if (type == long_integer_type_node || type == long_unsigned_type_node)
791 pp_character (pp, 'l');
792 else if (type == long_long_integer_type_node
793 || type == long_long_unsigned_type_node)
794 pp_string (pp, "ll");
795 }
796
797 /* Print out a CHARACTER literal. */
798
799 static void
800 pp_c_character_constant (c_pretty_printer *pp, tree c)
801 {
802 tree type = TREE_TYPE (c);
803 if (type == wchar_type_node)
804 pp_character (pp, 'L');
805 pp_quote (pp);
806 if (host_integerp (c, TYPE_UNSIGNED (type)))
807 pp_c_char (pp, tree_low_cst (c, TYPE_UNSIGNED (type)));
808 else
809 pp_scalar (pp, "\\x%x", (unsigned) TREE_INT_CST_LOW (c));
810 pp_quote (pp);
811 }
812
813 /* Print out a BOOLEAN literal. */
814
815 static void
816 pp_c_bool_constant (c_pretty_printer *pp, tree b)
817 {
818 if (b == boolean_false_node)
819 {
820 if (c_dialect_cxx ())
821 pp_c_identifier (pp, "false");
822 else if (flag_isoc99)
823 pp_c_identifier (pp, "_False");
824 else
825 pp_unsupported_tree (pp, b);
826 }
827 else if (b == boolean_true_node)
828 {
829 if (c_dialect_cxx ())
830 pp_c_identifier (pp, "true");
831 else if (flag_isoc99)
832 pp_c_identifier (pp, "_True");
833 else
834 pp_unsupported_tree (pp, b);
835 }
836 else if (TREE_CODE (b) == INTEGER_CST)
837 pp_c_integer_constant (pp, b);
838 else
839 pp_unsupported_tree (pp, b);
840 }
841
842 /* Attempt to print out an ENUMERATOR. Return true on success. Else return
843 false; that means the value was obtained by a cast, in which case
844 print out the type-id part of the cast-expression -- the casted value
845 is then printed by pp_c_integer_literal. */
846
847 static bool
848 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
849 {
850 bool value_is_named = true;
851 tree type = TREE_TYPE (e);
852 tree value;
853
854 /* Find the name of this constant. */
855 for (value = TYPE_VALUES (type);
856 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
857 value = TREE_CHAIN (value))
858 ;
859
860 if (value != NULL_TREE)
861 pp_id_expression (pp, TREE_PURPOSE (value));
862 else
863 {
864 /* Value must have been cast. */
865 pp_c_type_cast (pp, type);
866 value_is_named = false;
867 }
868
869 return value_is_named;
870 }
871
872 /* Print out a REAL value as a decimal-floating-constant. */
873
874 static void
875 pp_c_floating_constant (c_pretty_printer *pp, tree r)
876 {
877 real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
878 sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
879 pp_string (pp, pp_buffer(pp)->digit_buffer);
880 if (TREE_TYPE (r) == float_type_node)
881 pp_character (pp, 'f');
882 else if (TREE_TYPE (r) == long_double_type_node)
883 pp_character (pp, 'l');
884 }
885
886 /* Pretty-print a compound literal expression. GNU extensions include
887 vector constants. */
888
889 static void
890 pp_c_compound_literal (c_pretty_printer *pp, tree e)
891 {
892 tree type = TREE_TYPE (e);
893 pp_c_type_cast (pp, type);
894
895 switch (TREE_CODE (type))
896 {
897 case RECORD_TYPE:
898 case UNION_TYPE:
899 case ARRAY_TYPE:
900 case VECTOR_TYPE:
901 case COMPLEX_TYPE:
902 pp_c_brace_enclosed_initializer_list (pp, e);
903 break;
904
905 default:
906 pp_unsupported_tree (pp, e);
907 break;
908 }
909 }
910
911 /* constant:
912 integer-constant
913 floating-constant
914 enumeration-constant
915 character-constant */
916
917 void
918 pp_c_constant (c_pretty_printer *pp, tree e)
919 {
920 const enum tree_code code = TREE_CODE (e);
921
922 switch (code)
923 {
924 case INTEGER_CST:
925 {
926 tree type = TREE_TYPE (e);
927 if (type == boolean_type_node)
928 pp_c_bool_constant (pp, e);
929 else if (type == char_type_node)
930 pp_c_character_constant (pp, e);
931 else if (TREE_CODE (type) == ENUMERAL_TYPE
932 && pp_c_enumeration_constant (pp, e))
933 ;
934 else
935 pp_c_integer_constant (pp, e);
936 }
937 break;
938
939 case REAL_CST:
940 pp_c_floating_constant (pp, e);
941 break;
942
943 case STRING_CST:
944 pp_c_string_literal (pp, e);
945 break;
946
947 default:
948 pp_unsupported_tree (pp, e);
949 break;
950 }
951 }
952
953 /* Pretty-print an IDENTIFIER_NODE, preceded by whitespace is necessary. */
954
955 void
956 pp_c_identifier (c_pretty_printer *pp, const char *id)
957 {
958 pp_c_maybe_whitespace (pp);
959 pp_identifier (pp, id);
960 pp_base (pp)->padding = pp_before;
961 }
962
963 /* Pretty-print a C primary-expression.
964 primary-expression:
965 identifier
966 constant
967 string-literal
968 ( expression ) */
969
970 void
971 pp_c_primary_expression (c_pretty_printer *pp, tree e)
972 {
973 switch (TREE_CODE (e))
974 {
975 case VAR_DECL:
976 case PARM_DECL:
977 case FIELD_DECL:
978 case CONST_DECL:
979 case FUNCTION_DECL:
980 case LABEL_DECL:
981 pp_c_tree_decl_identifier (pp, e);
982 break;
983
984 case IDENTIFIER_NODE:
985 pp_c_tree_identifier (pp, e);
986 break;
987
988 case ERROR_MARK:
989 pp_c_identifier (pp, "<erroneous-expression>");
990 break;
991
992 case RESULT_DECL:
993 pp_c_identifier (pp, "<return-value>");
994 break;
995
996 case INTEGER_CST:
997 case REAL_CST:
998 case STRING_CST:
999 pp_c_constant (pp, e);
1000 break;
1001
1002 case TARGET_EXPR:
1003 pp_c_identifier (pp, "__builtin_memcpy");
1004 pp_c_left_paren (pp);
1005 pp_ampersand (pp);
1006 pp_primary_expression (pp, TREE_OPERAND (e, 0));
1007 pp_separate_with (pp, ',');
1008 pp_ampersand (pp);
1009 pp_initializer (pp, TREE_OPERAND (e, 1));
1010 if (TREE_OPERAND (e, 2))
1011 {
1012 pp_separate_with (pp, ',');
1013 pp_c_expression (pp, TREE_OPERAND (e, 2));
1014 }
1015 pp_c_right_paren (pp);
1016 break;
1017
1018 case STMT_EXPR:
1019 pp_c_left_paren (pp);
1020 pp_statement (pp, STMT_EXPR_STMT (e));
1021 pp_c_right_paren (pp);
1022 break;
1023
1024 default:
1025 /* FIXME: Make sure we won't get into an infinie loop. */
1026 pp_c_left_paren (pp);
1027 pp_expression (pp, e);
1028 pp_c_right_paren (pp);
1029 break;
1030 }
1031 }
1032
1033 /* Print out a C initializer -- also support C compound-literals.
1034 initializer:
1035 assignment-expression:
1036 { initializer-list }
1037 { initializer-list , } */
1038
1039 static void
1040 pp_c_initializer (c_pretty_printer *pp, tree e)
1041 {
1042 if (TREE_CODE (e) == CONSTRUCTOR)
1043 pp_c_brace_enclosed_initializer_list (pp, e);
1044 else
1045 pp_expression (pp, e);
1046 }
1047
1048 /* init-declarator:
1049 declarator:
1050 declarator = initializer */
1051
1052 void
1053 pp_c_init_declarator (c_pretty_printer *pp, tree t)
1054 {
1055 pp_declarator (pp, t);
1056 /* We don't want to output function definitions here. There are handled
1057 elsewhere (and the syntactic form is bogus anyway). */
1058 if (TREE_CODE (t) != FUNCTION_DECL && DECL_INITIAL (t))
1059 {
1060 tree init = DECL_INITIAL (t);
1061 /* This C++ bit is handled here because it is easier to do so.
1062 In templates, the C++ parser builds a TREE_LIST for a
1063 direct-initialization; the TREE_PURPOSE is the variable to
1064 initialize and the TREE_VALUE is the initializer. */
1065 if (TREE_CODE (init) == TREE_LIST)
1066 {
1067 pp_c_left_paren (pp);
1068 pp_expression (pp, TREE_VALUE (init));
1069 pp_right_paren (pp);
1070 }
1071 else
1072 {
1073 pp_space (pp);
1074 pp_equal (pp);
1075 pp_space (pp);
1076 pp_c_initializer (pp, init);
1077 }
1078 }
1079 }
1080
1081 /* initializer-list:
1082 designation(opt) initializer
1083 initializer-list , designation(opt) initializer
1084
1085 designation:
1086 designator-list =
1087
1088 designator-list:
1089 designator
1090 designator-list designator
1091
1092 designator:
1093 [ constant-expression ]
1094 identifier */
1095
1096 static void
1097 pp_c_initializer_list (c_pretty_printer *pp, tree e)
1098 {
1099 tree type = TREE_TYPE (e);
1100 const enum tree_code code = TREE_CODE (type);
1101
1102 switch (code)
1103 {
1104 case RECORD_TYPE:
1105 case UNION_TYPE:
1106 case ARRAY_TYPE:
1107 {
1108 tree init = TREE_OPERAND (e, 0);
1109 for (; init != NULL_TREE; init = TREE_CHAIN (init))
1110 {
1111 if (code == RECORD_TYPE || code == UNION_TYPE)
1112 {
1113 pp_c_dot (pp);
1114 pp_c_primary_expression (pp, TREE_PURPOSE (init));
1115 }
1116 else
1117 {
1118 pp_c_left_bracket (pp);
1119 if (TREE_PURPOSE (init))
1120 pp_c_constant (pp, TREE_PURPOSE (init));
1121 pp_c_right_bracket (pp);
1122 }
1123 pp_c_whitespace (pp);
1124 pp_equal (pp);
1125 pp_c_whitespace (pp);
1126 pp_initializer (pp, TREE_VALUE (init));
1127 if (TREE_CHAIN (init))
1128 pp_separate_with (pp, ',');
1129 }
1130 }
1131 return;
1132
1133 case VECTOR_TYPE:
1134 if (TREE_CODE (e) == VECTOR_CST)
1135 pp_c_expression_list (pp, TREE_VECTOR_CST_ELTS (e));
1136 else if (TREE_CODE (e) == CONSTRUCTOR)
1137 pp_c_expression_list (pp, CONSTRUCTOR_ELTS (e));
1138 else
1139 break;
1140 return;
1141
1142 case COMPLEX_TYPE:
1143 if (TREE_CODE (e) == CONSTRUCTOR)
1144 pp_c_expression_list (pp, CONSTRUCTOR_ELTS (e));
1145 else if (TREE_CODE (e) == COMPLEX_CST || TREE_CODE (e) == COMPLEX_EXPR)
1146 {
1147 const bool cst = TREE_CODE (e) == COMPLEX_CST;
1148 pp_expression (pp, cst ? TREE_REALPART (e) : TREE_OPERAND (e, 0));
1149 pp_separate_with (pp, ',');
1150 pp_expression (pp, cst ? TREE_IMAGPART (e) : TREE_OPERAND (e, 1));
1151 }
1152 else
1153 break;
1154 return;
1155
1156 default:
1157 break;
1158 }
1159
1160 pp_unsupported_tree (pp, type);
1161 }
1162
1163 /* Pretty-print a brace-enclosed initializer-list. */
1164
1165 static void
1166 pp_c_brace_enclosed_initializer_list (c_pretty_printer *pp, tree l)
1167 {
1168 pp_c_left_brace (pp);
1169 pp_c_initializer_list (pp, l);
1170 pp_c_right_brace (pp);
1171 }
1172
1173
1174 /* This is a convenient function, used to bridge gap between C and C++
1175 grammars.
1176
1177 id-expression:
1178 identifier */
1179
1180 void
1181 pp_c_id_expression (c_pretty_printer *pp, tree t)
1182 {
1183 switch (TREE_CODE (t))
1184 {
1185 case VAR_DECL:
1186 case PARM_DECL:
1187 case CONST_DECL:
1188 case TYPE_DECL:
1189 case FUNCTION_DECL:
1190 case FIELD_DECL:
1191 case LABEL_DECL:
1192 pp_c_tree_decl_identifier (pp, t);
1193 break;
1194
1195 case IDENTIFIER_NODE:
1196 pp_c_tree_identifier (pp, t);
1197 break;
1198
1199 default:
1200 pp_unsupported_tree (pp, t);
1201 break;
1202 }
1203 }
1204
1205 /* postfix-expression:
1206 primary-expression
1207 postfix-expression [ expression ]
1208 postfix-expression ( argument-expression-list(opt) )
1209 postfix-expression . identifier
1210 postfix-expression -> identifier
1211 postfix-expression ++
1212 postfix-expression --
1213 ( type-name ) { initializer-list }
1214 ( type-name ) { initializer-list , } */
1215
1216 void
1217 pp_c_postfix_expression (c_pretty_printer *pp, tree e)
1218 {
1219 enum tree_code code = TREE_CODE (e);
1220 switch (code)
1221 {
1222 case POSTINCREMENT_EXPR:
1223 case POSTDECREMENT_EXPR:
1224 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1225 pp_identifier (pp, code == POSTINCREMENT_EXPR ? "++" : "--");
1226 break;
1227
1228 case ARROW_EXPR:
1229 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1230 pp_c_arrow (pp);
1231 break;
1232
1233 case ARRAY_REF:
1234 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1235 pp_c_left_bracket (pp);
1236 pp_expression (pp, TREE_OPERAND (e, 1));
1237 pp_c_right_bracket (pp);
1238 break;
1239
1240 case CALL_EXPR:
1241 pp_postfix_expression (pp, TREE_OPERAND (e, 0));
1242 pp_c_call_argument_list (pp, TREE_OPERAND (e, 1));
1243 break;
1244
1245 case ABS_EXPR:
1246 pp_c_identifier (pp, "__builtin_abs");
1247 pp_c_left_paren (pp);
1248 pp_expression (pp, TREE_OPERAND (e, 0));
1249 pp_c_right_paren (pp);
1250 break;
1251
1252 case COMPONENT_REF:
1253 {
1254 tree object = TREE_OPERAND (e, 0);
1255 if (TREE_CODE (object) == INDIRECT_REF)
1256 {
1257 pp_postfix_expression (pp, TREE_OPERAND (object, 0));
1258 pp_c_arrow (pp);
1259 }
1260 else
1261 {
1262 pp_postfix_expression (pp, object);
1263 pp_c_dot (pp);
1264 }
1265 pp_expression (pp, TREE_OPERAND (e, 1));
1266 }
1267 break;
1268
1269 case COMPLEX_CST:
1270 case VECTOR_CST:
1271 case COMPLEX_EXPR:
1272 pp_c_compound_literal (pp, e);
1273 break;
1274
1275 case COMPOUND_LITERAL_EXPR:
1276 e = DECL_INITIAL (COMPOUND_LITERAL_EXPR_DECL (e));
1277 /* Fall through. */
1278 case CONSTRUCTOR:
1279 pp_initializer (pp, e);
1280 break;
1281
1282 case VA_ARG_EXPR:
1283 pp_c_identifier (pp, "__builtin_va_arg");
1284 pp_c_left_paren (pp);
1285 pp_assignment_expression (pp, TREE_OPERAND (e, 0));
1286 pp_separate_with (pp, ',');
1287 pp_type_id (pp, TREE_TYPE (e));
1288 pp_c_right_paren (pp);
1289 break;
1290
1291 case ADDR_EXPR:
1292 if (TREE_CODE (TREE_OPERAND (e, 0)) == FUNCTION_DECL)
1293 {
1294 pp_c_id_expression (pp, TREE_OPERAND (e, 0));
1295 break;
1296 }
1297 /* else fall through. */
1298
1299 default:
1300 pp_primary_expression (pp, e);
1301 break;
1302 }
1303 }
1304
1305 /* Print out an expression-list; E is expected to be a TREE_LIST. */
1306
1307 void
1308 pp_c_expression_list (c_pretty_printer *pp, tree e)
1309 {
1310 for (; e != NULL_TREE; e = TREE_CHAIN (e))
1311 {
1312 pp_expression (pp, TREE_VALUE (e));
1313 if (TREE_CHAIN (e))
1314 pp_separate_with (pp, ',');
1315 }
1316 }
1317
1318 /* Print out an expression-list in parens, as in a function call. */
1319
1320 void
1321 pp_c_call_argument_list (c_pretty_printer *pp, tree t)
1322 {
1323 pp_c_left_paren (pp);
1324 if (t && TREE_CODE (t) == TREE_LIST)
1325 pp_c_expression_list (pp, t);
1326 pp_c_right_paren (pp);
1327 }
1328
1329 /* unary-expression:
1330 postfix-expression
1331 ++ cast-expression
1332 -- cast-expression
1333 unary-operator cast-expression
1334 sizeof unary-expression
1335 sizeof ( type-id )
1336
1337 unary-operator: one of
1338 * & + - ! ~
1339
1340 GNU extensions.
1341 unary-expression:
1342 __alignof__ unary-expression
1343 __alignof__ ( type-id )
1344 __real__ unary-expression
1345 __imag__ unary-expression */
1346
1347 void
1348 pp_c_unary_expression (c_pretty_printer *pp, tree e)
1349 {
1350 enum tree_code code = TREE_CODE (e);
1351 switch (code)
1352 {
1353 case PREINCREMENT_EXPR:
1354 case PREDECREMENT_EXPR:
1355 pp_identifier (pp, code == PREINCREMENT_EXPR ? "++" : "--");
1356 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1357 break;
1358
1359 case ADDR_EXPR:
1360 case INDIRECT_REF:
1361 case NEGATE_EXPR:
1362 case BIT_NOT_EXPR:
1363 case TRUTH_NOT_EXPR:
1364 case CONJ_EXPR:
1365 /* String literal are used by address. */
1366 if (code == ADDR_EXPR && TREE_CODE (TREE_OPERAND (e, 0)) != STRING_CST)
1367 pp_ampersand (pp);
1368 else if (code == INDIRECT_REF)
1369 pp_c_star (pp);
1370 else if (code == NEGATE_EXPR)
1371 pp_minus (pp);
1372 else if (code == BIT_NOT_EXPR || code == CONJ_EXPR)
1373 pp_complement (pp);
1374 else if (code == TRUTH_NOT_EXPR)
1375 pp_exclamation (pp);
1376 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1377 break;
1378
1379 case SIZEOF_EXPR:
1380 case ALIGNOF_EXPR:
1381 pp_c_identifier (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
1382 pp_c_whitespace (pp);
1383 if (TYPE_P (TREE_OPERAND (e, 0)))
1384 pp_c_type_cast (pp, TREE_OPERAND (e, 0));
1385 else
1386 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1387 break;
1388
1389 case REALPART_EXPR:
1390 case IMAGPART_EXPR:
1391 pp_c_identifier (pp, code == REALPART_EXPR ? "__real__" : "__imag__");
1392 pp_c_whitespace (pp);
1393 pp_unary_expression (pp, TREE_OPERAND (e, 0));
1394 break;
1395
1396 default:
1397 pp_postfix_expression (pp, e);
1398 break;
1399 }
1400 }
1401
1402 /* cast-expression:
1403 unary-expression
1404 ( type-name ) cast-expression */
1405
1406 void
1407 pp_c_cast_expression (c_pretty_printer *pp, tree e)
1408 {
1409 switch (TREE_CODE (e))
1410 {
1411 case FLOAT_EXPR:
1412 case FIX_TRUNC_EXPR:
1413 case CONVERT_EXPR:
1414 pp_c_type_cast (pp, TREE_TYPE (e));
1415 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1416 break;
1417
1418 default:
1419 pp_unary_expression (pp, e);
1420 }
1421 }
1422
1423 /* multiplicative-expression:
1424 cast-expression
1425 multiplicative-expression * cast-expression
1426 multiplicative-expression / cast-expression
1427 multiplicative-expression % cast-expression */
1428
1429 static void
1430 pp_c_multiplicative_expression (c_pretty_printer *pp, tree e)
1431 {
1432 enum tree_code code = TREE_CODE (e);
1433 switch (code)
1434 {
1435 case MULT_EXPR:
1436 case TRUNC_DIV_EXPR:
1437 case TRUNC_MOD_EXPR:
1438 pp_multiplicative_expression (pp, TREE_OPERAND (e, 0));
1439 pp_c_whitespace (pp);
1440 if (code == MULT_EXPR)
1441 pp_c_star (pp);
1442 else if (code == TRUNC_DIV_EXPR)
1443 pp_slash (pp);
1444 else
1445 pp_modulo (pp);
1446 pp_c_whitespace (pp);
1447 pp_c_cast_expression (pp, TREE_OPERAND (e, 1));
1448 break;
1449
1450 default:
1451 pp_c_cast_expression (pp, e);
1452 break;
1453 }
1454 }
1455
1456 /* additive-expression:
1457 multiplicative-expression
1458 additive-expression + multiplicative-expression
1459 additive-expression - multiplicative-expression */
1460
1461 static void
1462 pp_c_additive_expression (c_pretty_printer *pp, tree e)
1463 {
1464 enum tree_code code = TREE_CODE (e);
1465 switch (code)
1466 {
1467 case PLUS_EXPR:
1468 case MINUS_EXPR:
1469 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1470 pp_c_whitespace (pp);
1471 if (code == PLUS_EXPR)
1472 pp_plus (pp);
1473 else
1474 pp_minus (pp);
1475 pp_c_whitespace (pp);
1476 pp_multiplicative_expression (pp, TREE_OPERAND (e, 1));
1477 break;
1478
1479 default:
1480 pp_multiplicative_expression (pp, e);
1481 break;
1482 }
1483 }
1484
1485 /* additive-expression:
1486 additive-expression
1487 shift-expression << additive-expression
1488 shift-expression >> additive-expression */
1489
1490 static void
1491 pp_c_shift_expression (c_pretty_printer *pp, tree e)
1492 {
1493 enum tree_code code = TREE_CODE (e);
1494 switch (code)
1495 {
1496 case LSHIFT_EXPR:
1497 case RSHIFT_EXPR:
1498 pp_c_shift_expression (pp, TREE_OPERAND (e, 0));
1499 pp_c_whitespace (pp);
1500 pp_identifier (pp, code == LSHIFT_EXPR ? "<<" : ">>");
1501 pp_c_whitespace (pp);
1502 pp_c_additive_expression (pp, TREE_OPERAND (e, 1));
1503 break;
1504
1505 default:
1506 pp_c_additive_expression (pp, e);
1507 }
1508 }
1509
1510 /* relational-expression:
1511 shift-expression
1512 relational-expression < shift-expression
1513 relational-expression > shift-expression
1514 relational-expression <= shift-expression
1515 relational-expression >= shift-expression */
1516
1517 static void
1518 pp_c_relational_expression (c_pretty_printer *pp, tree e)
1519 {
1520 enum tree_code code = TREE_CODE (e);
1521 switch (code)
1522 {
1523 case LT_EXPR:
1524 case GT_EXPR:
1525 case LE_EXPR:
1526 case GE_EXPR:
1527 pp_c_relational_expression (pp, TREE_OPERAND (e, 0));
1528 pp_c_whitespace (pp);
1529 if (code == LT_EXPR)
1530 pp_less (pp);
1531 else if (code == GT_EXPR)
1532 pp_greater (pp);
1533 else if (code == LE_EXPR)
1534 pp_identifier (pp, "<=");
1535 else if (code == GE_EXPR)
1536 pp_identifier (pp, ">=");
1537 pp_c_whitespace (pp);
1538 pp_c_shift_expression (pp, TREE_OPERAND (e, 1));
1539 break;
1540
1541 default:
1542 pp_c_shift_expression (pp, e);
1543 break;
1544 }
1545 }
1546
1547 /* equality-expression:
1548 relational-expression
1549 equality-expression == relational-expression
1550 equality-equality != relational-expression */
1551
1552 static void
1553 pp_c_equality_expression (c_pretty_printer *pp, tree e)
1554 {
1555 enum tree_code code = TREE_CODE (e);
1556 switch (code)
1557 {
1558 case EQ_EXPR:
1559 case NE_EXPR:
1560 pp_c_equality_expression (pp, TREE_OPERAND (e, 0));
1561 pp_c_whitespace (pp);
1562 pp_identifier (pp, code == EQ_EXPR ? "==" : "!=");
1563 pp_c_whitespace (pp);
1564 pp_c_relational_expression (pp, TREE_OPERAND (e, 1));
1565 break;
1566
1567 default:
1568 pp_c_relational_expression (pp, e);
1569 break;
1570 }
1571 }
1572
1573 /* AND-expression:
1574 equality-expression
1575 AND-expression & equality-equality */
1576
1577 static void
1578 pp_c_and_expression (c_pretty_printer *pp, tree e)
1579 {
1580 if (TREE_CODE (e) == BIT_AND_EXPR)
1581 {
1582 pp_c_and_expression (pp, TREE_OPERAND (e, 0));
1583 pp_c_whitespace (pp);
1584 pp_ampersand (pp);
1585 pp_c_whitespace (pp);
1586 pp_c_equality_expression (pp, TREE_OPERAND (e, 1));
1587 }
1588 else
1589 pp_c_equality_expression (pp, e);
1590 }
1591
1592 /* exclusive-OR-expression:
1593 AND-expression
1594 exclusive-OR-expression ^ AND-expression */
1595
1596 static void
1597 pp_c_exclusive_or_expression (c_pretty_printer *pp, tree e)
1598 {
1599 if (TREE_CODE (e) == BIT_XOR_EXPR)
1600 {
1601 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1602 pp_c_maybe_whitespace (pp);
1603 pp_carret (pp);
1604 pp_c_whitespace (pp);
1605 pp_c_and_expression (pp, TREE_OPERAND (e, 1));
1606 }
1607 else
1608 pp_c_and_expression (pp, e);
1609 }
1610
1611 /* inclusive-OR-expression:
1612 exclusive-OR-expression
1613 inclusive-OR-expression | exclusive-OR-expression */
1614
1615 static void
1616 pp_c_inclusive_or_expression (c_pretty_printer *pp, tree e)
1617 {
1618 if (TREE_CODE (e) == BIT_IOR_EXPR)
1619 {
1620 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 0));
1621 pp_c_whitespace (pp);
1622 pp_bar (pp);
1623 pp_c_whitespace (pp);
1624 pp_c_exclusive_or_expression (pp, TREE_OPERAND (e, 1));
1625 }
1626 else
1627 pp_c_exclusive_or_expression (pp, e);
1628 }
1629
1630 /* logical-AND-expression:
1631 inclusive-OR-expression
1632 logical-AND-expression && inclusive-OR-expression */
1633
1634 static void
1635 pp_c_logical_and_expression (c_pretty_printer *pp, tree e)
1636 {
1637 if (TREE_CODE (e) == TRUTH_ANDIF_EXPR)
1638 {
1639 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 0));
1640 pp_c_whitespace (pp);
1641 pp_identifier (pp, "&&");
1642 pp_c_whitespace (pp);
1643 pp_c_inclusive_or_expression (pp, TREE_OPERAND (e, 1));
1644 }
1645 else
1646 pp_c_inclusive_or_expression (pp, e);
1647 }
1648
1649 /* logical-OR-expression:
1650 logical-AND-expression
1651 logical-OR-expression || logical-AND-expression */
1652
1653 void
1654 pp_c_logical_or_expression (c_pretty_printer *pp, tree e)
1655 {
1656 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
1657 {
1658 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1659 pp_c_whitespace (pp);
1660 pp_identifier (pp, "||");
1661 pp_c_whitespace (pp);
1662 pp_c_logical_and_expression (pp, TREE_OPERAND (e, 1));
1663 }
1664 else
1665 pp_c_logical_and_expression (pp, e);
1666 }
1667
1668 /* conditional-expression:
1669 logical-OR-expression
1670 logical-OR-expression ? expression : conditional-expression */
1671
1672 static void
1673 pp_c_conditional_expression (c_pretty_printer *pp, tree e)
1674 {
1675 if (TREE_CODE (e) == COND_EXPR)
1676 {
1677 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1678 pp_c_whitespace (pp);
1679 pp_question (pp);
1680 pp_c_whitespace (pp);
1681 pp_expression (pp, TREE_OPERAND (e, 1));
1682 pp_c_whitespace (pp);
1683 pp_colon (pp);
1684 pp_c_whitespace (pp);
1685 pp_c_conditional_expression (pp, TREE_OPERAND (e, 2));
1686 }
1687 else
1688 pp_c_logical_or_expression (pp, e);
1689 }
1690
1691
1692 /* assignment-expression:
1693 conditional-expression
1694 unary-expression assignment-operator assignment-expression
1695
1696 assignment-expression: one of
1697 = *= /= %= += -= >>= <<= &= ^= |= */
1698
1699 static void
1700 pp_c_assignment_expression (c_pretty_printer *pp, tree e)
1701 {
1702 if (TREE_CODE (e) == MODIFY_EXPR || TREE_CODE (e) == INIT_EXPR)
1703 {
1704 pp_c_unary_expression (pp, TREE_OPERAND (e, 0));
1705 pp_c_whitespace (pp);
1706 pp_equal (pp);
1707 pp_space (pp);
1708 pp_c_expression (pp, TREE_OPERAND (e, 1));
1709 }
1710 else
1711 pp_c_conditional_expression (pp, e);
1712 }
1713
1714 /* expression:
1715 assignment-expression
1716 expression , assignment-expression
1717
1718 Implementation note: instead of going through the usual recursion
1719 chain, I take the liberty of dispatching nodes to the appropriate
1720 functions. This makes some redundancy, but it worths it. That also
1721 prevents a possible infinite recursion between pp_c_primary_expression ()
1722 and pp_c_expression (). */
1723
1724 void
1725 pp_c_expression (c_pretty_printer *pp, tree e)
1726 {
1727 switch (TREE_CODE (e))
1728 {
1729 case INTEGER_CST:
1730 pp_c_integer_constant (pp, e);
1731 break;
1732
1733 case REAL_CST:
1734 pp_c_floating_constant (pp, e);
1735 break;
1736
1737 case STRING_CST:
1738 pp_c_string_literal (pp, e);
1739 break;
1740
1741 case IDENTIFIER_NODE:
1742 case FUNCTION_DECL:
1743 case VAR_DECL:
1744 case CONST_DECL:
1745 case PARM_DECL:
1746 case RESULT_DECL:
1747 case FIELD_DECL:
1748 case LABEL_DECL:
1749 case ERROR_MARK:
1750 case STMT_EXPR:
1751 pp_primary_expression (pp, e);
1752 break;
1753
1754 case POSTINCREMENT_EXPR:
1755 case POSTDECREMENT_EXPR:
1756 case ARROW_EXPR:
1757 case ARRAY_REF:
1758 case CALL_EXPR:
1759 case COMPONENT_REF:
1760 case COMPLEX_CST:
1761 case COMPLEX_EXPR:
1762 case VECTOR_CST:
1763 case ABS_EXPR:
1764 case CONSTRUCTOR:
1765 case COMPOUND_LITERAL_EXPR:
1766 case VA_ARG_EXPR:
1767 pp_postfix_expression (pp, e);
1768 break;
1769
1770 case CONJ_EXPR:
1771 case ADDR_EXPR:
1772 case INDIRECT_REF:
1773 case NEGATE_EXPR:
1774 case BIT_NOT_EXPR:
1775 case TRUTH_NOT_EXPR:
1776 case PREINCREMENT_EXPR:
1777 case PREDECREMENT_EXPR:
1778 case SIZEOF_EXPR:
1779 case ALIGNOF_EXPR:
1780 case REALPART_EXPR:
1781 case IMAGPART_EXPR:
1782 pp_c_unary_expression (pp, e);
1783 break;
1784
1785 case FLOAT_EXPR:
1786 case FIX_TRUNC_EXPR:
1787 case CONVERT_EXPR:
1788 pp_c_cast_expression (pp, e);
1789 break;
1790
1791 case MULT_EXPR:
1792 case TRUNC_MOD_EXPR:
1793 case TRUNC_DIV_EXPR:
1794 pp_multiplicative_expression (pp, e);
1795 break;
1796
1797 case LSHIFT_EXPR:
1798 case RSHIFT_EXPR:
1799 pp_c_shift_expression (pp, e);
1800 break;
1801
1802 case LT_EXPR:
1803 case GT_EXPR:
1804 case LE_EXPR:
1805 case GE_EXPR:
1806 pp_c_relational_expression (pp, e);
1807 break;
1808
1809 case BIT_AND_EXPR:
1810 pp_c_and_expression (pp, e);
1811 break;
1812
1813 case BIT_XOR_EXPR:
1814 pp_c_exclusive_or_expression (pp, e);
1815 break;
1816
1817 case BIT_IOR_EXPR:
1818 pp_c_inclusive_or_expression (pp, e);
1819 break;
1820
1821 case TRUTH_ANDIF_EXPR:
1822 pp_c_logical_and_expression (pp, e);
1823 break;
1824
1825 case TRUTH_ORIF_EXPR:
1826 pp_c_logical_or_expression (pp, e);
1827 break;
1828
1829 case EQ_EXPR:
1830 case NE_EXPR:
1831 pp_c_equality_expression (pp, e);
1832 break;
1833
1834 case COND_EXPR:
1835 pp_conditional_expression (pp, e);
1836 break;
1837
1838 case PLUS_EXPR:
1839 case MINUS_EXPR:
1840 pp_c_additive_expression (pp, e);
1841 break;
1842
1843 case MODIFY_EXPR:
1844 case INIT_EXPR:
1845 pp_assignment_expression (pp, e);
1846 break;
1847
1848 case COMPOUND_EXPR:
1849 pp_c_left_paren (pp);
1850 pp_expression (pp, TREE_OPERAND (e, 0));
1851 pp_separate_with (pp, ',');
1852 pp_assignment_expression (pp, TREE_OPERAND (e, 1));
1853 pp_c_right_paren (pp);
1854 break;
1855
1856 case NOP_EXPR:
1857 case NON_LVALUE_EXPR:
1858 case SAVE_EXPR:
1859 case UNSAVE_EXPR:
1860 pp_expression (pp, TREE_OPERAND (e, 0));
1861 break;
1862
1863 case TARGET_EXPR:
1864 pp_postfix_expression (pp, TREE_OPERAND (e, 1));
1865 break;
1866
1867 default:
1868 pp_unsupported_tree (pp, e);
1869 break;
1870 }
1871 }
1872
1873
1874 \f
1875 /* Statements. */
1876
1877 /* statement:
1878 labeled-statement
1879 compound-statement
1880 expression-statement
1881 selection-statement
1882 iteration-statement
1883 jump-statement */
1884
1885 void
1886 pp_c_statement (c_pretty_printer *pp, tree stmt)
1887 {
1888 enum tree_code code;
1889
1890 if (stmt == NULL)
1891 return;
1892
1893 code = TREE_CODE (stmt);
1894 switch (code)
1895 {
1896 case STATEMENT_LIST:
1897 {
1898 tree_stmt_iterator tsi;
1899
1900 if (pp_needs_newline (pp))
1901 pp_newline_and_indent (pp, 0);
1902 pp_c_left_brace (pp);
1903 pp_newline_and_indent (pp, 3);
1904 for (tsi = tsi_start (stmt); !tsi_end_p (tsi); tsi_next (&tsi))
1905 pp_statement (pp, tsi_stmt (tsi));
1906 pp_newline_and_indent (pp, -3);
1907 pp_c_right_brace (pp);
1908 pp_needs_newline (pp) = true;
1909 }
1910 break;
1911
1912 /* compound-statement:
1913 { block-item-list(opt) }
1914
1915 block-item-list:
1916 block-item
1917 block-item-list block-item
1918
1919 block-item:
1920 declaration
1921 statement */
1922 case COMPOUND_STMT:
1923 if (pp_needs_newline (pp))
1924 pp_newline_and_indent (pp, 0);
1925 pp_c_left_brace (pp);
1926 pp_newline_and_indent (pp, 3);
1927 pp_statement (pp, COMPOUND_BODY (stmt));
1928 pp_newline_and_indent (pp, -3);
1929 pp_c_right_brace (pp);
1930 pp_needs_newline (pp) = true;
1931 break;
1932
1933 /* expression-statement:
1934 expression(opt) ; */
1935 case EXPR_STMT:
1936 case CLEANUP_STMT:
1937 if (pp_needs_newline (pp))
1938 pp_newline_and_indent (pp, 0);
1939 {
1940 tree e = code == EXPR_STMT
1941 ? EXPR_STMT_EXPR (stmt)
1942 : CLEANUP_EXPR (stmt);
1943 if (e)
1944 pp_expression (pp, e);
1945 }
1946 pp_c_semicolon (pp);
1947 pp_needs_newline (pp) = true;
1948 break;
1949
1950 /* selection-statement:
1951 if ( expression ) statement
1952 if ( expression ) statement else statement
1953 switch ( expression ) statement */
1954 case IF_STMT:
1955 if (pp_needs_newline (pp))
1956 pp_newline_and_indent (pp, 0);
1957 pp_c_identifier (pp, "if");
1958 pp_c_whitespace (pp);
1959 pp_c_left_paren (pp);
1960 pp_expression (pp, IF_COND (stmt));
1961 pp_c_right_paren (pp);
1962 pp_newline_and_indent (pp, 3);
1963 pp_statement (pp, THEN_CLAUSE (stmt));
1964 pp_newline_and_indent (pp, -3);
1965 if (ELSE_CLAUSE (stmt))
1966 {
1967 tree else_clause = ELSE_CLAUSE (stmt);
1968 pp_c_identifier (pp, "else");
1969 if (TREE_CODE (else_clause) == IF_STMT)
1970 pp_c_whitespace (pp);
1971 else
1972 pp_newline_and_indent (pp, 3);
1973 pp_statement (pp, else_clause);
1974 if (TREE_CODE (else_clause) != IF_STMT)
1975 pp_newline_and_indent (pp, -3);
1976 }
1977 break;
1978
1979 case SWITCH_STMT:
1980 if (pp_needs_newline (pp))
1981 pp_newline_and_indent (pp, 0);
1982 pp_c_identifier (pp, "switch");
1983 pp_space (pp);
1984 pp_c_left_paren (pp);
1985 pp_expression (pp, SWITCH_COND (stmt));
1986 pp_c_right_paren (pp);
1987 pp_indentation (pp) += 3;
1988 pp_needs_newline (pp) = true;
1989 pp_statement (pp, SWITCH_BODY (stmt));
1990 pp_newline_and_indent (pp, -3);
1991 break;
1992
1993 /* iteration-statement:
1994 while ( expression ) statement
1995 do statement while ( expression ) ;
1996 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1997 for ( declaration expression(opt) ; expression(opt) ) statement */
1998 case WHILE_STMT:
1999 if (pp_needs_newline (pp))
2000 pp_newline_and_indent (pp, 0);
2001 pp_c_identifier (pp, "while");
2002 pp_space (pp);
2003 pp_c_left_paren (pp);
2004 pp_expression (pp, WHILE_COND (stmt));
2005 pp_c_right_paren (pp);
2006 pp_newline_and_indent (pp, 3);
2007 pp_statement (pp, WHILE_BODY (stmt));
2008 pp_indentation (pp) -= 3;
2009 pp_needs_newline (pp) = true;
2010 break;
2011
2012 case DO_STMT:
2013 if (pp_needs_newline (pp))
2014 pp_newline_and_indent (pp, 0);
2015 pp_c_identifier (pp, "do");
2016 pp_newline_and_indent (pp, 3);
2017 pp_statement (pp, DO_BODY (stmt));
2018 pp_newline_and_indent (pp, -3);
2019 pp_c_identifier (pp, "while");
2020 pp_space (pp);
2021 pp_c_left_paren (pp);
2022 pp_expression (pp, DO_COND (stmt));
2023 pp_c_right_paren (pp);
2024 pp_c_semicolon (pp);
2025 pp_needs_newline (pp) = true;
2026 break;
2027
2028 case FOR_STMT:
2029 if (pp_needs_newline (pp))
2030 pp_newline_and_indent (pp, 0);
2031 pp_c_identifier (pp, "for");
2032 pp_space (pp);
2033 pp_c_left_paren (pp);
2034 if (FOR_INIT_STMT (stmt))
2035 pp_statement (pp, FOR_INIT_STMT (stmt));
2036 else
2037 pp_c_semicolon (pp);
2038 pp_needs_newline (pp) = false;
2039 pp_c_whitespace (pp);
2040 if (FOR_COND (stmt))
2041 pp_expression (pp, FOR_COND (stmt));
2042 pp_c_semicolon (pp);
2043 pp_needs_newline (pp) = false;
2044 pp_c_whitespace (pp);
2045 if (FOR_EXPR (stmt))
2046 pp_expression (pp, FOR_EXPR (stmt));
2047 pp_c_right_paren (pp);
2048 pp_newline_and_indent (pp, 3);
2049 pp_statement (pp, FOR_BODY (stmt));
2050 pp_indentation (pp) -= 3;
2051 pp_needs_newline (pp) = true;
2052 break;
2053
2054 /* jump-statement:
2055 goto identifier;
2056 continue ;
2057 return expression(opt) ; */
2058 case BREAK_STMT:
2059 case CONTINUE_STMT:
2060 if (pp_needs_newline (pp))
2061 pp_newline_and_indent (pp, 0);
2062 pp_identifier (pp, code == BREAK_STMT ? "break" : "continue");
2063 pp_c_semicolon (pp);
2064 pp_needs_newline (pp) = true;
2065 break;
2066
2067 case RETURN_STMT:
2068 {
2069 tree e = RETURN_STMT_EXPR (stmt);
2070 if (pp_needs_newline (pp))
2071 pp_newline_and_indent (pp, 0);
2072 pp_c_identifier (pp, "return");
2073 pp_c_whitespace (pp);
2074 if (e)
2075 {
2076 if (TREE_CODE (e) == INIT_EXPR
2077 && TREE_CODE (TREE_OPERAND (e, 0)) == RESULT_DECL)
2078 e = TREE_OPERAND (e, 1);
2079 pp_expression (pp, e);
2080 }
2081 pp_c_semicolon (pp);
2082 pp_needs_newline (pp) = true;
2083 }
2084 break;
2085
2086 case DECL_STMT:
2087 if (pp_needs_newline (pp))
2088 pp_newline_and_indent (pp, 0);
2089 pp_declaration (pp, DECL_STMT_DECL (stmt));
2090 pp_needs_newline (pp) = true;
2091 break;
2092
2093 default:
2094 pp_unsupported_tree (pp, stmt);
2095 }
2096 }
2097
2098 \f
2099 /* Initialize the PRETTY-PRINTER for handling C codes. */
2100
2101 void
2102 pp_c_pretty_printer_init (c_pretty_printer *pp)
2103 {
2104 pp->offset_list = 0;
2105
2106 pp->declaration = pp_c_declaration;
2107 pp->declaration_specifiers = pp_c_declaration_specifiers;
2108 pp->declarator = pp_c_declarator;
2109 pp->direct_declarator = pp_c_direct_declarator;
2110 pp->type_specifier_seq = pp_c_specifier_qualifier_list;
2111 pp->abstract_declarator = pp_c_abstract_declarator;
2112 pp->direct_abstract_declarator = pp_c_direct_abstract_declarator;
2113 pp->ptr_operator = pp_c_pointer;
2114 pp->parameter_list = pp_c_parameter_type_list;
2115 pp->type_id = pp_c_type_id;
2116 pp->simple_type_specifier = pp_c_type_specifier;
2117 pp->function_specifier = pp_c_function_specifier;
2118 pp->storage_class_specifier = pp_c_storage_class_specifier;
2119
2120 pp->statement = pp_c_statement;
2121
2122 pp->id_expression = pp_c_id_expression;
2123 pp->primary_expression = pp_c_primary_expression;
2124 pp->postfix_expression = pp_c_postfix_expression;
2125 pp->unary_expression = pp_c_unary_expression;
2126 pp->initializer = pp_c_initializer;
2127 pp->multiplicative_expression = pp_c_multiplicative_expression;
2128 pp->conditional_expression = pp_c_conditional_expression;
2129 pp->assignment_expression = pp_c_assignment_expression;
2130 pp->expression = pp_c_expression;
2131 }
2132
2133
2134 /* Print the tree T in full, on file FILE. */
2135
2136 void
2137 print_c_tree (FILE *file, tree t)
2138 {
2139 static c_pretty_printer pp_rec;
2140 static bool initialized = 0;
2141 c_pretty_printer *pp = &pp_rec;
2142
2143 if (!initialized)
2144 {
2145 initialized = 1;
2146 pp_construct (pp_base (pp), NULL, 0);
2147 pp_c_pretty_printer_init (pp);
2148 pp_needs_newline (pp) = true;
2149 }
2150 pp_base (pp)->buffer->stream = file;
2151
2152 pp_statement (pp, t);
2153
2154 pp_newline (pp);
2155 pp_flush (pp);
2156 }
2157
2158 /* Print the tree T in full, on stderr. */
2159
2160 void
2161 debug_c_tree (tree t)
2162 {
2163 print_c_tree (stderr, t);
2164 fputc ('\n', stderr);
2165 }
2166
2167 /* Output the DECL_NAME of T. If T has no DECL_NAME, output a string made
2168 up of T's memory address. */
2169
2170 void
2171 pp_c_tree_decl_identifier (c_pretty_printer *pp, tree t)
2172 {
2173 const char *name;
2174
2175 if (!DECL_P (t))
2176 abort ();
2177
2178 if (DECL_NAME (t))
2179 name = IDENTIFIER_POINTER (DECL_NAME (t));
2180 else
2181 {
2182 static char xname[8];
2183 sprintf (xname, "<U%4x>", ((unsigned)((unsigned long)(t) & 0xffff)));
2184 name = xname;
2185 }
2186
2187 pp_c_identifier (pp, name);
2188 }