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