comparison gcc/cp/method.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
comparison
equal deleted inserted replaced
68:561a7518be6b 111:04ced10e8804
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
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_CXX_DESTRUCTOR_P (thunk) = 0;
141 DECL_CXX_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_CXX_DESTRUCTOR_P (alias) = 0;
227 DECL_CXX_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
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 warning_sentinel w (warn_useless_cast);
490 exp = build_static_cast (type, exp, tf_warning_or_error);
491 if (DECL_PACK_P (parm))
492 exp = make_pack_expansion (exp);
493 return exp;
494 }
495
496 /* Strip all inheriting constructors, if any, to return the original
497 constructor from a (possibly indirect) base class. */
498
499 tree
500 strip_inheriting_ctors (tree dfn)
501 {
502 if (!flag_new_inheriting_ctors)
503 return dfn;
504 tree fn = dfn;
505 while (tree inh = DECL_INHERITED_CTOR (fn))
506 fn = OVL_FIRST (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 (ovl_iterator iter (inh); iter; ++iter)
543 {
544 tree one = inherited_ctor_binfo_1 (binfo, *iter);
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 if (FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn)) == void_list_node)
579 /* No user-declared parameters to omit. */
580 return false;
581 tree binfo = inherited_ctor_binfo (fn);
582 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
583 if (BINFO_VIRTUAL_P (binfo))
584 return true;
585 return false;
586 }
587
588 /* True iff constructor(s) INH inherited into BINFO initializes INIT_BINFO.
589 This can be true for multiple virtual bases as well as one direct
590 non-virtual base. */
591
592 static bool
593 binfo_inherited_from (tree binfo, tree init_binfo, tree inh)
594 {
595 /* inh is an OVERLOAD if we inherited the same constructor along
596 multiple paths, check all of them. */
597 for (ovl_iterator iter (inh); iter; ++iter)
598 {
599 tree fn = *iter;
600 tree base = DECL_CONTEXT (fn);
601 tree base_binfo = NULL_TREE;
602 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
603 if (BINFO_TYPE (base_binfo) == base)
604 break;
605 if (base_binfo == init_binfo
606 || (flag_new_inheriting_ctors
607 && binfo_inherited_from (base_binfo, init_binfo,
608 DECL_INHERITED_CTOR (fn))))
609 return true;
610 }
611 return false;
612 }
613
614 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
615 given the parameter or parameters PARM, possibly inherited constructor
616 base INH, or move flag MOVE_P. */
617
618 static tree
619 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
620 tree member_init_list)
621 {
622 tree init;
623 if (inh)
624 {
625 /* An inheriting constructor only has a mem-initializer for
626 the base it inherits from. */
627 if (!binfo_inherited_from (TYPE_BINFO (current_class_type), binfo, inh))
628 return member_init_list;
629
630 tree *p = &init;
631 init = NULL_TREE;
632 for (; parm; parm = DECL_CHAIN (parm))
633 {
634 tree exp = forward_parm (parm);
635 *p = build_tree_list (NULL_TREE, exp);
636 p = &TREE_CHAIN (*p);
637 }
638 }
639 else
640 {
641 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
642 tf_warning_or_error);
643 if (move_p)
644 init = move (init);
645 init = build_tree_list (NULL_TREE, init);
646 }
647 return tree_cons (binfo, init, member_init_list);
648 }
649
650 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
651 constructor. */
652
653 static void
654 do_build_copy_constructor (tree fndecl)
655 {
656 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
657 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
658 bool trivial = trivial_fn_p (fndecl);
659 tree inh = DECL_INHERITED_CTOR (fndecl);
660
661 if (!inh)
662 parm = convert_from_reference (parm);
663
664 if (trivial)
665 {
666 if (is_empty_class (current_class_type))
667 /* Don't copy the padding byte; it might not have been allocated
668 if *this is a base subobject. */;
669 else if (tree_int_cst_equal (TYPE_SIZE (current_class_type),
670 CLASSTYPE_SIZE (current_class_type)))
671 {
672 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
673 finish_expr_stmt (t);
674 }
675 else
676 {
677 /* We must only copy the non-tail padding parts. */
678 tree base_size = CLASSTYPE_SIZE_UNIT (current_class_type);
679 base_size = size_binop (MINUS_EXPR, base_size, size_int (1));
680 tree array_type = build_array_type (unsigned_char_type_node,
681 build_index_type (base_size));
682 tree alias_set = build_int_cst (TREE_TYPE (current_class_ptr), 0);
683 tree lhs = build2 (MEM_REF, array_type,
684 current_class_ptr, alias_set);
685 tree rhs = build2 (MEM_REF, array_type,
686 TREE_OPERAND (parm, 0), alias_set);
687 tree t = build2 (INIT_EXPR, void_type_node, lhs, rhs);
688 finish_expr_stmt (t);
689 }
690 }
691 else
692 {
693 tree fields = TYPE_FIELDS (current_class_type);
694 tree member_init_list = NULL_TREE;
695 int cvquals = cp_type_quals (TREE_TYPE (parm));
696 int i;
697 tree binfo, base_binfo;
698 tree init;
699 vec<tree, va_gc> *vbases;
700
701 /* Initialize all the base-classes with the parameter converted
702 to their type so that we get their copy constructor and not
703 another constructor that takes current_class_type. We must
704 deal with the binfo's directly as a direct base might be
705 inaccessible due to ambiguity. */
706 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
707 vec_safe_iterate (vbases, i, &binfo); i++)
708 {
709 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
710 member_init_list);
711 }
712
713 for (binfo = TYPE_BINFO (current_class_type), i = 0;
714 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
715 {
716 if (BINFO_VIRTUAL_P (base_binfo))
717 continue;
718 member_init_list = add_one_base_init (base_binfo, parm, move_p,
719 inh, member_init_list);
720 }
721
722 for (; fields; fields = DECL_CHAIN (fields))
723 {
724 tree field = fields;
725 tree expr_type;
726
727 if (TREE_CODE (field) != FIELD_DECL)
728 continue;
729 if (inh)
730 continue;
731
732 expr_type = TREE_TYPE (field);
733 if (DECL_NAME (field))
734 {
735 if (VFIELD_NAME_P (DECL_NAME (field)))
736 continue;
737 }
738 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
739 /* Just use the field; anonymous types can't have
740 nontrivial copy ctors or assignment ops or this
741 function would be deleted. */;
742 else
743 continue;
744
745 /* Compute the type of "init->field". If the copy-constructor
746 parameter is, for example, "const S&", and the type of
747 the field is "T", then the type will usually be "const
748 T". (There are no cv-qualified variants of reference
749 types.) */
750 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
751 {
752 int quals = cvquals;
753
754 if (DECL_MUTABLE_P (field))
755 quals &= ~TYPE_QUAL_CONST;
756 quals |= cp_type_quals (expr_type);
757 expr_type = cp_build_qualified_type (expr_type, quals);
758 }
759
760 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
761 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
762 /* 'move' breaks bit-fields, and has no effect for scalars. */
763 && !scalarish_type_p (expr_type))
764 init = move (init);
765 init = build_tree_list (NULL_TREE, init);
766
767 member_init_list = tree_cons (field, init, member_init_list);
768 }
769 finish_mem_initializers (member_init_list);
770 }
771 }
772
773 static void
774 do_build_copy_assign (tree fndecl)
775 {
776 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
777 tree compound_stmt;
778 bool move_p = move_fn_p (fndecl);
779 bool trivial = trivial_fn_p (fndecl);
780 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
781
782 compound_stmt = begin_compound_stmt (0);
783 parm = convert_from_reference (parm);
784
785 if (trivial
786 && is_empty_class (current_class_type))
787 /* Don't copy the padding byte; it might not have been allocated
788 if *this is a base subobject. */;
789 else if (trivial)
790 {
791 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
792 finish_expr_stmt (t);
793 }
794 else
795 {
796 tree fields;
797 int cvquals = cp_type_quals (TREE_TYPE (parm));
798 int i;
799 tree binfo, base_binfo;
800
801 /* Assign to each of the direct base classes. */
802 for (binfo = TYPE_BINFO (current_class_type), i = 0;
803 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
804 {
805 tree converted_parm;
806 vec<tree, va_gc> *parmvec;
807
808 /* We must convert PARM directly to the base class
809 explicitly since the base class may be ambiguous. */
810 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
811 tf_warning_or_error);
812 if (move_p)
813 converted_parm = move (converted_parm);
814 /* Call the base class assignment operator. */
815 parmvec = make_tree_vector_single (converted_parm);
816 finish_expr_stmt
817 (build_special_member_call (current_class_ref,
818 cp_assignment_operator_id (NOP_EXPR),
819 &parmvec,
820 base_binfo,
821 flags,
822 tf_warning_or_error));
823 release_tree_vector (parmvec);
824 }
825
826 /* Assign to each of the non-static data members. */
827 for (fields = TYPE_FIELDS (current_class_type);
828 fields;
829 fields = DECL_CHAIN (fields))
830 {
831 tree comp = current_class_ref;
832 tree init = parm;
833 tree field = fields;
834 tree expr_type;
835 int quals;
836
837 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
838 continue;
839
840 expr_type = TREE_TYPE (field);
841
842 if (CP_TYPE_CONST_P (expr_type))
843 {
844 error ("non-static const member %q#D, can%'t use default "
845 "assignment operator", field);
846 continue;
847 }
848 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
849 {
850 error ("non-static reference member %q#D, can%'t use "
851 "default assignment operator", field);
852 continue;
853 }
854
855 if (DECL_NAME (field))
856 {
857 if (VFIELD_NAME_P (DECL_NAME (field)))
858 continue;
859 }
860 else if (ANON_AGGR_TYPE_P (expr_type)
861 && TYPE_FIELDS (expr_type) != NULL_TREE)
862 /* Just use the field; anonymous types can't have
863 nontrivial copy ctors or assignment ops or this
864 function would be deleted. */;
865 else
866 continue;
867
868 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
869
870 /* Compute the type of init->field */
871 quals = cvquals;
872 if (DECL_MUTABLE_P (field))
873 quals &= ~TYPE_QUAL_CONST;
874 expr_type = cp_build_qualified_type (expr_type, quals);
875
876 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
877 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
878 /* 'move' breaks bit-fields, and has no effect for scalars. */
879 && !scalarish_type_p (expr_type))
880 init = move (init);
881
882 if (DECL_NAME (field))
883 init = cp_build_modify_expr (input_location, comp, NOP_EXPR, init,
884 tf_warning_or_error);
885 else
886 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
887 finish_expr_stmt (init);
888 }
889 }
890 finish_return_stmt (current_class_ref);
891 finish_compound_stmt (compound_stmt);
892 }
893
894 /* Synthesize FNDECL, a non-static member function. */
895
896 void
897 synthesize_method (tree fndecl)
898 {
899 bool nested = (current_function_decl != NULL_TREE);
900 tree context = decl_function_context (fndecl);
901 bool need_body = true;
902 tree stmt;
903 location_t save_input_location = input_location;
904 int error_count = errorcount;
905 int warning_count = warningcount + werrorcount;
906
907 /* Reset the source location, we might have been previously
908 deferred, and thus have saved where we were first needed. */
909 DECL_SOURCE_LOCATION (fndecl)
910 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
911
912 /* If we've been asked to synthesize a clone, just synthesize the
913 cloned function instead. Doing so will automatically fill in the
914 body for the clone. */
915 if (DECL_CLONED_FUNCTION_P (fndecl))
916 fndecl = DECL_CLONED_FUNCTION (fndecl);
917
918 /* We may be in the middle of deferred access check. Disable
919 it now. */
920 push_deferring_access_checks (dk_no_deferred);
921
922 if (! context)
923 push_to_top_level ();
924 else if (nested)
925 push_function_context ();
926
927 input_location = DECL_SOURCE_LOCATION (fndecl);
928
929 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
930 stmt = begin_function_body ();
931
932 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
933 {
934 do_build_copy_assign (fndecl);
935 need_body = false;
936 }
937 else if (DECL_CONSTRUCTOR_P (fndecl))
938 {
939 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
940 if (arg_chain != void_list_node)
941 do_build_copy_constructor (fndecl);
942 else
943 finish_mem_initializers (NULL_TREE);
944 }
945
946 /* If we haven't yet generated the body of the function, just
947 generate an empty compound statement. */
948 if (need_body)
949 {
950 tree compound_stmt;
951 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
952 finish_compound_stmt (compound_stmt);
953 }
954
955 finish_function_body (stmt);
956 expand_or_defer_fn (finish_function (/*inline_p=*/false));
957
958 input_location = save_input_location;
959
960 if (! context)
961 pop_from_top_level ();
962 else if (nested)
963 pop_function_context ();
964
965 pop_deferring_access_checks ();
966
967 if (error_count != errorcount || warning_count != warningcount + werrorcount)
968 inform (input_location, "synthesized method %qD first required here ",
969 fndecl);
970 }
971
972 /* Build a reference to type TYPE with cv-quals QUALS, which is an
973 rvalue if RVALUE is true. */
974
975 static tree
976 build_stub_type (tree type, int quals, bool rvalue)
977 {
978 tree argtype = cp_build_qualified_type (type, quals);
979 return cp_build_reference_type (argtype, rvalue);
980 }
981
982 /* Build a dummy glvalue from dereferencing a dummy reference of type
983 REFTYPE. */
984
985 static tree
986 build_stub_object (tree reftype)
987 {
988 if (TREE_CODE (reftype) != REFERENCE_TYPE)
989 reftype = cp_build_reference_type (reftype, /*rval*/true);
990 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
991 return convert_from_reference (stub);
992 }
993
994 /* Determine which function will be called when looking up NAME in TYPE,
995 called with a single ARGTYPE argument, or no argument if ARGTYPE is
996 null. FLAGS and COMPLAIN are as for build_new_method_call.
997
998 Returns a FUNCTION_DECL if all is well.
999 Returns NULL_TREE if overload resolution failed.
1000 Returns error_mark_node if the chosen function cannot be called. */
1001
1002 static tree
1003 locate_fn_flags (tree type, tree name, tree argtype, int flags,
1004 tsubst_flags_t complain)
1005 {
1006 tree ob, fn, fns, binfo, rval;
1007 vec<tree, va_gc> *args;
1008
1009 if (TYPE_P (type))
1010 binfo = TYPE_BINFO (type);
1011 else
1012 {
1013 binfo = type;
1014 type = BINFO_TYPE (binfo);
1015 }
1016
1017 ob = build_stub_object (cp_build_reference_type (type, false));
1018 args = make_tree_vector ();
1019 if (argtype)
1020 {
1021 if (TREE_CODE (argtype) == TREE_LIST)
1022 {
1023 for (tree elt = argtype; elt && elt != void_list_node;
1024 elt = TREE_CHAIN (elt))
1025 {
1026 tree type = TREE_VALUE (elt);
1027 tree arg = build_stub_object (type);
1028 vec_safe_push (args, arg);
1029 }
1030 }
1031 else
1032 {
1033 tree arg = build_stub_object (argtype);
1034 args->quick_push (arg);
1035 }
1036 }
1037
1038 fns = lookup_fnfields (binfo, name, 0);
1039 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
1040
1041 release_tree_vector (args);
1042 if (fn && rval == error_mark_node)
1043 return rval;
1044 else
1045 return fn;
1046 }
1047
1048 /* Locate the dtor of TYPE. */
1049
1050 tree
1051 get_dtor (tree type, tsubst_flags_t complain)
1052 {
1053 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
1054 LOOKUP_NORMAL, complain);
1055 if (fn == error_mark_node)
1056 return NULL_TREE;
1057 return fn;
1058 }
1059
1060 /* Locate the default ctor of TYPE. */
1061
1062 tree
1063 locate_ctor (tree type)
1064 {
1065 tree fn;
1066
1067 push_deferring_access_checks (dk_no_check);
1068 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
1069 LOOKUP_SPECULATIVE, tf_none);
1070 pop_deferring_access_checks ();
1071 if (fn == error_mark_node)
1072 return NULL_TREE;
1073 return fn;
1074 }
1075
1076 /* Likewise, but give any appropriate errors. */
1077
1078 tree
1079 get_default_ctor (tree type)
1080 {
1081 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
1082 LOOKUP_NORMAL, tf_warning_or_error);
1083 if (fn == error_mark_node)
1084 return NULL_TREE;
1085 return fn;
1086 }
1087
1088 /* Locate the copy ctor of TYPE. */
1089
1090 tree
1091 get_copy_ctor (tree type, tsubst_flags_t complain)
1092 {
1093 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
1094 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
1095 tree argtype = build_stub_type (type, quals, false);
1096 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
1097 LOOKUP_NORMAL, complain);
1098 if (fn == error_mark_node)
1099 return NULL_TREE;
1100 return fn;
1101 }
1102
1103 /* Locate the copy assignment operator of TYPE. */
1104
1105 tree
1106 get_copy_assign (tree type)
1107 {
1108 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
1109 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
1110 tree argtype = build_stub_type (type, quals, false);
1111 tree fn = locate_fn_flags (type, cp_assignment_operator_id (NOP_EXPR), argtype,
1112 LOOKUP_NORMAL, tf_warning_or_error);
1113 if (fn == error_mark_node)
1114 return NULL_TREE;
1115 return fn;
1116 }
1117
1118 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
1119 return it if it calls something other than a trivial special member
1120 function. */
1121
1122 static tree
1123 check_nontriv (tree *tp, int *, void *)
1124 {
1125 tree fn = cp_get_callee (*tp);
1126 if (fn == NULL_TREE)
1127 return NULL_TREE;
1128
1129 if (TREE_CODE (fn) == ADDR_EXPR)
1130 fn = TREE_OPERAND (fn, 0);
1131
1132 if (TREE_CODE (fn) != FUNCTION_DECL
1133 || !trivial_fn_p (fn))
1134 return fn;
1135 return NULL_TREE;
1136 }
1137
1138 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1139
1140 static tree
1141 assignable_expr (tree to, tree from)
1142 {
1143 ++cp_unevaluated_operand;
1144 to = build_stub_object (to);
1145 from = build_stub_object (from);
1146 tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, tf_none);
1147 --cp_unevaluated_operand;
1148 return r;
1149 }
1150
1151 /* The predicate condition for a template specialization
1152 is_constructible<T, Args...> shall be satisfied if and only if the
1153 following variable definition would be well-formed for some invented
1154 variable t: T t(create<Args>()...);
1155
1156 Return something equivalent in well-formedness and triviality. */
1157
1158 static tree
1159 constructible_expr (tree to, tree from)
1160 {
1161 tree expr;
1162 if (CLASS_TYPE_P (to))
1163 {
1164 tree ctype = to;
1165 vec<tree, va_gc> *args = NULL;
1166 cp_unevaluated cp_uneval_guard;
1167 if (TREE_CODE (to) != REFERENCE_TYPE)
1168 to = cp_build_reference_type (to, /*rval*/false);
1169 tree ob = build_stub_object (to);
1170 for (; from; from = TREE_CHAIN (from))
1171 vec_safe_push (args, build_stub_object (TREE_VALUE (from)));
1172 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
1173 ctype, LOOKUP_NORMAL, tf_none);
1174 if (expr == error_mark_node)
1175 return error_mark_node;
1176 /* The current state of the standard vis-a-vis LWG 2116 is that
1177 is_*constructible involves destruction as well. */
1178 if (type_build_dtor_call (ctype))
1179 {
1180 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
1181 NULL, ctype, LOOKUP_NORMAL,
1182 tf_none);
1183 if (dtor == error_mark_node)
1184 return error_mark_node;
1185 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
1186 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
1187 }
1188 }
1189 else
1190 {
1191 if (from == NULL_TREE)
1192 return build_value_init (strip_array_types (to), tf_none);
1193 else if (TREE_CHAIN (from))
1194 return error_mark_node; // too many initializers
1195 from = build_stub_object (TREE_VALUE (from));
1196 expr = perform_direct_initialization_if_possible (to, from,
1197 /*cast*/false,
1198 tf_none);
1199 }
1200 return expr;
1201 }
1202
1203 /* Returns a tree iff TO is assignable (if CODE is MODIFY_EXPR) or
1204 constructible (otherwise) from FROM, which is a single type for
1205 assignment or a list of types for construction. */
1206
1207 static tree
1208 is_xible_helper (enum tree_code code, tree to, tree from, bool trivial)
1209 {
1210 if (VOID_TYPE_P (to) || ABSTRACT_CLASS_TYPE_P (to)
1211 || (from && FUNC_OR_METHOD_TYPE_P (from)
1212 && (TYPE_READONLY (from) || FUNCTION_REF_QUALIFIED (from))))
1213 return error_mark_node;
1214 tree expr;
1215 if (code == MODIFY_EXPR)
1216 expr = assignable_expr (to, from);
1217 else if (trivial && from && TREE_CHAIN (from))
1218 return error_mark_node; // only 0- and 1-argument ctors can be trivial
1219 else
1220 expr = constructible_expr (to, from);
1221 return expr;
1222 }
1223
1224 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1225 constructible (otherwise) from FROM, which is a single type for
1226 assignment or a list of types for construction. */
1227
1228 bool
1229 is_trivially_xible (enum tree_code code, tree to, tree from)
1230 {
1231 tree expr;
1232 expr = is_xible_helper (code, to, from, /*trivial*/true);
1233
1234 if (expr == error_mark_node)
1235 return false;
1236 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
1237 return !nt;
1238 }
1239
1240 /* Returns true iff TO is assignable (if CODE is MODIFY_EXPR) or
1241 constructible (otherwise) from FROM, which is a single type for
1242 assignment or a list of types for construction. */
1243
1244 bool
1245 is_xible (enum tree_code code, tree to, tree from)
1246 {
1247 tree expr = is_xible_helper (code, to, from, /*trivial*/false);
1248 if (expr == error_mark_node)
1249 return false;
1250 return !!expr;
1251 }
1252
1253 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1254 DELETED_P or give an error message MSG with argument ARG. */
1255
1256 static void
1257 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
1258 bool *deleted_p, bool *constexpr_p,
1259 bool diag, tree arg, bool dtor_from_ctor = false)
1260 {
1261 if (!fn || fn == error_mark_node)
1262 {
1263 if (deleted_p)
1264 *deleted_p = true;
1265 return;
1266 }
1267
1268 if (spec_p)
1269 {
1270 maybe_instantiate_noexcept (fn);
1271 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1272 *spec_p = merge_exception_specifiers (*spec_p, raises);
1273 }
1274
1275 if (!trivial_fn_p (fn) && !dtor_from_ctor)
1276 {
1277 if (trivial_p)
1278 *trivial_p = false;
1279 if (TREE_CODE (arg) == FIELD_DECL
1280 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1281 {
1282 if (deleted_p)
1283 *deleted_p = true;
1284 if (diag)
1285 error ("union member %q+D with non-trivial %qD", arg, fn);
1286 }
1287 }
1288
1289 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1290 {
1291 *constexpr_p = false;
1292 if (diag)
1293 {
1294 inform (DECL_SOURCE_LOCATION (fn),
1295 "defaulted constructor calls non-constexpr %qD", fn);
1296 explain_invalid_constexpr_fn (fn);
1297 }
1298 }
1299 }
1300
1301 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1302 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
1303 called from a synthesized constructor, in which case we don't consider
1304 the triviality of the subobject destructor. */
1305
1306 static void
1307 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1308 int quals, bool copy_arg_p, bool move_p,
1309 bool assign_p, tree *spec_p, bool *trivial_p,
1310 bool *deleted_p, bool *constexpr_p,
1311 bool diag, int flags, tsubst_flags_t complain,
1312 bool dtor_from_ctor)
1313 {
1314 tree field;
1315 for (field = fields; field; field = DECL_CHAIN (field))
1316 {
1317 tree mem_type, argtype, rval;
1318
1319 if (TREE_CODE (field) != FIELD_DECL
1320 || DECL_ARTIFICIAL (field))
1321 continue;
1322
1323 mem_type = strip_array_types (TREE_TYPE (field));
1324 if (assign_p)
1325 {
1326 bool bad = true;
1327 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1328 {
1329 if (diag)
1330 error ("non-static const member %q#D, can%'t use default "
1331 "assignment operator", field);
1332 }
1333 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1334 {
1335 if (diag)
1336 error ("non-static reference member %q#D, can%'t use "
1337 "default assignment operator", field);
1338 }
1339 else
1340 bad = false;
1341
1342 if (bad && deleted_p)
1343 *deleted_p = true;
1344 }
1345 else if (sfk == sfk_constructor || sfk == sfk_inheriting_constructor)
1346 {
1347 bool bad;
1348
1349 if (DECL_INITIAL (field))
1350 {
1351 if (diag && DECL_INITIAL (field) == error_mark_node)
1352 inform (DECL_SOURCE_LOCATION (field),
1353 "initializer for %q#D is invalid", field);
1354 if (trivial_p)
1355 *trivial_p = false;
1356 /* Core 1351: If the field has an NSDMI that could throw, the
1357 default constructor is noexcept(false). */
1358 if (spec_p)
1359 {
1360 tree nsdmi = get_nsdmi (field, /*ctor*/false, complain);
1361 if (!expr_noexcept_p (nsdmi, complain))
1362 *spec_p = noexcept_false_spec;
1363 }
1364 /* Don't do the normal processing. */
1365 continue;
1366 }
1367
1368 bad = false;
1369 if (CP_TYPE_CONST_P (mem_type)
1370 && default_init_uninitialized_part (mem_type))
1371 {
1372 if (diag)
1373 {
1374 error ("uninitialized const member in %q#T",
1375 current_class_type);
1376 inform (DECL_SOURCE_LOCATION (field),
1377 "%q#D should be initialized", field);
1378 }
1379 bad = true;
1380 }
1381 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1382 {
1383 if (diag)
1384 {
1385 error ("uninitialized reference member in %q#T",
1386 current_class_type);
1387 inform (DECL_SOURCE_LOCATION (field),
1388 "%q#D should be initialized", field);
1389 }
1390 bad = true;
1391 }
1392
1393 if (bad && deleted_p)
1394 *deleted_p = true;
1395
1396 /* For an implicitly-defined default constructor to be constexpr,
1397 every member must have a user-provided default constructor or
1398 an explicit initializer. */
1399 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1400 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1401 {
1402 *constexpr_p = false;
1403 if (diag)
1404 inform (DECL_SOURCE_LOCATION (field),
1405 "defaulted default constructor does not "
1406 "initialize %q#D", field);
1407 }
1408 }
1409 else if (sfk == sfk_copy_constructor)
1410 {
1411 /* 12.8p11b5 */
1412 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1413 && TYPE_REF_IS_RVALUE (mem_type))
1414 {
1415 if (diag)
1416 error ("copying non-static data member %q#D of rvalue "
1417 "reference type", field);
1418 if (deleted_p)
1419 *deleted_p = true;
1420 }
1421 }
1422
1423 if (!CLASS_TYPE_P (mem_type))
1424 continue;
1425
1426 if (ANON_AGGR_TYPE_P (mem_type))
1427 {
1428 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1429 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1430 deleted_p, constexpr_p,
1431 diag, flags, complain, dtor_from_ctor);
1432 continue;
1433 }
1434
1435 if (copy_arg_p)
1436 {
1437 int mem_quals = cp_type_quals (mem_type) | quals;
1438 if (DECL_MUTABLE_P (field))
1439 mem_quals &= ~TYPE_QUAL_CONST;
1440 argtype = build_stub_type (mem_type, mem_quals, move_p);
1441 }
1442 else
1443 argtype = NULL_TREE;
1444
1445 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1446
1447 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1448 constexpr_p, diag, field, dtor_from_ctor);
1449 }
1450 }
1451
1452 /* Base walker helper for synthesized_method_walk. Inspect a direct
1453 or virtual base. BINFO is the parent type's binfo. BASE_BINFO is
1454 the base binfo of interests. All other parms are as for
1455 synthesized_method_walk, or its local vars. */
1456
1457 static tree
1458 synthesized_method_base_walk (tree binfo, tree base_binfo,
1459 int quals, bool copy_arg_p,
1460 bool move_p, bool ctor_p,
1461 tree *inheriting_ctor, tree inherited_parms,
1462 tree fnname, int flags, bool diag,
1463 tree *spec_p, bool *trivial_p,
1464 bool *deleted_p, bool *constexpr_p)
1465 {
1466 bool inherited_binfo = false;
1467 tree argtype = NULL_TREE;
1468 deferring_kind defer = dk_no_deferred;
1469
1470 if (copy_arg_p)
1471 argtype = build_stub_type (BINFO_TYPE (base_binfo), quals, move_p);
1472 else if (inheriting_ctor
1473 && (inherited_binfo
1474 = binfo_inherited_from (binfo, base_binfo, *inheriting_ctor)))
1475 {
1476 argtype = inherited_parms;
1477 /* Don't check access on the inherited constructor. */
1478 if (flag_new_inheriting_ctors)
1479 defer = dk_deferred;
1480 }
1481 /* To be conservative, ignore access to the base dtor that
1482 DR1658 instructs us to ignore. See the comment in
1483 synthesized_method_walk. */
1484 else if (cxx_dialect >= cxx14 && fnname == complete_dtor_identifier
1485 && BINFO_VIRTUAL_P (base_binfo)
1486 && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo)))
1487 defer = dk_no_check;
1488
1489 if (defer != dk_no_deferred)
1490 push_deferring_access_checks (defer);
1491 tree rval = locate_fn_flags (base_binfo, fnname, argtype, flags,
1492 diag ? tf_warning_or_error : tf_none);
1493 if (defer != dk_no_deferred)
1494 pop_deferring_access_checks ();
1495
1496 /* Replace an inherited template with the appropriate specialization. */
1497 if (inherited_binfo && rval
1498 && DECL_P (*inheriting_ctor) && DECL_P (rval)
1499 && DECL_CONTEXT (*inheriting_ctor) == DECL_CONTEXT (rval))
1500 *inheriting_ctor = DECL_CLONED_FUNCTION (rval);
1501
1502 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1503 constexpr_p, diag, BINFO_TYPE (base_binfo));
1504 if (ctor_p &&
1505 (!BINFO_VIRTUAL_P (base_binfo)
1506 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))))
1507 {
1508 /* In a constructor we also need to check the subobject
1509 destructors for cleanup of partially constructed objects. */
1510 tree dtor = locate_fn_flags (base_binfo, complete_dtor_identifier,
1511 NULL_TREE, flags,
1512 diag ? tf_warning_or_error : tf_none);
1513 /* Note that we don't pass down trivial_p; the subobject
1514 destructors don't affect triviality of the constructor. Nor
1515 do they affect constexpr-ness (a constant expression doesn't
1516 throw) or exception-specification (a throw from one of the
1517 dtors would be a double-fault). */
1518 process_subob_fn (dtor, NULL, NULL, deleted_p, NULL, false,
1519 BINFO_TYPE (base_binfo), /*dtor_from_ctor*/true);
1520 }
1521
1522 return rval;
1523 }
1524
1525 /* The caller wants to generate an implicit declaration of SFK for
1526 CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
1527 TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
1528 referent appropriately. If DIAG is true, we're either being called
1529 from maybe_explain_implicit_delete to give errors, or if
1530 CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
1531
1532 static void
1533 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1534 tree *spec_p, bool *trivial_p, bool *deleted_p,
1535 bool *constexpr_p, bool diag,
1536 tree *inheriting_ctor, tree inherited_parms)
1537 {
1538 tree binfo, base_binfo, fnname;
1539 int i;
1540
1541 if (spec_p)
1542 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1543
1544 if (deleted_p)
1545 {
1546 /* "The closure type associated with a lambda-expression has a deleted
1547 default constructor and a deleted copy assignment operator."
1548 This is diagnosed in maybe_explain_implicit_delete. */
1549 if (LAMBDA_TYPE_P (ctype)
1550 && (sfk == sfk_constructor
1551 || sfk == sfk_copy_assignment))
1552 {
1553 *deleted_p = true;
1554 return;
1555 }
1556
1557 *deleted_p = false;
1558 }
1559
1560 bool ctor_p = false;
1561 bool assign_p = false;
1562 bool check_vdtor = false;
1563 switch (sfk)
1564 {
1565 case sfk_move_assignment:
1566 case sfk_copy_assignment:
1567 assign_p = true;
1568 fnname = cp_assignment_operator_id (NOP_EXPR);
1569 break;
1570
1571 case sfk_destructor:
1572 check_vdtor = true;
1573 /* The synthesized method will call base dtors, but check complete
1574 here to avoid having to deal with VTT. */
1575 fnname = complete_dtor_identifier;
1576 break;
1577
1578 case sfk_constructor:
1579 case sfk_move_constructor:
1580 case sfk_copy_constructor:
1581 case sfk_inheriting_constructor:
1582 ctor_p = true;
1583 fnname = complete_ctor_identifier;
1584 break;
1585
1586 default:
1587 gcc_unreachable ();
1588 }
1589
1590 gcc_assert ((sfk == sfk_inheriting_constructor)
1591 == (inheriting_ctor && *inheriting_ctor != NULL_TREE));
1592
1593 /* If that user-written default constructor would satisfy the
1594 requirements of a constexpr constructor (7.1.5), the
1595 implicitly-defined default constructor is constexpr.
1596
1597 The implicitly-defined copy/move assignment operator is constexpr if
1598 - X is a literal type, and
1599 - the assignment operator selected to copy/move each direct base class
1600 subobject is a constexpr function, and
1601 - for each non-static data member of X that is of class type (or array
1602 thereof), the assignment operator selected to copy/move that
1603 member is a constexpr function. */
1604 if (constexpr_p)
1605 *constexpr_p = ctor_p || (assign_p && cxx_dialect >= cxx14);
1606
1607 bool move_p = false;
1608 bool copy_arg_p = false;
1609 switch (sfk)
1610 {
1611 case sfk_constructor:
1612 case sfk_destructor:
1613 case sfk_inheriting_constructor:
1614 break;
1615
1616 case sfk_move_constructor:
1617 case sfk_move_assignment:
1618 move_p = true;
1619 /* FALLTHRU */
1620 case sfk_copy_constructor:
1621 case sfk_copy_assignment:
1622 copy_arg_p = true;
1623 break;
1624
1625 default:
1626 gcc_unreachable ();
1627 }
1628
1629 bool expected_trivial = type_has_trivial_fn (ctype, sfk);
1630 if (trivial_p)
1631 *trivial_p = expected_trivial;
1632
1633 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1634 class versions and other properties of the type. But a subobject
1635 class can be trivially copyable and yet have overload resolution
1636 choose a template constructor for initialization, depending on
1637 rvalueness and cv-quals. And furthermore, a member in a base might
1638 be trivial but deleted or otherwise not callable. So we can't exit
1639 early in C++0x. The same considerations apply in C++98/03, but
1640 there the definition of triviality does not consider overload
1641 resolution, so a constructor can be trivial even if it would otherwise
1642 call a non-trivial constructor. */
1643 if (expected_trivial
1644 && (!copy_arg_p || cxx_dialect < cxx11))
1645 {
1646 if (constexpr_p && sfk == sfk_constructor)
1647 {
1648 bool cx = trivial_default_constructor_is_constexpr (ctype);
1649 *constexpr_p = cx;
1650 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1651 /* A trivial constructor doesn't have any NSDMI. */
1652 inform (input_location, "defaulted default constructor does "
1653 "not initialize any non-static data member");
1654 }
1655 if (!diag && cxx_dialect < cxx11)
1656 return;
1657 }
1658
1659 ++cp_unevaluated_operand;
1660 ++c_inhibit_evaluation_warnings;
1661 push_deferring_access_checks (dk_no_deferred);
1662
1663 tree scope = push_scope (ctype);
1664
1665 int flags = LOOKUP_NORMAL | LOOKUP_SPECULATIVE;
1666 if (sfk != sfk_inheriting_constructor)
1667 flags |= LOOKUP_DEFAULTED;
1668
1669 tsubst_flags_t complain = diag ? tf_warning_or_error : tf_none;
1670 if (diag && spec_p)
1671 /* We're in get_defaulted_eh_spec; we don't actually want any walking
1672 diagnostics, we just want complain set. */
1673 diag = false;
1674 int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED;
1675
1676 for (binfo = TYPE_BINFO (ctype), i = 0;
1677 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1678 {
1679 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1680 /* We'll handle virtual bases below. */
1681 continue;
1682
1683 tree fn = synthesized_method_base_walk (binfo, base_binfo, quals,
1684 copy_arg_p, move_p, ctor_p,
1685 inheriting_ctor,
1686 inherited_parms,
1687 fnname, flags, diag,
1688 spec_p, trivial_p,
1689 deleted_p, constexpr_p);
1690
1691 if (diag && assign_p && move_p
1692 && BINFO_VIRTUAL_P (base_binfo)
1693 && fn && TREE_CODE (fn) == FUNCTION_DECL
1694 && move_fn_p (fn) && !trivial_fn_p (fn)
1695 && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
1696 warning (OPT_Wvirtual_move_assign,
1697 "defaulted move assignment for %qT calls a non-trivial "
1698 "move assignment operator for virtual base %qT",
1699 ctype, BINFO_TYPE (base_binfo));
1700
1701 if (check_vdtor && type_has_virtual_destructor (BINFO_TYPE (base_binfo)))
1702 {
1703 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1704 to have a null fn (no class-specific op delete). */
1705 fn = locate_fn_flags (ctype, cp_operator_id (DELETE_EXPR),
1706 ptr_type_node, flags, tf_none);
1707 if (fn && fn == error_mark_node)
1708 {
1709 if (complain & tf_error)
1710 locate_fn_flags (ctype, cp_operator_id (DELETE_EXPR),
1711 ptr_type_node, flags, complain);
1712 if (deleted_p)
1713 *deleted_p = true;
1714 }
1715 check_vdtor = false;
1716 }
1717 }
1718
1719 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (ctype);
1720 if (assign_p)
1721 /* Already examined vbases above. */;
1722 else if (vec_safe_is_empty (vbases))
1723 /* No virtual bases to worry about. */;
1724 else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14
1725 /* DR 1658 specifies that vbases of abstract classes are
1726 ignored for both ctors and dtors. However, that breaks
1727 virtual dtor overriding when the ignored base has a
1728 throwing destructor. So, ignore that piece of 1658. A
1729 defect has been filed (no number yet). */
1730 && sfk != sfk_destructor)
1731 /* Vbase cdtors are not relevant. */;
1732 else
1733 {
1734 if (constexpr_p)
1735 *constexpr_p = false;
1736
1737 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1738 synthesized_method_base_walk (binfo, base_binfo, quals,
1739 copy_arg_p, move_p, ctor_p,
1740 inheriting_ctor, inherited_parms,
1741 fnname, flags, diag,
1742 spec_p, trivial_p,
1743 deleted_p, constexpr_p);
1744 }
1745
1746 /* Now handle the non-static data members. */
1747 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1748 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1749 deleted_p, constexpr_p,
1750 diag, flags, complain, /*dtor_from_ctor*/false);
1751 if (ctor_p)
1752 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1753 sfk_destructor, TYPE_UNQUALIFIED, false,
1754 false, false, NULL, NULL,
1755 deleted_p, NULL,
1756 false, flags, complain, /*dtor_from_ctor*/true);
1757
1758 pop_scope (scope);
1759
1760 pop_deferring_access_checks ();
1761 --cp_unevaluated_operand;
1762 --c_inhibit_evaluation_warnings;
1763 }
1764
1765 /* DECL is a defaulted function whose exception specification is now
1766 needed. Return what it should be. */
1767
1768 tree
1769 get_defaulted_eh_spec (tree decl, tsubst_flags_t complain)
1770 {
1771 if (DECL_CLONED_FUNCTION_P (decl))
1772 decl = DECL_CLONED_FUNCTION (decl);
1773 special_function_kind sfk = special_function_p (decl);
1774 tree ctype = DECL_CONTEXT (decl);
1775 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1776 tree parm_type = TREE_VALUE (parms);
1777 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1778 tree spec = empty_except_spec;
1779 bool diag = !DECL_DELETED_FN (decl) && (complain & tf_error);
1780 tree inh = DECL_INHERITED_CTOR (decl);
1781 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
1782 NULL, diag, &inh, parms);
1783 return spec;
1784 }
1785
1786 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1787 return true; else return false. */
1788
1789 bool
1790 maybe_explain_implicit_delete (tree decl)
1791 {
1792 /* If decl is a clone, get the primary variant. */
1793 decl = DECL_ORIGIN (decl);
1794 gcc_assert (DECL_DELETED_FN (decl));
1795 if (DECL_DEFAULTED_FN (decl))
1796 {
1797 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1798 static hash_set<tree> *explained;
1799
1800 special_function_kind sfk;
1801 location_t loc;
1802 bool informed;
1803 tree ctype;
1804
1805 if (!explained)
1806 explained = new hash_set<tree>;
1807 if (explained->add (decl))
1808 return true;
1809
1810 sfk = special_function_p (decl);
1811 ctype = DECL_CONTEXT (decl);
1812 loc = input_location;
1813 input_location = DECL_SOURCE_LOCATION (decl);
1814
1815 informed = false;
1816 if (LAMBDA_TYPE_P (ctype))
1817 {
1818 informed = true;
1819 if (sfk == sfk_constructor)
1820 inform (DECL_SOURCE_LOCATION (decl),
1821 "a lambda closure type has a deleted default constructor");
1822 else if (sfk == sfk_copy_assignment)
1823 inform (DECL_SOURCE_LOCATION (decl),
1824 "a lambda closure type has a deleted copy assignment operator");
1825 else
1826 informed = false;
1827 }
1828 else if (DECL_ARTIFICIAL (decl)
1829 && (sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
1830 && classtype_has_move_assign_or_move_ctor_p (ctype, true))
1831 {
1832 inform (DECL_SOURCE_LOCATION (decl),
1833 "%q#D is implicitly declared as deleted because %qT "
1834 "declares a move constructor or move assignment operator",
1835 decl, ctype);
1836 informed = true;
1837 }
1838 else if (sfk == sfk_inheriting_constructor)
1839 {
1840 tree binfo = inherited_ctor_binfo (decl);
1841 if (TREE_CODE (binfo) != TREE_BINFO)
1842 {
1843 inform (DECL_SOURCE_LOCATION (decl),
1844 "%q#D inherits from multiple base subobjects",
1845 decl);
1846 informed = true;
1847 }
1848 }
1849 if (!informed)
1850 {
1851 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1852 tree parm_type = TREE_VALUE (parms);
1853 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1854 tree raises = NULL_TREE;
1855 bool deleted_p = false;
1856 tree scope = push_scope (ctype);
1857 tree inh = DECL_INHERITED_CTOR (decl);
1858
1859 synthesized_method_walk (ctype, sfk, const_p,
1860 &raises, NULL, &deleted_p, NULL, false,
1861 &inh, parms);
1862 if (deleted_p)
1863 {
1864 inform (DECL_SOURCE_LOCATION (decl),
1865 "%q#D is implicitly deleted because the default "
1866 "definition would be ill-formed:", decl);
1867 synthesized_method_walk (ctype, sfk, const_p,
1868 NULL, NULL, NULL, NULL, true,
1869 &inh, parms);
1870 }
1871 else if (!comp_except_specs
1872 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1873 raises, ce_normal))
1874 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1875 "deleted because its exception-specification does not "
1876 "match the implicit exception-specification %qX",
1877 decl, raises);
1878 else if (flag_checking)
1879 gcc_unreachable ();
1880
1881 pop_scope (scope);
1882 }
1883
1884 input_location = loc;
1885 return true;
1886 }
1887 return false;
1888 }
1889
1890 /* DECL is a defaulted function which was declared constexpr. Explain why
1891 it can't be constexpr. */
1892
1893 void
1894 explain_implicit_non_constexpr (tree decl)
1895 {
1896 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1897 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1898 tree inh = DECL_INHERITED_CTOR (decl);
1899 bool dummy;
1900 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1901 special_function_p (decl), const_p,
1902 NULL, NULL, NULL, &dummy, true,
1903 &inh,
1904 FUNCTION_FIRST_USER_PARMTYPE (decl));
1905 }
1906
1907 /* DECL is an instantiation of an inheriting constructor template. Deduce
1908 the correct exception-specification and deletedness for this particular
1909 specialization. */
1910
1911 void
1912 deduce_inheriting_ctor (tree decl)
1913 {
1914 decl = DECL_ORIGIN (decl);
1915 gcc_assert (DECL_INHERITED_CTOR (decl));
1916 tree spec;
1917 bool trivial, constexpr_, deleted;
1918 tree inh = DECL_INHERITED_CTOR (decl);
1919 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1920 false, &spec, &trivial, &deleted, &constexpr_,
1921 /*diag*/false,
1922 &inh,
1923 FUNCTION_FIRST_USER_PARMTYPE (decl));
1924 if (TREE_CODE (inherited_ctor_binfo (decl)) != TREE_BINFO)
1925 /* Inherited the same constructor from different base subobjects. */
1926 deleted = true;
1927 DECL_DELETED_FN (decl) = deleted;
1928 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1929 SET_DECL_INHERITED_CTOR (decl, inh);
1930
1931 tree clone;
1932 FOR_EACH_CLONE (clone, decl)
1933 {
1934 DECL_DELETED_FN (clone) = deleted;
1935 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
1936 SET_DECL_INHERITED_CTOR (clone, inh);
1937 }
1938 }
1939
1940 /* Implicitly declare the special function indicated by KIND, as a
1941 member of TYPE. For copy constructors and assignment operators,
1942 CONST_P indicates whether these functions should take a const
1943 reference argument or a non-const reference. Returns the
1944 FUNCTION_DECL for the implicitly declared function. */
1945
1946 tree
1947 implicitly_declare_fn (special_function_kind kind, tree type,
1948 bool const_p, tree inherited_ctor,
1949 tree inherited_parms)
1950 {
1951 tree fn;
1952 tree parameter_types = void_list_node;
1953 tree return_type;
1954 tree fn_type;
1955 tree raises = empty_except_spec;
1956 tree rhs_parm_type = NULL_TREE;
1957 tree this_parm;
1958 tree name;
1959 HOST_WIDE_INT saved_processing_template_decl;
1960 bool deleted_p;
1961 bool constexpr_p;
1962
1963 /* Because we create declarations for implicitly declared functions
1964 lazily, we may be creating the declaration for a member of TYPE
1965 while in some completely different context. However, TYPE will
1966 never be a dependent class (because we never want to do lookups
1967 for implicitly defined functions in a dependent class).
1968 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1969 because we only create clones for constructors and destructors
1970 when not in a template. */
1971 gcc_assert (!dependent_type_p (type));
1972 saved_processing_template_decl = processing_template_decl;
1973 processing_template_decl = 0;
1974
1975 type = TYPE_MAIN_VARIANT (type);
1976
1977 if (targetm.cxx.cdtor_returns_this ())
1978 {
1979 if (kind == sfk_destructor)
1980 /* See comment in check_special_function_return_type. */
1981 return_type = build_pointer_type (void_type_node);
1982 else
1983 return_type = build_pointer_type (type);
1984 }
1985 else
1986 return_type = void_type_node;
1987
1988 switch (kind)
1989 {
1990 case sfk_destructor:
1991 /* Destructor. */
1992 name = dtor_identifier;
1993 break;
1994
1995 case sfk_constructor:
1996 /* Default constructor. */
1997 name = ctor_identifier;
1998 break;
1999
2000 case sfk_copy_constructor:
2001 case sfk_copy_assignment:
2002 case sfk_move_constructor:
2003 case sfk_move_assignment:
2004 case sfk_inheriting_constructor:
2005 {
2006 if (kind == sfk_copy_assignment
2007 || kind == sfk_move_assignment)
2008 {
2009 return_type = build_reference_type (type);
2010 name = cp_assignment_operator_id (NOP_EXPR);
2011 }
2012 else
2013 name = ctor_identifier;
2014
2015 if (kind == sfk_inheriting_constructor)
2016 parameter_types = inherited_parms;
2017 else
2018 {
2019 if (const_p)
2020 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
2021 else
2022 rhs_parm_type = type;
2023 bool move_p = (kind == sfk_move_assignment
2024 || kind == sfk_move_constructor);
2025 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
2026
2027 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
2028 }
2029 break;
2030 }
2031 default:
2032 gcc_unreachable ();
2033 }
2034
2035 bool trivial_p = false;
2036
2037 if (inherited_ctor)
2038 {
2039 /* For an inheriting constructor, just copy these flags from the
2040 inherited constructor until deduce_inheriting_ctor. */
2041 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
2042 deleted_p = DECL_DELETED_FN (inherited_ctor);
2043 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
2044 }
2045 else if (cxx_dialect >= cxx11)
2046 {
2047 raises = noexcept_deferred_spec;
2048 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
2049 &deleted_p, &constexpr_p, false,
2050 &inherited_ctor, inherited_parms);
2051 }
2052 else
2053 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
2054 &deleted_p, &constexpr_p, false,
2055 &inherited_ctor, inherited_parms);
2056 /* Don't bother marking a deleted constructor as constexpr. */
2057 if (deleted_p)
2058 constexpr_p = false;
2059 /* A trivial copy/move constructor is also a constexpr constructor,
2060 unless the class has virtual bases (7.1.5p4). */
2061 else if (trivial_p && cxx_dialect >= cxx11
2062 && (kind == sfk_copy_constructor
2063 || kind == sfk_move_constructor)
2064 && !CLASSTYPE_VBASECLASSES (type))
2065 gcc_assert (constexpr_p);
2066
2067 if (!trivial_p && type_has_trivial_fn (type, kind))
2068 type_set_nontrivial_flag (type, kind);
2069
2070 /* Create the function. */
2071 fn_type = build_method_type_directly (type, return_type, parameter_types);
2072 if (raises)
2073 fn_type = build_exception_variant (fn_type, raises);
2074 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
2075 if (kind != sfk_inheriting_constructor)
2076 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
2077
2078 if (!IDENTIFIER_CDTOR_P (name))
2079 /* Assignment operator. */
2080 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
2081 else if (IDENTIFIER_CTOR_P (name))
2082 DECL_CXX_CONSTRUCTOR_P (fn) = true;
2083 else
2084 DECL_CXX_DESTRUCTOR_P (fn) = true;
2085
2086 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
2087
2088 /* Create the explicit arguments. */
2089 if (rhs_parm_type)
2090 {
2091 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
2092 want its type to be included in the mangled function
2093 name. */
2094 tree decl = cp_build_parm_decl (fn, NULL_TREE, rhs_parm_type);
2095 TREE_READONLY (decl) = 1;
2096 retrofit_lang_decl (decl);
2097 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
2098 DECL_ARGUMENTS (fn) = decl;
2099 }
2100 else if (kind == sfk_inheriting_constructor)
2101 {
2102 tree *p = &DECL_ARGUMENTS (fn);
2103 int index = 1;
2104 for (tree parm = inherited_parms; parm && parm != void_list_node;
2105 parm = TREE_CHAIN (parm))
2106 {
2107 *p = cp_build_parm_decl (fn, NULL_TREE, TREE_VALUE (parm));
2108 retrofit_lang_decl (*p);
2109 DECL_PARM_LEVEL (*p) = 1;
2110 DECL_PARM_INDEX (*p) = index++;
2111 p = &DECL_CHAIN (*p);
2112 }
2113 SET_DECL_INHERITED_CTOR (fn, inherited_ctor);
2114 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
2115 /* A constructor so declared has the same access as the corresponding
2116 constructor in X. */
2117 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
2118 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
2119 /* Copy constexpr from the inherited constructor even if the
2120 inheriting constructor doesn't satisfy the requirements. */
2121 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
2122 }
2123 /* Add the "this" parameter. */
2124 this_parm = build_this_parm (fn, fn_type, TYPE_UNQUALIFIED);
2125 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
2126 DECL_ARGUMENTS (fn) = this_parm;
2127
2128 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
2129 DECL_IN_AGGR_P (fn) = 1;
2130 DECL_ARTIFICIAL (fn) = 1;
2131 DECL_DEFAULTED_FN (fn) = 1;
2132 if (cxx_dialect >= cxx11)
2133 {
2134 DECL_DELETED_FN (fn) = deleted_p;
2135 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
2136 }
2137 DECL_EXTERNAL (fn) = true;
2138 DECL_NOT_REALLY_EXTERN (fn) = 1;
2139 DECL_DECLARED_INLINE_P (fn) = 1;
2140 set_linkage_according_to_type (type, fn);
2141 if (TREE_PUBLIC (fn))
2142 DECL_COMDAT (fn) = 1;
2143 rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
2144 gcc_assert (!TREE_USED (fn));
2145
2146 /* Propagate constraints from the inherited constructor. */
2147 if (flag_concepts && inherited_ctor)
2148 if (tree orig_ci = get_constraints (inherited_ctor))
2149 {
2150 tree new_ci = copy_node (orig_ci);
2151 set_constraints (fn, new_ci);
2152 }
2153
2154 /* Restore PROCESSING_TEMPLATE_DECL. */
2155 processing_template_decl = saved_processing_template_decl;
2156
2157 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
2158 fn = add_inherited_template_parms (fn, inherited_ctor);
2159
2160 /* Warn about calling a non-trivial move assignment in a virtual base. */
2161 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
2162 && CLASSTYPE_VBASECLASSES (type))
2163 {
2164 location_t loc = input_location;
2165 input_location = DECL_SOURCE_LOCATION (fn);
2166 synthesized_method_walk (type, kind, const_p,
2167 NULL, NULL, NULL, NULL, true,
2168 NULL, NULL_TREE);
2169 input_location = loc;
2170 }
2171
2172 return fn;
2173 }
2174
2175 /* Gives any errors about defaulted functions which need to be deferred
2176 until the containing class is complete. */
2177
2178 void
2179 defaulted_late_check (tree fn)
2180 {
2181 /* Complain about invalid signature for defaulted fn. */
2182 tree ctx = DECL_CONTEXT (fn);
2183 special_function_kind kind = special_function_p (fn);
2184 bool fn_const_p = (copy_fn_p (fn) == 2);
2185 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
2186 NULL, NULL);
2187 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
2188
2189 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
2190 TREE_TYPE (TREE_TYPE (implicit_fn)))
2191 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2192 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
2193 {
2194 error ("defaulted declaration %q+D does not match the "
2195 "expected signature", fn);
2196 inform (DECL_SOURCE_LOCATION (fn),
2197 "expected signature: %qD", implicit_fn);
2198 return;
2199 }
2200
2201 if (DECL_DELETED_FN (implicit_fn))
2202 {
2203 DECL_DELETED_FN (fn) = 1;
2204 return;
2205 }
2206
2207 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
2208 exception-specification only if it is compatible (15.4) with the
2209 exception-specification on the implicit declaration. If a function
2210 is explicitly defaulted on its first declaration, (...) it is
2211 implicitly considered to have the same exception-specification as if
2212 it had been implicitly declared. */
2213 maybe_instantiate_noexcept (fn);
2214 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2215 if (!fn_spec)
2216 {
2217 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2218 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
2219 }
2220 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2221 /* Equivalent to the implicit spec. */;
2222 else if (DECL_DEFAULTED_IN_CLASS_P (fn)
2223 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2224 /* We can't compare an explicit exception-specification on a
2225 constructor defaulted in the class body to the implicit
2226 exception-specification until after we've parsed any NSDMI; see
2227 after_nsdmi_defaulted_late_checks. */;
2228 else
2229 {
2230 tree eh_spec = get_defaulted_eh_spec (fn);
2231 if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
2232 {
2233 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2234 DECL_DELETED_FN (fn) = true;
2235 else
2236 error ("function %q+D defaulted on its redeclaration "
2237 "with an exception-specification that differs from "
2238 "the implicit exception-specification %qX", fn, eh_spec);
2239 }
2240 }
2241
2242 if (DECL_DEFAULTED_IN_CLASS_P (fn)
2243 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
2244 {
2245 /* Hmm...should we do this for out-of-class too? Should it be OK to
2246 add constexpr later like inline, rather than requiring
2247 declarations to match? */
2248 DECL_DECLARED_CONSTEXPR_P (fn) = true;
2249 if (kind == sfk_constructor)
2250 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
2251 }
2252
2253 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
2254 && DECL_DECLARED_CONSTEXPR_P (fn))
2255 {
2256 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2257 {
2258 error ("explicitly defaulted function %q+D cannot be declared "
2259 "as constexpr because the implicit declaration is not "
2260 "constexpr:", fn);
2261 explain_implicit_non_constexpr (fn);
2262 }
2263 DECL_DECLARED_CONSTEXPR_P (fn) = false;
2264 }
2265 }
2266
2267 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
2268 exception-specifications on functions defaulted in the class body. */
2269
2270 void
2271 after_nsdmi_defaulted_late_checks (tree t)
2272 {
2273 if (uses_template_parms (t))
2274 return;
2275 if (t == error_mark_node)
2276 return;
2277 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2278 if (!DECL_ARTIFICIAL (fn)
2279 && DECL_DECLARES_FUNCTION_P (fn)
2280 && DECL_DEFAULTED_IN_CLASS_P (fn))
2281 {
2282 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2283 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2284 continue;
2285
2286 tree eh_spec = get_defaulted_eh_spec (fn);
2287 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
2288 eh_spec, ce_normal))
2289 DECL_DELETED_FN (fn) = true;
2290 }
2291 }
2292
2293 /* Returns true iff FN can be explicitly defaulted, and gives any
2294 errors if defaulting FN is ill-formed. */
2295
2296 bool
2297 defaultable_fn_check (tree fn)
2298 {
2299 special_function_kind kind = sfk_none;
2300
2301 if (template_parm_scope_p ())
2302 {
2303 error ("a template cannot be defaulted");
2304 return false;
2305 }
2306
2307 if (DECL_CONSTRUCTOR_P (fn))
2308 {
2309 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
2310 kind = sfk_constructor;
2311 else if (copy_fn_p (fn) > 0
2312 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
2313 == void_list_node))
2314 kind = sfk_copy_constructor;
2315 else if (move_fn_p (fn))
2316 kind = sfk_move_constructor;
2317 }
2318 else if (DECL_DESTRUCTOR_P (fn))
2319 kind = sfk_destructor;
2320 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2321 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
2322 {
2323 if (copy_fn_p (fn))
2324 kind = sfk_copy_assignment;
2325 else if (move_fn_p (fn))
2326 kind = sfk_move_assignment;
2327 }
2328
2329 if (kind == sfk_none)
2330 {
2331 error ("%qD cannot be defaulted", fn);
2332 return false;
2333 }
2334 else
2335 {
2336 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
2337 t && t != void_list_node; t = TREE_CHAIN (t))
2338 if (TREE_PURPOSE (t))
2339 {
2340 error ("defaulted function %q+D with default argument", fn);
2341 break;
2342 }
2343
2344 /* Avoid do_warn_unused_parameter warnings. */
2345 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
2346 if (DECL_NAME (p))
2347 TREE_NO_WARNING (p) = 1;
2348
2349 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
2350 /* Defer checking. */;
2351 else if (!processing_template_decl)
2352 defaulted_late_check (fn);
2353
2354 return true;
2355 }
2356 }
2357
2358 /* Add an implicit declaration to TYPE for the kind of function
2359 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2360 declaration. */
2361
2362 tree
2363 lazily_declare_fn (special_function_kind sfk, tree type)
2364 {
2365 tree fn;
2366 /* Whether or not the argument has a const reference type. */
2367 bool const_p = false;
2368
2369 type = TYPE_MAIN_VARIANT (type);
2370
2371 switch (sfk)
2372 {
2373 case sfk_constructor:
2374 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
2375 break;
2376 case sfk_copy_constructor:
2377 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
2378 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
2379 break;
2380 case sfk_move_constructor:
2381 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
2382 break;
2383 case sfk_copy_assignment:
2384 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
2385 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
2386 break;
2387 case sfk_move_assignment:
2388 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
2389 break;
2390 case sfk_destructor:
2391 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
2392 break;
2393 default:
2394 gcc_unreachable ();
2395 }
2396
2397 /* Declare the function. */
2398 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
2399
2400 /* [class.copy]/8 If the class definition declares a move constructor or
2401 move assignment operator, the implicitly declared copy constructor is
2402 defined as deleted.... */
2403 if ((sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
2404 && classtype_has_move_assign_or_move_ctor_p (type, true))
2405 DECL_DELETED_FN (fn) = true;
2406
2407 /* Destructors and assignment operators may be virtual. */
2408 if (sfk == sfk_destructor
2409 || sfk == sfk_move_assignment
2410 || sfk == sfk_copy_assignment)
2411 check_for_override (fn, type);
2412
2413 /* Add it to the class */
2414 bool added = add_method (type, fn, false);
2415 gcc_assert (added);
2416
2417 /* Add it to TYPE_FIELDS. */
2418 if (sfk == sfk_destructor
2419 && DECL_VIRTUAL_P (fn))
2420 /* The ABI requires that a virtual destructor go at the end of the
2421 vtable. */
2422 TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), fn);
2423 else
2424 {
2425 DECL_CHAIN (fn) = TYPE_FIELDS (type);
2426 TYPE_FIELDS (type) = fn;
2427 }
2428 /* Propagate TYPE_FIELDS. */
2429 fixup_type_variants (type);
2430
2431 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
2432 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
2433 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
2434 /* Create appropriate clones. */
2435 clone_function_decl (fn, /*update_methods=*/true);
2436
2437 return fn;
2438 }
2439
2440 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2441 as there are artificial parms in FN. */
2442
2443 tree
2444 skip_artificial_parms_for (const_tree fn, tree list)
2445 {
2446 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2447 list = TREE_CHAIN (list);
2448 else
2449 return list;
2450
2451 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2452 list = TREE_CHAIN (list);
2453 if (DECL_HAS_VTT_PARM_P (fn))
2454 list = TREE_CHAIN (list);
2455 return list;
2456 }
2457
2458 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2459 artificial parms in FN. */
2460
2461 int
2462 num_artificial_parms_for (const_tree fn)
2463 {
2464 int count = 0;
2465
2466 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2467 count++;
2468 else
2469 return 0;
2470
2471 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2472 count++;
2473 if (DECL_HAS_VTT_PARM_P (fn))
2474 count++;
2475 return count;
2476 }
2477
2478
2479 #include "gt-cp-method.h"