Daily bump.
[official-gcc.git] / gcc / cp / cxx-pretty-print.c
blobf5f91c804c1812616833d6fa5d2d62c253a7c796
1 /* Implementation of subroutines for the GNU C++ pretty-printer.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "intl.h"
26 #include "cp-tree.h"
27 #include "cxx-pretty-print.h"
28 #include "tree-pretty-print.h"
30 static void pp_cxx_unqualified_id (cxx_pretty_printer *, tree);
31 static void pp_cxx_nested_name_specifier (cxx_pretty_printer *, tree);
32 static void pp_cxx_qualified_id (cxx_pretty_printer *, tree);
33 static void pp_cxx_template_argument_list (cxx_pretty_printer *, tree);
34 static void pp_cxx_type_specifier_seq (cxx_pretty_printer *, tree);
35 static void pp_cxx_ptr_operator (cxx_pretty_printer *, tree);
36 static void pp_cxx_parameter_declaration_clause (cxx_pretty_printer *, tree);
37 static void pp_cxx_template_parameter (cxx_pretty_printer *, tree);
38 static void pp_cxx_cast_expression (cxx_pretty_printer *, tree);
39 static void pp_cxx_typeid_expression (cxx_pretty_printer *, tree);
42 static inline void
43 pp_cxx_nonconsecutive_character (cxx_pretty_printer *pp, int c)
45 const char *p = pp_last_position_in_text (pp);
47 if (p != NULL && *p == c)
48 pp_cxx_whitespace (pp);
49 pp_character (pp, c);
50 pp->padding = pp_none;
53 #define pp_cxx_expression_list(PP, T) \
54 pp_c_expression_list (PP, T)
55 #define pp_cxx_space_for_pointer_operator(PP, T) \
56 pp_c_space_for_pointer_operator (PP, T)
57 #define pp_cxx_init_declarator(PP, T) \
58 pp_c_init_declarator (PP, T)
59 #define pp_cxx_call_argument_list(PP, T) \
60 pp_c_call_argument_list (PP, T)
62 void
63 pp_cxx_colon_colon (cxx_pretty_printer *pp)
65 pp_colon_colon (pp);
66 pp->padding = pp_none;
69 void
70 pp_cxx_begin_template_argument_list (cxx_pretty_printer *pp)
72 pp_cxx_nonconsecutive_character (pp, '<');
75 void
76 pp_cxx_end_template_argument_list (cxx_pretty_printer *pp)
78 pp_cxx_nonconsecutive_character (pp, '>');
81 void
82 pp_cxx_separate_with (cxx_pretty_printer *pp, int c)
84 pp_separate_with (pp, c);
85 pp->padding = pp_none;
88 /* Expressions. */
90 static inline bool
91 is_destructor_name (tree name)
93 return name == complete_dtor_identifier
94 || name == base_dtor_identifier
95 || name == deleting_dtor_identifier;
98 /* conversion-function-id:
99 operator conversion-type-id
101 conversion-type-id:
102 type-specifier-seq conversion-declarator(opt)
104 conversion-declarator:
105 ptr-operator conversion-declarator(opt) */
107 static inline void
108 pp_cxx_conversion_function_id (cxx_pretty_printer *pp, tree t)
110 pp_cxx_ws_string (pp, "operator");
111 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
114 static inline void
115 pp_cxx_template_id (cxx_pretty_printer *pp, tree t)
117 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0));
118 pp_cxx_begin_template_argument_list (pp);
119 pp_cxx_template_argument_list (pp, TREE_OPERAND (t, 1));
120 pp_cxx_end_template_argument_list (pp);
123 /* Prints the unqualified part of the id-expression T.
125 unqualified-id:
126 identifier
127 operator-function-id
128 conversion-function-id
129 ~ class-name
130 template-id */
132 static void
133 pp_cxx_unqualified_id (cxx_pretty_printer *pp, tree t)
135 enum tree_code code = TREE_CODE (t);
136 switch (code)
138 case RESULT_DECL:
139 pp->translate_string ("<return-value>");
140 break;
142 case OVERLOAD:
143 t = OVL_CURRENT (t);
144 case VAR_DECL:
145 case PARM_DECL:
146 case CONST_DECL:
147 case TYPE_DECL:
148 case FUNCTION_DECL:
149 case NAMESPACE_DECL:
150 case FIELD_DECL:
151 case LABEL_DECL:
152 case USING_DECL:
153 case TEMPLATE_DECL:
154 t = DECL_NAME (t);
156 case IDENTIFIER_NODE:
157 if (t == NULL)
158 pp->translate_string ("<unnamed>");
159 else if (IDENTIFIER_TYPENAME_P (t))
160 pp_cxx_conversion_function_id (pp, t);
161 else
163 if (is_destructor_name (t))
165 pp_complement (pp);
166 /* FIXME: Why is this necessary? */
167 if (TREE_TYPE (t))
168 t = constructor_name (TREE_TYPE (t));
170 pp_cxx_tree_identifier (pp, t);
172 break;
174 case TEMPLATE_ID_EXPR:
175 pp_cxx_template_id (pp, t);
176 break;
178 case BASELINK:
179 pp_cxx_unqualified_id (pp, BASELINK_FUNCTIONS (t));
180 break;
182 case RECORD_TYPE:
183 case UNION_TYPE:
184 case ENUMERAL_TYPE:
185 case TYPENAME_TYPE:
186 case UNBOUND_CLASS_TEMPLATE:
187 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
188 if (CLASS_TYPE_P (t) && CLASSTYPE_USE_TEMPLATE (t))
190 pp_cxx_begin_template_argument_list (pp);
191 pp_cxx_template_argument_list (pp, INNERMOST_TEMPLATE_ARGS
192 (CLASSTYPE_TI_ARGS (t)));
193 pp_cxx_end_template_argument_list (pp);
195 break;
197 case BIT_NOT_EXPR:
198 pp_cxx_complement (pp);
199 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0));
200 break;
202 case TEMPLATE_TYPE_PARM:
203 case TEMPLATE_TEMPLATE_PARM:
204 if (TYPE_IDENTIFIER (t))
205 pp_cxx_unqualified_id (pp, TYPE_IDENTIFIER (t));
206 else
207 pp_cxx_canonical_template_parameter (pp, t);
208 break;
210 case TEMPLATE_PARM_INDEX:
211 pp_cxx_unqualified_id (pp, TEMPLATE_PARM_DECL (t));
212 break;
214 case BOUND_TEMPLATE_TEMPLATE_PARM:
215 pp_cxx_cv_qualifier_seq (pp, t);
216 pp_cxx_unqualified_id (pp, TYPE_IDENTIFIER (t));
217 pp_cxx_begin_template_argument_list (pp);
218 pp_cxx_template_argument_list (pp, TYPE_TI_ARGS (t));
219 pp_cxx_end_template_argument_list (pp);
220 break;
222 default:
223 pp_unsupported_tree (pp, t);
224 break;
228 /* Pretty-print out the token sequence ":: template" in template codes
229 where it is needed to "inline declare" the (following) member as
230 a template. This situation arises when SCOPE of T is dependent
231 on template parameters. */
233 static inline void
234 pp_cxx_template_keyword_if_needed (cxx_pretty_printer *pp, tree scope, tree t)
236 if (TREE_CODE (t) == TEMPLATE_ID_EXPR
237 && TYPE_P (scope) && dependent_type_p (scope))
238 pp_cxx_ws_string (pp, "template");
241 /* nested-name-specifier:
242 class-or-namespace-name :: nested-name-specifier(opt)
243 class-or-namespace-name :: template nested-name-specifier */
245 static void
246 pp_cxx_nested_name_specifier (cxx_pretty_printer *pp, tree t)
248 if (!SCOPE_FILE_SCOPE_P (t) && t != pp->enclosing_scope)
250 tree scope = get_containing_scope (t);
251 pp_cxx_nested_name_specifier (pp, scope);
252 pp_cxx_template_keyword_if_needed (pp, scope, t);
253 pp_cxx_unqualified_id (pp, t);
254 pp_cxx_colon_colon (pp);
258 /* qualified-id:
259 nested-name-specifier template(opt) unqualified-id */
261 static void
262 pp_cxx_qualified_id (cxx_pretty_printer *pp, tree t)
264 switch (TREE_CODE (t))
266 /* A pointer-to-member is always qualified. */
267 case PTRMEM_CST:
268 pp_cxx_nested_name_specifier (pp, PTRMEM_CST_CLASS (t));
269 pp_cxx_unqualified_id (pp, PTRMEM_CST_MEMBER (t));
270 break;
272 /* In Standard C++, functions cannot possibly be used as
273 nested-name-specifiers. However, there are situations where
274 is "makes sense" to output the surrounding function name for the
275 purpose of emphasizing on the scope kind. Just printing the
276 function name might not be sufficient as it may be overloaded; so,
277 we decorate the function with its signature too.
278 FIXME: This is probably the wrong pretty-printing for conversion
279 functions and some function templates. */
280 case OVERLOAD:
281 t = OVL_CURRENT (t);
282 case FUNCTION_DECL:
283 if (DECL_FUNCTION_MEMBER_P (t))
284 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
285 pp_cxx_unqualified_id
286 (pp, DECL_CONSTRUCTOR_P (t) ? DECL_CONTEXT (t) : t);
287 pp_cxx_parameter_declaration_clause (pp, TREE_TYPE (t));
288 break;
290 case OFFSET_REF:
291 case SCOPE_REF:
292 pp_cxx_nested_name_specifier (pp, TREE_OPERAND (t, 0));
293 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 1));
294 break;
296 default:
298 tree scope = get_containing_scope (t);
299 if (scope != pp->enclosing_scope)
301 pp_cxx_nested_name_specifier (pp, scope);
302 pp_cxx_template_keyword_if_needed (pp, scope, t);
304 pp_cxx_unqualified_id (pp, t);
306 break;
311 void
312 cxx_pretty_printer::constant (tree t)
314 switch (TREE_CODE (t))
316 case STRING_CST:
318 const bool in_parens = PAREN_STRING_LITERAL_P (t);
319 if (in_parens)
320 pp_cxx_left_paren (this);
321 c_pretty_printer::constant (t);
322 if (in_parens)
323 pp_cxx_right_paren (this);
325 break;
327 case INTEGER_CST:
328 if (NULLPTR_TYPE_P (TREE_TYPE (t)))
330 pp_string (this, "nullptr");
331 break;
333 /* else fall through. */
335 default:
336 c_pretty_printer::constant (t);
337 break;
341 /* id-expression:
342 unqualified-id
343 qualified-id */
345 void
346 cxx_pretty_printer::id_expression (tree t)
348 if (TREE_CODE (t) == OVERLOAD)
349 t = OVL_CURRENT (t);
350 if (DECL_P (t) && DECL_CONTEXT (t))
351 pp_cxx_qualified_id (this, t);
352 else
353 pp_cxx_unqualified_id (this, t);
356 /* user-defined literal:
357 literal ud-suffix */
359 void
360 pp_cxx_userdef_literal (cxx_pretty_printer *pp, tree t)
362 pp->constant (USERDEF_LITERAL_VALUE (t));
363 pp->id_expression (USERDEF_LITERAL_SUFFIX_ID (t));
367 /* primary-expression:
368 literal
369 this
370 :: identifier
371 :: operator-function-id
372 :: qualifier-id
373 ( expression )
374 id-expression
376 GNU Extensions:
377 __builtin_va_arg ( assignment-expression , type-id )
378 __builtin_offsetof ( type-id, offsetof-expression )
380 __has_nothrow_assign ( type-id )
381 __has_nothrow_constructor ( type-id )
382 __has_nothrow_copy ( type-id )
383 __has_trivial_assign ( type-id )
384 __has_trivial_constructor ( type-id )
385 __has_trivial_copy ( type-id )
386 __has_trivial_destructor ( type-id )
387 __has_virtual_destructor ( type-id )
388 __is_abstract ( type-id )
389 __is_base_of ( type-id , type-id )
390 __is_class ( type-id )
391 __is_convertible_to ( type-id , type-id )
392 __is_empty ( type-id )
393 __is_enum ( type-id )
394 __is_literal_type ( type-id )
395 __is_pod ( type-id )
396 __is_polymorphic ( type-id )
397 __is_std_layout ( type-id )
398 __is_trivial ( type-id )
399 __is_union ( type-id ) */
401 void
402 cxx_pretty_printer::primary_expression (tree t)
404 switch (TREE_CODE (t))
406 case VOID_CST:
407 case INTEGER_CST:
408 case REAL_CST:
409 case COMPLEX_CST:
410 case STRING_CST:
411 constant (t);
412 break;
414 case USERDEF_LITERAL:
415 pp_cxx_userdef_literal (this, t);
416 break;
418 case BASELINK:
419 t = BASELINK_FUNCTIONS (t);
420 case VAR_DECL:
421 case PARM_DECL:
422 case FIELD_DECL:
423 case FUNCTION_DECL:
424 case OVERLOAD:
425 case CONST_DECL:
426 case TEMPLATE_DECL:
427 id_expression (t);
428 break;
430 case RESULT_DECL:
431 case TEMPLATE_TYPE_PARM:
432 case TEMPLATE_TEMPLATE_PARM:
433 case TEMPLATE_PARM_INDEX:
434 pp_cxx_unqualified_id (this, t);
435 break;
437 case STMT_EXPR:
438 pp_cxx_left_paren (this);
439 statement (STMT_EXPR_STMT (t));
440 pp_cxx_right_paren (this);
441 break;
443 case TRAIT_EXPR:
444 pp_cxx_trait_expression (this, t);
445 break;
447 case VA_ARG_EXPR:
448 pp_cxx_va_arg_expression (this, t);
449 break;
451 case OFFSETOF_EXPR:
452 pp_cxx_offsetof_expression (this, t);
453 break;
455 default:
456 c_pretty_printer::primary_expression (t);
457 break;
461 /* postfix-expression:
462 primary-expression
463 postfix-expression [ expression ]
464 postfix-expression ( expression-list(opt) )
465 simple-type-specifier ( expression-list(opt) )
466 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
467 typename ::(opt) nested-name-specifier template(opt)
468 template-id ( expression-list(opt) )
469 postfix-expression . template(opt) ::(opt) id-expression
470 postfix-expression -> template(opt) ::(opt) id-expression
471 postfix-expression . pseudo-destructor-name
472 postfix-expression -> pseudo-destructor-name
473 postfix-expression ++
474 postfix-expression --
475 dynamic_cast < type-id > ( expression )
476 static_cast < type-id > ( expression )
477 reinterpret_cast < type-id > ( expression )
478 const_cast < type-id > ( expression )
479 typeid ( expression )
480 typeid ( type-id ) */
482 void
483 cxx_pretty_printer::postfix_expression (tree t)
485 enum tree_code code = TREE_CODE (t);
487 switch (code)
489 case AGGR_INIT_EXPR:
490 case CALL_EXPR:
492 tree fun = (code == AGGR_INIT_EXPR ? AGGR_INIT_EXPR_FN (t)
493 : CALL_EXPR_FN (t));
494 tree saved_scope = enclosing_scope;
495 bool skipfirst = false;
496 tree arg;
498 if (TREE_CODE (fun) == ADDR_EXPR)
499 fun = TREE_OPERAND (fun, 0);
501 /* In templates, where there is no way to tell whether a given
502 call uses an actual member function. So the parser builds
503 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
504 instantiation time. */
505 if (TREE_CODE (fun) != FUNCTION_DECL)
507 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun))
509 tree object = (code == AGGR_INIT_EXPR
510 ? (AGGR_INIT_VIA_CTOR_P (t)
511 ? AGGR_INIT_EXPR_SLOT (t)
512 : AGGR_INIT_EXPR_ARG (t, 0))
513 : CALL_EXPR_ARG (t, 0));
515 while (TREE_CODE (object) == NOP_EXPR)
516 object = TREE_OPERAND (object, 0);
518 if (TREE_CODE (object) == ADDR_EXPR)
519 object = TREE_OPERAND (object, 0);
521 if (!TYPE_PTR_P (TREE_TYPE (object)))
523 postfix_expression (object);
524 pp_cxx_dot (this);
526 else
528 postfix_expression (object);
529 pp_cxx_arrow (this);
531 skipfirst = true;
532 enclosing_scope = strip_pointer_operator (TREE_TYPE (object));
535 postfix_expression (fun);
536 enclosing_scope = saved_scope;
537 pp_cxx_left_paren (this);
538 if (code == AGGR_INIT_EXPR)
540 aggr_init_expr_arg_iterator iter;
541 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
543 if (skipfirst)
544 skipfirst = false;
545 else
547 expression (arg);
548 if (more_aggr_init_expr_args_p (&iter))
549 pp_cxx_separate_with (this, ',');
553 else
555 call_expr_arg_iterator iter;
556 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
558 if (skipfirst)
559 skipfirst = false;
560 else
562 expression (arg);
563 if (more_call_expr_args_p (&iter))
564 pp_cxx_separate_with (this, ',');
568 pp_cxx_right_paren (this);
570 if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t))
572 pp_cxx_separate_with (this, ',');
573 postfix_expression (AGGR_INIT_EXPR_SLOT (t));
575 break;
577 case BASELINK:
578 case VAR_DECL:
579 case PARM_DECL:
580 case FIELD_DECL:
581 case FUNCTION_DECL:
582 case OVERLOAD:
583 case CONST_DECL:
584 case TEMPLATE_DECL:
585 case RESULT_DECL:
586 primary_expression (t);
587 break;
589 case DYNAMIC_CAST_EXPR:
590 case STATIC_CAST_EXPR:
591 case REINTERPRET_CAST_EXPR:
592 case CONST_CAST_EXPR:
593 if (code == DYNAMIC_CAST_EXPR)
594 pp_cxx_ws_string (this, "dynamic_cast");
595 else if (code == STATIC_CAST_EXPR)
596 pp_cxx_ws_string (this, "static_cast");
597 else if (code == REINTERPRET_CAST_EXPR)
598 pp_cxx_ws_string (this, "reinterpret_cast");
599 else
600 pp_cxx_ws_string (this, "const_cast");
601 pp_cxx_begin_template_argument_list (this);
602 type_id (TREE_TYPE (t));
603 pp_cxx_end_template_argument_list (this);
604 pp_left_paren (this);
605 expression (TREE_OPERAND (t, 0));
606 pp_right_paren (this);
607 break;
609 case EMPTY_CLASS_EXPR:
610 type_id (TREE_TYPE (t));
611 pp_left_paren (this);
612 pp_right_paren (this);
613 break;
615 case TYPEID_EXPR:
616 pp_cxx_typeid_expression (this, t);
617 break;
619 case PSEUDO_DTOR_EXPR:
620 postfix_expression (TREE_OPERAND (t, 0));
621 pp_cxx_dot (this);
622 if (TREE_OPERAND (t, 1))
624 pp_cxx_qualified_id (this, TREE_OPERAND (t, 1));
625 pp_cxx_colon_colon (this);
627 pp_complement (this);
628 pp_cxx_unqualified_id (this, TREE_OPERAND (t, 2));
629 break;
631 case ARROW_EXPR:
632 postfix_expression (TREE_OPERAND (t, 0));
633 pp_cxx_arrow (this);
634 break;
636 default:
637 c_pretty_printer::postfix_expression (t);
638 break;
642 /* new-expression:
643 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
644 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
646 new-placement:
647 ( expression-list )
649 new-type-id:
650 type-specifier-seq new-declarator(opt)
652 new-declarator:
653 ptr-operator new-declarator(opt)
654 direct-new-declarator
656 direct-new-declarator
657 [ expression ]
658 direct-new-declarator [ constant-expression ]
660 new-initializer:
661 ( expression-list(opt) ) */
663 static void
664 pp_cxx_new_expression (cxx_pretty_printer *pp, tree t)
666 enum tree_code code = TREE_CODE (t);
667 tree type = TREE_OPERAND (t, 1);
668 tree init = TREE_OPERAND (t, 2);
669 switch (code)
671 case NEW_EXPR:
672 case VEC_NEW_EXPR:
673 if (NEW_EXPR_USE_GLOBAL (t))
674 pp_cxx_colon_colon (pp);
675 pp_cxx_ws_string (pp, "new");
676 if (TREE_OPERAND (t, 0))
678 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
679 pp_space (pp);
681 if (TREE_CODE (type) == ARRAY_REF)
682 type = build_cplus_array_type
683 (TREE_OPERAND (type, 0),
684 build_index_type (fold_build2_loc (input_location,
685 MINUS_EXPR, integer_type_node,
686 TREE_OPERAND (type, 1),
687 integer_one_node)));
688 pp->type_id (type);
689 if (init)
691 pp_left_paren (pp);
692 if (TREE_CODE (init) == TREE_LIST)
693 pp_c_expression_list (pp, init);
694 else if (init == void_node)
695 ; /* OK, empty initializer list. */
696 else
697 pp->expression (init);
698 pp_right_paren (pp);
700 break;
702 default:
703 pp_unsupported_tree (pp, t);
707 /* delete-expression:
708 ::(opt) delete cast-expression
709 ::(opt) delete [ ] cast-expression */
711 static void
712 pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t)
714 enum tree_code code = TREE_CODE (t);
715 switch (code)
717 case DELETE_EXPR:
718 case VEC_DELETE_EXPR:
719 if (DELETE_EXPR_USE_GLOBAL (t))
720 pp_cxx_colon_colon (pp);
721 pp_cxx_ws_string (pp, "delete");
722 pp_space (pp);
723 if (code == VEC_DELETE_EXPR
724 || DELETE_EXPR_USE_VEC (t))
726 pp_left_bracket (pp);
727 pp_right_bracket (pp);
728 pp_space (pp);
730 pp_c_cast_expression (pp, TREE_OPERAND (t, 0));
731 break;
733 default:
734 pp_unsupported_tree (pp, t);
738 /* unary-expression:
739 postfix-expression
740 ++ cast-expression
741 -- cast-expression
742 unary-operator cast-expression
743 sizeof unary-expression
744 sizeof ( type-id )
745 sizeof ... ( identifier )
746 new-expression
747 delete-expression
749 unary-operator: one of
750 * & + - !
752 GNU extensions:
753 __alignof__ unary-expression
754 __alignof__ ( type-id ) */
756 void
757 cxx_pretty_printer::unary_expression (tree t)
759 enum tree_code code = TREE_CODE (t);
760 switch (code)
762 case NEW_EXPR:
763 case VEC_NEW_EXPR:
764 pp_cxx_new_expression (this, t);
765 break;
767 case DELETE_EXPR:
768 case VEC_DELETE_EXPR:
769 pp_cxx_delete_expression (this, t);
770 break;
772 case SIZEOF_EXPR:
773 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
775 pp_cxx_ws_string (this, "sizeof");
776 pp_cxx_ws_string (this, "...");
777 pp_cxx_whitespace (this);
778 pp_cxx_left_paren (this);
779 if (TYPE_P (TREE_OPERAND (t, 0)))
780 type_id (TREE_OPERAND (t, 0));
781 else
782 unary_expression (TREE_OPERAND (t, 0));
783 pp_cxx_right_paren (this);
784 break;
786 /* Fall through */
788 case ALIGNOF_EXPR:
789 pp_cxx_ws_string (this, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
790 pp_cxx_whitespace (this);
791 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
793 pp_cxx_left_paren (this);
794 type_id (TREE_TYPE (TREE_OPERAND (t, 0)));
795 pp_cxx_right_paren (this);
797 else if (TYPE_P (TREE_OPERAND (t, 0)))
799 pp_cxx_left_paren (this);
800 type_id (TREE_OPERAND (t, 0));
801 pp_cxx_right_paren (this);
803 else
804 unary_expression (TREE_OPERAND (t, 0));
805 break;
807 case AT_ENCODE_EXPR:
808 pp_cxx_ws_string (this, "@encode");
809 pp_cxx_whitespace (this);
810 pp_cxx_left_paren (this);
811 type_id (TREE_OPERAND (t, 0));
812 pp_cxx_right_paren (this);
813 break;
815 case NOEXCEPT_EXPR:
816 pp_cxx_ws_string (this, "noexcept");
817 pp_cxx_whitespace (this);
818 pp_cxx_left_paren (this);
819 expression (TREE_OPERAND (t, 0));
820 pp_cxx_right_paren (this);
821 break;
823 case UNARY_PLUS_EXPR:
824 pp_plus (this);
825 pp_cxx_cast_expression (this, TREE_OPERAND (t, 0));
826 break;
828 default:
829 c_pretty_printer::unary_expression (t);
830 break;
834 /* cast-expression:
835 unary-expression
836 ( type-id ) cast-expression */
838 static void
839 pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t)
841 switch (TREE_CODE (t))
843 case CAST_EXPR:
844 case IMPLICIT_CONV_EXPR:
845 pp->type_id (TREE_TYPE (t));
846 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
847 break;
849 default:
850 pp_c_cast_expression (pp, t);
851 break;
855 /* pm-expression:
856 cast-expression
857 pm-expression .* cast-expression
858 pm-expression ->* cast-expression */
860 static void
861 pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t)
863 switch (TREE_CODE (t))
865 /* Handle unfortunate OFFSET_REF overloading here. */
866 case OFFSET_REF:
867 if (TYPE_P (TREE_OPERAND (t, 0)))
869 pp_cxx_qualified_id (pp, t);
870 break;
872 /* Else fall through. */
873 case MEMBER_REF:
874 case DOTSTAR_EXPR:
875 pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0));
876 if (TREE_CODE (t) == MEMBER_REF)
877 pp_cxx_arrow (pp);
878 else
879 pp_cxx_dot (pp);
880 pp_star(pp);
881 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1));
882 break;
885 default:
886 pp_cxx_cast_expression (pp, t);
887 break;
891 /* multiplicative-expression:
892 pm-expression
893 multiplicative-expression * pm-expression
894 multiplicative-expression / pm-expression
895 multiplicative-expression % pm-expression */
897 void
898 cxx_pretty_printer::multiplicative_expression (tree e)
900 enum tree_code code = TREE_CODE (e);
901 switch (code)
903 case MULT_EXPR:
904 case TRUNC_DIV_EXPR:
905 case TRUNC_MOD_EXPR:
906 multiplicative_expression (TREE_OPERAND (e, 0));
907 pp_space (this);
908 if (code == MULT_EXPR)
909 pp_star (this);
910 else if (code == TRUNC_DIV_EXPR)
911 pp_slash (this);
912 else
913 pp_modulo (this);
914 pp_space (this);
915 pp_cxx_pm_expression (this, TREE_OPERAND (e, 1));
916 break;
918 default:
919 pp_cxx_pm_expression (this, e);
920 break;
924 /* conditional-expression:
925 logical-or-expression
926 logical-or-expression ? expression : assignment-expression */
928 void
929 cxx_pretty_printer::conditional_expression (tree e)
931 if (TREE_CODE (e) == COND_EXPR)
933 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
934 pp_space (this);
935 pp_question (this);
936 pp_space (this);
937 expression (TREE_OPERAND (e, 1));
938 pp_space (this);
939 assignment_expression (TREE_OPERAND (e, 2));
941 else
942 pp_c_logical_or_expression (this, e);
945 /* Pretty-print a compound assignment operator token as indicated by T. */
947 static void
948 pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t)
950 const char *op;
952 switch (TREE_CODE (t))
954 case NOP_EXPR:
955 op = "=";
956 break;
958 case PLUS_EXPR:
959 op = "+=";
960 break;
962 case MINUS_EXPR:
963 op = "-=";
964 break;
966 case TRUNC_DIV_EXPR:
967 op = "/=";
968 break;
970 case TRUNC_MOD_EXPR:
971 op = "%=";
972 break;
974 default:
975 op = get_tree_code_name (TREE_CODE (t));
976 break;
979 pp_cxx_ws_string (pp, op);
983 /* assignment-expression:
984 conditional-expression
985 logical-or-expression assignment-operator assignment-expression
986 throw-expression
988 throw-expression:
989 throw assignment-expression(opt)
991 assignment-operator: one of
992 = *= /= %= += -= >>= <<= &= ^= |= */
994 void
995 cxx_pretty_printer::assignment_expression (tree e)
997 switch (TREE_CODE (e))
999 case MODIFY_EXPR:
1000 case INIT_EXPR:
1001 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
1002 pp_space (this);
1003 pp_equal (this);
1004 pp_space (this);
1005 assignment_expression (TREE_OPERAND (e, 1));
1006 break;
1008 case THROW_EXPR:
1009 pp_cxx_ws_string (this, "throw");
1010 if (TREE_OPERAND (e, 0))
1011 assignment_expression (TREE_OPERAND (e, 0));
1012 break;
1014 case MODOP_EXPR:
1015 pp_c_logical_or_expression (this, TREE_OPERAND (e, 0));
1016 pp_cxx_assignment_operator (this, TREE_OPERAND (e, 1));
1017 assignment_expression (TREE_OPERAND (e, 2));
1018 break;
1020 default:
1021 conditional_expression (e);
1022 break;
1026 void
1027 cxx_pretty_printer::expression (tree t)
1029 switch (TREE_CODE (t))
1031 case STRING_CST:
1032 case VOID_CST:
1033 case INTEGER_CST:
1034 case REAL_CST:
1035 case COMPLEX_CST:
1036 constant (t);
1037 break;
1039 case USERDEF_LITERAL:
1040 pp_cxx_userdef_literal (this, t);
1041 break;
1043 case RESULT_DECL:
1044 pp_cxx_unqualified_id (this, t);
1045 break;
1047 #if 0
1048 case OFFSET_REF:
1049 #endif
1050 case SCOPE_REF:
1051 case PTRMEM_CST:
1052 pp_cxx_qualified_id (this, t);
1053 break;
1055 case OVERLOAD:
1056 t = OVL_CURRENT (t);
1057 case VAR_DECL:
1058 case PARM_DECL:
1059 case FIELD_DECL:
1060 case CONST_DECL:
1061 case FUNCTION_DECL:
1062 case BASELINK:
1063 case TEMPLATE_DECL:
1064 case TEMPLATE_TYPE_PARM:
1065 case TEMPLATE_PARM_INDEX:
1066 case TEMPLATE_TEMPLATE_PARM:
1067 case STMT_EXPR:
1068 primary_expression (t);
1069 break;
1071 case CALL_EXPR:
1072 case DYNAMIC_CAST_EXPR:
1073 case STATIC_CAST_EXPR:
1074 case REINTERPRET_CAST_EXPR:
1075 case CONST_CAST_EXPR:
1076 #if 0
1077 case MEMBER_REF:
1078 #endif
1079 case EMPTY_CLASS_EXPR:
1080 case TYPEID_EXPR:
1081 case PSEUDO_DTOR_EXPR:
1082 case AGGR_INIT_EXPR:
1083 case ARROW_EXPR:
1084 postfix_expression (t);
1085 break;
1087 case NEW_EXPR:
1088 case VEC_NEW_EXPR:
1089 pp_cxx_new_expression (this, t);
1090 break;
1092 case DELETE_EXPR:
1093 case VEC_DELETE_EXPR:
1094 pp_cxx_delete_expression (this, t);
1095 break;
1097 case SIZEOF_EXPR:
1098 case ALIGNOF_EXPR:
1099 case NOEXCEPT_EXPR:
1100 unary_expression (t);
1101 break;
1103 case CAST_EXPR:
1104 case IMPLICIT_CONV_EXPR:
1105 pp_cxx_cast_expression (this, t);
1106 break;
1108 case OFFSET_REF:
1109 case MEMBER_REF:
1110 case DOTSTAR_EXPR:
1111 pp_cxx_pm_expression (this, t);
1112 break;
1114 case MULT_EXPR:
1115 case TRUNC_DIV_EXPR:
1116 case TRUNC_MOD_EXPR:
1117 multiplicative_expression (t);
1118 break;
1120 case COND_EXPR:
1121 conditional_expression (t);
1122 break;
1124 case MODIFY_EXPR:
1125 case INIT_EXPR:
1126 case THROW_EXPR:
1127 case MODOP_EXPR:
1128 assignment_expression (t);
1129 break;
1131 case NON_DEPENDENT_EXPR:
1132 case MUST_NOT_THROW_EXPR:
1133 expression (TREE_OPERAND (t, 0));
1134 break;
1136 case EXPR_PACK_EXPANSION:
1137 expression (PACK_EXPANSION_PATTERN (t));
1138 pp_cxx_ws_string (this, "...");
1139 break;
1141 case TEMPLATE_ID_EXPR:
1142 pp_cxx_template_id (this, t);
1143 break;
1145 case NONTYPE_ARGUMENT_PACK:
1147 tree args = ARGUMENT_PACK_ARGS (t);
1148 int i, len = TREE_VEC_LENGTH (args);
1149 for (i = 0; i < len; ++i)
1151 if (i > 0)
1152 pp_cxx_separate_with (this, ',');
1153 expression (TREE_VEC_ELT (args, i));
1156 break;
1158 case LAMBDA_EXPR:
1159 pp_cxx_ws_string (this, "<lambda>");
1160 break;
1162 case PAREN_EXPR:
1163 pp_cxx_left_paren (this);
1164 expression (TREE_OPERAND (t, 0));
1165 pp_cxx_right_paren (this);
1166 break;
1168 default:
1169 c_pretty_printer::expression (t);
1170 break;
1175 /* Declarations. */
1177 /* function-specifier:
1178 inline
1179 virtual
1180 explicit */
1182 void
1183 cxx_pretty_printer::function_specifier (tree t)
1185 switch (TREE_CODE (t))
1187 case FUNCTION_DECL:
1188 if (DECL_VIRTUAL_P (t))
1189 pp_cxx_ws_string (this, "virtual");
1190 else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t))
1191 pp_cxx_ws_string (this, "explicit");
1192 else
1193 c_pretty_printer::function_specifier (t);
1195 default:
1196 break;
1200 /* decl-specifier-seq:
1201 decl-specifier-seq(opt) decl-specifier
1203 decl-specifier:
1204 storage-class-specifier
1205 type-specifier
1206 function-specifier
1207 friend
1208 typedef */
1210 void
1211 cxx_pretty_printer::declaration_specifiers (tree t)
1213 switch (TREE_CODE (t))
1215 case VAR_DECL:
1216 case PARM_DECL:
1217 case CONST_DECL:
1218 case FIELD_DECL:
1219 storage_class_specifier (t);
1220 declaration_specifiers (TREE_TYPE (t));
1221 break;
1223 case TYPE_DECL:
1224 pp_cxx_ws_string (this, "typedef");
1225 declaration_specifiers (TREE_TYPE (t));
1226 break;
1228 case FUNCTION_DECL:
1229 /* Constructors don't have return types. And conversion functions
1230 do not have a type-specifier in their return types. */
1231 if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t))
1232 function_specifier (t);
1233 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1234 declaration_specifiers (TREE_TYPE (TREE_TYPE (t)));
1235 else
1236 default:
1237 c_pretty_printer::declaration_specifiers (t);
1238 break;
1242 /* simple-type-specifier:
1243 ::(opt) nested-name-specifier(opt) type-name
1244 ::(opt) nested-name-specifier(opt) template(opt) template-id
1245 char
1246 wchar_t
1247 bool
1248 short
1250 long
1251 signed
1252 unsigned
1253 float
1254 double
1255 void */
1257 void
1258 cxx_pretty_printer::simple_type_specifier (tree t)
1260 switch (TREE_CODE (t))
1262 case RECORD_TYPE:
1263 case UNION_TYPE:
1264 case ENUMERAL_TYPE:
1265 pp_cxx_qualified_id (this, t);
1266 break;
1268 case TEMPLATE_TYPE_PARM:
1269 case TEMPLATE_TEMPLATE_PARM:
1270 case TEMPLATE_PARM_INDEX:
1271 case BOUND_TEMPLATE_TEMPLATE_PARM:
1272 pp_cxx_unqualified_id (this, t);
1273 break;
1275 case TYPENAME_TYPE:
1276 pp_cxx_ws_string (this, "typename");
1277 pp_cxx_nested_name_specifier (this, TYPE_CONTEXT (t));
1278 pp_cxx_unqualified_id (this, TYPE_NAME (t));
1279 break;
1281 default:
1282 c_pretty_printer::simple_type_specifier (t);
1283 break;
1287 /* type-specifier-seq:
1288 type-specifier type-specifier-seq(opt)
1290 type-specifier:
1291 simple-type-specifier
1292 class-specifier
1293 enum-specifier
1294 elaborated-type-specifier
1295 cv-qualifier */
1297 static void
1298 pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t)
1300 switch (TREE_CODE (t))
1302 case TEMPLATE_DECL:
1303 case TEMPLATE_TYPE_PARM:
1304 case TEMPLATE_TEMPLATE_PARM:
1305 case TYPE_DECL:
1306 case BOUND_TEMPLATE_TEMPLATE_PARM:
1307 pp_cxx_cv_qualifier_seq (pp, t);
1308 pp->simple_type_specifier (t);
1309 break;
1311 case METHOD_TYPE:
1312 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
1313 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1314 pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t));
1315 break;
1317 case DECLTYPE_TYPE:
1318 pp_cxx_ws_string (pp, "decltype");
1319 pp_cxx_left_paren (pp);
1320 pp->expression (DECLTYPE_TYPE_EXPR (t));
1321 pp_cxx_right_paren (pp);
1322 break;
1324 case RECORD_TYPE:
1325 if (TYPE_PTRMEMFUNC_P (t))
1327 tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t);
1328 pp->declaration_specifiers (TREE_TYPE (TREE_TYPE (pfm)));
1329 pp_cxx_whitespace (pp);
1330 pp_cxx_ptr_operator (pp, t);
1331 break;
1333 /* else fall through */
1335 default:
1336 if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t)))
1337 pp_c_specifier_qualifier_list (pp, t);
1341 /* ptr-operator:
1342 * cv-qualifier-seq(opt)
1344 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1346 static void
1347 pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t)
1349 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
1350 t = TREE_TYPE (t);
1351 switch (TREE_CODE (t))
1353 case REFERENCE_TYPE:
1354 case POINTER_TYPE:
1355 if (TYPE_PTR_OR_PTRMEM_P (TREE_TYPE (t)))
1356 pp_cxx_ptr_operator (pp, TREE_TYPE (t));
1357 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (TREE_TYPE (t)));
1358 if (TYPE_PTR_P (t))
1360 pp_star (pp);
1361 pp_cxx_cv_qualifier_seq (pp, t);
1363 else
1364 pp_ampersand (pp);
1365 break;
1367 case RECORD_TYPE:
1368 if (TYPE_PTRMEMFUNC_P (t))
1370 pp_cxx_left_paren (pp);
1371 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t));
1372 pp_star (pp);
1373 break;
1375 case OFFSET_TYPE:
1376 if (TYPE_PTRMEM_P (t))
1378 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
1379 pp_cxx_left_paren (pp);
1380 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t));
1381 pp_star (pp);
1382 pp_cxx_cv_qualifier_seq (pp, t);
1383 break;
1385 /* else fall through. */
1387 default:
1388 pp_unsupported_tree (pp, t);
1389 break;
1393 static inline tree
1394 pp_cxx_implicit_parameter_type (tree mf)
1396 return class_of_this_parm (TREE_TYPE (mf));
1400 parameter-declaration:
1401 decl-specifier-seq declarator
1402 decl-specifier-seq declarator = assignment-expression
1403 decl-specifier-seq abstract-declarator(opt)
1404 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1406 static inline void
1407 pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t)
1409 pp->declaration_specifiers (t);
1410 if (TYPE_P (t))
1411 pp->abstract_declarator (t);
1412 else
1413 pp->declarator (t);
1416 /* parameter-declaration-clause:
1417 parameter-declaration-list(opt) ...(opt)
1418 parameter-declaration-list , ...
1420 parameter-declaration-list:
1421 parameter-declaration
1422 parameter-declaration-list , parameter-declaration */
1424 static void
1425 pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t)
1427 tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t);
1428 tree types =
1429 TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t);
1430 const bool abstract = args == NULL || pp->flags & pp_c_flag_abstract;
1431 bool first = true;
1433 /* Skip artificial parameter for nonstatic member functions. */
1434 if (TREE_CODE (t) == METHOD_TYPE)
1435 types = TREE_CHAIN (types);
1437 pp_cxx_left_paren (pp);
1438 for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types))
1440 if (!first)
1441 pp_cxx_separate_with (pp, ',');
1442 first = false;
1443 pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args);
1444 if (!abstract && pp->flags & pp_cxx_flag_default_argument)
1446 pp_cxx_whitespace (pp);
1447 pp_equal (pp);
1448 pp_cxx_whitespace (pp);
1449 pp->assignment_expression (TREE_PURPOSE (types));
1452 pp_cxx_right_paren (pp);
1455 /* exception-specification:
1456 throw ( type-id-list(opt) )
1458 type-id-list
1459 type-id
1460 type-id-list , type-id */
1462 static void
1463 pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t)
1465 tree ex_spec = TYPE_RAISES_EXCEPTIONS (t);
1466 bool need_comma = false;
1468 if (ex_spec == NULL)
1469 return;
1470 if (TREE_PURPOSE (ex_spec))
1472 pp_cxx_ws_string (pp, "noexcept");
1473 pp_cxx_whitespace (pp);
1474 pp_cxx_left_paren (pp);
1475 if (DEFERRED_NOEXCEPT_SPEC_P (ex_spec))
1476 pp_cxx_ws_string (pp, "<uninstantiated>");
1477 else
1478 pp->expression (TREE_PURPOSE (ex_spec));
1479 pp_cxx_right_paren (pp);
1480 return;
1482 pp_cxx_ws_string (pp, "throw");
1483 pp_cxx_left_paren (pp);
1484 for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec))
1486 tree type = TREE_VALUE (ex_spec);
1487 tree argpack = NULL_TREE;
1488 int i, len = 1;
1490 if (ARGUMENT_PACK_P (type))
1492 argpack = ARGUMENT_PACK_ARGS (type);
1493 len = TREE_VEC_LENGTH (argpack);
1496 for (i = 0; i < len; ++i)
1498 if (argpack)
1499 type = TREE_VEC_ELT (argpack, i);
1501 if (need_comma)
1502 pp_cxx_separate_with (pp, ',');
1503 else
1504 need_comma = true;
1506 pp->type_id (type);
1509 pp_cxx_right_paren (pp);
1512 /* direct-declarator:
1513 declarator-id
1514 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1515 exception-specification(opt)
1516 direct-declaration [ constant-expression(opt) ]
1517 ( declarator ) */
1519 void
1520 cxx_pretty_printer::direct_declarator (tree t)
1522 switch (TREE_CODE (t))
1524 case VAR_DECL:
1525 case PARM_DECL:
1526 case CONST_DECL:
1527 case FIELD_DECL:
1528 if (DECL_NAME (t))
1530 pp_cxx_space_for_pointer_operator (this, TREE_TYPE (t));
1532 if ((TREE_CODE (t) == PARM_DECL && DECL_PACK_P (t))
1533 || template_parameter_pack_p (t))
1534 /* A function parameter pack or non-type template
1535 parameter pack. */
1536 pp_cxx_ws_string (this, "...");
1538 id_expression (DECL_NAME (t));
1540 abstract_declarator (TREE_TYPE (t));
1541 break;
1543 case FUNCTION_DECL:
1544 pp_cxx_space_for_pointer_operator (this, TREE_TYPE (TREE_TYPE (t)));
1545 expression (t);
1546 pp_cxx_parameter_declaration_clause (this, t);
1548 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1550 padding = pp_before;
1551 pp_cxx_cv_qualifier_seq (this, pp_cxx_implicit_parameter_type (t));
1554 pp_cxx_exception_specification (this, TREE_TYPE (t));
1555 break;
1557 case TYPENAME_TYPE:
1558 case TEMPLATE_DECL:
1559 case TEMPLATE_TYPE_PARM:
1560 case TEMPLATE_PARM_INDEX:
1561 case TEMPLATE_TEMPLATE_PARM:
1562 break;
1564 default:
1565 c_pretty_printer::direct_declarator (t);
1566 break;
1570 /* declarator:
1571 direct-declarator
1572 ptr-operator declarator */
1574 void
1575 cxx_pretty_printer::declarator (tree t)
1577 direct_declarator (t);
1580 /* ctor-initializer:
1581 : mem-initializer-list
1583 mem-initializer-list:
1584 mem-initializer
1585 mem-initializer , mem-initializer-list
1587 mem-initializer:
1588 mem-initializer-id ( expression-list(opt) )
1590 mem-initializer-id:
1591 ::(opt) nested-name-specifier(opt) class-name
1592 identifier */
1594 static void
1595 pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t)
1597 t = TREE_OPERAND (t, 0);
1598 pp_cxx_whitespace (pp);
1599 pp_colon (pp);
1600 pp_cxx_whitespace (pp);
1601 for (; t; t = TREE_CHAIN (t))
1603 tree purpose = TREE_PURPOSE (t);
1604 bool is_pack = PACK_EXPANSION_P (purpose);
1606 if (is_pack)
1607 pp->primary_expression (PACK_EXPANSION_PATTERN (purpose));
1608 else
1609 pp->primary_expression (purpose);
1610 pp_cxx_call_argument_list (pp, TREE_VALUE (t));
1611 if (is_pack)
1612 pp_cxx_ws_string (pp, "...");
1613 if (TREE_CHAIN (t))
1614 pp_cxx_separate_with (pp, ',');
1618 /* function-definition:
1619 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1620 decl-specifier-seq(opt) declarator function-try-block */
1622 static void
1623 pp_cxx_function_definition (cxx_pretty_printer *pp, tree t)
1625 tree saved_scope = pp->enclosing_scope;
1626 pp->declaration_specifiers (t);
1627 pp->declarator (t);
1628 pp_needs_newline (pp) = true;
1629 pp->enclosing_scope = DECL_CONTEXT (t);
1630 if (DECL_SAVED_TREE (t))
1631 pp->statement (DECL_SAVED_TREE (t));
1632 else
1633 pp_cxx_semicolon (pp);
1634 pp_newline_and_flush (pp);
1635 pp->enclosing_scope = saved_scope;
1638 /* abstract-declarator:
1639 ptr-operator abstract-declarator(opt)
1640 direct-abstract-declarator */
1642 void
1643 cxx_pretty_printer::abstract_declarator (tree t)
1645 if (TYPE_PTRMEM_P (t))
1646 pp_cxx_right_paren (this);
1647 else if (POINTER_TYPE_P (t))
1649 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1650 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1651 pp_cxx_right_paren (this);
1652 t = TREE_TYPE (t);
1654 direct_abstract_declarator (t);
1657 /* direct-abstract-declarator:
1658 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1659 cv-qualifier-seq(opt) exception-specification(opt)
1660 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1661 ( abstract-declarator ) */
1663 void
1664 cxx_pretty_printer::direct_abstract_declarator (tree t)
1666 switch (TREE_CODE (t))
1668 case REFERENCE_TYPE:
1669 abstract_declarator (t);
1670 break;
1672 case RECORD_TYPE:
1673 if (TYPE_PTRMEMFUNC_P (t))
1674 direct_abstract_declarator (TYPE_PTRMEMFUNC_FN_TYPE (t));
1675 break;
1677 case METHOD_TYPE:
1678 case FUNCTION_TYPE:
1679 pp_cxx_parameter_declaration_clause (this, t);
1680 direct_abstract_declarator (TREE_TYPE (t));
1681 if (TREE_CODE (t) == METHOD_TYPE)
1683 padding = pp_before;
1684 pp_cxx_cv_qualifier_seq (this, class_of_this_parm (t));
1686 pp_cxx_exception_specification (this, t);
1687 break;
1689 case TYPENAME_TYPE:
1690 case TEMPLATE_TYPE_PARM:
1691 case TEMPLATE_TEMPLATE_PARM:
1692 case BOUND_TEMPLATE_TEMPLATE_PARM:
1693 case UNBOUND_CLASS_TEMPLATE:
1694 break;
1696 default:
1697 c_pretty_printer::direct_abstract_declarator (t);
1698 break;
1702 /* type-id:
1703 type-specifier-seq abstract-declarator(opt) */
1705 void
1706 cxx_pretty_printer::type_id (tree t)
1708 pp_flags saved_flags = flags;
1709 flags |= pp_c_flag_abstract;
1711 switch (TREE_CODE (t))
1713 case TYPE_DECL:
1714 case UNION_TYPE:
1715 case RECORD_TYPE:
1716 case ENUMERAL_TYPE:
1717 case TYPENAME_TYPE:
1718 case BOUND_TEMPLATE_TEMPLATE_PARM:
1719 case UNBOUND_CLASS_TEMPLATE:
1720 case TEMPLATE_TEMPLATE_PARM:
1721 case TEMPLATE_TYPE_PARM:
1722 case TEMPLATE_PARM_INDEX:
1723 case TEMPLATE_DECL:
1724 case TYPEOF_TYPE:
1725 case UNDERLYING_TYPE:
1726 case DECLTYPE_TYPE:
1727 case TEMPLATE_ID_EXPR:
1728 pp_cxx_type_specifier_seq (this, t);
1729 break;
1731 case TYPE_PACK_EXPANSION:
1732 type_id (PACK_EXPANSION_PATTERN (t));
1733 pp_cxx_ws_string (this, "...");
1734 break;
1736 default:
1737 c_pretty_printer::type_id (t);
1738 break;
1741 flags = saved_flags;
1744 /* template-argument-list:
1745 template-argument ...(opt)
1746 template-argument-list, template-argument ...(opt)
1748 template-argument:
1749 assignment-expression
1750 type-id
1751 template-name */
1753 static void
1754 pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t)
1756 int i;
1757 bool need_comma = false;
1759 if (t == NULL)
1760 return;
1761 for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
1763 tree arg = TREE_VEC_ELT (t, i);
1764 tree argpack = NULL_TREE;
1765 int idx, len = 1;
1767 if (ARGUMENT_PACK_P (arg))
1769 argpack = ARGUMENT_PACK_ARGS (arg);
1770 len = TREE_VEC_LENGTH (argpack);
1773 for (idx = 0; idx < len; idx++)
1775 if (argpack)
1776 arg = TREE_VEC_ELT (argpack, idx);
1778 if (need_comma)
1779 pp_cxx_separate_with (pp, ',');
1780 else
1781 need_comma = true;
1783 if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL
1784 && TYPE_P (DECL_TEMPLATE_RESULT (arg))))
1785 pp->type_id (arg);
1786 else
1787 pp->expression (arg);
1793 static void
1794 pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t)
1796 t = DECL_EXPR_DECL (t);
1797 pp_cxx_type_specifier_seq (pp, t);
1798 if (TYPE_P (t))
1799 pp->abstract_declarator (t);
1800 else
1801 pp->declarator (t);
1804 /* Statements. */
1806 void
1807 cxx_pretty_printer::statement (tree t)
1809 switch (TREE_CODE (t))
1811 case CTOR_INITIALIZER:
1812 pp_cxx_ctor_initializer (this, t);
1813 break;
1815 case USING_STMT:
1816 pp_cxx_ws_string (this, "using");
1817 pp_cxx_ws_string (this, "namespace");
1818 if (DECL_CONTEXT (t))
1819 pp_cxx_nested_name_specifier (this, DECL_CONTEXT (t));
1820 pp_cxx_qualified_id (this, USING_STMT_NAMESPACE (t));
1821 break;
1823 case USING_DECL:
1824 pp_cxx_ws_string (this, "using");
1825 pp_cxx_nested_name_specifier (this, USING_DECL_SCOPE (t));
1826 pp_cxx_unqualified_id (this, DECL_NAME (t));
1827 break;
1829 case EH_SPEC_BLOCK:
1830 break;
1832 /* try-block:
1833 try compound-statement handler-seq */
1834 case TRY_BLOCK:
1835 pp_maybe_newline_and_indent (this, 0);
1836 pp_cxx_ws_string (this, "try");
1837 pp_newline_and_indent (this, 3);
1838 statement (TRY_STMTS (t));
1839 pp_newline_and_indent (this, -3);
1840 if (CLEANUP_P (t))
1842 else
1843 statement (TRY_HANDLERS (t));
1844 break;
1847 handler-seq:
1848 handler handler-seq(opt)
1850 handler:
1851 catch ( exception-declaration ) compound-statement
1853 exception-declaration:
1854 type-specifier-seq declarator
1855 type-specifier-seq abstract-declarator
1856 ... */
1857 case HANDLER:
1858 pp_cxx_ws_string (this, "catch");
1859 pp_cxx_left_paren (this);
1860 pp_cxx_exception_declaration (this, HANDLER_PARMS (t));
1861 pp_cxx_right_paren (this);
1862 pp_indentation (this) += 3;
1863 pp_needs_newline (this) = true;
1864 statement (HANDLER_BODY (t));
1865 pp_indentation (this) -= 3;
1866 pp_needs_newline (this) = true;
1867 break;
1869 /* selection-statement:
1870 if ( expression ) statement
1871 if ( expression ) statement else statement */
1872 case IF_STMT:
1873 pp_cxx_ws_string (this, "if");
1874 pp_cxx_whitespace (this);
1875 pp_cxx_left_paren (this);
1876 expression (IF_COND (t));
1877 pp_cxx_right_paren (this);
1878 pp_newline_and_indent (this, 2);
1879 statement (THEN_CLAUSE (t));
1880 pp_newline_and_indent (this, -2);
1881 if (ELSE_CLAUSE (t))
1883 tree else_clause = ELSE_CLAUSE (t);
1884 pp_cxx_ws_string (this, "else");
1885 if (TREE_CODE (else_clause) == IF_STMT)
1886 pp_cxx_whitespace (this);
1887 else
1888 pp_newline_and_indent (this, 2);
1889 statement (else_clause);
1890 if (TREE_CODE (else_clause) != IF_STMT)
1891 pp_newline_and_indent (this, -2);
1893 break;
1895 case SWITCH_STMT:
1896 pp_cxx_ws_string (this, "switch");
1897 pp_space (this);
1898 pp_cxx_left_paren (this);
1899 expression (SWITCH_STMT_COND (t));
1900 pp_cxx_right_paren (this);
1901 pp_indentation (this) += 3;
1902 pp_needs_newline (this) = true;
1903 statement (SWITCH_STMT_BODY (t));
1904 pp_newline_and_indent (this, -3);
1905 break;
1907 /* iteration-statement:
1908 while ( expression ) statement
1909 do statement while ( expression ) ;
1910 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1911 for ( declaration expression(opt) ; expression(opt) ) statement */
1912 case WHILE_STMT:
1913 pp_cxx_ws_string (this, "while");
1914 pp_space (this);
1915 pp_cxx_left_paren (this);
1916 expression (WHILE_COND (t));
1917 pp_cxx_right_paren (this);
1918 pp_newline_and_indent (this, 3);
1919 statement (WHILE_BODY (t));
1920 pp_indentation (this) -= 3;
1921 pp_needs_newline (this) = true;
1922 break;
1924 case DO_STMT:
1925 pp_cxx_ws_string (this, "do");
1926 pp_newline_and_indent (this, 3);
1927 statement (DO_BODY (t));
1928 pp_newline_and_indent (this, -3);
1929 pp_cxx_ws_string (this, "while");
1930 pp_space (this);
1931 pp_cxx_left_paren (this);
1932 expression (DO_COND (t));
1933 pp_cxx_right_paren (this);
1934 pp_cxx_semicolon (this);
1935 pp_needs_newline (this) = true;
1936 break;
1938 case FOR_STMT:
1939 pp_cxx_ws_string (this, "for");
1940 pp_space (this);
1941 pp_cxx_left_paren (this);
1942 if (FOR_INIT_STMT (t))
1943 statement (FOR_INIT_STMT (t));
1944 else
1945 pp_cxx_semicolon (this);
1946 pp_needs_newline (this) = false;
1947 pp_cxx_whitespace (this);
1948 if (FOR_COND (t))
1949 expression (FOR_COND (t));
1950 pp_cxx_semicolon (this);
1951 pp_needs_newline (this) = false;
1952 pp_cxx_whitespace (this);
1953 if (FOR_EXPR (t))
1954 expression (FOR_EXPR (t));
1955 pp_cxx_right_paren (this);
1956 pp_newline_and_indent (this, 3);
1957 statement (FOR_BODY (t));
1958 pp_indentation (this) -= 3;
1959 pp_needs_newline (this) = true;
1960 break;
1962 case RANGE_FOR_STMT:
1963 pp_cxx_ws_string (this, "for");
1964 pp_space (this);
1965 pp_cxx_left_paren (this);
1966 statement (RANGE_FOR_DECL (t));
1967 pp_space (this);
1968 pp_needs_newline (this) = false;
1969 pp_colon (this);
1970 pp_space (this);
1971 statement (RANGE_FOR_EXPR (t));
1972 pp_cxx_right_paren (this);
1973 pp_newline_and_indent (this, 3);
1974 statement (FOR_BODY (t));
1975 pp_indentation (this) -= 3;
1976 pp_needs_newline (this) = true;
1977 break;
1979 /* jump-statement:
1980 goto identifier;
1981 continue ;
1982 return expression(opt) ; */
1983 case BREAK_STMT:
1984 case CONTINUE_STMT:
1985 pp_string (this, TREE_CODE (t) == BREAK_STMT ? "break" : "continue");
1986 pp_cxx_semicolon (this);
1987 pp_needs_newline (this) = true;
1988 break;
1990 /* expression-statement:
1991 expression(opt) ; */
1992 case EXPR_STMT:
1993 expression (EXPR_STMT_EXPR (t));
1994 pp_cxx_semicolon (this);
1995 pp_needs_newline (this) = true;
1996 break;
1998 case CLEANUP_STMT:
1999 pp_cxx_ws_string (this, "try");
2000 pp_newline_and_indent (this, 2);
2001 statement (CLEANUP_BODY (t));
2002 pp_newline_and_indent (this, -2);
2003 pp_cxx_ws_string (this, CLEANUP_EH_ONLY (t) ? "catch" : "finally");
2004 pp_newline_and_indent (this, 2);
2005 statement (CLEANUP_EXPR (t));
2006 pp_newline_and_indent (this, -2);
2007 break;
2009 case STATIC_ASSERT:
2010 declaration (t);
2011 break;
2013 default:
2014 c_pretty_printer::statement (t);
2015 break;
2019 /* original-namespace-definition:
2020 namespace identifier { namespace-body }
2022 As an edge case, we also handle unnamed namespace definition here. */
2024 static void
2025 pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t)
2027 pp_cxx_ws_string (pp, "namespace");
2028 if (DECL_CONTEXT (t))
2029 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
2030 if (DECL_NAME (t))
2031 pp_cxx_unqualified_id (pp, t);
2032 pp_cxx_whitespace (pp);
2033 pp_cxx_left_brace (pp);
2034 /* We do not print the namespace-body. */
2035 pp_cxx_whitespace (pp);
2036 pp_cxx_right_brace (pp);
2039 /* namespace-alias:
2040 identifier
2042 namespace-alias-definition:
2043 namespace identifier = qualified-namespace-specifier ;
2045 qualified-namespace-specifier:
2046 ::(opt) nested-name-specifier(opt) namespace-name */
2048 static void
2049 pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t)
2051 pp_cxx_ws_string (pp, "namespace");
2052 if (DECL_CONTEXT (t))
2053 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
2054 pp_cxx_unqualified_id (pp, t);
2055 pp_cxx_whitespace (pp);
2056 pp_equal (pp);
2057 pp_cxx_whitespace (pp);
2058 if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)))
2059 pp_cxx_nested_name_specifier (pp,
2060 DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)));
2061 pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t));
2062 pp_cxx_semicolon (pp);
2065 /* simple-declaration:
2066 decl-specifier-seq(opt) init-declarator-list(opt) */
2068 static void
2069 pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t)
2071 pp->declaration_specifiers (t);
2072 pp_cxx_init_declarator (pp, t);
2073 pp_cxx_semicolon (pp);
2074 pp_needs_newline (pp) = true;
2078 template-parameter-list:
2079 template-parameter
2080 template-parameter-list , template-parameter */
2082 static inline void
2083 pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t)
2085 const int n = TREE_VEC_LENGTH (t);
2086 int i;
2087 for (i = 0; i < n; ++i)
2089 if (i)
2090 pp_cxx_separate_with (pp, ',');
2091 pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i));
2095 /* template-parameter:
2096 type-parameter
2097 parameter-declaration
2099 type-parameter:
2100 class ...(opt) identifier(opt)
2101 class identifier(opt) = type-id
2102 typename identifier(opt)
2103 typename ...(opt) identifier(opt) = type-id
2104 template < template-parameter-list > class ...(opt) identifier(opt)
2105 template < template-parameter-list > class identifier(opt) = template-name */
2107 static void
2108 pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t)
2110 tree parameter = TREE_VALUE (t);
2111 switch (TREE_CODE (parameter))
2113 case TYPE_DECL:
2114 pp_cxx_ws_string (pp, "class");
2115 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
2116 pp_cxx_ws_string (pp, "...");
2117 if (DECL_NAME (parameter))
2118 pp_cxx_tree_identifier (pp, DECL_NAME (parameter));
2119 /* FIXME: Check if we should print also default argument. */
2120 break;
2122 case PARM_DECL:
2123 pp_cxx_parameter_declaration (pp, parameter);
2124 break;
2126 case TEMPLATE_DECL:
2127 break;
2129 default:
2130 pp_unsupported_tree (pp, t);
2131 break;
2135 /* Pretty-print a template parameter in the canonical form
2136 "template-parameter-<level>-<position in parameter list>". */
2138 void
2139 pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm)
2141 const enum tree_code code = TREE_CODE (parm);
2143 /* Brings type template parameters to the canonical forms. */
2144 if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM
2145 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
2146 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
2148 pp_cxx_begin_template_argument_list (pp);
2149 pp->translate_string ("template-parameter-");
2150 pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm));
2151 pp_minus (pp);
2152 pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1);
2153 pp_cxx_end_template_argument_list (pp);
2157 template-declaration:
2158 export(opt) template < template-parameter-list > declaration */
2160 static void
2161 pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t)
2163 tree tmpl = most_general_template (t);
2164 tree level;
2166 pp_maybe_newline_and_indent (pp, 0);
2167 for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level))
2169 pp_cxx_ws_string (pp, "template");
2170 pp_cxx_begin_template_argument_list (pp);
2171 pp_cxx_template_parameter_list (pp, TREE_VALUE (level));
2172 pp_cxx_end_template_argument_list (pp);
2173 pp_newline_and_indent (pp, 3);
2175 if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t))
2176 pp_cxx_function_definition (pp, t);
2177 else
2178 pp_cxx_simple_declaration (pp, t);
2181 static void
2182 pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t)
2184 pp_unsupported_tree (pp, t);
2187 static void
2188 pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t)
2190 pp_unsupported_tree (pp, t);
2194 declaration:
2195 block-declaration
2196 function-definition
2197 template-declaration
2198 explicit-instantiation
2199 explicit-specialization
2200 linkage-specification
2201 namespace-definition
2203 block-declaration:
2204 simple-declaration
2205 asm-definition
2206 namespace-alias-definition
2207 using-declaration
2208 using-directive
2209 static_assert-declaration */
2210 void
2211 cxx_pretty_printer::declaration (tree t)
2213 if (TREE_CODE (t) == STATIC_ASSERT)
2215 pp_cxx_ws_string (this, "static_assert");
2216 pp_cxx_left_paren (this);
2217 expression (STATIC_ASSERT_CONDITION (t));
2218 pp_cxx_separate_with (this, ',');
2219 expression (STATIC_ASSERT_MESSAGE (t));
2220 pp_cxx_right_paren (this);
2222 else if (!DECL_LANG_SPECIFIC (t))
2223 pp_cxx_simple_declaration (this, t);
2224 else if (DECL_USE_TEMPLATE (t))
2225 switch (DECL_USE_TEMPLATE (t))
2227 case 1:
2228 pp_cxx_template_declaration (this, t);
2229 break;
2231 case 2:
2232 pp_cxx_explicit_specialization (this, t);
2233 break;
2235 case 3:
2236 pp_cxx_explicit_instantiation (this, t);
2237 break;
2239 default:
2240 break;
2242 else switch (TREE_CODE (t))
2244 case VAR_DECL:
2245 case TYPE_DECL:
2246 pp_cxx_simple_declaration (this, t);
2247 break;
2249 case FUNCTION_DECL:
2250 if (DECL_SAVED_TREE (t))
2251 pp_cxx_function_definition (this, t);
2252 else
2253 pp_cxx_simple_declaration (this, t);
2254 break;
2256 case NAMESPACE_DECL:
2257 if (DECL_NAMESPACE_ALIAS (t))
2258 pp_cxx_namespace_alias_definition (this, t);
2259 else
2260 pp_cxx_original_namespace_definition (this, t);
2261 break;
2263 default:
2264 pp_unsupported_tree (this, t);
2265 break;
2269 static void
2270 pp_cxx_typeid_expression (cxx_pretty_printer *pp, tree t)
2272 t = TREE_OPERAND (t, 0);
2273 pp_cxx_ws_string (pp, "typeid");
2274 pp_cxx_left_paren (pp);
2275 if (TYPE_P (t))
2276 pp->type_id (t);
2277 else
2278 pp->expression (t);
2279 pp_cxx_right_paren (pp);
2282 void
2283 pp_cxx_va_arg_expression (cxx_pretty_printer *pp, tree t)
2285 pp_cxx_ws_string (pp, "va_arg");
2286 pp_cxx_left_paren (pp);
2287 pp->assignment_expression (TREE_OPERAND (t, 0));
2288 pp_cxx_separate_with (pp, ',');
2289 pp->type_id (TREE_TYPE (t));
2290 pp_cxx_right_paren (pp);
2293 static bool
2294 pp_cxx_offsetof_expression_1 (cxx_pretty_printer *pp, tree t)
2296 switch (TREE_CODE (t))
2298 case ARROW_EXPR:
2299 if (TREE_CODE (TREE_OPERAND (t, 0)) == STATIC_CAST_EXPR
2300 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
2302 pp->type_id (TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))));
2303 pp_cxx_separate_with (pp, ',');
2304 return true;
2306 return false;
2307 case COMPONENT_REF:
2308 if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2309 return false;
2310 if (TREE_CODE (TREE_OPERAND (t, 0)) != ARROW_EXPR)
2311 pp_cxx_dot (pp);
2312 pp->expression (TREE_OPERAND (t, 1));
2313 return true;
2314 case ARRAY_REF:
2315 if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2316 return false;
2317 pp_left_bracket (pp);
2318 pp->expression (TREE_OPERAND (t, 1));
2319 pp_right_bracket (pp);
2320 return true;
2321 default:
2322 return false;
2326 void
2327 pp_cxx_offsetof_expression (cxx_pretty_printer *pp, tree t)
2329 pp_cxx_ws_string (pp, "offsetof");
2330 pp_cxx_left_paren (pp);
2331 if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2332 pp->expression (TREE_OPERAND (t, 0));
2333 pp_cxx_right_paren (pp);
2336 void
2337 pp_cxx_trait_expression (cxx_pretty_printer *pp, tree t)
2339 cp_trait_kind kind = TRAIT_EXPR_KIND (t);
2341 switch (kind)
2343 case CPTK_HAS_NOTHROW_ASSIGN:
2344 pp_cxx_ws_string (pp, "__has_nothrow_assign");
2345 break;
2346 case CPTK_HAS_TRIVIAL_ASSIGN:
2347 pp_cxx_ws_string (pp, "__has_trivial_assign");
2348 break;
2349 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
2350 pp_cxx_ws_string (pp, "__has_nothrow_constructor");
2351 break;
2352 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
2353 pp_cxx_ws_string (pp, "__has_trivial_constructor");
2354 break;
2355 case CPTK_HAS_NOTHROW_COPY:
2356 pp_cxx_ws_string (pp, "__has_nothrow_copy");
2357 break;
2358 case CPTK_HAS_TRIVIAL_COPY:
2359 pp_cxx_ws_string (pp, "__has_trivial_copy");
2360 break;
2361 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
2362 pp_cxx_ws_string (pp, "__has_trivial_destructor");
2363 break;
2364 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
2365 pp_cxx_ws_string (pp, "__has_virtual_destructor");
2366 break;
2367 case CPTK_IS_ABSTRACT:
2368 pp_cxx_ws_string (pp, "__is_abstract");
2369 break;
2370 case CPTK_IS_BASE_OF:
2371 pp_cxx_ws_string (pp, "__is_base_of");
2372 break;
2373 case CPTK_IS_CLASS:
2374 pp_cxx_ws_string (pp, "__is_class");
2375 break;
2376 case CPTK_IS_CONVERTIBLE_TO:
2377 pp_cxx_ws_string (pp, "__is_convertible_to");
2378 break;
2379 case CPTK_IS_EMPTY:
2380 pp_cxx_ws_string (pp, "__is_empty");
2381 break;
2382 case CPTK_IS_ENUM:
2383 pp_cxx_ws_string (pp, "__is_enum");
2384 break;
2385 case CPTK_IS_FINAL:
2386 pp_cxx_ws_string (pp, "__is_final");
2387 break;
2388 case CPTK_IS_POD:
2389 pp_cxx_ws_string (pp, "__is_pod");
2390 break;
2391 case CPTK_IS_POLYMORPHIC:
2392 pp_cxx_ws_string (pp, "__is_polymorphic");
2393 break;
2394 case CPTK_IS_STD_LAYOUT:
2395 pp_cxx_ws_string (pp, "__is_std_layout");
2396 break;
2397 case CPTK_IS_TRIVIAL:
2398 pp_cxx_ws_string (pp, "__is_trivial");
2399 break;
2400 case CPTK_IS_UNION:
2401 pp_cxx_ws_string (pp, "__is_union");
2402 break;
2403 case CPTK_IS_LITERAL_TYPE:
2404 pp_cxx_ws_string (pp, "__is_literal_type");
2405 break;
2407 default:
2408 gcc_unreachable ();
2411 pp_cxx_left_paren (pp);
2412 pp->type_id (TRAIT_EXPR_TYPE1 (t));
2414 if (kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO)
2416 pp_cxx_separate_with (pp, ',');
2417 pp->type_id (TRAIT_EXPR_TYPE2 (t));
2420 pp_cxx_right_paren (pp);
2423 typedef c_pretty_print_fn pp_fun;
2425 /* Initialization of a C++ pretty-printer object. */
2427 cxx_pretty_printer::cxx_pretty_printer ()
2428 : c_pretty_printer (),
2429 enclosing_scope (global_namespace)
2431 pp_set_line_maximum_length (this, 0);
2433 type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq;
2434 parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause;