2012-09-04 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / cp / method.c
blobc21ae152d6b2ab90edb1ac57bc30741ad513e112
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;
1228 push_deferring_access_checks (dk_no_deferred);
1230 scope = push_scope (ctype);
1232 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE|LOOKUP_DEFAULTED;
1234 complain = diag ? tf_warning_or_error : tf_none;
1236 if (const_p)
1237 quals = TYPE_QUAL_CONST;
1238 else
1239 quals = TYPE_UNQUALIFIED;
1240 argtype = NULL_TREE;
1242 for (binfo = TYPE_BINFO (ctype), i = 0;
1243 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1245 tree basetype = BINFO_TYPE (base_binfo);
1247 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1248 /* We'll handle virtual bases below. */
1249 continue;
1251 if (copy_arg_p)
1252 argtype = build_stub_type (basetype, quals, move_p);
1253 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1255 process_subob_fn (rval, move_p, spec_p, trivial_p, deleted_p,
1256 constexpr_p, no_implicit_p, diag, basetype);
1257 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1259 /* In a constructor we also need to check the subobject
1260 destructors for cleanup of partially constructed objects. */
1261 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1262 NULL_TREE, flags, complain);
1263 /* Note that we don't pass down trivial_p; the subobject
1264 destructors don't affect triviality of the constructor. Nor
1265 do they affect constexpr-ness (a constant expression doesn't
1266 throw) or exception-specification (a throw from one of the
1267 dtors would be a double-fault). */
1268 process_subob_fn (rval, false, NULL, NULL,
1269 deleted_p, NULL, NULL, false,
1270 basetype);
1273 if (check_vdtor && type_has_virtual_destructor (basetype))
1275 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1276 ptr_type_node, flags, complain);
1277 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1278 to have a null rval (no class-specific op delete). */
1279 if (rval && rval == error_mark_node && deleted_p)
1280 *deleted_p = true;
1281 check_vdtor = false;
1285 vbases = CLASSTYPE_VBASECLASSES (ctype);
1286 if (vbases == NULL)
1287 /* No virtual bases to worry about. */;
1288 else if (assign_p && move_p && no_implicit_p)
1290 /* Don't implicitly declare a defaulted move assignment if a virtual
1291 base has non-trivial move assignment, since moving the same base
1292 more than once is dangerous. */
1293 /* Should the spec be changed to allow vbases that only occur once? */
1294 FOR_EACH_VEC_ELT (tree, vbases, i, base_binfo)
1296 tree basetype = BINFO_TYPE (base_binfo);
1297 if (copy_arg_p)
1298 argtype = build_stub_type (basetype, quals, move_p);
1299 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1300 if (rval && rval != error_mark_node
1301 && move_fn_p (rval) && !trivial_fn_p (rval))
1303 *no_implicit_p = true;
1304 break;
1308 else if (!assign_p)
1310 if (constexpr_p)
1311 *constexpr_p = false;
1312 FOR_EACH_VEC_ELT (tree, vbases, i, base_binfo)
1314 tree basetype = BINFO_TYPE (base_binfo);
1315 if (copy_arg_p)
1316 argtype = build_stub_type (basetype, quals, move_p);
1317 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1319 process_subob_fn (rval, move_p, spec_p, trivial_p, deleted_p,
1320 constexpr_p, no_implicit_p, diag, basetype);
1321 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1323 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1324 NULL_TREE, flags, complain);
1325 process_subob_fn (rval, false, NULL, NULL,
1326 deleted_p, NULL, NULL, false,
1327 basetype);
1332 /* Now handle the non-static data members. */
1333 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1334 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1335 deleted_p, constexpr_p, no_implicit_p,
1336 diag, flags, complain);
1337 if (ctor_p)
1338 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1339 sfk_destructor, TYPE_UNQUALIFIED, false,
1340 false, false, NULL, NULL,
1341 deleted_p, NULL,
1342 NULL, false, flags, complain);
1344 pop_scope (scope);
1346 pop_deferring_access_checks ();
1347 --cp_unevaluated_operand;
1348 --c_inhibit_evaluation_warnings;
1351 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1352 return true; else return false. */
1354 bool
1355 maybe_explain_implicit_delete (tree decl)
1357 /* If decl is a clone, get the primary variant. */
1358 decl = DECL_ORIGIN (decl);
1359 gcc_assert (DECL_DELETED_FN (decl));
1360 if (DECL_DEFAULTED_FN (decl))
1362 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1363 static struct pointer_set_t *explained;
1365 special_function_kind sfk;
1366 location_t loc;
1367 bool informed;
1368 tree ctype;
1370 if (!explained)
1371 explained = pointer_set_create ();
1372 if (pointer_set_insert (explained, decl))
1373 return true;
1375 sfk = special_function_p (decl);
1376 ctype = DECL_CONTEXT (decl);
1377 loc = input_location;
1378 input_location = DECL_SOURCE_LOCATION (decl);
1380 informed = false;
1381 if (LAMBDA_TYPE_P (ctype))
1383 informed = true;
1384 if (sfk == sfk_constructor)
1385 inform (DECL_SOURCE_LOCATION (decl),
1386 "a lambda closure type has a deleted default constructor");
1387 else if (sfk == sfk_copy_assignment)
1388 inform (DECL_SOURCE_LOCATION (decl),
1389 "a lambda closure type has a deleted copy assignment operator");
1390 else
1391 informed = false;
1393 else if (DECL_ARTIFICIAL (decl)
1394 && (sfk == sfk_copy_assignment
1395 || sfk == sfk_copy_constructor)
1396 && (type_has_user_declared_move_constructor (ctype)
1397 || type_has_user_declared_move_assign (ctype)))
1399 inform (0, "%q+#D is implicitly declared as deleted because %qT "
1400 "declares a move constructor or move assignment operator",
1401 decl, ctype);
1402 informed = true;
1404 if (!informed)
1406 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1407 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1408 tree scope = push_scope (ctype);
1409 inform (0, "%q+#D is implicitly deleted because the default "
1410 "definition would be ill-formed:", decl);
1411 pop_scope (scope);
1412 synthesized_method_walk (ctype, sfk, const_p,
1413 NULL, NULL, NULL, NULL, NULL, true);
1416 input_location = loc;
1417 return true;
1419 return false;
1422 /* DECL is a defaulted function which was declared constexpr. Explain why
1423 it can't be constexpr. */
1425 void
1426 explain_implicit_non_constexpr (tree decl)
1428 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1429 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1430 bool dummy;
1431 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1432 special_function_p (decl), const_p,
1433 NULL, NULL, NULL, &dummy, NULL, true);
1436 /* Implicitly declare the special function indicated by KIND, as a
1437 member of TYPE. For copy constructors and assignment operators,
1438 CONST_P indicates whether these functions should take a const
1439 reference argument or a non-const reference. Returns the
1440 FUNCTION_DECL for the implicitly declared function. */
1442 tree
1443 implicitly_declare_fn (special_function_kind kind, tree type, bool const_p)
1445 tree fn;
1446 tree parameter_types = void_list_node;
1447 tree return_type;
1448 tree fn_type;
1449 tree raises = empty_except_spec;
1450 tree rhs_parm_type = NULL_TREE;
1451 tree this_parm;
1452 tree name;
1453 HOST_WIDE_INT saved_processing_template_decl;
1454 bool deleted_p;
1455 bool trivial_p;
1456 bool constexpr_p;
1457 bool no_implicit_p;
1459 /* Because we create declarations for implicitly declared functions
1460 lazily, we may be creating the declaration for a member of TYPE
1461 while in some completely different context. However, TYPE will
1462 never be a dependent class (because we never want to do lookups
1463 for implicitly defined functions in a dependent class).
1464 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1465 because we only create clones for constructors and destructors
1466 when not in a template. */
1467 gcc_assert (!dependent_type_p (type));
1468 saved_processing_template_decl = processing_template_decl;
1469 processing_template_decl = 0;
1471 type = TYPE_MAIN_VARIANT (type);
1473 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1475 if (kind == sfk_destructor)
1476 /* See comment in check_special_function_return_type. */
1477 return_type = build_pointer_type (void_type_node);
1478 else
1479 return_type = build_pointer_type (type);
1481 else
1482 return_type = void_type_node;
1484 switch (kind)
1486 case sfk_destructor:
1487 /* Destructor. */
1488 name = constructor_name (type);
1489 break;
1491 case sfk_constructor:
1492 /* Default constructor. */
1493 name = constructor_name (type);
1494 break;
1496 case sfk_copy_constructor:
1497 case sfk_copy_assignment:
1498 case sfk_move_constructor:
1499 case sfk_move_assignment:
1501 bool move_p;
1502 if (kind == sfk_copy_assignment
1503 || kind == sfk_move_assignment)
1505 return_type = build_reference_type (type);
1506 name = ansi_assopname (NOP_EXPR);
1508 else
1509 name = constructor_name (type);
1511 if (const_p)
1512 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1513 else
1514 rhs_parm_type = type;
1515 move_p = (kind == sfk_move_assignment
1516 || kind == sfk_move_constructor);
1517 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1519 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1520 break;
1522 default:
1523 gcc_unreachable ();
1526 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1527 &deleted_p, &constexpr_p, &no_implicit_p, false);
1528 /* Don't bother marking a deleted constructor as constexpr. */
1529 if (deleted_p)
1530 constexpr_p = false;
1531 /* A trivial copy/move constructor is also a constexpr constructor. */
1532 else if (trivial_p && cxx_dialect >= cxx0x
1533 && (kind == sfk_copy_constructor
1534 || kind == sfk_move_constructor))
1535 gcc_assert (constexpr_p);
1537 if (!trivial_p && type_has_trivial_fn (type, kind))
1538 type_set_nontrivial_flag (type, kind);
1540 /* Create the function. */
1541 fn_type = build_method_type_directly (type, return_type, parameter_types);
1542 if (raises)
1543 fn_type = build_exception_variant (fn_type, raises);
1544 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1545 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1546 if (kind == sfk_constructor || kind == sfk_copy_constructor
1547 || kind == sfk_move_constructor)
1548 DECL_CONSTRUCTOR_P (fn) = 1;
1549 else if (kind == sfk_destructor)
1550 DECL_DESTRUCTOR_P (fn) = 1;
1551 else
1553 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1554 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1557 /* If pointers to member functions use the least significant bit to
1558 indicate whether a function is virtual, ensure a pointer
1559 to this function will have that bit clear. */
1560 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1561 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1562 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1564 /* Create the explicit arguments. */
1565 if (rhs_parm_type)
1567 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1568 want its type to be included in the mangled function
1569 name. */
1570 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1571 TREE_READONLY (decl) = 1;
1572 retrofit_lang_decl (decl);
1573 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
1574 DECL_ARGUMENTS (fn) = decl;
1576 /* Add the "this" parameter. */
1577 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1578 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1579 DECL_ARGUMENTS (fn) = this_parm;
1581 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1582 set_linkage_according_to_type (type, fn);
1583 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1584 DECL_IN_AGGR_P (fn) = 1;
1585 DECL_ARTIFICIAL (fn) = 1;
1586 DECL_DEFAULTED_FN (fn) = 1;
1587 if (cxx_dialect >= cxx0x)
1589 DECL_DELETED_FN (fn) = deleted_p;
1590 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
1592 FNDECL_SUPPRESS_IMPLICIT_DECL (fn) = no_implicit_p;
1593 DECL_EXTERNAL (fn) = true;
1594 DECL_NOT_REALLY_EXTERN (fn) = 1;
1595 DECL_DECLARED_INLINE_P (fn) = 1;
1596 gcc_assert (!TREE_USED (fn));
1598 /* Restore PROCESSING_TEMPLATE_DECL. */
1599 processing_template_decl = saved_processing_template_decl;
1601 return fn;
1604 /* Gives any errors about defaulted functions which need to be deferred
1605 until the containing class is complete. */
1607 void
1608 defaulted_late_check (tree fn)
1610 /* Complain about invalid signature for defaulted fn. */
1611 tree ctx = DECL_CONTEXT (fn);
1612 special_function_kind kind = special_function_p (fn);
1613 bool fn_const_p = (copy_fn_p (fn) == 2);
1614 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p);
1616 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1617 TREE_TYPE (TREE_TYPE (implicit_fn)))
1618 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1619 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
1621 error ("defaulted declaration %q+D", fn);
1622 error_at (DECL_SOURCE_LOCATION (fn),
1623 "does not match expected signature %qD", implicit_fn);
1626 /* 8.4.2/2: If it is explicitly defaulted on its first declaration, it is
1627 implicitly considered to have the same exception-specification as if
1628 it had been implicitly declared. */
1629 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1631 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
1632 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
1634 maybe_instantiate_noexcept (fn);
1635 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
1636 eh_spec, ce_normal))
1637 error ("function %q+D defaulted on its first declaration "
1638 "with an exception-specification that differs from "
1639 "the implicit declaration %q#D", fn, implicit_fn);
1641 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
1642 if (DECL_DECLARED_CONSTEXPR_P (implicit_fn))
1644 /* Hmm...should we do this for out-of-class too? Should it be OK to
1645 add constexpr later like inline, rather than requiring
1646 declarations to match? */
1647 DECL_DECLARED_CONSTEXPR_P (fn) = true;
1648 if (kind == sfk_constructor)
1649 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
1653 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
1654 && DECL_DECLARED_CONSTEXPR_P (fn))
1656 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
1658 error ("explicitly defaulted function %q+D cannot be declared "
1659 "as constexpr because the implicit declaration is not "
1660 "constexpr:", fn);
1661 explain_implicit_non_constexpr (fn);
1663 DECL_DECLARED_CONSTEXPR_P (fn) = false;
1666 if (DECL_DELETED_FN (implicit_fn))
1667 DECL_DELETED_FN (fn) = 1;
1670 /* Returns true iff FN can be explicitly defaulted, and gives any
1671 errors if defaulting FN is ill-formed. */
1673 bool
1674 defaultable_fn_check (tree fn)
1676 special_function_kind kind = sfk_none;
1678 if (template_parm_scope_p ())
1680 error ("a template cannot be defaulted");
1681 return false;
1684 if (DECL_CONSTRUCTOR_P (fn))
1686 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
1687 kind = sfk_constructor;
1688 else if (copy_fn_p (fn) > 0
1689 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
1690 == void_list_node))
1691 kind = sfk_copy_constructor;
1692 else if (move_fn_p (fn))
1693 kind = sfk_move_constructor;
1695 else if (DECL_DESTRUCTOR_P (fn))
1696 kind = sfk_destructor;
1697 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
1698 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
1700 if (copy_fn_p (fn))
1701 kind = sfk_copy_assignment;
1702 else if (move_fn_p (fn))
1703 kind = sfk_move_assignment;
1706 if (kind == sfk_none)
1708 error ("%qD cannot be defaulted", fn);
1709 return false;
1711 else
1713 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
1714 for (; t && t != void_list_node; t = TREE_CHAIN (t))
1715 if (TREE_PURPOSE (t))
1717 error ("defaulted function %q+D with default argument", fn);
1718 break;
1720 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
1721 /* Defer checking. */;
1722 else if (!processing_template_decl)
1723 defaulted_late_check (fn);
1725 return true;
1729 /* Add an implicit declaration to TYPE for the kind of function
1730 indicated by SFK. Return the FUNCTION_DECL for the new implicit
1731 declaration. */
1733 tree
1734 lazily_declare_fn (special_function_kind sfk, tree type)
1736 tree fn;
1737 /* Whether or not the argument has a const reference type. */
1738 bool const_p = false;
1740 switch (sfk)
1742 case sfk_constructor:
1743 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
1744 break;
1745 case sfk_copy_constructor:
1746 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
1747 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
1748 break;
1749 case sfk_move_constructor:
1750 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
1751 break;
1752 case sfk_copy_assignment:
1753 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
1754 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
1755 break;
1756 case sfk_move_assignment:
1757 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
1758 break;
1759 case sfk_destructor:
1760 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
1761 break;
1762 default:
1763 gcc_unreachable ();
1766 /* Declare the function. */
1767 fn = implicitly_declare_fn (sfk, type, const_p);
1769 /* [class.copy]/8 If the class definition declares a move constructor or
1770 move assignment operator, the implicitly declared copy constructor is
1771 defined as deleted.... */
1772 if ((sfk == sfk_copy_assignment
1773 || sfk == sfk_copy_constructor)
1774 && (type_has_user_declared_move_constructor (type)
1775 || type_has_user_declared_move_assign (type)))
1776 DECL_DELETED_FN (fn) = true;
1778 /* For move variants, rather than declare them as deleted we just
1779 don't declare them at all. */
1780 if (DECL_DELETED_FN (fn)
1781 && (sfk == sfk_move_constructor
1782 || sfk == sfk_move_assignment))
1783 return NULL_TREE;
1785 /* We also suppress implicit move if it would call a non-trivial copy. */
1786 if (FNDECL_SUPPRESS_IMPLICIT_DECL (fn))
1787 return NULL_TREE;
1789 /* A destructor may be virtual. */
1790 if (sfk == sfk_destructor
1791 || sfk == sfk_move_assignment
1792 || sfk == sfk_copy_assignment)
1793 check_for_override (fn, type);
1794 /* Add it to CLASSTYPE_METHOD_VEC. */
1795 add_method (type, fn, NULL_TREE);
1796 /* Add it to TYPE_METHODS. */
1797 if (sfk == sfk_destructor
1798 && DECL_VIRTUAL_P (fn)
1799 && abi_version_at_least (2))
1800 /* The ABI requires that a virtual destructor go at the end of the
1801 vtable. */
1802 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
1803 else
1805 /* G++ 3.2 put the implicit destructor at the *beginning* of the
1806 TYPE_METHODS list, which cause the destructor to be emitted
1807 in an incorrect location in the vtable. */
1808 if (warn_abi && sfk == sfk_destructor && DECL_VIRTUAL_P (fn))
1809 warning (OPT_Wabi, "vtable layout for class %qT may not be ABI-compliant"
1810 "and may change in a future version of GCC due to "
1811 "implicit virtual destructor",
1812 type);
1813 DECL_CHAIN (fn) = TYPE_METHODS (type);
1814 TYPE_METHODS (type) = fn;
1816 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
1817 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
1818 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
1819 /* Create appropriate clones. */
1820 clone_function_decl (fn, /*update_method_vec=*/true);
1822 return fn;
1825 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
1826 as there are artificial parms in FN. */
1828 tree
1829 skip_artificial_parms_for (const_tree fn, tree list)
1831 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1832 list = TREE_CHAIN (list);
1833 else
1834 return list;
1836 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1837 list = TREE_CHAIN (list);
1838 if (DECL_HAS_VTT_PARM_P (fn))
1839 list = TREE_CHAIN (list);
1840 return list;
1843 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
1844 artificial parms in FN. */
1847 num_artificial_parms_for (const_tree fn)
1849 int count = 0;
1851 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1852 count++;
1853 else
1854 return 0;
1856 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1857 count++;
1858 if (DECL_HAS_VTT_PARM_P (fn))
1859 count++;
1860 return count;
1864 #include "gt-cp-method.h"