* doc/install.texi (Downloading the Source): Update references to
[official-gcc.git] / gcc / cp / method.c
blobd13a0cf3a9eec1db4e43e79a675eb4c50e98c225
1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* Handle method declarations. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "flags.h"
31 #include "toplev.h"
32 #include "tm_p.h"
33 #include "target.h"
34 #include "common/common-target.h"
35 #include "diagnostic.h"
36 #include "cgraph.h"
37 #include "gimple.h"
39 /* Various flags to control the mangling process. */
41 enum mangling_flags
43 /* No flags. */
44 mf_none = 0,
45 /* The thing we are presently mangling is part of a template type,
46 rather than a fully instantiated type. Therefore, we may see
47 complex expressions where we would normally expect to see a
48 simple integer constant. */
49 mf_maybe_uninstantiated = 1,
50 /* When mangling a numeric value, use the form `_XX_' (instead of
51 just `XX') if the value has more than one digit. */
52 mf_use_underscores_around_value = 2
55 typedef enum mangling_flags mangling_flags;
57 static void do_build_copy_assign (tree);
58 static void do_build_copy_constructor (tree);
59 static tree make_alias_for_thunk (tree);
61 /* Called once to initialize method.c. */
63 void
64 init_method (void)
66 init_mangle ();
69 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
70 indicates whether it is a this or result adjusting thunk.
71 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
72 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
73 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
74 adjusting thunks, we scale it to a byte offset. For covariant
75 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
76 the returned thunk with finish_thunk. */
78 tree
79 make_thunk (tree function, bool this_adjusting,
80 tree fixed_offset, tree virtual_offset)
82 HOST_WIDE_INT d;
83 tree thunk;
85 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
86 /* We can have this thunks to covariant thunks, but not vice versa. */
87 gcc_assert (!DECL_THIS_THUNK_P (function));
88 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
90 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
91 if (this_adjusting && virtual_offset)
92 virtual_offset
93 = size_binop (MULT_EXPR,
94 virtual_offset,
95 convert (ssizetype,
96 TYPE_SIZE_UNIT (vtable_entry_type)));
98 d = tree_low_cst (fixed_offset, 0);
100 /* See if we already have the thunk in question. For this_adjusting
101 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
102 will be a BINFO. */
103 for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
104 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
105 && THUNK_FIXED_OFFSET (thunk) == d
106 && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
107 && (!virtual_offset
108 || (this_adjusting
109 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
110 virtual_offset)
111 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
112 return thunk;
114 /* All thunks must be created before FUNCTION is actually emitted;
115 the ABI requires that all thunks be emitted together with the
116 function to which they transfer control. */
117 gcc_assert (!TREE_ASM_WRITTEN (function));
118 /* Likewise, we can only be adding thunks to a function declared in
119 the class currently being laid out. */
120 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
121 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
123 thunk = build_decl (DECL_SOURCE_LOCATION (function),
124 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
125 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
126 cxx_dup_lang_specific_decl (thunk);
127 DECL_VIRTUAL_P (thunk) = true;
128 SET_DECL_THUNKS (thunk, NULL_TREE);
130 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
131 TREE_READONLY (thunk) = TREE_READONLY (function);
132 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
133 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
134 SET_DECL_THUNK_P (thunk, this_adjusting);
135 THUNK_TARGET (thunk) = function;
136 THUNK_FIXED_OFFSET (thunk) = d;
137 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
138 THUNK_ALIAS (thunk) = NULL_TREE;
140 DECL_INTERFACE_KNOWN (thunk) = 1;
141 DECL_NOT_REALLY_EXTERN (thunk) = 1;
142 DECL_COMDAT (thunk) = DECL_COMDAT (function);
143 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
144 /* The thunk itself is not a constructor or destructor, even if
145 the thing it is thunking to is. */
146 DECL_DESTRUCTOR_P (thunk) = 0;
147 DECL_CONSTRUCTOR_P (thunk) = 0;
148 DECL_EXTERNAL (thunk) = 1;
149 DECL_ARTIFICIAL (thunk) = 1;
150 /* The THUNK is not a pending inline, even if the FUNCTION is. */
151 DECL_PENDING_INLINE_P (thunk) = 0;
152 DECL_DECLARED_INLINE_P (thunk) = 0;
153 /* Nor is it a template instantiation. */
154 DECL_USE_TEMPLATE (thunk) = 0;
155 DECL_TEMPLATE_INFO (thunk) = NULL;
157 /* Add it to the list of thunks associated with FUNCTION. */
158 DECL_CHAIN (thunk) = DECL_THUNKS (function);
159 SET_DECL_THUNKS (function, thunk);
161 return thunk;
164 /* Finish THUNK, a thunk decl. */
166 void
167 finish_thunk (tree thunk)
169 tree function, name;
170 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
171 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
173 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
174 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
175 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
176 function = THUNK_TARGET (thunk);
177 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
178 fixed_offset, virtual_offset);
180 /* We can end up with declarations of (logically) different
181 covariant thunks, that do identical adjustments. The two thunks
182 will be adjusting between within different hierarchies, which
183 happen to have the same layout. We must nullify one of them to
184 refer to the other. */
185 if (DECL_RESULT_THUNK_P (thunk))
187 tree cov_probe;
189 for (cov_probe = DECL_THUNKS (function);
190 cov_probe; cov_probe = DECL_CHAIN (cov_probe))
191 if (DECL_NAME (cov_probe) == name)
193 gcc_assert (!DECL_THUNKS (thunk));
194 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
195 ? THUNK_ALIAS (cov_probe) : cov_probe);
196 break;
200 DECL_NAME (thunk) = name;
201 SET_DECL_ASSEMBLER_NAME (thunk, name);
204 static GTY (()) int thunk_labelno;
206 /* Create a static alias to target. */
208 tree
209 make_alias_for (tree target, tree newid)
211 tree alias = build_decl (DECL_SOURCE_LOCATION (target),
212 TREE_CODE (target), newid, TREE_TYPE (target));
213 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
214 cxx_dup_lang_specific_decl (alias);
215 DECL_CONTEXT (alias) = NULL;
216 TREE_READONLY (alias) = TREE_READONLY (target);
217 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
218 TREE_PUBLIC (alias) = 0;
219 DECL_INTERFACE_KNOWN (alias) = 1;
220 if (DECL_LANG_SPECIFIC (alias))
222 DECL_NOT_REALLY_EXTERN (alias) = 1;
223 DECL_USE_TEMPLATE (alias) = 0;
224 DECL_TEMPLATE_INFO (alias) = NULL;
226 DECL_EXTERNAL (alias) = 0;
227 DECL_ARTIFICIAL (alias) = 1;
228 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
229 if (TREE_CODE (alias) == FUNCTION_DECL)
231 DECL_SAVED_FUNCTION_DATA (alias) = NULL;
232 DECL_DESTRUCTOR_P (alias) = 0;
233 DECL_CONSTRUCTOR_P (alias) = 0;
234 DECL_PENDING_INLINE_P (alias) = 0;
235 DECL_DECLARED_INLINE_P (alias) = 0;
236 DECL_INITIAL (alias) = error_mark_node;
237 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
239 else
240 TREE_STATIC (alias) = 1;
241 TREE_ADDRESSABLE (alias) = 1;
242 TREE_USED (alias) = 1;
243 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
244 return alias;
247 static tree
248 make_alias_for_thunk (tree function)
250 tree alias;
251 char buf[256];
253 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
254 thunk_labelno++;
256 alias = make_alias_for (function, get_identifier (buf));
258 if (!flag_syntax_only)
260 struct cgraph_node *funcn, *aliasn;
261 funcn = cgraph_get_node (function);
262 gcc_checking_assert (funcn);
263 aliasn = cgraph_same_body_alias (funcn, alias, function);
264 DECL_ASSEMBLER_NAME (function);
265 gcc_assert (aliasn != NULL);
268 return alias;
271 /* Emit the definition of a C++ multiple inheritance or covariant
272 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
273 immediately. */
275 void
276 use_thunk (tree thunk_fndecl, bool emit_p)
278 tree a, t, function, alias;
279 tree virtual_offset;
280 HOST_WIDE_INT fixed_offset, virtual_value;
281 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
282 struct cgraph_node *funcn, *thunk_node;
284 /* We should have called finish_thunk to give it a name. */
285 gcc_assert (DECL_NAME (thunk_fndecl));
287 /* We should never be using an alias, always refer to the
288 aliased thunk. */
289 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
291 if (TREE_ASM_WRITTEN (thunk_fndecl))
292 return;
294 function = THUNK_TARGET (thunk_fndecl);
295 if (DECL_RESULT (thunk_fndecl))
296 /* We already turned this thunk into an ordinary function.
297 There's no need to process this thunk again. */
298 return;
300 if (DECL_THUNK_P (function))
301 /* The target is itself a thunk, process it now. */
302 use_thunk (function, emit_p);
304 /* Thunks are always addressable; they only appear in vtables. */
305 TREE_ADDRESSABLE (thunk_fndecl) = 1;
307 /* Figure out what function is being thunked to. It's referenced in
308 this translation unit. */
309 TREE_ADDRESSABLE (function) = 1;
310 mark_used (function);
311 if (!emit_p)
312 return;
314 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
315 alias = make_alias_for_thunk (function);
316 else
317 alias = function;
319 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
320 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
322 if (virtual_offset)
324 if (!this_adjusting)
325 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
326 virtual_value = tree_low_cst (virtual_offset, /*pos=*/0);
327 gcc_assert (virtual_value);
329 else
330 virtual_value = 0;
332 /* And, if we need to emit the thunk, it's used. */
333 mark_used (thunk_fndecl);
334 /* This thunk is actually defined. */
335 DECL_EXTERNAL (thunk_fndecl) = 0;
336 /* The linkage of the function may have changed. FIXME in linkage
337 rewrite. */
338 gcc_assert (DECL_INTERFACE_KNOWN (function));
339 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
340 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
341 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
342 = DECL_VISIBILITY_SPECIFIED (function);
343 DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
344 DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
346 if (flag_syntax_only)
348 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
349 return;
352 push_to_top_level ();
354 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
355 && targetm_common.have_named_sections)
357 resolve_unique_section (function, 0, flag_function_sections);
359 if (DECL_SECTION_NAME (function) != NULL && DECL_ONE_ONLY (function))
361 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
363 /* Output the thunk into the same section as function. */
364 DECL_SECTION_NAME (thunk_fndecl) = DECL_SECTION_NAME (function);
368 /* Set up cloned argument trees for the thunk. */
369 t = NULL_TREE;
370 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
372 tree x = copy_node (a);
373 DECL_CHAIN (x) = t;
374 DECL_CONTEXT (x) = thunk_fndecl;
375 SET_DECL_RTL (x, NULL);
376 DECL_HAS_VALUE_EXPR_P (x) = 0;
377 TREE_ADDRESSABLE (x) = 0;
378 t = x;
380 a = nreverse (t);
381 DECL_ARGUMENTS (thunk_fndecl) = a;
382 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
383 funcn = cgraph_get_node (function);
384 gcc_checking_assert (funcn);
385 thunk_node = cgraph_add_thunk (funcn, thunk_fndecl, function,
386 this_adjusting, fixed_offset, virtual_value,
387 virtual_offset, alias);
388 if (DECL_ONE_ONLY (function))
389 symtab_add_to_same_comdat_group ((symtab_node) thunk_node,
390 (symtab_node) funcn);
392 if (!this_adjusting
393 || !targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
394 virtual_value, alias))
396 /* If this is a covariant thunk, or we don't have the necessary
397 code for efficient thunks, generate a thunk function that
398 just makes a call to the real function. Unfortunately, this
399 doesn't work for varargs. */
401 if (varargs_function_p (function))
402 error ("generic thunk code fails for method %q#D which uses %<...%>",
403 function);
406 pop_from_top_level ();
409 /* Code for synthesizing methods which have default semantics defined. */
411 /* True iff CTYPE has a trivial SFK. */
413 static bool
414 type_has_trivial_fn (tree ctype, special_function_kind sfk)
416 switch (sfk)
418 case sfk_constructor:
419 return !TYPE_HAS_COMPLEX_DFLT (ctype);
420 case sfk_copy_constructor:
421 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
422 case sfk_move_constructor:
423 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
424 case sfk_copy_assignment:
425 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
426 case sfk_move_assignment:
427 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
428 case sfk_destructor:
429 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
430 case sfk_inheriting_constructor:
431 return false;
432 default:
433 gcc_unreachable ();
437 /* Note that CTYPE has a non-trivial SFK even though we previously thought
438 it was trivial. */
440 static void
441 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
443 switch (sfk)
445 case sfk_constructor:
446 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
447 return;
448 case sfk_copy_constructor:
449 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
450 return;
451 case sfk_move_constructor:
452 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
453 return;
454 case sfk_copy_assignment:
455 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
456 return;
457 case sfk_move_assignment:
458 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
459 return;
460 case sfk_destructor:
461 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
462 return;
463 case sfk_inheriting_constructor:
464 default:
465 gcc_unreachable ();
469 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
471 bool
472 trivial_fn_p (tree fn)
474 if (!DECL_DEFAULTED_FN (fn))
475 return false;
477 /* If fn is a clone, get the primary variant. */
478 fn = DECL_ORIGIN (fn);
479 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
482 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
483 given the parameter or parameters PARM, possibly inherited constructor
484 base INH, or move flag MOVE_P. */
486 static tree
487 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
488 tree member_init_list)
490 tree init;
491 if (inh)
493 /* An inheriting constructor only has a mem-initializer for
494 the base it inherits from. */
495 if (BINFO_TYPE (binfo) != inh)
496 return member_init_list;
498 tree *p = &init;
499 init = NULL_TREE;
500 for (; parm; parm = DECL_CHAIN (parm))
502 tree exp = convert_from_reference (parm);
503 if (TREE_CODE (TREE_TYPE (parm)) != REFERENCE_TYPE)
504 exp = move (exp);
505 *p = build_tree_list (NULL_TREE, exp);
506 p = &TREE_CHAIN (*p);
509 else
511 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
512 tf_warning_or_error);
513 if (move_p)
514 init = move (init);
515 init = build_tree_list (NULL_TREE, init);
517 return tree_cons (binfo, init, member_init_list);
520 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
521 constructor. */
523 static void
524 do_build_copy_constructor (tree fndecl)
526 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
527 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
528 bool trivial = trivial_fn_p (fndecl);
529 tree inh = DECL_INHERITED_CTOR_BASE (fndecl);
531 if (!inh)
532 parm = convert_from_reference (parm);
534 if (trivial
535 && is_empty_class (current_class_type))
536 /* Don't copy the padding byte; it might not have been allocated
537 if *this is a base subobject. */;
538 else if (trivial)
540 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
541 finish_expr_stmt (t);
543 else
545 tree fields = TYPE_FIELDS (current_class_type);
546 tree member_init_list = NULL_TREE;
547 int cvquals = cp_type_quals (TREE_TYPE (parm));
548 int i;
549 tree binfo, base_binfo;
550 tree init;
551 vec<tree, va_gc> *vbases;
553 /* Initialize all the base-classes with the parameter converted
554 to their type so that we get their copy constructor and not
555 another constructor that takes current_class_type. We must
556 deal with the binfo's directly as a direct base might be
557 inaccessible due to ambiguity. */
558 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
559 vec_safe_iterate (vbases, i, &binfo); i++)
561 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
562 member_init_list);
565 for (binfo = TYPE_BINFO (current_class_type), i = 0;
566 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
568 if (BINFO_VIRTUAL_P (base_binfo))
569 continue;
570 member_init_list = add_one_base_init (base_binfo, parm, move_p,
571 inh, member_init_list);
574 for (; fields; fields = DECL_CHAIN (fields))
576 tree field = fields;
577 tree expr_type;
579 if (TREE_CODE (field) != FIELD_DECL)
580 continue;
581 if (inh)
582 continue;
584 expr_type = TREE_TYPE (field);
585 if (DECL_NAME (field))
587 if (VFIELD_NAME_P (DECL_NAME (field)))
588 continue;
590 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
591 /* Just use the field; anonymous types can't have
592 nontrivial copy ctors or assignment ops or this
593 function would be deleted. */;
594 else
595 continue;
597 /* Compute the type of "init->field". If the copy-constructor
598 parameter is, for example, "const S&", and the type of
599 the field is "T", then the type will usually be "const
600 T". (There are no cv-qualified variants of reference
601 types.) */
602 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
604 int quals = cvquals;
606 if (DECL_MUTABLE_P (field))
607 quals &= ~TYPE_QUAL_CONST;
608 quals |= cp_type_quals (expr_type);
609 expr_type = cp_build_qualified_type (expr_type, quals);
612 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
613 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE)
614 init = move (init);
615 init = build_tree_list (NULL_TREE, init);
617 member_init_list = tree_cons (field, init, member_init_list);
619 finish_mem_initializers (member_init_list);
623 static void
624 do_build_copy_assign (tree fndecl)
626 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
627 tree compound_stmt;
628 bool move_p = move_fn_p (fndecl);
629 bool trivial = trivial_fn_p (fndecl);
630 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
632 compound_stmt = begin_compound_stmt (0);
633 parm = convert_from_reference (parm);
635 if (trivial
636 && is_empty_class (current_class_type))
637 /* Don't copy the padding byte; it might not have been allocated
638 if *this is a base subobject. */;
639 else if (trivial)
641 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
642 finish_expr_stmt (t);
644 else
646 tree fields;
647 int cvquals = cp_type_quals (TREE_TYPE (parm));
648 int i;
649 tree binfo, base_binfo;
651 /* Assign to each of the direct base classes. */
652 for (binfo = TYPE_BINFO (current_class_type), i = 0;
653 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
655 tree converted_parm;
656 vec<tree, va_gc> *parmvec;
658 /* We must convert PARM directly to the base class
659 explicitly since the base class may be ambiguous. */
660 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
661 tf_warning_or_error);
662 if (move_p)
663 converted_parm = move (converted_parm);
664 /* Call the base class assignment operator. */
665 parmvec = make_tree_vector_single (converted_parm);
666 finish_expr_stmt
667 (build_special_member_call (current_class_ref,
668 ansi_assopname (NOP_EXPR),
669 &parmvec,
670 base_binfo,
671 flags,
672 tf_warning_or_error));
673 release_tree_vector (parmvec);
676 /* Assign to each of the non-static data members. */
677 for (fields = TYPE_FIELDS (current_class_type);
678 fields;
679 fields = DECL_CHAIN (fields))
681 tree comp = current_class_ref;
682 tree init = parm;
683 tree field = fields;
684 tree expr_type;
685 int quals;
687 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
688 continue;
690 expr_type = TREE_TYPE (field);
692 if (CP_TYPE_CONST_P (expr_type))
694 error ("non-static const member %q#D, can%'t use default "
695 "assignment operator", field);
696 continue;
698 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
700 error ("non-static reference member %q#D, can%'t use "
701 "default assignment operator", field);
702 continue;
705 if (DECL_NAME (field))
707 if (VFIELD_NAME_P (DECL_NAME (field)))
708 continue;
710 else if (ANON_AGGR_TYPE_P (expr_type)
711 && TYPE_FIELDS (expr_type) != NULL_TREE)
712 /* Just use the field; anonymous types can't have
713 nontrivial copy ctors or assignment ops or this
714 function would be deleted. */;
715 else
716 continue;
718 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
720 /* Compute the type of init->field */
721 quals = cvquals;
722 if (DECL_MUTABLE_P (field))
723 quals &= ~TYPE_QUAL_CONST;
724 expr_type = cp_build_qualified_type (expr_type, quals);
726 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
727 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE)
728 init = move (init);
730 if (DECL_NAME (field))
731 init = cp_build_modify_expr (comp, NOP_EXPR, init,
732 tf_warning_or_error);
733 else
734 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
735 finish_expr_stmt (init);
738 finish_return_stmt (current_class_ref);
739 finish_compound_stmt (compound_stmt);
742 /* Synthesize FNDECL, a non-static member function. */
744 void
745 synthesize_method (tree fndecl)
747 bool nested = (current_function_decl != NULL_TREE);
748 tree context = decl_function_context (fndecl);
749 bool need_body = true;
750 tree stmt;
751 location_t save_input_location = input_location;
752 int error_count = errorcount;
753 int warning_count = warningcount;
755 /* Reset the source location, we might have been previously
756 deferred, and thus have saved where we were first needed. */
757 DECL_SOURCE_LOCATION (fndecl)
758 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
760 /* If we've been asked to synthesize a clone, just synthesize the
761 cloned function instead. Doing so will automatically fill in the
762 body for the clone. */
763 if (DECL_CLONED_FUNCTION_P (fndecl))
764 fndecl = DECL_CLONED_FUNCTION (fndecl);
766 /* We may be in the middle of deferred access check. Disable
767 it now. */
768 push_deferring_access_checks (dk_no_deferred);
770 if (! context)
771 push_to_top_level ();
772 else if (nested)
773 push_function_context ();
775 input_location = DECL_SOURCE_LOCATION (fndecl);
777 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
778 stmt = begin_function_body ();
780 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
782 do_build_copy_assign (fndecl);
783 need_body = false;
785 else if (DECL_CONSTRUCTOR_P (fndecl))
787 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
788 if (arg_chain != void_list_node)
789 do_build_copy_constructor (fndecl);
790 else
791 finish_mem_initializers (NULL_TREE);
794 /* If we haven't yet generated the body of the function, just
795 generate an empty compound statement. */
796 if (need_body)
798 tree compound_stmt;
799 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
800 finish_compound_stmt (compound_stmt);
803 finish_function_body (stmt);
804 expand_or_defer_fn (finish_function (0));
806 input_location = save_input_location;
808 if (! context)
809 pop_from_top_level ();
810 else if (nested)
811 pop_function_context ();
813 pop_deferring_access_checks ();
815 if (error_count != errorcount || warning_count != warningcount)
816 inform (input_location, "synthesized method %qD first required here ",
817 fndecl);
820 /* Build a reference to type TYPE with cv-quals QUALS, which is an
821 rvalue if RVALUE is true. */
823 static tree
824 build_stub_type (tree type, int quals, bool rvalue)
826 tree argtype = cp_build_qualified_type (type, quals);
827 return cp_build_reference_type (argtype, rvalue);
830 /* Build a dummy glvalue from dereferencing a dummy reference of type
831 REFTYPE. */
833 static tree
834 build_stub_object (tree reftype)
836 tree stub = build1 (NOP_EXPR, reftype, integer_one_node);
837 return convert_from_reference (stub);
840 /* Determine which function will be called when looking up NAME in TYPE,
841 called with a single ARGTYPE argument, or no argument if ARGTYPE is
842 null. FLAGS and COMPLAIN are as for build_new_method_call.
844 Returns a FUNCTION_DECL if all is well.
845 Returns NULL_TREE if overload resolution failed.
846 Returns error_mark_node if the chosen function cannot be called. */
848 static tree
849 locate_fn_flags (tree type, tree name, tree argtype, int flags,
850 tsubst_flags_t complain)
852 tree ob, fn, fns, binfo, rval;
853 vec<tree, va_gc> *args;
855 if (TYPE_P (type))
856 binfo = TYPE_BINFO (type);
857 else
859 binfo = type;
860 type = BINFO_TYPE (binfo);
863 ob = build_stub_object (cp_build_reference_type (type, false));
864 args = make_tree_vector ();
865 if (argtype)
867 if (TREE_CODE (argtype) == TREE_LIST)
869 for (tree elt = argtype; elt != void_list_node;
870 elt = TREE_CHAIN (elt))
872 tree type = TREE_VALUE (elt);
873 if (TREE_CODE (type) != REFERENCE_TYPE)
874 type = cp_build_reference_type (type, /*rval*/true);
875 tree arg = build_stub_object (type);
876 vec_safe_push (args, arg);
879 else
881 tree arg = build_stub_object (argtype);
882 args->quick_push (arg);
886 fns = lookup_fnfields (binfo, name, 0);
887 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
889 release_tree_vector (args);
890 if (fn && rval == error_mark_node)
891 return rval;
892 else
893 return fn;
896 /* Locate the dtor of TYPE. */
898 tree
899 get_dtor (tree type, tsubst_flags_t complain)
901 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
902 LOOKUP_NORMAL, complain);
903 if (fn == error_mark_node)
904 return NULL_TREE;
905 return fn;
908 /* Locate the default ctor of TYPE. */
910 tree
911 locate_ctor (tree type)
913 tree fn;
915 push_deferring_access_checks (dk_no_check);
916 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
917 LOOKUP_SPECULATIVE, tf_none);
918 pop_deferring_access_checks ();
919 if (fn == error_mark_node)
920 return NULL_TREE;
921 return fn;
924 /* Likewise, but give any appropriate errors. */
926 tree
927 get_default_ctor (tree type)
929 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
930 LOOKUP_NORMAL, tf_warning_or_error);
931 if (fn == error_mark_node)
932 return NULL_TREE;
933 return fn;
936 /* Locate the copy ctor of TYPE. */
938 tree
939 get_copy_ctor (tree type, tsubst_flags_t complain)
941 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
942 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
943 tree argtype = build_stub_type (type, quals, false);
944 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
945 LOOKUP_NORMAL, complain);
946 if (fn == error_mark_node)
947 return NULL_TREE;
948 return fn;
951 /* Locate the copy assignment operator of TYPE. */
953 tree
954 get_copy_assign (tree type)
956 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
957 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
958 tree argtype = build_stub_type (type, quals, false);
959 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
960 LOOKUP_NORMAL, tf_warning_or_error);
961 if (fn == error_mark_node)
962 return NULL_TREE;
963 return fn;
966 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
967 DELETED_P or give an error message MSG with argument ARG. */
969 static void
970 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
971 bool *deleted_p, bool *constexpr_p,
972 bool diag, tree arg)
974 if (!fn || fn == error_mark_node)
975 goto bad;
977 if (spec_p)
979 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
980 *spec_p = merge_exception_specifiers (*spec_p, raises, fn);
983 if (!trivial_fn_p (fn))
985 if (trivial_p)
986 *trivial_p = false;
987 if (TREE_CODE (arg) == FIELD_DECL
988 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
990 if (deleted_p)
991 *deleted_p = true;
992 if (diag)
993 error ("union member %q+D with non-trivial %qD", arg, fn);
997 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
999 *constexpr_p = false;
1000 if (diag)
1002 inform (0, "defaulted constructor calls non-constexpr "
1003 "%q+D", fn);
1004 explain_invalid_constexpr_fn (fn);
1008 return;
1010 bad:
1011 if (deleted_p)
1012 *deleted_p = true;
1015 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1016 aggregates. */
1018 static void
1019 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1020 int quals, bool copy_arg_p, bool move_p,
1021 bool assign_p, tree *spec_p, bool *trivial_p,
1022 bool *deleted_p, bool *constexpr_p,
1023 bool diag, int flags, tsubst_flags_t complain)
1025 tree field;
1026 for (field = fields; field; field = DECL_CHAIN (field))
1028 tree mem_type, argtype, rval;
1030 if (TREE_CODE (field) != FIELD_DECL
1031 || DECL_ARTIFICIAL (field))
1032 continue;
1034 mem_type = strip_array_types (TREE_TYPE (field));
1035 if (assign_p)
1037 bool bad = true;
1038 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1040 if (diag)
1041 error ("non-static const member %q#D, can%'t use default "
1042 "assignment operator", field);
1044 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1046 if (diag)
1047 error ("non-static reference member %q#D, can%'t use "
1048 "default assignment operator", field);
1050 else
1051 bad = false;
1053 if (bad && deleted_p)
1054 *deleted_p = true;
1056 else if (sfk == sfk_constructor)
1058 bool bad;
1060 if (DECL_INITIAL (field))
1062 if (diag && DECL_INITIAL (field) == error_mark_node)
1063 inform (0, "initializer for %q+#D is invalid", field);
1064 if (trivial_p)
1065 *trivial_p = false;
1066 #if 0
1067 /* Core 1351: If the field has an NSDMI that could throw, the
1068 default constructor is noexcept(false). FIXME this is
1069 broken by deferred parsing and 1360 saying we can't lazily
1070 declare a non-trivial default constructor. Also this
1071 needs to do deferred instantiation. Disable until the
1072 conflict between 1351 and 1360 is resolved. */
1073 if (spec_p && !expr_noexcept_p (DECL_INITIAL (field), complain))
1074 *spec_p = noexcept_false_spec;
1075 #endif
1077 /* Don't do the normal processing. */
1078 continue;
1081 bad = false;
1082 if (CP_TYPE_CONST_P (mem_type)
1083 && default_init_uninitialized_part (mem_type))
1085 if (diag)
1086 error ("uninitialized non-static const member %q#D",
1087 field);
1088 bad = true;
1090 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1092 if (diag)
1093 error ("uninitialized non-static reference member %q#D",
1094 field);
1095 bad = true;
1098 if (bad && deleted_p)
1099 *deleted_p = true;
1101 /* For an implicitly-defined default constructor to be constexpr,
1102 every member must have a user-provided default constructor or
1103 an explicit initializer. */
1104 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1105 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1107 *constexpr_p = false;
1108 if (diag)
1109 inform (0, "defaulted default constructor does not "
1110 "initialize %q+#D", field);
1114 if (!CLASS_TYPE_P (mem_type))
1115 continue;
1117 if (ANON_AGGR_TYPE_P (mem_type))
1119 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1120 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1121 deleted_p, constexpr_p,
1122 diag, flags, complain);
1123 continue;
1126 if (copy_arg_p)
1128 int mem_quals = cp_type_quals (mem_type) | quals;
1129 if (DECL_MUTABLE_P (field))
1130 mem_quals &= ~TYPE_QUAL_CONST;
1131 argtype = build_stub_type (mem_type, mem_quals, move_p);
1133 else
1134 argtype = NULL_TREE;
1136 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1138 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1139 constexpr_p, diag, field);
1143 /* The caller wants to generate an implicit declaration of SFK for CTYPE
1144 which is const if relevant and CONST_P is set. If spec_p, trivial_p and
1145 deleted_p are non-null, set their referent appropriately. If diag is
1146 true, we're either being called from maybe_explain_implicit_delete to
1147 give errors, or if constexpr_p is non-null, from
1148 explain_invalid_constexpr_fn. */
1150 static void
1151 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1152 tree *spec_p, bool *trivial_p, bool *deleted_p,
1153 bool *constexpr_p, bool diag,
1154 tree inherited_base, tree inherited_parms)
1156 tree binfo, base_binfo, scope, fnname, rval, argtype;
1157 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1158 vec<tree, va_gc> *vbases;
1159 int i, quals, flags;
1160 tsubst_flags_t complain;
1161 bool ctor_p;
1163 if (spec_p)
1164 *spec_p = (cxx_dialect >= cxx0x ? noexcept_true_spec : empty_except_spec);
1166 if (deleted_p)
1168 /* "The closure type associated with a lambda-expression has a deleted
1169 default constructor and a deleted copy assignment operator."
1170 This is diagnosed in maybe_explain_implicit_delete. */
1171 if (LAMBDA_TYPE_P (ctype)
1172 && (sfk == sfk_constructor
1173 || sfk == sfk_copy_assignment))
1175 *deleted_p = true;
1176 return;
1179 *deleted_p = false;
1182 ctor_p = false;
1183 assign_p = false;
1184 check_vdtor = false;
1185 switch (sfk)
1187 case sfk_move_assignment:
1188 case sfk_copy_assignment:
1189 assign_p = true;
1190 fnname = ansi_assopname (NOP_EXPR);
1191 break;
1193 case sfk_destructor:
1194 check_vdtor = true;
1195 /* The synthesized method will call base dtors, but check complete
1196 here to avoid having to deal with VTT. */
1197 fnname = complete_dtor_identifier;
1198 break;
1200 case sfk_constructor:
1201 case sfk_move_constructor:
1202 case sfk_copy_constructor:
1203 case sfk_inheriting_constructor:
1204 ctor_p = true;
1205 fnname = complete_ctor_identifier;
1206 break;
1208 default:
1209 gcc_unreachable ();
1212 gcc_assert ((sfk == sfk_inheriting_constructor)
1213 == (inherited_base != NULL_TREE));
1215 /* If that user-written default constructor would satisfy the
1216 requirements of a constexpr constructor (7.1.5), the
1217 implicitly-defined default constructor is constexpr. */
1218 if (constexpr_p)
1219 *constexpr_p = ctor_p;
1221 move_p = false;
1222 switch (sfk)
1224 case sfk_constructor:
1225 case sfk_destructor:
1226 case sfk_inheriting_constructor:
1227 copy_arg_p = false;
1228 break;
1230 case sfk_move_constructor:
1231 case sfk_move_assignment:
1232 move_p = true;
1233 case sfk_copy_constructor:
1234 case sfk_copy_assignment:
1235 copy_arg_p = true;
1236 break;
1238 default:
1239 gcc_unreachable ();
1242 expected_trivial = type_has_trivial_fn (ctype, sfk);
1243 if (trivial_p)
1244 *trivial_p = expected_trivial;
1246 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1247 class versions and other properties of the type. But a subobject
1248 class can be trivially copyable and yet have overload resolution
1249 choose a template constructor for initialization, depending on
1250 rvalueness and cv-quals. So we can't exit early for copy/move
1251 methods in C++0x. The same considerations apply in C++98/03, but
1252 there the definition of triviality does not consider overload
1253 resolution, so a constructor can be trivial even if it would otherwise
1254 call a non-trivial constructor. */
1255 if (expected_trivial
1256 && (!copy_arg_p || cxx_dialect < cxx0x))
1258 if (constexpr_p && sfk == sfk_constructor)
1260 bool cx = trivial_default_constructor_is_constexpr (ctype);
1261 *constexpr_p = cx;
1262 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1263 /* A trivial constructor doesn't have any NSDMI. */
1264 inform (input_location, "defaulted default constructor does "
1265 "not initialize any non-static data member");
1267 if (!diag)
1268 return;
1271 ++cp_unevaluated_operand;
1272 ++c_inhibit_evaluation_warnings;
1273 push_deferring_access_checks (dk_no_deferred);
1275 scope = push_scope (ctype);
1277 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE;
1278 if (!inherited_base)
1279 flags |= LOOKUP_DEFAULTED;
1281 complain = diag ? tf_warning_or_error : tf_none;
1283 if (const_p)
1284 quals = TYPE_QUAL_CONST;
1285 else
1286 quals = TYPE_UNQUALIFIED;
1287 argtype = NULL_TREE;
1289 for (binfo = TYPE_BINFO (ctype), i = 0;
1290 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1292 tree basetype = BINFO_TYPE (base_binfo);
1294 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1295 /* We'll handle virtual bases below. */
1296 continue;
1298 if (copy_arg_p)
1299 argtype = build_stub_type (basetype, quals, move_p);
1300 else if (basetype == inherited_base)
1301 argtype = inherited_parms;
1302 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1303 if (inherited_base)
1304 argtype = NULL_TREE;
1306 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1307 constexpr_p, diag, basetype);
1308 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1310 /* In a constructor we also need to check the subobject
1311 destructors for cleanup of partially constructed objects. */
1312 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1313 NULL_TREE, flags, complain);
1314 /* Note that we don't pass down trivial_p; the subobject
1315 destructors don't affect triviality of the constructor. Nor
1316 do they affect constexpr-ness (a constant expression doesn't
1317 throw) or exception-specification (a throw from one of the
1318 dtors would be a double-fault). */
1319 process_subob_fn (rval, NULL, NULL,
1320 deleted_p, NULL, false,
1321 basetype);
1324 if (check_vdtor && type_has_virtual_destructor (basetype))
1326 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1327 ptr_type_node, flags, complain);
1328 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1329 to have a null rval (no class-specific op delete). */
1330 if (rval && rval == error_mark_node && deleted_p)
1331 *deleted_p = true;
1332 check_vdtor = false;
1335 if (diag && assign_p && move_p
1336 && BINFO_VIRTUAL_P (base_binfo)
1337 && rval && TREE_CODE (rval) == FUNCTION_DECL
1338 && move_fn_p (rval) && !trivial_fn_p (rval))
1339 warning (OPT_Wvirtual_move_assign,
1340 "defaulted move assignment for %qT calls a non-trivial "
1341 "move assignment operator for virtual base %qT",
1342 ctype, basetype);
1345 vbases = CLASSTYPE_VBASECLASSES (ctype);
1346 if (vbases == NULL)
1347 /* No virtual bases to worry about. */;
1348 else if (!assign_p)
1350 if (constexpr_p)
1351 *constexpr_p = false;
1352 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1354 tree basetype = BINFO_TYPE (base_binfo);
1355 if (copy_arg_p)
1356 argtype = build_stub_type (basetype, quals, move_p);
1357 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1359 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1360 constexpr_p, diag, basetype);
1361 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1363 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1364 NULL_TREE, flags, complain);
1365 process_subob_fn (rval, NULL, NULL,
1366 deleted_p, NULL, false,
1367 basetype);
1372 /* Now handle the non-static data members. */
1373 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1374 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1375 deleted_p, constexpr_p,
1376 diag, flags, complain);
1377 if (ctor_p)
1378 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1379 sfk_destructor, TYPE_UNQUALIFIED, false,
1380 false, false, NULL, NULL,
1381 deleted_p, NULL,
1382 false, flags, complain);
1384 pop_scope (scope);
1386 pop_deferring_access_checks ();
1387 --cp_unevaluated_operand;
1388 --c_inhibit_evaluation_warnings;
1391 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1392 return true; else return false. */
1394 bool
1395 maybe_explain_implicit_delete (tree decl)
1397 /* If decl is a clone, get the primary variant. */
1398 decl = DECL_ORIGIN (decl);
1399 gcc_assert (DECL_DELETED_FN (decl));
1400 if (DECL_DEFAULTED_FN (decl))
1402 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1403 static struct pointer_set_t *explained;
1405 special_function_kind sfk;
1406 location_t loc;
1407 bool informed;
1408 tree ctype;
1410 if (!explained)
1411 explained = pointer_set_create ();
1412 if (pointer_set_insert (explained, decl))
1413 return true;
1415 sfk = special_function_p (decl);
1416 ctype = DECL_CONTEXT (decl);
1417 loc = input_location;
1418 input_location = DECL_SOURCE_LOCATION (decl);
1420 informed = false;
1421 if (LAMBDA_TYPE_P (ctype))
1423 informed = true;
1424 if (sfk == sfk_constructor)
1425 inform (DECL_SOURCE_LOCATION (decl),
1426 "a lambda closure type has a deleted default constructor");
1427 else if (sfk == sfk_copy_assignment)
1428 inform (DECL_SOURCE_LOCATION (decl),
1429 "a lambda closure type has a deleted copy assignment operator");
1430 else
1431 informed = false;
1433 else if (DECL_ARTIFICIAL (decl)
1434 && (sfk == sfk_copy_assignment
1435 || sfk == sfk_copy_constructor)
1436 && (type_has_user_declared_move_constructor (ctype)
1437 || type_has_user_declared_move_assign (ctype)))
1439 inform (0, "%q+#D is implicitly declared as deleted because %qT "
1440 "declares a move constructor or move assignment operator",
1441 decl, ctype);
1442 informed = true;
1444 if (!informed)
1446 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1447 tree parm_type = TREE_VALUE (parms);
1448 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1449 tree scope = push_scope (ctype);
1450 inform (0, "%q+#D is implicitly deleted because the default "
1451 "definition would be ill-formed:", decl);
1452 pop_scope (scope);
1453 synthesized_method_walk (ctype, sfk, const_p,
1454 NULL, NULL, NULL, NULL, true,
1455 DECL_INHERITED_CTOR_BASE (decl), parms);
1458 input_location = loc;
1459 return true;
1461 return false;
1464 /* DECL is a defaulted function which was declared constexpr. Explain why
1465 it can't be constexpr. */
1467 void
1468 explain_implicit_non_constexpr (tree decl)
1470 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1471 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1472 bool dummy;
1473 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1474 special_function_p (decl), const_p,
1475 NULL, NULL, NULL, &dummy, true,
1476 NULL_TREE, NULL_TREE);
1479 /* DECL is an instantiation of an inheriting constructor template. Deduce
1480 the correct exception-specification and deletedness for this particular
1481 specialization. */
1483 void
1484 deduce_inheriting_ctor (tree decl)
1486 gcc_assert (DECL_INHERITED_CTOR_BASE (decl));
1487 tree spec;
1488 bool trivial, constexpr_, deleted;
1489 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1490 false, &spec, &trivial, &deleted, &constexpr_,
1491 /*diag*/false,
1492 DECL_INHERITED_CTOR_BASE (decl),
1493 FUNCTION_FIRST_USER_PARMTYPE (decl));
1494 DECL_DELETED_FN (decl) = deleted;
1495 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1498 /* Implicitly declare the special function indicated by KIND, as a
1499 member of TYPE. For copy constructors and assignment operators,
1500 CONST_P indicates whether these functions should take a const
1501 reference argument or a non-const reference. Returns the
1502 FUNCTION_DECL for the implicitly declared function. */
1504 tree
1505 implicitly_declare_fn (special_function_kind kind, tree type,
1506 bool const_p, tree inherited_ctor,
1507 tree inherited_parms)
1509 tree fn;
1510 tree parameter_types = void_list_node;
1511 tree return_type;
1512 tree fn_type;
1513 tree raises = empty_except_spec;
1514 tree rhs_parm_type = NULL_TREE;
1515 tree this_parm;
1516 tree name;
1517 HOST_WIDE_INT saved_processing_template_decl;
1518 bool deleted_p;
1519 bool constexpr_p;
1521 /* Because we create declarations for implicitly declared functions
1522 lazily, we may be creating the declaration for a member of TYPE
1523 while in some completely different context. However, TYPE will
1524 never be a dependent class (because we never want to do lookups
1525 for implicitly defined functions in a dependent class).
1526 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1527 because we only create clones for constructors and destructors
1528 when not in a template. */
1529 gcc_assert (!dependent_type_p (type));
1530 saved_processing_template_decl = processing_template_decl;
1531 processing_template_decl = 0;
1533 type = TYPE_MAIN_VARIANT (type);
1535 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1537 if (kind == sfk_destructor)
1538 /* See comment in check_special_function_return_type. */
1539 return_type = build_pointer_type (void_type_node);
1540 else
1541 return_type = build_pointer_type (type);
1543 else
1544 return_type = void_type_node;
1546 switch (kind)
1548 case sfk_destructor:
1549 /* Destructor. */
1550 name = constructor_name (type);
1551 break;
1553 case sfk_constructor:
1554 /* Default constructor. */
1555 name = constructor_name (type);
1556 break;
1558 case sfk_copy_constructor:
1559 case sfk_copy_assignment:
1560 case sfk_move_constructor:
1561 case sfk_move_assignment:
1562 case sfk_inheriting_constructor:
1564 bool move_p;
1565 if (kind == sfk_copy_assignment
1566 || kind == sfk_move_assignment)
1568 return_type = build_reference_type (type);
1569 name = ansi_assopname (NOP_EXPR);
1571 else
1572 name = constructor_name (type);
1574 if (kind == sfk_inheriting_constructor)
1575 parameter_types = inherited_parms;
1576 else
1578 if (const_p)
1579 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1580 else
1581 rhs_parm_type = type;
1582 move_p = (kind == sfk_move_assignment
1583 || kind == sfk_move_constructor);
1584 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1586 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1588 break;
1590 default:
1591 gcc_unreachable ();
1594 tree inherited_base = (inherited_ctor
1595 ? DECL_CONTEXT (inherited_ctor)
1596 : NULL_TREE);
1597 bool trivial_p = false;
1599 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1601 /* For an inheriting constructor template, just copy these flags from
1602 the inherited constructor template for now. */
1603 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
1604 deleted_p = DECL_DELETED_FN (DECL_TEMPLATE_RESULT (inherited_ctor));
1605 constexpr_p
1606 = DECL_DECLARED_CONSTEXPR_P (DECL_TEMPLATE_RESULT (inherited_ctor));
1608 else
1609 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1610 &deleted_p, &constexpr_p, false,
1611 inherited_base, inherited_parms);
1612 /* Don't bother marking a deleted constructor as constexpr. */
1613 if (deleted_p)
1614 constexpr_p = false;
1615 /* A trivial copy/move constructor is also a constexpr constructor. */
1616 else if (trivial_p && cxx_dialect >= cxx0x
1617 && (kind == sfk_copy_constructor
1618 || kind == sfk_move_constructor))
1619 gcc_assert (constexpr_p);
1621 if (!trivial_p && type_has_trivial_fn (type, kind))
1622 type_set_nontrivial_flag (type, kind);
1624 /* Create the function. */
1625 fn_type = build_method_type_directly (type, return_type, parameter_types);
1626 if (raises)
1627 fn_type = build_exception_variant (fn_type, raises);
1628 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1629 if (kind != sfk_inheriting_constructor)
1630 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1631 if (kind == sfk_constructor || kind == sfk_copy_constructor
1632 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
1633 DECL_CONSTRUCTOR_P (fn) = 1;
1634 else if (kind == sfk_destructor)
1635 DECL_DESTRUCTOR_P (fn) = 1;
1636 else
1638 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1639 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1642 /* If pointers to member functions use the least significant bit to
1643 indicate whether a function is virtual, ensure a pointer
1644 to this function will have that bit clear. */
1645 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1646 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1647 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1649 /* Create the explicit arguments. */
1650 if (rhs_parm_type)
1652 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1653 want its type to be included in the mangled function
1654 name. */
1655 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1656 TREE_READONLY (decl) = 1;
1657 retrofit_lang_decl (decl);
1658 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
1659 DECL_ARGUMENTS (fn) = decl;
1661 else if (kind == sfk_inheriting_constructor)
1663 tree *p = &DECL_ARGUMENTS (fn);
1664 int index = 1;
1665 for (tree parm = inherited_parms; parm != void_list_node;
1666 parm = TREE_CHAIN (parm))
1668 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
1669 retrofit_lang_decl (*p);
1670 DECL_PARM_LEVEL (*p) = 1;
1671 DECL_PARM_INDEX (*p) = index++;
1672 DECL_CONTEXT (*p) = fn;
1673 p = &DECL_CHAIN (*p);
1675 SET_DECL_INHERITED_CTOR_BASE (fn, inherited_base);
1676 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
1677 /* A constructor so declared has the same access as the corresponding
1678 constructor in X. */
1679 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
1680 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
1681 /* Copy constexpr from the inherited constructor even if the
1682 inheriting constructor doesn't satisfy the requirements. */
1683 constexpr_p
1684 = DECL_DECLARED_CONSTEXPR_P (STRIP_TEMPLATE (inherited_ctor));
1686 /* Add the "this" parameter. */
1687 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1688 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1689 DECL_ARGUMENTS (fn) = this_parm;
1691 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1692 set_linkage_according_to_type (type, fn);
1693 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1694 DECL_IN_AGGR_P (fn) = 1;
1695 DECL_ARTIFICIAL (fn) = 1;
1696 DECL_DEFAULTED_FN (fn) = 1;
1697 if (cxx_dialect >= cxx0x)
1699 DECL_DELETED_FN (fn) = deleted_p;
1700 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
1702 DECL_EXTERNAL (fn) = true;
1703 DECL_NOT_REALLY_EXTERN (fn) = 1;
1704 DECL_DECLARED_INLINE_P (fn) = 1;
1705 gcc_assert (!TREE_USED (fn));
1707 /* Restore PROCESSING_TEMPLATE_DECL. */
1708 processing_template_decl = saved_processing_template_decl;
1710 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1711 fn = add_inherited_template_parms (fn, inherited_ctor);
1713 /* Warn about calling a non-trivial move assignment in a virtual base. */
1714 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
1715 && CLASSTYPE_VBASECLASSES (type))
1717 location_t loc = input_location;
1718 input_location = DECL_SOURCE_LOCATION (fn);
1719 synthesized_method_walk (type, kind, const_p,
1720 NULL, NULL, NULL, NULL, true,
1721 NULL_TREE, NULL_TREE);
1722 input_location = loc;
1725 return fn;
1728 /* Gives any errors about defaulted functions which need to be deferred
1729 until the containing class is complete. */
1731 void
1732 defaulted_late_check (tree fn)
1734 /* Complain about invalid signature for defaulted fn. */
1735 tree ctx = DECL_CONTEXT (fn);
1736 special_function_kind kind = special_function_p (fn);
1737 bool fn_const_p = (copy_fn_p (fn) == 2);
1738 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
1739 NULL, NULL);
1741 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1742 TREE_TYPE (TREE_TYPE (implicit_fn)))
1743 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1744 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
1746 error ("defaulted declaration %q+D", fn);
1747 error_at (DECL_SOURCE_LOCATION (fn),
1748 "does not match expected signature %qD", implicit_fn);
1751 /* 8.4.2/2: If it is explicitly defaulted on its first declaration, it is
1752 implicitly considered to have the same exception-specification as if
1753 it had been implicitly declared. */
1754 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1756 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
1757 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
1759 maybe_instantiate_noexcept (fn);
1760 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
1761 eh_spec, ce_normal))
1762 error ("function %q+D defaulted on its first declaration "
1763 "with an exception-specification that differs from "
1764 "the implicit declaration %q#D", fn, implicit_fn);
1766 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
1767 if (DECL_DECLARED_CONSTEXPR_P (implicit_fn))
1769 /* Hmm...should we do this for out-of-class too? Should it be OK to
1770 add constexpr later like inline, rather than requiring
1771 declarations to match? */
1772 DECL_DECLARED_CONSTEXPR_P (fn) = true;
1773 if (kind == sfk_constructor)
1774 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
1778 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
1779 && DECL_DECLARED_CONSTEXPR_P (fn))
1781 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
1783 error ("explicitly defaulted function %q+D cannot be declared "
1784 "as constexpr because the implicit declaration is not "
1785 "constexpr:", fn);
1786 explain_implicit_non_constexpr (fn);
1788 DECL_DECLARED_CONSTEXPR_P (fn) = false;
1791 if (DECL_DELETED_FN (implicit_fn))
1792 DECL_DELETED_FN (fn) = 1;
1795 /* Returns true iff FN can be explicitly defaulted, and gives any
1796 errors if defaulting FN is ill-formed. */
1798 bool
1799 defaultable_fn_check (tree fn)
1801 special_function_kind kind = sfk_none;
1803 if (template_parm_scope_p ())
1805 error ("a template cannot be defaulted");
1806 return false;
1809 if (DECL_CONSTRUCTOR_P (fn))
1811 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
1812 kind = sfk_constructor;
1813 else if (copy_fn_p (fn) > 0
1814 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
1815 == void_list_node))
1816 kind = sfk_copy_constructor;
1817 else if (move_fn_p (fn))
1818 kind = sfk_move_constructor;
1820 else if (DECL_DESTRUCTOR_P (fn))
1821 kind = sfk_destructor;
1822 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
1823 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
1825 if (copy_fn_p (fn))
1826 kind = sfk_copy_assignment;
1827 else if (move_fn_p (fn))
1828 kind = sfk_move_assignment;
1831 if (kind == sfk_none)
1833 error ("%qD cannot be defaulted", fn);
1834 return false;
1836 else
1838 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
1839 for (; t && t != void_list_node; t = TREE_CHAIN (t))
1840 if (TREE_PURPOSE (t))
1842 error ("defaulted function %q+D with default argument", fn);
1843 break;
1845 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
1846 /* Defer checking. */;
1847 else if (!processing_template_decl)
1848 defaulted_late_check (fn);
1850 return true;
1854 /* Add an implicit declaration to TYPE for the kind of function
1855 indicated by SFK. Return the FUNCTION_DECL for the new implicit
1856 declaration. */
1858 tree
1859 lazily_declare_fn (special_function_kind sfk, tree type)
1861 tree fn;
1862 /* Whether or not the argument has a const reference type. */
1863 bool const_p = false;
1865 switch (sfk)
1867 case sfk_constructor:
1868 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
1869 break;
1870 case sfk_copy_constructor:
1871 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
1872 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
1873 break;
1874 case sfk_move_constructor:
1875 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
1876 break;
1877 case sfk_copy_assignment:
1878 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
1879 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
1880 break;
1881 case sfk_move_assignment:
1882 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
1883 break;
1884 case sfk_destructor:
1885 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
1886 break;
1887 default:
1888 gcc_unreachable ();
1891 /* Declare the function. */
1892 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
1894 /* [class.copy]/8 If the class definition declares a move constructor or
1895 move assignment operator, the implicitly declared copy constructor is
1896 defined as deleted.... */
1897 if ((sfk == sfk_copy_assignment
1898 || sfk == sfk_copy_constructor)
1899 && (type_has_user_declared_move_constructor (type)
1900 || type_has_user_declared_move_assign (type)))
1901 DECL_DELETED_FN (fn) = true;
1903 /* A destructor may be virtual. */
1904 if (sfk == sfk_destructor
1905 || sfk == sfk_move_assignment
1906 || sfk == sfk_copy_assignment)
1907 check_for_override (fn, type);
1908 /* Add it to CLASSTYPE_METHOD_VEC. */
1909 add_method (type, fn, NULL_TREE);
1910 /* Add it to TYPE_METHODS. */
1911 if (sfk == sfk_destructor
1912 && DECL_VIRTUAL_P (fn)
1913 && abi_version_at_least (2))
1914 /* The ABI requires that a virtual destructor go at the end of the
1915 vtable. */
1916 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
1917 else
1919 /* G++ 3.2 put the implicit destructor at the *beginning* of the
1920 TYPE_METHODS list, which cause the destructor to be emitted
1921 in an incorrect location in the vtable. */
1922 if (warn_abi && sfk == sfk_destructor && DECL_VIRTUAL_P (fn))
1923 warning (OPT_Wabi, "vtable layout for class %qT may not be ABI-compliant"
1924 "and may change in a future version of GCC due to "
1925 "implicit virtual destructor",
1926 type);
1927 DECL_CHAIN (fn) = TYPE_METHODS (type);
1928 TYPE_METHODS (type) = fn;
1930 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
1931 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
1932 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
1933 /* Create appropriate clones. */
1934 clone_function_decl (fn, /*update_method_vec=*/true);
1936 return fn;
1939 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
1940 as there are artificial parms in FN. */
1942 tree
1943 skip_artificial_parms_for (const_tree fn, tree list)
1945 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1946 list = TREE_CHAIN (list);
1947 else
1948 return list;
1950 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1951 list = TREE_CHAIN (list);
1952 if (DECL_HAS_VTT_PARM_P (fn))
1953 list = TREE_CHAIN (list);
1954 return list;
1957 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
1958 artificial parms in FN. */
1961 num_artificial_parms_for (const_tree fn)
1963 int count = 0;
1965 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1966 count++;
1967 else
1968 return 0;
1970 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1971 count++;
1972 if (DECL_HAS_VTT_PARM_P (fn))
1973 count++;
1974 return count;
1978 #include "gt-cp-method.h"