OSDN Git Service

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