OSDN Git Service

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