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