merge from trunk
[official-gcc.git] / gcc / cp / cxx-pretty-print.c
blob4851af901f7e5af05e13cf3805dfd88bdb357ef8
1 /* Implementation of subroutines for the GNU C++ pretty-printer.
2 Copyright (C) 2003, 2004, 2005, 2007, 2008,
3 2009 Free Software Foundation, Inc.
4 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "real.h"
27 #include "intl.h"
28 #include "cxx-pretty-print.h"
29 #include "cp-tree.h"
30 #include "toplev.h"
32 /* Translate if being used for diagnostics, but not for dump files or
33 __PRETTY_FUNCTION. */
34 #define M_(msgid) (pp_translate_identifiers (pp) ? _(msgid) : (msgid))
36 static void pp_cxx_unqualified_id (cxx_pretty_printer *, tree);
37 static void pp_cxx_nested_name_specifier (cxx_pretty_printer *, tree);
38 static void pp_cxx_qualified_id (cxx_pretty_printer *, tree);
39 static void pp_cxx_assignment_expression (cxx_pretty_printer *, tree);
40 static void pp_cxx_expression (cxx_pretty_printer *, tree);
41 static void pp_cxx_template_argument_list (cxx_pretty_printer *, tree);
42 static void pp_cxx_type_specifier_seq (cxx_pretty_printer *, tree);
43 static void pp_cxx_ptr_operator (cxx_pretty_printer *, tree);
44 static void pp_cxx_type_id (cxx_pretty_printer *, tree);
45 static void pp_cxx_direct_abstract_declarator (cxx_pretty_printer *, tree);
46 static void pp_cxx_declarator (cxx_pretty_printer *, tree);
47 static void pp_cxx_parameter_declaration_clause (cxx_pretty_printer *, tree);
48 static void pp_cxx_abstract_declarator (cxx_pretty_printer *, tree);
49 static void pp_cxx_statement (cxx_pretty_printer *, tree);
50 static void pp_cxx_template_parameter (cxx_pretty_printer *, tree);
51 static void pp_cxx_cast_expression (cxx_pretty_printer *, tree);
52 static void pp_cxx_typeid_expression (cxx_pretty_printer *, tree);
55 static inline void
56 pp_cxx_nonconsecutive_character (cxx_pretty_printer *pp, int c)
58 const char *p = pp_last_position_in_text (pp);
60 if (p != NULL && *p == c)
61 pp_cxx_whitespace (pp);
62 pp_character (pp, c);
63 pp_base (pp)->padding = pp_none;
66 #define pp_cxx_storage_class_specifier(PP, T) \
67 pp_c_storage_class_specifier (pp_c_base (PP), T)
68 #define pp_cxx_expression_list(PP, T) \
69 pp_c_expression_list (pp_c_base (PP), T)
70 #define pp_cxx_space_for_pointer_operator(PP, T) \
71 pp_c_space_for_pointer_operator (pp_c_base (PP), T)
72 #define pp_cxx_init_declarator(PP, T) \
73 pp_c_init_declarator (pp_c_base (PP), T)
74 #define pp_cxx_call_argument_list(PP, T) \
75 pp_c_call_argument_list (pp_c_base (PP), T)
77 void
78 pp_cxx_colon_colon (cxx_pretty_printer *pp)
80 pp_colon_colon (pp);
81 pp_base (pp)->padding = pp_none;
84 void
85 pp_cxx_begin_template_argument_list (cxx_pretty_printer *pp)
87 pp_cxx_nonconsecutive_character (pp, '<');
90 void
91 pp_cxx_end_template_argument_list (cxx_pretty_printer *pp)
93 pp_cxx_nonconsecutive_character (pp, '>');
96 void
97 pp_cxx_separate_with (cxx_pretty_printer *pp, int c)
99 pp_separate_with (pp, c);
100 pp_base (pp)->padding = pp_none;
103 /* Expressions. */
105 static inline bool
106 is_destructor_name (tree name)
108 return name == complete_dtor_identifier
109 || name == base_dtor_identifier
110 || name == deleting_dtor_identifier;
113 /* conversion-function-id:
114 operator conversion-type-id
116 conversion-type-id:
117 type-specifier-seq conversion-declarator(opt)
119 conversion-declarator:
120 ptr-operator conversion-declarator(opt) */
122 static inline void
123 pp_cxx_conversion_function_id (cxx_pretty_printer *pp, tree t)
125 pp_cxx_ws_string (pp, "operator");
126 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
129 static inline void
130 pp_cxx_template_id (cxx_pretty_printer *pp, tree t)
132 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0));
133 pp_cxx_begin_template_argument_list (pp);
134 pp_cxx_template_argument_list (pp, TREE_OPERAND (t, 1));
135 pp_cxx_end_template_argument_list (pp);
138 /* Prints the unqualified part of the id-expression T.
140 unqualified-id:
141 identifier
142 operator-function-id
143 conversion-function-id
144 ~ class-name
145 template-id */
147 static void
148 pp_cxx_unqualified_id (cxx_pretty_printer *pp, tree t)
150 enum tree_code code = TREE_CODE (t);
151 switch (code)
153 case RESULT_DECL:
154 pp_cxx_ws_string (pp, M_("<return-value>"));
155 break;
157 case OVERLOAD:
158 t = OVL_CURRENT (t);
159 case VAR_DECL:
160 case PARM_DECL:
161 case CONST_DECL:
162 case TYPE_DECL:
163 case FUNCTION_DECL:
164 case NAMESPACE_DECL:
165 case FIELD_DECL:
166 case LABEL_DECL:
167 case USING_DECL:
168 case TEMPLATE_DECL:
169 t = DECL_NAME (t);
171 case IDENTIFIER_NODE:
172 if (t == NULL)
173 pp_cxx_ws_string (pp, M_("<unnamed>"));
174 else if (IDENTIFIER_TYPENAME_P (t))
175 pp_cxx_conversion_function_id (pp, t);
176 else
178 if (is_destructor_name (t))
180 pp_complement (pp);
181 /* FIXME: Why is this necessary? */
182 if (TREE_TYPE (t))
183 t = constructor_name (TREE_TYPE (t));
185 pp_cxx_tree_identifier (pp, t);
187 break;
189 case TEMPLATE_ID_EXPR:
190 pp_cxx_template_id (pp, t);
191 break;
193 case BASELINK:
194 pp_cxx_unqualified_id (pp, BASELINK_FUNCTIONS (t));
195 break;
197 case RECORD_TYPE:
198 case UNION_TYPE:
199 case ENUMERAL_TYPE:
200 case TYPENAME_TYPE:
201 case UNBOUND_CLASS_TEMPLATE:
202 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
203 if (CLASS_TYPE_P (t) && CLASSTYPE_USE_TEMPLATE (t))
205 pp_cxx_begin_template_argument_list (pp);
206 pp_cxx_template_argument_list (pp, CLASSTYPE_TI_ARGS (t));
207 pp_cxx_end_template_argument_list (pp);
209 break;
211 case BIT_NOT_EXPR:
212 pp_cxx_complement (pp);
213 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0));
214 break;
216 case TEMPLATE_TYPE_PARM:
217 case TEMPLATE_TEMPLATE_PARM:
218 if (TYPE_IDENTIFIER (t))
219 pp_cxx_unqualified_id (pp, TYPE_IDENTIFIER (t));
220 else
221 pp_cxx_canonical_template_parameter (pp, t);
222 break;
224 case TEMPLATE_PARM_INDEX:
225 pp_cxx_unqualified_id (pp, TEMPLATE_PARM_DECL (t));
226 break;
228 case BOUND_TEMPLATE_TEMPLATE_PARM:
229 pp_cxx_cv_qualifier_seq (pp, t);
230 pp_cxx_unqualified_id (pp, TYPE_IDENTIFIER (t));
231 pp_cxx_begin_template_argument_list (pp);
232 pp_cxx_template_argument_list (pp, TYPE_TI_ARGS (t));
233 pp_cxx_end_template_argument_list (pp);
234 break;
236 default:
237 pp_unsupported_tree (pp, t);
238 break;
242 /* Pretty-print out the token sequence ":: template" in template codes
243 where it is needed to "inline declare" the (following) member as
244 a template. This situation arises when SCOPE of T is dependent
245 on template parameters. */
247 static inline void
248 pp_cxx_template_keyword_if_needed (cxx_pretty_printer *pp, tree scope, tree t)
250 if (TREE_CODE (t) == TEMPLATE_ID_EXPR
251 && TYPE_P (scope) && dependent_type_p (scope))
252 pp_cxx_ws_string (pp, "template");
255 /* nested-name-specifier:
256 class-or-namespace-name :: nested-name-specifier(opt)
257 class-or-namespace-name :: template nested-name-specifier */
259 static void
260 pp_cxx_nested_name_specifier (cxx_pretty_printer *pp, tree t)
262 if (t != NULL && t != pp->enclosing_scope)
264 tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
265 pp_cxx_nested_name_specifier (pp, scope);
266 pp_cxx_template_keyword_if_needed (pp, scope, t);
267 pp_cxx_unqualified_id (pp, t);
268 pp_cxx_colon_colon (pp);
272 /* qualified-id:
273 nested-name-specifier template(opt) unqualified-id */
275 static void
276 pp_cxx_qualified_id (cxx_pretty_printer *pp, tree t)
278 switch (TREE_CODE (t))
280 /* A pointer-to-member is always qualified. */
281 case PTRMEM_CST:
282 pp_cxx_nested_name_specifier (pp, PTRMEM_CST_CLASS (t));
283 pp_cxx_unqualified_id (pp, PTRMEM_CST_MEMBER (t));
284 break;
286 /* In Standard C++, functions cannot possibly be used as
287 nested-name-specifiers. However, there are situations where
288 is "makes sense" to output the surrounding function name for the
289 purpose of emphasizing on the scope kind. Just printing the
290 function name might not be sufficient as it may be overloaded; so,
291 we decorate the function with its signature too.
292 FIXME: This is probably the wrong pretty-printing for conversion
293 functions and some function templates. */
294 case OVERLOAD:
295 t = OVL_CURRENT (t);
296 case FUNCTION_DECL:
297 if (DECL_FUNCTION_MEMBER_P (t))
298 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
299 pp_cxx_unqualified_id
300 (pp, DECL_CONSTRUCTOR_P (t) ? DECL_CONTEXT (t) : t);
301 pp_cxx_parameter_declaration_clause (pp, TREE_TYPE (t));
302 break;
304 case OFFSET_REF:
305 case SCOPE_REF:
306 pp_cxx_nested_name_specifier (pp, TREE_OPERAND (t, 0));
307 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 1));
308 break;
310 default:
312 tree scope = TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t);
313 if (scope != pp->enclosing_scope)
315 pp_cxx_nested_name_specifier (pp, scope);
316 pp_cxx_template_keyword_if_needed (pp, scope, t);
318 pp_cxx_unqualified_id (pp, t);
320 break;
325 static void
326 pp_cxx_constant (cxx_pretty_printer *pp, tree t)
328 switch (TREE_CODE (t))
330 case STRING_CST:
332 const bool in_parens = PAREN_STRING_LITERAL_P (t);
333 if (in_parens)
334 pp_cxx_left_paren (pp);
335 pp_c_constant (pp_c_base (pp), t);
336 if (in_parens)
337 pp_cxx_right_paren (pp);
339 break;
341 default:
342 pp_c_constant (pp_c_base (pp), t);
343 break;
347 /* id-expression:
348 unqualified-id
349 qualified-id */
351 static inline void
352 pp_cxx_id_expression (cxx_pretty_printer *pp, tree t)
354 if (TREE_CODE (t) == OVERLOAD)
355 t = OVL_CURRENT (t);
356 if (DECL_P (t) && DECL_CONTEXT (t))
357 pp_cxx_qualified_id (pp, t);
358 else
359 pp_cxx_unqualified_id (pp, t);
362 /* primary-expression:
363 literal
364 this
365 :: identifier
366 :: operator-function-id
367 :: qualifier-id
368 ( expression )
369 id-expression
371 GNU Extensions:
372 __builtin_va_arg ( assignment-expression , type-id )
373 __builtin_offsetof ( type-id, offsetof-expression )
375 __has_nothrow_assign ( type-id )
376 __has_nothrow_constructor ( type-id )
377 __has_nothrow_copy ( type-id )
378 __has_trivial_assign ( type-id )
379 __has_trivial_constructor ( type-id )
380 __has_trivial_copy ( type-id )
381 __has_trivial_destructor ( type-id )
382 __has_virtual_destructor ( type-id )
383 __is_abstract ( type-id )
384 __is_base_of ( type-id , type-id )
385 __is_class ( type-id )
386 __is_convertible_to ( type-id , type-id )
387 __is_empty ( type-id )
388 __is_enum ( type-id )
389 __is_pod ( type-id )
390 __is_polymorphic ( type-id )
391 __is_union ( type-id ) */
393 static void
394 pp_cxx_primary_expression (cxx_pretty_printer *pp, tree t)
396 switch (TREE_CODE (t))
398 case INTEGER_CST:
399 case REAL_CST:
400 case COMPLEX_CST:
401 case STRING_CST:
402 pp_cxx_constant (pp, t);
403 break;
405 case BASELINK:
406 t = BASELINK_FUNCTIONS (t);
407 case VAR_DECL:
408 case PARM_DECL:
409 case FIELD_DECL:
410 case FUNCTION_DECL:
411 case OVERLOAD:
412 case CONST_DECL:
413 case TEMPLATE_DECL:
414 pp_cxx_id_expression (pp, t);
415 break;
417 case RESULT_DECL:
418 case TEMPLATE_TYPE_PARM:
419 case TEMPLATE_TEMPLATE_PARM:
420 case TEMPLATE_PARM_INDEX:
421 pp_cxx_unqualified_id (pp, t);
422 break;
424 case STMT_EXPR:
425 pp_cxx_left_paren (pp);
426 pp_cxx_statement (pp, STMT_EXPR_STMT (t));
427 pp_cxx_right_paren (pp);
428 break;
430 case TRAIT_EXPR:
431 pp_cxx_trait_expression (pp, t);
432 break;
434 case VA_ARG_EXPR:
435 pp_cxx_va_arg_expression (pp, t);
436 break;
438 case OFFSETOF_EXPR:
439 pp_cxx_offsetof_expression (pp, t);
440 break;
442 default:
443 pp_c_primary_expression (pp_c_base (pp), t);
444 break;
448 /* postfix-expression:
449 primary-expression
450 postfix-expression [ expression ]
451 postfix-expression ( expression-list(opt) )
452 simple-type-specifier ( expression-list(opt) )
453 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
454 typename ::(opt) nested-name-specifier template(opt)
455 template-id ( expression-list(opt) )
456 postfix-expression . template(opt) ::(opt) id-expression
457 postfix-expression -> template(opt) ::(opt) id-expression
458 postfix-expression . pseudo-destructor-name
459 postfix-expression -> pseudo-destructor-name
460 postfix-expression ++
461 postfix-expression --
462 dynamic_cast < type-id > ( expression )
463 static_cast < type-id > ( expression )
464 reinterpret_cast < type-id > ( expression )
465 const_cast < type-id > ( expression )
466 typeid ( expression )
467 typeid ( type-id ) */
469 static void
470 pp_cxx_postfix_expression (cxx_pretty_printer *pp, tree t)
472 enum tree_code code = TREE_CODE (t);
474 switch (code)
476 case AGGR_INIT_EXPR:
477 case CALL_EXPR:
479 tree fun = (code == AGGR_INIT_EXPR ? AGGR_INIT_EXPR_FN (t)
480 : CALL_EXPR_FN (t));
481 tree saved_scope = pp->enclosing_scope;
482 bool skipfirst = false;
483 tree arg;
485 if (TREE_CODE (fun) == ADDR_EXPR)
486 fun = TREE_OPERAND (fun, 0);
488 /* In templates, where there is no way to tell whether a given
489 call uses an actual member function. So the parser builds
490 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
491 instantiation time. */
492 if (TREE_CODE (fun) != FUNCTION_DECL)
494 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun))
496 tree object = (code == AGGR_INIT_EXPR
497 ? (AGGR_INIT_VIA_CTOR_P (t)
498 ? AGGR_INIT_EXPR_SLOT (t)
499 : AGGR_INIT_EXPR_ARG (t, 0))
500 : CALL_EXPR_ARG (t, 0));
502 while (TREE_CODE (object) == NOP_EXPR)
503 object = TREE_OPERAND (object, 0);
505 if (TREE_CODE (object) == ADDR_EXPR)
506 object = TREE_OPERAND (object, 0);
508 if (TREE_CODE (TREE_TYPE (object)) != POINTER_TYPE)
510 pp_cxx_postfix_expression (pp, object);
511 pp_cxx_dot (pp);
513 else
515 pp_cxx_postfix_expression (pp, object);
516 pp_cxx_arrow (pp);
518 skipfirst = true;
519 pp->enclosing_scope = strip_pointer_operator (TREE_TYPE (object));
522 pp_cxx_postfix_expression (pp, fun);
523 pp->enclosing_scope = saved_scope;
524 pp_cxx_left_paren (pp);
525 if (code == AGGR_INIT_EXPR)
527 aggr_init_expr_arg_iterator iter;
528 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
530 if (skipfirst)
531 skipfirst = false;
532 else
534 pp_cxx_expression (pp, arg);
535 if (more_aggr_init_expr_args_p (&iter))
536 pp_cxx_separate_with (pp, ',');
540 else
542 call_expr_arg_iterator iter;
543 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
545 if (skipfirst)
546 skipfirst = false;
547 else
549 pp_cxx_expression (pp, arg);
550 if (more_call_expr_args_p (&iter))
551 pp_cxx_separate_with (pp, ',');
555 pp_cxx_right_paren (pp);
557 if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t))
559 pp_cxx_separate_with (pp, ',');
560 pp_cxx_postfix_expression (pp, AGGR_INIT_EXPR_SLOT (t));
562 break;
564 case BASELINK:
565 case VAR_DECL:
566 case PARM_DECL:
567 case FIELD_DECL:
568 case FUNCTION_DECL:
569 case OVERLOAD:
570 case CONST_DECL:
571 case TEMPLATE_DECL:
572 case RESULT_DECL:
573 pp_cxx_primary_expression (pp, t);
574 break;
576 case DYNAMIC_CAST_EXPR:
577 case STATIC_CAST_EXPR:
578 case REINTERPRET_CAST_EXPR:
579 case CONST_CAST_EXPR:
580 if (code == DYNAMIC_CAST_EXPR)
581 pp_cxx_ws_string (pp, "dynamic_cast");
582 else if (code == STATIC_CAST_EXPR)
583 pp_cxx_ws_string (pp, "static_cast");
584 else if (code == REINTERPRET_CAST_EXPR)
585 pp_cxx_ws_string (pp, "reinterpret_cast");
586 else
587 pp_cxx_ws_string (pp, "const_cast");
588 pp_cxx_begin_template_argument_list (pp);
589 pp_cxx_type_id (pp, TREE_TYPE (t));
590 pp_cxx_end_template_argument_list (pp);
591 pp_left_paren (pp);
592 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
593 pp_right_paren (pp);
594 break;
596 case EMPTY_CLASS_EXPR:
597 pp_cxx_type_id (pp, TREE_TYPE (t));
598 pp_left_paren (pp);
599 pp_right_paren (pp);
600 break;
602 case TYPEID_EXPR:
603 pp_cxx_typeid_expression (pp, t);
604 break;
606 case PSEUDO_DTOR_EXPR:
607 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
608 pp_cxx_dot (pp);
609 pp_cxx_qualified_id (pp, TREE_OPERAND (t, 1));
610 pp_cxx_colon_colon (pp);
611 pp_complement (pp);
612 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 2));
613 break;
615 case ARROW_EXPR:
616 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
617 pp_cxx_arrow (pp);
618 break;
620 default:
621 pp_c_postfix_expression (pp_c_base (pp), t);
622 break;
626 /* new-expression:
627 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
628 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
630 new-placement:
631 ( expression-list )
633 new-type-id:
634 type-specifier-seq new-declarator(opt)
636 new-declarator:
637 ptr-operator new-declarator(opt)
638 direct-new-declarator
640 direct-new-declarator
641 [ expression ]
642 direct-new-declarator [ constant-expression ]
644 new-initializer:
645 ( expression-list(opt) ) */
647 static void
648 pp_cxx_new_expression (cxx_pretty_printer *pp, tree t)
650 enum tree_code code = TREE_CODE (t);
651 tree type = TREE_OPERAND (t, 1);
652 tree init = TREE_OPERAND (t, 2);
653 switch (code)
655 case NEW_EXPR:
656 case VEC_NEW_EXPR:
657 if (NEW_EXPR_USE_GLOBAL (t))
658 pp_cxx_colon_colon (pp);
659 pp_cxx_ws_string (pp, "new");
660 if (TREE_OPERAND (t, 0))
662 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
663 pp_space (pp);
665 if (TREE_CODE (type) == ARRAY_REF)
666 type = build_cplus_array_type
667 (TREE_OPERAND (type, 0),
668 build_index_type (fold_build2_loc (input_location,
669 MINUS_EXPR, integer_type_node,
670 TREE_OPERAND (type, 1),
671 integer_one_node)));
672 pp_cxx_type_id (pp, type);
673 if (init)
675 pp_left_paren (pp);
676 if (TREE_CODE (init) == TREE_LIST)
677 pp_c_expression_list (pp_c_base (pp), init);
678 else if (init == void_zero_node)
679 ; /* OK, empty initializer list. */
680 else
681 pp_cxx_expression (pp, init);
682 pp_right_paren (pp);
684 break;
686 default:
687 pp_unsupported_tree (pp, t);
691 /* delete-expression:
692 ::(opt) delete cast-expression
693 ::(opt) delete [ ] cast-expression */
695 static void
696 pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t)
698 enum tree_code code = TREE_CODE (t);
699 switch (code)
701 case DELETE_EXPR:
702 case VEC_DELETE_EXPR:
703 if (DELETE_EXPR_USE_GLOBAL (t))
704 pp_cxx_colon_colon (pp);
705 pp_cxx_ws_string (pp, "delete");
706 pp_space (pp);
707 if (code == VEC_DELETE_EXPR
708 || DELETE_EXPR_USE_VEC (t))
710 pp_left_bracket (pp);
711 pp_right_bracket (pp);
712 pp_space (pp);
714 pp_c_cast_expression (pp_c_base (pp), TREE_OPERAND (t, 0));
715 break;
717 default:
718 pp_unsupported_tree (pp, t);
722 /* unary-expression:
723 postfix-expression
724 ++ cast-expression
725 -- cast-expression
726 unary-operator cast-expression
727 sizeof unary-expression
728 sizeof ( type-id )
729 sizeof ... ( identifier )
730 new-expression
731 delete-expression
733 unary-operator: one of
734 * & + - !
736 GNU extensions:
737 __alignof__ unary-expression
738 __alignof__ ( type-id ) */
740 static void
741 pp_cxx_unary_expression (cxx_pretty_printer *pp, tree t)
743 enum tree_code code = TREE_CODE (t);
744 switch (code)
746 case NEW_EXPR:
747 case VEC_NEW_EXPR:
748 pp_cxx_new_expression (pp, t);
749 break;
751 case DELETE_EXPR:
752 case VEC_DELETE_EXPR:
753 pp_cxx_delete_expression (pp, t);
754 break;
756 case SIZEOF_EXPR:
757 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
759 pp_cxx_ws_string (pp, "sizeof");
760 pp_cxx_ws_string (pp, "...");
761 pp_cxx_whitespace (pp);
762 pp_cxx_left_paren (pp);
763 if (TYPE_P (TREE_OPERAND (t, 0)))
764 pp_cxx_type_id (pp, TREE_OPERAND (t, 0));
765 else
766 pp_unary_expression (pp, TREE_OPERAND (t, 0));
767 pp_cxx_right_paren (pp);
768 break;
770 /* Fall through */
772 case ALIGNOF_EXPR:
773 pp_cxx_ws_string (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
774 pp_cxx_whitespace (pp);
775 if (TYPE_P (TREE_OPERAND (t, 0)))
777 pp_cxx_left_paren (pp);
778 pp_cxx_type_id (pp, TREE_OPERAND (t, 0));
779 pp_cxx_right_paren (pp);
781 else
782 pp_unary_expression (pp, TREE_OPERAND (t, 0));
783 break;
785 case UNARY_PLUS_EXPR:
786 pp_plus (pp);
787 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 0));
788 break;
790 default:
791 pp_c_unary_expression (pp_c_base (pp), t);
792 break;
796 /* cast-expression:
797 unary-expression
798 ( type-id ) cast-expression */
800 static void
801 pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t)
803 switch (TREE_CODE (t))
805 case CAST_EXPR:
806 pp_cxx_type_id (pp, TREE_TYPE (t));
807 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
808 break;
810 default:
811 pp_c_cast_expression (pp_c_base (pp), t);
812 break;
816 /* pm-expression:
817 cast-expression
818 pm-expression .* cast-expression
819 pm-expression ->* cast-expression */
821 static void
822 pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t)
824 switch (TREE_CODE (t))
826 /* Handle unfortunate OFFSET_REF overloading here. */
827 case OFFSET_REF:
828 if (TYPE_P (TREE_OPERAND (t, 0)))
830 pp_cxx_qualified_id (pp, t);
831 break;
833 /* Else fall through. */
834 case MEMBER_REF:
835 case DOTSTAR_EXPR:
836 pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0));
837 if (TREE_CODE (t) == MEMBER_REF)
838 pp_cxx_arrow (pp);
839 else
840 pp_cxx_dot (pp);
841 pp_star(pp);
842 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1));
843 break;
846 default:
847 pp_cxx_cast_expression (pp, t);
848 break;
852 /* multiplicative-expression:
853 pm-expression
854 multiplicative-expression * pm-expression
855 multiplicative-expression / pm-expression
856 multiplicative-expression % pm-expression */
858 static void
859 pp_cxx_multiplicative_expression (cxx_pretty_printer *pp, tree e)
861 enum tree_code code = TREE_CODE (e);
862 switch (code)
864 case MULT_EXPR:
865 case TRUNC_DIV_EXPR:
866 case TRUNC_MOD_EXPR:
867 pp_cxx_multiplicative_expression (pp, TREE_OPERAND (e, 0));
868 pp_space (pp);
869 if (code == MULT_EXPR)
870 pp_star (pp);
871 else if (code == TRUNC_DIV_EXPR)
872 pp_slash (pp);
873 else
874 pp_modulo (pp);
875 pp_space (pp);
876 pp_cxx_pm_expression (pp, TREE_OPERAND (e, 1));
877 break;
879 default:
880 pp_cxx_pm_expression (pp, e);
881 break;
885 /* conditional-expression:
886 logical-or-expression
887 logical-or-expression ? expression : assignment-expression */
889 static void
890 pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e)
892 if (TREE_CODE (e) == COND_EXPR)
894 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
895 pp_space (pp);
896 pp_question (pp);
897 pp_space (pp);
898 pp_cxx_expression (pp, TREE_OPERAND (e, 1));
899 pp_space (pp);
900 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
902 else
903 pp_c_logical_or_expression (pp_c_base (pp), e);
906 /* Pretty-print a compound assignment operator token as indicated by T. */
908 static void
909 pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t)
911 const char *op;
913 switch (TREE_CODE (t))
915 case NOP_EXPR:
916 op = "=";
917 break;
919 case PLUS_EXPR:
920 op = "+=";
921 break;
923 case MINUS_EXPR:
924 op = "-=";
925 break;
927 case TRUNC_DIV_EXPR:
928 op = "/=";
929 break;
931 case TRUNC_MOD_EXPR:
932 op = "%=";
933 break;
935 default:
936 op = tree_code_name[TREE_CODE (t)];
937 break;
940 pp_cxx_ws_string (pp, op);
944 /* assignment-expression:
945 conditional-expression
946 logical-or-expression assignment-operator assignment-expression
947 throw-expression
949 throw-expression:
950 throw assignment-expression(opt)
952 assignment-operator: one of
953 = *= /= %= += -= >>= <<= &= ^= |= */
955 static void
956 pp_cxx_assignment_expression (cxx_pretty_printer *pp, tree e)
958 switch (TREE_CODE (e))
960 case MODIFY_EXPR:
961 case INIT_EXPR:
962 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
963 pp_space (pp);
964 pp_equal (pp);
965 pp_space (pp);
966 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 1));
967 break;
969 case THROW_EXPR:
970 pp_cxx_ws_string (pp, "throw");
971 if (TREE_OPERAND (e, 0))
972 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 0));
973 break;
975 case MODOP_EXPR:
976 pp_c_logical_or_expression (pp_c_base (pp), TREE_OPERAND (e, 0));
977 pp_cxx_assignment_operator (pp, TREE_OPERAND (e, 1));
978 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
979 break;
981 default:
982 pp_cxx_conditional_expression (pp, e);
983 break;
987 static void
988 pp_cxx_expression (cxx_pretty_printer *pp, tree t)
990 switch (TREE_CODE (t))
992 case STRING_CST:
993 case INTEGER_CST:
994 case REAL_CST:
995 case COMPLEX_CST:
996 pp_cxx_constant (pp, t);
997 break;
999 case RESULT_DECL:
1000 pp_cxx_unqualified_id (pp, t);
1001 break;
1003 #if 0
1004 case OFFSET_REF:
1005 #endif
1006 case SCOPE_REF:
1007 case PTRMEM_CST:
1008 pp_cxx_qualified_id (pp, t);
1009 break;
1011 case OVERLOAD:
1012 t = OVL_CURRENT (t);
1013 case VAR_DECL:
1014 case PARM_DECL:
1015 case FIELD_DECL:
1016 case CONST_DECL:
1017 case FUNCTION_DECL:
1018 case BASELINK:
1019 case TEMPLATE_DECL:
1020 case TEMPLATE_TYPE_PARM:
1021 case TEMPLATE_PARM_INDEX:
1022 case TEMPLATE_TEMPLATE_PARM:
1023 case STMT_EXPR:
1024 pp_cxx_primary_expression (pp, t);
1025 break;
1027 case CALL_EXPR:
1028 case DYNAMIC_CAST_EXPR:
1029 case STATIC_CAST_EXPR:
1030 case REINTERPRET_CAST_EXPR:
1031 case CONST_CAST_EXPR:
1032 #if 0
1033 case MEMBER_REF:
1034 #endif
1035 case EMPTY_CLASS_EXPR:
1036 case TYPEID_EXPR:
1037 case PSEUDO_DTOR_EXPR:
1038 case AGGR_INIT_EXPR:
1039 case ARROW_EXPR:
1040 pp_cxx_postfix_expression (pp, t);
1041 break;
1043 case NEW_EXPR:
1044 case VEC_NEW_EXPR:
1045 pp_cxx_new_expression (pp, t);
1046 break;
1048 case DELETE_EXPR:
1049 case VEC_DELETE_EXPR:
1050 pp_cxx_delete_expression (pp, t);
1051 break;
1053 case SIZEOF_EXPR:
1054 case ALIGNOF_EXPR:
1055 pp_cxx_unary_expression (pp, t);
1056 break;
1058 case CAST_EXPR:
1059 pp_cxx_cast_expression (pp, t);
1060 break;
1062 case OFFSET_REF:
1063 case MEMBER_REF:
1064 case DOTSTAR_EXPR:
1065 pp_cxx_pm_expression (pp, t);
1066 break;
1068 case MULT_EXPR:
1069 case TRUNC_DIV_EXPR:
1070 case TRUNC_MOD_EXPR:
1071 pp_cxx_multiplicative_expression (pp, t);
1072 break;
1074 case COND_EXPR:
1075 pp_cxx_conditional_expression (pp, t);
1076 break;
1078 case MODIFY_EXPR:
1079 case INIT_EXPR:
1080 case THROW_EXPR:
1081 case MODOP_EXPR:
1082 pp_cxx_assignment_expression (pp, t);
1083 break;
1085 case NON_DEPENDENT_EXPR:
1086 case MUST_NOT_THROW_EXPR:
1087 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
1088 break;
1090 case EXPR_PACK_EXPANSION:
1091 pp_cxx_expression (pp, PACK_EXPANSION_PATTERN (t));
1092 pp_cxx_ws_string (pp, "...");
1093 break;
1095 case NONTYPE_ARGUMENT_PACK:
1097 tree args = ARGUMENT_PACK_ARGS (t);
1098 int i, len = TREE_VEC_LENGTH (args);
1099 for (i = 0; i < len; ++i)
1101 if (i > 0)
1102 pp_cxx_separate_with (pp, ',');
1103 pp_cxx_expression (pp, TREE_VEC_ELT (args, i));
1106 break;
1108 default:
1109 pp_c_expression (pp_c_base (pp), t);
1110 break;
1115 /* Declarations. */
1117 /* function-specifier:
1118 inline
1119 virtual
1120 explicit */
1122 static void
1123 pp_cxx_function_specifier (cxx_pretty_printer *pp, tree t)
1125 switch (TREE_CODE (t))
1127 case FUNCTION_DECL:
1128 if (DECL_VIRTUAL_P (t))
1129 pp_cxx_ws_string (pp, "virtual");
1130 else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t))
1131 pp_cxx_ws_string (pp, "explicit");
1132 else
1133 pp_c_function_specifier (pp_c_base (pp), t);
1135 default:
1136 break;
1140 /* decl-specifier-seq:
1141 decl-specifier-seq(opt) decl-specifier
1143 decl-specifier:
1144 storage-class-specifier
1145 type-specifier
1146 function-specifier
1147 friend
1148 typedef */
1150 static void
1151 pp_cxx_decl_specifier_seq (cxx_pretty_printer *pp, tree t)
1153 switch (TREE_CODE (t))
1155 case VAR_DECL:
1156 case PARM_DECL:
1157 case CONST_DECL:
1158 case FIELD_DECL:
1159 pp_cxx_storage_class_specifier (pp, t);
1160 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1161 break;
1163 case TYPE_DECL:
1164 pp_cxx_ws_string (pp, "typedef");
1165 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1166 break;
1168 case RECORD_TYPE:
1169 if (TYPE_PTRMEMFUNC_P (t))
1171 tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t);
1172 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (pfm)));
1173 pp_cxx_whitespace (pp);
1174 pp_cxx_ptr_operator (pp, t);
1176 break;
1178 case FUNCTION_DECL:
1179 /* Constructors don't have return types. And conversion functions
1180 do not have a type-specifier in their return types. */
1181 if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t))
1182 pp_cxx_function_specifier (pp, t);
1183 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1184 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (t)));
1185 else
1186 default:
1187 pp_c_declaration_specifiers (pp_c_base (pp), t);
1188 break;
1192 /* simple-type-specifier:
1193 ::(opt) nested-name-specifier(opt) type-name
1194 ::(opt) nested-name-specifier(opt) template(opt) template-id
1195 char
1196 wchar_t
1197 bool
1198 short
1200 long
1201 signed
1202 unsigned
1203 float
1204 double
1205 void */
1207 static void
1208 pp_cxx_simple_type_specifier (cxx_pretty_printer *pp, tree t)
1210 switch (TREE_CODE (t))
1212 case RECORD_TYPE:
1213 case UNION_TYPE:
1214 case ENUMERAL_TYPE:
1215 pp_cxx_qualified_id (pp, t);
1216 break;
1218 case TEMPLATE_TYPE_PARM:
1219 case TEMPLATE_TEMPLATE_PARM:
1220 case TEMPLATE_PARM_INDEX:
1221 pp_cxx_unqualified_id (pp, t);
1222 break;
1224 case TYPENAME_TYPE:
1225 pp_cxx_ws_string (pp, "typename");
1226 pp_cxx_nested_name_specifier (pp, TYPE_CONTEXT (t));
1227 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
1228 break;
1230 default:
1231 pp_c_type_specifier (pp_c_base (pp), t);
1232 break;
1236 /* type-specifier-seq:
1237 type-specifier type-specifier-seq(opt)
1239 type-specifier:
1240 simple-type-specifier
1241 class-specifier
1242 enum-specifier
1243 elaborated-type-specifier
1244 cv-qualifier */
1246 static void
1247 pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t)
1249 switch (TREE_CODE (t))
1251 case TEMPLATE_DECL:
1252 case TEMPLATE_TYPE_PARM:
1253 case TEMPLATE_TEMPLATE_PARM:
1254 case TYPE_DECL:
1255 case BOUND_TEMPLATE_TEMPLATE_PARM:
1256 pp_cxx_cv_qualifier_seq (pp, t);
1257 pp_cxx_simple_type_specifier (pp, t);
1258 break;
1260 case METHOD_TYPE:
1261 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
1262 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1263 pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t));
1264 break;
1266 case DECLTYPE_TYPE:
1267 pp_cxx_ws_string (pp, "decltype");
1268 pp_cxx_left_paren (pp);
1269 pp_cxx_expression (pp, DECLTYPE_TYPE_EXPR (t));
1270 pp_cxx_right_paren (pp);
1271 break;
1273 default:
1274 if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t)))
1275 pp_c_specifier_qualifier_list (pp_c_base (pp), t);
1279 /* ptr-operator:
1280 * cv-qualifier-seq(opt)
1282 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1284 static void
1285 pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t)
1287 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
1288 t = TREE_TYPE (t);
1289 switch (TREE_CODE (t))
1291 case REFERENCE_TYPE:
1292 case POINTER_TYPE:
1293 if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE
1294 || TYPE_PTR_TO_MEMBER_P (TREE_TYPE (t)))
1295 pp_cxx_ptr_operator (pp, TREE_TYPE (t));
1296 if (TREE_CODE (t) == POINTER_TYPE)
1298 pp_star (pp);
1299 pp_cxx_cv_qualifier_seq (pp, t);
1301 else
1302 pp_ampersand (pp);
1303 break;
1305 case RECORD_TYPE:
1306 if (TYPE_PTRMEMFUNC_P (t))
1308 pp_cxx_left_paren (pp);
1309 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t));
1310 pp_star (pp);
1311 break;
1313 case OFFSET_TYPE:
1314 if (TYPE_PTR_TO_MEMBER_P (t))
1316 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
1317 pp_cxx_left_paren (pp);
1318 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t));
1319 pp_star (pp);
1320 pp_cxx_cv_qualifier_seq (pp, t);
1321 break;
1323 /* else fall through. */
1325 default:
1326 pp_unsupported_tree (pp, t);
1327 break;
1331 static inline tree
1332 pp_cxx_implicit_parameter_type (tree mf)
1334 return TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (mf))));
1338 parameter-declaration:
1339 decl-specifier-seq declarator
1340 decl-specifier-seq declarator = assignment-expression
1341 decl-specifier-seq abstract-declarator(opt)
1342 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1344 static inline void
1345 pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t)
1347 pp_cxx_decl_specifier_seq (pp, t);
1348 if (TYPE_P (t))
1349 pp_cxx_abstract_declarator (pp, t);
1350 else
1351 pp_cxx_declarator (pp, t);
1354 /* parameter-declaration-clause:
1355 parameter-declaration-list(opt) ...(opt)
1356 parameter-declaration-list , ...
1358 parameter-declaration-list:
1359 parameter-declaration
1360 parameter-declaration-list , parameter-declaration */
1362 static void
1363 pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t)
1365 tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t);
1366 tree types =
1367 TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t);
1368 const bool abstract = args == NULL
1369 || pp_c_base (pp)->flags & pp_c_flag_abstract;
1370 bool first = true;
1372 /* Skip artificial parameter for nonstatic member functions. */
1373 if (TREE_CODE (t) == METHOD_TYPE)
1374 types = TREE_CHAIN (types);
1376 pp_cxx_left_paren (pp);
1377 for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types))
1379 if (!first)
1380 pp_cxx_separate_with (pp, ',');
1381 first = false;
1382 pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args);
1383 if (!abstract && pp_c_base (pp)->flags & pp_cxx_flag_default_argument)
1385 pp_cxx_whitespace (pp);
1386 pp_equal (pp);
1387 pp_cxx_whitespace (pp);
1388 pp_cxx_assignment_expression (pp, TREE_PURPOSE (types));
1391 pp_cxx_right_paren (pp);
1394 /* exception-specification:
1395 throw ( type-id-list(opt) )
1397 type-id-list
1398 type-id
1399 type-id-list , type-id */
1401 static void
1402 pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t)
1404 tree ex_spec = TYPE_RAISES_EXCEPTIONS (t);
1405 bool need_comma = false;
1407 if (!TYPE_NOTHROW_P (t) && ex_spec == NULL)
1408 return;
1409 pp_cxx_ws_string (pp, "throw");
1410 pp_cxx_left_paren (pp);
1411 for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec))
1413 tree type = TREE_VALUE (ex_spec);
1414 tree argpack = NULL_TREE;
1415 int i, len = 1;
1417 if (ARGUMENT_PACK_P (type))
1419 argpack = ARGUMENT_PACK_ARGS (type);
1420 len = TREE_VEC_LENGTH (argpack);
1423 for (i = 0; i < len; ++i)
1425 if (argpack)
1426 type = TREE_VEC_ELT (argpack, i);
1428 if (need_comma)
1429 pp_cxx_separate_with (pp, ',');
1430 else
1431 need_comma = true;
1433 pp_cxx_type_id (pp, type);
1436 pp_cxx_right_paren (pp);
1439 /* direct-declarator:
1440 declarator-id
1441 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1442 exception-specification(opt)
1443 direct-declaration [ constant-expression(opt) ]
1444 ( declarator ) */
1446 static void
1447 pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t)
1449 switch (TREE_CODE (t))
1451 case VAR_DECL:
1452 case PARM_DECL:
1453 case CONST_DECL:
1454 case FIELD_DECL:
1455 if (DECL_NAME (t))
1457 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1459 if ((TREE_CODE (t) == PARM_DECL && FUNCTION_PARAMETER_PACK_P (t))
1460 || template_parameter_pack_p (t))
1461 /* A function parameter pack or non-type template
1462 parameter pack. */
1463 pp_cxx_ws_string (pp, "...");
1465 pp_cxx_id_expression (pp, DECL_NAME (t));
1467 pp_cxx_abstract_declarator (pp, TREE_TYPE (t));
1468 break;
1470 case FUNCTION_DECL:
1471 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
1472 pp_cxx_id_expression (pp, t);
1473 pp_cxx_parameter_declaration_clause (pp, t);
1475 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1477 pp_base (pp)->padding = pp_before;
1478 pp_cxx_cv_qualifier_seq (pp, pp_cxx_implicit_parameter_type (t));
1481 pp_cxx_exception_specification (pp, TREE_TYPE (t));
1482 break;
1484 case TYPENAME_TYPE:
1485 case TEMPLATE_DECL:
1486 case TEMPLATE_TYPE_PARM:
1487 case TEMPLATE_PARM_INDEX:
1488 case TEMPLATE_TEMPLATE_PARM:
1489 break;
1491 default:
1492 pp_c_direct_declarator (pp_c_base (pp), t);
1493 break;
1497 /* declarator:
1498 direct-declarator
1499 ptr-operator declarator */
1501 static void
1502 pp_cxx_declarator (cxx_pretty_printer *pp, tree t)
1504 pp_cxx_direct_declarator (pp, t);
1507 /* ctor-initializer:
1508 : mem-initializer-list
1510 mem-initializer-list:
1511 mem-initializer
1512 mem-initializer , mem-initializer-list
1514 mem-initializer:
1515 mem-initializer-id ( expression-list(opt) )
1517 mem-initializer-id:
1518 ::(opt) nested-name-specifier(opt) class-name
1519 identifier */
1521 static void
1522 pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t)
1524 t = TREE_OPERAND (t, 0);
1525 pp_cxx_whitespace (pp);
1526 pp_colon (pp);
1527 pp_cxx_whitespace (pp);
1528 for (; t; t = TREE_CHAIN (t))
1530 tree purpose = TREE_PURPOSE (t);
1531 bool is_pack = PACK_EXPANSION_P (purpose);
1533 if (is_pack)
1534 pp_cxx_primary_expression (pp, PACK_EXPANSION_PATTERN (purpose));
1535 else
1536 pp_cxx_primary_expression (pp, purpose);
1537 pp_cxx_call_argument_list (pp, TREE_VALUE (t));
1538 if (is_pack)
1539 pp_cxx_ws_string (pp, "...");
1540 if (TREE_CHAIN (t))
1541 pp_cxx_separate_with (pp, ',');
1545 /* function-definition:
1546 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1547 decl-specifier-seq(opt) declarator function-try-block */
1549 static void
1550 pp_cxx_function_definition (cxx_pretty_printer *pp, tree t)
1552 tree saved_scope = pp->enclosing_scope;
1553 pp_cxx_decl_specifier_seq (pp, t);
1554 pp_cxx_declarator (pp, t);
1555 pp_needs_newline (pp) = true;
1556 pp->enclosing_scope = DECL_CONTEXT (t);
1557 if (DECL_SAVED_TREE (t))
1558 pp_cxx_statement (pp, DECL_SAVED_TREE (t));
1559 else
1561 pp_cxx_semicolon (pp);
1562 pp_needs_newline (pp) = true;
1564 pp_flush (pp);
1565 pp->enclosing_scope = saved_scope;
1568 /* abstract-declarator:
1569 ptr-operator abstract-declarator(opt)
1570 direct-abstract-declarator */
1572 static void
1573 pp_cxx_abstract_declarator (cxx_pretty_printer *pp, tree t)
1575 if (TYPE_PTRMEM_P (t) || TYPE_PTRMEMFUNC_P (t))
1576 pp_cxx_right_paren (pp);
1577 else if (POINTER_TYPE_P (t))
1579 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1580 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1581 pp_cxx_right_paren (pp);
1582 t = TREE_TYPE (t);
1584 pp_cxx_direct_abstract_declarator (pp, t);
1587 /* direct-abstract-declarator:
1588 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1589 cv-qualifier-seq(opt) exception-specification(opt)
1590 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1591 ( abstract-declarator ) */
1593 static void
1594 pp_cxx_direct_abstract_declarator (cxx_pretty_printer *pp, tree t)
1596 switch (TREE_CODE (t))
1598 case REFERENCE_TYPE:
1599 pp_cxx_abstract_declarator (pp, t);
1600 break;
1602 case RECORD_TYPE:
1603 if (TYPE_PTRMEMFUNC_P (t))
1604 pp_cxx_direct_abstract_declarator (pp, TYPE_PTRMEMFUNC_FN_TYPE (t));
1605 break;
1607 case METHOD_TYPE:
1608 case FUNCTION_TYPE:
1609 pp_cxx_parameter_declaration_clause (pp, t);
1610 pp_cxx_direct_abstract_declarator (pp, TREE_TYPE (t));
1611 if (TREE_CODE (t) == METHOD_TYPE)
1613 pp_base (pp)->padding = pp_before;
1614 pp_cxx_cv_qualifier_seq
1615 (pp, TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))));
1617 pp_cxx_exception_specification (pp, t);
1618 break;
1620 case TYPENAME_TYPE:
1621 case TEMPLATE_TYPE_PARM:
1622 case TEMPLATE_TEMPLATE_PARM:
1623 case BOUND_TEMPLATE_TEMPLATE_PARM:
1624 case UNBOUND_CLASS_TEMPLATE:
1625 break;
1627 default:
1628 pp_c_direct_abstract_declarator (pp_c_base (pp), t);
1629 break;
1633 /* type-id:
1634 type-specifier-seq abstract-declarator(opt) */
1636 static void
1637 pp_cxx_type_id (cxx_pretty_printer *pp, tree t)
1639 pp_flags saved_flags = pp_c_base (pp)->flags;
1640 pp_c_base (pp)->flags |= pp_c_flag_abstract;
1642 switch (TREE_CODE (t))
1644 case TYPE_DECL:
1645 case UNION_TYPE:
1646 case RECORD_TYPE:
1647 case ENUMERAL_TYPE:
1648 case TYPENAME_TYPE:
1649 case BOUND_TEMPLATE_TEMPLATE_PARM:
1650 case UNBOUND_CLASS_TEMPLATE:
1651 case TEMPLATE_TEMPLATE_PARM:
1652 case TEMPLATE_TYPE_PARM:
1653 case TEMPLATE_PARM_INDEX:
1654 case TEMPLATE_DECL:
1655 case TYPEOF_TYPE:
1656 case DECLTYPE_TYPE:
1657 case TEMPLATE_ID_EXPR:
1658 pp_cxx_type_specifier_seq (pp, t);
1659 break;
1661 case TYPE_PACK_EXPANSION:
1662 pp_cxx_type_id (pp, PACK_EXPANSION_PATTERN (t));
1663 pp_cxx_ws_string (pp, "...");
1664 break;
1666 default:
1667 pp_c_type_id (pp_c_base (pp), t);
1668 break;
1671 pp_c_base (pp)->flags = saved_flags;
1674 /* template-argument-list:
1675 template-argument ...(opt)
1676 template-argument-list, template-argument ...(opt)
1678 template-argument:
1679 assignment-expression
1680 type-id
1681 template-name */
1683 static void
1684 pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t)
1686 int i;
1687 bool need_comma = false;
1689 if (t == NULL)
1690 return;
1691 for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
1693 tree arg = TREE_VEC_ELT (t, i);
1694 tree argpack = NULL_TREE;
1695 int idx, len = 1;
1697 if (ARGUMENT_PACK_P (arg))
1699 argpack = ARGUMENT_PACK_ARGS (arg);
1700 len = TREE_VEC_LENGTH (argpack);
1703 for (idx = 0; idx < len; idx++)
1705 if (argpack)
1706 arg = TREE_VEC_ELT (argpack, idx);
1708 if (need_comma)
1709 pp_cxx_separate_with (pp, ',');
1710 else
1711 need_comma = true;
1713 if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL
1714 && TYPE_P (DECL_TEMPLATE_RESULT (arg))))
1715 pp_cxx_type_id (pp, arg);
1716 else
1717 pp_cxx_expression (pp, arg);
1723 static void
1724 pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t)
1726 t = DECL_EXPR_DECL (t);
1727 pp_cxx_type_specifier_seq (pp, t);
1728 if (TYPE_P (t))
1729 pp_cxx_abstract_declarator (pp, t);
1730 else
1731 pp_cxx_declarator (pp, t);
1734 /* Statements. */
1736 static void
1737 pp_cxx_statement (cxx_pretty_printer *pp, tree t)
1739 switch (TREE_CODE (t))
1741 case CTOR_INITIALIZER:
1742 pp_cxx_ctor_initializer (pp, t);
1743 break;
1745 case USING_STMT:
1746 pp_cxx_ws_string (pp, "using");
1747 pp_cxx_ws_string (pp, "namespace");
1748 if (DECL_CONTEXT (t))
1749 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1750 pp_cxx_qualified_id (pp, USING_STMT_NAMESPACE (t));
1751 break;
1753 case USING_DECL:
1754 pp_cxx_ws_string (pp, "using");
1755 pp_cxx_nested_name_specifier (pp, USING_DECL_SCOPE (t));
1756 pp_cxx_unqualified_id (pp, DECL_NAME (t));
1757 break;
1759 case EH_SPEC_BLOCK:
1760 break;
1762 /* try-block:
1763 try compound-statement handler-seq */
1764 case TRY_BLOCK:
1765 pp_maybe_newline_and_indent (pp, 0);
1766 pp_cxx_ws_string (pp, "try");
1767 pp_newline_and_indent (pp, 3);
1768 pp_cxx_statement (pp, TRY_STMTS (t));
1769 pp_newline_and_indent (pp, -3);
1770 if (CLEANUP_P (t))
1772 else
1773 pp_cxx_statement (pp, TRY_HANDLERS (t));
1774 break;
1777 handler-seq:
1778 handler handler-seq(opt)
1780 handler:
1781 catch ( exception-declaration ) compound-statement
1783 exception-declaration:
1784 type-specifier-seq declarator
1785 type-specifier-seq abstract-declarator
1786 ... */
1787 case HANDLER:
1788 pp_cxx_ws_string (pp, "catch");
1789 pp_cxx_left_paren (pp);
1790 pp_cxx_exception_declaration (pp, HANDLER_PARMS (t));
1791 pp_cxx_right_paren (pp);
1792 pp_indentation (pp) += 3;
1793 pp_needs_newline (pp) = true;
1794 pp_cxx_statement (pp, HANDLER_BODY (t));
1795 pp_indentation (pp) -= 3;
1796 pp_needs_newline (pp) = true;
1797 break;
1799 /* selection-statement:
1800 if ( expression ) statement
1801 if ( expression ) statement else statement */
1802 case IF_STMT:
1803 pp_cxx_ws_string (pp, "if");
1804 pp_cxx_whitespace (pp);
1805 pp_cxx_left_paren (pp);
1806 pp_cxx_expression (pp, IF_COND (t));
1807 pp_cxx_right_paren (pp);
1808 pp_newline_and_indent (pp, 2);
1809 pp_cxx_statement (pp, THEN_CLAUSE (t));
1810 pp_newline_and_indent (pp, -2);
1811 if (ELSE_CLAUSE (t))
1813 tree else_clause = ELSE_CLAUSE (t);
1814 pp_cxx_ws_string (pp, "else");
1815 if (TREE_CODE (else_clause) == IF_STMT)
1816 pp_cxx_whitespace (pp);
1817 else
1818 pp_newline_and_indent (pp, 2);
1819 pp_cxx_statement (pp, else_clause);
1820 if (TREE_CODE (else_clause) != IF_STMT)
1821 pp_newline_and_indent (pp, -2);
1823 break;
1825 case SWITCH_STMT:
1826 pp_cxx_ws_string (pp, "switch");
1827 pp_space (pp);
1828 pp_cxx_left_paren (pp);
1829 pp_cxx_expression (pp, SWITCH_STMT_COND (t));
1830 pp_cxx_right_paren (pp);
1831 pp_indentation (pp) += 3;
1832 pp_needs_newline (pp) = true;
1833 pp_cxx_statement (pp, SWITCH_STMT_BODY (t));
1834 pp_newline_and_indent (pp, -3);
1835 break;
1837 /* iteration-statement:
1838 while ( expression ) statement
1839 do statement while ( expression ) ;
1840 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1841 for ( declaration expression(opt) ; expression(opt) ) statement */
1842 case WHILE_STMT:
1843 pp_cxx_ws_string (pp, "while");
1844 pp_space (pp);
1845 pp_cxx_left_paren (pp);
1846 pp_cxx_expression (pp, WHILE_COND (t));
1847 pp_cxx_right_paren (pp);
1848 pp_newline_and_indent (pp, 3);
1849 pp_cxx_statement (pp, WHILE_BODY (t));
1850 pp_indentation (pp) -= 3;
1851 pp_needs_newline (pp) = true;
1852 break;
1854 case DO_STMT:
1855 pp_cxx_ws_string (pp, "do");
1856 pp_newline_and_indent (pp, 3);
1857 pp_cxx_statement (pp, DO_BODY (t));
1858 pp_newline_and_indent (pp, -3);
1859 pp_cxx_ws_string (pp, "while");
1860 pp_space (pp);
1861 pp_cxx_left_paren (pp);
1862 pp_cxx_expression (pp, DO_COND (t));
1863 pp_cxx_right_paren (pp);
1864 pp_cxx_semicolon (pp);
1865 pp_needs_newline (pp) = true;
1866 break;
1868 case FOR_STMT:
1869 pp_cxx_ws_string (pp, "for");
1870 pp_space (pp);
1871 pp_cxx_left_paren (pp);
1872 if (FOR_INIT_STMT (t))
1873 pp_cxx_statement (pp, FOR_INIT_STMT (t));
1874 else
1875 pp_cxx_semicolon (pp);
1876 pp_needs_newline (pp) = false;
1877 pp_cxx_whitespace (pp);
1878 if (FOR_COND (t))
1879 pp_cxx_expression (pp, FOR_COND (t));
1880 pp_cxx_semicolon (pp);
1881 pp_needs_newline (pp) = false;
1882 pp_cxx_whitespace (pp);
1883 if (FOR_EXPR (t))
1884 pp_cxx_expression (pp, FOR_EXPR (t));
1885 pp_cxx_right_paren (pp);
1886 pp_newline_and_indent (pp, 3);
1887 pp_cxx_statement (pp, FOR_BODY (t));
1888 pp_indentation (pp) -= 3;
1889 pp_needs_newline (pp) = true;
1890 break;
1892 /* jump-statement:
1893 goto identifier;
1894 continue ;
1895 return expression(opt) ; */
1896 case BREAK_STMT:
1897 case CONTINUE_STMT:
1898 pp_string (pp, TREE_CODE (t) == BREAK_STMT ? "break" : "continue");
1899 pp_cxx_semicolon (pp);
1900 pp_needs_newline (pp) = true;
1901 break;
1903 /* expression-statement:
1904 expression(opt) ; */
1905 case EXPR_STMT:
1906 pp_cxx_expression (pp, EXPR_STMT_EXPR (t));
1907 pp_cxx_semicolon (pp);
1908 pp_needs_newline (pp) = true;
1909 break;
1911 case CLEANUP_STMT:
1912 pp_cxx_ws_string (pp, "try");
1913 pp_newline_and_indent (pp, 2);
1914 pp_cxx_statement (pp, CLEANUP_BODY (t));
1915 pp_newline_and_indent (pp, -2);
1916 pp_cxx_ws_string (pp, CLEANUP_EH_ONLY (t) ? "catch" : "finally");
1917 pp_newline_and_indent (pp, 2);
1918 pp_cxx_statement (pp, CLEANUP_EXPR (t));
1919 pp_newline_and_indent (pp, -2);
1920 break;
1922 case STATIC_ASSERT:
1923 pp_cxx_declaration (pp, t);
1924 break;
1926 default:
1927 pp_c_statement (pp_c_base (pp), t);
1928 break;
1932 /* original-namespace-definition:
1933 namespace identifier { namespace-body }
1935 As an edge case, we also handle unnamed namespace definition here. */
1937 static void
1938 pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t)
1940 pp_cxx_ws_string (pp, "namespace");
1941 if (DECL_CONTEXT (t))
1942 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1943 if (DECL_NAME (t))
1944 pp_cxx_unqualified_id (pp, t);
1945 pp_cxx_whitespace (pp);
1946 pp_cxx_left_brace (pp);
1947 /* We do not print the namespace-body. */
1948 pp_cxx_whitespace (pp);
1949 pp_cxx_right_brace (pp);
1952 /* namespace-alias:
1953 identifier
1955 namespace-alias-definition:
1956 namespace identifier = qualified-namespace-specifier ;
1958 qualified-namespace-specifier:
1959 ::(opt) nested-name-specifier(opt) namespace-name */
1961 static void
1962 pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t)
1964 pp_cxx_ws_string (pp, "namespace");
1965 if (DECL_CONTEXT (t))
1966 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1967 pp_cxx_unqualified_id (pp, t);
1968 pp_cxx_whitespace (pp);
1969 pp_equal (pp);
1970 pp_cxx_whitespace (pp);
1971 if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)))
1972 pp_cxx_nested_name_specifier (pp,
1973 DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)));
1974 pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t));
1975 pp_cxx_semicolon (pp);
1978 /* simple-declaration:
1979 decl-specifier-seq(opt) init-declarator-list(opt) */
1981 static void
1982 pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t)
1984 pp_cxx_decl_specifier_seq (pp, t);
1985 pp_cxx_init_declarator (pp, t);
1986 pp_cxx_semicolon (pp);
1987 pp_needs_newline (pp) = true;
1991 template-parameter-list:
1992 template-parameter
1993 template-parameter-list , template-parameter */
1995 static inline void
1996 pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t)
1998 const int n = TREE_VEC_LENGTH (t);
1999 int i;
2000 for (i = 0; i < n; ++i)
2002 if (i)
2003 pp_cxx_separate_with (pp, ',');
2004 pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i));
2008 /* template-parameter:
2009 type-parameter
2010 parameter-declaration
2012 type-parameter:
2013 class ...(opt) identifier(opt)
2014 class identifier(opt) = type-id
2015 typename identifier(opt)
2016 typename ...(opt) identifier(opt) = type-id
2017 template < template-parameter-list > class ...(opt) identifier(opt)
2018 template < template-parameter-list > class identifier(opt) = template-name */
2020 static void
2021 pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t)
2023 tree parameter = TREE_VALUE (t);
2024 switch (TREE_CODE (parameter))
2026 case TYPE_DECL:
2027 pp_cxx_ws_string (pp, "class");
2028 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
2029 pp_cxx_ws_string (pp, "...");
2030 if (DECL_NAME (parameter))
2031 pp_cxx_tree_identifier (pp, DECL_NAME (parameter));
2032 /* FIXME: Check if we should print also default argument. */
2033 break;
2035 case PARM_DECL:
2036 pp_cxx_parameter_declaration (pp, parameter);
2037 break;
2039 case TEMPLATE_DECL:
2040 break;
2042 default:
2043 pp_unsupported_tree (pp, t);
2044 break;
2048 /* Pretty-print a template parameter in the canonical form
2049 "template-parameter-<level>-<position in parameter list>". */
2051 void
2052 pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm)
2054 const enum tree_code code = TREE_CODE (parm);
2056 /* Brings type template parameters to the canonical forms. */
2057 if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM
2058 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
2059 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
2061 pp_cxx_begin_template_argument_list (pp);
2062 pp_cxx_ws_string (pp, M_("template-parameter-"));
2063 pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm));
2064 pp_minus (pp);
2065 pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1);
2066 pp_cxx_end_template_argument_list (pp);
2070 template-declaration:
2071 export(opt) template < template-parameter-list > declaration */
2073 static void
2074 pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t)
2076 tree tmpl = most_general_template (t);
2077 tree level;
2078 int i = 0;
2080 pp_maybe_newline_and_indent (pp, 0);
2081 for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level))
2083 pp_cxx_ws_string (pp, "template");
2084 pp_cxx_begin_template_argument_list (pp);
2085 pp_cxx_template_parameter_list (pp, TREE_VALUE (level));
2086 pp_cxx_end_template_argument_list (pp);
2087 pp_newline_and_indent (pp, 3);
2088 i += 3;
2090 if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t))
2091 pp_cxx_function_definition (pp, t);
2092 else
2093 pp_cxx_simple_declaration (pp, t);
2096 static void
2097 pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t)
2099 pp_unsupported_tree (pp, t);
2102 static void
2103 pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t)
2105 pp_unsupported_tree (pp, t);
2109 declaration:
2110 block-declaration
2111 function-definition
2112 template-declaration
2113 explicit-instantiation
2114 explicit-specialization
2115 linkage-specification
2116 namespace-definition
2118 block-declaration:
2119 simple-declaration
2120 asm-definition
2121 namespace-alias-definition
2122 using-declaration
2123 using-directive
2124 static_assert-declaration */
2125 void
2126 pp_cxx_declaration (cxx_pretty_printer *pp, tree t)
2128 if (TREE_CODE (t) == STATIC_ASSERT)
2130 pp_cxx_ws_string (pp, "static_assert");
2131 pp_cxx_left_paren (pp);
2132 pp_cxx_expression (pp, STATIC_ASSERT_CONDITION (t));
2133 pp_cxx_separate_with (pp, ',');
2134 pp_cxx_expression (pp, STATIC_ASSERT_MESSAGE (t));
2135 pp_cxx_right_paren (pp);
2137 else if (!DECL_LANG_SPECIFIC (t))
2138 pp_cxx_simple_declaration (pp, t);
2139 else if (DECL_USE_TEMPLATE (t))
2140 switch (DECL_USE_TEMPLATE (t))
2142 case 1:
2143 pp_cxx_template_declaration (pp, t);
2144 break;
2146 case 2:
2147 pp_cxx_explicit_specialization (pp, t);
2148 break;
2150 case 3:
2151 pp_cxx_explicit_instantiation (pp, t);
2152 break;
2154 default:
2155 break;
2157 else switch (TREE_CODE (t))
2159 case VAR_DECL:
2160 case TYPE_DECL:
2161 pp_cxx_simple_declaration (pp, t);
2162 break;
2164 case FUNCTION_DECL:
2165 if (DECL_SAVED_TREE (t))
2166 pp_cxx_function_definition (pp, t);
2167 else
2168 pp_cxx_simple_declaration (pp, t);
2169 break;
2171 case NAMESPACE_DECL:
2172 if (DECL_NAMESPACE_ALIAS (t))
2173 pp_cxx_namespace_alias_definition (pp, t);
2174 else
2175 pp_cxx_original_namespace_definition (pp, t);
2176 break;
2178 default:
2179 pp_unsupported_tree (pp, t);
2180 break;
2184 static void
2185 pp_cxx_typeid_expression (cxx_pretty_printer *pp, tree t)
2187 t = TREE_OPERAND (t, 0);
2188 pp_cxx_ws_string (pp, "typeid");
2189 pp_cxx_left_paren (pp);
2190 if (TYPE_P (t))
2191 pp_cxx_type_id (pp, t);
2192 else
2193 pp_cxx_expression (pp, t);
2194 pp_cxx_right_paren (pp);
2197 void
2198 pp_cxx_va_arg_expression (cxx_pretty_printer *pp, tree t)
2200 pp_cxx_ws_string (pp, "va_arg");
2201 pp_cxx_left_paren (pp);
2202 pp_cxx_assignment_expression (pp, TREE_OPERAND (t, 0));
2203 pp_cxx_separate_with (pp, ',');
2204 pp_cxx_type_id (pp, TREE_TYPE (t));
2205 pp_cxx_right_paren (pp);
2208 static bool
2209 pp_cxx_offsetof_expression_1 (cxx_pretty_printer *pp, tree t)
2211 switch (TREE_CODE (t))
2213 case ARROW_EXPR:
2214 if (TREE_CODE (TREE_OPERAND (t, 0)) == STATIC_CAST_EXPR
2215 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
2217 pp_cxx_type_id (pp, TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))));
2218 pp_cxx_separate_with (pp, ',');
2219 return true;
2221 return false;
2222 case COMPONENT_REF:
2223 if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2224 return false;
2225 if (TREE_CODE (TREE_OPERAND (t, 0)) != ARROW_EXPR)
2226 pp_cxx_dot (pp);
2227 pp_cxx_expression (pp, TREE_OPERAND (t, 1));
2228 return true;
2229 case ARRAY_REF:
2230 if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2231 return false;
2232 pp_left_bracket (pp);
2233 pp_cxx_expression (pp, TREE_OPERAND (t, 1));
2234 pp_right_bracket (pp);
2235 return true;
2236 default:
2237 return false;
2241 void
2242 pp_cxx_offsetof_expression (cxx_pretty_printer *pp, tree t)
2244 pp_cxx_ws_string (pp, "offsetof");
2245 pp_cxx_left_paren (pp);
2246 if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2247 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
2248 pp_cxx_right_paren (pp);
2251 void
2252 pp_cxx_trait_expression (cxx_pretty_printer *pp, tree t)
2254 cp_trait_kind kind = TRAIT_EXPR_KIND (t);
2256 switch (kind)
2258 case CPTK_HAS_NOTHROW_ASSIGN:
2259 pp_cxx_ws_string (pp, "__has_nothrow_assign");
2260 break;
2261 case CPTK_HAS_TRIVIAL_ASSIGN:
2262 pp_cxx_ws_string (pp, "__has_trivial_assign");
2263 break;
2264 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
2265 pp_cxx_ws_string (pp, "__has_nothrow_constructor");
2266 break;
2267 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
2268 pp_cxx_ws_string (pp, "__has_trivial_constructor");
2269 break;
2270 case CPTK_HAS_NOTHROW_COPY:
2271 pp_cxx_ws_string (pp, "__has_nothrow_copy");
2272 break;
2273 case CPTK_HAS_TRIVIAL_COPY:
2274 pp_cxx_ws_string (pp, "__has_trivial_copy");
2275 break;
2276 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
2277 pp_cxx_ws_string (pp, "__has_trivial_destructor");
2278 break;
2279 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
2280 pp_cxx_ws_string (pp, "__has_virtual_destructor");
2281 break;
2282 case CPTK_IS_ABSTRACT:
2283 pp_cxx_ws_string (pp, "__is_abstract");
2284 break;
2285 case CPTK_IS_BASE_OF:
2286 pp_cxx_ws_string (pp, "__is_base_of");
2287 break;
2288 case CPTK_IS_CLASS:
2289 pp_cxx_ws_string (pp, "__is_class");
2290 break;
2291 case CPTK_IS_CONVERTIBLE_TO:
2292 pp_cxx_ws_string (pp, "__is_convertible_to");
2293 break;
2294 case CPTK_IS_EMPTY:
2295 pp_cxx_ws_string (pp, "__is_empty");
2296 break;
2297 case CPTK_IS_ENUM:
2298 pp_cxx_ws_string (pp, "__is_enum");
2299 break;
2300 case CPTK_IS_POD:
2301 pp_cxx_ws_string (pp, "__is_pod");
2302 break;
2303 case CPTK_IS_POLYMORPHIC:
2304 pp_cxx_ws_string (pp, "__is_polymorphic");
2305 break;
2306 case CPTK_IS_STD_LAYOUT:
2307 pp_cxx_ws_string (pp, "__is_std_layout");
2308 break;
2309 case CPTK_IS_TRIVIAL:
2310 pp_cxx_ws_string (pp, "__is_trivial");
2311 break;
2312 case CPTK_IS_UNION:
2313 pp_cxx_ws_string (pp, "__is_union");
2314 break;
2316 default:
2317 gcc_unreachable ();
2320 pp_cxx_left_paren (pp);
2321 pp_cxx_type_id (pp, TRAIT_EXPR_TYPE1 (t));
2323 if (kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO)
2325 pp_cxx_separate_with (pp, ',');
2326 pp_cxx_type_id (pp, TRAIT_EXPR_TYPE2 (t));
2329 pp_cxx_right_paren (pp);
2332 typedef c_pretty_print_fn pp_fun;
2334 /* Initialization of a C++ pretty-printer object. */
2336 void
2337 pp_cxx_pretty_printer_init (cxx_pretty_printer *pp)
2339 pp_c_pretty_printer_init (pp_c_base (pp));
2340 pp_set_line_maximum_length (pp, 0);
2342 pp->c_base.declaration = (pp_fun) pp_cxx_declaration;
2343 pp->c_base.declaration_specifiers = (pp_fun) pp_cxx_decl_specifier_seq;
2344 pp->c_base.function_specifier = (pp_fun) pp_cxx_function_specifier;
2345 pp->c_base.type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq;
2346 pp->c_base.declarator = (pp_fun) pp_cxx_declarator;
2347 pp->c_base.direct_declarator = (pp_fun) pp_cxx_direct_declarator;
2348 pp->c_base.parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause;
2349 pp->c_base.type_id = (pp_fun) pp_cxx_type_id;
2350 pp->c_base.abstract_declarator = (pp_fun) pp_cxx_abstract_declarator;
2351 pp->c_base.direct_abstract_declarator =
2352 (pp_fun) pp_cxx_direct_abstract_declarator;
2353 pp->c_base.simple_type_specifier = (pp_fun)pp_cxx_simple_type_specifier;
2355 /* pp->c_base.statement = (pp_fun) pp_cxx_statement; */
2357 pp->c_base.constant = (pp_fun) pp_cxx_constant;
2358 pp->c_base.id_expression = (pp_fun) pp_cxx_id_expression;
2359 pp->c_base.primary_expression = (pp_fun) pp_cxx_primary_expression;
2360 pp->c_base.postfix_expression = (pp_fun) pp_cxx_postfix_expression;
2361 pp->c_base.unary_expression = (pp_fun) pp_cxx_unary_expression;
2362 pp->c_base.multiplicative_expression = (pp_fun) pp_cxx_multiplicative_expression;
2363 pp->c_base.conditional_expression = (pp_fun) pp_cxx_conditional_expression;
2364 pp->c_base.assignment_expression = (pp_fun) pp_cxx_assignment_expression;
2365 pp->c_base.expression = (pp_fun) pp_cxx_expression;
2366 pp->enclosing_scope = global_namespace;