1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
5 Free Software Foundation, Inc.
6 Contributed by Michael Tiemann (tiemann@cygnus.com)
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
25 /* Handle method declarations. */
28 #include "coretypes.h"
36 #include "common/common-target.h"
37 #include "diagnostic.h"
41 /* Various flags to control the mangling process. */
47 /* The thing we are presently mangling is part of a template type,
48 rather than a fully instantiated type. Therefore, we may see
49 complex expressions where we would normally expect to see a
50 simple integer constant. */
51 mf_maybe_uninstantiated
= 1,
52 /* When mangling a numeric value, use the form `_XX_' (instead of
53 just `XX') if the value has more than one digit. */
54 mf_use_underscores_around_value
= 2
57 typedef enum mangling_flags mangling_flags
;
59 static void do_build_copy_assign (tree
);
60 static void do_build_copy_constructor (tree
);
61 static tree
make_alias_for_thunk (tree
);
63 /* Called once to initialize method.c. */
71 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
72 indicates whether it is a this or result adjusting thunk.
73 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
74 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
75 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
76 adjusting thunks, we scale it to a byte offset. For covariant
77 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
78 the returned thunk with finish_thunk. */
81 make_thunk (tree function
, bool this_adjusting
,
82 tree fixed_offset
, tree virtual_offset
)
87 gcc_assert (TREE_CODE (function
) == FUNCTION_DECL
);
88 /* We can have this thunks to covariant thunks, but not vice versa. */
89 gcc_assert (!DECL_THIS_THUNK_P (function
));
90 gcc_assert (!DECL_RESULT_THUNK_P (function
) || this_adjusting
);
92 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
93 if (this_adjusting
&& virtual_offset
)
95 = size_binop (MULT_EXPR
,
98 TYPE_SIZE_UNIT (vtable_entry_type
)));
100 d
= tree_low_cst (fixed_offset
, 0);
102 /* See if we already have the thunk in question. For this_adjusting
103 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
105 for (thunk
= DECL_THUNKS (function
); thunk
; thunk
= DECL_CHAIN (thunk
))
106 if (DECL_THIS_THUNK_P (thunk
) == this_adjusting
107 && THUNK_FIXED_OFFSET (thunk
) == d
108 && !virtual_offset
== !THUNK_VIRTUAL_OFFSET (thunk
)
111 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk
),
113 : THUNK_VIRTUAL_OFFSET (thunk
) == virtual_offset
)))
116 /* All thunks must be created before FUNCTION is actually emitted;
117 the ABI requires that all thunks be emitted together with the
118 function to which they transfer control. */
119 gcc_assert (!TREE_ASM_WRITTEN (function
));
120 /* Likewise, we can only be adding thunks to a function declared in
121 the class currently being laid out. */
122 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function
))
123 && TYPE_BEING_DEFINED (DECL_CONTEXT (function
)));
125 thunk
= build_decl (DECL_SOURCE_LOCATION (function
),
126 FUNCTION_DECL
, NULL_TREE
, TREE_TYPE (function
));
127 DECL_LANG_SPECIFIC (thunk
) = DECL_LANG_SPECIFIC (function
);
128 cxx_dup_lang_specific_decl (thunk
);
129 DECL_VIRTUAL_P (thunk
) = true;
130 SET_DECL_THUNKS (thunk
, NULL_TREE
);
132 DECL_CONTEXT (thunk
) = DECL_CONTEXT (function
);
133 TREE_READONLY (thunk
) = TREE_READONLY (function
);
134 TREE_THIS_VOLATILE (thunk
) = TREE_THIS_VOLATILE (function
);
135 TREE_PUBLIC (thunk
) = TREE_PUBLIC (function
);
136 SET_DECL_THUNK_P (thunk
, this_adjusting
);
137 THUNK_TARGET (thunk
) = function
;
138 THUNK_FIXED_OFFSET (thunk
) = d
;
139 THUNK_VIRTUAL_OFFSET (thunk
) = virtual_offset
;
140 THUNK_ALIAS (thunk
) = NULL_TREE
;
142 DECL_INTERFACE_KNOWN (thunk
) = 1;
143 DECL_NOT_REALLY_EXTERN (thunk
) = 1;
144 DECL_COMDAT (thunk
) = DECL_COMDAT (function
);
145 DECL_SAVED_FUNCTION_DATA (thunk
) = NULL
;
146 /* The thunk itself is not a constructor or destructor, even if
147 the thing it is thunking to is. */
148 DECL_DESTRUCTOR_P (thunk
) = 0;
149 DECL_CONSTRUCTOR_P (thunk
) = 0;
150 DECL_EXTERNAL (thunk
) = 1;
151 DECL_ARTIFICIAL (thunk
) = 1;
152 /* The THUNK is not a pending inline, even if the FUNCTION is. */
153 DECL_PENDING_INLINE_P (thunk
) = 0;
154 DECL_DECLARED_INLINE_P (thunk
) = 0;
155 /* Nor is it a template instantiation. */
156 DECL_USE_TEMPLATE (thunk
) = 0;
157 DECL_TEMPLATE_INFO (thunk
) = NULL
;
159 /* Add it to the list of thunks associated with FUNCTION. */
160 DECL_CHAIN (thunk
) = DECL_THUNKS (function
);
161 SET_DECL_THUNKS (function
, thunk
);
166 /* Finish THUNK, a thunk decl. */
169 finish_thunk (tree thunk
)
172 tree fixed_offset
= ssize_int (THUNK_FIXED_OFFSET (thunk
));
173 tree virtual_offset
= THUNK_VIRTUAL_OFFSET (thunk
);
175 gcc_assert (!DECL_NAME (thunk
) && DECL_THUNK_P (thunk
));
176 if (virtual_offset
&& DECL_RESULT_THUNK_P (thunk
))
177 virtual_offset
= BINFO_VPTR_FIELD (virtual_offset
);
178 function
= THUNK_TARGET (thunk
);
179 name
= mangle_thunk (function
, DECL_THIS_THUNK_P (thunk
),
180 fixed_offset
, virtual_offset
);
182 /* We can end up with declarations of (logically) different
183 covariant thunks, that do identical adjustments. The two thunks
184 will be adjusting between within different hierarchies, which
185 happen to have the same layout. We must nullify one of them to
186 refer to the other. */
187 if (DECL_RESULT_THUNK_P (thunk
))
191 for (cov_probe
= DECL_THUNKS (function
);
192 cov_probe
; cov_probe
= DECL_CHAIN (cov_probe
))
193 if (DECL_NAME (cov_probe
) == name
)
195 gcc_assert (!DECL_THUNKS (thunk
));
196 THUNK_ALIAS (thunk
) = (THUNK_ALIAS (cov_probe
)
197 ? THUNK_ALIAS (cov_probe
) : cov_probe
);
202 DECL_NAME (thunk
) = name
;
203 SET_DECL_ASSEMBLER_NAME (thunk
, name
);
206 static GTY (()) int thunk_labelno
;
208 /* Create a static alias to target. */
211 make_alias_for (tree target
, tree newid
)
213 tree alias
= build_decl (DECL_SOURCE_LOCATION (target
),
214 TREE_CODE (target
), newid
, TREE_TYPE (target
));
215 DECL_LANG_SPECIFIC (alias
) = DECL_LANG_SPECIFIC (target
);
216 cxx_dup_lang_specific_decl (alias
);
217 DECL_CONTEXT (alias
) = NULL
;
218 TREE_READONLY (alias
) = TREE_READONLY (target
);
219 TREE_THIS_VOLATILE (alias
) = TREE_THIS_VOLATILE (target
);
220 TREE_PUBLIC (alias
) = 0;
221 DECL_INTERFACE_KNOWN (alias
) = 1;
222 if (DECL_LANG_SPECIFIC (alias
))
224 DECL_NOT_REALLY_EXTERN (alias
) = 1;
225 DECL_USE_TEMPLATE (alias
) = 0;
226 DECL_TEMPLATE_INFO (alias
) = NULL
;
228 DECL_EXTERNAL (alias
) = 0;
229 DECL_ARTIFICIAL (alias
) = 1;
230 DECL_TEMPLATE_INSTANTIATED (alias
) = 0;
231 if (TREE_CODE (alias
) == FUNCTION_DECL
)
233 DECL_SAVED_FUNCTION_DATA (alias
) = NULL
;
234 DECL_DESTRUCTOR_P (alias
) = 0;
235 DECL_CONSTRUCTOR_P (alias
) = 0;
236 DECL_PENDING_INLINE_P (alias
) = 0;
237 DECL_DECLARED_INLINE_P (alias
) = 0;
238 DECL_INITIAL (alias
) = error_mark_node
;
239 DECL_ARGUMENTS (alias
) = copy_list (DECL_ARGUMENTS (target
));
242 TREE_STATIC (alias
) = 1;
243 TREE_ADDRESSABLE (alias
) = 1;
244 TREE_USED (alias
) = 1;
245 SET_DECL_ASSEMBLER_NAME (alias
, DECL_NAME (alias
));
250 make_alias_for_thunk (tree function
)
255 targetm
.asm_out
.generate_internal_label (buf
, "LTHUNK", thunk_labelno
);
258 alias
= make_alias_for (function
, get_identifier (buf
));
260 if (!flag_syntax_only
)
262 struct cgraph_node
*funcn
, *aliasn
;
263 funcn
= cgraph_get_node (function
);
264 gcc_checking_assert (funcn
);
265 aliasn
= cgraph_same_body_alias (funcn
, alias
, function
);
266 DECL_ASSEMBLER_NAME (function
);
267 gcc_assert (aliasn
!= NULL
);
273 /* Emit the definition of a C++ multiple inheritance or covariant
274 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
278 use_thunk (tree thunk_fndecl
, bool emit_p
)
280 tree a
, t
, function
, alias
;
282 HOST_WIDE_INT fixed_offset
, virtual_value
;
283 bool this_adjusting
= DECL_THIS_THUNK_P (thunk_fndecl
);
284 struct cgraph_node
*funcn
, *thunk_node
;
286 /* We should have called finish_thunk to give it a name. */
287 gcc_assert (DECL_NAME (thunk_fndecl
));
289 /* We should never be using an alias, always refer to the
291 gcc_assert (!THUNK_ALIAS (thunk_fndecl
));
293 if (TREE_ASM_WRITTEN (thunk_fndecl
))
296 function
= THUNK_TARGET (thunk_fndecl
);
297 if (DECL_RESULT (thunk_fndecl
))
298 /* We already turned this thunk into an ordinary function.
299 There's no need to process this thunk again. */
302 if (DECL_THUNK_P (function
))
303 /* The target is itself a thunk, process it now. */
304 use_thunk (function
, emit_p
);
306 /* Thunks are always addressable; they only appear in vtables. */
307 TREE_ADDRESSABLE (thunk_fndecl
) = 1;
309 /* Figure out what function is being thunked to. It's referenced in
310 this translation unit. */
311 TREE_ADDRESSABLE (function
) = 1;
312 mark_used (function
);
316 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function
))
317 alias
= make_alias_for_thunk (function
);
321 fixed_offset
= THUNK_FIXED_OFFSET (thunk_fndecl
);
322 virtual_offset
= THUNK_VIRTUAL_OFFSET (thunk_fndecl
);
327 virtual_offset
= BINFO_VPTR_FIELD (virtual_offset
);
328 virtual_value
= tree_low_cst (virtual_offset
, /*pos=*/0);
329 gcc_assert (virtual_value
);
334 /* And, if we need to emit the thunk, it's used. */
335 mark_used (thunk_fndecl
);
336 /* This thunk is actually defined. */
337 DECL_EXTERNAL (thunk_fndecl
) = 0;
338 /* The linkage of the function may have changed. FIXME in linkage
340 gcc_assert (DECL_INTERFACE_KNOWN (function
));
341 TREE_PUBLIC (thunk_fndecl
) = TREE_PUBLIC (function
);
342 DECL_VISIBILITY (thunk_fndecl
) = DECL_VISIBILITY (function
);
343 DECL_VISIBILITY_SPECIFIED (thunk_fndecl
)
344 = DECL_VISIBILITY_SPECIFIED (function
);
345 DECL_COMDAT (thunk_fndecl
) = DECL_COMDAT (function
);
346 DECL_WEAK (thunk_fndecl
) = DECL_WEAK (function
);
348 if (flag_syntax_only
)
350 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
354 push_to_top_level ();
356 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function
)
357 && targetm_common
.have_named_sections
)
359 resolve_unique_section (function
, 0, flag_function_sections
);
361 if (DECL_SECTION_NAME (function
) != NULL
&& DECL_ONE_ONLY (function
))
363 resolve_unique_section (thunk_fndecl
, 0, flag_function_sections
);
365 /* Output the thunk into the same section as function. */
366 DECL_SECTION_NAME (thunk_fndecl
) = DECL_SECTION_NAME (function
);
370 /* Set up cloned argument trees for the thunk. */
372 for (a
= DECL_ARGUMENTS (function
); a
; a
= DECL_CHAIN (a
))
374 tree x
= copy_node (a
);
376 DECL_CONTEXT (x
) = thunk_fndecl
;
377 SET_DECL_RTL (x
, NULL
);
378 DECL_HAS_VALUE_EXPR_P (x
) = 0;
379 TREE_ADDRESSABLE (x
) = 0;
383 DECL_ARGUMENTS (thunk_fndecl
) = a
;
384 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
385 funcn
= cgraph_get_node (function
);
386 gcc_checking_assert (funcn
);
387 thunk_node
= cgraph_add_thunk (funcn
, thunk_fndecl
, function
,
388 this_adjusting
, fixed_offset
, virtual_value
,
389 virtual_offset
, alias
);
390 if (DECL_ONE_ONLY (function
))
391 symtab_add_to_same_comdat_group ((symtab_node
) thunk_node
,
392 (symtab_node
) funcn
);
395 || !targetm
.asm_out
.can_output_mi_thunk (thunk_fndecl
, fixed_offset
,
396 virtual_value
, alias
))
398 /* If this is a covariant thunk, or we don't have the necessary
399 code for efficient thunks, generate a thunk function that
400 just makes a call to the real function. Unfortunately, this
401 doesn't work for varargs. */
403 if (varargs_function_p (function
))
404 error ("generic thunk code fails for method %q#D which uses %<...%>",
408 pop_from_top_level ();
411 /* Code for synthesizing methods which have default semantics defined. */
413 /* True iff CTYPE has a trivial SFK. */
416 type_has_trivial_fn (tree ctype
, special_function_kind sfk
)
420 case sfk_constructor
:
421 return !TYPE_HAS_COMPLEX_DFLT (ctype
);
422 case sfk_copy_constructor
:
423 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype
);
424 case sfk_move_constructor
:
425 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype
);
426 case sfk_copy_assignment
:
427 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype
);
428 case sfk_move_assignment
:
429 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype
);
431 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
);
432 case sfk_inheriting_constructor
:
439 /* Note that CTYPE has a non-trivial SFK even though we previously thought
443 type_set_nontrivial_flag (tree ctype
, special_function_kind sfk
)
447 case sfk_constructor
:
448 TYPE_HAS_COMPLEX_DFLT (ctype
) = true;
450 case sfk_copy_constructor
:
451 TYPE_HAS_COMPLEX_COPY_CTOR (ctype
) = true;
453 case sfk_move_constructor
:
454 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype
) = true;
456 case sfk_copy_assignment
:
457 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype
) = true;
459 case sfk_move_assignment
:
460 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype
) = true;
463 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
) = true;
465 case sfk_inheriting_constructor
:
471 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
474 trivial_fn_p (tree fn
)
476 if (!DECL_DEFAULTED_FN (fn
))
479 /* If fn is a clone, get the primary variant. */
480 fn
= DECL_ORIGIN (fn
);
481 return type_has_trivial_fn (DECL_CONTEXT (fn
), special_function_p (fn
));
484 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
485 given the parameter or parameters PARM, possibly inherited constructor
486 base INH, or move flag MOVE_P. */
489 add_one_base_init (tree binfo
, tree parm
, bool move_p
, tree inh
,
490 tree member_init_list
)
495 /* An inheriting constructor only has a mem-initializer for
496 the base it inherits from. */
497 if (BINFO_TYPE (binfo
) != inh
)
498 return member_init_list
;
502 for (; parm
; parm
= DECL_CHAIN (parm
))
504 tree exp
= convert_from_reference (parm
);
505 if (TREE_CODE (TREE_TYPE (parm
)) != REFERENCE_TYPE
)
507 *p
= build_tree_list (NULL_TREE
, exp
);
508 p
= &TREE_CHAIN (*p
);
513 init
= build_base_path (PLUS_EXPR
, parm
, binfo
, 1,
514 tf_warning_or_error
);
517 init
= build_tree_list (NULL_TREE
, init
);
519 return tree_cons (binfo
, init
, member_init_list
);
522 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
526 do_build_copy_constructor (tree fndecl
)
528 tree parm
= FUNCTION_FIRST_USER_PARM (fndecl
);
529 bool move_p
= DECL_MOVE_CONSTRUCTOR_P (fndecl
);
530 bool trivial
= trivial_fn_p (fndecl
);
531 tree inh
= DECL_INHERITED_CTOR_BASE (fndecl
);
534 parm
= convert_from_reference (parm
);
537 && is_empty_class (current_class_type
))
538 /* Don't copy the padding byte; it might not have been allocated
539 if *this is a base subobject. */;
542 tree t
= build2 (INIT_EXPR
, void_type_node
, current_class_ref
, parm
);
543 finish_expr_stmt (t
);
547 tree fields
= TYPE_FIELDS (current_class_type
);
548 tree member_init_list
= NULL_TREE
;
549 int cvquals
= cp_type_quals (TREE_TYPE (parm
));
551 tree binfo
, base_binfo
;
553 vec
<tree
, va_gc
> *vbases
;
555 /* Initialize all the base-classes with the parameter converted
556 to their type so that we get their copy constructor and not
557 another constructor that takes current_class_type. We must
558 deal with the binfo's directly as a direct base might be
559 inaccessible due to ambiguity. */
560 for (vbases
= CLASSTYPE_VBASECLASSES (current_class_type
), i
= 0;
561 vec_safe_iterate (vbases
, i
, &binfo
); i
++)
563 member_init_list
= add_one_base_init (binfo
, parm
, move_p
, inh
,
567 for (binfo
= TYPE_BINFO (current_class_type
), i
= 0;
568 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
570 if (BINFO_VIRTUAL_P (base_binfo
))
572 member_init_list
= add_one_base_init (base_binfo
, parm
, move_p
,
573 inh
, member_init_list
);
576 for (; fields
; fields
= DECL_CHAIN (fields
))
581 if (TREE_CODE (field
) != FIELD_DECL
)
586 expr_type
= TREE_TYPE (field
);
587 if (DECL_NAME (field
))
589 if (VFIELD_NAME_P (DECL_NAME (field
)))
592 else if (ANON_AGGR_TYPE_P (expr_type
) && TYPE_FIELDS (expr_type
))
593 /* Just use the field; anonymous types can't have
594 nontrivial copy ctors or assignment ops or this
595 function would be deleted. */;
599 /* Compute the type of "init->field". If the copy-constructor
600 parameter is, for example, "const S&", and the type of
601 the field is "T", then the type will usually be "const
602 T". (There are no cv-qualified variants of reference
604 if (TREE_CODE (expr_type
) != REFERENCE_TYPE
)
608 if (DECL_MUTABLE_P (field
))
609 quals
&= ~TYPE_QUAL_CONST
;
610 quals
|= cp_type_quals (expr_type
);
611 expr_type
= cp_build_qualified_type (expr_type
, quals
);
614 init
= build3 (COMPONENT_REF
, expr_type
, parm
, field
, NULL_TREE
);
615 if (move_p
&& TREE_CODE (expr_type
) != REFERENCE_TYPE
)
617 init
= build_tree_list (NULL_TREE
, init
);
619 member_init_list
= tree_cons (field
, init
, member_init_list
);
621 finish_mem_initializers (member_init_list
);
626 do_build_copy_assign (tree fndecl
)
628 tree parm
= DECL_CHAIN (DECL_ARGUMENTS (fndecl
));
630 bool move_p
= move_fn_p (fndecl
);
631 bool trivial
= trivial_fn_p (fndecl
);
632 int flags
= LOOKUP_NORMAL
| LOOKUP_NONVIRTUAL
| LOOKUP_DEFAULTED
;
634 compound_stmt
= begin_compound_stmt (0);
635 parm
= convert_from_reference (parm
);
638 && is_empty_class (current_class_type
))
639 /* Don't copy the padding byte; it might not have been allocated
640 if *this is a base subobject. */;
643 tree t
= build2 (MODIFY_EXPR
, void_type_node
, current_class_ref
, parm
);
644 finish_expr_stmt (t
);
649 int cvquals
= cp_type_quals (TREE_TYPE (parm
));
651 tree binfo
, base_binfo
;
653 /* Assign to each of the direct base classes. */
654 for (binfo
= TYPE_BINFO (current_class_type
), i
= 0;
655 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
658 vec
<tree
, va_gc
> *parmvec
;
660 /* We must convert PARM directly to the base class
661 explicitly since the base class may be ambiguous. */
662 converted_parm
= build_base_path (PLUS_EXPR
, parm
, base_binfo
, 1,
663 tf_warning_or_error
);
665 converted_parm
= move (converted_parm
);
666 /* Call the base class assignment operator. */
667 parmvec
= make_tree_vector_single (converted_parm
);
669 (build_special_member_call (current_class_ref
,
670 ansi_assopname (NOP_EXPR
),
674 tf_warning_or_error
));
675 release_tree_vector (parmvec
);
678 /* Assign to each of the non-static data members. */
679 for (fields
= TYPE_FIELDS (current_class_type
);
681 fields
= DECL_CHAIN (fields
))
683 tree comp
= current_class_ref
;
689 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
692 expr_type
= TREE_TYPE (field
);
694 if (CP_TYPE_CONST_P (expr_type
))
696 error ("non-static const member %q#D, can%'t use default "
697 "assignment operator", field
);
700 else if (TREE_CODE (expr_type
) == REFERENCE_TYPE
)
702 error ("non-static reference member %q#D, can%'t use "
703 "default assignment operator", field
);
707 if (DECL_NAME (field
))
709 if (VFIELD_NAME_P (DECL_NAME (field
)))
712 else if (ANON_AGGR_TYPE_P (expr_type
)
713 && TYPE_FIELDS (expr_type
) != NULL_TREE
)
714 /* Just use the field; anonymous types can't have
715 nontrivial copy ctors or assignment ops or this
716 function would be deleted. */;
720 comp
= build3 (COMPONENT_REF
, expr_type
, comp
, field
, NULL_TREE
);
722 /* Compute the type of init->field */
724 if (DECL_MUTABLE_P (field
))
725 quals
&= ~TYPE_QUAL_CONST
;
726 expr_type
= cp_build_qualified_type (expr_type
, quals
);
728 init
= build3 (COMPONENT_REF
, expr_type
, init
, field
, NULL_TREE
);
729 if (move_p
&& TREE_CODE (expr_type
) != REFERENCE_TYPE
)
732 if (DECL_NAME (field
))
733 init
= cp_build_modify_expr (comp
, NOP_EXPR
, init
,
734 tf_warning_or_error
);
736 init
= build2 (MODIFY_EXPR
, TREE_TYPE (comp
), comp
, init
);
737 finish_expr_stmt (init
);
740 finish_return_stmt (current_class_ref
);
741 finish_compound_stmt (compound_stmt
);
744 /* Synthesize FNDECL, a non-static member function. */
747 synthesize_method (tree fndecl
)
749 bool nested
= (current_function_decl
!= NULL_TREE
);
750 tree context
= decl_function_context (fndecl
);
751 bool need_body
= true;
753 location_t save_input_location
= input_location
;
754 int error_count
= errorcount
;
755 int warning_count
= warningcount
;
757 /* Reset the source location, we might have been previously
758 deferred, and thus have saved where we were first needed. */
759 DECL_SOURCE_LOCATION (fndecl
)
760 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl
)));
762 /* If we've been asked to synthesize a clone, just synthesize the
763 cloned function instead. Doing so will automatically fill in the
764 body for the clone. */
765 if (DECL_CLONED_FUNCTION_P (fndecl
))
766 fndecl
= DECL_CLONED_FUNCTION (fndecl
);
768 /* We may be in the middle of deferred access check. Disable
770 push_deferring_access_checks (dk_no_deferred
);
773 push_to_top_level ();
775 push_function_context ();
777 input_location
= DECL_SOURCE_LOCATION (fndecl
);
779 start_preparsed_function (fndecl
, NULL_TREE
, SF_DEFAULT
| SF_PRE_PARSED
);
780 stmt
= begin_function_body ();
782 if (DECL_OVERLOADED_OPERATOR_P (fndecl
) == NOP_EXPR
)
784 do_build_copy_assign (fndecl
);
787 else if (DECL_CONSTRUCTOR_P (fndecl
))
789 tree arg_chain
= FUNCTION_FIRST_USER_PARMTYPE (fndecl
);
790 if (arg_chain
!= void_list_node
)
791 do_build_copy_constructor (fndecl
);
793 finish_mem_initializers (NULL_TREE
);
796 /* If we haven't yet generated the body of the function, just
797 generate an empty compound statement. */
801 compound_stmt
= begin_compound_stmt (BCS_FN_BODY
);
802 finish_compound_stmt (compound_stmt
);
805 finish_function_body (stmt
);
806 expand_or_defer_fn (finish_function (0));
808 input_location
= save_input_location
;
811 pop_from_top_level ();
813 pop_function_context ();
815 pop_deferring_access_checks ();
817 if (error_count
!= errorcount
|| warning_count
!= warningcount
)
818 inform (input_location
, "synthesized method %qD first required here ",
822 /* Build a reference to type TYPE with cv-quals QUALS, which is an
823 rvalue if RVALUE is true. */
826 build_stub_type (tree type
, int quals
, bool rvalue
)
828 tree argtype
= cp_build_qualified_type (type
, quals
);
829 return cp_build_reference_type (argtype
, rvalue
);
832 /* Build a dummy glvalue from dereferencing a dummy reference of type
836 build_stub_object (tree reftype
)
838 tree stub
= build1 (NOP_EXPR
, reftype
, integer_one_node
);
839 return convert_from_reference (stub
);
842 /* Determine which function will be called when looking up NAME in TYPE,
843 called with a single ARGTYPE argument, or no argument if ARGTYPE is
844 null. FLAGS and COMPLAIN are as for build_new_method_call.
846 Returns a FUNCTION_DECL if all is well.
847 Returns NULL_TREE if overload resolution failed.
848 Returns error_mark_node if the chosen function cannot be called. */
851 locate_fn_flags (tree type
, tree name
, tree argtype
, int flags
,
852 tsubst_flags_t complain
)
854 tree ob
, fn
, fns
, binfo
, rval
;
855 vec
<tree
, va_gc
> *args
;
858 binfo
= TYPE_BINFO (type
);
862 type
= BINFO_TYPE (binfo
);
865 ob
= build_stub_object (cp_build_reference_type (type
, false));
866 args
= make_tree_vector ();
869 if (TREE_CODE (argtype
) == TREE_LIST
)
871 for (tree elt
= argtype
; elt
!= void_list_node
;
872 elt
= TREE_CHAIN (elt
))
874 tree type
= TREE_VALUE (elt
);
875 if (TREE_CODE (type
) != REFERENCE_TYPE
)
876 type
= cp_build_reference_type (type
, /*rval*/true);
877 tree arg
= build_stub_object (type
);
878 vec_safe_push (args
, arg
);
883 tree arg
= build_stub_object (argtype
);
884 args
->quick_push (arg
);
888 fns
= lookup_fnfields (binfo
, name
, 0);
889 rval
= build_new_method_call (ob
, fns
, &args
, binfo
, flags
, &fn
, complain
);
891 release_tree_vector (args
);
892 if (fn
&& rval
== error_mark_node
)
898 /* Locate the dtor of TYPE. */
901 get_dtor (tree type
, tsubst_flags_t complain
)
903 tree fn
= locate_fn_flags (type
, complete_dtor_identifier
, NULL_TREE
,
904 LOOKUP_NORMAL
, complain
);
905 if (fn
== error_mark_node
)
910 /* Locate the default ctor of TYPE. */
913 locate_ctor (tree type
)
917 push_deferring_access_checks (dk_no_check
);
918 fn
= locate_fn_flags (type
, complete_ctor_identifier
, NULL_TREE
,
919 LOOKUP_SPECULATIVE
, tf_none
);
920 pop_deferring_access_checks ();
921 if (fn
== error_mark_node
)
926 /* Likewise, but give any appropriate errors. */
929 get_default_ctor (tree type
)
931 tree fn
= locate_fn_flags (type
, complete_ctor_identifier
, NULL_TREE
,
932 LOOKUP_NORMAL
, tf_warning_or_error
);
933 if (fn
== error_mark_node
)
938 /* Locate the copy ctor of TYPE. */
941 get_copy_ctor (tree type
, tsubst_flags_t complain
)
943 int quals
= (TYPE_HAS_CONST_COPY_CTOR (type
)
944 ? TYPE_QUAL_CONST
: TYPE_UNQUALIFIED
);
945 tree argtype
= build_stub_type (type
, quals
, false);
946 tree fn
= locate_fn_flags (type
, complete_ctor_identifier
, argtype
,
947 LOOKUP_NORMAL
, complain
);
948 if (fn
== error_mark_node
)
953 /* Locate the copy assignment operator of TYPE. */
956 get_copy_assign (tree type
)
958 int quals
= (TYPE_HAS_CONST_COPY_ASSIGN (type
)
959 ? TYPE_QUAL_CONST
: TYPE_UNQUALIFIED
);
960 tree argtype
= build_stub_type (type
, quals
, false);
961 tree fn
= locate_fn_flags (type
, ansi_assopname (NOP_EXPR
), argtype
,
962 LOOKUP_NORMAL
, tf_warning_or_error
);
963 if (fn
== error_mark_node
)
968 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
969 DELETED_P or give an error message MSG with argument ARG. */
972 process_subob_fn (tree fn
, tree
*spec_p
, bool *trivial_p
,
973 bool *deleted_p
, bool *constexpr_p
,
976 if (!fn
|| fn
== error_mark_node
)
981 tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
982 *spec_p
= merge_exception_specifiers (*spec_p
, raises
, fn
);
985 if (!trivial_fn_p (fn
))
989 if (TREE_CODE (arg
) == FIELD_DECL
990 && TREE_CODE (DECL_CONTEXT (arg
)) == UNION_TYPE
)
995 error ("union member %q+D with non-trivial %qD", arg
, fn
);
999 if (constexpr_p
&& !DECL_DECLARED_CONSTEXPR_P (fn
))
1001 *constexpr_p
= false;
1004 inform (0, "defaulted constructor calls non-constexpr "
1006 explain_invalid_constexpr_fn (fn
);
1017 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1021 walk_field_subobs (tree fields
, tree fnname
, special_function_kind sfk
,
1022 int quals
, bool copy_arg_p
, bool move_p
,
1023 bool assign_p
, tree
*spec_p
, bool *trivial_p
,
1024 bool *deleted_p
, bool *constexpr_p
,
1025 bool diag
, int flags
, tsubst_flags_t complain
)
1028 for (field
= fields
; field
; field
= DECL_CHAIN (field
))
1030 tree mem_type
, argtype
, rval
;
1032 if (TREE_CODE (field
) != FIELD_DECL
1033 || DECL_ARTIFICIAL (field
))
1036 mem_type
= strip_array_types (TREE_TYPE (field
));
1040 if (CP_TYPE_CONST_P (mem_type
) && !CLASS_TYPE_P (mem_type
))
1043 error ("non-static const member %q#D, can%'t use default "
1044 "assignment operator", field
);
1046 else if (TREE_CODE (mem_type
) == REFERENCE_TYPE
)
1049 error ("non-static reference member %q#D, can%'t use "
1050 "default assignment operator", field
);
1055 if (bad
&& deleted_p
)
1058 else if (sfk
== sfk_constructor
)
1062 if (DECL_INITIAL (field
))
1064 if (diag
&& DECL_INITIAL (field
) == error_mark_node
)
1065 inform (0, "initializer for %q+#D is invalid", field
);
1069 /* Core 1351: If the field has an NSDMI that could throw, the
1070 default constructor is noexcept(false). FIXME this is
1071 broken by deferred parsing and 1360 saying we can't lazily
1072 declare a non-trivial default constructor. Also this
1073 needs to do deferred instantiation. Disable until the
1074 conflict between 1351 and 1360 is resolved. */
1075 if (spec_p
&& !expr_noexcept_p (DECL_INITIAL (field
), complain
))
1076 *spec_p
= noexcept_false_spec
;
1079 /* Don't do the normal processing. */
1084 if (CP_TYPE_CONST_P (mem_type
)
1085 && default_init_uninitialized_part (mem_type
))
1088 error ("uninitialized non-static const member %q#D",
1092 else if (TREE_CODE (mem_type
) == REFERENCE_TYPE
)
1095 error ("uninitialized non-static reference member %q#D",
1100 if (bad
&& deleted_p
)
1103 /* For an implicitly-defined default constructor to be constexpr,
1104 every member must have a user-provided default constructor or
1105 an explicit initializer. */
1106 if (constexpr_p
&& !CLASS_TYPE_P (mem_type
)
1107 && TREE_CODE (DECL_CONTEXT (field
)) != UNION_TYPE
)
1109 *constexpr_p
= false;
1111 inform (0, "defaulted default constructor does not "
1112 "initialize %q+#D", field
);
1116 if (!CLASS_TYPE_P (mem_type
))
1119 if (ANON_AGGR_TYPE_P (mem_type
))
1121 walk_field_subobs (TYPE_FIELDS (mem_type
), fnname
, sfk
, quals
,
1122 copy_arg_p
, move_p
, assign_p
, spec_p
, trivial_p
,
1123 deleted_p
, constexpr_p
,
1124 diag
, flags
, complain
);
1130 int mem_quals
= cp_type_quals (mem_type
) | quals
;
1131 if (DECL_MUTABLE_P (field
))
1132 mem_quals
&= ~TYPE_QUAL_CONST
;
1133 argtype
= build_stub_type (mem_type
, mem_quals
, move_p
);
1136 argtype
= NULL_TREE
;
1138 rval
= locate_fn_flags (mem_type
, fnname
, argtype
, flags
, complain
);
1140 process_subob_fn (rval
, spec_p
, trivial_p
, deleted_p
,
1141 constexpr_p
, diag
, field
);
1145 /* The caller wants to generate an implicit declaration of SFK for CTYPE
1146 which is const if relevant and CONST_P is set. If spec_p, trivial_p and
1147 deleted_p are non-null, set their referent appropriately. If diag is
1148 true, we're either being called from maybe_explain_implicit_delete to
1149 give errors, or if constexpr_p is non-null, from
1150 explain_invalid_constexpr_fn. */
1153 synthesized_method_walk (tree ctype
, special_function_kind sfk
, bool const_p
,
1154 tree
*spec_p
, bool *trivial_p
, bool *deleted_p
,
1155 bool *constexpr_p
, bool diag
,
1156 tree inherited_base
, tree inherited_parms
)
1158 tree binfo
, base_binfo
, scope
, fnname
, rval
, argtype
;
1159 bool move_p
, copy_arg_p
, assign_p
, expected_trivial
, check_vdtor
;
1160 vec
<tree
, va_gc
> *vbases
;
1161 int i
, quals
, flags
;
1162 tsubst_flags_t complain
;
1166 *spec_p
= (cxx_dialect
>= cxx0x
? noexcept_true_spec
: empty_except_spec
);
1170 /* "The closure type associated with a lambda-expression has a deleted
1171 default constructor and a deleted copy assignment operator."
1172 This is diagnosed in maybe_explain_implicit_delete. */
1173 if (LAMBDA_TYPE_P (ctype
)
1174 && (sfk
== sfk_constructor
1175 || sfk
== sfk_copy_assignment
))
1186 check_vdtor
= false;
1189 case sfk_move_assignment
:
1190 case sfk_copy_assignment
:
1192 fnname
= ansi_assopname (NOP_EXPR
);
1195 case sfk_destructor
:
1197 /* The synthesized method will call base dtors, but check complete
1198 here to avoid having to deal with VTT. */
1199 fnname
= complete_dtor_identifier
;
1202 case sfk_constructor
:
1203 case sfk_move_constructor
:
1204 case sfk_copy_constructor
:
1205 case sfk_inheriting_constructor
:
1207 fnname
= complete_ctor_identifier
;
1214 gcc_assert ((sfk
== sfk_inheriting_constructor
)
1215 == (inherited_base
!= NULL_TREE
));
1217 /* If that user-written default constructor would satisfy the
1218 requirements of a constexpr constructor (7.1.5), the
1219 implicitly-defined default constructor is constexpr. */
1221 *constexpr_p
= ctor_p
;
1226 case sfk_constructor
:
1227 case sfk_destructor
:
1228 case sfk_inheriting_constructor
:
1232 case sfk_move_constructor
:
1233 case sfk_move_assignment
:
1235 case sfk_copy_constructor
:
1236 case sfk_copy_assignment
:
1244 expected_trivial
= type_has_trivial_fn (ctype
, sfk
);
1246 *trivial_p
= expected_trivial
;
1248 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1249 class versions and other properties of the type. But a subobject
1250 class can be trivially copyable and yet have overload resolution
1251 choose a template constructor for initialization, depending on
1252 rvalueness and cv-quals. So we can't exit early for copy/move
1253 methods in C++0x. The same considerations apply in C++98/03, but
1254 there the definition of triviality does not consider overload
1255 resolution, so a constructor can be trivial even if it would otherwise
1256 call a non-trivial constructor. */
1257 if (expected_trivial
1258 && (!copy_arg_p
|| cxx_dialect
< cxx0x
))
1260 if (constexpr_p
&& sfk
== sfk_constructor
)
1262 bool cx
= trivial_default_constructor_is_constexpr (ctype
);
1264 if (diag
&& !cx
&& TREE_CODE (ctype
) == UNION_TYPE
)
1265 /* A trivial constructor doesn't have any NSDMI. */
1266 inform (input_location
, "defaulted default constructor does "
1267 "not initialize any non-static data member");
1273 ++cp_unevaluated_operand
;
1274 ++c_inhibit_evaluation_warnings
;
1275 push_deferring_access_checks (dk_no_deferred
);
1277 scope
= push_scope (ctype
);
1279 flags
= LOOKUP_NORMAL
|LOOKUP_SPECULATIVE
;
1280 if (!inherited_base
)
1281 flags
|= LOOKUP_DEFAULTED
;
1283 complain
= diag
? tf_warning_or_error
: tf_none
;
1286 quals
= TYPE_QUAL_CONST
;
1288 quals
= TYPE_UNQUALIFIED
;
1289 argtype
= NULL_TREE
;
1291 for (binfo
= TYPE_BINFO (ctype
), i
= 0;
1292 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
1294 tree basetype
= BINFO_TYPE (base_binfo
);
1296 if (!assign_p
&& BINFO_VIRTUAL_P (base_binfo
))
1297 /* We'll handle virtual bases below. */
1301 argtype
= build_stub_type (basetype
, quals
, move_p
);
1302 else if (basetype
== inherited_base
)
1303 argtype
= inherited_parms
;
1304 rval
= locate_fn_flags (base_binfo
, fnname
, argtype
, flags
, complain
);
1306 argtype
= NULL_TREE
;
1308 process_subob_fn (rval
, spec_p
, trivial_p
, deleted_p
,
1309 constexpr_p
, diag
, basetype
);
1310 if (ctor_p
&& TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype
))
1312 /* In a constructor we also need to check the subobject
1313 destructors for cleanup of partially constructed objects. */
1314 rval
= locate_fn_flags (base_binfo
, complete_dtor_identifier
,
1315 NULL_TREE
, flags
, complain
);
1316 /* Note that we don't pass down trivial_p; the subobject
1317 destructors don't affect triviality of the constructor. Nor
1318 do they affect constexpr-ness (a constant expression doesn't
1319 throw) or exception-specification (a throw from one of the
1320 dtors would be a double-fault). */
1321 process_subob_fn (rval
, NULL
, NULL
,
1322 deleted_p
, NULL
, false,
1326 if (check_vdtor
&& type_has_virtual_destructor (basetype
))
1328 rval
= locate_fn_flags (ctype
, ansi_opname (DELETE_EXPR
),
1329 ptr_type_node
, flags
, complain
);
1330 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1331 to have a null rval (no class-specific op delete). */
1332 if (rval
&& rval
== error_mark_node
&& deleted_p
)
1334 check_vdtor
= false;
1337 if (diag
&& assign_p
&& move_p
1338 && BINFO_VIRTUAL_P (base_binfo
)
1339 && rval
&& TREE_CODE (rval
) == FUNCTION_DECL
1340 && move_fn_p (rval
) && !trivial_fn_p (rval
))
1341 warning (OPT_Wvirtual_move_assign
,
1342 "defaulted move assignment for %qT calls a non-trivial "
1343 "move assignment operator for virtual base %qT",
1347 vbases
= CLASSTYPE_VBASECLASSES (ctype
);
1349 /* No virtual bases to worry about. */;
1353 *constexpr_p
= false;
1354 FOR_EACH_VEC_ELT (*vbases
, i
, base_binfo
)
1356 tree basetype
= BINFO_TYPE (base_binfo
);
1358 argtype
= build_stub_type (basetype
, quals
, move_p
);
1359 rval
= locate_fn_flags (base_binfo
, fnname
, argtype
, flags
, complain
);
1361 process_subob_fn (rval
, spec_p
, trivial_p
, deleted_p
,
1362 constexpr_p
, diag
, basetype
);
1363 if (ctor_p
&& TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype
))
1365 rval
= locate_fn_flags (base_binfo
, complete_dtor_identifier
,
1366 NULL_TREE
, flags
, complain
);
1367 process_subob_fn (rval
, NULL
, NULL
,
1368 deleted_p
, NULL
, false,
1374 /* Now handle the non-static data members. */
1375 walk_field_subobs (TYPE_FIELDS (ctype
), fnname
, sfk
, quals
,
1376 copy_arg_p
, move_p
, assign_p
, spec_p
, trivial_p
,
1377 deleted_p
, constexpr_p
,
1378 diag
, flags
, complain
);
1380 walk_field_subobs (TYPE_FIELDS (ctype
), complete_dtor_identifier
,
1381 sfk_destructor
, TYPE_UNQUALIFIED
, false,
1382 false, false, NULL
, NULL
,
1384 false, flags
, complain
);
1388 pop_deferring_access_checks ();
1389 --cp_unevaluated_operand
;
1390 --c_inhibit_evaluation_warnings
;
1393 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1394 return true; else return false. */
1397 maybe_explain_implicit_delete (tree decl
)
1399 /* If decl is a clone, get the primary variant. */
1400 decl
= DECL_ORIGIN (decl
);
1401 gcc_assert (DECL_DELETED_FN (decl
));
1402 if (DECL_DEFAULTED_FN (decl
))
1404 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1405 static struct pointer_set_t
*explained
;
1407 special_function_kind sfk
;
1413 explained
= pointer_set_create ();
1414 if (pointer_set_insert (explained
, decl
))
1417 sfk
= special_function_p (decl
);
1418 ctype
= DECL_CONTEXT (decl
);
1419 loc
= input_location
;
1420 input_location
= DECL_SOURCE_LOCATION (decl
);
1423 if (LAMBDA_TYPE_P (ctype
))
1426 if (sfk
== sfk_constructor
)
1427 inform (DECL_SOURCE_LOCATION (decl
),
1428 "a lambda closure type has a deleted default constructor");
1429 else if (sfk
== sfk_copy_assignment
)
1430 inform (DECL_SOURCE_LOCATION (decl
),
1431 "a lambda closure type has a deleted copy assignment operator");
1435 else if (DECL_ARTIFICIAL (decl
)
1436 && (sfk
== sfk_copy_assignment
1437 || sfk
== sfk_copy_constructor
)
1438 && (type_has_user_declared_move_constructor (ctype
)
1439 || type_has_user_declared_move_assign (ctype
)))
1441 inform (0, "%q+#D is implicitly declared as deleted because %qT "
1442 "declares a move constructor or move assignment operator",
1448 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
1449 tree parm_type
= TREE_VALUE (parms
);
1450 bool const_p
= CP_TYPE_CONST_P (non_reference (parm_type
));
1451 tree scope
= push_scope (ctype
);
1452 inform (0, "%q+#D is implicitly deleted because the default "
1453 "definition would be ill-formed:", decl
);
1455 synthesized_method_walk (ctype
, sfk
, const_p
,
1456 NULL
, NULL
, NULL
, NULL
, true,
1457 DECL_INHERITED_CTOR_BASE (decl
), parms
);
1460 input_location
= loc
;
1466 /* DECL is a defaulted function which was declared constexpr. Explain why
1467 it can't be constexpr. */
1470 explain_implicit_non_constexpr (tree decl
)
1472 tree parm_type
= TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl
));
1473 bool const_p
= CP_TYPE_CONST_P (non_reference (parm_type
));
1475 synthesized_method_walk (DECL_CLASS_CONTEXT (decl
),
1476 special_function_p (decl
), const_p
,
1477 NULL
, NULL
, NULL
, &dummy
, true,
1478 NULL_TREE
, NULL_TREE
);
1481 /* DECL is an instantiation of an inheriting constructor template. Deduce
1482 the correct exception-specification and deletedness for this particular
1486 deduce_inheriting_ctor (tree decl
)
1488 gcc_assert (DECL_INHERITED_CTOR_BASE (decl
));
1490 bool trivial
, constexpr_
, deleted
;
1491 synthesized_method_walk (DECL_CONTEXT (decl
), sfk_inheriting_constructor
,
1492 false, &spec
, &trivial
, &deleted
, &constexpr_
,
1494 DECL_INHERITED_CTOR_BASE (decl
),
1495 FUNCTION_FIRST_USER_PARMTYPE (decl
));
1496 DECL_DELETED_FN (decl
) = deleted
;
1497 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
), spec
);
1500 /* Implicitly declare the special function indicated by KIND, as a
1501 member of TYPE. For copy constructors and assignment operators,
1502 CONST_P indicates whether these functions should take a const
1503 reference argument or a non-const reference. Returns the
1504 FUNCTION_DECL for the implicitly declared function. */
1507 implicitly_declare_fn (special_function_kind kind
, tree type
,
1508 bool const_p
, tree inherited_ctor
,
1509 tree inherited_parms
)
1512 tree parameter_types
= void_list_node
;
1515 tree raises
= empty_except_spec
;
1516 tree rhs_parm_type
= NULL_TREE
;
1519 HOST_WIDE_INT saved_processing_template_decl
;
1524 /* Because we create declarations for implicitly declared functions
1525 lazily, we may be creating the declaration for a member of TYPE
1526 while in some completely different context. However, TYPE will
1527 never be a dependent class (because we never want to do lookups
1528 for implicitly defined functions in a dependent class).
1529 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1530 because we only create clones for constructors and destructors
1531 when not in a template. */
1532 gcc_assert (!dependent_type_p (type
));
1533 saved_processing_template_decl
= processing_template_decl
;
1534 processing_template_decl
= 0;
1536 type
= TYPE_MAIN_VARIANT (type
);
1538 if (targetm
.cxx
.cdtor_returns_this () && !TYPE_FOR_JAVA (type
))
1540 if (kind
== sfk_destructor
)
1541 /* See comment in check_special_function_return_type. */
1542 return_type
= build_pointer_type (void_type_node
);
1544 return_type
= build_pointer_type (type
);
1547 return_type
= void_type_node
;
1551 case sfk_destructor
:
1553 name
= constructor_name (type
);
1556 case sfk_constructor
:
1557 /* Default constructor. */
1558 name
= constructor_name (type
);
1561 case sfk_copy_constructor
:
1562 case sfk_copy_assignment
:
1563 case sfk_move_constructor
:
1564 case sfk_move_assignment
:
1565 case sfk_inheriting_constructor
:
1568 if (kind
== sfk_copy_assignment
1569 || kind
== sfk_move_assignment
)
1571 return_type
= build_reference_type (type
);
1572 name
= ansi_assopname (NOP_EXPR
);
1575 name
= constructor_name (type
);
1577 if (kind
== sfk_inheriting_constructor
)
1578 parameter_types
= inherited_parms
;
1582 rhs_parm_type
= cp_build_qualified_type (type
, TYPE_QUAL_CONST
);
1584 rhs_parm_type
= type
;
1585 move_p
= (kind
== sfk_move_assignment
1586 || kind
== sfk_move_constructor
);
1587 rhs_parm_type
= cp_build_reference_type (rhs_parm_type
, move_p
);
1589 parameter_types
= tree_cons (NULL_TREE
, rhs_parm_type
, parameter_types
);
1597 tree inherited_base
= (inherited_ctor
1598 ? DECL_CONTEXT (inherited_ctor
)
1600 if (inherited_ctor
&& TREE_CODE (inherited_ctor
) == TEMPLATE_DECL
)
1602 /* For an inheriting constructor template, just copy these flags from
1603 the inherited constructor template for now. */
1604 raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor
));
1606 deleted_p
= DECL_DELETED_FN (DECL_TEMPLATE_RESULT (inherited_ctor
));
1608 = DECL_DECLARED_CONSTEXPR_P (DECL_TEMPLATE_RESULT (inherited_ctor
));
1611 synthesized_method_walk (type
, kind
, const_p
, &raises
, &trivial_p
,
1612 &deleted_p
, &constexpr_p
, false,
1613 inherited_base
, inherited_parms
);
1614 /* Don't bother marking a deleted constructor as constexpr. */
1616 constexpr_p
= false;
1617 /* A trivial copy/move constructor is also a constexpr constructor. */
1618 else if (trivial_p
&& cxx_dialect
>= cxx0x
1619 && (kind
== sfk_copy_constructor
1620 || kind
== sfk_move_constructor
))
1621 gcc_assert (constexpr_p
);
1623 if (!trivial_p
&& type_has_trivial_fn (type
, kind
))
1624 type_set_nontrivial_flag (type
, kind
);
1626 /* Create the function. */
1627 fn_type
= build_method_type_directly (type
, return_type
, parameter_types
);
1629 fn_type
= build_exception_variant (fn_type
, raises
);
1630 fn
= build_lang_decl (FUNCTION_DECL
, name
, fn_type
);
1631 if (kind
!= sfk_inheriting_constructor
)
1632 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (TYPE_NAME (type
));
1633 if (kind
== sfk_constructor
|| kind
== sfk_copy_constructor
1634 || kind
== sfk_move_constructor
|| kind
== sfk_inheriting_constructor
)
1635 DECL_CONSTRUCTOR_P (fn
) = 1;
1636 else if (kind
== sfk_destructor
)
1637 DECL_DESTRUCTOR_P (fn
) = 1;
1640 DECL_ASSIGNMENT_OPERATOR_P (fn
) = 1;
1641 SET_OVERLOADED_OPERATOR_CODE (fn
, NOP_EXPR
);
1644 /* If pointers to member functions use the least significant bit to
1645 indicate whether a function is virtual, ensure a pointer
1646 to this function will have that bit clear. */
1647 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_pfn
1648 && DECL_ALIGN (fn
) < 2 * BITS_PER_UNIT
)
1649 DECL_ALIGN (fn
) = 2 * BITS_PER_UNIT
;
1651 /* Create the explicit arguments. */
1654 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1655 want its type to be included in the mangled function
1657 tree decl
= cp_build_parm_decl (NULL_TREE
, rhs_parm_type
);
1658 TREE_READONLY (decl
) = 1;
1659 retrofit_lang_decl (decl
);
1660 DECL_PARM_INDEX (decl
) = DECL_PARM_LEVEL (decl
) = 1;
1661 DECL_ARGUMENTS (fn
) = decl
;
1663 else if (kind
== sfk_inheriting_constructor
)
1665 tree
*p
= &DECL_ARGUMENTS (fn
);
1667 for (tree parm
= inherited_parms
; parm
!= void_list_node
;
1668 parm
= TREE_CHAIN (parm
))
1670 *p
= cp_build_parm_decl (NULL_TREE
, TREE_VALUE (parm
));
1671 retrofit_lang_decl (*p
);
1672 DECL_PARM_LEVEL (*p
) = 1;
1673 DECL_PARM_INDEX (*p
) = index
++;
1674 DECL_CONTEXT (*p
) = fn
;
1675 p
= &DECL_CHAIN (*p
);
1677 SET_DECL_INHERITED_CTOR_BASE (fn
, inherited_base
);
1678 DECL_NONCONVERTING_P (fn
) = DECL_NONCONVERTING_P (inherited_ctor
);
1679 /* A constructor so declared has the same access as the corresponding
1680 constructor in X. */
1681 TREE_PRIVATE (fn
) = TREE_PRIVATE (inherited_ctor
);
1682 TREE_PROTECTED (fn
) = TREE_PROTECTED (inherited_ctor
);
1683 /* Copy constexpr from the inherited constructor even if the
1684 inheriting constructor doesn't satisfy the requirements. */
1686 = DECL_DECLARED_CONSTEXPR_P (STRIP_TEMPLATE (inherited_ctor
));
1688 /* Add the "this" parameter. */
1689 this_parm
= build_this_parm (fn_type
, TYPE_UNQUALIFIED
);
1690 DECL_CHAIN (this_parm
) = DECL_ARGUMENTS (fn
);
1691 DECL_ARGUMENTS (fn
) = this_parm
;
1693 grokclassfn (type
, fn
, kind
== sfk_destructor
? DTOR_FLAG
: NO_SPECIAL
);
1694 set_linkage_according_to_type (type
, fn
);
1695 rest_of_decl_compilation (fn
, toplevel_bindings_p (), at_eof
);
1696 DECL_IN_AGGR_P (fn
) = 1;
1697 DECL_ARTIFICIAL (fn
) = 1;
1698 DECL_DEFAULTED_FN (fn
) = 1;
1699 if (cxx_dialect
>= cxx0x
)
1701 DECL_DELETED_FN (fn
) = deleted_p
;
1702 DECL_DECLARED_CONSTEXPR_P (fn
) = constexpr_p
;
1704 DECL_EXTERNAL (fn
) = true;
1705 DECL_NOT_REALLY_EXTERN (fn
) = 1;
1706 DECL_DECLARED_INLINE_P (fn
) = 1;
1707 gcc_assert (!TREE_USED (fn
));
1709 /* Restore PROCESSING_TEMPLATE_DECL. */
1710 processing_template_decl
= saved_processing_template_decl
;
1712 if (inherited_ctor
&& TREE_CODE (inherited_ctor
) == TEMPLATE_DECL
)
1713 fn
= add_inherited_template_parms (fn
, inherited_ctor
);
1715 /* Warn about calling a non-trivial move assignment in a virtual base. */
1716 if (kind
== sfk_move_assignment
&& !deleted_p
&& !trivial_p
1717 && CLASSTYPE_VBASECLASSES (type
))
1719 location_t loc
= input_location
;
1720 input_location
= DECL_SOURCE_LOCATION (fn
);
1721 synthesized_method_walk (type
, kind
, const_p
,
1722 NULL
, NULL
, NULL
, NULL
, true,
1723 NULL_TREE
, NULL_TREE
);
1724 input_location
= loc
;
1730 /* Gives any errors about defaulted functions which need to be deferred
1731 until the containing class is complete. */
1734 defaulted_late_check (tree fn
)
1736 /* Complain about invalid signature for defaulted fn. */
1737 tree ctx
= DECL_CONTEXT (fn
);
1738 special_function_kind kind
= special_function_p (fn
);
1739 bool fn_const_p
= (copy_fn_p (fn
) == 2);
1740 tree implicit_fn
= implicitly_declare_fn (kind
, ctx
, fn_const_p
,
1743 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn
)),
1744 TREE_TYPE (TREE_TYPE (implicit_fn
)))
1745 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1746 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn
))))
1748 error ("defaulted declaration %q+D", fn
);
1749 error_at (DECL_SOURCE_LOCATION (fn
),
1750 "does not match expected signature %qD", implicit_fn
);
1753 /* 8.4.2/2: If it is explicitly defaulted on its first declaration, it is
1754 implicitly considered to have the same exception-specification as if
1755 it had been implicitly declared. */
1756 if (DECL_DEFAULTED_IN_CLASS_P (fn
))
1758 tree eh_spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn
));
1759 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)))
1761 maybe_instantiate_noexcept (fn
);
1762 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)),
1763 eh_spec
, ce_normal
))
1764 error ("function %q+D defaulted on its first declaration "
1765 "with an exception-specification that differs from "
1766 "the implicit declaration %q#D", fn
, implicit_fn
);
1768 TREE_TYPE (fn
) = build_exception_variant (TREE_TYPE (fn
), eh_spec
);
1769 if (DECL_DECLARED_CONSTEXPR_P (implicit_fn
))
1771 /* Hmm...should we do this for out-of-class too? Should it be OK to
1772 add constexpr later like inline, rather than requiring
1773 declarations to match? */
1774 DECL_DECLARED_CONSTEXPR_P (fn
) = true;
1775 if (kind
== sfk_constructor
)
1776 TYPE_HAS_CONSTEXPR_CTOR (ctx
) = true;
1780 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn
)
1781 && DECL_DECLARED_CONSTEXPR_P (fn
))
1783 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
1785 error ("explicitly defaulted function %q+D cannot be declared "
1786 "as constexpr because the implicit declaration is not "
1788 explain_implicit_non_constexpr (fn
);
1790 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
1793 if (DECL_DELETED_FN (implicit_fn
))
1794 DECL_DELETED_FN (fn
) = 1;
1797 /* Returns true iff FN can be explicitly defaulted, and gives any
1798 errors if defaulting FN is ill-formed. */
1801 defaultable_fn_check (tree fn
)
1803 special_function_kind kind
= sfk_none
;
1805 if (template_parm_scope_p ())
1807 error ("a template cannot be defaulted");
1811 if (DECL_CONSTRUCTOR_P (fn
))
1813 if (FUNCTION_FIRST_USER_PARMTYPE (fn
) == void_list_node
)
1814 kind
= sfk_constructor
;
1815 else if (copy_fn_p (fn
) > 0
1816 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn
))
1818 kind
= sfk_copy_constructor
;
1819 else if (move_fn_p (fn
))
1820 kind
= sfk_move_constructor
;
1822 else if (DECL_DESTRUCTOR_P (fn
))
1823 kind
= sfk_destructor
;
1824 else if (DECL_ASSIGNMENT_OPERATOR_P (fn
)
1825 && DECL_OVERLOADED_OPERATOR_P (fn
) == NOP_EXPR
)
1828 kind
= sfk_copy_assignment
;
1829 else if (move_fn_p (fn
))
1830 kind
= sfk_move_assignment
;
1833 if (kind
== sfk_none
)
1835 error ("%qD cannot be defaulted", fn
);
1840 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
1841 for (; t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
1842 if (TREE_PURPOSE (t
))
1844 error ("defaulted function %q+D with default argument", fn
);
1847 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn
)))
1848 /* Defer checking. */;
1849 else if (!processing_template_decl
)
1850 defaulted_late_check (fn
);
1856 /* Add an implicit declaration to TYPE for the kind of function
1857 indicated by SFK. Return the FUNCTION_DECL for the new implicit
1861 lazily_declare_fn (special_function_kind sfk
, tree type
)
1864 /* Whether or not the argument has a const reference type. */
1865 bool const_p
= false;
1869 case sfk_constructor
:
1870 CLASSTYPE_LAZY_DEFAULT_CTOR (type
) = 0;
1872 case sfk_copy_constructor
:
1873 const_p
= TYPE_HAS_CONST_COPY_CTOR (type
);
1874 CLASSTYPE_LAZY_COPY_CTOR (type
) = 0;
1876 case sfk_move_constructor
:
1877 CLASSTYPE_LAZY_MOVE_CTOR (type
) = 0;
1879 case sfk_copy_assignment
:
1880 const_p
= TYPE_HAS_CONST_COPY_ASSIGN (type
);
1881 CLASSTYPE_LAZY_COPY_ASSIGN (type
) = 0;
1883 case sfk_move_assignment
:
1884 CLASSTYPE_LAZY_MOVE_ASSIGN (type
) = 0;
1886 case sfk_destructor
:
1887 CLASSTYPE_LAZY_DESTRUCTOR (type
) = 0;
1893 /* Declare the function. */
1894 fn
= implicitly_declare_fn (sfk
, type
, const_p
, NULL
, NULL
);
1896 /* [class.copy]/8 If the class definition declares a move constructor or
1897 move assignment operator, the implicitly declared copy constructor is
1898 defined as deleted.... */
1899 if ((sfk
== sfk_copy_assignment
1900 || sfk
== sfk_copy_constructor
)
1901 && (type_has_user_declared_move_constructor (type
)
1902 || type_has_user_declared_move_assign (type
)))
1903 DECL_DELETED_FN (fn
) = true;
1905 /* A destructor may be virtual. */
1906 if (sfk
== sfk_destructor
1907 || sfk
== sfk_move_assignment
1908 || sfk
== sfk_copy_assignment
)
1909 check_for_override (fn
, type
);
1910 /* Add it to CLASSTYPE_METHOD_VEC. */
1911 add_method (type
, fn
, NULL_TREE
);
1912 /* Add it to TYPE_METHODS. */
1913 if (sfk
== sfk_destructor
1914 && DECL_VIRTUAL_P (fn
)
1915 && abi_version_at_least (2))
1916 /* The ABI requires that a virtual destructor go at the end of the
1918 TYPE_METHODS (type
) = chainon (TYPE_METHODS (type
), fn
);
1921 /* G++ 3.2 put the implicit destructor at the *beginning* of the
1922 TYPE_METHODS list, which cause the destructor to be emitted
1923 in an incorrect location in the vtable. */
1924 if (warn_abi
&& sfk
== sfk_destructor
&& DECL_VIRTUAL_P (fn
))
1925 warning (OPT_Wabi
, "vtable layout for class %qT may not be ABI-compliant"
1926 "and may change in a future version of GCC due to "
1927 "implicit virtual destructor",
1929 DECL_CHAIN (fn
) = TYPE_METHODS (type
);
1930 TYPE_METHODS (type
) = fn
;
1932 maybe_add_class_template_decl_list (type
, fn
, /*friend_p=*/0);
1933 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
)
1934 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
))
1935 /* Create appropriate clones. */
1936 clone_function_decl (fn
, /*update_method_vec=*/true);
1941 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
1942 as there are artificial parms in FN. */
1945 skip_artificial_parms_for (const_tree fn
, tree list
)
1947 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
1948 list
= TREE_CHAIN (list
);
1952 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
1953 list
= TREE_CHAIN (list
);
1954 if (DECL_HAS_VTT_PARM_P (fn
))
1955 list
= TREE_CHAIN (list
);
1959 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
1960 artificial parms in FN. */
1963 num_artificial_parms_for (const_tree fn
)
1967 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
1972 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
1974 if (DECL_HAS_VTT_PARM_P (fn
))
1980 #include "gt-cp-method.h"