OSDN Git Service

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