OSDN Git Service

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