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