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