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