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