1 1.1 mrg /* Handle the hair of processing (but not expanding) inline functions. 2 1.1 mrg Also manage function and variable name overloading. 3 1.1 mrg Copyright (C) 1987-2022 Free Software Foundation, Inc. 4 1.1 mrg Contributed by Michael Tiemann (tiemann (at) cygnus.com) 5 1.1 mrg 6 1.1 mrg This file is part of GCC. 7 1.1 mrg 8 1.1 mrg GCC is free software; you can redistribute it and/or modify 9 1.1 mrg it under the terms of the GNU General Public License as published by 10 1.1 mrg the Free Software Foundation; either version 3, or (at your option) 11 1.1 mrg any later version. 12 1.1 mrg 13 1.1 mrg GCC is distributed in the hope that it will be useful, 14 1.1 mrg but WITHOUT ANY WARRANTY; without even the implied warranty of 15 1.1 mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 1.1 mrg GNU General Public License for more details. 17 1.1 mrg 18 1.1 mrg You should have received a copy of the GNU General Public License 19 1.1 mrg along with GCC; see the file COPYING3. If not see 20 1.1 mrg <http://www.gnu.org/licenses/>. */ 21 1.1 mrg 22 1.1 mrg 23 1.1 mrg /* Handle method declarations. */ 24 1.1 mrg #include "config.h" 25 1.1 mrg #include "system.h" 26 1.1 mrg #include "coretypes.h" 27 1.1 mrg #include "target.h" 28 1.1 mrg #include "cp-tree.h" 29 1.1 mrg #include "decl.h" 30 1.1 mrg #include "stringpool.h" 31 1.1 mrg #include "cgraph.h" 32 1.1 mrg #include "varasm.h" 33 1.1 mrg #include "toplev.h" 34 1.1 mrg #include "intl.h" 35 1.1 mrg #include "common/common-target.h" 36 1.1 mrg 37 1.1 mrg static void do_build_copy_assign (tree); 38 1.1 mrg static void do_build_copy_constructor (tree); 39 1.1 mrg static tree make_alias_for_thunk (tree); 40 1.1 mrg 41 1.1 mrg /* Called once to initialize method.cc. */ 42 1.1 mrg 43 1.1 mrg void 44 1.1 mrg init_method (void) 45 1.1 mrg { 46 1.1 mrg init_mangle (); 47 1.1 mrg } 48 1.1 mrg 49 1.1 mrg /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING 51 1.1 mrg indicates whether it is a this or result adjusting thunk. 52 1.1 mrg FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment 53 1.1 mrg (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET 54 1.1 mrg never is. VIRTUAL_OFFSET is the /index/ into the vtable for this 55 1.1 mrg adjusting thunks, we scale it to a byte offset. For covariant 56 1.1 mrg thunks VIRTUAL_OFFSET is the virtual binfo. You must post process 57 1.1 mrg the returned thunk with finish_thunk. */ 58 1.1 mrg 59 1.1 mrg tree 60 1.1 mrg make_thunk (tree function, bool this_adjusting, 61 1.1 mrg tree fixed_offset, tree virtual_offset) 62 1.1 mrg { 63 1.1 mrg HOST_WIDE_INT d; 64 1.1 mrg tree thunk; 65 1.1 mrg 66 1.1 mrg gcc_assert (TREE_CODE (function) == FUNCTION_DECL); 67 1.1 mrg /* We can have this thunks to covariant thunks, but not vice versa. */ 68 1.1 mrg gcc_assert (!DECL_THIS_THUNK_P (function)); 69 1.1 mrg gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting); 70 1.1 mrg 71 1.1 mrg /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */ 72 1.1 mrg if (this_adjusting && virtual_offset) 73 1.1 mrg virtual_offset 74 1.1 mrg = size_binop (MULT_EXPR, 75 1.1 mrg virtual_offset, 76 1.1 mrg convert (ssizetype, 77 1.1 mrg TYPE_SIZE_UNIT (vtable_entry_type))); 78 1.1 mrg 79 1.1 mrg d = tree_to_shwi (fixed_offset); 80 1.1 mrg 81 1.1 mrg /* See if we already have the thunk in question. For this_adjusting 82 1.1 mrg thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it 83 1.1 mrg will be a BINFO. */ 84 1.1 mrg for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk)) 85 1.1 mrg if (DECL_THIS_THUNK_P (thunk) == this_adjusting 86 1.1 mrg && THUNK_FIXED_OFFSET (thunk) == d 87 1.1 mrg && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk) 88 1.1 mrg && (!virtual_offset 89 1.1 mrg || (this_adjusting 90 1.1 mrg ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk), 91 1.1 mrg virtual_offset) 92 1.1 mrg : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset))) 93 1.1 mrg return thunk; 94 1.1 mrg 95 1.1 mrg /* All thunks must be created before FUNCTION is actually emitted; 96 1.1 mrg the ABI requires that all thunks be emitted together with the 97 1.1 mrg function to which they transfer control. */ 98 1.1 mrg gcc_assert (!TREE_ASM_WRITTEN (function)); 99 1.1 mrg /* Likewise, we can only be adding thunks to a function declared in 100 1.1 mrg the class currently being laid out. */ 101 1.1 mrg gcc_assert (TYPE_SIZE (DECL_CONTEXT (function)) 102 1.1 mrg && TYPE_BEING_DEFINED (DECL_CONTEXT (function))); 103 1.1 mrg 104 1.1 mrg thunk = build_decl (DECL_SOURCE_LOCATION (function), 105 1.1 mrg FUNCTION_DECL, NULL_TREE, TREE_TYPE (function)); 106 1.1 mrg DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function); 107 1.1 mrg cxx_dup_lang_specific_decl (thunk); 108 1.1 mrg DECL_VIRTUAL_P (thunk) = true; 109 1.1 mrg SET_DECL_THUNKS (thunk, NULL_TREE); 110 1.1 mrg 111 1.1 mrg DECL_CONTEXT (thunk) = DECL_CONTEXT (function); 112 1.1 mrg TREE_READONLY (thunk) = TREE_READONLY (function); 113 1.1 mrg TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function); 114 1.1 mrg TREE_PUBLIC (thunk) = TREE_PUBLIC (function); 115 1.1 mrg SET_DECL_THUNK_P (thunk, this_adjusting); 116 1.1 mrg THUNK_TARGET (thunk) = function; 117 1.1 mrg THUNK_FIXED_OFFSET (thunk) = d; 118 1.1 mrg THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset; 119 1.1 mrg THUNK_ALIAS (thunk) = NULL_TREE; 120 1.1 mrg 121 1.1 mrg DECL_INTERFACE_KNOWN (thunk) = 1; 122 1.1 mrg DECL_NOT_REALLY_EXTERN (thunk) = 1; 123 1.1 mrg DECL_COMDAT (thunk) = DECL_COMDAT (function); 124 1.1 mrg DECL_SAVED_AUTO_RETURN_TYPE (thunk) = NULL; 125 1.1 mrg /* The thunk itself is not a constructor or destructor, even if 126 1.1 mrg the thing it is thunking to is. */ 127 1.1 mrg DECL_CXX_DESTRUCTOR_P (thunk) = 0; 128 1.1 mrg DECL_CXX_CONSTRUCTOR_P (thunk) = 0; 129 1.1 mrg DECL_EXTERNAL (thunk) = 1; 130 1.1 mrg DECL_ARTIFICIAL (thunk) = 1; 131 1.1 mrg /* The THUNK is not a pending inline, even if the FUNCTION is. */ 132 1.1 mrg DECL_PENDING_INLINE_P (thunk) = 0; 133 1.1 mrg DECL_DECLARED_INLINE_P (thunk) = 0; 134 1.1 mrg /* Nor is it a template instantiation. */ 135 1.1 mrg DECL_USE_TEMPLATE (thunk) = 0; 136 1.1 mrg DECL_TEMPLATE_INFO (thunk) = NULL; 137 1.1 mrg 138 1.1 mrg /* Add it to the list of thunks associated with FUNCTION. */ 139 1.1 mrg DECL_CHAIN (thunk) = DECL_THUNKS (function); 140 1.1 mrg SET_DECL_THUNKS (function, thunk); 141 1.1 mrg 142 1.1 mrg return thunk; 143 1.1 mrg } 144 1.1 mrg 145 1.1 mrg /* Finish THUNK, a thunk decl. */ 146 1.1 mrg 147 1.1 mrg void 148 1.1 mrg finish_thunk (tree thunk) 149 1.1 mrg { 150 1.1 mrg tree function, name; 151 1.1 mrg tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk)); 152 1.1 mrg tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk); 153 1.1 mrg 154 1.1 mrg gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk)); 155 1.1 mrg if (virtual_offset && DECL_RESULT_THUNK_P (thunk)) 156 1.1 mrg virtual_offset = BINFO_VPTR_FIELD (virtual_offset); 157 1.1 mrg function = THUNK_TARGET (thunk); 158 1.1 mrg name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk), 159 1.1 mrg fixed_offset, virtual_offset, thunk); 160 1.1 mrg 161 1.1 mrg /* We can end up with declarations of (logically) different 162 1.1 mrg covariant thunks, that do identical adjustments. The two thunks 163 1.1 mrg will be adjusting between within different hierarchies, which 164 1.1 mrg happen to have the same layout. We must nullify one of them to 165 1.1 mrg refer to the other. */ 166 1.1 mrg if (DECL_RESULT_THUNK_P (thunk)) 167 1.1 mrg { 168 1.1 mrg tree cov_probe; 169 1.1 mrg 170 1.1 mrg for (cov_probe = DECL_THUNKS (function); 171 1.1 mrg cov_probe; cov_probe = DECL_CHAIN (cov_probe)) 172 1.1 mrg if (DECL_NAME (cov_probe) == name) 173 1.1 mrg { 174 1.1 mrg gcc_assert (!DECL_THUNKS (thunk)); 175 1.1 mrg THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe) 176 1.1 mrg ? THUNK_ALIAS (cov_probe) : cov_probe); 177 1.1 mrg break; 178 1.1 mrg } 179 1.1 mrg } 180 1.1 mrg 181 1.1 mrg DECL_NAME (thunk) = name; 182 1.1 mrg SET_DECL_ASSEMBLER_NAME (thunk, name); 183 1.1 mrg } 184 1.1 mrg 185 1.1 mrg static GTY (()) int thunk_labelno; 186 1.1 mrg 187 1.1 mrg /* Create a static alias to target. */ 188 1.1 mrg 189 1.1 mrg tree 190 1.1 mrg make_alias_for (tree target, tree newid) 191 1.1 mrg { 192 1.1 mrg tree alias = build_decl (DECL_SOURCE_LOCATION (target), 193 1.1 mrg TREE_CODE (target), newid, TREE_TYPE (target)); 194 1.1 mrg DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target); 195 1.1 mrg cxx_dup_lang_specific_decl (alias); 196 1.1 mrg DECL_CONTEXT (alias) = DECL_CONTEXT (target); 197 1.1 mrg TREE_READONLY (alias) = TREE_READONLY (target); 198 1.1 mrg TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target); 199 1.1 mrg TREE_PUBLIC (alias) = 0; 200 1.1 mrg DECL_INTERFACE_KNOWN (alias) = 1; 201 1.1 mrg if (DECL_LANG_SPECIFIC (alias)) 202 1.1 mrg { 203 1.1 mrg DECL_NOT_REALLY_EXTERN (alias) = 1; 204 1.1 mrg DECL_USE_TEMPLATE (alias) = 0; 205 1.1 mrg DECL_TEMPLATE_INFO (alias) = NULL; 206 1.1 mrg } 207 1.1 mrg DECL_EXTERNAL (alias) = 0; 208 1.1 mrg DECL_ARTIFICIAL (alias) = 1; 209 1.1 mrg DECL_TEMPLATE_INSTANTIATED (alias) = 0; 210 1.1 mrg if (TREE_CODE (alias) == FUNCTION_DECL) 211 1.1 mrg { 212 1.1 mrg DECL_SAVED_AUTO_RETURN_TYPE (alias) = NULL; 213 1.1 mrg DECL_CXX_DESTRUCTOR_P (alias) = 0; 214 1.1 mrg DECL_CXX_CONSTRUCTOR_P (alias) = 0; 215 1.1 mrg DECL_PENDING_INLINE_P (alias) = 0; 216 1.1 mrg DECL_DECLARED_INLINE_P (alias) = 0; 217 1.1 mrg DECL_INITIAL (alias) = error_mark_node; 218 1.1 mrg DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target)); 219 1.1 mrg } 220 1.1 mrg else 221 1.1 mrg TREE_STATIC (alias) = 1; 222 1.1 mrg TREE_ADDRESSABLE (alias) = 1; 223 1.1 mrg TREE_USED (alias) = 1; 224 1.1 mrg SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias)); 225 1.1 mrg return alias; 226 1.1 mrg } 227 1.1 mrg 228 1.1 mrg static tree 229 1.1 mrg make_alias_for_thunk (tree function) 230 1.1 mrg { 231 1.1 mrg tree alias; 232 1.1 mrg char buf[256]; 233 1.1 mrg 234 1.1 mrg targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno); 235 1.1 mrg thunk_labelno++; 236 1.1 mrg 237 1.1 mrg alias = make_alias_for (function, get_identifier (buf)); 238 1.1 mrg 239 1.1 mrg if (!flag_syntax_only) 240 1.1 mrg { 241 1.1 mrg struct cgraph_node *funcn, *aliasn; 242 1.1 mrg funcn = cgraph_node::get (function); 243 1.1 mrg gcc_checking_assert (funcn); 244 1.1 mrg aliasn = cgraph_node::create_same_body_alias (alias, function); 245 1.1 mrg DECL_ASSEMBLER_NAME (function); 246 1.1 mrg gcc_assert (aliasn != NULL); 247 1.1 mrg } 248 1.1 mrg 249 1.1 mrg return alias; 250 1.1 mrg } 251 1.1 mrg 252 1.1 mrg /* Emit the definition of a C++ multiple inheritance or covariant 253 1.1 mrg return vtable thunk. If EMIT_P is nonzero, the thunk is emitted 254 1.1 mrg immediately. */ 255 1.1 mrg 256 1.1 mrg void 257 1.1 mrg use_thunk (tree thunk_fndecl, bool emit_p) 258 1.1 mrg { 259 1.1 mrg tree a, t, function, alias; 260 1.1 mrg tree virtual_offset; 261 1.1 mrg HOST_WIDE_INT fixed_offset, virtual_value; 262 1.1 mrg bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl); 263 1.1 mrg struct cgraph_node *funcn, *thunk_node; 264 1.1 mrg 265 1.1 mrg /* We should have called finish_thunk to give it a name. */ 266 1.1 mrg gcc_assert (DECL_NAME (thunk_fndecl)); 267 1.1 mrg 268 1.1 mrg /* We should never be using an alias, always refer to the 269 1.1 mrg aliased thunk. */ 270 1.1 mrg gcc_assert (!THUNK_ALIAS (thunk_fndecl)); 271 1.1 mrg 272 1.1 mrg if (TREE_ASM_WRITTEN (thunk_fndecl)) 273 1.1 mrg return; 274 1.1 mrg 275 1.1 mrg function = THUNK_TARGET (thunk_fndecl); 276 1.1 mrg if (DECL_RESULT (thunk_fndecl)) 277 1.1 mrg /* We already turned this thunk into an ordinary function. 278 1.1 mrg There's no need to process this thunk again. */ 279 1.1 mrg return; 280 1.1 mrg 281 1.1 mrg if (DECL_THUNK_P (function)) 282 1.1 mrg /* The target is itself a thunk, process it now. */ 283 1.1 mrg use_thunk (function, emit_p); 284 1.1 mrg 285 1.1 mrg /* Thunks are always addressable; they only appear in vtables. */ 286 1.1 mrg TREE_ADDRESSABLE (thunk_fndecl) = 1; 287 1.1 mrg 288 1.1 mrg /* Don't diagnose deprecated or unavailable functions just because they 289 1.1 mrg have thunks emitted for them. */ 290 1.1 mrg auto du = make_temp_override (deprecated_state, 291 1.1 mrg UNAVAILABLE_DEPRECATED_SUPPRESS); 292 1.1 mrg 293 1.1 mrg /* Figure out what function is being thunked to. It's referenced in 294 1.1 mrg this translation unit. */ 295 1.1 mrg TREE_ADDRESSABLE (function) = 1; 296 1.1 mrg mark_used (function); 297 1.1 mrg if (!emit_p) 298 1.1 mrg return; 299 1.1 mrg 300 1.1 mrg if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)) 301 1.1 mrg alias = make_alias_for_thunk (function); 302 1.1 mrg else 303 1.1 mrg alias = function; 304 1.1 mrg 305 1.1 mrg fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl); 306 1.1 mrg virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl); 307 1.1 mrg 308 1.1 mrg if (virtual_offset) 309 1.1 mrg { 310 1.1 mrg if (!this_adjusting) 311 1.1 mrg virtual_offset = BINFO_VPTR_FIELD (virtual_offset); 312 1.1 mrg virtual_value = tree_to_shwi (virtual_offset); 313 1.1 mrg gcc_assert (virtual_value); 314 1.1 mrg } 315 1.1 mrg else 316 1.1 mrg virtual_value = 0; 317 1.1 mrg 318 1.1 mrg /* And, if we need to emit the thunk, it's used. */ 319 1.1 mrg mark_used (thunk_fndecl); 320 1.1 mrg /* This thunk is actually defined. */ 321 1.1 mrg DECL_EXTERNAL (thunk_fndecl) = 0; 322 1.1 mrg /* The linkage of the function may have changed. FIXME in linkage 323 1.1 mrg rewrite. */ 324 1.1 mrg gcc_assert (DECL_INTERFACE_KNOWN (function)); 325 1.1 mrg TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function); 326 1.1 mrg DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function); 327 1.1 mrg DECL_VISIBILITY_SPECIFIED (thunk_fndecl) 328 1.1 mrg = DECL_VISIBILITY_SPECIFIED (function); 329 1.1 mrg DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function); 330 1.1 mrg DECL_WEAK (thunk_fndecl) = DECL_WEAK (function); 331 1.1 mrg 332 1.1 mrg if (flag_syntax_only) 333 1.1 mrg { 334 1.1 mrg TREE_ASM_WRITTEN (thunk_fndecl) = 1; 335 1.1 mrg return; 336 1.1 mrg } 337 1.1 mrg 338 1.1 mrg push_to_top_level (); 339 1.1 mrg 340 1.1 mrg if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function) 341 1.1 mrg && targetm_common.have_named_sections) 342 1.1 mrg { 343 1.1 mrg tree fn = function; 344 1.1 mrg struct symtab_node *symbol; 345 1.1 mrg 346 1.1 mrg if ((symbol = symtab_node::get (function)) 347 1.1 mrg && symbol->alias) 348 1.1 mrg { 349 1.1 mrg if (symbol->analyzed) 350 1.1 mrg fn = symtab_node::get (function)->ultimate_alias_target ()->decl; 351 1.1 mrg else 352 1.1 mrg fn = symtab_node::get (function)->alias_target; 353 1.1 mrg } 354 1.1 mrg resolve_unique_section (fn, 0, flag_function_sections); 355 1.1 mrg 356 1.1 mrg if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn)) 357 1.1 mrg { 358 1.1 mrg resolve_unique_section (thunk_fndecl, 0, flag_function_sections); 359 1.1 mrg 360 1.1 mrg /* Output the thunk into the same section as function. */ 361 1.1 mrg set_decl_section_name (thunk_fndecl, fn); 362 1.1 mrg symtab_node::get (thunk_fndecl)->implicit_section 363 1.1 mrg = symtab_node::get (fn)->implicit_section; 364 1.1 mrg } 365 1.1 mrg } 366 1.1 mrg 367 1.1 mrg /* Set up cloned argument trees for the thunk. */ 368 1.1 mrg t = NULL_TREE; 369 1.1 mrg for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a)) 370 1.1 mrg { 371 1.1 mrg tree x = copy_node (a); 372 1.1 mrg DECL_CHAIN (x) = t; 373 1.1 mrg DECL_CONTEXT (x) = thunk_fndecl; 374 1.1 mrg SET_DECL_RTL (x, NULL); 375 1.1 mrg DECL_HAS_VALUE_EXPR_P (x) = 0; 376 1.1 mrg TREE_ADDRESSABLE (x) = 0; 377 1.1 mrg t = x; 378 1.1 mrg } 379 1.1 mrg a = nreverse (t); 380 1.1 mrg DECL_ARGUMENTS (thunk_fndecl) = a; 381 1.1 mrg TREE_ASM_WRITTEN (thunk_fndecl) = 1; 382 1.1 mrg funcn = cgraph_node::get (function); 383 1.1 mrg gcc_checking_assert (funcn); 384 1.1 mrg thunk_node = funcn->create_thunk (thunk_fndecl, function, 385 1.1 mrg this_adjusting, fixed_offset, virtual_value, 386 1.1 mrg 0, virtual_offset, alias); 387 1.1 mrg if (DECL_ONE_ONLY (function)) 388 1.1 mrg thunk_node->add_to_same_comdat_group (funcn); 389 1.1 mrg 390 1.1 mrg pop_from_top_level (); 391 1.1 mrg } 392 1.1 mrg 393 1.1 mrg /* Code for synthesizing methods which have default semantics defined. */ 395 1.1 mrg 396 1.1 mrg /* True iff CTYPE has a trivial SFK. */ 397 1.1 mrg 398 1.1 mrg static bool 399 1.1 mrg type_has_trivial_fn (tree ctype, special_function_kind sfk) 400 1.1 mrg { 401 1.1 mrg switch (sfk) 402 1.1 mrg { 403 1.1 mrg case sfk_constructor: 404 1.1 mrg return !TYPE_HAS_COMPLEX_DFLT (ctype); 405 1.1 mrg case sfk_copy_constructor: 406 1.1 mrg return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype); 407 1.1 mrg case sfk_move_constructor: 408 1.1 mrg return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype); 409 1.1 mrg case sfk_copy_assignment: 410 1.1 mrg return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype); 411 1.1 mrg case sfk_move_assignment: 412 1.1 mrg return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype); 413 1.1 mrg case sfk_destructor: 414 1.1 mrg case sfk_virtual_destructor: 415 1.1 mrg return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype); 416 1.1 mrg case sfk_inheriting_constructor: 417 1.1 mrg case sfk_comparison: 418 1.1 mrg return false; 419 1.1 mrg default: 420 1.1 mrg gcc_unreachable (); 421 1.1 mrg } 422 1.1 mrg } 423 1.1 mrg 424 1.1 mrg /* Note that CTYPE has a non-trivial SFK even though we previously thought 425 1.1 mrg it was trivial. */ 426 1.1 mrg 427 1.1 mrg static void 428 1.1 mrg type_set_nontrivial_flag (tree ctype, special_function_kind sfk) 429 1.1 mrg { 430 1.1 mrg switch (sfk) 431 1.1 mrg { 432 1.1 mrg case sfk_constructor: 433 1.1 mrg TYPE_HAS_COMPLEX_DFLT (ctype) = true; 434 1.1 mrg return; 435 1.1 mrg case sfk_copy_constructor: 436 1.1 mrg TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true; 437 1.1 mrg return; 438 1.1 mrg case sfk_move_constructor: 439 1.1 mrg TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true; 440 1.1 mrg return; 441 1.1 mrg case sfk_copy_assignment: 442 1.1 mrg TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true; 443 1.1 mrg return; 444 1.1 mrg case sfk_move_assignment: 445 1.1 mrg TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true; 446 1.1 mrg return; 447 1.1 mrg case sfk_destructor: 448 1.1 mrg TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true; 449 1.1 mrg return; 450 1.1 mrg case sfk_inheriting_constructor: 451 1.1 mrg default: 452 1.1 mrg gcc_unreachable (); 453 1.1 mrg } 454 1.1 mrg } 455 1.1 mrg 456 1.1 mrg /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */ 457 1.1 mrg 458 1.1 mrg bool 459 1.1 mrg trivial_fn_p (tree fn) 460 1.1 mrg { 461 1.1 mrg if (TREE_CODE (fn) == TEMPLATE_DECL) 462 1.1 mrg return false; 463 1.1 mrg if (!DECL_DEFAULTED_FN (fn)) 464 1.1 mrg return false; 465 1.1 mrg 466 1.1 mrg /* If fn is a clone, get the primary variant. */ 467 1.1 mrg if (tree prim = DECL_CLONED_FUNCTION (fn)) 468 1.1 mrg fn = prim; 469 1.1 mrg return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn)); 470 1.1 mrg } 471 1.1 mrg 472 1.1 mrg /* PARM is a PARM_DECL for a function which we want to forward to another 473 1.1 mrg function without changing its value category, a la std::forward. */ 474 1.1 mrg 475 1.1 mrg tree 476 1.1 mrg forward_parm (tree parm) 477 1.1 mrg { 478 1.1 mrg tree exp = convert_from_reference (parm); 479 1.1 mrg tree type = TREE_TYPE (parm); 480 1.1 mrg if (DECL_PACK_P (parm)) 481 1.1 mrg type = PACK_EXPANSION_PATTERN (type); 482 1.1 mrg if (!TYPE_REF_P (type)) 483 1.1 mrg type = cp_build_reference_type (type, /*rval=*/true); 484 1.1 mrg warning_sentinel w (warn_useless_cast); 485 1.1 mrg exp = build_static_cast (input_location, type, exp, 486 1.1 mrg tf_warning_or_error); 487 1.1 mrg if (DECL_PACK_P (parm)) 488 1.1 mrg exp = make_pack_expansion (exp); 489 1.1 mrg return exp; 490 1.1 mrg } 491 1.1 mrg 492 1.1 mrg /* Strip all inheriting constructors, if any, to return the original 493 1.1 mrg constructor from a (possibly indirect) base class. */ 494 1.1 mrg 495 1.1 mrg tree 496 1.1 mrg strip_inheriting_ctors (tree dfn) 497 1.1 mrg { 498 1.1 mrg if (!flag_new_inheriting_ctors) 499 1.1 mrg return dfn; 500 1.1 mrg tree fn = dfn; 501 1.1 mrg while (tree inh = DECL_INHERITED_CTOR (fn)) 502 1.1 mrg fn = OVL_FIRST (inh); 503 1.1 mrg 504 1.1 mrg if (TREE_CODE (fn) == TEMPLATE_DECL 505 1.1 mrg && TREE_CODE (dfn) == FUNCTION_DECL) 506 1.1 mrg fn = DECL_TEMPLATE_RESULT (fn); 507 1.1 mrg return fn; 508 1.1 mrg } 509 1.1 mrg 510 1.1 mrg /* Find the binfo for the base subobject of BINFO being initialized by 511 1.1 mrg inherited constructor FNDECL (a member of a direct base of BINFO). */ 512 1.1 mrg 513 1.1 mrg static tree inherited_ctor_binfo (tree, tree); 514 1.1 mrg static tree 515 1.1 mrg inherited_ctor_binfo_1 (tree binfo, tree fndecl) 516 1.1 mrg { 517 1.1 mrg tree base = DECL_CONTEXT (fndecl); 518 1.1 mrg tree base_binfo; 519 1.1 mrg for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 520 1.1 mrg if (BINFO_TYPE (base_binfo) == base) 521 1.1 mrg return inherited_ctor_binfo (base_binfo, fndecl); 522 1.1 mrg 523 1.1 mrg gcc_unreachable(); 524 1.1 mrg } 525 1.1 mrg 526 1.1 mrg /* Find the binfo for the base subobject of BINFO being initialized by 527 1.1 mrg inheriting constructor FNDECL (a member of BINFO), or BINFO if FNDECL is not 528 1.1 mrg an inheriting constructor. */ 529 1.1 mrg 530 1.1 mrg static tree 531 1.1 mrg inherited_ctor_binfo (tree binfo, tree fndecl) 532 1.1 mrg { 533 1.1 mrg tree inh = DECL_INHERITED_CTOR (fndecl); 534 1.1 mrg if (!inh) 535 1.1 mrg return binfo; 536 1.1 mrg 537 1.1 mrg tree results = NULL_TREE; 538 1.1 mrg for (ovl_iterator iter (inh); iter; ++iter) 539 1.1 mrg { 540 1.1 mrg tree one = inherited_ctor_binfo_1 (binfo, *iter); 541 1.1 mrg if (!results) 542 1.1 mrg results = one; 543 1.1 mrg else if (one != results) 544 1.1 mrg results = tree_cons (NULL_TREE, one, results); 545 1.1 mrg } 546 1.1 mrg return results; 547 1.1 mrg } 548 1.1 mrg 549 1.1 mrg /* Find the binfo for the base subobject being initialized by inheriting 550 1.1 mrg constructor FNDECL, or NULL_TREE if FNDECL is not an inheriting 551 1.1 mrg constructor. */ 552 1.1 mrg 553 1.1 mrg tree 554 1.1 mrg inherited_ctor_binfo (tree fndecl) 555 1.1 mrg { 556 1.1 mrg if (!DECL_INHERITED_CTOR (fndecl)) 557 1.1 mrg return NULL_TREE; 558 1.1 mrg tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl)); 559 1.1 mrg return inherited_ctor_binfo (binfo, fndecl); 560 1.1 mrg } 561 1.1 mrg 562 1.1 mrg 563 1.1 mrg /* True if we should omit all user-declared parameters from a base 564 1.1 mrg construtor built from complete constructor FN. 565 1.1 mrg That's when the ctor is inherited from a virtual base. */ 566 1.1 mrg 567 1.1 mrg bool 568 1.1 mrg base_ctor_omit_inherited_parms (tree comp_ctor) 569 1.1 mrg { 570 1.1 mrg gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (comp_ctor)); 571 1.1 mrg 572 1.1 mrg if (!flag_new_inheriting_ctors) 573 1.1 mrg /* We only optimize away the parameters in the new model. */ 574 1.1 mrg return false; 575 1.1 mrg 576 1.1 mrg if (!CLASSTYPE_VBASECLASSES (DECL_CONTEXT (comp_ctor))) 577 1.1 mrg return false; 578 1.1 mrg 579 1.1 mrg if (FUNCTION_FIRST_USER_PARMTYPE (comp_ctor) == void_list_node) 580 1.1 mrg /* No user-declared parameters to omit. */ 581 1.1 mrg return false; 582 1.1 mrg 583 1.1 mrg for (tree binfo = inherited_ctor_binfo (comp_ctor); 584 1.1 mrg binfo; 585 1.1 mrg binfo = BINFO_INHERITANCE_CHAIN (binfo)) 586 1.1 mrg if (BINFO_VIRTUAL_P (binfo)) 587 1.1 mrg return true; 588 1.1 mrg 589 1.1 mrg return false; 590 1.1 mrg } 591 1.1 mrg 592 1.1 mrg 593 1.1 mrg /* True if we should omit all user-declared parameters from constructor FN, 594 1.1 mrg because it is a base clone of a ctor inherited from a virtual base. */ 595 1.1 mrg 596 1.1 mrg bool 597 1.1 mrg ctor_omit_inherited_parms (tree fn) 598 1.1 mrg { 599 1.1 mrg gcc_checking_assert (TREE_CODE (fn) == FUNCTION_DECL); 600 1.1 mrg 601 1.1 mrg if (!DECL_BASE_CONSTRUCTOR_P (fn)) 602 1.1 mrg return false; 603 1.1 mrg 604 1.1 mrg return base_ctor_omit_inherited_parms (DECL_CLONED_FUNCTION (fn)); 605 1.1 mrg } 606 1.1 mrg 607 1.1 mrg /* True iff constructor(s) INH inherited into BINFO initializes INIT_BINFO. 608 1.1 mrg This can be true for multiple virtual bases as well as one direct 609 1.1 mrg non-virtual base. */ 610 1.1 mrg 611 1.1 mrg static bool 612 1.1 mrg binfo_inherited_from (tree binfo, tree init_binfo, tree inh) 613 1.1 mrg { 614 1.1 mrg /* inh is an OVERLOAD if we inherited the same constructor along 615 1.1 mrg multiple paths, check all of them. */ 616 1.1 mrg for (ovl_iterator iter (inh); iter; ++iter) 617 1.1 mrg { 618 1.1 mrg tree fn = *iter; 619 1.1 mrg tree base = DECL_CONTEXT (fn); 620 1.1 mrg tree base_binfo = NULL_TREE; 621 1.1 mrg for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 622 1.1 mrg if (BINFO_TYPE (base_binfo) == base) 623 1.1 mrg break; 624 1.1 mrg if (base_binfo == init_binfo 625 1.1 mrg || (flag_new_inheriting_ctors 626 1.1 mrg && binfo_inherited_from (base_binfo, init_binfo, 627 1.1 mrg DECL_INHERITED_CTOR (fn)))) 628 1.1 mrg return true; 629 1.1 mrg } 630 1.1 mrg return false; 631 1.1 mrg } 632 1.1 mrg 633 1.1 mrg /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO 634 1.1 mrg given the parameter or parameters PARM, possibly inherited constructor 635 1.1 mrg base INH, or move flag MOVE_P. */ 636 1.1 mrg 637 1.1 mrg static tree 638 1.1 mrg add_one_base_init (tree binfo, tree parm, bool move_p, tree inh, 639 1.1 mrg tree member_init_list) 640 1.1 mrg { 641 1.1 mrg tree init; 642 1.1 mrg if (inh) 643 1.1 mrg { 644 1.1 mrg /* An inheriting constructor only has a mem-initializer for 645 1.1 mrg the base it inherits from. */ 646 1.1 mrg if (!binfo_inherited_from (TYPE_BINFO (current_class_type), binfo, inh)) 647 1.1 mrg return member_init_list; 648 1.1 mrg 649 1.1 mrg tree *p = &init; 650 1.1 mrg init = NULL_TREE; 651 1.1 mrg for (; parm; parm = DECL_CHAIN (parm)) 652 1.1 mrg { 653 1.1 mrg tree exp = forward_parm (parm); 654 1.1 mrg *p = build_tree_list (NULL_TREE, exp); 655 1.1 mrg p = &TREE_CHAIN (*p); 656 1.1 mrg } 657 1.1 mrg } 658 1.1 mrg else 659 1.1 mrg { 660 1.1 mrg init = build_base_path (PLUS_EXPR, parm, binfo, 1, 661 1.1 mrg tf_warning_or_error); 662 1.1 mrg if (move_p) 663 1.1 mrg init = move (init); 664 1.1 mrg init = build_tree_list (NULL_TREE, init); 665 1.1 mrg } 666 1.1 mrg return tree_cons (binfo, init, member_init_list); 667 1.1 mrg } 668 1.1 mrg 669 1.1 mrg /* Generate code for default X(X&) or X(X&&) constructor or an inheriting 670 1.1 mrg constructor. */ 671 1.1 mrg 672 1.1 mrg static void 673 1.1 mrg do_build_copy_constructor (tree fndecl) 674 1.1 mrg { 675 1.1 mrg tree parm = FUNCTION_FIRST_USER_PARM (fndecl); 676 1.1 mrg bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl); 677 1.1 mrg bool trivial = trivial_fn_p (fndecl); 678 1.1 mrg tree inh = DECL_INHERITED_CTOR (fndecl); 679 1.1 mrg 680 1.1 mrg if (!inh) 681 1.1 mrg parm = convert_from_reference (parm); 682 1.1 mrg 683 1.1 mrg if (trivial) 684 1.1 mrg { 685 1.1 mrg if (is_empty_class (current_class_type)) 686 1.1 mrg /* Don't copy the padding byte; it might not have been allocated 687 1.1 mrg if *this is a base subobject. */; 688 1.1 mrg else if (tree_int_cst_equal (TYPE_SIZE (current_class_type), 689 1.1 mrg CLASSTYPE_SIZE (current_class_type))) 690 1.1 mrg { 691 1.1 mrg tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm); 692 1.1 mrg finish_expr_stmt (t); 693 1.1 mrg } 694 1.1 mrg else 695 1.1 mrg { 696 1.1 mrg /* We must only copy the non-tail padding parts. */ 697 1.1 mrg tree base_size = CLASSTYPE_SIZE_UNIT (current_class_type); 698 1.1 mrg base_size = size_binop (MINUS_EXPR, base_size, size_int (1)); 699 1.1 mrg tree array_type = build_array_type (unsigned_char_type_node, 700 1.1 mrg build_index_type (base_size)); 701 1.1 mrg tree alias_set = build_int_cst (TREE_TYPE (current_class_ptr), 0); 702 1.1 mrg tree lhs = build2 (MEM_REF, array_type, 703 1.1 mrg current_class_ptr, alias_set); 704 1.1 mrg tree rhs = build2 (MEM_REF, array_type, 705 1.1 mrg TREE_OPERAND (parm, 0), alias_set); 706 1.1 mrg tree t = build2 (INIT_EXPR, void_type_node, lhs, rhs); 707 1.1 mrg finish_expr_stmt (t); 708 1.1 mrg } 709 1.1 mrg } 710 1.1 mrg else 711 1.1 mrg { 712 1.1 mrg tree member_init_list = NULL_TREE; 713 1.1 mrg int i; 714 1.1 mrg tree binfo, base_binfo; 715 1.1 mrg vec<tree, va_gc> *vbases; 716 1.1 mrg 717 1.1 mrg /* Initialize all the base-classes with the parameter converted 718 1.1 mrg to their type so that we get their copy constructor and not 719 1.1 mrg another constructor that takes current_class_type. We must 720 1.1 mrg deal with the binfo's directly as a direct base might be 721 1.1 mrg inaccessible due to ambiguity. */ 722 1.1 mrg for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0; 723 1.1 mrg vec_safe_iterate (vbases, i, &binfo); i++) 724 1.1 mrg { 725 1.1 mrg member_init_list = add_one_base_init (binfo, parm, move_p, inh, 726 1.1 mrg member_init_list); 727 1.1 mrg } 728 1.1 mrg 729 1.1 mrg for (binfo = TYPE_BINFO (current_class_type), i = 0; 730 1.1 mrg BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 731 1.1 mrg { 732 1.1 mrg if (BINFO_VIRTUAL_P (base_binfo)) 733 1.1 mrg continue; 734 1.1 mrg member_init_list = add_one_base_init (base_binfo, parm, move_p, 735 1.1 mrg inh, member_init_list); 736 1.1 mrg } 737 1.1 mrg 738 1.1 mrg if (!inh) 739 1.1 mrg { 740 1.1 mrg int cvquals = cp_type_quals (TREE_TYPE (parm)); 741 1.1 mrg 742 1.1 mrg for (tree fields = TYPE_FIELDS (current_class_type); 743 1.1 mrg fields; fields = DECL_CHAIN (fields)) 744 1.1 mrg { 745 1.1 mrg tree field = fields; 746 1.1 mrg tree expr_type; 747 1.1 mrg 748 1.1 mrg if (TREE_CODE (field) != FIELD_DECL) 749 1.1 mrg continue; 750 1.1 mrg 751 1.1 mrg expr_type = TREE_TYPE (field); 752 1.1 mrg if (DECL_NAME (field)) 753 1.1 mrg { 754 1.1 mrg if (VFIELD_NAME_P (DECL_NAME (field))) 755 1.1 mrg continue; 756 1.1 mrg } 757 1.1 mrg else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type)) 758 1.1 mrg /* Just use the field; anonymous types can't have 759 1.1 mrg nontrivial copy ctors or assignment ops or this 760 1.1 mrg function would be deleted. */; 761 1.1 mrg else 762 1.1 mrg continue; 763 1.1 mrg 764 1.1 mrg /* Compute the type of "init->field". If the copy-constructor 765 1.1 mrg parameter is, for example, "const S&", and the type of 766 1.1 mrg the field is "T", then the type will usually be "const 767 1.1 mrg T". (There are no cv-qualified variants of reference 768 1.1 mrg types.) */ 769 1.1 mrg if (!TYPE_REF_P (expr_type)) 770 1.1 mrg { 771 1.1 mrg int quals = cvquals; 772 1.1 mrg 773 1.1 mrg if (DECL_MUTABLE_P (field)) 774 1.1 mrg quals &= ~TYPE_QUAL_CONST; 775 1.1 mrg quals |= cp_type_quals (expr_type); 776 1.1 mrg expr_type = cp_build_qualified_type (expr_type, quals); 777 1.1 mrg } 778 1.1 mrg 779 1.1 mrg tree init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE); 780 1.1 mrg if (move_p && !TYPE_REF_P (expr_type) 781 1.1 mrg /* 'move' breaks bit-fields, and has no effect for scalars. */ 782 1.1 mrg && !scalarish_type_p (expr_type)) 783 1.1 mrg init = move (init); 784 1.1 mrg init = build_tree_list (NULL_TREE, init); 785 1.1 mrg 786 1.1 mrg member_init_list = tree_cons (field, init, member_init_list); 787 1.1 mrg } 788 1.1 mrg } 789 1.1 mrg 790 1.1 mrg finish_mem_initializers (member_init_list); 791 1.1 mrg } 792 1.1 mrg } 793 1.1 mrg 794 1.1 mrg static void 795 1.1 mrg do_build_copy_assign (tree fndecl) 796 1.1 mrg { 797 1.1 mrg tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl)); 798 1.1 mrg tree compound_stmt; 799 1.1 mrg bool move_p = move_fn_p (fndecl); 800 1.1 mrg bool trivial = trivial_fn_p (fndecl); 801 1.1 mrg int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED; 802 1.1 mrg 803 1.1 mrg compound_stmt = begin_compound_stmt (0); 804 1.1 mrg parm = convert_from_reference (parm); 805 1.1 mrg 806 1.1 mrg if (trivial 807 1.1 mrg && is_empty_class (current_class_type)) 808 1.1 mrg /* Don't copy the padding byte; it might not have been allocated 809 1.1 mrg if *this is a base subobject. */; 810 1.1 mrg else if (trivial) 811 1.1 mrg { 812 1.1 mrg tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm); 813 1.1 mrg finish_expr_stmt (t); 814 1.1 mrg } 815 1.1 mrg else 816 1.1 mrg { 817 1.1 mrg tree fields; 818 1.1 mrg int cvquals = cp_type_quals (TREE_TYPE (parm)); 819 1.1 mrg int i; 820 1.1 mrg tree binfo, base_binfo; 821 1.1 mrg 822 1.1 mrg /* Assign to each of the direct base classes. */ 823 1.1 mrg for (binfo = TYPE_BINFO (current_class_type), i = 0; 824 1.1 mrg BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 825 1.1 mrg { 826 1.1 mrg tree converted_parm; 827 1.1 mrg 828 1.1 mrg /* We must convert PARM directly to the base class 829 1.1 mrg explicitly since the base class may be ambiguous. */ 830 1.1 mrg converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1, 831 1.1 mrg tf_warning_or_error); 832 1.1 mrg if (move_p) 833 1.1 mrg converted_parm = move (converted_parm); 834 1.1 mrg /* Call the base class assignment operator. */ 835 1.1 mrg releasing_vec parmvec (make_tree_vector_single (converted_parm)); 836 1.1 mrg finish_expr_stmt 837 1.1 mrg (build_special_member_call (current_class_ref, 838 1.1 mrg assign_op_identifier, 839 1.1 mrg &parmvec, 840 1.1 mrg base_binfo, 841 1.1 mrg flags, 842 1.1 mrg tf_warning_or_error)); 843 1.1 mrg } 844 1.1 mrg 845 1.1 mrg /* Assign to each of the non-static data members. */ 846 1.1 mrg for (fields = TYPE_FIELDS (current_class_type); 847 1.1 mrg fields; 848 1.1 mrg fields = DECL_CHAIN (fields)) 849 1.1 mrg { 850 1.1 mrg tree comp = current_class_ref; 851 1.1 mrg tree init = parm; 852 1.1 mrg tree field = fields; 853 1.1 mrg tree expr_type; 854 1.1 mrg int quals; 855 1.1 mrg 856 1.1 mrg if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field)) 857 1.1 mrg continue; 858 1.1 mrg 859 1.1 mrg expr_type = TREE_TYPE (field); 860 1.1 mrg 861 1.1 mrg if (CP_TYPE_CONST_P (expr_type)) 862 1.1 mrg { 863 1.1 mrg error ("non-static const member %q#D, cannot use default " 864 1.1 mrg "assignment operator", field); 865 1.1 mrg continue; 866 1.1 mrg } 867 1.1 mrg else if (TYPE_REF_P (expr_type)) 868 1.1 mrg { 869 1.1 mrg error ("non-static reference member %q#D, cannot use " 870 1.1 mrg "default assignment operator", field); 871 1.1 mrg continue; 872 1.1 mrg } 873 1.1 mrg 874 1.1 mrg if (DECL_NAME (field)) 875 1.1 mrg { 876 1.1 mrg if (VFIELD_NAME_P (DECL_NAME (field))) 877 1.1 mrg continue; 878 1.1 mrg } 879 1.1 mrg else if (ANON_AGGR_TYPE_P (expr_type) 880 1.1 mrg && TYPE_FIELDS (expr_type) != NULL_TREE) 881 1.1 mrg /* Just use the field; anonymous types can't have 882 1.1 mrg nontrivial copy ctors or assignment ops or this 883 1.1 mrg function would be deleted. */; 884 1.1 mrg else 885 1.1 mrg continue; 886 1.1 mrg 887 1.1 mrg comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE); 888 1.1 mrg 889 1.1 mrg /* Compute the type of init->field */ 890 1.1 mrg quals = cvquals; 891 1.1 mrg if (DECL_MUTABLE_P (field)) 892 1.1 mrg quals &= ~TYPE_QUAL_CONST; 893 1.1 mrg expr_type = cp_build_qualified_type (expr_type, quals); 894 1.1 mrg 895 1.1 mrg init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE); 896 1.1 mrg if (move_p && !TYPE_REF_P (expr_type) 897 1.1 mrg /* 'move' breaks bit-fields, and has no effect for scalars. */ 898 1.1 mrg && !scalarish_type_p (expr_type)) 899 1.1 mrg init = move (init); 900 1.1 mrg 901 1.1 mrg if (DECL_NAME (field)) 902 1.1 mrg init = cp_build_modify_expr (input_location, comp, NOP_EXPR, init, 903 1.1 mrg tf_warning_or_error); 904 1.1 mrg else 905 1.1 mrg init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init); 906 1.1 mrg finish_expr_stmt (init); 907 1.1 mrg } 908 1.1 mrg } 909 1.1 mrg finish_return_stmt (current_class_ref); 910 1.1 mrg finish_compound_stmt (compound_stmt); 911 1.1 mrg } 912 1.1 mrg 913 1.1 mrg /* C++20 <compare> comparison category types. */ 914 1.1 mrg 915 1.1 mrg enum comp_cat_tag 916 1.1 mrg { 917 1.1 mrg cc_partial_ordering, 918 1.1 mrg cc_weak_ordering, 919 1.1 mrg cc_strong_ordering, 920 1.1 mrg cc_last 921 1.1 mrg }; 922 1.1 mrg 923 1.1 mrg /* Names of the comparison categories and their value members, to be indexed by 924 1.1 mrg comp_cat_tag enumerators. genericize_spaceship below relies on the ordering 925 1.1 mrg of the members. */ 926 1.1 mrg 927 1.1 mrg struct comp_cat_info_t 928 1.1 mrg { 929 1.1 mrg const char *name; 930 1.1 mrg const char *members[4]; 931 1.1 mrg }; 932 1.1 mrg static const comp_cat_info_t comp_cat_info[cc_last] 933 1.1 mrg = { 934 1.1 mrg { "partial_ordering", { "equivalent", "greater", "less", "unordered" } }, 935 1.1 mrg { "weak_ordering", { "equivalent", "greater", "less" } }, 936 1.1 mrg { "strong_ordering", { "equal", "greater", "less" } } 937 1.1 mrg }; 938 1.1 mrg 939 1.1 mrg /* A cache of the category types to speed repeated lookups. */ 940 1.1 mrg 941 1.1 mrg static GTY((deletable)) tree comp_cat_cache[cc_last]; 942 1.1 mrg 943 1.1 mrg /* Look up one of the result variables in the comparison category type. */ 944 1.1 mrg 945 1.1 mrg static tree 946 1.1 mrg lookup_comparison_result (tree type, const char *name_str, 947 1.1 mrg tsubst_flags_t complain = tf_warning_or_error) 948 1.1 mrg { 949 1.1 mrg tree name = get_identifier (name_str); 950 1.1 mrg tree decl = lookup_qualified_name (type, name); 951 1.1 mrg if (TREE_CODE (decl) != VAR_DECL) 952 1.1 mrg { 953 1.1 mrg if (complain & tf_error) 954 1.1 mrg { 955 1.1 mrg auto_diagnostic_group d; 956 1.1 mrg if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST) 957 1.1 mrg qualified_name_lookup_error (type, name, decl, input_location); 958 1.1 mrg else 959 1.1 mrg error ("%qD is not a static data member", decl); 960 1.1 mrg inform (input_location, "determining value of %qs", "operator<=>"); 961 1.1 mrg } 962 1.1 mrg return error_mark_node; 963 1.1 mrg } 964 1.1 mrg return decl; 965 1.1 mrg } 966 1.1 mrg 967 1.1 mrg /* Look up a <compare> comparison category type in std. */ 968 1.1 mrg 969 1.1 mrg static tree 970 1.1 mrg lookup_comparison_category (comp_cat_tag tag, 971 1.1 mrg tsubst_flags_t complain = tf_warning_or_error) 972 1.1 mrg { 973 1.1 mrg if (tree cached = comp_cat_cache[tag]) 974 1.1 mrg return cached; 975 1.1 mrg 976 1.1 mrg tree name = get_identifier (comp_cat_info[tag].name); 977 1.1 mrg tree decl = lookup_qualified_name (std_node, name); 978 1.1 mrg if (TREE_CODE (decl) != TYPE_DECL) 979 1.1 mrg { 980 1.1 mrg if (complain & tf_error) 981 1.1 mrg { 982 1.1 mrg auto_diagnostic_group d; 983 1.1 mrg if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST) 984 1.1 mrg qualified_name_lookup_error (std_node, name, decl, input_location); 985 1.1 mrg else 986 1.1 mrg error ("%qD is not a type", decl); 987 1.1 mrg inform (input_location, "forming type of %qs", "operator<=>"); 988 1.1 mrg } 989 1.1 mrg return error_mark_node; 990 1.1 mrg } 991 1.1 mrg /* Also make sure we can look up the value members now, since we won't 992 1.1 mrg really use them until genericize time. */ 993 1.1 mrg tree type = TREE_TYPE (decl); 994 1.1 mrg for (int i = 0; i < 4; ++i) 995 1.1 mrg { 996 1.1 mrg const char *p = comp_cat_info[tag].members[i]; 997 1.1 mrg if (!p) break; 998 1.1 mrg if (lookup_comparison_result (type, p, complain) 999 1.1 mrg == error_mark_node) 1000 1.1 mrg return error_mark_node; 1001 1.1 mrg } 1002 1.1 mrg return comp_cat_cache[tag] = type; 1003 1.1 mrg } 1004 1.1 mrg 1005 1.1 mrg /* Wrapper that takes the tag rather than the type. */ 1006 1.1 mrg 1007 1.1 mrg static tree 1008 1.1 mrg lookup_comparison_result (comp_cat_tag tag, const char *name_str, 1009 1.1 mrg tsubst_flags_t complain = tf_warning_or_error) 1010 1.1 mrg { 1011 1.1 mrg tree type = lookup_comparison_category (tag, complain); 1012 1.1 mrg return lookup_comparison_result (type, name_str, complain); 1013 1.1 mrg } 1014 1.1 mrg 1015 1.1 mrg /* Wrapper that takes the index into the members array instead of the name. */ 1016 1.1 mrg 1017 1.1 mrg static tree 1018 1.1 mrg lookup_comparison_result (comp_cat_tag tag, tree type, int idx) 1019 1.1 mrg { 1020 1.1 mrg const char *name_str = comp_cat_info[tag].members[idx]; 1021 1.1 mrg if (!name_str) 1022 1.1 mrg return NULL_TREE; 1023 1.1 mrg return lookup_comparison_result (type, name_str); 1024 1.1 mrg } 1025 1.1 mrg 1026 1.1 mrg /* Does TYPE correspond to TAG? */ 1027 1.1 mrg 1028 1.1 mrg static bool 1029 1.1 mrg is_cat (tree type, comp_cat_tag tag) 1030 1.1 mrg { 1031 1.1 mrg tree name = TYPE_LINKAGE_IDENTIFIER (type); 1032 1.1 mrg return id_equal (name, comp_cat_info[tag].name); 1033 1.1 mrg } 1034 1.1 mrg 1035 1.1 mrg /* Return the comp_cat_tag for TYPE. */ 1036 1.1 mrg 1037 1.1 mrg static comp_cat_tag 1038 1.1 mrg cat_tag_for (tree type) 1039 1.1 mrg { 1040 1.1 mrg if (!CLASS_TYPE_P (type) || !decl_in_std_namespace_p (TYPE_MAIN_DECL (type))) 1041 1.1 mrg return cc_last; 1042 1.1 mrg for (int i = 0; i < cc_last; ++i) 1043 1.1 mrg { 1044 1.1 mrg comp_cat_tag tag = (comp_cat_tag)i; 1045 1.1 mrg if (is_cat (type, tag)) 1046 1.1 mrg return tag; 1047 1.1 mrg } 1048 1.1 mrg return cc_last; 1049 1.1 mrg } 1050 1.1 mrg 1051 1.1 mrg /* Return the comparison category tag of a <=> expression with non-class type 1052 1.1 mrg OPTYPE. */ 1053 1.1 mrg 1054 1.1 mrg static comp_cat_tag 1055 1.1 mrg spaceship_comp_cat (tree optype) 1056 1.1 mrg { 1057 1.1 mrg if (INTEGRAL_OR_ENUMERATION_TYPE_P (optype) || TYPE_PTROBV_P (optype)) 1058 1.1 mrg return cc_strong_ordering; 1059 1.1 mrg else if (TREE_CODE (optype) == REAL_TYPE) 1060 1.1 mrg return cc_partial_ordering; 1061 1.1 mrg 1062 1.1 mrg /* ??? should vector <=> produce a vector of one of the above? */ 1063 1.1 mrg gcc_unreachable (); 1064 1.1 mrg } 1065 1.1 mrg 1066 1.1 mrg /* Return the comparison category type of a <=> expression with non-class type 1067 1.1 mrg OPTYPE. */ 1068 1.1 mrg 1069 1.1 mrg tree 1070 1.1 mrg spaceship_type (tree optype, tsubst_flags_t complain) 1071 1.1 mrg { 1072 1.1 mrg comp_cat_tag tag = spaceship_comp_cat (optype); 1073 1.1 mrg return lookup_comparison_category (tag, complain); 1074 1.1 mrg } 1075 1.1 mrg 1076 1.1 mrg /* Turn <=> with type TYPE and operands OP0 and OP1 into GENERIC. 1077 1.1 mrg This is also used by build_comparison_op for fallback to op< and op== 1078 1.1 mrg in a defaulted op<=>. */ 1079 1.1 mrg 1080 1.1 mrg tree 1081 1.1 mrg genericize_spaceship (location_t loc, tree type, tree op0, tree op1) 1082 1.1 mrg { 1083 1.1 mrg /* ??? maybe optimize based on knowledge of representation? */ 1084 1.1 mrg comp_cat_tag tag = cat_tag_for (type); 1085 1.1 mrg 1086 1.1 mrg if (tag == cc_last && is_auto (type)) 1087 1.1 mrg { 1088 1.1 mrg /* build_comparison_op is checking to see if we want to suggest changing 1089 1.1 mrg the op<=> return type from auto to a specific comparison category; any 1090 1.1 mrg category will do for now. */ 1091 1.1 mrg tag = cc_strong_ordering; 1092 1.1 mrg type = lookup_comparison_category (tag, tf_none); 1093 1.1 mrg if (type == error_mark_node) 1094 1.1 mrg return error_mark_node; 1095 1.1 mrg } 1096 1.1 mrg 1097 1.1 mrg gcc_checking_assert (tag < cc_last); 1098 1.1 mrg 1099 1.1 mrg tree r; 1100 1.1 mrg bool scalar = SCALAR_TYPE_P (TREE_TYPE (op0)); 1101 1.1 mrg if (scalar) 1102 1.1 mrg { 1103 1.1 mrg op0 = save_expr (op0); 1104 1.1 mrg op1 = save_expr (op1); 1105 1.1 mrg } 1106 1.1 mrg 1107 1.1 mrg tree gt = lookup_comparison_result (tag, type, 1); 1108 1.1 mrg 1109 1.1 mrg int flags = LOOKUP_NORMAL; 1110 1.1 mrg tsubst_flags_t complain = tf_none; 1111 1.1 mrg tree comp; 1112 1.1 mrg 1113 1.1 mrg if (tag == cc_partial_ordering) 1114 1.1 mrg { 1115 1.1 mrg /* op0 == op1 ? equivalent : op0 < op1 ? less : 1116 1.1 mrg op1 < op0 ? greater : unordered */ 1117 1.1 mrg tree uo = lookup_comparison_result (tag, type, 3); 1118 1.1 mrg if (scalar) 1119 1.1 mrg { 1120 1.1 mrg /* For scalars use the low level operations; using build_new_op causes 1121 1.1 mrg trouble with constexpr eval in the middle of genericize (100367). */ 1122 1.1 mrg comp = fold_build2 (LT_EXPR, boolean_type_node, op1, op0); 1123 1.1 mrg r = fold_build3 (COND_EXPR, type, comp, gt, uo); 1124 1.1 mrg } 1125 1.1 mrg else 1126 1.1 mrg { 1127 1.1 mrg comp = build_new_op (loc, LT_EXPR, flags, op1, op0, complain); 1128 1.1 mrg r = build_conditional_expr (loc, comp, gt, uo, complain); 1129 1.1 mrg } 1130 1.1 mrg } 1131 1.1 mrg else 1132 1.1 mrg /* op0 == op1 ? equal : op0 < op1 ? less : greater */ 1133 1.1 mrg r = gt; 1134 1.1 mrg 1135 1.1 mrg tree lt = lookup_comparison_result (tag, type, 2); 1136 1.1 mrg if (scalar) 1137 1.1 mrg { 1138 1.1 mrg comp = fold_build2 (LT_EXPR, boolean_type_node, op0, op1); 1139 1.1 mrg r = fold_build3 (COND_EXPR, type, comp, lt, r); 1140 1.1 mrg } 1141 1.1 mrg else 1142 1.1 mrg { 1143 1.1 mrg comp = build_new_op (loc, LT_EXPR, flags, op0, op1, complain); 1144 1.1 mrg r = build_conditional_expr (loc, comp, lt, r, complain); 1145 1.1 mrg } 1146 1.1 mrg 1147 1.1 mrg tree eq = lookup_comparison_result (tag, type, 0); 1148 1.1 mrg if (scalar) 1149 1.1 mrg { 1150 1.1 mrg comp = fold_build2 (EQ_EXPR, boolean_type_node, op0, op1); 1151 1.1 mrg r = fold_build3 (COND_EXPR, type, comp, eq, r); 1152 1.1 mrg } 1153 1.1 mrg else 1154 1.1 mrg { 1155 1.1 mrg comp = build_new_op (loc, EQ_EXPR, flags, op0, op1, complain); 1156 1.1 mrg r = build_conditional_expr (loc, comp, eq, r, complain); 1157 1.1 mrg } 1158 1.1 mrg 1159 1.1 mrg return r; 1160 1.1 mrg } 1161 1.1 mrg 1162 1.1 mrg /* Check that the signature of a defaulted comparison operator is 1163 1.1 mrg well-formed. */ 1164 1.1 mrg 1165 1.1 mrg static bool 1166 1.1 mrg early_check_defaulted_comparison (tree fn) 1167 1.1 mrg { 1168 1.1 mrg location_t loc = DECL_SOURCE_LOCATION (fn); 1169 1.1 mrg tree ctx; 1170 1.1 mrg if (DECL_CLASS_SCOPE_P (fn)) 1171 1.1 mrg ctx = DECL_CONTEXT (fn); 1172 1.1 mrg else 1173 1.1 mrg ctx = DECL_FRIEND_CONTEXT (fn); 1174 1.1 mrg bool ok = true; 1175 1.1 mrg 1176 1.1 mrg if (cxx_dialect < cxx20) 1177 1.1 mrg { 1178 1.1 mrg error_at (loc, "defaulted %qD only available with %<-std=c++20%> or " 1179 1.1 mrg "%<-std=gnu++20%>", fn); 1180 1.1 mrg return false; 1181 1.1 mrg } 1182 1.1 mrg 1183 1.1 mrg if (!DECL_OVERLOADED_OPERATOR_IS (fn, SPACESHIP_EXPR) 1184 1.1 mrg && !same_type_p (TREE_TYPE (TREE_TYPE (fn)), boolean_type_node)) 1185 1.1 mrg { 1186 1.1 mrg diagnostic_t kind = DK_UNSPECIFIED; 1187 1.1 mrg int opt = 0; 1188 1.1 mrg if (is_auto (TREE_TYPE (fn))) 1189 1.1 mrg kind = DK_PEDWARN; 1190 1.1 mrg else 1191 1.1 mrg kind = DK_ERROR; 1192 1.1 mrg emit_diagnostic (kind, loc, opt, 1193 1.1 mrg "defaulted %qD must return %<bool%>", fn); 1194 1.1 mrg if (kind == DK_ERROR) 1195 1.1 mrg ok = false; 1196 1.1 mrg } 1197 1.1 mrg 1198 1.1 mrg bool mem = DECL_NONSTATIC_MEMBER_FUNCTION_P (fn); 1199 1.1 mrg if (mem && type_memfn_quals (TREE_TYPE (fn)) != TYPE_QUAL_CONST) 1200 1.1 mrg { 1201 1.1 mrg error_at (loc, "defaulted %qD must be %<const%>", fn); 1202 1.1 mrg ok = false; 1203 1.1 mrg } 1204 1.1 mrg if (mem && type_memfn_rqual (TREE_TYPE (fn)) == REF_QUAL_RVALUE) 1205 1.1 mrg { 1206 1.1 mrg error_at (loc, "defaulted %qD must not have %<&&%> ref-qualifier", fn); 1207 1.1 mrg ok = false; 1208 1.1 mrg } 1209 1.1 mrg tree parmnode = FUNCTION_FIRST_USER_PARMTYPE (fn); 1210 1.1 mrg bool saw_byval = false; 1211 1.1 mrg bool saw_byref = mem; 1212 1.1 mrg bool saw_bad = false; 1213 1.1 mrg for (; parmnode != void_list_node; parmnode = TREE_CHAIN (parmnode)) 1214 1.1 mrg { 1215 1.1 mrg tree parmtype = TREE_VALUE (parmnode); 1216 1.1 mrg if (CLASS_TYPE_P (parmtype)) 1217 1.1 mrg saw_byval = true; 1218 1.1 mrg else if (TREE_CODE (parmtype) == REFERENCE_TYPE 1219 1.1 mrg && !TYPE_REF_IS_RVALUE (parmtype) 1220 1.1 mrg && TYPE_QUALS (TREE_TYPE (parmtype)) == TYPE_QUAL_CONST) 1221 1.1 mrg { 1222 1.1 mrg saw_byref = true; 1223 1.1 mrg parmtype = TREE_TYPE (parmtype); 1224 1.1 mrg } 1225 1.1 mrg else 1226 1.1 mrg saw_bad = true; 1227 1.1 mrg 1228 1.1 mrg if (!saw_bad && !ctx) 1229 1.1 mrg { 1230 1.1 mrg /* Defaulted outside the class body. */ 1231 1.1 mrg ctx = TYPE_MAIN_VARIANT (parmtype); 1232 1.1 mrg if (!is_friend (ctx, fn)) 1233 1.1 mrg { 1234 1.1 mrg error_at (loc, "defaulted %qD is not a friend of %qT", fn, ctx); 1235 1.1 mrg inform (location_of (ctx), "declared here"); 1236 1.1 mrg ok = false; 1237 1.1 mrg } 1238 1.1 mrg } 1239 1.1 mrg else if (!same_type_ignoring_top_level_qualifiers_p (parmtype, ctx)) 1240 1.1 mrg saw_bad = true; 1241 1.1 mrg } 1242 1.1 mrg 1243 1.1 mrg if (saw_bad || (saw_byval && saw_byref)) 1244 1.1 mrg { 1245 1.1 mrg if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)) 1246 1.1 mrg error_at (loc, "defaulted member %qD must have parameter type " 1247 1.1 mrg "%<const %T&%>", fn, ctx); 1248 1.1 mrg else if (saw_bad) 1249 1.1 mrg error_at (loc, "defaulted %qD must have parameters of either type " 1250 1.1 mrg "%<const %T&%> or %qT", fn, ctx, ctx); 1251 1.1 mrg else 1252 1.1 mrg error_at (loc, "defaulted %qD must have parameters of either type " 1253 1.1 mrg "%<const %T&%> or %qT, not both", fn, ctx, ctx); 1254 1.1 mrg ok = false; 1255 1.1 mrg } 1256 1.1 mrg 1257 1.1 mrg /* We still need to deduce deleted/constexpr/noexcept and maybe return. */ 1258 1.1 mrg DECL_MAYBE_DELETED (fn) = ok; 1259 1.1 mrg 1260 1.1 mrg return ok; 1261 1.1 mrg } 1262 1.1 mrg 1263 1.1 mrg /* Subroutine of build_comparison_op. Given the vec of memberwise 1264 1.1 mrg comparisons COMPS, calculate the overall comparison category for 1265 1.1 mrg operator<=>. */ 1266 1.1 mrg 1267 1.1 mrg static tree 1268 1.1 mrg common_comparison_type (vec<tree> &comps) 1269 1.1 mrg { 1270 1.1 mrg tree seen[cc_last] = {}; 1271 1.1 mrg 1272 1.1 mrg for (unsigned i = 0; i < comps.length(); ++i) 1273 1.1 mrg { 1274 1.1 mrg tree comp = comps[i]; 1275 1.1 mrg if (TREE_CODE (comp) == TREE_LIST) 1276 1.1 mrg comp = TREE_VALUE (comp); 1277 1.1 mrg tree ctype = TREE_TYPE (comp); 1278 1.1 mrg comp_cat_tag tag = cat_tag_for (ctype); 1279 1.1 mrg /* build_comparison_op already checked this. */ 1280 1.1 mrg gcc_checking_assert (tag < cc_last); 1281 1.1 mrg seen[tag] = ctype; 1282 1.1 mrg } 1283 1.1 mrg 1284 1.1 mrg /* Otherwise, if at least one T i is std::partial_ordering, U is 1285 1.1 mrg std::partial_ordering. */ 1286 1.1 mrg if (tree t = seen[cc_partial_ordering]) return t; 1287 1.1 mrg 1288 1.1 mrg /* Otherwise, if at least one T i is std::weak_ordering, U is 1289 1.1 mrg std::weak_ordering. */ 1290 1.1 mrg if (tree t = seen[cc_weak_ordering]) return t; 1291 1.1 mrg 1292 1.1 mrg /* Otherwise, U is std::strong_ordering. */ 1293 1.1 mrg if (tree t = seen[cc_strong_ordering]) return t; 1294 1.1 mrg return lookup_comparison_category (cc_strong_ordering); 1295 1.1 mrg } 1296 1.1 mrg 1297 1.1 mrg /* Data structure for build_comparison_op. */ 1298 1.1 mrg 1299 1.1 mrg struct comp_info 1300 1.1 mrg { 1301 1.1 mrg tree fndecl; 1302 1.1 mrg location_t loc; 1303 1.1 mrg tsubst_flags_t complain; 1304 1.1 mrg tree_code code; 1305 1.1 mrg comp_cat_tag retcat; 1306 1.1 mrg bool first_time; 1307 1.1 mrg bool constexp; 1308 1.1 mrg bool was_constexp; 1309 1.1 mrg bool noex; 1310 1.1 mrg 1311 1.1 mrg comp_info (tree fndecl, tsubst_flags_t complain) 1312 1.1 mrg : fndecl (fndecl), complain (complain) 1313 1.1 mrg { 1314 1.1 mrg loc = DECL_SOURCE_LOCATION (fndecl); 1315 1.1 mrg 1316 1.1 mrg first_time = DECL_MAYBE_DELETED (fndecl); 1317 1.1 mrg DECL_MAYBE_DELETED (fndecl) = false; 1318 1.1 mrg 1319 1.1 mrg /* Do we want to try to set constexpr? */ 1320 1.1 mrg was_constexp = DECL_DECLARED_CONSTEXPR_P (fndecl); 1321 1.1 mrg constexp = first_time; 1322 1.1 mrg if (constexp) 1323 1.1 mrg /* Set this for var_in_constexpr_fn. */ 1324 1.1 mrg DECL_DECLARED_CONSTEXPR_P (fndecl) = true; 1325 1.1 mrg 1326 1.1 mrg /* Do we want to try to set noexcept? */ 1327 1.1 mrg noex = first_time; 1328 1.1 mrg if (noex) 1329 1.1 mrg { 1330 1.1 mrg tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl)); 1331 1.1 mrg if (raises && !UNEVALUATED_NOEXCEPT_SPEC_P (raises)) 1332 1.1 mrg /* There was an explicit exception-specification. */ 1333 1.1 mrg noex = false; 1334 1.1 mrg } 1335 1.1 mrg } 1336 1.1 mrg 1337 1.1 mrg /* EXPR is an expression built as part of the function body. 1338 1.1 mrg Adjust the properties appropriately. */ 1339 1.1 mrg void check (tree expr) 1340 1.1 mrg { 1341 1.1 mrg if (expr == error_mark_node) 1342 1.1 mrg DECL_DELETED_FN (fndecl) = true; 1343 1.1 mrg if ((constexp || was_constexp) 1344 1.1 mrg && !potential_rvalue_constant_expression (expr)) 1345 1.1 mrg { 1346 1.1 mrg if (was_constexp) 1347 1.1 mrg require_potential_rvalue_constant_expression (expr); 1348 1.1 mrg else 1349 1.1 mrg constexp = false; 1350 1.1 mrg } 1351 1.1 mrg if (noex && !expr_noexcept_p (expr, tf_none)) 1352 1.1 mrg noex = false; 1353 1.1 mrg } 1354 1.1 mrg 1355 1.1 mrg ~comp_info () 1356 1.1 mrg { 1357 1.1 mrg if (first_time) 1358 1.1 mrg { 1359 1.1 mrg DECL_DECLARED_CONSTEXPR_P (fndecl) = constexp || was_constexp; 1360 1.1 mrg tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl)); 1361 1.1 mrg if (!raises || UNEVALUATED_NOEXCEPT_SPEC_P (raises)) 1362 1.1 mrg { 1363 1.1 mrg raises = noex ? noexcept_true_spec : noexcept_false_spec; 1364 1.1 mrg TREE_TYPE (fndecl) = build_exception_variant (TREE_TYPE (fndecl), 1365 1.1 mrg raises); 1366 1.1 mrg } 1367 1.1 mrg } 1368 1.1 mrg } 1369 1.1 mrg }; 1370 1.1 mrg 1371 1.1 mrg /* Subroutine of build_comparison_op, to compare a single subobject. */ 1372 1.1 mrg 1373 1.1 mrg static tree 1374 1.1 mrg do_one_comp (location_t loc, const comp_info &info, tree sub, tree lhs, tree rhs) 1375 1.1 mrg { 1376 1.1 mrg const tree_code code = info.code; 1377 1.1 mrg const tree fndecl = info.fndecl; 1378 1.1 mrg const comp_cat_tag retcat = info.retcat; 1379 1.1 mrg const tsubst_flags_t complain = info.complain; 1380 1.1 mrg 1381 1.1 mrg tree overload = NULL_TREE; 1382 1.1 mrg int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED; 1383 1.1 mrg /* If we have an explicit comparison category return type we can fall back 1384 1.1 mrg to </=, so don't give an error yet if <=> lookup fails. */ 1385 1.1 mrg bool tentative = retcat != cc_last; 1386 1.1 mrg tree comp = build_new_op (loc, code, flags, lhs, rhs, 1387 1.1 mrg NULL_TREE, NULL_TREE, &overload, 1388 1.1 mrg tentative ? tf_none : complain); 1389 1.1 mrg 1390 1.1 mrg if (code != SPACESHIP_EXPR) 1391 1.1 mrg return comp; 1392 1.1 mrg 1393 1.1 mrg tree rettype = TREE_TYPE (TREE_TYPE (fndecl)); 1394 1.1 mrg 1395 1.1 mrg if (comp == error_mark_node) 1396 1.1 mrg { 1397 1.1 mrg if (overload == NULL_TREE && (tentative || complain)) 1398 1.1 mrg { 1399 1.1 mrg /* No viable <=>, try using op< and op==. */ 1400 1.1 mrg tree lteq = genericize_spaceship (loc, rettype, lhs, rhs); 1401 1.1 mrg if (lteq != error_mark_node) 1402 1.1 mrg { 1403 1.1 mrg /* We found usable < and ==. */ 1404 1.1 mrg if (retcat != cc_last) 1405 1.1 mrg /* Return type is a comparison category, use them. */ 1406 1.1 mrg comp = lteq; 1407 1.1 mrg else if (complain & tf_error) 1408 1.1 mrg /* Return type is auto, suggest changing it. */ 1409 1.1 mrg inform (info.loc, "changing the return type from %qs " 1410 1.1 mrg "to a comparison category type will allow the " 1411 1.1 mrg "comparison to use %qs and %qs", "auto", 1412 1.1 mrg "operator<", "operator=="); 1413 1.1 mrg } 1414 1.1 mrg else if (tentative && complain) 1415 1.1 mrg /* No usable < and ==, give an error for op<=>. */ 1416 1.1 mrg build_new_op (loc, code, flags, lhs, rhs, complain); 1417 1.1 mrg } 1418 1.1 mrg if (comp == error_mark_node) 1419 1.1 mrg return error_mark_node; 1420 1.1 mrg } 1421 1.1 mrg 1422 1.1 mrg if (FNDECL_USED_AUTO (fndecl) 1423 1.1 mrg && cat_tag_for (TREE_TYPE (comp)) == cc_last) 1424 1.1 mrg { 1425 1.1 mrg /* The operator function is defined as deleted if ... Ri is not a 1426 1.1 mrg comparison category type. */ 1427 1.1 mrg if (complain & tf_error) 1428 1.1 mrg inform (loc, 1429 1.1 mrg "three-way comparison of %qD has type %qT, not a " 1430 1.1 mrg "comparison category type", sub, TREE_TYPE (comp)); 1431 1.1 mrg return error_mark_node; 1432 1.1 mrg } 1433 1.1 mrg else if (!FNDECL_USED_AUTO (fndecl) 1434 1.1 mrg && !can_convert (rettype, TREE_TYPE (comp), complain)) 1435 1.1 mrg { 1436 1.1 mrg if (complain & tf_error) 1437 1.1 mrg error_at (loc, 1438 1.1 mrg "three-way comparison of %qD has type %qT, which " 1439 1.1 mrg "does not convert to %qT", 1440 1.1 mrg sub, TREE_TYPE (comp), rettype); 1441 1.1 mrg return error_mark_node; 1442 1.1 mrg } 1443 1.1 mrg 1444 1.1 mrg return comp; 1445 1.1 mrg } 1446 1.1 mrg 1447 1.1 mrg /* Build up the definition of a defaulted comparison operator. Unlike other 1448 1.1 mrg defaulted functions that use synthesized_method_walk to determine whether 1449 1.1 mrg the function is e.g. deleted, for comparisons we use the same code. We try 1450 1.1 mrg to use synthesize_method at the earliest opportunity and bail out if the 1451 1.1 mrg function ends up being deleted. */ 1452 1.1 mrg 1453 1.1 mrg void 1454 1.1 mrg build_comparison_op (tree fndecl, bool defining, tsubst_flags_t complain) 1455 1.1 mrg { 1456 1.1 mrg comp_info info (fndecl, complain); 1457 1.1 mrg 1458 1.1 mrg if (!defining && !(complain & tf_error) && !DECL_MAYBE_DELETED (fndecl)) 1459 1.1 mrg return; 1460 1.1 mrg 1461 1.1 mrg int flags = LOOKUP_NORMAL; 1462 1.1 mrg const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (fndecl)); 1463 1.1 mrg tree_code code = info.code = op->tree_code; 1464 1.1 mrg 1465 1.1 mrg tree lhs = DECL_ARGUMENTS (fndecl); 1466 1.1 mrg tree rhs = DECL_CHAIN (lhs); 1467 1.1 mrg if (is_this_parameter (lhs)) 1468 1.1 mrg lhs = cp_build_fold_indirect_ref (lhs); 1469 1.1 mrg else 1470 1.1 mrg lhs = convert_from_reference (lhs); 1471 1.1 mrg rhs = convert_from_reference (rhs); 1472 1.1 mrg tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs)); 1473 1.1 mrg gcc_assert (!defining || COMPLETE_TYPE_P (ctype)); 1474 1.1 mrg 1475 1.1 mrg iloc_sentinel ils (info.loc); 1476 1.1 mrg 1477 1.1 mrg /* A defaulted comparison operator function for class C is defined as 1478 1.1 mrg deleted if ... C has variant members. */ 1479 1.1 mrg if (TREE_CODE (ctype) == UNION_TYPE 1480 1.1 mrg && next_initializable_field (TYPE_FIELDS (ctype))) 1481 1.1 mrg { 1482 1.1 mrg if (complain & tf_error) 1483 1.1 mrg inform (info.loc, "cannot default compare union %qT", ctype); 1484 1.1 mrg DECL_DELETED_FN (fndecl) = true; 1485 1.1 mrg return; 1486 1.1 mrg } 1487 1.1 mrg 1488 1.1 mrg tree compound_stmt = NULL_TREE; 1489 1.1 mrg if (defining) 1490 1.1 mrg compound_stmt = begin_compound_stmt (0); 1491 1.1 mrg else 1492 1.1 mrg ++cp_unevaluated_operand; 1493 1.1 mrg 1494 1.1 mrg tree rettype = TREE_TYPE (TREE_TYPE (fndecl)); 1495 1.1 mrg if (code != SPACESHIP_EXPR && is_auto (rettype)) 1496 1.1 mrg { 1497 1.1 mrg rettype = boolean_type_node; 1498 1.1 mrg apply_deduced_return_type (fndecl, rettype); 1499 1.1 mrg } 1500 1.1 mrg 1501 1.1 mrg if (code == EQ_EXPR || code == SPACESHIP_EXPR) 1502 1.1 mrg { 1503 1.1 mrg comp_cat_tag &retcat = (info.retcat = cc_last); 1504 1.1 mrg if (code == SPACESHIP_EXPR && !FNDECL_USED_AUTO (fndecl)) 1505 1.1 mrg retcat = cat_tag_for (rettype); 1506 1.1 mrg 1507 1.1 mrg bool bad = false; 1508 1.1 mrg auto_vec<tree> comps; 1509 1.1 mrg 1510 1.1 mrg /* Compare the base subobjects. We handle them this way, rather than in 1511 1.1 mrg the field loop below, because maybe_instantiate_noexcept might bring 1512 1.1 mrg us here before we've built the base fields. */ 1513 1.1 mrg for (tree base_binfo : BINFO_BASE_BINFOS (TYPE_BINFO (ctype))) 1514 1.1 mrg { 1515 1.1 mrg tree lhs_base 1516 1.1 mrg = build_base_path (PLUS_EXPR, lhs, base_binfo, 0, complain); 1517 1.1 mrg tree rhs_base 1518 1.1 mrg = build_base_path (PLUS_EXPR, rhs, base_binfo, 0, complain); 1519 1.1 mrg 1520 1.1 mrg location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (ctype)); 1521 1.1 mrg tree comp = do_one_comp (loc, info, BINFO_TYPE (base_binfo), 1522 1.1 mrg lhs_base, rhs_base); 1523 1.1 mrg if (comp == error_mark_node) 1524 1.1 mrg { 1525 1.1 mrg bad = true; 1526 1.1 mrg continue; 1527 1.1 mrg } 1528 1.1 mrg 1529 1.1 mrg comps.safe_push (comp); 1530 1.1 mrg } 1531 1.1 mrg 1532 1.1 mrg /* Now compare the field subobjects. */ 1533 1.1 mrg for (tree field = next_initializable_field (TYPE_FIELDS (ctype)); 1534 1.1 mrg field; 1535 1.1 mrg field = next_initializable_field (DECL_CHAIN (field))) 1536 1.1 mrg { 1537 1.1 mrg if (DECL_VIRTUAL_P (field) || DECL_FIELD_IS_BASE (field)) 1538 1.1 mrg /* We ignore the vptr, and we already handled bases. */ 1539 1.1 mrg continue; 1540 1.1 mrg 1541 1.1 mrg tree expr_type = TREE_TYPE (field); 1542 1.1 mrg 1543 1.1 mrg location_t field_loc = DECL_SOURCE_LOCATION (field); 1544 1.1 mrg 1545 1.1 mrg /* A defaulted comparison operator function for class C is defined as 1546 1.1 mrg deleted if any non-static data member of C is of reference type or 1547 1.1 mrg C has variant members. */ 1548 1.1 mrg if (TREE_CODE (expr_type) == REFERENCE_TYPE) 1549 1.1 mrg { 1550 1.1 mrg if (complain & tf_error) 1551 1.1 mrg inform (field_loc, "cannot default compare " 1552 1.1 mrg "reference member %qD", field); 1553 1.1 mrg bad = true; 1554 1.1 mrg continue; 1555 1.1 mrg } 1556 1.1 mrg else if (ANON_UNION_TYPE_P (expr_type) 1557 1.1 mrg && next_initializable_field (TYPE_FIELDS (expr_type))) 1558 1.1 mrg { 1559 1.1 mrg if (complain & tf_error) 1560 1.1 mrg inform (field_loc, "cannot default compare " 1561 1.1 mrg "anonymous union member"); 1562 1.1 mrg bad = true; 1563 1.1 mrg continue; 1564 1.1 mrg } 1565 1.1 mrg 1566 1.1 mrg tree lhs_mem = build3_loc (field_loc, COMPONENT_REF, expr_type, lhs, 1567 1.1 mrg field, NULL_TREE); 1568 1.1 mrg tree rhs_mem = build3_loc (field_loc, COMPONENT_REF, expr_type, rhs, 1569 1.1 mrg field, NULL_TREE); 1570 1.1 mrg tree loop_indexes = NULL_TREE; 1571 1.1 mrg while (TREE_CODE (expr_type) == ARRAY_TYPE) 1572 1.1 mrg { 1573 1.1 mrg /* Flexible array member. */ 1574 1.1 mrg if (TYPE_DOMAIN (expr_type) == NULL_TREE 1575 1.1 mrg || TYPE_MAX_VALUE (TYPE_DOMAIN (expr_type)) == NULL_TREE) 1576 1.1 mrg { 1577 1.1 mrg if (complain & tf_error) 1578 1.1 mrg inform (field_loc, "cannot default compare " 1579 1.1 mrg "flexible array member"); 1580 1.1 mrg bad = true; 1581 1.1 mrg break; 1582 1.1 mrg } 1583 1.1 mrg tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (expr_type)); 1584 1.1 mrg /* [0] array. No subobjects to compare, just skip it. */ 1585 1.1 mrg if (integer_all_onesp (maxval)) 1586 1.1 mrg break; 1587 1.1 mrg tree idx; 1588 1.1 mrg /* [1] array, no loop needed, just add [0] ARRAY_REF. 1589 1.1 mrg Similarly if !defining. */ 1590 1.1 mrg if (integer_zerop (maxval) || !defining) 1591 1.1 mrg idx = size_zero_node; 1592 1.1 mrg /* Some other array, will need runtime loop. */ 1593 1.1 mrg else 1594 1.1 mrg { 1595 1.1 mrg idx = force_target_expr (sizetype, maxval, complain); 1596 1.1 mrg loop_indexes = tree_cons (idx, NULL_TREE, loop_indexes); 1597 1.1 mrg } 1598 1.1 mrg expr_type = TREE_TYPE (expr_type); 1599 1.1 mrg lhs_mem = build4_loc (field_loc, ARRAY_REF, expr_type, lhs_mem, 1600 1.1 mrg idx, NULL_TREE, NULL_TREE); 1601 1.1 mrg rhs_mem = build4_loc (field_loc, ARRAY_REF, expr_type, rhs_mem, 1602 1.1 mrg idx, NULL_TREE, NULL_TREE); 1603 1.1 mrg } 1604 1.1 mrg if (TREE_CODE (expr_type) == ARRAY_TYPE) 1605 1.1 mrg continue; 1606 1.1 mrg 1607 1.1 mrg tree comp = do_one_comp (field_loc, info, field, lhs_mem, rhs_mem); 1608 1.1 mrg if (comp == error_mark_node) 1609 1.1 mrg { 1610 1.1 mrg bad = true; 1611 1.1 mrg continue; 1612 1.1 mrg } 1613 1.1 mrg 1614 1.1 mrg /* Most of the time, comp is the expression that should be evaluated 1615 1.1 mrg to compare the two members. If the expression needs to be 1616 1.1 mrg evaluated more than once in a loop, it will be a TREE_LIST 1617 1.1 mrg instead, whose TREE_VALUE is the expression for one array element, 1618 1.1 mrg TREE_PURPOSE is innermost iterator temporary and if the array 1619 1.1 mrg is multidimensional, TREE_CHAIN will contain another TREE_LIST 1620 1.1 mrg with second innermost iterator in its TREE_PURPOSE and so on. */ 1621 1.1 mrg if (loop_indexes) 1622 1.1 mrg { 1623 1.1 mrg TREE_VALUE (loop_indexes) = comp; 1624 1.1 mrg comp = loop_indexes; 1625 1.1 mrg } 1626 1.1 mrg comps.safe_push (comp); 1627 1.1 mrg } 1628 1.1 mrg if (code == SPACESHIP_EXPR && is_auto (rettype)) 1629 1.1 mrg { 1630 1.1 mrg rettype = common_comparison_type (comps); 1631 1.1 mrg apply_deduced_return_type (fndecl, rettype); 1632 1.1 mrg } 1633 1.1 mrg if (bad) 1634 1.1 mrg { 1635 1.1 mrg DECL_DELETED_FN (fndecl) = true; 1636 1.1 mrg goto out; 1637 1.1 mrg } 1638 1.1 mrg for (unsigned i = 0; i < comps.length(); ++i) 1639 1.1 mrg { 1640 1.1 mrg tree comp = comps[i]; 1641 1.1 mrg tree eq, retval = NULL_TREE, if_ = NULL_TREE; 1642 1.1 mrg tree loop_indexes = NULL_TREE; 1643 1.1 mrg if (defining) 1644 1.1 mrg { 1645 1.1 mrg if (TREE_CODE (comp) == TREE_LIST) 1646 1.1 mrg { 1647 1.1 mrg loop_indexes = comp; 1648 1.1 mrg comp = TREE_VALUE (comp); 1649 1.1 mrg loop_indexes = nreverse (loop_indexes); 1650 1.1 mrg for (tree loop_index = loop_indexes; loop_index; 1651 1.1 mrg loop_index = TREE_CHAIN (loop_index)) 1652 1.1 mrg { 1653 1.1 mrg tree for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE); 1654 1.1 mrg tree idx = TREE_PURPOSE (loop_index); 1655 1.1 mrg tree maxval = TARGET_EXPR_INITIAL (idx); 1656 1.1 mrg TARGET_EXPR_INITIAL (idx) = size_zero_node; 1657 1.1 mrg add_stmt (idx); 1658 1.1 mrg finish_init_stmt (for_stmt); 1659 1.1 mrg finish_for_cond (build2 (LE_EXPR, boolean_type_node, idx, 1660 1.1 mrg maxval), for_stmt, false, 0); 1661 1.1 mrg finish_for_expr (cp_build_unary_op (PREINCREMENT_EXPR, 1662 1.1 mrg TARGET_EXPR_SLOT (idx), 1663 1.1 mrg false, complain), 1664 1.1 mrg for_stmt); 1665 1.1 mrg /* Store in TREE_VALUE the for_stmt tree, so that we can 1666 1.1 mrg later on call finish_for_stmt on it (in the reverse 1667 1.1 mrg order). */ 1668 1.1 mrg TREE_VALUE (loop_index) = for_stmt; 1669 1.1 mrg } 1670 1.1 mrg loop_indexes = nreverse (loop_indexes); 1671 1.1 mrg } 1672 1.1 mrg if_ = begin_if_stmt (); 1673 1.1 mrg } 1674 1.1 mrg /* Spaceship is specified to use !=, but for the comparison category 1675 1.1 mrg types, != is equivalent to !(==), so let's use == directly. */ 1676 1.1 mrg if (code == EQ_EXPR) 1677 1.1 mrg { 1678 1.1 mrg /* if (x==y); else return false; */ 1679 1.1 mrg eq = comp; 1680 1.1 mrg retval = boolean_false_node; 1681 1.1 mrg } 1682 1.1 mrg else 1683 1.1 mrg { 1684 1.1 mrg /* if (auto v = x<=>y, v == 0); else return v; */ 1685 1.1 mrg if (TREE_CODE (comp) == SPACESHIP_EXPR) 1686 1.1 mrg TREE_TYPE (comp) = rettype; 1687 1.1 mrg else 1688 1.1 mrg comp = build_static_cast (input_location, rettype, comp, 1689 1.1 mrg complain); 1690 1.1 mrg info.check (comp); 1691 1.1 mrg if (defining) 1692 1.1 mrg { 1693 1.1 mrg tree var = create_temporary_var (rettype); 1694 1.1 mrg pushdecl (var); 1695 1.1 mrg cp_finish_decl (var, comp, false, NULL_TREE, flags); 1696 1.1 mrg comp = retval = var; 1697 1.1 mrg } 1698 1.1 mrg eq = build_new_op (info.loc, EQ_EXPR, flags, comp, 1699 1.1 mrg integer_zero_node, NULL_TREE, NULL_TREE, 1700 1.1 mrg NULL, complain); 1701 1.1 mrg } 1702 1.1 mrg tree ceq = contextual_conv_bool (eq, complain); 1703 1.1 mrg info.check (ceq); 1704 1.1 mrg if (defining) 1705 1.1 mrg { 1706 1.1 mrg finish_if_stmt_cond (ceq, if_); 1707 1.1 mrg finish_then_clause (if_); 1708 1.1 mrg begin_else_clause (if_); 1709 1.1 mrg finish_return_stmt (retval); 1710 1.1 mrg finish_else_clause (if_); 1711 1.1 mrg finish_if_stmt (if_); 1712 1.1 mrg for (tree loop_index = loop_indexes; loop_index; 1713 1.1 mrg loop_index = TREE_CHAIN (loop_index)) 1714 1.1 mrg finish_for_stmt (TREE_VALUE (loop_index)); 1715 1.1 mrg } 1716 1.1 mrg } 1717 1.1 mrg if (defining) 1718 1.1 mrg { 1719 1.1 mrg tree val; 1720 1.1 mrg if (code == EQ_EXPR) 1721 1.1 mrg val = boolean_true_node; 1722 1.1 mrg else 1723 1.1 mrg { 1724 1.1 mrg tree seql = lookup_comparison_result (cc_strong_ordering, 1725 1.1 mrg "equal", complain); 1726 1.1 mrg val = build_static_cast (input_location, rettype, seql, 1727 1.1 mrg complain); 1728 1.1 mrg } 1729 1.1 mrg finish_return_stmt (val); 1730 1.1 mrg } 1731 1.1 mrg } 1732 1.1 mrg else if (code == NE_EXPR) 1733 1.1 mrg { 1734 1.1 mrg tree comp = build_new_op (info.loc, EQ_EXPR, flags, lhs, rhs, 1735 1.1 mrg NULL_TREE, NULL_TREE, NULL, complain); 1736 1.1 mrg comp = contextual_conv_bool (comp, complain); 1737 1.1 mrg info.check (comp); 1738 1.1 mrg if (defining) 1739 1.1 mrg { 1740 1.1 mrg tree neg = build1 (TRUTH_NOT_EXPR, boolean_type_node, comp); 1741 1.1 mrg finish_return_stmt (neg); 1742 1.1 mrg } 1743 1.1 mrg } 1744 1.1 mrg else 1745 1.1 mrg { 1746 1.1 mrg tree comp = build_new_op (info.loc, SPACESHIP_EXPR, flags, lhs, rhs, 1747 1.1 mrg NULL_TREE, NULL_TREE, NULL, complain); 1748 1.1 mrg tree comp2 = build_new_op (info.loc, code, flags, comp, integer_zero_node, 1749 1.1 mrg NULL_TREE, NULL_TREE, NULL, complain); 1750 1.1 mrg info.check (comp2); 1751 1.1 mrg if (defining) 1752 1.1 mrg finish_return_stmt (comp2); 1753 1.1 mrg } 1754 1.1 mrg 1755 1.1 mrg out: 1756 1.1 mrg if (defining) 1757 1.1 mrg finish_compound_stmt (compound_stmt); 1758 1.1 mrg else 1759 1.1 mrg --cp_unevaluated_operand; 1760 1.1 mrg } 1761 1.1 mrg 1762 1.1 mrg /* True iff DECL is an implicitly-declared special member function with no real 1763 1.1 mrg source location, so we can use its DECL_SOURCE_LOCATION to remember where we 1764 1.1 mrg triggered its synthesis. */ 1765 1.1 mrg 1766 1.1 mrg bool 1767 1.1 mrg decl_remember_implicit_trigger_p (tree decl) 1768 1.1 mrg { 1769 1.1 mrg if (!DECL_ARTIFICIAL (decl)) 1770 1.1 mrg return false; 1771 1.1 mrg special_function_kind sfk = special_function_p (decl); 1772 1.1 mrg /* Inherited constructors have the location of their using-declaration, and 1773 1.1 mrg operator== has the location of the corresponding operator<=>. */ 1774 1.1 mrg return (sfk != sfk_inheriting_constructor 1775 1.1 mrg && sfk != sfk_comparison); 1776 1.1 mrg } 1777 1.1 mrg 1778 1.1 mrg /* Synthesize FNDECL, a non-static member function. */ 1779 1.1 mrg 1780 1.1 mrg void 1781 1.1 mrg synthesize_method (tree fndecl) 1782 1.1 mrg { 1783 1.1 mrg bool nested = (current_function_decl != NULL_TREE); 1784 1.1 mrg tree context = decl_function_context (fndecl); 1785 1.1 mrg bool need_body = true; 1786 1.1 mrg tree stmt; 1787 1.1 mrg location_t save_input_location = input_location; 1788 1.1 mrg int error_count = errorcount; 1789 1.1 mrg int warning_count = warningcount + werrorcount; 1790 1.1 mrg special_function_kind sfk = special_function_p (fndecl); 1791 1.1 mrg 1792 1.1 mrg /* Reset the source location, we might have been previously 1793 1.1 mrg deferred, and thus have saved where we were first needed. */ 1794 1.1 mrg if (decl_remember_implicit_trigger_p (fndecl)) 1795 1.1 mrg DECL_SOURCE_LOCATION (fndecl) 1796 1.1 mrg = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl))); 1797 1.1 mrg 1798 1.1 mrg /* If we've been asked to synthesize a clone, just synthesize the 1799 1.1 mrg cloned function instead. Doing so will automatically fill in the 1800 1.1 mrg body for the clone. */ 1801 1.1 mrg if (DECL_CLONED_FUNCTION_P (fndecl)) 1802 1.1 mrg fndecl = DECL_CLONED_FUNCTION (fndecl); 1803 1.1 mrg 1804 1.1 mrg /* We may be in the middle of deferred access check. Disable 1805 1.1 mrg it now. */ 1806 1.1 mrg push_deferring_access_checks (dk_no_deferred); 1807 1.1 mrg 1808 1.1 mrg if (! context) 1809 1.1 mrg push_to_top_level (); 1810 1.1 mrg else if (nested) 1811 1.1 mrg push_function_context (); 1812 1.1 mrg 1813 1.1 mrg input_location = DECL_SOURCE_LOCATION (fndecl); 1814 1.1 mrg 1815 1.1 mrg start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED); 1816 1.1 mrg stmt = begin_function_body (); 1817 1.1 mrg 1818 1.1 mrg if (DECL_ASSIGNMENT_OPERATOR_P (fndecl) 1819 1.1 mrg && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR)) 1820 1.1 mrg { 1821 1.1 mrg do_build_copy_assign (fndecl); 1822 1.1 mrg need_body = false; 1823 1.1 mrg } 1824 1.1 mrg else if (DECL_CONSTRUCTOR_P (fndecl)) 1825 1.1 mrg { 1826 1.1 mrg tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl); 1827 1.1 mrg if (arg_chain != void_list_node) 1828 1.1 mrg do_build_copy_constructor (fndecl); 1829 1.1 mrg else 1830 1.1 mrg finish_mem_initializers (NULL_TREE); 1831 1.1 mrg } 1832 1.1 mrg else if (sfk == sfk_comparison) 1833 1.1 mrg { 1834 1.1 mrg /* Pass tf_none so the function is just deleted if there's a problem. */ 1835 1.1 mrg build_comparison_op (fndecl, true, tf_none); 1836 1.1 mrg need_body = false; 1837 1.1 mrg } 1838 1.1 mrg 1839 1.1 mrg /* If we haven't yet generated the body of the function, just 1840 1.1 mrg generate an empty compound statement. */ 1841 1.1 mrg if (need_body) 1842 1.1 mrg { 1843 1.1 mrg tree compound_stmt; 1844 1.1 mrg compound_stmt = begin_compound_stmt (BCS_FN_BODY); 1845 1.1 mrg finish_compound_stmt (compound_stmt); 1846 1.1 mrg } 1847 1.1 mrg 1848 1.1 mrg finish_function_body (stmt); 1849 1.1 mrg finish_function (/*inline_p=*/false); 1850 1.1 mrg 1851 1.1 mrg if (!DECL_DELETED_FN (fndecl)) 1852 1.1 mrg expand_or_defer_fn (fndecl); 1853 1.1 mrg 1854 1.1 mrg input_location = save_input_location; 1855 1.1 mrg 1856 1.1 mrg if (! context) 1857 1.1 mrg pop_from_top_level (); 1858 1.1 mrg else if (nested) 1859 1.1 mrg pop_function_context (); 1860 1.1 mrg 1861 1.1 mrg pop_deferring_access_checks (); 1862 1.1 mrg 1863 1.1 mrg if (error_count != errorcount || warning_count != warningcount + werrorcount) 1864 1.1 mrg if (DECL_ARTIFICIAL (fndecl)) 1865 1.1 mrg inform (input_location, "synthesized method %qD first required here", 1866 1.1 mrg fndecl); 1867 1.1 mrg } 1868 1.1 mrg 1869 1.1 mrg /* Like synthesize_method, but don't actually synthesize defaulted comparison 1870 1.1 mrg methods if their class is still incomplete. Just deduce the return 1871 1.1 mrg type in that case. */ 1872 1.1 mrg 1873 1.1 mrg void 1874 1.1 mrg maybe_synthesize_method (tree fndecl) 1875 1.1 mrg { 1876 1.1 mrg if (special_function_p (fndecl) == sfk_comparison) 1877 1.1 mrg { 1878 1.1 mrg tree lhs = DECL_ARGUMENTS (fndecl); 1879 1.1 mrg if (is_this_parameter (lhs)) 1880 1.1 mrg lhs = cp_build_fold_indirect_ref (lhs); 1881 1.1 mrg else 1882 1.1 mrg lhs = convert_from_reference (lhs); 1883 1.1 mrg tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs)); 1884 1.1 mrg if (!COMPLETE_TYPE_P (ctype)) 1885 1.1 mrg { 1886 1.1 mrg push_deferring_access_checks (dk_no_deferred); 1887 1.1 mrg build_comparison_op (fndecl, false, tf_none); 1888 1.1 mrg pop_deferring_access_checks (); 1889 1.1 mrg return; 1890 1.1 mrg } 1891 1.1 mrg } 1892 1.1 mrg return synthesize_method (fndecl); 1893 1.1 mrg } 1894 1.1 mrg 1895 1.1 mrg /* Build a reference to type TYPE with cv-quals QUALS, which is an 1896 1.1 mrg rvalue if RVALUE is true. */ 1897 1.1 mrg 1898 1.1 mrg static tree 1899 1.1 mrg build_stub_type (tree type, int quals, bool rvalue) 1900 1.1 mrg { 1901 1.1 mrg tree argtype = cp_build_qualified_type (type, quals); 1902 1.1 mrg return cp_build_reference_type (argtype, rvalue); 1903 1.1 mrg } 1904 1.1 mrg 1905 1.1 mrg /* Build a dummy glvalue from dereferencing a dummy reference of type 1906 1.1 mrg REFTYPE. */ 1907 1.1 mrg 1908 1.1 mrg tree 1909 1.1 mrg build_stub_object (tree reftype) 1910 1.1 mrg { 1911 1.1 mrg if (!TYPE_REF_P (reftype)) 1912 1.1 mrg reftype = cp_build_reference_type (reftype, /*rval*/true); 1913 1.1 mrg tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node); 1914 1.1 mrg return convert_from_reference (stub); 1915 1.1 mrg } 1916 1.1 mrg 1917 1.1 mrg /* Determine which function will be called when looking up NAME in TYPE, 1918 1.1 mrg called with a single ARGTYPE argument, or no argument if ARGTYPE is 1919 1.1 mrg null. FLAGS and COMPLAIN are as for build_new_method_call. 1920 1.1 mrg 1921 1.1 mrg Returns a FUNCTION_DECL if all is well. 1922 1.1 mrg Returns NULL_TREE if overload resolution failed. 1923 1.1 mrg Returns error_mark_node if the chosen function cannot be called. */ 1924 1.1 mrg 1925 1.1 mrg static tree 1926 1.1 mrg locate_fn_flags (tree type, tree name, tree argtype, int flags, 1927 1.1 mrg tsubst_flags_t complain) 1928 1.1 mrg { 1929 1.1 mrg tree ob, fn, fns, binfo, rval; 1930 1.1 mrg 1931 1.1 mrg if (TYPE_P (type)) 1932 1.1 mrg binfo = TYPE_BINFO (type); 1933 1.1 mrg else 1934 1.1 mrg { 1935 1.1 mrg binfo = type; 1936 1.1 mrg type = BINFO_TYPE (binfo); 1937 1.1 mrg } 1938 1.1 mrg 1939 1.1 mrg ob = build_stub_object (cp_build_reference_type (type, false)); 1940 1.1 mrg releasing_vec args; 1941 1.1 mrg if (argtype) 1942 1.1 mrg { 1943 1.1 mrg if (TREE_CODE (argtype) == TREE_LIST) 1944 1.1 mrg { 1945 1.1 mrg for (tree elt = argtype; elt && elt != void_list_node; 1946 1.1 mrg elt = TREE_CHAIN (elt)) 1947 1.1 mrg { 1948 1.1 mrg tree type = TREE_VALUE (elt); 1949 1.1 mrg tree arg = build_stub_object (type); 1950 1.1 mrg vec_safe_push (args, arg); 1951 1.1 mrg } 1952 1.1 mrg } 1953 1.1 mrg else 1954 1.1 mrg { 1955 1.1 mrg tree arg = build_stub_object (argtype); 1956 1.1 mrg args->quick_push (arg); 1957 1.1 mrg } 1958 1.1 mrg } 1959 1.1 mrg 1960 1.1 mrg fns = lookup_fnfields (binfo, name, 0, complain); 1961 1.1 mrg rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain); 1962 1.1 mrg 1963 1.1 mrg if (fn && rval == error_mark_node) 1964 1.1 mrg return rval; 1965 1.1 mrg else 1966 1.1 mrg return fn; 1967 1.1 mrg } 1968 1.1 mrg 1969 1.1 mrg /* Locate the dtor of TYPE. */ 1970 1.1 mrg 1971 1.1 mrg tree 1972 1.1 mrg get_dtor (tree type, tsubst_flags_t complain) 1973 1.1 mrg { 1974 1.1 mrg tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE, 1975 1.1 mrg LOOKUP_NORMAL, complain); 1976 1.1 mrg if (fn == error_mark_node) 1977 1.1 mrg return NULL_TREE; 1978 1.1 mrg return fn; 1979 1.1 mrg } 1980 1.1 mrg 1981 1.1 mrg /* Locate the default ctor of TYPE. */ 1982 1.1 mrg 1983 1.1 mrg tree 1984 1.1 mrg locate_ctor (tree type) 1985 1.1 mrg { 1986 1.1 mrg tree fn; 1987 1.1 mrg 1988 1.1 mrg push_deferring_access_checks (dk_no_check); 1989 1.1 mrg fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE, 1990 1.1 mrg LOOKUP_SPECULATIVE, tf_none); 1991 1.1 mrg pop_deferring_access_checks (); 1992 1.1 mrg if (fn == error_mark_node) 1993 1.1 mrg return NULL_TREE; 1994 1.1 mrg return fn; 1995 1.1 mrg } 1996 1.1 mrg 1997 1.1 mrg /* Likewise, but give any appropriate errors. */ 1998 1.1 mrg 1999 1.1 mrg tree 2000 1.1 mrg get_default_ctor (tree type) 2001 1.1 mrg { 2002 1.1 mrg tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE, 2003 1.1 mrg LOOKUP_NORMAL, tf_warning_or_error); 2004 1.1 mrg if (fn == error_mark_node) 2005 1.1 mrg return NULL_TREE; 2006 1.1 mrg return fn; 2007 1.1 mrg } 2008 1.1 mrg 2009 1.1 mrg /* Locate the copy ctor of TYPE. */ 2010 1.1 mrg 2011 1.1 mrg tree 2012 1.1 mrg get_copy_ctor (tree type, tsubst_flags_t complain) 2013 1.1 mrg { 2014 1.1 mrg int quals = (TYPE_HAS_CONST_COPY_CTOR (type) 2015 1.1 mrg ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED); 2016 1.1 mrg tree argtype = build_stub_type (type, quals, false); 2017 1.1 mrg tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype, 2018 1.1 mrg LOOKUP_NORMAL, complain); 2019 1.1 mrg if (fn == error_mark_node) 2020 1.1 mrg return NULL_TREE; 2021 1.1 mrg return fn; 2022 1.1 mrg } 2023 1.1 mrg 2024 1.1 mrg /* Locate the copy assignment operator of TYPE. */ 2025 1.1 mrg 2026 1.1 mrg tree 2027 1.1 mrg get_copy_assign (tree type) 2028 1.1 mrg { 2029 1.1 mrg int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type) 2030 1.1 mrg ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED); 2031 1.1 mrg tree argtype = build_stub_type (type, quals, false); 2032 1.1 mrg tree fn = locate_fn_flags (type, assign_op_identifier, argtype, 2033 1.1 mrg LOOKUP_NORMAL, tf_warning_or_error); 2034 1.1 mrg if (fn == error_mark_node) 2035 1.1 mrg return NULL_TREE; 2036 1.1 mrg return fn; 2037 1.1 mrg } 2038 1.1 mrg 2039 1.1 mrg /* walk_tree helper function for is_trivially_xible. If *TP is a call, 2040 1.1 mrg return it if it calls something other than a trivial special member 2041 1.1 mrg function. */ 2042 1.1 mrg 2043 1.1 mrg static tree 2044 1.1 mrg check_nontriv (tree *tp, int *, void *) 2045 1.1 mrg { 2046 1.1 mrg tree fn = cp_get_callee (*tp); 2047 1.1 mrg if (fn == NULL_TREE) 2048 1.1 mrg return NULL_TREE; 2049 1.1 mrg 2050 1.1 mrg if (TREE_CODE (fn) == ADDR_EXPR) 2051 1.1 mrg fn = TREE_OPERAND (fn, 0); 2052 1.1 mrg 2053 1.1 mrg if (TREE_CODE (fn) != FUNCTION_DECL 2054 1.1 mrg || !trivial_fn_p (fn)) 2055 1.1 mrg return fn; 2056 1.1 mrg return NULL_TREE; 2057 1.1 mrg } 2058 1.1 mrg 2059 1.1 mrg /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */ 2060 1.1 mrg 2061 1.1 mrg static tree 2062 1.1 mrg assignable_expr (tree to, tree from) 2063 1.1 mrg { 2064 1.1 mrg cp_unevaluated cp_uneval_guard; 2065 1.1 mrg to = build_stub_object (to); 2066 1.1 mrg from = build_stub_object (from); 2067 1.1 mrg tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, tf_none); 2068 1.1 mrg return r; 2069 1.1 mrg } 2070 1.1 mrg 2071 1.1 mrg /* The predicate condition for a template specialization 2072 1.1 mrg is_constructible<T, Args...> shall be satisfied if and only if the 2073 1.1 mrg following variable definition would be well-formed for some invented 2074 1.1 mrg variable t: T t(create<Args>()...); 2075 1.1 mrg 2076 1.1 mrg Return something equivalent in well-formedness and triviality. */ 2077 1.1 mrg 2078 1.1 mrg static tree 2079 1.1 mrg constructible_expr (tree to, tree from) 2080 1.1 mrg { 2081 1.1 mrg tree expr; 2082 1.1 mrg cp_unevaluated cp_uneval_guard; 2083 1.1 mrg if (CLASS_TYPE_P (to)) 2084 1.1 mrg { 2085 1.1 mrg tree ctype = to; 2086 1.1 mrg vec<tree, va_gc> *args = NULL; 2087 1.1 mrg if (!TYPE_REF_P (to)) 2088 1.1 mrg to = cp_build_reference_type (to, /*rval*/false); 2089 1.1 mrg tree ob = build_stub_object (to); 2090 1.1 mrg for (; from; from = TREE_CHAIN (from)) 2091 1.1 mrg vec_safe_push (args, build_stub_object (TREE_VALUE (from))); 2092 1.1 mrg expr = build_special_member_call (ob, complete_ctor_identifier, &args, 2093 1.1 mrg ctype, LOOKUP_NORMAL, tf_none); 2094 1.1 mrg if (expr == error_mark_node) 2095 1.1 mrg return error_mark_node; 2096 1.1 mrg /* The current state of the standard vis-a-vis LWG 2116 is that 2097 1.1 mrg is_*constructible involves destruction as well. */ 2098 1.1 mrg if (type_build_dtor_call (ctype)) 2099 1.1 mrg { 2100 1.1 mrg tree dtor = build_special_member_call (ob, complete_dtor_identifier, 2101 1.1 mrg NULL, ctype, LOOKUP_NORMAL, 2102 1.1 mrg tf_none); 2103 1.1 mrg if (dtor == error_mark_node) 2104 1.1 mrg return error_mark_node; 2105 1.1 mrg if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype)) 2106 1.1 mrg expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor); 2107 1.1 mrg } 2108 1.1 mrg } 2109 1.1 mrg else 2110 1.1 mrg { 2111 1.1 mrg if (from == NULL_TREE) 2112 1.1 mrg return build_value_init (strip_array_types (to), tf_none); 2113 1.1 mrg const int len = list_length (from); 2114 1.1 mrg if (len > 1) 2115 1.1 mrg { 2116 1.1 mrg if (cxx_dialect < cxx20) 2117 1.1 mrg /* Too many initializers. */ 2118 1.1 mrg return error_mark_node; 2119 1.1 mrg 2120 1.1 mrg /* In C++20 this is well-formed: 2121 1.1 mrg using T = int[2]; 2122 1.1 mrg T t(1, 2); 2123 1.1 mrg which means that std::is_constructible_v<int[2], int, int> 2124 1.1 mrg should be true. */ 2125 1.1 mrg vec<constructor_elt, va_gc> *v; 2126 1.1 mrg vec_alloc (v, len); 2127 1.1 mrg for (tree t = from; t; t = TREE_CHAIN (t)) 2128 1.1 mrg { 2129 1.1 mrg tree stub = build_stub_object (TREE_VALUE (t)); 2130 1.1 mrg constructor_elt elt = { NULL_TREE, stub }; 2131 1.1 mrg v->quick_push (elt); 2132 1.1 mrg } 2133 1.1 mrg from = build_constructor (init_list_type_node, v); 2134 1.1 mrg CONSTRUCTOR_IS_DIRECT_INIT (from) = true; 2135 1.1 mrg CONSTRUCTOR_IS_PAREN_INIT (from) = true; 2136 1.1 mrg } 2137 1.1 mrg else 2138 1.1 mrg from = build_stub_object (TREE_VALUE (from)); 2139 1.1 mrg expr = perform_direct_initialization_if_possible (to, from, 2140 1.1 mrg /*cast*/false, 2141 1.1 mrg tf_none); 2142 1.1 mrg /* If t(e) didn't work, maybe t{e} will. */ 2143 1.1 mrg if (expr == NULL_TREE 2144 1.1 mrg && len == 1 2145 1.1 mrg && cxx_dialect >= cxx20) 2146 1.1 mrg { 2147 1.1 mrg from = build_constructor_single (init_list_type_node, NULL_TREE, 2148 1.1 mrg from); 2149 1.1 mrg CONSTRUCTOR_IS_DIRECT_INIT (from) = true; 2150 1.1 mrg CONSTRUCTOR_IS_PAREN_INIT (from) = true; 2151 1.1 mrg expr = perform_direct_initialization_if_possible (to, from, 2152 1.1 mrg /*cast*/false, 2153 1.1 mrg tf_none); 2154 1.1 mrg } 2155 1.1 mrg } 2156 1.1 mrg return expr; 2157 1.1 mrg } 2158 1.1 mrg 2159 1.1 mrg /* Returns a tree iff TO is assignable (if CODE is MODIFY_EXPR) or 2160 1.1 mrg constructible (otherwise) from FROM, which is a single type for 2161 1.1 mrg assignment or a list of types for construction. */ 2162 1.1 mrg 2163 1.1 mrg static tree 2164 1.1 mrg is_xible_helper (enum tree_code code, tree to, tree from, bool trivial) 2165 1.1 mrg { 2166 1.1 mrg to = complete_type (to); 2167 1.1 mrg deferring_access_check_sentinel acs (dk_no_deferred); 2168 1.1 mrg if (VOID_TYPE_P (to) || ABSTRACT_CLASS_TYPE_P (to) 2169 1.1 mrg || (from && FUNC_OR_METHOD_TYPE_P (from) 2170 1.1 mrg && (TYPE_READONLY (from) || FUNCTION_REF_QUALIFIED (from)))) 2171 1.1 mrg return error_mark_node; 2172 1.1 mrg tree expr; 2173 1.1 mrg if (code == MODIFY_EXPR) 2174 1.1 mrg expr = assignable_expr (to, from); 2175 1.1 mrg else if (trivial && from && TREE_CHAIN (from) 2176 1.1 mrg && cxx_dialect < cxx20) 2177 1.1 mrg return error_mark_node; // only 0- and 1-argument ctors can be trivial 2178 1.1 mrg // before C++20 aggregate paren init 2179 1.1 mrg else if (TREE_CODE (to) == ARRAY_TYPE && !TYPE_DOMAIN (to)) 2180 1.1 mrg return error_mark_node; // can't construct an array of unknown bound 2181 1.1 mrg else 2182 1.1 mrg expr = constructible_expr (to, from); 2183 1.1 mrg return expr; 2184 1.1 mrg } 2185 1.1 mrg 2186 1.1 mrg /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or 2187 1.1 mrg constructible (otherwise) from FROM, which is a single type for 2188 1.1 mrg assignment or a list of types for construction. */ 2189 1.1 mrg 2190 1.1 mrg bool 2191 1.1 mrg is_trivially_xible (enum tree_code code, tree to, tree from) 2192 1.1 mrg { 2193 1.1 mrg tree expr = is_xible_helper (code, to, from, /*trivial*/true); 2194 1.1 mrg if (expr == NULL_TREE || expr == error_mark_node) 2195 1.1 mrg return false; 2196 1.1 mrg tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL); 2197 1.1 mrg return !nt; 2198 1.1 mrg } 2199 1.1 mrg 2200 1.1 mrg /* Returns true iff TO is nothrow assignable (if CODE is MODIFY_EXPR) or 2201 1.1 mrg constructible (otherwise) from FROM, which is a single type for 2202 1.1 mrg assignment or a list of types for construction. */ 2203 1.1 mrg 2204 1.1 mrg bool 2205 1.1 mrg is_nothrow_xible (enum tree_code code, tree to, tree from) 2206 1.1 mrg { 2207 1.1 mrg tree expr = is_xible_helper (code, to, from, /*trivial*/false); 2208 1.1 mrg if (expr == NULL_TREE || expr == error_mark_node) 2209 1.1 mrg return false; 2210 1.1 mrg return expr_noexcept_p (expr, tf_none); 2211 1.1 mrg } 2212 1.1 mrg 2213 1.1 mrg /* Returns true iff TO is assignable (if CODE is MODIFY_EXPR) or 2214 1.1 mrg constructible (otherwise) from FROM, which is a single type for 2215 1.1 mrg assignment or a list of types for construction. */ 2216 1.1 mrg 2217 1.1 mrg bool 2218 1.1 mrg is_xible (enum tree_code code, tree to, tree from) 2219 1.1 mrg { 2220 1.1 mrg tree expr = is_xible_helper (code, to, from, /*trivial*/false); 2221 1.1 mrg if (expr == error_mark_node) 2222 1.1 mrg return false; 2223 1.1 mrg return !!expr; 2224 1.1 mrg } 2225 1.1 mrg 2226 1.1 mrg /* Categorize various special_function_kinds. */ 2227 1.1 mrg #define SFK_CTOR_P(sfk) \ 2228 1.1 mrg ((sfk) >= sfk_constructor && (sfk) <= sfk_move_constructor) 2229 1.1 mrg #define SFK_DTOR_P(sfk) \ 2230 1.1 mrg ((sfk) == sfk_destructor || (sfk) == sfk_virtual_destructor) 2231 1.1 mrg #define SFK_ASSIGN_P(sfk) \ 2232 1.1 mrg ((sfk) == sfk_copy_assignment || (sfk) == sfk_move_assignment) 2233 1.1 mrg #define SFK_COPY_P(sfk) \ 2234 1.1 mrg ((sfk) == sfk_copy_constructor || (sfk) == sfk_copy_assignment) 2235 1.1 mrg #define SFK_MOVE_P(sfk) \ 2236 1.1 mrg ((sfk) == sfk_move_constructor || (sfk) == sfk_move_assignment) 2237 1.1 mrg 2238 1.1 mrg /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and 2239 1.1 mrg DELETED_P or give an error message MSG with argument ARG. */ 2240 1.1 mrg 2241 1.1 mrg static void 2242 1.1 mrg process_subob_fn (tree fn, special_function_kind sfk, tree *spec_p, 2243 1.1 mrg bool *trivial_p, bool *deleted_p, bool *constexpr_p, 2244 1.1 mrg bool diag, tree arg, bool dtor_from_ctor = false) 2245 1.1 mrg { 2246 1.1 mrg if (!fn || fn == error_mark_node) 2247 1.1 mrg { 2248 1.1 mrg if (deleted_p) 2249 1.1 mrg *deleted_p = true; 2250 1.1 mrg return; 2251 1.1 mrg } 2252 1.1 mrg 2253 1.1 mrg if (spec_p) 2254 1.1 mrg { 2255 1.1 mrg if (!maybe_instantiate_noexcept (fn)) 2256 1.1 mrg *spec_p = error_mark_node; 2257 1.1 mrg else 2258 1.1 mrg { 2259 1.1 mrg tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)); 2260 1.1 mrg *spec_p = merge_exception_specifiers (*spec_p, raises); 2261 1.1 mrg } 2262 1.1 mrg } 2263 1.1 mrg 2264 1.1 mrg if (!trivial_fn_p (fn) && !dtor_from_ctor) 2265 1.1 mrg { 2266 1.1 mrg if (trivial_p) 2267 1.1 mrg *trivial_p = false; 2268 1.1 mrg if (TREE_CODE (arg) == FIELD_DECL 2269 1.1 mrg && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE) 2270 1.1 mrg { 2271 1.1 mrg if (deleted_p) 2272 1.1 mrg *deleted_p = true; 2273 1.1 mrg if (diag) 2274 1.1 mrg error ("union member %q+D with non-trivial %qD", arg, fn); 2275 1.1 mrg } 2276 1.1 mrg } 2277 1.1 mrg 2278 1.1 mrg if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn)) 2279 1.1 mrg { 2280 1.1 mrg *constexpr_p = false; 2281 1.1 mrg if (diag) 2282 1.1 mrg { 2283 1.1 mrg inform (DECL_SOURCE_LOCATION (fn), 2284 1.1 mrg SFK_DTOR_P (sfk) 2285 1.1 mrg ? G_("defaulted destructor calls non-%<constexpr%> %qD") 2286 1.1 mrg : G_("defaulted constructor calls non-%<constexpr%> %qD"), 2287 1.1 mrg fn); 2288 1.1 mrg explain_invalid_constexpr_fn (fn); 2289 1.1 mrg } 2290 1.1 mrg } 2291 1.1 mrg } 2292 1.1 mrg 2293 1.1 mrg /* Subroutine of synthesized_method_walk to allow recursion into anonymous 2294 1.1 mrg aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors 2295 1.1 mrg called from a synthesized constructor, in which case we don't consider 2296 1.1 mrg the triviality of the subobject destructor. */ 2297 1.1 mrg 2298 1.1 mrg static void 2299 1.1 mrg walk_field_subobs (tree fields, special_function_kind sfk, tree fnname, 2300 1.1 mrg int quals, tree *spec_p, bool *trivial_p, 2301 1.1 mrg bool *deleted_p, bool *constexpr_p, 2302 1.1 mrg bool diag, int flags, tsubst_flags_t complain, 2303 1.1 mrg bool dtor_from_ctor) 2304 1.1 mrg { 2305 1.1 mrg tree field; 2306 1.1 mrg for (field = fields; field; field = DECL_CHAIN (field)) 2307 1.1 mrg { 2308 1.1 mrg tree mem_type, argtype, rval; 2309 1.1 mrg 2310 1.1 mrg if (TREE_CODE (field) != FIELD_DECL 2311 1.1 mrg || DECL_ARTIFICIAL (field) 2312 1.1 mrg || DECL_UNNAMED_BIT_FIELD (field)) 2313 1.1 mrg continue; 2314 1.1 mrg 2315 1.1 mrg /* Variant members only affect deletedness. In particular, they don't 2316 1.1 mrg affect the exception-specification of a user-provided destructor, 2317 1.1 mrg which we're figuring out via get_defaulted_eh_spec. So if we aren't 2318 1.1 mrg asking if this is deleted, don't even look up the function; we don't 2319 1.1 mrg want an error about a deleted function we aren't actually calling. */ 2320 1.1 mrg if (sfk == sfk_destructor && deleted_p == NULL 2321 1.1 mrg && TREE_CODE (DECL_CONTEXT (field)) == UNION_TYPE) 2322 1.1 mrg break; 2323 1.1 mrg 2324 1.1 mrg mem_type = strip_array_types (TREE_TYPE (field)); 2325 1.1 mrg if (SFK_ASSIGN_P (sfk)) 2326 1.1 mrg { 2327 1.1 mrg bool bad = true; 2328 1.1 mrg if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type)) 2329 1.1 mrg { 2330 1.1 mrg if (diag) 2331 1.1 mrg error ("non-static const member %q#D, cannot use default " 2332 1.1 mrg "assignment operator", field); 2333 1.1 mrg } 2334 1.1 mrg else if (TYPE_REF_P (mem_type)) 2335 1.1 mrg { 2336 1.1 mrg if (diag) 2337 1.1 mrg error ("non-static reference member %q#D, cannot use " 2338 1.1 mrg "default assignment operator", field); 2339 1.1 mrg } 2340 1.1 mrg else 2341 1.1 mrg bad = false; 2342 1.1 mrg 2343 1.1 mrg if (bad && deleted_p) 2344 1.1 mrg *deleted_p = true; 2345 1.1 mrg } 2346 1.1 mrg else if (sfk == sfk_constructor || sfk == sfk_inheriting_constructor) 2347 1.1 mrg { 2348 1.1 mrg bool bad; 2349 1.1 mrg 2350 1.1 mrg if (DECL_INITIAL (field)) 2351 1.1 mrg { 2352 1.1 mrg if (diag && DECL_INITIAL (field) == error_mark_node) 2353 1.1 mrg inform (DECL_SOURCE_LOCATION (field), 2354 1.1 mrg "initializer for %q#D is invalid", field); 2355 1.1 mrg if (trivial_p) 2356 1.1 mrg *trivial_p = false; 2357 1.1 mrg /* Core 1351: If the field has an NSDMI that could throw, the 2358 1.1 mrg default constructor is noexcept(false). */ 2359 1.1 mrg if (spec_p) 2360 1.1 mrg { 2361 1.1 mrg tree nsdmi = get_nsdmi (field, /*ctor*/false, complain); 2362 1.1 mrg if (nsdmi == error_mark_node) 2363 1.1 mrg *spec_p = error_mark_node; 2364 1.1 mrg else if (*spec_p != error_mark_node 2365 1.1 mrg && !expr_noexcept_p (nsdmi, tf_none)) 2366 1.1 mrg *spec_p = noexcept_false_spec; 2367 1.1 mrg } 2368 1.1 mrg /* Don't do the normal processing. */ 2369 1.1 mrg continue; 2370 1.1 mrg } 2371 1.1 mrg 2372 1.1 mrg bad = false; 2373 1.1 mrg if (CP_TYPE_CONST_P (mem_type) 2374 1.1 mrg && default_init_uninitialized_part (mem_type)) 2375 1.1 mrg { 2376 1.1 mrg if (diag) 2377 1.1 mrg { 2378 1.1 mrg error ("uninitialized const member in %q#T", 2379 1.1 mrg current_class_type); 2380 1.1 mrg inform (DECL_SOURCE_LOCATION (field), 2381 1.1 mrg "%q#D should be initialized", field); 2382 1.1 mrg } 2383 1.1 mrg bad = true; 2384 1.1 mrg } 2385 1.1 mrg else if (TYPE_REF_P (mem_type)) 2386 1.1 mrg { 2387 1.1 mrg if (diag) 2388 1.1 mrg { 2389 1.1 mrg error ("uninitialized reference member in %q#T", 2390 1.1 mrg current_class_type); 2391 1.1 mrg inform (DECL_SOURCE_LOCATION (field), 2392 1.1 mrg "%q#D should be initialized", field); 2393 1.1 mrg } 2394 1.1 mrg bad = true; 2395 1.1 mrg } 2396 1.1 mrg 2397 1.1 mrg if (bad && deleted_p) 2398 1.1 mrg *deleted_p = true; 2399 1.1 mrg 2400 1.1 mrg /* Before C++20, for an implicitly-defined default constructor to 2401 1.1 mrg be constexpr, every member must have a user-provided default 2402 1.1 mrg constructor or an explicit initializer. */ 2403 1.1 mrg if (constexpr_p 2404 1.1 mrg && cxx_dialect < cxx20 2405 1.1 mrg && !CLASS_TYPE_P (mem_type) 2406 1.1 mrg && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE) 2407 1.1 mrg { 2408 1.1 mrg *constexpr_p = false; 2409 1.1 mrg if (diag) 2410 1.1 mrg inform (DECL_SOURCE_LOCATION (field), 2411 1.1 mrg "defaulted default constructor does not " 2412 1.1 mrg "initialize %q#D", field); 2413 1.1 mrg } 2414 1.1 mrg } 2415 1.1 mrg else if (sfk == sfk_copy_constructor) 2416 1.1 mrg { 2417 1.1 mrg /* 12.8p11b5 */ 2418 1.1 mrg if (TYPE_REF_P (mem_type) 2419 1.1 mrg && TYPE_REF_IS_RVALUE (mem_type)) 2420 1.1 mrg { 2421 1.1 mrg if (diag) 2422 1.1 mrg error ("copying non-static data member %q#D of rvalue " 2423 1.1 mrg "reference type", field); 2424 1.1 mrg if (deleted_p) 2425 1.1 mrg *deleted_p = true; 2426 1.1 mrg } 2427 1.1 mrg } 2428 1.1 mrg 2429 1.1 mrg if (!CLASS_TYPE_P (mem_type)) 2430 1.1 mrg continue; 2431 1.1 mrg 2432 1.1 mrg if (ANON_AGGR_TYPE_P (mem_type)) 2433 1.1 mrg { 2434 1.1 mrg walk_field_subobs (TYPE_FIELDS (mem_type), sfk, fnname, quals, 2435 1.1 mrg spec_p, trivial_p, deleted_p, constexpr_p, 2436 1.1 mrg diag, flags, complain, dtor_from_ctor); 2437 1.1 mrg continue; 2438 1.1 mrg } 2439 1.1 mrg 2440 1.1 mrg if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk)) 2441 1.1 mrg { 2442 1.1 mrg int mem_quals = cp_type_quals (mem_type) | quals; 2443 1.1 mrg if (DECL_MUTABLE_P (field)) 2444 1.1 mrg mem_quals &= ~TYPE_QUAL_CONST; 2445 1.1 mrg argtype = build_stub_type (mem_type, mem_quals, SFK_MOVE_P (sfk)); 2446 1.1 mrg } 2447 1.1 mrg else 2448 1.1 mrg argtype = NULL_TREE; 2449 1.1 mrg 2450 1.1 mrg rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain); 2451 1.1 mrg 2452 1.1 mrg process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p, 2453 1.1 mrg constexpr_p, diag, field, dtor_from_ctor); 2454 1.1 mrg } 2455 1.1 mrg } 2456 1.1 mrg 2457 1.1 mrg /* Base walker helper for synthesized_method_walk. Inspect a direct 2458 1.1 mrg or virtual base. BINFO is the parent type's binfo. BASE_BINFO is 2459 1.1 mrg the base binfo of interests. All other parms are as for 2460 1.1 mrg synthesized_method_walk, or its local vars. */ 2461 1.1 mrg 2462 1.1 mrg static tree 2463 1.1 mrg synthesized_method_base_walk (tree binfo, tree base_binfo, 2464 1.1 mrg special_function_kind sfk, tree fnname, int quals, 2465 1.1 mrg tree *inheriting_ctor, tree inherited_parms, 2466 1.1 mrg int flags, bool diag, 2467 1.1 mrg tree *spec_p, bool *trivial_p, 2468 1.1 mrg bool *deleted_p, bool *constexpr_p) 2469 1.1 mrg { 2470 1.1 mrg bool inherited_binfo = false; 2471 1.1 mrg tree argtype = NULL_TREE; 2472 1.1 mrg deferring_kind defer = dk_no_deferred; 2473 1.1 mrg 2474 1.1 mrg if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk)) 2475 1.1 mrg argtype = build_stub_type (BINFO_TYPE (base_binfo), quals, SFK_MOVE_P (sfk)); 2476 1.1 mrg else if (inheriting_ctor 2477 1.1 mrg && (inherited_binfo 2478 1.1 mrg = binfo_inherited_from (binfo, base_binfo, *inheriting_ctor))) 2479 1.1 mrg { 2480 1.1 mrg argtype = inherited_parms; 2481 1.1 mrg /* Don't check access on the inherited constructor. */ 2482 1.1 mrg if (flag_new_inheriting_ctors) 2483 1.1 mrg defer = dk_deferred; 2484 1.1 mrg } 2485 1.1 mrg else if (cxx_dialect >= cxx14 && sfk == sfk_virtual_destructor 2486 1.1 mrg && BINFO_VIRTUAL_P (base_binfo) 2487 1.1 mrg && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo))) 2488 1.1 mrg /* Don't check access when looking at vbases of abstract class's 2489 1.1 mrg virtual destructor. */ 2490 1.1 mrg defer = dk_no_check; 2491 1.1 mrg 2492 1.1 mrg if (defer != dk_no_deferred) 2493 1.1 mrg push_deferring_access_checks (defer); 2494 1.1 mrg tree rval = locate_fn_flags (base_binfo, fnname, argtype, flags, 2495 1.1 mrg diag ? tf_warning_or_error : tf_none); 2496 1.1 mrg if (defer != dk_no_deferred) 2497 1.1 mrg pop_deferring_access_checks (); 2498 1.1 mrg 2499 1.1 mrg /* Replace an inherited template with the appropriate specialization. */ 2500 1.1 mrg if (inherited_binfo && rval 2501 1.1 mrg && DECL_P (*inheriting_ctor) && DECL_P (rval) 2502 1.1 mrg && DECL_CONTEXT (*inheriting_ctor) == DECL_CONTEXT (rval)) 2503 1.1 mrg *inheriting_ctor = DECL_CLONED_FUNCTION (rval); 2504 1.1 mrg 2505 1.1 mrg process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p, 2506 1.1 mrg constexpr_p, diag, BINFO_TYPE (base_binfo)); 2507 1.1 mrg if (SFK_CTOR_P (sfk) 2508 1.1 mrg && (!BINFO_VIRTUAL_P (base_binfo) 2509 1.1 mrg || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))) 2510 1.1 mrg { 2511 1.1 mrg /* In a constructor we also need to check the subobject 2512 1.1 mrg destructors for cleanup of partially constructed objects. */ 2513 1.1 mrg tree dtor = locate_fn_flags (base_binfo, complete_dtor_identifier, 2514 1.1 mrg NULL_TREE, flags, 2515 1.1 mrg diag ? tf_warning_or_error : tf_none); 2516 1.1 mrg /* Note that we don't pass down trivial_p; the subobject 2517 1.1 mrg destructors don't affect triviality of the constructor. Nor 2518 1.1 mrg do they affect constexpr-ness (a constant expression doesn't 2519 1.1 mrg throw) or exception-specification (a throw from one of the 2520 1.1 mrg dtors would be a double-fault). */ 2521 1.1 mrg process_subob_fn (dtor, sfk, NULL, NULL, deleted_p, NULL, false, 2522 1.1 mrg BINFO_TYPE (base_binfo), /*dtor_from_ctor*/true); 2523 1.1 mrg } 2524 1.1 mrg 2525 1.1 mrg return rval; 2526 1.1 mrg } 2527 1.1 mrg 2528 1.1 mrg /* The caller wants to generate an implicit declaration of SFK for 2529 1.1 mrg CTYPE which is const if relevant and CONST_P is set. If SPEC_P, 2530 1.1 mrg TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their 2531 1.1 mrg referent appropriately. If DIAG is true, we're either being called 2532 1.1 mrg from maybe_explain_implicit_delete to give errors, or if 2533 1.1 mrg CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */ 2534 1.1 mrg 2535 1.1 mrg static void 2536 1.1 mrg synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p, 2537 1.1 mrg tree *spec_p, bool *trivial_p, bool *deleted_p, 2538 1.1 mrg bool *constexpr_p, bool diag, 2539 1.1 mrg tree *inheriting_ctor, tree inherited_parms) 2540 1.1 mrg { 2541 1.1 mrg tree binfo, base_binfo; 2542 1.1 mrg int i; 2543 1.1 mrg 2544 1.1 mrg /* SFK must be exactly one category. */ 2545 1.1 mrg gcc_checking_assert (SFK_DTOR_P(sfk) + SFK_CTOR_P(sfk) 2546 1.1 mrg + SFK_ASSIGN_P(sfk) == 1); 2547 1.1 mrg 2548 1.1 mrg if (spec_p) 2549 1.1 mrg *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec); 2550 1.1 mrg 2551 1.1 mrg if (deleted_p) 2552 1.1 mrg { 2553 1.1 mrg /* "The closure type associated with a lambda-expression has a deleted 2554 1.1 mrg default constructor and a deleted copy assignment operator." 2555 1.1 mrg This is diagnosed in maybe_explain_implicit_delete. 2556 1.1 mrg In C++20, only lambda-expressions with lambda-captures have those 2557 1.1 mrg deleted. */ 2558 1.1 mrg if (LAMBDA_TYPE_P (ctype) 2559 1.1 mrg && (sfk == sfk_constructor || sfk == sfk_copy_assignment) 2560 1.1 mrg && (cxx_dialect < cxx20 2561 1.1 mrg || LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (ctype)) 2562 1.1 mrg || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE 2563 1.1 mrg (CLASSTYPE_LAMBDA_EXPR (ctype)) != CPLD_NONE)) 2564 1.1 mrg { 2565 1.1 mrg *deleted_p = true; 2566 1.1 mrg return; 2567 1.1 mrg } 2568 1.1 mrg 2569 1.1 mrg *deleted_p = false; 2570 1.1 mrg } 2571 1.1 mrg 2572 1.1 mrg bool check_vdtor = false; 2573 1.1 mrg tree fnname; 2574 1.1 mrg 2575 1.1 mrg if (SFK_DTOR_P (sfk)) 2576 1.1 mrg { 2577 1.1 mrg check_vdtor = true; 2578 1.1 mrg /* The synthesized method will call base dtors, but check complete 2579 1.1 mrg here to avoid having to deal with VTT. */ 2580 1.1 mrg fnname = complete_dtor_identifier; 2581 1.1 mrg } 2582 1.1 mrg else if (SFK_ASSIGN_P (sfk)) 2583 1.1 mrg fnname = assign_op_identifier; 2584 1.1 mrg else 2585 1.1 mrg fnname = complete_ctor_identifier; 2586 1.1 mrg 2587 1.1 mrg gcc_assert ((sfk == sfk_inheriting_constructor) 2588 1.1 mrg == (inheriting_ctor && *inheriting_ctor != NULL_TREE)); 2589 1.1 mrg 2590 1.1 mrg /* If that user-written default constructor would satisfy the 2591 1.1 mrg requirements of a constexpr constructor (7.1.5), the 2592 1.1 mrg implicitly-defined default constructor is constexpr. 2593 1.1 mrg 2594 1.1 mrg The implicitly-defined copy/move assignment operator is constexpr if 2595 1.1 mrg - X is a literal type, and 2596 1.1 mrg - the assignment operator selected to copy/move each direct base class 2597 1.1 mrg subobject is a constexpr function, and 2598 1.1 mrg - for each non-static data member of X that is of class type (or array 2599 1.1 mrg thereof), the assignment operator selected to copy/move that 2600 1.1 mrg member is a constexpr function. */ 2601 1.1 mrg if (constexpr_p) 2602 1.1 mrg *constexpr_p = (SFK_CTOR_P (sfk) 2603 1.1 mrg || (SFK_ASSIGN_P (sfk) && cxx_dialect >= cxx14) 2604 1.1 mrg || (SFK_DTOR_P (sfk) && cxx_dialect >= cxx20)); 2605 1.1 mrg 2606 1.1 mrg bool expected_trivial = type_has_trivial_fn (ctype, sfk); 2607 1.1 mrg if (trivial_p) 2608 1.1 mrg *trivial_p = expected_trivial; 2609 1.1 mrg 2610 1.1 mrg /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base 2611 1.1 mrg class versions and other properties of the type. But a subobject 2612 1.1 mrg class can be trivially copyable and yet have overload resolution 2613 1.1 mrg choose a template constructor for initialization, depending on 2614 1.1 mrg rvalueness and cv-quals. And furthermore, a member in a base might 2615 1.1 mrg be trivial but deleted or otherwise not callable. So we can't exit 2616 1.1 mrg early in C++0x. The same considerations apply in C++98/03, but 2617 1.1 mrg there the definition of triviality does not consider overload 2618 1.1 mrg resolution, so a constructor can be trivial even if it would otherwise 2619 1.1 mrg call a non-trivial constructor. */ 2620 1.1 mrg if (expected_trivial 2621 1.1 mrg && (!(SFK_COPY_P (sfk) || SFK_MOVE_P (sfk)) || cxx_dialect < cxx11)) 2622 1.1 mrg { 2623 1.1 mrg if (constexpr_p && sfk == sfk_constructor) 2624 1.1 mrg { 2625 1.1 mrg bool cx = trivial_default_constructor_is_constexpr (ctype); 2626 1.1 mrg *constexpr_p = cx; 2627 1.1 mrg if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE) 2628 1.1 mrg /* A trivial constructor doesn't have any NSDMI. */ 2629 1.1 mrg inform (input_location, "defaulted default constructor does " 2630 1.1 mrg "not initialize any non-static data member"); 2631 1.1 mrg } 2632 1.1 mrg if (!diag && cxx_dialect < cxx11) 2633 1.1 mrg return; 2634 1.1 mrg } 2635 1.1 mrg 2636 1.1 mrg ++cp_unevaluated_operand; 2637 1.1 mrg ++c_inhibit_evaluation_warnings; 2638 1.1 mrg push_deferring_access_checks (dk_no_deferred); 2639 1.1 mrg 2640 1.1 mrg tree scope = push_scope (ctype); 2641 1.1 mrg 2642 1.1 mrg int flags = LOOKUP_NORMAL | LOOKUP_SPECULATIVE; 2643 1.1 mrg if (sfk != sfk_inheriting_constructor) 2644 1.1 mrg flags |= LOOKUP_DEFAULTED; 2645 1.1 mrg 2646 1.1 mrg tsubst_flags_t complain = diag ? tf_warning_or_error : tf_none; 2647 1.1 mrg if (diag && spec_p) 2648 1.1 mrg /* We're in get_defaulted_eh_spec; we don't actually want any walking 2649 1.1 mrg diagnostics, we just want complain set. */ 2650 1.1 mrg diag = false; 2651 1.1 mrg int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED; 2652 1.1 mrg 2653 1.1 mrg for (binfo = TYPE_BINFO (ctype), i = 0; 2654 1.1 mrg BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 2655 1.1 mrg { 2656 1.1 mrg if (!SFK_ASSIGN_P (sfk) && BINFO_VIRTUAL_P (base_binfo)) 2657 1.1 mrg /* We'll handle virtual bases below. */ 2658 1.1 mrg continue; 2659 1.1 mrg 2660 1.1 mrg tree fn = synthesized_method_base_walk (binfo, base_binfo, 2661 1.1 mrg sfk, fnname, quals, 2662 1.1 mrg inheriting_ctor, inherited_parms, 2663 1.1 mrg flags, diag, spec_p, trivial_p, 2664 1.1 mrg deleted_p, constexpr_p); 2665 1.1 mrg 2666 1.1 mrg if (diag && SFK_ASSIGN_P (sfk) && SFK_MOVE_P (sfk) 2667 1.1 mrg && BINFO_VIRTUAL_P (base_binfo) 2668 1.1 mrg && fn && TREE_CODE (fn) == FUNCTION_DECL 2669 1.1 mrg && move_fn_p (fn) && !trivial_fn_p (fn) 2670 1.1 mrg && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo))) 2671 1.1 mrg warning (OPT_Wvirtual_move_assign, 2672 1.1 mrg "defaulted move assignment for %qT calls a non-trivial " 2673 1.1 mrg "move assignment operator for virtual base %qT", 2674 1.1 mrg ctype, BINFO_TYPE (base_binfo)); 2675 1.1 mrg 2676 1.1 mrg if (check_vdtor && type_has_virtual_destructor (BINFO_TYPE (base_binfo))) 2677 1.1 mrg { 2678 1.1 mrg /* Unlike for base ctor/op=/dtor, for operator delete it's fine 2679 1.1 mrg to have a null fn (no class-specific op delete). */ 2680 1.1 mrg fn = locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR), 2681 1.1 mrg ptr_type_node, flags, tf_none); 2682 1.1 mrg if (fn && fn == error_mark_node) 2683 1.1 mrg { 2684 1.1 mrg if (complain & tf_error) 2685 1.1 mrg locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR), 2686 1.1 mrg ptr_type_node, flags, complain); 2687 1.1 mrg if (deleted_p) 2688 1.1 mrg *deleted_p = true; 2689 1.1 mrg } 2690 1.1 mrg check_vdtor = false; 2691 1.1 mrg } 2692 1.1 mrg } 2693 1.1 mrg 2694 1.1 mrg vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (ctype); 2695 1.1 mrg if (SFK_ASSIGN_P (sfk)) 2696 1.1 mrg /* Already examined vbases above. */; 2697 1.1 mrg else if (vec_safe_is_empty (vbases)) 2698 1.1 mrg /* No virtual bases to worry about. */; 2699 1.1 mrg else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14 2700 1.1 mrg /* DR 1658 specifies that vbases of abstract classes are 2701 1.1 mrg ignored for both ctors and dtors. Except DR 2336 2702 1.1 mrg overrides that skipping when determing the eh-spec of a 2703 1.1 mrg virtual destructor. */ 2704 1.1 mrg && sfk != sfk_virtual_destructor) 2705 1.1 mrg /* Vbase cdtors are not relevant. */; 2706 1.1 mrg else 2707 1.1 mrg { 2708 1.1 mrg if (constexpr_p) 2709 1.1 mrg *constexpr_p = false; 2710 1.1 mrg 2711 1.1 mrg FOR_EACH_VEC_ELT (*vbases, i, base_binfo) 2712 1.1 mrg synthesized_method_base_walk (binfo, base_binfo, sfk, fnname, quals, 2713 1.1 mrg inheriting_ctor, inherited_parms, 2714 1.1 mrg flags, diag, 2715 1.1 mrg spec_p, trivial_p, deleted_p, constexpr_p); 2716 1.1 mrg } 2717 1.1 mrg 2718 1.1 mrg /* Now handle the non-static data members. */ 2719 1.1 mrg walk_field_subobs (TYPE_FIELDS (ctype), sfk, fnname, quals, 2720 1.1 mrg spec_p, trivial_p, deleted_p, constexpr_p, 2721 1.1 mrg diag, flags, complain, /*dtor_from_ctor*/false); 2722 1.1 mrg if (SFK_CTOR_P (sfk)) 2723 1.1 mrg walk_field_subobs (TYPE_FIELDS (ctype), sfk_destructor, 2724 1.1 mrg complete_dtor_identifier, TYPE_UNQUALIFIED, 2725 1.1 mrg NULL, NULL, deleted_p, NULL, 2726 1.1 mrg false, flags, complain, /*dtor_from_ctor*/true); 2727 1.1 mrg 2728 1.1 mrg pop_scope (scope); 2729 1.1 mrg 2730 1.1 mrg pop_deferring_access_checks (); 2731 1.1 mrg --cp_unevaluated_operand; 2732 1.1 mrg --c_inhibit_evaluation_warnings; 2733 1.1 mrg } 2734 1.1 mrg 2735 1.1 mrg /* DECL is a defaulted function whose exception specification is now 2736 1.1 mrg needed. Return what it should be. */ 2737 1.1 mrg 2738 1.1 mrg tree 2739 1.1 mrg get_defaulted_eh_spec (tree decl, tsubst_flags_t complain) 2740 1.1 mrg { 2741 1.1 mrg /* For DECL_MAYBE_DELETED this should already have been handled by 2742 1.1 mrg synthesize_method. */ 2743 1.1 mrg gcc_assert (!DECL_MAYBE_DELETED (decl)); 2744 1.1 mrg 2745 1.1 mrg if (DECL_CLONED_FUNCTION_P (decl)) 2746 1.1 mrg decl = DECL_CLONED_FUNCTION (decl); 2747 1.1 mrg special_function_kind sfk = special_function_p (decl); 2748 1.1 mrg tree ctype = DECL_CONTEXT (decl); 2749 1.1 mrg tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl); 2750 1.1 mrg tree parm_type = TREE_VALUE (parms); 2751 1.1 mrg bool const_p = CP_TYPE_CONST_P (non_reference (parm_type)); 2752 1.1 mrg tree spec = empty_except_spec; 2753 1.1 mrg bool diag = !DECL_DELETED_FN (decl) && (complain & tf_error); 2754 1.1 mrg tree inh = DECL_INHERITED_CTOR (decl); 2755 1.1 mrg if (SFK_DTOR_P (sfk) && DECL_VIRTUAL_P (decl)) 2756 1.1 mrg /* We have to examine virtual bases even if abstract. */ 2757 1.1 mrg sfk = sfk_virtual_destructor; 2758 1.1 mrg bool pushed = false; 2759 1.1 mrg if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) 2760 1.1 mrg pushed = push_tinst_level (decl); 2761 1.1 mrg synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL, 2762 1.1 mrg NULL, diag, &inh, parms); 2763 1.1 mrg if (pushed) 2764 1.1 mrg pop_tinst_level (); 2765 1.1 mrg return spec; 2766 1.1 mrg } 2767 1.1 mrg 2768 1.1 mrg /* DECL is a deleted function. If it's implicitly deleted, explain why and 2769 1.1 mrg return true; else return false. */ 2770 1.1 mrg 2771 1.1 mrg bool 2772 1.1 mrg maybe_explain_implicit_delete (tree decl) 2773 1.1 mrg { 2774 1.1 mrg /* If decl is a clone, get the primary variant. */ 2775 1.1 mrg decl = DECL_ORIGIN (decl); 2776 1.1 mrg gcc_assert (DECL_DELETED_FN (decl)); 2777 1.1 mrg if (DECL_DEFAULTED_FN (decl)) 2778 1.1 mrg { 2779 1.1 mrg /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */ 2780 1.1 mrg static hash_set<tree> *explained; 2781 1.1 mrg 2782 1.1 mrg special_function_kind sfk; 2783 1.1 mrg location_t loc; 2784 1.1 mrg bool informed; 2785 1.1 mrg tree ctype; 2786 1.1 mrg 2787 1.1 mrg if (!explained) 2788 1.1 mrg explained = new hash_set<tree>; 2789 1.1 mrg if (explained->add (decl)) 2790 1.1 mrg return true; 2791 1.1 mrg 2792 1.1 mrg sfk = special_function_p (decl); 2793 1.1 mrg ctype = DECL_CONTEXT (decl); 2794 1.1 mrg loc = input_location; 2795 1.1 mrg input_location = DECL_SOURCE_LOCATION (decl); 2796 1.1 mrg 2797 1.1 mrg informed = false; 2798 1.1 mrg if (LAMBDA_TYPE_P (ctype)) 2799 1.1 mrg { 2800 1.1 mrg informed = true; 2801 1.1 mrg if (sfk == sfk_constructor) 2802 1.1 mrg inform (DECL_SOURCE_LOCATION (decl), 2803 1.1 mrg "a lambda closure type has a deleted default constructor"); 2804 1.1 mrg else if (sfk == sfk_copy_assignment) 2805 1.1 mrg inform (DECL_SOURCE_LOCATION (decl), 2806 1.1 mrg "a lambda closure type has a deleted copy assignment operator"); 2807 1.1 mrg else 2808 1.1 mrg informed = false; 2809 1.1 mrg } 2810 1.1 mrg else if (DECL_ARTIFICIAL (decl) 2811 1.1 mrg && (sfk == sfk_copy_assignment || sfk == sfk_copy_constructor) 2812 1.1 mrg && classtype_has_move_assign_or_move_ctor_p (ctype, true)) 2813 1.1 mrg { 2814 1.1 mrg inform (DECL_SOURCE_LOCATION (decl), 2815 1.1 mrg "%q#D is implicitly declared as deleted because %qT " 2816 1.1 mrg "declares a move constructor or move assignment operator", 2817 1.1 mrg decl, ctype); 2818 1.1 mrg informed = true; 2819 1.1 mrg } 2820 1.1 mrg else if (sfk == sfk_inheriting_constructor) 2821 1.1 mrg { 2822 1.1 mrg tree binfo = inherited_ctor_binfo (decl); 2823 1.1 mrg if (TREE_CODE (binfo) != TREE_BINFO) 2824 1.1 mrg { 2825 1.1 mrg inform (DECL_SOURCE_LOCATION (decl), 2826 1.1 mrg "%q#D inherits from multiple base subobjects", 2827 1.1 mrg decl); 2828 1.1 mrg informed = true; 2829 1.1 mrg } 2830 1.1 mrg } 2831 1.1 mrg if (!informed && sfk == sfk_comparison) 2832 1.1 mrg { 2833 1.1 mrg inform (DECL_SOURCE_LOCATION (decl), 2834 1.1 mrg "%q#D is implicitly deleted because the default " 2835 1.1 mrg "definition would be ill-formed:", decl); 2836 1.1 mrg build_comparison_op (decl, false, tf_warning_or_error); 2837 1.1 mrg } 2838 1.1 mrg else if (!informed) 2839 1.1 mrg { 2840 1.1 mrg tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl); 2841 1.1 mrg bool const_p = false; 2842 1.1 mrg if (parms) 2843 1.1 mrg { 2844 1.1 mrg tree parm_type = TREE_VALUE (parms); 2845 1.1 mrg const_p = CP_TYPE_CONST_P (non_reference (parm_type)); 2846 1.1 mrg } 2847 1.1 mrg tree raises = NULL_TREE; 2848 1.1 mrg bool deleted_p = false; 2849 1.1 mrg tree scope = push_scope (ctype); 2850 1.1 mrg tree inh = DECL_INHERITED_CTOR (decl); 2851 1.1 mrg 2852 1.1 mrg synthesized_method_walk (ctype, sfk, const_p, 2853 1.1 mrg &raises, NULL, &deleted_p, NULL, false, 2854 1.1 mrg &inh, parms); 2855 1.1 mrg if (deleted_p) 2856 1.1 mrg { 2857 1.1 mrg inform (DECL_SOURCE_LOCATION (decl), 2858 1.1 mrg "%q#D is implicitly deleted because the default " 2859 1.1 mrg "definition would be ill-formed:", decl); 2860 1.1 mrg synthesized_method_walk (ctype, sfk, const_p, 2861 1.1 mrg NULL, NULL, &deleted_p, NULL, true, 2862 1.1 mrg &inh, parms); 2863 1.1 mrg } 2864 1.1 mrg else if (!comp_except_specs 2865 1.1 mrg (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)), 2866 1.1 mrg raises, ce_normal)) 2867 1.1 mrg inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly " 2868 1.1 mrg "deleted because its exception-specification does not " 2869 1.1 mrg "match the implicit exception-specification %qX", 2870 1.1 mrg decl, raises); 2871 1.1 mrg else if (flag_checking) 2872 1.1 mrg gcc_unreachable (); 2873 1.1 mrg 2874 1.1 mrg pop_scope (scope); 2875 1.1 mrg } 2876 1.1 mrg 2877 1.1 mrg input_location = loc; 2878 1.1 mrg return true; 2879 1.1 mrg } 2880 1.1 mrg return false; 2881 1.1 mrg } 2882 1.1 mrg 2883 1.1 mrg /* DECL is a defaulted function which was declared constexpr. Explain why 2884 1.1 mrg it can't be constexpr. */ 2885 1.1 mrg 2886 1.1 mrg void 2887 1.1 mrg explain_implicit_non_constexpr (tree decl) 2888 1.1 mrg { 2889 1.1 mrg tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl); 2890 1.1 mrg bool const_p = CP_TYPE_CONST_P (non_reference (TREE_VALUE (parms))); 2891 1.1 mrg tree inh = DECL_INHERITED_CTOR (decl); 2892 1.1 mrg bool dummy; 2893 1.1 mrg special_function_kind sfk = special_function_p (decl); 2894 1.1 mrg if (sfk == sfk_comparison) 2895 1.1 mrg { 2896 1.1 mrg DECL_DECLARED_CONSTEXPR_P (decl) = true; 2897 1.1 mrg build_comparison_op (decl, false, tf_warning_or_error); 2898 1.1 mrg DECL_DECLARED_CONSTEXPR_P (decl) = false; 2899 1.1 mrg } 2900 1.1 mrg else 2901 1.1 mrg synthesized_method_walk (DECL_CLASS_CONTEXT (decl), 2902 1.1 mrg sfk, const_p, 2903 1.1 mrg NULL, NULL, NULL, &dummy, true, 2904 1.1 mrg &inh, parms); 2905 1.1 mrg } 2906 1.1 mrg 2907 1.1 mrg /* DECL is an instantiation of an inheriting constructor template. Deduce 2908 1.1 mrg the correct exception-specification and deletedness for this particular 2909 1.1 mrg specialization. Return true if the deduction succeeds; false otherwise. */ 2910 1.1 mrg 2911 1.1 mrg bool 2912 1.1 mrg deduce_inheriting_ctor (tree decl) 2913 1.1 mrg { 2914 1.1 mrg decl = DECL_ORIGIN (decl); 2915 1.1 mrg gcc_assert (DECL_INHERITED_CTOR (decl)); 2916 1.1 mrg tree spec; 2917 1.1 mrg bool trivial, constexpr_, deleted; 2918 1.1 mrg tree inh = DECL_INHERITED_CTOR (decl); 2919 1.1 mrg synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor, 2920 1.1 mrg false, &spec, &trivial, &deleted, &constexpr_, 2921 1.1 mrg /*diag*/false, 2922 1.1 mrg &inh, 2923 1.1 mrg FUNCTION_FIRST_USER_PARMTYPE (decl)); 2924 1.1 mrg if (spec == error_mark_node) 2925 1.1 mrg return false; 2926 1.1 mrg if (TREE_CODE (inherited_ctor_binfo (decl)) != TREE_BINFO) 2927 1.1 mrg /* Inherited the same constructor from different base subobjects. */ 2928 1.1 mrg deleted = true; 2929 1.1 mrg DECL_DELETED_FN (decl) = deleted; 2930 1.1 mrg TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec); 2931 1.1 mrg SET_DECL_INHERITED_CTOR (decl, inh); 2932 1.1 mrg 2933 1.1 mrg tree clone; 2934 1.1 mrg FOR_EACH_CLONE (clone, decl) 2935 1.1 mrg { 2936 1.1 mrg DECL_DELETED_FN (clone) = deleted; 2937 1.1 mrg TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec); 2938 1.1 mrg SET_DECL_INHERITED_CTOR (clone, inh); 2939 1.1 mrg } 2940 1.1 mrg 2941 1.1 mrg return true; 2942 1.1 mrg } 2943 1.1 mrg 2944 1.1 mrg /* Implicitly declare the special function indicated by KIND, as a 2945 1.1 mrg member of TYPE. For copy constructors and assignment operators, 2946 1.1 mrg CONST_P indicates whether these functions should take a const 2947 1.1 mrg reference argument or a non-const reference. 2948 1.1 mrg Returns the FUNCTION_DECL for the implicitly declared function. */ 2949 1.1 mrg 2950 1.1 mrg tree 2951 1.1 mrg implicitly_declare_fn (special_function_kind kind, tree type, 2952 1.1 mrg bool const_p, tree pattern_fn, 2953 1.1 mrg tree inherited_parms) 2954 1.1 mrg { 2955 1.1 mrg tree fn; 2956 1.1 mrg tree parameter_types = void_list_node; 2957 1.1 mrg tree return_type; 2958 1.1 mrg tree fn_type; 2959 1.1 mrg tree raises = empty_except_spec; 2960 1.1 mrg tree rhs_parm_type = NULL_TREE; 2961 1.1 mrg tree this_parm; 2962 1.1 mrg tree name; 2963 1.1 mrg HOST_WIDE_INT saved_processing_template_decl; 2964 1.1 mrg bool deleted_p = false; 2965 1.1 mrg bool constexpr_p = false; 2966 1.1 mrg tree inherited_ctor = (kind == sfk_inheriting_constructor 2967 1.1 mrg ? pattern_fn : NULL_TREE); 2968 1.1 mrg 2969 1.1 mrg /* Because we create declarations for implicitly declared functions 2970 1.1 mrg lazily, we may be creating the declaration for a member of TYPE 2971 1.1 mrg while in some completely different context. However, TYPE will 2972 1.1 mrg never be a dependent class (because we never want to do lookups 2973 1.1 mrg for implicitly defined functions in a dependent class). */ 2974 1.1 mrg gcc_assert (!dependent_type_p (type)); 2975 1.1 mrg 2976 1.1 mrg /* If the member-specification does not explicitly declare any member or 2977 1.1 mrg friend named operator==, an == operator function is declared 2978 1.1 mrg implicitly for each three-way comparison operator function defined as 2979 1.1 mrg defaulted in the member-specification, with the same access and 2980 1.1 mrg function-definition and in the same class scope as the respective 2981 1.1 mrg three-way comparison operator function, except that the return type is 2982 1.1 mrg replaced with bool and the declarator-id is replaced with 2983 1.1 mrg operator==. 2984 1.1 mrg 2985 1.1 mrg [Note: Such an implicitly-declared == operator for a class X is 2986 1.1 mrg defined as defaulted in the definition of X and has the same 2987 1.1 mrg parameter-declaration-clause and trailing requires-clause as the 2988 1.1 mrg respective three-way comparison operator. It is declared with friend, 2989 1.1 mrg virtual, constexpr, or consteval if the three-way comparison operator 2990 1.1 mrg function is so declared. If the three-way comparison operator function 2991 1.1 mrg has no noexcept-specifier, the implicitly-declared == operator 2992 1.1 mrg function has an implicit exception specification (14.5) that may 2993 1.1 mrg differ from the implicit exception specification of the three-way 2994 1.1 mrg comparison operator function. --end note] */ 2995 1.1 mrg if (kind == sfk_comparison) 2996 1.1 mrg { 2997 1.1 mrg fn = copy_operator_fn (pattern_fn, EQ_EXPR); 2998 1.1 mrg DECL_ARTIFICIAL (fn) = 1; 2999 1.1 mrg TREE_TYPE (fn) = change_return_type (boolean_type_node, TREE_TYPE (fn)); 3000 1.1 mrg return fn; 3001 1.1 mrg } 3002 1.1 mrg 3003 1.1 mrg /* Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here 3004 1.1 mrg because we only create clones for constructors and destructors 3005 1.1 mrg when not in a template. */ 3006 1.1 mrg saved_processing_template_decl = processing_template_decl; 3007 1.1 mrg processing_template_decl = 0; 3008 1.1 mrg 3009 1.1 mrg type = TYPE_MAIN_VARIANT (type); 3010 1.1 mrg 3011 1.1 mrg if (targetm.cxx.cdtor_returns_this ()) 3012 1.1 mrg { 3013 1.1 mrg if (kind == sfk_destructor) 3014 1.1 mrg /* See comment in check_special_function_return_type. */ 3015 1.1 mrg return_type = build_pointer_type (void_type_node); 3016 1.1 mrg else 3017 1.1 mrg return_type = build_pointer_type (type); 3018 1.1 mrg } 3019 1.1 mrg else 3020 1.1 mrg return_type = void_type_node; 3021 1.1 mrg 3022 1.1 mrg int this_quals = TYPE_UNQUALIFIED; 3023 1.1 mrg switch (kind) 3024 1.1 mrg { 3025 1.1 mrg case sfk_destructor: 3026 1.1 mrg /* Destructor. */ 3027 1.1 mrg name = dtor_identifier; 3028 1.1 mrg break; 3029 1.1 mrg 3030 1.1 mrg case sfk_constructor: 3031 1.1 mrg /* Default constructor. */ 3032 1.1 mrg name = ctor_identifier; 3033 1.1 mrg break; 3034 1.1 mrg 3035 1.1 mrg case sfk_copy_constructor: 3036 1.1 mrg case sfk_copy_assignment: 3037 1.1 mrg case sfk_move_constructor: 3038 1.1 mrg case sfk_move_assignment: 3039 1.1 mrg case sfk_inheriting_constructor: 3040 1.1 mrg { 3041 1.1 mrg if (kind == sfk_copy_assignment 3042 1.1 mrg || kind == sfk_move_assignment) 3043 1.1 mrg { 3044 1.1 mrg return_type = build_reference_type (type); 3045 1.1 mrg name = assign_op_identifier; 3046 1.1 mrg } 3047 1.1 mrg else 3048 1.1 mrg name = ctor_identifier; 3049 1.1 mrg 3050 1.1 mrg if (kind == sfk_inheriting_constructor) 3051 1.1 mrg parameter_types = inherited_parms; 3052 1.1 mrg else 3053 1.1 mrg { 3054 1.1 mrg if (const_p) 3055 1.1 mrg rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST); 3056 1.1 mrg else 3057 1.1 mrg rhs_parm_type = type; 3058 1.1 mrg bool move_p = (kind == sfk_move_assignment 3059 1.1 mrg || kind == sfk_move_constructor); 3060 1.1 mrg rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p); 3061 1.1 mrg 3062 1.1 mrg parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types); 3063 1.1 mrg } 3064 1.1 mrg break; 3065 1.1 mrg } 3066 1.1 mrg 3067 1.1 mrg default: 3068 1.1 mrg gcc_unreachable (); 3069 1.1 mrg } 3070 1.1 mrg 3071 1.1 mrg bool trivial_p = false; 3072 1.1 mrg 3073 1.1 mrg if (inherited_ctor) 3074 1.1 mrg { 3075 1.1 mrg /* For an inheriting constructor, just copy these flags from the 3076 1.1 mrg inherited constructor until deduce_inheriting_ctor. */ 3077 1.1 mrg raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor)); 3078 1.1 mrg deleted_p = DECL_DELETED_FN (inherited_ctor); 3079 1.1 mrg constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor); 3080 1.1 mrg } 3081 1.1 mrg else if (cxx_dialect >= cxx11) 3082 1.1 mrg { 3083 1.1 mrg raises = noexcept_deferred_spec; 3084 1.1 mrg synthesized_method_walk (type, kind, const_p, NULL, &trivial_p, 3085 1.1 mrg &deleted_p, &constexpr_p, false, 3086 1.1 mrg &inherited_ctor, inherited_parms); 3087 1.1 mrg } 3088 1.1 mrg else 3089 1.1 mrg synthesized_method_walk (type, kind, const_p, &raises, &trivial_p, 3090 1.1 mrg &deleted_p, &constexpr_p, false, 3091 1.1 mrg &inherited_ctor, inherited_parms); 3092 1.1 mrg /* Don't bother marking a deleted constructor as constexpr. */ 3093 1.1 mrg if (deleted_p) 3094 1.1 mrg constexpr_p = false; 3095 1.1 mrg /* A trivial copy/move constructor is also a constexpr constructor, 3096 1.1 mrg unless the class has virtual bases (7.1.5p4). */ 3097 1.1 mrg else if (trivial_p 3098 1.1 mrg && cxx_dialect >= cxx11 3099 1.1 mrg && (kind == sfk_copy_constructor 3100 1.1 mrg || kind == sfk_move_constructor) 3101 1.1 mrg && !CLASSTYPE_VBASECLASSES (type)) 3102 1.1 mrg gcc_assert (constexpr_p); 3103 1.1 mrg 3104 1.1 mrg if (!trivial_p && type_has_trivial_fn (type, kind)) 3105 1.1 mrg type_set_nontrivial_flag (type, kind); 3106 1.1 mrg 3107 1.1 mrg /* Create the function. */ 3108 1.1 mrg tree this_type = cp_build_qualified_type (type, this_quals); 3109 1.1 mrg fn_type = build_method_type_directly (this_type, return_type, 3110 1.1 mrg parameter_types); 3111 1.1 mrg 3112 1.1 mrg if (raises) 3113 1.1 mrg { 3114 1.1 mrg if (raises != error_mark_node) 3115 1.1 mrg fn_type = build_exception_variant (fn_type, raises); 3116 1.1 mrg else 3117 1.1 mrg { 3118 1.1 mrg /* Can happen, e.g., in C++98 mode for an ill-formed non-static data 3119 1.1 mrg member initializer (c++/89914). Also, in C++98, we might have 3120 1.1 mrg failed to deduce RAISES, so try again but complain this time. */ 3121 1.1 mrg if (cxx_dialect < cxx11) 3122 1.1 mrg synthesized_method_walk (type, kind, const_p, &raises, nullptr, 3123 1.1 mrg nullptr, nullptr, /*diag=*/true, 3124 1.1 mrg &inherited_ctor, inherited_parms); 3125 1.1 mrg /* We should have seen an error at this point. */ 3126 1.1 mrg gcc_assert (seen_error ()); 3127 1.1 mrg } 3128 1.1 mrg } 3129 1.1 mrg fn = build_lang_decl (FUNCTION_DECL, name, fn_type); 3130 1.1 mrg if (kind != sfk_inheriting_constructor) 3131 1.1 mrg DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type)); 3132 1.1 mrg 3133 1.1 mrg if (IDENTIFIER_OVL_OP_P (name)) 3134 1.1 mrg { 3135 1.1 mrg const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (name); 3136 1.1 mrg DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) = op->ovl_op_code; 3137 1.1 mrg } 3138 1.1 mrg else if (IDENTIFIER_CTOR_P (name)) 3139 1.1 mrg DECL_CXX_CONSTRUCTOR_P (fn) = true; 3140 1.1 mrg else if (IDENTIFIER_DTOR_P (name)) 3141 1.1 mrg DECL_CXX_DESTRUCTOR_P (fn) = true; 3142 1.1 mrg else 3143 1.1 mrg gcc_unreachable (); 3144 1.1 mrg 3145 1.1 mrg SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY); 3146 1.1 mrg 3147 1.1 mrg /* Create the explicit arguments. */ 3148 1.1 mrg if (rhs_parm_type) 3149 1.1 mrg { 3150 1.1 mrg /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we 3151 1.1 mrg want its type to be included in the mangled function 3152 1.1 mrg name. */ 3153 1.1 mrg tree decl = cp_build_parm_decl (fn, NULL_TREE, rhs_parm_type); 3154 1.1 mrg TREE_READONLY (decl) = 1; 3155 1.1 mrg retrofit_lang_decl (decl); 3156 1.1 mrg DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1; 3157 1.1 mrg DECL_ARGUMENTS (fn) = decl; 3158 1.1 mrg } 3159 1.1 mrg else if (kind == sfk_inheriting_constructor) 3160 1.1 mrg { 3161 1.1 mrg tree *p = &DECL_ARGUMENTS (fn); 3162 1.1 mrg int index = 1; 3163 1.1 mrg for (tree parm = inherited_parms; parm && parm != void_list_node; 3164 1.1 mrg parm = TREE_CHAIN (parm)) 3165 1.1 mrg { 3166 1.1 mrg *p = cp_build_parm_decl (fn, NULL_TREE, TREE_VALUE (parm)); 3167 1.1 mrg retrofit_lang_decl (*p); 3168 1.1 mrg DECL_PARM_LEVEL (*p) = 1; 3169 1.1 mrg DECL_PARM_INDEX (*p) = index++; 3170 1.1 mrg p = &DECL_CHAIN (*p); 3171 1.1 mrg } 3172 1.1 mrg SET_DECL_INHERITED_CTOR (fn, inherited_ctor); 3173 1.1 mrg DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor); 3174 1.1 mrg /* A constructor so declared has the same access as the corresponding 3175 1.1 mrg constructor in X. */ 3176 1.1 mrg TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor); 3177 1.1 mrg TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor); 3178 1.1 mrg /* Copy constexpr from the inherited constructor even if the 3179 1.1 mrg inheriting constructor doesn't satisfy the requirements. */ 3180 1.1 mrg constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor); 3181 1.1 mrg } 3182 1.1 mrg 3183 1.1 mrg /* Add the "this" parameter. */ 3184 1.1 mrg this_parm = build_this_parm (fn, fn_type, this_quals); 3185 1.1 mrg DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn); 3186 1.1 mrg DECL_ARGUMENTS (fn) = this_parm; 3187 1.1 mrg 3188 1.1 mrg grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL); 3189 1.1 mrg 3190 1.1 mrg DECL_IN_AGGR_P (fn) = 1; 3191 1.1 mrg DECL_ARTIFICIAL (fn) = 1; 3192 1.1 mrg DECL_DEFAULTED_FN (fn) = 1; 3193 1.1 mrg if (cxx_dialect >= cxx11) 3194 1.1 mrg { 3195 1.1 mrg DECL_DELETED_FN (fn) = deleted_p; 3196 1.1 mrg DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p; 3197 1.1 mrg } 3198 1.1 mrg DECL_EXTERNAL (fn) = true; 3199 1.1 mrg DECL_NOT_REALLY_EXTERN (fn) = 1; 3200 1.1 mrg DECL_DECLARED_INLINE_P (fn) = 1; 3201 1.1 mrg set_linkage_according_to_type (type, fn); 3202 1.1 mrg if (TREE_PUBLIC (fn)) 3203 1.1 mrg DECL_COMDAT (fn) = 1; 3204 1.1 mrg rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof); 3205 1.1 mrg gcc_assert (!TREE_USED (fn)); 3206 1.1 mrg 3207 1.1 mrg /* Propagate constraints from the inherited constructor. */ 3208 1.1 mrg if (flag_concepts && inherited_ctor) 3209 1.1 mrg if (tree orig_ci = get_constraints (inherited_ctor)) 3210 1.1 mrg { 3211 1.1 mrg tree new_ci = copy_node (orig_ci); 3212 1.1 mrg set_constraints (fn, new_ci); 3213 1.1 mrg } 3214 1.1 mrg 3215 1.1 mrg /* Restore PROCESSING_TEMPLATE_DECL. */ 3216 1.1 mrg processing_template_decl = saved_processing_template_decl; 3217 1.1 mrg 3218 1.1 mrg if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL) 3219 1.1 mrg fn = add_inherited_template_parms (fn, inherited_ctor); 3220 1.1 mrg 3221 1.1 mrg /* Warn about calling a non-trivial move assignment in a virtual base. */ 3222 1.1 mrg if (kind == sfk_move_assignment && !deleted_p && !trivial_p 3223 1.1 mrg && CLASSTYPE_VBASECLASSES (type)) 3224 1.1 mrg { 3225 1.1 mrg location_t loc = input_location; 3226 1.1 mrg input_location = DECL_SOURCE_LOCATION (fn); 3227 1.1 mrg synthesized_method_walk (type, kind, const_p, 3228 1.1 mrg NULL, NULL, NULL, NULL, true, 3229 1.1 mrg NULL, NULL_TREE); 3230 1.1 mrg input_location = loc; 3231 1.1 mrg } 3232 1.1 mrg 3233 1.1 mrg return fn; 3234 1.1 mrg } 3235 1.1 mrg 3236 1.1 mrg /* Gives any errors about defaulted functions which need to be deferred 3237 1.1 mrg until the containing class is complete. */ 3238 1.1 mrg 3239 1.1 mrg void 3240 1.1 mrg defaulted_late_check (tree fn) 3241 1.1 mrg { 3242 1.1 mrg /* Complain about invalid signature for defaulted fn. */ 3243 1.1 mrg tree ctx = DECL_CONTEXT (fn); 3244 1.1 mrg special_function_kind kind = special_function_p (fn); 3245 1.1 mrg 3246 1.1 mrg if (kind == sfk_comparison) 3247 1.1 mrg { 3248 1.1 mrg /* If the function was declared constexpr, check that the definition 3249 1.1 mrg qualifies. Otherwise we can define the function lazily. */ 3250 1.1 mrg if (DECL_DECLARED_CONSTEXPR_P (fn) && !DECL_INITIAL (fn)) 3251 1.1 mrg { 3252 1.1 mrg /* Prevent GC. */ 3253 1.1 mrg function_depth++; 3254 1.1 mrg synthesize_method (fn); 3255 1.1 mrg function_depth--; 3256 1.1 mrg } 3257 1.1 mrg return; 3258 1.1 mrg } 3259 1.1 mrg 3260 1.1 mrg bool fn_const_p = (copy_fn_p (fn) == 2); 3261 1.1 mrg tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p, 3262 1.1 mrg NULL, NULL); 3263 1.1 mrg tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn)); 3264 1.1 mrg 3265 1.1 mrg if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)), 3266 1.1 mrg TREE_TYPE (TREE_TYPE (implicit_fn))) 3267 1.1 mrg || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), 3268 1.1 mrg TYPE_ARG_TYPES (TREE_TYPE (implicit_fn)))) 3269 1.1 mrg { 3270 1.1 mrg error ("defaulted declaration %q+D does not match the " 3271 1.1 mrg "expected signature", fn); 3272 1.1 mrg inform (DECL_SOURCE_LOCATION (fn), 3273 1.1 mrg "expected signature: %qD", implicit_fn); 3274 1.1 mrg } 3275 1.1 mrg 3276 1.1 mrg if (DECL_DELETED_FN (implicit_fn)) 3277 1.1 mrg { 3278 1.1 mrg DECL_DELETED_FN (fn) = 1; 3279 1.1 mrg return; 3280 1.1 mrg } 3281 1.1 mrg 3282 1.1 mrg /* If a function is explicitly defaulted on its first declaration without an 3283 1.1 mrg exception-specification, it is implicitly considered to have the same 3284 1.1 mrg exception-specification as if it had been implicitly declared. */ 3285 1.1 mrg if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)) 3286 1.1 mrg && DECL_DEFAULTED_IN_CLASS_P (fn)) 3287 1.1 mrg TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec); 3288 1.1 mrg 3289 1.1 mrg if (DECL_DEFAULTED_IN_CLASS_P (fn) 3290 1.1 mrg && DECL_DECLARED_CONSTEXPR_P (implicit_fn)) 3291 1.1 mrg { 3292 1.1 mrg /* Hmm...should we do this for out-of-class too? Should it be OK to 3293 1.1 mrg add constexpr later like inline, rather than requiring 3294 1.1 mrg declarations to match? */ 3295 1.1 mrg DECL_DECLARED_CONSTEXPR_P (fn) = true; 3296 1.1 mrg if (kind == sfk_constructor) 3297 1.1 mrg TYPE_HAS_CONSTEXPR_CTOR (ctx) = true; 3298 1.1 mrg } 3299 1.1 mrg 3300 1.1 mrg if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn) 3301 1.1 mrg && DECL_DECLARED_CONSTEXPR_P (fn)) 3302 1.1 mrg { 3303 1.1 mrg if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx)) 3304 1.1 mrg { 3305 1.1 mrg error ("explicitly defaulted function %q+D cannot be declared " 3306 1.1 mrg "%qs because the implicit declaration is not %qs:", fn, 3307 1.1 mrg DECL_IMMEDIATE_FUNCTION_P (fn) ? "consteval" : "constexpr", 3308 1.1 mrg "constexpr"); 3309 1.1 mrg explain_implicit_non_constexpr (fn); 3310 1.1 mrg } 3311 1.1 mrg DECL_DECLARED_CONSTEXPR_P (fn) = false; 3312 1.1 mrg } 3313 1.1 mrg } 3314 1.1 mrg 3315 1.1 mrg /* Returns true iff FN can be explicitly defaulted, and gives any 3316 1.1 mrg errors if defaulting FN is ill-formed. */ 3317 1.1 mrg 3318 1.1 mrg bool 3319 1.1 mrg defaultable_fn_check (tree fn) 3320 1.1 mrg { 3321 1.1 mrg special_function_kind kind = sfk_none; 3322 1.1 mrg 3323 1.1 mrg if (template_parm_scope_p ()) 3324 1.1 mrg { 3325 1.1 mrg error ("a template cannot be defaulted"); 3326 1.1 mrg return false; 3327 1.1 mrg } 3328 1.1 mrg 3329 1.1 mrg if (DECL_CONSTRUCTOR_P (fn)) 3330 1.1 mrg { 3331 1.1 mrg if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node) 3332 1.1 mrg kind = sfk_constructor; 3333 1.1 mrg else if (copy_fn_p (fn) > 0 3334 1.1 mrg && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn)) 3335 1.1 mrg == void_list_node)) 3336 1.1 mrg kind = sfk_copy_constructor; 3337 1.1 mrg else if (move_fn_p (fn)) 3338 1.1 mrg kind = sfk_move_constructor; 3339 1.1 mrg } 3340 1.1 mrg else if (DECL_DESTRUCTOR_P (fn)) 3341 1.1 mrg kind = sfk_destructor; 3342 1.1 mrg else if (DECL_ASSIGNMENT_OPERATOR_P (fn) 3343 1.1 mrg && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)) 3344 1.1 mrg { 3345 1.1 mrg if (copy_fn_p (fn)) 3346 1.1 mrg kind = sfk_copy_assignment; 3347 1.1 mrg else if (move_fn_p (fn)) 3348 1.1 mrg kind = sfk_move_assignment; 3349 1.1 mrg } 3350 1.1 mrg else if (DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) >= OVL_OP_EQ_EXPR 3351 1.1 mrg && DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) <= OVL_OP_SPACESHIP_EXPR) 3352 1.1 mrg { 3353 1.1 mrg kind = sfk_comparison; 3354 1.1 mrg if (!early_check_defaulted_comparison (fn)) 3355 1.1 mrg return false; 3356 1.1 mrg } 3357 1.1 mrg 3358 1.1 mrg if (kind == sfk_none) 3359 1.1 mrg { 3360 1.1 mrg error ("%qD cannot be defaulted", fn); 3361 1.1 mrg return false; 3362 1.1 mrg } 3363 1.1 mrg else 3364 1.1 mrg { 3365 1.1 mrg for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn); 3366 1.1 mrg t && t != void_list_node; t = TREE_CHAIN (t)) 3367 1.1 mrg if (TREE_PURPOSE (t)) 3368 1.1 mrg { 3369 1.1 mrg error ("defaulted function %q+D with default argument", fn); 3370 1.1 mrg break; 3371 1.1 mrg } 3372 1.1 mrg 3373 1.1 mrg /* Avoid do_warn_unused_parameter warnings. */ 3374 1.1 mrg for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p)) 3375 1.1 mrg if (DECL_NAME (p)) 3376 1.1 mrg suppress_warning (p, OPT_Wunused_parameter); 3377 1.1 mrg 3378 1.1 mrg if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) 3379 1.1 mrg /* Defer checking. */; 3380 1.1 mrg else if (!processing_template_decl) 3381 1.1 mrg defaulted_late_check (fn); 3382 1.1 mrg 3383 1.1 mrg return true; 3384 1.1 mrg } 3385 1.1 mrg } 3386 1.1 mrg 3387 1.1 mrg /* Add an implicit declaration to TYPE for the kind of function 3388 1.1 mrg indicated by SFK. Return the FUNCTION_DECL for the new implicit 3389 1.1 mrg declaration. */ 3390 1.1 mrg 3391 1.1 mrg tree 3392 1.1 mrg lazily_declare_fn (special_function_kind sfk, tree type) 3393 1.1 mrg { 3394 1.1 mrg tree fn; 3395 1.1 mrg /* Whether or not the argument has a const reference type. */ 3396 1.1 mrg bool const_p = false; 3397 1.1 mrg 3398 1.1 mrg type = TYPE_MAIN_VARIANT (type); 3399 1.1 mrg 3400 1.1 mrg switch (sfk) 3401 1.1 mrg { 3402 1.1 mrg case sfk_constructor: 3403 1.1 mrg CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0; 3404 1.1 mrg break; 3405 1.1 mrg case sfk_copy_constructor: 3406 1.1 mrg const_p = TYPE_HAS_CONST_COPY_CTOR (type); 3407 1.1 mrg CLASSTYPE_LAZY_COPY_CTOR (type) = 0; 3408 1.1 mrg break; 3409 1.1 mrg case sfk_move_constructor: 3410 1.1 mrg CLASSTYPE_LAZY_MOVE_CTOR (type) = 0; 3411 1.1 mrg break; 3412 1.1 mrg case sfk_copy_assignment: 3413 1.1 mrg const_p = TYPE_HAS_CONST_COPY_ASSIGN (type); 3414 1.1 mrg CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0; 3415 1.1 mrg break; 3416 1.1 mrg case sfk_move_assignment: 3417 1.1 mrg CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0; 3418 1.1 mrg break; 3419 1.1 mrg case sfk_destructor: 3420 1.1 mrg CLASSTYPE_LAZY_DESTRUCTOR (type) = 0; 3421 1.1 mrg break; 3422 1.1 mrg default: 3423 1.1 mrg gcc_unreachable (); 3424 1.1 mrg } 3425 1.1 mrg 3426 1.1 mrg /* Declare the function. */ 3427 1.1 mrg fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL); 3428 1.1 mrg 3429 1.1 mrg /* [class.copy]/8 If the class definition declares a move constructor or 3430 1.1 mrg move assignment operator, the implicitly declared copy constructor is 3431 1.1 mrg defined as deleted.... */ 3432 1.1 mrg if ((sfk == sfk_copy_assignment || sfk == sfk_copy_constructor) 3433 1.1 mrg && cxx_dialect >= cxx11) 3434 1.1 mrg { 3435 1.1 mrg if (classtype_has_move_assign_or_move_ctor_p (type, true)) 3436 1.1 mrg DECL_DELETED_FN (fn) = true; 3437 1.1 mrg else if (classtype_has_depr_implicit_copy (type)) 3438 1.1 mrg /* The implicit definition of a copy constructor as defaulted is 3439 1.1 mrg deprecated if the class has a user-declared copy assignment operator 3440 1.1 mrg or a user-declared destructor. The implicit definition of a copy 3441 1.1 mrg assignment operator as defaulted is deprecated if the class has a 3442 1.1 mrg user-declared copy constructor or a user-declared destructor (15.4, 3443 1.1 mrg 15.8). */ 3444 1.1 mrg TREE_DEPRECATED (fn) = true; 3445 1.1 mrg } 3446 1.1 mrg 3447 1.1 mrg /* Destructors and assignment operators may be virtual. */ 3448 1.1 mrg if (sfk == sfk_destructor 3449 1.1 mrg || sfk == sfk_move_assignment 3450 1.1 mrg || sfk == sfk_copy_assignment) 3451 1.1 mrg check_for_override (fn, type); 3452 1.1 mrg 3453 1.1 mrg /* Add it to the class */ 3454 1.1 mrg bool added = add_method (type, fn, false); 3455 1.1 mrg gcc_assert (added || errorcount); 3456 1.1 mrg 3457 1.1 mrg /* Add it to TYPE_FIELDS. */ 3458 1.1 mrg if (sfk == sfk_destructor 3459 1.1 mrg && DECL_VIRTUAL_P (fn)) 3460 1.1 mrg /* The ABI requires that a virtual destructor go at the end of the 3461 1.1 mrg vtable. */ 3462 1.1 mrg TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), fn); 3463 1.1 mrg else 3464 1.1 mrg { 3465 1.1 mrg DECL_CHAIN (fn) = TYPE_FIELDS (type); 3466 1.1 mrg TYPE_FIELDS (type) = fn; 3467 1.1 mrg } 3468 1.1 mrg /* Propagate TYPE_FIELDS. */ 3469 1.1 mrg fixup_type_variants (type); 3470 1.1 mrg 3471 1.1 mrg maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0); 3472 1.1 mrg if (DECL_MAYBE_IN_CHARGE_CDTOR_P (fn)) 3473 1.1 mrg /* Create appropriate clones. */ 3474 1.1 mrg clone_cdtor (fn, /*update_methods=*/true); 3475 1.1 mrg 3476 1.1 mrg return fn; 3477 1.1 mrg } 3478 1.1 mrg 3479 1.1 mrg /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST 3480 1.1 mrg as there are artificial parms in FN. */ 3481 1.1 mrg 3482 1.1 mrg tree 3483 1.1 mrg skip_artificial_parms_for (const_tree fn, tree list) 3484 1.1 mrg { 3485 1.1 mrg if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)) 3486 1.1 mrg list = TREE_CHAIN (list); 3487 1.1 mrg else 3488 1.1 mrg return list; 3489 1.1 mrg 3490 1.1 mrg if (DECL_HAS_IN_CHARGE_PARM_P (fn)) 3491 1.1 mrg list = TREE_CHAIN (list); 3492 1.1 mrg if (DECL_HAS_VTT_PARM_P (fn)) 3493 1.1 mrg list = TREE_CHAIN (list); 3494 1.1 mrg return list; 3495 1.1 mrg } 3496 1.1 mrg 3497 1.1 mrg /* Given a FUNCTION_DECL FN and a chain LIST, return the number of 3498 1.1 mrg artificial parms in FN. */ 3499 1.1 mrg 3500 1.1 mrg int 3501 1.1 mrg num_artificial_parms_for (const_tree fn) 3502 1.1 mrg { 3503 1.1 mrg int count = 0; 3504 1.1 mrg 3505 1.1 mrg if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)) 3506 1.1 mrg count++; 3507 1.1 mrg else 3508 1.1 mrg return 0; 3509 1.1 mrg 3510 1.1 mrg if (DECL_HAS_IN_CHARGE_PARM_P (fn)) 3511 1.1 mrg count++; 3512 1.1 mrg if (DECL_HAS_VTT_PARM_P (fn)) 3513 1.1 mrg count++; 3514 1.1 mrg return count; 3515 1.1 mrg } 3516 1.1 mrg 3517 3518 #include "gt-cp-method.h" 3519