OSDN Git Service

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