* method.c (process_subob_fn): Make sure no_implicit_p is non-null
[official-gcc.git] / gcc / cp / method.c
blob20a6c87d9a6b5edd0b460eedff75bc635e7b8d5b
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 "diagnostic.h"
38 #include "cgraph.h"
39 #include "gimple.h"
41 /* Various flags to control the mangling process. */
43 enum mangling_flags
45 /* No flags. */
46 mf_none = 0,
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. */
65 void
66 init_method (void)
68 init_mangle ();
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. */
80 tree
81 make_thunk (tree function, bool this_adjusting,
82 tree fixed_offset, tree virtual_offset)
84 HOST_WIDE_INT d;
85 tree thunk;
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)
94 virtual_offset
95 = size_binop (MULT_EXPR,
96 virtual_offset,
97 convert (ssizetype,
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
104 will be a BINFO. */
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)
109 && (!virtual_offset
110 || (this_adjusting
111 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
112 virtual_offset)
113 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
114 return thunk;
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_THUNKS (thunk) = NULL_TREE;
131 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
132 TREE_READONLY (thunk) = TREE_READONLY (function);
133 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
134 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
135 SET_DECL_THUNK_P (thunk, this_adjusting);
136 THUNK_TARGET (thunk) = function;
137 THUNK_FIXED_OFFSET (thunk) = d;
138 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
139 THUNK_ALIAS (thunk) = NULL_TREE;
141 DECL_INTERFACE_KNOWN (thunk) = 1;
142 DECL_NOT_REALLY_EXTERN (thunk) = 1;
143 DECL_COMDAT (thunk) = DECL_COMDAT (function);
144 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
145 /* The thunk itself is not a constructor or destructor, even if
146 the thing it is thunking to is. */
147 DECL_DESTRUCTOR_P (thunk) = 0;
148 DECL_CONSTRUCTOR_P (thunk) = 0;
149 DECL_EXTERNAL (thunk) = 1;
150 DECL_ARTIFICIAL (thunk) = 1;
151 /* The THUNK is not a pending inline, even if the FUNCTION is. */
152 DECL_PENDING_INLINE_P (thunk) = 0;
153 DECL_DECLARED_INLINE_P (thunk) = 0;
154 /* Nor is it a template instantiation. */
155 DECL_USE_TEMPLATE (thunk) = 0;
156 DECL_TEMPLATE_INFO (thunk) = NULL;
158 /* Add it to the list of thunks associated with FUNCTION. */
159 DECL_CHAIN (thunk) = DECL_THUNKS (function);
160 DECL_THUNKS (function) = thunk;
162 return thunk;
165 /* Finish THUNK, a thunk decl. */
167 void
168 finish_thunk (tree thunk)
170 tree function, name;
171 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
172 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
174 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
175 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
176 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
177 function = THUNK_TARGET (thunk);
178 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
179 fixed_offset, virtual_offset);
181 /* We can end up with declarations of (logically) different
182 covariant thunks, that do identical adjustments. The two thunks
183 will be adjusting between within different hierarchies, which
184 happen to have the same layout. We must nullify one of them to
185 refer to the other. */
186 if (DECL_RESULT_THUNK_P (thunk))
188 tree cov_probe;
190 for (cov_probe = DECL_THUNKS (function);
191 cov_probe; cov_probe = DECL_CHAIN (cov_probe))
192 if (DECL_NAME (cov_probe) == name)
194 gcc_assert (!DECL_THUNKS (thunk));
195 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
196 ? THUNK_ALIAS (cov_probe) : cov_probe);
197 break;
201 DECL_NAME (thunk) = name;
202 SET_DECL_ASSEMBLER_NAME (thunk, name);
205 static GTY (()) int thunk_labelno;
207 /* Create a static alias to target. */
209 tree
210 make_alias_for (tree target, tree newid)
212 tree alias = build_decl (DECL_SOURCE_LOCATION (target),
213 TREE_CODE (target), newid, TREE_TYPE (target));
214 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
215 cxx_dup_lang_specific_decl (alias);
216 DECL_CONTEXT (alias) = NULL;
217 TREE_READONLY (alias) = TREE_READONLY (target);
218 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
219 TREE_PUBLIC (alias) = 0;
220 DECL_INTERFACE_KNOWN (alias) = 1;
221 if (DECL_LANG_SPECIFIC (alias))
223 DECL_NOT_REALLY_EXTERN (alias) = 1;
224 DECL_USE_TEMPLATE (alias) = 0;
225 DECL_TEMPLATE_INFO (alias) = NULL;
227 DECL_EXTERNAL (alias) = 0;
228 DECL_ARTIFICIAL (alias) = 1;
229 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
230 if (TREE_CODE (alias) == FUNCTION_DECL)
232 DECL_SAVED_FUNCTION_DATA (alias) = NULL;
233 DECL_DESTRUCTOR_P (alias) = 0;
234 DECL_CONSTRUCTOR_P (alias) = 0;
235 DECL_PENDING_INLINE_P (alias) = 0;
236 DECL_DECLARED_INLINE_P (alias) = 0;
237 DECL_INITIAL (alias) = error_mark_node;
238 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
240 else
241 TREE_STATIC (alias) = 1;
242 TREE_ADDRESSABLE (alias) = 1;
243 TREE_USED (alias) = 1;
244 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
245 return alias;
248 static tree
249 make_alias_for_thunk (tree function)
251 tree alias;
252 char buf[256];
254 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
255 thunk_labelno++;
257 alias = make_alias_for (function, get_identifier (buf));
259 if (!flag_syntax_only)
261 struct cgraph_node *funcn, *aliasn;
262 funcn = cgraph_get_node (function);
263 gcc_checking_assert (funcn);
264 aliasn = cgraph_same_body_alias (funcn, alias, function);
265 DECL_ASSEMBLER_NAME (function);
266 gcc_assert (aliasn != NULL);
269 return alias;
272 /* Emit the definition of a C++ multiple inheritance or covariant
273 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
274 immediately. */
276 void
277 use_thunk (tree thunk_fndecl, bool emit_p)
279 tree a, t, function, alias;
280 tree virtual_offset;
281 HOST_WIDE_INT fixed_offset, virtual_value;
282 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
283 struct cgraph_node *funcn, *thunk_node;
285 /* We should have called finish_thunk to give it a name. */
286 gcc_assert (DECL_NAME (thunk_fndecl));
288 /* We should never be using an alias, always refer to the
289 aliased thunk. */
290 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
292 if (TREE_ASM_WRITTEN (thunk_fndecl))
293 return;
295 function = THUNK_TARGET (thunk_fndecl);
296 if (DECL_RESULT (thunk_fndecl))
297 /* We already turned this thunk into an ordinary function.
298 There's no need to process this thunk again. */
299 return;
301 if (DECL_THUNK_P (function))
302 /* The target is itself a thunk, process it now. */
303 use_thunk (function, emit_p);
305 /* Thunks are always addressable; they only appear in vtables. */
306 TREE_ADDRESSABLE (thunk_fndecl) = 1;
308 /* Figure out what function is being thunked to. It's referenced in
309 this translation unit. */
310 TREE_ADDRESSABLE (function) = 1;
311 mark_used (function);
312 if (!emit_p)
313 return;
315 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
316 alias = make_alias_for_thunk (function);
317 else
318 alias = function;
320 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
321 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
323 if (virtual_offset)
325 if (!this_adjusting)
326 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
327 virtual_value = tree_low_cst (virtual_offset, /*pos=*/0);
328 gcc_assert (virtual_value);
330 else
331 virtual_value = 0;
333 /* And, if we need to emit the thunk, it's used. */
334 mark_used (thunk_fndecl);
335 /* This thunk is actually defined. */
336 DECL_EXTERNAL (thunk_fndecl) = 0;
337 /* The linkage of the function may have changed. FIXME in linkage
338 rewrite. */
339 gcc_assert (DECL_INTERFACE_KNOWN (function));
340 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
341 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
342 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
343 = DECL_VISIBILITY_SPECIFIED (function);
344 DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
345 DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
347 if (flag_syntax_only)
349 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
350 return;
353 push_to_top_level ();
355 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
356 && targetm_common.have_named_sections)
358 resolve_unique_section (function, 0, flag_function_sections);
360 if (DECL_SECTION_NAME (function) != NULL && DECL_ONE_ONLY (function))
362 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
364 /* Output the thunk into the same section as function. */
365 DECL_SECTION_NAME (thunk_fndecl) = DECL_SECTION_NAME (function);
369 /* Set up cloned argument trees for the thunk. */
370 t = NULL_TREE;
371 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
373 tree x = copy_node (a);
374 DECL_CHAIN (x) = t;
375 DECL_CONTEXT (x) = thunk_fndecl;
376 SET_DECL_RTL (x, NULL);
377 DECL_HAS_VALUE_EXPR_P (x) = 0;
378 TREE_ADDRESSABLE (x) = 0;
379 t = x;
381 a = nreverse (t);
382 DECL_ARGUMENTS (thunk_fndecl) = a;
383 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
384 funcn = cgraph_get_node (function);
385 gcc_checking_assert (funcn);
386 thunk_node = cgraph_add_thunk (funcn, thunk_fndecl, function,
387 this_adjusting, fixed_offset, virtual_value,
388 virtual_offset, alias);
389 if (DECL_ONE_ONLY (function))
390 symtab_add_to_same_comdat_group ((symtab_node) thunk_node,
391 (symtab_node) funcn);
393 if (!this_adjusting
394 || !targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
395 virtual_value, alias))
397 /* If this is a covariant thunk, or we don't have the necessary
398 code for efficient thunks, generate a thunk function that
399 just makes a call to the real function. Unfortunately, this
400 doesn't work for varargs. */
402 if (varargs_function_p (function))
403 error ("generic thunk code fails for method %q#D which uses %<...%>",
404 function);
407 pop_from_top_level ();
410 /* Code for synthesizing methods which have default semantics defined. */
412 /* True iff CTYPE has a trivial SFK. */
414 static bool
415 type_has_trivial_fn (tree ctype, special_function_kind sfk)
417 switch (sfk)
419 case sfk_constructor:
420 return !TYPE_HAS_COMPLEX_DFLT (ctype);
421 case sfk_copy_constructor:
422 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
423 case sfk_move_constructor:
424 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
425 case sfk_copy_assignment:
426 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
427 case sfk_move_assignment:
428 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
429 case sfk_destructor:
430 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
431 default:
432 gcc_unreachable ();
436 /* Note that CTYPE has a non-trivial SFK even though we previously thought
437 it was trivial. */
439 static void
440 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
442 switch (sfk)
444 case sfk_constructor:
445 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
446 return;
447 case sfk_copy_constructor:
448 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
449 return;
450 case sfk_move_constructor:
451 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
452 return;
453 case sfk_copy_assignment:
454 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
455 return;
456 case sfk_move_assignment:
457 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
458 return;
459 case sfk_destructor:
460 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
461 return;
462 default:
463 gcc_unreachable ();
467 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
469 bool
470 trivial_fn_p (tree fn)
472 if (!DECL_DEFAULTED_FN (fn))
473 return false;
475 /* If fn is a clone, get the primary variant. */
476 fn = DECL_ORIGIN (fn);
477 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
480 /* Generate code for default X(X&) or X(X&&) constructor. */
482 static void
483 do_build_copy_constructor (tree fndecl)
485 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
486 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
487 bool trivial = trivial_fn_p (fndecl);
489 parm = convert_from_reference (parm);
491 if (trivial
492 && is_empty_class (current_class_type))
493 /* Don't copy the padding byte; it might not have been allocated
494 if *this is a base subobject. */;
495 else if (trivial)
497 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
498 finish_expr_stmt (t);
500 else
502 tree fields = TYPE_FIELDS (current_class_type);
503 tree member_init_list = NULL_TREE;
504 int cvquals = cp_type_quals (TREE_TYPE (parm));
505 int i;
506 tree binfo, base_binfo;
507 tree init;
508 VEC(tree,gc) *vbases;
510 /* Initialize all the base-classes with the parameter converted
511 to their type so that we get their copy constructor and not
512 another constructor that takes current_class_type. We must
513 deal with the binfo's directly as a direct base might be
514 inaccessible due to ambiguity. */
515 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
516 VEC_iterate (tree, vbases, i, binfo); i++)
518 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
519 tf_warning_or_error);
520 if (move_p)
521 init = move (init);
522 member_init_list
523 = tree_cons (binfo,
524 build_tree_list (NULL_TREE, init),
525 member_init_list);
528 for (binfo = TYPE_BINFO (current_class_type), i = 0;
529 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
531 if (BINFO_VIRTUAL_P (base_binfo))
532 continue;
534 init = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
535 tf_warning_or_error);
536 if (move_p)
537 init = move (init);
538 member_init_list
539 = tree_cons (base_binfo,
540 build_tree_list (NULL_TREE, init),
541 member_init_list);
544 for (; fields; fields = DECL_CHAIN (fields))
546 tree field = fields;
547 tree expr_type;
549 if (TREE_CODE (field) != FIELD_DECL)
550 continue;
552 expr_type = TREE_TYPE (field);
553 if (DECL_NAME (field))
555 if (VFIELD_NAME_P (DECL_NAME (field)))
556 continue;
558 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
559 /* Just use the field; anonymous types can't have
560 nontrivial copy ctors or assignment ops or this
561 function would be deleted. */;
562 else
563 continue;
565 /* Compute the type of "init->field". If the copy-constructor
566 parameter is, for example, "const S&", and the type of
567 the field is "T", then the type will usually be "const
568 T". (There are no cv-qualified variants of reference
569 types.) */
570 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
572 int quals = cvquals;
574 if (DECL_MUTABLE_P (field))
575 quals &= ~TYPE_QUAL_CONST;
576 quals |= cp_type_quals (expr_type);
577 expr_type = cp_build_qualified_type (expr_type, quals);
580 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
581 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE)
582 init = move (init);
583 init = build_tree_list (NULL_TREE, init);
585 member_init_list = tree_cons (field, init, member_init_list);
587 finish_mem_initializers (member_init_list);
591 static void
592 do_build_copy_assign (tree fndecl)
594 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
595 tree compound_stmt;
596 bool move_p = move_fn_p (fndecl);
597 bool trivial = trivial_fn_p (fndecl);
598 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
600 compound_stmt = begin_compound_stmt (0);
601 parm = convert_from_reference (parm);
603 if (trivial
604 && is_empty_class (current_class_type))
605 /* Don't copy the padding byte; it might not have been allocated
606 if *this is a base subobject. */;
607 else if (trivial)
609 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
610 finish_expr_stmt (t);
612 else
614 tree fields;
615 int cvquals = cp_type_quals (TREE_TYPE (parm));
616 int i;
617 tree binfo, base_binfo;
619 /* Assign to each of the direct base classes. */
620 for (binfo = TYPE_BINFO (current_class_type), i = 0;
621 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
623 tree converted_parm;
624 VEC(tree,gc) *parmvec;
626 /* We must convert PARM directly to the base class
627 explicitly since the base class may be ambiguous. */
628 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
629 tf_warning_or_error);
630 if (move_p)
631 converted_parm = move (converted_parm);
632 /* Call the base class assignment operator. */
633 parmvec = make_tree_vector_single (converted_parm);
634 finish_expr_stmt
635 (build_special_member_call (current_class_ref,
636 ansi_assopname (NOP_EXPR),
637 &parmvec,
638 base_binfo,
639 flags,
640 tf_warning_or_error));
641 release_tree_vector (parmvec);
644 /* Assign to each of the non-static data members. */
645 for (fields = TYPE_FIELDS (current_class_type);
646 fields;
647 fields = DECL_CHAIN (fields))
649 tree comp = current_class_ref;
650 tree init = parm;
651 tree field = fields;
652 tree expr_type;
653 int quals;
655 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
656 continue;
658 expr_type = TREE_TYPE (field);
660 if (CP_TYPE_CONST_P (expr_type))
662 error ("non-static const member %q#D, can%'t use default "
663 "assignment operator", field);
664 continue;
666 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
668 error ("non-static reference member %q#D, can%'t use "
669 "default assignment operator", field);
670 continue;
673 if (DECL_NAME (field))
675 if (VFIELD_NAME_P (DECL_NAME (field)))
676 continue;
678 else if (ANON_AGGR_TYPE_P (expr_type)
679 && TYPE_FIELDS (expr_type) != NULL_TREE)
680 /* Just use the field; anonymous types can't have
681 nontrivial copy ctors or assignment ops or this
682 function would be deleted. */;
683 else
684 continue;
686 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
688 /* Compute the type of init->field */
689 quals = cvquals;
690 if (DECL_MUTABLE_P (field))
691 quals &= ~TYPE_QUAL_CONST;
692 expr_type = cp_build_qualified_type (expr_type, quals);
694 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
695 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE)
696 init = move (init);
698 if (DECL_NAME (field))
699 init = cp_build_modify_expr (comp, NOP_EXPR, init,
700 tf_warning_or_error);
701 else
702 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
703 finish_expr_stmt (init);
706 finish_return_stmt (current_class_ref);
707 finish_compound_stmt (compound_stmt);
710 /* Synthesize FNDECL, a non-static member function. */
712 void
713 synthesize_method (tree fndecl)
715 bool nested = (current_function_decl != NULL_TREE);
716 tree context = decl_function_context (fndecl);
717 bool need_body = true;
718 tree stmt;
719 location_t save_input_location = input_location;
720 int error_count = errorcount;
721 int warning_count = warningcount;
723 /* Reset the source location, we might have been previously
724 deferred, and thus have saved where we were first needed. */
725 DECL_SOURCE_LOCATION (fndecl)
726 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
728 /* If we've been asked to synthesize a clone, just synthesize the
729 cloned function instead. Doing so will automatically fill in the
730 body for the clone. */
731 if (DECL_CLONED_FUNCTION_P (fndecl))
732 fndecl = DECL_CLONED_FUNCTION (fndecl);
734 /* We may be in the middle of deferred access check. Disable
735 it now. */
736 push_deferring_access_checks (dk_no_deferred);
738 if (! context)
739 push_to_top_level ();
740 else if (nested)
741 push_function_context ();
743 input_location = DECL_SOURCE_LOCATION (fndecl);
745 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
746 stmt = begin_function_body ();
748 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
750 do_build_copy_assign (fndecl);
751 need_body = false;
753 else if (DECL_CONSTRUCTOR_P (fndecl))
755 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
756 if (arg_chain != void_list_node)
757 do_build_copy_constructor (fndecl);
758 else
759 finish_mem_initializers (NULL_TREE);
762 /* If we haven't yet generated the body of the function, just
763 generate an empty compound statement. */
764 if (need_body)
766 tree compound_stmt;
767 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
768 finish_compound_stmt (compound_stmt);
771 finish_function_body (stmt);
772 expand_or_defer_fn (finish_function (0));
774 input_location = save_input_location;
776 if (! context)
777 pop_from_top_level ();
778 else if (nested)
779 pop_function_context ();
781 pop_deferring_access_checks ();
783 if (error_count != errorcount || warning_count != warningcount)
784 inform (input_location, "synthesized method %qD first required here ",
785 fndecl);
788 /* Build a reference to type TYPE with cv-quals QUALS, which is an
789 rvalue if RVALUE is true. */
791 static tree
792 build_stub_type (tree type, int quals, bool rvalue)
794 tree argtype = cp_build_qualified_type (type, quals);
795 return cp_build_reference_type (argtype, rvalue);
798 /* Build a dummy glvalue from dereferencing a dummy reference of type
799 REFTYPE. */
801 static tree
802 build_stub_object (tree reftype)
804 tree stub = build1 (NOP_EXPR, reftype, integer_one_node);
805 return convert_from_reference (stub);
808 /* Determine which function will be called when looking up NAME in TYPE,
809 called with a single ARGTYPE argument, or no argument if ARGTYPE is
810 null. FLAGS and COMPLAIN are as for build_new_method_call.
812 Returns a FUNCTION_DECL if all is well.
813 Returns NULL_TREE if overload resolution failed.
814 Returns error_mark_node if the chosen function cannot be called. */
816 static tree
817 locate_fn_flags (tree type, tree name, tree argtype, int flags,
818 tsubst_flags_t complain)
820 tree ob, fn, fns, binfo, rval;
821 VEC(tree,gc) *args;
823 if (TYPE_P (type))
824 binfo = TYPE_BINFO (type);
825 else
827 binfo = type;
828 type = BINFO_TYPE (binfo);
831 ob = build_stub_object (cp_build_reference_type (type, false));
832 args = make_tree_vector ();
833 if (argtype)
835 tree arg = build_stub_object (argtype);
836 VEC_quick_push (tree, args, arg);
839 fns = lookup_fnfields (binfo, name, 0);
840 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
842 release_tree_vector (args);
843 if (fn && rval == error_mark_node)
844 return rval;
845 else
846 return fn;
849 /* Locate the dtor of TYPE. */
851 tree
852 get_dtor (tree type, tsubst_flags_t complain)
854 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
855 LOOKUP_NORMAL, complain);
856 if (fn == error_mark_node)
857 return NULL_TREE;
858 return fn;
861 /* Locate the default ctor of TYPE. */
863 tree
864 locate_ctor (tree type)
866 tree fn;
868 push_deferring_access_checks (dk_no_check);
869 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
870 LOOKUP_SPECULATIVE, tf_none);
871 pop_deferring_access_checks ();
872 if (fn == error_mark_node)
873 return NULL_TREE;
874 return fn;
877 /* Likewise, but give any appropriate errors. */
879 tree
880 get_default_ctor (tree type)
882 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
883 LOOKUP_NORMAL, tf_warning_or_error);
884 if (fn == error_mark_node)
885 return NULL_TREE;
886 return fn;
889 /* Locate the copy ctor of TYPE. */
891 tree
892 get_copy_ctor (tree type, tsubst_flags_t complain)
894 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
895 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
896 tree argtype = build_stub_type (type, quals, false);
897 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
898 LOOKUP_NORMAL, complain);
899 if (fn == error_mark_node)
900 return NULL_TREE;
901 return fn;
904 /* Locate the copy assignment operator of TYPE. */
906 tree
907 get_copy_assign (tree type)
909 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
910 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
911 tree argtype = build_stub_type (type, quals, false);
912 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
913 LOOKUP_NORMAL, tf_warning_or_error);
914 if (fn == error_mark_node)
915 return NULL_TREE;
916 return fn;
919 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
920 DELETED_P or give an error message MSG with argument ARG. */
922 static void
923 process_subob_fn (tree fn, bool move_p, tree *spec_p, bool *trivial_p,
924 bool *deleted_p, bool *constexpr_p, bool *no_implicit_p,
925 bool diag, tree arg)
927 if (!fn || fn == error_mark_node)
928 goto bad;
930 if (spec_p)
932 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
933 *spec_p = merge_exception_specifiers (*spec_p, raises, fn);
936 if (!trivial_fn_p (fn))
938 if (trivial_p)
939 *trivial_p = false;
940 if (TREE_CODE (arg) == FIELD_DECL
941 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
943 if (deleted_p)
944 *deleted_p = true;
945 if (diag)
946 error ("union member %q+D with non-trivial %qD", arg, fn);
950 /* Core 1402: A non-trivial copy op suppresses the implicit
951 declaration of the move ctor/op=. */
952 if (no_implicit_p && move_p && !move_fn_p (fn) && !trivial_fn_p (fn))
953 *no_implicit_p = true;
955 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
957 *constexpr_p = false;
958 if (diag)
960 inform (0, "defaulted constructor calls non-constexpr "
961 "%q+D", fn);
962 explain_invalid_constexpr_fn (fn);
966 return;
968 bad:
969 if (deleted_p)
970 *deleted_p = true;
973 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
974 aggregates. */
976 static void
977 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
978 int quals, bool copy_arg_p, bool move_p,
979 bool assign_p, tree *spec_p, bool *trivial_p,
980 bool *deleted_p, bool *constexpr_p, bool *no_implicit_p,
981 bool diag, int flags, tsubst_flags_t complain)
983 tree field;
984 for (field = fields; field; field = DECL_CHAIN (field))
986 tree mem_type, argtype, rval;
988 if (TREE_CODE (field) != FIELD_DECL
989 || DECL_ARTIFICIAL (field))
990 continue;
992 mem_type = strip_array_types (TREE_TYPE (field));
993 if (assign_p)
995 bool bad = true;
996 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
998 if (diag)
999 error ("non-static const member %q#D, can%'t use default "
1000 "assignment operator", field);
1002 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1004 if (diag)
1005 error ("non-static reference member %q#D, can%'t use "
1006 "default assignment operator", field);
1008 else
1009 bad = false;
1011 if (bad && deleted_p)
1012 *deleted_p = true;
1014 else if (sfk == sfk_constructor)
1016 bool bad;
1018 if (DECL_INITIAL (field))
1020 if (diag && DECL_INITIAL (field) == error_mark_node)
1021 inform (0, "initializer for %q+#D is invalid", field);
1022 if (trivial_p)
1023 *trivial_p = false;
1024 #if 0
1025 /* Core 1351: If the field has an NSDMI that could throw, the
1026 default constructor is noexcept(false). FIXME this is
1027 broken by deferred parsing and 1360 saying we can't lazily
1028 declare a non-trivial default constructor. Also this
1029 needs to do deferred instantiation. Disable until the
1030 conflict between 1351 and 1360 is resolved. */
1031 if (spec_p && !expr_noexcept_p (DECL_INITIAL (field), complain))
1032 *spec_p = noexcept_false_spec;
1033 #endif
1035 /* Don't do the normal processing. */
1036 continue;
1039 bad = false;
1040 if (CP_TYPE_CONST_P (mem_type)
1041 && default_init_uninitialized_part (mem_type))
1043 if (diag)
1044 error ("uninitialized non-static const member %q#D",
1045 field);
1046 bad = true;
1048 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1050 if (diag)
1051 error ("uninitialized non-static reference member %q#D",
1052 field);
1053 bad = true;
1056 if (bad && deleted_p)
1057 *deleted_p = true;
1059 /* For an implicitly-defined default constructor to be constexpr,
1060 every member must have a user-provided default constructor or
1061 an explicit initializer. */
1062 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1063 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1065 *constexpr_p = false;
1066 if (diag)
1067 inform (0, "defaulted default constructor does not "
1068 "initialize %q+#D", field);
1072 if (!CLASS_TYPE_P (mem_type))
1073 continue;
1075 if (ANON_AGGR_TYPE_P (mem_type))
1077 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1078 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1079 deleted_p, constexpr_p, no_implicit_p,
1080 diag, flags, complain);
1081 continue;
1084 if (copy_arg_p)
1086 int mem_quals = cp_type_quals (mem_type) | quals;
1087 if (DECL_MUTABLE_P (field))
1088 mem_quals &= ~TYPE_QUAL_CONST;
1089 argtype = build_stub_type (mem_type, mem_quals, move_p);
1091 else
1092 argtype = NULL_TREE;
1094 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1096 process_subob_fn (rval, move_p, spec_p, trivial_p, deleted_p,
1097 constexpr_p, no_implicit_p, diag, field);
1101 /* The caller wants to generate an implicit declaration of SFK for CTYPE
1102 which is const if relevant and CONST_P is set. If spec_p, trivial_p and
1103 deleted_p are non-null, set their referent appropriately. If diag is
1104 true, we're either being called from maybe_explain_implicit_delete to
1105 give errors, or if constexpr_p is non-null, from
1106 explain_invalid_constexpr_fn. */
1108 static void
1109 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1110 tree *spec_p, bool *trivial_p, bool *deleted_p,
1111 bool *constexpr_p, bool *no_implicit_p, bool diag)
1113 tree binfo, base_binfo, scope, fnname, rval, argtype;
1114 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1115 VEC(tree,gc) *vbases;
1116 int i, quals, flags;
1117 tsubst_flags_t complain;
1118 bool ctor_p;
1120 if (spec_p)
1121 *spec_p = (cxx_dialect >= cxx0x ? noexcept_true_spec : empty_except_spec);
1123 if (no_implicit_p)
1124 *no_implicit_p = false;
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 for (binfo = TYPE_BINFO (ctype), i = 0;
1242 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1244 tree basetype = BINFO_TYPE (base_binfo);
1246 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1247 /* We'll handle virtual bases below. */
1248 continue;
1250 if (copy_arg_p)
1251 argtype = build_stub_type (basetype, quals, move_p);
1252 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1254 process_subob_fn (rval, move_p, spec_p, trivial_p, deleted_p,
1255 constexpr_p, no_implicit_p, diag, basetype);
1256 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1258 /* In a constructor we also need to check the subobject
1259 destructors for cleanup of partially constructed objects. */
1260 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1261 NULL_TREE, flags, complain);
1262 /* Note that we don't pass down trivial_p; the subobject
1263 destructors don't affect triviality of the constructor. Nor
1264 do they affect constexpr-ness (a constant expression doesn't
1265 throw) or exception-specification (a throw from one of the
1266 dtors would be a double-fault). */
1267 process_subob_fn (rval, false, NULL, NULL,
1268 deleted_p, NULL, NULL, false,
1269 basetype);
1272 if (check_vdtor && type_has_virtual_destructor (basetype))
1274 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1275 ptr_type_node, flags, complain);
1276 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1277 to have a null rval (no class-specific op delete). */
1278 if (rval && rval == error_mark_node && deleted_p)
1279 *deleted_p = true;
1280 check_vdtor = false;
1284 vbases = CLASSTYPE_VBASECLASSES (ctype);
1285 if (vbases == NULL)
1286 /* No virtual bases to worry about. */;
1287 else if (assign_p && move_p && no_implicit_p)
1289 /* Don't implicitly declare a defaulted move assignment if a virtual
1290 base has non-trivial move assignment, since moving the same base
1291 more than once is dangerous. */
1292 /* Should the spec be changed to allow vbases that only occur once? */
1293 FOR_EACH_VEC_ELT (tree, vbases, i, base_binfo)
1295 tree basetype = BINFO_TYPE (base_binfo);
1296 if (copy_arg_p)
1297 argtype = build_stub_type (basetype, quals, move_p);
1298 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1299 if (rval && rval != error_mark_node
1300 && move_fn_p (rval) && !trivial_fn_p (rval))
1302 *no_implicit_p = true;
1303 break;
1307 else if (!assign_p)
1309 if (constexpr_p)
1310 *constexpr_p = false;
1311 FOR_EACH_VEC_ELT (tree, vbases, i, base_binfo)
1313 tree basetype = BINFO_TYPE (base_binfo);
1314 if (copy_arg_p)
1315 argtype = build_stub_type (basetype, quals, move_p);
1316 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1318 process_subob_fn (rval, move_p, spec_p, trivial_p, deleted_p,
1319 constexpr_p, no_implicit_p, diag, basetype);
1320 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1322 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1323 NULL_TREE, flags, complain);
1324 process_subob_fn (rval, false, NULL, NULL,
1325 deleted_p, NULL, NULL, false,
1326 basetype);
1331 /* Now handle the non-static data members. */
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, no_implicit_p,
1335 diag, flags, complain);
1336 if (ctor_p)
1337 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1338 sfk_destructor, TYPE_UNQUALIFIED, false,
1339 false, false, NULL, NULL,
1340 deleted_p, NULL,
1341 NULL, false, flags, complain);
1343 pop_scope (scope);
1345 --cp_unevaluated_operand;
1346 --c_inhibit_evaluation_warnings;
1349 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1350 return true; else return false. */
1352 bool
1353 maybe_explain_implicit_delete (tree decl)
1355 /* If decl is a clone, get the primary variant. */
1356 decl = DECL_ORIGIN (decl);
1357 gcc_assert (DECL_DELETED_FN (decl));
1358 if (DECL_DEFAULTED_FN (decl))
1360 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1361 static struct pointer_set_t *explained;
1363 special_function_kind sfk;
1364 location_t loc;
1365 bool informed;
1366 tree ctype;
1368 if (!explained)
1369 explained = pointer_set_create ();
1370 if (pointer_set_insert (explained, decl))
1371 return true;
1373 sfk = special_function_p (decl);
1374 ctype = DECL_CONTEXT (decl);
1375 loc = input_location;
1376 input_location = DECL_SOURCE_LOCATION (decl);
1378 informed = false;
1379 if (LAMBDA_TYPE_P (ctype))
1381 informed = true;
1382 if (sfk == sfk_constructor)
1383 inform (DECL_SOURCE_LOCATION (decl),
1384 "a lambda closure type has a deleted default constructor");
1385 else if (sfk == sfk_copy_assignment)
1386 inform (DECL_SOURCE_LOCATION (decl),
1387 "a lambda closure type has a deleted copy assignment operator");
1388 else
1389 informed = false;
1391 else if (DECL_ARTIFICIAL (decl)
1392 && (sfk == sfk_copy_assignment
1393 || sfk == sfk_copy_constructor)
1394 && (type_has_user_declared_move_constructor (ctype)
1395 || type_has_user_declared_move_assign (ctype)))
1397 inform (0, "%q+#D is implicitly declared as deleted because %qT "
1398 "declares a move constructor or move assignment operator",
1399 decl, ctype);
1400 informed = true;
1402 if (!informed)
1404 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1405 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1406 tree scope = push_scope (ctype);
1407 inform (0, "%q+#D is implicitly deleted because the default "
1408 "definition would be ill-formed:", decl);
1409 pop_scope (scope);
1410 synthesized_method_walk (ctype, sfk, const_p,
1411 NULL, NULL, NULL, NULL, NULL, true);
1414 input_location = loc;
1415 return true;
1417 return false;
1420 /* DECL is a defaulted function which was declared constexpr. Explain why
1421 it can't be constexpr. */
1423 void
1424 explain_implicit_non_constexpr (tree decl)
1426 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1427 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1428 bool dummy;
1429 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1430 special_function_p (decl), const_p,
1431 NULL, NULL, NULL, &dummy, NULL, true);
1434 /* Implicitly declare the special function indicated by KIND, as a
1435 member of TYPE. For copy constructors and assignment operators,
1436 CONST_P indicates whether these functions should take a const
1437 reference argument or a non-const reference. Returns the
1438 FUNCTION_DECL for the implicitly declared function. */
1440 tree
1441 implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
1443 tree fn;
1444 tree parameter_types = void_list_node;
1445 tree return_type;
1446 tree fn_type;
1447 tree raises = empty_except_spec;
1448 tree rhs_parm_type = NULL_TREE;
1449 tree this_parm;
1450 tree name;
1451 HOST_WIDE_INT saved_processing_template_decl;
1452 bool deleted_p;
1453 bool trivial_p;
1454 bool constexpr_p;
1455 bool no_implicit_p;
1457 /* Because we create declarations for implicitly declared functions
1458 lazily, we may be creating the declaration for a member of TYPE
1459 while in some completely different context. However, TYPE will
1460 never be a dependent class (because we never want to do lookups
1461 for implicitly defined functions in a dependent class).
1462 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1463 because we only create clones for constructors and destructors
1464 when not in a template. */
1465 gcc_assert (!dependent_type_p (type));
1466 saved_processing_template_decl = processing_template_decl;
1467 processing_template_decl = 0;
1469 type = TYPE_MAIN_VARIANT (type);
1471 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1473 if (kind == sfk_destructor)
1474 /* See comment in check_special_function_return_type. */
1475 return_type = build_pointer_type (void_type_node);
1476 else
1477 return_type = build_pointer_type (type);
1479 else
1480 return_type = void_type_node;
1482 switch (kind)
1484 case sfk_destructor:
1485 /* Destructor. */
1486 name = constructor_name (type);
1487 break;
1489 case sfk_constructor:
1490 /* Default constructor. */
1491 name = constructor_name (type);
1492 break;
1494 case sfk_copy_constructor:
1495 case sfk_copy_assignment:
1496 case sfk_move_constructor:
1497 case sfk_move_assignment:
1499 bool move_p;
1500 if (kind == sfk_copy_assignment
1501 || kind == sfk_move_assignment)
1503 return_type = build_reference_type (type);
1504 name = ansi_assopname (NOP_EXPR);
1506 else
1507 name = constructor_name (type);
1509 if (const_p)
1510 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1511 else
1512 rhs_parm_type = type;
1513 move_p = (kind == sfk_move_assignment
1514 || kind == sfk_move_constructor);
1515 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1517 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1518 break;
1520 default:
1521 gcc_unreachable ();
1524 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1525 &deleted_p, &constexpr_p, &no_implicit_p, false);
1526 /* Don't bother marking a deleted constructor as constexpr. */
1527 if (deleted_p)
1528 constexpr_p = false;
1529 /* A trivial copy/move constructor is also a constexpr constructor. */
1530 else if (trivial_p && cxx_dialect >= cxx0x
1531 && (kind == sfk_copy_constructor
1532 || kind == sfk_move_constructor))
1533 gcc_assert (constexpr_p);
1535 if (!trivial_p && type_has_trivial_fn (type, kind))
1536 type_set_nontrivial_flag (type, kind);
1538 /* Create the function. */
1539 fn_type = build_method_type_directly (type, return_type, parameter_types);
1540 if (raises)
1541 fn_type = build_exception_variant (fn_type, raises);
1542 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1543 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1544 if (kind == sfk_constructor || kind == sfk_copy_constructor
1545 || kind == sfk_move_constructor)
1546 DECL_CONSTRUCTOR_P (fn) = 1;
1547 else if (kind == sfk_destructor)
1548 DECL_DESTRUCTOR_P (fn) = 1;
1549 else
1551 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1552 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1555 /* If pointers to member functions use the least significant bit to
1556 indicate whether a function is virtual, ensure a pointer
1557 to this function will have that bit clear. */
1558 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1559 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1560 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1562 /* Create the explicit arguments. */
1563 if (rhs_parm_type)
1565 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1566 want its type to be included in the mangled function
1567 name. */
1568 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1569 TREE_READONLY (decl) = 1;
1570 retrofit_lang_decl (decl);
1571 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
1572 DECL_ARGUMENTS (fn) = decl;
1574 /* Add the "this" parameter. */
1575 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1576 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1577 DECL_ARGUMENTS (fn) = this_parm;
1579 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1580 set_linkage_according_to_type (type, fn);
1581 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1582 DECL_IN_AGGR_P (fn) = 1;
1583 DECL_ARTIFICIAL (fn) = 1;
1584 DECL_DEFAULTED_FN (fn) = 1;
1585 if (cxx_dialect >= cxx0x)
1587 DECL_DELETED_FN (fn) = deleted_p;
1588 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
1590 FNDECL_SUPPRESS_IMPLICIT_DECL (fn) = no_implicit_p;
1591 DECL_EXTERNAL (fn) = true;
1592 DECL_NOT_REALLY_EXTERN (fn) = 1;
1593 DECL_DECLARED_INLINE_P (fn) = 1;
1594 gcc_assert (!TREE_USED (fn));
1596 /* Restore PROCESSING_TEMPLATE_DECL. */
1597 processing_template_decl = saved_processing_template_decl;
1599 return fn;
1602 /* Gives any errors about defaulted functions which need to be deferred
1603 until the containing class is complete. */
1605 void
1606 defaulted_late_check (tree fn)
1608 /* Complain about invalid signature for defaulted fn. */
1609 tree ctx = DECL_CONTEXT (fn);
1610 special_function_kind kind = special_function_p (fn);
1611 bool fn_const_p = (copy_fn_p (fn) == 2);
1612 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p);
1614 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1615 TREE_TYPE (TREE_TYPE (implicit_fn)))
1616 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1617 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
1619 error ("defaulted declaration %q+D", fn);
1620 error_at (DECL_SOURCE_LOCATION (fn),
1621 "does not match expected signature %qD", implicit_fn);
1624 /* 8.4.2/2: If it is explicitly defaulted on its first declaration, it is
1625 implicitly considered to have the same exception-specification as if
1626 it had been implicitly declared. */
1627 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1629 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
1630 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
1632 maybe_instantiate_noexcept (fn);
1633 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
1634 eh_spec, ce_normal))
1635 error ("function %q+D defaulted on its first declaration "
1636 "with an exception-specification that differs from "
1637 "the implicit declaration %q#D", fn, implicit_fn);
1639 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
1640 if (DECL_DECLARED_CONSTEXPR_P (implicit_fn))
1642 /* Hmm...should we do this for out-of-class too? Should it be OK to
1643 add constexpr later like inline, rather than requiring
1644 declarations to match? */
1645 DECL_DECLARED_CONSTEXPR_P (fn) = true;
1646 if (kind == sfk_constructor)
1647 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
1651 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
1652 && DECL_DECLARED_CONSTEXPR_P (fn))
1654 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
1656 error ("explicitly defaulted function %q+D cannot be declared "
1657 "as constexpr because the implicit declaration is not "
1658 "constexpr:", fn);
1659 explain_implicit_non_constexpr (fn);
1661 DECL_DECLARED_CONSTEXPR_P (fn) = false;
1664 if (DECL_DELETED_FN (implicit_fn))
1665 DECL_DELETED_FN (fn) = 1;
1668 /* Returns true iff FN can be explicitly defaulted, and gives any
1669 errors if defaulting FN is ill-formed. */
1671 bool
1672 defaultable_fn_check (tree fn)
1674 special_function_kind kind = sfk_none;
1676 if (template_parm_scope_p ())
1678 error ("a template cannot be defaulted");
1679 return false;
1682 if (DECL_CONSTRUCTOR_P (fn))
1684 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
1685 kind = sfk_constructor;
1686 else if (copy_fn_p (fn) > 0
1687 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
1688 == void_list_node))
1689 kind = sfk_copy_constructor;
1690 else if (move_fn_p (fn))
1691 kind = sfk_move_constructor;
1693 else if (DECL_DESTRUCTOR_P (fn))
1694 kind = sfk_destructor;
1695 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
1696 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
1698 if (copy_fn_p (fn))
1699 kind = sfk_copy_assignment;
1700 else if (move_fn_p (fn))
1701 kind = sfk_move_assignment;
1704 if (kind == sfk_none)
1706 error ("%qD cannot be defaulted", fn);
1707 return false;
1709 else
1711 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
1712 for (; t && t != void_list_node; t = TREE_CHAIN (t))
1713 if (TREE_PURPOSE (t))
1715 error ("defaulted function %q+D with default argument", fn);
1716 break;
1718 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
1719 /* Defer checking. */;
1720 else if (!processing_template_decl)
1721 defaulted_late_check (fn);
1723 return true;
1727 /* Add an implicit declaration to TYPE for the kind of function
1728 indicated by SFK. Return the FUNCTION_DECL for the new implicit
1729 declaration. */
1731 tree
1732 lazily_declare_fn (special_function_kind sfk, tree type)
1734 tree fn;
1735 /* Whether or not the argument has a const reference type. */
1736 bool const_p = false;
1738 switch (sfk)
1740 case sfk_constructor:
1741 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
1742 break;
1743 case sfk_copy_constructor:
1744 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
1745 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
1746 break;
1747 case sfk_move_constructor:
1748 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
1749 break;
1750 case sfk_copy_assignment:
1751 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
1752 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
1753 break;
1754 case sfk_move_assignment:
1755 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
1756 break;
1757 case sfk_destructor:
1758 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
1759 break;
1760 default:
1761 gcc_unreachable ();
1764 /* Declare the function. */
1765 fn = implicitly_declare_fn (sfk, type, const_p);
1767 /* [class.copy]/8 If the class definition declares a move constructor or
1768 move assignment operator, the implicitly declared copy constructor is
1769 defined as deleted.... */
1770 if ((sfk == sfk_copy_assignment
1771 || sfk == sfk_copy_constructor)
1772 && (type_has_user_declared_move_constructor (type)
1773 || type_has_user_declared_move_assign (type)))
1774 DECL_DELETED_FN (fn) = true;
1776 /* For move variants, rather than declare them as deleted we just
1777 don't declare them at all. */
1778 if (DECL_DELETED_FN (fn)
1779 && (sfk == sfk_move_constructor
1780 || sfk == sfk_move_assignment))
1781 return NULL_TREE;
1783 /* We also suppress implicit move if it would call a non-trivial copy. */
1784 if (FNDECL_SUPPRESS_IMPLICIT_DECL (fn))
1785 return NULL_TREE;
1787 /* A destructor may be virtual. */
1788 if (sfk == sfk_destructor
1789 || sfk == sfk_move_assignment
1790 || sfk == sfk_copy_assignment)
1791 check_for_override (fn, type);
1792 /* Add it to CLASSTYPE_METHOD_VEC. */
1793 add_method (type, fn, NULL_TREE);
1794 /* Add it to TYPE_METHODS. */
1795 if (sfk == sfk_destructor
1796 && DECL_VIRTUAL_P (fn)
1797 && abi_version_at_least (2))
1798 /* The ABI requires that a virtual destructor go at the end of the
1799 vtable. */
1800 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
1801 else
1803 /* G++ 3.2 put the implicit destructor at the *beginning* of the
1804 TYPE_METHODS list, which cause the destructor to be emitted
1805 in an incorrect location in the vtable. */
1806 if (warn_abi && sfk == sfk_destructor && DECL_VIRTUAL_P (fn))
1807 warning (OPT_Wabi, "vtable layout for class %qT may not be ABI-compliant"
1808 "and may change in a future version of GCC due to "
1809 "implicit virtual destructor",
1810 type);
1811 DECL_CHAIN (fn) = TYPE_METHODS (type);
1812 TYPE_METHODS (type) = fn;
1814 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
1815 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
1816 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
1817 /* Create appropriate clones. */
1818 clone_function_decl (fn, /*update_method_vec=*/true);
1820 return fn;
1823 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
1824 as there are artificial parms in FN. */
1826 tree
1827 skip_artificial_parms_for (const_tree fn, tree list)
1829 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1830 list = TREE_CHAIN (list);
1831 else
1832 return list;
1834 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1835 list = TREE_CHAIN (list);
1836 if (DECL_HAS_VTT_PARM_P (fn))
1837 list = TREE_CHAIN (list);
1838 return list;
1841 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
1842 artificial parms in FN. */
1845 num_artificial_parms_for (const_tree fn)
1847 int count = 0;
1849 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1850 count++;
1851 else
1852 return 0;
1854 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1855 count++;
1856 if (DECL_HAS_VTT_PARM_P (fn))
1857 count++;
1858 return count;
1862 #include "gt-cp-method.h"