1 /* Handle the hair of processing (but not expanding) inline functions. 2 Also manage function and variable name overloading. 3 Copyright (C) 1987-2017 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 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 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 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, thunk); 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 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 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 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 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 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 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 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 /* Strip all inheriting constructors, if any, to return the original 497 constructor from a (possibly indirect) base class. */ 498 499 tree 500 strip_inheriting_ctors (tree dfn) 501 { 502 if (!flag_new_inheriting_ctors) 503 return dfn; 504 tree fn = dfn; 505 while (tree inh = DECL_INHERITED_CTOR (fn)) 506 { 507 inh = OVL_CURRENT (inh); 508 fn = inh; 509 } 510 if (TREE_CODE (fn) == TEMPLATE_DECL 511 && TREE_CODE (dfn) == FUNCTION_DECL) 512 fn = DECL_TEMPLATE_RESULT (fn); 513 return fn; 514 } 515 516 /* Find the binfo for the base subobject of BINFO being initialized by 517 inherited constructor FNDECL (a member of a direct base of BINFO). */ 518 519 static tree inherited_ctor_binfo (tree, tree); 520 static tree 521 inherited_ctor_binfo_1 (tree binfo, tree fndecl) 522 { 523 tree base = DECL_CONTEXT (fndecl); 524 tree base_binfo; 525 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 526 if (BINFO_TYPE (base_binfo) == base) 527 return inherited_ctor_binfo (base_binfo, fndecl); 528 529 gcc_unreachable(); 530 } 531 532 /* Find the binfo for the base subobject of BINFO being initialized by 533 inheriting constructor FNDECL (a member of BINFO), or BINFO if FNDECL is not 534 an inheriting constructor. */ 535 536 static tree 537 inherited_ctor_binfo (tree binfo, tree fndecl) 538 { 539 tree inh = DECL_INHERITED_CTOR (fndecl); 540 if (!inh) 541 return binfo; 542 543 tree results = NULL_TREE; 544 for (; inh; inh = OVL_NEXT (inh)) 545 { 546 tree one = inherited_ctor_binfo_1 (binfo, OVL_CURRENT (inh)); 547 if (!results) 548 results = one; 549 else if (one != results) 550 results = tree_cons (NULL_TREE, one, results); 551 } 552 return results; 553 } 554 555 /* Find the binfo for the base subobject being initialized by inheriting 556 constructor FNDECL, or NULL_TREE if FNDECL is not an inheriting 557 constructor. */ 558 559 tree 560 inherited_ctor_binfo (tree fndecl) 561 { 562 if (!DECL_INHERITED_CTOR (fndecl)) 563 return NULL_TREE; 564 tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl)); 565 return inherited_ctor_binfo (binfo, fndecl); 566 } 567 568 /* True if we should omit all user-declared parameters from constructor FN, 569 because it is a base clone of a ctor inherited from a virtual base. */ 570 571 bool 572 ctor_omit_inherited_parms (tree fn) 573 { 574 if (!flag_new_inheriting_ctors) 575 /* We only optimize away the parameters in the new model. */ 576 return false; 577 if (!DECL_BASE_CONSTRUCTOR_P (fn) 578 || !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn))) 579 return false; 580 if (FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn)) == void_list_node) 581 /* No user-declared parameters to omit. */ 582 return false; 583 tree binfo = inherited_ctor_binfo (fn); 584 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo)) 585 if (BINFO_VIRTUAL_P (binfo)) 586 return true; 587 return false; 588 } 589 590 /* True iff constructor(s) INH inherited into BINFO initializes INIT_BINFO. 591 This can be true for multiple virtual bases as well as one direct 592 non-virtual base. */ 593 594 static bool 595 binfo_inherited_from (tree binfo, tree init_binfo, tree inh) 596 { 597 /* inh is an OVERLOAD if we inherited the same constructor along 598 multiple paths, check all of them. */ 599 for (; inh; inh = OVL_NEXT (inh)) 600 { 601 tree fn = OVL_CURRENT (inh); 602 tree base = DECL_CONTEXT (fn); 603 tree base_binfo = NULL_TREE; 604 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 605 if (BINFO_TYPE (base_binfo) == base) 606 break; 607 if (base_binfo == init_binfo 608 || (flag_new_inheriting_ctors 609 && binfo_inherited_from (base_binfo, init_binfo, 610 DECL_INHERITED_CTOR (fn)))) 611 return true; 612 } 613 return false; 614 } 615 616 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO 617 given the parameter or parameters PARM, possibly inherited constructor 618 base INH, or move flag MOVE_P. */ 619 620 static tree 621 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh, 622 tree member_init_list) 623 { 624 tree init; 625 if (inh) 626 { 627 /* An inheriting constructor only has a mem-initializer for 628 the base it inherits from. */ 629 if (!binfo_inherited_from (TYPE_BINFO (current_class_type), binfo, inh)) 630 return member_init_list; 631 632 tree *p = &init; 633 init = NULL_TREE; 634 for (; parm; parm = DECL_CHAIN (parm)) 635 { 636 tree exp = forward_parm (parm); 637 *p = build_tree_list (NULL_TREE, exp); 638 p = &TREE_CHAIN (*p); 639 } 640 } 641 else 642 { 643 init = build_base_path (PLUS_EXPR, parm, binfo, 1, 644 tf_warning_or_error); 645 if (move_p) 646 init = move (init); 647 init = build_tree_list (NULL_TREE, init); 648 } 649 return tree_cons (binfo, init, member_init_list); 650 } 651 652 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting 653 constructor. */ 654 655 static void 656 do_build_copy_constructor (tree fndecl) 657 { 658 tree parm = FUNCTION_FIRST_USER_PARM (fndecl); 659 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl); 660 bool trivial = trivial_fn_p (fndecl); 661 tree inh = DECL_INHERITED_CTOR (fndecl); 662 663 if (!inh) 664 parm = convert_from_reference (parm); 665 666 if (trivial) 667 { 668 if (is_empty_class (current_class_type)) 669 /* Don't copy the padding byte; it might not have been allocated 670 if *this is a base subobject. */; 671 else if (tree_int_cst_equal (TYPE_SIZE (current_class_type), 672 CLASSTYPE_SIZE (current_class_type))) 673 { 674 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm); 675 finish_expr_stmt (t); 676 } 677 else 678 { 679 /* We must only copy the non-tail padding parts. */ 680 tree base_size = CLASSTYPE_SIZE_UNIT (current_class_type); 681 base_size = size_binop (MINUS_EXPR, base_size, size_int (1)); 682 tree array_type = build_array_type (unsigned_char_type_node, 683 build_index_type (base_size)); 684 tree alias_set = build_int_cst (TREE_TYPE (current_class_ptr), 0); 685 tree lhs = build2 (MEM_REF, array_type, 686 current_class_ptr, alias_set); 687 tree rhs = build2 (MEM_REF, array_type, 688 TREE_OPERAND (parm, 0), alias_set); 689 tree t = build2 (INIT_EXPR, void_type_node, lhs, rhs); 690 finish_expr_stmt (t); 691 } 692 } 693 else 694 { 695 tree fields = TYPE_FIELDS (current_class_type); 696 tree member_init_list = NULL_TREE; 697 int cvquals = cp_type_quals (TREE_TYPE (parm)); 698 int i; 699 tree binfo, base_binfo; 700 tree init; 701 vec<tree, va_gc> *vbases; 702 703 /* Initialize all the base-classes with the parameter converted 704 to their type so that we get their copy constructor and not 705 another constructor that takes current_class_type. We must 706 deal with the binfo's directly as a direct base might be 707 inaccessible due to ambiguity. */ 708 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0; 709 vec_safe_iterate (vbases, i, &binfo); i++) 710 { 711 member_init_list = add_one_base_init (binfo, parm, move_p, inh, 712 member_init_list); 713 } 714 715 for (binfo = TYPE_BINFO (current_class_type), i = 0; 716 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 717 { 718 if (BINFO_VIRTUAL_P (base_binfo)) 719 continue; 720 member_init_list = add_one_base_init (base_binfo, parm, move_p, 721 inh, member_init_list); 722 } 723 724 for (; fields; fields = DECL_CHAIN (fields)) 725 { 726 tree field = fields; 727 tree expr_type; 728 729 if (TREE_CODE (field) != FIELD_DECL) 730 continue; 731 if (inh) 732 continue; 733 734 expr_type = TREE_TYPE (field); 735 if (DECL_NAME (field)) 736 { 737 if (VFIELD_NAME_P (DECL_NAME (field))) 738 continue; 739 } 740 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type)) 741 /* Just use the field; anonymous types can't have 742 nontrivial copy ctors or assignment ops or this 743 function would be deleted. */; 744 else 745 continue; 746 747 /* Compute the type of "init->field". If the copy-constructor 748 parameter is, for example, "const S&", and the type of 749 the field is "T", then the type will usually be "const 750 T". (There are no cv-qualified variants of reference 751 types.) */ 752 if (TREE_CODE (expr_type) != REFERENCE_TYPE) 753 { 754 int quals = cvquals; 755 756 if (DECL_MUTABLE_P (field)) 757 quals &= ~TYPE_QUAL_CONST; 758 quals |= cp_type_quals (expr_type); 759 expr_type = cp_build_qualified_type (expr_type, quals); 760 } 761 762 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE); 763 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE 764 /* 'move' breaks bit-fields, and has no effect for scalars. */ 765 && !scalarish_type_p (expr_type)) 766 init = move (init); 767 init = build_tree_list (NULL_TREE, init); 768 769 member_init_list = tree_cons (field, init, member_init_list); 770 } 771 finish_mem_initializers (member_init_list); 772 } 773 } 774 775 static void 776 do_build_copy_assign (tree fndecl) 777 { 778 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl)); 779 tree compound_stmt; 780 bool move_p = move_fn_p (fndecl); 781 bool trivial = trivial_fn_p (fndecl); 782 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED; 783 784 compound_stmt = begin_compound_stmt (0); 785 parm = convert_from_reference (parm); 786 787 if (trivial 788 && is_empty_class (current_class_type)) 789 /* Don't copy the padding byte; it might not have been allocated 790 if *this is a base subobject. */; 791 else if (trivial) 792 { 793 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm); 794 finish_expr_stmt (t); 795 } 796 else 797 { 798 tree fields; 799 int cvquals = cp_type_quals (TREE_TYPE (parm)); 800 int i; 801 tree binfo, base_binfo; 802 803 /* Assign to each of the direct base classes. */ 804 for (binfo = TYPE_BINFO (current_class_type), i = 0; 805 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 806 { 807 tree converted_parm; 808 vec<tree, va_gc> *parmvec; 809 810 /* We must convert PARM directly to the base class 811 explicitly since the base class may be ambiguous. */ 812 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1, 813 tf_warning_or_error); 814 if (move_p) 815 converted_parm = move (converted_parm); 816 /* Call the base class assignment operator. */ 817 parmvec = make_tree_vector_single (converted_parm); 818 finish_expr_stmt 819 (build_special_member_call (current_class_ref, 820 cp_assignment_operator_id (NOP_EXPR), 821 &parmvec, 822 base_binfo, 823 flags, 824 tf_warning_or_error)); 825 release_tree_vector (parmvec); 826 } 827 828 /* Assign to each of the non-static data members. */ 829 for (fields = TYPE_FIELDS (current_class_type); 830 fields; 831 fields = DECL_CHAIN (fields)) 832 { 833 tree comp = current_class_ref; 834 tree init = parm; 835 tree field = fields; 836 tree expr_type; 837 int quals; 838 839 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field)) 840 continue; 841 842 expr_type = TREE_TYPE (field); 843 844 if (CP_TYPE_CONST_P (expr_type)) 845 { 846 error ("non-static const member %q#D, can%'t use default " 847 "assignment operator", field); 848 continue; 849 } 850 else if (TREE_CODE (expr_type) == REFERENCE_TYPE) 851 { 852 error ("non-static reference member %q#D, can%'t use " 853 "default assignment operator", field); 854 continue; 855 } 856 857 if (DECL_NAME (field)) 858 { 859 if (VFIELD_NAME_P (DECL_NAME (field))) 860 continue; 861 } 862 else if (ANON_AGGR_TYPE_P (expr_type) 863 && TYPE_FIELDS (expr_type) != NULL_TREE) 864 /* Just use the field; anonymous types can't have 865 nontrivial copy ctors or assignment ops or this 866 function would be deleted. */; 867 else 868 continue; 869 870 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE); 871 872 /* Compute the type of init->field */ 873 quals = cvquals; 874 if (DECL_MUTABLE_P (field)) 875 quals &= ~TYPE_QUAL_CONST; 876 expr_type = cp_build_qualified_type (expr_type, quals); 877 878 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE); 879 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE 880 /* 'move' breaks bit-fields, and has no effect for scalars. */ 881 && !scalarish_type_p (expr_type)) 882 init = move (init); 883 884 if (DECL_NAME (field)) 885 init = cp_build_modify_expr (input_location, comp, NOP_EXPR, init, 886 tf_warning_or_error); 887 else 888 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init); 889 finish_expr_stmt (init); 890 } 891 } 892 finish_return_stmt (current_class_ref); 893 finish_compound_stmt (compound_stmt); 894 } 895 896 /* Synthesize FNDECL, a non-static member function. */ 897 898 void 899 synthesize_method (tree fndecl) 900 { 901 bool nested = (current_function_decl != NULL_TREE); 902 tree context = decl_function_context (fndecl); 903 bool need_body = true; 904 tree stmt; 905 location_t save_input_location = input_location; 906 int error_count = errorcount; 907 int warning_count = warningcount + werrorcount; 908 909 /* Reset the source location, we might have been previously 910 deferred, and thus have saved where we were first needed. */ 911 DECL_SOURCE_LOCATION (fndecl) 912 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl))); 913 914 /* If we've been asked to synthesize a clone, just synthesize the 915 cloned function instead. Doing so will automatically fill in the 916 body for the clone. */ 917 if (DECL_CLONED_FUNCTION_P (fndecl)) 918 fndecl = DECL_CLONED_FUNCTION (fndecl); 919 920 /* We may be in the middle of deferred access check. Disable 921 it now. */ 922 push_deferring_access_checks (dk_no_deferred); 923 924 if (! context) 925 push_to_top_level (); 926 else if (nested) 927 push_function_context (); 928 929 input_location = DECL_SOURCE_LOCATION (fndecl); 930 931 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED); 932 stmt = begin_function_body (); 933 934 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR) 935 { 936 do_build_copy_assign (fndecl); 937 need_body = false; 938 } 939 else if (DECL_CONSTRUCTOR_P (fndecl)) 940 { 941 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl); 942 if (arg_chain != void_list_node) 943 do_build_copy_constructor (fndecl); 944 else 945 finish_mem_initializers (NULL_TREE); 946 } 947 948 /* If we haven't yet generated the body of the function, just 949 generate an empty compound statement. */ 950 if (need_body) 951 { 952 tree compound_stmt; 953 compound_stmt = begin_compound_stmt (BCS_FN_BODY); 954 finish_compound_stmt (compound_stmt); 955 } 956 957 finish_function_body (stmt); 958 expand_or_defer_fn (finish_function (0)); 959 960 input_location = save_input_location; 961 962 if (! context) 963 pop_from_top_level (); 964 else if (nested) 965 pop_function_context (); 966 967 pop_deferring_access_checks (); 968 969 if (error_count != errorcount || warning_count != warningcount + werrorcount) 970 inform (input_location, "synthesized method %qD first required here ", 971 fndecl); 972 } 973 974 /* Build a reference to type TYPE with cv-quals QUALS, which is an 975 rvalue if RVALUE is true. */ 976 977 static tree 978 build_stub_type (tree type, int quals, bool rvalue) 979 { 980 tree argtype = cp_build_qualified_type (type, quals); 981 return cp_build_reference_type (argtype, rvalue); 982 } 983 984 /* Build a dummy glvalue from dereferencing a dummy reference of type 985 REFTYPE. */ 986 987 static tree 988 build_stub_object (tree reftype) 989 { 990 if (TREE_CODE (reftype) != REFERENCE_TYPE) 991 reftype = cp_build_reference_type (reftype, /*rval*/true); 992 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node); 993 return convert_from_reference (stub); 994 } 995 996 /* Determine which function will be called when looking up NAME in TYPE, 997 called with a single ARGTYPE argument, or no argument if ARGTYPE is 998 null. FLAGS and COMPLAIN are as for build_new_method_call. 999 1000 Returns a FUNCTION_DECL if all is well. 1001 Returns NULL_TREE if overload resolution failed. 1002 Returns error_mark_node if the chosen function cannot be called. */ 1003 1004 static tree 1005 locate_fn_flags (tree type, tree name, tree argtype, int flags, 1006 tsubst_flags_t complain) 1007 { 1008 tree ob, fn, fns, binfo, rval; 1009 vec<tree, va_gc> *args; 1010 1011 if (TYPE_P (type)) 1012 binfo = TYPE_BINFO (type); 1013 else 1014 { 1015 binfo = type; 1016 type = BINFO_TYPE (binfo); 1017 } 1018 1019 ob = build_stub_object (cp_build_reference_type (type, false)); 1020 args = make_tree_vector (); 1021 if (argtype) 1022 { 1023 if (TREE_CODE (argtype) == TREE_LIST) 1024 { 1025 for (tree elt = argtype; elt && elt != void_list_node; 1026 elt = TREE_CHAIN (elt)) 1027 { 1028 tree type = TREE_VALUE (elt); 1029 tree arg = build_stub_object (type); 1030 vec_safe_push (args, arg); 1031 } 1032 } 1033 else 1034 { 1035 tree arg = build_stub_object (argtype); 1036 args->quick_push (arg); 1037 } 1038 } 1039 1040 fns = lookup_fnfields (binfo, name, 0); 1041 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain); 1042 1043 release_tree_vector (args); 1044 if (fn && rval == error_mark_node) 1045 return rval; 1046 else 1047 return fn; 1048 } 1049 1050 /* Locate the dtor of TYPE. */ 1051 1052 tree 1053 get_dtor (tree type, tsubst_flags_t complain) 1054 { 1055 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE, 1056 LOOKUP_NORMAL, complain); 1057 if (fn == error_mark_node) 1058 return NULL_TREE; 1059 return fn; 1060 } 1061 1062 /* Locate the default ctor of TYPE. */ 1063 1064 tree 1065 locate_ctor (tree type) 1066 { 1067 tree fn; 1068 1069 push_deferring_access_checks (dk_no_check); 1070 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE, 1071 LOOKUP_SPECULATIVE, tf_none); 1072 pop_deferring_access_checks (); 1073 if (fn == error_mark_node) 1074 return NULL_TREE; 1075 return fn; 1076 } 1077 1078 /* Likewise, but give any appropriate errors. */ 1079 1080 tree 1081 get_default_ctor (tree type) 1082 { 1083 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE, 1084 LOOKUP_NORMAL, tf_warning_or_error); 1085 if (fn == error_mark_node) 1086 return NULL_TREE; 1087 return fn; 1088 } 1089 1090 /* Locate the copy ctor of TYPE. */ 1091 1092 tree 1093 get_copy_ctor (tree type, tsubst_flags_t complain) 1094 { 1095 int quals = (TYPE_HAS_CONST_COPY_CTOR (type) 1096 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED); 1097 tree argtype = build_stub_type (type, quals, false); 1098 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype, 1099 LOOKUP_NORMAL, complain); 1100 if (fn == error_mark_node) 1101 return NULL_TREE; 1102 return fn; 1103 } 1104 1105 /* Locate the copy assignment operator of TYPE. */ 1106 1107 tree 1108 get_copy_assign (tree type) 1109 { 1110 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type) 1111 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED); 1112 tree argtype = build_stub_type (type, quals, false); 1113 tree fn = locate_fn_flags (type, cp_assignment_operator_id (NOP_EXPR), argtype, 1114 LOOKUP_NORMAL, tf_warning_or_error); 1115 if (fn == error_mark_node) 1116 return NULL_TREE; 1117 return fn; 1118 } 1119 1120 /* walk_tree helper function for is_trivially_xible. If *TP is a call, 1121 return it if it calls something other than a trivial special member 1122 function. */ 1123 1124 static tree 1125 check_nontriv (tree *tp, int *, void *) 1126 { 1127 tree fn = cp_get_callee (*tp); 1128 if (fn == NULL_TREE) 1129 return NULL_TREE; 1130 1131 if (TREE_CODE (fn) == ADDR_EXPR) 1132 fn = TREE_OPERAND (fn, 0); 1133 1134 if (TREE_CODE (fn) != FUNCTION_DECL 1135 || !trivial_fn_p (fn)) 1136 return fn; 1137 return NULL_TREE; 1138 } 1139 1140 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */ 1141 1142 static tree 1143 assignable_expr (tree to, tree from) 1144 { 1145 ++cp_unevaluated_operand; 1146 to = build_stub_object (to); 1147 from = build_stub_object (from); 1148 tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, tf_none); 1149 --cp_unevaluated_operand; 1150 return r; 1151 } 1152 1153 /* The predicate condition for a template specialization 1154 is_constructible<T, Args...> shall be satisfied if and only if the 1155 following variable definition would be well-formed for some invented 1156 variable t: T t(create<Args>()...); 1157 1158 Return something equivalent in well-formedness and triviality. */ 1159 1160 static tree 1161 constructible_expr (tree to, tree from) 1162 { 1163 tree expr; 1164 if (CLASS_TYPE_P (to)) 1165 { 1166 tree ctype = to; 1167 vec<tree, va_gc> *args = NULL; 1168 cp_unevaluated cp_uneval_guard; 1169 if (TREE_CODE (to) != REFERENCE_TYPE) 1170 to = cp_build_reference_type (to, /*rval*/false); 1171 tree ob = build_stub_object (to); 1172 for (; from; from = TREE_CHAIN (from)) 1173 vec_safe_push (args, build_stub_object (TREE_VALUE (from))); 1174 expr = build_special_member_call (ob, complete_ctor_identifier, &args, 1175 ctype, LOOKUP_NORMAL, tf_none); 1176 if (expr == error_mark_node) 1177 return error_mark_node; 1178 /* The current state of the standard vis-a-vis LWG 2116 is that 1179 is_*constructible involves destruction as well. */ 1180 if (type_build_dtor_call (ctype)) 1181 { 1182 tree dtor = build_special_member_call (ob, complete_dtor_identifier, 1183 NULL, ctype, LOOKUP_NORMAL, 1184 tf_none); 1185 if (dtor == error_mark_node) 1186 return error_mark_node; 1187 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype)) 1188 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor); 1189 } 1190 } 1191 else 1192 { 1193 if (from == NULL_TREE) 1194 return build_value_init (to, tf_none); 1195 else if (TREE_CHAIN (from)) 1196 return error_mark_node; // too many initializers 1197 from = build_stub_object (TREE_VALUE (from)); 1198 expr = perform_direct_initialization_if_possible (to, from, 1199 /*cast*/false, 1200 tf_none); 1201 } 1202 return expr; 1203 } 1204 1205 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or 1206 constructible (otherwise) from FROM, which is a single type for 1207 assignment or a list of types for construction. */ 1208 1209 bool 1210 is_trivially_xible (enum tree_code code, tree to, tree from) 1211 { 1212 tree expr; 1213 if (code == MODIFY_EXPR) 1214 expr = assignable_expr (to, from); 1215 else if (from && TREE_CHAIN (from)) 1216 return false; // only 0- and 1-argument ctors can be trivial 1217 else 1218 expr = constructible_expr (to, from); 1219 1220 if (expr == error_mark_node) 1221 return false; 1222 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL); 1223 return !nt; 1224 } 1225 1226 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and 1227 DELETED_P or give an error message MSG with argument ARG. */ 1228 1229 static void 1230 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p, 1231 bool *deleted_p, bool *constexpr_p, 1232 bool diag, tree arg, bool dtor_from_ctor = false) 1233 { 1234 if (!fn || fn == error_mark_node) 1235 { 1236 if (deleted_p) 1237 *deleted_p = true; 1238 return; 1239 } 1240 1241 if (spec_p) 1242 { 1243 maybe_instantiate_noexcept (fn); 1244 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)); 1245 *spec_p = merge_exception_specifiers (*spec_p, raises); 1246 } 1247 1248 if (!trivial_fn_p (fn) && !dtor_from_ctor) 1249 { 1250 if (trivial_p) 1251 *trivial_p = false; 1252 if (TREE_CODE (arg) == FIELD_DECL 1253 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE) 1254 { 1255 if (deleted_p) 1256 *deleted_p = true; 1257 if (diag) 1258 error ("union member %q+D with non-trivial %qD", arg, fn); 1259 } 1260 } 1261 1262 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn)) 1263 { 1264 *constexpr_p = false; 1265 if (diag) 1266 { 1267 inform (DECL_SOURCE_LOCATION (fn), 1268 "defaulted constructor calls non-constexpr %qD", fn); 1269 explain_invalid_constexpr_fn (fn); 1270 } 1271 } 1272 } 1273 1274 /* Subroutine of synthesized_method_walk to allow recursion into anonymous 1275 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors 1276 called from a synthesized constructor, in which case we don't consider 1277 the triviality of the subobject destructor. */ 1278 1279 static void 1280 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk, 1281 int quals, bool copy_arg_p, bool move_p, 1282 bool assign_p, tree *spec_p, bool *trivial_p, 1283 bool *deleted_p, bool *constexpr_p, 1284 bool diag, int flags, tsubst_flags_t complain, 1285 bool dtor_from_ctor) 1286 { 1287 tree field; 1288 for (field = fields; field; field = DECL_CHAIN (field)) 1289 { 1290 tree mem_type, argtype, rval; 1291 1292 if (TREE_CODE (field) != FIELD_DECL 1293 || DECL_ARTIFICIAL (field)) 1294 continue; 1295 1296 mem_type = strip_array_types (TREE_TYPE (field)); 1297 if (assign_p) 1298 { 1299 bool bad = true; 1300 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type)) 1301 { 1302 if (diag) 1303 error ("non-static const member %q#D, can%'t use default " 1304 "assignment operator", field); 1305 } 1306 else if (TREE_CODE (mem_type) == REFERENCE_TYPE) 1307 { 1308 if (diag) 1309 error ("non-static reference member %q#D, can%'t use " 1310 "default assignment operator", field); 1311 } 1312 else 1313 bad = false; 1314 1315 if (bad && deleted_p) 1316 *deleted_p = true; 1317 } 1318 else if (sfk == sfk_constructor || sfk == sfk_inheriting_constructor) 1319 { 1320 bool bad; 1321 1322 if (DECL_INITIAL (field)) 1323 { 1324 if (diag && DECL_INITIAL (field) == error_mark_node) 1325 inform (DECL_SOURCE_LOCATION (field), 1326 "initializer for %q#D is invalid", field); 1327 if (trivial_p) 1328 *trivial_p = false; 1329 /* Core 1351: If the field has an NSDMI that could throw, the 1330 default constructor is noexcept(false). */ 1331 if (spec_p) 1332 { 1333 tree nsdmi = get_nsdmi (field, /*ctor*/false); 1334 if (!expr_noexcept_p (nsdmi, complain)) 1335 *spec_p = noexcept_false_spec; 1336 } 1337 /* Don't do the normal processing. */ 1338 continue; 1339 } 1340 1341 bad = false; 1342 if (CP_TYPE_CONST_P (mem_type) 1343 && default_init_uninitialized_part (mem_type)) 1344 { 1345 if (diag) 1346 { 1347 error ("uninitialized const member in %q#T", 1348 current_class_type); 1349 inform (DECL_SOURCE_LOCATION (field), 1350 "%q#D should be initialized", field); 1351 } 1352 bad = true; 1353 } 1354 else if (TREE_CODE (mem_type) == REFERENCE_TYPE) 1355 { 1356 if (diag) 1357 { 1358 error ("uninitialized reference member in %q#T", 1359 current_class_type); 1360 inform (DECL_SOURCE_LOCATION (field), 1361 "%q#D should be initialized", field); 1362 } 1363 bad = true; 1364 } 1365 1366 if (bad && deleted_p) 1367 *deleted_p = true; 1368 1369 /* For an implicitly-defined default constructor to be constexpr, 1370 every member must have a user-provided default constructor or 1371 an explicit initializer. */ 1372 if (constexpr_p && !CLASS_TYPE_P (mem_type) 1373 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE) 1374 { 1375 *constexpr_p = false; 1376 if (diag) 1377 inform (DECL_SOURCE_LOCATION (field), 1378 "defaulted default constructor does not " 1379 "initialize %q#D", field); 1380 } 1381 } 1382 else if (sfk == sfk_copy_constructor) 1383 { 1384 /* 12.8p11b5 */ 1385 if (TREE_CODE (mem_type) == REFERENCE_TYPE 1386 && TYPE_REF_IS_RVALUE (mem_type)) 1387 { 1388 if (diag) 1389 error ("copying non-static data member %q#D of rvalue " 1390 "reference type", field); 1391 if (deleted_p) 1392 *deleted_p = true; 1393 } 1394 } 1395 1396 if (!CLASS_TYPE_P (mem_type)) 1397 continue; 1398 1399 if (ANON_AGGR_TYPE_P (mem_type)) 1400 { 1401 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals, 1402 copy_arg_p, move_p, assign_p, spec_p, trivial_p, 1403 deleted_p, constexpr_p, 1404 diag, flags, complain, dtor_from_ctor); 1405 continue; 1406 } 1407 1408 if (copy_arg_p) 1409 { 1410 int mem_quals = cp_type_quals (mem_type) | quals; 1411 if (DECL_MUTABLE_P (field)) 1412 mem_quals &= ~TYPE_QUAL_CONST; 1413 argtype = build_stub_type (mem_type, mem_quals, move_p); 1414 } 1415 else 1416 argtype = NULL_TREE; 1417 1418 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain); 1419 1420 process_subob_fn (rval, spec_p, trivial_p, deleted_p, 1421 constexpr_p, diag, field, dtor_from_ctor); 1422 } 1423 } 1424 1425 /* Base walker helper for synthesized_method_walk. Inspect a direct 1426 or virtual base. BINFO is the parent type's binfo. BASE_BINFO is 1427 the base binfo of interests. All other parms are as for 1428 synthesized_method_walk, or its local vars. */ 1429 1430 static tree 1431 synthesized_method_base_walk (tree binfo, tree base_binfo, 1432 int quals, bool copy_arg_p, 1433 bool move_p, bool ctor_p, 1434 tree *inheriting_ctor, tree inherited_parms, 1435 tree fnname, int flags, bool diag, 1436 tree *spec_p, bool *trivial_p, 1437 bool *deleted_p, bool *constexpr_p) 1438 { 1439 bool inherited_binfo = false; 1440 tree argtype = NULL_TREE; 1441 deferring_kind defer = dk_no_deferred; 1442 1443 if (copy_arg_p) 1444 argtype = build_stub_type (BINFO_TYPE (base_binfo), quals, move_p); 1445 else if (inheriting_ctor 1446 && (inherited_binfo 1447 = binfo_inherited_from (binfo, base_binfo, *inheriting_ctor))) 1448 { 1449 argtype = inherited_parms; 1450 /* Don't check access on the inherited constructor. */ 1451 if (flag_new_inheriting_ctors) 1452 defer = dk_deferred; 1453 } 1454 /* To be conservative, ignore access to the base dtor that 1455 DR1658 instructs us to ignore. See the comment in 1456 synthesized_method_walk. */ 1457 else if (cxx_dialect >= cxx14 && fnname == complete_dtor_identifier 1458 && BINFO_VIRTUAL_P (base_binfo) 1459 && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo))) 1460 defer = dk_no_check; 1461 1462 if (defer != dk_no_deferred) 1463 push_deferring_access_checks (defer); 1464 tree rval = locate_fn_flags (base_binfo, fnname, argtype, flags, 1465 diag ? tf_warning_or_error : tf_none); 1466 if (defer != dk_no_deferred) 1467 pop_deferring_access_checks (); 1468 1469 /* Replace an inherited template with the appropriate specialization. */ 1470 if (inherited_binfo && rval 1471 && DECL_P (*inheriting_ctor) && DECL_P (rval) 1472 && DECL_CONTEXT (*inheriting_ctor) == DECL_CONTEXT (rval)) 1473 *inheriting_ctor = DECL_CLONED_FUNCTION (rval); 1474 1475 process_subob_fn (rval, spec_p, trivial_p, deleted_p, 1476 constexpr_p, diag, BINFO_TYPE (base_binfo)); 1477 if (ctor_p && 1478 (!BINFO_VIRTUAL_P (base_binfo) 1479 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))) 1480 { 1481 /* In a constructor we also need to check the subobject 1482 destructors for cleanup of partially constructed objects. */ 1483 tree dtor = locate_fn_flags (base_binfo, complete_dtor_identifier, 1484 NULL_TREE, flags, 1485 diag ? tf_warning_or_error : tf_none); 1486 /* Note that we don't pass down trivial_p; the subobject 1487 destructors don't affect triviality of the constructor. Nor 1488 do they affect constexpr-ness (a constant expression doesn't 1489 throw) or exception-specification (a throw from one of the 1490 dtors would be a double-fault). */ 1491 process_subob_fn (dtor, NULL, NULL, deleted_p, NULL, false, 1492 BINFO_TYPE (base_binfo), /*dtor_from_ctor*/true); 1493 } 1494 1495 return rval; 1496 } 1497 1498 /* The caller wants to generate an implicit declaration of SFK for 1499 CTYPE which is const if relevant and CONST_P is set. If SPEC_P, 1500 TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their 1501 referent appropriately. If DIAG is true, we're either being called 1502 from maybe_explain_implicit_delete to give errors, or if 1503 CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */ 1504 1505 static void 1506 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p, 1507 tree *spec_p, bool *trivial_p, bool *deleted_p, 1508 bool *constexpr_p, bool diag, 1509 tree *inheriting_ctor, tree inherited_parms) 1510 { 1511 tree binfo, base_binfo, fnname; 1512 int i; 1513 1514 if (spec_p) 1515 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec); 1516 1517 if (deleted_p) 1518 { 1519 /* "The closure type associated with a lambda-expression has a deleted 1520 default constructor and a deleted copy assignment operator." 1521 This is diagnosed in maybe_explain_implicit_delete. */ 1522 if (LAMBDA_TYPE_P (ctype) 1523 && (sfk == sfk_constructor 1524 || sfk == sfk_copy_assignment)) 1525 { 1526 *deleted_p = true; 1527 return; 1528 } 1529 1530 *deleted_p = false; 1531 } 1532 1533 bool ctor_p = false; 1534 bool assign_p = false; 1535 bool check_vdtor = false; 1536 switch (sfk) 1537 { 1538 case sfk_move_assignment: 1539 case sfk_copy_assignment: 1540 assign_p = true; 1541 fnname = cp_assignment_operator_id (NOP_EXPR); 1542 break; 1543 1544 case sfk_destructor: 1545 check_vdtor = true; 1546 /* The synthesized method will call base dtors, but check complete 1547 here to avoid having to deal with VTT. */ 1548 fnname = complete_dtor_identifier; 1549 break; 1550 1551 case sfk_constructor: 1552 case sfk_move_constructor: 1553 case sfk_copy_constructor: 1554 case sfk_inheriting_constructor: 1555 ctor_p = true; 1556 fnname = complete_ctor_identifier; 1557 break; 1558 1559 default: 1560 gcc_unreachable (); 1561 } 1562 1563 gcc_assert ((sfk == sfk_inheriting_constructor) 1564 == (inheriting_ctor && *inheriting_ctor != NULL_TREE)); 1565 1566 /* If that user-written default constructor would satisfy the 1567 requirements of a constexpr constructor (7.1.5), the 1568 implicitly-defined default constructor is constexpr. 1569 1570 The implicitly-defined copy/move assignment operator is constexpr if 1571 - X is a literal type, and 1572 - the assignment operator selected to copy/move each direct base class 1573 subobject is a constexpr function, and 1574 - for each non-static data member of X that is of class type (or array 1575 thereof), the assignment operator selected to copy/move that 1576 member is a constexpr function. */ 1577 if (constexpr_p) 1578 *constexpr_p = ctor_p || (assign_p && cxx_dialect >= cxx14); 1579 1580 bool move_p = false; 1581 bool copy_arg_p = false; 1582 switch (sfk) 1583 { 1584 case sfk_constructor: 1585 case sfk_destructor: 1586 case sfk_inheriting_constructor: 1587 break; 1588 1589 case sfk_move_constructor: 1590 case sfk_move_assignment: 1591 move_p = true; 1592 /* FALLTHRU */ 1593 case sfk_copy_constructor: 1594 case sfk_copy_assignment: 1595 copy_arg_p = true; 1596 break; 1597 1598 default: 1599 gcc_unreachable (); 1600 } 1601 1602 bool expected_trivial = type_has_trivial_fn (ctype, sfk); 1603 if (trivial_p) 1604 *trivial_p = expected_trivial; 1605 1606 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base 1607 class versions and other properties of the type. But a subobject 1608 class can be trivially copyable and yet have overload resolution 1609 choose a template constructor for initialization, depending on 1610 rvalueness and cv-quals. And furthermore, a member in a base might 1611 be trivial but deleted or otherwise not callable. So we can't exit 1612 early in C++0x. The same considerations apply in C++98/03, but 1613 there the definition of triviality does not consider overload 1614 resolution, so a constructor can be trivial even if it would otherwise 1615 call a non-trivial constructor. */ 1616 if (expected_trivial 1617 && (!copy_arg_p || cxx_dialect < cxx11)) 1618 { 1619 if (constexpr_p && sfk == sfk_constructor) 1620 { 1621 bool cx = trivial_default_constructor_is_constexpr (ctype); 1622 *constexpr_p = cx; 1623 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE) 1624 /* A trivial constructor doesn't have any NSDMI. */ 1625 inform (input_location, "defaulted default constructor does " 1626 "not initialize any non-static data member"); 1627 } 1628 if (!diag && cxx_dialect < cxx11) 1629 return; 1630 } 1631 1632 ++cp_unevaluated_operand; 1633 ++c_inhibit_evaluation_warnings; 1634 push_deferring_access_checks (dk_no_deferred); 1635 1636 tree scope = push_scope (ctype); 1637 1638 int flags = LOOKUP_NORMAL | LOOKUP_SPECULATIVE; 1639 if (sfk != sfk_inheriting_constructor) 1640 flags |= LOOKUP_DEFAULTED; 1641 1642 tsubst_flags_t complain = diag ? tf_warning_or_error : tf_none; 1643 int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED; 1644 1645 for (binfo = TYPE_BINFO (ctype), i = 0; 1646 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 1647 { 1648 if (!assign_p && BINFO_VIRTUAL_P (base_binfo)) 1649 /* We'll handle virtual bases below. */ 1650 continue; 1651 1652 tree fn = synthesized_method_base_walk (binfo, base_binfo, quals, 1653 copy_arg_p, move_p, ctor_p, 1654 inheriting_ctor, 1655 inherited_parms, 1656 fnname, flags, diag, 1657 spec_p, trivial_p, 1658 deleted_p, constexpr_p); 1659 1660 if (diag && assign_p && move_p 1661 && BINFO_VIRTUAL_P (base_binfo) 1662 && fn && TREE_CODE (fn) == FUNCTION_DECL 1663 && move_fn_p (fn) && !trivial_fn_p (fn) 1664 && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo))) 1665 warning (OPT_Wvirtual_move_assign, 1666 "defaulted move assignment for %qT calls a non-trivial " 1667 "move assignment operator for virtual base %qT", 1668 ctype, BINFO_TYPE (base_binfo)); 1669 1670 if (check_vdtor && type_has_virtual_destructor (BINFO_TYPE (base_binfo))) 1671 { 1672 fn = locate_fn_flags (ctype, cp_operator_id (DELETE_EXPR), 1673 ptr_type_node, flags, complain); 1674 /* Unlike for base ctor/op=/dtor, for operator delete it's fine 1675 to have a null fn (no class-specific op delete). */ 1676 if (fn && fn == error_mark_node && deleted_p) 1677 *deleted_p = true; 1678 check_vdtor = false; 1679 } 1680 } 1681 1682 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (ctype); 1683 if (assign_p) 1684 /* Already examined vbases above. */; 1685 else if (vec_safe_is_empty (vbases)) 1686 /* No virtual bases to worry about. */; 1687 else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14 1688 /* DR 1658 specifies that vbases of abstract classes are 1689 ignored for both ctors and dtors. However, that breaks 1690 virtual dtor overriding when the ignored base has a 1691 throwing destructor. So, ignore that piece of 1658. A 1692 defect has been filed (no number yet). */ 1693 && sfk != sfk_destructor) 1694 /* Vbase cdtors are not relevant. */; 1695 else 1696 { 1697 if (constexpr_p) 1698 *constexpr_p = false; 1699 1700 FOR_EACH_VEC_ELT (*vbases, i, base_binfo) 1701 synthesized_method_base_walk (binfo, base_binfo, quals, 1702 copy_arg_p, move_p, ctor_p, 1703 inheriting_ctor, inherited_parms, 1704 fnname, flags, diag, 1705 spec_p, trivial_p, 1706 deleted_p, constexpr_p); 1707 } 1708 1709 /* Now handle the non-static data members. */ 1710 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals, 1711 copy_arg_p, move_p, assign_p, spec_p, trivial_p, 1712 deleted_p, constexpr_p, 1713 diag, flags, complain, /*dtor_from_ctor*/false); 1714 if (ctor_p) 1715 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier, 1716 sfk_destructor, TYPE_UNQUALIFIED, false, 1717 false, false, NULL, NULL, 1718 deleted_p, NULL, 1719 false, flags, complain, /*dtor_from_ctor*/true); 1720 1721 pop_scope (scope); 1722 1723 pop_deferring_access_checks (); 1724 --cp_unevaluated_operand; 1725 --c_inhibit_evaluation_warnings; 1726 } 1727 1728 /* DECL is a defaulted function whose exception specification is now 1729 needed. Return what it should be. */ 1730 1731 tree 1732 get_defaulted_eh_spec (tree decl) 1733 { 1734 if (DECL_CLONED_FUNCTION_P (decl)) 1735 decl = DECL_CLONED_FUNCTION (decl); 1736 special_function_kind sfk = special_function_p (decl); 1737 tree ctype = DECL_CONTEXT (decl); 1738 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl); 1739 tree parm_type = TREE_VALUE (parms); 1740 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type)); 1741 tree spec = empty_except_spec; 1742 tree inh = DECL_INHERITED_CTOR (decl); 1743 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL, 1744 NULL, false, &inh, parms); 1745 return spec; 1746 } 1747 1748 /* DECL is a deleted function. If it's implicitly deleted, explain why and 1749 return true; else return false. */ 1750 1751 bool 1752 maybe_explain_implicit_delete (tree decl) 1753 { 1754 /* If decl is a clone, get the primary variant. */ 1755 decl = DECL_ORIGIN (decl); 1756 gcc_assert (DECL_DELETED_FN (decl)); 1757 if (DECL_DEFAULTED_FN (decl)) 1758 { 1759 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */ 1760 static hash_set<tree> *explained; 1761 1762 special_function_kind sfk; 1763 location_t loc; 1764 bool informed; 1765 tree ctype; 1766 1767 if (!explained) 1768 explained = new hash_set<tree>; 1769 if (explained->add (decl)) 1770 return true; 1771 1772 sfk = special_function_p (decl); 1773 ctype = DECL_CONTEXT (decl); 1774 loc = input_location; 1775 input_location = DECL_SOURCE_LOCATION (decl); 1776 1777 informed = false; 1778 if (LAMBDA_TYPE_P (ctype)) 1779 { 1780 informed = true; 1781 if (sfk == sfk_constructor) 1782 inform (DECL_SOURCE_LOCATION (decl), 1783 "a lambda closure type has a deleted default constructor"); 1784 else if (sfk == sfk_copy_assignment) 1785 inform (DECL_SOURCE_LOCATION (decl), 1786 "a lambda closure type has a deleted copy assignment operator"); 1787 else 1788 informed = false; 1789 } 1790 else if (DECL_ARTIFICIAL (decl) 1791 && (sfk == sfk_copy_assignment 1792 || sfk == sfk_copy_constructor) 1793 && (type_has_user_declared_move_constructor (ctype) 1794 || type_has_user_declared_move_assign (ctype))) 1795 { 1796 inform (DECL_SOURCE_LOCATION (decl), 1797 "%q#D is implicitly declared as deleted because %qT " 1798 "declares a move constructor or move assignment operator", 1799 decl, ctype); 1800 informed = true; 1801 } 1802 else if (sfk == sfk_inheriting_constructor) 1803 { 1804 tree binfo = inherited_ctor_binfo (decl); 1805 if (TREE_CODE (binfo) != TREE_BINFO) 1806 { 1807 inform (DECL_SOURCE_LOCATION (decl), 1808 "%q#D inherits from multiple base subobjects", 1809 decl); 1810 informed = true; 1811 } 1812 } 1813 if (!informed) 1814 { 1815 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl); 1816 tree parm_type = TREE_VALUE (parms); 1817 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type)); 1818 tree raises = NULL_TREE; 1819 bool deleted_p = false; 1820 tree scope = push_scope (ctype); 1821 tree inh = DECL_INHERITED_CTOR (decl); 1822 1823 synthesized_method_walk (ctype, sfk, const_p, 1824 &raises, NULL, &deleted_p, NULL, false, 1825 &inh, parms); 1826 if (deleted_p) 1827 { 1828 inform (DECL_SOURCE_LOCATION (decl), 1829 "%q#D is implicitly deleted because the default " 1830 "definition would be ill-formed:", decl); 1831 synthesized_method_walk (ctype, sfk, const_p, 1832 NULL, NULL, NULL, NULL, true, 1833 &inh, parms); 1834 } 1835 else if (!comp_except_specs 1836 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)), 1837 raises, ce_normal)) 1838 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly " 1839 "deleted because its exception-specification does not " 1840 "match the implicit exception-specification %qX", 1841 decl, raises); 1842 else if (flag_checking) 1843 gcc_unreachable (); 1844 1845 pop_scope (scope); 1846 } 1847 1848 input_location = loc; 1849 return true; 1850 } 1851 return false; 1852 } 1853 1854 /* DECL is a defaulted function which was declared constexpr. Explain why 1855 it can't be constexpr. */ 1856 1857 void 1858 explain_implicit_non_constexpr (tree decl) 1859 { 1860 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl)); 1861 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type)); 1862 tree inh = DECL_INHERITED_CTOR (decl); 1863 bool dummy; 1864 synthesized_method_walk (DECL_CLASS_CONTEXT (decl), 1865 special_function_p (decl), const_p, 1866 NULL, NULL, NULL, &dummy, true, 1867 &inh, 1868 FUNCTION_FIRST_USER_PARMTYPE (decl)); 1869 } 1870 1871 /* DECL is an instantiation of an inheriting constructor template. Deduce 1872 the correct exception-specification and deletedness for this particular 1873 specialization. */ 1874 1875 void 1876 deduce_inheriting_ctor (tree decl) 1877 { 1878 decl = DECL_ORIGIN (decl); 1879 gcc_assert (DECL_INHERITED_CTOR (decl)); 1880 tree spec; 1881 bool trivial, constexpr_, deleted; 1882 tree inh = DECL_INHERITED_CTOR (decl); 1883 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor, 1884 false, &spec, &trivial, &deleted, &constexpr_, 1885 /*diag*/false, 1886 &inh, 1887 FUNCTION_FIRST_USER_PARMTYPE (decl)); 1888 if (TREE_CODE (inherited_ctor_binfo (decl)) != TREE_BINFO) 1889 /* Inherited the same constructor from different base subobjects. */ 1890 deleted = true; 1891 DECL_DELETED_FN (decl) = deleted; 1892 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec); 1893 SET_DECL_INHERITED_CTOR (decl, inh); 1894 1895 tree clone; 1896 FOR_EACH_CLONE (clone, decl) 1897 { 1898 DECL_DELETED_FN (clone) = deleted; 1899 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec); 1900 SET_DECL_INHERITED_CTOR (clone, inh); 1901 } 1902 } 1903 1904 /* Implicitly declare the special function indicated by KIND, as a 1905 member of TYPE. For copy constructors and assignment operators, 1906 CONST_P indicates whether these functions should take a const 1907 reference argument or a non-const reference. Returns the 1908 FUNCTION_DECL for the implicitly declared function. */ 1909 1910 tree 1911 implicitly_declare_fn (special_function_kind kind, tree type, 1912 bool const_p, tree inherited_ctor, 1913 tree inherited_parms) 1914 { 1915 tree fn; 1916 tree parameter_types = void_list_node; 1917 tree return_type; 1918 tree fn_type; 1919 tree raises = empty_except_spec; 1920 tree rhs_parm_type = NULL_TREE; 1921 tree this_parm; 1922 tree name; 1923 HOST_WIDE_INT saved_processing_template_decl; 1924 bool deleted_p; 1925 bool constexpr_p; 1926 1927 /* Because we create declarations for implicitly declared functions 1928 lazily, we may be creating the declaration for a member of TYPE 1929 while in some completely different context. However, TYPE will 1930 never be a dependent class (because we never want to do lookups 1931 for implicitly defined functions in a dependent class). 1932 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here 1933 because we only create clones for constructors and destructors 1934 when not in a template. */ 1935 gcc_assert (!dependent_type_p (type)); 1936 saved_processing_template_decl = processing_template_decl; 1937 processing_template_decl = 0; 1938 1939 type = TYPE_MAIN_VARIANT (type); 1940 1941 if (targetm.cxx.cdtor_returns_this ()) 1942 { 1943 if (kind == sfk_destructor) 1944 /* See comment in check_special_function_return_type. */ 1945 return_type = build_pointer_type (void_type_node); 1946 else 1947 return_type = build_pointer_type (type); 1948 } 1949 else 1950 return_type = void_type_node; 1951 1952 switch (kind) 1953 { 1954 case sfk_destructor: 1955 /* Destructor. */ 1956 name = constructor_name (type); 1957 break; 1958 1959 case sfk_constructor: 1960 /* Default constructor. */ 1961 name = constructor_name (type); 1962 break; 1963 1964 case sfk_copy_constructor: 1965 case sfk_copy_assignment: 1966 case sfk_move_constructor: 1967 case sfk_move_assignment: 1968 case sfk_inheriting_constructor: 1969 { 1970 bool move_p; 1971 if (kind == sfk_copy_assignment 1972 || kind == sfk_move_assignment) 1973 { 1974 return_type = build_reference_type (type); 1975 name = cp_assignment_operator_id (NOP_EXPR); 1976 } 1977 else 1978 name = constructor_name (type); 1979 1980 if (kind == sfk_inheriting_constructor) 1981 parameter_types = inherited_parms; 1982 else 1983 { 1984 if (const_p) 1985 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST); 1986 else 1987 rhs_parm_type = type; 1988 move_p = (kind == sfk_move_assignment 1989 || kind == sfk_move_constructor); 1990 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p); 1991 1992 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types); 1993 } 1994 break; 1995 } 1996 default: 1997 gcc_unreachable (); 1998 } 1999 2000 bool trivial_p = false; 2001 2002 if (inherited_ctor) 2003 { 2004 /* For an inheriting constructor, just copy these flags from the 2005 inherited constructor until deduce_inheriting_ctor. */ 2006 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor)); 2007 deleted_p = DECL_DELETED_FN (inherited_ctor); 2008 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor); 2009 } 2010 else if (cxx_dialect >= cxx11) 2011 { 2012 raises = unevaluated_noexcept_spec (); 2013 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p, 2014 &deleted_p, &constexpr_p, false, 2015 &inherited_ctor, inherited_parms); 2016 } 2017 else 2018 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p, 2019 &deleted_p, &constexpr_p, false, 2020 &inherited_ctor, inherited_parms); 2021 /* Don't bother marking a deleted constructor as constexpr. */ 2022 if (deleted_p) 2023 constexpr_p = false; 2024 /* A trivial copy/move constructor is also a constexpr constructor, 2025 unless the class has virtual bases (7.1.5p4). */ 2026 else if (trivial_p && cxx_dialect >= cxx11 2027 && (kind == sfk_copy_constructor 2028 || kind == sfk_move_constructor) 2029 && !CLASSTYPE_VBASECLASSES (type)) 2030 gcc_assert (constexpr_p); 2031 2032 if (!trivial_p && type_has_trivial_fn (type, kind)) 2033 type_set_nontrivial_flag (type, kind); 2034 2035 /* Create the function. */ 2036 fn_type = build_method_type_directly (type, return_type, parameter_types); 2037 if (raises) 2038 fn_type = build_exception_variant (fn_type, raises); 2039 fn = build_lang_decl (FUNCTION_DECL, name, fn_type); 2040 if (kind != sfk_inheriting_constructor) 2041 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type)); 2042 if (kind == sfk_constructor || kind == sfk_copy_constructor 2043 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor) 2044 DECL_CONSTRUCTOR_P (fn) = 1; 2045 else if (kind == sfk_destructor) 2046 DECL_DESTRUCTOR_P (fn) = 1; 2047 else 2048 { 2049 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1; 2050 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR); 2051 } 2052 2053 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY); 2054 2055 /* Create the explicit arguments. */ 2056 if (rhs_parm_type) 2057 { 2058 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we 2059 want its type to be included in the mangled function 2060 name. */ 2061 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type); 2062 TREE_READONLY (decl) = 1; 2063 retrofit_lang_decl (decl); 2064 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1; 2065 DECL_ARGUMENTS (fn) = decl; 2066 } 2067 else if (kind == sfk_inheriting_constructor) 2068 { 2069 tree *p = &DECL_ARGUMENTS (fn); 2070 int index = 1; 2071 for (tree parm = inherited_parms; parm && parm != void_list_node; 2072 parm = TREE_CHAIN (parm)) 2073 { 2074 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm)); 2075 retrofit_lang_decl (*p); 2076 DECL_PARM_LEVEL (*p) = 1; 2077 DECL_PARM_INDEX (*p) = index++; 2078 DECL_CONTEXT (*p) = fn; 2079 p = &DECL_CHAIN (*p); 2080 } 2081 SET_DECL_INHERITED_CTOR (fn, inherited_ctor); 2082 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor); 2083 /* A constructor so declared has the same access as the corresponding 2084 constructor in X. */ 2085 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor); 2086 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor); 2087 /* Copy constexpr from the inherited constructor even if the 2088 inheriting constructor doesn't satisfy the requirements. */ 2089 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor); 2090 } 2091 /* Add the "this" parameter. */ 2092 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED); 2093 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn); 2094 DECL_ARGUMENTS (fn) = this_parm; 2095 2096 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL); 2097 DECL_IN_AGGR_P (fn) = 1; 2098 DECL_ARTIFICIAL (fn) = 1; 2099 DECL_DEFAULTED_FN (fn) = 1; 2100 if (cxx_dialect >= cxx11) 2101 { 2102 DECL_DELETED_FN (fn) = deleted_p; 2103 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p; 2104 } 2105 DECL_EXTERNAL (fn) = true; 2106 DECL_NOT_REALLY_EXTERN (fn) = 1; 2107 DECL_DECLARED_INLINE_P (fn) = 1; 2108 set_linkage_according_to_type (type, fn); 2109 if (TREE_PUBLIC (fn)) 2110 DECL_COMDAT (fn) = 1; 2111 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof); 2112 gcc_assert (!TREE_USED (fn)); 2113 2114 /* Propagate constraints from the inherited constructor. */ 2115 if (flag_concepts && inherited_ctor) 2116 if (tree orig_ci = get_constraints (inherited_ctor)) 2117 { 2118 tree new_ci = copy_node (orig_ci); 2119 set_constraints (fn, new_ci); 2120 } 2121 2122 /* Restore PROCESSING_TEMPLATE_DECL. */ 2123 processing_template_decl = saved_processing_template_decl; 2124 2125 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL) 2126 fn = add_inherited_template_parms (fn, inherited_ctor); 2127 2128 /* Warn about calling a non-trivial move assignment in a virtual base. */ 2129 if (kind == sfk_move_assignment && !deleted_p && !trivial_p 2130 && CLASSTYPE_VBASECLASSES (type)) 2131 { 2132 location_t loc = input_location; 2133 input_location = DECL_SOURCE_LOCATION (fn); 2134 synthesized_method_walk (type, kind, const_p, 2135 NULL, NULL, NULL, NULL, true, 2136 NULL, NULL_TREE); 2137 input_location = loc; 2138 } 2139 2140 return fn; 2141 } 2142 2143 /* Gives any errors about defaulted functions which need to be deferred 2144 until the containing class is complete. */ 2145 2146 void 2147 defaulted_late_check (tree fn) 2148 { 2149 /* Complain about invalid signature for defaulted fn. */ 2150 tree ctx = DECL_CONTEXT (fn); 2151 special_function_kind kind = special_function_p (fn); 2152 bool fn_const_p = (copy_fn_p (fn) == 2); 2153 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p, 2154 NULL, NULL); 2155 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn)); 2156 2157 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)), 2158 TREE_TYPE (TREE_TYPE (implicit_fn))) 2159 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), 2160 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn)))) 2161 { 2162 error ("defaulted declaration %q+D", fn); 2163 error_at (DECL_SOURCE_LOCATION (fn), 2164 "does not match expected signature %qD", implicit_fn); 2165 } 2166 2167 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit 2168 exception-specification only if it is compatible (15.4) with the 2169 exception-specification on the implicit declaration. If a function 2170 is explicitly defaulted on its first declaration, (...) it is 2171 implicitly considered to have the same exception-specification as if 2172 it had been implicitly declared. */ 2173 maybe_instantiate_noexcept (fn); 2174 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)); 2175 if (!fn_spec) 2176 { 2177 if (DECL_DEFAULTED_IN_CLASS_P (fn)) 2178 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec); 2179 } 2180 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec)) 2181 /* Equivalent to the implicit spec. */; 2182 else if (DECL_DEFAULTED_IN_CLASS_P (fn) 2183 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx)) 2184 /* We can't compare an explicit exception-specification on a 2185 constructor defaulted in the class body to the implicit 2186 exception-specification until after we've parsed any NSDMI; see 2187 after_nsdmi_defaulted_late_checks. */; 2188 else 2189 { 2190 tree eh_spec = get_defaulted_eh_spec (fn); 2191 if (!comp_except_specs (fn_spec, eh_spec, ce_normal)) 2192 { 2193 if (DECL_DEFAULTED_IN_CLASS_P (fn)) 2194 DECL_DELETED_FN (fn) = true; 2195 else 2196 error ("function %q+D defaulted on its redeclaration " 2197 "with an exception-specification that differs from " 2198 "the implicit exception-specification %qX", fn, eh_spec); 2199 } 2200 } 2201 2202 if (DECL_DEFAULTED_IN_CLASS_P (fn) 2203 && DECL_DECLARED_CONSTEXPR_P (implicit_fn)) 2204 { 2205 /* Hmm...should we do this for out-of-class too? Should it be OK to 2206 add constexpr later like inline, rather than requiring 2207 declarations to match? */ 2208 DECL_DECLARED_CONSTEXPR_P (fn) = true; 2209 if (kind == sfk_constructor) 2210 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true; 2211 } 2212 2213 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn) 2214 && DECL_DECLARED_CONSTEXPR_P (fn)) 2215 { 2216 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx)) 2217 { 2218 error ("explicitly defaulted function %q+D cannot be declared " 2219 "as constexpr because the implicit declaration is not " 2220 "constexpr:", fn); 2221 explain_implicit_non_constexpr (fn); 2222 } 2223 DECL_DECLARED_CONSTEXPR_P (fn) = false; 2224 } 2225 2226 if (DECL_DELETED_FN (implicit_fn)) 2227 DECL_DELETED_FN (fn) = 1; 2228 } 2229 2230 /* OK, we've parsed the NSDMI for class T, now we can check any explicit 2231 exception-specifications on functions defaulted in the class body. */ 2232 2233 void 2234 after_nsdmi_defaulted_late_checks (tree t) 2235 { 2236 if (uses_template_parms (t)) 2237 return; 2238 if (t == error_mark_node) 2239 return; 2240 for (tree fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn)) 2241 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn)) 2242 { 2243 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)); 2244 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec)) 2245 continue; 2246 2247 tree eh_spec = get_defaulted_eh_spec (fn); 2248 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)), 2249 eh_spec, ce_normal)) 2250 DECL_DELETED_FN (fn) = true; 2251 } 2252 } 2253 2254 /* Returns true iff FN can be explicitly defaulted, and gives any 2255 errors if defaulting FN is ill-formed. */ 2256 2257 bool 2258 defaultable_fn_check (tree fn) 2259 { 2260 special_function_kind kind = sfk_none; 2261 2262 if (template_parm_scope_p ()) 2263 { 2264 error ("a template cannot be defaulted"); 2265 return false; 2266 } 2267 2268 if (DECL_CONSTRUCTOR_P (fn)) 2269 { 2270 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node) 2271 kind = sfk_constructor; 2272 else if (copy_fn_p (fn) > 0 2273 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn)) 2274 == void_list_node)) 2275 kind = sfk_copy_constructor; 2276 else if (move_fn_p (fn)) 2277 kind = sfk_move_constructor; 2278 } 2279 else if (DECL_DESTRUCTOR_P (fn)) 2280 kind = sfk_destructor; 2281 else if (DECL_ASSIGNMENT_OPERATOR_P (fn) 2282 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR) 2283 { 2284 if (copy_fn_p (fn)) 2285 kind = sfk_copy_assignment; 2286 else if (move_fn_p (fn)) 2287 kind = sfk_move_assignment; 2288 } 2289 2290 if (kind == sfk_none) 2291 { 2292 error ("%qD cannot be defaulted", fn); 2293 return false; 2294 } 2295 else 2296 { 2297 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn); 2298 t && t != void_list_node; t = TREE_CHAIN (t)) 2299 if (TREE_PURPOSE (t)) 2300 { 2301 error ("defaulted function %q+D with default argument", fn); 2302 break; 2303 } 2304 2305 /* Avoid do_warn_unused_parameter warnings. */ 2306 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p)) 2307 if (DECL_NAME (p)) 2308 TREE_NO_WARNING (p) = 1; 2309 2310 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn))) 2311 /* Defer checking. */; 2312 else if (!processing_template_decl) 2313 defaulted_late_check (fn); 2314 2315 return true; 2316 } 2317 } 2318 2319 /* Add an implicit declaration to TYPE for the kind of function 2320 indicated by SFK. Return the FUNCTION_DECL for the new implicit 2321 declaration. */ 2322 2323 tree 2324 lazily_declare_fn (special_function_kind sfk, tree type) 2325 { 2326 tree fn; 2327 /* Whether or not the argument has a const reference type. */ 2328 bool const_p = false; 2329 2330 type = TYPE_MAIN_VARIANT (type); 2331 2332 switch (sfk) 2333 { 2334 case sfk_constructor: 2335 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0; 2336 break; 2337 case sfk_copy_constructor: 2338 const_p = TYPE_HAS_CONST_COPY_CTOR (type); 2339 CLASSTYPE_LAZY_COPY_CTOR (type) = 0; 2340 break; 2341 case sfk_move_constructor: 2342 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0; 2343 break; 2344 case sfk_copy_assignment: 2345 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type); 2346 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0; 2347 break; 2348 case sfk_move_assignment: 2349 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0; 2350 break; 2351 case sfk_destructor: 2352 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0; 2353 break; 2354 default: 2355 gcc_unreachable (); 2356 } 2357 2358 /* Declare the function. */ 2359 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL); 2360 2361 /* [class.copy]/8 If the class definition declares a move constructor or 2362 move assignment operator, the implicitly declared copy constructor is 2363 defined as deleted.... */ 2364 if ((sfk == sfk_copy_assignment 2365 || sfk == sfk_copy_constructor) 2366 && (type_has_user_declared_move_constructor (type) 2367 || type_has_user_declared_move_assign (type))) 2368 DECL_DELETED_FN (fn) = true; 2369 2370 /* A destructor may be virtual. */ 2371 if (sfk == sfk_destructor 2372 || sfk == sfk_move_assignment 2373 || sfk == sfk_copy_assignment) 2374 check_for_override (fn, type); 2375 /* Add it to CLASSTYPE_METHOD_VEC. */ 2376 add_method (type, fn, NULL_TREE); 2377 /* Add it to TYPE_METHODS. */ 2378 if (sfk == sfk_destructor 2379 && DECL_VIRTUAL_P (fn)) 2380 /* The ABI requires that a virtual destructor go at the end of the 2381 vtable. */ 2382 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn); 2383 else 2384 { 2385 DECL_CHAIN (fn) = TYPE_METHODS (type); 2386 TYPE_METHODS (type) = fn; 2387 } 2388 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0); 2389 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn) 2390 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)) 2391 /* Create appropriate clones. */ 2392 clone_function_decl (fn, /*update_method_vec=*/true); 2393 2394 return fn; 2395 } 2396 2397 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST 2398 as there are artificial parms in FN. */ 2399 2400 tree 2401 skip_artificial_parms_for (const_tree fn, tree list) 2402 { 2403 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)) 2404 list = TREE_CHAIN (list); 2405 else 2406 return list; 2407 2408 if (DECL_HAS_IN_CHARGE_PARM_P (fn)) 2409 list = TREE_CHAIN (list); 2410 if (DECL_HAS_VTT_PARM_P (fn)) 2411 list = TREE_CHAIN (list); 2412 return list; 2413 } 2414 2415 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of 2416 artificial parms in FN. */ 2417 2418 int 2419 num_artificial_parms_for (const_tree fn) 2420 { 2421 int count = 0; 2422 2423 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)) 2424 count++; 2425 else 2426 return 0; 2427 2428 if (DECL_HAS_IN_CHARGE_PARM_P (fn)) 2429 count++; 2430 if (DECL_HAS_VTT_PARM_P (fn)) 2431 count++; 2432 return count; 2433 } 2434 2435 2436 #include "gt-cp-method.h" 2437