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