c-pretty-print.h (c_pretty_printer::id_expression): Now a virtual function.
[gcc.git] / gcc / cp / cxx-pretty-print.c
1 /* Implementation of subroutines for the GNU C++ pretty-printer.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "intl.h"
26 #include "cp-tree.h"
27 #include "cxx-pretty-print.h"
28 #include "tree-pretty-print.h"
29
30 /* Translate if being used for diagnostics, but not for dump files or
31 __PRETTY_FUNCTION. */
32 #define M_(msgid) (pp_translate_identifiers (pp) ? _(msgid) : (msgid))
33
34 static void pp_cxx_unqualified_id (cxx_pretty_printer *, tree);
35 static void pp_cxx_nested_name_specifier (cxx_pretty_printer *, tree);
36 static void pp_cxx_qualified_id (cxx_pretty_printer *, tree);
37 static void pp_cxx_assignment_expression (cxx_pretty_printer *, tree);
38 static void pp_cxx_expression (cxx_pretty_printer *, tree);
39 static void pp_cxx_template_argument_list (cxx_pretty_printer *, tree);
40 static void pp_cxx_type_specifier_seq (cxx_pretty_printer *, tree);
41 static void pp_cxx_ptr_operator (cxx_pretty_printer *, tree);
42 static void pp_cxx_type_id (cxx_pretty_printer *, tree);
43 static void pp_cxx_direct_abstract_declarator (cxx_pretty_printer *, tree);
44 static void pp_cxx_declarator (cxx_pretty_printer *, tree);
45 static void pp_cxx_parameter_declaration_clause (cxx_pretty_printer *, tree);
46 static void pp_cxx_abstract_declarator (cxx_pretty_printer *, tree);
47 static void pp_cxx_statement (cxx_pretty_printer *, tree);
48 static void pp_cxx_template_parameter (cxx_pretty_printer *, tree);
49 static void pp_cxx_cast_expression (cxx_pretty_printer *, tree);
50 static void pp_cxx_typeid_expression (cxx_pretty_printer *, tree);
51 \f
52
53 static inline void
54 pp_cxx_nonconsecutive_character (cxx_pretty_printer *pp, int c)
55 {
56 const char *p = pp_last_position_in_text (pp);
57
58 if (p != NULL && *p == c)
59 pp_cxx_whitespace (pp);
60 pp_character (pp, c);
61 pp->padding = pp_none;
62 }
63
64 #define pp_cxx_storage_class_specifier(PP, T) \
65 pp_c_storage_class_specifier (PP, T)
66 #define pp_cxx_expression_list(PP, T) \
67 pp_c_expression_list (PP, T)
68 #define pp_cxx_space_for_pointer_operator(PP, T) \
69 pp_c_space_for_pointer_operator (PP, T)
70 #define pp_cxx_init_declarator(PP, T) \
71 pp_c_init_declarator (PP, T)
72 #define pp_cxx_call_argument_list(PP, T) \
73 pp_c_call_argument_list (PP, T)
74
75 void
76 pp_cxx_colon_colon (cxx_pretty_printer *pp)
77 {
78 pp_colon_colon (pp);
79 pp->padding = pp_none;
80 }
81
82 void
83 pp_cxx_begin_template_argument_list (cxx_pretty_printer *pp)
84 {
85 pp_cxx_nonconsecutive_character (pp, '<');
86 }
87
88 void
89 pp_cxx_end_template_argument_list (cxx_pretty_printer *pp)
90 {
91 pp_cxx_nonconsecutive_character (pp, '>');
92 }
93
94 void
95 pp_cxx_separate_with (cxx_pretty_printer *pp, int c)
96 {
97 pp_separate_with (pp, c);
98 pp->padding = pp_none;
99 }
100
101 /* Expressions. */
102
103 static inline bool
104 is_destructor_name (tree name)
105 {
106 return name == complete_dtor_identifier
107 || name == base_dtor_identifier
108 || name == deleting_dtor_identifier;
109 }
110
111 /* conversion-function-id:
112 operator conversion-type-id
113
114 conversion-type-id:
115 type-specifier-seq conversion-declarator(opt)
116
117 conversion-declarator:
118 ptr-operator conversion-declarator(opt) */
119
120 static inline void
121 pp_cxx_conversion_function_id (cxx_pretty_printer *pp, tree t)
122 {
123 pp_cxx_ws_string (pp, "operator");
124 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
125 }
126
127 static inline void
128 pp_cxx_template_id (cxx_pretty_printer *pp, tree t)
129 {
130 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0));
131 pp_cxx_begin_template_argument_list (pp);
132 pp_cxx_template_argument_list (pp, TREE_OPERAND (t, 1));
133 pp_cxx_end_template_argument_list (pp);
134 }
135
136 /* Prints the unqualified part of the id-expression T.
137
138 unqualified-id:
139 identifier
140 operator-function-id
141 conversion-function-id
142 ~ class-name
143 template-id */
144
145 static void
146 pp_cxx_unqualified_id (cxx_pretty_printer *pp, tree t)
147 {
148 enum tree_code code = TREE_CODE (t);
149 switch (code)
150 {
151 case RESULT_DECL:
152 pp_cxx_ws_string (pp, M_("<return-value>"));
153 break;
154
155 case OVERLOAD:
156 t = OVL_CURRENT (t);
157 case VAR_DECL:
158 case PARM_DECL:
159 case CONST_DECL:
160 case TYPE_DECL:
161 case FUNCTION_DECL:
162 case NAMESPACE_DECL:
163 case FIELD_DECL:
164 case LABEL_DECL:
165 case USING_DECL:
166 case TEMPLATE_DECL:
167 t = DECL_NAME (t);
168
169 case IDENTIFIER_NODE:
170 if (t == NULL)
171 pp_cxx_ws_string (pp, M_("<unnamed>"));
172 else if (IDENTIFIER_TYPENAME_P (t))
173 pp_cxx_conversion_function_id (pp, t);
174 else
175 {
176 if (is_destructor_name (t))
177 {
178 pp_complement (pp);
179 /* FIXME: Why is this necessary? */
180 if (TREE_TYPE (t))
181 t = constructor_name (TREE_TYPE (t));
182 }
183 pp_cxx_tree_identifier (pp, t);
184 }
185 break;
186
187 case TEMPLATE_ID_EXPR:
188 pp_cxx_template_id (pp, t);
189 break;
190
191 case BASELINK:
192 pp_cxx_unqualified_id (pp, BASELINK_FUNCTIONS (t));
193 break;
194
195 case RECORD_TYPE:
196 case UNION_TYPE:
197 case ENUMERAL_TYPE:
198 case TYPENAME_TYPE:
199 case UNBOUND_CLASS_TEMPLATE:
200 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
201 if (CLASS_TYPE_P (t) && CLASSTYPE_USE_TEMPLATE (t))
202 {
203 pp_cxx_begin_template_argument_list (pp);
204 pp_cxx_template_argument_list (pp, INNERMOST_TEMPLATE_ARGS
205 (CLASSTYPE_TI_ARGS (t)));
206 pp_cxx_end_template_argument_list (pp);
207 }
208 break;
209
210 case BIT_NOT_EXPR:
211 pp_cxx_complement (pp);
212 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 0));
213 break;
214
215 case TEMPLATE_TYPE_PARM:
216 case TEMPLATE_TEMPLATE_PARM:
217 if (TYPE_IDENTIFIER (t))
218 pp_cxx_unqualified_id (pp, TYPE_IDENTIFIER (t));
219 else
220 pp_cxx_canonical_template_parameter (pp, t);
221 break;
222
223 case TEMPLATE_PARM_INDEX:
224 pp_cxx_unqualified_id (pp, TEMPLATE_PARM_DECL (t));
225 break;
226
227 case BOUND_TEMPLATE_TEMPLATE_PARM:
228 pp_cxx_cv_qualifier_seq (pp, t);
229 pp_cxx_unqualified_id (pp, TYPE_IDENTIFIER (t));
230 pp_cxx_begin_template_argument_list (pp);
231 pp_cxx_template_argument_list (pp, TYPE_TI_ARGS (t));
232 pp_cxx_end_template_argument_list (pp);
233 break;
234
235 default:
236 pp_unsupported_tree (pp, t);
237 break;
238 }
239 }
240
241 /* Pretty-print out the token sequence ":: template" in template codes
242 where it is needed to "inline declare" the (following) member as
243 a template. This situation arises when SCOPE of T is dependent
244 on template parameters. */
245
246 static inline void
247 pp_cxx_template_keyword_if_needed (cxx_pretty_printer *pp, tree scope, tree t)
248 {
249 if (TREE_CODE (t) == TEMPLATE_ID_EXPR
250 && TYPE_P (scope) && dependent_type_p (scope))
251 pp_cxx_ws_string (pp, "template");
252 }
253
254 /* nested-name-specifier:
255 class-or-namespace-name :: nested-name-specifier(opt)
256 class-or-namespace-name :: template nested-name-specifier */
257
258 static void
259 pp_cxx_nested_name_specifier (cxx_pretty_printer *pp, tree t)
260 {
261 if (!SCOPE_FILE_SCOPE_P (t) && t != pp->enclosing_scope)
262 {
263 tree scope = get_containing_scope (t);
264 pp_cxx_nested_name_specifier (pp, scope);
265 pp_cxx_template_keyword_if_needed (pp, scope, t);
266 pp_cxx_unqualified_id (pp, t);
267 pp_cxx_colon_colon (pp);
268 }
269 }
270
271 /* qualified-id:
272 nested-name-specifier template(opt) unqualified-id */
273
274 static void
275 pp_cxx_qualified_id (cxx_pretty_printer *pp, tree t)
276 {
277 switch (TREE_CODE (t))
278 {
279 /* A pointer-to-member is always qualified. */
280 case PTRMEM_CST:
281 pp_cxx_nested_name_specifier (pp, PTRMEM_CST_CLASS (t));
282 pp_cxx_unqualified_id (pp, PTRMEM_CST_MEMBER (t));
283 break;
284
285 /* In Standard C++, functions cannot possibly be used as
286 nested-name-specifiers. However, there are situations where
287 is "makes sense" to output the surrounding function name for the
288 purpose of emphasizing on the scope kind. Just printing the
289 function name might not be sufficient as it may be overloaded; so,
290 we decorate the function with its signature too.
291 FIXME: This is probably the wrong pretty-printing for conversion
292 functions and some function templates. */
293 case OVERLOAD:
294 t = OVL_CURRENT (t);
295 case FUNCTION_DECL:
296 if (DECL_FUNCTION_MEMBER_P (t))
297 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
298 pp_cxx_unqualified_id
299 (pp, DECL_CONSTRUCTOR_P (t) ? DECL_CONTEXT (t) : t);
300 pp_cxx_parameter_declaration_clause (pp, TREE_TYPE (t));
301 break;
302
303 case OFFSET_REF:
304 case SCOPE_REF:
305 pp_cxx_nested_name_specifier (pp, TREE_OPERAND (t, 0));
306 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 1));
307 break;
308
309 default:
310 {
311 tree scope = get_containing_scope (t);
312 if (scope != pp->enclosing_scope)
313 {
314 pp_cxx_nested_name_specifier (pp, scope);
315 pp_cxx_template_keyword_if_needed (pp, scope, t);
316 }
317 pp_cxx_unqualified_id (pp, t);
318 }
319 break;
320 }
321 }
322
323
324 void
325 cxx_pretty_printer::constant (tree t)
326 {
327 switch (TREE_CODE (t))
328 {
329 case STRING_CST:
330 {
331 const bool in_parens = PAREN_STRING_LITERAL_P (t);
332 if (in_parens)
333 pp_cxx_left_paren (this);
334 c_pretty_printer::constant (t);
335 if (in_parens)
336 pp_cxx_right_paren (this);
337 }
338 break;
339
340 case INTEGER_CST:
341 if (NULLPTR_TYPE_P (TREE_TYPE (t)))
342 {
343 pp_string (this, "nullptr");
344 break;
345 }
346 /* else fall through. */
347
348 default:
349 c_pretty_printer::constant (t);
350 break;
351 }
352 }
353
354 /* id-expression:
355 unqualified-id
356 qualified-id */
357
358 void
359 cxx_pretty_printer::id_expression (tree t)
360 {
361 if (TREE_CODE (t) == OVERLOAD)
362 t = OVL_CURRENT (t);
363 if (DECL_P (t) && DECL_CONTEXT (t))
364 pp_cxx_qualified_id (this, t);
365 else
366 pp_cxx_unqualified_id (this, t);
367 }
368
369 /* user-defined literal:
370 literal ud-suffix */
371
372 void
373 pp_cxx_userdef_literal (cxx_pretty_printer *pp, tree t)
374 {
375 pp_constant (pp, USERDEF_LITERAL_VALUE (t));
376 pp_id_expression (pp, USERDEF_LITERAL_SUFFIX_ID (t));
377 }
378
379
380 /* primary-expression:
381 literal
382 this
383 :: identifier
384 :: operator-function-id
385 :: qualifier-id
386 ( expression )
387 id-expression
388
389 GNU Extensions:
390 __builtin_va_arg ( assignment-expression , type-id )
391 __builtin_offsetof ( type-id, offsetof-expression )
392
393 __has_nothrow_assign ( type-id )
394 __has_nothrow_constructor ( type-id )
395 __has_nothrow_copy ( type-id )
396 __has_trivial_assign ( type-id )
397 __has_trivial_constructor ( type-id )
398 __has_trivial_copy ( type-id )
399 __has_trivial_destructor ( type-id )
400 __has_virtual_destructor ( type-id )
401 __is_abstract ( type-id )
402 __is_base_of ( type-id , type-id )
403 __is_class ( type-id )
404 __is_convertible_to ( type-id , type-id )
405 __is_empty ( type-id )
406 __is_enum ( type-id )
407 __is_literal_type ( type-id )
408 __is_pod ( type-id )
409 __is_polymorphic ( type-id )
410 __is_std_layout ( type-id )
411 __is_trivial ( type-id )
412 __is_union ( type-id ) */
413
414 static void
415 pp_cxx_primary_expression (cxx_pretty_printer *pp, tree t)
416 {
417 switch (TREE_CODE (t))
418 {
419 case INTEGER_CST:
420 case REAL_CST:
421 case COMPLEX_CST:
422 case STRING_CST:
423 pp_constant (pp, t);
424 break;
425
426 case USERDEF_LITERAL:
427 pp_cxx_userdef_literal (pp, t);
428 break;
429
430 case BASELINK:
431 t = BASELINK_FUNCTIONS (t);
432 case VAR_DECL:
433 case PARM_DECL:
434 case FIELD_DECL:
435 case FUNCTION_DECL:
436 case OVERLOAD:
437 case CONST_DECL:
438 case TEMPLATE_DECL:
439 pp_id_expression (pp, t);
440 break;
441
442 case RESULT_DECL:
443 case TEMPLATE_TYPE_PARM:
444 case TEMPLATE_TEMPLATE_PARM:
445 case TEMPLATE_PARM_INDEX:
446 pp_cxx_unqualified_id (pp, t);
447 break;
448
449 case STMT_EXPR:
450 pp_cxx_left_paren (pp);
451 pp_cxx_statement (pp, STMT_EXPR_STMT (t));
452 pp_cxx_right_paren (pp);
453 break;
454
455 case TRAIT_EXPR:
456 pp_cxx_trait_expression (pp, t);
457 break;
458
459 case VA_ARG_EXPR:
460 pp_cxx_va_arg_expression (pp, t);
461 break;
462
463 case OFFSETOF_EXPR:
464 pp_cxx_offsetof_expression (pp, t);
465 break;
466
467 default:
468 pp_c_primary_expression (pp, t);
469 break;
470 }
471 }
472
473 /* postfix-expression:
474 primary-expression
475 postfix-expression [ expression ]
476 postfix-expression ( expression-list(opt) )
477 simple-type-specifier ( expression-list(opt) )
478 typename ::(opt) nested-name-specifier identifier ( expression-list(opt) )
479 typename ::(opt) nested-name-specifier template(opt)
480 template-id ( expression-list(opt) )
481 postfix-expression . template(opt) ::(opt) id-expression
482 postfix-expression -> template(opt) ::(opt) id-expression
483 postfix-expression . pseudo-destructor-name
484 postfix-expression -> pseudo-destructor-name
485 postfix-expression ++
486 postfix-expression --
487 dynamic_cast < type-id > ( expression )
488 static_cast < type-id > ( expression )
489 reinterpret_cast < type-id > ( expression )
490 const_cast < type-id > ( expression )
491 typeid ( expression )
492 typeid ( type-id ) */
493
494 static void
495 pp_cxx_postfix_expression (cxx_pretty_printer *pp, tree t)
496 {
497 enum tree_code code = TREE_CODE (t);
498
499 switch (code)
500 {
501 case AGGR_INIT_EXPR:
502 case CALL_EXPR:
503 {
504 tree fun = (code == AGGR_INIT_EXPR ? AGGR_INIT_EXPR_FN (t)
505 : CALL_EXPR_FN (t));
506 tree saved_scope = pp->enclosing_scope;
507 bool skipfirst = false;
508 tree arg;
509
510 if (TREE_CODE (fun) == ADDR_EXPR)
511 fun = TREE_OPERAND (fun, 0);
512
513 /* In templates, where there is no way to tell whether a given
514 call uses an actual member function. So the parser builds
515 FUN as a COMPONENT_REF or a plain IDENTIFIER_NODE until
516 instantiation time. */
517 if (TREE_CODE (fun) != FUNCTION_DECL)
518 ;
519 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun))
520 {
521 tree object = (code == AGGR_INIT_EXPR
522 ? (AGGR_INIT_VIA_CTOR_P (t)
523 ? AGGR_INIT_EXPR_SLOT (t)
524 : AGGR_INIT_EXPR_ARG (t, 0))
525 : CALL_EXPR_ARG (t, 0));
526
527 while (TREE_CODE (object) == NOP_EXPR)
528 object = TREE_OPERAND (object, 0);
529
530 if (TREE_CODE (object) == ADDR_EXPR)
531 object = TREE_OPERAND (object, 0);
532
533 if (!TYPE_PTR_P (TREE_TYPE (object)))
534 {
535 pp_cxx_postfix_expression (pp, object);
536 pp_cxx_dot (pp);
537 }
538 else
539 {
540 pp_cxx_postfix_expression (pp, object);
541 pp_cxx_arrow (pp);
542 }
543 skipfirst = true;
544 pp->enclosing_scope = strip_pointer_operator (TREE_TYPE (object));
545 }
546
547 pp_cxx_postfix_expression (pp, fun);
548 pp->enclosing_scope = saved_scope;
549 pp_cxx_left_paren (pp);
550 if (code == AGGR_INIT_EXPR)
551 {
552 aggr_init_expr_arg_iterator iter;
553 FOR_EACH_AGGR_INIT_EXPR_ARG (arg, iter, t)
554 {
555 if (skipfirst)
556 skipfirst = false;
557 else
558 {
559 pp_cxx_expression (pp, arg);
560 if (more_aggr_init_expr_args_p (&iter))
561 pp_cxx_separate_with (pp, ',');
562 }
563 }
564 }
565 else
566 {
567 call_expr_arg_iterator iter;
568 FOR_EACH_CALL_EXPR_ARG (arg, iter, t)
569 {
570 if (skipfirst)
571 skipfirst = false;
572 else
573 {
574 pp_cxx_expression (pp, arg);
575 if (more_call_expr_args_p (&iter))
576 pp_cxx_separate_with (pp, ',');
577 }
578 }
579 }
580 pp_cxx_right_paren (pp);
581 }
582 if (code == AGGR_INIT_EXPR && AGGR_INIT_VIA_CTOR_P (t))
583 {
584 pp_cxx_separate_with (pp, ',');
585 pp_cxx_postfix_expression (pp, AGGR_INIT_EXPR_SLOT (t));
586 }
587 break;
588
589 case BASELINK:
590 case VAR_DECL:
591 case PARM_DECL:
592 case FIELD_DECL:
593 case FUNCTION_DECL:
594 case OVERLOAD:
595 case CONST_DECL:
596 case TEMPLATE_DECL:
597 case RESULT_DECL:
598 pp_cxx_primary_expression (pp, t);
599 break;
600
601 case DYNAMIC_CAST_EXPR:
602 case STATIC_CAST_EXPR:
603 case REINTERPRET_CAST_EXPR:
604 case CONST_CAST_EXPR:
605 if (code == DYNAMIC_CAST_EXPR)
606 pp_cxx_ws_string (pp, "dynamic_cast");
607 else if (code == STATIC_CAST_EXPR)
608 pp_cxx_ws_string (pp, "static_cast");
609 else if (code == REINTERPRET_CAST_EXPR)
610 pp_cxx_ws_string (pp, "reinterpret_cast");
611 else
612 pp_cxx_ws_string (pp, "const_cast");
613 pp_cxx_begin_template_argument_list (pp);
614 pp_cxx_type_id (pp, TREE_TYPE (t));
615 pp_cxx_end_template_argument_list (pp);
616 pp_left_paren (pp);
617 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
618 pp_right_paren (pp);
619 break;
620
621 case EMPTY_CLASS_EXPR:
622 pp_cxx_type_id (pp, TREE_TYPE (t));
623 pp_left_paren (pp);
624 pp_right_paren (pp);
625 break;
626
627 case TYPEID_EXPR:
628 pp_cxx_typeid_expression (pp, t);
629 break;
630
631 case PSEUDO_DTOR_EXPR:
632 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
633 pp_cxx_dot (pp);
634 pp_cxx_qualified_id (pp, TREE_OPERAND (t, 1));
635 pp_cxx_colon_colon (pp);
636 pp_complement (pp);
637 pp_cxx_unqualified_id (pp, TREE_OPERAND (t, 2));
638 break;
639
640 case ARROW_EXPR:
641 pp_cxx_postfix_expression (pp, TREE_OPERAND (t, 0));
642 pp_cxx_arrow (pp);
643 break;
644
645 default:
646 pp_c_postfix_expression (pp, t);
647 break;
648 }
649 }
650
651 /* new-expression:
652 ::(opt) new new-placement(opt) new-type-id new-initializer(opt)
653 ::(opt) new new-placement(opt) ( type-id ) new-initializer(opt)
654
655 new-placement:
656 ( expression-list )
657
658 new-type-id:
659 type-specifier-seq new-declarator(opt)
660
661 new-declarator:
662 ptr-operator new-declarator(opt)
663 direct-new-declarator
664
665 direct-new-declarator
666 [ expression ]
667 direct-new-declarator [ constant-expression ]
668
669 new-initializer:
670 ( expression-list(opt) ) */
671
672 static void
673 pp_cxx_new_expression (cxx_pretty_printer *pp, tree t)
674 {
675 enum tree_code code = TREE_CODE (t);
676 tree type = TREE_OPERAND (t, 1);
677 tree init = TREE_OPERAND (t, 2);
678 switch (code)
679 {
680 case NEW_EXPR:
681 case VEC_NEW_EXPR:
682 if (NEW_EXPR_USE_GLOBAL (t))
683 pp_cxx_colon_colon (pp);
684 pp_cxx_ws_string (pp, "new");
685 if (TREE_OPERAND (t, 0))
686 {
687 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
688 pp_space (pp);
689 }
690 if (TREE_CODE (type) == ARRAY_REF)
691 type = build_cplus_array_type
692 (TREE_OPERAND (type, 0),
693 build_index_type (fold_build2_loc (input_location,
694 MINUS_EXPR, integer_type_node,
695 TREE_OPERAND (type, 1),
696 integer_one_node)));
697 pp_cxx_type_id (pp, type);
698 if (init)
699 {
700 pp_left_paren (pp);
701 if (TREE_CODE (init) == TREE_LIST)
702 pp_c_expression_list (pp, init);
703 else if (init == void_zero_node)
704 ; /* OK, empty initializer list. */
705 else
706 pp_cxx_expression (pp, init);
707 pp_right_paren (pp);
708 }
709 break;
710
711 default:
712 pp_unsupported_tree (pp, t);
713 }
714 }
715
716 /* delete-expression:
717 ::(opt) delete cast-expression
718 ::(opt) delete [ ] cast-expression */
719
720 static void
721 pp_cxx_delete_expression (cxx_pretty_printer *pp, tree t)
722 {
723 enum tree_code code = TREE_CODE (t);
724 switch (code)
725 {
726 case DELETE_EXPR:
727 case VEC_DELETE_EXPR:
728 if (DELETE_EXPR_USE_GLOBAL (t))
729 pp_cxx_colon_colon (pp);
730 pp_cxx_ws_string (pp, "delete");
731 pp_space (pp);
732 if (code == VEC_DELETE_EXPR
733 || DELETE_EXPR_USE_VEC (t))
734 {
735 pp_left_bracket (pp);
736 pp_right_bracket (pp);
737 pp_space (pp);
738 }
739 pp_c_cast_expression (pp, TREE_OPERAND (t, 0));
740 break;
741
742 default:
743 pp_unsupported_tree (pp, t);
744 }
745 }
746
747 /* unary-expression:
748 postfix-expression
749 ++ cast-expression
750 -- cast-expression
751 unary-operator cast-expression
752 sizeof unary-expression
753 sizeof ( type-id )
754 sizeof ... ( identifier )
755 new-expression
756 delete-expression
757
758 unary-operator: one of
759 * & + - !
760
761 GNU extensions:
762 __alignof__ unary-expression
763 __alignof__ ( type-id ) */
764
765 static void
766 pp_cxx_unary_expression (cxx_pretty_printer *pp, tree t)
767 {
768 enum tree_code code = TREE_CODE (t);
769 switch (code)
770 {
771 case NEW_EXPR:
772 case VEC_NEW_EXPR:
773 pp_cxx_new_expression (pp, t);
774 break;
775
776 case DELETE_EXPR:
777 case VEC_DELETE_EXPR:
778 pp_cxx_delete_expression (pp, t);
779 break;
780
781 case SIZEOF_EXPR:
782 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
783 {
784 pp_cxx_ws_string (pp, "sizeof");
785 pp_cxx_ws_string (pp, "...");
786 pp_cxx_whitespace (pp);
787 pp_cxx_left_paren (pp);
788 if (TYPE_P (TREE_OPERAND (t, 0)))
789 pp_cxx_type_id (pp, TREE_OPERAND (t, 0));
790 else
791 pp_unary_expression (pp, TREE_OPERAND (t, 0));
792 pp_cxx_right_paren (pp);
793 break;
794 }
795 /* Fall through */
796
797 case ALIGNOF_EXPR:
798 pp_cxx_ws_string (pp, code == SIZEOF_EXPR ? "sizeof" : "__alignof__");
799 pp_cxx_whitespace (pp);
800 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
801 {
802 pp_cxx_left_paren (pp);
803 pp_cxx_type_id (pp, TREE_TYPE (TREE_OPERAND (t, 0)));
804 pp_cxx_right_paren (pp);
805 }
806 else if (TYPE_P (TREE_OPERAND (t, 0)))
807 {
808 pp_cxx_left_paren (pp);
809 pp_cxx_type_id (pp, TREE_OPERAND (t, 0));
810 pp_cxx_right_paren (pp);
811 }
812 else
813 pp_unary_expression (pp, TREE_OPERAND (t, 0));
814 break;
815
816 case AT_ENCODE_EXPR:
817 pp_cxx_ws_string (pp, "@encode");
818 pp_cxx_whitespace (pp);
819 pp_cxx_left_paren (pp);
820 pp_cxx_type_id (pp, TREE_OPERAND (t, 0));
821 pp_cxx_right_paren (pp);
822 break;
823
824 case NOEXCEPT_EXPR:
825 pp_cxx_ws_string (pp, "noexcept");
826 pp_cxx_whitespace (pp);
827 pp_cxx_left_paren (pp);
828 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
829 pp_cxx_right_paren (pp);
830 break;
831
832 case UNARY_PLUS_EXPR:
833 pp_plus (pp);
834 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 0));
835 break;
836
837 default:
838 pp_c_unary_expression (pp, t);
839 break;
840 }
841 }
842
843 /* cast-expression:
844 unary-expression
845 ( type-id ) cast-expression */
846
847 static void
848 pp_cxx_cast_expression (cxx_pretty_printer *pp, tree t)
849 {
850 switch (TREE_CODE (t))
851 {
852 case CAST_EXPR:
853 case IMPLICIT_CONV_EXPR:
854 pp_cxx_type_id (pp, TREE_TYPE (t));
855 pp_cxx_call_argument_list (pp, TREE_OPERAND (t, 0));
856 break;
857
858 default:
859 pp_c_cast_expression (pp, t);
860 break;
861 }
862 }
863
864 /* pm-expression:
865 cast-expression
866 pm-expression .* cast-expression
867 pm-expression ->* cast-expression */
868
869 static void
870 pp_cxx_pm_expression (cxx_pretty_printer *pp, tree t)
871 {
872 switch (TREE_CODE (t))
873 {
874 /* Handle unfortunate OFFSET_REF overloading here. */
875 case OFFSET_REF:
876 if (TYPE_P (TREE_OPERAND (t, 0)))
877 {
878 pp_cxx_qualified_id (pp, t);
879 break;
880 }
881 /* Else fall through. */
882 case MEMBER_REF:
883 case DOTSTAR_EXPR:
884 pp_cxx_pm_expression (pp, TREE_OPERAND (t, 0));
885 if (TREE_CODE (t) == MEMBER_REF)
886 pp_cxx_arrow (pp);
887 else
888 pp_cxx_dot (pp);
889 pp_star(pp);
890 pp_cxx_cast_expression (pp, TREE_OPERAND (t, 1));
891 break;
892
893
894 default:
895 pp_cxx_cast_expression (pp, t);
896 break;
897 }
898 }
899
900 /* multiplicative-expression:
901 pm-expression
902 multiplicative-expression * pm-expression
903 multiplicative-expression / pm-expression
904 multiplicative-expression % pm-expression */
905
906 static void
907 pp_cxx_multiplicative_expression (cxx_pretty_printer *pp, tree e)
908 {
909 enum tree_code code = TREE_CODE (e);
910 switch (code)
911 {
912 case MULT_EXPR:
913 case TRUNC_DIV_EXPR:
914 case TRUNC_MOD_EXPR:
915 pp_cxx_multiplicative_expression (pp, TREE_OPERAND (e, 0));
916 pp_space (pp);
917 if (code == MULT_EXPR)
918 pp_star (pp);
919 else if (code == TRUNC_DIV_EXPR)
920 pp_slash (pp);
921 else
922 pp_modulo (pp);
923 pp_space (pp);
924 pp_cxx_pm_expression (pp, TREE_OPERAND (e, 1));
925 break;
926
927 default:
928 pp_cxx_pm_expression (pp, e);
929 break;
930 }
931 }
932
933 /* conditional-expression:
934 logical-or-expression
935 logical-or-expression ? expression : assignment-expression */
936
937 static void
938 pp_cxx_conditional_expression (cxx_pretty_printer *pp, tree e)
939 {
940 if (TREE_CODE (e) == COND_EXPR)
941 {
942 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
943 pp_space (pp);
944 pp_question (pp);
945 pp_space (pp);
946 pp_cxx_expression (pp, TREE_OPERAND (e, 1));
947 pp_space (pp);
948 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
949 }
950 else
951 pp_c_logical_or_expression (pp, e);
952 }
953
954 /* Pretty-print a compound assignment operator token as indicated by T. */
955
956 static void
957 pp_cxx_assignment_operator (cxx_pretty_printer *pp, tree t)
958 {
959 const char *op;
960
961 switch (TREE_CODE (t))
962 {
963 case NOP_EXPR:
964 op = "=";
965 break;
966
967 case PLUS_EXPR:
968 op = "+=";
969 break;
970
971 case MINUS_EXPR:
972 op = "-=";
973 break;
974
975 case TRUNC_DIV_EXPR:
976 op = "/=";
977 break;
978
979 case TRUNC_MOD_EXPR:
980 op = "%=";
981 break;
982
983 default:
984 op = tree_code_name[TREE_CODE (t)];
985 break;
986 }
987
988 pp_cxx_ws_string (pp, op);
989 }
990
991
992 /* assignment-expression:
993 conditional-expression
994 logical-or-expression assignment-operator assignment-expression
995 throw-expression
996
997 throw-expression:
998 throw assignment-expression(opt)
999
1000 assignment-operator: one of
1001 = *= /= %= += -= >>= <<= &= ^= |= */
1002
1003 static void
1004 pp_cxx_assignment_expression (cxx_pretty_printer *pp, tree e)
1005 {
1006 switch (TREE_CODE (e))
1007 {
1008 case MODIFY_EXPR:
1009 case INIT_EXPR:
1010 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1011 pp_space (pp);
1012 pp_equal (pp);
1013 pp_space (pp);
1014 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 1));
1015 break;
1016
1017 case THROW_EXPR:
1018 pp_cxx_ws_string (pp, "throw");
1019 if (TREE_OPERAND (e, 0))
1020 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 0));
1021 break;
1022
1023 case MODOP_EXPR:
1024 pp_c_logical_or_expression (pp, TREE_OPERAND (e, 0));
1025 pp_cxx_assignment_operator (pp, TREE_OPERAND (e, 1));
1026 pp_cxx_assignment_expression (pp, TREE_OPERAND (e, 2));
1027 break;
1028
1029 default:
1030 pp_cxx_conditional_expression (pp, e);
1031 break;
1032 }
1033 }
1034
1035 static void
1036 pp_cxx_expression (cxx_pretty_printer *pp, tree t)
1037 {
1038 switch (TREE_CODE (t))
1039 {
1040 case STRING_CST:
1041 case INTEGER_CST:
1042 case REAL_CST:
1043 case COMPLEX_CST:
1044 pp_constant (pp, t);
1045 break;
1046
1047 case USERDEF_LITERAL:
1048 pp_cxx_userdef_literal (pp, t);
1049 break;
1050
1051 case RESULT_DECL:
1052 pp_cxx_unqualified_id (pp, t);
1053 break;
1054
1055 #if 0
1056 case OFFSET_REF:
1057 #endif
1058 case SCOPE_REF:
1059 case PTRMEM_CST:
1060 pp_cxx_qualified_id (pp, t);
1061 break;
1062
1063 case OVERLOAD:
1064 t = OVL_CURRENT (t);
1065 case VAR_DECL:
1066 case PARM_DECL:
1067 case FIELD_DECL:
1068 case CONST_DECL:
1069 case FUNCTION_DECL:
1070 case BASELINK:
1071 case TEMPLATE_DECL:
1072 case TEMPLATE_TYPE_PARM:
1073 case TEMPLATE_PARM_INDEX:
1074 case TEMPLATE_TEMPLATE_PARM:
1075 case STMT_EXPR:
1076 pp_cxx_primary_expression (pp, t);
1077 break;
1078
1079 case CALL_EXPR:
1080 case DYNAMIC_CAST_EXPR:
1081 case STATIC_CAST_EXPR:
1082 case REINTERPRET_CAST_EXPR:
1083 case CONST_CAST_EXPR:
1084 #if 0
1085 case MEMBER_REF:
1086 #endif
1087 case EMPTY_CLASS_EXPR:
1088 case TYPEID_EXPR:
1089 case PSEUDO_DTOR_EXPR:
1090 case AGGR_INIT_EXPR:
1091 case ARROW_EXPR:
1092 pp_cxx_postfix_expression (pp, t);
1093 break;
1094
1095 case NEW_EXPR:
1096 case VEC_NEW_EXPR:
1097 pp_cxx_new_expression (pp, t);
1098 break;
1099
1100 case DELETE_EXPR:
1101 case VEC_DELETE_EXPR:
1102 pp_cxx_delete_expression (pp, t);
1103 break;
1104
1105 case SIZEOF_EXPR:
1106 case ALIGNOF_EXPR:
1107 case NOEXCEPT_EXPR:
1108 pp_cxx_unary_expression (pp, t);
1109 break;
1110
1111 case CAST_EXPR:
1112 case IMPLICIT_CONV_EXPR:
1113 pp_cxx_cast_expression (pp, t);
1114 break;
1115
1116 case OFFSET_REF:
1117 case MEMBER_REF:
1118 case DOTSTAR_EXPR:
1119 pp_cxx_pm_expression (pp, t);
1120 break;
1121
1122 case MULT_EXPR:
1123 case TRUNC_DIV_EXPR:
1124 case TRUNC_MOD_EXPR:
1125 pp_cxx_multiplicative_expression (pp, t);
1126 break;
1127
1128 case COND_EXPR:
1129 pp_cxx_conditional_expression (pp, t);
1130 break;
1131
1132 case MODIFY_EXPR:
1133 case INIT_EXPR:
1134 case THROW_EXPR:
1135 case MODOP_EXPR:
1136 pp_cxx_assignment_expression (pp, t);
1137 break;
1138
1139 case NON_DEPENDENT_EXPR:
1140 case MUST_NOT_THROW_EXPR:
1141 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
1142 break;
1143
1144 case EXPR_PACK_EXPANSION:
1145 pp_cxx_expression (pp, PACK_EXPANSION_PATTERN (t));
1146 pp_cxx_ws_string (pp, "...");
1147 break;
1148
1149 case TEMPLATE_ID_EXPR:
1150 pp_cxx_template_id (pp, t);
1151 break;
1152
1153 case NONTYPE_ARGUMENT_PACK:
1154 {
1155 tree args = ARGUMENT_PACK_ARGS (t);
1156 int i, len = TREE_VEC_LENGTH (args);
1157 for (i = 0; i < len; ++i)
1158 {
1159 if (i > 0)
1160 pp_cxx_separate_with (pp, ',');
1161 pp_cxx_expression (pp, TREE_VEC_ELT (args, i));
1162 }
1163 }
1164 break;
1165
1166 case LAMBDA_EXPR:
1167 pp_cxx_ws_string (pp, "<lambda>");
1168 break;
1169
1170 case PAREN_EXPR:
1171 pp_cxx_left_paren (pp);
1172 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
1173 pp_cxx_right_paren (pp);
1174 break;
1175
1176 default:
1177 pp_c_expression (pp, t);
1178 break;
1179 }
1180 }
1181
1182
1183 /* Declarations. */
1184
1185 /* function-specifier:
1186 inline
1187 virtual
1188 explicit */
1189
1190 static void
1191 pp_cxx_function_specifier (cxx_pretty_printer *pp, tree t)
1192 {
1193 switch (TREE_CODE (t))
1194 {
1195 case FUNCTION_DECL:
1196 if (DECL_VIRTUAL_P (t))
1197 pp_cxx_ws_string (pp, "virtual");
1198 else if (DECL_CONSTRUCTOR_P (t) && DECL_NONCONVERTING_P (t))
1199 pp_cxx_ws_string (pp, "explicit");
1200 else
1201 pp_c_function_specifier (pp, t);
1202
1203 default:
1204 break;
1205 }
1206 }
1207
1208 /* decl-specifier-seq:
1209 decl-specifier-seq(opt) decl-specifier
1210
1211 decl-specifier:
1212 storage-class-specifier
1213 type-specifier
1214 function-specifier
1215 friend
1216 typedef */
1217
1218 static void
1219 pp_cxx_decl_specifier_seq (cxx_pretty_printer *pp, tree t)
1220 {
1221 switch (TREE_CODE (t))
1222 {
1223 case VAR_DECL:
1224 case PARM_DECL:
1225 case CONST_DECL:
1226 case FIELD_DECL:
1227 pp_cxx_storage_class_specifier (pp, t);
1228 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1229 break;
1230
1231 case TYPE_DECL:
1232 pp_cxx_ws_string (pp, "typedef");
1233 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (t));
1234 break;
1235
1236 case FUNCTION_DECL:
1237 /* Constructors don't have return types. And conversion functions
1238 do not have a type-specifier in their return types. */
1239 if (DECL_CONSTRUCTOR_P (t) || DECL_CONV_FN_P (t))
1240 pp_cxx_function_specifier (pp, t);
1241 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1242 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (t)));
1243 else
1244 default:
1245 pp_c_declaration_specifiers (pp, t);
1246 break;
1247 }
1248 }
1249
1250 /* simple-type-specifier:
1251 ::(opt) nested-name-specifier(opt) type-name
1252 ::(opt) nested-name-specifier(opt) template(opt) template-id
1253 char
1254 wchar_t
1255 bool
1256 short
1257 int
1258 long
1259 signed
1260 unsigned
1261 float
1262 double
1263 void */
1264
1265 static void
1266 pp_cxx_simple_type_specifier (cxx_pretty_printer *pp, tree t)
1267 {
1268 switch (TREE_CODE (t))
1269 {
1270 case RECORD_TYPE:
1271 case UNION_TYPE:
1272 case ENUMERAL_TYPE:
1273 pp_cxx_qualified_id (pp, t);
1274 break;
1275
1276 case TEMPLATE_TYPE_PARM:
1277 case TEMPLATE_TEMPLATE_PARM:
1278 case TEMPLATE_PARM_INDEX:
1279 case BOUND_TEMPLATE_TEMPLATE_PARM:
1280 pp_cxx_unqualified_id (pp, t);
1281 break;
1282
1283 case TYPENAME_TYPE:
1284 pp_cxx_ws_string (pp, "typename");
1285 pp_cxx_nested_name_specifier (pp, TYPE_CONTEXT (t));
1286 pp_cxx_unqualified_id (pp, TYPE_NAME (t));
1287 break;
1288
1289 default:
1290 pp_c_type_specifier (pp, t);
1291 break;
1292 }
1293 }
1294
1295 /* type-specifier-seq:
1296 type-specifier type-specifier-seq(opt)
1297
1298 type-specifier:
1299 simple-type-specifier
1300 class-specifier
1301 enum-specifier
1302 elaborated-type-specifier
1303 cv-qualifier */
1304
1305 static void
1306 pp_cxx_type_specifier_seq (cxx_pretty_printer *pp, tree t)
1307 {
1308 switch (TREE_CODE (t))
1309 {
1310 case TEMPLATE_DECL:
1311 case TEMPLATE_TYPE_PARM:
1312 case TEMPLATE_TEMPLATE_PARM:
1313 case TYPE_DECL:
1314 case BOUND_TEMPLATE_TEMPLATE_PARM:
1315 pp_cxx_cv_qualifier_seq (pp, t);
1316 pp_cxx_simple_type_specifier (pp, t);
1317 break;
1318
1319 case METHOD_TYPE:
1320 pp_cxx_type_specifier_seq (pp, TREE_TYPE (t));
1321 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1322 pp_cxx_nested_name_specifier (pp, TYPE_METHOD_BASETYPE (t));
1323 break;
1324
1325 case DECLTYPE_TYPE:
1326 pp_cxx_ws_string (pp, "decltype");
1327 pp_cxx_left_paren (pp);
1328 pp_cxx_expression (pp, DECLTYPE_TYPE_EXPR (t));
1329 pp_cxx_right_paren (pp);
1330 break;
1331
1332 case RECORD_TYPE:
1333 if (TYPE_PTRMEMFUNC_P (t))
1334 {
1335 tree pfm = TYPE_PTRMEMFUNC_FN_TYPE (t);
1336 pp_cxx_decl_specifier_seq (pp, TREE_TYPE (TREE_TYPE (pfm)));
1337 pp_cxx_whitespace (pp);
1338 pp_cxx_ptr_operator (pp, t);
1339 break;
1340 }
1341 /* else fall through */
1342
1343 default:
1344 if (!(TREE_CODE (t) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (t)))
1345 pp_c_specifier_qualifier_list (pp, t);
1346 }
1347 }
1348
1349 /* ptr-operator:
1350 * cv-qualifier-seq(opt)
1351 &
1352 ::(opt) nested-name-specifier * cv-qualifier-seq(opt) */
1353
1354 static void
1355 pp_cxx_ptr_operator (cxx_pretty_printer *pp, tree t)
1356 {
1357 if (!TYPE_P (t) && TREE_CODE (t) != TYPE_DECL)
1358 t = TREE_TYPE (t);
1359 switch (TREE_CODE (t))
1360 {
1361 case REFERENCE_TYPE:
1362 case POINTER_TYPE:
1363 if (TYPE_PTR_OR_PTRMEM_P (TREE_TYPE (t)))
1364 pp_cxx_ptr_operator (pp, TREE_TYPE (t));
1365 pp_c_attributes_display (pp, TYPE_ATTRIBUTES (TREE_TYPE (t)));
1366 if (TYPE_PTR_P (t))
1367 {
1368 pp_star (pp);
1369 pp_cxx_cv_qualifier_seq (pp, t);
1370 }
1371 else
1372 pp_ampersand (pp);
1373 break;
1374
1375 case RECORD_TYPE:
1376 if (TYPE_PTRMEMFUNC_P (t))
1377 {
1378 pp_cxx_left_paren (pp);
1379 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEMFUNC_OBJECT_TYPE (t));
1380 pp_star (pp);
1381 break;
1382 }
1383 case OFFSET_TYPE:
1384 if (TYPE_PTRMEM_P (t))
1385 {
1386 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
1387 pp_cxx_left_paren (pp);
1388 pp_cxx_nested_name_specifier (pp, TYPE_PTRMEM_CLASS_TYPE (t));
1389 pp_star (pp);
1390 pp_cxx_cv_qualifier_seq (pp, t);
1391 break;
1392 }
1393 /* else fall through. */
1394
1395 default:
1396 pp_unsupported_tree (pp, t);
1397 break;
1398 }
1399 }
1400
1401 static inline tree
1402 pp_cxx_implicit_parameter_type (tree mf)
1403 {
1404 return class_of_this_parm (TREE_TYPE (mf));
1405 }
1406
1407 /*
1408 parameter-declaration:
1409 decl-specifier-seq declarator
1410 decl-specifier-seq declarator = assignment-expression
1411 decl-specifier-seq abstract-declarator(opt)
1412 decl-specifier-seq abstract-declarator(opt) assignment-expression */
1413
1414 static inline void
1415 pp_cxx_parameter_declaration (cxx_pretty_printer *pp, tree t)
1416 {
1417 pp_cxx_decl_specifier_seq (pp, t);
1418 if (TYPE_P (t))
1419 pp_cxx_abstract_declarator (pp, t);
1420 else
1421 pp_cxx_declarator (pp, t);
1422 }
1423
1424 /* parameter-declaration-clause:
1425 parameter-declaration-list(opt) ...(opt)
1426 parameter-declaration-list , ...
1427
1428 parameter-declaration-list:
1429 parameter-declaration
1430 parameter-declaration-list , parameter-declaration */
1431
1432 static void
1433 pp_cxx_parameter_declaration_clause (cxx_pretty_printer *pp, tree t)
1434 {
1435 tree args = TYPE_P (t) ? NULL : FUNCTION_FIRST_USER_PARM (t);
1436 tree types =
1437 TYPE_P (t) ? TYPE_ARG_TYPES (t) : FUNCTION_FIRST_USER_PARMTYPE (t);
1438 const bool abstract = args == NULL || pp->flags & pp_c_flag_abstract;
1439 bool first = true;
1440
1441 /* Skip artificial parameter for nonstatic member functions. */
1442 if (TREE_CODE (t) == METHOD_TYPE)
1443 types = TREE_CHAIN (types);
1444
1445 pp_cxx_left_paren (pp);
1446 for (; args; args = TREE_CHAIN (args), types = TREE_CHAIN (types))
1447 {
1448 if (!first)
1449 pp_cxx_separate_with (pp, ',');
1450 first = false;
1451 pp_cxx_parameter_declaration (pp, abstract ? TREE_VALUE (types) : args);
1452 if (!abstract && pp->flags & pp_cxx_flag_default_argument)
1453 {
1454 pp_cxx_whitespace (pp);
1455 pp_equal (pp);
1456 pp_cxx_whitespace (pp);
1457 pp_cxx_assignment_expression (pp, TREE_PURPOSE (types));
1458 }
1459 }
1460 pp_cxx_right_paren (pp);
1461 }
1462
1463 /* exception-specification:
1464 throw ( type-id-list(opt) )
1465
1466 type-id-list
1467 type-id
1468 type-id-list , type-id */
1469
1470 static void
1471 pp_cxx_exception_specification (cxx_pretty_printer *pp, tree t)
1472 {
1473 tree ex_spec = TYPE_RAISES_EXCEPTIONS (t);
1474 bool need_comma = false;
1475
1476 if (ex_spec == NULL)
1477 return;
1478 if (TREE_PURPOSE (ex_spec))
1479 {
1480 pp_cxx_ws_string (pp, "noexcept");
1481 pp_cxx_whitespace (pp);
1482 pp_cxx_left_paren (pp);
1483 if (DEFERRED_NOEXCEPT_SPEC_P (ex_spec))
1484 pp_cxx_ws_string (pp, "<uninstantiated>");
1485 else
1486 pp_cxx_expression (pp, TREE_PURPOSE (ex_spec));
1487 pp_cxx_right_paren (pp);
1488 return;
1489 }
1490 pp_cxx_ws_string (pp, "throw");
1491 pp_cxx_left_paren (pp);
1492 for (; ex_spec && TREE_VALUE (ex_spec); ex_spec = TREE_CHAIN (ex_spec))
1493 {
1494 tree type = TREE_VALUE (ex_spec);
1495 tree argpack = NULL_TREE;
1496 int i, len = 1;
1497
1498 if (ARGUMENT_PACK_P (type))
1499 {
1500 argpack = ARGUMENT_PACK_ARGS (type);
1501 len = TREE_VEC_LENGTH (argpack);
1502 }
1503
1504 for (i = 0; i < len; ++i)
1505 {
1506 if (argpack)
1507 type = TREE_VEC_ELT (argpack, i);
1508
1509 if (need_comma)
1510 pp_cxx_separate_with (pp, ',');
1511 else
1512 need_comma = true;
1513
1514 pp_cxx_type_id (pp, type);
1515 }
1516 }
1517 pp_cxx_right_paren (pp);
1518 }
1519
1520 /* direct-declarator:
1521 declarator-id
1522 direct-declarator ( parameter-declaration-clause ) cv-qualifier-seq(opt)
1523 exception-specification(opt)
1524 direct-declaration [ constant-expression(opt) ]
1525 ( declarator ) */
1526
1527 static void
1528 pp_cxx_direct_declarator (cxx_pretty_printer *pp, tree t)
1529 {
1530 switch (TREE_CODE (t))
1531 {
1532 case VAR_DECL:
1533 case PARM_DECL:
1534 case CONST_DECL:
1535 case FIELD_DECL:
1536 if (DECL_NAME (t))
1537 {
1538 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (t));
1539
1540 if ((TREE_CODE (t) == PARM_DECL && FUNCTION_PARAMETER_PACK_P (t))
1541 || template_parameter_pack_p (t))
1542 /* A function parameter pack or non-type template
1543 parameter pack. */
1544 pp_cxx_ws_string (pp, "...");
1545
1546 pp_id_expression (pp, DECL_NAME (t));
1547 }
1548 pp_cxx_abstract_declarator (pp, TREE_TYPE (t));
1549 break;
1550
1551 case FUNCTION_DECL:
1552 pp_cxx_space_for_pointer_operator (pp, TREE_TYPE (TREE_TYPE (t)));
1553 pp_id_expression (pp, t);
1554 pp_cxx_parameter_declaration_clause (pp, t);
1555
1556 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (t))
1557 {
1558 pp->padding = pp_before;
1559 pp_cxx_cv_qualifier_seq (pp, pp_cxx_implicit_parameter_type (t));
1560 }
1561
1562 pp_cxx_exception_specification (pp, TREE_TYPE (t));
1563 break;
1564
1565 case TYPENAME_TYPE:
1566 case TEMPLATE_DECL:
1567 case TEMPLATE_TYPE_PARM:
1568 case TEMPLATE_PARM_INDEX:
1569 case TEMPLATE_TEMPLATE_PARM:
1570 break;
1571
1572 default:
1573 pp_c_direct_declarator (pp, t);
1574 break;
1575 }
1576 }
1577
1578 /* declarator:
1579 direct-declarator
1580 ptr-operator declarator */
1581
1582 static void
1583 pp_cxx_declarator (cxx_pretty_printer *pp, tree t)
1584 {
1585 pp_cxx_direct_declarator (pp, t);
1586 }
1587
1588 /* ctor-initializer:
1589 : mem-initializer-list
1590
1591 mem-initializer-list:
1592 mem-initializer
1593 mem-initializer , mem-initializer-list
1594
1595 mem-initializer:
1596 mem-initializer-id ( expression-list(opt) )
1597
1598 mem-initializer-id:
1599 ::(opt) nested-name-specifier(opt) class-name
1600 identifier */
1601
1602 static void
1603 pp_cxx_ctor_initializer (cxx_pretty_printer *pp, tree t)
1604 {
1605 t = TREE_OPERAND (t, 0);
1606 pp_cxx_whitespace (pp);
1607 pp_colon (pp);
1608 pp_cxx_whitespace (pp);
1609 for (; t; t = TREE_CHAIN (t))
1610 {
1611 tree purpose = TREE_PURPOSE (t);
1612 bool is_pack = PACK_EXPANSION_P (purpose);
1613
1614 if (is_pack)
1615 pp_cxx_primary_expression (pp, PACK_EXPANSION_PATTERN (purpose));
1616 else
1617 pp_cxx_primary_expression (pp, purpose);
1618 pp_cxx_call_argument_list (pp, TREE_VALUE (t));
1619 if (is_pack)
1620 pp_cxx_ws_string (pp, "...");
1621 if (TREE_CHAIN (t))
1622 pp_cxx_separate_with (pp, ',');
1623 }
1624 }
1625
1626 /* function-definition:
1627 decl-specifier-seq(opt) declarator ctor-initializer(opt) function-body
1628 decl-specifier-seq(opt) declarator function-try-block */
1629
1630 static void
1631 pp_cxx_function_definition (cxx_pretty_printer *pp, tree t)
1632 {
1633 tree saved_scope = pp->enclosing_scope;
1634 pp_cxx_decl_specifier_seq (pp, t);
1635 pp_cxx_declarator (pp, t);
1636 pp_needs_newline (pp) = true;
1637 pp->enclosing_scope = DECL_CONTEXT (t);
1638 if (DECL_SAVED_TREE (t))
1639 pp_cxx_statement (pp, DECL_SAVED_TREE (t));
1640 else
1641 pp_cxx_semicolon (pp);
1642 pp_newline_and_flush (pp);
1643 pp->enclosing_scope = saved_scope;
1644 }
1645
1646 /* abstract-declarator:
1647 ptr-operator abstract-declarator(opt)
1648 direct-abstract-declarator */
1649
1650 static void
1651 pp_cxx_abstract_declarator (cxx_pretty_printer *pp, tree t)
1652 {
1653 if (TYPE_PTRMEM_P (t))
1654 pp_cxx_right_paren (pp);
1655 else if (POINTER_TYPE_P (t))
1656 {
1657 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
1658 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1659 pp_cxx_right_paren (pp);
1660 t = TREE_TYPE (t);
1661 }
1662 pp_cxx_direct_abstract_declarator (pp, t);
1663 }
1664
1665 /* direct-abstract-declarator:
1666 direct-abstract-declarator(opt) ( parameter-declaration-clause )
1667 cv-qualifier-seq(opt) exception-specification(opt)
1668 direct-abstract-declarator(opt) [ constant-expression(opt) ]
1669 ( abstract-declarator ) */
1670
1671 static void
1672 pp_cxx_direct_abstract_declarator (cxx_pretty_printer *pp, tree t)
1673 {
1674 switch (TREE_CODE (t))
1675 {
1676 case REFERENCE_TYPE:
1677 pp_cxx_abstract_declarator (pp, t);
1678 break;
1679
1680 case RECORD_TYPE:
1681 if (TYPE_PTRMEMFUNC_P (t))
1682 pp_cxx_direct_abstract_declarator (pp, TYPE_PTRMEMFUNC_FN_TYPE (t));
1683 break;
1684
1685 case METHOD_TYPE:
1686 case FUNCTION_TYPE:
1687 pp_cxx_parameter_declaration_clause (pp, t);
1688 pp_cxx_direct_abstract_declarator (pp, TREE_TYPE (t));
1689 if (TREE_CODE (t) == METHOD_TYPE)
1690 {
1691 pp->padding = pp_before;
1692 pp_cxx_cv_qualifier_seq (pp, class_of_this_parm (t));
1693 }
1694 pp_cxx_exception_specification (pp, t);
1695 break;
1696
1697 case TYPENAME_TYPE:
1698 case TEMPLATE_TYPE_PARM:
1699 case TEMPLATE_TEMPLATE_PARM:
1700 case BOUND_TEMPLATE_TEMPLATE_PARM:
1701 case UNBOUND_CLASS_TEMPLATE:
1702 break;
1703
1704 default:
1705 pp_c_direct_abstract_declarator (pp, t);
1706 break;
1707 }
1708 }
1709
1710 /* type-id:
1711 type-specifier-seq abstract-declarator(opt) */
1712
1713 static void
1714 pp_cxx_type_id (cxx_pretty_printer *pp, tree t)
1715 {
1716 pp_flags saved_flags = pp->flags;
1717 pp->flags |= pp_c_flag_abstract;
1718
1719 switch (TREE_CODE (t))
1720 {
1721 case TYPE_DECL:
1722 case UNION_TYPE:
1723 case RECORD_TYPE:
1724 case ENUMERAL_TYPE:
1725 case TYPENAME_TYPE:
1726 case BOUND_TEMPLATE_TEMPLATE_PARM:
1727 case UNBOUND_CLASS_TEMPLATE:
1728 case TEMPLATE_TEMPLATE_PARM:
1729 case TEMPLATE_TYPE_PARM:
1730 case TEMPLATE_PARM_INDEX:
1731 case TEMPLATE_DECL:
1732 case TYPEOF_TYPE:
1733 case UNDERLYING_TYPE:
1734 case DECLTYPE_TYPE:
1735 case TEMPLATE_ID_EXPR:
1736 pp_cxx_type_specifier_seq (pp, t);
1737 break;
1738
1739 case TYPE_PACK_EXPANSION:
1740 pp_cxx_type_id (pp, PACK_EXPANSION_PATTERN (t));
1741 pp_cxx_ws_string (pp, "...");
1742 break;
1743
1744 default:
1745 pp_c_type_id (pp, t);
1746 break;
1747 }
1748
1749 pp->flags = saved_flags;
1750 }
1751
1752 /* template-argument-list:
1753 template-argument ...(opt)
1754 template-argument-list, template-argument ...(opt)
1755
1756 template-argument:
1757 assignment-expression
1758 type-id
1759 template-name */
1760
1761 static void
1762 pp_cxx_template_argument_list (cxx_pretty_printer *pp, tree t)
1763 {
1764 int i;
1765 bool need_comma = false;
1766
1767 if (t == NULL)
1768 return;
1769 for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
1770 {
1771 tree arg = TREE_VEC_ELT (t, i);
1772 tree argpack = NULL_TREE;
1773 int idx, len = 1;
1774
1775 if (ARGUMENT_PACK_P (arg))
1776 {
1777 argpack = ARGUMENT_PACK_ARGS (arg);
1778 len = TREE_VEC_LENGTH (argpack);
1779 }
1780
1781 for (idx = 0; idx < len; idx++)
1782 {
1783 if (argpack)
1784 arg = TREE_VEC_ELT (argpack, idx);
1785
1786 if (need_comma)
1787 pp_cxx_separate_with (pp, ',');
1788 else
1789 need_comma = true;
1790
1791 if (TYPE_P (arg) || (TREE_CODE (arg) == TEMPLATE_DECL
1792 && TYPE_P (DECL_TEMPLATE_RESULT (arg))))
1793 pp_cxx_type_id (pp, arg);
1794 else
1795 pp_cxx_expression (pp, arg);
1796 }
1797 }
1798 }
1799
1800
1801 static void
1802 pp_cxx_exception_declaration (cxx_pretty_printer *pp, tree t)
1803 {
1804 t = DECL_EXPR_DECL (t);
1805 pp_cxx_type_specifier_seq (pp, t);
1806 if (TYPE_P (t))
1807 pp_cxx_abstract_declarator (pp, t);
1808 else
1809 pp_cxx_declarator (pp, t);
1810 }
1811
1812 /* Statements. */
1813
1814 static void
1815 pp_cxx_statement (cxx_pretty_printer *pp, tree t)
1816 {
1817 switch (TREE_CODE (t))
1818 {
1819 case CTOR_INITIALIZER:
1820 pp_cxx_ctor_initializer (pp, t);
1821 break;
1822
1823 case USING_STMT:
1824 pp_cxx_ws_string (pp, "using");
1825 pp_cxx_ws_string (pp, "namespace");
1826 if (DECL_CONTEXT (t))
1827 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
1828 pp_cxx_qualified_id (pp, USING_STMT_NAMESPACE (t));
1829 break;
1830
1831 case USING_DECL:
1832 pp_cxx_ws_string (pp, "using");
1833 pp_cxx_nested_name_specifier (pp, USING_DECL_SCOPE (t));
1834 pp_cxx_unqualified_id (pp, DECL_NAME (t));
1835 break;
1836
1837 case EH_SPEC_BLOCK:
1838 break;
1839
1840 /* try-block:
1841 try compound-statement handler-seq */
1842 case TRY_BLOCK:
1843 pp_maybe_newline_and_indent (pp, 0);
1844 pp_cxx_ws_string (pp, "try");
1845 pp_newline_and_indent (pp, 3);
1846 pp_cxx_statement (pp, TRY_STMTS (t));
1847 pp_newline_and_indent (pp, -3);
1848 if (CLEANUP_P (t))
1849 ;
1850 else
1851 pp_cxx_statement (pp, TRY_HANDLERS (t));
1852 break;
1853
1854 /*
1855 handler-seq:
1856 handler handler-seq(opt)
1857
1858 handler:
1859 catch ( exception-declaration ) compound-statement
1860
1861 exception-declaration:
1862 type-specifier-seq declarator
1863 type-specifier-seq abstract-declarator
1864 ... */
1865 case HANDLER:
1866 pp_cxx_ws_string (pp, "catch");
1867 pp_cxx_left_paren (pp);
1868 pp_cxx_exception_declaration (pp, HANDLER_PARMS (t));
1869 pp_cxx_right_paren (pp);
1870 pp_indentation (pp) += 3;
1871 pp_needs_newline (pp) = true;
1872 pp_cxx_statement (pp, HANDLER_BODY (t));
1873 pp_indentation (pp) -= 3;
1874 pp_needs_newline (pp) = true;
1875 break;
1876
1877 /* selection-statement:
1878 if ( expression ) statement
1879 if ( expression ) statement else statement */
1880 case IF_STMT:
1881 pp_cxx_ws_string (pp, "if");
1882 pp_cxx_whitespace (pp);
1883 pp_cxx_left_paren (pp);
1884 pp_cxx_expression (pp, IF_COND (t));
1885 pp_cxx_right_paren (pp);
1886 pp_newline_and_indent (pp, 2);
1887 pp_cxx_statement (pp, THEN_CLAUSE (t));
1888 pp_newline_and_indent (pp, -2);
1889 if (ELSE_CLAUSE (t))
1890 {
1891 tree else_clause = ELSE_CLAUSE (t);
1892 pp_cxx_ws_string (pp, "else");
1893 if (TREE_CODE (else_clause) == IF_STMT)
1894 pp_cxx_whitespace (pp);
1895 else
1896 pp_newline_and_indent (pp, 2);
1897 pp_cxx_statement (pp, else_clause);
1898 if (TREE_CODE (else_clause) != IF_STMT)
1899 pp_newline_and_indent (pp, -2);
1900 }
1901 break;
1902
1903 case SWITCH_STMT:
1904 pp_cxx_ws_string (pp, "switch");
1905 pp_space (pp);
1906 pp_cxx_left_paren (pp);
1907 pp_cxx_expression (pp, SWITCH_STMT_COND (t));
1908 pp_cxx_right_paren (pp);
1909 pp_indentation (pp) += 3;
1910 pp_needs_newline (pp) = true;
1911 pp_cxx_statement (pp, SWITCH_STMT_BODY (t));
1912 pp_newline_and_indent (pp, -3);
1913 break;
1914
1915 /* iteration-statement:
1916 while ( expression ) statement
1917 do statement while ( expression ) ;
1918 for ( expression(opt) ; expression(opt) ; expression(opt) ) statement
1919 for ( declaration expression(opt) ; expression(opt) ) statement */
1920 case WHILE_STMT:
1921 pp_cxx_ws_string (pp, "while");
1922 pp_space (pp);
1923 pp_cxx_left_paren (pp);
1924 pp_cxx_expression (pp, WHILE_COND (t));
1925 pp_cxx_right_paren (pp);
1926 pp_newline_and_indent (pp, 3);
1927 pp_cxx_statement (pp, WHILE_BODY (t));
1928 pp_indentation (pp) -= 3;
1929 pp_needs_newline (pp) = true;
1930 break;
1931
1932 case DO_STMT:
1933 pp_cxx_ws_string (pp, "do");
1934 pp_newline_and_indent (pp, 3);
1935 pp_cxx_statement (pp, DO_BODY (t));
1936 pp_newline_and_indent (pp, -3);
1937 pp_cxx_ws_string (pp, "while");
1938 pp_space (pp);
1939 pp_cxx_left_paren (pp);
1940 pp_cxx_expression (pp, DO_COND (t));
1941 pp_cxx_right_paren (pp);
1942 pp_cxx_semicolon (pp);
1943 pp_needs_newline (pp) = true;
1944 break;
1945
1946 case FOR_STMT:
1947 pp_cxx_ws_string (pp, "for");
1948 pp_space (pp);
1949 pp_cxx_left_paren (pp);
1950 if (FOR_INIT_STMT (t))
1951 pp_cxx_statement (pp, FOR_INIT_STMT (t));
1952 else
1953 pp_cxx_semicolon (pp);
1954 pp_needs_newline (pp) = false;
1955 pp_cxx_whitespace (pp);
1956 if (FOR_COND (t))
1957 pp_cxx_expression (pp, FOR_COND (t));
1958 pp_cxx_semicolon (pp);
1959 pp_needs_newline (pp) = false;
1960 pp_cxx_whitespace (pp);
1961 if (FOR_EXPR (t))
1962 pp_cxx_expression (pp, FOR_EXPR (t));
1963 pp_cxx_right_paren (pp);
1964 pp_newline_and_indent (pp, 3);
1965 pp_cxx_statement (pp, FOR_BODY (t));
1966 pp_indentation (pp) -= 3;
1967 pp_needs_newline (pp) = true;
1968 break;
1969
1970 case RANGE_FOR_STMT:
1971 pp_cxx_ws_string (pp, "for");
1972 pp_space (pp);
1973 pp_cxx_left_paren (pp);
1974 pp_cxx_statement (pp, RANGE_FOR_DECL (t));
1975 pp_space (pp);
1976 pp_needs_newline (pp) = false;
1977 pp_colon (pp);
1978 pp_space (pp);
1979 pp_cxx_statement (pp, RANGE_FOR_EXPR (t));
1980 pp_cxx_right_paren (pp);
1981 pp_newline_and_indent (pp, 3);
1982 pp_cxx_statement (pp, FOR_BODY (t));
1983 pp_indentation (pp) -= 3;
1984 pp_needs_newline (pp) = true;
1985 break;
1986
1987 /* jump-statement:
1988 goto identifier;
1989 continue ;
1990 return expression(opt) ; */
1991 case BREAK_STMT:
1992 case CONTINUE_STMT:
1993 pp_string (pp, TREE_CODE (t) == BREAK_STMT ? "break" : "continue");
1994 pp_cxx_semicolon (pp);
1995 pp_needs_newline (pp) = true;
1996 break;
1997
1998 /* expression-statement:
1999 expression(opt) ; */
2000 case EXPR_STMT:
2001 pp_cxx_expression (pp, EXPR_STMT_EXPR (t));
2002 pp_cxx_semicolon (pp);
2003 pp_needs_newline (pp) = true;
2004 break;
2005
2006 case CLEANUP_STMT:
2007 pp_cxx_ws_string (pp, "try");
2008 pp_newline_and_indent (pp, 2);
2009 pp_cxx_statement (pp, CLEANUP_BODY (t));
2010 pp_newline_and_indent (pp, -2);
2011 pp_cxx_ws_string (pp, CLEANUP_EH_ONLY (t) ? "catch" : "finally");
2012 pp_newline_and_indent (pp, 2);
2013 pp_cxx_statement (pp, CLEANUP_EXPR (t));
2014 pp_newline_and_indent (pp, -2);
2015 break;
2016
2017 case STATIC_ASSERT:
2018 pp_cxx_declaration (pp, t);
2019 break;
2020
2021 default:
2022 pp_c_statement (pp, t);
2023 break;
2024 }
2025 }
2026
2027 /* original-namespace-definition:
2028 namespace identifier { namespace-body }
2029
2030 As an edge case, we also handle unnamed namespace definition here. */
2031
2032 static void
2033 pp_cxx_original_namespace_definition (cxx_pretty_printer *pp, tree t)
2034 {
2035 pp_cxx_ws_string (pp, "namespace");
2036 if (DECL_CONTEXT (t))
2037 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
2038 if (DECL_NAME (t))
2039 pp_cxx_unqualified_id (pp, t);
2040 pp_cxx_whitespace (pp);
2041 pp_cxx_left_brace (pp);
2042 /* We do not print the namespace-body. */
2043 pp_cxx_whitespace (pp);
2044 pp_cxx_right_brace (pp);
2045 }
2046
2047 /* namespace-alias:
2048 identifier
2049
2050 namespace-alias-definition:
2051 namespace identifier = qualified-namespace-specifier ;
2052
2053 qualified-namespace-specifier:
2054 ::(opt) nested-name-specifier(opt) namespace-name */
2055
2056 static void
2057 pp_cxx_namespace_alias_definition (cxx_pretty_printer *pp, tree t)
2058 {
2059 pp_cxx_ws_string (pp, "namespace");
2060 if (DECL_CONTEXT (t))
2061 pp_cxx_nested_name_specifier (pp, DECL_CONTEXT (t));
2062 pp_cxx_unqualified_id (pp, t);
2063 pp_cxx_whitespace (pp);
2064 pp_equal (pp);
2065 pp_cxx_whitespace (pp);
2066 if (DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)))
2067 pp_cxx_nested_name_specifier (pp,
2068 DECL_CONTEXT (DECL_NAMESPACE_ALIAS (t)));
2069 pp_cxx_qualified_id (pp, DECL_NAMESPACE_ALIAS (t));
2070 pp_cxx_semicolon (pp);
2071 }
2072
2073 /* simple-declaration:
2074 decl-specifier-seq(opt) init-declarator-list(opt) */
2075
2076 static void
2077 pp_cxx_simple_declaration (cxx_pretty_printer *pp, tree t)
2078 {
2079 pp_cxx_decl_specifier_seq (pp, t);
2080 pp_cxx_init_declarator (pp, t);
2081 pp_cxx_semicolon (pp);
2082 pp_needs_newline (pp) = true;
2083 }
2084
2085 /*
2086 template-parameter-list:
2087 template-parameter
2088 template-parameter-list , template-parameter */
2089
2090 static inline void
2091 pp_cxx_template_parameter_list (cxx_pretty_printer *pp, tree t)
2092 {
2093 const int n = TREE_VEC_LENGTH (t);
2094 int i;
2095 for (i = 0; i < n; ++i)
2096 {
2097 if (i)
2098 pp_cxx_separate_with (pp, ',');
2099 pp_cxx_template_parameter (pp, TREE_VEC_ELT (t, i));
2100 }
2101 }
2102
2103 /* template-parameter:
2104 type-parameter
2105 parameter-declaration
2106
2107 type-parameter:
2108 class ...(opt) identifier(opt)
2109 class identifier(opt) = type-id
2110 typename identifier(opt)
2111 typename ...(opt) identifier(opt) = type-id
2112 template < template-parameter-list > class ...(opt) identifier(opt)
2113 template < template-parameter-list > class identifier(opt) = template-name */
2114
2115 static void
2116 pp_cxx_template_parameter (cxx_pretty_printer *pp, tree t)
2117 {
2118 tree parameter = TREE_VALUE (t);
2119 switch (TREE_CODE (parameter))
2120 {
2121 case TYPE_DECL:
2122 pp_cxx_ws_string (pp, "class");
2123 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
2124 pp_cxx_ws_string (pp, "...");
2125 if (DECL_NAME (parameter))
2126 pp_cxx_tree_identifier (pp, DECL_NAME (parameter));
2127 /* FIXME: Check if we should print also default argument. */
2128 break;
2129
2130 case PARM_DECL:
2131 pp_cxx_parameter_declaration (pp, parameter);
2132 break;
2133
2134 case TEMPLATE_DECL:
2135 break;
2136
2137 default:
2138 pp_unsupported_tree (pp, t);
2139 break;
2140 }
2141 }
2142
2143 /* Pretty-print a template parameter in the canonical form
2144 "template-parameter-<level>-<position in parameter list>". */
2145
2146 void
2147 pp_cxx_canonical_template_parameter (cxx_pretty_printer *pp, tree parm)
2148 {
2149 const enum tree_code code = TREE_CODE (parm);
2150
2151 /* Brings type template parameters to the canonical forms. */
2152 if (code == TEMPLATE_TYPE_PARM || code == TEMPLATE_TEMPLATE_PARM
2153 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
2154 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
2155
2156 pp_cxx_begin_template_argument_list (pp);
2157 pp_cxx_ws_string (pp, M_("template-parameter-"));
2158 pp_wide_integer (pp, TEMPLATE_PARM_LEVEL (parm));
2159 pp_minus (pp);
2160 pp_wide_integer (pp, TEMPLATE_PARM_IDX (parm) + 1);
2161 pp_cxx_end_template_argument_list (pp);
2162 }
2163
2164 /*
2165 template-declaration:
2166 export(opt) template < template-parameter-list > declaration */
2167
2168 static void
2169 pp_cxx_template_declaration (cxx_pretty_printer *pp, tree t)
2170 {
2171 tree tmpl = most_general_template (t);
2172 tree level;
2173
2174 pp_maybe_newline_and_indent (pp, 0);
2175 for (level = DECL_TEMPLATE_PARMS (tmpl); level; level = TREE_CHAIN (level))
2176 {
2177 pp_cxx_ws_string (pp, "template");
2178 pp_cxx_begin_template_argument_list (pp);
2179 pp_cxx_template_parameter_list (pp, TREE_VALUE (level));
2180 pp_cxx_end_template_argument_list (pp);
2181 pp_newline_and_indent (pp, 3);
2182 }
2183 if (TREE_CODE (t) == FUNCTION_DECL && DECL_SAVED_TREE (t))
2184 pp_cxx_function_definition (pp, t);
2185 else
2186 pp_cxx_simple_declaration (pp, t);
2187 }
2188
2189 static void
2190 pp_cxx_explicit_specialization (cxx_pretty_printer *pp, tree t)
2191 {
2192 pp_unsupported_tree (pp, t);
2193 }
2194
2195 static void
2196 pp_cxx_explicit_instantiation (cxx_pretty_printer *pp, tree t)
2197 {
2198 pp_unsupported_tree (pp, t);
2199 }
2200
2201 /*
2202 declaration:
2203 block-declaration
2204 function-definition
2205 template-declaration
2206 explicit-instantiation
2207 explicit-specialization
2208 linkage-specification
2209 namespace-definition
2210
2211 block-declaration:
2212 simple-declaration
2213 asm-definition
2214 namespace-alias-definition
2215 using-declaration
2216 using-directive
2217 static_assert-declaration */
2218 void
2219 pp_cxx_declaration (cxx_pretty_printer *pp, tree t)
2220 {
2221 if (TREE_CODE (t) == STATIC_ASSERT)
2222 {
2223 pp_cxx_ws_string (pp, "static_assert");
2224 pp_cxx_left_paren (pp);
2225 pp_cxx_expression (pp, STATIC_ASSERT_CONDITION (t));
2226 pp_cxx_separate_with (pp, ',');
2227 pp_cxx_expression (pp, STATIC_ASSERT_MESSAGE (t));
2228 pp_cxx_right_paren (pp);
2229 }
2230 else if (!DECL_LANG_SPECIFIC (t))
2231 pp_cxx_simple_declaration (pp, t);
2232 else if (DECL_USE_TEMPLATE (t))
2233 switch (DECL_USE_TEMPLATE (t))
2234 {
2235 case 1:
2236 pp_cxx_template_declaration (pp, t);
2237 break;
2238
2239 case 2:
2240 pp_cxx_explicit_specialization (pp, t);
2241 break;
2242
2243 case 3:
2244 pp_cxx_explicit_instantiation (pp, t);
2245 break;
2246
2247 default:
2248 break;
2249 }
2250 else switch (TREE_CODE (t))
2251 {
2252 case VAR_DECL:
2253 case TYPE_DECL:
2254 pp_cxx_simple_declaration (pp, t);
2255 break;
2256
2257 case FUNCTION_DECL:
2258 if (DECL_SAVED_TREE (t))
2259 pp_cxx_function_definition (pp, t);
2260 else
2261 pp_cxx_simple_declaration (pp, t);
2262 break;
2263
2264 case NAMESPACE_DECL:
2265 if (DECL_NAMESPACE_ALIAS (t))
2266 pp_cxx_namespace_alias_definition (pp, t);
2267 else
2268 pp_cxx_original_namespace_definition (pp, t);
2269 break;
2270
2271 default:
2272 pp_unsupported_tree (pp, t);
2273 break;
2274 }
2275 }
2276
2277 static void
2278 pp_cxx_typeid_expression (cxx_pretty_printer *pp, tree t)
2279 {
2280 t = TREE_OPERAND (t, 0);
2281 pp_cxx_ws_string (pp, "typeid");
2282 pp_cxx_left_paren (pp);
2283 if (TYPE_P (t))
2284 pp_cxx_type_id (pp, t);
2285 else
2286 pp_cxx_expression (pp, t);
2287 pp_cxx_right_paren (pp);
2288 }
2289
2290 void
2291 pp_cxx_va_arg_expression (cxx_pretty_printer *pp, tree t)
2292 {
2293 pp_cxx_ws_string (pp, "va_arg");
2294 pp_cxx_left_paren (pp);
2295 pp_cxx_assignment_expression (pp, TREE_OPERAND (t, 0));
2296 pp_cxx_separate_with (pp, ',');
2297 pp_cxx_type_id (pp, TREE_TYPE (t));
2298 pp_cxx_right_paren (pp);
2299 }
2300
2301 static bool
2302 pp_cxx_offsetof_expression_1 (cxx_pretty_printer *pp, tree t)
2303 {
2304 switch (TREE_CODE (t))
2305 {
2306 case ARROW_EXPR:
2307 if (TREE_CODE (TREE_OPERAND (t, 0)) == STATIC_CAST_EXPR
2308 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
2309 {
2310 pp_cxx_type_id (pp, TREE_TYPE (TREE_TYPE (TREE_OPERAND (t, 0))));
2311 pp_cxx_separate_with (pp, ',');
2312 return true;
2313 }
2314 return false;
2315 case COMPONENT_REF:
2316 if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2317 return false;
2318 if (TREE_CODE (TREE_OPERAND (t, 0)) != ARROW_EXPR)
2319 pp_cxx_dot (pp);
2320 pp_cxx_expression (pp, TREE_OPERAND (t, 1));
2321 return true;
2322 case ARRAY_REF:
2323 if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2324 return false;
2325 pp_left_bracket (pp);
2326 pp_cxx_expression (pp, TREE_OPERAND (t, 1));
2327 pp_right_bracket (pp);
2328 return true;
2329 default:
2330 return false;
2331 }
2332 }
2333
2334 void
2335 pp_cxx_offsetof_expression (cxx_pretty_printer *pp, tree t)
2336 {
2337 pp_cxx_ws_string (pp, "offsetof");
2338 pp_cxx_left_paren (pp);
2339 if (!pp_cxx_offsetof_expression_1 (pp, TREE_OPERAND (t, 0)))
2340 pp_cxx_expression (pp, TREE_OPERAND (t, 0));
2341 pp_cxx_right_paren (pp);
2342 }
2343
2344 void
2345 pp_cxx_trait_expression (cxx_pretty_printer *pp, tree t)
2346 {
2347 cp_trait_kind kind = TRAIT_EXPR_KIND (t);
2348
2349 switch (kind)
2350 {
2351 case CPTK_HAS_NOTHROW_ASSIGN:
2352 pp_cxx_ws_string (pp, "__has_nothrow_assign");
2353 break;
2354 case CPTK_HAS_TRIVIAL_ASSIGN:
2355 pp_cxx_ws_string (pp, "__has_trivial_assign");
2356 break;
2357 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
2358 pp_cxx_ws_string (pp, "__has_nothrow_constructor");
2359 break;
2360 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
2361 pp_cxx_ws_string (pp, "__has_trivial_constructor");
2362 break;
2363 case CPTK_HAS_NOTHROW_COPY:
2364 pp_cxx_ws_string (pp, "__has_nothrow_copy");
2365 break;
2366 case CPTK_HAS_TRIVIAL_COPY:
2367 pp_cxx_ws_string (pp, "__has_trivial_copy");
2368 break;
2369 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
2370 pp_cxx_ws_string (pp, "__has_trivial_destructor");
2371 break;
2372 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
2373 pp_cxx_ws_string (pp, "__has_virtual_destructor");
2374 break;
2375 case CPTK_IS_ABSTRACT:
2376 pp_cxx_ws_string (pp, "__is_abstract");
2377 break;
2378 case CPTK_IS_BASE_OF:
2379 pp_cxx_ws_string (pp, "__is_base_of");
2380 break;
2381 case CPTK_IS_CLASS:
2382 pp_cxx_ws_string (pp, "__is_class");
2383 break;
2384 case CPTK_IS_CONVERTIBLE_TO:
2385 pp_cxx_ws_string (pp, "__is_convertible_to");
2386 break;
2387 case CPTK_IS_EMPTY:
2388 pp_cxx_ws_string (pp, "__is_empty");
2389 break;
2390 case CPTK_IS_ENUM:
2391 pp_cxx_ws_string (pp, "__is_enum");
2392 break;
2393 case CPTK_IS_FINAL:
2394 pp_cxx_ws_string (pp, "__is_final");
2395 break;
2396 case CPTK_IS_POD:
2397 pp_cxx_ws_string (pp, "__is_pod");
2398 break;
2399 case CPTK_IS_POLYMORPHIC:
2400 pp_cxx_ws_string (pp, "__is_polymorphic");
2401 break;
2402 case CPTK_IS_STD_LAYOUT:
2403 pp_cxx_ws_string (pp, "__is_std_layout");
2404 break;
2405 case CPTK_IS_TRIVIAL:
2406 pp_cxx_ws_string (pp, "__is_trivial");
2407 break;
2408 case CPTK_IS_UNION:
2409 pp_cxx_ws_string (pp, "__is_union");
2410 break;
2411 case CPTK_IS_LITERAL_TYPE:
2412 pp_cxx_ws_string (pp, "__is_literal_type");
2413 break;
2414
2415 default:
2416 gcc_unreachable ();
2417 }
2418
2419 pp_cxx_left_paren (pp);
2420 pp_cxx_type_id (pp, TRAIT_EXPR_TYPE1 (t));
2421
2422 if (kind == CPTK_IS_BASE_OF || kind == CPTK_IS_CONVERTIBLE_TO)
2423 {
2424 pp_cxx_separate_with (pp, ',');
2425 pp_cxx_type_id (pp, TRAIT_EXPR_TYPE2 (t));
2426 }
2427
2428 pp_cxx_right_paren (pp);
2429 }
2430 \f
2431 typedef c_pretty_print_fn pp_fun;
2432
2433 /* Initialization of a C++ pretty-printer object. */
2434
2435 cxx_pretty_printer::cxx_pretty_printer ()
2436 : c_pretty_printer (),
2437 enclosing_scope (global_namespace)
2438 {
2439 pp_set_line_maximum_length (this, 0);
2440
2441 declaration = (pp_fun) pp_cxx_declaration;
2442 declaration_specifiers = (pp_fun) pp_cxx_decl_specifier_seq;
2443 function_specifier = (pp_fun) pp_cxx_function_specifier;
2444 type_specifier_seq = (pp_fun) pp_cxx_type_specifier_seq;
2445 declarator = (pp_fun) pp_cxx_declarator;
2446 direct_declarator = (pp_fun) pp_cxx_direct_declarator;
2447 parameter_list = (pp_fun) pp_cxx_parameter_declaration_clause;
2448 type_id = (pp_fun) pp_cxx_type_id;
2449 abstract_declarator = (pp_fun) pp_cxx_abstract_declarator;
2450 direct_abstract_declarator = (pp_fun) pp_cxx_direct_abstract_declarator;
2451 simple_type_specifier = (pp_fun) pp_cxx_simple_type_specifier;
2452
2453 /* pp->statement = (pp_fun) pp_cxx_statement; */
2454
2455 primary_expression = (pp_fun) pp_cxx_primary_expression;
2456 postfix_expression = (pp_fun) pp_cxx_postfix_expression;
2457 unary_expression = (pp_fun) pp_cxx_unary_expression;
2458 multiplicative_expression = (pp_fun) pp_cxx_multiplicative_expression;
2459 conditional_expression = (pp_fun) pp_cxx_conditional_expression;
2460 assignment_expression = (pp_fun) pp_cxx_assignment_expression;
2461 expression = (pp_fun) pp_cxx_expression;
2462 }