* method.c (synthesized_method_walk): Avoid changing
[official-gcc.git] / gcc / cp / method.c
blob7ea2de9ac8ea2980543a52b6113242727b98ced0
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)
13 any later version.
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. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "flags.h"
33 #include "toplev.h"
34 #include "tm_p.h"
35 #include "target.h"
36 #include "common/common-target.h"
37 #include "tree-pass.h"
38 #include "diagnostic.h"
39 #include "cgraph.h"
40 #include "gimple.h"
42 /* Various flags to control the mangling process. */
44 enum mangling_flags
46 /* No flags. */
47 mf_none = 0,
48 /* The thing we are presently mangling is part of a template type,
49 rather than a fully instantiated type. Therefore, we may see
50 complex expressions where we would normally expect to see a
51 simple integer constant. */
52 mf_maybe_uninstantiated = 1,
53 /* When mangling a numeric value, use the form `_XX_' (instead of
54 just `XX') if the value has more than one digit. */
55 mf_use_underscores_around_value = 2
58 typedef enum mangling_flags mangling_flags;
60 static void do_build_copy_assign (tree);
61 static void do_build_copy_constructor (tree);
62 static tree make_alias_for_thunk (tree);
64 /* Called once to initialize method.c. */
66 void
67 init_method (void)
69 init_mangle ();
72 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
73 indicates whether it is a this or result adjusting thunk.
74 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
75 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
76 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
77 adjusting thunks, we scale it to a byte offset. For covariant
78 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
79 the returned thunk with finish_thunk. */
81 tree
82 make_thunk (tree function, bool this_adjusting,
83 tree fixed_offset, tree virtual_offset)
85 HOST_WIDE_INT d;
86 tree thunk;
88 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
89 /* We can have this thunks to covariant thunks, but not vice versa. */
90 gcc_assert (!DECL_THIS_THUNK_P (function));
91 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
93 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
94 if (this_adjusting && virtual_offset)
95 virtual_offset
96 = size_binop (MULT_EXPR,
97 virtual_offset,
98 convert (ssizetype,
99 TYPE_SIZE_UNIT (vtable_entry_type)));
101 d = tree_low_cst (fixed_offset, 0);
103 /* See if we already have the thunk in question. For this_adjusting
104 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
105 will be a BINFO. */
106 for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
107 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
108 && THUNK_FIXED_OFFSET (thunk) == d
109 && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
110 && (!virtual_offset
111 || (this_adjusting
112 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
113 virtual_offset)
114 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
115 return thunk;
117 /* All thunks must be created before FUNCTION is actually emitted;
118 the ABI requires that all thunks be emitted together with the
119 function to which they transfer control. */
120 gcc_assert (!TREE_ASM_WRITTEN (function));
121 /* Likewise, we can only be adding thunks to a function declared in
122 the class currently being laid out. */
123 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
124 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
126 thunk = build_decl (DECL_SOURCE_LOCATION (function),
127 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
128 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
129 cxx_dup_lang_specific_decl (thunk);
130 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 DECL_THUNKS (function) = thunk;
163 return thunk;
166 /* Finish THUNK, a thunk decl. */
168 void
169 finish_thunk (tree thunk)
171 tree function, name;
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))
189 tree cov_probe;
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);
198 break;
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. */
210 tree
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));
241 else
242 TREE_STATIC (alias) = 1;
243 TREE_ADDRESSABLE (alias) = 1;
244 TREE_USED (alias) = 1;
245 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
246 return alias;
249 static tree
250 make_alias_for_thunk (tree function)
252 tree alias;
253 char buf[256];
255 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
256 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);
270 return alias;
273 /* Emit the definition of a C++ multiple inheritance or covariant
274 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
275 immediately. */
277 void
278 use_thunk (tree thunk_fndecl, bool emit_p)
280 tree a, t, function, alias;
281 tree virtual_offset;
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
290 aliased thunk. */
291 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
293 if (TREE_ASM_WRITTEN (thunk_fndecl))
294 return;
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. */
300 return;
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);
313 if (!emit_p)
314 return;
316 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
317 alias = make_alias_for_thunk (function);
318 else
319 alias = function;
321 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
322 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
324 if (virtual_offset)
326 if (!this_adjusting)
327 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
328 virtual_value = tree_low_cst (virtual_offset, /*pos=*/0);
329 gcc_assert (virtual_value);
331 else
332 virtual_value = 0;
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
339 rewrite. */
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;
351 return;
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. */
371 t = NULL_TREE;
372 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
374 tree x = copy_node (a);
375 DECL_CHAIN (x) = t;
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;
380 t = x;
382 a = nreverse (t);
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);
394 if (!this_adjusting
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 %<...%>",
405 function);
408 pop_from_top_level ();
411 /* Code for synthesizing methods which have default semantics defined. */
413 /* True iff CTYPE has a trivial SFK. */
415 static bool
416 type_has_trivial_fn (tree ctype, special_function_kind sfk)
418 switch (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);
430 case sfk_destructor:
431 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
432 default:
433 gcc_unreachable ();
437 /* Note that CTYPE has a non-trivial SFK even though we previously thought
438 it was trivial. */
440 static void
441 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
443 switch (sfk)
445 case sfk_constructor:
446 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
447 return;
448 case sfk_copy_constructor:
449 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
450 return;
451 case sfk_move_constructor:
452 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
453 return;
454 case sfk_copy_assignment:
455 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
456 return;
457 case sfk_move_assignment:
458 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
459 return;
460 case sfk_destructor:
461 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
462 return;
463 default:
464 gcc_unreachable ();
468 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
470 bool
471 trivial_fn_p (tree fn)
473 if (!DECL_DEFAULTED_FN (fn))
474 return false;
476 /* If fn is a clone, get the primary variant. */
477 fn = DECL_ORIGIN (fn);
478 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
481 /* Generate code for default X(X&) or X(X&&) constructor. */
483 static void
484 do_build_copy_constructor (tree fndecl)
486 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
487 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
488 bool trivial = trivial_fn_p (fndecl);
490 parm = convert_from_reference (parm);
492 if (trivial
493 && is_empty_class (current_class_type))
494 /* Don't copy the padding byte; it might not have been allocated
495 if *this is a base subobject. */;
496 else if (trivial)
498 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
499 finish_expr_stmt (t);
501 else
503 tree fields = TYPE_FIELDS (current_class_type);
504 tree member_init_list = NULL_TREE;
505 int cvquals = cp_type_quals (TREE_TYPE (parm));
506 int i;
507 tree binfo, base_binfo;
508 tree init;
509 VEC(tree,gc) *vbases;
511 /* Initialize all the base-classes with the parameter converted
512 to their type so that we get their copy constructor and not
513 another constructor that takes current_class_type. We must
514 deal with the binfo's directly as a direct base might be
515 inaccessible due to ambiguity. */
516 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
517 VEC_iterate (tree, vbases, i, binfo); i++)
519 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
520 tf_warning_or_error);
521 if (move_p)
522 init = move (init);
523 member_init_list
524 = tree_cons (binfo,
525 build_tree_list (NULL_TREE, init),
526 member_init_list);
529 for (binfo = TYPE_BINFO (current_class_type), i = 0;
530 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
532 if (BINFO_VIRTUAL_P (base_binfo))
533 continue;
535 init = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
536 tf_warning_or_error);
537 if (move_p)
538 init = move (init);
539 member_init_list
540 = tree_cons (base_binfo,
541 build_tree_list (NULL_TREE, init),
542 member_init_list);
545 for (; fields; fields = DECL_CHAIN (fields))
547 tree field = fields;
548 tree expr_type;
550 if (TREE_CODE (field) != FIELD_DECL)
551 continue;
553 expr_type = TREE_TYPE (field);
554 if (DECL_NAME (field))
556 if (VFIELD_NAME_P (DECL_NAME (field)))
557 continue;
559 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
560 /* Just use the field; anonymous types can't have
561 nontrivial copy ctors or assignment ops or this
562 function would be deleted. */;
563 else
564 continue;
566 /* Compute the type of "init->field". If the copy-constructor
567 parameter is, for example, "const S&", and the type of
568 the field is "T", then the type will usually be "const
569 T". (There are no cv-qualified variants of reference
570 types.) */
571 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
573 int quals = cvquals;
575 if (DECL_MUTABLE_P (field))
576 quals &= ~TYPE_QUAL_CONST;
577 quals |= cp_type_quals (expr_type);
578 expr_type = cp_build_qualified_type (expr_type, quals);
581 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
582 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE)
583 init = move (init);
584 init = build_tree_list (NULL_TREE, init);
586 member_init_list = tree_cons (field, init, member_init_list);
588 finish_mem_initializers (member_init_list);
592 static void
593 do_build_copy_assign (tree fndecl)
595 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
596 tree compound_stmt;
597 bool move_p = move_fn_p (fndecl);
598 bool trivial = trivial_fn_p (fndecl);
599 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
601 compound_stmt = begin_compound_stmt (0);
602 parm = convert_from_reference (parm);
604 if (trivial
605 && is_empty_class (current_class_type))
606 /* Don't copy the padding byte; it might not have been allocated
607 if *this is a base subobject. */;
608 else if (trivial)
610 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
611 finish_expr_stmt (t);
613 else
615 tree fields;
616 int cvquals = cp_type_quals (TREE_TYPE (parm));
617 int i;
618 tree binfo, base_binfo;
620 /* Assign to each of the direct base classes. */
621 for (binfo = TYPE_BINFO (current_class_type), i = 0;
622 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
624 tree converted_parm;
625 VEC(tree,gc) *parmvec;
627 /* We must convert PARM directly to the base class
628 explicitly since the base class may be ambiguous. */
629 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
630 tf_warning_or_error);
631 if (move_p)
632 converted_parm = move (converted_parm);
633 /* Call the base class assignment operator. */
634 parmvec = make_tree_vector_single (converted_parm);
635 finish_expr_stmt
636 (build_special_member_call (current_class_ref,
637 ansi_assopname (NOP_EXPR),
638 &parmvec,
639 base_binfo,
640 flags,
641 tf_warning_or_error));
642 release_tree_vector (parmvec);
645 /* Assign to each of the non-static data members. */
646 for (fields = TYPE_FIELDS (current_class_type);
647 fields;
648 fields = DECL_CHAIN (fields))
650 tree comp = current_class_ref;
651 tree init = parm;
652 tree field = fields;
653 tree expr_type;
654 int quals;
656 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
657 continue;
659 expr_type = TREE_TYPE (field);
661 if (CP_TYPE_CONST_P (expr_type))
663 error ("non-static const member %q#D, can%'t use default "
664 "assignment operator", field);
665 continue;
667 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
669 error ("non-static reference member %q#D, can%'t use "
670 "default assignment operator", field);
671 continue;
674 if (DECL_NAME (field))
676 if (VFIELD_NAME_P (DECL_NAME (field)))
677 continue;
679 else if (ANON_AGGR_TYPE_P (expr_type)
680 && TYPE_FIELDS (expr_type) != NULL_TREE)
681 /* Just use the field; anonymous types can't have
682 nontrivial copy ctors or assignment ops or this
683 function would be deleted. */;
684 else
685 continue;
687 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
689 /* Compute the type of init->field */
690 quals = cvquals;
691 if (DECL_MUTABLE_P (field))
692 quals &= ~TYPE_QUAL_CONST;
693 expr_type = cp_build_qualified_type (expr_type, quals);
695 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
696 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE)
697 init = move (init);
699 if (DECL_NAME (field))
700 init = cp_build_modify_expr (comp, NOP_EXPR, init,
701 tf_warning_or_error);
702 else
703 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
704 finish_expr_stmt (init);
707 finish_return_stmt (current_class_ref);
708 finish_compound_stmt (compound_stmt);
711 /* Synthesize FNDECL, a non-static member function. */
713 void
714 synthesize_method (tree fndecl)
716 bool nested = (current_function_decl != NULL_TREE);
717 tree context = decl_function_context (fndecl);
718 bool need_body = true;
719 tree stmt;
720 location_t save_input_location = input_location;
721 int error_count = errorcount;
722 int warning_count = warningcount;
724 /* Reset the source location, we might have been previously
725 deferred, and thus have saved where we were first needed. */
726 DECL_SOURCE_LOCATION (fndecl)
727 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
729 /* If we've been asked to synthesize a clone, just synthesize the
730 cloned function instead. Doing so will automatically fill in the
731 body for the clone. */
732 if (DECL_CLONED_FUNCTION_P (fndecl))
733 fndecl = DECL_CLONED_FUNCTION (fndecl);
735 /* We may be in the middle of deferred access check. Disable
736 it now. */
737 push_deferring_access_checks (dk_no_deferred);
739 if (! context)
740 push_to_top_level ();
741 else if (nested)
742 push_function_context ();
744 input_location = DECL_SOURCE_LOCATION (fndecl);
746 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
747 stmt = begin_function_body ();
749 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
751 do_build_copy_assign (fndecl);
752 need_body = false;
754 else if (DECL_CONSTRUCTOR_P (fndecl))
756 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
757 if (arg_chain != void_list_node)
758 do_build_copy_constructor (fndecl);
759 else
760 finish_mem_initializers (NULL_TREE);
763 /* If we haven't yet generated the body of the function, just
764 generate an empty compound statement. */
765 if (need_body)
767 tree compound_stmt;
768 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
769 finish_compound_stmt (compound_stmt);
772 finish_function_body (stmt);
773 expand_or_defer_fn (finish_function (0));
775 input_location = save_input_location;
777 if (! context)
778 pop_from_top_level ();
779 else if (nested)
780 pop_function_context ();
782 pop_deferring_access_checks ();
784 if (error_count != errorcount || warning_count != warningcount)
785 inform (input_location, "synthesized method %qD first required here ",
786 fndecl);
789 /* Build a reference to type TYPE with cv-quals QUALS, which is an
790 rvalue if RVALUE is true. */
792 static tree
793 build_stub_type (tree type, int quals, bool rvalue)
795 tree argtype = cp_build_qualified_type (type, quals);
796 return cp_build_reference_type (argtype, rvalue);
799 /* Build a dummy glvalue from dereferencing a dummy reference of type
800 REFTYPE. */
802 static tree
803 build_stub_object (tree reftype)
805 tree stub = build1 (NOP_EXPR, reftype, integer_one_node);
806 return convert_from_reference (stub);
809 /* Determine which function will be called when looking up NAME in TYPE,
810 called with a single ARGTYPE argument, or no argument if ARGTYPE is
811 null. FLAGS and COMPLAIN are as for build_new_method_call.
813 Returns a FUNCTION_DECL if all is well.
814 Returns NULL_TREE if overload resolution failed.
815 Returns error_mark_node if the chosen function cannot be called. */
817 static tree
818 locate_fn_flags (tree type, tree name, tree argtype, int flags,
819 tsubst_flags_t complain)
821 tree ob, fn, fns, binfo, rval;
822 VEC(tree,gc) *args;
824 if (TYPE_P (type))
825 binfo = TYPE_BINFO (type);
826 else
828 binfo = type;
829 type = BINFO_TYPE (binfo);
832 ob = build_stub_object (cp_build_reference_type (type, false));
833 args = make_tree_vector ();
834 if (argtype)
836 tree arg = build_stub_object (argtype);
837 VEC_quick_push (tree, args, arg);
840 fns = lookup_fnfields (binfo, name, 0);
841 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
843 release_tree_vector (args);
844 if (fn && rval == error_mark_node)
845 return rval;
846 else
847 return fn;
850 /* Locate the dtor of TYPE. */
852 tree
853 get_dtor (tree type, tsubst_flags_t complain)
855 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
856 LOOKUP_NORMAL, complain);
857 if (fn == error_mark_node)
858 return NULL_TREE;
859 return fn;
862 /* Locate the default ctor of TYPE. */
864 tree
865 locate_ctor (tree type)
867 tree fn;
869 push_deferring_access_checks (dk_no_check);
870 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
871 LOOKUP_SPECULATIVE, tf_none);
872 pop_deferring_access_checks ();
873 if (fn == error_mark_node)
874 return NULL_TREE;
875 return fn;
878 /* Likewise, but give any appropriate errors. */
880 tree
881 get_default_ctor (tree type)
883 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
884 LOOKUP_NORMAL, tf_warning_or_error);
885 if (fn == error_mark_node)
886 return NULL_TREE;
887 return fn;
890 /* Locate the copy ctor of TYPE. */
892 tree
893 get_copy_ctor (tree type, tsubst_flags_t complain)
895 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
896 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
897 tree argtype = build_stub_type (type, quals, false);
898 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
899 LOOKUP_NORMAL, complain);
900 if (fn == error_mark_node)
901 return NULL_TREE;
902 return fn;
905 /* Locate the copy assignment operator of TYPE. */
907 tree
908 get_copy_assign (tree type)
910 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
911 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
912 tree argtype = build_stub_type (type, quals, false);
913 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
914 LOOKUP_NORMAL, tf_warning_or_error);
915 if (fn == error_mark_node)
916 return NULL_TREE;
917 return fn;
920 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
921 DELETED_P or give an error message MSG with argument ARG. */
923 static void
924 process_subob_fn (tree fn, bool move_p, tree *spec_p, bool *trivial_p,
925 bool *deleted_p, bool *constexpr_p,
926 const char *msg, tree arg)
928 if (!fn || fn == error_mark_node)
929 goto bad;
931 if (spec_p)
933 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
934 *spec_p = merge_exception_specifiers (*spec_p, raises, fn);
937 if (!trivial_fn_p (fn))
939 if (trivial_p)
940 *trivial_p = false;
941 if (TREE_CODE (arg) == FIELD_DECL
942 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
944 if (deleted_p)
945 *deleted_p = true;
946 if (msg)
947 error ("union member %q+D with non-trivial %qD", arg, fn);
951 if (move_p && !move_fn_p (fn) && !trivial_fn_p (fn))
953 if (msg)
954 error (msg, arg);
955 goto bad;
958 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
960 *constexpr_p = false;
961 if (msg)
963 inform (0, "defaulted constructor calls non-constexpr "
964 "%q+D", fn);
965 explain_invalid_constexpr_fn (fn);
969 return;
971 bad:
972 if (deleted_p)
973 *deleted_p = true;
976 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
977 aggregates. */
979 static void
980 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
981 int quals, bool copy_arg_p, bool move_p,
982 bool assign_p, tree *spec_p, bool *trivial_p,
983 bool *deleted_p, bool *constexpr_p, const char *msg,
984 int flags, tsubst_flags_t complain)
986 tree field;
987 for (field = fields; field; field = DECL_CHAIN (field))
989 tree mem_type, argtype, rval;
991 if (TREE_CODE (field) != FIELD_DECL
992 || DECL_ARTIFICIAL (field))
993 continue;
995 mem_type = strip_array_types (TREE_TYPE (field));
996 if (assign_p)
998 bool bad = true;
999 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1001 if (msg)
1002 error ("non-static const member %q#D, can%'t use default "
1003 "assignment operator", field);
1005 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1007 if (msg)
1008 error ("non-static reference member %q#D, can%'t use "
1009 "default assignment operator", field);
1011 else
1012 bad = false;
1014 if (bad && deleted_p)
1015 *deleted_p = true;
1017 else if (sfk == sfk_constructor)
1019 bool bad;
1021 if (DECL_INITIAL (field))
1023 if (msg && DECL_INITIAL (field) == error_mark_node)
1024 inform (0, "initializer for %q+#D is invalid", field);
1025 if (trivial_p)
1026 *trivial_p = false;
1027 #if 0
1028 /* Core 1351: If the field has an NSDMI that could throw, the
1029 default constructor is noexcept(false). FIXME this is
1030 broken by deferred parsing and 1360 saying we can't lazily
1031 declare a non-trivial default constructor. Also this
1032 needs to do deferred instantiation. Disable until the
1033 conflict between 1351 and 1360 is resolved. */
1034 if (spec_p && !expr_noexcept_p (DECL_INITIAL (field), complain))
1035 *spec_p = noexcept_false_spec;
1036 #endif
1038 /* Don't do the normal processing. */
1039 continue;
1042 bad = false;
1043 if (CP_TYPE_CONST_P (mem_type)
1044 && default_init_uninitialized_part (mem_type))
1046 if (msg)
1047 error ("uninitialized non-static const member %q#D",
1048 field);
1049 bad = true;
1051 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1053 if (msg)
1054 error ("uninitialized non-static reference member %q#D",
1055 field);
1056 bad = true;
1059 if (bad && deleted_p)
1060 *deleted_p = true;
1062 /* For an implicitly-defined default constructor to be constexpr,
1063 every member must have a user-provided default constructor or
1064 an explicit initializer. */
1065 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1066 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1068 *constexpr_p = false;
1069 if (msg)
1070 inform (0, "defaulted default constructor does not "
1071 "initialize %q+#D", field);
1075 if (!CLASS_TYPE_P (mem_type))
1076 continue;
1078 if (ANON_AGGR_TYPE_P (mem_type))
1080 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1081 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1082 deleted_p, constexpr_p, msg, flags, complain);
1083 continue;
1086 if (copy_arg_p)
1088 int mem_quals = cp_type_quals (mem_type) | quals;
1089 if (DECL_MUTABLE_P (field))
1090 mem_quals &= ~TYPE_QUAL_CONST;
1091 argtype = build_stub_type (mem_type, mem_quals, move_p);
1093 else
1094 argtype = NULL_TREE;
1096 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1098 process_subob_fn (rval, move_p, spec_p, trivial_p, deleted_p,
1099 constexpr_p, msg, field);
1103 /* The caller wants to generate an implicit declaration of SFK for CTYPE
1104 which is const if relevant and CONST_P is set. If spec_p, trivial_p and
1105 deleted_p are non-null, set their referent appropriately. If diag is
1106 true, we're either being called from maybe_explain_implicit_delete to
1107 give errors, or if constexpr_p is non-null, from
1108 explain_invalid_constexpr_fn. */
1110 static void
1111 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1112 tree *spec_p, bool *trivial_p, bool *deleted_p,
1113 bool *constexpr_p, bool diag)
1115 tree binfo, base_binfo, scope, fnname, rval, argtype;
1116 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1117 VEC(tree,gc) *vbases;
1118 int i, quals, flags;
1119 tsubst_flags_t complain;
1120 const char *msg;
1121 bool ctor_p;
1123 if (spec_p)
1124 *spec_p = (cxx_dialect >= cxx0x ? noexcept_true_spec : empty_except_spec);
1126 if (deleted_p)
1128 /* "The closure type associated with a lambda-expression has a deleted
1129 default constructor and a deleted copy assignment operator."
1130 This is diagnosed in maybe_explain_implicit_delete. */
1131 if (LAMBDA_TYPE_P (ctype)
1132 && (sfk == sfk_constructor
1133 || sfk == sfk_copy_assignment))
1135 *deleted_p = true;
1136 return;
1139 *deleted_p = false;
1142 ctor_p = false;
1143 assign_p = false;
1144 check_vdtor = false;
1145 switch (sfk)
1147 case sfk_move_assignment:
1148 case sfk_copy_assignment:
1149 assign_p = true;
1150 fnname = ansi_assopname (NOP_EXPR);
1151 break;
1153 case sfk_destructor:
1154 check_vdtor = true;
1155 /* The synthesized method will call base dtors, but check complete
1156 here to avoid having to deal with VTT. */
1157 fnname = complete_dtor_identifier;
1158 break;
1160 case sfk_constructor:
1161 case sfk_move_constructor:
1162 case sfk_copy_constructor:
1163 ctor_p = true;
1164 fnname = complete_ctor_identifier;
1165 break;
1167 default:
1168 gcc_unreachable ();
1171 /* If that user-written default constructor would satisfy the
1172 requirements of a constexpr constructor (7.1.5), the
1173 implicitly-defined default constructor is constexpr. */
1174 if (constexpr_p)
1175 *constexpr_p = ctor_p;
1177 move_p = false;
1178 switch (sfk)
1180 case sfk_constructor:
1181 case sfk_destructor:
1182 copy_arg_p = false;
1183 break;
1185 case sfk_move_constructor:
1186 case sfk_move_assignment:
1187 move_p = true;
1188 case sfk_copy_constructor:
1189 case sfk_copy_assignment:
1190 copy_arg_p = true;
1191 break;
1193 default:
1194 gcc_unreachable ();
1197 expected_trivial = type_has_trivial_fn (ctype, sfk);
1198 if (trivial_p)
1199 *trivial_p = expected_trivial;
1201 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1202 class versions and other properties of the type. But a subobject
1203 class can be trivially copyable and yet have overload resolution
1204 choose a template constructor for initialization, depending on
1205 rvalueness and cv-quals. So we can't exit early for copy/move
1206 methods in C++0x. The same considerations apply in C++98/03, but
1207 there the definition of triviality does not consider overload
1208 resolution, so a constructor can be trivial even if it would otherwise
1209 call a non-trivial constructor. */
1210 if (expected_trivial
1211 && (!copy_arg_p || cxx_dialect < cxx0x))
1213 if (constexpr_p && sfk == sfk_constructor)
1215 bool cx = trivial_default_constructor_is_constexpr (ctype);
1216 *constexpr_p = cx;
1217 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1218 /* A trivial constructor doesn't have any NSDMI. */
1219 inform (input_location, "defaulted default constructor does "
1220 "not initialize any non-static data member");
1222 if (!diag)
1223 return;
1226 ++cp_unevaluated_operand;
1227 ++c_inhibit_evaluation_warnings;
1229 scope = push_scope (ctype);
1231 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE|LOOKUP_DEFAULTED;
1233 complain = diag ? tf_warning_or_error : tf_none;
1235 if (const_p)
1236 quals = TYPE_QUAL_CONST;
1237 else
1238 quals = TYPE_UNQUALIFIED;
1239 argtype = NULL_TREE;
1241 if (!diag)
1242 msg = NULL;
1243 else if (assign_p)
1244 msg = ("base %qT does not have a move assignment operator or trivial "
1245 "copy assignment operator");
1246 else
1247 msg = ("base %qT does not have a move constructor or trivial "
1248 "copy constructor");
1250 for (binfo = TYPE_BINFO (ctype), i = 0;
1251 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1253 tree basetype = BINFO_TYPE (base_binfo);
1254 if (copy_arg_p)
1255 argtype = build_stub_type (basetype, quals, move_p);
1256 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1258 process_subob_fn (rval, move_p, spec_p, trivial_p, deleted_p,
1259 constexpr_p, msg, basetype);
1260 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1262 /* In a constructor we also need to check the subobject
1263 destructors for cleanup of partially constructed objects. */
1264 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1265 NULL_TREE, flags, complain);
1266 /* Note that we don't pass down trivial_p; the subobject
1267 destructors don't affect triviality of the constructor. Nor
1268 do they affect constexpr-ness (a constant expression doesn't
1269 throw) or exception-specification (a throw from one of the
1270 dtors would be a double-fault). */
1271 process_subob_fn (rval, false, NULL, NULL,
1272 deleted_p, NULL, NULL,
1273 basetype);
1276 if (check_vdtor && type_has_virtual_destructor (basetype))
1278 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1279 ptr_type_node, flags, complain);
1280 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1281 to have a null rval (no class-specific op delete). */
1282 if (rval && rval == error_mark_node && deleted_p)
1283 *deleted_p = true;
1284 check_vdtor = false;
1288 vbases = CLASSTYPE_VBASECLASSES (ctype);
1289 if (vbases && assign_p && move_p)
1291 /* Should the spec be changed to allow vbases that only occur once? */
1292 if (diag)
1293 error ("%qT has virtual bases, default move assignment operator "
1294 "cannot be generated", ctype);
1295 else if (deleted_p)
1296 *deleted_p = true;
1298 else if (!assign_p)
1300 if (diag)
1301 msg = ("virtual base %qT does not have a move constructor "
1302 "or trivial copy constructor");
1303 if (vbases && constexpr_p)
1304 *constexpr_p = false;
1305 FOR_EACH_VEC_ELT (tree, vbases, i, base_binfo)
1307 tree basetype = BINFO_TYPE (base_binfo);
1308 if (copy_arg_p)
1309 argtype = build_stub_type (basetype, quals, move_p);
1310 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1312 process_subob_fn (rval, move_p, spec_p, trivial_p, deleted_p,
1313 constexpr_p, msg, basetype);
1314 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1316 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1317 NULL_TREE, flags, complain);
1318 process_subob_fn (rval, false, NULL, NULL,
1319 deleted_p, NULL, NULL,
1320 basetype);
1324 if (!diag)
1325 /* Leave msg null. */;
1326 else if (assign_p)
1327 msg = ("non-static data member %qD does not have a move "
1328 "assignment operator or trivial copy assignment operator");
1329 else
1330 msg = ("non-static data member %qD does not have a move "
1331 "constructor or trivial copy constructor");
1332 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1333 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1334 deleted_p, constexpr_p, msg, flags, complain);
1335 if (ctor_p)
1336 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1337 sfk_destructor, TYPE_UNQUALIFIED, false,
1338 false, false, NULL, NULL,
1339 deleted_p, NULL,
1340 NULL, flags, complain);
1342 pop_scope (scope);
1344 --cp_unevaluated_operand;
1345 --c_inhibit_evaluation_warnings;
1348 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1349 return true; else return false. */
1351 bool
1352 maybe_explain_implicit_delete (tree decl)
1354 /* If decl is a clone, get the primary variant. */
1355 decl = DECL_ORIGIN (decl);
1356 gcc_assert (DECL_DELETED_FN (decl));
1357 if (DECL_DEFAULTED_FN (decl))
1359 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1360 static struct pointer_set_t *explained;
1362 special_function_kind sfk;
1363 location_t loc;
1364 bool informed;
1365 tree ctype;
1367 if (!explained)
1368 explained = pointer_set_create ();
1369 if (pointer_set_insert (explained, decl))
1370 return true;
1372 sfk = special_function_p (decl);
1373 ctype = DECL_CONTEXT (decl);
1374 loc = input_location;
1375 input_location = DECL_SOURCE_LOCATION (decl);
1377 informed = false;
1378 if (LAMBDA_TYPE_P (ctype))
1380 informed = true;
1381 if (sfk == sfk_constructor)
1382 inform (DECL_SOURCE_LOCATION (decl),
1383 "a lambda closure type has a deleted default constructor");
1384 else if (sfk == sfk_copy_assignment)
1385 inform (DECL_SOURCE_LOCATION (decl),
1386 "a lambda closure type has a deleted copy assignment operator");
1387 else
1388 informed = false;
1390 else if (DECL_ARTIFICIAL (decl)
1391 && (sfk == sfk_copy_assignment
1392 || sfk == sfk_copy_constructor)
1393 && (type_has_user_declared_move_constructor (ctype)
1394 || type_has_user_declared_move_assign (ctype)))
1396 inform (0, "%q+#D is implicitly declared as deleted because %qT "
1397 "declares a move constructor or move assignment operator",
1398 decl, ctype);
1399 informed = true;
1401 if (!informed)
1403 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1404 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1405 tree scope = push_scope (ctype);
1406 inform (0, "%q+#D is implicitly deleted because the default "
1407 "definition would be ill-formed:", decl);
1408 pop_scope (scope);
1409 synthesized_method_walk (ctype, sfk, const_p,
1410 NULL, NULL, NULL, NULL, true);
1413 input_location = loc;
1414 return true;
1416 return false;
1419 /* DECL is a defaulted function which was declared constexpr. Explain why
1420 it can't be constexpr. */
1422 void
1423 explain_implicit_non_constexpr (tree decl)
1425 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1426 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1427 bool dummy;
1428 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1429 special_function_p (decl), const_p,
1430 NULL, NULL, NULL, &dummy, true);
1433 /* Implicitly declare the special function indicated by KIND, as a
1434 member of TYPE. For copy constructors and assignment operators,
1435 CONST_P indicates whether these functions should take a const
1436 reference argument or a non-const reference. Returns the
1437 FUNCTION_DECL for the implicitly declared function. */
1439 tree
1440 implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
1442 tree fn;
1443 tree parameter_types = void_list_node;
1444 tree return_type;
1445 tree fn_type;
1446 tree raises = empty_except_spec;
1447 tree rhs_parm_type = NULL_TREE;
1448 tree this_parm;
1449 tree name;
1450 HOST_WIDE_INT saved_processing_template_decl;
1451 bool deleted_p;
1452 bool trivial_p;
1453 bool constexpr_p;
1455 /* Because we create declarations for implicitly declared functions
1456 lazily, we may be creating the declaration for a member of TYPE
1457 while in some completely different context. However, TYPE will
1458 never be a dependent class (because we never want to do lookups
1459 for implicitly defined functions in a dependent class).
1460 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1461 because we only create clones for constructors and destructors
1462 when not in a template. */
1463 gcc_assert (!dependent_type_p (type));
1464 saved_processing_template_decl = processing_template_decl;
1465 processing_template_decl = 0;
1467 type = TYPE_MAIN_VARIANT (type);
1469 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1471 if (kind == sfk_destructor)
1472 /* See comment in check_special_function_return_type. */
1473 return_type = build_pointer_type (void_type_node);
1474 else
1475 return_type = build_pointer_type (type);
1477 else
1478 return_type = void_type_node;
1480 switch (kind)
1482 case sfk_destructor:
1483 /* Destructor. */
1484 name = constructor_name (type);
1485 break;
1487 case sfk_constructor:
1488 /* Default constructor. */
1489 name = constructor_name (type);
1490 break;
1492 case sfk_copy_constructor:
1493 case sfk_copy_assignment:
1494 case sfk_move_constructor:
1495 case sfk_move_assignment:
1497 bool move_p;
1498 if (kind == sfk_copy_assignment
1499 || kind == sfk_move_assignment)
1501 return_type = build_reference_type (type);
1502 name = ansi_assopname (NOP_EXPR);
1504 else
1505 name = constructor_name (type);
1507 if (const_p)
1508 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1509 else
1510 rhs_parm_type = type;
1511 move_p = (kind == sfk_move_assignment
1512 || kind == sfk_move_constructor);
1513 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1515 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1516 break;
1518 default:
1519 gcc_unreachable ();
1522 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1523 &deleted_p, &constexpr_p, false);
1524 /* Don't bother marking a deleted constructor as constexpr. */
1525 if (deleted_p)
1526 constexpr_p = false;
1527 /* A trivial copy/move constructor is also a constexpr constructor. */
1528 else if (trivial_p && cxx_dialect >= cxx0x
1529 && (kind == sfk_copy_constructor
1530 || kind == sfk_move_constructor))
1531 gcc_assert (constexpr_p);
1533 if (!trivial_p && type_has_trivial_fn (type, kind))
1534 type_set_nontrivial_flag (type, kind);
1536 /* Create the function. */
1537 fn_type = build_method_type_directly (type, return_type, parameter_types);
1538 if (raises)
1539 fn_type = build_exception_variant (fn_type, raises);
1540 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1541 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1542 if (kind == sfk_constructor || kind == sfk_copy_constructor
1543 || kind == sfk_move_constructor)
1544 DECL_CONSTRUCTOR_P (fn) = 1;
1545 else if (kind == sfk_destructor)
1546 DECL_DESTRUCTOR_P (fn) = 1;
1547 else
1549 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1550 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1553 /* If pointers to member functions use the least significant bit to
1554 indicate whether a function is virtual, ensure a pointer
1555 to this function will have that bit clear. */
1556 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1557 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1558 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1560 /* Create the explicit arguments. */
1561 if (rhs_parm_type)
1563 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1564 want its type to be included in the mangled function
1565 name. */
1566 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1567 TREE_READONLY (decl) = 1;
1568 retrofit_lang_decl (decl);
1569 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
1570 DECL_ARGUMENTS (fn) = decl;
1572 /* Add the "this" parameter. */
1573 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1574 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1575 DECL_ARGUMENTS (fn) = this_parm;
1577 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1578 set_linkage_according_to_type (type, fn);
1579 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1580 DECL_IN_AGGR_P (fn) = 1;
1581 DECL_ARTIFICIAL (fn) = 1;
1582 DECL_DEFAULTED_FN (fn) = 1;
1583 if (cxx_dialect >= cxx0x)
1585 DECL_DELETED_FN (fn) = deleted_p;
1586 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
1588 DECL_EXTERNAL (fn) = true;
1589 DECL_NOT_REALLY_EXTERN (fn) = 1;
1590 DECL_DECLARED_INLINE_P (fn) = 1;
1591 gcc_assert (!TREE_USED (fn));
1593 /* Restore PROCESSING_TEMPLATE_DECL. */
1594 processing_template_decl = saved_processing_template_decl;
1596 return fn;
1599 /* Gives any errors about defaulted functions which need to be deferred
1600 until the containing class is complete. */
1602 void
1603 defaulted_late_check (tree fn)
1605 /* Complain about invalid signature for defaulted fn. */
1606 tree ctx = DECL_CONTEXT (fn);
1607 special_function_kind kind = special_function_p (fn);
1608 bool fn_const_p = (copy_fn_p (fn) == 2);
1609 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p);
1611 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1612 TREE_TYPE (TREE_TYPE (implicit_fn)))
1613 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1614 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
1616 error ("defaulted declaration %q+D", fn);
1617 error_at (DECL_SOURCE_LOCATION (fn),
1618 "does not match expected signature %qD", implicit_fn);
1621 /* 8.4.2/2: If it is explicitly defaulted on its first declaration, it is
1622 implicitly considered to have the same exception-specification as if
1623 it had been implicitly declared. */
1624 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1626 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
1627 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
1629 maybe_instantiate_noexcept (fn);
1630 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
1631 eh_spec, ce_normal))
1632 error ("function %q+D defaulted on its first declaration "
1633 "with an exception-specification that differs from "
1634 "the implicit declaration %q#D", fn, implicit_fn);
1636 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
1637 if (DECL_DECLARED_CONSTEXPR_P (implicit_fn))
1639 /* Hmm...should we do this for out-of-class too? Should it be OK to
1640 add constexpr later like inline, rather than requiring
1641 declarations to match? */
1642 DECL_DECLARED_CONSTEXPR_P (fn) = true;
1643 if (kind == sfk_constructor)
1644 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
1648 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
1649 && DECL_DECLARED_CONSTEXPR_P (fn))
1651 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
1653 error ("explicitly defaulted function %q+D cannot be declared "
1654 "as constexpr because the implicit declaration is not "
1655 "constexpr:", fn);
1656 explain_implicit_non_constexpr (fn);
1658 DECL_DECLARED_CONSTEXPR_P (fn) = false;
1661 if (DECL_DELETED_FN (implicit_fn))
1662 DECL_DELETED_FN (fn) = 1;
1665 /* Returns true iff FN can be explicitly defaulted, and gives any
1666 errors if defaulting FN is ill-formed. */
1668 bool
1669 defaultable_fn_check (tree fn)
1671 special_function_kind kind = sfk_none;
1673 if (template_parm_scope_p ())
1675 error ("a template cannot be defaulted");
1676 return false;
1679 if (DECL_CONSTRUCTOR_P (fn))
1681 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
1682 kind = sfk_constructor;
1683 else if (copy_fn_p (fn) > 0
1684 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
1685 == void_list_node))
1686 kind = sfk_copy_constructor;
1687 else if (move_fn_p (fn))
1688 kind = sfk_move_constructor;
1690 else if (DECL_DESTRUCTOR_P (fn))
1691 kind = sfk_destructor;
1692 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
1693 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
1695 if (copy_fn_p (fn))
1696 kind = sfk_copy_assignment;
1697 else if (move_fn_p (fn))
1698 kind = sfk_move_assignment;
1701 if (kind == sfk_none)
1703 error ("%qD cannot be defaulted", fn);
1704 return false;
1706 else
1708 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
1709 for (; t && t != void_list_node; t = TREE_CHAIN (t))
1710 if (TREE_PURPOSE (t))
1712 error ("defaulted function %q+D with default argument", fn);
1713 break;
1715 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
1716 /* Defer checking. */;
1717 else if (!processing_template_decl)
1718 defaulted_late_check (fn);
1720 return true;
1724 /* Add an implicit declaration to TYPE for the kind of function
1725 indicated by SFK. Return the FUNCTION_DECL for the new implicit
1726 declaration. */
1728 tree
1729 lazily_declare_fn (special_function_kind sfk, tree type)
1731 tree fn;
1732 /* Whether or not the argument has a const reference type. */
1733 bool const_p = false;
1735 switch (sfk)
1737 case sfk_constructor:
1738 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
1739 break;
1740 case sfk_copy_constructor:
1741 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
1742 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
1743 break;
1744 case sfk_move_constructor:
1745 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
1746 break;
1747 case sfk_copy_assignment:
1748 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
1749 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
1750 break;
1751 case sfk_move_assignment:
1752 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
1753 break;
1754 case sfk_destructor:
1755 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
1756 break;
1757 default:
1758 gcc_unreachable ();
1761 /* Declare the function. */
1762 fn = implicitly_declare_fn (sfk, type, const_p);
1764 /* [class.copy]/8 If the class definition declares a move constructor or
1765 move assignment operator, the implicitly declared copy constructor is
1766 defined as deleted.... */
1767 if ((sfk == sfk_copy_assignment
1768 || sfk == sfk_copy_constructor)
1769 && (type_has_user_declared_move_constructor (type)
1770 || type_has_user_declared_move_assign (type)))
1771 DECL_DELETED_FN (fn) = true;
1773 /* For move variants, rather than declare them as deleted we just
1774 don't declare them at all. */
1775 if (DECL_DELETED_FN (fn)
1776 && (sfk == sfk_move_constructor
1777 || sfk == sfk_move_assignment))
1778 return NULL_TREE;
1780 /* A destructor may be virtual. */
1781 if (sfk == sfk_destructor
1782 || sfk == sfk_move_assignment
1783 || sfk == sfk_copy_assignment)
1784 check_for_override (fn, type);
1785 /* Add it to CLASSTYPE_METHOD_VEC. */
1786 add_method (type, fn, NULL_TREE);
1787 /* Add it to TYPE_METHODS. */
1788 if (sfk == sfk_destructor
1789 && DECL_VIRTUAL_P (fn)
1790 && abi_version_at_least (2))
1791 /* The ABI requires that a virtual destructor go at the end of the
1792 vtable. */
1793 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
1794 else
1796 /* G++ 3.2 put the implicit destructor at the *beginning* of the
1797 TYPE_METHODS list, which cause the destructor to be emitted
1798 in an incorrect location in the vtable. */
1799 if (warn_abi && sfk == sfk_destructor && DECL_VIRTUAL_P (fn))
1800 warning (OPT_Wabi, "vtable layout for class %qT may not be ABI-compliant"
1801 "and may change in a future version of GCC due to "
1802 "implicit virtual destructor",
1803 type);
1804 DECL_CHAIN (fn) = TYPE_METHODS (type);
1805 TYPE_METHODS (type) = fn;
1807 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
1808 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
1809 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
1810 /* Create appropriate clones. */
1811 clone_function_decl (fn, /*update_method_vec=*/true);
1813 return fn;
1816 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
1817 as there are artificial parms in FN. */
1819 tree
1820 skip_artificial_parms_for (const_tree fn, tree list)
1822 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1823 list = TREE_CHAIN (list);
1824 else
1825 return list;
1827 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1828 list = TREE_CHAIN (list);
1829 if (DECL_HAS_VTT_PARM_P (fn))
1830 list = TREE_CHAIN (list);
1831 return list;
1834 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
1835 artificial parms in FN. */
1838 num_artificial_parms_for (const_tree fn)
1840 int count = 0;
1842 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1843 count++;
1844 else
1845 return 0;
1847 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1848 count++;
1849 if (DECL_HAS_VTT_PARM_P (fn))
1850 count++;
1851 return count;
1855 #include "gt-cp-method.h"