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