Implement P0195R2, C++17 variadic using.
[gcc.git] / gcc / cp / method.c
1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22
23 /* Handle method declarations. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "target.h"
28 #include "cp-tree.h"
29 #include "stringpool.h"
30 #include "cgraph.h"
31 #include "varasm.h"
32 #include "toplev.h"
33 #include "common/common-target.h"
34
35 /* Various flags to control the mangling process. */
36
37 enum mangling_flags
38 {
39 /* No flags. */
40 mf_none = 0,
41 /* The thing we are presently mangling is part of a template type,
42 rather than a fully instantiated type. Therefore, we may see
43 complex expressions where we would normally expect to see a
44 simple integer constant. */
45 mf_maybe_uninstantiated = 1,
46 /* When mangling a numeric value, use the form `_XX_' (instead of
47 just `XX') if the value has more than one digit. */
48 mf_use_underscores_around_value = 2
49 };
50
51 static void do_build_copy_assign (tree);
52 static void do_build_copy_constructor (tree);
53 static tree make_alias_for_thunk (tree);
54
55 /* Called once to initialize method.c. */
56
57 void
58 init_method (void)
59 {
60 init_mangle ();
61 }
62 \f
63 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
64 indicates whether it is a this or result adjusting thunk.
65 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
66 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
67 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
68 adjusting thunks, we scale it to a byte offset. For covariant
69 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
70 the returned thunk with finish_thunk. */
71
72 tree
73 make_thunk (tree function, bool this_adjusting,
74 tree fixed_offset, tree virtual_offset)
75 {
76 HOST_WIDE_INT d;
77 tree thunk;
78
79 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
80 /* We can have this thunks to covariant thunks, but not vice versa. */
81 gcc_assert (!DECL_THIS_THUNK_P (function));
82 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
83
84 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
85 if (this_adjusting && virtual_offset)
86 virtual_offset
87 = size_binop (MULT_EXPR,
88 virtual_offset,
89 convert (ssizetype,
90 TYPE_SIZE_UNIT (vtable_entry_type)));
91
92 d = tree_to_shwi (fixed_offset);
93
94 /* See if we already have the thunk in question. For this_adjusting
95 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
96 will be a BINFO. */
97 for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
98 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
99 && THUNK_FIXED_OFFSET (thunk) == d
100 && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
101 && (!virtual_offset
102 || (this_adjusting
103 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
104 virtual_offset)
105 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
106 return thunk;
107
108 /* All thunks must be created before FUNCTION is actually emitted;
109 the ABI requires that all thunks be emitted together with the
110 function to which they transfer control. */
111 gcc_assert (!TREE_ASM_WRITTEN (function));
112 /* Likewise, we can only be adding thunks to a function declared in
113 the class currently being laid out. */
114 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
115 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
116
117 thunk = build_decl (DECL_SOURCE_LOCATION (function),
118 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
119 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
120 cxx_dup_lang_specific_decl (thunk);
121 DECL_VIRTUAL_P (thunk) = true;
122 SET_DECL_THUNKS (thunk, NULL_TREE);
123
124 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
125 TREE_READONLY (thunk) = TREE_READONLY (function);
126 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
127 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
128 SET_DECL_THUNK_P (thunk, this_adjusting);
129 THUNK_TARGET (thunk) = function;
130 THUNK_FIXED_OFFSET (thunk) = d;
131 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
132 THUNK_ALIAS (thunk) = NULL_TREE;
133
134 DECL_INTERFACE_KNOWN (thunk) = 1;
135 DECL_NOT_REALLY_EXTERN (thunk) = 1;
136 DECL_COMDAT (thunk) = DECL_COMDAT (function);
137 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
138 /* The thunk itself is not a constructor or destructor, even if
139 the thing it is thunking to is. */
140 DECL_DESTRUCTOR_P (thunk) = 0;
141 DECL_CONSTRUCTOR_P (thunk) = 0;
142 DECL_EXTERNAL (thunk) = 1;
143 DECL_ARTIFICIAL (thunk) = 1;
144 /* The THUNK is not a pending inline, even if the FUNCTION is. */
145 DECL_PENDING_INLINE_P (thunk) = 0;
146 DECL_DECLARED_INLINE_P (thunk) = 0;
147 /* Nor is it a template instantiation. */
148 DECL_USE_TEMPLATE (thunk) = 0;
149 DECL_TEMPLATE_INFO (thunk) = NULL;
150
151 /* Add it to the list of thunks associated with FUNCTION. */
152 DECL_CHAIN (thunk) = DECL_THUNKS (function);
153 SET_DECL_THUNKS (function, thunk);
154
155 return thunk;
156 }
157
158 /* Finish THUNK, a thunk decl. */
159
160 void
161 finish_thunk (tree thunk)
162 {
163 tree function, name;
164 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
165 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
166
167 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
168 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
169 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
170 function = THUNK_TARGET (thunk);
171 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
172 fixed_offset, virtual_offset, thunk);
173
174 /* We can end up with declarations of (logically) different
175 covariant thunks, that do identical adjustments. The two thunks
176 will be adjusting between within different hierarchies, which
177 happen to have the same layout. We must nullify one of them to
178 refer to the other. */
179 if (DECL_RESULT_THUNK_P (thunk))
180 {
181 tree cov_probe;
182
183 for (cov_probe = DECL_THUNKS (function);
184 cov_probe; cov_probe = DECL_CHAIN (cov_probe))
185 if (DECL_NAME (cov_probe) == name)
186 {
187 gcc_assert (!DECL_THUNKS (thunk));
188 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
189 ? THUNK_ALIAS (cov_probe) : cov_probe);
190 break;
191 }
192 }
193
194 DECL_NAME (thunk) = name;
195 SET_DECL_ASSEMBLER_NAME (thunk, name);
196 }
197
198 static GTY (()) int thunk_labelno;
199
200 /* Create a static alias to target. */
201
202 tree
203 make_alias_for (tree target, tree newid)
204 {
205 tree alias = build_decl (DECL_SOURCE_LOCATION (target),
206 TREE_CODE (target), newid, TREE_TYPE (target));
207 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
208 cxx_dup_lang_specific_decl (alias);
209 DECL_CONTEXT (alias) = NULL;
210 TREE_READONLY (alias) = TREE_READONLY (target);
211 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
212 TREE_PUBLIC (alias) = 0;
213 DECL_INTERFACE_KNOWN (alias) = 1;
214 if (DECL_LANG_SPECIFIC (alias))
215 {
216 DECL_NOT_REALLY_EXTERN (alias) = 1;
217 DECL_USE_TEMPLATE (alias) = 0;
218 DECL_TEMPLATE_INFO (alias) = NULL;
219 }
220 DECL_EXTERNAL (alias) = 0;
221 DECL_ARTIFICIAL (alias) = 1;
222 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
223 if (TREE_CODE (alias) == FUNCTION_DECL)
224 {
225 DECL_SAVED_FUNCTION_DATA (alias) = NULL;
226 DECL_DESTRUCTOR_P (alias) = 0;
227 DECL_CONSTRUCTOR_P (alias) = 0;
228 DECL_PENDING_INLINE_P (alias) = 0;
229 DECL_DECLARED_INLINE_P (alias) = 0;
230 DECL_INITIAL (alias) = error_mark_node;
231 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
232 }
233 else
234 TREE_STATIC (alias) = 1;
235 TREE_ADDRESSABLE (alias) = 1;
236 TREE_USED (alias) = 1;
237 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
238 return alias;
239 }
240
241 static tree
242 make_alias_for_thunk (tree function)
243 {
244 tree alias;
245 char buf[256];
246
247 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
248 thunk_labelno++;
249
250 alias = make_alias_for (function, get_identifier (buf));
251
252 if (!flag_syntax_only)
253 {
254 struct cgraph_node *funcn, *aliasn;
255 funcn = cgraph_node::get (function);
256 gcc_checking_assert (funcn);
257 aliasn = cgraph_node::create_same_body_alias (alias, function);
258 DECL_ASSEMBLER_NAME (function);
259 gcc_assert (aliasn != NULL);
260 }
261
262 return alias;
263 }
264
265 /* Emit the definition of a C++ multiple inheritance or covariant
266 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
267 immediately. */
268
269 void
270 use_thunk (tree thunk_fndecl, bool emit_p)
271 {
272 tree a, t, function, alias;
273 tree virtual_offset;
274 HOST_WIDE_INT fixed_offset, virtual_value;
275 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
276 struct cgraph_node *funcn, *thunk_node;
277
278 /* We should have called finish_thunk to give it a name. */
279 gcc_assert (DECL_NAME (thunk_fndecl));
280
281 /* We should never be using an alias, always refer to the
282 aliased thunk. */
283 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
284
285 if (TREE_ASM_WRITTEN (thunk_fndecl))
286 return;
287
288 function = THUNK_TARGET (thunk_fndecl);
289 if (DECL_RESULT (thunk_fndecl))
290 /* We already turned this thunk into an ordinary function.
291 There's no need to process this thunk again. */
292 return;
293
294 if (DECL_THUNK_P (function))
295 /* The target is itself a thunk, process it now. */
296 use_thunk (function, emit_p);
297
298 /* Thunks are always addressable; they only appear in vtables. */
299 TREE_ADDRESSABLE (thunk_fndecl) = 1;
300
301 /* Figure out what function is being thunked to. It's referenced in
302 this translation unit. */
303 TREE_ADDRESSABLE (function) = 1;
304 mark_used (function);
305 if (!emit_p)
306 return;
307
308 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
309 alias = make_alias_for_thunk (function);
310 else
311 alias = function;
312
313 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
314 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
315
316 if (virtual_offset)
317 {
318 if (!this_adjusting)
319 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
320 virtual_value = tree_to_shwi (virtual_offset);
321 gcc_assert (virtual_value);
322 }
323 else
324 virtual_value = 0;
325
326 /* And, if we need to emit the thunk, it's used. */
327 mark_used (thunk_fndecl);
328 /* This thunk is actually defined. */
329 DECL_EXTERNAL (thunk_fndecl) = 0;
330 /* The linkage of the function may have changed. FIXME in linkage
331 rewrite. */
332 gcc_assert (DECL_INTERFACE_KNOWN (function));
333 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
334 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
335 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
336 = DECL_VISIBILITY_SPECIFIED (function);
337 DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
338 DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
339
340 if (flag_syntax_only)
341 {
342 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
343 return;
344 }
345
346 push_to_top_level ();
347
348 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
349 && targetm_common.have_named_sections)
350 {
351 tree fn = function;
352 struct symtab_node *symbol;
353
354 if ((symbol = symtab_node::get (function))
355 && symbol->alias)
356 {
357 if (symbol->analyzed)
358 fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
359 else
360 fn = symtab_node::get (function)->alias_target;
361 }
362 resolve_unique_section (fn, 0, flag_function_sections);
363
364 if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
365 {
366 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
367
368 /* Output the thunk into the same section as function. */
369 set_decl_section_name (thunk_fndecl, DECL_SECTION_NAME (fn));
370 symtab_node::get (thunk_fndecl)->implicit_section
371 = symtab_node::get (fn)->implicit_section;
372 }
373 }
374
375 /* Set up cloned argument trees for the thunk. */
376 t = NULL_TREE;
377 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
378 {
379 tree x = copy_node (a);
380 DECL_CHAIN (x) = t;
381 DECL_CONTEXT (x) = thunk_fndecl;
382 SET_DECL_RTL (x, NULL);
383 DECL_HAS_VALUE_EXPR_P (x) = 0;
384 TREE_ADDRESSABLE (x) = 0;
385 t = x;
386 }
387 a = nreverse (t);
388 DECL_ARGUMENTS (thunk_fndecl) = a;
389 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
390 funcn = cgraph_node::get (function);
391 gcc_checking_assert (funcn);
392 thunk_node = funcn->create_thunk (thunk_fndecl, function,
393 this_adjusting, fixed_offset, virtual_value,
394 virtual_offset, alias);
395 if (DECL_ONE_ONLY (function))
396 thunk_node->add_to_same_comdat_group (funcn);
397
398 pop_from_top_level ();
399 }
400 \f
401 /* Code for synthesizing methods which have default semantics defined. */
402
403 /* True iff CTYPE has a trivial SFK. */
404
405 static bool
406 type_has_trivial_fn (tree ctype, special_function_kind sfk)
407 {
408 switch (sfk)
409 {
410 case sfk_constructor:
411 return !TYPE_HAS_COMPLEX_DFLT (ctype);
412 case sfk_copy_constructor:
413 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
414 case sfk_move_constructor:
415 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
416 case sfk_copy_assignment:
417 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
418 case sfk_move_assignment:
419 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
420 case sfk_destructor:
421 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
422 case sfk_inheriting_constructor:
423 return false;
424 default:
425 gcc_unreachable ();
426 }
427 }
428
429 /* Note that CTYPE has a non-trivial SFK even though we previously thought
430 it was trivial. */
431
432 static void
433 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
434 {
435 switch (sfk)
436 {
437 case sfk_constructor:
438 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
439 return;
440 case sfk_copy_constructor:
441 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
442 return;
443 case sfk_move_constructor:
444 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
445 return;
446 case sfk_copy_assignment:
447 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
448 return;
449 case sfk_move_assignment:
450 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
451 return;
452 case sfk_destructor:
453 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
454 return;
455 case sfk_inheriting_constructor:
456 default:
457 gcc_unreachable ();
458 }
459 }
460
461 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
462
463 bool
464 trivial_fn_p (tree fn)
465 {
466 if (TREE_CODE (fn) == TEMPLATE_DECL)
467 return false;
468 if (!DECL_DEFAULTED_FN (fn))
469 return false;
470
471 /* If fn is a clone, get the primary variant. */
472 if (tree prim = DECL_CLONED_FUNCTION (fn))
473 fn = prim;
474 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
475 }
476
477 /* PARM is a PARM_DECL for a function which we want to forward to another
478 function without changing its value category, a la std::forward. */
479
480 tree
481 forward_parm (tree parm)
482 {
483 tree exp = convert_from_reference (parm);
484 tree type = TREE_TYPE (parm);
485 if (DECL_PACK_P (parm))
486 type = PACK_EXPANSION_PATTERN (type);
487 if (TREE_CODE (type) != REFERENCE_TYPE)
488 type = cp_build_reference_type (type, /*rval=*/true);
489 exp = build_static_cast (type, exp, tf_warning_or_error);
490 if (DECL_PACK_P (parm))
491 exp = make_pack_expansion (exp);
492 return exp;
493 }
494
495 /* Strip all inheriting constructors, if any, to return the original
496 constructor from a (possibly indirect) base class. */
497
498 tree
499 strip_inheriting_ctors (tree dfn)
500 {
501 gcc_assert (flag_new_inheriting_ctors);
502 tree fn = dfn;
503 while (tree inh = DECL_INHERITED_CTOR (fn))
504 {
505 inh = OVL_CURRENT (inh);
506 fn = inh;
507 }
508 if (TREE_CODE (fn) == TEMPLATE_DECL
509 && TREE_CODE (dfn) == FUNCTION_DECL)
510 fn = DECL_TEMPLATE_RESULT (fn);
511 return fn;
512 }
513
514 /* Find the binfo for the base subobject of BINFO being initialized by
515 inherited constructor FNDECL (a member of a direct base of BINFO). */
516
517 static tree inherited_ctor_binfo (tree, tree);
518 static tree
519 inherited_ctor_binfo_1 (tree binfo, tree fndecl)
520 {
521 tree base = DECL_CONTEXT (fndecl);
522 tree base_binfo;
523 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
524 if (BINFO_TYPE (base_binfo) == base)
525 return inherited_ctor_binfo (base_binfo, fndecl);
526
527 gcc_unreachable();
528 }
529
530 /* Find the binfo for the base subobject of BINFO being initialized by
531 inheriting constructor FNDECL (a member of BINFO), or BINFO if FNDECL is not
532 an inheriting constructor. */
533
534 static tree
535 inherited_ctor_binfo (tree binfo, tree fndecl)
536 {
537 tree inh = DECL_INHERITED_CTOR (fndecl);
538 if (!inh)
539 return binfo;
540
541 tree results = NULL_TREE;
542 for (; inh; inh = OVL_NEXT (inh))
543 {
544 tree one = inherited_ctor_binfo_1 (binfo, OVL_CURRENT (inh));
545 if (!results)
546 results = one;
547 else if (one != results)
548 results = tree_cons (NULL_TREE, one, results);
549 }
550 return results;
551 }
552
553 /* Find the binfo for the base subobject being initialized by inheriting
554 constructor FNDECL, or NULL_TREE if FNDECL is not an inheriting
555 constructor. */
556
557 tree
558 inherited_ctor_binfo (tree fndecl)
559 {
560 if (!DECL_INHERITED_CTOR (fndecl))
561 return NULL_TREE;
562 tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
563 return inherited_ctor_binfo (binfo, fndecl);
564 }
565
566 /* True if we should omit all user-declared parameters from constructor FN,
567 because it is a base clone of a ctor inherited from a virtual base. */
568
569 bool
570 ctor_omit_inherited_parms (tree fn)
571 {
572 if (!flag_new_inheriting_ctors)
573 /* We only optimize away the parameters in the new model. */
574 return false;
575 if (!DECL_BASE_CONSTRUCTOR_P (fn)
576 || !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
577 return false;
578 tree binfo = inherited_ctor_binfo (fn);
579 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
580 if (BINFO_VIRTUAL_P (binfo))
581 return true;
582 return false;
583 }
584
585 /* True iff constructor(s) INH inherited into BINFO initializes INIT_BINFO.
586 This can be true for multiple virtual bases as well as one direct
587 non-virtual base. */
588
589 static bool
590 binfo_inherited_from (tree binfo, tree init_binfo, tree inh)
591 {
592 /* inh is an OVERLOAD if we inherited the same constructor along
593 multiple paths, check all of them. */
594 for (; inh; inh = OVL_NEXT (inh))
595 {
596 tree fn = OVL_CURRENT (inh);
597 tree base = DECL_CONTEXT (fn);
598 tree base_binfo = NULL_TREE;
599 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
600 if (BINFO_TYPE (base_binfo) == base)
601 break;
602 if (base_binfo == init_binfo
603 || (flag_new_inheriting_ctors
604 && binfo_inherited_from (base_binfo, init_binfo,
605 DECL_INHERITED_CTOR (fn))))
606 return true;
607 }
608 return false;
609 }
610
611 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
612 given the parameter or parameters PARM, possibly inherited constructor
613 base INH, or move flag MOVE_P. */
614
615 static tree
616 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
617 tree member_init_list)
618 {
619 tree init;
620 if (inh)
621 {
622 /* An inheriting constructor only has a mem-initializer for
623 the base it inherits from. */
624 if (!binfo_inherited_from (TYPE_BINFO (current_class_type), binfo, inh))
625 return member_init_list;
626
627 tree *p = &init;
628 init = NULL_TREE;
629 for (; parm; parm = DECL_CHAIN (parm))
630 {
631 tree exp = forward_parm (parm);
632 *p = build_tree_list (NULL_TREE, exp);
633 p = &TREE_CHAIN (*p);
634 }
635 }
636 else
637 {
638 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
639 tf_warning_or_error);
640 if (move_p)
641 init = move (init);
642 init = build_tree_list (NULL_TREE, init);
643 }
644 return tree_cons (binfo, init, member_init_list);
645 }
646
647 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
648 constructor. */
649
650 static void
651 do_build_copy_constructor (tree fndecl)
652 {
653 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
654 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
655 bool trivial = trivial_fn_p (fndecl);
656 tree inh = DECL_INHERITED_CTOR (fndecl);
657
658 if (!inh)
659 parm = convert_from_reference (parm);
660
661 if (trivial)
662 {
663 if (is_empty_class (current_class_type))
664 /* Don't copy the padding byte; it might not have been allocated
665 if *this is a base subobject. */;
666 else if (tree_int_cst_equal (TYPE_SIZE (current_class_type),
667 CLASSTYPE_SIZE (current_class_type)))
668 {
669 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
670 finish_expr_stmt (t);
671 }
672 else
673 {
674 /* We must only copy the non-tail padding parts. */
675 tree base_size = CLASSTYPE_SIZE_UNIT (current_class_type);
676 base_size = size_binop (MINUS_EXPR, base_size, size_int (1));
677 tree array_type = build_array_type (unsigned_char_type_node,
678 build_index_type (base_size));
679 tree alias_set = build_int_cst (TREE_TYPE (current_class_ptr), 0);
680 tree lhs = build2 (MEM_REF, array_type,
681 current_class_ptr, alias_set);
682 tree rhs = build2 (MEM_REF, array_type,
683 TREE_OPERAND (parm, 0), alias_set);
684 tree t = build2 (INIT_EXPR, void_type_node, lhs, rhs);
685 finish_expr_stmt (t);
686 }
687 }
688 else
689 {
690 tree fields = TYPE_FIELDS (current_class_type);
691 tree member_init_list = NULL_TREE;
692 int cvquals = cp_type_quals (TREE_TYPE (parm));
693 int i;
694 tree binfo, base_binfo;
695 tree init;
696 vec<tree, va_gc> *vbases;
697
698 /* Initialize all the base-classes with the parameter converted
699 to their type so that we get their copy constructor and not
700 another constructor that takes current_class_type. We must
701 deal with the binfo's directly as a direct base might be
702 inaccessible due to ambiguity. */
703 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
704 vec_safe_iterate (vbases, i, &binfo); i++)
705 {
706 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
707 member_init_list);
708 }
709
710 for (binfo = TYPE_BINFO (current_class_type), i = 0;
711 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
712 {
713 if (BINFO_VIRTUAL_P (base_binfo))
714 continue;
715 member_init_list = add_one_base_init (base_binfo, parm, move_p,
716 inh, member_init_list);
717 }
718
719 for (; fields; fields = DECL_CHAIN (fields))
720 {
721 tree field = fields;
722 tree expr_type;
723
724 if (TREE_CODE (field) != FIELD_DECL)
725 continue;
726 if (inh)
727 continue;
728
729 expr_type = TREE_TYPE (field);
730 if (DECL_NAME (field))
731 {
732 if (VFIELD_NAME_P (DECL_NAME (field)))
733 continue;
734 }
735 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
736 /* Just use the field; anonymous types can't have
737 nontrivial copy ctors or assignment ops or this
738 function would be deleted. */;
739 else
740 continue;
741
742 /* Compute the type of "init->field". If the copy-constructor
743 parameter is, for example, "const S&", and the type of
744 the field is "T", then the type will usually be "const
745 T". (There are no cv-qualified variants of reference
746 types.) */
747 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
748 {
749 int quals = cvquals;
750
751 if (DECL_MUTABLE_P (field))
752 quals &= ~TYPE_QUAL_CONST;
753 quals |= cp_type_quals (expr_type);
754 expr_type = cp_build_qualified_type (expr_type, quals);
755 }
756
757 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
758 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
759 /* 'move' breaks bit-fields, and has no effect for scalars. */
760 && !scalarish_type_p (expr_type))
761 init = move (init);
762 init = build_tree_list (NULL_TREE, init);
763
764 member_init_list = tree_cons (field, init, member_init_list);
765 }
766 finish_mem_initializers (member_init_list);
767 }
768 }
769
770 static void
771 do_build_copy_assign (tree fndecl)
772 {
773 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
774 tree compound_stmt;
775 bool move_p = move_fn_p (fndecl);
776 bool trivial = trivial_fn_p (fndecl);
777 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
778
779 compound_stmt = begin_compound_stmt (0);
780 parm = convert_from_reference (parm);
781
782 if (trivial
783 && is_empty_class (current_class_type))
784 /* Don't copy the padding byte; it might not have been allocated
785 if *this is a base subobject. */;
786 else if (trivial)
787 {
788 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
789 finish_expr_stmt (t);
790 }
791 else
792 {
793 tree fields;
794 int cvquals = cp_type_quals (TREE_TYPE (parm));
795 int i;
796 tree binfo, base_binfo;
797
798 /* Assign to each of the direct base classes. */
799 for (binfo = TYPE_BINFO (current_class_type), i = 0;
800 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
801 {
802 tree converted_parm;
803 vec<tree, va_gc> *parmvec;
804
805 /* We must convert PARM directly to the base class
806 explicitly since the base class may be ambiguous. */
807 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
808 tf_warning_or_error);
809 if (move_p)
810 converted_parm = move (converted_parm);
811 /* Call the base class assignment operator. */
812 parmvec = make_tree_vector_single (converted_parm);
813 finish_expr_stmt
814 (build_special_member_call (current_class_ref,
815 ansi_assopname (NOP_EXPR),
816 &parmvec,
817 base_binfo,
818 flags,
819 tf_warning_or_error));
820 release_tree_vector (parmvec);
821 }
822
823 /* Assign to each of the non-static data members. */
824 for (fields = TYPE_FIELDS (current_class_type);
825 fields;
826 fields = DECL_CHAIN (fields))
827 {
828 tree comp = current_class_ref;
829 tree init = parm;
830 tree field = fields;
831 tree expr_type;
832 int quals;
833
834 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
835 continue;
836
837 expr_type = TREE_TYPE (field);
838
839 if (CP_TYPE_CONST_P (expr_type))
840 {
841 error ("non-static const member %q#D, can%'t use default "
842 "assignment operator", field);
843 continue;
844 }
845 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
846 {
847 error ("non-static reference member %q#D, can%'t use "
848 "default assignment operator", field);
849 continue;
850 }
851
852 if (DECL_NAME (field))
853 {
854 if (VFIELD_NAME_P (DECL_NAME (field)))
855 continue;
856 }
857 else if (ANON_AGGR_TYPE_P (expr_type)
858 && TYPE_FIELDS (expr_type) != NULL_TREE)
859 /* Just use the field; anonymous types can't have
860 nontrivial copy ctors or assignment ops or this
861 function would be deleted. */;
862 else
863 continue;
864
865 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
866
867 /* Compute the type of init->field */
868 quals = cvquals;
869 if (DECL_MUTABLE_P (field))
870 quals &= ~TYPE_QUAL_CONST;
871 expr_type = cp_build_qualified_type (expr_type, quals);
872
873 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
874 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
875 /* 'move' breaks bit-fields, and has no effect for scalars. */
876 && !scalarish_type_p (expr_type))
877 init = move (init);
878
879 if (DECL_NAME (field))
880 init = cp_build_modify_expr (input_location, comp, NOP_EXPR, init,
881 tf_warning_or_error);
882 else
883 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
884 finish_expr_stmt (init);
885 }
886 }
887 finish_return_stmt (current_class_ref);
888 finish_compound_stmt (compound_stmt);
889 }
890
891 /* Synthesize FNDECL, a non-static member function. */
892
893 void
894 synthesize_method (tree fndecl)
895 {
896 bool nested = (current_function_decl != NULL_TREE);
897 tree context = decl_function_context (fndecl);
898 bool need_body = true;
899 tree stmt;
900 location_t save_input_location = input_location;
901 int error_count = errorcount;
902 int warning_count = warningcount + werrorcount;
903
904 /* Reset the source location, we might have been previously
905 deferred, and thus have saved where we were first needed. */
906 DECL_SOURCE_LOCATION (fndecl)
907 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
908
909 /* If we've been asked to synthesize a clone, just synthesize the
910 cloned function instead. Doing so will automatically fill in the
911 body for the clone. */
912 if (DECL_CLONED_FUNCTION_P (fndecl))
913 fndecl = DECL_CLONED_FUNCTION (fndecl);
914
915 /* We may be in the middle of deferred access check. Disable
916 it now. */
917 push_deferring_access_checks (dk_no_deferred);
918
919 if (! context)
920 push_to_top_level ();
921 else if (nested)
922 push_function_context ();
923
924 input_location = DECL_SOURCE_LOCATION (fndecl);
925
926 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
927 stmt = begin_function_body ();
928
929 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
930 {
931 do_build_copy_assign (fndecl);
932 need_body = false;
933 }
934 else if (DECL_CONSTRUCTOR_P (fndecl))
935 {
936 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
937 if (arg_chain != void_list_node)
938 do_build_copy_constructor (fndecl);
939 else
940 finish_mem_initializers (NULL_TREE);
941 }
942
943 /* If we haven't yet generated the body of the function, just
944 generate an empty compound statement. */
945 if (need_body)
946 {
947 tree compound_stmt;
948 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
949 finish_compound_stmt (compound_stmt);
950 }
951
952 finish_function_body (stmt);
953 expand_or_defer_fn (finish_function (0));
954
955 input_location = save_input_location;
956
957 if (! context)
958 pop_from_top_level ();
959 else if (nested)
960 pop_function_context ();
961
962 pop_deferring_access_checks ();
963
964 if (error_count != errorcount || warning_count != warningcount + werrorcount)
965 inform (input_location, "synthesized method %qD first required here ",
966 fndecl);
967 }
968
969 /* Build a reference to type TYPE with cv-quals QUALS, which is an
970 rvalue if RVALUE is true. */
971
972 static tree
973 build_stub_type (tree type, int quals, bool rvalue)
974 {
975 tree argtype = cp_build_qualified_type (type, quals);
976 return cp_build_reference_type (argtype, rvalue);
977 }
978
979 /* Build a dummy glvalue from dereferencing a dummy reference of type
980 REFTYPE. */
981
982 static tree
983 build_stub_object (tree reftype)
984 {
985 if (TREE_CODE (reftype) != REFERENCE_TYPE)
986 reftype = cp_build_reference_type (reftype, /*rval*/true);
987 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
988 return convert_from_reference (stub);
989 }
990
991 /* Determine which function will be called when looking up NAME in TYPE,
992 called with a single ARGTYPE argument, or no argument if ARGTYPE is
993 null. FLAGS and COMPLAIN are as for build_new_method_call.
994
995 Returns a FUNCTION_DECL if all is well.
996 Returns NULL_TREE if overload resolution failed.
997 Returns error_mark_node if the chosen function cannot be called. */
998
999 static tree
1000 locate_fn_flags (tree type, tree name, tree argtype, int flags,
1001 tsubst_flags_t complain)
1002 {
1003 tree ob, fn, fns, binfo, rval;
1004 vec<tree, va_gc> *args;
1005
1006 if (TYPE_P (type))
1007 binfo = TYPE_BINFO (type);
1008 else
1009 {
1010 binfo = type;
1011 type = BINFO_TYPE (binfo);
1012 }
1013
1014 ob = build_stub_object (cp_build_reference_type (type, false));
1015 args = make_tree_vector ();
1016 if (argtype)
1017 {
1018 if (TREE_CODE (argtype) == TREE_LIST)
1019 {
1020 for (tree elt = argtype; elt && elt != void_list_node;
1021 elt = TREE_CHAIN (elt))
1022 {
1023 tree type = TREE_VALUE (elt);
1024 tree arg = build_stub_object (type);
1025 vec_safe_push (args, arg);
1026 }
1027 }
1028 else
1029 {
1030 tree arg = build_stub_object (argtype);
1031 args->quick_push (arg);
1032 }
1033 }
1034
1035 fns = lookup_fnfields (binfo, name, 0);
1036 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
1037
1038 release_tree_vector (args);
1039 if (fn && rval == error_mark_node)
1040 return rval;
1041 else
1042 return fn;
1043 }
1044
1045 /* Locate the dtor of TYPE. */
1046
1047 tree
1048 get_dtor (tree type, tsubst_flags_t complain)
1049 {
1050 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
1051 LOOKUP_NORMAL, complain);
1052 if (fn == error_mark_node)
1053 return NULL_TREE;
1054 return fn;
1055 }
1056
1057 /* Locate the default ctor of TYPE. */
1058
1059 tree
1060 locate_ctor (tree type)
1061 {
1062 tree fn;
1063
1064 push_deferring_access_checks (dk_no_check);
1065 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
1066 LOOKUP_SPECULATIVE, tf_none);
1067 pop_deferring_access_checks ();
1068 if (fn == error_mark_node)
1069 return NULL_TREE;
1070 return fn;
1071 }
1072
1073 /* Likewise, but give any appropriate errors. */
1074
1075 tree
1076 get_default_ctor (tree type)
1077 {
1078 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
1079 LOOKUP_NORMAL, tf_warning_or_error);
1080 if (fn == error_mark_node)
1081 return NULL_TREE;
1082 return fn;
1083 }
1084
1085 /* Locate the copy ctor of TYPE. */
1086
1087 tree
1088 get_copy_ctor (tree type, tsubst_flags_t complain)
1089 {
1090 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
1091 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
1092 tree argtype = build_stub_type (type, quals, false);
1093 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
1094 LOOKUP_NORMAL, complain);
1095 if (fn == error_mark_node)
1096 return NULL_TREE;
1097 return fn;
1098 }
1099
1100 /* Locate the copy assignment operator of TYPE. */
1101
1102 tree
1103 get_copy_assign (tree type)
1104 {
1105 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
1106 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
1107 tree argtype = build_stub_type (type, quals, false);
1108 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
1109 LOOKUP_NORMAL, tf_warning_or_error);
1110 if (fn == error_mark_node)
1111 return NULL_TREE;
1112 return fn;
1113 }
1114
1115 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
1116 return it if it calls something other than a trivial special member
1117 function. */
1118
1119 static tree
1120 check_nontriv (tree *tp, int *, void *)
1121 {
1122 tree fn = cp_get_callee (*tp);
1123 if (fn == NULL_TREE)
1124 return NULL_TREE;
1125
1126 if (TREE_CODE (fn) == ADDR_EXPR)
1127 fn = TREE_OPERAND (fn, 0);
1128
1129 if (TREE_CODE (fn) != FUNCTION_DECL
1130 || !trivial_fn_p (fn))
1131 return fn;
1132 return NULL_TREE;
1133 }
1134
1135 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1136
1137 static tree
1138 assignable_expr (tree to, tree from)
1139 {
1140 ++cp_unevaluated_operand;
1141 to = build_stub_object (to);
1142 from = build_stub_object (from);
1143 tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, tf_none);
1144 --cp_unevaluated_operand;
1145 return r;
1146 }
1147
1148 /* The predicate condition for a template specialization
1149 is_constructible<T, Args...> shall be satisfied if and only if the
1150 following variable definition would be well-formed for some invented
1151 variable t: T t(create<Args>()...);
1152
1153 Return something equivalent in well-formedness and triviality. */
1154
1155 static tree
1156 constructible_expr (tree to, tree from)
1157 {
1158 tree expr;
1159 if (CLASS_TYPE_P (to))
1160 {
1161 tree ctype = to;
1162 vec<tree, va_gc> *args = NULL;
1163 if (TREE_CODE (to) != REFERENCE_TYPE)
1164 to = cp_build_reference_type (to, /*rval*/false);
1165 tree ob = build_stub_object (to);
1166 for (; from; from = TREE_CHAIN (from))
1167 vec_safe_push (args, build_stub_object (TREE_VALUE (from)));
1168 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
1169 ctype, LOOKUP_NORMAL, tf_none);
1170 if (expr == error_mark_node)
1171 return error_mark_node;
1172 /* The current state of the standard vis-a-vis LWG 2116 is that
1173 is_*constructible involves destruction as well. */
1174 if (type_build_dtor_call (ctype))
1175 {
1176 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
1177 NULL, ctype, LOOKUP_NORMAL,
1178 tf_none);
1179 if (dtor == error_mark_node)
1180 return error_mark_node;
1181 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
1182 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
1183 }
1184 }
1185 else
1186 {
1187 if (from == NULL_TREE)
1188 return build_value_init (to, tf_none);
1189 else if (TREE_CHAIN (from))
1190 return error_mark_node; // too many initializers
1191 from = build_stub_object (TREE_VALUE (from));
1192 expr = perform_direct_initialization_if_possible (to, from,
1193 /*cast*/false,
1194 tf_none);
1195 }
1196 return expr;
1197 }
1198
1199 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1200 constructible (otherwise) from FROM, which is a single type for
1201 assignment or a list of types for construction. */
1202
1203 bool
1204 is_trivially_xible (enum tree_code code, tree to, tree from)
1205 {
1206 tree expr;
1207 if (code == MODIFY_EXPR)
1208 expr = assignable_expr (to, from);
1209 else if (from && TREE_CHAIN (from))
1210 return false; // only 0- and 1-argument ctors can be trivial
1211 else
1212 expr = constructible_expr (to, from);
1213
1214 if (expr == error_mark_node)
1215 return false;
1216 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
1217 return !nt;
1218 }
1219
1220 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1221 DELETED_P or give an error message MSG with argument ARG. */
1222
1223 static void
1224 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
1225 bool *deleted_p, bool *constexpr_p,
1226 bool diag, tree arg, bool dtor_from_ctor = false)
1227 {
1228 if (!fn || fn == error_mark_node)
1229 {
1230 if (deleted_p)
1231 *deleted_p = true;
1232 return;
1233 }
1234
1235 if (spec_p)
1236 {
1237 maybe_instantiate_noexcept (fn);
1238 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1239 *spec_p = merge_exception_specifiers (*spec_p, raises);
1240 }
1241
1242 if (!trivial_fn_p (fn) && !dtor_from_ctor)
1243 {
1244 if (trivial_p)
1245 *trivial_p = false;
1246 if (TREE_CODE (arg) == FIELD_DECL
1247 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1248 {
1249 if (deleted_p)
1250 *deleted_p = true;
1251 if (diag)
1252 error ("union member %q+D with non-trivial %qD", arg, fn);
1253 }
1254 }
1255
1256 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1257 {
1258 *constexpr_p = false;
1259 if (diag)
1260 {
1261 inform (DECL_SOURCE_LOCATION (fn),
1262 "defaulted constructor calls non-constexpr %qD", fn);
1263 explain_invalid_constexpr_fn (fn);
1264 }
1265 }
1266 }
1267
1268 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1269 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
1270 called from a synthesized constructor, in which case we don't consider
1271 the triviality of the subobject destructor. */
1272
1273 static void
1274 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1275 int quals, bool copy_arg_p, bool move_p,
1276 bool assign_p, tree *spec_p, bool *trivial_p,
1277 bool *deleted_p, bool *constexpr_p,
1278 bool diag, int flags, tsubst_flags_t complain,
1279 bool dtor_from_ctor)
1280 {
1281 tree field;
1282 for (field = fields; field; field = DECL_CHAIN (field))
1283 {
1284 tree mem_type, argtype, rval;
1285
1286 if (TREE_CODE (field) != FIELD_DECL
1287 || DECL_ARTIFICIAL (field))
1288 continue;
1289
1290 mem_type = strip_array_types (TREE_TYPE (field));
1291 if (assign_p)
1292 {
1293 bool bad = true;
1294 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1295 {
1296 if (diag)
1297 error ("non-static const member %q#D, can%'t use default "
1298 "assignment operator", field);
1299 }
1300 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1301 {
1302 if (diag)
1303 error ("non-static reference member %q#D, can%'t use "
1304 "default assignment operator", field);
1305 }
1306 else
1307 bad = false;
1308
1309 if (bad && deleted_p)
1310 *deleted_p = true;
1311 }
1312 else if (sfk == sfk_constructor)
1313 {
1314 bool bad;
1315
1316 if (DECL_INITIAL (field))
1317 {
1318 if (diag && DECL_INITIAL (field) == error_mark_node)
1319 inform (DECL_SOURCE_LOCATION (field),
1320 "initializer for %q#D is invalid", field);
1321 if (trivial_p)
1322 *trivial_p = false;
1323 /* Core 1351: If the field has an NSDMI that could throw, the
1324 default constructor is noexcept(false). */
1325 if (spec_p)
1326 {
1327 tree nsdmi = get_nsdmi (field, /*ctor*/false);
1328 if (!expr_noexcept_p (nsdmi, complain))
1329 *spec_p = noexcept_false_spec;
1330 }
1331 /* Don't do the normal processing. */
1332 continue;
1333 }
1334
1335 bad = false;
1336 if (CP_TYPE_CONST_P (mem_type)
1337 && default_init_uninitialized_part (mem_type))
1338 {
1339 if (diag)
1340 {
1341 error ("uninitialized const member in %q#T",
1342 current_class_type);
1343 inform (DECL_SOURCE_LOCATION (field),
1344 "%q#D should be initialized", field);
1345 }
1346 bad = true;
1347 }
1348 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1349 {
1350 if (diag)
1351 {
1352 error ("uninitialized reference member in %q#T",
1353 current_class_type);
1354 inform (DECL_SOURCE_LOCATION (field),
1355 "%q#D should be initialized", field);
1356 }
1357 bad = true;
1358 }
1359
1360 if (bad && deleted_p)
1361 *deleted_p = true;
1362
1363 /* For an implicitly-defined default constructor to be constexpr,
1364 every member must have a user-provided default constructor or
1365 an explicit initializer. */
1366 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1367 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1368 {
1369 *constexpr_p = false;
1370 if (diag)
1371 inform (DECL_SOURCE_LOCATION (field),
1372 "defaulted default constructor does not "
1373 "initialize %q#D", field);
1374 }
1375 }
1376 else if (sfk == sfk_copy_constructor)
1377 {
1378 /* 12.8p11b5 */
1379 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1380 && TYPE_REF_IS_RVALUE (mem_type))
1381 {
1382 if (diag)
1383 error ("copying non-static data member %q#D of rvalue "
1384 "reference type", field);
1385 if (deleted_p)
1386 *deleted_p = true;
1387 }
1388 }
1389
1390 if (!CLASS_TYPE_P (mem_type))
1391 continue;
1392
1393 if (ANON_AGGR_TYPE_P (mem_type))
1394 {
1395 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1396 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1397 deleted_p, constexpr_p,
1398 diag, flags, complain, dtor_from_ctor);
1399 continue;
1400 }
1401
1402 if (copy_arg_p)
1403 {
1404 int mem_quals = cp_type_quals (mem_type) | quals;
1405 if (DECL_MUTABLE_P (field))
1406 mem_quals &= ~TYPE_QUAL_CONST;
1407 argtype = build_stub_type (mem_type, mem_quals, move_p);
1408 }
1409 else
1410 argtype = NULL_TREE;
1411
1412 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1413
1414 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1415 constexpr_p, diag, field, dtor_from_ctor);
1416 }
1417 }
1418
1419 /* The caller wants to generate an implicit declaration of SFK for
1420 CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
1421 TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
1422 referent appropriately. If DIAG is true, we're either being called
1423 from maybe_explain_implicit_delete to give errors, or if
1424 CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
1425
1426 static void
1427 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1428 tree *spec_p, bool *trivial_p, bool *deleted_p,
1429 bool *constexpr_p, bool diag,
1430 tree inheriting_ctor, tree inherited_parms)
1431 {
1432 tree binfo, base_binfo, scope, fnname, rval, argtype;
1433 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1434 vec<tree, va_gc> *vbases;
1435 int i, quals, flags;
1436 tsubst_flags_t complain;
1437 bool ctor_p;
1438
1439 if (spec_p)
1440 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1441
1442 if (deleted_p)
1443 {
1444 /* "The closure type associated with a lambda-expression has a deleted
1445 default constructor and a deleted copy assignment operator."
1446 This is diagnosed in maybe_explain_implicit_delete. */
1447 if (LAMBDA_TYPE_P (ctype)
1448 && (sfk == sfk_constructor
1449 || sfk == sfk_copy_assignment))
1450 {
1451 *deleted_p = true;
1452 return;
1453 }
1454
1455 *deleted_p = false;
1456 }
1457
1458 ctor_p = false;
1459 assign_p = false;
1460 check_vdtor = false;
1461 switch (sfk)
1462 {
1463 case sfk_move_assignment:
1464 case sfk_copy_assignment:
1465 assign_p = true;
1466 fnname = ansi_assopname (NOP_EXPR);
1467 break;
1468
1469 case sfk_destructor:
1470 check_vdtor = true;
1471 /* The synthesized method will call base dtors, but check complete
1472 here to avoid having to deal with VTT. */
1473 fnname = complete_dtor_identifier;
1474 break;
1475
1476 case sfk_constructor:
1477 case sfk_move_constructor:
1478 case sfk_copy_constructor:
1479 case sfk_inheriting_constructor:
1480 ctor_p = true;
1481 fnname = complete_ctor_identifier;
1482 break;
1483
1484 default:
1485 gcc_unreachable ();
1486 }
1487
1488 gcc_assert ((sfk == sfk_inheriting_constructor)
1489 == (inheriting_ctor != NULL_TREE));
1490
1491 /* If that user-written default constructor would satisfy the
1492 requirements of a constexpr constructor (7.1.5), the
1493 implicitly-defined default constructor is constexpr.
1494
1495 The implicitly-defined copy/move assignment operator is constexpr if
1496 - X is a literal type, and
1497 - the assignment operator selected to copy/move each direct base class
1498 subobject is a constexpr function, and
1499 - for each non-static data member of X that is of class type (or array
1500 thereof), the assignment operator selected to copy/move that member is a
1501 constexpr function. */
1502 if (constexpr_p)
1503 *constexpr_p = ctor_p
1504 || (assign_p && cxx_dialect >= cxx14);
1505
1506 move_p = false;
1507 switch (sfk)
1508 {
1509 case sfk_constructor:
1510 case sfk_destructor:
1511 case sfk_inheriting_constructor:
1512 copy_arg_p = false;
1513 break;
1514
1515 case sfk_move_constructor:
1516 case sfk_move_assignment:
1517 move_p = true;
1518 /* FALLTHRU */
1519 case sfk_copy_constructor:
1520 case sfk_copy_assignment:
1521 copy_arg_p = true;
1522 break;
1523
1524 default:
1525 gcc_unreachable ();
1526 }
1527
1528 expected_trivial = type_has_trivial_fn (ctype, sfk);
1529 if (trivial_p)
1530 *trivial_p = expected_trivial;
1531
1532 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1533 class versions and other properties of the type. But a subobject
1534 class can be trivially copyable and yet have overload resolution
1535 choose a template constructor for initialization, depending on
1536 rvalueness and cv-quals. And furthermore, a member in a base might
1537 be trivial but deleted or otherwise not callable. So we can't exit
1538 early in C++0x. The same considerations apply in C++98/03, but
1539 there the definition of triviality does not consider overload
1540 resolution, so a constructor can be trivial even if it would otherwise
1541 call a non-trivial constructor. */
1542 if (expected_trivial
1543 && (!copy_arg_p || cxx_dialect < cxx11))
1544 {
1545 if (constexpr_p && sfk == sfk_constructor)
1546 {
1547 bool cx = trivial_default_constructor_is_constexpr (ctype);
1548 *constexpr_p = cx;
1549 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1550 /* A trivial constructor doesn't have any NSDMI. */
1551 inform (input_location, "defaulted default constructor does "
1552 "not initialize any non-static data member");
1553 }
1554 if (!diag && cxx_dialect < cxx11)
1555 return;
1556 }
1557
1558 ++cp_unevaluated_operand;
1559 ++c_inhibit_evaluation_warnings;
1560 push_deferring_access_checks (dk_no_deferred);
1561
1562 scope = push_scope (ctype);
1563
1564 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE;
1565 if (!inheriting_ctor)
1566 flags |= LOOKUP_DEFAULTED;
1567
1568 complain = diag ? tf_warning_or_error : tf_none;
1569
1570 if (const_p)
1571 quals = TYPE_QUAL_CONST;
1572 else
1573 quals = TYPE_UNQUALIFIED;
1574 argtype = NULL_TREE;
1575
1576 for (binfo = TYPE_BINFO (ctype), i = 0;
1577 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1578 {
1579 tree basetype = BINFO_TYPE (base_binfo);
1580
1581 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1582 /* We'll handle virtual bases below. */
1583 continue;
1584
1585 bool inherited_binfo = false;
1586
1587 if (copy_arg_p)
1588 argtype = build_stub_type (basetype, quals, move_p);
1589 else if ((inherited_binfo
1590 = binfo_inherited_from (binfo, base_binfo, inheriting_ctor)))
1591 {
1592 /* Don't check access on the inherited constructor. */
1593 argtype = inherited_parms;
1594 if (flag_new_inheriting_ctors)
1595 push_deferring_access_checks (dk_deferred);
1596 }
1597 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1598 if (inherited_binfo)
1599 {
1600 if (flag_new_inheriting_ctors)
1601 pop_deferring_access_checks ();
1602 argtype = NULL_TREE;
1603 }
1604
1605 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1606 constexpr_p, diag, basetype);
1607 if (ctor_p)
1608 {
1609 /* In a constructor we also need to check the subobject
1610 destructors for cleanup of partially constructed objects. */
1611 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1612 NULL_TREE, flags, complain);
1613 /* Note that we don't pass down trivial_p; the subobject
1614 destructors don't affect triviality of the constructor. Nor
1615 do they affect constexpr-ness (a constant expression doesn't
1616 throw) or exception-specification (a throw from one of the
1617 dtors would be a double-fault). */
1618 process_subob_fn (rval, NULL, NULL,
1619 deleted_p, NULL, false,
1620 basetype, /*dtor_from_ctor*/true);
1621 }
1622
1623 if (check_vdtor && type_has_virtual_destructor (basetype))
1624 {
1625 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1626 ptr_type_node, flags, complain);
1627 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1628 to have a null rval (no class-specific op delete). */
1629 if (rval && rval == error_mark_node && deleted_p)
1630 *deleted_p = true;
1631 check_vdtor = false;
1632 }
1633
1634 if (diag && assign_p && move_p
1635 && BINFO_VIRTUAL_P (base_binfo)
1636 && rval && TREE_CODE (rval) == FUNCTION_DECL
1637 && move_fn_p (rval) && !trivial_fn_p (rval)
1638 && vbase_has_user_provided_move_assign (basetype))
1639 warning (OPT_Wvirtual_move_assign,
1640 "defaulted move assignment for %qT calls a non-trivial "
1641 "move assignment operator for virtual base %qT",
1642 ctype, basetype);
1643 }
1644
1645 vbases = CLASSTYPE_VBASECLASSES (ctype);
1646 if (assign_p)
1647 /* No need to examine vbases here. */;
1648 else if (vec_safe_is_empty (vbases))
1649 /* No virtual bases to worry about. */;
1650 else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14)
1651 /* Vbase cdtors are not relevant. */;
1652 else
1653 {
1654 if (constexpr_p)
1655 *constexpr_p = false;
1656 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1657 {
1658 tree basetype = BINFO_TYPE (base_binfo);
1659 bool inherited_binfo = false;
1660
1661 if (copy_arg_p)
1662 argtype = build_stub_type (basetype, quals, move_p);
1663 else if ((inherited_binfo
1664 = binfo_inherited_from (binfo, base_binfo, inheriting_ctor)))
1665 {
1666 argtype = inherited_parms;
1667 if (flag_new_inheriting_ctors)
1668 push_deferring_access_checks (dk_deferred);
1669 }
1670 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1671 if (inherited_binfo)
1672 {
1673 if (flag_new_inheriting_ctors)
1674 pop_deferring_access_checks ();
1675 argtype = NULL_TREE;
1676 }
1677
1678 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1679 constexpr_p, diag, basetype);
1680 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1681 {
1682 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1683 NULL_TREE, flags, complain);
1684 process_subob_fn (rval, NULL, NULL,
1685 deleted_p, NULL, false,
1686 basetype, /*dtor_from_ctor*/true);
1687 }
1688 }
1689 }
1690
1691 /* Now handle the non-static data members. */
1692 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1693 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1694 deleted_p, constexpr_p,
1695 diag, flags, complain, /*dtor_from_ctor*/false);
1696 if (ctor_p)
1697 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1698 sfk_destructor, TYPE_UNQUALIFIED, false,
1699 false, false, NULL, NULL,
1700 deleted_p, NULL,
1701 false, flags, complain, /*dtor_from_ctor*/true);
1702
1703 pop_scope (scope);
1704
1705 pop_deferring_access_checks ();
1706 --cp_unevaluated_operand;
1707 --c_inhibit_evaluation_warnings;
1708 }
1709
1710 /* DECL is a defaulted function whose exception specification is now
1711 needed. Return what it should be. */
1712
1713 tree
1714 get_defaulted_eh_spec (tree decl)
1715 {
1716 if (DECL_CLONED_FUNCTION_P (decl))
1717 decl = DECL_CLONED_FUNCTION (decl);
1718 special_function_kind sfk = special_function_p (decl);
1719 tree ctype = DECL_CONTEXT (decl);
1720 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1721 tree parm_type = TREE_VALUE (parms);
1722 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1723 tree spec = empty_except_spec;
1724 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
1725 NULL, false, DECL_INHERITED_CTOR (decl),
1726 parms);
1727 return spec;
1728 }
1729
1730 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1731 return true; else return false. */
1732
1733 bool
1734 maybe_explain_implicit_delete (tree decl)
1735 {
1736 /* If decl is a clone, get the primary variant. */
1737 decl = DECL_ORIGIN (decl);
1738 gcc_assert (DECL_DELETED_FN (decl));
1739 if (DECL_DEFAULTED_FN (decl))
1740 {
1741 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1742 static hash_set<tree> *explained;
1743
1744 special_function_kind sfk;
1745 location_t loc;
1746 bool informed;
1747 tree ctype;
1748
1749 if (!explained)
1750 explained = new hash_set<tree>;
1751 if (explained->add (decl))
1752 return true;
1753
1754 sfk = special_function_p (decl);
1755 ctype = DECL_CONTEXT (decl);
1756 loc = input_location;
1757 input_location = DECL_SOURCE_LOCATION (decl);
1758
1759 informed = false;
1760 if (LAMBDA_TYPE_P (ctype))
1761 {
1762 informed = true;
1763 if (sfk == sfk_constructor)
1764 inform (DECL_SOURCE_LOCATION (decl),
1765 "a lambda closure type has a deleted default constructor");
1766 else if (sfk == sfk_copy_assignment)
1767 inform (DECL_SOURCE_LOCATION (decl),
1768 "a lambda closure type has a deleted copy assignment operator");
1769 else
1770 informed = false;
1771 }
1772 else if (DECL_ARTIFICIAL (decl)
1773 && (sfk == sfk_copy_assignment
1774 || sfk == sfk_copy_constructor)
1775 && (type_has_user_declared_move_constructor (ctype)
1776 || type_has_user_declared_move_assign (ctype)))
1777 {
1778 inform (DECL_SOURCE_LOCATION (decl),
1779 "%q#D is implicitly declared as deleted because %qT "
1780 "declares a move constructor or move assignment operator",
1781 decl, ctype);
1782 informed = true;
1783 }
1784 else if (sfk == sfk_inheriting_constructor)
1785 {
1786 tree binfo = inherited_ctor_binfo (decl);
1787 if (TREE_CODE (binfo) != TREE_BINFO)
1788 {
1789 inform (DECL_SOURCE_LOCATION (decl),
1790 "%q#D inherits from multiple base subobjects",
1791 decl);
1792 informed = true;
1793 }
1794 }
1795 if (!informed)
1796 {
1797 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1798 tree parm_type = TREE_VALUE (parms);
1799 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1800 tree raises = NULL_TREE;
1801 bool deleted_p = false;
1802 tree scope = push_scope (ctype);
1803
1804 synthesized_method_walk (ctype, sfk, const_p,
1805 &raises, NULL, &deleted_p, NULL, false,
1806 DECL_INHERITED_CTOR (decl), parms);
1807 if (deleted_p)
1808 {
1809 inform (DECL_SOURCE_LOCATION (decl),
1810 "%q#D is implicitly deleted because the default "
1811 "definition would be ill-formed:", decl);
1812 synthesized_method_walk (ctype, sfk, const_p,
1813 NULL, NULL, NULL, NULL, true,
1814 DECL_INHERITED_CTOR (decl), parms);
1815 }
1816 else if (!comp_except_specs
1817 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1818 raises, ce_normal))
1819 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1820 "deleted because its exception-specification does not "
1821 "match the implicit exception-specification %qX",
1822 decl, raises);
1823 else if (flag_checking)
1824 gcc_unreachable ();
1825
1826 pop_scope (scope);
1827 }
1828
1829 input_location = loc;
1830 return true;
1831 }
1832 return false;
1833 }
1834
1835 /* DECL is a defaulted function which was declared constexpr. Explain why
1836 it can't be constexpr. */
1837
1838 void
1839 explain_implicit_non_constexpr (tree decl)
1840 {
1841 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1842 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1843 bool dummy;
1844 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1845 special_function_p (decl), const_p,
1846 NULL, NULL, NULL, &dummy, true,
1847 DECL_INHERITED_CTOR (decl),
1848 FUNCTION_FIRST_USER_PARMTYPE (decl));
1849 }
1850
1851 /* DECL is an instantiation of an inheriting constructor template. Deduce
1852 the correct exception-specification and deletedness for this particular
1853 specialization. */
1854
1855 void
1856 deduce_inheriting_ctor (tree decl)
1857 {
1858 gcc_assert (DECL_INHERITED_CTOR (decl));
1859 tree spec;
1860 bool trivial, constexpr_, deleted;
1861 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1862 false, &spec, &trivial, &deleted, &constexpr_,
1863 /*diag*/false,
1864 DECL_INHERITED_CTOR (decl),
1865 FUNCTION_FIRST_USER_PARMTYPE (decl));
1866 if (TREE_CODE (inherited_ctor_binfo (decl)) != TREE_BINFO)
1867 /* Inherited the same constructor from different base subobjects. */
1868 deleted = true;
1869 DECL_DELETED_FN (decl) = deleted;
1870 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1871 }
1872
1873 /* Implicitly declare the special function indicated by KIND, as a
1874 member of TYPE. For copy constructors and assignment operators,
1875 CONST_P indicates whether these functions should take a const
1876 reference argument or a non-const reference. Returns the
1877 FUNCTION_DECL for the implicitly declared function. */
1878
1879 tree
1880 implicitly_declare_fn (special_function_kind kind, tree type,
1881 bool const_p, tree inherited_ctor,
1882 tree inherited_parms)
1883 {
1884 tree fn;
1885 tree parameter_types = void_list_node;
1886 tree return_type;
1887 tree fn_type;
1888 tree raises = empty_except_spec;
1889 tree rhs_parm_type = NULL_TREE;
1890 tree this_parm;
1891 tree name;
1892 HOST_WIDE_INT saved_processing_template_decl;
1893 bool deleted_p;
1894 bool constexpr_p;
1895
1896 /* Because we create declarations for implicitly declared functions
1897 lazily, we may be creating the declaration for a member of TYPE
1898 while in some completely different context. However, TYPE will
1899 never be a dependent class (because we never want to do lookups
1900 for implicitly defined functions in a dependent class).
1901 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1902 because we only create clones for constructors and destructors
1903 when not in a template. */
1904 gcc_assert (!dependent_type_p (type));
1905 saved_processing_template_decl = processing_template_decl;
1906 processing_template_decl = 0;
1907
1908 type = TYPE_MAIN_VARIANT (type);
1909
1910 if (targetm.cxx.cdtor_returns_this ())
1911 {
1912 if (kind == sfk_destructor)
1913 /* See comment in check_special_function_return_type. */
1914 return_type = build_pointer_type (void_type_node);
1915 else
1916 return_type = build_pointer_type (type);
1917 }
1918 else
1919 return_type = void_type_node;
1920
1921 switch (kind)
1922 {
1923 case sfk_destructor:
1924 /* Destructor. */
1925 name = constructor_name (type);
1926 break;
1927
1928 case sfk_constructor:
1929 /* Default constructor. */
1930 name = constructor_name (type);
1931 break;
1932
1933 case sfk_copy_constructor:
1934 case sfk_copy_assignment:
1935 case sfk_move_constructor:
1936 case sfk_move_assignment:
1937 case sfk_inheriting_constructor:
1938 {
1939 bool move_p;
1940 if (kind == sfk_copy_assignment
1941 || kind == sfk_move_assignment)
1942 {
1943 return_type = build_reference_type (type);
1944 name = ansi_assopname (NOP_EXPR);
1945 }
1946 else
1947 name = constructor_name (type);
1948
1949 if (kind == sfk_inheriting_constructor)
1950 parameter_types = inherited_parms;
1951 else
1952 {
1953 if (const_p)
1954 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1955 else
1956 rhs_parm_type = type;
1957 move_p = (kind == sfk_move_assignment
1958 || kind == sfk_move_constructor);
1959 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1960
1961 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1962 }
1963 break;
1964 }
1965 default:
1966 gcc_unreachable ();
1967 }
1968
1969 bool trivial_p = false;
1970
1971 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1972 {
1973 /* For an inheriting constructor template, just copy these flags from
1974 the inherited constructor template for now. */
1975 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
1976 deleted_p = DECL_DELETED_FN (inherited_ctor);
1977 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1978 }
1979 else if (cxx_dialect >= cxx11)
1980 {
1981 raises = unevaluated_noexcept_spec ();
1982 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
1983 &deleted_p, &constexpr_p, false,
1984 inherited_ctor, inherited_parms);
1985 }
1986 else
1987 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1988 &deleted_p, &constexpr_p, false,
1989 inherited_ctor, inherited_parms);
1990 /* Don't bother marking a deleted constructor as constexpr. */
1991 if (deleted_p)
1992 constexpr_p = false;
1993 /* A trivial copy/move constructor is also a constexpr constructor,
1994 unless the class has virtual bases (7.1.5p4). */
1995 else if (trivial_p && cxx_dialect >= cxx11
1996 && (kind == sfk_copy_constructor
1997 || kind == sfk_move_constructor)
1998 && !CLASSTYPE_VBASECLASSES (type))
1999 gcc_assert (constexpr_p);
2000
2001 if (!trivial_p && type_has_trivial_fn (type, kind))
2002 type_set_nontrivial_flag (type, kind);
2003
2004 /* Create the function. */
2005 fn_type = build_method_type_directly (type, return_type, parameter_types);
2006 if (raises)
2007 fn_type = build_exception_variant (fn_type, raises);
2008 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
2009 if (kind != sfk_inheriting_constructor)
2010 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
2011 if (kind == sfk_constructor || kind == sfk_copy_constructor
2012 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
2013 DECL_CONSTRUCTOR_P (fn) = 1;
2014 else if (kind == sfk_destructor)
2015 DECL_DESTRUCTOR_P (fn) = 1;
2016 else
2017 {
2018 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
2019 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
2020 }
2021
2022 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
2023
2024 /* Create the explicit arguments. */
2025 if (rhs_parm_type)
2026 {
2027 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
2028 want its type to be included in the mangled function
2029 name. */
2030 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
2031 TREE_READONLY (decl) = 1;
2032 retrofit_lang_decl (decl);
2033 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
2034 DECL_ARGUMENTS (fn) = decl;
2035 }
2036 else if (kind == sfk_inheriting_constructor)
2037 {
2038 tree *p = &DECL_ARGUMENTS (fn);
2039 int index = 1;
2040 for (tree parm = inherited_parms; parm && parm != void_list_node;
2041 parm = TREE_CHAIN (parm))
2042 {
2043 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
2044 retrofit_lang_decl (*p);
2045 DECL_PARM_LEVEL (*p) = 1;
2046 DECL_PARM_INDEX (*p) = index++;
2047 DECL_CONTEXT (*p) = fn;
2048 p = &DECL_CHAIN (*p);
2049 }
2050 SET_DECL_INHERITED_CTOR (fn, inherited_ctor);
2051 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
2052 /* A constructor so declared has the same access as the corresponding
2053 constructor in X. */
2054 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
2055 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
2056 /* Copy constexpr from the inherited constructor even if the
2057 inheriting constructor doesn't satisfy the requirements. */
2058 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
2059 }
2060 /* Add the "this" parameter. */
2061 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
2062 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
2063 DECL_ARGUMENTS (fn) = this_parm;
2064
2065 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
2066 DECL_IN_AGGR_P (fn) = 1;
2067 DECL_ARTIFICIAL (fn) = 1;
2068 DECL_DEFAULTED_FN (fn) = 1;
2069 if (cxx_dialect >= cxx11)
2070 {
2071 DECL_DELETED_FN (fn) = deleted_p;
2072 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
2073 }
2074 DECL_EXTERNAL (fn) = true;
2075 DECL_NOT_REALLY_EXTERN (fn) = 1;
2076 DECL_DECLARED_INLINE_P (fn) = 1;
2077 set_linkage_according_to_type (type, fn);
2078 if (TREE_PUBLIC (fn))
2079 DECL_COMDAT (fn) = 1;
2080 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
2081 gcc_assert (!TREE_USED (fn));
2082
2083 /* Propagate constraints from the inherited constructor. */
2084 if (flag_concepts && inherited_ctor)
2085 if (tree orig_ci = get_constraints (inherited_ctor))
2086 {
2087 tree new_ci = copy_node (orig_ci);
2088 set_constraints (fn, new_ci);
2089 }
2090
2091 /* Restore PROCESSING_TEMPLATE_DECL. */
2092 processing_template_decl = saved_processing_template_decl;
2093
2094 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
2095 fn = add_inherited_template_parms (fn, inherited_ctor);
2096
2097 /* Warn about calling a non-trivial move assignment in a virtual base. */
2098 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
2099 && CLASSTYPE_VBASECLASSES (type))
2100 {
2101 location_t loc = input_location;
2102 input_location = DECL_SOURCE_LOCATION (fn);
2103 synthesized_method_walk (type, kind, const_p,
2104 NULL, NULL, NULL, NULL, true,
2105 NULL_TREE, NULL_TREE);
2106 input_location = loc;
2107 }
2108
2109 return fn;
2110 }
2111
2112 /* Gives any errors about defaulted functions which need to be deferred
2113 until the containing class is complete. */
2114
2115 void
2116 defaulted_late_check (tree fn)
2117 {
2118 /* Complain about invalid signature for defaulted fn. */
2119 tree ctx = DECL_CONTEXT (fn);
2120 special_function_kind kind = special_function_p (fn);
2121 bool fn_const_p = (copy_fn_p (fn) == 2);
2122 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
2123 NULL, NULL);
2124 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
2125
2126 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
2127 TREE_TYPE (TREE_TYPE (implicit_fn)))
2128 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2129 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
2130 {
2131 error ("defaulted declaration %q+D", fn);
2132 error_at (DECL_SOURCE_LOCATION (fn),
2133 "does not match expected signature %qD", implicit_fn);
2134 }
2135
2136 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
2137 exception-specification only if it is compatible (15.4) with the
2138 exception-specification on the implicit declaration. If a function
2139 is explicitly defaulted on its first declaration, (...) it is
2140 implicitly considered to have the same exception-specification as if
2141 it had been implicitly declared. */
2142 maybe_instantiate_noexcept (fn);
2143 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2144 if (!fn_spec)
2145 {
2146 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2147 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
2148 }
2149 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2150 /* Equivalent to the implicit spec. */;
2151 else if (DECL_DEFAULTED_IN_CLASS_P (fn)
2152 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2153 /* We can't compare an explicit exception-specification on a
2154 constructor defaulted in the class body to the implicit
2155 exception-specification until after we've parsed any NSDMI; see
2156 after_nsdmi_defaulted_late_checks. */;
2157 else
2158 {
2159 tree eh_spec = get_defaulted_eh_spec (fn);
2160 if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
2161 {
2162 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2163 DECL_DELETED_FN (fn) = true;
2164 else
2165 error ("function %q+D defaulted on its redeclaration "
2166 "with an exception-specification that differs from "
2167 "the implicit exception-specification %qX", fn, eh_spec);
2168 }
2169 }
2170
2171 if (DECL_DEFAULTED_IN_CLASS_P (fn)
2172 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
2173 {
2174 /* Hmm...should we do this for out-of-class too? Should it be OK to
2175 add constexpr later like inline, rather than requiring
2176 declarations to match? */
2177 DECL_DECLARED_CONSTEXPR_P (fn) = true;
2178 if (kind == sfk_constructor)
2179 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
2180 }
2181
2182 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
2183 && DECL_DECLARED_CONSTEXPR_P (fn))
2184 {
2185 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2186 {
2187 error ("explicitly defaulted function %q+D cannot be declared "
2188 "as constexpr because the implicit declaration is not "
2189 "constexpr:", fn);
2190 explain_implicit_non_constexpr (fn);
2191 }
2192 DECL_DECLARED_CONSTEXPR_P (fn) = false;
2193 }
2194
2195 if (DECL_DELETED_FN (implicit_fn))
2196 DECL_DELETED_FN (fn) = 1;
2197 }
2198
2199 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
2200 exception-specifications on functions defaulted in the class body. */
2201
2202 void
2203 after_nsdmi_defaulted_late_checks (tree t)
2204 {
2205 if (uses_template_parms (t))
2206 return;
2207 if (t == error_mark_node)
2208 return;
2209 for (tree fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2210 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
2211 {
2212 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2213 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2214 continue;
2215
2216 tree eh_spec = get_defaulted_eh_spec (fn);
2217 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
2218 eh_spec, ce_normal))
2219 DECL_DELETED_FN (fn) = true;
2220 }
2221 }
2222
2223 /* Returns true iff FN can be explicitly defaulted, and gives any
2224 errors if defaulting FN is ill-formed. */
2225
2226 bool
2227 defaultable_fn_check (tree fn)
2228 {
2229 special_function_kind kind = sfk_none;
2230
2231 if (template_parm_scope_p ())
2232 {
2233 error ("a template cannot be defaulted");
2234 return false;
2235 }
2236
2237 if (DECL_CONSTRUCTOR_P (fn))
2238 {
2239 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
2240 kind = sfk_constructor;
2241 else if (copy_fn_p (fn) > 0
2242 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
2243 == void_list_node))
2244 kind = sfk_copy_constructor;
2245 else if (move_fn_p (fn))
2246 kind = sfk_move_constructor;
2247 }
2248 else if (DECL_DESTRUCTOR_P (fn))
2249 kind = sfk_destructor;
2250 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2251 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
2252 {
2253 if (copy_fn_p (fn))
2254 kind = sfk_copy_assignment;
2255 else if (move_fn_p (fn))
2256 kind = sfk_move_assignment;
2257 }
2258
2259 if (kind == sfk_none)
2260 {
2261 error ("%qD cannot be defaulted", fn);
2262 return false;
2263 }
2264 else
2265 {
2266 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
2267 t && t != void_list_node; t = TREE_CHAIN (t))
2268 if (TREE_PURPOSE (t))
2269 {
2270 error ("defaulted function %q+D with default argument", fn);
2271 break;
2272 }
2273
2274 /* Avoid do_warn_unused_parameter warnings. */
2275 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
2276 if (DECL_NAME (p))
2277 TREE_NO_WARNING (p) = 1;
2278
2279 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
2280 /* Defer checking. */;
2281 else if (!processing_template_decl)
2282 defaulted_late_check (fn);
2283
2284 return true;
2285 }
2286 }
2287
2288 /* Add an implicit declaration to TYPE for the kind of function
2289 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2290 declaration. */
2291
2292 tree
2293 lazily_declare_fn (special_function_kind sfk, tree type)
2294 {
2295 tree fn;
2296 /* Whether or not the argument has a const reference type. */
2297 bool const_p = false;
2298
2299 type = TYPE_MAIN_VARIANT (type);
2300
2301 switch (sfk)
2302 {
2303 case sfk_constructor:
2304 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
2305 break;
2306 case sfk_copy_constructor:
2307 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
2308 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
2309 break;
2310 case sfk_move_constructor:
2311 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
2312 break;
2313 case sfk_copy_assignment:
2314 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
2315 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
2316 break;
2317 case sfk_move_assignment:
2318 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
2319 break;
2320 case sfk_destructor:
2321 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
2322 break;
2323 default:
2324 gcc_unreachable ();
2325 }
2326
2327 /* Declare the function. */
2328 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
2329
2330 /* [class.copy]/8 If the class definition declares a move constructor or
2331 move assignment operator, the implicitly declared copy constructor is
2332 defined as deleted.... */
2333 if ((sfk == sfk_copy_assignment
2334 || sfk == sfk_copy_constructor)
2335 && (type_has_user_declared_move_constructor (type)
2336 || type_has_user_declared_move_assign (type)))
2337 DECL_DELETED_FN (fn) = true;
2338
2339 /* A destructor may be virtual. */
2340 if (sfk == sfk_destructor
2341 || sfk == sfk_move_assignment
2342 || sfk == sfk_copy_assignment)
2343 check_for_override (fn, type);
2344 /* Add it to CLASSTYPE_METHOD_VEC. */
2345 add_method (type, fn, NULL_TREE);
2346 /* Add it to TYPE_METHODS. */
2347 if (sfk == sfk_destructor
2348 && DECL_VIRTUAL_P (fn))
2349 /* The ABI requires that a virtual destructor go at the end of the
2350 vtable. */
2351 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
2352 else
2353 {
2354 DECL_CHAIN (fn) = TYPE_METHODS (type);
2355 TYPE_METHODS (type) = fn;
2356 }
2357 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
2358 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
2359 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
2360 /* Create appropriate clones. */
2361 clone_function_decl (fn, /*update_method_vec=*/true);
2362
2363 return fn;
2364 }
2365
2366 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2367 as there are artificial parms in FN. */
2368
2369 tree
2370 skip_artificial_parms_for (const_tree fn, tree list)
2371 {
2372 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2373 list = TREE_CHAIN (list);
2374 else
2375 return list;
2376
2377 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2378 list = TREE_CHAIN (list);
2379 if (DECL_HAS_VTT_PARM_P (fn))
2380 list = TREE_CHAIN (list);
2381 return list;
2382 }
2383
2384 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2385 artificial parms in FN. */
2386
2387 int
2388 num_artificial_parms_for (const_tree fn)
2389 {
2390 int count = 0;
2391
2392 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2393 count++;
2394 else
2395 return 0;
2396
2397 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2398 count++;
2399 if (DECL_HAS_VTT_PARM_P (fn))
2400 count++;
2401 return count;
2402 }
2403
2404
2405 #include "gt-cp-method.h"