OSDN Git Service

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