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