OSDN Git Service

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