1 /* Handle parameterized types (templates) for GNU -*- C++ -*-. 2 Copyright (C) 1992-2024 Free Software Foundation, Inc. 3 Written by Ken Raeburn (raeburn (at) cygnus.com) while at Watchmaker Computing. 4 Rewritten by Jason Merrill (jason (at) cygnus.com). 5 6 This file is part of GCC. 7 8 GCC is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 3, or (at your option) 11 any later version. 12 13 GCC is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with GCC; see the file COPYING3. If not see 20 <http://www.gnu.org/licenses/>. */ 21 22 /* Known bugs or deficiencies include: 23 24 all methods must be provided in header files; can't use a source 25 file that contains only the method templates and "just win". 26 27 Fixed by: C++20 modules. */ 28 29 #include "config.h" 30 #define INCLUDE_ALGORITHM // for std::equal 31 #include "system.h" 32 #include "coretypes.h" 33 #include "cp-tree.h" 34 #include "timevar.h" 35 #include "stringpool.h" 36 #include "varasm.h" 37 #include "attribs.h" 38 #include "stor-layout.h" 39 #include "intl.h" 40 #include "c-family/c-objc.h" 41 #include "cp-objcp-common.h" 42 #include "toplev.h" 43 #include "tree-iterator.h" 44 #include "type-utils.h" 45 #include "gimplify.h" 46 #include "gcc-rich-location.h" 47 #include "selftest.h" 48 #include "target.h" 49 #include "builtins.h" 50 #include "omp-general.h" 51 52 /* The type of functions taking a tree, and some additional data, and 53 returning an int. */ 54 typedef int (*tree_fn_t) (tree, void*); 55 56 /* The PENDING_TEMPLATES is a list of templates whose instantiations 57 have been deferred, either because their definitions were not yet 58 available, or because we were putting off doing the work. */ 59 struct GTY ((chain_next ("%h.next"))) pending_template 60 { 61 struct pending_template *next; 62 struct tinst_level *tinst; 63 }; 64 65 static GTY(()) struct pending_template *pending_templates; 66 static GTY(()) struct pending_template *last_pending_template; 67 68 int processing_template_parmlist; 69 static int template_header_count; 70 71 static vec<int> inline_parm_levels; 72 73 static GTY(()) struct tinst_level *current_tinst_level; 74 75 static GTY(()) vec<tree, va_gc> *saved_access_scope; 76 77 /* Live only within one (recursive) call to tsubst_expr. We use 78 this to pass the statement expression node from the STMT_EXPR 79 to the EXPR_STMT that is its result. */ 80 static tree cur_stmt_expr; 81 82 // -------------------------------------------------------------------------- // 83 // Local Specialization Stack 84 // 85 // Implementation of the RAII helper for creating new local 86 // specializations. 87 local_specialization_stack::local_specialization_stack (lss_policy policy) 88 : saved (local_specializations) 89 { 90 if (policy == lss_nop) 91 ; 92 else if (policy == lss_blank || !saved) 93 local_specializations = new hash_map<tree, tree>; 94 else 95 local_specializations = new hash_map<tree, tree>(*saved); 96 } 97 98 local_specialization_stack::~local_specialization_stack () 99 { 100 if (local_specializations != saved) 101 { 102 delete local_specializations; 103 local_specializations = saved; 104 } 105 } 106 107 /* True if we've recursed into fn_type_unification too many times. */ 108 static bool excessive_deduction_depth; 109 110 struct spec_hasher : ggc_ptr_hash<spec_entry> 111 { 112 static hashval_t hash (tree, tree); 113 static hashval_t hash (spec_entry *); 114 static bool equal (spec_entry *, spec_entry *); 115 }; 116 117 /* The general template is not in these tables. */ 118 typedef hash_table<spec_hasher> spec_hash_table; 119 static GTY (()) spec_hash_table *decl_specializations; 120 static GTY (()) spec_hash_table *type_specializations; 121 122 /* Contains canonical template parameter types. The vector is indexed by 123 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a 124 TREE_LIST, whose TREE_VALUEs contain the canonical template 125 parameters of various types and levels. */ 126 static GTY(()) vec<tree, va_gc> *canonical_template_parms; 127 128 #define UNIFY_ALLOW_NONE 0 129 #define UNIFY_ALLOW_MORE_CV_QUAL 1 130 #define UNIFY_ALLOW_LESS_CV_QUAL 2 131 #define UNIFY_ALLOW_DERIVED 4 132 #define UNIFY_ALLOW_INTEGER 8 133 #define UNIFY_ALLOW_OUTER_LEVEL 16 134 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 135 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 136 137 enum template_base_result { 138 tbr_incomplete_type, 139 tbr_ambiguous_baseclass, 140 tbr_success 141 }; 142 143 static bool resolve_overloaded_unification (tree, tree, tree, tree, 144 unification_kind_t, int, 145 bool); 146 static int try_one_overload (tree, tree, tree, tree, tree, 147 unification_kind_t, int, bool, bool); 148 static int unify (tree, tree, tree, tree, int, bool); 149 static void add_pending_template (tree); 150 static tree reopen_tinst_level (struct tinst_level *); 151 static tree tsubst_initializer_list (tree, tree); 152 static tree get_partial_spec_bindings (tree, tree, tree); 153 static void tsubst_enum (tree, tree, tree); 154 static bool check_instantiated_args (tree, tree, tsubst_flags_t); 155 static int check_non_deducible_conversion (tree, tree, unification_kind_t, int, 156 struct conversion **, bool, bool); 157 static int maybe_adjust_types_for_deduction (tree, unification_kind_t, 158 tree*, tree*, tree); 159 static int type_unification_real (tree, tree, tree, const tree *, 160 unsigned int, int, unification_kind_t, 161 vec<deferred_access_check, va_gc> **, 162 bool); 163 static void note_template_header (int); 164 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t); 165 static tree convert_nontype_argument (tree, tree, tsubst_flags_t); 166 static tree convert_template_argument (tree, tree, tree, 167 tsubst_flags_t, int, tree); 168 static tree for_each_template_parm (tree, tree_fn_t, void*, 169 hash_set<tree> *, bool, tree_fn_t = NULL); 170 static tree expand_template_argument_pack (tree); 171 static tree build_template_parm_index (int, int, int, tree, tree); 172 static bool inline_needs_template_parms (tree, bool); 173 static void push_inline_template_parms_recursive (tree, int); 174 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t); 175 static int mark_template_parm (tree, void *); 176 static int template_parm_this_level_p (tree, void *); 177 static tree tsubst_friend_function (tree, tree); 178 static tree tsubst_friend_class (tree, tree); 179 static int can_complete_type_without_circularity (tree); 180 static tree get_bindings (tree, tree, tree, bool); 181 static int template_decl_level (tree); 182 static int check_cv_quals_for_unify (int, tree, tree); 183 static int unify_pack_expansion (tree, tree, tree, 184 tree, unification_kind_t, bool, bool); 185 static tree copy_template_args (tree); 186 static tree tsubst_template_parms (tree, tree, tsubst_flags_t); 187 static void tsubst_each_template_parm_constraints (tree, tree, tsubst_flags_t); 188 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int); 189 static tree tsubst_aggr_type_1 (tree, tree, tsubst_flags_t, tree, int); 190 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree); 191 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); 192 static bool check_specialization_scope (void); 193 static tree process_partial_specialization (tree); 194 static enum template_base_result get_template_base (tree, tree, tree, tree, 195 bool , tree *); 196 static tree try_class_unification (tree, tree, tree, tree, bool); 197 static bool class_nttp_const_wrapper_p (tree t); 198 static int coerce_template_template_parms (tree, tree, tsubst_flags_t, 199 tree, tree); 200 static bool template_template_parm_bindings_ok_p (tree, tree); 201 static void tsubst_default_arguments (tree, tsubst_flags_t); 202 static tree for_each_template_parm_r (tree *, int *, void *); 203 static tree copy_default_args_to_explicit_spec_1 (tree, tree); 204 static void copy_default_args_to_explicit_spec (tree); 205 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t); 206 static bool dependent_template_arg_p (tree); 207 static bool dependent_type_p_r (tree); 208 static tree tsubst_stmt (tree, tree, tsubst_flags_t, tree); 209 static tree tsubst_decl (tree, tree, tsubst_flags_t, bool = true); 210 static tree tsubst_scope (tree, tree, tsubst_flags_t, tree); 211 static tree tsubst_name (tree, tree, tsubst_flags_t, tree); 212 static void perform_instantiation_time_access_checks (tree, tree); 213 static tree listify (tree); 214 static tree listify_autos (tree, tree); 215 static tree tsubst_template_parm (tree, tree, tsubst_flags_t); 216 static tree instantiate_alias_template (tree, tree, tsubst_flags_t); 217 static tree get_underlying_template (tree); 218 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree); 219 static tree canonicalize_expr_argument (tree, tsubst_flags_t); 220 static tree make_argument_pack (tree); 221 static tree enclosing_instantiation_of (tree tctx); 222 static void instantiate_body (tree pattern, tree args, tree d, bool nested); 223 static tree maybe_dependent_member_ref (tree, tree, tsubst_flags_t, tree); 224 static void mark_template_arguments_used (tree, tree); 225 static bool uses_outer_template_parms (tree); 226 static tree alias_ctad_tweaks (tree, tree); 227 static tree inherited_ctad_tweaks (tree, tree, tsubst_flags_t); 228 static tree deduction_guides_for (tree, bool&, tsubst_flags_t); 229 230 /* Make the current scope suitable for access checking when we are 231 processing T. T can be FUNCTION_DECL for instantiated function 232 template, VAR_DECL for static member variable, or TYPE_DECL for 233 for a class or alias template (needed by instantiate_decl). */ 234 235 void 236 push_access_scope (tree t) 237 { 238 gcc_assert (VAR_OR_FUNCTION_DECL_P (t) 239 || TREE_CODE (t) == TYPE_DECL); 240 241 if (DECL_FRIEND_CONTEXT (t)) 242 push_nested_class (DECL_FRIEND_CONTEXT (t)); 243 else if (DECL_IMPLICIT_TYPEDEF_P (t) 244 && CLASS_TYPE_P (TREE_TYPE (t))) 245 push_nested_class (TREE_TYPE (t)); 246 else if (DECL_CLASS_SCOPE_P (t)) 247 push_nested_class (DECL_CONTEXT (t)); 248 else if (deduction_guide_p (t) && DECL_ARTIFICIAL (t)) 249 /* An artificial deduction guide should have the same access as 250 the constructor. */ 251 push_nested_class (TREE_TYPE (TREE_TYPE (t))); 252 else 253 push_to_top_level (); 254 255 if (TREE_CODE (t) == FUNCTION_DECL) 256 { 257 vec_safe_push (saved_access_scope, current_function_decl); 258 current_function_decl = t; 259 } 260 } 261 262 /* Restore the scope set up by push_access_scope. T is the node we 263 are processing. */ 264 265 void 266 pop_access_scope (tree t) 267 { 268 if (TREE_CODE (t) == FUNCTION_DECL) 269 current_function_decl = saved_access_scope->pop(); 270 271 if (DECL_FRIEND_CONTEXT (t) 272 || (DECL_IMPLICIT_TYPEDEF_P (t) 273 && CLASS_TYPE_P (TREE_TYPE (t))) 274 || DECL_CLASS_SCOPE_P (t) 275 || (deduction_guide_p (t) && DECL_ARTIFICIAL (t))) 276 pop_nested_class (); 277 else 278 pop_from_top_level (); 279 } 280 281 /* Do any processing required when DECL (a member template 282 declaration) is finished. Returns the TEMPLATE_DECL corresponding 283 to DECL, unless it is a specialization, in which case the DECL 284 itself is returned. */ 285 286 tree 287 finish_member_template_decl (tree decl) 288 { 289 if (decl == error_mark_node) 290 return error_mark_node; 291 292 gcc_assert (DECL_P (decl)); 293 294 if (TREE_CODE (decl) == TYPE_DECL) 295 { 296 tree type; 297 298 type = TREE_TYPE (decl); 299 if (type == error_mark_node) 300 return error_mark_node; 301 if (MAYBE_CLASS_TYPE_P (type) 302 && CLASSTYPE_TEMPLATE_INFO (type) 303 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) 304 { 305 tree tmpl = CLASSTYPE_TI_TEMPLATE (type); 306 check_member_template (tmpl); 307 return tmpl; 308 } 309 return NULL_TREE; 310 } 311 else if (TREE_CODE (decl) == FIELD_DECL) 312 error_at (DECL_SOURCE_LOCATION (decl), 313 "data member %qD cannot be a member template", decl); 314 else if (DECL_TEMPLATE_INFO (decl)) 315 { 316 if (!DECL_TEMPLATE_SPECIALIZATION (decl)) 317 { 318 check_member_template (DECL_TI_TEMPLATE (decl)); 319 return DECL_TI_TEMPLATE (decl); 320 } 321 else 322 return NULL_TREE; 323 } 324 else 325 error_at (DECL_SOURCE_LOCATION (decl), 326 "invalid member template declaration %qD", decl); 327 328 return error_mark_node; 329 } 330 331 /* Create a template info node. */ 332 333 tree 334 build_template_info (tree template_decl, tree template_args) 335 { 336 tree result = make_node (TEMPLATE_INFO); 337 TI_TEMPLATE (result) = template_decl; 338 TI_ARGS (result) = template_args; 339 return result; 340 } 341 342 /* DECL_TEMPLATE_INFO, if applicable, or NULL_TREE. */ 343 344 static tree 345 decl_template_info (const_tree decl) 346 { 347 /* This needs to match template_info_decl_check. */ 348 if (DECL_LANG_SPECIFIC (decl)) 349 switch (TREE_CODE (decl)) 350 { 351 case FUNCTION_DECL: 352 if (DECL_THUNK_P (decl)) 353 break; 354 gcc_fallthrough (); 355 case VAR_DECL: 356 case FIELD_DECL: 357 case TYPE_DECL: 358 case CONCEPT_DECL: 359 case TEMPLATE_DECL: 360 return DECL_TEMPLATE_INFO (decl); 361 362 default: 363 break; 364 } 365 return NULL_TREE; 366 } 367 368 /* Return the template info node corresponding to T, whatever T is. */ 369 370 tree 371 get_template_info (const_tree t) 372 { 373 tree tinfo = NULL_TREE; 374 375 if (!t || t == error_mark_node) 376 return NULL; 377 378 if (TREE_CODE (t) == NAMESPACE_DECL 379 || TREE_CODE (t) == PARM_DECL) 380 return NULL; 381 382 if (DECL_P (t)) 383 tinfo = decl_template_info (t); 384 385 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t)) 386 t = TREE_TYPE (t); 387 388 if (OVERLOAD_TYPE_P (t)) 389 tinfo = TYPE_TEMPLATE_INFO (t); 390 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) 391 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t); 392 393 return tinfo; 394 } 395 396 /* Returns the template nesting level of the indicated class TYPE. 397 398 For example, in: 399 template <class T> 400 struct A 401 { 402 template <class U> 403 struct B {}; 404 }; 405 406 A<T>::B<U> has depth two, while A<T> has depth one. 407 Both A<T>::B<int> and A<int>::B<U> have depth one, if 408 they are instantiations, not specializations. 409 410 This function is guaranteed to return 0 if passed NULL_TREE so 411 that, for example, `template_class_depth (current_class_type)' is 412 always safe. */ 413 414 int 415 template_class_depth (tree type) 416 { 417 int depth; 418 419 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; ) 420 { 421 tree tinfo = get_template_info (type); 422 423 if (tinfo 424 && TREE_CODE (TI_TEMPLATE (tinfo)) == TEMPLATE_DECL 425 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) 426 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))) 427 ++depth; 428 429 if (DECL_P (type)) 430 { 431 if (tree fctx = DECL_FRIEND_CONTEXT (type)) 432 type = fctx; 433 else 434 type = CP_DECL_CONTEXT (type); 435 } 436 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type)) 437 type = LAMBDA_TYPE_EXTRA_SCOPE (type); 438 else 439 type = CP_TYPE_CONTEXT (type); 440 } 441 442 return depth; 443 } 444 445 /* Return TRUE if NODE instantiates a template that has arguments of 446 its own, be it directly a primary template or indirectly through a 447 partial specializations. */ 448 static bool 449 instantiates_primary_template_p (tree node) 450 { 451 tree tinfo = get_template_info (node); 452 if (!tinfo) 453 return false; 454 455 tree tmpl = TI_TEMPLATE (tinfo); 456 if (PRIMARY_TEMPLATE_P (tmpl)) 457 return true; 458 459 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl)) 460 return false; 461 462 /* So now we know we have a specialization, but it could be a full 463 or a partial specialization. To tell which, compare the depth of 464 its template arguments with those of its context. */ 465 466 tree ctxt = DECL_CONTEXT (tmpl); 467 tree ctinfo = get_template_info (ctxt); 468 if (!ctinfo) 469 return true; 470 471 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo)) 472 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo))); 473 } 474 475 /* Subroutine of maybe_begin_member_template_processing. 476 Returns true if processing DECL needs us to push template parms. */ 477 478 static bool 479 inline_needs_template_parms (tree decl, bool nsdmi) 480 { 481 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl))) 482 return false; 483 484 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) 485 > (current_template_depth + DECL_TEMPLATE_SPECIALIZATION (decl))); 486 } 487 488 /* Subroutine of maybe_begin_member_template_processing. 489 Push the template parms in PARMS, starting from LEVELS steps into the 490 chain, and ending at the beginning, since template parms are listed 491 innermost first. */ 492 493 static void 494 push_inline_template_parms_recursive (tree parmlist, int levels) 495 { 496 tree parms = TREE_VALUE (parmlist); 497 int i; 498 499 if (levels > 1) 500 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); 501 502 ++processing_template_decl; 503 current_template_parms 504 = tree_cons (size_int (current_template_depth + 1), 505 parms, current_template_parms); 506 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) 507 = TEMPLATE_PARMS_CONSTRAINTS (parmlist); 508 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; 509 510 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec, 511 NULL); 512 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) 513 { 514 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); 515 516 if (error_operand_p (parm)) 517 continue; 518 519 gcc_assert (DECL_P (parm)); 520 521 switch (TREE_CODE (parm)) 522 { 523 case TYPE_DECL: 524 case TEMPLATE_DECL: 525 pushdecl (parm); 526 break; 527 528 case PARM_DECL: 529 /* Push the CONST_DECL. */ 530 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm))); 531 break; 532 533 default: 534 gcc_unreachable (); 535 } 536 } 537 } 538 539 /* Restore the template parameter context for a member template, a 540 friend template defined in a class definition, or a non-template 541 member of template class. */ 542 543 void 544 maybe_begin_member_template_processing (tree decl) 545 { 546 tree parms; 547 int levels = 0; 548 bool nsdmi = TREE_CODE (decl) == FIELD_DECL; 549 550 if (nsdmi) 551 { 552 tree ctx = DECL_CONTEXT (decl); 553 decl = (CLASSTYPE_TEMPLATE_INFO (ctx) 554 /* Disregard full specializations (c++/60999). */ 555 && uses_template_parms (ctx) 556 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE); 557 } 558 559 if (inline_needs_template_parms (decl, nsdmi)) 560 { 561 parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); 562 levels = TMPL_PARMS_DEPTH (parms) - current_template_depth; 563 564 if (DECL_TEMPLATE_SPECIALIZATION (decl)) 565 { 566 --levels; 567 parms = TREE_CHAIN (parms); 568 } 569 570 push_inline_template_parms_recursive (parms, levels); 571 } 572 573 /* Remember how many levels of template parameters we pushed so that 574 we can pop them later. */ 575 inline_parm_levels.safe_push (levels); 576 } 577 578 /* Undo the effects of maybe_begin_member_template_processing. */ 579 580 void 581 maybe_end_member_template_processing (void) 582 { 583 int i; 584 int last; 585 586 if (inline_parm_levels.length () == 0) 587 return; 588 589 last = inline_parm_levels.pop (); 590 for (i = 0; i < last; ++i) 591 { 592 --processing_template_decl; 593 current_template_parms = TREE_CHAIN (current_template_parms); 594 poplevel (0, 0, 0); 595 } 596 } 597 598 /* Return a new template argument vector which contains all of ARGS, 599 but has as its innermost set of arguments the EXTRA_ARGS. */ 600 601 tree 602 add_to_template_args (tree args, tree extra_args) 603 { 604 tree new_args; 605 int extra_depth; 606 int i; 607 int j; 608 609 if (args == NULL_TREE || extra_args == error_mark_node) 610 return extra_args; 611 612 extra_depth = TMPL_ARGS_DEPTH (extra_args); 613 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); 614 615 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) 616 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); 617 618 for (j = 1; j <= extra_depth; ++j, ++i) 619 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); 620 621 return new_args; 622 } 623 624 /* Like add_to_template_args, but only the outermost ARGS are added to 625 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH 626 (EXTRA_ARGS) levels are added. This function is used to combine 627 the template arguments from a partial instantiation with the 628 template arguments used to attain the full instantiation from the 629 partial instantiation. 630 631 If ARGS is a TEMPLATE_DECL, use its parameters as args. */ 632 633 tree 634 add_outermost_template_args (tree args, tree extra_args) 635 { 636 tree new_args; 637 638 if (!args) 639 return extra_args; 640 if (TREE_CODE (args) == TEMPLATE_DECL) 641 { 642 tree ti = get_template_info (DECL_TEMPLATE_RESULT (args)); 643 args = TI_ARGS (ti); 644 } 645 646 /* If there are more levels of EXTRA_ARGS than there are ARGS, 647 something very fishy is going on. */ 648 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args)); 649 650 /* If *all* the new arguments will be the EXTRA_ARGS, just return 651 them. */ 652 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) 653 return extra_args; 654 655 /* For the moment, we make ARGS look like it contains fewer levels. */ 656 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); 657 658 new_args = add_to_template_args (args, extra_args); 659 660 /* Now, we restore ARGS to its full dimensions. */ 661 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); 662 663 return new_args; 664 } 665 666 /* Return the N levels of innermost template arguments from the ARGS. */ 667 668 tree 669 get_innermost_template_args (tree args, int n) 670 { 671 tree new_args; 672 int extra_levels; 673 int i; 674 675 gcc_assert (n >= 0); 676 677 /* If N is 1, just return the innermost set of template arguments. */ 678 if (n == 1) 679 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); 680 681 /* If we're not removing anything, just return the arguments we were 682 given. */ 683 extra_levels = TMPL_ARGS_DEPTH (args) - n; 684 gcc_assert (extra_levels >= 0); 685 if (extra_levels == 0) 686 return args; 687 688 /* Make a new set of arguments, not containing the outer arguments. */ 689 new_args = make_tree_vec (n); 690 for (i = 1; i <= n; ++i) 691 SET_TMPL_ARGS_LEVEL (new_args, i, 692 TMPL_ARGS_LEVEL (args, i + extra_levels)); 693 694 return new_args; 695 } 696 697 /* The inverse of get_innermost_template_args: Return all but the innermost 698 EXTRA_LEVELS levels of template arguments from the ARGS. */ 699 700 static tree 701 strip_innermost_template_args (tree args, int extra_levels) 702 { 703 tree new_args; 704 int n = TMPL_ARGS_DEPTH (args) - extra_levels; 705 int i; 706 707 gcc_assert (n >= 0); 708 709 /* If N is 1, just return the outermost set of template arguments. */ 710 if (n == 1) 711 return TMPL_ARGS_LEVEL (args, 1); 712 713 /* If we're not removing anything, just return the arguments we were 714 given. */ 715 gcc_assert (extra_levels >= 0); 716 if (extra_levels == 0) 717 return args; 718 719 /* Make a new set of arguments, not containing the inner arguments. */ 720 new_args = make_tree_vec (n); 721 for (i = 1; i <= n; ++i) 722 SET_TMPL_ARGS_LEVEL (new_args, i, 723 TMPL_ARGS_LEVEL (args, i)); 724 725 return new_args; 726 } 727 728 /* We've got a template header coming up; push to a new level for storing 729 the parms. */ 730 731 void 732 begin_template_parm_list (void) 733 { 734 /* We use a non-tag-transparent scope here, which causes pushtag to 735 put tags in this scope, rather than in the enclosing class or 736 namespace scope. This is the right thing, since we want 737 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a 738 global template class, push_template_decl handles putting the 739 TEMPLATE_DECL into top-level scope. For a nested template class, 740 e.g.: 741 742 template <class T> struct S1 { 743 template <class T> struct S2 {}; 744 }; 745 746 pushtag contains special code to insert the TEMPLATE_DECL for S2 747 at the right scope. */ 748 begin_scope (sk_template_parms, NULL); 749 ++processing_template_decl; 750 ++processing_template_parmlist; 751 note_template_header (0); 752 753 /* Add a dummy parameter level while we process the parameter list. */ 754 current_template_parms 755 = tree_cons (size_int (current_template_depth + 1), 756 make_tree_vec (0), 757 current_template_parms); 758 } 759 760 /* This routine is called when a specialization is declared. If it is 761 invalid to declare a specialization here, an error is reported and 762 false is returned, otherwise this routine will return true. */ 763 764 static bool 765 check_specialization_scope (void) 766 { 767 tree scope = current_scope (); 768 769 /* [temp.expl.spec] 770 771 An explicit specialization shall be declared in the namespace of 772 which the template is a member, or, for member templates, in the 773 namespace of which the enclosing class or enclosing class 774 template is a member. An explicit specialization of a member 775 function, member class or static data member of a class template 776 shall be declared in the namespace of which the class template 777 is a member. */ 778 if (scope && TREE_CODE (scope) != NAMESPACE_DECL) 779 { 780 error ("explicit specialization in non-namespace scope %qD", scope); 781 return false; 782 } 783 784 /* [temp.expl.spec] 785 786 In an explicit specialization declaration for a member of a class 787 template or a member template that appears in namespace scope, 788 the member template and some of its enclosing class templates may 789 remain unspecialized, except that the declaration shall not 790 explicitly specialize a class member template if its enclosing 791 class templates are not explicitly specialized as well. */ 792 if (current_template_parms) 793 { 794 error ("enclosing class templates are not explicitly specialized"); 795 return false; 796 } 797 798 return true; 799 } 800 801 /* We've just seen template <>. */ 802 803 bool 804 begin_specialization (void) 805 { 806 begin_scope (sk_template_spec, NULL); 807 note_template_header (1); 808 return check_specialization_scope (); 809 } 810 811 /* Called at then end of processing a declaration preceded by 812 template<>. */ 813 814 void 815 end_specialization (void) 816 { 817 finish_scope (); 818 reset_specialization (); 819 } 820 821 /* Any template <>'s that we have seen thus far are not referring to a 822 function specialization. */ 823 824 void 825 reset_specialization (void) 826 { 827 processing_specialization = 0; 828 template_header_count = 0; 829 } 830 831 /* We've just seen a template header. If SPECIALIZATION is nonzero, 832 it was of the form template <>. */ 833 834 static void 835 note_template_header (int specialization) 836 { 837 processing_specialization = specialization; 838 template_header_count++; 839 } 840 841 /* We're beginning an explicit instantiation. */ 842 843 void 844 begin_explicit_instantiation (void) 845 { 846 gcc_assert (!processing_explicit_instantiation); 847 processing_explicit_instantiation = true; 848 } 849 850 851 void 852 end_explicit_instantiation (void) 853 { 854 gcc_assert (processing_explicit_instantiation); 855 processing_explicit_instantiation = false; 856 } 857 858 /* An explicit specialization or partial specialization of TMPL is being 859 declared. Check that the namespace in which the specialization is 860 occurring is permissible. Returns false iff it is invalid to 861 specialize TMPL in the current namespace. */ 862 863 static bool 864 check_specialization_namespace (tree tmpl) 865 { 866 tree tpl_ns = decl_namespace_context (tmpl); 867 868 /* [tmpl.expl.spec] 869 870 An explicit specialization shall be declared in a namespace enclosing the 871 specialized template. An explicit specialization whose declarator-id is 872 not qualified shall be declared in the nearest enclosing namespace of the 873 template, or, if the namespace is inline (7.3.1), any namespace from its 874 enclosing namespace set. */ 875 if (current_scope() != DECL_CONTEXT (tmpl) 876 && !at_namespace_scope_p ()) 877 { 878 error ("specialization of %qD must appear at namespace scope", tmpl); 879 return false; 880 } 881 882 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11)) 883 /* Same or enclosing namespace. */ 884 return true; 885 else 886 { 887 auto_diagnostic_group d; 888 if (permerror (input_location, 889 "specialization of %qD in different namespace", tmpl)) 890 inform (DECL_SOURCE_LOCATION (tmpl), 891 " from definition of %q#D", tmpl); 892 return false; 893 } 894 } 895 896 /* SPEC is an explicit instantiation. Check that it is valid to 897 perform this explicit instantiation in the current namespace. */ 898 899 static void 900 check_explicit_instantiation_namespace (tree spec) 901 { 902 tree ns; 903 904 /* DR 275: An explicit instantiation shall appear in an enclosing 905 namespace of its template. */ 906 ns = decl_namespace_context (spec); 907 if (!is_nested_namespace (current_namespace, ns)) 908 permerror (input_location, "explicit instantiation of %qD in namespace %qD " 909 "(which does not enclose namespace %qD)", 910 spec, current_namespace, ns); 911 } 912 913 /* Returns true if TYPE is a new partial specialization that needs to be 914 set up. This may also modify TYPE to point to the correct (new or 915 existing) constrained partial specialization. */ 916 917 static bool 918 maybe_new_partial_specialization (tree& type) 919 { 920 /* An implicit instantiation of an incomplete type implies 921 the definition of a new class template. 922 923 template<typename T> 924 struct S; 925 926 template<typename T> 927 struct S<T*>; 928 929 Here, S<T*> is an implicit instantiation of S whose type 930 is incomplete. */ 931 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type)) 932 return true; 933 934 /* It can also be the case that TYPE is a completed specialization. 935 Continuing the previous example, suppose we also declare: 936 937 template<typename T> 938 requires Integral<T> 939 struct S<T*>; 940 941 Here, S<T*> refers to the specialization S<T*> defined 942 above. However, we need to differentiate definitions because 943 we intend to define a new partial specialization. In this case, 944 we rely on the fact that the constraints are different for 945 this declaration than that above. 946 947 Note that we also get here for injected class names and 948 late-parsed template definitions. We must ensure that we 949 do not create new type declarations for those cases. */ 950 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) 951 { 952 tree tmpl = CLASSTYPE_TI_TEMPLATE (type); 953 tree args = CLASSTYPE_TI_ARGS (type); 954 955 /* If there are no template parameters, this cannot be a new 956 partial template specialization? */ 957 if (!current_template_parms) 958 return false; 959 960 /* The injected-class-name is not a new partial specialization. */ 961 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type))) 962 return false; 963 964 /* If the constraints are not the same as those of the primary 965 then, we can probably create a new specialization. */ 966 tree type_constr = current_template_constraints (); 967 968 if (type == TREE_TYPE (tmpl)) 969 { 970 tree main_constr = get_constraints (tmpl); 971 if (equivalent_constraints (type_constr, main_constr)) 972 return false; 973 } 974 975 /* Also, if there's a pre-existing specialization with matching 976 constraints, then this also isn't new. */ 977 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); 978 while (specs) 979 { 980 tree spec_tmpl = TREE_VALUE (specs); 981 tree spec_args = TREE_PURPOSE (specs); 982 tree spec_constr = get_constraints (spec_tmpl); 983 if (comp_template_args (args, spec_args) 984 && equivalent_constraints (type_constr, spec_constr)) 985 { 986 type = TREE_TYPE (spec_tmpl); 987 return false; 988 } 989 specs = TREE_CHAIN (specs); 990 } 991 992 /* Create a new type node (and corresponding type decl) 993 for the newly declared specialization. */ 994 tree t = make_class_type (TREE_CODE (type)); 995 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type); 996 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args)); 997 TYPE_CONTEXT (t) = TYPE_CONTEXT (type); 998 999 /* We only need a separate type node for storing the definition of this 1000 partial specialization; uses of S<T*> are unconstrained, so all are 1001 equivalent. So keep TYPE_CANONICAL the same. */ 1002 TYPE_CANONICAL (t) = TYPE_CANONICAL (type); 1003 1004 /* Build the corresponding type decl. */ 1005 tree d = create_implicit_typedef (DECL_NAME (tmpl), t); 1006 DECL_CONTEXT (d) = TYPE_CONTEXT (t); 1007 DECL_SOURCE_LOCATION (d) = input_location; 1008 TREE_PUBLIC (d) = TREE_PUBLIC (DECL_TEMPLATE_RESULT (tmpl)); 1009 1010 set_instantiating_module (d); 1011 DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl); 1012 1013 type = t; 1014 return true; 1015 } 1016 1017 return false; 1018 } 1019 1020 /* The TYPE is being declared. If it is a template type, that means it 1021 is a partial specialization. Do appropriate error-checking. */ 1022 1023 tree 1024 maybe_process_partial_specialization (tree type) 1025 { 1026 tree context; 1027 1028 if (type == error_mark_node) 1029 return error_mark_node; 1030 1031 /* A lambda that appears in specialization context is not itself a 1032 specialization. */ 1033 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type)) 1034 return type; 1035 1036 /* An injected-class-name is not a specialization. */ 1037 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type))) 1038 return type; 1039 1040 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) 1041 { 1042 error ("name of class shadows template template parameter %qD", 1043 TYPE_NAME (type)); 1044 return error_mark_node; 1045 } 1046 1047 context = TYPE_CONTEXT (type); 1048 1049 if (TYPE_ALIAS_P (type)) 1050 { 1051 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type); 1052 1053 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo))) 1054 error ("specialization of alias template %qD", 1055 TI_TEMPLATE (tinfo)); 1056 else 1057 error ("explicit specialization of non-template %qT", type); 1058 return error_mark_node; 1059 } 1060 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) 1061 { 1062 /* This is for ordinary explicit specialization and partial 1063 specialization of a template class such as: 1064 1065 template <> class C<int>; 1066 1067 or: 1068 1069 template <class T> class C<T*>; 1070 1071 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */ 1072 1073 if (maybe_new_partial_specialization (type)) 1074 { 1075 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type)) 1076 && !at_namespace_scope_p ()) 1077 return error_mark_node; 1078 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); 1079 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location; 1080 if (processing_template_decl) 1081 { 1082 tree decl = push_template_decl (TYPE_MAIN_DECL (type)); 1083 if (decl == error_mark_node) 1084 return error_mark_node; 1085 return TREE_TYPE (decl); 1086 } 1087 } 1088 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) 1089 error ("specialization of %qT after instantiation", type); 1090 else if (errorcount && !processing_specialization 1091 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type) 1092 && !uses_template_parms (CLASSTYPE_TI_ARGS (type))) 1093 /* Trying to define a specialization either without a template<> header 1094 or in an inappropriate place. We've already given an error, so just 1095 bail now so we don't actually define the specialization. */ 1096 return error_mark_node; 1097 } 1098 else if (CLASS_TYPE_P (type) 1099 && !CLASSTYPE_USE_TEMPLATE (type) 1100 && CLASSTYPE_TEMPLATE_INFO (type) 1101 && context && CLASS_TYPE_P (context) 1102 && CLASSTYPE_TEMPLATE_INFO (context)) 1103 { 1104 /* This is for an explicit specialization of member class 1105 template according to [temp.expl.spec/18]: 1106 1107 template <> template <class U> class C<int>::D; 1108 1109 The context `C<int>' must be an implicit instantiation. 1110 Otherwise this is just a member class template declared 1111 earlier like: 1112 1113 template <> class C<int> { template <class U> class D; }; 1114 template <> template <class U> class C<int>::D; 1115 1116 In the first case, `C<int>::D' is a specialization of `C<T>::D' 1117 while in the second case, `C<int>::D' is a primary template 1118 and `C<T>::D' may not exist. */ 1119 1120 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context) 1121 && !COMPLETE_TYPE_P (type)) 1122 { 1123 tree t; 1124 tree tmpl = CLASSTYPE_TI_TEMPLATE (type); 1125 1126 if (current_namespace 1127 != decl_namespace_context (tmpl)) 1128 { 1129 if (permerror (input_location, 1130 "specialization of %qD in different namespace", 1131 type)) 1132 inform (DECL_SOURCE_LOCATION (tmpl), 1133 "from definition of %q#D", tmpl); 1134 } 1135 1136 /* Check for invalid specialization after instantiation: 1137 1138 template <> template <> class C<int>::D<int>; 1139 template <> template <class U> class C<int>::D; */ 1140 1141 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); 1142 t; t = TREE_CHAIN (t)) 1143 { 1144 tree inst = TREE_VALUE (t); 1145 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst) 1146 || !COMPLETE_OR_OPEN_TYPE_P (inst)) 1147 { 1148 /* We already have a full specialization of this partial 1149 instantiation, or a full specialization has been 1150 looked up but not instantiated. Reassign it to the 1151 new member specialization template. */ 1152 spec_entry elt; 1153 spec_entry *entry; 1154 1155 elt.tmpl = most_general_template (tmpl); 1156 elt.args = CLASSTYPE_TI_ARGS (inst); 1157 elt.spec = inst; 1158 1159 type_specializations->remove_elt (&elt); 1160 1161 elt.tmpl = tmpl; 1162 CLASSTYPE_TI_ARGS (inst) 1163 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args); 1164 1165 spec_entry **slot 1166 = type_specializations->find_slot (&elt, INSERT); 1167 entry = ggc_alloc<spec_entry> (); 1168 *entry = elt; 1169 *slot = entry; 1170 } 1171 else 1172 /* But if we've had an implicit instantiation, that's a 1173 problem ([temp.expl.spec]/6). */ 1174 error ("specialization %qT after instantiation %qT", 1175 type, inst); 1176 } 1177 1178 /* Mark TYPE as a specialization. And as a result, we only 1179 have one level of template argument for the innermost 1180 class template. */ 1181 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); 1182 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location; 1183 CLASSTYPE_TI_ARGS (type) 1184 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); 1185 } 1186 } 1187 else if (processing_specialization) 1188 { 1189 /* Someday C++0x may allow for enum template specialization. */ 1190 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE 1191 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context)) 1192 pedwarn (input_location, OPT_Wpedantic, "template specialization " 1193 "of %qD not allowed by ISO C++", type); 1194 else 1195 { 1196 error ("explicit specialization of non-template %qT", type); 1197 return error_mark_node; 1198 } 1199 } 1200 1201 return type; 1202 } 1203 1204 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have 1205 gone through coerce_template_parms by now. */ 1206 1207 static void 1208 verify_unstripped_args_1 (tree inner) 1209 { 1210 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i) 1211 { 1212 tree arg = TREE_VEC_ELT (inner, i); 1213 if (TREE_CODE (arg) == TEMPLATE_DECL) 1214 /* OK */; 1215 else if (TYPE_P (arg)) 1216 gcc_assert (strip_typedefs (arg, NULL) == arg); 1217 else if (ARGUMENT_PACK_P (arg)) 1218 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg)); 1219 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg)) 1220 /* Allow typedefs on the type of a non-type argument, since a 1221 parameter can have them. */; 1222 else 1223 gcc_assert (strip_typedefs_expr (arg, NULL) == arg); 1224 } 1225 } 1226 1227 static void 1228 verify_unstripped_args (tree args) 1229 { 1230 ++processing_template_decl; 1231 if (!any_dependent_template_arguments_p (args)) 1232 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args)); 1233 --processing_template_decl; 1234 } 1235 1236 /* Retrieve the specialization (in the sense of [temp.spec] - a 1237 specialization is either an instantiation or an explicit 1238 specialization) of TMPL for the given template ARGS. If there is 1239 no such specialization, return NULL_TREE. The ARGS are a vector of 1240 arguments, or a vector of vectors of arguments, in the case of 1241 templates with more than one level of parameters. 1242 1243 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true, 1244 then we search for a partial specialization matching ARGS. This 1245 parameter is ignored if TMPL is not a class template. 1246 1247 We can also look up a FIELD_DECL, if it is a lambda capture pack; the 1248 result is a NONTYPE_ARGUMENT_PACK. */ 1249 1250 static tree 1251 retrieve_specialization (tree tmpl, tree args, hashval_t hash) 1252 { 1253 if (tmpl == NULL_TREE) 1254 return NULL_TREE; 1255 1256 if (args == error_mark_node) 1257 return NULL_TREE; 1258 1259 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL 1260 || TREE_CODE (tmpl) == FIELD_DECL); 1261 1262 /* There should be as many levels of arguments as there are 1263 levels of parameters. */ 1264 gcc_assert (TMPL_ARGS_DEPTH (args) 1265 == (TREE_CODE (tmpl) == TEMPLATE_DECL 1266 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)) 1267 : template_class_depth (DECL_CONTEXT (tmpl)))); 1268 1269 if (flag_checking) 1270 verify_unstripped_args (args); 1271 1272 /* Lambda functions in templates aren't instantiated normally, but through 1273 tsubst_lambda_expr. */ 1274 if (lambda_fn_in_template_p (tmpl)) 1275 return NULL_TREE; 1276 1277 spec_entry elt; 1278 elt.tmpl = tmpl; 1279 elt.args = args; 1280 elt.spec = NULL_TREE; 1281 1282 spec_hash_table *specializations; 1283 if (DECL_CLASS_TEMPLATE_P (tmpl)) 1284 specializations = type_specializations; 1285 else 1286 specializations = decl_specializations; 1287 1288 if (hash == 0) 1289 hash = spec_hasher::hash (&elt); 1290 if (spec_entry *found = specializations->find_with_hash (&elt, hash)) 1291 return found->spec; 1292 1293 return NULL_TREE; 1294 } 1295 1296 /* Like retrieve_specialization, but for local declarations. */ 1297 1298 tree 1299 retrieve_local_specialization (tree tmpl) 1300 { 1301 if (local_specializations == NULL) 1302 return NULL_TREE; 1303 1304 tree *slot = local_specializations->get (tmpl); 1305 return slot ? *slot : NULL_TREE; 1306 } 1307 1308 /* Returns nonzero iff DECL is a specialization of TMPL. */ 1309 1310 int 1311 is_specialization_of (tree decl, tree tmpl) 1312 { 1313 tree t; 1314 1315 if (TREE_CODE (decl) == FUNCTION_DECL) 1316 { 1317 for (t = decl; 1318 t != NULL_TREE; 1319 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) 1320 if (t == tmpl) 1321 return 1; 1322 } 1323 else 1324 { 1325 gcc_assert (TREE_CODE (decl) == TYPE_DECL); 1326 1327 for (t = TREE_TYPE (decl); 1328 t != NULL_TREE; 1329 t = CLASSTYPE_USE_TEMPLATE (t) 1330 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) 1331 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) 1332 return 1; 1333 } 1334 1335 return 0; 1336 } 1337 1338 /* Returns nonzero iff DECL is a specialization of friend declaration 1339 FRIEND_DECL according to [temp.friend]. */ 1340 1341 bool 1342 is_specialization_of_friend (tree decl, tree friend_decl) 1343 { 1344 bool need_template = true; 1345 int template_depth; 1346 1347 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL 1348 || TREE_CODE (decl) == TYPE_DECL); 1349 1350 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function 1351 of a template class, we want to check if DECL is a specialization 1352 if this. */ 1353 if (TREE_CODE (friend_decl) == FUNCTION_DECL 1354 && DECL_CLASS_SCOPE_P (friend_decl) 1355 && DECL_TEMPLATE_INFO (friend_decl) 1356 && !DECL_USE_TEMPLATE (friend_decl)) 1357 { 1358 /* We want a TEMPLATE_DECL for `is_specialization_of'. */ 1359 friend_decl = DECL_TI_TEMPLATE (friend_decl); 1360 need_template = false; 1361 } 1362 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL 1363 && !PRIMARY_TEMPLATE_P (friend_decl)) 1364 need_template = false; 1365 1366 /* There is nothing to do if this is not a template friend. */ 1367 if (TREE_CODE (friend_decl) != TEMPLATE_DECL) 1368 return false; 1369 1370 if (is_specialization_of (decl, friend_decl)) 1371 return true; 1372 1373 /* [temp.friend/6] 1374 A member of a class template may be declared to be a friend of a 1375 non-template class. In this case, the corresponding member of 1376 every specialization of the class template is a friend of the 1377 class granting friendship. 1378 1379 For example, given a template friend declaration 1380 1381 template <class T> friend void A<T>::f(); 1382 1383 the member function below is considered a friend 1384 1385 template <> struct A<int> { 1386 void f(); 1387 }; 1388 1389 For this type of template friend, TEMPLATE_DEPTH below will be 1390 nonzero. To determine if DECL is a friend of FRIEND, we first 1391 check if the enclosing class is a specialization of another. */ 1392 1393 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl)); 1394 if (template_depth 1395 && DECL_CLASS_SCOPE_P (decl) 1396 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)), 1397 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl)))) 1398 { 1399 /* Next, we check the members themselves. In order to handle 1400 a few tricky cases, such as when FRIEND_DECL's are 1401 1402 template <class T> friend void A<T>::g(T t); 1403 template <class T> template <T t> friend void A<T>::h(); 1404 1405 and DECL's are 1406 1407 void A<int>::g(int); 1408 template <int> void A<int>::h(); 1409 1410 we need to figure out ARGS, the template arguments from 1411 the context of DECL. This is required for template substitution 1412 of `T' in the function parameter of `g' and template parameter 1413 of `h' in the above examples. Here ARGS corresponds to `int'. */ 1414 1415 tree context = DECL_CONTEXT (decl); 1416 tree args = NULL_TREE; 1417 int current_depth = 0; 1418 1419 while (current_depth < template_depth) 1420 { 1421 if (CLASSTYPE_TEMPLATE_INFO (context)) 1422 { 1423 if (current_depth == 0) 1424 args = TYPE_TI_ARGS (context); 1425 else 1426 args = add_to_template_args (TYPE_TI_ARGS (context), args); 1427 current_depth++; 1428 } 1429 context = TYPE_CONTEXT (context); 1430 } 1431 1432 if (TREE_CODE (decl) == FUNCTION_DECL) 1433 { 1434 bool is_template; 1435 tree friend_type; 1436 tree decl_type; 1437 tree friend_args_type; 1438 tree decl_args_type; 1439 1440 /* Make sure that both DECL and FRIEND_DECL are templates or 1441 non-templates. */ 1442 is_template = DECL_TEMPLATE_INFO (decl) 1443 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)); 1444 if (need_template ^ is_template) 1445 return false; 1446 else if (is_template) 1447 { 1448 /* If both are templates, check template parameter list. */ 1449 tree friend_parms 1450 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl), 1451 args, tf_none); 1452 if (!comp_template_parms 1453 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)), 1454 friend_parms)) 1455 return false; 1456 1457 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl)); 1458 } 1459 else 1460 decl_type = TREE_TYPE (decl); 1461 1462 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args, 1463 tf_none, NULL_TREE); 1464 if (friend_type == error_mark_node) 1465 return false; 1466 1467 /* Check if return types match. */ 1468 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type))) 1469 return false; 1470 1471 /* Check if function parameter types match, ignoring the 1472 `this' parameter. */ 1473 friend_args_type = TYPE_ARG_TYPES (friend_type); 1474 decl_args_type = TYPE_ARG_TYPES (decl_type); 1475 if (DECL_IOBJ_MEMBER_FUNCTION_P (friend_decl)) 1476 friend_args_type = TREE_CHAIN (friend_args_type); 1477 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl)) 1478 decl_args_type = TREE_CHAIN (decl_args_type); 1479 1480 return compparms (decl_args_type, friend_args_type); 1481 } 1482 else 1483 { 1484 /* DECL is a TYPE_DECL */ 1485 bool is_template; 1486 tree decl_type = TREE_TYPE (decl); 1487 1488 /* Make sure that both DECL and FRIEND_DECL are templates or 1489 non-templates. */ 1490 is_template 1491 = CLASSTYPE_TEMPLATE_INFO (decl_type) 1492 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type)); 1493 1494 if (need_template ^ is_template) 1495 return false; 1496 else if (is_template) 1497 { 1498 tree friend_parms; 1499 /* If both are templates, check the name of the two 1500 TEMPLATE_DECL's first because is_friend didn't. */ 1501 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type)) 1502 != DECL_NAME (friend_decl)) 1503 return false; 1504 1505 /* Now check template parameter list. */ 1506 friend_parms 1507 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl), 1508 args, tf_none); 1509 return comp_template_parms 1510 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)), 1511 friend_parms); 1512 } 1513 else 1514 return (DECL_NAME (decl) 1515 == DECL_NAME (friend_decl)); 1516 } 1517 } 1518 return false; 1519 } 1520 1521 /* Register the specialization SPEC as a specialization of TMPL with 1522 the indicated ARGS. IS_FRIEND indicates whether the specialization 1523 is actually just a friend declaration. ATTRLIST is the list of 1524 attributes that the specialization is declared with or NULL when 1525 it isn't. Returns SPEC, or an equivalent prior declaration, if 1526 available. 1527 1528 We also store instantiations of field packs in the hash table, even 1529 though they are not themselves templates, to make lookup easier. */ 1530 1531 static tree 1532 register_specialization (tree spec, tree tmpl, tree args, bool is_friend, 1533 hashval_t hash) 1534 { 1535 tree fn; 1536 1537 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec)) 1538 || (TREE_CODE (tmpl) == FIELD_DECL 1539 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)); 1540 1541 spec_entry elt; 1542 elt.tmpl = tmpl; 1543 elt.args = args; 1544 elt.spec = spec; 1545 1546 if (hash == 0) 1547 hash = spec_hasher::hash (&elt); 1548 1549 spec_entry **slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT); 1550 if (*slot) 1551 fn = (*slot)->spec; 1552 else 1553 fn = NULL_TREE; 1554 1555 /* We can sometimes try to re-register a specialization that we've 1556 already got. In particular, regenerate_decl_from_template calls 1557 duplicate_decls which will update the specialization list. But, 1558 we'll still get called again here anyhow. It's more convenient 1559 to simply allow this than to try to prevent it. */ 1560 if (fn == spec) 1561 return spec; 1562 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec)) 1563 { 1564 if (DECL_TEMPLATE_INSTANTIATION (fn)) 1565 { 1566 if (DECL_ODR_USED (fn) 1567 || DECL_EXPLICIT_INSTANTIATION (fn)) 1568 { 1569 error ("specialization of %qD after instantiation", 1570 fn); 1571 return error_mark_node; 1572 } 1573 else 1574 { 1575 tree clone; 1576 /* This situation should occur only if the first 1577 specialization is an implicit instantiation, the 1578 second is an explicit specialization, and the 1579 implicit instantiation has not yet been used. That 1580 situation can occur if we have implicitly 1581 instantiated a member function and then specialized 1582 it later. 1583 1584 We can also wind up here if a friend declaration that 1585 looked like an instantiation turns out to be a 1586 specialization: 1587 1588 template <class T> void foo(T); 1589 class S { friend void foo<>(int) }; 1590 template <> void foo(int); 1591 1592 We transform the existing DECL in place so that any 1593 pointers to it become pointers to the updated 1594 declaration. 1595 1596 If there was a definition for the template, but not 1597 for the specialization, we want this to look as if 1598 there were no definition, and vice versa. */ 1599 DECL_INITIAL (fn) = NULL_TREE; 1600 duplicate_decls (spec, fn, /*hiding=*/is_friend); 1601 1602 /* The call to duplicate_decls will have applied 1603 [temp.expl.spec]: 1604 1605 An explicit specialization of a function template 1606 is inline only if it is explicitly declared to be, 1607 and independently of whether its function template 1608 is. 1609 1610 to the primary function; now copy the inline bits to 1611 the various clones. */ 1612 FOR_EACH_CLONE (clone, fn) 1613 { 1614 DECL_DECLARED_INLINE_P (clone) 1615 = DECL_DECLARED_INLINE_P (fn); 1616 DECL_SOURCE_LOCATION (clone) 1617 = DECL_SOURCE_LOCATION (fn); 1618 DECL_DELETED_FN (clone) 1619 = DECL_DELETED_FN (fn); 1620 } 1621 check_specialization_namespace (tmpl); 1622 1623 return fn; 1624 } 1625 } 1626 else if (DECL_TEMPLATE_SPECIALIZATION (fn)) 1627 { 1628 tree dd = duplicate_decls (spec, fn, /*hiding=*/is_friend); 1629 if (dd == error_mark_node) 1630 /* We've already complained in duplicate_decls. */ 1631 return error_mark_node; 1632 1633 if (dd == NULL_TREE && DECL_INITIAL (spec)) 1634 /* Dup decl failed, but this is a new definition. Set the 1635 line number so any errors match this new 1636 definition. */ 1637 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec); 1638 1639 return fn; 1640 } 1641 } 1642 else if (fn) 1643 return duplicate_decls (spec, fn, /*hiding=*/is_friend); 1644 1645 /* A specialization must be declared in the same namespace as the 1646 template it is specializing. */ 1647 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec) 1648 && !check_specialization_namespace (tmpl)) 1649 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl); 1650 1651 spec_entry *entry = ggc_alloc<spec_entry> (); 1652 gcc_assert (tmpl && args && spec); 1653 *entry = elt; 1654 *slot = entry; 1655 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec) 1656 && PRIMARY_TEMPLATE_P (tmpl) 1657 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE) 1658 || variable_template_p (tmpl)) 1659 /* If TMPL is a forward declaration of a template function, keep a list 1660 of all specializations in case we need to reassign them to a friend 1661 template later in tsubst_friend_function. 1662 1663 Also keep a list of all variable template instantiations so that 1664 process_partial_specialization can check whether a later partial 1665 specialization would have used it. */ 1666 DECL_TEMPLATE_INSTANTIATIONS (tmpl) 1667 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl)); 1668 1669 return spec; 1670 } 1671 1672 /* Restricts tree and type comparisons. */ 1673 int comparing_specializations; 1674 int comparing_dependent_aliases; 1675 1676 /* Whether we are comparing template arguments during partial ordering 1677 (and therefore want the comparison to look through dependent alias 1678 template specializations). */ 1679 1680 static int comparing_for_partial_ordering; 1681 1682 /* Returns true iff two spec_entry nodes are equivalent. */ 1683 1684 bool 1685 spec_hasher::equal (spec_entry *e1, spec_entry *e2) 1686 { 1687 int equal; 1688 1689 ++comparing_specializations; 1690 ++comparing_dependent_aliases; 1691 ++processing_template_decl; 1692 equal = (e1->tmpl == e2->tmpl 1693 && comp_template_args (e1->args, e2->args)); 1694 if (equal && flag_concepts 1695 /* tmpl could be a FIELD_DECL for a capture pack. */ 1696 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL 1697 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl)) 1698 && uses_template_parms (e1->args)) 1699 { 1700 /* Partial specializations of a variable template can be distinguished by 1701 constraints. */ 1702 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE; 1703 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE; 1704 equal = equivalent_constraints (c1, c2); 1705 } 1706 --processing_template_decl; 1707 --comparing_dependent_aliases; 1708 --comparing_specializations; 1709 1710 return equal; 1711 } 1712 1713 /* Returns a hash for a template TMPL and template arguments ARGS. */ 1714 1715 static hashval_t 1716 hash_tmpl_and_args (tree tmpl, tree args) 1717 { 1718 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0); 1719 return iterative_hash_template_arg (args, val); 1720 } 1721 1722 hashval_t 1723 spec_hasher::hash (tree tmpl, tree args) 1724 { 1725 ++comparing_specializations; 1726 hashval_t val = hash_tmpl_and_args (tmpl, args); 1727 --comparing_specializations; 1728 return val; 1729 } 1730 1731 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members, 1732 ignoring SPEC. */ 1733 1734 hashval_t 1735 spec_hasher::hash (spec_entry *e) 1736 { 1737 return spec_hasher::hash (e->tmpl, e->args); 1738 } 1739 1740 /* Recursively calculate a hash value for a template argument ARG, for use 1741 in the hash tables of template specializations. We must be 1742 careful to (at least) skip the same entities template_args_equal 1743 does. */ 1744 1745 hashval_t 1746 iterative_hash_template_arg (tree arg, hashval_t val) 1747 { 1748 if (arg == NULL_TREE) 1749 return iterative_hash_object (arg, val); 1750 1751 if (!TYPE_P (arg)) 1752 /* Strip nop-like things, but not the same as STRIP_NOPS. */ 1753 while (CONVERT_EXPR_P (arg) 1754 || TREE_CODE (arg) == NON_LVALUE_EXPR 1755 || class_nttp_const_wrapper_p (arg)) 1756 arg = TREE_OPERAND (arg, 0); 1757 1758 enum tree_code code = TREE_CODE (arg); 1759 1760 val = iterative_hash_object (code, val); 1761 1762 switch (code) 1763 { 1764 case ARGUMENT_PACK_SELECT: 1765 /* Getting here with an ARGUMENT_PACK_SELECT means we're probably 1766 preserving it in a hash table, which is bad because it will change 1767 meaning when gen_elem_of_pack_expansion_instantiation changes the 1768 ARGUMENT_PACK_SELECT_INDEX. */ 1769 gcc_unreachable (); 1770 1771 case ERROR_MARK: 1772 return val; 1773 1774 case IDENTIFIER_NODE: 1775 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val); 1776 1777 case TREE_VEC: 1778 for (tree elt : tree_vec_range (arg)) 1779 val = iterative_hash_template_arg (elt, val); 1780 return val; 1781 1782 case TYPE_PACK_EXPANSION: 1783 case EXPR_PACK_EXPANSION: 1784 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val); 1785 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val); 1786 1787 case TYPE_ARGUMENT_PACK: 1788 case NONTYPE_ARGUMENT_PACK: 1789 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val); 1790 1791 case TREE_LIST: 1792 for (; arg; arg = TREE_CHAIN (arg)) 1793 val = iterative_hash_template_arg (TREE_VALUE (arg), val); 1794 return val; 1795 1796 case OVERLOAD: 1797 for (lkp_iterator iter (arg); iter; ++iter) 1798 val = iterative_hash_template_arg (*iter, val); 1799 return val; 1800 1801 case CONSTRUCTOR: 1802 { 1803 iterative_hash_template_arg (TREE_TYPE (arg), val); 1804 for (auto &e: CONSTRUCTOR_ELTS (arg)) 1805 { 1806 val = iterative_hash_template_arg (e.index, val); 1807 val = iterative_hash_template_arg (e.value, val); 1808 } 1809 return val; 1810 } 1811 1812 case PARM_DECL: 1813 if (!DECL_ARTIFICIAL (arg)) 1814 { 1815 val = iterative_hash_object (DECL_PARM_INDEX (arg), val); 1816 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val); 1817 } 1818 return iterative_hash_template_arg (TREE_TYPE (arg), val); 1819 1820 case TEMPLATE_DECL: 1821 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg)) 1822 return iterative_hash_template_arg (TREE_TYPE (arg), val); 1823 break; 1824 1825 case TARGET_EXPR: 1826 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val); 1827 1828 case PTRMEM_CST: 1829 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val); 1830 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val); 1831 1832 case TEMPLATE_PARM_INDEX: 1833 val = iterative_hash_template_arg 1834 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val); 1835 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val); 1836 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val); 1837 1838 case TRAIT_EXPR: 1839 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val); 1840 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val); 1841 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val); 1842 1843 case BASELINK: 1844 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)), 1845 val); 1846 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)), 1847 val); 1848 1849 case MODOP_EXPR: 1850 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val); 1851 code = TREE_CODE (TREE_OPERAND (arg, 1)); 1852 val = iterative_hash_object (code, val); 1853 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val); 1854 1855 case LAMBDA_EXPR: 1856 /* [temp.over.link] Two lambda-expressions are never considered 1857 equivalent. 1858 1859 So just hash the closure type. */ 1860 return iterative_hash_template_arg (TREE_TYPE (arg), val); 1861 1862 case CAST_EXPR: 1863 case IMPLICIT_CONV_EXPR: 1864 case STATIC_CAST_EXPR: 1865 case REINTERPRET_CAST_EXPR: 1866 case CONST_CAST_EXPR: 1867 case DYNAMIC_CAST_EXPR: 1868 case NEW_EXPR: 1869 val = iterative_hash_template_arg (TREE_TYPE (arg), val); 1870 /* Now hash operands as usual. */ 1871 break; 1872 1873 case CALL_EXPR: 1874 { 1875 tree fn = CALL_EXPR_FN (arg); 1876 if (tree name = call_expr_dependent_name (arg)) 1877 { 1878 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) 1879 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val); 1880 fn = name; 1881 } 1882 val = iterative_hash_template_arg (fn, val); 1883 call_expr_arg_iterator ai; 1884 for (tree x = first_call_expr_arg (arg, &ai); x; 1885 x = next_call_expr_arg (&ai)) 1886 val = iterative_hash_template_arg (x, val); 1887 return val; 1888 } 1889 1890 default: 1891 break; 1892 } 1893 1894 char tclass = TREE_CODE_CLASS (code); 1895 switch (tclass) 1896 { 1897 case tcc_type: 1898 if (tree ats = alias_template_specialization_p (arg, nt_transparent)) 1899 { 1900 // We want an alias specialization that survived strip_typedefs 1901 // to hash differently from its TYPE_CANONICAL, to avoid hash 1902 // collisions that compare as different in template_args_equal. 1903 // These could be dependent specializations that strip_typedefs 1904 // left alone, or untouched specializations because 1905 // coerce_template_parms returns the unconverted template 1906 // arguments if it sees incomplete argument packs. 1907 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats); 1908 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti)); 1909 } 1910 1911 switch (code) 1912 { 1913 case DECLTYPE_TYPE: 1914 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val); 1915 break; 1916 1917 case TYPENAME_TYPE: 1918 if (comparing_specializations) 1919 { 1920 /* Hash the components that are relevant to TYPENAME_TYPE 1921 equivalence as determined by structural_comptypes. We 1922 can only coherently do this when comparing_specializations 1923 is set, because otherwise structural_comptypes tries 1924 resolving TYPENAME_TYPE via the current instantiation. */ 1925 tree context = TYPE_MAIN_VARIANT (TYPE_CONTEXT (arg)); 1926 tree fullname = TYPENAME_TYPE_FULLNAME (arg); 1927 val = iterative_hash_template_arg (context, val); 1928 val = iterative_hash_template_arg (fullname, val); 1929 } 1930 break; 1931 1932 default: 1933 if (tree canonical = TYPE_CANONICAL (arg)) 1934 val = iterative_hash_object (TYPE_HASH (canonical), val); 1935 else if (tree ti = TYPE_TEMPLATE_INFO (arg)) 1936 { 1937 val = iterative_hash_template_arg (TI_TEMPLATE (ti), val); 1938 val = iterative_hash_template_arg (TI_ARGS (ti), val); 1939 } 1940 break; 1941 } 1942 1943 return val; 1944 1945 case tcc_declaration: 1946 case tcc_constant: 1947 return iterative_hash_expr (arg, val); 1948 1949 default: 1950 gcc_assert (IS_EXPR_CODE_CLASS (tclass)); 1951 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i) 1952 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val); 1953 return val; 1954 } 1955 } 1956 1957 /* Unregister the specialization SPEC as a specialization of TMPL. 1958 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true 1959 if the SPEC was listed as a specialization of TMPL. 1960 1961 Note that SPEC has been ggc_freed, so we can't look inside it. */ 1962 1963 bool 1964 reregister_specialization (tree spec, tree tinfo, tree new_spec) 1965 { 1966 spec_entry *entry; 1967 spec_entry elt; 1968 1969 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo)); 1970 elt.args = TI_ARGS (tinfo); 1971 elt.spec = NULL_TREE; 1972 1973 entry = decl_specializations->find (&elt); 1974 if (entry != NULL) 1975 { 1976 gcc_assert (entry->spec == spec || entry->spec == new_spec); 1977 gcc_assert (new_spec != NULL_TREE); 1978 entry->spec = new_spec; 1979 return 1; 1980 } 1981 1982 return 0; 1983 } 1984 1985 /* Like register_specialization, but for local declarations. We are 1986 registering SPEC, an instantiation of TMPL. */ 1987 1988 void 1989 register_local_specialization (tree spec, tree tmpl) 1990 { 1991 gcc_assert (tmpl != spec); 1992 local_specializations->put (tmpl, spec); 1993 } 1994 1995 /* Registers T as a specialization of itself. This is used to preserve 1996 the references to already-parsed parameters when instantiating 1997 postconditions. */ 1998 1999 void 2000 register_local_identity (tree t) 2001 { 2002 local_specializations->put (t, t); 2003 } 2004 2005 /* TYPE is a class type. Returns true if TYPE is an explicitly 2006 specialized class. */ 2007 2008 bool 2009 explicit_class_specialization_p (tree type) 2010 { 2011 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) 2012 return false; 2013 return !uses_template_parms (CLASSTYPE_TI_ARGS (type)); 2014 } 2015 2016 /* Print the list of functions at FNS, going through all the overloads 2017 for each element of the list. Alternatively, FNS cannot be a 2018 TREE_LIST, in which case it will be printed together with all the 2019 overloads. 2020 2021 MORE and *STR should respectively be FALSE and NULL when the function 2022 is called from the outside. They are used internally on recursive 2023 calls. print_candidates manages the two parameters and leaves NULL 2024 in *STR when it ends. */ 2025 2026 static void 2027 print_candidates_1 (tree fns, char **str, bool more = false) 2028 { 2029 if (TREE_CODE (fns) == TREE_LIST) 2030 for (; fns; fns = TREE_CHAIN (fns)) 2031 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns)); 2032 else 2033 for (lkp_iterator iter (fns); iter;) 2034 { 2035 tree cand = *iter; 2036 ++iter; 2037 2038 const char *pfx = *str; 2039 if (!pfx) 2040 { 2041 if (more || iter) 2042 pfx = _("candidates are:"); 2043 else 2044 pfx = _("candidate is:"); 2045 *str = get_spaces (pfx); 2046 } 2047 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand); 2048 } 2049 } 2050 2051 /* Print the list of candidate FNS in an error message. FNS can also 2052 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */ 2053 2054 void 2055 print_candidates (tree fns) 2056 { 2057 char *str = NULL; 2058 print_candidates_1 (fns, &str); 2059 free (str); 2060 } 2061 2062 /* Get a (possibly) constrained template declaration for the 2063 purpose of ordering candidates. */ 2064 static tree 2065 get_template_for_ordering (tree list) 2066 { 2067 gcc_assert (TREE_CODE (list) == TREE_LIST); 2068 tree f = TREE_VALUE (list); 2069 if (tree ti = DECL_TEMPLATE_INFO (f)) 2070 return TI_TEMPLATE (ti); 2071 return f; 2072 } 2073 2074 /* Among candidates having the same signature, return the 2075 most constrained or NULL_TREE if there is no best candidate. 2076 If the signatures of candidates vary (e.g., template 2077 specialization vs. member function), then there can be no 2078 most constrained. 2079 2080 Note that we don't compare constraints on the functions 2081 themselves, but rather those of their templates. */ 2082 static tree 2083 most_constrained_function (tree candidates) 2084 { 2085 // Try to find the best candidate in a first pass. 2086 tree champ = candidates; 2087 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c)) 2088 { 2089 int winner = more_constrained (get_template_for_ordering (champ), 2090 get_template_for_ordering (c)); 2091 if (winner == -1) 2092 champ = c; // The candidate is more constrained 2093 else if (winner == 0) 2094 return NULL_TREE; // Neither is more constrained 2095 } 2096 2097 // Verify that the champ is better than previous candidates. 2098 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) { 2099 if (!more_constrained (get_template_for_ordering (champ), 2100 get_template_for_ordering (c))) 2101 return NULL_TREE; 2102 } 2103 2104 return champ; 2105 } 2106 2107 2108 /* Returns the template (one of the functions given by TEMPLATE_ID) 2109 which can be specialized to match the indicated DECL with the 2110 explicit template args given in TEMPLATE_ID. The DECL may be 2111 NULL_TREE if none is available. In that case, the functions in 2112 TEMPLATE_ID are non-members. 2113 2114 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a 2115 specialization of a member template. 2116 2117 The TEMPLATE_COUNT is the number of references to qualifying 2118 template classes that appeared in the name of the function. See 2119 check_explicit_specialization for a more accurate description. 2120 2121 TSK indicates what kind of template declaration (if any) is being 2122 declared. TSK_TEMPLATE indicates that the declaration given by 2123 DECL, though a FUNCTION_DECL, has template parameters, and is 2124 therefore a template function. 2125 2126 The template args (those explicitly specified and those deduced) 2127 are output in a newly created vector *TARGS_OUT. 2128 2129 If it is impossible to determine the result, an error message is 2130 issued. The error_mark_node is returned to indicate failure. */ 2131 2132 static tree 2133 determine_specialization (tree template_id, 2134 tree decl, 2135 tree* targs_out, 2136 int need_member_template, 2137 int template_count, 2138 tmpl_spec_kind tsk) 2139 { 2140 tree fns; 2141 tree targs; 2142 tree explicit_targs; 2143 tree candidates = NULL_TREE; 2144 2145 /* A TREE_LIST of templates of which DECL may be a specialization. 2146 The TREE_VALUE of each node is a TEMPLATE_DECL. The 2147 corresponding TREE_PURPOSE is the set of template arguments that, 2148 when used to instantiate the template, would produce a function 2149 with the signature of DECL. */ 2150 tree templates = NULL_TREE; 2151 int header_count; 2152 cp_binding_level *b; 2153 2154 *targs_out = NULL_TREE; 2155 2156 if (template_id == error_mark_node || decl == error_mark_node) 2157 return error_mark_node; 2158 2159 /* We shouldn't be specializing a member template of an 2160 unspecialized class template; we already gave an error in 2161 check_specialization_scope, now avoid crashing. */ 2162 if (!VAR_P (decl) 2163 && template_count && DECL_CLASS_SCOPE_P (decl) 2164 && template_class_depth (DECL_CONTEXT (decl)) > 0) 2165 { 2166 gcc_assert (errorcount); 2167 return error_mark_node; 2168 } 2169 2170 fns = TREE_OPERAND (template_id, 0); 2171 explicit_targs = TREE_OPERAND (template_id, 1); 2172 2173 if (fns == error_mark_node) 2174 return error_mark_node; 2175 2176 /* Check for baselinks. */ 2177 if (BASELINK_P (fns)) 2178 fns = BASELINK_FUNCTIONS (fns); 2179 2180 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns)) 2181 { 2182 error_at (DECL_SOURCE_LOCATION (decl), 2183 "%qD is not a function template", fns); 2184 return error_mark_node; 2185 } 2186 else if (VAR_P (decl) && !variable_template_p (fns)) 2187 { 2188 error ("%qD is not a variable template", fns); 2189 return error_mark_node; 2190 } 2191 2192 /* Count the number of template headers specified for this 2193 specialization. */ 2194 header_count = 0; 2195 for (b = current_binding_level; 2196 b->kind == sk_template_parms; 2197 b = b->level_chain) 2198 ++header_count; 2199 2200 tree orig_fns = fns; 2201 bool header_mismatch = false; 2202 2203 if (variable_template_p (fns)) 2204 { 2205 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns)); 2206 targs = coerce_template_parms (parms, explicit_targs, fns, 2207 tf_warning_or_error); 2208 if (targs != error_mark_node 2209 && constraints_satisfied_p (fns, targs)) 2210 templates = tree_cons (targs, fns, templates); 2211 } 2212 else for (lkp_iterator iter (fns); iter; ++iter) 2213 { 2214 tree fn = *iter; 2215 2216 if (TREE_CODE (fn) == TEMPLATE_DECL) 2217 { 2218 tree decl_arg_types; 2219 tree fn_arg_types; 2220 2221 /* In case of explicit specialization, we need to check if 2222 the number of template headers appearing in the specialization 2223 is correct. This is usually done in check_explicit_specialization, 2224 but the check done there cannot be exhaustive when specializing 2225 member functions. Consider the following code: 2226 2227 template <> void A<int>::f(int); 2228 template <> template <> void A<int>::f(int); 2229 2230 Assuming that A<int> is not itself an explicit specialization 2231 already, the first line specializes "f" which is a non-template 2232 member function, whilst the second line specializes "f" which 2233 is a template member function. So both lines are syntactically 2234 correct, and check_explicit_specialization does not reject 2235 them. 2236 2237 Here, we can do better, as we are matching the specialization 2238 against the declarations. We count the number of template 2239 headers, and we check if they match TEMPLATE_COUNT + 1 2240 (TEMPLATE_COUNT is the number of qualifying template classes, 2241 plus there must be another header for the member template 2242 itself). 2243 2244 Notice that if header_count is zero, this is not a 2245 specialization but rather a template instantiation, so there 2246 is no check we can perform here. */ 2247 if (header_count && header_count != template_count + 1) 2248 { 2249 header_mismatch = true; 2250 continue; 2251 } 2252 2253 /* Check that the number of template arguments at the 2254 innermost level for DECL is the same as for FN. */ 2255 if (current_binding_level->kind == sk_template_parms 2256 && !current_binding_level->explicit_spec_p 2257 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn)) 2258 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS 2259 (current_template_parms)))) 2260 continue; 2261 2262 /* DECL might be a specialization of FN. */ 2263 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); 2264 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); 2265 2266 /* For a non-static member function, we need to make sure 2267 that the const qualification is the same. Since 2268 get_bindings does not try to merge the "this" parameter, 2269 we must do the comparison explicitly. */ 2270 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)) 2271 { 2272 if (!same_type_p (TREE_VALUE (fn_arg_types), 2273 TREE_VALUE (decl_arg_types))) 2274 continue; 2275 2276 /* And the ref-qualification. */ 2277 if (type_memfn_rqual (TREE_TYPE (decl)) 2278 != type_memfn_rqual (TREE_TYPE (fn))) 2279 continue; 2280 } 2281 2282 /* Skip the "this" parameter and, for constructors of 2283 classes with virtual bases, the VTT parameter. A 2284 full specialization of a constructor will have a VTT 2285 parameter, but a template never will. */ 2286 decl_arg_types 2287 = skip_artificial_parms_for (decl, decl_arg_types); 2288 fn_arg_types 2289 = skip_artificial_parms_for (fn, fn_arg_types); 2290 2291 /* Function templates cannot be specializations; there are 2292 no partial specializations of functions. Therefore, if 2293 the type of DECL does not match FN, there is no 2294 match. 2295 2296 Note that it should never be the case that we have both 2297 candidates added here, and for regular member functions 2298 below. */ 2299 if (tsk == tsk_template) 2300 { 2301 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn), 2302 current_template_parms)) 2303 continue; 2304 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), 2305 TREE_TYPE (TREE_TYPE (fn)))) 2306 continue; 2307 if (!compparms (fn_arg_types, decl_arg_types)) 2308 continue; 2309 2310 tree freq = get_constraints (fn); 2311 tree dreq = get_constraints (decl); 2312 if (!freq != !dreq) 2313 continue; 2314 if (freq) 2315 { 2316 /* C++20 CA104: Substitute directly into the 2317 constraint-expression. */ 2318 tree fargs = DECL_TI_ARGS (fn); 2319 tsubst_flags_t complain = tf_none; 2320 freq = tsubst_constraint_info (freq, fargs, complain, fn); 2321 if (!cp_tree_equal (freq, dreq)) 2322 continue; 2323 } 2324 2325 candidates = tree_cons (NULL_TREE, fn, candidates); 2326 continue; 2327 } 2328 2329 /* See whether this function might be a specialization of this 2330 template. Suppress access control because we might be trying 2331 to make this specialization a friend, and we have already done 2332 access control for the declaration of the specialization. */ 2333 push_deferring_access_checks (dk_no_check); 2334 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true); 2335 pop_deferring_access_checks (); 2336 2337 if (!targs) 2338 /* We cannot deduce template arguments that when used to 2339 specialize TMPL will produce DECL. */ 2340 continue; 2341 2342 if (uses_template_parms (targs)) 2343 /* We deduced something involving 'auto', which isn't a valid 2344 template argument. */ 2345 continue; 2346 2347 /* Save this template, and the arguments deduced. */ 2348 templates = tree_cons (targs, fn, templates); 2349 } 2350 else if (need_member_template) 2351 /* FN is an ordinary member function, and we need a 2352 specialization of a member template. */ 2353 ; 2354 else if (TREE_CODE (fn) != FUNCTION_DECL) 2355 /* We can get IDENTIFIER_NODEs here in certain erroneous 2356 cases. */ 2357 ; 2358 else if (!DECL_FUNCTION_MEMBER_P (fn)) 2359 /* This is just an ordinary non-member function. Nothing can 2360 be a specialization of that. */ 2361 ; 2362 else if (DECL_ARTIFICIAL (fn)) 2363 /* Cannot specialize functions that are created implicitly. */ 2364 ; 2365 else 2366 { 2367 tree decl_arg_types; 2368 2369 /* This is an ordinary member function. However, since 2370 we're here, we can assume its enclosing class is a 2371 template class. For example, 2372 2373 template <typename T> struct S { void f(); }; 2374 template <> void S<int>::f() {} 2375 2376 Here, S<int>::f is a non-template, but S<int> is a 2377 template class. If FN has the same type as DECL, we 2378 might be in business. */ 2379 2380 if (!DECL_TEMPLATE_INFO (fn)) 2381 /* Its enclosing class is an explicit specialization 2382 of a template class. This is not a candidate. */ 2383 continue; 2384 2385 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), 2386 TREE_TYPE (TREE_TYPE (fn)))) 2387 /* The return types differ. */ 2388 continue; 2389 2390 /* Adjust the type of DECL in case FN is a static member. */ 2391 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); 2392 if (DECL_STATIC_FUNCTION_P (fn) 2393 && DECL_IOBJ_MEMBER_FUNCTION_P (decl)) 2394 decl_arg_types = TREE_CHAIN (decl_arg_types); 2395 2396 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), 2397 decl_arg_types)) 2398 continue; 2399 2400 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn) 2401 && (type_memfn_rqual (TREE_TYPE (decl)) 2402 != type_memfn_rqual (TREE_TYPE (fn)))) 2403 continue; 2404 2405 // If the deduced arguments do not satisfy the constraints, 2406 // this is not a candidate. 2407 if (flag_concepts && !constraints_satisfied_p (fn)) 2408 continue; 2409 2410 // Add the candidate. 2411 candidates = tree_cons (NULL_TREE, fn, candidates); 2412 } 2413 } 2414 2415 if (templates && TREE_CHAIN (templates)) 2416 { 2417 /* We have: 2418 2419 [temp.expl.spec] 2420 2421 It is possible for a specialization with a given function 2422 signature to be instantiated from more than one function 2423 template. In such cases, explicit specification of the 2424 template arguments must be used to uniquely identify the 2425 function template specialization being specialized. 2426 2427 Note that here, there's no suggestion that we're supposed to 2428 determine which of the candidate templates is most 2429 specialized. However, we, also have: 2430 2431 [temp.func.order] 2432 2433 Partial ordering of overloaded function template 2434 declarations is used in the following contexts to select 2435 the function template to which a function template 2436 specialization refers: 2437 2438 -- when an explicit specialization refers to a function 2439 template. 2440 2441 So, we do use the partial ordering rules, at least for now. 2442 This extension can only serve to make invalid programs valid, 2443 so it's safe. And, there is strong anecdotal evidence that 2444 the committee intended the partial ordering rules to apply; 2445 the EDG front end has that behavior, and John Spicer claims 2446 that the committee simply forgot to delete the wording in 2447 [temp.expl.spec]. */ 2448 tree tmpl = most_specialized_instantiation (templates); 2449 if (tmpl != error_mark_node) 2450 { 2451 templates = tmpl; 2452 TREE_CHAIN (templates) = NULL_TREE; 2453 } 2454 } 2455 2456 // Concepts allows multiple declarations of member functions 2457 // with the same signature. Like above, we need to rely on 2458 // on the partial ordering of those candidates to determine which 2459 // is the best. 2460 if (flag_concepts && candidates && TREE_CHAIN (candidates)) 2461 { 2462 if (tree cand = most_constrained_function (candidates)) 2463 { 2464 candidates = cand; 2465 TREE_CHAIN (cand) = NULL_TREE; 2466 } 2467 } 2468 2469 if (templates == NULL_TREE && candidates == NULL_TREE) 2470 { 2471 error ("template-id %qD for %q+D does not match any template " 2472 "declaration", template_id, decl); 2473 if (header_mismatch) 2474 inform (DECL_SOURCE_LOCATION (decl), 2475 "saw %d %<template<>%>, need %d for " 2476 "specializing a member function template", 2477 header_count, template_count + 1); 2478 print_candidates (orig_fns); 2479 return error_mark_node; 2480 } 2481 else if ((templates && TREE_CHAIN (templates)) 2482 || (candidates && TREE_CHAIN (candidates)) 2483 || (templates && candidates)) 2484 { 2485 error ("ambiguous template specialization %qD for %q+D", 2486 template_id, decl); 2487 candidates = chainon (candidates, templates); 2488 print_candidates (candidates); 2489 return error_mark_node; 2490 } 2491 2492 /* We have one, and exactly one, match. */ 2493 if (candidates) 2494 { 2495 tree fn = TREE_VALUE (candidates); 2496 *targs_out = copy_node (DECL_TI_ARGS (fn)); 2497 2498 /* Propagate the candidate's constraints to the declaration. */ 2499 if (tsk != tsk_template) 2500 set_constraints (decl, get_constraints (fn)); 2501 2502 /* DECL is a re-declaration or partial instantiation of a template 2503 function. */ 2504 if (TREE_CODE (fn) == TEMPLATE_DECL) 2505 return fn; 2506 /* It was a specialization of an ordinary member function in a 2507 template class. */ 2508 return DECL_TI_TEMPLATE (fn); 2509 } 2510 2511 /* It was a specialization of a template. */ 2512 tree tmpl = TREE_VALUE (templates); 2513 *targs_out = add_outermost_template_args (tmpl, TREE_PURPOSE (templates)); 2514 2515 /* Propagate the template's constraints to the declaration. */ 2516 if (tsk != tsk_template) 2517 set_constraints (decl, get_constraints (tmpl)); 2518 2519 return tmpl; 2520 } 2521 2522 /* Returns a chain of parameter types, exactly like the SPEC_TYPES, 2523 but with the default argument values filled in from those in the 2524 TMPL_TYPES. */ 2525 2526 static tree 2527 copy_default_args_to_explicit_spec_1 (tree spec_types, 2528 tree tmpl_types) 2529 { 2530 tree new_spec_types; 2531 2532 if (!spec_types) 2533 return NULL_TREE; 2534 2535 if (spec_types == void_list_node) 2536 return void_list_node; 2537 2538 /* Substitute into the rest of the list. */ 2539 new_spec_types = 2540 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), 2541 TREE_CHAIN (tmpl_types)); 2542 2543 /* Add the default argument for this parameter. */ 2544 return hash_tree_cons (TREE_PURPOSE (tmpl_types), 2545 TREE_VALUE (spec_types), 2546 new_spec_types); 2547 } 2548 2549 /* DECL is an explicit specialization. Replicate default arguments 2550 from the template it specializes. (That way, code like: 2551 2552 template <class T> void f(T = 3); 2553 template <> void f(double); 2554 void g () { f (); } 2555 2556 works, as required.) An alternative approach would be to look up 2557 the correct default arguments at the call-site, but this approach 2558 is consistent with how implicit instantiations are handled. */ 2559 2560 static void 2561 copy_default_args_to_explicit_spec (tree decl) 2562 { 2563 tree tmpl; 2564 tree spec_types; 2565 tree tmpl_types; 2566 tree new_spec_types; 2567 tree old_type; 2568 tree new_type; 2569 tree t; 2570 tree object_type = NULL_TREE; 2571 tree in_charge = NULL_TREE; 2572 tree vtt = NULL_TREE; 2573 2574 /* See if there's anything we need to do. */ 2575 tmpl = DECL_TI_TEMPLATE (decl); 2576 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); 2577 for (t = tmpl_types; t; t = TREE_CHAIN (t)) 2578 if (TREE_PURPOSE (t)) 2579 break; 2580 if (!t) 2581 return; 2582 2583 old_type = TREE_TYPE (decl); 2584 spec_types = TYPE_ARG_TYPES (old_type); 2585 2586 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl)) 2587 { 2588 /* Remove the this pointer, but remember the object's type for 2589 CV quals. */ 2590 object_type = TREE_TYPE (TREE_VALUE (spec_types)); 2591 spec_types = TREE_CHAIN (spec_types); 2592 tmpl_types = TREE_CHAIN (tmpl_types); 2593 2594 if (DECL_HAS_IN_CHARGE_PARM_P (decl)) 2595 { 2596 /* DECL may contain more parameters than TMPL due to the extra 2597 in-charge parameter in constructors and destructors. */ 2598 in_charge = spec_types; 2599 spec_types = TREE_CHAIN (spec_types); 2600 } 2601 if (DECL_HAS_VTT_PARM_P (decl)) 2602 { 2603 vtt = spec_types; 2604 spec_types = TREE_CHAIN (spec_types); 2605 } 2606 } 2607 2608 /* Compute the merged default arguments. */ 2609 new_spec_types = 2610 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); 2611 2612 /* Compute the new FUNCTION_TYPE. */ 2613 if (object_type) 2614 { 2615 if (vtt) 2616 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), 2617 TREE_VALUE (vtt), 2618 new_spec_types); 2619 2620 if (in_charge) 2621 /* Put the in-charge parameter back. */ 2622 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), 2623 TREE_VALUE (in_charge), 2624 new_spec_types); 2625 2626 new_type = build_method_type_directly (object_type, 2627 TREE_TYPE (old_type), 2628 new_spec_types); 2629 } 2630 else 2631 new_type = build_function_type (TREE_TYPE (old_type), 2632 new_spec_types); 2633 new_type = cp_build_type_attribute_variant (new_type, 2634 TYPE_ATTRIBUTES (old_type)); 2635 new_type = cxx_copy_lang_qualifiers (new_type, old_type); 2636 2637 TREE_TYPE (decl) = new_type; 2638 } 2639 2640 /* Return the number of template headers we expect to see for a definition 2641 or specialization of CTYPE or one of its non-template members. */ 2642 2643 int 2644 num_template_headers_for_class (tree ctype) 2645 { 2646 int num_templates = 0; 2647 2648 while (ctype && CLASS_TYPE_P (ctype)) 2649 { 2650 /* You're supposed to have one `template <...>' for every 2651 template class, but you don't need one for a full 2652 specialization. For example: 2653 2654 template <class T> struct S{}; 2655 template <> struct S<int> { void f(); }; 2656 void S<int>::f () {} 2657 2658 is correct; there shouldn't be a `template <>' for the 2659 definition of `S<int>::f'. */ 2660 if (!CLASSTYPE_TEMPLATE_INFO (ctype)) 2661 /* If CTYPE does not have template information of any 2662 kind, then it is not a template, nor is it nested 2663 within a template. */ 2664 break; 2665 if (explicit_class_specialization_p (ctype)) 2666 break; 2667 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype))) 2668 ++num_templates; 2669 2670 ctype = TYPE_CONTEXT (ctype); 2671 } 2672 2673 return num_templates; 2674 } 2675 2676 /* Do a simple sanity check on the template headers that precede the 2677 variable declaration DECL. */ 2678 2679 void 2680 check_template_variable (tree decl) 2681 { 2682 tree ctx = CP_DECL_CONTEXT (decl); 2683 int wanted = num_template_headers_for_class (ctx); 2684 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl) 2685 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))) 2686 { 2687 if (cxx_dialect < cxx14) 2688 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__14_extensions, 2689 "variable templates only available with " 2690 "%<-std=c++14%> or %<-std=gnu++14%>"); 2691 2692 // Namespace-scope variable templates should have a template header. 2693 ++wanted; 2694 } 2695 if (template_header_count > wanted) 2696 { 2697 auto_diagnostic_group d; 2698 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0, 2699 "too many template headers for %qD " 2700 "(should be %d)", 2701 decl, wanted); 2702 if (warned && CLASS_TYPE_P (ctx) 2703 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx)) 2704 inform (DECL_SOURCE_LOCATION (decl), 2705 "members of an explicitly specialized class are defined " 2706 "without a template header"); 2707 } 2708 } 2709 2710 /* An explicit specialization whose declarator-id or class-head-name is not 2711 qualified shall be declared in the nearest enclosing namespace of the 2712 template, or, if the namespace is inline (7.3.1), any namespace from its 2713 enclosing namespace set. 2714 2715 If the name declared in the explicit instantiation is an unqualified name, 2716 the explicit instantiation shall appear in the namespace where its template 2717 is declared or, if that namespace is inline (7.3.1), any namespace from its 2718 enclosing namespace set. */ 2719 2720 void 2721 check_unqualified_spec_or_inst (tree t, location_t loc) 2722 { 2723 tree tmpl = most_general_template (t); 2724 if (DECL_NAMESPACE_SCOPE_P (tmpl) 2725 && !is_nested_namespace (current_namespace, 2726 CP_DECL_CONTEXT (tmpl), true)) 2727 { 2728 if (processing_specialization) 2729 permerror (loc, "explicit specialization of %qD outside its " 2730 "namespace must use a nested-name-specifier", tmpl); 2731 else if (processing_explicit_instantiation 2732 && cxx_dialect >= cxx11) 2733 /* This was allowed in C++98, so only pedwarn. */ 2734 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD " 2735 "outside its namespace must use a nested-name-" 2736 "specifier", tmpl); 2737 } 2738 } 2739 2740 /* Warn for a template specialization SPEC that is missing some of a set 2741 of function or type attributes that the template TEMPL is declared with. 2742 ATTRLIST is a list of additional attributes that SPEC should be taken 2743 to ultimately be declared with. */ 2744 2745 static void 2746 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist) 2747 { 2748 if (DECL_FUNCTION_TEMPLATE_P (tmpl)) 2749 tmpl = DECL_TEMPLATE_RESULT (tmpl); 2750 2751 /* Avoid warning if the difference between the primary and 2752 the specialization is not in one of the attributes below. */ 2753 const char* const blacklist[] = { 2754 "alloc_align", "alloc_size", "assume_aligned", "format", 2755 "format_arg", "malloc", "nonnull", NULL 2756 }; 2757 2758 /* Put together a list of the black listed attributes that the primary 2759 template is declared with that the specialization is not, in case 2760 it's not apparent from the most recent declaration of the primary. */ 2761 pretty_printer str; 2762 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist, 2763 blacklist, &str); 2764 2765 if (!nattrs) 2766 return; 2767 2768 auto_diagnostic_group d; 2769 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes, 2770 "explicit specialization %q#D may be missing attributes", 2771 spec)) 2772 inform (DECL_SOURCE_LOCATION (tmpl), 2773 nattrs > 1 2774 ? G_("missing primary template attributes %s") 2775 : G_("missing primary template attribute %s"), 2776 pp_formatted_text (&str)); 2777 } 2778 2779 /* Check to see if the function just declared, as indicated in 2780 DECLARATOR, and in DECL, is a specialization of a function 2781 template. We may also discover that the declaration is an explicit 2782 instantiation at this point. 2783 2784 Returns DECL, or an equivalent declaration that should be used 2785 instead if all goes well. Issues an error message if something is 2786 amiss. Returns error_mark_node if the error is not easily 2787 recoverable. 2788 2789 FLAGS is a bitmask consisting of the following flags: 2790 2791 2: The function has a definition. 2792 4: The function is a friend. 2793 2794 The TEMPLATE_COUNT is the number of references to qualifying 2795 template classes that appeared in the name of the function. For 2796 example, in 2797 2798 template <class T> struct S { void f(); }; 2799 void S<int>::f(); 2800 2801 the TEMPLATE_COUNT would be 1. However, explicitly specialized 2802 classes are not counted in the TEMPLATE_COUNT, so that in 2803 2804 template <class T> struct S {}; 2805 template <> struct S<int> { void f(); } 2806 template <> void S<int>::f(); 2807 2808 the TEMPLATE_COUNT would be 0. (Note that this declaration is 2809 invalid; there should be no template <>.) 2810 2811 If the function is a specialization, it is marked as such via 2812 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO 2813 is set up correctly, and it is added to the list of specializations 2814 for that template. */ 2815 2816 tree 2817 check_explicit_specialization (tree declarator, 2818 tree decl, 2819 int template_count, 2820 int flags, 2821 tree attrlist) 2822 { 2823 int have_def = flags & 2; 2824 int is_friend = flags & 4; 2825 bool is_concept = flags & 8; 2826 int specialization = 0; 2827 int explicit_instantiation = 0; 2828 int member_specialization = 0; 2829 tree ctype = DECL_CLASS_CONTEXT (decl); 2830 tree dname = DECL_NAME (decl); 2831 tmpl_spec_kind tsk; 2832 2833 if (is_friend) 2834 { 2835 if (!processing_specialization) 2836 tsk = tsk_none; 2837 else 2838 tsk = tsk_excessive_parms; 2839 } 2840 else 2841 tsk = current_tmpl_spec_kind (template_count); 2842 2843 switch (tsk) 2844 { 2845 case tsk_none: 2846 if (processing_specialization && !VAR_P (decl)) 2847 { 2848 specialization = 1; 2849 SET_DECL_TEMPLATE_SPECIALIZATION (decl); 2850 } 2851 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR 2852 || (DECL_LANG_SPECIFIC (decl) 2853 && DECL_IMPLICIT_INSTANTIATION (decl))) 2854 { 2855 if (is_friend) 2856 /* This could be something like: 2857 2858 template <class T> void f(T); 2859 class S { friend void f<>(int); } */ 2860 specialization = 1; 2861 else 2862 { 2863 /* This case handles bogus declarations like template <> 2864 template <class T> void f<int>(); */ 2865 2866 error_at (cp_expr_loc_or_input_loc (declarator), 2867 "template-id %qE in declaration of primary template", 2868 declarator); 2869 return decl; 2870 } 2871 } 2872 break; 2873 2874 case tsk_invalid_member_spec: 2875 /* The error has already been reported in 2876 check_specialization_scope. */ 2877 return error_mark_node; 2878 2879 case tsk_invalid_expl_inst: 2880 error ("template parameter list used in explicit instantiation"); 2881 2882 /* Fall through. */ 2883 2884 case tsk_expl_inst: 2885 if (have_def) 2886 error ("definition provided for explicit instantiation"); 2887 2888 explicit_instantiation = 1; 2889 break; 2890 2891 case tsk_excessive_parms: 2892 case tsk_insufficient_parms: 2893 if (tsk == tsk_excessive_parms) 2894 error ("too many template parameter lists in declaration of %qD", 2895 decl); 2896 else if (template_header_count) 2897 error("too few template parameter lists in declaration of %qD", decl); 2898 else 2899 error("explicit specialization of %qD must be introduced by " 2900 "%<template <>%>", decl); 2901 2902 /* Fall through. */ 2903 case tsk_expl_spec: 2904 if (is_concept) 2905 error ("explicit specialization declared %<concept%>"); 2906 2907 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR) 2908 /* In cases like template<> constexpr bool v = true; 2909 We'll give an error in check_template_variable. */ 2910 break; 2911 2912 SET_DECL_TEMPLATE_SPECIALIZATION (decl); 2913 if (ctype) 2914 member_specialization = 1; 2915 else 2916 specialization = 1; 2917 break; 2918 2919 case tsk_template: 2920 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) 2921 { 2922 /* This case handles bogus declarations like template <> 2923 template <class T> void f<int>(); */ 2924 2925 if (!uses_template_parms (TREE_OPERAND (declarator, 1))) 2926 error_at (cp_expr_loc_or_input_loc (declarator), 2927 "template-id %qE in declaration of primary template", 2928 declarator); 2929 else if (variable_template_p (TREE_OPERAND (declarator, 0))) 2930 { 2931 /* Partial specialization of variable template. */ 2932 SET_DECL_TEMPLATE_SPECIALIZATION (decl); 2933 specialization = 1; 2934 goto ok; 2935 } 2936 else if (cxx_dialect < cxx14) 2937 error_at (cp_expr_loc_or_input_loc (declarator), 2938 "non-type partial specialization %qE " 2939 "is not allowed", declarator); 2940 else 2941 error_at (cp_expr_loc_or_input_loc (declarator), 2942 "non-class, non-variable partial specialization %qE " 2943 "is not allowed", declarator); 2944 return decl; 2945 ok:; 2946 } 2947 2948 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) 2949 /* This is a specialization of a member template, without 2950 specialization the containing class. Something like: 2951 2952 template <class T> struct S { 2953 template <class U> void f (U); 2954 }; 2955 template <> template <class U> void S<int>::f(U) {} 2956 2957 That's a specialization -- but of the entire template. */ 2958 specialization = 1; 2959 break; 2960 2961 default: 2962 gcc_unreachable (); 2963 } 2964 2965 if ((specialization || member_specialization) 2966 /* This doesn't apply to variable templates. */ 2967 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl))) 2968 { 2969 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); 2970 for (; t; t = TREE_CHAIN (t)) 2971 if (TREE_PURPOSE (t)) 2972 { 2973 permerror (input_location, 2974 "default argument specified in explicit specialization"); 2975 break; 2976 } 2977 } 2978 2979 if (specialization || member_specialization || explicit_instantiation) 2980 { 2981 tree tmpl = NULL_TREE; 2982 tree targs = NULL_TREE; 2983 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR); 2984 bool found_hidden = false; 2985 2986 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ 2987 if (!was_template_id) 2988 { 2989 tree fns; 2990 2991 gcc_assert (identifier_p (declarator)); 2992 if (ctype) 2993 fns = dname; 2994 else 2995 { 2996 /* If there is no class context, the explicit instantiation 2997 must be at namespace scope. */ 2998 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl)); 2999 3000 /* Find the namespace binding, using the declaration 3001 context. */ 3002 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname, 3003 LOOK_want::NORMAL, true); 3004 if (fns == error_mark_node) 3005 { 3006 /* If lookup fails, look for a friend declaration so we can 3007 give a better diagnostic. */ 3008 fns = (lookup_qualified_name 3009 (CP_DECL_CONTEXT (decl), dname, 3010 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND, 3011 /*complain*/true)); 3012 found_hidden = true; 3013 } 3014 3015 if (fns == error_mark_node || !is_overloaded_fn (fns)) 3016 { 3017 error ("%qD is not a template function", dname); 3018 fns = error_mark_node; 3019 } 3020 } 3021 3022 declarator = lookup_template_function (fns, NULL_TREE); 3023 } 3024 3025 if (declarator == error_mark_node) 3026 return error_mark_node; 3027 3028 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) 3029 { 3030 if (!explicit_instantiation) 3031 /* A specialization in class scope. This is invalid, 3032 but the error will already have been flagged by 3033 check_specialization_scope. */ 3034 return error_mark_node; 3035 else 3036 { 3037 /* It's not valid to write an explicit instantiation in 3038 class scope, e.g.: 3039 3040 class C { template void f(); } 3041 3042 This case is caught by the parser. However, on 3043 something like: 3044 3045 template class C { void f(); }; 3046 3047 (which is invalid) we can get here. The error will be 3048 issued later. */ 3049 ; 3050 } 3051 3052 return decl; 3053 } 3054 else if (ctype != NULL_TREE 3055 && (identifier_p (TREE_OPERAND (declarator, 0)))) 3056 { 3057 // We'll match variable templates in start_decl. 3058 if (VAR_P (decl)) 3059 return decl; 3060 3061 /* Find the list of functions in ctype that have the same 3062 name as the declared function. */ 3063 tree name = TREE_OPERAND (declarator, 0); 3064 3065 if (constructor_name_p (name, ctype)) 3066 { 3067 if (DECL_CONSTRUCTOR_P (decl) 3068 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype) 3069 : !CLASSTYPE_DESTRUCTOR (ctype)) 3070 { 3071 /* From [temp.expl.spec]: 3072 3073 If such an explicit specialization for the member 3074 of a class template names an implicitly-declared 3075 special member function (clause _special_), the 3076 program is ill-formed. 3077 3078 Similar language is found in [temp.explicit]. */ 3079 error ("specialization of implicitly-declared special member function"); 3080 return error_mark_node; 3081 } 3082 3083 name = DECL_NAME (decl); 3084 } 3085 3086 /* For a type-conversion operator, We might be looking for 3087 `operator int' which will be a specialization of 3088 `operator T'. Grab all the conversion operators, and 3089 then select from them. */ 3090 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name) 3091 ? conv_op_identifier : name); 3092 3093 if (fns == NULL_TREE) 3094 { 3095 error ("no member function %qD declared in %qT", name, ctype); 3096 return error_mark_node; 3097 } 3098 else 3099 TREE_OPERAND (declarator, 0) = fns; 3100 } 3101 3102 /* Figure out what exactly is being specialized at this point. 3103 Note that for an explicit instantiation, even one for a 3104 member function, we cannot tell a priori whether the 3105 instantiation is for a member template, or just a member 3106 function of a template class. Even if a member template is 3107 being instantiated, the member template arguments may be 3108 elided if they can be deduced from the rest of the 3109 declaration. */ 3110 tmpl = determine_specialization (declarator, decl, 3111 &targs, 3112 member_specialization, 3113 template_count, 3114 tsk); 3115 3116 if (!tmpl || tmpl == error_mark_node) 3117 /* We couldn't figure out what this declaration was 3118 specializing. */ 3119 return error_mark_node; 3120 else 3121 { 3122 if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL) 3123 { 3124 auto_diagnostic_group d; 3125 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0, 3126 "friend declaration %qD is not visible to " 3127 "explicit specialization", tmpl)) 3128 inform (DECL_SOURCE_LOCATION (tmpl), 3129 "friend declaration here"); 3130 } 3131 3132 if (!ctype && !is_friend 3133 && CP_DECL_CONTEXT (decl) == current_namespace) 3134 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl)); 3135 3136 tree gen_tmpl = most_general_template (tmpl); 3137 3138 if (explicit_instantiation) 3139 { 3140 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that 3141 is done by do_decl_instantiation later. */ 3142 3143 int arg_depth = TMPL_ARGS_DEPTH (targs); 3144 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); 3145 3146 if (arg_depth > parm_depth) 3147 { 3148 /* If TMPL is not the most general template (for 3149 example, if TMPL is a friend template that is 3150 injected into namespace scope), then there will 3151 be too many levels of TARGS. Remove some of them 3152 here. */ 3153 int i; 3154 tree new_targs; 3155 3156 new_targs = make_tree_vec (parm_depth); 3157 for (i = arg_depth - parm_depth; i < arg_depth; ++i) 3158 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) 3159 = TREE_VEC_ELT (targs, i); 3160 targs = new_targs; 3161 } 3162 3163 return instantiate_template (tmpl, targs, tf_error); 3164 } 3165 3166 /* If we thought that the DECL was a member function, but it 3167 turns out to be specializing a static member function, 3168 make DECL a static member function as well. */ 3169 if (DECL_FUNCTION_TEMPLATE_P (tmpl) 3170 && DECL_STATIC_FUNCTION_P (tmpl) 3171 && DECL_IOBJ_MEMBER_FUNCTION_P (decl)) 3172 revert_static_member_fn (decl); 3173 3174 /* If this is a specialization of a member template of a 3175 template class, we want to return the TEMPLATE_DECL, not 3176 the specialization of it. */ 3177 if (tsk == tsk_template && !was_template_id) 3178 { 3179 tree result = DECL_TEMPLATE_RESULT (tmpl); 3180 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); 3181 DECL_INITIAL (result) = NULL_TREE; 3182 if (have_def) 3183 { 3184 tree parm; 3185 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); 3186 DECL_SOURCE_LOCATION (result) 3187 = DECL_SOURCE_LOCATION (decl); 3188 /* We want to use the argument list specified in the 3189 definition, not in the original declaration. */ 3190 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl); 3191 for (parm = DECL_ARGUMENTS (result); parm; 3192 parm = DECL_CHAIN (parm)) 3193 DECL_CONTEXT (parm) = result; 3194 } 3195 decl = register_specialization (tmpl, gen_tmpl, targs, 3196 is_friend, 0); 3197 remove_contract_attributes (result); 3198 return decl; 3199 } 3200 3201 /* Set up the DECL_TEMPLATE_INFO for DECL. */ 3202 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs); 3203 3204 if (was_template_id) 3205 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true; 3206 3207 /* Inherit default function arguments from the template 3208 DECL is specializing. */ 3209 if (DECL_FUNCTION_TEMPLATE_P (tmpl)) 3210 copy_default_args_to_explicit_spec (decl); 3211 3212 /* This specialization has the same protection as the 3213 template it specializes. */ 3214 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); 3215 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); 3216 3217 /* 7.1.1-1 [dcl.stc] 3218 3219 A storage-class-specifier shall not be specified in an 3220 explicit specialization... 3221 3222 The parser rejects these, so unless action is taken here, 3223 explicit function specializations will always appear with 3224 global linkage. 3225 3226 The action recommended by the C++ CWG in response to C++ 3227 defect report 605 is to make the storage class and linkage 3228 of the explicit specialization match the templated function: 3229 3230 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605 3231 */ 3232 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl)) 3233 { 3234 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl); 3235 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL); 3236 3237 /* A concept cannot be specialized. */ 3238 if (DECL_DECLARED_CONCEPT_P (tmpl_func)) 3239 { 3240 error ("explicit specialization of function concept %qD", 3241 gen_tmpl); 3242 return error_mark_node; 3243 } 3244 3245 /* This specialization has the same linkage and visibility as 3246 the function template it specializes. */ 3247 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func); 3248 if (! TREE_PUBLIC (decl)) 3249 { 3250 DECL_INTERFACE_KNOWN (decl) = 1; 3251 DECL_NOT_REALLY_EXTERN (decl) = 1; 3252 } 3253 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func); 3254 if (DECL_VISIBILITY_SPECIFIED (tmpl_func)) 3255 { 3256 DECL_VISIBILITY_SPECIFIED (decl) = 1; 3257 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func); 3258 } 3259 } 3260 3261 /* If DECL is a friend declaration, declared using an 3262 unqualified name, the namespace associated with DECL may 3263 have been set incorrectly. For example, in: 3264 3265 template <typename T> void f(T); 3266 namespace N { 3267 struct S { friend void f<int>(int); } 3268 } 3269 3270 we will have set the DECL_CONTEXT for the friend 3271 declaration to N, rather than to the global namespace. */ 3272 if (DECL_NAMESPACE_SCOPE_P (decl)) 3273 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl); 3274 3275 if (is_friend && !have_def) 3276 /* This is not really a declaration of a specialization. 3277 It's just the name of an instantiation. But, it's not 3278 a request for an instantiation, either. */ 3279 SET_DECL_IMPLICIT_INSTANTIATION (decl); 3280 else if (TREE_CODE (decl) == FUNCTION_DECL) 3281 /* A specialization is not necessarily COMDAT. */ 3282 DECL_COMDAT (decl) = (TREE_PUBLIC (decl) 3283 && DECL_DECLARED_INLINE_P (decl)); 3284 else if (VAR_P (decl)) 3285 DECL_COMDAT (decl) = false; 3286 3287 /* If this is a full specialization, register it so that we can find 3288 it again. Partial specializations will be registered in 3289 process_partial_specialization. */ 3290 if (!processing_template_decl) 3291 { 3292 warn_spec_missing_attributes (gen_tmpl, decl, attrlist); 3293 3294 decl = register_specialization (decl, gen_tmpl, targs, 3295 is_friend, 0); 3296 } 3297 3298 /* If this is a specialization, splice any contracts that may have 3299 been inherited from the template, removing them. */ 3300 if (decl != error_mark_node && DECL_TEMPLATE_SPECIALIZATION (decl)) 3301 remove_contract_attributes (decl); 3302 3303 /* A 'structor should already have clones. */ 3304 gcc_assert (decl == error_mark_node 3305 || variable_template_p (tmpl) 3306 || !(DECL_CONSTRUCTOR_P (decl) 3307 || DECL_DESTRUCTOR_P (decl)) 3308 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl))); 3309 } 3310 } 3311 3312 return decl; 3313 } 3314 3315 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template 3316 parameters. These are represented in the same format used for 3317 DECL_TEMPLATE_PARMS. */ 3318 3319 int 3320 comp_template_parms (const_tree parms1, const_tree parms2) 3321 { 3322 if (parms1 == parms2) 3323 return 1; 3324 3325 tree t1 = TREE_VALUE (parms1); 3326 tree t2 = TREE_VALUE (parms2); 3327 int i; 3328 3329 gcc_assert (TREE_CODE (t1) == TREE_VEC); 3330 gcc_assert (TREE_CODE (t2) == TREE_VEC); 3331 3332 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) 3333 return 0; 3334 3335 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) 3336 { 3337 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); 3338 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); 3339 3340 /* If either of the template parameters are invalid, assume 3341 they match for the sake of error recovery. */ 3342 if (error_operand_p (parm1) || error_operand_p (parm2)) 3343 return 1; 3344 3345 if (TREE_CODE (parm1) != TREE_CODE (parm2)) 3346 return 0; 3347 3348 if (TREE_CODE (parm1) == TYPE_DECL 3349 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm1)) 3350 == TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm2)))) 3351 continue; 3352 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) 3353 return 0; 3354 } 3355 3356 return 1; 3357 } 3358 3359 /* Returns true if two template parameters are declared with 3360 equivalent constraints. */ 3361 3362 static bool 3363 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2) 3364 { 3365 tree req1 = TREE_TYPE (parm1); 3366 tree req2 = TREE_TYPE (parm2); 3367 if (!req1 != !req2) 3368 return false; 3369 if (req1) 3370 return cp_tree_equal (req1, req2); 3371 return true; 3372 } 3373 3374 /* Returns true when two template parameters are equivalent. */ 3375 3376 static bool 3377 template_parameters_equivalent_p (const_tree parm1, const_tree parm2) 3378 { 3379 tree decl1 = TREE_VALUE (parm1); 3380 tree decl2 = TREE_VALUE (parm2); 3381 3382 /* If either of the template parameters are invalid, assume 3383 they match for the sake of error recovery. */ 3384 if (error_operand_p (decl1) || error_operand_p (decl2)) 3385 return true; 3386 3387 /* ... they declare parameters of the same kind. */ 3388 if (TREE_CODE (decl1) != TREE_CODE (decl2)) 3389 return false; 3390 3391 /* ... one parameter was introduced by a parameter declaration, then 3392 both are. This case arises as a result of eagerly rewriting declarations 3393 during parsing. */ 3394 if (DECL_IMPLICIT_TEMPLATE_PARM_P (decl1) 3395 != DECL_IMPLICIT_TEMPLATE_PARM_P (decl2)) 3396 return false; 3397 3398 /* ... if either declares a pack, they both do. */ 3399 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2)) 3400 return false; 3401 3402 if (TREE_CODE (decl1) == PARM_DECL) 3403 { 3404 /* ... if they declare non-type parameters, the types are equivalent. */ 3405 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2))) 3406 return false; 3407 } 3408 else if (TREE_CODE (decl2) == TEMPLATE_DECL) 3409 { 3410 /* ... if they declare template template parameters, their template 3411 parameter lists are equivalent. */ 3412 if (!template_heads_equivalent_p (decl1, decl2)) 3413 return false; 3414 } 3415 3416 /* ... if they are declared with a qualified-concept name, they both 3417 are, and those names are equivalent. */ 3418 return template_parameter_constraints_equivalent_p (parm1, parm2); 3419 } 3420 3421 /* Returns true if two template parameters lists are equivalent. 3422 Two template parameter lists are equivalent if they have the 3423 same length and their corresponding parameters are equivalent. 3424 3425 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the 3426 data structure returned by DECL_TEMPLATE_PARMS. 3427 3428 This is generally the same implementation as comp_template_parms 3429 except that it also the concept names and arguments used to 3430 introduce parameters. */ 3431 3432 static bool 3433 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2) 3434 { 3435 if (parms1 == parms2) 3436 return true; 3437 3438 tree list1 = TREE_VALUE (parms1); 3439 tree list2 = TREE_VALUE (parms2); 3440 3441 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2)) 3442 return 0; 3443 3444 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i) 3445 { 3446 tree parm1 = TREE_VEC_ELT (list1, i); 3447 tree parm2 = TREE_VEC_ELT (list2, i); 3448 if (!template_parameters_equivalent_p (parm1, parm2)) 3449 return false; 3450 } 3451 3452 return true; 3453 } 3454 3455 /* Return true if the requires-clause of the template parameter lists are 3456 equivalent and false otherwise. */ 3457 static bool 3458 template_requirements_equivalent_p (const_tree parms1, const_tree parms2) 3459 { 3460 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1); 3461 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2); 3462 if ((req1 != NULL_TREE) != (req2 != NULL_TREE)) 3463 return false; 3464 if (!cp_tree_equal (req1, req2)) 3465 return false; 3466 return true; 3467 } 3468 3469 /* Returns true if two template heads are equivalent. 17.6.6.1p6: 3470 Two template heads are equivalent if their template parameter 3471 lists are equivalent and their requires clauses are equivalent. 3472 3473 In pre-C++20, this is equivalent to calling comp_template_parms 3474 for the template parameters of TMPL1 and TMPL2. */ 3475 3476 bool 3477 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2) 3478 { 3479 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1); 3480 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2); 3481 3482 /* Don't change the matching rules for pre-C++20. */ 3483 if (cxx_dialect < cxx20) 3484 return comp_template_parms (parms1, parms2); 3485 3486 /* ... have the same number of template parameters, and their 3487 corresponding parameters are equivalent. */ 3488 if (!template_parameter_lists_equivalent_p (parms1, parms2)) 3489 return false; 3490 3491 /* ... if either has a requires-clause, they both do and their 3492 corresponding constraint-expressions are equivalent. */ 3493 return template_requirements_equivalent_p (parms1, parms2); 3494 } 3495 3496 /* Determine whether PARM is a parameter pack. */ 3497 3498 bool 3499 template_parameter_pack_p (const_tree parm) 3500 { 3501 /* Determine if we have a non-type template parameter pack. */ 3502 if (TREE_CODE (parm) == PARM_DECL) 3503 return (DECL_TEMPLATE_PARM_P (parm) 3504 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))); 3505 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX) 3506 return TEMPLATE_PARM_PARAMETER_PACK (parm); 3507 3508 /* If this is a list of template parameters, we could get a 3509 TYPE_DECL or a TEMPLATE_DECL. */ 3510 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL) 3511 parm = TREE_TYPE (parm); 3512 3513 /* Otherwise it must be a type template parameter. */ 3514 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM 3515 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) 3516 && TEMPLATE_TYPE_PARAMETER_PACK (parm)); 3517 } 3518 3519 /* Determine if T is a function parameter pack. */ 3520 3521 bool 3522 function_parameter_pack_p (const_tree t) 3523 { 3524 if (t && TREE_CODE (t) == PARM_DECL) 3525 return DECL_PACK_P (t); 3526 return false; 3527 } 3528 3529 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST. 3530 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */ 3531 3532 tree 3533 get_function_template_decl (const_tree primary_func_tmpl_inst) 3534 { 3535 if (! primary_func_tmpl_inst 3536 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL 3537 || ! primary_template_specialization_p (primary_func_tmpl_inst)) 3538 return NULL; 3539 3540 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst)); 3541 } 3542 3543 /* Return true iff the function parameter PARAM_DECL was expanded 3544 from the function parameter pack PACK. */ 3545 3546 bool 3547 function_parameter_expanded_from_pack_p (tree param_decl, tree pack) 3548 { 3549 if (DECL_ARTIFICIAL (param_decl) 3550 || !function_parameter_pack_p (pack)) 3551 return false; 3552 3553 /* The parameter pack and its pack arguments have the same 3554 DECL_PARM_INDEX. */ 3555 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl); 3556 } 3557 3558 /* Determine whether ARGS describes a variadic template args list, 3559 i.e., one that is terminated by a template argument pack. */ 3560 3561 static bool 3562 template_args_variadic_p (tree args) 3563 { 3564 int nargs; 3565 tree last_parm; 3566 3567 if (args == NULL_TREE) 3568 return false; 3569 3570 args = INNERMOST_TEMPLATE_ARGS (args); 3571 nargs = TREE_VEC_LENGTH (args); 3572 3573 if (nargs == 0) 3574 return false; 3575 3576 last_parm = TREE_VEC_ELT (args, nargs - 1); 3577 3578 return ARGUMENT_PACK_P (last_parm); 3579 } 3580 3581 /* Generate a new name for the parameter pack name NAME (an 3582 IDENTIFIER_NODE) that incorporates its */ 3583 3584 static tree 3585 make_ith_pack_parameter_name (tree name, int i) 3586 { 3587 /* Munge the name to include the parameter index. */ 3588 #define NUMBUF_LEN 128 3589 char numbuf[NUMBUF_LEN]; 3590 char* newname; 3591 int newname_len; 3592 3593 if (name == NULL_TREE) 3594 return name; 3595 snprintf (numbuf, NUMBUF_LEN, "%i", i); 3596 newname_len = IDENTIFIER_LENGTH (name) 3597 + strlen (numbuf) + 2; 3598 newname = (char*)alloca (newname_len); 3599 snprintf (newname, newname_len, 3600 "%s#%i", IDENTIFIER_POINTER (name), i); 3601 return get_identifier (newname); 3602 } 3603 3604 /* Return true if T is a primary function, class or alias template 3605 specialization, not including the template pattern. */ 3606 3607 bool 3608 primary_template_specialization_p (const_tree t) 3609 { 3610 if (!t) 3611 return false; 3612 3613 if (VAR_OR_FUNCTION_DECL_P (t)) 3614 return (DECL_LANG_SPECIFIC (t) 3615 && DECL_USE_TEMPLATE (t) 3616 && DECL_TEMPLATE_INFO (t) 3617 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))); 3618 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t))) 3619 return (CLASSTYPE_TEMPLATE_INFO (t) 3620 && CLASSTYPE_USE_TEMPLATE (t) 3621 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))); 3622 else if (alias_template_specialization_p (t, nt_transparent)) 3623 return true; 3624 return false; 3625 } 3626 3627 /* Return true if PARM is a template template parameter. */ 3628 3629 bool 3630 template_template_parameter_p (const_tree parm) 3631 { 3632 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm); 3633 } 3634 3635 /* Return true iff PARM is a DECL representing a type template 3636 parameter. */ 3637 3638 bool 3639 template_type_parameter_p (const_tree parm) 3640 { 3641 return (parm 3642 && (TREE_CODE (parm) == TYPE_DECL 3643 || TREE_CODE (parm) == TEMPLATE_DECL) 3644 && DECL_TEMPLATE_PARM_P (parm)); 3645 } 3646 3647 /* Return the template parameters of T if T is a 3648 primary template instantiation, NULL otherwise. */ 3649 3650 tree 3651 get_primary_template_innermost_parameters (const_tree t) 3652 { 3653 tree parms = NULL, template_info = NULL; 3654 3655 if ((template_info = get_template_info (t)) 3656 && primary_template_specialization_p (t)) 3657 parms = INNERMOST_TEMPLATE_PARMS 3658 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info))); 3659 3660 return parms; 3661 } 3662 3663 /* Returns the template arguments of T if T is a template instantiation, 3664 NULL otherwise. */ 3665 3666 tree 3667 get_template_innermost_arguments (const_tree t) 3668 { 3669 tree args = NULL, template_info = NULL; 3670 3671 if ((template_info = get_template_info (t)) 3672 && TI_ARGS (template_info)) 3673 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info)); 3674 3675 return args; 3676 } 3677 3678 /* Return the argument pack elements of T if T is a template argument pack, 3679 NULL otherwise. */ 3680 3681 tree 3682 get_template_argument_pack_elems (const_tree t) 3683 { 3684 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK 3685 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK) 3686 return NULL; 3687 3688 return ARGUMENT_PACK_ARGS (t); 3689 } 3690 3691 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the 3692 ARGUMENT_PACK_SELECT represents. */ 3693 3694 static tree 3695 argument_pack_select_arg (tree t) 3696 { 3697 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t)); 3698 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t)); 3699 3700 /* If the selected argument is an expansion E, that most likely means we were 3701 called from gen_elem_of_pack_expansion_instantiation during the 3702 substituting of an argument pack (of which the Ith element is a pack 3703 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion. 3704 In this case, the Ith element resulting from this substituting is going to 3705 be a pack expansion, which pattern is the pattern of E. Let's return the 3706 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the 3707 resulting pack expansion from it. */ 3708 if (PACK_EXPANSION_P (arg)) 3709 { 3710 /* Make sure we aren't throwing away arg info. */ 3711 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg)); 3712 arg = PACK_EXPANSION_PATTERN (arg); 3713 } 3714 3715 return arg; 3716 } 3717 3718 /* Return a modification of ARGS that's suitable for preserving inside a hash 3719 table. In particular, this replaces each ARGUMENT_PACK_SELECT with its 3720 underlying argument. ARGS is copied (upon modification) iff COW_P. */ 3721 3722 static tree 3723 preserve_args (tree args, bool cow_p = true) 3724 { 3725 if (!args) 3726 return NULL_TREE; 3727 3728 for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i) 3729 { 3730 tree t = TREE_VEC_ELT (args, i); 3731 tree r; 3732 if (!t) 3733 r = NULL_TREE; 3734 else if (TREE_CODE (t) == ARGUMENT_PACK_SELECT) 3735 r = argument_pack_select_arg (t); 3736 else if (TREE_CODE (t) == TREE_VEC) 3737 r = preserve_args (t, cow_p); 3738 else 3739 r = t; 3740 if (r != t) 3741 { 3742 if (cow_p) 3743 { 3744 args = copy_template_args (args); 3745 cow_p = false; 3746 } 3747 TREE_VEC_ELT (args, i) = r; 3748 } 3749 } 3750 3751 return args; 3752 } 3753 3754 /* True iff FN is a function representing a built-in variadic parameter 3755 pack. */ 3756 3757 bool 3758 builtin_pack_fn_p (tree fn) 3759 { 3760 if (!fn 3761 || TREE_CODE (fn) != FUNCTION_DECL 3762 || !DECL_IS_UNDECLARED_BUILTIN (fn)) 3763 return false; 3764 3765 if (id_equal (DECL_NAME (fn), "__integer_pack")) 3766 return true; 3767 3768 return false; 3769 } 3770 3771 /* True iff CALL is a call to a function representing a built-in variadic 3772 parameter pack. */ 3773 3774 static bool 3775 builtin_pack_call_p (tree call) 3776 { 3777 if (TREE_CODE (call) != CALL_EXPR) 3778 return false; 3779 return builtin_pack_fn_p (CALL_EXPR_FN (call)); 3780 } 3781 3782 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */ 3783 3784 static tree 3785 expand_integer_pack (tree call, tree args, tsubst_flags_t complain, 3786 tree in_decl) 3787 { 3788 tree ohi = CALL_EXPR_ARG (call, 0); 3789 tree hi = tsubst_expr (ohi, args, complain, in_decl); 3790 3791 if (instantiation_dependent_expression_p (hi)) 3792 { 3793 if (hi != ohi) 3794 { 3795 call = copy_node (call); 3796 CALL_EXPR_ARG (call, 0) = hi; 3797 } 3798 tree ex = make_pack_expansion (call, complain); 3799 tree vec = make_tree_vec (1); 3800 TREE_VEC_ELT (vec, 0) = ex; 3801 return vec; 3802 } 3803 else 3804 { 3805 hi = instantiate_non_dependent_expr (hi, complain); 3806 hi = cxx_constant_value (hi, complain); 3807 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1; 3808 3809 /* Calculate the largest value of len that won't make the size of the vec 3810 overflow an int. The compiler will exceed resource limits long before 3811 this, but it seems a decent place to diagnose. */ 3812 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1; 3813 3814 if (len < 0 || len > max) 3815 { 3816 if ((complain & tf_error) 3817 && hi != error_mark_node) 3818 error ("argument to %<__integer_pack%> must be between 0 and %d", 3819 max); 3820 return error_mark_node; 3821 } 3822 3823 tree vec = make_tree_vec (len); 3824 3825 for (int i = 0; i < len; ++i) 3826 TREE_VEC_ELT (vec, i) = size_int (i); 3827 3828 return vec; 3829 } 3830 } 3831 3832 /* Return a TREE_VEC for the expansion of built-in template parameter pack 3833 CALL. */ 3834 3835 static tree 3836 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain, 3837 tree in_decl) 3838 { 3839 if (!builtin_pack_call_p (call)) 3840 return NULL_TREE; 3841 3842 tree fn = CALL_EXPR_FN (call); 3843 3844 if (id_equal (DECL_NAME (fn), "__integer_pack")) 3845 return expand_integer_pack (call, args, complain, in_decl); 3846 3847 return NULL_TREE; 3848 } 3849 3850 /* Return true if the tree T has the extra args mechanism for 3851 avoiding partial instantiation. */ 3852 3853 static bool 3854 has_extra_args_mechanism_p (const_tree t) 3855 { 3856 return (PACK_EXPANSION_P (t) /* PACK_EXPANSION_EXTRA_ARGS */ 3857 || TREE_CODE (t) == REQUIRES_EXPR /* REQUIRES_EXPR_EXTRA_ARGS */ 3858 || (TREE_CODE (t) == IF_STMT 3859 && IF_STMT_CONSTEXPR_P (t)) /* IF_STMT_EXTRA_ARGS */ 3860 || TREE_CODE (t) == LAMBDA_EXPR); /* LAMBDA_EXPR_EXTRA_ARGS */ 3861 } 3862 3863 /* Return *_EXTRA_ARGS of the given supported tree T. */ 3864 3865 static tree& 3866 tree_extra_args (tree t) 3867 { 3868 gcc_checking_assert (has_extra_args_mechanism_p (t)); 3869 3870 if (PACK_EXPANSION_P (t)) 3871 return PACK_EXPANSION_EXTRA_ARGS (t); 3872 else if (TREE_CODE (t) == REQUIRES_EXPR) 3873 return REQUIRES_EXPR_EXTRA_ARGS (t); 3874 else if (TREE_CODE (t) == IF_STMT 3875 && IF_STMT_CONSTEXPR_P (t)) 3876 return IF_STMT_EXTRA_ARGS (t); 3877 else if (TREE_CODE (t) == LAMBDA_EXPR) 3878 return LAMBDA_EXPR_EXTRA_ARGS (t); 3879 3880 gcc_unreachable (); 3881 } 3882 3883 /* Structure used to track the progress of find_parameter_packs_r. */ 3884 struct find_parameter_pack_data 3885 { 3886 /* TREE_LIST that will contain all of the parameter packs found by 3887 the traversal. */ 3888 tree* parameter_packs; 3889 3890 /* Set of AST nodes that have been visited by the traversal. */ 3891 hash_set<tree> *visited; 3892 3893 /* True iff we're making a type pack expansion. */ 3894 bool type_pack_expansion_p; 3895 3896 /* True iff we found a subtree that has the extra args mechanism. */ 3897 bool found_extra_args_tree_p = false; 3898 }; 3899 3900 /* Identifies all of the argument packs that occur in a template 3901 argument and appends them to the TREE_LIST inside DATA, which is a 3902 find_parameter_pack_data structure. This is a subroutine of 3903 make_pack_expansion and uses_parameter_packs. */ 3904 static tree 3905 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data) 3906 { 3907 tree t = *tp; 3908 struct find_parameter_pack_data* ppd = 3909 (struct find_parameter_pack_data*)data; 3910 bool parameter_pack_p = false; 3911 3912 #define WALK_SUBTREE(NODE) \ 3913 cp_walk_tree (&(NODE), &find_parameter_packs_r, \ 3914 ppd, ppd->visited) \ 3915 3916 /* Don't look through typedefs; we are interested in whether a 3917 parameter pack is actually written in the expression/type we're 3918 looking at, not the target type. */ 3919 if (TYPE_P (t) && typedef_variant_p (t)) 3920 { 3921 /* But do look at arguments for an alias template. */ 3922 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t)) 3923 cp_walk_tree (&TI_ARGS (tinfo), 3924 &find_parameter_packs_r, 3925 ppd, ppd->visited); 3926 *walk_subtrees = 0; 3927 return NULL_TREE; 3928 } 3929 3930 /* Identify whether this is a parameter pack or not. */ 3931 switch (TREE_CODE (t)) 3932 { 3933 case TEMPLATE_PARM_INDEX: 3934 if (TEMPLATE_PARM_PARAMETER_PACK (t)) 3935 parameter_pack_p = true; 3936 break; 3937 3938 case TEMPLATE_TYPE_PARM: 3939 t = TYPE_MAIN_VARIANT (t); 3940 /* FALLTHRU */ 3941 case TEMPLATE_TEMPLATE_PARM: 3942 /* If the placeholder appears in the decl-specifier-seq of a function 3943 parameter pack (14.6.3), or the type-specifier-seq of a type-id that 3944 is a pack expansion, the invented template parameter is a template 3945 parameter pack. */ 3946 if (flag_concepts_ts && ppd->type_pack_expansion_p && is_auto (t) 3947 && TEMPLATE_TYPE_LEVEL (t) != 0) 3948 TEMPLATE_TYPE_PARAMETER_PACK (t) = true; 3949 if (TEMPLATE_TYPE_PARAMETER_PACK (t)) 3950 parameter_pack_p = true; 3951 break; 3952 3953 case FIELD_DECL: 3954 case PARM_DECL: 3955 if (DECL_PACK_P (t)) 3956 { 3957 /* We don't want to walk into the type of a PARM_DECL, 3958 because we don't want to see the type parameter pack. */ 3959 *walk_subtrees = 0; 3960 parameter_pack_p = true; 3961 } 3962 break; 3963 3964 case VAR_DECL: 3965 if (DECL_PACK_P (t)) 3966 { 3967 /* We don't want to walk into the type of a variadic capture proxy, 3968 because we don't want to see the type parameter pack. */ 3969 *walk_subtrees = 0; 3970 parameter_pack_p = true; 3971 } 3972 else if (variable_template_specialization_p (t)) 3973 { 3974 cp_walk_tree (&DECL_TI_ARGS (t), 3975 find_parameter_packs_r, 3976 ppd, ppd->visited); 3977 *walk_subtrees = 0; 3978 } 3979 break; 3980 3981 case CALL_EXPR: 3982 if (builtin_pack_call_p (t)) 3983 parameter_pack_p = true; 3984 break; 3985 3986 case BASES: 3987 parameter_pack_p = true; 3988 break; 3989 default: 3990 /* Not a parameter pack. */ 3991 break; 3992 } 3993 3994 if (parameter_pack_p) 3995 { 3996 /* Add this parameter pack to the list. */ 3997 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs); 3998 } 3999 4000 if (has_extra_args_mechanism_p (t) && !PACK_EXPANSION_P (t)) 4001 ppd->found_extra_args_tree_p = true; 4002 4003 if (TYPE_P (t)) 4004 cp_walk_tree (&TYPE_CONTEXT (t), 4005 &find_parameter_packs_r, ppd, ppd->visited); 4006 4007 /* This switch statement will return immediately if we don't find a 4008 parameter pack. ??? Should some of these be in cp_walk_subtrees? */ 4009 switch (TREE_CODE (t)) 4010 { 4011 case BOUND_TEMPLATE_TEMPLATE_PARM: 4012 /* Check the template itself. */ 4013 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)), 4014 &find_parameter_packs_r, ppd, ppd->visited); 4015 return NULL_TREE; 4016 4017 case DECL_EXPR: 4018 { 4019 tree decl = DECL_EXPR_DECL (t); 4020 /* Ignore the declaration of a capture proxy for a parameter pack. */ 4021 if (is_capture_proxy (decl)) 4022 *walk_subtrees = 0; 4023 if (is_typedef_decl (decl)) 4024 /* Since we stop at typedefs above, we need to look through them at 4025 the point of the DECL_EXPR. */ 4026 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl), 4027 &find_parameter_packs_r, ppd, ppd->visited); 4028 return NULL_TREE; 4029 } 4030 4031 case TEMPLATE_DECL: 4032 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t)) 4033 return NULL_TREE; 4034 cp_walk_tree (&TREE_TYPE (t), 4035 &find_parameter_packs_r, ppd, ppd->visited); 4036 return NULL_TREE; 4037 4038 case TYPE_PACK_EXPANSION: 4039 case EXPR_PACK_EXPANSION: 4040 *walk_subtrees = 0; 4041 return NULL_TREE; 4042 4043 case INTEGER_TYPE: 4044 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r, 4045 ppd, ppd->visited); 4046 *walk_subtrees = 0; 4047 return NULL_TREE; 4048 4049 case IDENTIFIER_NODE: 4050 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd, 4051 ppd->visited); 4052 *walk_subtrees = 0; 4053 return NULL_TREE; 4054 4055 case LAMBDA_EXPR: 4056 { 4057 /* Since we defer implicit capture, look in the parms and body. */ 4058 tree fn = lambda_function (t); 4059 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd, 4060 ppd->visited); 4061 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd, 4062 ppd->visited); 4063 return NULL_TREE; 4064 } 4065 4066 case DECLTYPE_TYPE: 4067 { 4068 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set 4069 type_pack_expansion_p to false so that any placeholders 4070 within the expression don't get marked as parameter packs. */ 4071 bool type_pack_expansion_p = ppd->type_pack_expansion_p; 4072 ppd->type_pack_expansion_p = false; 4073 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r, 4074 ppd, ppd->visited); 4075 ppd->type_pack_expansion_p = type_pack_expansion_p; 4076 *walk_subtrees = 0; 4077 return NULL_TREE; 4078 } 4079 4080 case IF_STMT: 4081 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r, 4082 ppd, ppd->visited); 4083 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r, 4084 ppd, ppd->visited); 4085 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r, 4086 ppd, ppd->visited); 4087 /* Don't walk into IF_STMT_EXTRA_ARGS. */ 4088 *walk_subtrees = 0; 4089 return NULL_TREE; 4090 4091 case TAG_DEFN: 4092 t = TREE_TYPE (t); 4093 if (CLASS_TYPE_P (t)) 4094 { 4095 /* Local class, need to look through the whole definition. 4096 TYPE_BINFO might be unset for a partial instantiation. */ 4097 if (TYPE_BINFO (t)) 4098 for (tree bb : BINFO_BASE_BINFOS (TYPE_BINFO (t))) 4099 cp_walk_tree (&BINFO_TYPE (bb), &find_parameter_packs_r, 4100 ppd, ppd->visited); 4101 } 4102 else 4103 /* Enum, look at the values. */ 4104 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l)) 4105 cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l)), 4106 &find_parameter_packs_r, 4107 ppd, ppd->visited); 4108 return NULL_TREE; 4109 4110 case FUNCTION_TYPE: 4111 case METHOD_TYPE: 4112 WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t)); 4113 break; 4114 4115 default: 4116 return NULL_TREE; 4117 } 4118 4119 #undef WALK_SUBTREE 4120 4121 return NULL_TREE; 4122 } 4123 4124 /* Determines if the expression or type T uses any parameter packs. */ 4125 tree 4126 uses_parameter_packs (tree t) 4127 { 4128 tree parameter_packs = NULL_TREE; 4129 struct find_parameter_pack_data ppd; 4130 ppd.parameter_packs = ¶meter_packs; 4131 ppd.visited = new hash_set<tree>; 4132 ppd.type_pack_expansion_p = false; 4133 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited); 4134 delete ppd.visited; 4135 return parameter_packs; 4136 } 4137 4138 /* Turn ARG, which may be an expression, type, or a TREE_LIST 4139 representation a base-class initializer into a parameter pack 4140 expansion. If all goes well, the resulting node will be an 4141 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST, 4142 respectively. */ 4143 tree 4144 make_pack_expansion (tree arg, tsubst_flags_t complain) 4145 { 4146 tree result; 4147 tree parameter_packs = NULL_TREE; 4148 bool for_types = false; 4149 struct find_parameter_pack_data ppd; 4150 4151 if (!arg || arg == error_mark_node) 4152 return arg; 4153 4154 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg)) 4155 { 4156 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base 4157 class initializer. In this case, the TREE_PURPOSE will be a 4158 _TYPE node (representing the base class expansion we're 4159 initializing) and the TREE_VALUE will be a TREE_LIST 4160 containing the initialization arguments. 4161 4162 The resulting expansion looks somewhat different from most 4163 expansions. Rather than returning just one _EXPANSION, we 4164 return a TREE_LIST whose TREE_PURPOSE is a 4165 TYPE_PACK_EXPANSION containing the bases that will be 4166 initialized. The TREE_VALUE will be identical to the 4167 original TREE_VALUE, which is a list of arguments that will 4168 be passed to each base. We do not introduce any new pack 4169 expansion nodes into the TREE_VALUE (although it is possible 4170 that some already exist), because the TREE_PURPOSE and 4171 TREE_VALUE all need to be expanded together with the same 4172 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the 4173 resulting TREE_PURPOSE will mention the parameter packs in 4174 both the bases and the arguments to the bases. */ 4175 tree purpose; 4176 tree value; 4177 tree parameter_packs = NULL_TREE; 4178 4179 /* Determine which parameter packs will be used by the base 4180 class expansion. */ 4181 ppd.visited = new hash_set<tree>; 4182 ppd.parameter_packs = ¶meter_packs; 4183 ppd.type_pack_expansion_p = false; 4184 gcc_assert (TYPE_P (TREE_PURPOSE (arg))); 4185 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r, 4186 &ppd, ppd.visited); 4187 4188 if (parameter_packs == NULL_TREE) 4189 { 4190 if (complain & tf_error) 4191 error ("base initializer expansion %qT contains no parameter packs", 4192 arg); 4193 delete ppd.visited; 4194 return error_mark_node; 4195 } 4196 4197 if (TREE_VALUE (arg) != void_type_node) 4198 { 4199 /* Collect the sets of parameter packs used in each of the 4200 initialization arguments. */ 4201 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value)) 4202 { 4203 /* Determine which parameter packs will be expanded in this 4204 argument. */ 4205 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r, 4206 &ppd, ppd.visited); 4207 } 4208 } 4209 4210 delete ppd.visited; 4211 4212 /* Create the pack expansion type for the base type. */ 4213 purpose = cxx_make_type (TYPE_PACK_EXPANSION); 4214 PACK_EXPANSION_PATTERN (purpose) = TREE_PURPOSE (arg); 4215 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs; 4216 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p (); 4217 4218 /* Just use structural equality for these TYPE_PACK_EXPANSIONS; 4219 they will rarely be compared to anything. */ 4220 SET_TYPE_STRUCTURAL_EQUALITY (purpose); 4221 4222 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE); 4223 } 4224 4225 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL) 4226 for_types = true; 4227 4228 /* Build the PACK_EXPANSION_* node. */ 4229 result = for_types 4230 ? cxx_make_type (TYPE_PACK_EXPANSION) 4231 : make_node (EXPR_PACK_EXPANSION); 4232 PACK_EXPANSION_PATTERN (result) = arg; 4233 if (TREE_CODE (result) == EXPR_PACK_EXPANSION) 4234 { 4235 /* Propagate type and const-expression information. */ 4236 TREE_TYPE (result) = TREE_TYPE (arg); 4237 TREE_CONSTANT (result) = TREE_CONSTANT (arg); 4238 /* Mark this read now, since the expansion might be length 0. */ 4239 mark_exp_read (arg); 4240 } 4241 else 4242 /* Just use structural equality for these TYPE_PACK_EXPANSIONS; 4243 they will rarely be compared to anything. */ 4244 SET_TYPE_STRUCTURAL_EQUALITY (result); 4245 4246 /* Determine which parameter packs will be expanded. */ 4247 ppd.parameter_packs = ¶meter_packs; 4248 ppd.visited = new hash_set<tree>; 4249 ppd.type_pack_expansion_p = TYPE_P (arg); 4250 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited); 4251 delete ppd.visited; 4252 4253 /* Make sure we found some parameter packs. */ 4254 if (parameter_packs == NULL_TREE) 4255 { 4256 if (complain & tf_error) 4257 { 4258 if (TYPE_P (arg)) 4259 error ("expansion pattern %qT contains no parameter packs", arg); 4260 else 4261 error ("expansion pattern %qE contains no parameter packs", arg); 4262 } 4263 return error_mark_node; 4264 } 4265 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs; 4266 4267 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p (); 4268 if (ppd.found_extra_args_tree_p) 4269 /* If the pattern of this pack expansion contains a subtree that has 4270 the extra args mechanism for avoiding partial instantiation, then 4271 force this pack expansion to also use extra args. Otherwise 4272 partial instantiation of this pack expansion may not lower the 4273 level of some parameter packs within the pattern, which would 4274 confuse tsubst_pack_expansion later (PR101764). */ 4275 PACK_EXPANSION_FORCE_EXTRA_ARGS_P (result) = true; 4276 4277 return result; 4278 } 4279 4280 /* Checks T for any "bare" parameter packs, which have not yet been 4281 expanded, and issues an error if any are found. This operation can 4282 only be done on full expressions or types (e.g., an expression 4283 statement, "if" condition, etc.), because we could have expressions like: 4284 4285 foo(f(g(h(args)))...) 4286 4287 where "args" is a parameter pack. check_for_bare_parameter_packs 4288 should not be called for the subexpressions args, h(args), 4289 g(h(args)), or f(g(h(args))), because we would produce erroneous 4290 error messages. 4291 4292 Returns TRUE and emits an error if there were bare parameter packs, 4293 returns FALSE otherwise. */ 4294 bool 4295 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */) 4296 { 4297 tree parameter_packs = NULL_TREE; 4298 struct find_parameter_pack_data ppd; 4299 4300 if (!processing_template_decl || !t || t == error_mark_node) 4301 return false; 4302 4303 if (TREE_CODE (t) == TYPE_DECL) 4304 t = TREE_TYPE (t); 4305 4306 ppd.parameter_packs = ¶meter_packs; 4307 ppd.visited = new hash_set<tree>; 4308 ppd.type_pack_expansion_p = false; 4309 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited); 4310 delete ppd.visited; 4311 4312 if (!parameter_packs) 4313 return false; 4314 4315 if (loc == UNKNOWN_LOCATION) 4316 loc = cp_expr_loc_or_input_loc (t); 4317 4318 /* It's OK for a lambda to have an unexpanded parameter pack from the 4319 containing context, but do complain about unexpanded capture packs. */ 4320 tree lam = current_lambda_expr (); 4321 if (lam) 4322 lam = TREE_TYPE (lam); 4323 4324 if (lam && lam != current_class_type) 4325 { 4326 /* We're in a lambda, but it isn't the innermost class. 4327 This should work, but currently doesn't. */ 4328 sorry_at (loc, "unexpanded parameter pack in local class in lambda"); 4329 return true; 4330 } 4331 4332 if (lam && CLASSTYPE_TEMPLATE_INFO (lam)) 4333 for (; parameter_packs; 4334 parameter_packs = TREE_CHAIN (parameter_packs)) 4335 { 4336 tree pack = TREE_VALUE (parameter_packs); 4337 if (is_capture_proxy (pack) 4338 || (TREE_CODE (pack) == PARM_DECL 4339 && DECL_CONTEXT (pack) 4340 && DECL_CONTEXT (DECL_CONTEXT (pack)) == lam)) 4341 break; 4342 } 4343 4344 if (parameter_packs) 4345 { 4346 error_at (loc, "parameter packs not expanded with %<...%>:"); 4347 while (parameter_packs) 4348 { 4349 tree pack = TREE_VALUE (parameter_packs); 4350 tree name = NULL_TREE; 4351 4352 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM 4353 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM) 4354 name = TYPE_NAME (pack); 4355 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX) 4356 name = DECL_NAME (TEMPLATE_PARM_DECL (pack)); 4357 else if (TREE_CODE (pack) == CALL_EXPR) 4358 name = DECL_NAME (CALL_EXPR_FN (pack)); 4359 else 4360 name = DECL_NAME (pack); 4361 4362 if (name) 4363 inform (loc, " %qD", name); 4364 else 4365 inform (loc, " %s", "<anonymous>"); 4366 4367 parameter_packs = TREE_CHAIN (parameter_packs); 4368 } 4369 4370 return true; 4371 } 4372 4373 return false; 4374 } 4375 4376 /* Expand any parameter packs that occur in the template arguments in 4377 ARGS. */ 4378 tree 4379 expand_template_argument_pack (tree args) 4380 { 4381 if (args == error_mark_node) 4382 return error_mark_node; 4383 4384 tree result_args = NULL_TREE; 4385 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0; 4386 int num_result_args = -1; 4387 int non_default_args_count = -1; 4388 4389 /* First, determine if we need to expand anything, and the number of 4390 slots we'll need. */ 4391 for (in_arg = 0; in_arg < nargs; ++in_arg) 4392 { 4393 tree arg = TREE_VEC_ELT (args, in_arg); 4394 if (arg == NULL_TREE) 4395 return args; 4396 if (ARGUMENT_PACK_P (arg)) 4397 { 4398 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)); 4399 if (num_result_args < 0) 4400 num_result_args = in_arg + num_packed; 4401 else 4402 num_result_args += num_packed; 4403 } 4404 else 4405 { 4406 if (num_result_args >= 0) 4407 num_result_args++; 4408 } 4409 } 4410 4411 /* If no expansion is necessary, we're done. */ 4412 if (num_result_args < 0) 4413 return args; 4414 4415 /* Expand arguments. */ 4416 result_args = make_tree_vec (num_result_args); 4417 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args)) 4418 non_default_args_count = 4419 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args); 4420 for (in_arg = 0; in_arg < nargs; ++in_arg) 4421 { 4422 tree arg = TREE_VEC_ELT (args, in_arg); 4423 if (ARGUMENT_PACK_P (arg)) 4424 { 4425 tree packed = ARGUMENT_PACK_ARGS (arg); 4426 int i, num_packed = TREE_VEC_LENGTH (packed); 4427 for (i = 0; i < num_packed; ++i, ++out_arg) 4428 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i); 4429 if (non_default_args_count > 0) 4430 non_default_args_count += num_packed - 1; 4431 } 4432 else 4433 { 4434 TREE_VEC_ELT (result_args, out_arg) = arg; 4435 ++out_arg; 4436 } 4437 } 4438 if (non_default_args_count >= 0) 4439 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count); 4440 return result_args; 4441 } 4442 4443 /* Checks if DECL shadows a template parameter. 4444 4445 [temp.local]: A template-parameter shall not be redeclared within its 4446 scope (including nested scopes). 4447 4448 Emits an error and returns TRUE if the DECL shadows a parameter, 4449 returns FALSE otherwise. */ 4450 4451 bool 4452 check_template_shadow (tree decl) 4453 { 4454 tree olddecl; 4455 4456 /* If we're not in a template, we can't possibly shadow a template 4457 parameter. */ 4458 if (!current_template_parms) 4459 return true; 4460 4461 /* Figure out what we're shadowing. */ 4462 decl = OVL_FIRST (decl); 4463 olddecl = innermost_non_namespace_value (DECL_NAME (decl)); 4464 4465 /* If there's no previous binding for this name, we're not shadowing 4466 anything, let alone a template parameter. */ 4467 if (!olddecl) 4468 return true; 4469 4470 /* If we're not shadowing a template parameter, we're done. Note 4471 that OLDDECL might be an OVERLOAD (or perhaps even an 4472 ERROR_MARK), so we can't just blithely assume it to be a _DECL 4473 node. */ 4474 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) 4475 return true; 4476 4477 /* We check for decl != olddecl to avoid bogus errors for using a 4478 name inside a class. We check TPFI to avoid duplicate errors for 4479 inline member templates. */ 4480 if (decl == olddecl 4481 || (DECL_TEMPLATE_PARM_P (decl) 4482 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms))) 4483 return true; 4484 4485 /* Don't complain about the injected class name, as we've already 4486 complained about the class itself. */ 4487 if (DECL_SELF_REFERENCE_P (decl)) 4488 return false; 4489 4490 if (DECL_TEMPLATE_PARM_P (decl)) 4491 error ("declaration of template parameter %q+D shadows " 4492 "template parameter", decl); 4493 else 4494 error ("declaration of %q+#D shadows template parameter", decl); 4495 inform (DECL_SOURCE_LOCATION (olddecl), 4496 "template parameter %qD declared here", olddecl); 4497 return false; 4498 } 4499 4500 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, 4501 ORIG_LEVEL, DECL, and TYPE. */ 4502 4503 static tree 4504 build_template_parm_index (int index, 4505 int level, 4506 int orig_level, 4507 tree decl, 4508 tree type) 4509 { 4510 tree t = make_node (TEMPLATE_PARM_INDEX); 4511 TEMPLATE_PARM_IDX (t) = index; 4512 TEMPLATE_PARM_LEVEL (t) = level; 4513 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; 4514 TEMPLATE_PARM_DECL (t) = decl; 4515 TREE_TYPE (t) = type; 4516 TREE_CONSTANT (t) = TREE_CONSTANT (decl); 4517 TREE_READONLY (t) = TREE_READONLY (decl); 4518 4519 return t; 4520 } 4521 4522 struct ctp_hasher : ggc_ptr_hash<tree_node> 4523 { 4524 static hashval_t hash (tree t) 4525 { 4526 ++comparing_specializations; 4527 tree_code code = TREE_CODE (t); 4528 hashval_t val = iterative_hash_object (code, 0); 4529 val = iterative_hash_object (TEMPLATE_TYPE_LEVEL (t), val); 4530 val = iterative_hash_object (TEMPLATE_TYPE_IDX (t), val); 4531 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) 4532 val = iterative_hash_template_arg (CLASS_PLACEHOLDER_TEMPLATE (t), val); 4533 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) 4534 val = iterative_hash_template_arg (TYPE_TI_ARGS (t), val); 4535 --comparing_specializations; 4536 return val; 4537 } 4538 4539 static bool equal (tree t, tree u) 4540 { 4541 ++comparing_specializations; 4542 bool eq = comptypes (t, u, COMPARE_STRUCTURAL); 4543 --comparing_specializations; 4544 return eq; 4545 } 4546 }; 4547 4548 static GTY (()) hash_table<ctp_hasher> *ctp_table; 4549 4550 /* Find the canonical type parameter for the given template type 4551 parameter. Returns the canonical type parameter, which may be TYPE 4552 if no such parameter existed. */ 4553 4554 tree 4555 canonical_type_parameter (tree type) 4556 { 4557 if (ctp_table == NULL) 4558 ctp_table = hash_table<ctp_hasher>::create_ggc (61); 4559 4560 tree& slot = *ctp_table->find_slot (type, INSERT); 4561 if (slot == NULL_TREE) 4562 slot = type; 4563 return slot; 4564 } 4565 4566 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose 4567 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a 4568 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a 4569 new one is created. */ 4570 4571 static tree 4572 reduce_template_parm_level (tree index, tree type, int levels, tree args, 4573 tsubst_flags_t complain) 4574 { 4575 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE 4576 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) 4577 != TEMPLATE_PARM_LEVEL (index) - levels) 4578 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index)))) 4579 { 4580 tree orig_decl = TEMPLATE_PARM_DECL (index); 4581 4582 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl), 4583 TREE_CODE (orig_decl), DECL_NAME (orig_decl), 4584 type); 4585 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl); 4586 TREE_READONLY (decl) = TREE_READONLY (orig_decl); 4587 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl); 4588 DECL_ARTIFICIAL (decl) = 1; 4589 SET_DECL_TEMPLATE_PARM_P (decl); 4590 4591 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index), 4592 TEMPLATE_PARM_LEVEL (index) - levels, 4593 TEMPLATE_PARM_ORIG_LEVEL (index), 4594 decl, type); 4595 TEMPLATE_PARM_DESCENDANTS (index) = tpi; 4596 TEMPLATE_PARM_PARAMETER_PACK (tpi) 4597 = TEMPLATE_PARM_PARAMETER_PACK (index); 4598 4599 /* Template template parameters need this. */ 4600 tree inner = decl; 4601 if (TREE_CODE (decl) == TEMPLATE_DECL) 4602 { 4603 inner = build_lang_decl_loc (DECL_SOURCE_LOCATION (decl), 4604 TYPE_DECL, DECL_NAME (decl), type); 4605 DECL_TEMPLATE_RESULT (decl) = inner; 4606 DECL_ARTIFICIAL (inner) = true; 4607 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (orig_decl), 4608 args, complain); 4609 DECL_TEMPLATE_PARMS (decl) = parms; 4610 tree orig_inner = DECL_TEMPLATE_RESULT (orig_decl); 4611 DECL_TEMPLATE_INFO (inner) 4612 = build_template_info (DECL_TI_TEMPLATE (orig_inner), 4613 template_parms_to_args (parms)); 4614 } 4615 4616 /* Attach the TPI to the decl. */ 4617 if (TREE_CODE (inner) == TYPE_DECL) 4618 TEMPLATE_TYPE_PARM_INDEX (type) = tpi; 4619 else 4620 DECL_INITIAL (decl) = tpi; 4621 } 4622 4623 return TEMPLATE_PARM_DESCENDANTS (index); 4624 } 4625 4626 /* Process information from new template parameter PARM and append it 4627 to the LIST being built. This new parameter is a non-type 4628 parameter iff IS_NON_TYPE is true. This new parameter is a 4629 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM 4630 is in PARM_LOC. */ 4631 4632 tree 4633 process_template_parm (tree list, location_t parm_loc, tree parm, 4634 bool is_non_type, bool is_parameter_pack) 4635 { 4636 gcc_assert (TREE_CODE (parm) == TREE_LIST); 4637 tree prev = NULL_TREE; 4638 int idx = 0; 4639 4640 if (list) 4641 { 4642 prev = tree_last (list); 4643 4644 tree p = TREE_VALUE (prev); 4645 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) 4646 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); 4647 else if (TREE_CODE (p) == PARM_DECL) 4648 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); 4649 4650 ++idx; 4651 } 4652 4653 tree decl = NULL_TREE; 4654 tree defval = TREE_PURPOSE (parm); 4655 tree constr = TREE_TYPE (parm); 4656 4657 if (is_non_type) 4658 { 4659 parm = TREE_VALUE (parm); 4660 4661 SET_DECL_TEMPLATE_PARM_P (parm); 4662 4663 if (TREE_TYPE (parm) != error_mark_node) 4664 { 4665 /* [temp.param] 4666 4667 The top-level cv-qualifiers on the template-parameter are 4668 ignored when determining its type. */ 4669 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); 4670 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) 4671 TREE_TYPE (parm) = error_mark_node; 4672 else if (uses_parameter_packs (TREE_TYPE (parm)) 4673 && !is_parameter_pack 4674 /* If we're in a nested template parameter list, the template 4675 template parameter could be a parameter pack. */ 4676 && processing_template_parmlist == 1) 4677 { 4678 /* This template parameter is not a parameter pack, but it 4679 should be. Complain about "bare" parameter packs. */ 4680 check_for_bare_parameter_packs (TREE_TYPE (parm)); 4681 4682 /* Recover by calling this a parameter pack. */ 4683 is_parameter_pack = true; 4684 } 4685 } 4686 4687 /* A template parameter is not modifiable. */ 4688 TREE_CONSTANT (parm) = 1; 4689 TREE_READONLY (parm) = 1; 4690 decl = build_decl (parm_loc, 4691 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); 4692 TREE_CONSTANT (decl) = 1; 4693 TREE_READONLY (decl) = 1; 4694 DECL_INITIAL (parm) = DECL_INITIAL (decl) 4695 = build_template_parm_index (idx, current_template_depth, 4696 current_template_depth, 4697 decl, TREE_TYPE (parm)); 4698 4699 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)) 4700 = is_parameter_pack; 4701 } 4702 else 4703 { 4704 tree t; 4705 parm = TREE_VALUE (TREE_VALUE (parm)); 4706 4707 if (parm && TREE_CODE (parm) == TEMPLATE_DECL) 4708 { 4709 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM); 4710 /* This is for distinguishing between real templates and template 4711 template parameters */ 4712 TREE_TYPE (parm) = t; 4713 4714 /* any_template_parm_r expects to be able to get the targs of a 4715 DECL_TEMPLATE_RESULT. */ 4716 tree result = DECL_TEMPLATE_RESULT (parm); 4717 TREE_TYPE (result) = t; 4718 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm)); 4719 tree tinfo = build_template_info (parm, args); 4720 retrofit_lang_decl (result); 4721 DECL_TEMPLATE_INFO (result) = tinfo; 4722 4723 decl = parm; 4724 } 4725 else 4726 { 4727 t = cxx_make_type (TEMPLATE_TYPE_PARM); 4728 /* parm is either IDENTIFIER_NODE or NULL_TREE. */ 4729 decl = build_decl (parm_loc, 4730 TYPE_DECL, parm, t); 4731 } 4732 4733 TYPE_NAME (t) = decl; 4734 TYPE_STUB_DECL (t) = decl; 4735 parm = decl; 4736 TEMPLATE_TYPE_PARM_INDEX (t) 4737 = build_template_parm_index (idx, current_template_depth, 4738 current_template_depth, 4739 decl, TREE_TYPE (parm)); 4740 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack; 4741 TYPE_CANONICAL (t) = canonical_type_parameter (t); 4742 } 4743 DECL_ARTIFICIAL (decl) = 1; 4744 SET_DECL_TEMPLATE_PARM_P (decl); 4745 4746 if (TREE_CODE (parm) == TEMPLATE_DECL 4747 && !uses_outer_template_parms (parm)) 4748 TEMPLATE_TEMPLATE_PARM_SIMPLE_P (TREE_TYPE (parm)) = true; 4749 4750 /* Build requirements for the type/template parameter. 4751 This must be done after SET_DECL_TEMPLATE_PARM_P or 4752 process_template_parm could fail. */ 4753 tree reqs = finish_shorthand_constraint (parm, constr); 4754 4755 decl = pushdecl (decl); 4756 if (!is_non_type) 4757 parm = decl; 4758 4759 /* Build the parameter node linking the parameter declaration, 4760 its default argument (if any), and its constraints (if any). */ 4761 parm = build_tree_list (defval, parm); 4762 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs; 4763 4764 if (prev) 4765 TREE_CHAIN (prev) = parm; 4766 else 4767 list = parm; 4768 4769 return list; 4770 } 4771 4772 /* The end of a template parameter list has been reached. Process the 4773 tree list into a parameter vector, converting each parameter into a more 4774 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others 4775 as PARM_DECLs. */ 4776 4777 tree 4778 end_template_parm_list (tree parms) 4779 { 4780 tree saved_parmlist = make_tree_vec (list_length (parms)); 4781 4782 /* Pop the dummy parameter level and add the real one. We do not 4783 morph the dummy parameter in place, as it might have been 4784 captured by a (nested) template-template-parm. */ 4785 current_template_parms = TREE_CHAIN (current_template_parms); 4786 4787 current_template_parms 4788 = tree_cons (size_int (current_template_depth + 1), 4789 saved_parmlist, current_template_parms); 4790 4791 for (unsigned ix = 0; parms; ix++) 4792 { 4793 tree parm = parms; 4794 parms = TREE_CHAIN (parms); 4795 TREE_CHAIN (parm) = NULL_TREE; 4796 4797 TREE_VEC_ELT (saved_parmlist, ix) = parm; 4798 } 4799 4800 --processing_template_parmlist; 4801 4802 return saved_parmlist; 4803 } 4804 4805 // Explicitly indicate the end of the template parameter list. We assume 4806 // that the current template parameters have been constructed and/or 4807 // managed explicitly, as when creating new template template parameters 4808 // from a shorthand constraint. 4809 void 4810 end_template_parm_list () 4811 { 4812 --processing_template_parmlist; 4813 } 4814 4815 /* end_template_decl is called after a template declaration is seen. */ 4816 4817 void 4818 end_template_decl (void) 4819 { 4820 reset_specialization (); 4821 4822 if (! processing_template_decl) 4823 return; 4824 4825 /* This matches the pushlevel in begin_template_parm_list. */ 4826 finish_scope (); 4827 4828 --processing_template_decl; 4829 current_template_parms = TREE_CHAIN (current_template_parms); 4830 } 4831 4832 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST 4833 thereof, and converts it into an argument suitable to be passed to 4834 the type substitution functions. Note that if the TREE_LIST contains 4835 an error_mark node, the returned argument is error_mark_node. */ 4836 4837 tree 4838 template_parm_to_arg (tree t) 4839 { 4840 if (!t) 4841 return NULL_TREE; 4842 4843 if (TREE_CODE (t) == TREE_LIST) 4844 t = TREE_VALUE (t); 4845 4846 if (error_operand_p (t)) 4847 return error_mark_node; 4848 4849 if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t)) 4850 { 4851 if (TREE_CODE (t) == TYPE_DECL 4852 || TREE_CODE (t) == TEMPLATE_DECL) 4853 t = TREE_TYPE (t); 4854 else 4855 t = DECL_INITIAL (t); 4856 } 4857 4858 gcc_assert (TEMPLATE_PARM_P (t)); 4859 4860 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM 4861 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) 4862 { 4863 if (TEMPLATE_TYPE_PARAMETER_PACK (t)) 4864 { 4865 /* Turn this argument into a TYPE_ARGUMENT_PACK 4866 with a single element, which expands T. */ 4867 tree vec = make_tree_vec (1); 4868 if (CHECKING_P) 4869 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec)); 4870 4871 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); 4872 4873 t = cxx_make_type (TYPE_ARGUMENT_PACK); 4874 ARGUMENT_PACK_ARGS (t) = vec; 4875 } 4876 } 4877 else 4878 { 4879 if (TEMPLATE_PARM_PARAMETER_PACK (t)) 4880 { 4881 /* Turn this argument into a NONTYPE_ARGUMENT_PACK 4882 with a single element, which expands T. */ 4883 tree vec = make_tree_vec (1); 4884 if (CHECKING_P) 4885 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec)); 4886 4887 t = convert_from_reference (t); 4888 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t); 4889 4890 t = make_node (NONTYPE_ARGUMENT_PACK); 4891 ARGUMENT_PACK_ARGS (t) = vec; 4892 } 4893 else 4894 t = convert_from_reference (t); 4895 } 4896 return t; 4897 } 4898 4899 /* If T looks like a generic template argument produced by template_parm_to_arg, 4900 return the corresponding template parameter, otherwise return NULL_TREE. */ 4901 4902 static tree 4903 template_arg_to_parm (tree t) 4904 { 4905 if (t == NULL_TREE) 4906 return NULL_TREE; 4907 4908 if (ARGUMENT_PACK_P (t)) 4909 { 4910 tree args = ARGUMENT_PACK_ARGS (t); 4911 if (TREE_VEC_LENGTH (args) == 1 4912 && PACK_EXPANSION_P (TREE_VEC_ELT (args, 0))) 4913 t = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (args, 0)); 4914 } 4915 4916 if (REFERENCE_REF_P (t)) 4917 t = TREE_OPERAND (t, 0); 4918 4919 if (TEMPLATE_PARM_P (t)) 4920 return t; 4921 else 4922 return NULL_TREE; 4923 } 4924 4925 /* Given a single level of template parameters (a TREE_VEC), return it 4926 as a set of template arguments. */ 4927 4928 tree 4929 template_parms_level_to_args (tree parms) 4930 { 4931 parms = copy_node (parms); 4932 TREE_TYPE (parms) = NULL_TREE; 4933 for (tree& parm : tree_vec_range (parms)) 4934 parm = template_parm_to_arg (parm); 4935 4936 if (CHECKING_P) 4937 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (parms, TREE_VEC_LENGTH (parms)); 4938 4939 return parms; 4940 } 4941 4942 /* Given a set of template parameters, return them as a set of template 4943 arguments. The template parameters are represented as a TREE_VEC, in 4944 the form documented in cp-tree.h for template arguments. */ 4945 4946 tree 4947 template_parms_to_args (tree parms) 4948 { 4949 tree header; 4950 tree args = NULL_TREE; 4951 int length = TMPL_PARMS_DEPTH (parms); 4952 int l = length; 4953 4954 /* If there is only one level of template parameters, we do not 4955 create a TREE_VEC of TREE_VECs. Instead, we return a single 4956 TREE_VEC containing the arguments. */ 4957 if (length > 1) 4958 args = make_tree_vec (length); 4959 4960 for (header = parms; header; header = TREE_CHAIN (header)) 4961 { 4962 tree a = template_parms_level_to_args (TREE_VALUE (header)); 4963 4964 if (length > 1) 4965 TREE_VEC_ELT (args, --l) = a; 4966 else 4967 args = a; 4968 } 4969 4970 return args; 4971 } 4972 4973 /* Within the declaration of a template, return the currently active 4974 template parameters as an argument TREE_VEC. */ 4975 4976 static tree 4977 current_template_args (void) 4978 { 4979 return template_parms_to_args (current_template_parms); 4980 } 4981 4982 /* Return the fully generic arguments for of TMPL, i.e. what 4983 current_template_args would be while parsing it. */ 4984 4985 tree 4986 generic_targs_for (tree tmpl) 4987 { 4988 if (tmpl == NULL_TREE) 4989 return NULL_TREE; 4990 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl) 4991 || DECL_TEMPLATE_SPECIALIZATION (tmpl)) 4992 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template 4993 template parameter, it has no TEMPLATE_INFO; for a partial 4994 specialization, it has the arguments for the primary template, and we 4995 want the arguments for the partial specialization. */; 4996 else if (tree result = DECL_TEMPLATE_RESULT (tmpl)) 4997 if (tree ti = get_template_info (result)) 4998 return TI_ARGS (ti); 4999 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)); 5000 } 5001 5002 /* Return the template arguments corresponding to the template parameters of 5003 DECL's enclosing scope. When DECL is a member of a partial specialization, 5004 this returns the arguments for the partial specialization as opposed to those 5005 for the primary template, which is the main difference between this function 5006 and simply using e.g. the TYPE_TI_ARGS of DECL's DECL_CONTEXT. */ 5007 5008 tree 5009 outer_template_args (const_tree decl) 5010 { 5011 if (TREE_CODE (decl) == TEMPLATE_DECL) 5012 decl = DECL_TEMPLATE_RESULT (decl); 5013 tree ti = get_template_info (decl); 5014 if (!ti) 5015 return NULL_TREE; 5016 tree args = TI_ARGS (ti); 5017 if (!PRIMARY_TEMPLATE_P (TI_TEMPLATE (ti))) 5018 return args; 5019 if (TMPL_ARGS_DEPTH (args) == 1) 5020 return NULL_TREE; 5021 return strip_innermost_template_args (args, 1); 5022 } 5023 5024 /* Update the declared TYPE by doing any lookups which were thought to be 5025 dependent, but are not now that we know the SCOPE of the declarator. */ 5026 5027 tree 5028 maybe_update_decl_type (tree orig_type, tree scope) 5029 { 5030 tree type = orig_type; 5031 5032 if (type == NULL_TREE) 5033 return type; 5034 5035 if (TREE_CODE (orig_type) == TYPE_DECL) 5036 type = TREE_TYPE (type); 5037 5038 if (scope && TYPE_P (scope) && dependent_type_p (scope) 5039 && dependent_type_p (type) 5040 /* Don't bother building up the args in this case. */ 5041 && TREE_CODE (type) != TEMPLATE_TYPE_PARM) 5042 { 5043 /* tsubst in the args corresponding to the template parameters, 5044 including auto if present. Most things will be unchanged, but 5045 make_typename_type and tsubst_qualified_id will resolve 5046 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */ 5047 tree args = current_template_args (); 5048 tree auto_node = type_uses_auto (type); 5049 tree pushed; 5050 if (auto_node) 5051 { 5052 tree auto_vec = make_tree_vec (1); 5053 TREE_VEC_ELT (auto_vec, 0) = auto_node; 5054 args = add_to_template_args (args, auto_vec); 5055 } 5056 pushed = push_scope (scope); 5057 type = tsubst (type, args, tf_warning_or_error, NULL_TREE); 5058 if (pushed) 5059 pop_scope (scope); 5060 } 5061 5062 if (type == error_mark_node) 5063 return orig_type; 5064 5065 if (TREE_CODE (orig_type) == TYPE_DECL) 5066 { 5067 if (same_type_p (type, TREE_TYPE (orig_type))) 5068 type = orig_type; 5069 else 5070 type = TYPE_NAME (type); 5071 } 5072 return type; 5073 } 5074 5075 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated 5076 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true, 5077 the new template is a member template. */ 5078 5079 static tree 5080 build_template_decl (tree decl, tree parms, bool member_template_p) 5081 { 5082 gcc_checking_assert (TREE_CODE (decl) != TEMPLATE_DECL); 5083 5084 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); 5085 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl)); 5086 DECL_TEMPLATE_PARMS (tmpl) = parms; 5087 DECL_TEMPLATE_RESULT (tmpl) = decl; 5088 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); 5089 TREE_TYPE (tmpl) = TREE_TYPE (decl); 5090 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); 5091 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p; 5092 5093 /* Propagate module information from the decl. */ 5094 DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl); 5095 5096 return tmpl; 5097 } 5098 5099 struct template_parm_data 5100 { 5101 /* The level of the template parameters we are currently 5102 processing. */ 5103 int level; 5104 5105 /* The index of the specialization argument we are currently 5106 processing. */ 5107 int current_arg; 5108 5109 /* An array whose size is the number of template parameters. The 5110 elements are nonzero if the parameter has been used in any one 5111 of the arguments processed so far. */ 5112 int* parms; 5113 5114 /* An array whose size is the number of template arguments. The 5115 elements are nonzero if the argument makes use of template 5116 parameters of this level. */ 5117 int* arg_uses_template_parms; 5118 }; 5119 5120 /* Subroutine of push_template_decl used to see if each template 5121 parameter in a partial specialization is used in the explicit 5122 argument list. If T is of the LEVEL given in DATA (which is 5123 treated as a template_parm_data*), then DATA->PARMS is marked 5124 appropriately. */ 5125 5126 static int 5127 mark_template_parm (tree t, void* data) 5128 { 5129 int level; 5130 int idx; 5131 struct template_parm_data* tpd = (struct template_parm_data*) data; 5132 5133 template_parm_level_and_index (t, &level, &idx); 5134 5135 if (level == tpd->level) 5136 { 5137 tpd->parms[idx] = 1; 5138 tpd->arg_uses_template_parms[tpd->current_arg] = 1; 5139 } 5140 5141 /* In C++17 the type of a non-type argument is a deduced context. */ 5142 if (cxx_dialect >= cxx17 5143 && TREE_CODE (t) == TEMPLATE_PARM_INDEX) 5144 for_each_template_parm (TREE_TYPE (t), 5145 &mark_template_parm, 5146 data, 5147 NULL, 5148 /*include_nondeduced_p=*/false); 5149 5150 /* Return zero so that for_each_template_parm will continue the 5151 traversal of the tree; we want to mark *every* template parm. */ 5152 return 0; 5153 } 5154 5155 /* Process the partial specialization DECL. */ 5156 5157 static tree 5158 process_partial_specialization (tree decl) 5159 { 5160 tree type = TREE_TYPE (decl); 5161 tree tinfo = get_template_info (decl); 5162 tree maintmpl = TI_TEMPLATE (tinfo); 5163 tree specargs = TI_ARGS (tinfo); 5164 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); 5165 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); 5166 tree inner_parms; 5167 tree inst; 5168 int nargs = TREE_VEC_LENGTH (inner_args); 5169 int ntparms; 5170 int i; 5171 bool did_error_intro = false; 5172 struct template_parm_data tpd; 5173 struct template_parm_data tpd2; 5174 5175 gcc_assert (current_template_parms); 5176 5177 /* A concept cannot be specialized. */ 5178 if (flag_concepts && variable_concept_p (maintmpl)) 5179 { 5180 error ("specialization of variable concept %q#D", maintmpl); 5181 return error_mark_node; 5182 } 5183 5184 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); 5185 ntparms = TREE_VEC_LENGTH (inner_parms); 5186 5187 /* We check that each of the template parameters given in the 5188 partial specialization is used in the argument list to the 5189 specialization. For example: 5190 5191 template <class T> struct S; 5192 template <class T> struct S<T*>; 5193 5194 The second declaration is OK because `T*' uses the template 5195 parameter T, whereas 5196 5197 template <class T> struct S<int>; 5198 5199 is no good. Even trickier is: 5200 5201 template <class T> 5202 struct S1 5203 { 5204 template <class U> 5205 struct S2; 5206 template <class U> 5207 struct S2<T>; 5208 }; 5209 5210 The S2<T> declaration is actually invalid; it is a 5211 full-specialization. Of course, 5212 5213 template <class U> 5214 struct S2<T (*)(U)>; 5215 5216 or some such would have been OK. */ 5217 tpd.level = TMPL_PARMS_DEPTH (current_template_parms); 5218 tpd.parms = XALLOCAVEC (int, ntparms); 5219 memset (tpd.parms, 0, sizeof (int) * ntparms); 5220 5221 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs); 5222 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs); 5223 for (i = 0; i < nargs; ++i) 5224 { 5225 tpd.current_arg = i; 5226 for_each_template_parm (TREE_VEC_ELT (inner_args, i), 5227 &mark_template_parm, 5228 &tpd, 5229 NULL, 5230 /*include_nondeduced_p=*/false); 5231 } 5232 for (i = 0; i < ntparms; ++i) 5233 if (tpd.parms[i] == 0) 5234 { 5235 /* One of the template parms was not used in a deduced context in the 5236 specialization. */ 5237 if (!did_error_intro) 5238 { 5239 error ("template parameters not deducible in " 5240 "partial specialization:"); 5241 did_error_intro = true; 5242 } 5243 5244 inform (input_location, " %qD", 5245 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); 5246 } 5247 5248 if (did_error_intro) 5249 return error_mark_node; 5250 5251 /* [temp.class.spec] 5252 5253 The argument list of the specialization shall not be identical to 5254 the implicit argument list of the primary template. */ 5255 tree main_args 5256 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl))); 5257 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args)) 5258 && (!flag_concepts 5259 || !strictly_subsumes (current_template_constraints (), maintmpl))) 5260 { 5261 if (!flag_concepts) 5262 error ("partial specialization %q+D does not specialize " 5263 "any template arguments; to define the primary template, " 5264 "remove the template argument list", decl); 5265 else 5266 error ("partial specialization %q+D does not specialize any " 5267 "template arguments and is not more constrained than " 5268 "the primary template; to define the primary template, " 5269 "remove the template argument list", decl); 5270 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here"); 5271 } 5272 5273 /* A partial specialization that replaces multiple parameters of the 5274 primary template with a pack expansion is less specialized for those 5275 parameters. */ 5276 if (nargs < DECL_NTPARMS (maintmpl)) 5277 { 5278 error ("partial specialization is not more specialized than the " 5279 "primary template because it replaces multiple parameters " 5280 "with a pack expansion"); 5281 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here"); 5282 /* Avoid crash in process_partial_specialization. */ 5283 return decl; 5284 } 5285 5286 else if (nargs > DECL_NTPARMS (maintmpl)) 5287 { 5288 error ("too many arguments for partial specialization %qT", type); 5289 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here"); 5290 /* Avoid crash below. */ 5291 return decl; 5292 } 5293 5294 /* If we aren't in a dependent class, we can actually try deduction. */ 5295 else if (tpd.level == 1 5296 /* FIXME we should be able to handle a partial specialization of a 5297 partial instantiation, but currently we can't (c++/41727). */ 5298 && TMPL_ARGS_DEPTH (specargs) == 1 5299 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs)) 5300 { 5301 auto_diagnostic_group d; 5302 if (pedwarn (input_location, 0, 5303 "partial specialization %qD is not more specialized than", 5304 decl)) 5305 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD", 5306 maintmpl); 5307 } 5308 5309 /* [temp.spec.partial] 5310 5311 The type of a template parameter corresponding to a specialized 5312 non-type argument shall not be dependent on a parameter of the 5313 specialization. 5314 5315 Also, we verify that pack expansions only occur at the 5316 end of the argument list. */ 5317 tpd2.parms = 0; 5318 for (i = 0; i < nargs; ++i) 5319 { 5320 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i)); 5321 tree arg = TREE_VEC_ELT (inner_args, i); 5322 tree packed_args = NULL_TREE; 5323 int j, len = 1; 5324 5325 if (ARGUMENT_PACK_P (arg)) 5326 { 5327 /* Extract the arguments from the argument pack. We'll be 5328 iterating over these in the following loop. */ 5329 packed_args = ARGUMENT_PACK_ARGS (arg); 5330 len = TREE_VEC_LENGTH (packed_args); 5331 } 5332 5333 for (j = 0; j < len; j++) 5334 { 5335 if (packed_args) 5336 /* Get the Jth argument in the parameter pack. */ 5337 arg = TREE_VEC_ELT (packed_args, j); 5338 5339 if (PACK_EXPANSION_P (arg)) 5340 { 5341 /* Pack expansions must come at the end of the 5342 argument list. */ 5343 if ((packed_args && j < len - 1) 5344 || (!packed_args && i < nargs - 1)) 5345 { 5346 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) 5347 error ("parameter pack argument %qE must be at the " 5348 "end of the template argument list", arg); 5349 else 5350 error ("parameter pack argument %qT must be at the " 5351 "end of the template argument list", arg); 5352 } 5353 } 5354 5355 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) 5356 /* We only care about the pattern. */ 5357 arg = PACK_EXPANSION_PATTERN (arg); 5358 5359 if (/* These first two lines are the `non-type' bit. */ 5360 !TYPE_P (arg) 5361 && TREE_CODE (arg) != TEMPLATE_DECL 5362 /* This next two lines are the `argument expression is not just a 5363 simple identifier' condition and also the `specialized 5364 non-type argument' bit. */ 5365 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX 5366 && !((REFERENCE_REF_P (arg) 5367 || TREE_CODE (arg) == VIEW_CONVERT_EXPR) 5368 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX)) 5369 { 5370 /* Look at the corresponding template parameter, 5371 marking which template parameters its type depends 5372 upon. */ 5373 tree type = TREE_TYPE (parm); 5374 5375 if (!tpd2.parms) 5376 { 5377 /* We haven't yet initialized TPD2. Do so now. */ 5378 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs); 5379 /* The number of parameters here is the number in the 5380 main template, which, as checked in the assertion 5381 above, is NARGS. */ 5382 tpd2.parms = XALLOCAVEC (int, nargs); 5383 tpd2.level = 5384 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); 5385 } 5386 5387 /* Mark the template parameters. But this time, we're 5388 looking for the template parameters of the main 5389 template, not in the specialization. */ 5390 tpd2.current_arg = i; 5391 tpd2.arg_uses_template_parms[i] = 0; 5392 memset (tpd2.parms, 0, sizeof (int) * nargs); 5393 for_each_template_parm (type, 5394 &mark_template_parm, 5395 &tpd2, 5396 NULL, 5397 /*include_nondeduced_p=*/false); 5398 5399 if (tpd2.arg_uses_template_parms [i]) 5400 { 5401 /* The type depended on some template parameters. 5402 If they are fully specialized in the 5403 specialization, that's OK. */ 5404 int j; 5405 int count = 0; 5406 for (j = 0; j < nargs; ++j) 5407 if (tpd2.parms[j] != 0 5408 && tpd.arg_uses_template_parms [j]) 5409 ++count; 5410 if (count != 0) 5411 error_n (input_location, count, 5412 "type %qT of template argument %qE depends " 5413 "on a template parameter", 5414 "type %qT of template argument %qE depends " 5415 "on template parameters", 5416 type, 5417 arg); 5418 } 5419 } 5420 } 5421 } 5422 5423 /* We should only get here once. */ 5424 if (TREE_CODE (decl) == TYPE_DECL) 5425 gcc_assert (!COMPLETE_TYPE_P (type)); 5426 5427 // Build the template decl. 5428 tree tmpl = build_template_decl (decl, current_template_parms, 5429 DECL_MEMBER_TEMPLATE_P (maintmpl)); 5430 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); 5431 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs); 5432 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl; 5433 5434 /* Give template template parms a DECL_CONTEXT of the template 5435 for which they are a parameter. */ 5436 for (i = 0; i < ntparms; ++i) 5437 { 5438 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i)); 5439 if (TREE_CODE (parm) == TEMPLATE_DECL) 5440 DECL_CONTEXT (parm) = tmpl; 5441 } 5442 5443 if (VAR_P (decl)) 5444 { 5445 /* We didn't register this in check_explicit_specialization so we could 5446 wait until the constraints were set. */ 5447 tree reg = register_specialization (decl, maintmpl, specargs, false, 0); 5448 if (reg != decl) 5449 /* Redeclaration. */ 5450 return reg; 5451 } 5452 else 5453 associate_classtype_constraints (type); 5454 5455 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) 5456 = tree_cons (specargs, tmpl, 5457 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); 5458 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; 5459 /* Link the DECL_TEMPLATE_RESULT back to the partial TEMPLATE_DECL. */ 5460 gcc_checking_assert (!TI_PARTIAL_INFO (tinfo)); 5461 TI_PARTIAL_INFO (tinfo) = build_template_info (tmpl, NULL_TREE); 5462 5463 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst; 5464 inst = TREE_CHAIN (inst)) 5465 { 5466 tree instance = TREE_VALUE (inst); 5467 if (TYPE_P (instance) 5468 ? (COMPLETE_TYPE_P (instance) 5469 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance)) 5470 : DECL_TEMPLATE_INSTANTIATION (instance)) 5471 { 5472 tree partial_ti = most_specialized_partial_spec (instance, tf_none, 5473 /*rechecking=*/true); 5474 tree inst_decl = (DECL_P (instance) 5475 ? instance : TYPE_NAME (instance)); 5476 if (!partial_ti) 5477 /* OK */; 5478 else if (partial_ti == error_mark_node) 5479 permerror (input_location, 5480 "declaration of %qD ambiguates earlier template " 5481 "instantiation for %qD", decl, inst_decl); 5482 else if (TI_TEMPLATE (partial_ti) == tmpl) 5483 permerror (input_location, 5484 "partial specialization of %qD after instantiation " 5485 "of %qD", decl, inst_decl); 5486 } 5487 } 5488 5489 return decl; 5490 } 5491 5492 /* PARM is a template parameter of some form; return the corresponding 5493 TEMPLATE_PARM_INDEX. */ 5494 5495 static tree 5496 get_template_parm_index (tree parm) 5497 { 5498 if (TREE_CODE (parm) == PARM_DECL 5499 || TREE_CODE (parm) == CONST_DECL) 5500 parm = DECL_INITIAL (parm); 5501 else if (TREE_CODE (parm) == TYPE_DECL 5502 || TREE_CODE (parm) == TEMPLATE_DECL) 5503 parm = TREE_TYPE (parm); 5504 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM 5505 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM 5506 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) 5507 parm = TEMPLATE_TYPE_PARM_INDEX (parm); 5508 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX); 5509 return parm; 5510 } 5511 5512 /* Subroutine of fixed_parameter_pack_p below. Look for any template 5513 parameter packs used by the template parameter PARM. */ 5514 5515 static void 5516 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd) 5517 { 5518 /* A type parm can't refer to another parm. */ 5519 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node) 5520 return; 5521 else if (TREE_CODE (parm) == PARM_DECL) 5522 { 5523 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r, 5524 ppd, ppd->visited); 5525 return; 5526 } 5527 5528 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL); 5529 5530 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm)); 5531 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i) 5532 { 5533 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i)); 5534 if (template_parameter_pack_p (p)) 5535 /* Any packs in the type are expanded by this parameter. */; 5536 else 5537 fixed_parameter_pack_p_1 (p, ppd); 5538 } 5539 } 5540 5541 /* PARM is a template parameter pack. Return any parameter packs used in 5542 its type or the type of any of its template parameters. If there are 5543 any such packs, it will be instantiated into a fixed template parameter 5544 list by partial instantiation rather than be fully deduced. */ 5545 5546 tree 5547 fixed_parameter_pack_p (tree parm) 5548 { 5549 /* This can only be true in a member template. */ 5550 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2) 5551 return NULL_TREE; 5552 /* This can only be true for a parameter pack. */ 5553 if (!template_parameter_pack_p (parm)) 5554 return NULL_TREE; 5555 /* A type parm can't refer to another parm. */ 5556 if (TREE_CODE (parm) == TYPE_DECL) 5557 return NULL_TREE; 5558 5559 tree parameter_packs = NULL_TREE; 5560 struct find_parameter_pack_data ppd; 5561 ppd.parameter_packs = ¶meter_packs; 5562 ppd.visited = new hash_set<tree>; 5563 ppd.type_pack_expansion_p = false; 5564 5565 fixed_parameter_pack_p_1 (parm, &ppd); 5566 5567 delete ppd.visited; 5568 return parameter_packs; 5569 } 5570 5571 /* Check that a template declaration's use of default arguments and 5572 parameter packs is not invalid. Here, PARMS are the template 5573 parameters. IS_PRIMARY is true if DECL is the thing declared by 5574 a primary template. IS_PARTIAL is true if DECL is a partial 5575 specialization. 5576 5577 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend 5578 function template declaration or a friend class template 5579 declaration. In the function case, 1 indicates a declaration, 2 5580 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are 5581 emitted for extraneous default arguments. 5582 5583 Returns TRUE if there were no errors found, FALSE otherwise. */ 5584 5585 bool 5586 check_default_tmpl_args (tree decl, tree parms, bool is_primary, 5587 bool is_partial, int is_friend_decl) 5588 { 5589 const char *msg; 5590 int last_level_to_check; 5591 tree parm_level; 5592 bool no_errors = true; 5593 5594 /* [temp.param] 5595 5596 A default template-argument shall not be specified in a 5597 function template declaration or a function template definition, nor 5598 in the template-parameter-list of the definition of a member of a 5599 class template. */ 5600 5601 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL 5602 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl))) 5603 /* You can't have a function template declaration in a local 5604 scope, nor you can you define a member of a class template in a 5605 local scope. */ 5606 return true; 5607 5608 if ((TREE_CODE (decl) == TYPE_DECL 5609 && TREE_TYPE (decl) 5610 && LAMBDA_TYPE_P (TREE_TYPE (decl))) 5611 || (TREE_CODE (decl) == FUNCTION_DECL 5612 && LAMBDA_FUNCTION_P (decl))) 5613 /* A lambda doesn't have an explicit declaration; don't complain 5614 about the parms of the enclosing class. */ 5615 return true; 5616 5617 if (current_class_type 5618 && !TYPE_BEING_DEFINED (current_class_type) 5619 && DECL_LANG_SPECIFIC (decl) 5620 && DECL_DECLARES_FUNCTION_P (decl) 5621 /* If this is either a friend defined in the scope of the class 5622 or a member function. */ 5623 && (DECL_FUNCTION_MEMBER_P (decl) 5624 ? same_type_p (DECL_CONTEXT (decl), current_class_type) 5625 : DECL_FRIEND_CONTEXT (decl) 5626 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type) 5627 : false) 5628 /* And, if it was a member function, it really was defined in 5629 the scope of the class. */ 5630 && (!DECL_FUNCTION_MEMBER_P (decl) 5631 || DECL_INITIALIZED_IN_CLASS_P (decl))) 5632 /* We already checked these parameters when the template was 5633 declared, so there's no need to do it again now. This function 5634 was defined in class scope, but we're processing its body now 5635 that the class is complete. */ 5636 return true; 5637 5638 /* Core issue 226 (C++0x only): the following only applies to class 5639 templates. */ 5640 if (is_primary 5641 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)) 5642 { 5643 /* [temp.param] 5644 5645 If a template-parameter has a default template-argument, all 5646 subsequent template-parameters shall have a default 5647 template-argument supplied. */ 5648 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) 5649 { 5650 tree inner_parms = TREE_VALUE (parm_level); 5651 int ntparms = TREE_VEC_LENGTH (inner_parms); 5652 int seen_def_arg_p = 0; 5653 int i; 5654 5655 for (i = 0; i < ntparms; ++i) 5656 { 5657 tree parm = TREE_VEC_ELT (inner_parms, i); 5658 5659 if (parm == error_mark_node) 5660 continue; 5661 5662 if (TREE_PURPOSE (parm)) 5663 seen_def_arg_p = 1; 5664 else if (seen_def_arg_p 5665 && !template_parameter_pack_p (TREE_VALUE (parm))) 5666 { 5667 error ("no default argument for %qD", TREE_VALUE (parm)); 5668 /* For better subsequent error-recovery, we indicate that 5669 there should have been a default argument. */ 5670 TREE_PURPOSE (parm) = error_mark_node; 5671 no_errors = false; 5672 } 5673 else if (!is_partial 5674 && !is_friend_decl 5675 /* Don't complain about an enclosing partial 5676 specialization. */ 5677 && parm_level == parms 5678 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl)) 5679 && i < ntparms - 1 5680 && template_parameter_pack_p (TREE_VALUE (parm)) 5681 /* A fixed parameter pack will be partially 5682 instantiated into a fixed length list. */ 5683 && !fixed_parameter_pack_p (TREE_VALUE (parm))) 5684 { 5685 /* A primary class template, primary variable template 5686 (DR 2032), or alias template can only have one 5687 parameter pack, at the end of the template 5688 parameter list. */ 5689 5690 error ("parameter pack %q+D must be at the end of the" 5691 " template parameter list", TREE_VALUE (parm)); 5692 5693 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)) 5694 = error_mark_node; 5695 no_errors = false; 5696 } 5697 } 5698 } 5699 } 5700 5701 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL) 5702 || is_partial 5703 || !is_primary 5704 || is_friend_decl) 5705 /* For an ordinary class template, default template arguments are 5706 allowed at the innermost level, e.g.: 5707 template <class T = int> 5708 struct S {}; 5709 but, in a partial specialization, they're not allowed even 5710 there, as we have in [temp.class.spec]: 5711 5712 The template parameter list of a specialization shall not 5713 contain default template argument values. 5714 5715 So, for a partial specialization, or for a function template 5716 (in C++98/C++03), we look at all of them. */ 5717 ; 5718 else 5719 /* But, for a primary class template that is not a partial 5720 specialization we look at all template parameters except the 5721 innermost ones. */ 5722 parms = TREE_CHAIN (parms); 5723 5724 /* Figure out what error message to issue. */ 5725 if (is_friend_decl == 2) 5726 msg = G_("default template arguments may not be used in function template " 5727 "friend re-declaration"); 5728 else if (is_friend_decl) 5729 msg = G_("default template arguments may not be used in template " 5730 "friend declarations"); 5731 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98)) 5732 msg = G_("default template arguments may not be used in function templates " 5733 "without %<-std=c++11%> or %<-std=gnu++11%>"); 5734 else if (is_partial) 5735 msg = G_("default template arguments may not be used in " 5736 "partial specializations"); 5737 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type)) 5738 msg = G_("default argument for template parameter for class enclosing %qD"); 5739 else 5740 /* Per [temp.param]/9, "A default template-argument shall not be 5741 specified in the template-parameter-lists of the definition of 5742 a member of a class template that appears outside of the member's 5743 class.", thus if we aren't handling a member of a class template 5744 there is no need to examine the parameters. */ 5745 return true; 5746 5747 if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) 5748 /* If we're inside a class definition, there's no need to 5749 examine the parameters to the class itself. On the one 5750 hand, they will be checked when the class is defined, and, 5751 on the other, default arguments are valid in things like: 5752 template <class T = double> 5753 struct S { template <class U> void f(U); }; 5754 Here the default argument for `S' has no bearing on the 5755 declaration of `f'. */ 5756 last_level_to_check = template_class_depth (current_class_type) + 1; 5757 else 5758 /* Check everything. */ 5759 last_level_to_check = 0; 5760 5761 for (parm_level = parms; 5762 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; 5763 parm_level = TREE_CHAIN (parm_level)) 5764 { 5765 tree inner_parms = TREE_VALUE (parm_level); 5766 int i; 5767 int ntparms; 5768 5769 ntparms = TREE_VEC_LENGTH (inner_parms); 5770 for (i = 0; i < ntparms; ++i) 5771 { 5772 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node) 5773 continue; 5774 5775 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) 5776 { 5777 if (msg) 5778 { 5779 no_errors = false; 5780 if (is_friend_decl == 2) 5781 return no_errors; 5782 5783 error (msg, decl); 5784 msg = 0; 5785 } 5786 5787 /* Clear out the default argument so that we are not 5788 confused later. */ 5789 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; 5790 } 5791 } 5792 5793 /* At this point, if we're still interested in issuing messages, 5794 they must apply to classes surrounding the object declared. */ 5795 if (msg) 5796 msg = G_("default argument for template parameter for class " 5797 "enclosing %qD"); 5798 } 5799 5800 return no_errors; 5801 } 5802 5803 /* Worker for push_template_decl_real, called via 5804 for_each_template_parm. DATA is really an int, indicating the 5805 level of the parameters we are interested in. If T is a template 5806 parameter of that level, return nonzero. */ 5807 5808 static int 5809 template_parm_this_level_p (tree t, void* data) 5810 { 5811 int this_level = *(int *)data; 5812 int level; 5813 5814 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) 5815 level = TEMPLATE_PARM_LEVEL (t); 5816 else 5817 level = TEMPLATE_TYPE_LEVEL (t); 5818 return level == this_level; 5819 } 5820 5821 /* Worker for uses_outer_template_parms, called via for_each_template_parm. 5822 DATA is really an int, indicating the innermost outer level of parameters. 5823 If T is a template parameter of that level or further out, return 5824 nonzero. */ 5825 5826 static int 5827 template_parm_outer_level (tree t, void *data) 5828 { 5829 int this_level = *(int *)data; 5830 int level; 5831 5832 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) 5833 level = TEMPLATE_PARM_LEVEL (t); 5834 else 5835 level = TEMPLATE_TYPE_LEVEL (t); 5836 return level <= this_level; 5837 } 5838 5839 /* Creates a TEMPLATE_DECL for the indicated DECL using the template 5840 parameters given by current_template_args, or reuses a 5841 previously existing one, if appropriate. Returns the DECL, or an 5842 equivalent one, if it is replaced via a call to duplicate_decls. 5843 5844 If IS_FRIEND is true, DECL is a friend declaration. */ 5845 5846 tree 5847 push_template_decl (tree decl, bool is_friend) 5848 { 5849 if (decl == error_mark_node || !current_template_parms) 5850 return error_mark_node; 5851 5852 /* See if this is a partial specialization. */ 5853 bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl) 5854 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE 5855 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) 5856 || (VAR_P (decl) 5857 && DECL_LANG_SPECIFIC (decl) 5858 && DECL_TEMPLATE_SPECIALIZATION (decl) 5859 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)))); 5860 5861 /* No surprising friend functions. */ 5862 gcc_checking_assert (is_friend 5863 || !(TREE_CODE (decl) == FUNCTION_DECL 5864 && DECL_UNIQUE_FRIEND_P (decl))); 5865 5866 tree ctx; 5867 if (is_friend) 5868 /* For a friend, we want the context of the friend, not 5869 the type of which it is a friend. */ 5870 ctx = CP_DECL_CONTEXT (decl); 5871 else if (CP_DECL_CONTEXT (decl) 5872 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) 5873 /* In the case of a virtual function, we want the class in which 5874 it is defined. */ 5875 ctx = CP_DECL_CONTEXT (decl); 5876 else 5877 /* Otherwise, if we're currently defining some class, the DECL 5878 is assumed to be a member of the class. */ 5879 ctx = current_scope (); 5880 5881 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) 5882 ctx = NULL_TREE; 5883 5884 if (!DECL_CONTEXT (decl)) 5885 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 5886 5887 /* See if this is a primary template. */ 5888 bool is_primary = false; 5889 if (is_friend && ctx 5890 && uses_template_parms_level (ctx, current_template_depth)) 5891 /* A friend template that specifies a class context, i.e. 5892 template <typename T> friend void A<T>::f(); 5893 is not primary. */ 5894 ; 5895 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl))) 5896 /* Lambdas are not primary. */ 5897 ; 5898 else 5899 is_primary = template_parm_scope_p (); 5900 5901 /* True if the template is a member template, in the sense of 5902 [temp.mem]. */ 5903 bool member_template_p = false; 5904 5905 if (is_primary) 5906 { 5907 warning (OPT_Wtemplates, "template %qD declared", decl); 5908 5909 if (DECL_CLASS_SCOPE_P (decl)) 5910 member_template_p = true; 5911 5912 if (TREE_CODE (decl) == TYPE_DECL 5913 && IDENTIFIER_ANON_P (DECL_NAME (decl))) 5914 { 5915 error ("template class without a name"); 5916 return error_mark_node; 5917 } 5918 else if (TREE_CODE (decl) == FUNCTION_DECL) 5919 { 5920 if (member_template_p) 5921 { 5922 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl)) 5923 error ("member template %qD may not have virt-specifiers", decl); 5924 } 5925 if (DECL_DESTRUCTOR_P (decl)) 5926 { 5927 /* [temp.mem] 5928 5929 A destructor shall not be a member template. */ 5930 error_at (DECL_SOURCE_LOCATION (decl), 5931 "destructor %qD declared as member template", decl); 5932 return error_mark_node; 5933 } 5934 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl)) 5935 && (!prototype_p (TREE_TYPE (decl)) 5936 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node 5937 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))) 5938 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))) 5939 == void_list_node))) 5940 { 5941 /* [basic.stc.dynamic.allocation] 5942 5943 An allocation function can be a function 5944 template. ... Template allocation functions shall 5945 have two or more parameters. */ 5946 error ("invalid template declaration of %qD", decl); 5947 return error_mark_node; 5948 } 5949 } 5950 else if (DECL_IMPLICIT_TYPEDEF_P (decl) 5951 && CLASS_TYPE_P (TREE_TYPE (decl))) 5952 /* Class template. */; 5953 else if (TREE_CODE (decl) == TYPE_DECL 5954 && TYPE_DECL_ALIAS_P (decl)) 5955 /* alias-declaration */ 5956 gcc_assert (!DECL_ARTIFICIAL (decl)); 5957 else if (VAR_P (decl)) 5958 /* C++14 variable template. */; 5959 else if (TREE_CODE (decl) == CONCEPT_DECL) 5960 /* C++20 concept definitions. */; 5961 else 5962 { 5963 error ("template declaration of %q#D", decl); 5964 return error_mark_node; 5965 } 5966 } 5967 5968 bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl) 5969 && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL) 5970 || (VAR_OR_FUNCTION_DECL_P (decl) 5971 && DECL_LOCAL_DECL_P (decl)))); 5972 5973 /* Check to see that the rules regarding the use of default 5974 arguments are not being violated. We check args for a friend 5975 functions when we know whether it's a definition, introducing 5976 declaration or re-declaration. */ 5977 if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)) 5978 check_default_tmpl_args (decl, current_template_parms, 5979 is_primary, is_partial, is_friend); 5980 5981 /* Ensure that there are no parameter packs in the type of this 5982 declaration that have not been expanded. */ 5983 if (TREE_CODE (decl) == FUNCTION_DECL) 5984 { 5985 /* Check each of the arguments individually to see if there are 5986 any bare parameter packs. */ 5987 tree type = TREE_TYPE (decl); 5988 tree arg = DECL_ARGUMENTS (decl); 5989 tree argtype = TYPE_ARG_TYPES (type); 5990 5991 while (arg && argtype) 5992 { 5993 if (!DECL_PACK_P (arg) 5994 && check_for_bare_parameter_packs (TREE_TYPE (arg))) 5995 { 5996 /* This is a PARM_DECL that contains unexpanded parameter 5997 packs. We have already complained about this in the 5998 check_for_bare_parameter_packs call, so just replace 5999 these types with ERROR_MARK_NODE. */ 6000 TREE_TYPE (arg) = error_mark_node; 6001 TREE_VALUE (argtype) = error_mark_node; 6002 } 6003 6004 arg = DECL_CHAIN (arg); 6005 argtype = TREE_CHAIN (argtype); 6006 } 6007 6008 /* Check for bare parameter packs in the return type and the 6009 exception specifiers. */ 6010 if (check_for_bare_parameter_packs (TREE_TYPE (type))) 6011 /* Errors were already issued, set return type to int 6012 as the frontend doesn't expect error_mark_node as 6013 the return type. */ 6014 TREE_TYPE (type) = integer_type_node; 6015 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type))) 6016 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE; 6017 } 6018 else 6019 { 6020 if (check_for_bare_parameter_packs (is_typedef_decl (decl) 6021 ? DECL_ORIGINAL_TYPE (decl) 6022 : TREE_TYPE (decl))) 6023 { 6024 TREE_TYPE (decl) = error_mark_node; 6025 return error_mark_node; 6026 } 6027 6028 if (is_partial && VAR_P (decl) 6029 && check_for_bare_parameter_packs (DECL_TI_ARGS (decl))) 6030 return error_mark_node; 6031 } 6032 6033 if (is_partial) 6034 return process_partial_specialization (decl); 6035 6036 tree args = current_template_args (); 6037 tree tmpl = NULL_TREE; 6038 bool new_template_p = false; 6039 if (local_p) 6040 { 6041 /* Does not get a template head. */ 6042 tmpl = NULL_TREE; 6043 gcc_checking_assert (!is_primary); 6044 } 6045 else if (!ctx 6046 || TREE_CODE (ctx) == FUNCTION_DECL 6047 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx)) 6048 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl))) 6049 || (is_friend && !(DECL_LANG_SPECIFIC (decl) 6050 && DECL_TEMPLATE_INFO (decl)))) 6051 { 6052 if (DECL_LANG_SPECIFIC (decl) 6053 && DECL_TEMPLATE_INFO (decl) 6054 && DECL_TI_TEMPLATE (decl)) 6055 tmpl = DECL_TI_TEMPLATE (decl); 6056 /* If DECL is a TYPE_DECL for a class-template, then there won't 6057 be DECL_LANG_SPECIFIC. The information equivalent to 6058 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ 6059 else if (DECL_IMPLICIT_TYPEDEF_P (decl) 6060 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) 6061 && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) 6062 { 6063 /* Since a template declaration already existed for this 6064 class-type, we must be redeclaring it here. Make sure 6065 that the redeclaration is valid. */ 6066 redeclare_class_template (TREE_TYPE (decl), 6067 current_template_parms, 6068 current_template_constraints ()); 6069 /* We don't need to create a new TEMPLATE_DECL; just use the 6070 one we already had. */ 6071 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); 6072 } 6073 else 6074 { 6075 tmpl = build_template_decl (decl, current_template_parms, 6076 member_template_p); 6077 new_template_p = true; 6078 6079 if (DECL_LANG_SPECIFIC (decl) 6080 && DECL_TEMPLATE_SPECIALIZATION (decl)) 6081 { 6082 /* A specialization of a member template of a template 6083 class. */ 6084 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); 6085 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); 6086 DECL_TEMPLATE_INFO (decl) = NULL_TREE; 6087 } 6088 } 6089 } 6090 else 6091 { 6092 tree a, t, current, parms; 6093 int i; 6094 tree tinfo = get_template_info (decl); 6095 6096 if (!tinfo) 6097 { 6098 error ("template definition of non-template %q#D", decl); 6099 return error_mark_node; 6100 } 6101 6102 tmpl = TI_TEMPLATE (tinfo); 6103 6104 if (DECL_FUNCTION_TEMPLATE_P (tmpl) 6105 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) 6106 && DECL_TEMPLATE_SPECIALIZATION (decl) 6107 && DECL_MEMBER_TEMPLATE_P (tmpl)) 6108 { 6109 /* The declaration is a specialization of a member 6110 template, declared outside the class. Therefore, the 6111 innermost template arguments will be NULL, so we 6112 replace them with the arguments determined by the 6113 earlier call to check_explicit_specialization. */ 6114 args = DECL_TI_ARGS (decl); 6115 6116 tree new_tmpl 6117 = build_template_decl (decl, current_template_parms, 6118 member_template_p); 6119 DECL_TI_TEMPLATE (decl) = new_tmpl; 6120 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); 6121 DECL_TEMPLATE_INFO (new_tmpl) 6122 = build_template_info (tmpl, args); 6123 6124 register_specialization (new_tmpl, 6125 most_general_template (tmpl), 6126 args, 6127 is_friend, 0); 6128 return decl; 6129 } 6130 6131 /* Make sure the template headers we got make sense. */ 6132 6133 parms = DECL_TEMPLATE_PARMS (tmpl); 6134 i = TMPL_PARMS_DEPTH (parms); 6135 if (TMPL_ARGS_DEPTH (args) != i) 6136 { 6137 error ("expected %d levels of template parms for %q#D, got %d", 6138 i, decl, TMPL_ARGS_DEPTH (args)); 6139 DECL_INTERFACE_KNOWN (decl) = 1; 6140 return error_mark_node; 6141 } 6142 else 6143 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) 6144 { 6145 a = TMPL_ARGS_LEVEL (args, i); 6146 t = INNERMOST_TEMPLATE_PARMS (parms); 6147 6148 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) 6149 { 6150 if (current == decl) 6151 error ("got %d template parameters for %q#D", 6152 TREE_VEC_LENGTH (a), decl); 6153 else 6154 error ("got %d template parameters for %q#T", 6155 TREE_VEC_LENGTH (a), current); 6156 error (" but %d required", TREE_VEC_LENGTH (t)); 6157 /* Avoid crash in import_export_decl. */ 6158 DECL_INTERFACE_KNOWN (decl) = 1; 6159 return error_mark_node; 6160 } 6161 6162 if (current == decl) 6163 current = ctx; 6164 else if (current == NULL_TREE) 6165 /* Can happen in erroneous input. */ 6166 break; 6167 else 6168 current = get_containing_scope (current); 6169 } 6170 6171 /* Check that the parms are used in the appropriate qualifying scopes 6172 in the declarator. */ 6173 if (!comp_template_args 6174 (TI_ARGS (tinfo), 6175 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl))))) 6176 { 6177 error ("template arguments to %qD do not match original " 6178 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl)); 6179 if (!uses_template_parms (TI_ARGS (tinfo))) 6180 inform (input_location, "use %<template<>%> for" 6181 " an explicit specialization"); 6182 /* Avoid crash in import_export_decl. */ 6183 DECL_INTERFACE_KNOWN (decl) = 1; 6184 return error_mark_node; 6185 } 6186 6187 /* Check that the constraints for each enclosing template scope are 6188 consistent with the original declarations. */ 6189 if (flag_concepts) 6190 { 6191 tree decl_parms = DECL_TEMPLATE_PARMS (tmpl); 6192 tree scope_parms = current_template_parms; 6193 if (PRIMARY_TEMPLATE_P (tmpl)) 6194 { 6195 decl_parms = TREE_CHAIN (decl_parms); 6196 scope_parms = TREE_CHAIN (scope_parms); 6197 } 6198 while (decl_parms) 6199 { 6200 if (!template_requirements_equivalent_p (decl_parms, scope_parms)) 6201 { 6202 error ("redeclaration of %qD with different constraints", 6203 TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms))); 6204 break; 6205 } 6206 decl_parms = TREE_CHAIN (decl_parms); 6207 scope_parms = TREE_CHAIN (scope_parms); 6208 } 6209 } 6210 } 6211 6212 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl); 6213 6214 if (new_template_p) 6215 { 6216 /* Push template declarations for global functions and types. 6217 Note that we do not try to push a global template friend 6218 declared in a template class; such a thing may well depend on 6219 the template parameters of the class and we'll push it when 6220 instantiating the befriending class. */ 6221 if (!ctx 6222 && !(is_friend && template_class_depth (current_class_type) > 0)) 6223 { 6224 tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend); 6225 if (pushed == error_mark_node) 6226 return error_mark_node; 6227 6228 /* pushdecl may have found an existing template. */ 6229 if (pushed != tmpl) 6230 { 6231 decl = DECL_TEMPLATE_RESULT (pushed); 6232 tmpl = NULL_TREE; 6233 } 6234 } 6235 else if (is_friend) 6236 { 6237 /* Record this decl as belonging to the current class. It's 6238 not chained onto anything else. */ 6239 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true; 6240 gcc_checking_assert (!DECL_CHAIN (tmpl)); 6241 DECL_CHAIN (tmpl) = current_scope (); 6242 } 6243 } 6244 else if (tmpl) 6245 /* The type may have been completed, or (erroneously) changed. */ 6246 TREE_TYPE (tmpl) = TREE_TYPE (decl); 6247 6248 if (tmpl) 6249 { 6250 if (is_primary) 6251 { 6252 tree parms = DECL_TEMPLATE_PARMS (tmpl); 6253 6254 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; 6255 6256 /* Give template template parms a DECL_CONTEXT of the template 6257 for which they are a parameter. */ 6258 parms = INNERMOST_TEMPLATE_PARMS (parms); 6259 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) 6260 { 6261 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); 6262 if (TREE_CODE (parm) == TEMPLATE_DECL) 6263 DECL_CONTEXT (parm) = tmpl; 6264 } 6265 6266 if (TREE_CODE (decl) == TYPE_DECL 6267 && TYPE_DECL_ALIAS_P (decl)) 6268 { 6269 if (tree constr 6270 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl))) 6271 { 6272 /* ??? Why don't we do this here for all templates? */ 6273 constr = build_constraints (constr, NULL_TREE); 6274 set_constraints (decl, constr); 6275 } 6276 } 6277 } 6278 6279 /* The DECL_TI_ARGS of DECL contains full set of arguments 6280 referring wback to its most general template. If TMPL is a 6281 specialization, ARGS may only have the innermost set of 6282 arguments. Add the missing argument levels if necessary. */ 6283 if (DECL_TEMPLATE_INFO (tmpl)) 6284 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args); 6285 6286 tree info = build_template_info (tmpl, args); 6287 6288 if (DECL_IMPLICIT_TYPEDEF_P (decl)) 6289 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); 6290 else 6291 { 6292 retrofit_lang_decl (decl); 6293 DECL_TEMPLATE_INFO (decl) = info; 6294 } 6295 } 6296 6297 if (flag_implicit_templates 6298 && !is_friend 6299 && TREE_PUBLIC (decl) 6300 && VAR_OR_FUNCTION_DECL_P (decl)) 6301 /* Set DECL_COMDAT on template instantiations; if we force 6302 them to be emitted by explicit instantiation, 6303 mark_needed will tell cgraph to do the right thing. */ 6304 DECL_COMDAT (decl) = true; 6305 6306 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl); 6307 6308 return decl; 6309 } 6310 6311 /* FN is an inheriting constructor that inherits from the constructor 6312 template INHERITED; turn FN into a constructor template with a matching 6313 template header. */ 6314 6315 tree 6316 add_inherited_template_parms (tree fn, tree inherited) 6317 { 6318 tree inner_parms 6319 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited)); 6320 inner_parms = copy_node (inner_parms); 6321 tree parms 6322 = tree_cons (size_int (current_template_depth + 1), 6323 inner_parms, current_template_parms); 6324 tree tmpl = build_template_decl (fn, parms, /*member*/true); 6325 tree args = template_parms_to_args (parms); 6326 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args); 6327 DECL_ARTIFICIAL (tmpl) = true; 6328 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; 6329 return tmpl; 6330 } 6331 6332 /* Called when a class template TYPE is redeclared with the indicated 6333 template PARMS, e.g.: 6334 6335 template <class T> struct S; 6336 template <class T> struct S {}; */ 6337 6338 bool 6339 redeclare_class_template (tree type, tree parms, tree cons) 6340 { 6341 tree tmpl; 6342 tree tmpl_parms; 6343 int i; 6344 6345 if (!TYPE_TEMPLATE_INFO (type)) 6346 { 6347 error ("%qT is not a template type", type); 6348 return false; 6349 } 6350 6351 tmpl = TYPE_TI_TEMPLATE (type); 6352 if (!PRIMARY_TEMPLATE_P (tmpl)) 6353 /* The type is nested in some template class. Nothing to worry 6354 about here; there are no new template parameters for the nested 6355 type. */ 6356 return true; 6357 6358 if (!parms) 6359 { 6360 error ("template specifiers not specified in declaration of %qD", 6361 tmpl); 6362 return false; 6363 } 6364 6365 parms = INNERMOST_TEMPLATE_PARMS (parms); 6366 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); 6367 6368 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) 6369 { 6370 error_n (input_location, TREE_VEC_LENGTH (parms), 6371 "redeclared with %d template parameter", 6372 "redeclared with %d template parameters", 6373 TREE_VEC_LENGTH (parms)); 6374 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms), 6375 "previous declaration %qD used %d template parameter", 6376 "previous declaration %qD used %d template parameters", 6377 tmpl, TREE_VEC_LENGTH (tmpl_parms)); 6378 return false; 6379 } 6380 6381 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) 6382 { 6383 tree tmpl_parm; 6384 tree parm; 6385 6386 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node 6387 || TREE_VEC_ELT (parms, i) == error_mark_node) 6388 continue; 6389 6390 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); 6391 if (error_operand_p (tmpl_parm)) 6392 return false; 6393 6394 parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); 6395 6396 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or 6397 TEMPLATE_DECL. */ 6398 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm) 6399 || (TREE_CODE (tmpl_parm) != TYPE_DECL 6400 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))) 6401 || (TREE_CODE (tmpl_parm) != PARM_DECL 6402 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm)) 6403 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))) 6404 || (TREE_CODE (tmpl_parm) == PARM_DECL 6405 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm)) 6406 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))))) 6407 { 6408 auto_diagnostic_group d; 6409 error ("template parameter %q+#D", tmpl_parm); 6410 if (DECL_P (parm)) 6411 inform (DECL_SOURCE_LOCATION (parm), "redeclared here as %q#D", parm); 6412 else 6413 inform (input_location, "redeclared here"); 6414 return false; 6415 } 6416 6417 /* The parameters can be declared to introduce different 6418 constraints. */ 6419 tree p1 = TREE_VEC_ELT (tmpl_parms, i); 6420 tree p2 = TREE_VEC_ELT (parms, i); 6421 if (!template_parameter_constraints_equivalent_p (p1, p2)) 6422 { 6423 auto_diagnostic_group d; 6424 error ("declaration of template parameter %q+#D with different " 6425 "constraints", parm); 6426 inform (DECL_SOURCE_LOCATION (tmpl_parm), 6427 "original declaration appeared here"); 6428 return false; 6429 } 6430 6431 /* Give each template template parm in this redeclaration a 6432 DECL_CONTEXT of the template for which they are a parameter. */ 6433 if (TREE_CODE (parm) == TEMPLATE_DECL) 6434 { 6435 gcc_checking_assert (DECL_CONTEXT (parm) == NULL_TREE 6436 || DECL_CONTEXT (parm) == tmpl); 6437 DECL_CONTEXT (parm) = tmpl; 6438 } 6439 } 6440 6441 if (!merge_default_template_args (parms, tmpl_parms, /*class_p=*/true)) 6442 return false; 6443 6444 tree ci = get_constraints (tmpl); 6445 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE; 6446 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE; 6447 6448 /* Two classes with different constraints declare different entities. */ 6449 if (!cp_tree_equal (req1, req2)) 6450 { 6451 auto_diagnostic_group d; 6452 error_at (input_location, "redeclaration of %q#D with different " 6453 "constraints", tmpl); 6454 inform (DECL_SOURCE_LOCATION (tmpl), 6455 "original declaration appeared here"); 6456 return false; 6457 } 6458 6459 return true; 6460 } 6461 6462 /* The actual substitution part of instantiate_non_dependent_expr, 6463 to be used when the caller has already checked 6464 !instantiation_dependent_uneval_expression_p (expr) 6465 and cleared processing_template_decl. */ 6466 6467 tree 6468 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain) 6469 { 6470 return tsubst_expr (expr, /*args=*/NULL_TREE, complain, /*in_decl=*/NULL_TREE); 6471 } 6472 6473 /* Instantiate the non-dependent expression EXPR. */ 6474 6475 tree 6476 instantiate_non_dependent_expr (tree expr, 6477 tsubst_flags_t complain /* = tf_error */) 6478 { 6479 if (expr == NULL_TREE) 6480 return NULL_TREE; 6481 6482 if (processing_template_decl) 6483 { 6484 /* The caller should have checked this already. */ 6485 gcc_checking_assert (!instantiation_dependent_uneval_expression_p (expr)); 6486 processing_template_decl_sentinel s; 6487 expr = instantiate_non_dependent_expr_internal (expr, complain); 6488 } 6489 return expr; 6490 } 6491 6492 /* Like instantiate_non_dependent_expr, but return NULL_TREE if the 6493 expression is dependent or non-constant. */ 6494 6495 tree 6496 instantiate_non_dependent_or_null (tree expr) 6497 { 6498 if (expr == NULL_TREE) 6499 return NULL_TREE; 6500 if (processing_template_decl) 6501 { 6502 if (!is_nondependent_constant_expression (expr)) 6503 expr = NULL_TREE; 6504 else 6505 { 6506 processing_template_decl_sentinel s; 6507 expr = instantiate_non_dependent_expr_internal (expr, tf_error); 6508 } 6509 } 6510 return expr; 6511 } 6512 6513 /* True iff T is a specialization of a variable template. */ 6514 6515 bool 6516 variable_template_specialization_p (tree t) 6517 { 6518 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t)) 6519 return false; 6520 tree tmpl = DECL_TI_TEMPLATE (t); 6521 return variable_template_p (tmpl); 6522 } 6523 6524 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias 6525 template declaration, or a TYPE_DECL for an alias declaration. */ 6526 6527 bool 6528 alias_type_or_template_p (tree t) 6529 { 6530 if (t == NULL_TREE) 6531 return false; 6532 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t)) 6533 || (TYPE_P (t) 6534 && TYPE_NAME (t) 6535 && TYPE_DECL_ALIAS_P (TYPE_NAME (t))) 6536 || DECL_ALIAS_TEMPLATE_P (t)); 6537 } 6538 6539 /* If T is a specialization of an alias template, return it; otherwise return 6540 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */ 6541 6542 tree 6543 alias_template_specialization_p (const_tree t, 6544 bool transparent_typedefs) 6545 { 6546 if (!TYPE_P (t)) 6547 return NULL_TREE; 6548 6549 /* It's an alias template specialization if it's an alias and its 6550 TYPE_NAME is a specialization of a primary template. */ 6551 if (typedef_variant_p (t)) 6552 { 6553 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t)) 6554 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))) 6555 return CONST_CAST_TREE (t); 6556 if (transparent_typedefs) 6557 return alias_template_specialization_p (DECL_ORIGINAL_TYPE 6558 (TYPE_NAME (t)), 6559 transparent_typedefs); 6560 } 6561 6562 return NULL_TREE; 6563 } 6564 6565 /* A cache of the result of complex_alias_template_p. */ 6566 6567 static GTY((deletable)) hash_map<const_tree, tree> *complex_alias_tmpl_info; 6568 6569 /* Data structure for complex_alias_template_*. */ 6570 6571 struct uses_all_template_parms_data 6572 { 6573 int level; 6574 tree *seen; 6575 }; 6576 6577 /* walk_tree callback for complex_alias_template_p. */ 6578 6579 static tree 6580 complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_) 6581 { 6582 tree t = *tp; 6583 auto &data = *(struct uses_all_template_parms_data*)data_; 6584 6585 switch (TREE_CODE (t)) 6586 { 6587 case TEMPLATE_TYPE_PARM: 6588 case TEMPLATE_PARM_INDEX: 6589 case TEMPLATE_TEMPLATE_PARM: 6590 case BOUND_TEMPLATE_TEMPLATE_PARM: 6591 { 6592 tree idx = get_template_parm_index (t); 6593 if (TEMPLATE_PARM_LEVEL (idx) == data.level) 6594 data.seen[TEMPLATE_PARM_IDX (idx)] = boolean_true_node; 6595 } 6596 6597 default:; 6598 } 6599 6600 if (!PACK_EXPANSION_P (t)) 6601 return 0; 6602 6603 /* An alias template with a pack expansion that expands a pack from the 6604 enclosing class needs to be considered complex, to avoid confusion with 6605 the same pack being used as an argument to the alias's own template 6606 parameter (91966). */ 6607 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack; 6608 pack = TREE_CHAIN (pack)) 6609 { 6610 tree parm_pack = TREE_VALUE (pack); 6611 if (!TEMPLATE_PARM_P (parm_pack)) 6612 continue; 6613 int idx, level; 6614 template_parm_level_and_index (parm_pack, &level, &idx); 6615 if (level < data.level) 6616 return t; 6617 6618 /* Consider the expanded packs to be used outside the expansion... */ 6619 data.seen[idx] = boolean_true_node; 6620 } 6621 6622 /* ...but don't walk into the pattern. Consider PR104008: 6623 6624 template <typename T, typename... Ts> 6625 using IsOneOf = disjunction<is_same<T, Ts>...>; 6626 6627 where IsOneOf seemingly uses all of its template parameters in its 6628 expansion (and does not expand a pack from the enclosing class), so the 6629 alias was not marked as complex. However, if it is used like 6630 "IsOneOf<T>", the empty pack for Ts means that T no longer appears in the 6631 expansion. So only Ts is considered used by the pack expansion. */ 6632 *walk_subtrees = false; 6633 6634 return 0; 6635 } 6636 6637 /* An alias template is complex from a SFINAE perspective if a template-id 6638 using that alias can be ill-formed when the expansion is not, as with 6639 the void_t template. 6640 6641 If this predicate returns true in the ordinary case, the out parameter 6642 SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if 6643 the I'th template parameter of the alias template is used in the alias. */ 6644 6645 static bool 6646 complex_alias_template_p (const_tree tmpl, tree *seen_out) 6647 { 6648 tmpl = most_general_template (tmpl); 6649 if (!PRIMARY_TEMPLATE_P (tmpl)) 6650 return false; 6651 6652 /* A renaming alias isn't complex. */ 6653 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl) 6654 return false; 6655 6656 /* Any other constrained alias is complex. */ 6657 if (get_constraints (tmpl)) 6658 return true; 6659 6660 /* An alias with dependent type attributes is complex. */ 6661 if (any_dependent_type_attributes_p (DECL_ATTRIBUTES 6662 (DECL_TEMPLATE_RESULT (tmpl)))) 6663 return true; 6664 6665 if (!complex_alias_tmpl_info) 6666 complex_alias_tmpl_info = hash_map<const_tree, tree>::create_ggc (13); 6667 6668 if (tree *slot = complex_alias_tmpl_info->get (tmpl)) 6669 { 6670 tree result = *slot; 6671 if (result == boolean_false_node) 6672 return false; 6673 if (result == boolean_true_node) 6674 return true; 6675 gcc_assert (TREE_CODE (result) == TREE_VEC); 6676 if (seen_out) 6677 *seen_out = result; 6678 return true; 6679 } 6680 6681 struct uses_all_template_parms_data data; 6682 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); 6683 tree parms = DECL_TEMPLATE_PARMS (tmpl); 6684 data.level = TMPL_PARMS_DEPTH (parms); 6685 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms)); 6686 tree seen = make_tree_vec (len); 6687 data.seen = TREE_VEC_BEGIN (seen); 6688 for (int i = 0; i < len; ++i) 6689 data.seen[i] = boolean_false_node; 6690 6691 if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data)) 6692 { 6693 complex_alias_tmpl_info->put (tmpl, boolean_true_node); 6694 return true; 6695 } 6696 6697 for (int i = 0; i < len; ++i) 6698 if (data.seen[i] != boolean_true_node) 6699 { 6700 complex_alias_tmpl_info->put (tmpl, seen); 6701 if (seen_out) 6702 *seen_out = seen; 6703 return true; 6704 } 6705 6706 complex_alias_tmpl_info->put (tmpl, boolean_false_node); 6707 return false; 6708 } 6709 6710 /* If T is a specialization of a complex alias template with a dependent 6711 argument for an unused template parameter, return it; otherwise return 6712 NULL_TREE. If T is a typedef to such a specialization, return the 6713 specialization. */ 6714 6715 tree 6716 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs) 6717 { 6718 if (t == error_mark_node) 6719 return NULL_TREE; 6720 gcc_assert (TYPE_P (t)); 6721 6722 if (!processing_template_decl || !typedef_variant_p (t)) 6723 return NULL_TREE; 6724 6725 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t)) 6726 { 6727 tree seen = NULL_TREE; 6728 if (complex_alias_template_p (TI_TEMPLATE (tinfo), &seen)) 6729 { 6730 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)); 6731 if (!seen) 6732 { 6733 if (any_dependent_template_arguments_p (args)) 6734 return CONST_CAST_TREE (t); 6735 } 6736 else 6737 { 6738 gcc_assert (TREE_VEC_LENGTH (args) == TREE_VEC_LENGTH (seen)); 6739 for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i) 6740 if (TREE_VEC_ELT (seen, i) != boolean_true_node 6741 && dependent_template_arg_p (TREE_VEC_ELT (args, i))) 6742 return CONST_CAST_TREE (t); 6743 } 6744 6745 return NULL_TREE; 6746 } 6747 } 6748 6749 if (transparent_typedefs) 6750 { 6751 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t)); 6752 return dependent_alias_template_spec_p (utype, transparent_typedefs); 6753 } 6754 6755 return NULL_TREE; 6756 } 6757 6758 /* Return the number of innermost template parameters in TMPL. */ 6759 6760 static int 6761 num_innermost_template_parms (const_tree tmpl) 6762 { 6763 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl)); 6764 return TREE_VEC_LENGTH (parms); 6765 } 6766 6767 /* Return either TMPL or another template that it is equivalent to under DR 6768 1286: An alias that just changes the name of a template is equivalent to 6769 the other template. */ 6770 6771 static tree 6772 get_underlying_template (tree tmpl) 6773 { 6774 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); 6775 while (DECL_ALIAS_TEMPLATE_P (tmpl)) 6776 { 6777 /* Determine if the alias is equivalent to an underlying template. */ 6778 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); 6779 /* The underlying type may have been ill-formed. Don't proceed. */ 6780 if (!orig_type) 6781 break; 6782 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type); 6783 if (!tinfo) 6784 break; 6785 6786 tree underlying = TI_TEMPLATE (tinfo); 6787 if (!PRIMARY_TEMPLATE_P (underlying) 6788 || (num_innermost_template_parms (tmpl) 6789 != num_innermost_template_parms (underlying))) 6790 break; 6791 6792 /* Does the alias add cv-quals? */ 6793 if (TYPE_QUALS (TREE_TYPE (underlying)) != TYPE_QUALS (TREE_TYPE (tmpl))) 6794 break; 6795 6796 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl)); 6797 if (!comp_template_args (TI_ARGS (tinfo), alias_args)) 6798 break; 6799 6800 /* Are any default template arguments equivalent? */ 6801 tree aparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl)); 6802 tree uparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (underlying)); 6803 const int nparms = TREE_VEC_LENGTH (aparms); 6804 for (int i = 0; i < nparms; ++i) 6805 { 6806 tree adefarg = TREE_PURPOSE (TREE_VEC_ELT (aparms, i)); 6807 tree udefarg = TREE_PURPOSE (TREE_VEC_ELT (uparms, i)); 6808 if (!template_args_equal (adefarg, udefarg)) 6809 goto top_break; 6810 } 6811 6812 /* If TMPL adds or changes any constraints, it isn't equivalent. I think 6813 it's appropriate to treat a less-constrained alias as equivalent. */ 6814 if (!at_least_as_constrained (underlying, tmpl)) 6815 break; 6816 6817 /* If TMPL adds dependent type attributes, it isn't equivalent. */ 6818 if (any_dependent_type_attributes_p (DECL_ATTRIBUTES 6819 (DECL_TEMPLATE_RESULT (tmpl)))) 6820 break; 6821 6822 /* Alias is equivalent. Strip it and repeat. */ 6823 tmpl = underlying; 6824 } 6825 top_break:; 6826 6827 return tmpl; 6828 } 6829 6830 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which 6831 must be a reference-to-function or a pointer-to-function type, as specified 6832 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set, 6833 and check that the resulting function has external linkage. */ 6834 6835 static tree 6836 convert_nontype_argument_function (tree type, tree expr, 6837 tsubst_flags_t complain) 6838 { 6839 tree fns = expr; 6840 tree fn, fn_no_ptr; 6841 linkage_kind linkage; 6842 6843 fn = instantiate_type (type, fns, tf_none); 6844 if (fn == error_mark_node) 6845 return error_mark_node; 6846 6847 if (value_dependent_expression_p (fn)) 6848 goto accept; 6849 6850 fn_no_ptr = fn; 6851 if (REFERENCE_REF_P (fn_no_ptr)) 6852 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0); 6853 fn_no_ptr = strip_fnptr_conv (fn_no_ptr); 6854 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR) 6855 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0); 6856 if (BASELINK_P (fn_no_ptr)) 6857 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr); 6858 6859 /* [temp.arg.nontype]/1 6860 6861 A template-argument for a non-type, non-template template-parameter 6862 shall be one of: 6863 [...] 6864 -- the address of an object or function with external [C++11: or 6865 internal] linkage. */ 6866 6867 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr); 6868 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL) 6869 { 6870 if (complain & tf_error) 6871 { 6872 location_t loc = cp_expr_loc_or_input_loc (expr); 6873 error_at (loc, "%qE is not a valid template argument for type %qT", 6874 expr, type); 6875 if (TYPE_PTR_P (type)) 6876 inform (loc, "it must be the address of a function " 6877 "with external linkage"); 6878 else 6879 inform (loc, "it must be the name of a function with " 6880 "external linkage"); 6881 } 6882 return NULL_TREE; 6883 } 6884 6885 linkage = decl_linkage (fn_no_ptr); 6886 if ((cxx_dialect < cxx11 && linkage != lk_external) 6887 || (cxx_dialect < cxx17 && linkage == lk_none)) 6888 { 6889 if (complain & tf_error) 6890 { 6891 location_t loc = cp_expr_loc_or_input_loc (expr); 6892 if (cxx_dialect >= cxx11) 6893 error_at (loc, "%qE is not a valid template argument for type " 6894 "%qT because %qD has no linkage", 6895 expr, type, fn_no_ptr); 6896 else 6897 error_at (loc, "%qE is not a valid template argument for type " 6898 "%qT because %qD does not have external linkage", 6899 expr, type, fn_no_ptr); 6900 } 6901 return NULL_TREE; 6902 } 6903 6904 accept: 6905 if (TYPE_REF_P (type)) 6906 { 6907 if (REFERENCE_REF_P (fn)) 6908 fn = TREE_OPERAND (fn, 0); 6909 else 6910 fn = build_address (fn); 6911 } 6912 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn))) 6913 fn = build_nop (type, fn); 6914 6915 return fn; 6916 } 6917 6918 /* Subroutine of convert_nontype_argument. 6919 Check if EXPR of type TYPE is a valid pointer-to-member constant. 6920 Emit an error otherwise. */ 6921 6922 static bool 6923 check_valid_ptrmem_cst_expr (tree type, tree expr, 6924 tsubst_flags_t complain) 6925 { 6926 tree orig_expr = expr; 6927 STRIP_NOPS (expr); 6928 if (null_ptr_cst_p (expr)) 6929 return true; 6930 if (TREE_CODE (expr) == PTRMEM_CST 6931 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type), 6932 PTRMEM_CST_CLASS (expr))) 6933 return true; 6934 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr)) 6935 return true; 6936 if (processing_template_decl 6937 && TREE_CODE (expr) == ADDR_EXPR 6938 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF) 6939 return true; 6940 if (complain & tf_error) 6941 { 6942 location_t loc = cp_expr_loc_or_input_loc (orig_expr); 6943 error_at (loc, "%qE is not a valid template argument for type %qT", 6944 orig_expr, type); 6945 if (TREE_CODE (expr) != PTRMEM_CST) 6946 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>"); 6947 else 6948 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr)); 6949 } 6950 return false; 6951 } 6952 6953 /* Returns TRUE iff the address of OP is value-dependent. 6954 6955 14.6.2.4 [temp.dep.temp]: 6956 A non-integral non-type template-argument is dependent if its type is 6957 dependent or it has either of the following forms 6958 qualified-id 6959 & qualified-id 6960 and contains a nested-name-specifier which specifies a class-name that 6961 names a dependent type. 6962 6963 We generalize this to just say that the address of a member of a 6964 dependent class is value-dependent; the above doesn't cover the 6965 address of a static data member named with an unqualified-id. */ 6966 6967 static bool 6968 has_value_dependent_address (tree op) 6969 { 6970 STRIP_ANY_LOCATION_WRAPPER (op); 6971 6972 /* We could use get_inner_reference here, but there's no need; 6973 this is only relevant for template non-type arguments, which 6974 can only be expressed as &id-expression. */ 6975 if (DECL_P (op)) 6976 { 6977 tree ctx = CP_DECL_CONTEXT (op); 6978 6979 if (TYPE_P (ctx) && dependent_type_p (ctx)) 6980 return true; 6981 6982 if (VAR_P (op) 6983 && TREE_STATIC (op) 6984 && TREE_CODE (ctx) == FUNCTION_DECL 6985 && type_dependent_expression_p (ctx)) 6986 return true; 6987 } 6988 6989 return false; 6990 } 6991 6992 /* The next set of functions are used for providing helpful explanatory 6993 diagnostics for failed overload resolution. Their messages should be 6994 indented by two spaces for consistency with the messages in 6995 call.cc */ 6996 6997 static int 6998 unify_success (bool /*explain_p*/) 6999 { 7000 return 0; 7001 } 7002 7003 /* Other failure functions should call this one, to provide a single function 7004 for setting a breakpoint on. */ 7005 7006 static int 7007 unify_invalid (bool /*explain_p*/) 7008 { 7009 return 1; 7010 } 7011 7012 static int 7013 unify_parameter_deduction_failure (bool explain_p, tree parm) 7014 { 7015 if (explain_p) 7016 inform (input_location, 7017 " couldn%'t deduce template parameter %qD", parm); 7018 return unify_invalid (explain_p); 7019 } 7020 7021 static int 7022 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg) 7023 { 7024 if (explain_p) 7025 inform (input_location, 7026 " types %qT and %qT have incompatible cv-qualifiers", 7027 parm, arg); 7028 return unify_invalid (explain_p); 7029 } 7030 7031 static int 7032 unify_type_mismatch (bool explain_p, tree parm, tree arg) 7033 { 7034 if (explain_p) 7035 inform (input_location, " mismatched types %qT and %qT", parm, arg); 7036 return unify_invalid (explain_p); 7037 } 7038 7039 static int 7040 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg) 7041 { 7042 if (explain_p) 7043 inform (input_location, 7044 " template parameter %qD is not a parameter pack, but " 7045 "argument %qD is", 7046 parm, arg); 7047 return unify_invalid (explain_p); 7048 } 7049 7050 static int 7051 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg) 7052 { 7053 if (explain_p) 7054 inform (input_location, 7055 " template argument %qE does not match " 7056 "pointer-to-member constant %qE", 7057 arg, parm); 7058 return unify_invalid (explain_p); 7059 } 7060 7061 static int 7062 unify_expression_unequal (bool explain_p, tree parm, tree arg) 7063 { 7064 if (explain_p) 7065 inform (input_location, " %qE is not equivalent to %qE", parm, arg); 7066 return unify_invalid (explain_p); 7067 } 7068 7069 static int 7070 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg) 7071 { 7072 if (explain_p) 7073 inform (input_location, 7074 " inconsistent parameter pack deduction with %qT and %qT", 7075 old_arg, new_arg); 7076 return unify_invalid (explain_p); 7077 } 7078 7079 static int 7080 unify_inconsistency (bool explain_p, tree parm, tree first, tree second) 7081 { 7082 if (explain_p) 7083 { 7084 if (TYPE_P (parm)) 7085 inform (input_location, 7086 " deduced conflicting types for parameter %qT (%qT and %qT)", 7087 parm, first, second); 7088 else 7089 inform (input_location, 7090 " deduced conflicting values for non-type parameter " 7091 "%qE (%qE and %qE)", parm, first, second); 7092 } 7093 return unify_invalid (explain_p); 7094 } 7095 7096 static int 7097 unify_vla_arg (bool explain_p, tree arg) 7098 { 7099 if (explain_p) 7100 inform (input_location, 7101 " variable-sized array type %qT is not " 7102 "a valid template argument", 7103 arg); 7104 return unify_invalid (explain_p); 7105 } 7106 7107 static int 7108 unify_method_type_error (bool explain_p, tree arg) 7109 { 7110 if (explain_p) 7111 inform (input_location, 7112 " member function type %qT is not a valid template argument", 7113 arg); 7114 return unify_invalid (explain_p); 7115 } 7116 7117 static int 7118 unify_arity (bool explain_p, int have, int wanted, bool least_p = false) 7119 { 7120 if (explain_p) 7121 { 7122 if (least_p) 7123 inform_n (input_location, wanted, 7124 " candidate expects at least %d argument, %d provided", 7125 " candidate expects at least %d arguments, %d provided", 7126 wanted, have); 7127 else 7128 inform_n (input_location, wanted, 7129 " candidate expects %d argument, %d provided", 7130 " candidate expects %d arguments, %d provided", 7131 wanted, have); 7132 } 7133 return unify_invalid (explain_p); 7134 } 7135 7136 static int 7137 unify_too_many_arguments (bool explain_p, int have, int wanted) 7138 { 7139 return unify_arity (explain_p, have, wanted); 7140 } 7141 7142 static int 7143 unify_too_few_arguments (bool explain_p, int have, int wanted, 7144 bool least_p = false) 7145 { 7146 return unify_arity (explain_p, have, wanted, least_p); 7147 } 7148 7149 static int 7150 unify_arg_conversion (bool explain_p, tree to_type, 7151 tree from_type, tree arg) 7152 { 7153 if (explain_p) 7154 inform (cp_expr_loc_or_input_loc (arg), 7155 " cannot convert %qE (type %qT) to type %qT", 7156 arg, from_type, to_type); 7157 return unify_invalid (explain_p); 7158 } 7159 7160 static int 7161 unify_no_common_base (bool explain_p, enum template_base_result r, 7162 tree parm, tree arg) 7163 { 7164 if (explain_p) 7165 switch (r) 7166 { 7167 case tbr_ambiguous_baseclass: 7168 inform (input_location, " %qT is an ambiguous base class of %qT", 7169 parm, arg); 7170 break; 7171 default: 7172 inform (input_location, " %qT is not derived from %qT", arg, parm); 7173 break; 7174 } 7175 return unify_invalid (explain_p); 7176 } 7177 7178 static int 7179 unify_inconsistent_template_template_parameters (bool explain_p) 7180 { 7181 if (explain_p) 7182 inform (input_location, 7183 " template parameters of a template template argument are " 7184 "inconsistent with other deduced template arguments"); 7185 return unify_invalid (explain_p); 7186 } 7187 7188 static int 7189 unify_template_deduction_failure (bool explain_p, tree parm, tree arg) 7190 { 7191 if (explain_p) 7192 inform (input_location, 7193 " cannot deduce a template for %qT from non-template type %qT", 7194 parm, arg); 7195 return unify_invalid (explain_p); 7196 } 7197 7198 static int 7199 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg) 7200 { 7201 if (explain_p) 7202 inform (input_location, 7203 " template argument %qE does not match %qE", arg, parm); 7204 return unify_invalid (explain_p); 7205 } 7206 7207 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an 7208 argument for TYPE, points to an unsuitable object. 7209 7210 Also adjust the type of the index in C++20 array subobject references. */ 7211 7212 static bool 7213 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain) 7214 { 7215 switch (TREE_CODE (expr)) 7216 { 7217 CASE_CONVERT: 7218 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0), 7219 complain); 7220 7221 case TARGET_EXPR: 7222 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr), 7223 complain); 7224 7225 case CONSTRUCTOR: 7226 { 7227 for (auto &e: CONSTRUCTOR_ELTS (expr)) 7228 if (invalid_tparm_referent_p (TREE_TYPE (e.value), e.value, complain)) 7229 return true; 7230 } 7231 break; 7232 7233 case ADDR_EXPR: 7234 { 7235 tree decl = TREE_OPERAND (expr, 0); 7236 7237 if (cxx_dialect >= cxx20) 7238 while (TREE_CODE (decl) == COMPONENT_REF 7239 || TREE_CODE (decl) == ARRAY_REF) 7240 { 7241 tree &op = TREE_OPERAND (decl, 1); 7242 if (TREE_CODE (decl) == ARRAY_REF 7243 && TREE_CODE (op) == INTEGER_CST) 7244 /* Canonicalize array offsets to ptrdiff_t; how they were 7245 written doesn't matter for subobject identity. */ 7246 op = fold_convert (ptrdiff_type_node, op); 7247 decl = TREE_OPERAND (decl, 0); 7248 } 7249 7250 if (!VAR_OR_FUNCTION_DECL_P (decl)) 7251 { 7252 if (complain & tf_error) 7253 error_at (cp_expr_loc_or_input_loc (expr), 7254 "%qE is not a valid template argument of type %qT " 7255 "because %qE is not a variable or function", 7256 expr, type, decl); 7257 return true; 7258 } 7259 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl)) 7260 { 7261 if (complain & tf_error) 7262 error_at (cp_expr_loc_or_input_loc (expr), 7263 "%qE is not a valid template argument of type %qT " 7264 "in C++98 because %qD does not have external linkage", 7265 expr, type, decl); 7266 return true; 7267 } 7268 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17) 7269 && decl_linkage (decl) == lk_none) 7270 { 7271 if (complain & tf_error) 7272 error_at (cp_expr_loc_or_input_loc (expr), 7273 "%qE is not a valid template argument of type %qT " 7274 "because %qD has no linkage", expr, type, decl); 7275 return true; 7276 } 7277 /* C++17: For a non-type template-parameter of reference or pointer 7278 type, the value of the constant expression shall not refer to (or 7279 for a pointer type, shall not be the address of): 7280 * a subobject (4.5), 7281 * a temporary object (15.2), 7282 * a string literal (5.13.5), 7283 * the result of a typeid expression (8.2.8), or 7284 * a predefined __func__ variable (11.4.1). */ 7285 else if (VAR_P (decl) && DECL_ARTIFICIAL (decl) 7286 && !DECL_NTTP_OBJECT_P (decl)) 7287 { 7288 gcc_checking_assert (DECL_TINFO_P (decl) || DECL_FNAME_P (decl)); 7289 if (complain & tf_error) 7290 error ("the address of %qD is not a valid template argument", 7291 decl); 7292 return true; 7293 } 7294 else if (cxx_dialect < cxx20 7295 && !(same_type_ignoring_top_level_qualifiers_p 7296 (strip_array_types (TREE_TYPE (type)), 7297 strip_array_types (TREE_TYPE (decl))))) 7298 { 7299 if (complain & tf_error) 7300 error ("the address of the %qT subobject of %qD is not a " 7301 "valid template argument", TREE_TYPE (type), decl); 7302 return true; 7303 } 7304 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) 7305 { 7306 if (complain & tf_error) 7307 error ("the address of %qD is not a valid template argument " 7308 "because it does not have static storage duration", 7309 decl); 7310 return true; 7311 } 7312 } 7313 break; 7314 7315 default: 7316 if (!INDIRECT_TYPE_P (type)) 7317 /* We're only concerned about pointers and references here. */; 7318 else if (cxx_dialect >= cxx11 && integer_zerop (expr)) 7319 /* Null pointer values are OK in C++11. */; 7320 else 7321 { 7322 if (VAR_P (expr)) 7323 { 7324 if (complain & tf_error) 7325 error ("%qD is not a valid template argument " 7326 "because %qD is a variable, not the address of " 7327 "a variable", expr, expr); 7328 return true; 7329 } 7330 else 7331 { 7332 if (complain & tf_error) 7333 error ("%qE is not a valid template argument for %qT " 7334 "because it is not the address of a variable", 7335 expr, type); 7336 return true; 7337 } 7338 } 7339 } 7340 return false; 7341 7342 } 7343 7344 /* Return a VAR_DECL for the C++20 template parameter object corresponding to 7345 template argument EXPR. */ 7346 7347 static tree 7348 create_template_parm_object (tree expr, tsubst_flags_t complain) 7349 { 7350 tree orig = expr; 7351 if (TREE_CODE (expr) == TARGET_EXPR) 7352 expr = TARGET_EXPR_INITIAL (expr); 7353 7354 if (!TREE_CONSTANT (expr)) 7355 { 7356 if ((complain & tf_error) 7357 && require_rvalue_constant_expression (orig)) 7358 cxx_constant_value (orig); 7359 return error_mark_node; 7360 } 7361 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain)) 7362 return error_mark_node; 7363 7364 /* This is no longer a compound literal. */ 7365 gcc_assert (!TREE_HAS_CONSTRUCTOR (expr)); 7366 7367 return get_template_parm_object (expr, mangle_template_parm_object (expr)); 7368 } 7369 7370 /* The template arguments corresponding to template parameter objects of types 7371 that contain pointers to members. */ 7372 7373 static GTY(()) hash_map<tree, tree> *tparm_obj_values; 7374 7375 /* Find or build an nttp object for (already-validated) EXPR with name 7376 NAME. */ 7377 7378 tree 7379 get_template_parm_object (tree expr, tree name) 7380 { 7381 tree decl = get_global_binding (name); 7382 if (decl) 7383 return decl; 7384 7385 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST); 7386 decl = create_temporary_var (type); 7387 DECL_NTTP_OBJECT_P (decl) = true; 7388 DECL_CONTEXT (decl) = NULL_TREE; 7389 TREE_STATIC (decl) = true; 7390 DECL_DECLARED_CONSTEXPR_P (decl) = true; 7391 TREE_READONLY (decl) = true; 7392 DECL_NAME (decl) = name; 7393 SET_DECL_ASSEMBLER_NAME (decl, name); 7394 comdat_linkage (decl); 7395 7396 if (!zero_init_p (type)) 7397 { 7398 /* If EXPR contains any PTRMEM_CST, they will get clobbered by 7399 lower_var_init before we're done mangling. So store the original 7400 value elsewhere. */ 7401 tree copy = unshare_constructor (expr); 7402 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy); 7403 } 7404 7405 pushdecl_top_level_and_finish (decl, expr); 7406 7407 return decl; 7408 } 7409 7410 /* Return the actual template argument corresponding to template parameter 7411 object VAR. */ 7412 7413 tree 7414 tparm_object_argument (tree var) 7415 { 7416 if (zero_init_p (TREE_TYPE (var))) 7417 return DECL_INITIAL (var); 7418 return *(tparm_obj_values->get (var)); 7419 } 7420 7421 /* Attempt to convert the non-type template parameter EXPR to the 7422 indicated TYPE. If the conversion is successful, return the 7423 converted value. If the conversion is unsuccessful, return 7424 NULL_TREE if we issued an error message, or error_mark_node if we 7425 did not. We issue error messages for out-and-out bad template 7426 parameters, but not simply because the conversion failed, since we 7427 might be just trying to do argument deduction. Both TYPE and EXPR 7428 must be non-dependent. 7429 7430 The conversion follows the special rules described in 7431 [temp.arg.nontype], and it is much more strict than an implicit 7432 conversion. 7433 7434 This function is called twice for each template argument (see 7435 lookup_template_class for a more accurate description of this 7436 problem). This means that we need to handle expressions which 7437 are not valid in a C++ source, but can be created from the 7438 first call (for instance, casts to perform conversions). These 7439 hacks can go away after we fix the double coercion problem. */ 7440 7441 static tree 7442 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain) 7443 { 7444 tree expr_type; 7445 location_t loc = cp_expr_loc_or_input_loc (expr); 7446 7447 /* Detect immediately string literals as invalid non-type argument. 7448 This special-case is not needed for correctness (we would easily 7449 catch this later), but only to provide better diagnostic for this 7450 common user mistake. As suggested by DR 100, we do not mention 7451 linkage issues in the diagnostic as this is not the point. */ 7452 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type)) 7453 { 7454 if (complain & tf_error) 7455 error ("%qE is not a valid template argument for type %qT " 7456 "because string literals can never be used in this context", 7457 expr, type); 7458 return NULL_TREE; 7459 } 7460 7461 /* Add the ADDR_EXPR now for the benefit of 7462 value_dependent_expression_p. */ 7463 if (TYPE_PTROBV_P (type) 7464 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE) 7465 { 7466 expr = decay_conversion (expr, complain); 7467 if (expr == error_mark_node) 7468 return error_mark_node; 7469 } 7470 7471 /* If we are in a template, EXPR may be non-dependent, but still 7472 have a syntactic, rather than semantic, form. For example, EXPR 7473 might be a SCOPE_REF, rather than the VAR_DECL to which the 7474 SCOPE_REF refers. Preserving the qualifying scope is necessary 7475 so that access checking can be performed when the template is 7476 instantiated -- but here we need the resolved form so that we can 7477 convert the argument. */ 7478 bool non_dep = false; 7479 if (TYPE_REF_OBJ_P (type) 7480 && has_value_dependent_address (expr)) 7481 /* If we want the address and it's value-dependent, don't fold. */; 7482 else if (processing_template_decl 7483 && !instantiation_dependent_expression_p (expr)) 7484 non_dep = true; 7485 if (error_operand_p (expr)) 7486 return error_mark_node; 7487 expr_type = TREE_TYPE (expr); 7488 7489 /* If the argument is non-dependent, perform any conversions in 7490 non-dependent context as well. */ 7491 processing_template_decl_sentinel s (non_dep); 7492 if (non_dep) 7493 expr = instantiate_non_dependent_expr_internal (expr, complain); 7494 7495 bool val_dep_p = value_dependent_expression_p (expr); 7496 if (val_dep_p) 7497 expr = canonicalize_expr_argument (expr, complain); 7498 else 7499 STRIP_ANY_LOCATION_WRAPPER (expr); 7500 7501 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied 7502 to a non-type argument of "nullptr". */ 7503 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type)) 7504 expr = fold_simple (convert (type, expr)); 7505 7506 /* In C++11, integral or enumeration non-type template arguments can be 7507 arbitrary constant expressions. Pointer and pointer to 7508 member arguments can be general constant expressions that evaluate 7509 to a null value, but otherwise still need to be of a specific form. */ 7510 if (cxx_dialect >= cxx11) 7511 { 7512 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type)) 7513 /* A PTRMEM_CST is already constant, and a valid template 7514 argument for a parameter of pointer to member type, we just want 7515 to leave it in that form rather than lower it to a 7516 CONSTRUCTOR. */; 7517 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type) 7518 || cxx_dialect >= cxx17) 7519 { 7520 /* C++17: A template-argument for a non-type template-parameter shall 7521 be a converted constant expression (8.20) of the type of the 7522 template-parameter. */ 7523 expr = build_converted_constant_expr (type, expr, complain); 7524 if (expr == error_mark_node) 7525 /* Make sure we return NULL_TREE only if we have really issued 7526 an error, as described above. */ 7527 return (complain & tf_error) ? NULL_TREE : error_mark_node; 7528 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR) 7529 { 7530 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true; 7531 return expr; 7532 } 7533 expr = maybe_constant_value (expr, NULL_TREE, mce_true); 7534 expr = convert_from_reference (expr); 7535 /* EXPR may have become value-dependent. */ 7536 val_dep_p = value_dependent_expression_p (expr); 7537 } 7538 else if (TYPE_PTR_OR_PTRMEM_P (type)) 7539 { 7540 tree folded = maybe_constant_value (expr, NULL_TREE, mce_true); 7541 if (TYPE_PTR_P (type) ? integer_zerop (folded) 7542 : null_member_pointer_value_p (folded)) 7543 expr = folded; 7544 } 7545 } 7546 7547 if (TYPE_REF_P (type)) 7548 expr = mark_lvalue_use (expr); 7549 else 7550 expr = mark_rvalue_use (expr); 7551 7552 /* HACK: Due to double coercion, we can get a 7553 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here, 7554 which is the tree that we built on the first call (see 7555 below when coercing to reference to object or to reference to 7556 function). We just strip everything and get to the arg. 7557 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C 7558 for examples. */ 7559 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type)) 7560 { 7561 /* Check this before we strip *& to avoid redundancy. */ 7562 if (!mark_single_function (expr, complain)) 7563 return error_mark_node; 7564 7565 tree probe_type, probe = expr; 7566 if (REFERENCE_REF_P (probe)) 7567 probe = TREE_OPERAND (probe, 0); 7568 probe_type = TREE_TYPE (probe); 7569 if (TREE_CODE (probe) == NOP_EXPR) 7570 { 7571 /* ??? Maybe we could use convert_from_reference here, but we 7572 would need to relax its constraints because the NOP_EXPR 7573 could actually change the type to something more cv-qualified, 7574 and this is not folded by convert_from_reference. */ 7575 tree addr = TREE_OPERAND (probe, 0); 7576 if (TYPE_REF_P (probe_type) 7577 && TREE_CODE (addr) == ADDR_EXPR 7578 && TYPE_PTR_P (TREE_TYPE (addr)) 7579 && (same_type_ignoring_top_level_qualifiers_p 7580 (TREE_TYPE (probe_type), 7581 TREE_TYPE (TREE_TYPE (addr))))) 7582 { 7583 expr = TREE_OPERAND (addr, 0); 7584 expr_type = TREE_TYPE (probe_type); 7585 } 7586 } 7587 } 7588 7589 /* [temp.arg.nontype]/5, bullet 1 7590 7591 For a non-type template-parameter of integral or enumeration type, 7592 integral promotions (_conv.prom_) and integral conversions 7593 (_conv.integral_) are applied. */ 7594 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type) 7595 || SCALAR_FLOAT_TYPE_P (type)) 7596 { 7597 if (cxx_dialect < cxx11) 7598 { 7599 tree t = build_converted_constant_expr (type, expr, complain); 7600 t = maybe_constant_value (t); 7601 if (t != error_mark_node) 7602 expr = t; 7603 } 7604 7605 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr))) 7606 return error_mark_node; 7607 7608 /* Notice that there are constant expressions like '4 % 0' which 7609 do not fold into integer constants. */ 7610 if (!CONSTANT_CLASS_P (expr) && !val_dep_p) 7611 { 7612 if (complain & tf_error) 7613 { 7614 int errs = errorcount, warns = warningcount + werrorcount; 7615 if (!require_potential_constant_expression (expr)) 7616 expr = error_mark_node; 7617 else 7618 expr = cxx_constant_value (expr); 7619 if (errorcount > errs || warningcount + werrorcount > warns) 7620 inform (loc, "in template argument for type %qT", type); 7621 if (expr == error_mark_node) 7622 return NULL_TREE; 7623 /* else cxx_constant_value complained but gave us 7624 a real constant, so go ahead. */ 7625 if (!CONSTANT_CLASS_P (expr)) 7626 { 7627 /* Some assemble time constant expressions like 7628 (intptr_t)&&lab1 - (intptr_t)&&lab2 or 7629 4 + (intptr_t)&&var satisfy reduced_constant_expression_p 7630 as we can emit them into .rodata initializers of 7631 variables, yet they can't fold into an INTEGER_CST at 7632 compile time. Refuse them here. */ 7633 gcc_checking_assert (reduced_constant_expression_p (expr)); 7634 error_at (loc, "template argument %qE for type %qT not " 7635 "a compile-time constant", expr, type); 7636 return NULL_TREE; 7637 } 7638 } 7639 else 7640 return NULL_TREE; 7641 } 7642 7643 /* Avoid typedef problems. */ 7644 if (TREE_TYPE (expr) != type) 7645 expr = fold_convert (type, expr); 7646 } 7647 /* [temp.arg.nontype]/5, bullet 2 7648 7649 For a non-type template-parameter of type pointer to object, 7650 qualification conversions (_conv.qual_) and the array-to-pointer 7651 conversion (_conv.array_) are applied. */ 7652 else if (TYPE_PTROBV_P (type)) 7653 { 7654 tree decayed = expr; 7655 7656 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from 7657 decay_conversion or an explicit cast. If it's a problematic cast, 7658 we'll complain about it below. */ 7659 if (TREE_CODE (expr) == NOP_EXPR) 7660 { 7661 tree probe = expr; 7662 STRIP_NOPS (probe); 7663 if (TREE_CODE (probe) == ADDR_EXPR 7664 && TYPE_PTR_P (TREE_TYPE (probe))) 7665 { 7666 expr = probe; 7667 expr_type = TREE_TYPE (expr); 7668 } 7669 } 7670 7671 /* [temp.arg.nontype]/1 (TC1 version, DR 49): 7672 7673 A template-argument for a non-type, non-template template-parameter 7674 shall be one of: [...] 7675 7676 -- the name of a non-type template-parameter; 7677 -- the address of an object or function with external linkage, [...] 7678 expressed as "& id-expression" where the & is optional if the name 7679 refers to a function or array, or if the corresponding 7680 template-parameter is a reference. 7681 7682 Here, we do not care about functions, as they are invalid anyway 7683 for a parameter of type pointer-to-object. */ 7684 7685 if (val_dep_p) 7686 /* Non-type template parameters are OK. */ 7687 ; 7688 else if (cxx_dialect >= cxx11 && integer_zerop (expr)) 7689 /* Null pointer values are OK in C++11. */; 7690 else if (TREE_CODE (expr) != ADDR_EXPR 7691 && !INDIRECT_TYPE_P (expr_type)) 7692 /* Other values, like integer constants, might be valid 7693 non-type arguments of some other type. */ 7694 return error_mark_node; 7695 else if (invalid_tparm_referent_p (type, expr, complain)) 7696 return NULL_TREE; 7697 7698 expr = decayed; 7699 7700 expr = perform_qualification_conversions (type, expr); 7701 if (expr == error_mark_node) 7702 return error_mark_node; 7703 } 7704 /* [temp.arg.nontype]/5, bullet 3 7705 7706 For a non-type template-parameter of type reference to object, no 7707 conversions apply. The type referred to by the reference may be more 7708 cv-qualified than the (otherwise identical) type of the 7709 template-argument. The template-parameter is bound directly to the 7710 template-argument, which must be an lvalue. */ 7711 else if (TYPE_REF_OBJ_P (type)) 7712 { 7713 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type), 7714 expr_type)) 7715 return error_mark_node; 7716 7717 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type)) 7718 { 7719 if (complain & tf_error) 7720 error ("%qE is not a valid template argument for type %qT " 7721 "because of conflicts in cv-qualification", expr, type); 7722 return NULL_TREE; 7723 } 7724 7725 if (!lvalue_p (expr)) 7726 { 7727 if (complain & tf_error) 7728 error ("%qE is not a valid template argument for type %qT " 7729 "because it is not an lvalue", expr, type); 7730 return NULL_TREE; 7731 } 7732 7733 /* [temp.arg.nontype]/1 7734 7735 A template-argument for a non-type, non-template template-parameter 7736 shall be one of: [...] 7737 7738 -- the address of an object or function with external linkage. */ 7739 if (INDIRECT_REF_P (expr) 7740 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0)))) 7741 { 7742 expr = TREE_OPERAND (expr, 0); 7743 if (DECL_P (expr)) 7744 { 7745 if (complain & tf_error) 7746 error ("%q#D is not a valid template argument for type %qT " 7747 "because a reference variable does not have a constant " 7748 "address", expr, type); 7749 return NULL_TREE; 7750 } 7751 } 7752 7753 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p) 7754 /* OK, dependent reference. We don't want to ask whether a DECL is 7755 itself value-dependent, since what we want here is its address. */; 7756 else 7757 { 7758 expr = build_address (expr); 7759 7760 if (invalid_tparm_referent_p (type, expr, complain)) 7761 return NULL_TREE; 7762 } 7763 7764 if (!same_type_p (type, TREE_TYPE (expr))) 7765 expr = build_nop (type, expr); 7766 } 7767 /* [temp.arg.nontype]/5, bullet 4 7768 7769 For a non-type template-parameter of type pointer to function, only 7770 the function-to-pointer conversion (_conv.func_) is applied. If the 7771 template-argument represents a set of overloaded functions (or a 7772 pointer to such), the matching function is selected from the set 7773 (_over.over_). */ 7774 else if (TYPE_PTRFN_P (type)) 7775 { 7776 /* If the argument is a template-id, we might not have enough 7777 context information to decay the pointer. */ 7778 if (!type_unknown_p (expr_type)) 7779 { 7780 expr = decay_conversion (expr, complain); 7781 if (expr == error_mark_node) 7782 return error_mark_node; 7783 } 7784 7785 if (cxx_dialect >= cxx11 && integer_zerop (expr)) 7786 /* Null pointer values are OK in C++11. */ 7787 return perform_qualification_conversions (type, expr); 7788 7789 expr = convert_nontype_argument_function (type, expr, complain); 7790 if (!expr || expr == error_mark_node) 7791 return expr; 7792 } 7793 /* [temp.arg.nontype]/5, bullet 5 7794 7795 For a non-type template-parameter of type reference to function, no 7796 conversions apply. If the template-argument represents a set of 7797 overloaded functions, the matching function is selected from the set 7798 (_over.over_). */ 7799 else if (TYPE_REFFN_P (type)) 7800 { 7801 if (TREE_CODE (expr) == ADDR_EXPR) 7802 { 7803 if (complain & tf_error) 7804 { 7805 error ("%qE is not a valid template argument for type %qT " 7806 "because it is a pointer", expr, type); 7807 inform (input_location, "try using %qE instead", 7808 TREE_OPERAND (expr, 0)); 7809 } 7810 return NULL_TREE; 7811 } 7812 7813 expr = convert_nontype_argument_function (type, expr, complain); 7814 if (!expr || expr == error_mark_node) 7815 return expr; 7816 } 7817 /* [temp.arg.nontype]/5, bullet 6 7818 7819 For a non-type template-parameter of type pointer to member function, 7820 no conversions apply. If the template-argument represents a set of 7821 overloaded member functions, the matching member function is selected 7822 from the set (_over.over_). */ 7823 else if (TYPE_PTRMEMFUNC_P (type)) 7824 { 7825 expr = instantiate_type (type, expr, tf_none); 7826 if (expr == error_mark_node) 7827 return error_mark_node; 7828 7829 /* [temp.arg.nontype] bullet 1 says the pointer to member 7830 expression must be a pointer-to-member constant. */ 7831 if (!val_dep_p 7832 && !check_valid_ptrmem_cst_expr (type, expr, complain)) 7833 return NULL_TREE; 7834 7835 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST 7836 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */ 7837 if (fnptr_conv_p (type, TREE_TYPE (expr))) 7838 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr)); 7839 } 7840 /* [temp.arg.nontype]/5, bullet 7 7841 7842 For a non-type template-parameter of type pointer to data member, 7843 qualification conversions (_conv.qual_) are applied. */ 7844 else if (TYPE_PTRDATAMEM_P (type)) 7845 { 7846 /* [temp.arg.nontype] bullet 1 says the pointer to member 7847 expression must be a pointer-to-member constant. */ 7848 if (!val_dep_p 7849 && !check_valid_ptrmem_cst_expr (type, expr, complain)) 7850 return NULL_TREE; 7851 7852 expr = perform_qualification_conversions (type, expr); 7853 if (expr == error_mark_node) 7854 return expr; 7855 } 7856 else if (NULLPTR_TYPE_P (type)) 7857 { 7858 if (!NULLPTR_TYPE_P (TREE_TYPE (expr))) 7859 { 7860 if (complain & tf_error) 7861 error ("%qE is not a valid template argument for type %qT " 7862 "because it is of type %qT", expr, type, TREE_TYPE (expr)); 7863 return NULL_TREE; 7864 } 7865 return expr; 7866 } 7867 else if (CLASS_TYPE_P (type)) 7868 { 7869 /* Replace the argument with a reference to the corresponding template 7870 parameter object. */ 7871 if (!val_dep_p) 7872 expr = create_template_parm_object (expr, complain); 7873 if (expr == error_mark_node) 7874 return NULL_TREE; 7875 } 7876 /* A template non-type parameter must be one of the above. */ 7877 else 7878 gcc_unreachable (); 7879 7880 /* Sanity check: did we actually convert the argument to the 7881 right type? */ 7882 gcc_assert (same_type_ignoring_top_level_qualifiers_p 7883 (type, TREE_TYPE (expr))); 7884 return convert_from_reference (expr); 7885 } 7886 7887 /* Subroutine of coerce_template_template_parms, which returns 1 if 7888 PARM_PARM and ARG_PARM match using the rule for the template 7889 parameters of template template parameters. Both PARM and ARG are 7890 template parameters; the rest of the arguments are the same as for 7891 coerce_template_template_parms. 7892 */ 7893 static int 7894 coerce_template_template_parm (tree parm, 7895 tree arg, 7896 tsubst_flags_t complain, 7897 tree in_decl, 7898 tree outer_args) 7899 { 7900 if (arg == NULL_TREE || error_operand_p (arg) 7901 || parm == NULL_TREE || error_operand_p (parm)) 7902 return 0; 7903 7904 if (TREE_CODE (arg) != TREE_CODE (parm)) 7905 return 0; 7906 7907 switch (TREE_CODE (parm)) 7908 { 7909 case TEMPLATE_DECL: 7910 /* We encounter instantiations of templates like 7911 template <template <template <class> class> class TT> 7912 class C; */ 7913 { 7914 if (!coerce_template_template_parms 7915 (parm, arg, complain, in_decl, outer_args)) 7916 return 0; 7917 } 7918 /* Fall through. */ 7919 7920 case TYPE_DECL: 7921 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg)) 7922 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))) 7923 /* Argument is a parameter pack but parameter is not. */ 7924 return 0; 7925 break; 7926 7927 case PARM_DECL: 7928 /* The tsubst call is used to handle cases such as 7929 7930 template <int> class C {}; 7931 template <class T, template <T> class TT> class D {}; 7932 D<int, C> d; 7933 7934 i.e. the parameter list of TT depends on earlier parameters. */ 7935 if (!uses_template_parms (TREE_TYPE (arg))) 7936 { 7937 ++processing_template_decl; 7938 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl); 7939 --processing_template_decl; 7940 if (!uses_template_parms (t) 7941 && !same_type_p (t, TREE_TYPE (arg))) 7942 return 0; 7943 } 7944 7945 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg)) 7946 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))) 7947 /* Argument is a parameter pack but parameter is not. */ 7948 return 0; 7949 7950 break; 7951 7952 default: 7953 gcc_unreachable (); 7954 } 7955 7956 return 1; 7957 } 7958 7959 /* Coerce template argument list ARGLIST for use with template 7960 template-parameter TEMPL. */ 7961 7962 static tree 7963 coerce_template_args_for_ttp (tree templ, tree arglist, 7964 tsubst_flags_t complain) 7965 { 7966 /* Consider an example where a template template parameter declared as 7967 7968 template <class T, class U = std::allocator<T> > class TT 7969 7970 The template parameter level of T and U are one level larger than 7971 of TT. To proper process the default argument of U, say when an 7972 instantiation `TT<int>' is seen, we need to build the full 7973 arguments containing {int} as the innermost level. Outer levels, 7974 available when not appearing as default template argument, can be 7975 obtained from the arguments of the enclosing template. 7976 7977 Suppose that TT is later substituted with std::vector. The above 7978 instantiation is `TT<int, std::allocator<T> >' with TT at 7979 level 1, and T at level 2, while the template arguments at level 1 7980 becomes {std::vector} and the inner level 2 is {int}. */ 7981 7982 tree outer = DECL_CONTEXT (templ); 7983 if (outer) 7984 outer = generic_targs_for (outer); 7985 else if (current_template_parms) 7986 { 7987 /* This is an argument of the current template, so we haven't set 7988 DECL_CONTEXT yet. We can also get here when level-lowering a 7989 bound ttp. */ 7990 tree relevant_template_parms; 7991 7992 /* Parameter levels that are greater than the level of the given 7993 template template parm are irrelevant. */ 7994 relevant_template_parms = current_template_parms; 7995 while (TMPL_PARMS_DEPTH (relevant_template_parms) 7996 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ))) 7997 relevant_template_parms = TREE_CHAIN (relevant_template_parms); 7998 7999 outer = template_parms_to_args (relevant_template_parms); 8000 } 8001 8002 if (outer) 8003 arglist = add_to_template_args (outer, arglist); 8004 8005 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ); 8006 return coerce_template_parms (parmlist, arglist, templ, complain); 8007 } 8008 8009 /* A cache of template template parameters with match-all default 8010 arguments. */ 8011 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache; 8012 8013 /* T is a bound template template-parameter. Copy its arguments into default 8014 arguments of the template template-parameter's template parameters. */ 8015 8016 static tree 8017 add_defaults_to_ttp (tree otmpl) 8018 { 8019 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl)) 8020 return *c; 8021 8022 tree ntmpl = copy_node (otmpl); 8023 8024 tree ntype = copy_node (TREE_TYPE (otmpl)); 8025 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl; 8026 TYPE_MAIN_VARIANT (ntype) = ntype; 8027 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE; 8028 TYPE_NAME (ntype) = ntmpl; 8029 SET_TYPE_STRUCTURAL_EQUALITY (ntype); 8030 8031 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype) 8032 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype)); 8033 TEMPLATE_PARM_DECL (idx) = ntmpl; 8034 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype; 8035 8036 tree oparms = DECL_TEMPLATE_PARMS (otmpl); 8037 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms); 8038 TREE_CHAIN (parms) = TREE_CHAIN (oparms); 8039 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms)); 8040 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i) 8041 { 8042 tree o = TREE_VEC_ELT (vec, i); 8043 if (!template_parameter_pack_p (TREE_VALUE (o))) 8044 { 8045 tree n = TREE_VEC_ELT (vec, i) = copy_node (o); 8046 TREE_PURPOSE (n) = any_targ_node; 8047 } 8048 } 8049 8050 tree oresult = DECL_TEMPLATE_RESULT (otmpl); 8051 tree gen_otmpl = DECL_TI_TEMPLATE (oresult); 8052 tree gen_ntmpl; 8053 if (gen_otmpl == otmpl) 8054 gen_ntmpl = ntmpl; 8055 else 8056 gen_ntmpl = add_defaults_to_ttp (gen_otmpl); 8057 8058 tree nresult = copy_decl (oresult); 8059 DECL_TEMPLATE_INFO (nresult) 8060 = build_template_info (gen_ntmpl, TI_ARGS (DECL_TEMPLATE_INFO (oresult))); 8061 DECL_TEMPLATE_RESULT (ntmpl) = nresult; 8062 8063 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl); 8064 return ntmpl; 8065 } 8066 8067 /* ARG is a bound potential template template-argument, and PARGS is a list 8068 of arguments for the corresponding template template-parameter. Adjust 8069 PARGS as appropriate for application to ARG's template, and if ARG is a 8070 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template 8071 arguments to the template template parameter. */ 8072 8073 static tree 8074 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain) 8075 { 8076 ++processing_template_decl; 8077 tree arg_tmpl = TYPE_TI_TEMPLATE (arg); 8078 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl)) 8079 { 8080 /* When comparing two template template-parameters in partial ordering, 8081 rewrite the one currently being used as an argument to have default 8082 arguments for all parameters. */ 8083 arg_tmpl = add_defaults_to_ttp (arg_tmpl); 8084 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain); 8085 if (pargs != error_mark_node) 8086 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl), 8087 TYPE_TI_ARGS (arg)); 8088 } 8089 else 8090 { 8091 tree aparms 8092 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl)); 8093 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain); 8094 } 8095 --processing_template_decl; 8096 return pargs; 8097 } 8098 8099 /* Subroutine of unify for the case when PARM is a 8100 BOUND_TEMPLATE_TEMPLATE_PARM. */ 8101 8102 static int 8103 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg, 8104 bool explain_p) 8105 { 8106 tree parmvec = TYPE_TI_ARGS (parm); 8107 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg)); 8108 8109 /* The template template parm might be variadic and the argument 8110 not, so flatten both argument lists. */ 8111 parmvec = expand_template_argument_pack (parmvec); 8112 argvec = expand_template_argument_pack (argvec); 8113 8114 if (flag_new_ttp) 8115 { 8116 /* In keeping with P0522R0, adjust P's template arguments 8117 to apply to A's template; then flatten it again. */ 8118 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none); 8119 nparmvec = expand_template_argument_pack (nparmvec); 8120 8121 if (unify (tparms, targs, nparmvec, argvec, 8122 UNIFY_ALLOW_NONE, explain_p)) 8123 return 1; 8124 8125 /* If the P0522 adjustment eliminated a pack expansion, deduce 8126 empty packs. */ 8127 if (flag_new_ttp 8128 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec) 8129 && unify_pack_expansion (tparms, targs, parmvec, argvec, 8130 DEDUCE_EXACT, /*sub*/true, explain_p)) 8131 return 1; 8132 } 8133 else 8134 { 8135 /* Deduce arguments T, i from TT<T> or TT<i>. 8136 We check each element of PARMVEC and ARGVEC individually 8137 rather than the whole TREE_VEC since they can have 8138 different number of elements, which is allowed under N2555. */ 8139 8140 int len = TREE_VEC_LENGTH (parmvec); 8141 8142 /* Check if the parameters end in a pack, making them 8143 variadic. */ 8144 int parm_variadic_p = 0; 8145 if (len > 0 8146 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1))) 8147 parm_variadic_p = 1; 8148 8149 for (int i = 0; i < len - parm_variadic_p; ++i) 8150 /* If the template argument list of P contains a pack 8151 expansion that is not the last template argument, the 8152 entire template argument list is a non-deduced 8153 context. */ 8154 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i))) 8155 return unify_success (explain_p); 8156 8157 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p) 8158 return unify_too_few_arguments (explain_p, 8159 TREE_VEC_LENGTH (argvec), len); 8160 8161 for (int i = 0; i < len - parm_variadic_p; ++i) 8162 if (unify (tparms, targs, 8163 TREE_VEC_ELT (parmvec, i), 8164 TREE_VEC_ELT (argvec, i), 8165 UNIFY_ALLOW_NONE, explain_p)) 8166 return 1; 8167 8168 if (parm_variadic_p 8169 && unify_pack_expansion (tparms, targs, 8170 parmvec, argvec, 8171 DEDUCE_EXACT, 8172 /*subr=*/true, explain_p)) 8173 return 1; 8174 } 8175 8176 return 0; 8177 } 8178 8179 /* Return 1 if PARM_TMPL and ARG_TMPL match using rule for 8180 template template parameters. 8181 8182 Consider the example: 8183 template <class T> class A; 8184 template<template <class U> class TT> class B; 8185 8186 For B<A>, PARM_TMPL is TT, while ARG_TMPL is A, 8187 and OUTER_ARGS contains A. */ 8188 8189 static int 8190 coerce_template_template_parms (tree parm_tmpl, 8191 tree arg_tmpl, 8192 tsubst_flags_t complain, 8193 tree in_decl, 8194 tree outer_args) 8195 { 8196 int nparms, nargs, i; 8197 tree parm, arg; 8198 int variadic_p = 0; 8199 8200 tree parm_parms = DECL_INNERMOST_TEMPLATE_PARMS (parm_tmpl); 8201 tree arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (arg_tmpl); 8202 tree gen_arg_tmpl = most_general_template (arg_tmpl); 8203 tree gen_arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_arg_tmpl); 8204 8205 nparms = TREE_VEC_LENGTH (parm_parms); 8206 nargs = TREE_VEC_LENGTH (arg_parms); 8207 8208 if (flag_new_ttp) 8209 { 8210 /* P0522R0: A template template-parameter P is at least as specialized as 8211 a template template-argument A if, given the following rewrite to two 8212 function templates, the function template corresponding to P is at 8213 least as specialized as the function template corresponding to A 8214 according to the partial ordering rules for function templates 8215 ([temp.func.order]). Given an invented class template X with the 8216 template parameter list of A (including default arguments): 8217 8218 * Each of the two function templates has the same template parameters, 8219 respectively, as P or A. 8220 8221 * Each function template has a single function parameter whose type is 8222 a specialization of X with template arguments corresponding to the 8223 template parameters from the respective function template where, for 8224 each template parameter PP in the template parameter list of the 8225 function template, a corresponding template argument AA is formed. If 8226 PP declares a parameter pack, then AA is the pack expansion 8227 PP... ([temp.variadic]); otherwise, AA is the id-expression PP. 8228 8229 If the rewrite produces an invalid type, then P is not at least as 8230 specialized as A. */ 8231 8232 /* So coerce P's args to apply to A's parms, and then deduce between A's 8233 args and the converted args. If that succeeds, A is at least as 8234 specialized as P, so they match.*/ 8235 processing_template_decl_sentinel ptds (/*reset*/false); 8236 ++processing_template_decl; 8237 8238 tree pargs = template_parms_level_to_args (parm_parms); 8239 8240 /* PARM and ARG might be at different template depths, and we want to 8241 pass the right additional levels of args when coercing PARGS to 8242 ARG_PARMS in case we need to do any substitution into non-type 8243 template parameter types. 8244 8245 OUTER_ARGS are not the right outer levels in this case, as they are 8246 the args we're building up for PARM, and for the coercion we want the 8247 args for ARG. If DECL_CONTEXT isn't set for a template template 8248 parameter, we can assume that it's in the current scope. */ 8249 tree ctx = DECL_CONTEXT (arg_tmpl); 8250 if (!ctx && DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl)) 8251 ctx = current_scope (); 8252 tree scope_args = NULL_TREE; 8253 if (tree tinfo = get_template_info (ctx)) 8254 scope_args = TI_ARGS (tinfo); 8255 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl)) 8256 { 8257 int level = TEMPLATE_TYPE_LEVEL (TREE_TYPE (gen_arg_tmpl)); 8258 int scope_depth = TMPL_ARGS_DEPTH (scope_args); 8259 tree full_pargs = make_tree_vec (level + 1); 8260 8261 /* Only use as many levels from the scope as needed 8262 (excluding the level of ARG). */ 8263 for (int i = 0; i < level - 1; ++i) 8264 if (i < scope_depth) 8265 TREE_VEC_ELT (full_pargs, i) = TMPL_ARGS_LEVEL (scope_args, i + 1); 8266 else 8267 TREE_VEC_ELT (full_pargs, i) = make_tree_vec (0); 8268 8269 /* Add the arguments that appear at the levels of ARG. */ 8270 tree adjacent = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (arg_tmpl)); 8271 adjacent = TMPL_ARGS_LEVEL (adjacent, TMPL_ARGS_DEPTH (adjacent) - 1); 8272 TREE_VEC_ELT (full_pargs, level - 1) = adjacent; 8273 8274 TREE_VEC_ELT (full_pargs, level) = pargs; 8275 pargs = full_pargs; 8276 } 8277 else 8278 pargs = add_to_template_args (scope_args, pargs); 8279 8280 pargs = coerce_template_parms (gen_arg_parms, pargs, 8281 NULL_TREE, tf_none); 8282 if (pargs != error_mark_node) 8283 { 8284 tree targs = make_tree_vec (nargs); 8285 tree aargs = template_parms_level_to_args (arg_parms); 8286 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE, 8287 /*explain*/false)) 8288 return 1; 8289 } 8290 } 8291 8292 /* Determine whether we have a parameter pack at the end of the 8293 template template parameter's template parameter list. */ 8294 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node) 8295 { 8296 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1)); 8297 8298 if (error_operand_p (parm)) 8299 return 0; 8300 8301 switch (TREE_CODE (parm)) 8302 { 8303 case TEMPLATE_DECL: 8304 case TYPE_DECL: 8305 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))) 8306 variadic_p = 1; 8307 break; 8308 8309 case PARM_DECL: 8310 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))) 8311 variadic_p = 1; 8312 break; 8313 8314 default: 8315 gcc_unreachable (); 8316 } 8317 } 8318 8319 if (nargs != nparms 8320 && !(variadic_p && nargs >= nparms - 1)) 8321 return 0; 8322 8323 /* Check all of the template parameters except the parameter pack at 8324 the end (if any). */ 8325 for (i = 0; i < nparms - variadic_p; ++i) 8326 { 8327 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node 8328 || TREE_VEC_ELT (arg_parms, i) == error_mark_node) 8329 continue; 8330 8331 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); 8332 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); 8333 8334 if (!coerce_template_template_parm (parm, arg, complain, in_decl, 8335 outer_args)) 8336 return 0; 8337 8338 } 8339 8340 if (variadic_p) 8341 { 8342 /* Check each of the template parameters in the template 8343 argument against the template parameter pack at the end of 8344 the template template parameter. */ 8345 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node) 8346 return 0; 8347 8348 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); 8349 8350 for (; i < nargs; ++i) 8351 { 8352 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node) 8353 continue; 8354 8355 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); 8356 8357 if (!coerce_template_template_parm (parm, arg, complain, in_decl, 8358 outer_args)) 8359 return 0; 8360 } 8361 } 8362 8363 return 1; 8364 } 8365 8366 /* Verifies that the deduced template arguments (in TARGS) for the 8367 template template parameters (in TPARMS) represent valid bindings, 8368 by comparing the template parameter list of each template argument 8369 to the template parameter list of its corresponding template 8370 template parameter, in accordance with DR150. This 8371 routine can only be called after all template arguments have been 8372 deduced. It will return TRUE if all of the template template 8373 parameter bindings are okay, FALSE otherwise. */ 8374 bool 8375 template_template_parm_bindings_ok_p (tree tparms, tree targs) 8376 { 8377 int i, ntparms = TREE_VEC_LENGTH (tparms); 8378 bool ret = true; 8379 8380 /* We're dealing with template parms in this process. */ 8381 ++processing_template_decl; 8382 8383 targs = INNERMOST_TEMPLATE_ARGS (targs); 8384 8385 for (i = 0; i < ntparms; ++i) 8386 { 8387 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); 8388 tree targ = TREE_VEC_ELT (targs, i); 8389 8390 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ) 8391 { 8392 tree packed_args = NULL_TREE; 8393 int idx, len = 1; 8394 8395 if (ARGUMENT_PACK_P (targ)) 8396 { 8397 /* Look inside the argument pack. */ 8398 packed_args = ARGUMENT_PACK_ARGS (targ); 8399 len = TREE_VEC_LENGTH (packed_args); 8400 } 8401 8402 for (idx = 0; idx < len; ++idx) 8403 { 8404 if (packed_args) 8405 /* Extract the next argument from the argument 8406 pack. */ 8407 targ = TREE_VEC_ELT (packed_args, idx); 8408 8409 if (PACK_EXPANSION_P (targ)) 8410 /* Look at the pattern of the pack expansion. */ 8411 targ = PACK_EXPANSION_PATTERN (targ); 8412 8413 /* Extract the template parameters from the template 8414 argument. */ 8415 if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM) 8416 targ = TYPE_NAME (targ); 8417 8418 /* Verify that we can coerce the template template 8419 parameters from the template argument to the template 8420 parameter. This requires an exact match. */ 8421 if (TREE_CODE (targ) == TEMPLATE_DECL 8422 && !coerce_template_template_parms 8423 (tparm, 8424 targ, 8425 tf_none, 8426 tparm, 8427 targs)) 8428 { 8429 ret = false; 8430 goto out; 8431 } 8432 } 8433 } 8434 } 8435 8436 out: 8437 8438 --processing_template_decl; 8439 return ret; 8440 } 8441 8442 /* Since type attributes aren't mangled, we need to strip them from 8443 template type arguments. */ 8444 8445 tree 8446 canonicalize_type_argument (tree arg, tsubst_flags_t complain) 8447 { 8448 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg)) 8449 return arg; 8450 bool removed_attributes = false; 8451 tree canon = strip_typedefs (arg, &removed_attributes); 8452 if (removed_attributes 8453 && (complain & tf_warning)) 8454 warning (OPT_Wignored_attributes, 8455 "ignoring attributes on template argument %qT", arg); 8456 return canon; 8457 } 8458 8459 /* And from inside dependent non-type arguments like sizeof(Type). */ 8460 8461 static tree 8462 canonicalize_expr_argument (tree arg, tsubst_flags_t complain) 8463 { 8464 if (!arg || arg == error_mark_node) 8465 return arg; 8466 bool removed_attributes = false; 8467 tree canon = strip_typedefs_expr (arg, &removed_attributes); 8468 if (removed_attributes 8469 && (complain & tf_warning)) 8470 warning (OPT_Wignored_attributes, 8471 "ignoring attributes in template argument %qE", arg); 8472 return canon; 8473 } 8474 8475 /* A template declaration can be substituted for a constrained 8476 template template parameter only when the argument is no more 8477 constrained than the parameter. */ 8478 8479 static bool 8480 is_compatible_template_arg (tree parm, tree arg, tree args) 8481 { 8482 tree parm_cons = get_constraints (parm); 8483 8484 /* For now, allow constrained template template arguments 8485 and unconstrained template template parameters. */ 8486 if (parm_cons == NULL_TREE) 8487 return true; 8488 8489 /* If the template parameter is constrained, we need to rewrite its 8490 constraints in terms of the ARG's template parameters. This ensures 8491 that all of the template parameter types will have the same depth. 8492 8493 Note that this is only valid when coerce_template_template_parm is 8494 true for the innermost template parameters of PARM and ARG. In other 8495 words, because coercion is successful, this conversion will be valid. */ 8496 tree new_args = NULL_TREE; 8497 if (parm_cons) 8498 { 8499 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg); 8500 new_args = template_parms_level_to_args (aparms); 8501 new_args = add_to_template_args (args, new_args); 8502 ++processing_template_decl; 8503 parm_cons = tsubst_constraint_info (parm_cons, new_args, 8504 tf_none, NULL_TREE); 8505 --processing_template_decl; 8506 if (parm_cons == error_mark_node) 8507 return false; 8508 } 8509 8510 return ttp_subsumes (parm_cons, arg); 8511 } 8512 8513 // Convert a placeholder argument into a binding to the original 8514 // parameter. The original parameter is saved as the TREE_TYPE of 8515 // ARG. 8516 static inline tree 8517 convert_wildcard_argument (tree parm, tree arg) 8518 { 8519 TREE_TYPE (arg) = parm; 8520 return arg; 8521 } 8522 8523 /* We can't fully resolve ARG given as a non-type template argument to TYPE, 8524 because one of them is dependent. But we need to represent the 8525 conversion for the benefit of cp_tree_equal. */ 8526 8527 static tree 8528 maybe_convert_nontype_argument (tree type, tree arg, bool force) 8529 { 8530 /* Auto parms get no conversion. */ 8531 if (type_uses_auto (type)) 8532 return arg; 8533 /* ??? Do we need to push the IMPLICIT_CONV_EXPR into the pack expansion? 8534 That would complicate other things, and it doesn't seem necessary. */ 8535 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) 8536 return arg; 8537 /* We don't need or want to add this conversion now if we're going to use the 8538 argument for deduction. */ 8539 if (!value_dependent_expression_p (arg)) 8540 force = false; 8541 else if (!force) 8542 return arg; 8543 8544 type = cv_unqualified (type); 8545 tree argtype = TREE_TYPE (arg); 8546 if (argtype && same_type_p (type, argtype)) 8547 return arg; 8548 8549 arg = build1 (IMPLICIT_CONV_EXPR, type, arg); 8550 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true; 8551 IMPLICIT_CONV_EXPR_FORCED (arg) = force; 8552 return arg; 8553 } 8554 8555 /* Convert the indicated template ARG as necessary to match the 8556 indicated template PARM. Returns the converted ARG, or 8557 error_mark_node if the conversion was unsuccessful. Error and 8558 warning messages are issued under control of COMPLAIN. This 8559 conversion is for the Ith parameter in the parameter list. ARGS is 8560 the full set of template arguments deduced so far. */ 8561 8562 static tree 8563 convert_template_argument (tree parm, 8564 tree arg, 8565 tree args, 8566 tsubst_flags_t complain, 8567 int i, 8568 tree in_decl) 8569 { 8570 tree orig_arg; 8571 tree val; 8572 int is_type, requires_type, is_tmpl_type, requires_tmpl_type; 8573 8574 if (parm == error_mark_node || error_operand_p (arg)) 8575 return error_mark_node; 8576 8577 /* Trivially convert placeholders. */ 8578 if (TREE_CODE (arg) == WILDCARD_DECL) 8579 return convert_wildcard_argument (parm, arg); 8580 8581 if (arg == any_targ_node) 8582 return arg; 8583 8584 if (TREE_CODE (arg) == TREE_LIST 8585 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF) 8586 { 8587 /* The template argument was the name of some 8588 member function. That's usually 8589 invalid, but static members are OK. In any 8590 case, grab the underlying fields/functions 8591 and issue an error later if required. */ 8592 TREE_TYPE (arg) = unknown_type_node; 8593 } 8594 8595 orig_arg = arg; 8596 8597 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; 8598 requires_type = (TREE_CODE (parm) == TYPE_DECL 8599 || requires_tmpl_type); 8600 8601 /* When determining whether an argument pack expansion is a template, 8602 look at the pattern. */ 8603 if (PACK_EXPANSION_P (arg)) 8604 arg = PACK_EXPANSION_PATTERN (arg); 8605 8606 /* Deal with an injected-class-name used as a template template arg. */ 8607 if (requires_tmpl_type && CLASS_TYPE_P (arg)) 8608 { 8609 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg)); 8610 if (TREE_CODE (t) == TEMPLATE_DECL) 8611 { 8612 if (cxx_dialect >= cxx11) 8613 /* OK under DR 1004. */; 8614 else if (complain & tf_warning_or_error) 8615 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD" 8616 " used as template template argument", TYPE_NAME (arg)); 8617 else if (flag_pedantic_errors) 8618 t = arg; 8619 8620 arg = t; 8621 } 8622 } 8623 8624 is_tmpl_type = 8625 ((TREE_CODE (arg) == TEMPLATE_DECL 8626 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) 8627 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK) 8628 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM 8629 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); 8630 8631 if (is_tmpl_type 8632 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM 8633 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)) 8634 arg = TYPE_STUB_DECL (arg); 8635 8636 is_type = TYPE_P (arg) || is_tmpl_type; 8637 8638 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF 8639 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) 8640 { 8641 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR) 8642 { 8643 if (complain & tf_error) 8644 error ("invalid use of destructor %qE as a type", orig_arg); 8645 return error_mark_node; 8646 } 8647 8648 permerror (input_location, 8649 "to refer to a type member of a template parameter, " 8650 "use %<typename %E%>", orig_arg); 8651 8652 orig_arg = make_typename_type (TREE_OPERAND (arg, 0), 8653 TREE_OPERAND (arg, 1), 8654 typename_type, 8655 complain); 8656 arg = orig_arg; 8657 is_type = 1; 8658 } 8659 if (is_type != requires_type) 8660 { 8661 if (in_decl) 8662 { 8663 if (complain & tf_error) 8664 { 8665 error ("type/value mismatch at argument %d in template " 8666 "parameter list for %qD", 8667 i + 1, in_decl); 8668 if (is_type) 8669 { 8670 /* The template argument is a type, but we're expecting 8671 an expression. */ 8672 inform (input_location, 8673 " expected a constant of type %qT, got %qT", 8674 TREE_TYPE (parm), 8675 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg)); 8676 /* [temp.arg]/2: "In a template-argument, an ambiguity 8677 between a type-id and an expression is resolved to a 8678 type-id, regardless of the form of the corresponding 8679 template-parameter." So give the user a clue. */ 8680 if (TREE_CODE (arg) == FUNCTION_TYPE) 8681 inform (input_location, " ambiguous template argument " 8682 "for non-type template parameter is treated as " 8683 "function type"); 8684 } 8685 else if (requires_tmpl_type) 8686 inform (input_location, 8687 " expected a class template, got %qE", orig_arg); 8688 else 8689 inform (input_location, 8690 " expected a type, got %qE", orig_arg); 8691 } 8692 } 8693 return error_mark_node; 8694 } 8695 if (is_tmpl_type ^ requires_tmpl_type) 8696 { 8697 if (in_decl && (complain & tf_error)) 8698 { 8699 error ("type/value mismatch at argument %d in template " 8700 "parameter list for %qD", 8701 i + 1, in_decl); 8702 if (is_tmpl_type) 8703 inform (input_location, 8704 " expected a type, got %qT", DECL_NAME (arg)); 8705 else 8706 inform (input_location, 8707 " expected a class template, got %qT", orig_arg); 8708 } 8709 return error_mark_node; 8710 } 8711 8712 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg)) 8713 /* We already did the appropriate conversion when packing args. */ 8714 val = orig_arg; 8715 else if (is_type) 8716 { 8717 if (requires_tmpl_type) 8718 { 8719 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE) 8720 /* The number of argument required is not known yet. 8721 Just accept it for now. */ 8722 val = orig_arg; 8723 else 8724 { 8725 /* Strip alias templates that are equivalent to another 8726 template. */ 8727 arg = get_underlying_template (arg); 8728 8729 if (coerce_template_template_parms (parm, arg, 8730 complain, in_decl, 8731 args)) 8732 { 8733 val = arg; 8734 8735 /* TEMPLATE_TEMPLATE_PARM node is preferred over 8736 TEMPLATE_DECL. */ 8737 if (val != error_mark_node) 8738 { 8739 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val)) 8740 val = TREE_TYPE (val); 8741 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION) 8742 val = make_pack_expansion (val, complain); 8743 } 8744 } 8745 else 8746 { 8747 if (in_decl && (complain & tf_error)) 8748 { 8749 error ("type/value mismatch at argument %d in " 8750 "template parameter list for %qD", 8751 i + 1, in_decl); 8752 inform (input_location, 8753 " expected a template of type %qD, got %qT", 8754 parm, orig_arg); 8755 } 8756 8757 val = error_mark_node; 8758 } 8759 8760 // Check that the constraints are compatible before allowing the 8761 // substitution. 8762 if (val != error_mark_node) 8763 if (!is_compatible_template_arg (parm, arg, args)) 8764 { 8765 if (in_decl && (complain & tf_error)) 8766 { 8767 error ("constraint mismatch at argument %d in " 8768 "template parameter list for %qD", 8769 i + 1, in_decl); 8770 inform (input_location, " expected %qD but got %qD", 8771 parm, arg); 8772 } 8773 val = error_mark_node; 8774 } 8775 } 8776 } 8777 else 8778 val = orig_arg; 8779 /* We only form one instance of each template specialization. 8780 Therefore, if we use a non-canonical variant (i.e., a 8781 typedef), any future messages referring to the type will use 8782 the typedef, which is confusing if those future uses do not 8783 themselves also use the typedef. */ 8784 if (TYPE_P (val)) 8785 val = canonicalize_type_argument (val, complain); 8786 } 8787 else 8788 { 8789 tree t = TREE_TYPE (parm); 8790 8791 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm)) 8792 > TMPL_ARGS_DEPTH (args)) 8793 /* We don't have enough levels of args to do any substitution. This 8794 can happen in the context of -fnew-ttp-matching. */; 8795 else if (tree a = type_uses_auto (t)) 8796 { 8797 t = do_auto_deduction (t, arg, a, complain, adc_unify, args, 8798 LOOKUP_IMPLICIT, /*tmpl=*/in_decl); 8799 if (t == error_mark_node) 8800 return error_mark_node; 8801 } 8802 else 8803 t = tsubst (t, args, complain, in_decl); 8804 8805 /* Perform array-to-pointer and function-to-pointer conversion 8806 as per [temp.param]/10. */ 8807 t = type_decays_to (t); 8808 8809 if (invalid_nontype_parm_type_p (t, complain)) 8810 return error_mark_node; 8811 8812 /* Drop top-level cv-qualifiers on the substituted/deduced type of 8813 this non-type template parameter, as per [temp.param]/6. */ 8814 t = cv_unqualified (t); 8815 8816 if (t != TREE_TYPE (parm)) 8817 t = canonicalize_type_argument (t, complain); 8818 8819 /* We need to handle arguments for alias or concept templates 8820 differently: we need to force building an IMPLICIT_CONV_EXPR, because 8821 these arguments are going to be substituted directly into the 8822 dependent type; they might not get another chance at 8823 convert_nontype_argument. But if the argument ends up here again for 8824 a template that isn't one of those, remove the conversion for 8825 consistency between naming the same dependent type directly or through 8826 an alias. */ 8827 bool force_conv = in_decl && (DECL_ALIAS_TEMPLATE_P (in_decl) 8828 || concept_definition_p (in_decl)); 8829 if (!force_conv 8830 && TREE_CODE (orig_arg) == IMPLICIT_CONV_EXPR 8831 && IMPLICIT_CONV_EXPR_FORCED (orig_arg) 8832 && same_type_p (TREE_TYPE (orig_arg), t)) 8833 orig_arg = TREE_OPERAND (orig_arg, 0); 8834 8835 if (!type_dependent_expression_p (orig_arg) 8836 && !uses_template_parms (t)) 8837 /* We used to call digest_init here. However, digest_init 8838 will report errors, which we don't want when complain 8839 is zero. More importantly, digest_init will try too 8840 hard to convert things: for example, `0' should not be 8841 converted to pointer type at this point according to 8842 the standard. Accepting this is not merely an 8843 extension, since deciding whether or not these 8844 conversions can occur is part of determining which 8845 function template to call, or whether a given explicit 8846 argument specification is valid. */ 8847 val = convert_nontype_argument (t, orig_arg, complain); 8848 else 8849 { 8850 val = canonicalize_expr_argument (orig_arg, complain); 8851 val = maybe_convert_nontype_argument (t, val, force_conv); 8852 } 8853 8854 if (val == NULL_TREE) 8855 val = error_mark_node; 8856 else if (val == error_mark_node && (complain & tf_error)) 8857 error_at (cp_expr_loc_or_input_loc (orig_arg), 8858 "could not convert template argument %qE from %qT to %qT", 8859 orig_arg, TREE_TYPE (orig_arg), t); 8860 8861 if (INDIRECT_REF_P (val)) 8862 { 8863 /* Reject template arguments that are references to built-in 8864 functions with no library fallbacks. */ 8865 const_tree inner = TREE_OPERAND (val, 0); 8866 const_tree innertype = TREE_TYPE (inner); 8867 if (innertype 8868 && TYPE_REF_P (innertype) 8869 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE 8870 && TREE_OPERAND_LENGTH (inner) > 0 8871 && reject_gcc_builtin (TREE_OPERAND (inner, 0))) 8872 return error_mark_node; 8873 } 8874 8875 if (TREE_CODE (val) == SCOPE_REF) 8876 { 8877 /* Strip typedefs from the SCOPE_REF. */ 8878 tree type = canonicalize_type_argument (TREE_TYPE (val), complain); 8879 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0), 8880 complain); 8881 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1), 8882 QUALIFIED_NAME_IS_TEMPLATE (val)); 8883 } 8884 } 8885 8886 return val; 8887 } 8888 8889 /* Coerces the remaining template arguments in INNER_ARGS (from 8890 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS. 8891 Returns the coerced argument pack. PARM_IDX is the position of this 8892 parameter in the template parameter list. ARGS is the original 8893 template argument list. */ 8894 static tree 8895 coerce_template_parameter_pack (tree parms, 8896 int parm_idx, 8897 tree args, 8898 tree inner_args, 8899 int arg_idx, 8900 tree new_args, 8901 int* lost, 8902 tree in_decl, 8903 tsubst_flags_t complain) 8904 { 8905 tree parm = TREE_VEC_ELT (parms, parm_idx); 8906 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; 8907 tree packed_args; 8908 tree argument_pack; 8909 tree packed_parms = NULL_TREE; 8910 8911 if (arg_idx > nargs) 8912 arg_idx = nargs; 8913 8914 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm))) 8915 { 8916 /* When the template parameter is a non-type template parameter pack 8917 or template template parameter pack whose type or template 8918 parameters use parameter packs, we know exactly how many arguments 8919 we are looking for. Build a vector of the instantiated decls for 8920 these template parameters in PACKED_PARMS. */ 8921 /* We can't use make_pack_expansion here because it would interpret a 8922 _DECL as a use rather than a declaration. */ 8923 tree decl = TREE_VALUE (parm); 8924 tree exp = cxx_make_type (TYPE_PACK_EXPANSION); 8925 PACK_EXPANSION_PATTERN (exp) = decl; 8926 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs; 8927 SET_TYPE_STRUCTURAL_EQUALITY (exp); 8928 8929 TREE_VEC_LENGTH (args)--; 8930 packed_parms = tsubst_pack_expansion (exp, args, complain, decl); 8931 TREE_VEC_LENGTH (args)++; 8932 8933 if (packed_parms == error_mark_node) 8934 return error_mark_node; 8935 8936 /* If we're doing a partial instantiation of a member template, 8937 verify that all of the types used for the non-type 8938 template parameter pack are, in fact, valid for non-type 8939 template parameters. */ 8940 if (arg_idx < nargs 8941 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))) 8942 { 8943 int j, len = TREE_VEC_LENGTH (packed_parms); 8944 for (j = 0; j < len; ++j) 8945 { 8946 tree t = TREE_VEC_ELT (packed_parms, j); 8947 if (TREE_CODE (t) == PARM_DECL 8948 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain)) 8949 return error_mark_node; 8950 } 8951 /* We don't know how many args we have yet, just 8952 use the unconverted ones for now. */ 8953 return NULL_TREE; 8954 } 8955 8956 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms)); 8957 } 8958 /* Check if we have a placeholder pack, which indicates we're 8959 in the context of a introduction list. In that case we want 8960 to match this pack to the single placeholder. */ 8961 else if (arg_idx < nargs 8962 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL 8963 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx))) 8964 { 8965 nargs = arg_idx + 1; 8966 packed_args = make_tree_vec (1); 8967 } 8968 else 8969 packed_args = make_tree_vec (nargs - arg_idx); 8970 8971 /* Convert the remaining arguments, which will be a part of the 8972 parameter pack "parm". */ 8973 int first_pack_arg = arg_idx; 8974 for (; arg_idx < nargs; ++arg_idx) 8975 { 8976 tree arg = TREE_VEC_ELT (inner_args, arg_idx); 8977 tree actual_parm = TREE_VALUE (parm); 8978 int pack_idx = arg_idx - first_pack_arg; 8979 8980 if (packed_parms) 8981 { 8982 /* Once we've packed as many args as we have types, stop. */ 8983 if (pack_idx >= TREE_VEC_LENGTH (packed_parms)) 8984 break; 8985 else if (PACK_EXPANSION_P (arg)) 8986 /* We don't know how many args we have yet, just 8987 use the unconverted ones for now. */ 8988 return NULL_TREE; 8989 else 8990 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx); 8991 } 8992 8993 if (arg == error_mark_node) 8994 { 8995 if (complain & tf_error) 8996 error ("template argument %d is invalid", arg_idx + 1); 8997 } 8998 else 8999 arg = convert_template_argument (actual_parm, 9000 arg, new_args, complain, parm_idx, 9001 in_decl); 9002 if (arg == error_mark_node) 9003 (*lost)++; 9004 TREE_VEC_ELT (packed_args, pack_idx) = arg; 9005 } 9006 9007 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args) 9008 && TREE_VEC_LENGTH (packed_args) > 0) 9009 { 9010 if (complain & tf_error) 9011 error ("wrong number of template arguments (%d, should be %d)", 9012 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args)); 9013 return error_mark_node; 9014 } 9015 9016 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL 9017 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL) 9018 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK); 9019 else 9020 { 9021 argument_pack = make_node (NONTYPE_ARGUMENT_PACK); 9022 TREE_CONSTANT (argument_pack) = 1; 9023 } 9024 9025 ARGUMENT_PACK_ARGS (argument_pack) = packed_args; 9026 if (CHECKING_P) 9027 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args, 9028 TREE_VEC_LENGTH (packed_args)); 9029 return argument_pack; 9030 } 9031 9032 /* Returns the number of pack expansions in the template argument vector 9033 ARGS. */ 9034 9035 static int 9036 pack_expansion_args_count (tree args) 9037 { 9038 int i; 9039 int count = 0; 9040 if (args) 9041 for (i = 0; i < TREE_VEC_LENGTH (args); ++i) 9042 { 9043 tree elt = TREE_VEC_ELT (args, i); 9044 if (elt && PACK_EXPANSION_P (elt)) 9045 ++count; 9046 } 9047 return count; 9048 } 9049 9050 /* Convert all template arguments to their appropriate types, and 9051 return a vector containing the innermost resulting template 9052 arguments. If any error occurs, return error_mark_node. Error and 9053 warning messages are issued under control of COMPLAIN. 9054 9055 If PARMS represents all template parameters levels, this function 9056 returns a vector of vectors representing all the resulting argument 9057 levels. Note that in this case, only the innermost arguments are 9058 coerced because the outermost ones are supposed to have been coerced 9059 already. Otherwise, if PARMS represents only (the innermost) vector 9060 of parameters, this function returns a vector containing just the 9061 innermost resulting arguments. 9062 9063 If REQUIRE_ALL_ARGS is false, argument deduction will be performed 9064 for arguments not specified in ARGS. If REQUIRE_ALL_ARGS is true, 9065 arguments not specified in ARGS must have default arguments which 9066 we'll use to fill in ARGS. */ 9067 9068 tree 9069 coerce_template_parms (tree parms, 9070 tree args, 9071 tree in_decl, 9072 tsubst_flags_t complain, 9073 bool require_all_args /* = true */) 9074 { 9075 int nparms, nargs, parm_idx, arg_idx, lost = 0; 9076 tree orig_inner_args; 9077 tree inner_args; 9078 9079 /* When used as a boolean value, indicates whether this is a 9080 variadic template parameter list. Since it's an int, we can also 9081 subtract it from nparms to get the number of non-variadic 9082 parameters. */ 9083 int variadic_p = 0; 9084 int variadic_args_p = 0; 9085 int post_variadic_parms = 0; 9086 9087 /* Adjustment to nparms for fixed parameter packs. */ 9088 int fixed_pack_adjust = 0; 9089 int fixed_packs = 0; 9090 int missing = 0; 9091 9092 /* Likewise for parameters with default arguments. */ 9093 int default_p = 0; 9094 9095 if (args == error_mark_node) 9096 return error_mark_node; 9097 9098 bool return_full_args = false; 9099 if (TREE_CODE (parms) == TREE_LIST) 9100 { 9101 if (TMPL_PARMS_DEPTH (parms) > 1) 9102 { 9103 gcc_assert (TMPL_PARMS_DEPTH (parms) == TMPL_ARGS_DEPTH (args)); 9104 return_full_args = true; 9105 } 9106 parms = INNERMOST_TEMPLATE_PARMS (parms); 9107 } 9108 9109 nparms = TREE_VEC_LENGTH (parms); 9110 9111 /* Determine if there are any parameter packs or default arguments. */ 9112 for (parm_idx = 0; parm_idx < nparms; ++parm_idx) 9113 { 9114 tree parm = TREE_VEC_ELT (parms, parm_idx); 9115 if (variadic_p) 9116 ++post_variadic_parms; 9117 if (template_parameter_pack_p (TREE_VALUE (parm))) 9118 ++variadic_p; 9119 if (TREE_PURPOSE (parm)) 9120 ++default_p; 9121 } 9122 9123 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args); 9124 /* If there are no parameters that follow a parameter pack, we need to 9125 expand any argument packs so that we can deduce a parameter pack from 9126 some non-packed args followed by an argument pack, as in variadic85.C. 9127 If there are such parameters, we need to leave argument packs intact 9128 so the arguments are assigned properly. This can happen when dealing 9129 with a nested class inside a partial specialization of a class 9130 template, as in variadic92.C, or when deducing a template parameter pack 9131 from a sub-declarator, as in variadic114.C. */ 9132 if (!post_variadic_parms) 9133 inner_args = expand_template_argument_pack (inner_args); 9134 9135 /* Count any pack expansion args. */ 9136 variadic_args_p = pack_expansion_args_count (inner_args); 9137 9138 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; 9139 if ((nargs - variadic_args_p > nparms && !variadic_p) 9140 || (nargs < nparms - variadic_p 9141 && require_all_args 9142 && !variadic_args_p 9143 && (TREE_VEC_ELT (parms, nargs) != error_mark_node 9144 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))) 9145 { 9146 bad_nargs: 9147 if (complain & tf_error) 9148 { 9149 if (variadic_p || default_p) 9150 { 9151 nparms -= variadic_p + default_p; 9152 error ("wrong number of template arguments " 9153 "(%d, should be at least %d)", nargs, nparms); 9154 } 9155 else 9156 error ("wrong number of template arguments " 9157 "(%d, should be %d)", nargs, nparms); 9158 9159 if (in_decl) 9160 inform (DECL_SOURCE_LOCATION (in_decl), 9161 "provided for %qD", in_decl); 9162 } 9163 9164 return error_mark_node; 9165 } 9166 /* We can't pass a pack expansion to a non-pack parameter of an alias 9167 template (DR 1430). */ 9168 else if (in_decl 9169 && (DECL_ALIAS_TEMPLATE_P (in_decl) 9170 || concept_definition_p (in_decl)) 9171 && variadic_args_p 9172 && nargs - variadic_args_p < nparms - variadic_p) 9173 { 9174 if (complain & tf_error) 9175 { 9176 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i) 9177 { 9178 tree arg = TREE_VEC_ELT (inner_args, i); 9179 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); 9180 9181 if (PACK_EXPANSION_P (arg) 9182 && !template_parameter_pack_p (parm)) 9183 { 9184 if (DECL_ALIAS_TEMPLATE_P (in_decl)) 9185 error_at (location_of (arg), 9186 "pack expansion argument for non-pack parameter " 9187 "%qD of alias template %qD", parm, in_decl); 9188 else 9189 error_at (location_of (arg), 9190 "pack expansion argument for non-pack parameter " 9191 "%qD of concept %qD", parm, in_decl); 9192 inform (DECL_SOURCE_LOCATION (parm), "declared here"); 9193 goto found; 9194 } 9195 } 9196 gcc_unreachable (); 9197 found:; 9198 } 9199 return error_mark_node; 9200 } 9201 9202 /* We need to evaluate the template arguments, even though this 9203 template-id may be nested within a "sizeof". */ 9204 cp_evaluated ev; 9205 9206 tree new_args = add_outermost_template_args (args, make_tree_vec (nparms)); 9207 tree& new_inner_args = TMPL_ARGS_LEVEL (new_args, TMPL_ARGS_DEPTH (new_args)); 9208 int pack_adjust = 0; 9209 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++) 9210 { 9211 tree arg; 9212 tree parm; 9213 9214 /* Get the Ith template parameter. */ 9215 parm = TREE_VEC_ELT (parms, parm_idx); 9216 9217 if (parm == error_mark_node) 9218 { 9219 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node; 9220 continue; 9221 } 9222 9223 /* Calculate the next argument. */ 9224 if (arg_idx < nargs) 9225 arg = TREE_VEC_ELT (inner_args, arg_idx); 9226 else 9227 arg = NULL_TREE; 9228 9229 if (template_parameter_pack_p (TREE_VALUE (parm)) 9230 && (arg || require_all_args || !(complain & tf_partial)) 9231 && !(arg && ARGUMENT_PACK_P (arg))) 9232 { 9233 /* Some arguments will be placed in the 9234 template parameter pack PARM. */ 9235 arg = coerce_template_parameter_pack (parms, parm_idx, args, 9236 inner_args, arg_idx, 9237 new_args, &lost, 9238 in_decl, complain); 9239 9240 if (arg == NULL_TREE) 9241 { 9242 /* We don't know how many args we have yet, just use the 9243 unconverted (and still packed) ones for now. */ 9244 new_inner_args = orig_inner_args; 9245 arg_idx = nargs; 9246 break; 9247 } 9248 9249 TREE_VEC_ELT (new_inner_args, parm_idx) = arg; 9250 9251 /* Store this argument. */ 9252 if (arg == error_mark_node) 9253 { 9254 lost++; 9255 /* We are done with all of the arguments. */ 9256 arg_idx = nargs; 9257 break; 9258 } 9259 else 9260 { 9261 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1; 9262 arg_idx += pack_adjust; 9263 if (fixed_parameter_pack_p (TREE_VALUE (parm))) 9264 { 9265 ++fixed_packs; 9266 fixed_pack_adjust += pack_adjust; 9267 } 9268 } 9269 9270 continue; 9271 } 9272 else if (arg) 9273 { 9274 if (PACK_EXPANSION_P (arg)) 9275 { 9276 /* "If every valid specialization of a variadic template 9277 requires an empty template parameter pack, the template is 9278 ill-formed, no diagnostic required." So check that the 9279 pattern works with this parameter. */ 9280 tree pattern = PACK_EXPANSION_PATTERN (arg); 9281 tree conv = convert_template_argument (TREE_VALUE (parm), 9282 pattern, new_args, 9283 complain, parm_idx, 9284 in_decl); 9285 if (conv == error_mark_node) 9286 { 9287 if (complain & tf_error) 9288 inform (input_location, "so any instantiation with a " 9289 "non-empty parameter pack would be ill-formed"); 9290 ++lost; 9291 } 9292 else if (TYPE_P (conv) && !TYPE_P (pattern)) 9293 /* Recover from missing typename. */ 9294 TREE_VEC_ELT (inner_args, arg_idx) 9295 = make_pack_expansion (conv, complain); 9296 9297 /* We don't know how many args we have yet, just 9298 use the unconverted ones for now. */ 9299 new_inner_args = inner_args; 9300 arg_idx = nargs; 9301 break; 9302 } 9303 } 9304 else if (require_all_args) 9305 { 9306 /* There must be a default arg in this case. */ 9307 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args, 9308 complain, in_decl); 9309 /* The position of the first default template argument, 9310 is also the number of non-defaulted arguments in NEW_INNER_ARGS. 9311 Record that. */ 9312 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args)) 9313 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, 9314 arg_idx - pack_adjust); 9315 } 9316 else 9317 break; 9318 9319 if (arg == error_mark_node) 9320 { 9321 if (complain & tf_error) 9322 error ("template argument %d is invalid", arg_idx + 1); 9323 } 9324 else if (!arg) 9325 { 9326 /* This can occur if there was an error in the template 9327 parameter list itself (which we would already have 9328 reported) that we are trying to recover from, e.g., a class 9329 template with a parameter list such as 9330 template<typename..., typename> (cpp0x/variadic150.C). */ 9331 ++lost; 9332 9333 /* This can also happen with a fixed parameter pack (71834). */ 9334 if (arg_idx >= nargs) 9335 ++missing; 9336 } 9337 else 9338 arg = convert_template_argument (TREE_VALUE (parm), 9339 arg, new_args, complain, 9340 parm_idx, in_decl); 9341 9342 if (arg == error_mark_node) 9343 lost++; 9344 9345 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg; 9346 } 9347 9348 if (missing || arg_idx < nargs - variadic_args_p) 9349 { 9350 /* If we had fixed parameter packs, we didn't know how many arguments we 9351 actually needed earlier; now we do. */ 9352 nparms += fixed_pack_adjust; 9353 variadic_p -= fixed_packs; 9354 goto bad_nargs; 9355 } 9356 9357 if (arg_idx < nargs) 9358 { 9359 /* We had some pack expansion arguments that will only work if the packs 9360 are empty, but wait until instantiation time to complain. 9361 See variadic-ttp3.C. */ 9362 9363 /* Except that we can't provide empty packs to alias templates or 9364 concepts when there are no corresponding parameters. Basically, 9365 we can get here with this: 9366 9367 template<typename T> concept C = true; 9368 9369 template<typename... Args> 9370 requires C<Args...> 9371 void f(); 9372 9373 When parsing C<Args...>, we try to form a concept check of 9374 C<?, Args...>. Without the extra check for substituting an empty 9375 pack past the last parameter, we can accept the check as valid. 9376 9377 FIXME: This may be valid for alias templates (but I doubt it). 9378 9379 FIXME: The error could be better also. */ 9380 if (in_decl && concept_definition_p (in_decl)) 9381 { 9382 if (complain & tf_error) 9383 error_at (location_of (TREE_VEC_ELT (args, arg_idx)), 9384 "too many arguments"); 9385 return error_mark_node; 9386 } 9387 9388 int len = nparms + (nargs - arg_idx); 9389 tree args = make_tree_vec (len); 9390 int i = 0; 9391 for (; i < nparms; ++i) 9392 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i); 9393 for (; i < len; ++i, ++arg_idx) 9394 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args, 9395 arg_idx - pack_adjust); 9396 new_inner_args = args; 9397 } 9398 9399 if (lost) 9400 { 9401 gcc_assert (!(complain & tf_error) || seen_error ()); 9402 return error_mark_node; 9403 } 9404 9405 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args)) 9406 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, 9407 TREE_VEC_LENGTH (new_inner_args)); 9408 9409 return return_full_args ? new_args : new_inner_args; 9410 } 9411 9412 /* Returns true if T is a wrapper to make a C++20 template parameter 9413 object const. */ 9414 9415 static bool 9416 class_nttp_const_wrapper_p (tree t) 9417 { 9418 if (cxx_dialect < cxx20) 9419 return false; 9420 return (TREE_CODE (t) == VIEW_CONVERT_EXPR 9421 && CP_TYPE_CONST_P (TREE_TYPE (t)) 9422 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX); 9423 } 9424 9425 /* Returns 1 if template args OT and NT are equivalent. */ 9426 9427 int 9428 template_args_equal (tree ot, tree nt) 9429 { 9430 if (nt == ot) 9431 return 1; 9432 if (nt == NULL_TREE || ot == NULL_TREE) 9433 return false; 9434 if (nt == any_targ_node || ot == any_targ_node) 9435 return true; 9436 9437 if (class_nttp_const_wrapper_p (nt)) 9438 nt = TREE_OPERAND (nt, 0); 9439 if (class_nttp_const_wrapper_p (ot)) 9440 ot = TREE_OPERAND (ot, 0); 9441 9442 /* DR 1558: Don't treat an alias template specialization with dependent 9443 arguments as equivalent to its underlying type when used as a template 9444 argument; we need them to be distinct so that we substitute into the 9445 specialization arguments at instantiation time. And aliases can't be 9446 equivalent without being ==, so we don't need to look any deeper. 9447 9448 During partial ordering, however, we need to treat them normally so we can 9449 order uses of the same alias with different cv-qualification (79960). */ 9450 auto cso = make_temp_override (comparing_dependent_aliases); 9451 if (!comparing_for_partial_ordering) 9452 ++comparing_dependent_aliases; 9453 9454 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC) 9455 /* For member templates */ 9456 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt); 9457 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt)) 9458 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt) 9459 && template_args_equal (PACK_EXPANSION_PATTERN (ot), 9460 PACK_EXPANSION_PATTERN (nt)) 9461 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot), 9462 PACK_EXPANSION_EXTRA_ARGS (nt))); 9463 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt)) 9464 return cp_tree_equal (ot, nt); 9465 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT) 9466 gcc_unreachable (); 9467 else if (TYPE_P (nt) || TYPE_P (ot)) 9468 { 9469 if (!(TYPE_P (nt) && TYPE_P (ot))) 9470 return false; 9471 return same_type_p (ot, nt); 9472 } 9473 else 9474 { 9475 /* Try to treat a template non-type argument that has been converted 9476 to the parameter type as equivalent to one that hasn't yet. */ 9477 for (enum tree_code code1 = TREE_CODE (ot); 9478 CONVERT_EXPR_CODE_P (code1) 9479 || code1 == NON_LVALUE_EXPR; 9480 code1 = TREE_CODE (ot)) 9481 ot = TREE_OPERAND (ot, 0); 9482 9483 for (enum tree_code code2 = TREE_CODE (nt); 9484 CONVERT_EXPR_CODE_P (code2) 9485 || code2 == NON_LVALUE_EXPR; 9486 code2 = TREE_CODE (nt)) 9487 nt = TREE_OPERAND (nt, 0); 9488 9489 return cp_tree_equal (ot, nt); 9490 } 9491 } 9492 9493 /* Returns true iff the OLDARGS and NEWARGS are in fact identical sets of 9494 template arguments. Returns false otherwise, and updates OLDARG_PTR and 9495 NEWARG_PTR with the offending arguments if they are non-NULL. */ 9496 9497 bool 9498 comp_template_args (tree oldargs, tree newargs, 9499 tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */) 9500 { 9501 if (oldargs == newargs) 9502 return true; 9503 9504 if (!oldargs || !newargs) 9505 return false; 9506 9507 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) 9508 return false; 9509 9510 for (int i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) 9511 { 9512 tree nt = TREE_VEC_ELT (newargs, i); 9513 tree ot = TREE_VEC_ELT (oldargs, i); 9514 9515 if (! template_args_equal (ot, nt)) 9516 { 9517 if (oldarg_ptr != NULL) 9518 *oldarg_ptr = ot; 9519 if (newarg_ptr != NULL) 9520 *newarg_ptr = nt; 9521 return false; 9522 } 9523 } 9524 return true; 9525 } 9526 9527 static bool 9528 comp_template_args_porder (tree oargs, tree nargs) 9529 { 9530 ++comparing_for_partial_ordering; 9531 bool equal = comp_template_args (oargs, nargs); 9532 --comparing_for_partial_ordering; 9533 return equal; 9534 } 9535 9536 /* Implement a freelist interface for objects of type T. 9537 9538 Head is a separate object, rather than a regular member, so that we 9539 can define it as a GTY deletable pointer, which is highly 9540 desirable. A data member could be declared that way, but then the 9541 containing object would implicitly get GTY((user)), which would 9542 prevent us from instantiating freelists as global objects. 9543 Although this way we can create freelist global objects, they're 9544 such thin wrappers that instantiating temporaries at every use 9545 loses nothing and saves permanent storage for the freelist object. 9546 9547 Member functions next, anew, poison and reinit have default 9548 implementations that work for most of the types we're interested 9549 in, but if they don't work for some type, they should be explicitly 9550 specialized. See the comments before them for requirements, and 9551 the example specializations for the tree_list_freelist. */ 9552 template <typename T> 9553 class freelist 9554 { 9555 /* Return the next object in a chain. We could just do type 9556 punning, but if we access the object with its underlying type, we 9557 avoid strict-aliasing trouble. This needs only work between 9558 poison and reinit. */ 9559 static T *&next (T *obj) { return obj->next; } 9560 9561 /* Return a newly allocated, uninitialized or minimally-initialized 9562 object of type T. Any initialization performed by anew should 9563 either remain across the life of the object and the execution of 9564 poison, or be redone by reinit. */ 9565 static T *anew () { return ggc_alloc<T> (); } 9566 9567 /* Optionally scribble all over the bits holding the object, so that 9568 they become (mostly?) uninitialized memory. This is called while 9569 preparing to make the object part of the free list. */ 9570 static void poison (T *obj) { 9571 T *p ATTRIBUTE_UNUSED = obj; 9572 T **q ATTRIBUTE_UNUSED = &next (obj); 9573 9574 #ifdef ENABLE_GC_CHECKING 9575 /* Poison the data, to indicate the data is garbage. */ 9576 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p))); 9577 memset (p, 0xa5, sizeof (*p)); 9578 #endif 9579 /* Let valgrind know the object is free. */ 9580 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p))); 9581 9582 /* Let valgrind know the next portion of the object is available, 9583 but uninitialized. */ 9584 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q))); 9585 } 9586 9587 /* Bring an object that underwent at least one lifecycle after anew 9588 and before the most recent free and poison, back to a usable 9589 state, reinitializing whatever is needed for it to be 9590 functionally equivalent to an object just allocated and returned 9591 by anew. This may poison or clear the next field, used by 9592 freelist housekeeping after poison was called. */ 9593 static void reinit (T *obj) { 9594 T **q ATTRIBUTE_UNUSED = &next (obj); 9595 9596 #ifdef ENABLE_GC_CHECKING 9597 memset (q, 0xa5, sizeof (*q)); 9598 #endif 9599 /* Let valgrind know the entire object is available, but 9600 uninitialized. */ 9601 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj))); 9602 } 9603 9604 /* Reference a GTY-deletable pointer that points to the first object 9605 in the free list proper. */ 9606 T *&head; 9607 public: 9608 /* Construct a freelist object chaining objects off of HEAD. */ 9609 freelist (T *&head) : head(head) {} 9610 9611 /* Add OBJ to the free object list. The former head becomes OBJ's 9612 successor. */ 9613 void free (T *obj) 9614 { 9615 poison (obj); 9616 next (obj) = head; 9617 head = obj; 9618 } 9619 9620 /* Take an object from the free list, if one is available, or 9621 allocate a new one. Objects taken from the free list should be 9622 regarded as filled with garbage, except for bits that are 9623 configured to be preserved across free and alloc. */ 9624 T *alloc () 9625 { 9626 if (head) 9627 { 9628 T *obj = head; 9629 head = next (head); 9630 reinit (obj); 9631 return obj; 9632 } 9633 else 9634 return anew (); 9635 } 9636 }; 9637 9638 /* Explicitly specialize the interfaces for freelist<tree_node>: we 9639 want to allocate a TREE_LIST using the usual interface, and ensure 9640 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of 9641 build_tree_list logic in reinit, so this could go out of sync. */ 9642 template <> 9643 inline tree & 9644 freelist<tree_node>::next (tree obj) 9645 { 9646 return TREE_CHAIN (obj); 9647 } 9648 template <> 9649 inline tree 9650 freelist<tree_node>::anew () 9651 { 9652 return build_tree_list (NULL, NULL); 9653 } 9654 template <> 9655 inline void 9656 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED) 9657 { 9658 int size ATTRIBUTE_UNUSED = sizeof (tree_list); 9659 tree p ATTRIBUTE_UNUSED = obj; 9660 tree_base *b ATTRIBUTE_UNUSED = &obj->base; 9661 tree *q ATTRIBUTE_UNUSED = &next (obj); 9662 9663 #ifdef ENABLE_GC_CHECKING 9664 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST); 9665 9666 /* Poison the data, to indicate the data is garbage. */ 9667 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size)); 9668 memset (p, 0xa5, size); 9669 #endif 9670 /* Let valgrind know the object is free. */ 9671 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size)); 9672 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */ 9673 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b))); 9674 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q))); 9675 9676 #ifdef ENABLE_GC_CHECKING 9677 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b))); 9678 /* Keep TREE_CHAIN functional. */ 9679 TREE_SET_CODE (obj, TREE_LIST); 9680 #else 9681 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b))); 9682 #endif 9683 } 9684 template <> 9685 inline void 9686 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED) 9687 { 9688 tree_common *c ATTRIBUTE_UNUSED = &obj->common; 9689 9690 #ifdef ENABLE_GC_CHECKING 9691 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST); 9692 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list))); 9693 memset (obj, 0, sizeof (tree_list)); 9694 #endif 9695 9696 /* Let valgrind know the entire object is available, but 9697 uninitialized. */ 9698 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list))); 9699 9700 #ifdef ENABLE_GC_CHECKING 9701 TREE_SET_CODE (obj, TREE_LIST); 9702 #else 9703 TREE_CHAIN (obj) = NULL_TREE; 9704 TREE_TYPE (obj) = NULL_TREE; 9705 #endif 9706 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (c, sizeof (*c))); 9707 } 9708 9709 /* Point to the first object in the TREE_LIST freelist. */ 9710 static GTY((deletable)) tree tree_list_freelist_head; 9711 /* Return the/an actual TREE_LIST freelist. */ 9712 static inline freelist<tree_node> 9713 tree_list_freelist () 9714 { 9715 return tree_list_freelist_head; 9716 } 9717 9718 /* Point to the first object in the tinst_level freelist. */ 9719 static GTY((deletable)) tinst_level *tinst_level_freelist_head; 9720 /* Return the/an actual tinst_level freelist. */ 9721 static inline freelist<tinst_level> 9722 tinst_level_freelist () 9723 { 9724 return tinst_level_freelist_head; 9725 } 9726 9727 /* Point to the first object in the pending_template freelist. */ 9728 static GTY((deletable)) pending_template *pending_template_freelist_head; 9729 /* Return the/an actual pending_template freelist. */ 9730 static inline freelist<pending_template> 9731 pending_template_freelist () 9732 { 9733 return pending_template_freelist_head; 9734 } 9735 9736 /* Build the TREE_LIST object out of a split list, store it 9737 permanently, and return it. */ 9738 tree 9739 tinst_level::to_list () 9740 { 9741 gcc_assert (split_list_p ()); 9742 tree ret = tree_list_freelist ().alloc (); 9743 TREE_PURPOSE (ret) = tldcl; 9744 TREE_VALUE (ret) = targs; 9745 tldcl = ret; 9746 targs = NULL; 9747 gcc_assert (tree_list_p ()); 9748 return ret; 9749 } 9750 9751 const unsigned short tinst_level::refcount_infinity; 9752 9753 /* Increment OBJ's refcount unless it is already infinite. */ 9754 static tinst_level * 9755 inc_refcount_use (tinst_level *obj) 9756 { 9757 if (obj && obj->refcount != tinst_level::refcount_infinity) 9758 ++obj->refcount; 9759 return obj; 9760 } 9761 9762 /* Release storage for OBJ and node, if it's a TREE_LIST. */ 9763 void 9764 tinst_level::free (tinst_level *obj) 9765 { 9766 if (obj->tree_list_p ()) 9767 tree_list_freelist ().free (obj->get_node ()); 9768 tinst_level_freelist ().free (obj); 9769 } 9770 9771 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release 9772 OBJ's DECL and OBJ, and start over with the tinst_level object that 9773 used to be referenced by OBJ's NEXT. */ 9774 static void 9775 dec_refcount_use (tinst_level *obj) 9776 { 9777 while (obj 9778 && obj->refcount != tinst_level::refcount_infinity 9779 && !--obj->refcount) 9780 { 9781 tinst_level *next = obj->next; 9782 tinst_level::free (obj); 9783 obj = next; 9784 } 9785 } 9786 9787 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ 9788 and of the former PTR. Omitting the second argument is equivalent 9789 to passing (T*)NULL; this is allowed because passing the 9790 zero-valued integral constant NULL confuses type deduction and/or 9791 overload resolution. */ 9792 template <typename T> 9793 static void 9794 set_refcount_ptr (T *& ptr, T *obj = NULL) 9795 { 9796 T *save = ptr; 9797 ptr = inc_refcount_use (obj); 9798 dec_refcount_use (save); 9799 } 9800 9801 static void 9802 add_pending_template (tree d) 9803 { 9804 tree ti = (TYPE_P (d) 9805 ? CLASSTYPE_TEMPLATE_INFO (d) 9806 : DECL_TEMPLATE_INFO (d)); 9807 struct pending_template *pt; 9808 int level; 9809 9810 if (TI_PENDING_TEMPLATE_FLAG (ti)) 9811 return; 9812 9813 /* We are called both from instantiate_decl, where we've already had a 9814 tinst_level pushed, and instantiate_template, where we haven't. 9815 Compensate. */ 9816 gcc_assert (TREE_CODE (d) != TREE_LIST); 9817 level = !current_tinst_level 9818 || current_tinst_level->maybe_get_node () != d; 9819 9820 if (level) 9821 push_tinst_level (d); 9822 9823 pt = pending_template_freelist ().alloc (); 9824 pt->next = NULL; 9825 pt->tinst = NULL; 9826 set_refcount_ptr (pt->tinst, current_tinst_level); 9827 if (last_pending_template) 9828 last_pending_template->next = pt; 9829 else 9830 pending_templates = pt; 9831 9832 last_pending_template = pt; 9833 9834 TI_PENDING_TEMPLATE_FLAG (ti) = 1; 9835 9836 if (level) 9837 pop_tinst_level (); 9838 } 9839 9840 9841 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and 9842 ARGLIST. Valid choices for FNS are given in the cp-tree.def 9843 documentation for TEMPLATE_ID_EXPR. */ 9844 9845 tree 9846 lookup_template_function (tree fns, tree arglist) 9847 { 9848 if (fns == error_mark_node || arglist == error_mark_node) 9849 return error_mark_node; 9850 9851 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC); 9852 9853 if (!is_overloaded_fn (fns) && !identifier_p (fns)) 9854 { 9855 error ("%q#D is not a function template", fns); 9856 return error_mark_node; 9857 } 9858 9859 if (BASELINK_P (fns)) 9860 { 9861 fns = copy_node (fns); 9862 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR, 9863 unknown_type_node, 9864 BASELINK_FUNCTIONS (fns), 9865 arglist); 9866 return fns; 9867 } 9868 9869 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist); 9870 } 9871 9872 /* Within the scope of a template class S<T>, the name S gets bound 9873 (in build_self_reference) to a TYPE_DECL for the class, not a 9874 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, 9875 or one of its enclosing classes, and that type is a template, 9876 return the associated TEMPLATE_DECL. Otherwise, the original 9877 DECL is returned. 9878 9879 Also handle the case when DECL is a TREE_LIST of ambiguous 9880 injected-class-names from different bases. */ 9881 9882 tree 9883 maybe_get_template_decl_from_type_decl (tree decl) 9884 { 9885 if (decl == NULL_TREE) 9886 return decl; 9887 9888 /* DR 176: A lookup that finds an injected-class-name (10.2 9889 [class.member.lookup]) can result in an ambiguity in certain cases 9890 (for example, if it is found in more than one base class). If all of 9891 the injected-class-names that are found refer to specializations of 9892 the same class template, and if the name is followed by a 9893 template-argument-list, the reference refers to the class template 9894 itself and not a specialization thereof, and is not ambiguous. */ 9895 if (TREE_CODE (decl) == TREE_LIST) 9896 { 9897 tree t, tmpl = NULL_TREE; 9898 for (t = decl; t; t = TREE_CHAIN (t)) 9899 { 9900 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t)); 9901 if (!tmpl) 9902 tmpl = elt; 9903 else if (tmpl != elt) 9904 break; 9905 } 9906 if (tmpl && t == NULL_TREE) 9907 return tmpl; 9908 else 9909 return decl; 9910 } 9911 9912 return (decl != NULL_TREE 9913 && DECL_SELF_REFERENCE_P (decl) 9914 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) 9915 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; 9916 } 9917 9918 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of 9919 parameters, find the desired type. 9920 9921 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. 9922 9923 IN_DECL, if non-NULL, is the template declaration we are trying to 9924 instantiate. 9925 9926 If ENTERING_SCOPE is nonzero, we are about to enter the scope of 9927 the class we are looking up. 9928 9929 Issue error and warning messages under control of COMPLAIN. 9930 9931 If the template class is really a local class in a template 9932 function, then the FUNCTION_CONTEXT is the function in which it is 9933 being instantiated. 9934 9935 ??? Note that this function is currently called *twice* for each 9936 template-id: the first time from the parser, while creating the 9937 incomplete type (finish_template_type), and the second type during the 9938 real instantiation (instantiate_template_class). This is surely something 9939 that we want to avoid. It also causes some problems with argument 9940 coercion (see convert_nontype_argument for more information on this). */ 9941 9942 tree 9943 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context, 9944 int entering_scope, tsubst_flags_t complain) 9945 { 9946 auto_timevar tv (TV_TEMPLATE_INST); 9947 9948 tree templ = NULL_TREE, parmlist; 9949 tree t; 9950 spec_entry **slot; 9951 spec_entry *entry; 9952 spec_entry elt; 9953 hashval_t hash; 9954 9955 if (identifier_p (d1)) 9956 { 9957 tree value = innermost_non_namespace_value (d1); 9958 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value)) 9959 templ = value; 9960 else 9961 { 9962 if (context) 9963 push_decl_namespace (context); 9964 templ = lookup_name (d1); 9965 templ = maybe_get_template_decl_from_type_decl (templ); 9966 if (context) 9967 pop_decl_namespace (); 9968 } 9969 } 9970 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1))) 9971 { 9972 tree type = TREE_TYPE (d1); 9973 9974 /* If we are declaring a constructor, say A<T>::A<T>, we will get 9975 an implicit typename for the second A. Deal with it. */ 9976 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) 9977 type = TREE_TYPE (type); 9978 9979 if (CLASSTYPE_TEMPLATE_INFO (type)) 9980 { 9981 templ = CLASSTYPE_TI_TEMPLATE (type); 9982 d1 = DECL_NAME (templ); 9983 } 9984 } 9985 else if (TREE_CODE (d1) == ENUMERAL_TYPE 9986 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1))) 9987 { 9988 templ = TYPE_TI_TEMPLATE (d1); 9989 d1 = DECL_NAME (templ); 9990 } 9991 else if (DECL_TYPE_TEMPLATE_P (d1)) 9992 { 9993 templ = d1; 9994 d1 = DECL_NAME (templ); 9995 } 9996 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1)) 9997 { 9998 templ = d1; 9999 d1 = DECL_NAME (templ); 10000 } 10001 10002 /* Issue an error message if we didn't find a template. */ 10003 if (! templ) 10004 { 10005 if (complain & tf_error) 10006 error ("%qT is not a template", d1); 10007 return error_mark_node; 10008 } 10009 10010 if (TREE_CODE (templ) != TEMPLATE_DECL 10011 /* Make sure it's a user visible template, if it was named by 10012 the user. */ 10013 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ) 10014 && !PRIMARY_TEMPLATE_P (templ))) 10015 { 10016 if (complain & tf_error) 10017 { 10018 error ("non-template type %qT used as a template", d1); 10019 if (in_decl) 10020 error ("for template declaration %q+D", in_decl); 10021 } 10022 return error_mark_node; 10023 } 10024 10025 complain &= ~tf_user; 10026 10027 /* An alias that just changes the name of a template is equivalent to the 10028 other template, so if any of the arguments are pack expansions, strip 10029 the alias to avoid problems with a pack expansion passed to a non-pack 10030 alias template parameter (DR 1430). */ 10031 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist))) 10032 templ = get_underlying_template (templ); 10033 10034 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ)) 10035 { 10036 tree parm; 10037 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain); 10038 if (arglist2 == error_mark_node 10039 || (!uses_template_parms (arglist2) 10040 && check_instantiated_args (templ, arglist2, complain))) 10041 return error_mark_node; 10042 10043 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2); 10044 return parm; 10045 } 10046 else 10047 { 10048 tree template_type = TREE_TYPE (templ); 10049 tree gen_tmpl; 10050 tree type_decl; 10051 tree found = NULL_TREE; 10052 int arg_depth; 10053 int parm_depth; 10054 int is_dependent_type; 10055 int use_partial_inst_tmpl = false; 10056 10057 if (template_type == error_mark_node) 10058 /* An error occurred while building the template TEMPL, and a 10059 diagnostic has most certainly been emitted for that 10060 already. Let's propagate that error. */ 10061 return error_mark_node; 10062 10063 gen_tmpl = most_general_template (templ); 10064 if (modules_p ()) 10065 lazy_load_pendings (gen_tmpl); 10066 10067 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); 10068 parm_depth = TMPL_PARMS_DEPTH (parmlist); 10069 arg_depth = TMPL_ARGS_DEPTH (arglist); 10070 10071 if (arg_depth == 1 && parm_depth > 1) 10072 { 10073 /* We've been given an incomplete set of template arguments. 10074 For example, given: 10075 10076 template <class T> struct S1 { 10077 template <class U> struct S2 {}; 10078 template <class U> struct S2<U*> {}; 10079 }; 10080 10081 we will be called with an ARGLIST of `U*', but the 10082 TEMPLATE will be `template <class T> template 10083 <class U> struct S1<T>::S2'. We must fill in the missing 10084 arguments. */ 10085 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ)); 10086 arglist = add_outermost_template_args (TI_ARGS (ti), arglist); 10087 arg_depth = TMPL_ARGS_DEPTH (arglist); 10088 } 10089 10090 /* Now we should have enough arguments. */ 10091 gcc_assert (parm_depth == arg_depth); 10092 10093 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl)) 10094 { 10095 /* The user referred to a specialization of an alias 10096 template represented by GEN_TMPL. 10097 10098 [temp.alias]/2 says: 10099 10100 When a template-id refers to the specialization of an 10101 alias template, it is equivalent to the associated 10102 type obtained by substitution of its 10103 template-arguments for the template-parameters in the 10104 type-id of the alias template. */ 10105 10106 t = instantiate_alias_template (gen_tmpl, arglist, complain); 10107 /* Note that the call above (by indirectly calling 10108 register_specialization in tsubst_decl) registers the 10109 TYPE_DECL representing the specialization of the alias 10110 template. So next time someone substitutes ARGLIST for 10111 the template parms into the alias template (GEN_TMPL), 10112 she'll get that TYPE_DECL back. */ 10113 10114 if (t == error_mark_node) 10115 return error_mark_node; 10116 return TREE_TYPE (t); 10117 } 10118 10119 /* From here on, we're only interested in the most general 10120 template. */ 10121 10122 /* Shortcut looking up the current class scope again. */ 10123 for (tree cur = current_nonlambda_class_type (); 10124 cur != NULL_TREE; 10125 cur = get_containing_scope (cur)) 10126 { 10127 if (!CLASS_TYPE_P (cur)) 10128 continue; 10129 10130 tree ti = CLASSTYPE_TEMPLATE_INFO (cur); 10131 if (!ti || arg_depth > TMPL_ARGS_DEPTH (TI_ARGS (ti))) 10132 break; 10133 10134 if (gen_tmpl == most_general_template (TI_TEMPLATE (ti)) 10135 && comp_template_args (arglist, TI_ARGS (ti))) 10136 return cur; 10137 } 10138 10139 /* Calculate the BOUND_ARGS. These will be the args that are 10140 actually tsubst'd into the definition to create the 10141 instantiation. */ 10142 if (PRIMARY_TEMPLATE_P (gen_tmpl)) 10143 arglist = coerce_template_parms (parmlist, arglist, gen_tmpl, complain); 10144 10145 if (arglist == error_mark_node) 10146 /* We were unable to bind the arguments. */ 10147 return error_mark_node; 10148 10149 /* In the scope of a template class, explicit references to the 10150 template class refer to the type of the template, not any 10151 instantiation of it. For example, in: 10152 10153 template <class T> class C { void f(C<T>); } 10154 10155 the `C<T>' is just the same as `C'. Outside of the 10156 class, however, such a reference is an instantiation. */ 10157 if (entering_scope 10158 || !PRIMARY_TEMPLATE_P (gen_tmpl) 10159 || currently_open_class (template_type)) 10160 { 10161 tree tinfo = TYPE_TEMPLATE_INFO (template_type); 10162 10163 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist)) 10164 return template_type; 10165 } 10166 10167 /* If we already have this specialization, return it. */ 10168 elt.tmpl = gen_tmpl; 10169 elt.args = arglist; 10170 elt.spec = NULL_TREE; 10171 hash = spec_hasher::hash (&elt); 10172 entry = type_specializations->find_with_hash (&elt, hash); 10173 10174 if (entry) 10175 return entry->spec; 10176 10177 /* If the template's constraints are not satisfied, 10178 then we cannot form a valid type. 10179 10180 Note that the check is deferred until after the hash 10181 lookup. This prevents redundant checks on previously 10182 instantiated specializations. */ 10183 if (flag_concepts 10184 && !constraints_satisfied_p (gen_tmpl, arglist)) 10185 { 10186 if (complain & tf_error) 10187 { 10188 auto_diagnostic_group d; 10189 error ("template constraint failure for %qD", gen_tmpl); 10190 diagnose_constraints (input_location, gen_tmpl, arglist); 10191 } 10192 return error_mark_node; 10193 } 10194 10195 is_dependent_type = uses_template_parms (arglist); 10196 10197 /* If the deduced arguments are invalid, then the binding 10198 failed. */ 10199 if (!is_dependent_type 10200 && check_instantiated_args (gen_tmpl, 10201 INNERMOST_TEMPLATE_ARGS (arglist), 10202 complain)) 10203 return error_mark_node; 10204 10205 if (!is_dependent_type 10206 && !PRIMARY_TEMPLATE_P (gen_tmpl) 10207 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl)) 10208 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL) 10209 /* This occurs when the user has tried to define a tagged type 10210 in a scope that forbids it. We emitted an error during the 10211 parse. We didn't complete the bail out then, so here we 10212 are. */ 10213 return error_mark_node; 10214 10215 context = DECL_CONTEXT (gen_tmpl); 10216 if (context && TYPE_P (context)) 10217 { 10218 if (!uses_template_parms (DECL_CONTEXT (templ))) 10219 /* If the context of the partially instantiated template is 10220 already non-dependent, then we might as well use it. */ 10221 context = DECL_CONTEXT (templ); 10222 else 10223 { 10224 context = tsubst_aggr_type (context, arglist, 10225 complain, in_decl, true); 10226 /* Try completing the enclosing context if it's not already so. */ 10227 if (context != error_mark_node 10228 && !COMPLETE_TYPE_P (context)) 10229 { 10230 context = complete_type (context); 10231 if (COMPLETE_TYPE_P (context)) 10232 { 10233 /* Completion could have caused us to register the desired 10234 specialization already, so check the table again. */ 10235 entry = type_specializations->find_with_hash (&elt, hash); 10236 if (entry) 10237 return entry->spec; 10238 } 10239 } 10240 } 10241 } 10242 else 10243 context = tsubst (context, arglist, complain, in_decl); 10244 10245 if (context == error_mark_node) 10246 return error_mark_node; 10247 10248 if (!context) 10249 context = global_namespace; 10250 10251 /* Create the type. */ 10252 if (TREE_CODE (template_type) == ENUMERAL_TYPE) 10253 { 10254 if (!is_dependent_type) 10255 { 10256 set_current_access_from_decl (TYPE_NAME (template_type)); 10257 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE, 10258 tsubst (ENUM_UNDERLYING_TYPE (template_type), 10259 arglist, complain, in_decl), 10260 tsubst_attributes (TYPE_ATTRIBUTES (template_type), 10261 arglist, complain, in_decl), 10262 SCOPED_ENUM_P (template_type), NULL); 10263 10264 if (t == error_mark_node) 10265 return t; 10266 } 10267 else 10268 { 10269 /* We don't want to call start_enum for this type, since 10270 the values for the enumeration constants may involve 10271 template parameters. And, no one should be interested 10272 in the enumeration constants for such a type. */ 10273 t = cxx_make_type (ENUMERAL_TYPE); 10274 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type)); 10275 } 10276 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type)); 10277 ENUM_FIXED_UNDERLYING_TYPE_P (t) 10278 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type); 10279 } 10280 else if (CLASS_TYPE_P (template_type)) 10281 { 10282 /* Lambda closures are regenerated in tsubst_lambda_expr, not 10283 instantiated here. */ 10284 gcc_assert (!LAMBDA_TYPE_P (template_type)); 10285 10286 t = make_class_type (TREE_CODE (template_type)); 10287 CLASSTYPE_DECLARED_CLASS (t) 10288 = CLASSTYPE_DECLARED_CLASS (template_type); 10289 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); 10290 10291 /* A local class. Make sure the decl gets registered properly. */ 10292 if (context == current_function_decl) 10293 if (pushtag (DECL_NAME (gen_tmpl), t) 10294 == error_mark_node) 10295 return error_mark_node; 10296 10297 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist)) 10298 /* This instantiation is another name for the primary 10299 template type. Set the TYPE_CANONICAL field 10300 appropriately. */ 10301 TYPE_CANONICAL (t) = template_type; 10302 else if (any_template_arguments_need_structural_equality_p (arglist)) 10303 SET_TYPE_STRUCTURAL_EQUALITY (t); 10304 } 10305 else 10306 gcc_unreachable (); 10307 10308 /* If we called start_enum or pushtag above, this information 10309 will already be set up. */ 10310 type_decl = TYPE_NAME (t); 10311 if (!type_decl) 10312 { 10313 TYPE_CONTEXT (t) = FROB_CONTEXT (context); 10314 10315 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t); 10316 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); 10317 DECL_SOURCE_LOCATION (type_decl) 10318 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type)); 10319 } 10320 10321 set_instantiating_module (type_decl); 10322 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value 10323 of export flag. We want to propagate this because it might 10324 be a friend declaration that pushes a new hidden binding. */ 10325 DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl); 10326 10327 if (CLASS_TYPE_P (template_type)) 10328 { 10329 TREE_PRIVATE (type_decl) 10330 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type)); 10331 TREE_PROTECTED (type_decl) 10332 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type)); 10333 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type)) 10334 { 10335 DECL_VISIBILITY_SPECIFIED (type_decl) = 1; 10336 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type); 10337 } 10338 } 10339 10340 if (OVERLOAD_TYPE_P (t)) 10341 { 10342 static const char *tags[] = {"abi_tag", "may_alias"}; 10343 10344 for (unsigned ix = 0; ix != 2; ix++) 10345 { 10346 tree attributes 10347 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type)); 10348 10349 if (attributes) 10350 TYPE_ATTRIBUTES (t) 10351 = tree_cons (TREE_PURPOSE (attributes), 10352 TREE_VALUE (attributes), 10353 TYPE_ATTRIBUTES (t)); 10354 } 10355 } 10356 10357 /* Let's consider the explicit specialization of a member 10358 of a class template specialization that is implicitly instantiated, 10359 e.g.: 10360 template<class T> 10361 struct S 10362 { 10363 template<class U> struct M {}; //#0 10364 }; 10365 10366 template<> 10367 template<> 10368 struct S<int>::M<char> //#1 10369 { 10370 int i; 10371 }; 10372 [temp.expl.spec]/4 says this is valid. 10373 10374 In this case, when we write: 10375 S<int>::M<char> m; 10376 10377 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from 10378 the one of #0. 10379 10380 When we encounter #1, we want to store the partial instantiation 10381 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE. 10382 10383 For all cases other than this "explicit specialization of member of a 10384 class template", we just want to store the most general template into 10385 the CLASSTYPE_TI_TEMPLATE of M. 10386 10387 This case of "explicit specialization of member of a class template" 10388 only happens when: 10389 1/ the enclosing class is an instantiation of, and therefore not 10390 the same as, the context of the most general template, and 10391 2/ we aren't looking at the partial instantiation itself, i.e. 10392 the innermost arguments are not the same as the innermost parms of 10393 the most general template. 10394 10395 So it's only when 1/ and 2/ happens that we want to use the partial 10396 instantiation of the member template in lieu of its most general 10397 template. */ 10398 10399 if (PRIMARY_TEMPLATE_P (gen_tmpl) 10400 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist) 10401 /* the enclosing class must be an instantiation... */ 10402 && CLASS_TYPE_P (context) 10403 && !same_type_p (context, DECL_CONTEXT (gen_tmpl))) 10404 { 10405 TREE_VEC_LENGTH (arglist)--; 10406 ++processing_template_decl; 10407 tree tinfo = TYPE_TEMPLATE_INFO (TREE_TYPE (gen_tmpl)); 10408 tree partial_inst_args = 10409 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)), 10410 arglist, complain, NULL_TREE); 10411 --processing_template_decl; 10412 TREE_VEC_LENGTH (arglist)++; 10413 if (partial_inst_args == error_mark_node) 10414 return error_mark_node; 10415 use_partial_inst_tmpl = 10416 /*...and we must not be looking at the partial instantiation 10417 itself. */ 10418 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist), 10419 partial_inst_args); 10420 } 10421 10422 if (!use_partial_inst_tmpl) 10423 /* This case is easy; there are no member templates involved. */ 10424 found = gen_tmpl; 10425 else 10426 { 10427 /* This is a full instantiation of a member template. Find 10428 the partial instantiation of which this is an instance. */ 10429 10430 /* Temporarily reduce by one the number of levels in the ARGLIST 10431 so as to avoid comparing the last set of arguments. */ 10432 TREE_VEC_LENGTH (arglist)--; 10433 /* We don't use COMPLAIN in the following call because this isn't 10434 the immediate context of deduction. For instance, tf_partial 10435 could be set here as we might be at the beginning of template 10436 argument deduction when any explicitly specified template 10437 arguments are substituted into the function type. tf_partial 10438 could lead into trouble because we wouldn't find the partial 10439 instantiation that might have been created outside tf_partial 10440 context, because the levels of template parameters wouldn't 10441 match, because in a tf_partial context, tsubst doesn't reduce 10442 TEMPLATE_PARM_LEVEL. */ 10443 found = tsubst (gen_tmpl, arglist, tf_none, NULL_TREE); 10444 TREE_VEC_LENGTH (arglist)++; 10445 found = (TREE_CODE (found) == TEMPLATE_DECL 10446 ? found 10447 : CLASSTYPE_TI_TEMPLATE (found)); 10448 10449 if (DECL_CLASS_TEMPLATE_P (found) 10450 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found))) 10451 { 10452 /* If this partial instantiation is specialized, we want to 10453 use it for hash table lookup. */ 10454 elt.tmpl = found; 10455 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist); 10456 hash = spec_hasher::hash (&elt); 10457 } 10458 } 10459 10460 /* Build template info for the new specialization. */ 10461 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist)); 10462 10463 elt.spec = t; 10464 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT); 10465 gcc_checking_assert (*slot == NULL); 10466 entry = ggc_alloc<spec_entry> (); 10467 *entry = elt; 10468 *slot = entry; 10469 10470 /* Note this use of the partial instantiation so we can check it 10471 later in maybe_process_partial_specialization. */ 10472 DECL_TEMPLATE_INSTANTIATIONS (found) 10473 = tree_cons (arglist, t, 10474 DECL_TEMPLATE_INSTANTIATIONS (found)); 10475 10476 if (TREE_CODE (template_type) == ENUMERAL_TYPE 10477 && !uses_template_parms (current_nonlambda_scope ())) 10478 /* Now that the type has been registered on the instantiations 10479 list, we set up the enumerators. Because the enumeration 10480 constants may involve the enumeration type itself, we make 10481 sure to register the type first, and then create the 10482 constants. That way, doing tsubst_expr for the enumeration 10483 constants won't result in recursive calls here; we'll find 10484 the instantiation and exit above. */ 10485 tsubst_enum (template_type, t, arglist); 10486 10487 if (CLASS_TYPE_P (template_type) && is_dependent_type) 10488 /* If the type makes use of template parameters, the 10489 code that generates debugging information will crash. */ 10490 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1; 10491 10492 /* Possibly limit visibility based on template args. */ 10493 TREE_PUBLIC (type_decl) = 1; 10494 determine_visibility (type_decl); 10495 10496 inherit_targ_abi_tags (t); 10497 10498 return t; 10499 } 10500 } 10501 10502 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */ 10503 10504 tree 10505 lookup_template_variable (tree templ, tree arglist, tsubst_flags_t complain) 10506 { 10507 if (flag_concepts && variable_concept_p (templ)) 10508 return build_concept_check (templ, arglist, tf_none); 10509 10510 tree gen_templ = most_general_template (templ); 10511 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_templ); 10512 arglist = add_outermost_template_args (templ, arglist); 10513 arglist = coerce_template_parms (parms, arglist, templ, complain); 10514 if (arglist == error_mark_node) 10515 return error_mark_node; 10516 10517 /* The type of the expression is NULL_TREE since the template-id could refer 10518 to an explicit or partial specialization. */ 10519 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist); 10520 } 10521 10522 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR if it's 10523 not dependent. */ 10524 10525 tree 10526 finish_template_variable (tree var, tsubst_flags_t complain) 10527 { 10528 tree templ = TREE_OPERAND (var, 0); 10529 tree arglist = TREE_OPERAND (var, 1); 10530 10531 /* If the template or arguments are dependent, then we 10532 can't resolve the TEMPLATE_ID_EXPR yet. */ 10533 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ)) != 1 10534 || any_dependent_template_arguments_p (arglist)) 10535 return var; 10536 10537 if (flag_concepts && !constraints_satisfied_p (templ, arglist)) 10538 { 10539 if (complain & tf_error) 10540 { 10541 auto_diagnostic_group d; 10542 error ("use of invalid variable template %qE", var); 10543 diagnose_constraints (location_of (var), templ, arglist); 10544 } 10545 return error_mark_node; 10546 } 10547 10548 return instantiate_template (templ, arglist, complain); 10549 } 10550 10551 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having 10552 TARGS template args, and instantiate it if it's not dependent. */ 10553 10554 tree 10555 lookup_and_finish_template_variable (tree templ, tree targs, 10556 tsubst_flags_t complain) 10557 { 10558 tree var = lookup_template_variable (templ, targs, complain); 10559 if (var == error_mark_node) 10560 return error_mark_node; 10561 var = finish_template_variable (var, complain); 10562 mark_used (var, complain); 10563 return var; 10564 } 10565 10566 /* If the set of template parameters PARMS contains a template parameter 10567 at the given LEVEL and INDEX, then return this parameter. Otherwise 10568 return NULL_TREE. */ 10569 10570 static tree 10571 corresponding_template_parameter_list (tree parms, int level, int index) 10572 { 10573 while (TMPL_PARMS_DEPTH (parms) > level) 10574 parms = TREE_CHAIN (parms); 10575 10576 if (TMPL_PARMS_DEPTH (parms) != level 10577 || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index) 10578 return NULL_TREE; 10579 10580 return TREE_VEC_ELT (TREE_VALUE (parms), index); 10581 } 10582 10583 /* Return the TREE_LIST for the template parameter from PARMS that positionally 10584 corresponds to the template parameter PARM, or else return NULL_TREE. */ 10585 10586 static tree 10587 corresponding_template_parameter_list (tree parms, tree parm) 10588 { 10589 int level, index; 10590 template_parm_level_and_index (parm, &level, &index); 10591 return corresponding_template_parameter_list (parms, level, index); 10592 } 10593 10594 /* As above, but pull out the actual parameter. */ 10595 10596 static tree 10597 corresponding_template_parameter (tree parms, tree parm) 10598 { 10599 tree list = corresponding_template_parameter_list (parms, parm); 10600 if (!list) 10601 return NULL_TREE; 10602 10603 tree t = TREE_VALUE (list); 10604 /* As in template_parm_to_arg. */ 10605 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL) 10606 t = TREE_TYPE (t); 10607 else 10608 t = DECL_INITIAL (t); 10609 10610 gcc_assert (TEMPLATE_PARM_P (t)); 10611 return t; 10612 } 10613 10614 struct pair_fn_data 10616 { 10617 tree_fn_t fn; 10618 tree_fn_t any_fn; 10619 void *data; 10620 /* True when we should also visit template parameters that occur in 10621 non-deduced contexts. */ 10622 bool include_nondeduced_p; 10623 hash_set<tree> *visited; 10624 }; 10625 10626 /* Called from for_each_template_parm via walk_tree. */ 10627 10628 static tree 10629 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d) 10630 { 10631 tree t = *tp; 10632 struct pair_fn_data *pfd = (struct pair_fn_data *) d; 10633 tree_fn_t fn = pfd->fn; 10634 void *data = pfd->data; 10635 tree result = NULL_TREE; 10636 10637 #define WALK_SUBTREE(NODE) \ 10638 do \ 10639 { \ 10640 result = for_each_template_parm (NODE, fn, data, pfd->visited, \ 10641 pfd->include_nondeduced_p, \ 10642 pfd->any_fn); \ 10643 if (result) goto out; \ 10644 } \ 10645 while (0) 10646 10647 if (pfd->any_fn && (*pfd->any_fn)(t, data)) 10648 return t; 10649 10650 if (TYPE_P (t) 10651 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)) 10652 WALK_SUBTREE (TYPE_CONTEXT (t)); 10653 10654 switch (TREE_CODE (t)) 10655 { 10656 case RECORD_TYPE: 10657 if (TYPE_PTRMEMFUNC_P (t)) 10658 break; 10659 /* Fall through. */ 10660 10661 case UNION_TYPE: 10662 case ENUMERAL_TYPE: 10663 if (!TYPE_TEMPLATE_INFO (t)) 10664 *walk_subtrees = 0; 10665 else 10666 WALK_SUBTREE (TYPE_TI_ARGS (t)); 10667 break; 10668 10669 case INTEGER_TYPE: 10670 WALK_SUBTREE (TYPE_MIN_VALUE (t)); 10671 WALK_SUBTREE (TYPE_MAX_VALUE (t)); 10672 break; 10673 10674 case METHOD_TYPE: 10675 /* Since we're not going to walk subtrees, we have to do this 10676 explicitly here. */ 10677 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t)); 10678 /* Fall through. */ 10679 10680 case FUNCTION_TYPE: 10681 /* Check the return type. */ 10682 WALK_SUBTREE (TREE_TYPE (t)); 10683 10684 /* Check the parameter types. Since default arguments are not 10685 instantiated until they are needed, the TYPE_ARG_TYPES may 10686 contain expressions that involve template parameters. But, 10687 no-one should be looking at them yet. And, once they're 10688 instantiated, they don't contain template parameters, so 10689 there's no point in looking at them then, either. */ 10690 { 10691 tree parm; 10692 10693 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) 10694 WALK_SUBTREE (TREE_VALUE (parm)); 10695 10696 /* Since we've already handled the TYPE_ARG_TYPES, we don't 10697 want walk_tree walking into them itself. */ 10698 *walk_subtrees = 0; 10699 } 10700 10701 if (flag_noexcept_type) 10702 { 10703 tree spec = TYPE_RAISES_EXCEPTIONS (t); 10704 if (spec) 10705 WALK_SUBTREE (TREE_PURPOSE (spec)); 10706 } 10707 break; 10708 10709 case TYPEOF_TYPE: 10710 case DECLTYPE_TYPE: 10711 if (pfd->include_nondeduced_p 10712 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data, 10713 pfd->visited, 10714 pfd->include_nondeduced_p, 10715 pfd->any_fn)) 10716 return error_mark_node; 10717 *walk_subtrees = false; 10718 break; 10719 10720 case TRAIT_TYPE: 10721 if (pfd->include_nondeduced_p) 10722 { 10723 WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t)); 10724 WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t)); 10725 } 10726 *walk_subtrees = false; 10727 break; 10728 10729 case FUNCTION_DECL: 10730 case VAR_DECL: 10731 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) 10732 WALK_SUBTREE (DECL_TI_ARGS (t)); 10733 break; 10734 10735 case PARM_DECL: 10736 WALK_SUBTREE (TREE_TYPE (t)); 10737 break; 10738 10739 case CONST_DECL: 10740 if (DECL_TEMPLATE_PARM_P (t)) 10741 WALK_SUBTREE (DECL_INITIAL (t)); 10742 if (DECL_CONTEXT (t) 10743 && pfd->include_nondeduced_p) 10744 WALK_SUBTREE (DECL_CONTEXT (t)); 10745 break; 10746 10747 case BOUND_TEMPLATE_TEMPLATE_PARM: 10748 /* Record template parameters such as `T' inside `TT<T>'. */ 10749 WALK_SUBTREE (TYPE_TI_ARGS (t)); 10750 /* Fall through. */ 10751 10752 case TEMPLATE_TEMPLATE_PARM: 10753 case TEMPLATE_TYPE_PARM: 10754 case TEMPLATE_PARM_INDEX: 10755 if (fn && (*fn)(t, data)) 10756 return t; 10757 else if (!fn) 10758 return t; 10759 break; 10760 10761 case TEMPLATE_DECL: 10762 /* A template template parameter is encountered. */ 10763 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) 10764 WALK_SUBTREE (TREE_TYPE (t)); 10765 10766 /* Already substituted template template parameter */ 10767 *walk_subtrees = 0; 10768 break; 10769 10770 case TYPENAME_TYPE: 10771 /* A template-id in a TYPENAME_TYPE might be a deduced context after 10772 partial instantiation. */ 10773 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t)); 10774 *walk_subtrees = 0; 10775 break; 10776 10777 case INDIRECT_REF: 10778 case COMPONENT_REF: 10779 /* If there's no type, then this thing must be some expression 10780 involving template parameters. */ 10781 if (!fn && !TREE_TYPE (t)) 10782 return error_mark_node; 10783 break; 10784 10785 case CONSTRUCTOR: 10786 case TRAIT_EXPR: 10787 case PLUS_EXPR: 10788 case MULT_EXPR: 10789 case SCOPE_REF: 10790 /* These are non-deduced contexts. */ 10791 if (!pfd->include_nondeduced_p) 10792 *walk_subtrees = 0; 10793 break; 10794 10795 case MODOP_EXPR: 10796 case CAST_EXPR: 10797 case IMPLICIT_CONV_EXPR: 10798 case REINTERPRET_CAST_EXPR: 10799 case CONST_CAST_EXPR: 10800 case STATIC_CAST_EXPR: 10801 case DYNAMIC_CAST_EXPR: 10802 case ARROW_EXPR: 10803 case DOTSTAR_EXPR: 10804 case TYPEID_EXPR: 10805 case PSEUDO_DTOR_EXPR: 10806 if (!fn) 10807 return error_mark_node; 10808 break; 10809 10810 default: 10811 break; 10812 } 10813 10814 #undef WALK_SUBTREE 10815 10816 /* We didn't find any template parameters we liked. */ 10817 out: 10818 return result; 10819 } 10820 10821 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, 10822 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, 10823 call FN with the parameter and the DATA. 10824 If FN returns nonzero, the iteration is terminated, and 10825 for_each_template_parm returns 1. Otherwise, the iteration 10826 continues. If FN never returns a nonzero value, the value 10827 returned by for_each_template_parm is 0. If FN is NULL, it is 10828 considered to be the function which always returns 1. 10829 10830 If INCLUDE_NONDEDUCED_P, then this routine will also visit template 10831 parameters that occur in non-deduced contexts. When false, only 10832 visits those template parameters that can be deduced. */ 10833 10834 static tree 10835 for_each_template_parm (tree t, tree_fn_t fn, void* data, 10836 hash_set<tree> *visited, 10837 bool include_nondeduced_p, 10838 tree_fn_t any_fn) 10839 { 10840 struct pair_fn_data pfd; 10841 tree result; 10842 10843 /* Set up. */ 10844 pfd.fn = fn; 10845 pfd.any_fn = any_fn; 10846 pfd.data = data; 10847 pfd.include_nondeduced_p = include_nondeduced_p; 10848 10849 /* Walk the tree. (Conceptually, we would like to walk without 10850 duplicates, but for_each_template_parm_r recursively calls 10851 for_each_template_parm, so we would need to reorganize a fair 10852 bit to use walk_tree_without_duplicates, so we keep our own 10853 visited list.) */ 10854 if (visited) 10855 pfd.visited = visited; 10856 else 10857 pfd.visited = new hash_set<tree>; 10858 result = cp_walk_tree (&t, 10859 for_each_template_parm_r, 10860 &pfd, 10861 pfd.visited); 10862 10863 /* Clean up. */ 10864 if (!visited) 10865 { 10866 delete pfd.visited; 10867 pfd.visited = 0; 10868 } 10869 10870 return result; 10871 } 10872 10873 struct find_template_parameter_info 10874 { 10875 explicit find_template_parameter_info (tree ctx_parms) 10876 : ctx_parms (ctx_parms), 10877 max_depth (TMPL_PARMS_DEPTH (ctx_parms)) 10878 {} 10879 10880 hash_set<tree> visited; 10881 hash_set<tree> parms; 10882 tree parm_list = NULL_TREE; 10883 tree *parm_list_tail = &parm_list; 10884 tree ctx_parms; 10885 int max_depth; 10886 10887 tree find_in (tree); 10888 tree find_in_recursive (tree); 10889 bool found (tree); 10890 unsigned num_found () { return parms.elements (); } 10891 }; 10892 10893 /* Appends the declaration of T to the list in DATA. */ 10894 10895 static int 10896 keep_template_parm (tree t, void* data) 10897 { 10898 find_template_parameter_info *ftpi = (find_template_parameter_info*)data; 10899 10900 /* Template parameters declared within the expression are not part of 10901 the parameter mapping. For example, in this concept: 10902 10903 template<typename T> 10904 concept C = requires { <expr> } -> same_as<int>; 10905 10906 the return specifier same_as<int> declares a new decltype parameter 10907 that must not be part of the parameter mapping. The same is true 10908 for generic lambda parameters, lambda template parameters, etc. */ 10909 int level; 10910 int index; 10911 template_parm_level_and_index (t, &level, &index); 10912 if (level == 0 || level > ftpi->max_depth) 10913 return 0; 10914 10915 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) 10916 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the 10917 BOUND_TEMPLATE_TEMPLATE_PARM itself. */ 10918 t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t)); 10919 10920 /* This template parameter might be an argument to a cached dependent 10921 specalization that was formed earlier inside some other template, in 10922 which case the parameter is not among the ones that are in-scope. 10923 Look in CTX_PARMS to find the corresponding in-scope template 10924 parameter, and use it instead. */ 10925 if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t)) 10926 t = in_scope; 10927 10928 /* Arguments like const T yield parameters like const T. This means that 10929 a template-id like X<T, const T> would yield two distinct parameters: 10930 T and const T. Adjust types to their unqualified versions. */ 10931 if (TYPE_P (t)) 10932 t = TYPE_MAIN_VARIANT (t); 10933 if (!ftpi->parms.add (t)) 10934 { 10935 /* Append T to PARM_LIST. */ 10936 tree node = build_tree_list (NULL_TREE, t); 10937 *ftpi->parm_list_tail = node; 10938 ftpi->parm_list_tail = &TREE_CHAIN (node); 10939 } 10940 10941 /* Verify the parameter we found has a valid index. */ 10942 if (flag_checking) 10943 { 10944 tree parms = ftpi->ctx_parms; 10945 while (TMPL_PARMS_DEPTH (parms) > level) 10946 parms = TREE_CHAIN (parms); 10947 if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms))) 10948 gcc_assert (index < len); 10949 } 10950 10951 return 0; 10952 } 10953 10954 /* Ensure that we recursively examine certain terms that are not normally 10955 visited in for_each_template_parm_r. */ 10956 10957 static int 10958 any_template_parm_r (tree t, void *data) 10959 { 10960 find_template_parameter_info *ftpi = (find_template_parameter_info*)data; 10961 10962 #define WALK_SUBTREE(NODE) \ 10963 do \ 10964 { \ 10965 for_each_template_parm (NODE, keep_template_parm, data, \ 10966 &ftpi->visited, true, \ 10967 any_template_parm_r); \ 10968 } \ 10969 while (0) 10970 10971 /* A mention of a member alias/typedef is a use of all of its template 10972 arguments, including those from the enclosing class, so we don't use 10973 alias_template_specialization_p here. */ 10974 if (TYPE_P (t) && typedef_variant_p (t)) 10975 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t)) 10976 WALK_SUBTREE (TI_ARGS (tinfo)); 10977 10978 switch (TREE_CODE (t)) 10979 { 10980 case TEMPLATE_TYPE_PARM: 10981 /* Type constraints of a placeholder type may contain parameters. */ 10982 if (is_auto (t)) 10983 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t)) 10984 WALK_SUBTREE (constr); 10985 break; 10986 10987 case TEMPLATE_ID_EXPR: 10988 /* Search through references to variable templates. */ 10989 WALK_SUBTREE (TREE_OPERAND (t, 0)); 10990 WALK_SUBTREE (TREE_OPERAND (t, 1)); 10991 break; 10992 10993 case TEMPLATE_PARM_INDEX: 10994 WALK_SUBTREE (TREE_TYPE (t)); 10995 break; 10996 10997 case TEMPLATE_DECL: 10998 /* If T is a member template that shares template parameters with 10999 ctx_parms, we need to mark all those parameters for mapping. 11000 To that end, it should suffice to just walk the DECL_CONTEXT of 11001 the template (assuming the template is not overly general). */ 11002 WALK_SUBTREE (DECL_CONTEXT (t)); 11003 break; 11004 11005 case LAMBDA_EXPR: 11006 { 11007 /* Look in the parms and body. */ 11008 tree fn = lambda_function (t); 11009 WALK_SUBTREE (TREE_TYPE (fn)); 11010 WALK_SUBTREE (DECL_SAVED_TREE (fn)); 11011 } 11012 break; 11013 11014 case IDENTIFIER_NODE: 11015 if (IDENTIFIER_CONV_OP_P (t)) 11016 /* The conversion-type-id of a conversion operator may be dependent. */ 11017 WALK_SUBTREE (TREE_TYPE (t)); 11018 break; 11019 11020 case CONVERT_EXPR: 11021 if (is_dummy_object (t)) 11022 WALK_SUBTREE (TREE_TYPE (t)); 11023 break; 11024 11025 default: 11026 break; 11027 } 11028 11029 /* Keep walking. */ 11030 return 0; 11031 } 11032 11033 /* Look through T for template parameters. */ 11034 11035 tree 11036 find_template_parameter_info::find_in (tree t) 11037 { 11038 return for_each_template_parm (t, keep_template_parm, this, &visited, 11039 /*include_nondeduced*/true, 11040 any_template_parm_r); 11041 } 11042 11043 /* As above, but also recursively look into the default arguments of template 11044 parameters we found. Used for alias CTAD. */ 11045 11046 tree 11047 find_template_parameter_info::find_in_recursive (tree t) 11048 { 11049 if (tree r = find_in (t)) 11050 return r; 11051 /* Since newly found parms are added to the end of the list, we 11052 can just walk it until we reach the end. */ 11053 for (tree pl = parm_list; pl; pl = TREE_CHAIN (pl)) 11054 { 11055 tree parm = TREE_VALUE (pl); 11056 tree list = corresponding_template_parameter_list (ctx_parms, parm); 11057 if (tree r = find_in (TREE_PURPOSE (list))) 11058 return r; 11059 } 11060 return NULL_TREE; 11061 } 11062 11063 /* True if PARM was found by a previous call to find_in. PARM can be a 11064 TREE_LIST, a DECL_TEMPLATE_PARM_P, or a TEMPLATE_PARM_P. */ 11065 11066 bool 11067 find_template_parameter_info::found (tree parm) 11068 { 11069 if (TREE_CODE (parm) == TREE_LIST) 11070 parm = TREE_VALUE (parm); 11071 if (TREE_CODE (parm) == TYPE_DECL 11072 || TREE_CODE (parm) == TEMPLATE_DECL) 11073 parm = TREE_TYPE (parm); 11074 else 11075 parm = DECL_INITIAL (parm); 11076 gcc_checking_assert (TEMPLATE_PARM_P (parm)); 11077 return parms.contains (parm); 11078 } 11079 11080 /* Returns a list of unique template parameters found within T, where CTX_PARMS 11081 are the template parameters in scope. */ 11082 11083 tree 11084 find_template_parameters (tree t, tree ctx_parms) 11085 { 11086 if (!ctx_parms) 11087 return NULL_TREE; 11088 11089 find_template_parameter_info ftpi (ctx_parms); 11090 ftpi.find_in (t); 11091 return ftpi.parm_list; 11092 } 11093 11094 /* Returns true if T depends on any template parameter. */ 11095 11096 bool 11097 uses_template_parms (tree t) 11098 { 11099 if (t == NULL_TREE || t == error_mark_node) 11100 return false; 11101 11102 /* Namespaces can't depend on any template parameters. */ 11103 if (TREE_CODE (t) == NAMESPACE_DECL) 11104 return false; 11105 11106 processing_template_decl_sentinel ptds (/*reset*/false); 11107 ++processing_template_decl; 11108 11109 if (TYPE_P (t)) 11110 return dependent_type_p (t); 11111 else if (TREE_CODE (t) == TREE_VEC) 11112 return any_dependent_template_arguments_p (t); 11113 else if (TREE_CODE (t) == TREE_LIST) 11114 return (uses_template_parms (TREE_VALUE (t)) 11115 || uses_template_parms (TREE_CHAIN (t))); 11116 else if (TREE_CODE (t) == TYPE_DECL) 11117 return dependent_type_p (TREE_TYPE (t)); 11118 else 11119 return instantiation_dependent_expression_p (t); 11120 } 11121 11122 /* Returns true if T depends on any template parameter with level LEVEL. */ 11123 11124 bool 11125 uses_template_parms_level (tree t, int level) 11126 { 11127 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL, 11128 /*include_nondeduced_p=*/true); 11129 } 11130 11131 /* Returns true if the signature of DECL depends on any template parameter from 11132 its enclosing class. */ 11133 11134 static bool 11135 uses_outer_template_parms (tree decl) 11136 { 11137 int depth; 11138 if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl)) 11139 depth = TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)) - 1; 11140 else 11141 depth = template_class_depth (CP_DECL_CONTEXT (decl)); 11142 if (depth == 0) 11143 return false; 11144 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level, 11145 &depth, NULL, /*include_nondeduced_p=*/true)) 11146 return true; 11147 if (PRIMARY_TEMPLATE_P (decl) 11148 || DECL_TEMPLATE_TEMPLATE_PARM_P (decl)) 11149 { 11150 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl)); 11151 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) 11152 { 11153 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); 11154 tree defarg = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); 11155 if (TREE_CODE (parm) == PARM_DECL 11156 && for_each_template_parm (TREE_TYPE (parm), 11157 template_parm_outer_level, 11158 &depth, NULL, /*nondeduced*/true)) 11159 return true; 11160 if (TREE_CODE (parm) == TEMPLATE_DECL 11161 && uses_outer_template_parms (parm)) 11162 return true; 11163 if (defarg 11164 && for_each_template_parm (defarg, template_parm_outer_level, 11165 &depth, NULL, /*nondeduced*/true)) 11166 return true; 11167 } 11168 } 11169 if (uses_outer_template_parms_in_constraints (decl)) 11170 return true; 11171 return false; 11172 } 11173 11174 /* Returns true if the constraints of DECL depend on any template parameters 11175 from its enclosing scope. */ 11176 11177 bool 11178 uses_outer_template_parms_in_constraints (tree decl, tree ctx/*=NULL_TREE*/) 11179 { 11180 tree ci = get_constraints (decl); 11181 if (ci) 11182 ci = CI_ASSOCIATED_CONSTRAINTS (ci); 11183 if (!ci) 11184 return false; 11185 if (!ctx) 11186 { 11187 if (tree fc = DECL_FRIEND_CONTEXT (decl)) 11188 ctx = fc; 11189 else 11190 ctx = CP_DECL_CONTEXT (decl); 11191 } 11192 int depth = template_class_depth (ctx); 11193 if (depth == 0) 11194 return false; 11195 return for_each_template_parm (ci, template_parm_outer_level, 11196 &depth, NULL, /*nondeduced*/true); 11197 } 11198 11199 /* Returns TRUE iff INST is an instantiation we don't need to do in an 11200 ill-formed translation unit, i.e. a variable or function that isn't 11201 usable in a constant expression. */ 11202 11203 static inline bool 11204 neglectable_inst_p (tree d) 11205 { 11206 return (d && DECL_P (d) 11207 && !undeduced_auto_decl (d) 11208 && !(TREE_CODE (d) == FUNCTION_DECL 11209 ? FNDECL_MANIFESTLY_CONST_EVALUATED (d) 11210 : decl_maybe_constant_var_p (d))); 11211 } 11212 11213 /* Returns TRUE iff we should refuse to instantiate DECL because it's 11214 neglectable and instantiated from within an erroneous instantiation. */ 11215 11216 static bool 11217 limit_bad_template_recursion (tree decl) 11218 { 11219 struct tinst_level *lev = current_tinst_level; 11220 int errs = errorcount + sorrycount; 11221 if (errs == 0 || !neglectable_inst_p (decl)) 11222 return false; 11223 11224 /* Avoid instantiating members of an ill-formed class. */ 11225 bool refuse 11226 = (DECL_CLASS_SCOPE_P (decl) 11227 && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl))); 11228 11229 if (!refuse) 11230 { 11231 for (; lev; lev = lev->next) 11232 if (neglectable_inst_p (lev->maybe_get_node ())) 11233 break; 11234 refuse = (lev && errs > lev->errors); 11235 } 11236 11237 if (refuse) 11238 { 11239 /* Don't warn about it not being defined. */ 11240 suppress_warning (decl, OPT_Wunused); 11241 tree clone; 11242 FOR_EACH_CLONE (clone, decl) 11243 suppress_warning (clone, OPT_Wunused); 11244 } 11245 return refuse; 11246 } 11247 11248 static int tinst_depth; 11249 extern int max_tinst_depth; 11250 int depth_reached; 11251 11252 static GTY(()) struct tinst_level *last_error_tinst_level; 11253 11254 /* We're starting to instantiate D; record the template instantiation context 11255 at LOC for diagnostics and to restore it later. */ 11256 11257 bool 11258 push_tinst_level_loc (tree tldcl, tree targs, location_t loc) 11259 { 11260 struct tinst_level *new_level; 11261 11262 if (tinst_depth >= max_tinst_depth) 11263 { 11264 /* Tell error.cc not to try to instantiate any templates. */ 11265 at_eof = 3; 11266 fatal_error (input_location, 11267 "template instantiation depth exceeds maximum of %d" 11268 " (use %<-ftemplate-depth=%> to increase the maximum)", 11269 max_tinst_depth); 11270 return false; 11271 } 11272 11273 /* If the current instantiation caused problems, don't let it instantiate 11274 anything else. Do allow deduction substitution and decls usable in 11275 constant expressions. */ 11276 if (!targs && limit_bad_template_recursion (tldcl)) 11277 { 11278 /* Avoid no_linkage_errors and unused function (and all other) 11279 warnings for this decl. */ 11280 suppress_warning (tldcl); 11281 return false; 11282 } 11283 11284 /* When not -quiet, dump template instantiations other than functions, since 11285 announce_function will take care of those. */ 11286 if (!quiet_flag && !targs 11287 && TREE_CODE (tldcl) != TREE_LIST 11288 && TREE_CODE (tldcl) != FUNCTION_DECL) 11289 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS)); 11290 11291 new_level = tinst_level_freelist ().alloc (); 11292 new_level->tldcl = tldcl; 11293 new_level->targs = targs; 11294 new_level->locus = loc; 11295 new_level->errors = errorcount + sorrycount; 11296 new_level->next = NULL; 11297 new_level->refcount = 0; 11298 new_level->path = new_level->visible = nullptr; 11299 set_refcount_ptr (new_level->next, current_tinst_level); 11300 set_refcount_ptr (current_tinst_level, new_level); 11301 11302 ++tinst_depth; 11303 if (GATHER_STATISTICS && (tinst_depth > depth_reached)) 11304 depth_reached = tinst_depth; 11305 11306 return true; 11307 } 11308 11309 /* We're starting substitution of TMPL<ARGS>; record the template 11310 substitution context for diagnostics and to restore it later. */ 11311 11312 bool 11313 push_tinst_level (tree tmpl, tree args) 11314 { 11315 return push_tinst_level_loc (tmpl, args, input_location); 11316 } 11317 11318 /* We're starting to instantiate D; record INPUT_LOCATION and the 11319 template instantiation context for diagnostics and to restore it 11320 later. */ 11321 11322 bool 11323 push_tinst_level (tree d) 11324 { 11325 return push_tinst_level_loc (d, input_location); 11326 } 11327 11328 /* Likewise, but record LOC as the program location. */ 11329 11330 bool 11331 push_tinst_level_loc (tree d, location_t loc) 11332 { 11333 gcc_assert (TREE_CODE (d) != TREE_LIST); 11334 return push_tinst_level_loc (d, NULL, loc); 11335 } 11336 11337 /* We're done instantiating this template; return to the instantiation 11338 context. */ 11339 11340 void 11341 pop_tinst_level (void) 11342 { 11343 /* Restore the filename and line number stashed away when we started 11344 this instantiation. */ 11345 input_location = current_tinst_level->locus; 11346 set_refcount_ptr (current_tinst_level, current_tinst_level->next); 11347 --tinst_depth; 11348 } 11349 11350 /* We're instantiating a deferred template; restore the template 11351 instantiation context in which the instantiation was requested, which 11352 is one step out from LEVEL. Return the corresponding DECL or TYPE. */ 11353 11354 static tree 11355 reopen_tinst_level (struct tinst_level *level) 11356 { 11357 struct tinst_level *t; 11358 11359 tinst_depth = 0; 11360 for (t = level; t; t = t->next) 11361 ++tinst_depth; 11362 11363 set_refcount_ptr (current_tinst_level, level); 11364 pop_tinst_level (); 11365 if (current_tinst_level) 11366 current_tinst_level->errors = errorcount+sorrycount; 11367 return level->maybe_get_node (); 11368 } 11369 11370 /* Returns the TINST_LEVEL which gives the original instantiation 11371 context. */ 11372 11373 struct tinst_level * 11374 outermost_tinst_level (void) 11375 { 11376 struct tinst_level *level = current_tinst_level; 11377 if (level) 11378 while (level->next) 11379 level = level->next; 11380 return level; 11381 } 11382 11383 /* True iff T is a friend function declaration that is not itself a template 11384 and is not defined in a class template. */ 11385 11386 bool 11387 non_templated_friend_p (tree t) 11388 { 11389 if (t && TREE_CODE (t) == FUNCTION_DECL 11390 && DECL_UNIQUE_FRIEND_P (t)) 11391 { 11392 tree ti = DECL_TEMPLATE_INFO (t); 11393 if (!ti) 11394 return true; 11395 /* DECL_FRIEND_CONTEXT is set for a friend defined in class. */ 11396 if (DECL_FRIEND_CONTEXT (t)) 11397 return false; 11398 /* Non-templated friends in a class template are still represented with a 11399 TEMPLATE_DECL; check that its primary template is the befriending 11400 class. Note that DECL_PRIMARY_TEMPLATE is null for 11401 template <class T> friend A<T>::f(); */ 11402 tree tmpl = TI_TEMPLATE (ti); 11403 tree primary = DECL_PRIMARY_TEMPLATE (tmpl); 11404 return (primary && primary != tmpl); 11405 } 11406 else 11407 return false; 11408 } 11409 11410 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the 11411 vector of template arguments, as for tsubst. 11412 11413 Returns an appropriate tsubst'd friend declaration. */ 11414 11415 static tree 11416 tsubst_friend_function (tree decl, tree args) 11417 { 11418 tree new_friend; 11419 11420 if (TREE_CODE (decl) == FUNCTION_DECL 11421 && DECL_TEMPLATE_INSTANTIATION (decl) 11422 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) 11423 /* This was a friend declared with an explicit template 11424 argument list, e.g.: 11425 11426 friend void f<>(T); 11427 11428 to indicate that f was a template instantiation, not a new 11429 function declaration. Now, we have to figure out what 11430 instantiation of what template. */ 11431 { 11432 tree template_id, arglist, fns; 11433 tree new_args; 11434 tree tmpl; 11435 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type)); 11436 11437 /* Friend functions are looked up in the containing namespace scope. 11438 We must enter that scope, to avoid finding member functions of the 11439 current class with same name. */ 11440 push_nested_namespace (ns); 11441 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args, 11442 tf_warning_or_error, NULL_TREE); 11443 pop_nested_namespace (ns); 11444 arglist = tsubst (DECL_TI_ARGS (decl), args, 11445 tf_warning_or_error, NULL_TREE); 11446 template_id = lookup_template_function (fns, arglist); 11447 11448 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); 11449 tmpl = determine_specialization (template_id, new_friend, 11450 &new_args, 11451 /*need_member_template=*/0, 11452 TREE_VEC_LENGTH (args), 11453 tsk_none); 11454 return instantiate_template (tmpl, new_args, tf_error); 11455 } 11456 11457 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); 11458 if (new_friend == error_mark_node) 11459 return error_mark_node; 11460 11461 /* The NEW_FRIEND will look like an instantiation, to the 11462 compiler, but is not an instantiation from the point of view of 11463 the language. For example, we might have had: 11464 11465 template <class T> struct S { 11466 template <class U> friend void f(T, U); 11467 }; 11468 11469 Then, in S<int>, template <class U> void f(int, U) is not an 11470 instantiation of anything. */ 11471 11472 DECL_USE_TEMPLATE (new_friend) = 0; 11473 if (TREE_CODE (new_friend) == TEMPLATE_DECL) 11474 { 11475 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false; 11476 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; 11477 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend)) 11478 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl)); 11479 11480 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will 11481 match in decls_match. */ 11482 tree parms = DECL_TEMPLATE_PARMS (new_friend); 11483 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms); 11484 treqs = maybe_substitute_reqs_for (treqs, new_friend); 11485 if (treqs != TEMPLATE_PARMS_CONSTRAINTS (parms)) 11486 { 11487 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs; 11488 /* As well as each TEMPLATE_PARM_CONSTRAINTS. */ 11489 tsubst_each_template_parm_constraints (parms, args, 11490 tf_warning_or_error); 11491 } 11492 } 11493 11494 /* The mangled name for the NEW_FRIEND is incorrect. The function 11495 is not a template instantiation and should not be mangled like 11496 one. Therefore, we forget the mangling here; we'll recompute it 11497 later if we need it. */ 11498 if (TREE_CODE (new_friend) != TEMPLATE_DECL) 11499 { 11500 SET_DECL_RTL (new_friend, NULL); 11501 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE); 11502 } 11503 11504 if (DECL_NAMESPACE_SCOPE_P (new_friend)) 11505 { 11506 tree old_decl; 11507 tree ns; 11508 11509 /* We must save some information from NEW_FRIEND before calling 11510 duplicate decls since that function will free NEW_FRIEND if 11511 possible. */ 11512 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); 11513 tree new_friend_result_template_info = NULL_TREE; 11514 bool new_friend_is_defn = 11515 (new_friend_template_info 11516 && (DECL_INITIAL (DECL_TEMPLATE_RESULT 11517 (template_for_substitution (new_friend))) 11518 != NULL_TREE)); 11519 tree not_tmpl = new_friend; 11520 11521 if (TREE_CODE (new_friend) == TEMPLATE_DECL) 11522 { 11523 /* This declaration is a `primary' template. */ 11524 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; 11525 11526 not_tmpl = DECL_TEMPLATE_RESULT (new_friend); 11527 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl); 11528 } 11529 11530 /* We need to propagate module attachment for the new friend from the 11531 owner of this template. */ 11532 propagate_defining_module (new_friend, decl); 11533 11534 /* Inside pushdecl_namespace_level, we will push into the 11535 current namespace. However, the friend function should go 11536 into the namespace of the template. */ 11537 ns = decl_namespace_context (new_friend); 11538 push_nested_namespace (ns); 11539 old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true); 11540 pop_nested_namespace (ns); 11541 11542 if (old_decl == error_mark_node) 11543 return error_mark_node; 11544 11545 if (old_decl != new_friend) 11546 { 11547 /* This new friend declaration matched an existing 11548 declaration. For example, given: 11549 11550 template <class T> void f(T); 11551 template <class U> class C { 11552 template <class T> friend void f(T) {} 11553 }; 11554 11555 the friend declaration actually provides the definition 11556 of `f', once C has been instantiated for some type. So, 11557 old_decl will be the out-of-class template declaration, 11558 while new_friend is the in-class definition. 11559 11560 But, if `f' was called before this point, the 11561 instantiation of `f' will have DECL_TI_ARGS corresponding 11562 to `T' but not to `U', references to which might appear 11563 in the definition of `f'. Previously, the most general 11564 template for an instantiation of `f' was the out-of-class 11565 version; now it is the in-class version. Therefore, we 11566 run through all specialization of `f', adding to their 11567 DECL_TI_ARGS appropriately. In particular, they need a 11568 new set of outer arguments, corresponding to the 11569 arguments for this class instantiation. 11570 11571 The same situation can arise with something like this: 11572 11573 friend void f(int); 11574 template <class T> class C { 11575 friend void f(T) {} 11576 }; 11577 11578 when `C<int>' is instantiated. Now, `f(int)' is defined 11579 in the class. */ 11580 11581 if (!new_friend_is_defn) 11582 /* On the other hand, if the in-class declaration does 11583 *not* provide a definition, then we don't want to alter 11584 existing definitions. We can just leave everything 11585 alone. */ 11586 ; 11587 else 11588 { 11589 tree old_template = most_general_template (old_decl); 11590 tree new_template = TI_TEMPLATE (new_friend_template_info); 11591 tree new_args = TI_ARGS (new_friend_template_info); 11592 11593 /* Overwrite whatever template info was there before, if 11594 any, with the new template information pertaining to 11595 the declaration. */ 11596 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; 11597 11598 if (TREE_CODE (old_decl) != TEMPLATE_DECL) 11599 { 11600 /* We should have called reregister_specialization in 11601 duplicate_decls. */ 11602 gcc_assert (retrieve_specialization (new_template, 11603 new_args, 0) 11604 == old_decl); 11605 11606 /* Instantiate it if the global has already been used. */ 11607 if (DECL_ODR_USED (old_decl)) 11608 instantiate_decl (old_decl, /*defer_ok=*/true, 11609 /*expl_inst_class_mem_p=*/false); 11610 } 11611 else 11612 { 11613 tree t; 11614 11615 /* Indicate that the old function template is a partial 11616 instantiation. */ 11617 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) 11618 = new_friend_result_template_info; 11619 11620 gcc_assert (new_template 11621 == most_general_template (new_template)); 11622 gcc_assert (new_template != old_decl); 11623 11624 /* Reassign any specializations already in the hash table 11625 to the new more general template, and add the 11626 additional template args. */ 11627 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_template); 11628 t != NULL_TREE; 11629 t = TREE_CHAIN (t)) 11630 { 11631 tree spec = TREE_VALUE (t); 11632 spec_entry elt; 11633 11634 elt.tmpl = old_decl; 11635 elt.args = DECL_TI_ARGS (spec); 11636 elt.spec = NULL_TREE; 11637 11638 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (DECL_TI_ARGS (spec)) 11639 && !is_specialization_of_friend (spec, new_template)) 11640 continue; 11641 11642 decl_specializations->remove_elt (&elt); 11643 11644 tree& spec_args = DECL_TI_ARGS (spec); 11645 spec_args = add_outermost_template_args 11646 (new_args, INNERMOST_TEMPLATE_ARGS (spec_args)); 11647 11648 register_specialization 11649 (spec, new_template, spec_args, true, 0); 11650 11651 } 11652 DECL_TEMPLATE_INSTANTIATIONS (old_template) = NULL_TREE; 11653 } 11654 } 11655 11656 /* The information from NEW_FRIEND has been merged into OLD_DECL 11657 by duplicate_decls. */ 11658 new_friend = old_decl; 11659 } 11660 11661 /* We've just introduced a namespace-scope function in the purview 11662 without necessarily having opened the enclosing namespace, so 11663 make sure the namespace is in the purview now too. */ 11664 if (modules_p () 11665 && DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (new_friend)) 11666 && TREE_CODE (DECL_CONTEXT (new_friend)) == NAMESPACE_DECL) 11667 DECL_MODULE_PURVIEW_P (DECL_CONTEXT (new_friend)) = true; 11668 } 11669 else 11670 { 11671 tree context = DECL_CONTEXT (new_friend); 11672 bool dependent_p; 11673 11674 /* In the code 11675 template <class T> class C { 11676 template <class U> friend void C1<U>::f (); // case 1 11677 friend void C2<T>::f (); // case 2 11678 }; 11679 we only need to make sure CONTEXT is a complete type for 11680 case 2. To distinguish between the two cases, we note that 11681 CONTEXT of case 1 remains dependent type after tsubst while 11682 this isn't true for case 2. */ 11683 ++processing_template_decl; 11684 dependent_p = dependent_type_p (context); 11685 --processing_template_decl; 11686 11687 if (!dependent_p 11688 && !complete_type_or_else (context, NULL_TREE)) 11689 return error_mark_node; 11690 11691 if (COMPLETE_TYPE_P (context)) 11692 { 11693 tree fn = new_friend; 11694 /* do_friend adds the TEMPLATE_DECL for any member friend 11695 template even if it isn't a member template, i.e. 11696 template <class T> friend A<T>::f(); 11697 Look through it in that case. */ 11698 if (TREE_CODE (fn) == TEMPLATE_DECL 11699 && !PRIMARY_TEMPLATE_P (fn)) 11700 fn = DECL_TEMPLATE_RESULT (fn); 11701 /* Check to see that the declaration is really present, and, 11702 possibly obtain an improved declaration. */ 11703 fn = check_classfn (context, fn, NULL_TREE); 11704 11705 if (fn) 11706 new_friend = fn; 11707 } 11708 } 11709 11710 return new_friend; 11711 } 11712 11713 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of 11714 template arguments, as for tsubst. 11715 11716 Returns an appropriate tsubst'd friend type or error_mark_node on 11717 failure. */ 11718 11719 static tree 11720 tsubst_friend_class (tree friend_tmpl, tree args) 11721 { 11722 tree tmpl; 11723 11724 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl)) 11725 { 11726 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE); 11727 return TREE_TYPE (tmpl); 11728 } 11729 11730 tree context = CP_DECL_CONTEXT (friend_tmpl); 11731 if (TREE_CODE (context) == NAMESPACE_DECL) 11732 push_nested_namespace (context); 11733 else 11734 { 11735 context = tsubst (context, args, tf_error, NULL_TREE); 11736 push_nested_class (context); 11737 } 11738 11739 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE, 11740 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND); 11741 11742 if (!tmpl) 11743 /* If we didn't find by name lookup, the type may still exist but as a 11744 'hidden' import; we should check for this too to avoid accidentally 11745 instantiating a duplicate. */ 11746 tmpl = lookup_imported_hidden_friend (friend_tmpl); 11747 11748 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) 11749 { 11750 /* The friend template has already been declared. Just 11751 check to see that the declarations match, and install any new 11752 default parameters. We must tsubst the default parameters, 11753 of course. We only need the innermost template parameters 11754 because that is all that redeclare_class_template will look 11755 at. */ 11756 11757 if (modules_p ()) 11758 /* Check that the existing declaration's module attachment is 11759 compatible with the attachment of the friend template. */ 11760 module_may_redeclare (tmpl, friend_tmpl); 11761 11762 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) 11763 > TMPL_ARGS_DEPTH (args)) 11764 { 11765 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), 11766 args, tf_warning_or_error); 11767 tsubst_each_template_parm_constraints (parms, args, 11768 tf_warning_or_error); 11769 location_t saved_input_location = input_location; 11770 input_location = DECL_SOURCE_LOCATION (friend_tmpl); 11771 tree cons = get_constraints (friend_tmpl); 11772 ++processing_template_decl; 11773 cons = tsubst_constraint_info (cons, args, tf_warning_or_error, 11774 DECL_FRIEND_CONTEXT (friend_tmpl)); 11775 --processing_template_decl; 11776 redeclare_class_template (TREE_TYPE (tmpl), parms, cons); 11777 input_location = saved_input_location; 11778 } 11779 } 11780 else 11781 { 11782 /* The friend template has not already been declared. In this 11783 case, the instantiation of the template class will cause the 11784 injection of this template into the namespace scope. */ 11785 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE); 11786 11787 if (tmpl != error_mark_node) 11788 { 11789 /* The new TMPL is not an instantiation of anything, so we 11790 forget its origins. It is also not a specialization of 11791 anything. We don't reset CLASSTYPE_TI_TEMPLATE 11792 for the new type because that is supposed to be the 11793 corresponding template decl, i.e., TMPL. */ 11794 if (modules_p ()) 11795 { 11796 spec_entry elt; 11797 elt.tmpl = friend_tmpl; 11798 elt.args = CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)); 11799 elt.spec = TREE_TYPE (tmpl); 11800 type_specializations->remove_elt (&elt); 11801 } 11802 11803 DECL_USE_TEMPLATE (tmpl) = 0; 11804 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; 11805 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; 11806 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)) 11807 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))); 11808 11809 /* Substitute into and set the constraints on the new declaration. */ 11810 if (tree ci = get_constraints (friend_tmpl)) 11811 { 11812 ++processing_template_decl; 11813 ci = tsubst_constraint_info (ci, args, tf_warning_or_error, 11814 DECL_FRIEND_CONTEXT (friend_tmpl)); 11815 --processing_template_decl; 11816 set_constraints (tmpl, ci); 11817 tsubst_each_template_parm_constraints (DECL_TEMPLATE_PARMS (tmpl), 11818 args, tf_warning_or_error); 11819 } 11820 11821 /* We need to propagate the attachment of the original template to the 11822 newly instantiated template type. */ 11823 propagate_defining_module (tmpl, friend_tmpl); 11824 11825 /* Inject this template into the enclosing namspace scope. */ 11826 tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true); 11827 } 11828 } 11829 11830 if (TREE_CODE (context) == NAMESPACE_DECL) 11831 pop_nested_namespace (context); 11832 else 11833 pop_nested_class (); 11834 11835 return TREE_TYPE (tmpl); 11836 } 11837 11838 /* Returns zero if TYPE cannot be completed later due to circularity. 11839 Otherwise returns one. */ 11840 11841 static int 11842 can_complete_type_without_circularity (tree type) 11843 { 11844 if (type == NULL_TREE || type == error_mark_node) 11845 return 0; 11846 else if (COMPLETE_TYPE_P (type)) 11847 return 1; 11848 else if (TREE_CODE (type) == ARRAY_TYPE) 11849 return can_complete_type_without_circularity (TREE_TYPE (type)); 11850 else if (CLASS_TYPE_P (type) 11851 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type))) 11852 return 0; 11853 else 11854 return 1; 11855 } 11856 11857 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree, 11858 tsubst_flags_t, tree); 11859 11860 /* Instantiate the contract statement. */ 11861 11862 static tree 11863 tsubst_contract (tree decl, tree t, tree args, tsubst_flags_t complain, 11864 tree in_decl) 11865 { 11866 tree type = decl ? TREE_TYPE (TREE_TYPE (decl)) : NULL_TREE; 11867 bool auto_p = type_uses_auto (type); 11868 11869 tree r = copy_node (t); 11870 11871 /* Rebuild the result variable. */ 11872 if (type && POSTCONDITION_P (t) && POSTCONDITION_IDENTIFIER (t)) 11873 { 11874 tree oldvar = POSTCONDITION_IDENTIFIER (t); 11875 11876 tree newvar = copy_node (oldvar); 11877 TREE_TYPE (newvar) = type; 11878 DECL_CONTEXT (newvar) = decl; 11879 POSTCONDITION_IDENTIFIER (r) = newvar; 11880 11881 /* Make sure the postcondition is valid. */ 11882 location_t loc = DECL_SOURCE_LOCATION (oldvar); 11883 if (!auto_p) 11884 if (!check_postcondition_result (decl, type, loc)) 11885 return invalidate_contract (r); 11886 11887 /* Make the variable available for lookup. */ 11888 register_local_specialization (newvar, oldvar); 11889 } 11890 11891 /* Instantiate the condition. If the return type is undeduced, process 11892 the expression as if inside a template to avoid spurious type errors. */ 11893 if (auto_p) 11894 ++processing_template_decl; 11895 ++processing_contract_condition; 11896 CONTRACT_CONDITION (r) 11897 = tsubst_expr (CONTRACT_CONDITION (t), args, complain, in_decl); 11898 --processing_contract_condition; 11899 if (auto_p) 11900 --processing_template_decl; 11901 11902 /* And the comment. */ 11903 CONTRACT_COMMENT (r) 11904 = tsubst_expr (CONTRACT_COMMENT (r), args, complain, in_decl); 11905 11906 return r; 11907 } 11908 11909 /* Update T by instantiating its contract attribute. */ 11910 11911 static void 11912 tsubst_contract_attribute (tree decl, tree t, tree args, 11913 tsubst_flags_t complain, tree in_decl) 11914 { 11915 /* For non-specializations, adjust the current declaration to the most general 11916 version of in_decl. Because we defer the instantiation of contracts as long 11917 as possible, they are still written in terms of the parameters (and return 11918 type) of the most general template. */ 11919 tree tmpl = DECL_TI_TEMPLATE (in_decl); 11920 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl)) 11921 in_decl = DECL_TEMPLATE_RESULT (most_general_template (in_decl)); 11922 local_specialization_stack specs (lss_copy); 11923 register_parameter_specializations (in_decl, decl); 11924 11925 /* Get the contract to be instantiated. */ 11926 tree contract = CONTRACT_STATEMENT (t); 11927 11928 /* Use the complete set of template arguments for instantiation. The 11929 contract may not have been instantiated and still refer to outer levels 11930 of template parameters. */ 11931 args = DECL_TI_ARGS (decl); 11932 11933 /* For member functions, make this available for semantic analysis. */ 11934 tree save_ccp = current_class_ptr; 11935 tree save_ccr = current_class_ref; 11936 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl)) 11937 { 11938 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); 11939 tree this_type = TREE_TYPE (TREE_VALUE (arg_types)); 11940 inject_this_parameter (this_type, cp_type_quals (this_type)); 11941 } 11942 11943 contract = tsubst_contract (decl, contract, args, complain, in_decl); 11944 11945 current_class_ptr = save_ccp; 11946 current_class_ref = save_ccr; 11947 11948 /* Rebuild the attribute. */ 11949 TREE_VALUE (t) = build_tree_list (NULL_TREE, contract); 11950 } 11951 11952 /* Rebuild the attribute list for DECL, substituting into contracts 11953 as needed. */ 11954 11955 void 11956 tsubst_contract_attributes (tree decl, tree args, tsubst_flags_t complain, tree in_decl) 11957 { 11958 tree list = copy_list (DECL_ATTRIBUTES (decl)); 11959 for (tree attr = list; attr; attr = CONTRACT_CHAIN (attr)) 11960 { 11961 if (cxx_contract_attribute_p (attr)) 11962 tsubst_contract_attribute (decl, attr, args, complain, in_decl); 11963 } 11964 DECL_ATTRIBUTES (decl) = list; 11965 } 11966 11967 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either 11968 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */ 11969 11970 static tree 11971 tsubst_attribute (tree t, tree *decl_p, tree args, 11972 tsubst_flags_t complain, tree in_decl) 11973 { 11974 gcc_assert (ATTR_IS_DEPENDENT (t)); 11975 11976 /* Note that contract attributes are never substituted from this function. 11977 Their instantiation is triggered by regenerate_from_template_decl when 11978 we instantiate the body of the function. */ 11979 11980 tree val = TREE_VALUE (t); 11981 if (val == NULL_TREE) 11982 /* Nothing to do. */; 11983 else if ((flag_openmp || flag_openmp_simd) 11984 && is_attribute_p ("omp declare simd", 11985 get_attribute_name (t))) 11986 { 11987 tree clauses = TREE_VALUE (val); 11988 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args, 11989 complain, in_decl); 11990 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses); 11991 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD); 11992 tree parms = DECL_ARGUMENTS (*decl_p); 11993 clauses 11994 = c_omp_declare_simd_clauses_to_numbers (parms, clauses); 11995 if (clauses) 11996 val = build_tree_list (NULL_TREE, clauses); 11997 else 11998 val = NULL_TREE; 11999 } 12000 else if (flag_openmp 12001 && is_attribute_p ("omp declare variant base", 12002 get_attribute_name (t))) 12003 { 12004 ++cp_unevaluated_operand; 12005 tree varid = tsubst_expr (TREE_PURPOSE (val), args, complain, in_decl); 12006 --cp_unevaluated_operand; 12007 tree chain = TREE_CHAIN (val); 12008 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain)); 12009 tree ctx = copy_list (TREE_VALUE (val)); 12010 for (tree tss = ctx; tss; tss = TREE_CHAIN (tss)) 12011 { 12012 enum omp_tss_code set = OMP_TSS_CODE (tss); 12013 tree selectors = NULL_TREE; 12014 for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts; 12015 ts = TREE_CHAIN (ts)) 12016 { 12017 tree properties = NULL_TREE; 12018 tree scoreval = NULL_TREE; 12019 /* FIXME: The body of this loop should really be dispatching 12020 according to omp_ts_map[OMP_TS_CODE (TS)].tp_type instead 12021 of having hard-wired knowledge of specific selectors. */ 12022 if (OMP_TS_CODE (ts) == OMP_TRAIT_CONSTRUCT_SIMD 12023 && set == OMP_TRAIT_SET_CONSTRUCT) 12024 { 12025 tree clauses = OMP_TS_PROPERTIES (ts); 12026 clauses = tsubst_omp_clauses (clauses, 12027 C_ORT_OMP_DECLARE_SIMD, args, 12028 complain, in_decl); 12029 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses); 12030 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD); 12031 properties = clauses; 12032 } 12033 else 12034 { 12035 tree v = OMP_TS_SCORE (ts); 12036 if (v) 12037 { 12038 v = tsubst_expr (v, args, complain, in_decl); 12039 v = fold_non_dependent_expr (v); 12040 if (!INTEGRAL_TYPE_P (TREE_TYPE (v)) 12041 || TREE_CODE (v) != INTEGER_CST) 12042 { 12043 location_t loc 12044 = cp_expr_loc_or_loc (OMP_TS_SCORE (ts), 12045 match_loc); 12046 error_at (loc, "score argument must be " 12047 "constant integer expression"); 12048 return NULL_TREE; 12049 } 12050 else if (tree_int_cst_sgn (v) < 0) 12051 { 12052 location_t loc 12053 = cp_expr_loc_or_loc (OMP_TS_SCORE (ts), 12054 match_loc); 12055 error_at (loc, "score argument must be " 12056 "non-negative"); 12057 return NULL_TREE; 12058 } 12059 scoreval = v; 12060 } 12061 properties = copy_list (OMP_TS_PROPERTIES (ts)); 12062 for (tree p = properties; p; p = TREE_CHAIN (p)) 12063 if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE) 12064 continue; 12065 else if (OMP_TP_VALUE (p)) 12066 { 12067 bool allow_string 12068 = (OMP_TS_CODE (ts) != OMP_TRAIT_USER_CONDITION 12069 || set != OMP_TRAIT_SET_USER); 12070 tree v = OMP_TP_VALUE (p); 12071 if (TREE_CODE (v) == STRING_CST && allow_string) 12072 continue; 12073 v = tsubst_expr (v, args, complain, in_decl); 12074 v = fold_non_dependent_expr (v); 12075 if (!INTEGRAL_TYPE_P (TREE_TYPE (v)) 12076 || !tree_fits_shwi_p (v)) 12077 { 12078 location_t loc 12079 = cp_expr_loc_or_loc (OMP_TP_VALUE (p), 12080 match_loc); 12081 if (allow_string) 12082 error_at (loc, "property must be constant " 12083 "integer expression or string " 12084 "literal"); 12085 else 12086 error_at (loc, "property must be constant " 12087 "integer expression"); 12088 return NULL_TREE; 12089 } 12090 OMP_TP_VALUE (p) = v; 12091 } 12092 } 12093 selectors = make_trait_selector (OMP_TS_CODE (ts), scoreval, 12094 properties, selectors); 12095 } 12096 OMP_TSS_TRAIT_SELECTORS (tss) = nreverse (selectors); 12097 } 12098 val = tree_cons (varid, ctx, chain); 12099 } 12100 /* If the first attribute argument is an identifier, don't 12101 pass it through tsubst. Attributes like mode, format, 12102 cleanup and several target specific attributes expect it 12103 unmodified. */ 12104 else if (attribute_takes_identifier_p (get_attribute_name (t))) 12105 { 12106 tree chain 12107 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl); 12108 if (chain != TREE_CHAIN (val)) 12109 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain); 12110 } 12111 else if (PACK_EXPANSION_P (val)) 12112 { 12113 /* An attribute pack expansion. */ 12114 tree purp = TREE_PURPOSE (t); 12115 tree pack = tsubst_pack_expansion (val, args, complain, in_decl); 12116 if (pack == error_mark_node) 12117 return error_mark_node; 12118 int len = TREE_VEC_LENGTH (pack); 12119 tree list = NULL_TREE; 12120 tree *q = &list; 12121 for (int i = 0; i < len; ++i) 12122 { 12123 tree elt = TREE_VEC_ELT (pack, i); 12124 *q = build_tree_list (purp, elt); 12125 q = &TREE_CHAIN (*q); 12126 } 12127 return list; 12128 } 12129 else 12130 val = tsubst_expr (val, args, complain, in_decl); 12131 12132 if (val == error_mark_node) 12133 return error_mark_node; 12134 if (val != TREE_VALUE (t)) 12135 return build_tree_list (TREE_PURPOSE (t), val); 12136 return t; 12137 } 12138 12139 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it 12140 unchanged or a new TREE_LIST chain. */ 12141 12142 static tree 12143 tsubst_attributes (tree attributes, tree args, 12144 tsubst_flags_t complain, tree in_decl) 12145 { 12146 tree last_dep = NULL_TREE; 12147 12148 for (tree t = attributes; t; t = TREE_CHAIN (t)) 12149 if (ATTR_IS_DEPENDENT (t)) 12150 { 12151 last_dep = t; 12152 attributes = copy_list (attributes); 12153 break; 12154 } 12155 12156 if (last_dep) 12157 for (tree *p = &attributes; *p; ) 12158 { 12159 tree t = *p; 12160 if (ATTR_IS_DEPENDENT (t)) 12161 { 12162 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl); 12163 if (subst != t) 12164 { 12165 *p = subst; 12166 while (*p) 12167 p = &TREE_CHAIN (*p); 12168 *p = TREE_CHAIN (t); 12169 continue; 12170 } 12171 } 12172 p = &TREE_CHAIN (*p); 12173 } 12174 12175 return attributes; 12176 } 12177 12178 /* Apply any attributes which had to be deferred until instantiation 12179 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes; 12180 ARGS, COMPLAIN, IN_DECL are as tsubst. Returns true normally, 12181 false on error. */ 12182 12183 static bool 12184 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags, 12185 tree args, tsubst_flags_t complain, tree in_decl) 12186 { 12187 tree t; 12188 tree *p; 12189 12190 if (attributes == NULL_TREE) 12191 return true; 12192 12193 if (DECL_P (*decl_p)) 12194 { 12195 if (TREE_TYPE (*decl_p) == error_mark_node) 12196 return false; 12197 p = &DECL_ATTRIBUTES (*decl_p); 12198 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical 12199 to our attributes parameter. */ 12200 gcc_assert (*p == attributes); 12201 } 12202 else 12203 { 12204 p = &TYPE_ATTRIBUTES (*decl_p); 12205 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in 12206 lookup_template_class_1, and should be preserved. */ 12207 gcc_assert (*p != attributes); 12208 while (*p) 12209 p = &TREE_CHAIN (*p); 12210 } 12211 12212 /* save_template_attributes puts the dependent attributes at the beginning of 12213 the list; find the non-dependent ones. */ 12214 for (t = attributes; t; t = TREE_CHAIN (t)) 12215 if (!ATTR_IS_DEPENDENT (t)) 12216 break; 12217 tree nondep = t; 12218 12219 /* Apply any non-dependent attributes. */ 12220 *p = nondep; 12221 12222 if (nondep == attributes) 12223 return true; 12224 12225 /* And then any dependent ones. */ 12226 tree late_attrs = NULL_TREE; 12227 tree *q = &late_attrs; 12228 for (t = attributes; t != nondep; t = TREE_CHAIN (t)) 12229 { 12230 *q = tsubst_attribute (t, decl_p, args, complain, in_decl); 12231 if (*q == error_mark_node) 12232 return false; 12233 if (*q == t) 12234 { 12235 *q = copy_node (t); 12236 TREE_CHAIN (*q) = NULL_TREE; 12237 } 12238 while (*q) 12239 q = &TREE_CHAIN (*q); 12240 } 12241 12242 /* cplus_decl_attributes can add some attributes implicitly. For templates, 12243 those attributes should have been added already when those templates were 12244 parsed, and shouldn't be added based on from which context they are 12245 first time instantiated. */ 12246 auto o1 = make_temp_override (current_optimize_pragma, NULL_TREE); 12247 auto o2 = make_temp_override (optimization_current_node, 12248 optimization_default_node); 12249 auto o3 = make_temp_override (current_target_pragma, NULL_TREE); 12250 auto o4 = make_temp_override (scope_chain->omp_declare_target_attribute, 12251 NULL); 12252 auto o5 = make_temp_override (scope_chain->omp_begin_assumes, NULL); 12253 auto o6 = make_temp_override (target_option_current_node, 12254 target_option_default_node); 12255 12256 cplus_decl_attributes (decl_p, late_attrs, attr_flags); 12257 12258 return true; 12259 } 12260 12261 /* The template TMPL is being instantiated with the template arguments TARGS. 12262 Perform the access checks that we deferred when parsing the template. */ 12263 12264 static void 12265 perform_instantiation_time_access_checks (tree tmpl, tree targs) 12266 { 12267 unsigned i; 12268 deferred_access_check *chk; 12269 12270 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL) 12271 return; 12272 12273 if (vec<deferred_access_check, va_gc> *access_checks 12274 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl))) 12275 FOR_EACH_VEC_ELT (*access_checks, i, chk) 12276 { 12277 tree decl = chk->decl; 12278 tree diag_decl = chk->diag_decl; 12279 tree type_scope = TREE_TYPE (chk->binfo); 12280 12281 if (uses_template_parms (type_scope)) 12282 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE); 12283 12284 /* Make access check error messages point to the location 12285 of the use of the typedef. */ 12286 iloc_sentinel ils (chk->loc); 12287 perform_or_defer_access_check (TYPE_BINFO (type_scope), 12288 decl, diag_decl, tf_warning_or_error); 12289 } 12290 } 12291 12292 tree 12293 instantiate_class_template (tree type) 12294 { 12295 auto_timevar tv (TV_TEMPLATE_INST); 12296 12297 tree templ, args, pattern, t, member; 12298 tree typedecl; 12299 tree pbinfo; 12300 tree base_list; 12301 unsigned int saved_maximum_field_alignment; 12302 tree fn_context; 12303 12304 if (type == error_mark_node) 12305 return error_mark_node; 12306 12307 if (COMPLETE_OR_OPEN_TYPE_P (type) 12308 || uses_template_parms (type)) 12309 return type; 12310 12311 /* Figure out which template is being instantiated. */ 12312 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); 12313 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL); 12314 12315 /* Mark the type as in the process of being defined. */ 12316 TYPE_BEING_DEFINED (type) = 1; 12317 12318 /* We may be in the middle of deferred access check. Disable 12319 it now. */ 12320 deferring_access_check_sentinel acs (dk_no_deferred); 12321 12322 /* Determine what specialization of the original template to 12323 instantiate. */ 12324 t = most_specialized_partial_spec (type, tf_warning_or_error); 12325 if (t == error_mark_node) 12326 return error_mark_node; 12327 else if (t) 12328 { 12329 /* This TYPE is actually an instantiation of a partial 12330 specialization. We replace the innermost set of ARGS with 12331 the arguments appropriate for substitution. For example, 12332 given: 12333 12334 template <class T> struct S {}; 12335 template <class T> struct S<T*> {}; 12336 12337 and supposing that we are instantiating S<int*>, ARGS will 12338 presently be {int*} -- but we need {int}. */ 12339 pattern = TREE_TYPE (TI_TEMPLATE (t)); 12340 args = TI_ARGS (t); 12341 } 12342 else 12343 { 12344 pattern = TREE_TYPE (templ); 12345 args = CLASSTYPE_TI_ARGS (type); 12346 } 12347 12348 /* If the template we're instantiating is incomplete, then clearly 12349 there's nothing we can do. */ 12350 if (!COMPLETE_TYPE_P (pattern)) 12351 { 12352 /* We can try again later. */ 12353 TYPE_BEING_DEFINED (type) = 0; 12354 return type; 12355 } 12356 12357 /* If we've recursively instantiated too many templates, stop. */ 12358 if (! push_tinst_level (type)) 12359 return type; 12360 12361 int saved_unevaluated_operand = cp_unevaluated_operand; 12362 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings; 12363 12364 fn_context = decl_function_context (TYPE_MAIN_DECL (type)); 12365 /* Also avoid push_to_top_level for a lambda in an NSDMI. */ 12366 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type)) 12367 fn_context = error_mark_node; 12368 if (!fn_context) 12369 push_to_top_level (); 12370 else 12371 { 12372 cp_unevaluated_operand = 0; 12373 c_inhibit_evaluation_warnings = 0; 12374 } 12375 12376 mark_template_arguments_used (templ, CLASSTYPE_TI_ARGS (type)); 12377 12378 /* Use #pragma pack from the template context. */ 12379 saved_maximum_field_alignment = maximum_field_alignment; 12380 maximum_field_alignment = TYPE_PRECISION (pattern); 12381 12382 SET_CLASSTYPE_INTERFACE_UNKNOWN (type); 12383 12384 /* Set the input location to the most specialized template definition. 12385 This is needed if tsubsting causes an error. */ 12386 typedecl = TYPE_MAIN_DECL (pattern); 12387 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) = 12388 DECL_SOURCE_LOCATION (typedecl); 12389 12390 set_instantiating_module (TYPE_NAME (type)); 12391 12392 TYPE_PACKED (type) = TYPE_PACKED (pattern); 12393 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern)); 12394 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); 12395 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern); 12396 if (ANON_AGGR_TYPE_P (pattern)) 12397 SET_ANON_AGGR_TYPE_P (type); 12398 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern)) 12399 { 12400 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1; 12401 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern); 12402 /* Adjust visibility for template arguments. */ 12403 determine_visibility (TYPE_MAIN_DECL (type)); 12404 } 12405 if (CLASS_TYPE_P (type)) 12406 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern); 12407 12408 pbinfo = TYPE_BINFO (pattern); 12409 12410 /* We should never instantiate a nested class before its enclosing 12411 class; we need to look up the nested class by name before we can 12412 instantiate it, and that lookup should instantiate the enclosing 12413 class. */ 12414 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern)) 12415 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type))); 12416 12417 base_list = NULL_TREE; 12418 /* Defer access checking while we substitute into the types named in 12419 the base-clause. */ 12420 push_deferring_access_checks (dk_deferred); 12421 if (BINFO_N_BASE_BINFOS (pbinfo)) 12422 { 12423 tree pbase_binfo; 12424 int i; 12425 12426 /* Substitute into each of the bases to determine the actual 12427 basetypes. */ 12428 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++) 12429 { 12430 tree base; 12431 tree access = BINFO_BASE_ACCESS (pbinfo, i); 12432 tree expanded_bases = NULL_TREE; 12433 int idx, len = 1; 12434 12435 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo))) 12436 { 12437 expanded_bases = 12438 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo), 12439 args, tf_error, NULL_TREE); 12440 if (expanded_bases == error_mark_node) 12441 continue; 12442 12443 len = TREE_VEC_LENGTH (expanded_bases); 12444 } 12445 12446 for (idx = 0; idx < len; idx++) 12447 { 12448 if (expanded_bases) 12449 /* Extract the already-expanded base class. */ 12450 base = TREE_VEC_ELT (expanded_bases, idx); 12451 else 12452 /* Substitute to figure out the base class. */ 12453 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, 12454 NULL_TREE); 12455 12456 if (base == error_mark_node) 12457 continue; 12458 12459 base_list = tree_cons (access, base, base_list); 12460 if (BINFO_VIRTUAL_P (pbase_binfo)) 12461 TREE_TYPE (base_list) = integer_type_node; 12462 } 12463 } 12464 12465 /* The list is now in reverse order; correct that. */ 12466 base_list = nreverse (base_list); 12467 } 12468 /* Now call xref_basetypes to set up all the base-class 12469 information. */ 12470 xref_basetypes (type, base_list); 12471 12472 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern), 12473 (int) ATTR_FLAG_TYPE_IN_PLACE, 12474 args, tf_error, NULL_TREE); 12475 fixup_attribute_variants (type); 12476 12477 /* Now that our base classes are set up, enter the scope of the 12478 class, so that name lookups into base classes, etc. will work 12479 correctly. This is precisely analogous to what we do in 12480 begin_class_definition when defining an ordinary non-template 12481 class, except we also need to push the enclosing classes. */ 12482 push_nested_class (type); 12483 12484 /* Now check accessibility of the types named in its base-clause, 12485 relative to the scope of the class. */ 12486 pop_to_parent_deferring_access_checks (); 12487 12488 /* A vector to hold members marked with attribute used. */ 12489 auto_vec<tree> used; 12490 12491 /* Now members are processed in the order of declaration. */ 12492 for (member = CLASSTYPE_DECL_LIST (pattern); 12493 member; member = TREE_CHAIN (member)) 12494 { 12495 tree t = TREE_VALUE (member); 12496 12497 if (TREE_PURPOSE (member)) 12498 { 12499 if (TYPE_P (t)) 12500 { 12501 if (LAMBDA_TYPE_P (t)) 12502 /* A closure type for a lambda in an NSDMI or default argument. 12503 Ignore it; it will be regenerated when needed. */ 12504 continue; 12505 12506 /* If the member is a class template, we've 12507 already substituted its type. */ 12508 if (CLASS_TYPE_P (t) 12509 && CLASSTYPE_IS_TEMPLATE (t)) 12510 continue; 12511 12512 tree newtag = tsubst (t, args, tf_error, NULL_TREE); 12513 if (newtag == error_mark_node) 12514 continue; 12515 12516 if (TREE_CODE (newtag) != ENUMERAL_TYPE) 12517 { 12518 tree name = TYPE_IDENTIFIER (t); 12519 12520 /* Now, install the tag. We don't use pushtag 12521 because that does too much work -- creating an 12522 implicit typedef, which we've already done. */ 12523 set_identifier_type_value (name, TYPE_NAME (newtag)); 12524 maybe_add_class_template_decl_list (type, newtag, false); 12525 TREE_PUBLIC (TYPE_NAME (newtag)) = true; 12526 determine_visibility (TYPE_NAME (newtag)); 12527 } 12528 } 12529 else if (DECL_DECLARES_FUNCTION_P (t)) 12530 { 12531 tree r; 12532 12533 if (TREE_CODE (t) == TEMPLATE_DECL) 12534 ++processing_template_decl; 12535 r = tsubst (t, args, tf_error, NULL_TREE); 12536 if (TREE_CODE (t) == TEMPLATE_DECL) 12537 --processing_template_decl; 12538 12539 set_current_access_from_decl (r); 12540 finish_member_declaration (r); 12541 /* Instantiate members marked with attribute used. */ 12542 if (r != error_mark_node && DECL_PRESERVE_P (r)) 12543 used.safe_push (r); 12544 if (TREE_CODE (r) == FUNCTION_DECL 12545 && DECL_OMP_DECLARE_REDUCTION_P (r)) 12546 cp_check_omp_declare_reduction (r); 12547 } 12548 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t)) 12549 && LAMBDA_TYPE_P (TREE_TYPE (t))) 12550 /* A closure type for a lambda in an NSDMI or default argument. 12551 Ignore it; it will be regenerated when needed. */; 12552 else 12553 { 12554 /* Build new TYPE_FIELDS. */ 12555 if (TREE_CODE (t) == STATIC_ASSERT) 12556 tsubst_stmt (t, args, tf_warning_or_error, NULL_TREE); 12557 else if (TREE_CODE (t) != CONST_DECL) 12558 { 12559 tree r; 12560 tree vec = NULL_TREE; 12561 int len = 1; 12562 12563 gcc_checking_assert (TREE_CODE (t) != CONST_DECL); 12564 /* The file and line for this declaration, to 12565 assist in error message reporting. Since we 12566 called push_tinst_level above, we don't need to 12567 restore these. */ 12568 input_location = DECL_SOURCE_LOCATION (t); 12569 12570 if (TREE_CODE (t) == TEMPLATE_DECL) 12571 ++processing_template_decl; 12572 r = tsubst (t, args, tf_warning_or_error, NULL_TREE); 12573 if (TREE_CODE (t) == TEMPLATE_DECL) 12574 --processing_template_decl; 12575 12576 if (TREE_CODE (r) == TREE_VEC) 12577 { 12578 /* A capture pack became multiple fields. */ 12579 vec = r; 12580 len = TREE_VEC_LENGTH (vec); 12581 } 12582 12583 for (int i = 0; i < len; ++i) 12584 { 12585 if (vec) 12586 r = TREE_VEC_ELT (vec, i); 12587 if (VAR_P (r)) 12588 { 12589 /* In [temp.inst]: 12590 12591 [t]he initialization (and any associated 12592 side-effects) of a static data member does 12593 not occur unless the static data member is 12594 itself used in a way that requires the 12595 definition of the static data member to 12596 exist. 12597 12598 Therefore, we do not substitute into the 12599 initialized for the static data member here. */ 12600 finish_static_data_member_decl 12601 (r, 12602 /*init=*/NULL_TREE, 12603 /*init_const_expr_p=*/false, 12604 /*asmspec_tree=*/NULL_TREE, 12605 /*flags=*/0); 12606 /* Instantiate members marked with attribute used. */ 12607 if (r != error_mark_node && DECL_PRESERVE_P (r)) 12608 used.safe_push (r); 12609 } 12610 else if (TREE_CODE (r) == FIELD_DECL) 12611 { 12612 /* Determine whether R has a valid type and can be 12613 completed later. If R is invalid, then its type 12614 is replaced by error_mark_node. */ 12615 tree rtype = TREE_TYPE (r); 12616 if (can_complete_type_without_circularity (rtype)) 12617 complete_type (rtype); 12618 12619 if (!complete_or_array_type_p (rtype)) 12620 { 12621 /* If R's type couldn't be completed and 12622 it isn't a flexible array member (whose 12623 type is incomplete by definition) give 12624 an error. */ 12625 cxx_incomplete_type_error (r, rtype); 12626 TREE_TYPE (r) = error_mark_node; 12627 } 12628 else if (TREE_CODE (rtype) == ARRAY_TYPE 12629 && TYPE_DOMAIN (rtype) == NULL_TREE 12630 && (TREE_CODE (type) == UNION_TYPE 12631 || TREE_CODE (type) == QUAL_UNION_TYPE)) 12632 { 12633 error ("flexible array member %qD in union", r); 12634 TREE_TYPE (r) = error_mark_node; 12635 } 12636 else if (!verify_type_context (input_location, 12637 TCTX_FIELD, rtype)) 12638 TREE_TYPE (r) = error_mark_node; 12639 } 12640 12641 /* If it is a TYPE_DECL for a class-scoped 12642 ENUMERAL_TYPE, such a thing will already have 12643 been added to the field list by tsubst_enum 12644 in finish_member_declaration case above. */ 12645 if (!(TREE_CODE (r) == TYPE_DECL 12646 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE 12647 && DECL_ARTIFICIAL (r))) 12648 { 12649 set_current_access_from_decl (r); 12650 finish_member_declaration (r); 12651 } 12652 } 12653 } 12654 } 12655 } 12656 else 12657 { 12658 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t) 12659 || DECL_TEMPLATE_TEMPLATE_PARM_P (t)) 12660 { 12661 /* Build new CLASSTYPE_FRIEND_CLASSES. */ 12662 12663 tree friend_type = t; 12664 if (TREE_CODE (friend_type) == TEMPLATE_DECL) 12665 { 12666 /* template <class T> friend class C; */ 12667 friend_type = tsubst_friend_class (friend_type, args); 12668 } 12669 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE) 12670 { 12671 /* template <class T> friend class C::D; */ 12672 friend_type = tsubst (friend_type, args, 12673 tf_warning_or_error, NULL_TREE); 12674 if (TREE_CODE (friend_type) == TEMPLATE_DECL) 12675 friend_type = TREE_TYPE (friend_type); 12676 } 12677 else if (TREE_CODE (friend_type) == TYPENAME_TYPE 12678 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM) 12679 { 12680 /* This could be either 12681 12682 friend class T::C; 12683 12684 when dependent_type_p is false or 12685 12686 template <class U> friend class T::C; 12687 12688 otherwise. */ 12689 /* Bump processing_template_decl in case this is something like 12690 template <class T> friend struct A<T>::B. */ 12691 ++processing_template_decl; 12692 friend_type = tsubst (friend_type, args, 12693 tf_warning_or_error, NULL_TREE); 12694 --processing_template_decl; 12695 } 12696 else if (uses_template_parms (friend_type)) 12697 /* friend class C<T>; */ 12698 friend_type = tsubst (friend_type, args, 12699 tf_warning_or_error, NULL_TREE); 12700 12701 /* Otherwise it's 12702 12703 friend class C; 12704 12705 where C is already declared or 12706 12707 friend class C<int>; 12708 12709 We don't have to do anything in these cases. */ 12710 12711 if (friend_type != error_mark_node) 12712 make_friend_class (type, friend_type, /*complain=*/false); 12713 } 12714 else 12715 { 12716 /* Build new DECL_FRIENDLIST. */ 12717 tree r; 12718 12719 /* The file and line for this declaration, to 12720 assist in error message reporting. Since we 12721 called push_tinst_level above, we don't need to 12722 restore these. */ 12723 input_location = DECL_SOURCE_LOCATION (t); 12724 12725 if (TREE_CODE (t) == TEMPLATE_DECL) 12726 { 12727 ++processing_template_decl; 12728 push_deferring_access_checks (dk_no_check); 12729 } 12730 12731 r = tsubst_friend_function (t, args); 12732 add_friend (type, r, /*complain=*/false); 12733 if (TREE_CODE (t) == TEMPLATE_DECL) 12734 { 12735 pop_deferring_access_checks (); 12736 --processing_template_decl; 12737 } 12738 } 12739 } 12740 } 12741 12742 if (fn_context) 12743 { 12744 /* Restore these before substituting into the lambda capture 12745 initializers. */ 12746 cp_unevaluated_operand = saved_unevaluated_operand; 12747 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings; 12748 } 12749 12750 /* Set the file and line number information to whatever is given for 12751 the class itself. This puts error messages involving generated 12752 implicit functions at a predictable point, and the same point 12753 that would be used for non-template classes. */ 12754 input_location = DECL_SOURCE_LOCATION (typedecl); 12755 12756 unreverse_member_declarations (type); 12757 finish_struct_1 (type); 12758 TYPE_BEING_DEFINED (type) = 0; 12759 12760 /* Remember if instantiating this class ran into errors, so we can avoid 12761 instantiating member functions in limit_bad_template_recursion. We set 12762 this flag even if the problem was in another instantiation triggered by 12763 this one, as that will likely also cause trouble for member functions. */ 12764 if (errorcount + sorrycount > current_tinst_level->errors) 12765 CLASSTYPE_ERRONEOUS (type) = true; 12766 12767 /* We don't instantiate default arguments for member functions. 14.7.1: 12768 12769 The implicit instantiation of a class template specialization causes 12770 the implicit instantiation of the declarations, but not of the 12771 definitions or default arguments, of the class member functions, 12772 member classes, static data members and member templates.... */ 12773 12774 perform_instantiation_time_access_checks (pattern, args); 12775 perform_deferred_access_checks (tf_warning_or_error); 12776 12777 /* Now that we've gone through all the members, instantiate those 12778 marked with attribute used. We must do this in the context of 12779 the class -- not the context we pushed from, as that might be 12780 inside a template and change the behaviour of mark_used. */ 12781 for (tree x : used) 12782 mark_used (x); 12783 12784 pop_nested_class (); 12785 maximum_field_alignment = saved_maximum_field_alignment; 12786 if (!fn_context) 12787 pop_from_top_level (); 12788 pop_tinst_level (); 12789 12790 /* The vtable for a template class can be emitted in any translation 12791 unit in which the class is instantiated. When there is no key 12792 method, however, finish_struct_1 will already have added TYPE to 12793 the keyed_classes. */ 12794 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type)) 12795 vec_safe_push (keyed_classes, type); 12796 12797 return type; 12798 } 12799 12800 tree 12801 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl) 12802 { 12803 tree r; 12804 12805 if (!t) 12806 r = t; 12807 else if (TYPE_P (t)) 12808 r = tsubst (t, args, complain, in_decl); 12809 else 12810 { 12811 if (!(complain & tf_warning)) 12812 ++c_inhibit_evaluation_warnings; 12813 r = tsubst_expr (t, args, complain, in_decl); 12814 if (!(complain & tf_warning)) 12815 --c_inhibit_evaluation_warnings; 12816 } 12817 12818 return r; 12819 } 12820 12821 /* Given a function parameter pack TMPL_PARM and some function parameters 12822 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them 12823 and set *SPEC_P to point at the next point in the list. */ 12824 12825 tree 12826 extract_fnparm_pack (tree tmpl_parm, tree *spec_p) 12827 { 12828 /* Collect all of the extra "packed" parameters into an 12829 argument pack. */ 12830 tree argpack; 12831 tree spec_parm = *spec_p; 12832 int len; 12833 12834 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm)) 12835 if (tmpl_parm 12836 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm)) 12837 break; 12838 12839 spec_parm = *spec_p; 12840 if (len == 1 && DECL_PACK_P (spec_parm)) 12841 { 12842 /* The instantiation is still a parameter pack; don't wrap it in a 12843 NONTYPE_ARGUMENT_PACK. */ 12844 argpack = spec_parm; 12845 spec_parm = DECL_CHAIN (spec_parm); 12846 } 12847 else 12848 { 12849 /* Fill in PARMVEC with all of the parameters. */ 12850 tree parmvec = make_tree_vec (len); 12851 argpack = make_node (NONTYPE_ARGUMENT_PACK); 12852 for (int i = 0; i < len; i++) 12853 { 12854 tree elt = spec_parm; 12855 if (DECL_PACK_P (elt)) 12856 elt = make_pack_expansion (elt); 12857 TREE_VEC_ELT (parmvec, i) = elt; 12858 spec_parm = DECL_CHAIN (spec_parm); 12859 } 12860 12861 /* Build the argument packs. */ 12862 ARGUMENT_PACK_ARGS (argpack) = parmvec; 12863 } 12864 *spec_p = spec_parm; 12865 12866 return argpack; 12867 } 12868 12869 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a 12870 NONTYPE_ARGUMENT_PACK. */ 12871 12872 static tree 12873 make_fnparm_pack (tree spec_parm) 12874 { 12875 return extract_fnparm_pack (NULL_TREE, &spec_parm); 12876 } 12877 12878 /* Return 1 if the Ith element of the argument pack ARG_PACK is a 12879 pack expansion with no extra args, 2 if it has extra args, or 0 12880 if it is not a pack expansion. */ 12881 12882 static int 12883 argument_pack_element_is_expansion_p (tree arg_pack, int i) 12884 { 12885 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT) 12886 /* We're being called before this happens in tsubst_pack_expansion. */ 12887 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack); 12888 tree vec = ARGUMENT_PACK_ARGS (arg_pack); 12889 if (i >= TREE_VEC_LENGTH (vec)) 12890 return 0; 12891 tree elt = TREE_VEC_ELT (vec, i); 12892 if (DECL_P (elt)) 12893 /* A decl pack is itself an expansion. */ 12894 elt = TREE_TYPE (elt); 12895 if (!PACK_EXPANSION_P (elt)) 12896 return 0; 12897 if (PACK_EXPANSION_EXTRA_ARGS (elt)) 12898 return 2; 12899 return 1; 12900 } 12901 12902 12903 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */ 12904 12905 static tree 12906 make_argument_pack_select (tree arg_pack, unsigned index) 12907 { 12908 tree aps = make_node (ARGUMENT_PACK_SELECT); 12909 12910 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack; 12911 ARGUMENT_PACK_SELECT_INDEX (aps) = index; 12912 12913 return aps; 12914 } 12915 12916 /* This is a subroutine of tsubst_pack_expansion. 12917 12918 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS 12919 mechanism to store the (non complete list of) arguments of the 12920 substitution and return a non substituted pack expansion, in order 12921 to wait for when we have enough arguments to really perform the 12922 substitution. */ 12923 12924 static bool 12925 use_pack_expansion_extra_args_p (tree t, 12926 tree parm_packs, 12927 int arg_pack_len, 12928 bool has_empty_arg) 12929 { 12930 if (has_empty_arg 12931 && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t)) 12932 return true; 12933 12934 /* If one pack has an expansion and another pack has a normal 12935 argument or if one pack has an empty argument and an another 12936 one hasn't then tsubst_pack_expansion cannot perform the 12937 substitution and need to fall back on the 12938 PACK_EXPANSION_EXTRA mechanism. */ 12939 if (parm_packs == NULL_TREE) 12940 return false; 12941 else if (has_empty_arg) 12942 { 12943 /* If all the actual packs are pack expansions, we can still 12944 subsitute directly. */ 12945 for (tree p = parm_packs; p; p = TREE_CHAIN (p)) 12946 { 12947 tree a = TREE_VALUE (p); 12948 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT) 12949 a = ARGUMENT_PACK_SELECT_FROM_PACK (a); 12950 a = ARGUMENT_PACK_ARGS (a); 12951 if (TREE_VEC_LENGTH (a) == 1) 12952 a = TREE_VEC_ELT (a, 0); 12953 if (PACK_EXPANSION_P (a)) 12954 continue; 12955 return true; 12956 } 12957 return false; 12958 } 12959 12960 for (int i = 0 ; i < arg_pack_len; ++i) 12961 { 12962 bool has_expansion_arg = false; 12963 bool has_non_expansion_arg = false; 12964 for (tree parm_pack = parm_packs; 12965 parm_pack; 12966 parm_pack = TREE_CHAIN (parm_pack)) 12967 { 12968 tree arg = TREE_VALUE (parm_pack); 12969 12970 int exp = argument_pack_element_is_expansion_p (arg, i); 12971 if (exp == 2) 12972 /* We can't substitute a pack expansion with extra args into 12973 our pattern. */ 12974 return true; 12975 else if (exp) 12976 has_expansion_arg = true; 12977 else 12978 has_non_expansion_arg = true; 12979 } 12980 12981 if (has_expansion_arg && has_non_expansion_arg) 12982 { 12983 /* We can get here with: 12984 12985 template <class... Ts> struct X { 12986 template <class... Us> using Y = Z<void(Ts, Us)...>; 12987 }; 12988 template <class A, class... P> 12989 using foo = X<int, int>::Y<A, P...>; 12990 12991 where we compare int and A and then the second int and P..., 12992 whose expansion-ness doesn't match, but that's OK. */ 12993 return true; 12994 } 12995 } 12996 return false; 12997 } 12998 12999 /* [temp.variadic]/6 says that: 13000 13001 The instantiation of a pack expansion [...] 13002 produces a list E1,E2, ..., En, where N is the number of elements 13003 in the pack expansion parameters. 13004 13005 This subroutine of tsubst_pack_expansion produces one of these Ei. 13006 13007 PATTERN is the pattern of the pack expansion. PARM_PACKS is a 13008 TREE_LIST in which each TREE_PURPOSE is a parameter pack of 13009 PATTERN, and each TREE_VALUE is its corresponding argument pack. 13010 INDEX is the index 'i' of the element Ei to produce. ARGS, 13011 COMPLAIN, and IN_DECL are the same parameters as for the 13012 tsubst_pack_expansion function. 13013 13014 The function returns the resulting Ei upon successful completion, 13015 or error_mark_node. 13016 13017 Note that this function possibly modifies the ARGS parameter, so 13018 it's the responsibility of the caller to restore it. */ 13019 13020 static tree 13021 gen_elem_of_pack_expansion_instantiation (tree pattern, 13022 tree parm_packs, 13023 unsigned index, 13024 tree args /* This parm gets 13025 modified. */, 13026 tsubst_flags_t complain, 13027 tree in_decl) 13028 { 13029 tree t; 13030 bool ith_elem_is_expansion = false; 13031 13032 /* For each parameter pack, change the substitution of the parameter 13033 pack to the ith argument in its argument pack, then expand the 13034 pattern. */ 13035 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack)) 13036 { 13037 tree parm = TREE_PURPOSE (pack); 13038 tree arg_pack = TREE_VALUE (pack); 13039 tree aps; /* instance of ARGUMENT_PACK_SELECT. */ 13040 13041 ith_elem_is_expansion |= 13042 argument_pack_element_is_expansion_p (arg_pack, index); 13043 13044 /* Select the Ith argument from the pack. */ 13045 if (TREE_CODE (parm) == PARM_DECL 13046 || VAR_P (parm) 13047 || TREE_CODE (parm) == FIELD_DECL) 13048 { 13049 if (index == 0) 13050 { 13051 aps = make_argument_pack_select (arg_pack, index); 13052 if (!mark_used (parm, complain) && !(complain & tf_error)) 13053 return error_mark_node; 13054 register_local_specialization (aps, parm); 13055 } 13056 else 13057 aps = retrieve_local_specialization (parm); 13058 } 13059 else 13060 { 13061 int idx, level; 13062 template_parm_level_and_index (parm, &level, &idx); 13063 13064 if (index == 0) 13065 { 13066 aps = make_argument_pack_select (arg_pack, index); 13067 /* Update the corresponding argument. */ 13068 TMPL_ARG (args, level, idx) = aps; 13069 } 13070 else 13071 /* Re-use the ARGUMENT_PACK_SELECT. */ 13072 aps = TMPL_ARG (args, level, idx); 13073 } 13074 ARGUMENT_PACK_SELECT_INDEX (aps) = index; 13075 } 13076 13077 /* Substitute into the PATTERN with the (possibly altered) 13078 arguments. */ 13079 if (pattern == in_decl) 13080 /* Expanding a fixed parameter pack from 13081 coerce_template_parameter_pack. */ 13082 t = tsubst_decl (pattern, args, complain); 13083 else if (pattern == error_mark_node) 13084 t = error_mark_node; 13085 else if (!TYPE_P (pattern)) 13086 t = tsubst_expr (pattern, args, complain, in_decl); 13087 else 13088 { 13089 t = tsubst (pattern, args, complain, in_decl); 13090 if (is_auto (t) && !ith_elem_is_expansion) 13091 /* When expanding the fake auto... pack expansion from add_capture, we 13092 need to mark that the expansion is no longer a pack. */ 13093 TEMPLATE_TYPE_PARAMETER_PACK (t) = false; 13094 } 13095 13096 /* If the Ith argument pack element is a pack expansion, then 13097 the Ith element resulting from the substituting is going to 13098 be a pack expansion as well. */ 13099 if (ith_elem_is_expansion) 13100 t = make_pack_expansion (t, complain); 13101 13102 return t; 13103 } 13104 13105 /* When the unexpanded parameter pack in a fold expression expands to an empty 13106 sequence, the value of the expression is as follows; the program is 13107 ill-formed if the operator is not listed in this table. 13108 13109 && true 13110 || false 13111 , void() */ 13112 13113 tree 13114 expand_empty_fold (tree t, tsubst_flags_t complain) 13115 { 13116 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0)); 13117 if (!FOLD_EXPR_MODIFY_P (t)) 13118 switch (code) 13119 { 13120 case TRUTH_ANDIF_EXPR: 13121 return boolean_true_node; 13122 case TRUTH_ORIF_EXPR: 13123 return boolean_false_node; 13124 case COMPOUND_EXPR: 13125 return void_node; 13126 default: 13127 break; 13128 } 13129 13130 if (complain & tf_error) 13131 error_at (location_of (t), 13132 "fold of empty expansion over %O", code); 13133 return error_mark_node; 13134 } 13135 13136 /* Given a fold-expression T and a current LEFT and RIGHT operand, 13137 form an expression that combines the two terms using the 13138 operator of T. */ 13139 13140 static tree 13141 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain) 13142 { 13143 tree_code code = FOLD_EXPR_OP (t); 13144 13145 tree lookups = templated_operator_saved_lookups (t); 13146 13147 // Handle compound assignment operators. 13148 if (FOLD_EXPR_MODIFY_P (t)) 13149 return build_x_modify_expr (input_location, left, code, right, 13150 lookups, complain); 13151 13152 warning_sentinel s(warn_parentheses); 13153 switch (code) 13154 { 13155 case COMPOUND_EXPR: 13156 return build_x_compound_expr (input_location, left, right, 13157 lookups, complain); 13158 default: 13159 return build_x_binary_op (input_location, code, 13160 left, TREE_CODE (left), 13161 right, TREE_CODE (right), 13162 lookups, /*overload=*/NULL, 13163 complain); 13164 } 13165 } 13166 13167 /* Substitute ARGS into the pack of a fold expression T. */ 13168 13169 static inline tree 13170 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl) 13171 { 13172 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl); 13173 } 13174 13175 /* Substitute ARGS into the pack of a fold expression T. */ 13176 13177 static inline tree 13178 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl) 13179 { 13180 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl); 13181 } 13182 13183 /* Expand a PACK of arguments into a grouped as left fold. 13184 Given a pack containing elements A0, A1, ..., An and an 13185 operator @, this builds the expression: 13186 13187 ((A0 @ A1) @ A2) ... @ An 13188 13189 Note that PACK must not be empty. 13190 13191 The operator is defined by the original fold expression T. */ 13192 13193 static tree 13194 expand_left_fold (tree t, tree pack, tsubst_flags_t complain) 13195 { 13196 tree left = TREE_VEC_ELT (pack, 0); 13197 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i) 13198 { 13199 tree right = TREE_VEC_ELT (pack, i); 13200 left = fold_expression (t, left, right, complain); 13201 } 13202 return left; 13203 } 13204 13205 /* Substitute into a unary left fold expression. */ 13206 13207 static tree 13208 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain, 13209 tree in_decl) 13210 { 13211 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl); 13212 if (pack == error_mark_node) 13213 return error_mark_node; 13214 if (PACK_EXPANSION_P (pack)) 13215 { 13216 tree r = copy_node (t); 13217 FOLD_EXPR_PACK (r) = pack; 13218 return r; 13219 } 13220 if (TREE_VEC_LENGTH (pack) == 0) 13221 return expand_empty_fold (t, complain); 13222 else 13223 return expand_left_fold (t, pack, complain); 13224 } 13225 13226 /* Substitute into a binary left fold expression. 13227 13228 Do ths by building a single (non-empty) vector of argumnts and 13229 building the expression from those elements. */ 13230 13231 static tree 13232 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain, 13233 tree in_decl) 13234 { 13235 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl); 13236 if (pack == error_mark_node) 13237 return error_mark_node; 13238 tree init = tsubst_fold_expr_init (t, args, complain, in_decl); 13239 if (init == error_mark_node) 13240 return error_mark_node; 13241 13242 if (PACK_EXPANSION_P (pack)) 13243 { 13244 tree r = copy_node (t); 13245 FOLD_EXPR_PACK (r) = pack; 13246 FOLD_EXPR_INIT (r) = init; 13247 return r; 13248 } 13249 13250 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1); 13251 TREE_VEC_ELT (vec, 0) = init; 13252 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i) 13253 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i); 13254 13255 return expand_left_fold (t, vec, complain); 13256 } 13257 13258 /* Expand a PACK of arguments into a grouped as right fold. 13259 Given a pack containing elementns A0, A1, ..., and an 13260 operator @, this builds the expression: 13261 13262 A0@ ... (An-2 @ (An-1 @ An)) 13263 13264 Note that PACK must not be empty. 13265 13266 The operator is defined by the original fold expression T. */ 13267 13268 tree 13269 expand_right_fold (tree t, tree pack, tsubst_flags_t complain) 13270 { 13271 // Build the expression. 13272 int n = TREE_VEC_LENGTH (pack); 13273 tree right = TREE_VEC_ELT (pack, n - 1); 13274 for (--n; n != 0; --n) 13275 { 13276 tree left = TREE_VEC_ELT (pack, n - 1); 13277 right = fold_expression (t, left, right, complain); 13278 } 13279 return right; 13280 } 13281 13282 /* Substitute into a unary right fold expression. */ 13283 13284 static tree 13285 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain, 13286 tree in_decl) 13287 { 13288 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl); 13289 if (pack == error_mark_node) 13290 return error_mark_node; 13291 if (PACK_EXPANSION_P (pack)) 13292 { 13293 tree r = copy_node (t); 13294 FOLD_EXPR_PACK (r) = pack; 13295 return r; 13296 } 13297 if (TREE_VEC_LENGTH (pack) == 0) 13298 return expand_empty_fold (t, complain); 13299 else 13300 return expand_right_fold (t, pack, complain); 13301 } 13302 13303 /* Substitute into a binary right fold expression. 13304 13305 Do ths by building a single (non-empty) vector of arguments and 13306 building the expression from those elements. */ 13307 13308 static tree 13309 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain, 13310 tree in_decl) 13311 { 13312 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl); 13313 if (pack == error_mark_node) 13314 return error_mark_node; 13315 tree init = tsubst_fold_expr_init (t, args, complain, in_decl); 13316 if (init == error_mark_node) 13317 return error_mark_node; 13318 13319 if (PACK_EXPANSION_P (pack)) 13320 { 13321 tree r = copy_node (t); 13322 FOLD_EXPR_PACK (r) = pack; 13323 FOLD_EXPR_INIT (r) = init; 13324 return r; 13325 } 13326 13327 int n = TREE_VEC_LENGTH (pack); 13328 tree vec = make_tree_vec (n + 1); 13329 for (int i = 0; i < n; ++i) 13330 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i); 13331 TREE_VEC_ELT (vec, n) = init; 13332 13333 return expand_right_fold (t, vec, complain); 13334 } 13335 13336 /* Walk through the pattern of a pack expansion, adding everything in 13337 local_specializations to a list. */ 13338 13339 class el_data 13340 { 13341 public: 13342 /* Set of variables declared within the pattern. */ 13343 hash_set<tree> internal; 13344 /* Set of AST nodes that have been visited by the traversal. */ 13345 hash_set<tree> visited; 13346 /* List of local_specializations used within the pattern. */ 13347 tree extra; 13348 tsubst_flags_t complain; 13349 /* True iff we don't want to walk into unevaluated contexts. */ 13350 bool skip_unevaluated_operands = false; 13351 /* The unevaluated contexts that we avoided walking. */ 13352 auto_vec<tree> skipped_trees; 13353 13354 el_data (tsubst_flags_t c) 13355 : extra (NULL_TREE), complain (c) {} 13356 }; 13357 static tree 13358 extract_locals_r (tree *tp, int *walk_subtrees, void *data_) 13359 { 13360 el_data &data = *reinterpret_cast<el_data*>(data_); 13361 tree *extra = &data.extra; 13362 tsubst_flags_t complain = data.complain; 13363 13364 if (data.skip_unevaluated_operands 13365 && unevaluated_p (TREE_CODE (*tp))) 13366 { 13367 data.skipped_trees.safe_push (*tp); 13368 *walk_subtrees = 0; 13369 return NULL_TREE; 13370 } 13371 13372 if (TYPE_P (*tp) && typedef_variant_p (*tp)) 13373 /* Remember local typedefs (85214). */ 13374 tp = &TYPE_NAME (*tp); 13375 13376 if (has_extra_args_mechanism_p (*tp)) 13377 /* Assert *_EXTRA_ARGS is empty, because we don't want to walk it and 13378 potentially see a previously captured local in an evaluated context 13379 that's really only used in an unevaluated context (PR114303). This 13380 means callers of build_extra_args need to clear *_EXTRA_ARGS of the 13381 outermost tree. Nested *_EXTRA_ARGS should naturally be empty since 13382 the outermost (extra-args) tree will intercept any substitution before 13383 a nested tree can. */ 13384 gcc_checking_assert (tree_extra_args (*tp) == NULL_TREE 13385 /* Except a lambda nested inside an extra-args tree 13386 can have extra args if we deferred partial 13387 substitution into it at template parse time. But 13388 we don't walk LAMBDA_EXPR_EXTRA_ARGS anyway. */ 13389 || TREE_CODE (*tp) == LAMBDA_EXPR); 13390 13391 if (TREE_CODE (*tp) == DECL_EXPR) 13392 { 13393 tree decl = DECL_EXPR_DECL (*tp); 13394 data.internal.add (decl); 13395 if (VAR_P (decl) 13396 && DECL_DECOMPOSITION_P (decl) 13397 && TREE_TYPE (decl) != error_mark_node) 13398 { 13399 gcc_assert (DECL_NAME (decl) == NULL_TREE); 13400 for (tree decl2 = DECL_CHAIN (decl); 13401 decl2 13402 && VAR_P (decl2) 13403 && DECL_DECOMPOSITION_P (decl2) 13404 && DECL_NAME (decl2) 13405 && TREE_TYPE (decl2) != error_mark_node; 13406 decl2 = DECL_CHAIN (decl2)) 13407 { 13408 gcc_assert (DECL_DECOMP_BASE (decl2) == decl); 13409 data.internal.add (decl2); 13410 } 13411 } 13412 } 13413 else if (TREE_CODE (*tp) == LAMBDA_EXPR) 13414 { 13415 /* Since we defer implicit capture, look in the parms and body. */ 13416 tree fn = lambda_function (*tp); 13417 cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data, 13418 &data.visited); 13419 cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data, 13420 &data.visited); 13421 } 13422 else if (tree spec = retrieve_local_specialization (*tp)) 13423 { 13424 if (data.internal.contains (*tp)) 13425 /* Don't mess with variables declared within the pattern. */ 13426 return NULL_TREE; 13427 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK) 13428 { 13429 /* Maybe pull out the PARM_DECL for a partial instantiation. */ 13430 tree args = ARGUMENT_PACK_ARGS (spec); 13431 if (TREE_VEC_LENGTH (args) == 1) 13432 { 13433 tree elt = TREE_VEC_ELT (args, 0); 13434 if (PACK_EXPANSION_P (elt)) 13435 elt = PACK_EXPANSION_PATTERN (elt); 13436 if (DECL_PACK_P (elt)) 13437 spec = elt; 13438 } 13439 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK) 13440 { 13441 /* Handle lambda capture here, since we aren't doing any 13442 substitution now, and so tsubst_copy won't call 13443 process_outer_var_ref. */ 13444 tree args = ARGUMENT_PACK_ARGS (spec); 13445 int len = TREE_VEC_LENGTH (args); 13446 for (int i = 0; i < len; ++i) 13447 { 13448 tree arg = TREE_VEC_ELT (args, i); 13449 tree carg = arg; 13450 if (outer_automatic_var_p (arg)) 13451 carg = process_outer_var_ref (arg, complain); 13452 if (carg != arg) 13453 { 13454 /* Make a new NONTYPE_ARGUMENT_PACK of the capture 13455 proxies. */ 13456 if (i == 0) 13457 { 13458 spec = copy_node (spec); 13459 args = copy_node (args); 13460 ARGUMENT_PACK_ARGS (spec) = args; 13461 register_local_specialization (spec, *tp); 13462 } 13463 TREE_VEC_ELT (args, i) = carg; 13464 } 13465 } 13466 } 13467 } 13468 if (outer_automatic_var_p (spec)) 13469 spec = process_outer_var_ref (spec, complain); 13470 *extra = tree_cons (*tp, spec, *extra); 13471 } 13472 return NULL_TREE; 13473 } 13474 static tree 13475 extract_local_specs (tree pattern, tsubst_flags_t complain) 13476 { 13477 el_data data (complain); 13478 /* Walk the pattern twice, ignoring unevaluated operands the first time 13479 around, so that if a local specialization appears in both an evaluated 13480 and unevaluated context we prefer to process it in the evaluated context 13481 (since e.g. process_outer_var_ref is a no-op inside an unevaluated 13482 context). */ 13483 data.skip_unevaluated_operands = true; 13484 cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited); 13485 /* Now walk the unevaluated contexts we skipped the first time around. */ 13486 data.skip_unevaluated_operands = false; 13487 for (tree t : data.skipped_trees) 13488 { 13489 data.visited.remove (t); 13490 cp_walk_tree (&t, extract_locals_r, &data, &data.visited); 13491 } 13492 return data.extra; 13493 } 13494 13495 /* Extract any uses of local_specializations from PATTERN and add them to ARGS 13496 for use in PACK_EXPANSION_EXTRA_ARGS. */ 13497 13498 tree 13499 build_extra_args (tree pattern, tree args, tsubst_flags_t complain) 13500 { 13501 /* Make a copy of the extra arguments so that they won't get changed 13502 out from under us. */ 13503 tree extra = preserve_args (copy_template_args (args), /*cow_p=*/false); 13504 if (local_specializations) 13505 if (tree locals = extract_local_specs (pattern, complain)) 13506 extra = tree_cons (NULL_TREE, extra, locals); 13507 return extra; 13508 } 13509 13510 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the 13511 normal template args to ARGS. */ 13512 13513 tree 13514 add_extra_args (tree extra, tree args, tsubst_flags_t complain, tree in_decl) 13515 { 13516 if (extra && TREE_CODE (extra) == TREE_LIST) 13517 { 13518 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt)) 13519 { 13520 /* The partial instantiation involved local declarations collected in 13521 extract_local_specs; map from the general template to our local 13522 context. */ 13523 tree gen = TREE_PURPOSE (elt); 13524 tree inst = TREE_VALUE (elt); 13525 if (DECL_P (inst)) 13526 if (tree local = retrieve_local_specialization (inst)) 13527 inst = local; 13528 /* else inst is already a full instantiation of the pack. */ 13529 register_local_specialization (inst, gen); 13530 if (is_normal_capture_proxy (gen)) 13531 register_local_specialization (inst, DECL_CAPTURED_VARIABLE (gen)); 13532 } 13533 gcc_assert (!TREE_PURPOSE (extra)); 13534 extra = TREE_VALUE (extra); 13535 } 13536 if (uses_template_parms (extra)) 13537 { 13538 /* This can happen after dependent substitution into a 13539 requires-expr or a lambda that uses constexpr if. */ 13540 extra = tsubst_template_args (extra, args, complain, in_decl); 13541 args = add_outermost_template_args (args, extra); 13542 } 13543 else 13544 args = add_to_template_args (extra, args); 13545 return args; 13546 } 13547 13548 /* Substitute ARGS into T, which is an pack expansion 13549 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a 13550 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node 13551 (if only a partial substitution could be performed) or 13552 ERROR_MARK_NODE if there was an error. */ 13553 tree 13554 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, 13555 tree in_decl) 13556 { 13557 tree pattern; 13558 tree pack, packs = NULL_TREE; 13559 bool unsubstituted_packs = false; 13560 int i, len = -1; 13561 tree result; 13562 bool need_local_specializations = false; 13563 int levels; 13564 13565 gcc_assert (PACK_EXPANSION_P (t)); 13566 pattern = PACK_EXPANSION_PATTERN (t); 13567 13568 /* Add in any args remembered from an earlier partial instantiation. */ 13569 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args, complain, in_decl); 13570 13571 levels = TMPL_ARGS_DEPTH (args); 13572 13573 /* Determine the argument packs that will instantiate the parameter 13574 packs used in the expansion expression. While we're at it, 13575 compute the number of arguments to be expanded and make sure it 13576 is consistent. */ 13577 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack; 13578 pack = TREE_CHAIN (pack)) 13579 { 13580 tree parm_pack = TREE_VALUE (pack); 13581 tree arg_pack = NULL_TREE; 13582 tree orig_arg = NULL_TREE; 13583 int level = 0; 13584 13585 if (TREE_CODE (parm_pack) == BASES) 13586 { 13587 gcc_assert (parm_pack == pattern); 13588 tree type = tsubst (BASES_TYPE (parm_pack), args, complain, in_decl); 13589 if (BASES_DIRECT (parm_pack)) 13590 return calculate_direct_bases (type, complain); 13591 else 13592 return calculate_bases (type, complain); 13593 } 13594 else if (builtin_pack_call_p (parm_pack)) 13595 { 13596 if (parm_pack != pattern) 13597 { 13598 if (complain & tf_error) 13599 sorry ("%qE is not the entire pattern of the pack expansion", 13600 parm_pack); 13601 return error_mark_node; 13602 } 13603 return expand_builtin_pack_call (parm_pack, args, 13604 complain, in_decl); 13605 } 13606 else if (TREE_CODE (parm_pack) == PARM_DECL) 13607 { 13608 /* We know we have correct local_specializations if this 13609 expansion is at function scope, or if we're dealing with a 13610 local parameter in a requires expression; for the latter, 13611 tsubst_requires_expr set it up appropriately. */ 13612 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack)) 13613 arg_pack = retrieve_local_specialization (parm_pack); 13614 else 13615 /* We can't rely on local_specializations for a parameter 13616 name used later in a function declaration (such as in a 13617 late-specified return type). Even if it exists, it might 13618 have the wrong value for a recursive call. */ 13619 need_local_specializations = true; 13620 13621 if (!arg_pack) 13622 { 13623 /* This parameter pack was used in an unevaluated context. Just 13624 make a dummy decl, since it's only used for its type. */ 13625 ++cp_unevaluated_operand; 13626 arg_pack = tsubst_decl (parm_pack, args, complain); 13627 --cp_unevaluated_operand; 13628 if (arg_pack && DECL_PACK_P (arg_pack)) 13629 /* Partial instantiation of the parm_pack, we can't build 13630 up an argument pack yet. */ 13631 arg_pack = NULL_TREE; 13632 else 13633 arg_pack = make_fnparm_pack (arg_pack); 13634 } 13635 else if (DECL_PACK_P (arg_pack)) 13636 /* This argument pack isn't fully instantiated yet. */ 13637 arg_pack = NULL_TREE; 13638 } 13639 else if (is_capture_proxy (parm_pack)) 13640 { 13641 arg_pack = retrieve_local_specialization (parm_pack); 13642 if (DECL_PACK_P (arg_pack)) 13643 arg_pack = NULL_TREE; 13644 } 13645 else 13646 { 13647 int idx; 13648 template_parm_level_and_index (parm_pack, &level, &idx); 13649 if (level <= levels) 13650 arg_pack = TMPL_ARG (args, level, idx); 13651 13652 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM 13653 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack)) 13654 arg_pack = NULL_TREE; 13655 } 13656 13657 orig_arg = arg_pack; 13658 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT) 13659 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack); 13660 13661 if (arg_pack && !ARGUMENT_PACK_P (arg_pack)) 13662 /* This can only happen if we forget to expand an argument 13663 pack somewhere else. Just return an error, silently. */ 13664 { 13665 result = make_tree_vec (1); 13666 TREE_VEC_ELT (result, 0) = error_mark_node; 13667 return result; 13668 } 13669 13670 if (arg_pack) 13671 { 13672 int my_len = 13673 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)); 13674 13675 /* Don't bother trying to do a partial substitution with 13676 incomplete packs; we'll try again after deduction. */ 13677 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) 13678 return t; 13679 13680 if (len < 0) 13681 len = my_len; 13682 else if (len != my_len) 13683 { 13684 if (!(complain & tf_error)) 13685 /* Fail quietly. */; 13686 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION) 13687 error ("mismatched argument pack lengths while expanding %qT", 13688 pattern); 13689 else 13690 error ("mismatched argument pack lengths while expanding %qE", 13691 pattern); 13692 return error_mark_node; 13693 } 13694 13695 /* Keep track of the parameter packs and their corresponding 13696 argument packs. */ 13697 packs = tree_cons (parm_pack, arg_pack, packs); 13698 TREE_TYPE (packs) = orig_arg; 13699 } 13700 else 13701 { 13702 /* We can't substitute for this parameter pack. We use a flag as 13703 well as the missing_level counter because function parameter 13704 packs don't have a level. */ 13705 gcc_assert (processing_template_decl || is_auto (parm_pack)); 13706 unsubstituted_packs = true; 13707 } 13708 } 13709 13710 /* If the expansion is just T..., return the matching argument pack, unless 13711 we need to call convert_from_reference on all the elements. This is an 13712 important optimization; see c++/68422. */ 13713 if (!unsubstituted_packs 13714 && TREE_PURPOSE (packs) == pattern) 13715 { 13716 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs)); 13717 13718 /* If the argument pack is a single pack expansion, pull it out. */ 13719 if (TREE_VEC_LENGTH (args) == 1 13720 && pack_expansion_args_count (args)) 13721 { 13722 tree arg = TREE_VEC_ELT (args, 0); 13723 if (PACK_EXPANSION_SIZEOF_P (t) 13724 && !TEMPLATE_PARM_P (PACK_EXPANSION_PATTERN (arg))) 13725 /* Except if this isn't a simple sizeof...(T) which gets sZ 13726 mangling, keep the TREE_VEC to get sP mangling. */; 13727 else 13728 return TREE_VEC_ELT (args, 0); 13729 } 13730 13731 /* Types need no adjustment, nor does sizeof..., and if we still have 13732 some pack expansion args we won't do anything yet. */ 13733 if (TREE_CODE (t) == TYPE_PACK_EXPANSION 13734 || PACK_EXPANSION_SIZEOF_P (t) 13735 || pack_expansion_args_count (args)) 13736 return args; 13737 /* Also optimize expression pack expansions if we can tell that the 13738 elements won't have reference type. */ 13739 tree type = TREE_TYPE (pattern); 13740 if (type && !TYPE_REF_P (type) 13741 && !PACK_EXPANSION_P (type) 13742 && !WILDCARD_TYPE_P (type)) 13743 return args; 13744 /* Otherwise use the normal path so we get convert_from_reference. */ 13745 } 13746 13747 /* We cannot expand this expansion expression, because we don't have 13748 all of the argument packs we need. */ 13749 if (use_pack_expansion_extra_args_p (t, packs, len, unsubstituted_packs)) 13750 { 13751 /* We got some full packs, but we can't substitute them in until we 13752 have values for all the packs. So remember these until then. */ 13753 13754 t = make_pack_expansion (pattern, complain); 13755 PACK_EXPANSION_EXTRA_ARGS (t) 13756 = build_extra_args (pattern, args, complain); 13757 return t; 13758 } 13759 13760 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return 13761 type, so create our own local specializations map; the current map is 13762 either NULL or (in the case of recursive unification) might have 13763 bindings that we don't want to use or alter. */ 13764 local_specialization_stack lss (need_local_specializations 13765 ? lss_blank : lss_nop); 13766 13767 if (unsubstituted_packs) 13768 { 13769 /* There were no real arguments, we're just replacing a parameter 13770 pack with another version of itself. Substitute into the 13771 pattern and return a PACK_EXPANSION_*. The caller will need to 13772 deal with that. */ 13773 if (TREE_CODE (t) == EXPR_PACK_EXPANSION) 13774 result = tsubst_expr (pattern, args, complain, in_decl); 13775 else 13776 result = tsubst (pattern, args, complain, in_decl); 13777 result = make_pack_expansion (result, complain); 13778 PACK_EXPANSION_LOCAL_P (result) = PACK_EXPANSION_LOCAL_P (t); 13779 PACK_EXPANSION_SIZEOF_P (result) = PACK_EXPANSION_SIZEOF_P (t); 13780 if (PACK_EXPANSION_AUTO_P (t)) 13781 { 13782 /* This is a fake auto... pack expansion created in add_capture with 13783 _PACKS that don't appear in the pattern. Copy one over. */ 13784 packs = PACK_EXPANSION_PARAMETER_PACKS (t); 13785 pack = retrieve_local_specialization (TREE_VALUE (packs)); 13786 gcc_checking_assert (DECL_PACK_P (pack)); 13787 PACK_EXPANSION_PARAMETER_PACKS (result) 13788 = build_tree_list (NULL_TREE, pack); 13789 PACK_EXPANSION_AUTO_P (result) = true; 13790 } 13791 return result; 13792 } 13793 13794 gcc_assert (len >= 0); 13795 13796 /* For each argument in each argument pack, substitute into the 13797 pattern. */ 13798 result = make_tree_vec (len); 13799 tree elem_args = copy_template_args (args); 13800 for (i = 0; i < len; ++i) 13801 { 13802 t = gen_elem_of_pack_expansion_instantiation (pattern, packs, 13803 i, 13804 elem_args, complain, 13805 in_decl); 13806 TREE_VEC_ELT (result, i) = t; 13807 if (t == error_mark_node) 13808 { 13809 result = error_mark_node; 13810 break; 13811 } 13812 } 13813 13814 /* Update ARGS to restore the substitution from parameter packs to 13815 their argument packs. */ 13816 for (pack = packs; pack; pack = TREE_CHAIN (pack)) 13817 { 13818 tree parm = TREE_PURPOSE (pack); 13819 13820 if (TREE_CODE (parm) == PARM_DECL 13821 || VAR_P (parm) 13822 || TREE_CODE (parm) == FIELD_DECL) 13823 register_local_specialization (TREE_TYPE (pack), parm); 13824 else 13825 { 13826 int idx, level; 13827 13828 if (TREE_VALUE (pack) == NULL_TREE) 13829 continue; 13830 13831 template_parm_level_and_index (parm, &level, &idx); 13832 13833 /* Update the corresponding argument. */ 13834 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) 13835 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) = 13836 TREE_TYPE (pack); 13837 else 13838 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack); 13839 } 13840 } 13841 13842 /* If the dependent pack arguments were such that we end up with only a 13843 single pack expansion again, there's no need to keep it in a TREE_VEC. */ 13844 if (len == 1 && TREE_CODE (result) == TREE_VEC 13845 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0))) 13846 return TREE_VEC_ELT (result, 0); 13847 13848 return result; 13849 } 13850 13851 /* Make an argument pack out of the TREE_VEC VEC. */ 13852 13853 static tree 13854 make_argument_pack (tree vec) 13855 { 13856 tree pack; 13857 13858 if (TYPE_P (TREE_VEC_ELT (vec, 0))) 13859 pack = cxx_make_type (TYPE_ARGUMENT_PACK); 13860 else 13861 { 13862 pack = make_node (NONTYPE_ARGUMENT_PACK); 13863 TREE_CONSTANT (pack) = 1; 13864 } 13865 ARGUMENT_PACK_ARGS (pack) = vec; 13866 return pack; 13867 } 13868 13869 /* Return an exact copy of template args T that can be modified 13870 independently. */ 13871 13872 static tree 13873 copy_template_args (tree t) 13874 { 13875 if (t == error_mark_node) 13876 return t; 13877 13878 int len = TREE_VEC_LENGTH (t); 13879 tree new_vec = make_tree_vec (len); 13880 13881 for (int i = 0; i < len; ++i) 13882 { 13883 tree elt = TREE_VEC_ELT (t, i); 13884 if (elt && TREE_CODE (elt) == TREE_VEC) 13885 elt = copy_template_args (elt); 13886 TREE_VEC_ELT (new_vec, i) = elt; 13887 } 13888 13889 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec) 13890 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t); 13891 13892 return new_vec; 13893 } 13894 13895 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */ 13896 13897 tree 13898 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain, 13899 tree in_decl) 13900 { 13901 /* This flag is used only during deduction, and we don't expect to 13902 substitute such ARGUMENT_PACKs. */ 13903 gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (orig_arg)); 13904 13905 /* Substitute into each of the arguments. */ 13906 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg), 13907 args, complain, in_decl); 13908 if (pack_args == error_mark_node) 13909 return error_mark_node; 13910 13911 if (pack_args == ARGUMENT_PACK_ARGS (orig_arg)) 13912 return orig_arg; 13913 13914 /* If we're substituting into a generic ARGUMENT_PACK for a variadic 13915 template parameter, we might be able to avoid allocating a new 13916 ARGUMENT_PACK and reuse the corresponding ARGUMENT_PACK from ARGS 13917 if the substituted result is identical to it. */ 13918 if (tree parm = template_arg_to_parm (orig_arg)) 13919 { 13920 int level, index; 13921 template_parm_level_and_index (parm, &level, &index); 13922 if (TMPL_ARGS_DEPTH (args) >= level) 13923 if (tree arg = TMPL_ARG (args, level, index)) 13924 if (TREE_CODE (arg) == TREE_CODE (orig_arg) 13925 && ARGUMENT_PACK_ARGS (arg) == pack_args) 13926 { 13927 gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (arg)); 13928 return arg; 13929 } 13930 } 13931 13932 tree new_arg; 13933 if (TYPE_P (orig_arg)) 13934 { 13935 new_arg = cxx_make_type (TREE_CODE (orig_arg)); 13936 SET_TYPE_STRUCTURAL_EQUALITY (new_arg); 13937 } 13938 else 13939 { 13940 new_arg = make_node (TREE_CODE (orig_arg)); 13941 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg); 13942 } 13943 ARGUMENT_PACK_ARGS (new_arg) = pack_args; 13944 return new_arg; 13945 } 13946 13947 /* Substitute ARGS into the vector or list of template arguments T. */ 13948 13949 tree 13950 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl) 13951 { 13952 if (t == error_mark_node) 13953 return error_mark_node; 13954 13955 /* In "sizeof(X<I>)" we need to evaluate "I". */ 13956 cp_evaluated ev; 13957 13958 const int len = TREE_VEC_LENGTH (t); 13959 tree *elts = XALLOCAVEC (tree, len); 13960 int expanded_len_adjust = 0; 13961 13962 /* True iff the substituted result is identical to T. */ 13963 bool const_subst_p = true; 13964 13965 for (int i = 0; i < len; i++) 13966 { 13967 tree orig_arg = TREE_VEC_ELT (t, i); 13968 tree new_arg; 13969 13970 if (!orig_arg) 13971 new_arg = NULL_TREE; 13972 else if (TREE_CODE (orig_arg) == TREE_VEC) 13973 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl); 13974 else if (PACK_EXPANSION_P (orig_arg)) 13975 { 13976 /* Substitute into an expansion expression. */ 13977 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl); 13978 13979 if (TREE_CODE (new_arg) == TREE_VEC) 13980 /* Add to the expanded length adjustment the number of 13981 expanded arguments. We subtract one from this 13982 measurement, because the argument pack expression 13983 itself is already counted as 1 in 13984 LEN. EXPANDED_LEN_ADJUST can actually be negative, if 13985 the argument pack is empty. */ 13986 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1; 13987 } 13988 else if (ARGUMENT_PACK_P (orig_arg)) 13989 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl); 13990 else 13991 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl); 13992 13993 if (new_arg == error_mark_node) 13994 return error_mark_node; 13995 13996 elts[i] = new_arg; 13997 if (new_arg != orig_arg) 13998 const_subst_p = false; 13999 } 14000 14001 if (const_subst_p) 14002 return t; 14003 14004 tree maybe_reuse = NULL_TREE; 14005 14006 /* If ARGS and T are both multi-level, the substituted result may be 14007 identical to ARGS. */ 14008 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (t) 14009 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args) 14010 && TMPL_ARGS_DEPTH (t) == TMPL_ARGS_DEPTH (args)) 14011 maybe_reuse = args; 14012 /* If T appears to be a vector of generic template arguments, the 14013 substituted result may be identical to the corresponding level 14014 from ARGS. */ 14015 else if (tree parm = template_arg_to_parm (TREE_VEC_ELT (t, 0))) 14016 { 14017 int level, index; 14018 template_parm_level_and_index (parm, &level, &index); 14019 if (index == 0 && TMPL_ARGS_DEPTH (args) >= level) 14020 maybe_reuse = TMPL_ARGS_LEVEL (args, level); 14021 } 14022 14023 /* If the substituted result is identical to MAYBE_REUSE, return 14024 it and avoid allocating a new TREE_VEC, as an optimization. */ 14025 if (maybe_reuse != NULL_TREE 14026 && TREE_VEC_LENGTH (maybe_reuse) == len 14027 && std::equal (elts, elts+len, TREE_VEC_BEGIN (maybe_reuse))) 14028 return maybe_reuse; 14029 14030 /* If T consists of only a pack expansion for which substitution yielded 14031 a TREE_VEC of the expanded elements, then reuse that TREE_VEC instead 14032 of effectively making a copy. */ 14033 if (len == 1 14034 && PACK_EXPANSION_P (TREE_VEC_ELT (t, 0)) 14035 && TREE_CODE (elts[0]) == TREE_VEC) 14036 return elts[0]; 14037 14038 /* Make space for the expanded arguments coming from template 14039 argument packs. */ 14040 tree r = make_tree_vec (len + expanded_len_adjust); 14041 /* T can contain TREE_VECs. That happens if T contains the 14042 arguments for a member template. 14043 In that case each TREE_VEC in T represents a level of template 14044 arguments, and T won't carry any non defaulted argument count. 14045 It will rather be the nested TREE_VECs that will carry one. 14046 In other words, T carries a non defaulted argument count only 14047 if it doesn't contain any nested TREE_VEC. */ 14048 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (t)) 14049 { 14050 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t); 14051 count += expanded_len_adjust; 14052 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (r, count); 14053 } 14054 14055 int out = 0; 14056 for (int i = 0; i < len; i++) 14057 { 14058 tree orig_arg = TREE_VEC_ELT (t, i); 14059 if (orig_arg 14060 && PACK_EXPANSION_P (orig_arg) 14061 && TREE_CODE (elts[i]) == TREE_VEC) 14062 { 14063 /* Now expand the template argument pack "in place". */ 14064 for (int idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++) 14065 TREE_VEC_ELT (r, out) = TREE_VEC_ELT (elts[i], idx); 14066 } 14067 else 14068 { 14069 TREE_VEC_ELT (r, out) = elts[i]; 14070 out++; 14071 } 14072 } 14073 gcc_assert (out == TREE_VEC_LENGTH (r)); 14074 14075 return r; 14076 } 14077 14078 /* Substitute ARGS into one level PARMS of template parameters. */ 14079 14080 static tree 14081 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain) 14082 { 14083 if (parms == error_mark_node) 14084 return error_mark_node; 14085 14086 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms)); 14087 14088 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) 14089 { 14090 tree tuple = TREE_VEC_ELT (parms, i); 14091 14092 if (tuple == error_mark_node) 14093 continue; 14094 14095 TREE_VEC_ELT (new_vec, i) = 14096 tsubst_template_parm (tuple, args, complain); 14097 } 14098 14099 return new_vec; 14100 } 14101 14102 /* Return the result of substituting ARGS into the template parameters 14103 given by PARMS. If there are m levels of ARGS and m + n levels of 14104 PARMS, then the result will contain n levels of PARMS. For 14105 example, if PARMS is `template <class T> template <class U> 14106 template <T*, U, class V>' and ARGS is {{int}, {double}} then the 14107 result will be `template <int*, double, class V>'. */ 14108 14109 static tree 14110 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain) 14111 { 14112 tree r = NULL_TREE; 14113 tree* new_parms; 14114 14115 /* When substituting into a template, we must set 14116 PROCESSING_TEMPLATE_DECL as the template parameters may be 14117 dependent if they are based on one-another, and the dependency 14118 predicates are short-circuit outside of templates. */ 14119 ++processing_template_decl; 14120 14121 for (new_parms = &r; 14122 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); 14123 new_parms = &(TREE_CHAIN (*new_parms)), 14124 parms = TREE_CHAIN (parms)) 14125 { 14126 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms), 14127 args, complain); 14128 *new_parms = 14129 tree_cons (size_int (TMPL_PARMS_DEPTH (parms) 14130 - TMPL_ARGS_DEPTH (args)), 14131 new_vec, NULL_TREE); 14132 TEMPLATE_PARMS_CONSTRAINTS (*new_parms) 14133 = TEMPLATE_PARMS_CONSTRAINTS (parms); 14134 } 14135 14136 --processing_template_decl; 14137 14138 return r; 14139 } 14140 14141 /* Return the result of substituting ARGS into one template parameter 14142 given by T. T Must be a TREE_LIST which TREE_VALUE is the template 14143 parameter and which TREE_PURPOSE is the default argument of the 14144 template parameter. */ 14145 14146 static tree 14147 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain) 14148 { 14149 tree default_value, parm_decl; 14150 14151 if (args == NULL_TREE 14152 || t == NULL_TREE 14153 || t == error_mark_node) 14154 return t; 14155 14156 gcc_assert (TREE_CODE (t) == TREE_LIST); 14157 14158 default_value = TREE_PURPOSE (t); 14159 parm_decl = TREE_VALUE (t); 14160 14161 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE); 14162 if (TREE_CODE (parm_decl) == PARM_DECL 14163 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain)) 14164 parm_decl = error_mark_node; 14165 default_value = tsubst_template_arg (default_value, args, 14166 complain, NULL_TREE); 14167 14168 tree r = build_tree_list (default_value, parm_decl); 14169 TEMPLATE_PARM_CONSTRAINTS (r) = TEMPLATE_PARM_CONSTRAINTS (t); 14170 return r; 14171 } 14172 14173 /* Substitute in-place the TEMPLATE_PARM_CONSTRAINTS of each template 14174 parameter in PARMS for sake of declaration matching. */ 14175 14176 static void 14177 tsubst_each_template_parm_constraints (tree parms, tree args, 14178 tsubst_flags_t complain) 14179 { 14180 ++processing_template_decl; 14181 for (; parms; parms = TREE_CHAIN (parms)) 14182 { 14183 tree level = TREE_VALUE (parms); 14184 for (tree parm : tree_vec_range (level)) 14185 TEMPLATE_PARM_CONSTRAINTS (parm) 14186 = tsubst_constraint (TEMPLATE_PARM_CONSTRAINTS (parm), args, 14187 complain, NULL_TREE); 14188 } 14189 --processing_template_decl; 14190 } 14191 14192 /* Substitute the ARGS into the indicated aggregate (or enumeration) 14193 type T. If T is not an aggregate or enumeration type, it is 14194 handled as if by tsubst. IN_DECL is as for tsubst. If 14195 ENTERING_SCOPE is nonzero, T is the context for a template which 14196 we are presently tsubst'ing. Return the substituted value. */ 14197 14198 static tree 14199 tsubst_aggr_type (tree t, 14200 tree args, 14201 tsubst_flags_t complain, 14202 tree in_decl, 14203 int entering_scope) 14204 { 14205 if (t == NULL_TREE) 14206 return NULL_TREE; 14207 14208 /* Handle typedefs via tsubst so that they get consistently reused. */ 14209 if (typedef_variant_p (t)) 14210 { 14211 t = tsubst (t, args, complain, in_decl); 14212 if (t == error_mark_node) 14213 return error_mark_node; 14214 14215 /* The effect of entering_scope is that for a dependent specialization 14216 A<T>, lookup_template_class prefers to return A's primary template 14217 type instead of the implicit instantiation. So when entering_scope, 14218 we mirror this behavior by inspecting TYPE_CANONICAL appropriately, 14219 taking advantage of the fact that lookup_template_class links the two 14220 types by setting TYPE_CANONICAL of the latter to the former. */ 14221 if (entering_scope 14222 && CLASS_TYPE_P (t) 14223 && dependent_type_p (t) 14224 && TYPE_TEMPLATE_INFO (t) 14225 && TYPE_CANONICAL (t) == TREE_TYPE (TYPE_TI_TEMPLATE (t))) 14226 t = TYPE_CANONICAL (t); 14227 14228 return t; 14229 } 14230 14231 switch (TREE_CODE (t)) 14232 { 14233 case RECORD_TYPE: 14234 case ENUMERAL_TYPE: 14235 case UNION_TYPE: 14236 return tsubst_aggr_type_1 (t, args, complain, in_decl, entering_scope); 14237 14238 default: 14239 return tsubst (t, args, complain, in_decl); 14240 } 14241 } 14242 14243 /* The part of tsubst_aggr_type that's shared with the RECORD_, UNION_ 14244 and ENUMERAL_TYPE cases of tsubst. */ 14245 14246 static tree 14247 tsubst_aggr_type_1 (tree t, 14248 tree args, 14249 tsubst_flags_t complain, 14250 tree in_decl, 14251 int entering_scope) 14252 { 14253 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t)) 14254 { 14255 complain &= ~tf_qualifying_scope; 14256 14257 /* Figure out what arguments are appropriate for the 14258 type we are trying to find. For example, given: 14259 14260 template <class T> struct S; 14261 template <class T, class U> void f(T, U) { S<U> su; } 14262 14263 and supposing that we are instantiating f<int, double>, 14264 then our ARGS will be {int, double}, but, when looking up 14265 S we only want {double}. */ 14266 tree argvec = tsubst_template_args (TYPE_TI_ARGS (t), args, 14267 complain, in_decl); 14268 if (argvec == error_mark_node) 14269 return error_mark_node; 14270 14271 tree r = lookup_template_class (t, argvec, in_decl, NULL_TREE, 14272 entering_scope, complain); 14273 return cp_build_qualified_type (r, cp_type_quals (t), complain); 14274 } 14275 else 14276 /* This is not a template type, so there's nothing to do. */ 14277 return t; 14278 } 14279 14280 /* Map from a FUNCTION_DECL to a vec of default argument instantiations, 14281 indexed in reverse order of the parameters. */ 14282 14283 static GTY((cache)) hash_table<tree_vec_map_cache_hasher> *defarg_inst; 14284 14285 /* Return a reference to the vec* of defarg insts for FN. */ 14286 14287 static vec<tree,va_gc> *& 14288 defarg_insts_for (tree fn) 14289 { 14290 if (!defarg_inst) 14291 defarg_inst = hash_table<tree_vec_map_cache_hasher>::create_ggc (13); 14292 tree_vec_map in = { { fn }, nullptr }; 14293 tree_vec_map **slot 14294 = defarg_inst->find_slot_with_hash (&in, DECL_UID (fn), INSERT); 14295 if (!*slot) 14296 { 14297 *slot = ggc_alloc<tree_vec_map> (); 14298 **slot = in; 14299 } 14300 return (*slot)->to; 14301 } 14302 14303 /* Substitute into the default argument ARG (a default argument for 14304 FN), which has the indicated TYPE. */ 14305 14306 tree 14307 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg, 14308 tsubst_flags_t complain) 14309 { 14310 int errs = errorcount + sorrycount; 14311 14312 /* This can happen in invalid code. */ 14313 if (TREE_CODE (arg) == DEFERRED_PARSE) 14314 return arg; 14315 14316 /* Shortcut {}. */ 14317 if (BRACE_ENCLOSED_INITIALIZER_P (arg) 14318 && CONSTRUCTOR_NELTS (arg) == 0) 14319 return arg; 14320 14321 tree parm = FUNCTION_FIRST_USER_PARM (fn); 14322 parm = chain_index (parmnum, parm); 14323 tree parmtype = TREE_TYPE (parm); 14324 if (DECL_BY_REFERENCE (parm)) 14325 parmtype = TREE_TYPE (parmtype); 14326 if (parmtype == error_mark_node) 14327 return error_mark_node; 14328 14329 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype)); 14330 14331 /* Remember the location of the pointer to the vec rather than the location 14332 of the particular element, in case the vec grows in tsubst_expr. */ 14333 vec<tree,va_gc> *&defs = defarg_insts_for (fn); 14334 /* Index in reverse order to avoid allocating space for initial parameters 14335 that don't have default arguments. */ 14336 unsigned ridx = list_length (parm); 14337 if (vec_safe_length (defs) < ridx) 14338 vec_safe_grow_cleared (defs, ridx); 14339 else if (tree inst = (*defs)[ridx - 1]) 14340 return inst; 14341 14342 /* This default argument came from a template. Instantiate the 14343 default argument here, not in tsubst. In the case of 14344 something like: 14345 14346 template <class T> 14347 struct S { 14348 static T t(); 14349 void f(T = t()); 14350 }; 14351 14352 we must be careful to do name lookup in the scope of S<T>, 14353 rather than in the current class. */ 14354 push_to_top_level (); 14355 push_access_scope (fn); 14356 push_deferring_access_checks (dk_no_deferred); 14357 /* So in_immediate_context knows this is a default argument. */ 14358 begin_scope (sk_function_parms, fn); 14359 start_lambda_scope (parm); 14360 14361 /* The default argument expression may cause implicitly defined 14362 member functions to be synthesized, which will result in garbage 14363 collection. We must treat this situation as if we were within 14364 the body of function so as to avoid collecting live data on the 14365 stack. */ 14366 ++function_depth; 14367 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), complain, NULL_TREE); 14368 --function_depth; 14369 14370 finish_lambda_scope (); 14371 14372 /* Make sure the default argument is reasonable. */ 14373 arg = check_default_argument (type, arg, complain); 14374 14375 if (errorcount+sorrycount > errs 14376 && (complain & tf_warning_or_error)) 14377 inform (input_location, 14378 " when instantiating default argument for call to %qD", fn); 14379 14380 leave_scope (); 14381 pop_deferring_access_checks (); 14382 pop_access_scope (fn); 14383 pop_from_top_level (); 14384 14385 if (arg != error_mark_node && !cp_unevaluated_operand) 14386 (*defs)[ridx - 1] = arg; 14387 14388 return arg; 14389 } 14390 14391 /* Substitute into all the default arguments for FN. */ 14392 14393 static void 14394 tsubst_default_arguments (tree fn, tsubst_flags_t complain) 14395 { 14396 tree arg; 14397 tree tmpl_args; 14398 14399 tmpl_args = DECL_TI_ARGS (fn); 14400 14401 /* If this function is not yet instantiated, we certainly don't need 14402 its default arguments. */ 14403 if (uses_template_parms (tmpl_args)) 14404 return; 14405 /* Don't do this again for clones. */ 14406 if (DECL_CLONED_FUNCTION_P (fn)) 14407 return; 14408 14409 int i = 0; 14410 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); 14411 arg; 14412 arg = TREE_CHAIN (arg), ++i) 14413 if (TREE_PURPOSE (arg)) 14414 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i, 14415 TREE_VALUE (arg), 14416 TREE_PURPOSE (arg), 14417 complain); 14418 } 14419 14420 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */ 14421 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map; 14422 14423 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */ 14424 14425 void 14426 store_explicit_specifier (tree v, tree t) 14427 { 14428 if (!explicit_specifier_map) 14429 explicit_specifier_map = decl_tree_cache_map::create_ggc (37); 14430 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true; 14431 explicit_specifier_map->put (v, t); 14432 } 14433 14434 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */ 14435 14436 tree 14437 lookup_explicit_specifier (tree v) 14438 { 14439 return *explicit_specifier_map->get (v); 14440 } 14441 14442 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding 14443 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types 14444 are ARG_TYPES, and exception specification is RAISES, and otherwise is 14445 identical to T. */ 14446 14447 static tree 14448 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types, 14449 tree raises, tsubst_flags_t complain) 14450 { 14451 gcc_assert (FUNC_OR_METHOD_TYPE_P (t)); 14452 14453 tree new_type; 14454 if (TREE_CODE (t) == FUNCTION_TYPE) 14455 { 14456 new_type = build_function_type (return_type, arg_types); 14457 new_type = apply_memfn_quals (new_type, type_memfn_quals (t)); 14458 } 14459 else 14460 { 14461 tree r = TREE_TYPE (TREE_VALUE (arg_types)); 14462 /* Don't pick up extra function qualifiers from the basetype. */ 14463 r = cp_build_qualified_type (r, type_memfn_quals (t), complain); 14464 if (! MAYBE_CLASS_TYPE_P (r)) 14465 { 14466 /* [temp.deduct] 14467 14468 Type deduction may fail for any of the following 14469 reasons: 14470 14471 -- Attempting to create "pointer to member of T" when T 14472 is not a class type. */ 14473 if (complain & tf_error) 14474 error ("creating pointer to member function of non-class type %qT", 14475 r); 14476 return error_mark_node; 14477 } 14478 14479 new_type = build_method_type_directly (r, return_type, 14480 TREE_CHAIN (arg_types)); 14481 } 14482 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t)); 14483 14484 cp_ref_qualifier rqual = type_memfn_rqual (t); 14485 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t); 14486 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p); 14487 } 14488 14489 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of 14490 each of its formal parameters. If there is a disagreement then rebuild 14491 DECL's function type according to its formal parameter types, as part of a 14492 resolution for Core issues 1001/1322. */ 14493 14494 static void 14495 maybe_rebuild_function_decl_type (tree decl) 14496 { 14497 bool function_type_needs_rebuilding = false; 14498 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl)) 14499 { 14500 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl); 14501 while (parm_type_list && parm_type_list != void_list_node) 14502 { 14503 tree parm_type = TREE_VALUE (parm_type_list); 14504 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list)); 14505 if (!same_type_p (parm_type, formal_parm_type_unqual)) 14506 { 14507 function_type_needs_rebuilding = true; 14508 break; 14509 } 14510 14511 parm_list = DECL_CHAIN (parm_list); 14512 parm_type_list = TREE_CHAIN (parm_type_list); 14513 } 14514 } 14515 14516 if (!function_type_needs_rebuilding) 14517 return; 14518 14519 const tree fntype = TREE_TYPE (decl); 14520 tree parm_list = DECL_ARGUMENTS (decl); 14521 tree old_parm_type_list = TYPE_ARG_TYPES (fntype); 14522 tree new_parm_type_list = NULL_TREE; 14523 tree *q = &new_parm_type_list; 14524 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--) 14525 { 14526 *q = copy_node (old_parm_type_list); 14527 parm_list = DECL_CHAIN (parm_list); 14528 old_parm_type_list = TREE_CHAIN (old_parm_type_list); 14529 q = &TREE_CHAIN (*q); 14530 } 14531 while (old_parm_type_list && old_parm_type_list != void_list_node) 14532 { 14533 *q = copy_node (old_parm_type_list); 14534 tree *new_parm_type = &TREE_VALUE (*q); 14535 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list)); 14536 if (!same_type_p (*new_parm_type, formal_parm_type_unqual)) 14537 *new_parm_type = formal_parm_type_unqual; 14538 14539 parm_list = DECL_CHAIN (parm_list); 14540 old_parm_type_list = TREE_CHAIN (old_parm_type_list); 14541 q = &TREE_CHAIN (*q); 14542 } 14543 if (old_parm_type_list == void_list_node) 14544 *q = void_list_node; 14545 14546 TREE_TYPE (decl) 14547 = rebuild_function_or_method_type (fntype, 14548 TREE_TYPE (fntype), new_parm_type_list, 14549 TYPE_RAISES_EXCEPTIONS (fntype), tf_none); 14550 } 14551 14552 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */ 14553 14554 static tree 14555 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain, 14556 tree lambda_fntype, bool use_spec_table = true) 14557 { 14558 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE; 14559 hashval_t hash = 0; 14560 tree in_decl = t; 14561 14562 /* Nobody should be tsubst'ing into non-template functions. */ 14563 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE 14564 || DECL_LOCAL_DECL_P (t)); 14565 14566 if (DECL_LOCAL_DECL_P (t)) 14567 { 14568 if (tree spec = retrieve_local_specialization (t)) 14569 return spec; 14570 } 14571 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) 14572 { 14573 /* If T is not dependent, just return it. */ 14574 if (!uses_template_parms (DECL_TI_ARGS (t)) 14575 && !LAMBDA_FUNCTION_P (t)) 14576 return t; 14577 14578 /* A non-templated friend doesn't get DECL_TEMPLATE_INFO. */ 14579 if (non_templated_friend_p (t)) 14580 goto friend_case; 14581 14582 /* Calculate the most general template of which R is a 14583 specialization. */ 14584 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); 14585 14586 /* We're substituting a lambda function under tsubst_lambda_expr but not 14587 directly from it; find the matching function we're already inside. 14588 But don't do this if T is a generic lambda with a single level of 14589 template parms, as in that case we're doing a normal instantiation. */ 14590 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype 14591 && (!generic_lambda_fn_p (t) 14592 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1)) 14593 return enclosing_instantiation_of (t); 14594 14595 /* Calculate the complete set of arguments used to 14596 specialize R. */ 14597 if (use_spec_table && !lambda_fntype) 14598 { 14599 argvec = tsubst_template_args (DECL_TI_ARGS 14600 (DECL_TEMPLATE_RESULT 14601 (DECL_TI_TEMPLATE (t))), 14602 args, complain, in_decl); 14603 if (argvec == error_mark_node) 14604 return error_mark_node; 14605 14606 /* Check to see if we already have this specialization. */ 14607 hash = spec_hasher::hash (gen_tmpl, argvec); 14608 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash)) 14609 /* The spec for these args might be a partial instantiation of the 14610 template, but here what we want is the FUNCTION_DECL. */ 14611 return STRIP_TEMPLATE (spec); 14612 } 14613 else 14614 argvec = args; 14615 } 14616 else 14617 { 14618 /* This special case arises when we have something like this: 14619 14620 template <class T> struct S { 14621 friend void f<int>(int, double); 14622 }; 14623 14624 Here, the DECL_TI_TEMPLATE for the friend declaration 14625 will be an IDENTIFIER_NODE. We are being called from 14626 tsubst_friend_function, and we want only to create a 14627 new decl (R) with appropriate types so that we can call 14628 determine_specialization. */ 14629 friend_case: 14630 gen_tmpl = NULL_TREE; 14631 argvec = NULL_TREE; 14632 } 14633 14634 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype) 14635 : NULL_TREE); 14636 tree ctx = closure ? closure : DECL_CONTEXT (t); 14637 bool member = ctx && TYPE_P (ctx); 14638 14639 /* If this is a static or xobj lambda, remove the 'this' pointer added in 14640 tsubst_lambda_expr now that we know the closure type. */ 14641 if (lambda_fntype && !DECL_IOBJ_MEMBER_FUNCTION_P (t)) 14642 lambda_fntype = static_fn_type (lambda_fntype); 14643 14644 if (member && !closure) 14645 ctx = tsubst_aggr_type (ctx, args, 14646 complain, t, /*entering_scope=*/1); 14647 14648 tree type = (lambda_fntype ? lambda_fntype 14649 : tsubst (TREE_TYPE (t), args, 14650 complain | tf_fndecl_type, in_decl)); 14651 if (type == error_mark_node) 14652 return error_mark_node; 14653 14654 /* If we hit excessive deduction depth, the type is bogus even if 14655 it isn't error_mark_node, so don't build a decl. */ 14656 if (excessive_deduction_depth) 14657 return error_mark_node; 14658 14659 /* We do NOT check for matching decls pushed separately at this 14660 point, as they may not represent instantiations of this 14661 template, and in any case are considered separate under the 14662 discrete model. */ 14663 tree r = copy_decl (t); 14664 DECL_USE_TEMPLATE (r) = 0; 14665 TREE_TYPE (r) = type; 14666 /* Clear out the mangled name and RTL for the instantiation. */ 14667 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); 14668 SET_DECL_RTL (r, NULL); 14669 /* Leave DECL_INITIAL set on deleted instantiations. */ 14670 if (!DECL_DELETED_FN (r)) 14671 DECL_INITIAL (r) = NULL_TREE; 14672 DECL_CONTEXT (r) = ctx; 14673 set_instantiating_module (r); 14674 14675 /* Handle explicit(dependent-expr). */ 14676 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t)) 14677 { 14678 tree spec = lookup_explicit_specifier (t); 14679 spec = tsubst_expr (spec, args, complain, in_decl); 14680 spec = build_explicit_specifier (spec, complain); 14681 if (spec == error_mark_node) 14682 return error_mark_node; 14683 if (instantiation_dependent_expression_p (spec)) 14684 store_explicit_specifier (r, spec); 14685 else 14686 { 14687 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node); 14688 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r) = false; 14689 } 14690 } 14691 14692 /* OpenMP UDRs have the only argument a reference to the declared 14693 type. We want to diagnose if the declared type is a reference, 14694 which is invalid, but as references to references are usually 14695 quietly merged, diagnose it here. */ 14696 if (DECL_OMP_DECLARE_REDUCTION_P (t)) 14697 { 14698 tree argtype 14699 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t)))); 14700 argtype = tsubst (argtype, args, complain, in_decl); 14701 if (TYPE_REF_P (argtype)) 14702 error_at (DECL_SOURCE_LOCATION (t), 14703 "reference type %qT in " 14704 "%<#pragma omp declare reduction%>", argtype); 14705 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL) 14706 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t), 14707 argtype); 14708 } 14709 14710 if (member && DECL_CONV_FN_P (r)) 14711 /* Type-conversion operator. Reconstruct the name, in 14712 case it's the name of one of the template's parameters. */ 14713 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type)); 14714 14715 tree parms = DECL_ARGUMENTS (t); 14716 if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t)) 14717 parms = DECL_CHAIN (parms); 14718 parms = tsubst (parms, args, complain, t); 14719 for (tree parm = parms; parm; parm = DECL_CHAIN (parm)) 14720 DECL_CONTEXT (parm) = r; 14721 if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t)) 14722 { 14723 tree tparm = build_this_parm (r, closure, type_memfn_quals (type)); 14724 DECL_NAME (tparm) = closure_identifier; 14725 DECL_CHAIN (tparm) = parms; 14726 parms = tparm; 14727 } 14728 DECL_ARGUMENTS (r) = parms; 14729 DECL_RESULT (r) = NULL_TREE; 14730 14731 maybe_rebuild_function_decl_type (r); 14732 14733 TREE_STATIC (r) = 0; 14734 TREE_PUBLIC (r) = TREE_PUBLIC (t); 14735 DECL_EXTERNAL (r) = 1; 14736 /* If this is an instantiation of a function with internal 14737 linkage, we already know what object file linkage will be 14738 assigned to the instantiation. */ 14739 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r); 14740 DECL_DEFER_OUTPUT (r) = 0; 14741 DECL_CHAIN (r) = NULL_TREE; 14742 DECL_PENDING_INLINE_INFO (r) = 0; 14743 DECL_PENDING_INLINE_P (r) = 0; 14744 DECL_SAVED_TREE (r) = NULL_TREE; 14745 DECL_STRUCT_FUNCTION (r) = NULL; 14746 TREE_USED (r) = 0; 14747 /* We'll re-clone as appropriate in instantiate_template. */ 14748 DECL_CLONED_FUNCTION (r) = NULL_TREE; 14749 14750 /* If we aren't complaining now, return on error before we register 14751 the specialization so that we'll complain eventually. */ 14752 if ((complain & tf_error) == 0 14753 && IDENTIFIER_ANY_OP_P (DECL_NAME (r)) 14754 && !grok_op_properties (r, /*complain=*/false)) 14755 return error_mark_node; 14756 14757 /* If we are looking at an xobj lambda, we might need to check the type of 14758 its xobj parameter. */ 14759 if (LAMBDA_FUNCTION_P (r) && DECL_XOBJ_MEMBER_FUNCTION_P (r)) 14760 { 14761 tree closure_obj = DECL_CONTEXT (r); 14762 tree lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure_obj); 14763 tree obj_param = TREE_TYPE (DECL_ARGUMENTS (r)); 14764 14765 if (!(LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE 14766 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr))) 14767 /* If a lambda has an empty capture clause, an xobj parameter of 14768 unrelated type is not an error. */; 14769 else if (dependent_type_p (obj_param)) 14770 /* If we are coming from tsubst_lambda_expr we might not have 14771 substituted into our xobj parameter yet. We can't error out until 14772 we know what the type really is so do nothing... 14773 ...but if we are instantiating the call op for real and we don't 14774 have a real type then something has gone incredibly wrong. */ 14775 gcc_assert (lambda_fntype); 14776 else 14777 { 14778 /* We have a lambda with captures, and know the type of the xobj 14779 parameter, time to check it. */ 14780 tree obj_param_type = TYPE_MAIN_VARIANT (non_reference (obj_param)); 14781 if (!same_or_base_type_p (closure_obj, obj_param_type)) 14782 { 14783 /* This error does not emit when the lambda's call operator 14784 template is instantiated by taking its address, such as in 14785 the following case: 14786 14787 auto f = [x = 0](this auto&&){}; 14788 int (*fp)(int&) = &decltype(f)::operator(); 14789 14790 It only emits when explicitly calling the call operator with 14791 an explicit template parameter: 14792 14793 template<typename T> 14794 struct S : T { 14795 using T::operator(); 14796 operator int() const {return {};} 14797 }; 14798 14799 auto s = S{[x = 0](this auto&&) {}}; 14800 s.operator()<int>(); 14801 14802 This is due to resolve_address_of_overloaded_function being 14803 deficient at reporting candidates when overload resolution 14804 fails. 14805 14806 This diagnostic will be active in the first case if/when 14807 resolve_address_of_overloaded_function is fixed to properly 14808 emit candidates upon failure to resolve to an overload. */ 14809 if (complain & tf_error) 14810 error ("a lambda with captures may not have an explicit " 14811 "object parameter of an unrelated type"); 14812 return error_mark_node; 14813 } 14814 } 14815 } 14816 14817 /* Associate the constraints directly with the instantiation. We 14818 don't substitute through the constraints; that's only done when 14819 they are checked. */ 14820 if (tree ci = get_constraints (t)) 14821 set_constraints (r, ci); 14822 14823 if (DECL_FRIEND_CONTEXT (t)) 14824 SET_DECL_FRIEND_CONTEXT (r, 14825 tsubst (DECL_FRIEND_CONTEXT (t), 14826 args, complain, in_decl)); 14827 14828 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, 14829 args, complain, in_decl)) 14830 return error_mark_node; 14831 14832 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do 14833 this in the special friend case mentioned above where 14834 GEN_TMPL is NULL. */ 14835 if (gen_tmpl && !closure) 14836 { 14837 DECL_TEMPLATE_INFO (r) 14838 = build_template_info (gen_tmpl, argvec); 14839 SET_DECL_IMPLICIT_INSTANTIATION (r); 14840 14841 if (use_spec_table) 14842 { 14843 tree new_r 14844 = register_specialization (r, gen_tmpl, argvec, false, hash); 14845 if (new_r != r) 14846 /* We instantiated this while substituting into 14847 the type earlier (template/friend54.C). */ 14848 return new_r; 14849 } 14850 14851 /* We're not supposed to instantiate default arguments 14852 until they are called, for a template. But, for a 14853 declaration like: 14854 14855 template <class T> void f () 14856 { extern void g(int i = T()); } 14857 14858 we should do the substitution when the template is 14859 instantiated. We handle the member function case in 14860 instantiate_class_template since the default arguments 14861 might refer to other members of the class. */ 14862 if (!member 14863 && !PRIMARY_TEMPLATE_P (gen_tmpl) 14864 && !uses_template_parms (argvec)) 14865 tsubst_default_arguments (r, complain); 14866 } 14867 else if (DECL_LOCAL_DECL_P (r)) 14868 { 14869 if (!cp_unevaluated_operand) 14870 register_local_specialization (r, t); 14871 } 14872 else 14873 DECL_TEMPLATE_INFO (r) = NULL_TREE; 14874 14875 /* Copy the list of befriending classes. */ 14876 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r); 14877 *friends; 14878 friends = &TREE_CHAIN (*friends)) 14879 { 14880 *friends = copy_node (*friends); 14881 TREE_VALUE (*friends) 14882 = tsubst (TREE_VALUE (*friends), args, complain, in_decl); 14883 } 14884 14885 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r)) 14886 { 14887 maybe_retrofit_in_chrg (r); 14888 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r)) 14889 return error_mark_node; 14890 /* If this is an instantiation of a member template, clone it. 14891 If it isn't, that'll be handled by 14892 clone_constructors_and_destructors. */ 14893 if (gen_tmpl && PRIMARY_TEMPLATE_P (gen_tmpl)) 14894 clone_cdtor (r, /*update_methods=*/false); 14895 } 14896 else if ((complain & tf_error) != 0 14897 && IDENTIFIER_ANY_OP_P (DECL_NAME (r)) 14898 && !grok_op_properties (r, /*complain=*/true)) 14899 return error_mark_node; 14900 14901 /* Possibly limit visibility based on template args. */ 14902 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; 14903 if (DECL_VISIBILITY_SPECIFIED (t)) 14904 { 14905 DECL_VISIBILITY_SPECIFIED (r) = 0; 14906 DECL_ATTRIBUTES (r) 14907 = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); 14908 } 14909 determine_visibility (r); 14910 if (DECL_SECTION_NAME (t)) 14911 set_decl_section_name (r, t); 14912 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r) 14913 && COMPLETE_TYPE_P (DECL_CONTEXT (r)) 14914 && !processing_template_decl) 14915 defaulted_late_check (r); 14916 14917 if (flag_openmp) 14918 if (tree attr = lookup_attribute ("omp declare variant base", 14919 DECL_ATTRIBUTES (r))) 14920 omp_declare_variant_finalize (r, attr); 14921 14922 return r; 14923 } 14924 14925 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */ 14926 14927 static tree 14928 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain, 14929 tree lambda_fntype, tree lambda_tparms) 14930 { 14931 /* We can get here when processing a member function template, 14932 member class template, or template template parameter. */ 14933 tree decl = DECL_TEMPLATE_RESULT (t); 14934 tree in_decl = t; 14935 tree spec; 14936 tree tmpl_args; 14937 tree full_args = NULL_TREE; 14938 tree r; 14939 hashval_t hash = 0; 14940 14941 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) 14942 { 14943 /* Template template parameter is treated here. */ 14944 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); 14945 if (new_type == error_mark_node) 14946 r = error_mark_node; 14947 /* If we get a real template back, return it. This can happen in 14948 the context of most_specialized_partial_spec. */ 14949 else if (TREE_CODE (new_type) == TEMPLATE_DECL) 14950 r = new_type; 14951 else 14952 /* The new TEMPLATE_DECL was built in 14953 reduce_template_parm_level. */ 14954 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type); 14955 return r; 14956 } 14957 14958 if (!lambda_fntype) 14959 { 14960 /* We might already have an instance of this template. 14961 The ARGS are for the surrounding class type, so the 14962 full args contain the tsubst'd args for the context, 14963 plus the innermost args from the template decl. */ 14964 tmpl_args = DECL_CLASS_TEMPLATE_P (t) 14965 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) 14966 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); 14967 /* Because this is a template, the arguments will still be 14968 dependent, even after substitution. If 14969 PROCESSING_TEMPLATE_DECL is not set, the dependency 14970 predicates will short-circuit. */ 14971 ++processing_template_decl; 14972 full_args = tsubst_template_args (tmpl_args, args, 14973 complain, in_decl); 14974 --processing_template_decl; 14975 if (full_args == error_mark_node) 14976 return error_mark_node; 14977 14978 /* If this is a default template template argument, 14979 tsubst might not have changed anything. */ 14980 if (full_args == tmpl_args) 14981 return t; 14982 14983 hash = spec_hasher::hash (t, full_args); 14984 spec = retrieve_specialization (t, full_args, hash); 14985 if (spec != NULL_TREE) 14986 { 14987 if (TYPE_P (spec)) 14988 /* Type partial instantiations are stored as the type by 14989 lookup_template_class_1, not here as the template. */ 14990 spec = CLASSTYPE_TI_TEMPLATE (spec); 14991 else if (TREE_CODE (spec) != TEMPLATE_DECL) 14992 spec = DECL_TI_TEMPLATE (spec); 14993 return spec; 14994 } 14995 } 14996 14997 /* Make a new template decl. It will be similar to the 14998 original, but will record the current template arguments. 14999 We also create a new function declaration, which is just 15000 like the old one, but points to this new template, rather 15001 than the old one. */ 15002 r = copy_decl (t); 15003 gcc_assert (DECL_LANG_SPECIFIC (r) != 0); 15004 DECL_CHAIN (r) = NULL_TREE; 15005 15006 // Build new template info linking to the original template decl. 15007 if (!lambda_fntype) 15008 { 15009 DECL_TEMPLATE_INFO (r) = build_template_info (t, args); 15010 SET_DECL_IMPLICIT_INSTANTIATION (r); 15011 } 15012 else 15013 DECL_TEMPLATE_INFO (r) = NULL_TREE; 15014 15015 /* The template parameters for this new template are all the 15016 template parameters for the old template, except the 15017 outermost level of parameters. */ 15018 auto tparm_guard = make_temp_override (current_template_parms); 15019 DECL_TEMPLATE_PARMS (r) 15020 = current_template_parms 15021 = (lambda_tparms 15022 ? lambda_tparms 15023 : tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, 15024 complain)); 15025 15026 bool class_p = false; 15027 tree inner = decl; 15028 ++processing_template_decl; 15029 if (TREE_CODE (inner) == FUNCTION_DECL) 15030 inner = tsubst_function_decl (inner, args, complain, lambda_fntype, 15031 /*use_spec_table=*/false); 15032 else 15033 { 15034 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner)) 15035 { 15036 class_p = true; 15037 inner = TREE_TYPE (inner); 15038 } 15039 if (class_p) 15040 inner = tsubst_aggr_type (inner, args, complain, 15041 in_decl, /*entering*/1); 15042 else 15043 inner = tsubst_decl (inner, args, complain, /*use_spec_table=*/false); 15044 } 15045 --processing_template_decl; 15046 if (inner == error_mark_node) 15047 return error_mark_node; 15048 15049 if (class_p) 15050 { 15051 /* For a partial specialization, we need to keep pointing to 15052 the primary template. */ 15053 if (!DECL_TEMPLATE_SPECIALIZATION (t)) 15054 { 15055 CLASSTYPE_TI_TEMPLATE (inner) = r; 15056 CLASSTYPE_USE_TEMPLATE (inner) = 0; 15057 } 15058 15059 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner); 15060 inner = TYPE_MAIN_DECL (inner); 15061 } 15062 else if (lambda_fntype) 15063 { 15064 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r)); 15065 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args); 15066 } 15067 else 15068 { 15069 DECL_TI_TEMPLATE (inner) = r; 15070 /* Set DECL_TI_ARGS to the full set of template arguments, 15071 which tsubst_function_decl / tsubst_decl didn't do due to 15072 use_spec_table=false. */ 15073 DECL_TI_ARGS (inner) = full_args; 15074 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner); 15075 } 15076 15077 DECL_TEMPLATE_RESULT (r) = inner; 15078 TREE_TYPE (r) = TREE_TYPE (inner); 15079 DECL_CONTEXT (r) = DECL_CONTEXT (inner); 15080 15081 if (modules_p ()) 15082 { 15083 /* Propagate module information from the decl. */ 15084 DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner); 15085 if (DECL_LANG_SPECIFIC (inner)) 15086 /* If this is a constrained template, the above tsubst of 15087 inner can find the unconstrained template, which may have 15088 come from an import. This is ok, because we don't 15089 register this instantiation (see below). */ 15090 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner) 15091 || (TEMPLATE_PARMS_CONSTRAINTS 15092 (DECL_TEMPLATE_PARMS (t)))); 15093 } 15094 15095 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; 15096 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; 15097 15098 if (PRIMARY_TEMPLATE_P (t)) 15099 DECL_PRIMARY_TEMPLATE (r) = r; 15100 15101 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (r) = false; 15102 15103 if (!lambda_fntype && !class_p) 15104 { 15105 /* Record this non-type partial instantiation. */ 15106 /* FIXME we'd like to always register the TEMPLATE_DECL, or always 15107 the DECL_TEMPLATE_RESULT, but it seems the modules code relies 15108 on this current behavior. */ 15109 if (TREE_CODE (inner) == FUNCTION_DECL) 15110 register_specialization (r, t, full_args, false, hash); 15111 else 15112 register_specialization (inner, t, full_args, false, hash); 15113 } 15114 15115 return r; 15116 } 15117 15118 /* True if FN is the op() for a lambda in an uninstantiated template. */ 15119 15120 bool 15121 lambda_fn_in_template_p (tree fn) 15122 { 15123 if (!fn || !LAMBDA_FUNCTION_P (fn)) 15124 return false; 15125 tree closure = DECL_CONTEXT (fn); 15126 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE; 15127 } 15128 15129 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for 15130 which the above is true. */ 15131 15132 bool 15133 regenerated_lambda_fn_p (tree fn) 15134 { 15135 if (!fn || !LAMBDA_FUNCTION_P (fn)) 15136 return false; 15137 tree closure = DECL_CONTEXT (fn); 15138 tree lam = CLASSTYPE_LAMBDA_EXPR (closure); 15139 return LAMBDA_EXPR_REGEN_INFO (lam) != NULL_TREE; 15140 } 15141 15142 /* Return the LAMBDA_EXPR from which T was ultimately regenerated. 15143 If T is not a regenerated LAMBDA_EXPR, return T. */ 15144 15145 tree 15146 most_general_lambda (tree t) 15147 { 15148 while (tree ti = LAMBDA_EXPR_REGEN_INFO (t)) 15149 t = TI_TEMPLATE (ti); 15150 return t; 15151 } 15152 15153 /* Return the set of template arguments used to regenerate the lambda T 15154 from its most general lambda. */ 15155 15156 tree 15157 lambda_regenerating_args (tree t) 15158 { 15159 if (LAMBDA_FUNCTION_P (t)) 15160 t = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t)); 15161 gcc_assert (TREE_CODE (t) == LAMBDA_EXPR); 15162 if (tree ti = LAMBDA_EXPR_REGEN_INFO (t)) 15163 return TI_ARGS (ti); 15164 else 15165 return NULL_TREE; 15166 } 15167 15168 /* We're instantiating a variable from template function TCTX. Return the 15169 corresponding current enclosing scope. We can match them up using 15170 DECL_SOURCE_LOCATION because lambdas only ever have one source location, and 15171 the DECL_SOURCE_LOCATION for a function instantiation is updated to match 15172 the template definition in regenerate_decl_from_template. */ 15173 15174 static tree 15175 enclosing_instantiation_of (tree tctx) 15176 { 15177 tree fn = current_function_decl; 15178 15179 /* We shouldn't ever need to do this for other artificial functions. */ 15180 gcc_assert (!DECL_ARTIFICIAL (tctx) || LAMBDA_FUNCTION_P (tctx)); 15181 15182 for (; fn; fn = decl_function_context (fn)) 15183 if (DECL_SOURCE_LOCATION (fn) == DECL_SOURCE_LOCATION (tctx)) 15184 return fn; 15185 gcc_unreachable (); 15186 } 15187 15188 /* Substitute the ARGS into the T, which is a _DECL. Return the 15189 result of the substitution. Issue error and warning messages under 15190 control of COMPLAIN. The flag USE_SPEC_TABLE controls if we look up 15191 and insert into the specializations table or if we can assume it's 15192 the caller's responsibility; this is used by instantiate_template 15193 to avoid doing some redundant work. */ 15194 15195 static tree 15196 tsubst_decl (tree t, tree args, tsubst_flags_t complain, 15197 bool use_spec_table /* = true */) 15198 { 15199 #define RETURN(EXP) do { r = (EXP); goto out; } while(0) 15200 location_t saved_loc; 15201 tree r = NULL_TREE; 15202 tree in_decl = t; 15203 hashval_t hash = 0; 15204 15205 if (t == error_mark_node) 15206 return error_mark_node; 15207 15208 /* Set the filename and linenumber to improve error-reporting. */ 15209 saved_loc = input_location; 15210 input_location = DECL_SOURCE_LOCATION (t); 15211 15212 switch (TREE_CODE (t)) 15213 { 15214 case TEMPLATE_DECL: 15215 r = tsubst_template_decl (t, args, complain, 15216 /*lambda_fntype=*/NULL_TREE, 15217 /*lambda_tparms=*/NULL_TREE); 15218 break; 15219 15220 case FUNCTION_DECL: 15221 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE, 15222 use_spec_table); 15223 break; 15224 15225 case PARM_DECL: 15226 { 15227 tree type = NULL_TREE; 15228 int i, len = 1; 15229 tree expanded_types = NULL_TREE; 15230 tree prev_r = NULL_TREE; 15231 tree first_r = NULL_TREE; 15232 15233 if (DECL_PACK_P (t)) 15234 { 15235 /* If there is a local specialization that isn't a 15236 parameter pack, it means that we're doing a "simple" 15237 substitution from inside tsubst_pack_expansion. Just 15238 return the local specialization (which will be a single 15239 parm). */ 15240 tree spec = retrieve_local_specialization (t); 15241 if (spec 15242 && TREE_CODE (spec) == PARM_DECL 15243 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION) 15244 RETURN (spec); 15245 15246 /* Expand the TYPE_PACK_EXPANSION that provides the types for 15247 the parameters in this function parameter pack. */ 15248 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args, 15249 complain, in_decl); 15250 if (TREE_CODE (expanded_types) == TREE_VEC) 15251 { 15252 len = TREE_VEC_LENGTH (expanded_types); 15253 15254 /* Zero-length parameter packs are boring. Just substitute 15255 into the chain. */ 15256 if (len == 0 && !cp_unevaluated_operand) 15257 RETURN (tsubst (TREE_CHAIN (t), args, complain, 15258 TREE_CHAIN (t))); 15259 } 15260 else 15261 { 15262 /* All we did was update the type. Make a note of that. */ 15263 type = expanded_types; 15264 expanded_types = NULL_TREE; 15265 } 15266 } 15267 15268 /* Loop through all of the parameters we'll build. When T is 15269 a function parameter pack, LEN is the number of expanded 15270 types in EXPANDED_TYPES; otherwise, LEN is 1. */ 15271 r = NULL_TREE; 15272 for (i = 0; i < len; ++i) 15273 { 15274 prev_r = r; 15275 r = copy_node (t); 15276 if (DECL_TEMPLATE_PARM_P (t)) 15277 SET_DECL_TEMPLATE_PARM_P (r); 15278 15279 if (expanded_types) 15280 /* We're on the Ith parameter of the function parameter 15281 pack. */ 15282 { 15283 /* Get the Ith type. */ 15284 type = TREE_VEC_ELT (expanded_types, i); 15285 15286 /* Rename the parameter to include the index. */ 15287 DECL_NAME (r) 15288 = make_ith_pack_parameter_name (DECL_NAME (r), i); 15289 } 15290 else if (!type) 15291 /* We're dealing with a normal parameter. */ 15292 type = tsubst (TREE_TYPE (t), args, complain, in_decl); 15293 15294 type = type_decays_to (type); 15295 TREE_TYPE (r) = type; 15296 cp_apply_type_quals_to_decl (cp_type_quals (type), r); 15297 15298 if (DECL_INITIAL (r)) 15299 { 15300 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) 15301 DECL_INITIAL (r) = TREE_TYPE (r); 15302 else 15303 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, 15304 complain, in_decl); 15305 } 15306 15307 DECL_CONTEXT (r) = NULL_TREE; 15308 15309 if (!DECL_TEMPLATE_PARM_P (r)) 15310 DECL_ARG_TYPE (r) = type_passed_as (type); 15311 15312 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, 15313 args, complain, in_decl)) 15314 return error_mark_node; 15315 15316 /* Keep track of the first new parameter we 15317 generate. That's what will be returned to the 15318 caller. */ 15319 if (!first_r) 15320 first_r = r; 15321 15322 /* Build a proper chain of parameters when substituting 15323 into a function parameter pack. */ 15324 if (prev_r) 15325 DECL_CHAIN (prev_r) = r; 15326 } 15327 15328 /* If cp_unevaluated_operand is set, we're just looking for a 15329 single dummy parameter, so don't keep going. */ 15330 if (DECL_CHAIN (t) && !cp_unevaluated_operand) 15331 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args, 15332 complain, DECL_CHAIN (t)); 15333 15334 /* FIRST_R contains the start of the chain we've built. */ 15335 r = first_r; 15336 } 15337 break; 15338 15339 case FIELD_DECL: 15340 { 15341 tree type = NULL_TREE; 15342 tree vec = NULL_TREE; 15343 tree expanded_types = NULL_TREE; 15344 int len = 1; 15345 15346 if (PACK_EXPANSION_P (TREE_TYPE (t))) 15347 { 15348 /* This field is a lambda capture pack. Return a TREE_VEC of 15349 the expanded fields to instantiate_class_template_1. */ 15350 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args, 15351 complain, in_decl); 15352 if (TREE_CODE (expanded_types) == TREE_VEC) 15353 { 15354 len = TREE_VEC_LENGTH (expanded_types); 15355 vec = make_tree_vec (len); 15356 } 15357 else 15358 { 15359 /* All we did was update the type. Make a note of that. */ 15360 type = expanded_types; 15361 expanded_types = NULL_TREE; 15362 } 15363 } 15364 15365 for (int i = 0; i < len; ++i) 15366 { 15367 r = copy_decl (t); 15368 if (expanded_types) 15369 { 15370 type = TREE_VEC_ELT (expanded_types, i); 15371 DECL_NAME (r) 15372 = make_ith_pack_parameter_name (DECL_NAME (r), i); 15373 } 15374 else if (!type) 15375 type = tsubst (TREE_TYPE (t), args, complain, in_decl); 15376 15377 if (type == error_mark_node) 15378 RETURN (error_mark_node); 15379 TREE_TYPE (r) = type; 15380 cp_apply_type_quals_to_decl (cp_type_quals (type), r); 15381 15382 if (DECL_C_BIT_FIELD (r)) 15383 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the 15384 number of bits. */ 15385 DECL_BIT_FIELD_REPRESENTATIVE (r) 15386 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args, 15387 complain, in_decl); 15388 if (DECL_INITIAL (t)) 15389 { 15390 /* Set up DECL_TEMPLATE_INFO so that we can get at the 15391 NSDMI in perform_member_init. Still set DECL_INITIAL 15392 so that we know there is one. */ 15393 DECL_INITIAL (r) = void_node; 15394 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL); 15395 retrofit_lang_decl (r); 15396 DECL_TEMPLATE_INFO (r) = build_template_info (t, args); 15397 } 15398 /* We don't have to set DECL_CONTEXT here; it is set by 15399 finish_member_declaration. */ 15400 DECL_CHAIN (r) = NULL_TREE; 15401 15402 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0, 15403 args, complain, in_decl)) 15404 return error_mark_node; 15405 15406 if (vec) 15407 TREE_VEC_ELT (vec, i) = r; 15408 } 15409 15410 if (vec) 15411 r = vec; 15412 } 15413 break; 15414 15415 case USING_DECL: 15416 /* We reach here only for member using decls. We also need to check 15417 uses_template_parms because DECL_DEPENDENT_P is not set for a 15418 using-declaration that designates a member of the current 15419 instantiation (c++/53549). */ 15420 if (DECL_DEPENDENT_P (t) 15421 || uses_template_parms (USING_DECL_SCOPE (t))) 15422 { 15423 /* True iff this using-decl was written as a pack expansion 15424 (and a pack appeared in its scope or name). If a pack 15425 appeared in both, we expand the packs separately and 15426 manually merge them. */ 15427 bool variadic_p = false; 15428 15429 tree scope = USING_DECL_SCOPE (t); 15430 if (PACK_EXPANSION_P (scope)) 15431 { 15432 scope = tsubst_pack_expansion (scope, args, 15433 complain | tf_qualifying_scope, 15434 in_decl); 15435 variadic_p = true; 15436 } 15437 else 15438 scope = tsubst_scope (scope, args, complain, in_decl); 15439 15440 tree name = DECL_NAME (t); 15441 if (IDENTIFIER_CONV_OP_P (name) 15442 && PACK_EXPANSION_P (TREE_TYPE (name))) 15443 { 15444 name = tsubst_pack_expansion (TREE_TYPE (name), args, 15445 complain, in_decl); 15446 if (name == error_mark_node) 15447 { 15448 r = error_mark_node; 15449 break; 15450 } 15451 for (tree& elt : tree_vec_range (name)) 15452 elt = make_conv_op_name (elt); 15453 variadic_p = true; 15454 } 15455 else 15456 name = tsubst_name (name, args, complain, in_decl); 15457 15458 int len; 15459 if (!variadic_p) 15460 len = 1; 15461 else if (TREE_CODE (scope) == TREE_VEC 15462 && TREE_CODE (name) == TREE_VEC) 15463 { 15464 if (TREE_VEC_LENGTH (scope) != TREE_VEC_LENGTH (name)) 15465 { 15466 error ("mismatched argument pack lengths (%d vs %d)", 15467 TREE_VEC_LENGTH (scope), TREE_VEC_LENGTH (name)); 15468 r = error_mark_node; 15469 break; 15470 } 15471 len = TREE_VEC_LENGTH (scope); 15472 } 15473 else if (TREE_CODE (scope) == TREE_VEC) 15474 len = TREE_VEC_LENGTH (scope); 15475 else /* TREE_CODE (name) == TREE_VEC */ 15476 len = TREE_VEC_LENGTH (name); 15477 15478 r = make_tree_vec (len); 15479 for (int i = 0; i < len; ++i) 15480 { 15481 tree escope = (TREE_CODE (scope) == TREE_VEC 15482 ? TREE_VEC_ELT (scope, i) 15483 : scope); 15484 tree ename = (TREE_CODE (name) == TREE_VEC 15485 ? TREE_VEC_ELT (name, i) 15486 : name); 15487 tree elt = do_class_using_decl (escope, ename); 15488 if (!elt) 15489 { 15490 r = error_mark_node; 15491 break; 15492 } 15493 TREE_PROTECTED (elt) = TREE_PROTECTED (t); 15494 TREE_PRIVATE (elt) = TREE_PRIVATE (t); 15495 TREE_VEC_ELT (r, i) = elt; 15496 } 15497 15498 if (!variadic_p && r != error_mark_node) 15499 r = TREE_VEC_ELT (r, 0); 15500 } 15501 else 15502 { 15503 r = copy_node (t); 15504 DECL_CHAIN (r) = NULL_TREE; 15505 } 15506 break; 15507 15508 case TYPE_DECL: 15509 case VAR_DECL: 15510 { 15511 tree argvec = NULL_TREE; 15512 tree gen_tmpl = NULL_TREE; 15513 tree tmpl = NULL_TREE; 15514 tree type = NULL_TREE; 15515 15516 if (TREE_TYPE (t) == error_mark_node) 15517 RETURN (error_mark_node); 15518 15519 if (TREE_CODE (t) == TYPE_DECL 15520 && t == TYPE_MAIN_DECL (TREE_TYPE (t))) 15521 { 15522 /* If this is the canonical decl, we don't have to 15523 mess with instantiations, and often we can't (for 15524 typename, template type parms and such). Note that 15525 TYPE_NAME is not correct for the above test if 15526 we've copied the type for a typedef. */ 15527 type = tsubst (TREE_TYPE (t), args, complain, in_decl); 15528 if (type == error_mark_node) 15529 RETURN (error_mark_node); 15530 r = TYPE_NAME (type); 15531 break; 15532 } 15533 15534 /* Check to see if we already have the specialization we 15535 need. */ 15536 tree spec = NULL_TREE; 15537 bool local_p = false; 15538 tree ctx = DECL_CONTEXT (t); 15539 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)) 15540 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))) 15541 { 15542 local_p = false; 15543 if (DECL_CLASS_SCOPE_P (t)) 15544 { 15545 ctx = tsubst_aggr_type (ctx, args, 15546 complain, 15547 in_decl, /*entering_scope=*/1); 15548 if (DECL_SELF_REFERENCE_P (t)) 15549 /* The context and type of an injected-class-name are 15550 the same, so we don't need to substitute both. */ 15551 type = ctx; 15552 /* If CTX is unchanged, then T is in fact the 15553 specialization we want. That situation occurs when 15554 referencing a static data member within in its own 15555 class. We can use pointer equality, rather than 15556 same_type_p, because DECL_CONTEXT is always 15557 canonical... */ 15558 if (ctx == DECL_CONTEXT (t) 15559 /* ... unless T is a member template; in which 15560 case our caller can be willing to create a 15561 specialization of that template represented 15562 by T. */ 15563 && !(DECL_TI_TEMPLATE (t) 15564 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))) 15565 spec = t; 15566 } 15567 15568 if (!spec) 15569 { 15570 tmpl = DECL_TI_TEMPLATE (t); 15571 if (use_spec_table) 15572 { 15573 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); 15574 if (argvec == error_mark_node) 15575 RETURN (error_mark_node); 15576 gen_tmpl = most_general_template (tmpl); 15577 hash = spec_hasher::hash (gen_tmpl, argvec); 15578 spec = retrieve_specialization (gen_tmpl, argvec, hash); 15579 } 15580 else 15581 argvec = args; 15582 } 15583 } 15584 else 15585 { 15586 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))) 15587 /* Subsequent calls to pushdecl will fill this in. */ 15588 ctx = NULL_TREE; 15589 /* A local variable. */ 15590 local_p = true; 15591 /* Unless this is a reference to a static variable from an 15592 enclosing function, in which case we need to fill it in now. */ 15593 if (TREE_STATIC (t)) 15594 { 15595 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t)); 15596 if (fn != current_function_decl) 15597 ctx = fn; 15598 } 15599 spec = retrieve_local_specialization (t); 15600 } 15601 /* If we already have the specialization we need, there is 15602 nothing more to do. */ 15603 if (spec) 15604 { 15605 r = spec; 15606 break; 15607 } 15608 15609 /* Create a new node for the specialization we need. */ 15610 if (type == NULL_TREE) 15611 { 15612 if (is_typedef_decl (t)) 15613 type = DECL_ORIGINAL_TYPE (t); 15614 else 15615 type = TREE_TYPE (t); 15616 if (VAR_P (t) 15617 && VAR_HAD_UNKNOWN_BOUND (t) 15618 && type != error_mark_node) 15619 type = strip_array_domain (type); 15620 tsubst_flags_t tcomplain = complain; 15621 if (VAR_P (t)) 15622 tcomplain |= tf_tst_ok; 15623 type = tsubst (type, args, tcomplain, in_decl); 15624 /* Substituting the type might have recursively instantiated this 15625 same alias (c++/86171). */ 15626 if (use_spec_table && gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl) 15627 && (spec = retrieve_specialization (gen_tmpl, argvec, hash))) 15628 { 15629 r = spec; 15630 break; 15631 } 15632 } 15633 if (type == error_mark_node && !(complain & tf_error)) 15634 RETURN (error_mark_node); 15635 r = copy_decl (t); 15636 if (VAR_P (r)) 15637 { 15638 DECL_INITIALIZED_P (r) = 0; 15639 DECL_TEMPLATE_INSTANTIATED (r) = 0; 15640 if (TREE_CODE (type) == FUNCTION_TYPE) 15641 { 15642 /* It may seem that this case cannot occur, since: 15643 15644 typedef void f(); 15645 void g() { f x; } 15646 15647 declares a function, not a variable. However: 15648 15649 typedef void f(); 15650 template <typename T> void g() { T t; } 15651 template void g<f>(); 15652 15653 is an attempt to declare a variable with function 15654 type. */ 15655 error ("variable %qD has function type", 15656 /* R is not yet sufficiently initialized, so we 15657 just use its name. */ 15658 DECL_NAME (r)); 15659 RETURN (error_mark_node); 15660 } 15661 type = complete_type (type); 15662 /* Wait until cp_finish_decl to set this again, to handle 15663 circular dependency (template/instantiate6.C). */ 15664 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0; 15665 type = check_var_type (DECL_NAME (r), type, 15666 DECL_SOURCE_LOCATION (r)); 15667 if (DECL_HAS_VALUE_EXPR_P (t)) 15668 { 15669 tree ve = DECL_VALUE_EXPR (t); 15670 /* If the DECL_VALUE_EXPR is converted to the declared type, 15671 preserve the identity so that gimplify_type_sizes works. */ 15672 bool nop = (TREE_CODE (ve) == NOP_EXPR); 15673 if (nop) 15674 ve = TREE_OPERAND (ve, 0); 15675 ve = tsubst_expr (ve, args, complain, in_decl); 15676 if (REFERENCE_REF_P (ve)) 15677 { 15678 gcc_assert (TYPE_REF_P (type)); 15679 ve = TREE_OPERAND (ve, 0); 15680 } 15681 if (nop) 15682 ve = build_nop (type, ve); 15683 else if (DECL_LANG_SPECIFIC (t) 15684 && DECL_OMP_PRIVATIZED_MEMBER (t) 15685 && TREE_CODE (ve) == COMPONENT_REF 15686 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL 15687 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type) 15688 type = TREE_TYPE (ve); 15689 else 15690 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve)) 15691 == TYPE_MAIN_VARIANT (type)); 15692 SET_DECL_VALUE_EXPR (r, ve); 15693 } 15694 if (CP_DECL_THREAD_LOCAL_P (r) 15695 && !processing_template_decl) 15696 set_decl_tls_model (r, decl_default_tls_model (r)); 15697 } 15698 else if (DECL_SELF_REFERENCE_P (t)) 15699 SET_DECL_SELF_REFERENCE_P (r); 15700 TREE_TYPE (r) = type; 15701 cp_apply_type_quals_to_decl (cp_type_quals (type), r); 15702 DECL_CONTEXT (r) = ctx; 15703 /* Clear out the mangled name and RTL for the instantiation. */ 15704 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); 15705 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) 15706 SET_DECL_RTL (r, NULL); 15707 set_instantiating_module (r); 15708 15709 /* The initializer must not be expanded until it is required; 15710 see [temp.inst]. */ 15711 DECL_INITIAL (r) = NULL_TREE; 15712 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; 15713 if (VAR_P (r)) 15714 { 15715 if (DECL_LANG_SPECIFIC (r)) 15716 SET_DECL_DEPENDENT_INIT_P (r, false); 15717 15718 SET_DECL_MODE (r, VOIDmode); 15719 15720 /* Possibly limit visibility based on template args. */ 15721 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; 15722 if (DECL_VISIBILITY_SPECIFIED (t)) 15723 { 15724 DECL_VISIBILITY_SPECIFIED (r) = 0; 15725 DECL_ATTRIBUTES (r) 15726 = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); 15727 } 15728 determine_visibility (r); 15729 if ((!local_p || TREE_STATIC (t)) && DECL_SECTION_NAME (t)) 15730 set_decl_section_name (r, t); 15731 } 15732 15733 if (!local_p) 15734 { 15735 /* A static data member declaration is always marked 15736 external when it is declared in-class, even if an 15737 initializer is present. We mimic the non-template 15738 processing here. */ 15739 DECL_EXTERNAL (r) = 1; 15740 if (DECL_NAMESPACE_SCOPE_P (t)) 15741 DECL_NOT_REALLY_EXTERN (r) = 1; 15742 15743 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec); 15744 SET_DECL_IMPLICIT_INSTANTIATION (r); 15745 if (use_spec_table) 15746 register_specialization (r, gen_tmpl, argvec, false, hash); 15747 } 15748 else 15749 { 15750 if (DECL_LANG_SPECIFIC (r)) 15751 DECL_TEMPLATE_INFO (r) = NULL_TREE; 15752 if (!cp_unevaluated_operand) 15753 register_local_specialization (r, t); 15754 } 15755 15756 DECL_CHAIN (r) = NULL_TREE; 15757 15758 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 15759 /*flags=*/0, 15760 args, complain, in_decl)) 15761 return error_mark_node; 15762 15763 /* Preserve a typedef that names a type. */ 15764 if (is_typedef_decl (r) && type != error_mark_node) 15765 { 15766 DECL_ORIGINAL_TYPE (r) = NULL_TREE; 15767 set_underlying_type (r); 15768 15769 /* common_handle_aligned_attribute doesn't apply the alignment 15770 to DECL_ORIGINAL_TYPE. */ 15771 if (TYPE_USER_ALIGN (TREE_TYPE (t))) 15772 TREE_TYPE (r) = build_aligned_type (TREE_TYPE (r), 15773 TYPE_ALIGN (TREE_TYPE (t))); 15774 } 15775 15776 layout_decl (r, 0); 15777 } 15778 break; 15779 15780 default: 15781 gcc_unreachable (); 15782 } 15783 #undef RETURN 15784 15785 out: 15786 /* Restore the file and line information. */ 15787 input_location = saved_loc; 15788 15789 return r; 15790 } 15791 15792 /* Substitute into the complete parameter type list PARMS. */ 15793 15794 tree 15795 tsubst_function_parms (tree parms, 15796 tree args, 15797 tsubst_flags_t complain, 15798 tree in_decl) 15799 { 15800 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl); 15801 } 15802 15803 /* Substitute into the ARG_TYPES of a function type. 15804 If END is a TREE_CHAIN, leave it and any following types 15805 un-substituted. */ 15806 15807 static tree 15808 tsubst_arg_types (tree arg_types, 15809 tree args, 15810 tree end, 15811 tsubst_flags_t complain, 15812 tree in_decl) 15813 { 15814 tree type = NULL_TREE; 15815 int len = 1; 15816 tree expanded_args = NULL_TREE; 15817 15818 if (!arg_types || arg_types == void_list_node || arg_types == end) 15819 return arg_types; 15820 15821 if (PACK_EXPANSION_P (TREE_VALUE (arg_types))) 15822 { 15823 /* For a pack expansion, perform substitution on the 15824 entire expression. Later on, we'll handle the arguments 15825 one-by-one. */ 15826 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types), 15827 args, complain, in_decl); 15828 15829 if (TREE_CODE (expanded_args) == TREE_VEC) 15830 /* So that we'll spin through the parameters, one by one. */ 15831 len = TREE_VEC_LENGTH (expanded_args); 15832 else 15833 { 15834 /* We only partially substituted into the parameter 15835 pack. Our type is TYPE_PACK_EXPANSION. */ 15836 type = expanded_args; 15837 expanded_args = NULL_TREE; 15838 } 15839 } 15840 else 15841 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); 15842 15843 /* Check if a substituted type is erroneous before substituting into 15844 the rest of the chain. */ 15845 for (int i = 0; i < len; i++) 15846 { 15847 if (expanded_args) 15848 type = TREE_VEC_ELT (expanded_args, i); 15849 15850 if (type == error_mark_node) 15851 return error_mark_node; 15852 if (VOID_TYPE_P (type)) 15853 { 15854 if (complain & tf_error) 15855 { 15856 error ("invalid parameter type %qT", type); 15857 if (in_decl) 15858 error ("in declaration %q+D", in_decl); 15859 } 15860 return error_mark_node; 15861 } 15862 } 15863 15864 /* We do not substitute into default arguments here. The standard 15865 mandates that they be instantiated only when needed, which is 15866 done in build_over_call. */ 15867 tree default_arg = TREE_PURPOSE (arg_types); 15868 15869 /* Except that we do substitute default arguments under tsubst_lambda_expr, 15870 since the new op() won't have any associated template arguments for us 15871 to refer to later. */ 15872 if (lambda_fn_in_template_p (in_decl) 15873 || (in_decl && TREE_CODE (in_decl) == FUNCTION_DECL 15874 && DECL_LOCAL_DECL_P (in_decl))) 15875 default_arg = tsubst_expr (default_arg, args, complain, in_decl); 15876 15877 tree remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), 15878 args, end, complain, in_decl); 15879 if (remaining_arg_types == error_mark_node) 15880 return error_mark_node; 15881 15882 for (int i = len-1; i >= 0; i--) 15883 { 15884 if (expanded_args) 15885 type = TREE_VEC_ELT (expanded_args, i); 15886 15887 /* Do array-to-pointer, function-to-pointer conversion, and ignore 15888 top-level qualifiers as required. */ 15889 type = cv_unqualified (type_decays_to (type)); 15890 15891 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE) 15892 { 15893 /* We've instantiated a template before its default arguments 15894 have been parsed. This can happen for a nested template 15895 class, and is not an error unless we require the default 15896 argument in a call of this function. */ 15897 remaining_arg_types 15898 = tree_cons (default_arg, type, remaining_arg_types); 15899 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg), 15900 remaining_arg_types); 15901 } 15902 else 15903 remaining_arg_types 15904 = hash_tree_cons (default_arg, type, remaining_arg_types); 15905 } 15906 15907 return remaining_arg_types; 15908 } 15909 15910 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does 15911 *not* handle the exception-specification for FNTYPE, because the 15912 initial substitution of explicitly provided template parameters 15913 during argument deduction forbids substitution into the 15914 exception-specification: 15915 15916 [temp.deduct] 15917 15918 All references in the function type of the function template to the 15919 corresponding template parameters are replaced by the specified tem- 15920 plate argument values. If a substitution in a template parameter or 15921 in the function type of the function template results in an invalid 15922 type, type deduction fails. [Note: The equivalent substitution in 15923 exception specifications is done only when the function is instanti- 15924 ated, at which point a program is ill-formed if the substitution 15925 results in an invalid type.] */ 15926 15927 static tree 15928 tsubst_function_type (tree t, 15929 tree args, 15930 tsubst_flags_t complain, 15931 tree in_decl) 15932 { 15933 tree return_type; 15934 tree arg_types = NULL_TREE; 15935 15936 /* The TYPE_CONTEXT is not used for function/method types. */ 15937 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE); 15938 15939 /* DR 1227: Mixing immediate and non-immediate contexts in deduction 15940 failure. */ 15941 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t); 15942 15943 if (late_return_type_p) 15944 { 15945 /* Substitute the argument types. */ 15946 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE, 15947 complain, in_decl); 15948 if (arg_types == error_mark_node) 15949 return error_mark_node; 15950 15951 tree save_ccp = current_class_ptr; 15952 tree save_ccr = current_class_ref; 15953 tree this_type = (TREE_CODE (t) == METHOD_TYPE 15954 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE); 15955 bool do_inject = this_type && CLASS_TYPE_P (this_type); 15956 if (do_inject) 15957 { 15958 /* DR 1207: 'this' is in scope in the trailing return type. */ 15959 inject_this_parameter (this_type, cp_type_quals (this_type)); 15960 } 15961 15962 /* Substitute the return type. */ 15963 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); 15964 15965 if (do_inject) 15966 { 15967 current_class_ptr = save_ccp; 15968 current_class_ref = save_ccr; 15969 } 15970 } 15971 else 15972 /* Substitute the return type. */ 15973 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); 15974 15975 if (return_type == error_mark_node) 15976 return error_mark_node; 15977 /* DR 486 clarifies that creation of a function type with an 15978 invalid return type is a deduction failure. */ 15979 if (TREE_CODE (return_type) == ARRAY_TYPE 15980 || TREE_CODE (return_type) == FUNCTION_TYPE) 15981 { 15982 if (complain & tf_error) 15983 { 15984 if (TREE_CODE (return_type) == ARRAY_TYPE) 15985 error ("function returning an array"); 15986 else 15987 error ("function returning a function"); 15988 } 15989 return error_mark_node; 15990 } 15991 15992 if (!late_return_type_p) 15993 { 15994 /* Substitute the argument types. */ 15995 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE, 15996 complain, in_decl); 15997 if (arg_types == error_mark_node) 15998 return error_mark_node; 15999 } 16000 16001 /* Construct a new type node and return it. */ 16002 return rebuild_function_or_method_type (t, return_type, arg_types, 16003 /*raises=*/NULL_TREE, complain); 16004 } 16005 16006 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template 16007 ARGS into that specification, and return the substituted 16008 specification. If there is no specification, return NULL_TREE. */ 16009 16010 static tree 16011 tsubst_exception_specification (tree fntype, 16012 tree args, 16013 tsubst_flags_t complain, 16014 tree in_decl, 16015 bool defer_ok) 16016 { 16017 tree specs; 16018 tree new_specs; 16019 16020 specs = TYPE_RAISES_EXCEPTIONS (fntype); 16021 new_specs = NULL_TREE; 16022 if (specs && TREE_PURPOSE (specs)) 16023 { 16024 /* A noexcept-specifier. */ 16025 tree expr = TREE_PURPOSE (specs); 16026 if (TREE_CODE (expr) == INTEGER_CST) 16027 new_specs = expr; 16028 else if (defer_ok) 16029 { 16030 /* Defer instantiation of noexcept-specifiers to avoid 16031 excessive instantiations (c++/49107). */ 16032 new_specs = make_node (DEFERRED_NOEXCEPT); 16033 if (DEFERRED_NOEXCEPT_SPEC_P (specs)) 16034 { 16035 /* We already partially instantiated this member template, 16036 so combine the new args with the old. */ 16037 DEFERRED_NOEXCEPT_PATTERN (new_specs) 16038 = DEFERRED_NOEXCEPT_PATTERN (expr); 16039 DEFERRED_NOEXCEPT_ARGS (new_specs) 16040 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args); 16041 } 16042 else 16043 { 16044 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr; 16045 DEFERRED_NOEXCEPT_ARGS (new_specs) = args; 16046 } 16047 } 16048 else 16049 { 16050 if (DEFERRED_NOEXCEPT_SPEC_P (specs)) 16051 { 16052 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), 16053 args); 16054 expr = DEFERRED_NOEXCEPT_PATTERN (expr); 16055 } 16056 new_specs = tsubst_expr (expr, args, complain, in_decl); 16057 } 16058 new_specs = build_noexcept_spec (new_specs, complain); 16059 /* We've instantiated a template before a noexcept-specifier 16060 contained therein has been parsed. This can happen for 16061 a nested template class: 16062 16063 struct S { 16064 template<typename> struct B { B() noexcept(...); }; 16065 struct A : B<int> { ... use B() ... }; 16066 }; 16067 16068 where completing B<int> will trigger instantiating the 16069 noexcept, even though we only parse it at the end of S. */ 16070 if (UNPARSED_NOEXCEPT_SPEC_P (specs)) 16071 { 16072 gcc_checking_assert (defer_ok); 16073 vec_safe_push (DEFPARSE_INSTANTIATIONS (expr), new_specs); 16074 } 16075 } 16076 else if (specs) 16077 { 16078 if (! TREE_VALUE (specs)) 16079 new_specs = specs; 16080 else 16081 while (specs) 16082 { 16083 tree spec; 16084 int i, len = 1; 16085 tree expanded_specs = NULL_TREE; 16086 16087 if (PACK_EXPANSION_P (TREE_VALUE (specs))) 16088 { 16089 /* Expand the pack expansion type. */ 16090 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs), 16091 args, complain, 16092 in_decl); 16093 16094 if (expanded_specs == error_mark_node) 16095 return error_mark_node; 16096 else if (TREE_CODE (expanded_specs) == TREE_VEC) 16097 len = TREE_VEC_LENGTH (expanded_specs); 16098 else 16099 { 16100 /* We're substituting into a member template, so 16101 we got a TYPE_PACK_EXPANSION back. Add that 16102 expansion and move on. */ 16103 gcc_assert (TREE_CODE (expanded_specs) 16104 == TYPE_PACK_EXPANSION); 16105 new_specs = add_exception_specifier (new_specs, 16106 expanded_specs, 16107 complain); 16108 specs = TREE_CHAIN (specs); 16109 continue; 16110 } 16111 } 16112 16113 for (i = 0; i < len; ++i) 16114 { 16115 if (expanded_specs) 16116 spec = TREE_VEC_ELT (expanded_specs, i); 16117 else 16118 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl); 16119 if (spec == error_mark_node) 16120 return spec; 16121 new_specs = add_exception_specifier (new_specs, spec, 16122 complain); 16123 } 16124 16125 specs = TREE_CHAIN (specs); 16126 } 16127 } 16128 return new_specs; 16129 } 16130 16131 /* Substitute through a TREE_LIST of types or expressions, handling pack 16132 expansions. */ 16133 16134 tree 16135 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl) 16136 { 16137 if (t == void_list_node) 16138 return t; 16139 16140 tree purpose = TREE_PURPOSE (t); 16141 tree purposevec = NULL_TREE; 16142 if (!purpose) 16143 ; 16144 else if (PACK_EXPANSION_P (purpose)) 16145 { 16146 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl); 16147 if (TREE_CODE (purpose) == TREE_VEC) 16148 purposevec = purpose; 16149 } 16150 else if (TYPE_P (purpose)) 16151 purpose = tsubst (purpose, args, complain, in_decl); 16152 else 16153 purpose = tsubst_expr (purpose, args, complain, in_decl); 16154 if (purpose == error_mark_node || purposevec == error_mark_node) 16155 return error_mark_node; 16156 16157 tree value = TREE_VALUE (t); 16158 tree valuevec = NULL_TREE; 16159 if (!value) 16160 ; 16161 else if (PACK_EXPANSION_P (value)) 16162 { 16163 value = tsubst_pack_expansion (value, args, complain, in_decl); 16164 if (TREE_CODE (value) == TREE_VEC) 16165 valuevec = value; 16166 } 16167 else if (TYPE_P (value)) 16168 value = tsubst (value, args, complain, in_decl); 16169 else 16170 value = tsubst_expr (value, args, complain, in_decl); 16171 if (value == error_mark_node || valuevec == error_mark_node) 16172 return error_mark_node; 16173 16174 tree chain = TREE_CHAIN (t); 16175 if (!chain) 16176 ; 16177 else if (TREE_CODE (chain) == TREE_LIST) 16178 chain = tsubst_tree_list (chain, args, complain, in_decl); 16179 else if (TYPE_P (chain)) 16180 chain = tsubst (chain, args, complain, in_decl); 16181 else 16182 chain = tsubst_expr (chain, args, complain, in_decl); 16183 if (chain == error_mark_node) 16184 return error_mark_node; 16185 16186 if (purpose == TREE_PURPOSE (t) 16187 && value == TREE_VALUE (t) 16188 && chain == TREE_CHAIN (t)) 16189 return t; 16190 16191 int len; 16192 /* Determine the number of arguments. */ 16193 if (purposevec) 16194 { 16195 len = TREE_VEC_LENGTH (purposevec); 16196 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec)); 16197 } 16198 else if (valuevec) 16199 len = TREE_VEC_LENGTH (valuevec); 16200 else 16201 len = 1; 16202 16203 for (int i = len; i-- > 0; ) 16204 { 16205 if (purposevec) 16206 purpose = TREE_VEC_ELT (purposevec, i); 16207 if (valuevec) 16208 value = TREE_VEC_ELT (valuevec, i); 16209 16210 if (value && TYPE_P (value)) 16211 chain = hash_tree_cons (purpose, value, chain); 16212 else 16213 chain = tree_cons (purpose, value, chain); 16214 } 16215 16216 return chain; 16217 } 16218 16219 /* Take the tree structure T and replace template parameters used 16220 therein with the argument vector ARGS. IN_DECL is an associated 16221 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. 16222 Issue error and warning messages under control of COMPLAIN. Note 16223 that we must be relatively non-tolerant of extensions here, in 16224 order to preserve conformance; if we allow substitutions that 16225 should not be allowed, we may allow argument deductions that should 16226 not succeed, and therefore report ambiguous overload situations 16227 where there are none. In theory, we could allow the substitution, 16228 but indicate that it should have failed, and allow our caller to 16229 make sure that the right thing happens, but we don't try to do this 16230 yet. 16231 16232 This function is used for dealing with types, decls and the like; 16233 for expressions, use tsubst_expr or tsubst_copy. */ 16234 16235 tree 16236 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) 16237 { 16238 enum tree_code code; 16239 tree type, r = NULL_TREE; 16240 16241 if (t == NULL_TREE || t == error_mark_node 16242 || t == integer_type_node 16243 || t == void_type_node 16244 || t == char_type_node 16245 || t == unknown_type_node 16246 || TREE_CODE (t) == NAMESPACE_DECL 16247 || TREE_CODE (t) == TRANSLATION_UNIT_DECL) 16248 return t; 16249 16250 tsubst_flags_t tst_ok_flag = (complain & tf_tst_ok); 16251 complain &= ~tf_tst_ok; 16252 16253 tsubst_flags_t qualifying_scope_flag = (complain & tf_qualifying_scope); 16254 complain &= ~tf_qualifying_scope; 16255 16256 if (DECL_P (t)) 16257 return tsubst_decl (t, args, complain); 16258 16259 if (args == NULL_TREE) 16260 return t; 16261 16262 code = TREE_CODE (t); 16263 16264 gcc_assert (code != IDENTIFIER_NODE); 16265 type = TREE_TYPE (t); 16266 16267 gcc_assert (type != unknown_type_node); 16268 16269 if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl)) 16270 return d; 16271 16272 /* Reuse typedefs. We need to do this to handle dependent attributes, 16273 such as attribute aligned. */ 16274 if (TYPE_P (t) 16275 && typedef_variant_p (t)) 16276 { 16277 tree decl = TYPE_NAME (t); 16278 16279 if (alias_template_specialization_p (t, nt_opaque)) 16280 { 16281 /* DECL represents an alias template and we want to 16282 instantiate it. */ 16283 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); 16284 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl); 16285 r = instantiate_alias_template (tmpl, gen_args, complain); 16286 } 16287 else if (DECL_CLASS_SCOPE_P (decl) 16288 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl)) 16289 && uses_template_parms (DECL_CONTEXT (decl))) 16290 { 16291 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); 16292 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl); 16293 r = retrieve_specialization (tmpl, gen_args, 0); 16294 } 16295 else if (DECL_FUNCTION_SCOPE_P (decl) 16296 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl)) 16297 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl)))) 16298 r = retrieve_local_specialization (decl); 16299 else 16300 /* The typedef is from a non-template context. */ 16301 return t; 16302 16303 if (r) 16304 { 16305 r = TREE_TYPE (r); 16306 r = cp_build_qualified_type 16307 (r, cp_type_quals (t) | cp_type_quals (r), 16308 complain | tf_ignore_bad_quals); 16309 return r; 16310 } 16311 else 16312 { 16313 /* We don't have an instantiation yet, so drop the typedef. */ 16314 int quals = cp_type_quals (t); 16315 t = DECL_ORIGINAL_TYPE (decl); 16316 t = cp_build_qualified_type (t, quals, 16317 complain | tf_ignore_bad_quals); 16318 } 16319 } 16320 16321 bool fndecl_type = (complain & tf_fndecl_type); 16322 complain &= ~tf_fndecl_type; 16323 16324 if (type 16325 && code != TYPENAME_TYPE 16326 && code != TEMPLATE_TYPE_PARM 16327 && code != TEMPLATE_PARM_INDEX 16328 && code != IDENTIFIER_NODE 16329 && code != FUNCTION_TYPE 16330 && code != METHOD_TYPE) 16331 type = tsubst (type, args, complain, in_decl); 16332 if (type == error_mark_node) 16333 return error_mark_node; 16334 16335 switch (code) 16336 { 16337 case RECORD_TYPE: 16338 if (TYPE_PTRMEMFUNC_P (t)) 16339 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl); 16340 /* Fall through. */ 16341 case UNION_TYPE: 16342 case ENUMERAL_TYPE: 16343 return tsubst_aggr_type_1 (t, args, complain, in_decl, 16344 /*entering_scope=*/0); 16345 16346 case ERROR_MARK: 16347 case IDENTIFIER_NODE: 16348 case VOID_TYPE: 16349 case OPAQUE_TYPE: 16350 case REAL_TYPE: 16351 case COMPLEX_TYPE: 16352 case VECTOR_TYPE: 16353 case BOOLEAN_TYPE: 16354 case NULLPTR_TYPE: 16355 case LANG_TYPE: 16356 return t; 16357 16358 case INTEGER_TYPE: 16359 if (t == integer_type_node) 16360 return t; 16361 16362 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST 16363 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) 16364 return t; 16365 16366 { 16367 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); 16368 16369 max = tsubst_expr (omax, args, complain, in_decl); 16370 16371 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if 16372 needed. */ 16373 if (TREE_CODE (max) == NOP_EXPR 16374 && TREE_SIDE_EFFECTS (omax) 16375 && !TREE_TYPE (max)) 16376 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0)); 16377 16378 /* If we're in a partial instantiation, preserve the magic NOP_EXPR 16379 with TREE_SIDE_EFFECTS that indicates this is not an integral 16380 constant expression. */ 16381 if (processing_template_decl 16382 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR) 16383 { 16384 gcc_assert (TREE_CODE (max) == NOP_EXPR); 16385 TREE_SIDE_EFFECTS (max) = 1; 16386 } 16387 16388 return compute_array_index_type (NULL_TREE, max, complain); 16389 } 16390 16391 case TEMPLATE_TYPE_PARM: 16392 if (TEMPLATE_TYPE_LEVEL (t) == 0) 16393 { 16394 /* This is either an ordinary level-less auto or a CTAD placeholder 16395 auto. These get replaced only via do_auto_deduction which, in the 16396 ordinary case, temporarily overrides its level to 1 before calling 16397 tsubst. CTAD placeholders are replaced via do_class_deduction. */ 16398 gcc_checking_assert (is_auto (t)); 16399 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (t); 16400 if (!tmpl) 16401 /* Ordinary level-less auto has nothing to substitute. */ 16402 return t; 16403 16404 /* Substitute the template of this CTAD placeholder. */ 16405 tmpl = tsubst_expr (tmpl, args, complain, in_decl); 16406 if (TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM) 16407 tmpl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl); 16408 16409 if (tmpl != CLASS_PLACEHOLDER_TEMPLATE (t)) 16410 return make_template_placeholder (tmpl); 16411 else 16412 return t; 16413 } 16414 /* Fall through. */ 16415 case TEMPLATE_TEMPLATE_PARM: 16416 case BOUND_TEMPLATE_TEMPLATE_PARM: 16417 case TEMPLATE_PARM_INDEX: 16418 { 16419 int idx; 16420 int level; 16421 int levels; 16422 tree arg = NULL_TREE; 16423 16424 r = NULL_TREE; 16425 16426 gcc_assert (TREE_VEC_LENGTH (args) > 0); 16427 template_parm_level_and_index (t, &level, &idx); 16428 16429 levels = TMPL_ARGS_DEPTH (args); 16430 if (level <= levels 16431 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0) 16432 { 16433 arg = TMPL_ARG (args, level, idx); 16434 16435 /* See through ARGUMENT_PACK_SELECT arguments. */ 16436 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT) 16437 arg = argument_pack_select_arg (arg); 16438 } 16439 16440 if (arg == error_mark_node) 16441 return error_mark_node; 16442 else if (arg != NULL_TREE) 16443 { 16444 if (ARGUMENT_PACK_P (arg)) 16445 /* If ARG is an argument pack, we don't actually want to 16446 perform a substitution here, because substitutions 16447 for argument packs are only done 16448 element-by-element. We can get to this point when 16449 substituting the type of a non-type template 16450 parameter pack, when that type actually contains 16451 template parameter packs from an outer template, e.g., 16452 16453 template<typename... Types> struct A { 16454 template<Types... Values> struct B { }; 16455 }; */ 16456 return t; 16457 16458 if (code == TEMPLATE_TYPE_PARM) 16459 { 16460 int quals; 16461 16462 /* When building concept checks for the purpose of 16463 deducing placeholders, we can end up with wildcards 16464 where types are expected. Adjust this to the deduced 16465 value. */ 16466 if (TREE_CODE (arg) == WILDCARD_DECL) 16467 arg = TREE_TYPE (TREE_TYPE (arg)); 16468 16469 gcc_assert (TYPE_P (arg)); 16470 16471 quals = cp_type_quals (arg) | cp_type_quals (t); 16472 16473 return cp_build_qualified_type 16474 (arg, quals, complain | tf_ignore_bad_quals); 16475 } 16476 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM) 16477 { 16478 /* We are processing a type constructed from a 16479 template template parameter. */ 16480 tree argvec = tsubst (TYPE_TI_ARGS (t), 16481 args, complain, in_decl); 16482 if (argvec == error_mark_node) 16483 return error_mark_node; 16484 16485 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM 16486 || TREE_CODE (arg) == TEMPLATE_DECL 16487 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); 16488 16489 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE) 16490 /* Consider this code: 16491 16492 template <template <class> class Template> 16493 struct Internal { 16494 template <class Arg> using Bind = Template<Arg>; 16495 }; 16496 16497 template <template <class> class Template, class Arg> 16498 using Instantiate = Template<Arg>; //#0 16499 16500 template <template <class> class Template, 16501 class Argument> 16502 using Bind = 16503 Instantiate<Internal<Template>::template Bind, 16504 Argument>; //#1 16505 16506 When #1 is parsed, the 16507 BOUND_TEMPLATE_TEMPLATE_PARM representing the 16508 parameter `Template' in #0 matches the 16509 UNBOUND_CLASS_TEMPLATE representing the argument 16510 `Internal<Template>::template Bind'; We then want 16511 to assemble the type `Bind<Argument>' that can't 16512 be fully created right now, because 16513 `Internal<Template>' not being complete, the Bind 16514 template cannot be looked up in that context. So 16515 we need to "store" `Bind<Argument>' for later 16516 when the context of Bind becomes complete. Let's 16517 store that in a TYPENAME_TYPE. */ 16518 return make_typename_type (TYPE_CONTEXT (arg), 16519 build_nt (TEMPLATE_ID_EXPR, 16520 TYPE_IDENTIFIER (arg), 16521 argvec), 16522 typename_type, 16523 complain); 16524 16525 /* We can get a TEMPLATE_TEMPLATE_PARM here when we 16526 are resolving nested-types in the signature of a 16527 member function templates. Otherwise ARG is a 16528 TEMPLATE_DECL and is the real template to be 16529 instantiated. */ 16530 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) 16531 arg = TYPE_NAME (arg); 16532 16533 r = lookup_template_class (arg, 16534 argvec, in_decl, 16535 DECL_CONTEXT (arg), 16536 /*entering_scope=*/0, 16537 complain); 16538 return cp_build_qualified_type 16539 (r, cp_type_quals (t) | cp_type_quals (r), complain); 16540 } 16541 else if (code == TEMPLATE_TEMPLATE_PARM) 16542 return arg; 16543 else 16544 /* TEMPLATE_PARM_INDEX. */ 16545 return convert_from_reference (unshare_expr (arg)); 16546 } 16547 16548 if (level == 1) 16549 /* This can happen during the attempted tsubst'ing in 16550 unify. This means that we don't yet have any information 16551 about the template parameter in question. */ 16552 return t; 16553 16554 /* Early in template argument deduction substitution, we don't 16555 want to reduce the level of 'auto', or it will be confused 16556 with a normal template parm in subsequent deduction. 16557 Similarly, don't reduce the level of template parameters to 16558 avoid mismatches when deducing their types. */ 16559 if (complain & tf_partial) 16560 return t; 16561 16562 /* If we get here, we must have been looking at a parm for a 16563 more deeply nested template. Make a new version of this 16564 template parameter, but with a lower level. */ 16565 int quals; 16566 switch (code) 16567 { 16568 case TEMPLATE_TYPE_PARM: 16569 case TEMPLATE_TEMPLATE_PARM: 16570 quals = cp_type_quals (t); 16571 if (quals) 16572 { 16573 gcc_checking_assert (code == TEMPLATE_TYPE_PARM); 16574 t = TYPE_MAIN_VARIANT (t); 16575 } 16576 16577 if (tree d = TEMPLATE_TYPE_DESCENDANTS (t)) 16578 if (TEMPLATE_PARM_LEVEL (d) == TEMPLATE_TYPE_LEVEL (t) - levels 16579 && (code == TEMPLATE_TYPE_PARM 16580 || TEMPLATE_TEMPLATE_PARM_SIMPLE_P (t))) 16581 /* Cache lowering a type parameter or a simple template 16582 template parameter. */ 16583 r = TREE_TYPE (d); 16584 16585 if (!r) 16586 { 16587 r = copy_type (t); 16588 TEMPLATE_TYPE_PARM_INDEX (r) 16589 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), 16590 r, levels, args, complain); 16591 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); 16592 TYPE_MAIN_VARIANT (r) = r; 16593 TYPE_POINTER_TO (r) = NULL_TREE; 16594 TYPE_REFERENCE_TO (r) = NULL_TREE; 16595 16596 if (code == TEMPLATE_TYPE_PARM) 16597 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t)) 16598 /* Propagate constraints on placeholders since they are 16599 only instantiated during satisfaction. */ 16600 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r) = ci; 16601 16602 if (TYPE_STRUCTURAL_EQUALITY_P (t)) 16603 SET_TYPE_STRUCTURAL_EQUALITY (r); 16604 else 16605 TYPE_CANONICAL (r) = canonical_type_parameter (r); 16606 } 16607 16608 if (quals) 16609 r = cp_build_qualified_type (r, quals, 16610 complain | tf_ignore_bad_quals); 16611 break; 16612 16613 case BOUND_TEMPLATE_TEMPLATE_PARM: 16614 { 16615 tree tinfo = TYPE_TEMPLATE_INFO (t); 16616 /* We might need to substitute into the types of non-type 16617 template parameters. This also lowers the level of 16618 the ttp appropriately. */ 16619 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args, 16620 complain, in_decl); 16621 if (tmpl == error_mark_node) 16622 return error_mark_node; 16623 tree argvec = tsubst (TI_ARGS (tinfo), args, 16624 complain, in_decl); 16625 if (argvec == error_mark_node) 16626 return error_mark_node; 16627 r = lookup_template_class (tmpl, argvec, in_decl, NULL_TREE, 16628 /*entering_scope=*/false, complain); 16629 r = cp_build_qualified_type (r, cp_type_quals (t), complain); 16630 break; 16631 } 16632 16633 case TEMPLATE_PARM_INDEX: 16634 /* OK, now substitute the type of the non-type parameter. We 16635 couldn't do it earlier because it might be an auto parameter, 16636 and we wouldn't need to if we had an argument. */ 16637 type = tsubst (type, args, complain, in_decl); 16638 if (type == error_mark_node) 16639 return error_mark_node; 16640 r = reduce_template_parm_level (t, type, levels, args, complain); 16641 break; 16642 16643 default: 16644 gcc_unreachable (); 16645 } 16646 16647 return r; 16648 } 16649 16650 case TREE_LIST: 16651 return tsubst_tree_list (t, args, complain, in_decl); 16652 16653 case TREE_BINFO: 16654 /* We should never be tsubsting a binfo. */ 16655 gcc_unreachable (); 16656 16657 case TREE_VEC: 16658 /* A vector of template arguments. */ 16659 gcc_assert (!type); 16660 return tsubst_template_args (t, args, complain, in_decl); 16661 16662 case POINTER_TYPE: 16663 case REFERENCE_TYPE: 16664 { 16665 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE) 16666 return t; 16667 16668 /* [temp.deduct] 16669 16670 Type deduction may fail for any of the following 16671 reasons: 16672 16673 -- Attempting to create a pointer to reference type. 16674 -- Attempting to create a reference to a reference type or 16675 a reference to void. 16676 16677 Core issue 106 says that creating a reference to a reference 16678 during instantiation is no longer a cause for failure. We 16679 only enforce this check in strict C++98 mode. */ 16680 if ((TYPE_REF_P (type) 16681 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE)) 16682 || (code == REFERENCE_TYPE && VOID_TYPE_P (type))) 16683 { 16684 static location_t last_loc; 16685 16686 /* We keep track of the last time we issued this error 16687 message to avoid spewing a ton of messages during a 16688 single bad template instantiation. */ 16689 if (complain & tf_error 16690 && last_loc != input_location) 16691 { 16692 if (VOID_TYPE_P (type)) 16693 error ("forming reference to void"); 16694 else if (code == POINTER_TYPE) 16695 error ("forming pointer to reference type %qT", type); 16696 else 16697 error ("forming reference to reference type %qT", type); 16698 last_loc = input_location; 16699 } 16700 16701 return error_mark_node; 16702 } 16703 else if (TREE_CODE (type) == FUNCTION_TYPE 16704 && (type_memfn_quals (type) != TYPE_UNQUALIFIED 16705 || type_memfn_rqual (type) != REF_QUAL_NONE)) 16706 { 16707 if (complain & tf_error) 16708 { 16709 if (code == POINTER_TYPE) 16710 error ("forming pointer to qualified function type %qT", 16711 type); 16712 else 16713 error ("forming reference to qualified function type %qT", 16714 type); 16715 } 16716 return error_mark_node; 16717 } 16718 else if (code == POINTER_TYPE) 16719 { 16720 r = build_pointer_type (type); 16721 if (TREE_CODE (type) == METHOD_TYPE) 16722 r = build_ptrmemfunc_type (r); 16723 } 16724 else if (TYPE_REF_P (type)) 16725 /* In C++0x, during template argument substitution, when there is an 16726 attempt to create a reference to a reference type, reference 16727 collapsing is applied as described in [14.3.1/4 temp.arg.type]: 16728 16729 "If a template-argument for a template-parameter T names a type 16730 that is a reference to a type A, an attempt to create the type 16731 'lvalue reference to cv T' creates the type 'lvalue reference to 16732 A,' while an attempt to create the type type rvalue reference to 16733 cv T' creates the type T" 16734 */ 16735 r = cp_build_reference_type 16736 (TREE_TYPE (type), 16737 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type)); 16738 else 16739 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t)); 16740 r = cp_build_qualified_type (r, cp_type_quals (t), complain); 16741 16742 if (r != error_mark_node) 16743 /* Will this ever be needed for TYPE_..._TO values? */ 16744 layout_type (r); 16745 16746 return r; 16747 } 16748 case OFFSET_TYPE: 16749 { 16750 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); 16751 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r)) 16752 { 16753 /* [temp.deduct] 16754 16755 Type deduction may fail for any of the following 16756 reasons: 16757 16758 -- Attempting to create "pointer to member of T" when T 16759 is not a class type. */ 16760 if (complain & tf_error) 16761 error ("creating pointer to member of non-class type %qT", r); 16762 return error_mark_node; 16763 } 16764 if (TYPE_REF_P (type)) 16765 { 16766 if (complain & tf_error) 16767 error ("creating pointer to member reference type %qT", type); 16768 return error_mark_node; 16769 } 16770 if (VOID_TYPE_P (type)) 16771 { 16772 if (complain & tf_error) 16773 error ("creating pointer to member of type void"); 16774 return error_mark_node; 16775 } 16776 gcc_assert (TREE_CODE (type) != METHOD_TYPE); 16777 if (TREE_CODE (type) == FUNCTION_TYPE) 16778 { 16779 /* The type of the implicit object parameter gets its 16780 cv-qualifiers from the FUNCTION_TYPE. */ 16781 tree memptr; 16782 tree method_type 16783 = build_memfn_type (type, r, type_memfn_quals (type), 16784 type_memfn_rqual (type)); 16785 memptr = build_ptrmemfunc_type (build_pointer_type (method_type)); 16786 return cp_build_qualified_type (memptr, cp_type_quals (t), 16787 complain); 16788 } 16789 else 16790 return cp_build_qualified_type (build_ptrmem_type (r, type), 16791 cp_type_quals (t), 16792 complain); 16793 } 16794 case FUNCTION_TYPE: 16795 case METHOD_TYPE: 16796 { 16797 tree fntype; 16798 tree specs; 16799 fntype = tsubst_function_type (t, args, complain, in_decl); 16800 if (fntype == error_mark_node) 16801 return error_mark_node; 16802 16803 /* Substitute the exception specification. */ 16804 specs = tsubst_exception_specification (t, args, complain, in_decl, 16805 /*defer_ok*/fndecl_type); 16806 if (specs == error_mark_node) 16807 return error_mark_node; 16808 if (specs) 16809 fntype = build_exception_variant (fntype, specs); 16810 return fntype; 16811 } 16812 case ARRAY_TYPE: 16813 { 16814 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); 16815 if (domain == error_mark_node) 16816 return error_mark_node; 16817 16818 /* As an optimization, we avoid regenerating the array type if 16819 it will obviously be the same as T. */ 16820 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) 16821 return t; 16822 16823 /* These checks should match the ones in create_array_type_for_decl. 16824 16825 [temp.deduct] 16826 16827 The deduction may fail for any of the following reasons: 16828 16829 -- Attempting to create an array with an element type that 16830 is void, a function type, or a reference type, or [DR337] 16831 an abstract class type. */ 16832 if (VOID_TYPE_P (type) 16833 || TREE_CODE (type) == FUNCTION_TYPE 16834 || (TREE_CODE (type) == ARRAY_TYPE 16835 && TYPE_DOMAIN (type) == NULL_TREE) 16836 || TYPE_REF_P (type)) 16837 { 16838 if (complain & tf_error) 16839 error ("creating array of %qT", type); 16840 return error_mark_node; 16841 } 16842 16843 if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type, 16844 !(complain & tf_error))) 16845 return error_mark_node; 16846 16847 r = build_cplus_array_type (type, domain); 16848 16849 if (!valid_array_size_p (input_location, r, in_decl, 16850 (complain & tf_error))) 16851 return error_mark_node; 16852 16853 if (TYPE_USER_ALIGN (t)) 16854 { 16855 SET_TYPE_ALIGN (r, TYPE_ALIGN (t)); 16856 TYPE_USER_ALIGN (r) = 1; 16857 } 16858 16859 return r; 16860 } 16861 16862 case TYPENAME_TYPE: 16863 { 16864 tree ctx = TYPE_CONTEXT (t); 16865 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION) 16866 { 16867 ctx = tsubst_pack_expansion (ctx, args, 16868 complain | tf_qualifying_scope, 16869 in_decl); 16870 if (ctx == error_mark_node 16871 || TREE_VEC_LENGTH (ctx) > 1) 16872 return error_mark_node; 16873 if (TREE_VEC_LENGTH (ctx) == 0) 16874 { 16875 if (complain & tf_error) 16876 error ("%qD is instantiated for an empty pack", 16877 TYPENAME_TYPE_FULLNAME (t)); 16878 return error_mark_node; 16879 } 16880 ctx = TREE_VEC_ELT (ctx, 0); 16881 } 16882 else 16883 ctx = tsubst_aggr_type (ctx, args, 16884 complain | tf_qualifying_scope, 16885 in_decl, /*entering_scope=*/1); 16886 if (ctx == error_mark_node) 16887 return error_mark_node; 16888 16889 tree f = tsubst_name (TYPENAME_TYPE_FULLNAME (t), args, 16890 complain, in_decl); 16891 if (f == error_mark_node) 16892 return error_mark_node; 16893 16894 if (!MAYBE_CLASS_TYPE_P (ctx)) 16895 { 16896 if (complain & tf_error) 16897 error ("%qT is not a class, struct, or union type", ctx); 16898 return error_mark_node; 16899 } 16900 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) 16901 { 16902 /* Normally, make_typename_type does not require that the CTX 16903 have complete type in order to allow things like: 16904 16905 template <class T> struct S { typename S<T>::X Y; }; 16906 16907 But, such constructs have already been resolved by this 16908 point, so here CTX really should have complete type, unless 16909 it's a partial instantiation. */ 16910 if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain)) 16911 return error_mark_node; 16912 } 16913 16914 /* FIXME: TYPENAME_IS_CLASS_P conflates 'class' vs 'struct' vs 'union' 16915 tags. TYPENAME_TYPE should probably remember the exact tag that 16916 was written. */ 16917 enum tag_types tag_type 16918 = TYPENAME_IS_CLASS_P (t) ? class_type 16919 : TYPENAME_IS_ENUM_P (t) ? enum_type 16920 : typename_type; 16921 tsubst_flags_t tcomplain = complain | tf_keep_type_decl; 16922 tcomplain |= tst_ok_flag | qualifying_scope_flag; 16923 f = make_typename_type (ctx, f, tag_type, tcomplain); 16924 if (f == error_mark_node) 16925 return f; 16926 if (TREE_CODE (f) == TYPE_DECL) 16927 { 16928 complain |= tf_ignore_bad_quals; 16929 f = TREE_TYPE (f); 16930 } 16931 16932 if (TREE_CODE (f) != TYPENAME_TYPE) 16933 { 16934 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE) 16935 { 16936 if (complain & tf_error) 16937 error ("%qT resolves to %qT, which is not an enumeration type", 16938 t, f); 16939 else 16940 return error_mark_node; 16941 } 16942 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f)) 16943 { 16944 if (complain & tf_error) 16945 error ("%qT resolves to %qT, which is not a class type", 16946 t, f); 16947 else 16948 return error_mark_node; 16949 } 16950 } 16951 16952 return cp_build_qualified_type 16953 (f, cp_type_quals (f) | cp_type_quals (t), complain); 16954 } 16955 16956 case UNBOUND_CLASS_TEMPLATE: 16957 { 16958 tree name = TYPE_IDENTIFIER (t); 16959 if (name == error_mark_node) 16960 return error_mark_node; 16961 16962 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t)); 16963 parm_list = tsubst_template_parms (parm_list, args, complain); 16964 if (parm_list == error_mark_node) 16965 return error_mark_node; 16966 16967 if (parm_list && TMPL_PARMS_DEPTH (parm_list) > 1) 16968 ++processing_template_decl; 16969 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, 16970 in_decl, /*entering_scope=*/1); 16971 if (parm_list && TMPL_PARMS_DEPTH (parm_list) > 1) 16972 --processing_template_decl; 16973 if (ctx == error_mark_node) 16974 return error_mark_node; 16975 16976 return make_unbound_class_template (ctx, name, parm_list, complain); 16977 } 16978 16979 case TYPEOF_TYPE: 16980 { 16981 tree type; 16982 16983 ++cp_unevaluated_operand; 16984 ++c_inhibit_evaluation_warnings; 16985 16986 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args, complain, in_decl); 16987 16988 --cp_unevaluated_operand; 16989 --c_inhibit_evaluation_warnings; 16990 16991 type = finish_typeof (type); 16992 return cp_build_qualified_type (type, 16993 cp_type_quals (t) 16994 | cp_type_quals (type), 16995 complain); 16996 } 16997 16998 case DECLTYPE_TYPE: 16999 { 17000 tree type; 17001 17002 ++cp_unevaluated_operand; 17003 ++c_inhibit_evaluation_warnings; 17004 17005 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args, 17006 complain|tf_decltype, in_decl); 17007 17008 --cp_unevaluated_operand; 17009 --c_inhibit_evaluation_warnings; 17010 17011 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t)) 17012 type = lambda_capture_field_type (type, 17013 false /*explicit_init*/, 17014 DECLTYPE_FOR_REF_CAPTURE (t)); 17015 else if (DECLTYPE_FOR_LAMBDA_PROXY (t)) 17016 type = lambda_proxy_type (type); 17017 else 17018 { 17019 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t); 17020 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR 17021 && EXPR_P (type)) 17022 /* In a template ~id could be either a complement expression 17023 or an unqualified-id naming a destructor; if instantiating 17024 it produces an expression, it's not an id-expression or 17025 member access. */ 17026 id = false; 17027 type = finish_decltype_type (type, id, complain); 17028 } 17029 return cp_build_qualified_type (type, 17030 cp_type_quals (t) 17031 | cp_type_quals (type), 17032 complain | tf_ignore_bad_quals); 17033 } 17034 17035 case TRAIT_TYPE: 17036 { 17037 tree type1 = TRAIT_TYPE_TYPE1 (t); 17038 if (TYPE_P (type1)) 17039 type1 = tsubst (type1, args, complain, in_decl); 17040 else 17041 type1 = tsubst_expr (type1, args, complain, in_decl); 17042 tree type2 = tsubst (TRAIT_TYPE_TYPE2 (t), args, complain, in_decl); 17043 type = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2, complain); 17044 return cp_build_qualified_type (type, 17045 cp_type_quals (t) | cp_type_quals (type), 17046 complain | tf_ignore_bad_quals); 17047 } 17048 17049 case TYPE_ARGUMENT_PACK: 17050 case NONTYPE_ARGUMENT_PACK: 17051 return tsubst_argument_pack (t, args, complain, in_decl); 17052 17053 case VOID_CST: 17054 case INTEGER_CST: 17055 case REAL_CST: 17056 case STRING_CST: 17057 case PLUS_EXPR: 17058 case MINUS_EXPR: 17059 case NEGATE_EXPR: 17060 case NOP_EXPR: 17061 case INDIRECT_REF: 17062 case ADDR_EXPR: 17063 case CALL_EXPR: 17064 case ARRAY_REF: 17065 case SCOPE_REF: 17066 case OMP_ARRAY_SECTION: 17067 /* We should use one of the expression tsubsts for these codes. */ 17068 gcc_unreachable (); 17069 17070 default: 17071 sorry ("use of %qs in template", get_tree_code_name (code)); 17072 return error_mark_node; 17073 } 17074 } 17075 17076 /* Convenience wrapper over tsubst for substituting into the LHS 17077 of the :: scope resolution operator. */ 17078 17079 static tree 17080 tsubst_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl) 17081 { 17082 gcc_checking_assert (TYPE_P (t)); 17083 return tsubst (t, args, complain | tf_qualifying_scope, in_decl); 17084 } 17085 17086 /* Convenience wrapper over tsubst for substituting into an id-expression 17087 without resolving its terminal name. */ 17088 17089 static tree 17090 tsubst_name (tree t, tree args, tsubst_flags_t complain, tree in_decl) 17091 { 17092 return tsubst_expr (t, args, complain | tf_no_name_lookup, in_decl); 17093 } 17094 17095 /* OLDFNS is a lookup set of member functions from some class template, and 17096 NEWFNS is a lookup set of member functions from NEWTYPE, a specialization 17097 of that class template. Return the subset of NEWFNS which are 17098 specializations of a function from OLDFNS. */ 17099 17100 static tree 17101 filter_memfn_lookup (tree oldfns, tree newfns, tree newtype) 17102 { 17103 /* Record all member functions from the old lookup set OLDFNS into 17104 VISIBLE_SET. */ 17105 hash_set<tree> visible_set; 17106 bool seen_dep_using = false; 17107 for (tree fn : lkp_range (oldfns)) 17108 { 17109 if (TREE_CODE (fn) == USING_DECL) 17110 { 17111 /* Imprecisely handle dependent using-decl by keeping all members 17112 in the new lookup set that are defined in a base class, i.e. 17113 members that could plausibly have been introduced by this 17114 dependent using-decl. 17115 FIXME: Track which members are introduced by a dependent 17116 using-decl precisely, perhaps by performing another lookup 17117 from the substituted USING_DECL_SCOPE. */ 17118 gcc_checking_assert (DECL_DEPENDENT_P (fn)); 17119 seen_dep_using = true; 17120 } 17121 else 17122 visible_set.add (fn); 17123 } 17124 17125 /* Returns true iff (a less specialized version of) FN appeared in 17126 the old lookup set OLDFNS. */ 17127 auto visible_p = [newtype, seen_dep_using, &visible_set] (tree fn) { 17128 if (DECL_CONTEXT (fn) != newtype) 17129 /* FN is a member function from a base class, introduced via a 17130 using-decl; if it might have been introduced by a dependent 17131 using-decl then just conservatively keep it, otherwise look 17132 in the old lookup set for FN exactly. */ 17133 return seen_dep_using || visible_set.contains (fn); 17134 else if (TREE_CODE (fn) == TEMPLATE_DECL) 17135 /* FN is a member function template from the current class; 17136 look in the old lookup set for the TEMPLATE_DECL from which 17137 it was specialized. */ 17138 return visible_set.contains (DECL_TI_TEMPLATE (fn)); 17139 else 17140 /* FN is a non-template member function from the current class; 17141 look in the old lookup set for the FUNCTION_DECL from which 17142 it was specialized. */ 17143 return visible_set.contains (DECL_TEMPLATE_RESULT 17144 (DECL_TI_TEMPLATE (fn))); 17145 }; 17146 17147 bool lookup_changed_p = false; 17148 for (tree fn : lkp_range (newfns)) 17149 if (!visible_p (fn)) 17150 { 17151 lookup_changed_p = true; 17152 break; 17153 } 17154 if (!lookup_changed_p) 17155 return newfns; 17156 17157 /* Filter out from NEWFNS the member functions that weren't 17158 previously visible according to OLDFNS. */ 17159 tree filtered_fns = NULL_TREE; 17160 unsigned filtered_size = 0; 17161 for (tree fn : lkp_range (newfns)) 17162 if (visible_p (fn)) 17163 { 17164 filtered_fns = lookup_add (fn, filtered_fns); 17165 filtered_size++; 17166 } 17167 gcc_checking_assert (seen_dep_using 17168 ? filtered_size >= visible_set.elements () 17169 : filtered_size == visible_set.elements ()); 17170 17171 return filtered_fns; 17172 } 17173 17174 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the 17175 expression on the left-hand side of the "." or "->" operator. We 17176 only do the lookup if we had a dependent BASELINK. Otherwise we 17177 adjust it onto the instantiated heirarchy. */ 17178 17179 static tree 17180 tsubst_baselink (tree baselink, tree object_type, 17181 tree args, tsubst_flags_t complain, tree in_decl) 17182 { 17183 bool qualified_p = BASELINK_QUALIFIED_P (baselink); 17184 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)); 17185 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl); 17186 17187 tree optype = BASELINK_OPTYPE (baselink); 17188 optype = tsubst (optype, args, complain, in_decl); 17189 17190 tree template_args = NULL_TREE; 17191 bool template_id_p = false; 17192 tree fns = BASELINK_FUNCTIONS (baselink); 17193 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) 17194 { 17195 template_id_p = true; 17196 template_args = TREE_OPERAND (fns, 1); 17197 fns = TREE_OPERAND (fns, 0); 17198 if (template_args) 17199 template_args = tsubst_template_args (template_args, args, 17200 complain, in_decl); 17201 } 17202 17203 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink)); 17204 binfo_type = tsubst (binfo_type, args, complain, in_decl); 17205 bool dependent_p = (binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink)) 17206 || optype != BASELINK_OPTYPE (baselink)); 17207 17208 if (dependent_p) 17209 { 17210 tree name = OVL_NAME (fns); 17211 if (IDENTIFIER_CONV_OP_P (name)) 17212 name = make_conv_op_name (optype); 17213 17214 /* See maybe_dependent_member_ref. */ 17215 if ((complain & tf_dguide) && dependent_scope_p (qualifying_scope)) 17216 { 17217 if (template_id_p) 17218 name = build2 (TEMPLATE_ID_EXPR, unknown_type_node, name, 17219 template_args); 17220 return build_qualified_name (NULL_TREE, qualifying_scope, name, 17221 /* ::template */false); 17222 } 17223 17224 if (name == complete_dtor_identifier) 17225 /* Treat as-if non-dependent below. */ 17226 dependent_p = false; 17227 17228 bool maybe_incomplete = BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink); 17229 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1, 17230 complain); 17231 if (maybe_incomplete) 17232 { 17233 /* Filter out from the new lookup set those functions which didn't 17234 appear in the original lookup set (in a less specialized form). 17235 This is needed to preserve the consistency of member lookup 17236 performed in an incomplete-class context, within which 17237 later-declared members ought to remain invisible. */ 17238 BASELINK_FUNCTIONS (baselink) 17239 = filter_memfn_lookup (fns, BASELINK_FUNCTIONS (baselink), 17240 binfo_type); 17241 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink) = true; 17242 } 17243 17244 if (!baselink) 17245 { 17246 if ((complain & tf_error) 17247 && constructor_name_p (name, qualifying_scope)) 17248 error ("cannot call constructor %<%T::%D%> directly", 17249 qualifying_scope, name); 17250 return error_mark_node; 17251 } 17252 17253 fns = BASELINK_FUNCTIONS (baselink); 17254 } 17255 else 17256 { 17257 /* We're going to overwrite pieces below, make a duplicate. */ 17258 baselink = copy_node (baselink); 17259 17260 if (qualifying_scope != BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink))) 17261 { 17262 /* The decl we found was from non-dependent scope, but we still need 17263 to update the binfos for the instantiated qualifying_scope. */ 17264 BASELINK_ACCESS_BINFO (baselink) = TYPE_BINFO (qualifying_scope); 17265 BASELINK_BINFO (baselink) = lookup_base (qualifying_scope, binfo_type, 17266 ba_unique, nullptr, complain); 17267 } 17268 } 17269 17270 /* If lookup found a single function, mark it as used at this point. 17271 (If lookup found multiple functions the one selected later by 17272 overload resolution will be marked as used at that point.) */ 17273 if (!template_id_p && !really_overloaded_fn (fns)) 17274 { 17275 tree fn = OVL_FIRST (fns); 17276 bool ok = mark_used (fn, complain); 17277 if (!ok && !(complain & tf_error)) 17278 return error_mark_node; 17279 if (ok && BASELINK_P (baselink)) 17280 /* We might have instantiated an auto function. */ 17281 TREE_TYPE (baselink) = TREE_TYPE (fn); 17282 } 17283 17284 if (BASELINK_P (baselink)) 17285 { 17286 /* Add back the template arguments, if present. */ 17287 if (template_id_p) 17288 BASELINK_FUNCTIONS (baselink) 17289 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args); 17290 17291 /* Update the conversion operator type. */ 17292 BASELINK_OPTYPE (baselink) = optype; 17293 } 17294 17295 if (!object_type) 17296 object_type = current_class_type; 17297 17298 if (qualified_p || !dependent_p) 17299 { 17300 baselink = adjust_result_of_qualified_name_lookup (baselink, 17301 qualifying_scope, 17302 object_type); 17303 if (!qualified_p) 17304 /* We need to call adjust_result_of_qualified_name_lookup in case the 17305 destructor names a base class, but we unset BASELINK_QUALIFIED_P 17306 so that we still get virtual function binding. */ 17307 BASELINK_QUALIFIED_P (baselink) = false; 17308 } 17309 17310 return baselink; 17311 } 17312 17313 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is 17314 true if the qualified-id will be a postfix-expression in-and-of 17315 itself; false if more of the postfix-expression follows the 17316 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand 17317 of "&". */ 17318 17319 static tree 17320 tsubst_qualified_id (tree qualified_id, tree args, 17321 tsubst_flags_t complain, tree in_decl, 17322 bool done, bool address_p) 17323 { 17324 tree expr; 17325 tree scope; 17326 tree name; 17327 bool is_template; 17328 tree template_args; 17329 location_t loc = EXPR_LOCATION (qualified_id); 17330 17331 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF); 17332 17333 /* Figure out what name to look up. */ 17334 name = TREE_OPERAND (qualified_id, 1); 17335 if (TREE_CODE (name) == TEMPLATE_ID_EXPR) 17336 { 17337 is_template = true; 17338 template_args = TREE_OPERAND (name, 1); 17339 if (template_args) 17340 template_args = tsubst_template_args (template_args, args, 17341 complain, in_decl); 17342 if (template_args == error_mark_node) 17343 return error_mark_node; 17344 name = TREE_OPERAND (name, 0); 17345 } 17346 else 17347 { 17348 is_template = false; 17349 template_args = NULL_TREE; 17350 } 17351 17352 /* Substitute into the qualifying scope. When there are no ARGS, we 17353 are just trying to simplify a non-dependent expression. In that 17354 case the qualifying scope may be dependent, and, in any case, 17355 substituting will not help. */ 17356 scope = TREE_OPERAND (qualified_id, 0); 17357 if (args) 17358 { 17359 scope = tsubst_scope (scope, args, complain, in_decl); 17360 expr = tsubst_name (name, args, complain, in_decl); 17361 } 17362 else 17363 expr = name; 17364 17365 if (dependent_scope_p (scope)) 17366 { 17367 if (TREE_CODE (expr) == SCOPE_REF) 17368 /* We built one in tsubst_baselink. */ 17369 gcc_checking_assert (same_type_p (scope, TREE_OPERAND (expr, 0))); 17370 else 17371 { 17372 if (is_template) 17373 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, 17374 template_args); 17375 expr = build_qualified_name (NULL_TREE, scope, expr, 17376 QUALIFIED_NAME_IS_TEMPLATE 17377 (qualified_id)); 17378 } 17379 REF_PARENTHESIZED_P (expr) = REF_PARENTHESIZED_P (qualified_id); 17380 return expr; 17381 } 17382 17383 if (!BASELINK_P (name) && !DECL_P (expr)) 17384 { 17385 if (TREE_CODE (expr) == BIT_NOT_EXPR) 17386 { 17387 /* A BIT_NOT_EXPR is used to represent a destructor. */ 17388 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0))) 17389 { 17390 error ("qualifying type %qT does not match destructor name ~%qT", 17391 scope, TREE_OPERAND (expr, 0)); 17392 expr = error_mark_node; 17393 } 17394 else 17395 expr = lookup_qualified_name (scope, complete_dtor_identifier, 17396 LOOK_want::NORMAL, false); 17397 } 17398 else 17399 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false); 17400 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL 17401 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL) 17402 { 17403 if (complain & tf_error) 17404 { 17405 error ("dependent-name %qE is parsed as a non-type, but " 17406 "instantiation yields a type", qualified_id); 17407 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id); 17408 } 17409 return error_mark_node; 17410 } 17411 } 17412 17413 if (DECL_P (expr)) 17414 { 17415 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE, 17416 scope, complain)) 17417 return error_mark_node; 17418 /* Remember that there was a reference to this entity. */ 17419 if (!mark_used (expr, complain) && !(complain & tf_error)) 17420 return error_mark_node; 17421 } 17422 17423 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST) 17424 { 17425 if (complain & tf_error) 17426 qualified_name_lookup_error (scope, 17427 TREE_OPERAND (qualified_id, 1), 17428 expr, input_location); 17429 return error_mark_node; 17430 } 17431 17432 if (is_template) 17433 { 17434 /* We may be repeating a check already done during parsing, but 17435 if it was well-formed and passed then, it will pass again 17436 now, and if it didn't, we wouldn't have got here. The case 17437 we want to catch is when we couldn't tell then, and can now, 17438 namely when templ prior to substitution was an 17439 identifier. */ 17440 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args)) 17441 return error_mark_node; 17442 17443 if (variable_template_p (expr)) 17444 expr = lookup_and_finish_template_variable (expr, template_args, 17445 complain); 17446 else 17447 expr = lookup_template_function (expr, template_args); 17448 } 17449 17450 if (expr == error_mark_node && complain & tf_error) 17451 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1), 17452 expr, input_location); 17453 else if (TYPE_P (scope)) 17454 { 17455 expr = (adjust_result_of_qualified_name_lookup 17456 (expr, scope, current_nonlambda_class_type ())); 17457 expr = (finish_qualified_id_expr 17458 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id), 17459 QUALIFIED_NAME_IS_TEMPLATE (qualified_id), 17460 /*template_arg_p=*/false, complain)); 17461 } 17462 17463 /* Expressions do not generally have reference type. */ 17464 if (TREE_CODE (expr) != SCOPE_REF 17465 /* However, if we're about to form a pointer-to-member, we just 17466 want the referenced member referenced. */ 17467 && TREE_CODE (expr) != OFFSET_REF) 17468 expr = convert_from_reference (expr); 17469 17470 if (REF_PARENTHESIZED_P (qualified_id)) 17471 expr = force_paren_expr (expr); 17472 17473 expr = maybe_wrap_with_location (expr, loc); 17474 17475 return expr; 17476 } 17477 17478 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted 17479 initializer, DECL is the substituted VAR_DECL. Other arguments are as 17480 for tsubst. */ 17481 17482 static tree 17483 tsubst_init (tree init, tree decl, tree args, 17484 tsubst_flags_t complain, tree in_decl) 17485 { 17486 if (!init) 17487 return NULL_TREE; 17488 17489 init = tsubst_expr (init, args, complain, in_decl); 17490 17491 tree type = TREE_TYPE (decl); 17492 17493 if (!init && type != error_mark_node) 17494 { 17495 if (tree auto_node = type_uses_auto (type)) 17496 { 17497 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node)) 17498 { 17499 if (complain & tf_error) 17500 error ("initializer for %q#D expands to an empty list " 17501 "of expressions", decl); 17502 return error_mark_node; 17503 } 17504 } 17505 else if (!dependent_type_p (type)) 17506 { 17507 /* If we had an initializer but it 17508 instantiated to nothing, 17509 value-initialize the object. This will 17510 only occur when the initializer was a 17511 pack expansion where the parameter packs 17512 used in that expansion were of length 17513 zero. */ 17514 init = build_value_init (type, complain); 17515 if (TREE_CODE (init) == AGGR_INIT_EXPR) 17516 init = get_target_expr (init, complain); 17517 if (TREE_CODE (init) == TARGET_EXPR) 17518 TARGET_EXPR_DIRECT_INIT_P (init) = true; 17519 } 17520 } 17521 17522 return init; 17523 } 17524 17525 /* If T is a reference to a dependent member of the current instantiation C and 17526 we are trying to refer to that member in a partial instantiation of C, 17527 return a SCOPE_REF; otherwise, return NULL_TREE. 17528 17529 This can happen when forming a C++17 deduction guide, as in PR96199. */ 17530 17531 static tree 17532 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain, 17533 tree in_decl) 17534 { 17535 if (!(complain & tf_dguide)) 17536 return NULL_TREE; 17537 17538 tree decl = (t && TYPE_P (t)) ? TYPE_NAME (t) : t; 17539 if (!decl || !DECL_P (decl)) 17540 return NULL_TREE; 17541 17542 tree ctx = context_for_name_lookup (decl); 17543 if (!CLASS_TYPE_P (ctx)) 17544 return NULL_TREE; 17545 17546 ctx = tsubst (ctx, args, complain, in_decl); 17547 if (!dependent_scope_p (ctx)) 17548 return NULL_TREE; 17549 17550 if (TYPE_P (t)) 17551 { 17552 if (typedef_variant_p (t)) 17553 t = strip_typedefs (t); 17554 tree decl = TYPE_NAME (t); 17555 if (decl) 17556 decl = maybe_dependent_member_ref (decl, args, complain, in_decl); 17557 if (!decl) 17558 return NULL_TREE; 17559 return cp_build_qualified_type (TREE_TYPE (decl), cp_type_quals (t), 17560 complain); 17561 } 17562 17563 tree name = DECL_NAME (t); 17564 tree fullname = name; 17565 if (instantiates_primary_template_p (t)) 17566 { 17567 tree tinfo = get_template_info (t); 17568 name = DECL_NAME (TI_TEMPLATE (tinfo)); 17569 tree targs = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)); 17570 targs = tsubst_template_args (targs, args, complain, in_decl); 17571 fullname = build_nt (TEMPLATE_ID_EXPR, name, targs); 17572 } 17573 17574 if (TREE_CODE (t) == TYPE_DECL) 17575 { 17576 if (TREE_CODE (TREE_TYPE (t)) == TYPENAME_TYPE 17577 && TYPE_NAME (TREE_TYPE (t)) == t) 17578 /* The TYPE_DECL for a typename has DECL_CONTEXT of the typename 17579 scope, but it doesn't need to be rewritten again. */ 17580 return NULL_TREE; 17581 tree type = build_typename_type (ctx, name, fullname, typename_type); 17582 return TYPE_NAME (type); 17583 } 17584 else if (DECL_TYPE_TEMPLATE_P (t)) 17585 return make_unbound_class_template (ctx, name, 17586 NULL_TREE, complain); 17587 else 17588 return build_qualified_name (NULL_TREE, ctx, fullname, 17589 TREE_CODE (t) == TEMPLATE_DECL); 17590 } 17591 17592 /* Helper function for tsubst_omp_clauses, used for instantiation of 17593 OMP_CLAUSE_DECL of clauses. */ 17594 17595 static tree 17596 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain, 17597 tree in_decl, tree *iterator_cache) 17598 { 17599 if (decl == NULL_TREE || decl == ridpointers[RID_OMP_ALL_MEMORY]) 17600 return decl; 17601 17602 /* Handle OpenMP iterators. */ 17603 if (TREE_CODE (decl) == TREE_LIST 17604 && TREE_PURPOSE (decl) 17605 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC) 17606 { 17607 tree ret; 17608 if (iterator_cache[0] == TREE_PURPOSE (decl)) 17609 ret = iterator_cache[1]; 17610 else 17611 { 17612 tree *tp = &ret; 17613 begin_scope (sk_omp, NULL); 17614 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it)) 17615 { 17616 *tp = copy_node (it); 17617 TREE_VEC_ELT (*tp, 0) 17618 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain); 17619 DECL_CONTEXT (TREE_VEC_ELT (*tp, 0)) = current_function_decl; 17620 pushdecl (TREE_VEC_ELT (*tp, 0)); 17621 TREE_VEC_ELT (*tp, 1) 17622 = tsubst_stmt (TREE_VEC_ELT (it, 1), args, complain, in_decl); 17623 TREE_VEC_ELT (*tp, 2) 17624 = tsubst_stmt (TREE_VEC_ELT (it, 2), args, complain, in_decl); 17625 TREE_VEC_ELT (*tp, 3) 17626 = tsubst_stmt (TREE_VEC_ELT (it, 3), args, complain, in_decl); 17627 TREE_CHAIN (*tp) = NULL_TREE; 17628 tp = &TREE_CHAIN (*tp); 17629 } 17630 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0); 17631 iterator_cache[0] = TREE_PURPOSE (decl); 17632 iterator_cache[1] = ret; 17633 } 17634 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl), 17635 args, complain, 17636 in_decl, NULL)); 17637 } 17638 17639 /* Handle an OpenMP array section represented as a TREE_LIST (or 17640 OMP_CLAUSE_DOACROSS_KIND). An OMP_CLAUSE_DOACROSS (with a depend 17641 kind of OMP_CLAUSE_DOACROSS_SINK) can also be represented as a 17642 TREE_LIST. We can handle it exactly the same as an array section 17643 (purpose, value, and a chain), even though the nomenclature 17644 (low_bound, length, etc) is different. */ 17645 if (TREE_CODE (decl) == TREE_LIST) 17646 { 17647 tree low_bound 17648 = tsubst_stmt (TREE_PURPOSE (decl), args, complain, in_decl); 17649 tree length = tsubst_stmt (TREE_VALUE (decl), args, complain, in_decl); 17650 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain, 17651 in_decl, NULL); 17652 if (TREE_PURPOSE (decl) == low_bound 17653 && TREE_VALUE (decl) == length 17654 && TREE_CHAIN (decl) == chain) 17655 return decl; 17656 tree ret = tree_cons (low_bound, length, chain); 17657 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (ret) 17658 = OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (decl); 17659 return ret; 17660 } 17661 else if (TREE_CODE (decl) == OMP_ARRAY_SECTION) 17662 { 17663 tree low_bound 17664 = tsubst_stmt (TREE_OPERAND (decl, 1), args, complain, in_decl); 17665 tree length = tsubst_stmt (TREE_OPERAND (decl, 2), args, complain, 17666 in_decl); 17667 tree base = tsubst_omp_clause_decl (TREE_OPERAND (decl, 0), args, 17668 complain, in_decl, NULL); 17669 if (TREE_OPERAND (decl, 0) == base 17670 && TREE_OPERAND (decl, 1) == low_bound 17671 && TREE_OPERAND (decl, 2) == length) 17672 return decl; 17673 return build3 (OMP_ARRAY_SECTION, TREE_TYPE (base), base, low_bound, 17674 length); 17675 } 17676 tree ret = tsubst_stmt (decl, args, complain, in_decl); 17677 /* Undo convert_from_reference tsubst_expr could have called. */ 17678 if (decl 17679 && REFERENCE_REF_P (ret) 17680 && !REFERENCE_REF_P (decl)) 17681 ret = TREE_OPERAND (ret, 0); 17682 return ret; 17683 } 17684 17685 /* Like tsubst_copy, but specifically for OpenMP clauses. */ 17686 17687 static tree 17688 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort, 17689 tree args, tsubst_flags_t complain, tree in_decl) 17690 { 17691 tree new_clauses = NULL_TREE, nc, oc; 17692 tree linear_no_step = NULL_TREE; 17693 tree iterator_cache[2] = { NULL_TREE, NULL_TREE }; 17694 17695 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc)) 17696 { 17697 nc = copy_node (oc); 17698 OMP_CLAUSE_CHAIN (nc) = new_clauses; 17699 new_clauses = nc; 17700 17701 switch (OMP_CLAUSE_CODE (nc)) 17702 { 17703 case OMP_CLAUSE_LASTPRIVATE: 17704 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc)) 17705 { 17706 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list (); 17707 tsubst_stmt (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, 17708 complain, in_decl); 17709 OMP_CLAUSE_LASTPRIVATE_STMT (nc) 17710 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc)); 17711 } 17712 /* FALLTHRU */ 17713 case OMP_CLAUSE_PRIVATE: 17714 case OMP_CLAUSE_SHARED: 17715 case OMP_CLAUSE_FIRSTPRIVATE: 17716 case OMP_CLAUSE_COPYIN: 17717 case OMP_CLAUSE_COPYPRIVATE: 17718 case OMP_CLAUSE_UNIFORM: 17719 case OMP_CLAUSE_DEPEND: 17720 case OMP_CLAUSE_DOACROSS: 17721 case OMP_CLAUSE_AFFINITY: 17722 case OMP_CLAUSE_FROM: 17723 case OMP_CLAUSE_TO: 17724 case OMP_CLAUSE_MAP: 17725 case OMP_CLAUSE__CACHE_: 17726 case OMP_CLAUSE_NONTEMPORAL: 17727 case OMP_CLAUSE_USE_DEVICE_PTR: 17728 case OMP_CLAUSE_USE_DEVICE_ADDR: 17729 case OMP_CLAUSE_IS_DEVICE_PTR: 17730 case OMP_CLAUSE_HAS_DEVICE_ADDR: 17731 case OMP_CLAUSE_INCLUSIVE: 17732 case OMP_CLAUSE_EXCLUSIVE: 17733 OMP_CLAUSE_DECL (nc) 17734 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain, 17735 in_decl, iterator_cache); 17736 break; 17737 case OMP_CLAUSE_NUM_TEAMS: 17738 if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc)) 17739 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc) 17740 = tsubst_stmt (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc), args, 17741 complain, in_decl); 17742 /* FALLTHRU */ 17743 case OMP_CLAUSE_TILE: 17744 case OMP_CLAUSE_IF: 17745 case OMP_CLAUSE_SELF: 17746 case OMP_CLAUSE_NUM_THREADS: 17747 case OMP_CLAUSE_SCHEDULE: 17748 case OMP_CLAUSE_COLLAPSE: 17749 case OMP_CLAUSE_FINAL: 17750 case OMP_CLAUSE_DEVICE: 17751 case OMP_CLAUSE_DIST_SCHEDULE: 17752 case OMP_CLAUSE_THREAD_LIMIT: 17753 case OMP_CLAUSE_SAFELEN: 17754 case OMP_CLAUSE_SIMDLEN: 17755 case OMP_CLAUSE_NUM_TASKS: 17756 case OMP_CLAUSE_GRAINSIZE: 17757 case OMP_CLAUSE_PRIORITY: 17758 case OMP_CLAUSE_ORDERED: 17759 case OMP_CLAUSE_HINT: 17760 case OMP_CLAUSE_FILTER: 17761 case OMP_CLAUSE_NUM_GANGS: 17762 case OMP_CLAUSE_NUM_WORKERS: 17763 case OMP_CLAUSE_VECTOR_LENGTH: 17764 case OMP_CLAUSE_WORKER: 17765 case OMP_CLAUSE_VECTOR: 17766 case OMP_CLAUSE_ASYNC: 17767 case OMP_CLAUSE_WAIT: 17768 case OMP_CLAUSE_DETACH: 17769 OMP_CLAUSE_OPERAND (nc, 0) 17770 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl); 17771 break; 17772 case OMP_CLAUSE_REDUCTION: 17773 case OMP_CLAUSE_IN_REDUCTION: 17774 case OMP_CLAUSE_TASK_REDUCTION: 17775 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc)) 17776 { 17777 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc); 17778 if (TREE_CODE (placeholder) == SCOPE_REF) 17779 { 17780 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args, 17781 complain, in_decl); 17782 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc) 17783 = build_qualified_name (NULL_TREE, scope, 17784 TREE_OPERAND (placeholder, 1), 17785 false); 17786 } 17787 else 17788 gcc_assert (identifier_p (placeholder)); 17789 } 17790 OMP_CLAUSE_DECL (nc) 17791 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain, 17792 in_decl, NULL); 17793 break; 17794 case OMP_CLAUSE_GANG: 17795 case OMP_CLAUSE_ALIGNED: 17796 OMP_CLAUSE_DECL (nc) 17797 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain, 17798 in_decl, NULL); 17799 OMP_CLAUSE_OPERAND (nc, 1) 17800 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl); 17801 break; 17802 case OMP_CLAUSE_ALLOCATE: 17803 OMP_CLAUSE_DECL (nc) 17804 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain, 17805 in_decl, NULL); 17806 OMP_CLAUSE_OPERAND (nc, 1) 17807 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl); 17808 OMP_CLAUSE_OPERAND (nc, 2) 17809 = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 2), args, complain, in_decl); 17810 break; 17811 case OMP_CLAUSE_LINEAR: 17812 OMP_CLAUSE_DECL (nc) 17813 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain, 17814 in_decl, NULL); 17815 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE) 17816 { 17817 gcc_assert (!linear_no_step); 17818 linear_no_step = nc; 17819 } 17820 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc)) 17821 OMP_CLAUSE_LINEAR_STEP (nc) 17822 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args, 17823 complain, in_decl, NULL); 17824 else 17825 OMP_CLAUSE_LINEAR_STEP (nc) 17826 = tsubst_stmt (OMP_CLAUSE_LINEAR_STEP (oc), args, 17827 complain, in_decl); 17828 break; 17829 case OMP_CLAUSE_NOWAIT: 17830 case OMP_CLAUSE_DEFAULT: 17831 case OMP_CLAUSE_UNTIED: 17832 case OMP_CLAUSE_MERGEABLE: 17833 case OMP_CLAUSE_INBRANCH: 17834 case OMP_CLAUSE_NOTINBRANCH: 17835 case OMP_CLAUSE_PROC_BIND: 17836 case OMP_CLAUSE_FOR: 17837 case OMP_CLAUSE_PARALLEL: 17838 case OMP_CLAUSE_SECTIONS: 17839 case OMP_CLAUSE_TASKGROUP: 17840 case OMP_CLAUSE_NOGROUP: 17841 case OMP_CLAUSE_THREADS: 17842 case OMP_CLAUSE_SIMD: 17843 case OMP_CLAUSE_DEFAULTMAP: 17844 case OMP_CLAUSE_ORDER: 17845 case OMP_CLAUSE_BIND: 17846 case OMP_CLAUSE_INDEPENDENT: 17847 case OMP_CLAUSE_AUTO: 17848 case OMP_CLAUSE_SEQ: 17849 case OMP_CLAUSE_IF_PRESENT: 17850 case OMP_CLAUSE_FINALIZE: 17851 case OMP_CLAUSE_NOHOST: 17852 break; 17853 default: 17854 gcc_unreachable (); 17855 } 17856 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP) 17857 switch (OMP_CLAUSE_CODE (nc)) 17858 { 17859 case OMP_CLAUSE_SHARED: 17860 case OMP_CLAUSE_PRIVATE: 17861 case OMP_CLAUSE_FIRSTPRIVATE: 17862 case OMP_CLAUSE_LASTPRIVATE: 17863 case OMP_CLAUSE_COPYPRIVATE: 17864 case OMP_CLAUSE_LINEAR: 17865 case OMP_CLAUSE_REDUCTION: 17866 case OMP_CLAUSE_IN_REDUCTION: 17867 case OMP_CLAUSE_TASK_REDUCTION: 17868 case OMP_CLAUSE_USE_DEVICE_PTR: 17869 case OMP_CLAUSE_USE_DEVICE_ADDR: 17870 case OMP_CLAUSE_IS_DEVICE_PTR: 17871 case OMP_CLAUSE_HAS_DEVICE_ADDR: 17872 case OMP_CLAUSE_INCLUSIVE: 17873 case OMP_CLAUSE_EXCLUSIVE: 17874 case OMP_CLAUSE_ALLOCATE: 17875 /* tsubst_expr on SCOPE_REF results in returning 17876 finish_non_static_data_member result. Undo that here. */ 17877 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF 17878 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1)) 17879 == IDENTIFIER_NODE)) 17880 { 17881 tree t = OMP_CLAUSE_DECL (nc); 17882 tree v = t; 17883 while (v) 17884 switch (TREE_CODE (v)) 17885 { 17886 case COMPONENT_REF: 17887 case MEM_REF: 17888 case INDIRECT_REF: 17889 CASE_CONVERT: 17890 case POINTER_PLUS_EXPR: 17891 v = TREE_OPERAND (v, 0); 17892 continue; 17893 case PARM_DECL: 17894 if (DECL_CONTEXT (v) == current_function_decl 17895 && DECL_ARTIFICIAL (v) 17896 && DECL_NAME (v) == this_identifier) 17897 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1); 17898 /* FALLTHRU */ 17899 default: 17900 v = NULL_TREE; 17901 break; 17902 } 17903 } 17904 else if (VAR_P (OMP_CLAUSE_DECL (oc)) 17905 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc)) 17906 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc)) 17907 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc)) 17908 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc))) 17909 { 17910 tree decl = OMP_CLAUSE_DECL (nc); 17911 if (VAR_P (decl)) 17912 { 17913 retrofit_lang_decl (decl); 17914 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1; 17915 } 17916 } 17917 break; 17918 default: 17919 break; 17920 } 17921 } 17922 17923 new_clauses = nreverse (new_clauses); 17924 if (ort != C_ORT_OMP_DECLARE_SIMD) 17925 { 17926 new_clauses = finish_omp_clauses (new_clauses, ort); 17927 if (linear_no_step) 17928 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc)) 17929 if (nc == linear_no_step) 17930 { 17931 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE; 17932 break; 17933 } 17934 } 17935 return new_clauses; 17936 } 17937 17938 /* Like tsubst_expr, but unshare TREE_LIST nodes. */ 17939 17940 static tree 17941 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain, 17942 tree in_decl) 17943 { 17944 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl) 17945 17946 tree purpose, value, chain; 17947 17948 if (t == NULL) 17949 return t; 17950 17951 if (TREE_CODE (t) != TREE_LIST) 17952 return tsubst_expr (t, args, complain, in_decl); 17953 17954 if (t == void_list_node) 17955 return t; 17956 17957 purpose = TREE_PURPOSE (t); 17958 if (purpose) 17959 purpose = RECUR (purpose); 17960 value = TREE_VALUE (t); 17961 if (value) 17962 { 17963 if (TREE_CODE (value) != LABEL_DECL) 17964 value = RECUR (value); 17965 else 17966 { 17967 value = lookup_label (DECL_NAME (value)); 17968 gcc_assert (TREE_CODE (value) == LABEL_DECL); 17969 TREE_USED (value) = 1; 17970 } 17971 } 17972 chain = TREE_CHAIN (t); 17973 if (chain && chain != void_type_node) 17974 chain = RECUR (chain); 17975 return tree_cons (purpose, value, chain); 17976 #undef RECUR 17977 } 17978 17979 /* Used to temporarily communicate the list of #pragma omp parallel 17980 clauses to #pragma omp for instantiation if they are combined 17981 together. */ 17982 17983 static tree *omp_parallel_combined_clauses; 17984 17985 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree, 17986 cp_decomp *); 17987 17988 /* Substitute one OMP_FOR iterator. */ 17989 17990 static bool 17991 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv, 17992 tree initv, tree condv, tree incrv, tree *clauses, 17993 tree args, tsubst_flags_t complain, tree in_decl) 17994 { 17995 #define RECUR(NODE) \ 17996 tsubst_stmt ((NODE), args, complain, in_decl) 17997 tree decl, init, cond = NULL_TREE, incr = NULL_TREE; 17998 bool ret = false; 17999 18000 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i); 18001 gcc_assert (TREE_CODE (init) == MODIFY_EXPR); 18002 18003 decl = TREE_OPERAND (init, 0); 18004 init = TREE_OPERAND (init, 1); 18005 tree decl_expr = NULL_TREE; 18006 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace; 18007 if (range_for) 18008 { 18009 bool decomp = false; 18010 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl)) 18011 { 18012 tree v = DECL_VALUE_EXPR (decl); 18013 if (TREE_CODE (v) == ARRAY_REF 18014 && VAR_P (TREE_OPERAND (v, 0)) 18015 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0))) 18016 { 18017 cp_decomp decomp_d = { NULL_TREE, 0 }; 18018 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain); 18019 maybe_push_decl (d); 18020 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain, 18021 in_decl, &decomp_d); 18022 decomp = true; 18023 if (d == error_mark_node) 18024 decl = error_mark_node; 18025 else 18026 for (unsigned int i = 0; i < decomp_d.count; i++) 18027 { 18028 if (!DECL_HAS_VALUE_EXPR_P (decomp_d.decl)) 18029 { 18030 tree v = build_nt (ARRAY_REF, d, 18031 size_int (decomp_d.count - i - 1), 18032 NULL_TREE, NULL_TREE); 18033 SET_DECL_VALUE_EXPR (decomp_d.decl, v); 18034 DECL_HAS_VALUE_EXPR_P (decomp_d.decl) = 1; 18035 } 18036 fit_decomposition_lang_decl (decomp_d.decl, d); 18037 decomp_d.decl = DECL_CHAIN (decomp_d.decl); 18038 } 18039 } 18040 } 18041 decl = tsubst_decl (decl, args, complain); 18042 if (!decomp) 18043 maybe_push_decl (decl); 18044 } 18045 else if (init && TREE_CODE (init) == DECL_EXPR) 18046 { 18047 /* We need to jump through some hoops to handle declarations in the 18048 init-statement, since we might need to handle auto deduction, 18049 but we need to keep control of initialization. */ 18050 decl_expr = init; 18051 init = DECL_INITIAL (DECL_EXPR_DECL (init)); 18052 decl = tsubst_decl (decl, args, complain); 18053 } 18054 else 18055 { 18056 if (TREE_CODE (decl) == SCOPE_REF) 18057 { 18058 decl = RECUR (decl); 18059 if (TREE_CODE (decl) == COMPONENT_REF) 18060 { 18061 tree v = decl; 18062 while (v) 18063 switch (TREE_CODE (v)) 18064 { 18065 case COMPONENT_REF: 18066 case MEM_REF: 18067 case INDIRECT_REF: 18068 CASE_CONVERT: 18069 case POINTER_PLUS_EXPR: 18070 v = TREE_OPERAND (v, 0); 18071 continue; 18072 case PARM_DECL: 18073 if (DECL_CONTEXT (v) == current_function_decl 18074 && DECL_ARTIFICIAL (v) 18075 && DECL_NAME (v) == this_identifier) 18076 { 18077 decl = TREE_OPERAND (decl, 1); 18078 decl = omp_privatize_field (decl, false); 18079 } 18080 /* FALLTHRU */ 18081 default: 18082 v = NULL_TREE; 18083 break; 18084 } 18085 } 18086 } 18087 else 18088 decl = RECUR (decl); 18089 } 18090 if (init && TREE_CODE (init) == TREE_VEC) 18091 { 18092 init = copy_node (init); 18093 TREE_VEC_ELT (init, 0) 18094 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain); 18095 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1)); 18096 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2)); 18097 } 18098 else 18099 init = RECUR (init); 18100 18101 if (orig_declv && OMP_FOR_ORIG_DECLS (t)) 18102 { 18103 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i); 18104 if (TREE_CODE (o) == TREE_LIST) 18105 TREE_VEC_ELT (orig_declv, i) 18106 = tree_cons (RECUR (TREE_PURPOSE (o)), 18107 RECUR (TREE_VALUE (o)), 18108 NULL_TREE); 18109 else 18110 TREE_VEC_ELT (orig_declv, i) = RECUR (o); 18111 } 18112 18113 if (range_for) 18114 { 18115 tree this_pre_body = NULL_TREE; 18116 tree orig_init = NULL_TREE; 18117 tree orig_decl = NULL_TREE; 18118 tree init_sl = NULL_TREE; 18119 cp_convert_omp_range_for (this_pre_body, init_sl, decl, orig_decl, init, 18120 orig_init, cond, incr); 18121 if (orig_decl) 18122 { 18123 if (orig_declv == NULL_TREE) 18124 orig_declv = copy_node (declv); 18125 TREE_VEC_ELT (orig_declv, i) = orig_decl; 18126 ret = true; 18127 } 18128 else if (orig_declv) 18129 TREE_VEC_ELT (orig_declv, i) = decl; 18130 } 18131 18132 tree auto_node = type_uses_auto (TREE_TYPE (decl)); 18133 if (!range_for && auto_node && init) 18134 TREE_TYPE (decl) 18135 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain); 18136 18137 gcc_assert (!type_dependent_expression_p (decl)); 18138 18139 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for) 18140 { 18141 if (decl_expr) 18142 { 18143 /* Declare the variable, but don't let that initialize it. */ 18144 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr)); 18145 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE; 18146 RECUR (decl_expr); 18147 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav; 18148 } 18149 18150 if (!range_for) 18151 { 18152 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i); 18153 if (COMPARISON_CLASS_P (cond) 18154 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC) 18155 { 18156 tree lhs = RECUR (TREE_OPERAND (cond, 0)); 18157 tree rhs = copy_node (TREE_OPERAND (cond, 1)); 18158 TREE_VEC_ELT (rhs, 0) 18159 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain); 18160 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1)); 18161 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2)); 18162 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond), 18163 lhs, rhs); 18164 } 18165 else 18166 cond = RECUR (cond); 18167 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i); 18168 if (TREE_CODE (incr) == MODIFY_EXPR) 18169 { 18170 tree lhs = RECUR (TREE_OPERAND (incr, 0)); 18171 tree rhs = RECUR (TREE_OPERAND (incr, 1)); 18172 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs, 18173 NOP_EXPR, rhs, NULL_TREE, complain); 18174 } 18175 else 18176 incr = RECUR (incr); 18177 if (orig_declv && !OMP_FOR_ORIG_DECLS (t)) 18178 TREE_VEC_ELT (orig_declv, i) = decl; 18179 } 18180 TREE_VEC_ELT (declv, i) = decl; 18181 TREE_VEC_ELT (initv, i) = init; 18182 TREE_VEC_ELT (condv, i) = cond; 18183 TREE_VEC_ELT (incrv, i) = incr; 18184 return ret; 18185 } 18186 18187 if (decl_expr) 18188 { 18189 /* Declare and initialize the variable. */ 18190 RECUR (decl_expr); 18191 init = NULL_TREE; 18192 } 18193 else if (init) 18194 { 18195 tree *pc; 18196 int j; 18197 for (j = ((omp_parallel_combined_clauses == NULL 18198 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++) 18199 { 18200 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; ) 18201 { 18202 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE 18203 && OMP_CLAUSE_DECL (*pc) == decl) 18204 break; 18205 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE 18206 && OMP_CLAUSE_DECL (*pc) == decl) 18207 { 18208 if (j) 18209 break; 18210 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */ 18211 tree c = *pc; 18212 *pc = OMP_CLAUSE_CHAIN (c); 18213 OMP_CLAUSE_CHAIN (c) = *clauses; 18214 *clauses = c; 18215 } 18216 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE 18217 && OMP_CLAUSE_DECL (*pc) == decl) 18218 { 18219 error ("iteration variable %qD should not be firstprivate", 18220 decl); 18221 *pc = OMP_CLAUSE_CHAIN (*pc); 18222 } 18223 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION 18224 && OMP_CLAUSE_DECL (*pc) == decl) 18225 { 18226 error ("iteration variable %qD should not be reduction", 18227 decl); 18228 *pc = OMP_CLAUSE_CHAIN (*pc); 18229 } 18230 else 18231 pc = &OMP_CLAUSE_CHAIN (*pc); 18232 } 18233 if (*pc) 18234 break; 18235 } 18236 if (*pc == NULL_TREE) 18237 { 18238 tree c = build_omp_clause (input_location, 18239 TREE_CODE (t) == OMP_LOOP 18240 ? OMP_CLAUSE_LASTPRIVATE 18241 : OMP_CLAUSE_PRIVATE); 18242 OMP_CLAUSE_DECL (c) = decl; 18243 c = finish_omp_clauses (c, C_ORT_OMP); 18244 if (c) 18245 { 18246 OMP_CLAUSE_CHAIN (c) = *clauses; 18247 *clauses = c; 18248 } 18249 } 18250 } 18251 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i); 18252 if (COMPARISON_CLASS_P (cond)) 18253 { 18254 tree op0 = RECUR (TREE_OPERAND (cond, 0)); 18255 tree op1 = RECUR (TREE_OPERAND (cond, 1)); 18256 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1); 18257 } 18258 else 18259 cond = RECUR (cond); 18260 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i); 18261 switch (TREE_CODE (incr)) 18262 { 18263 case PREINCREMENT_EXPR: 18264 case PREDECREMENT_EXPR: 18265 case POSTINCREMENT_EXPR: 18266 case POSTDECREMENT_EXPR: 18267 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl), 18268 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE); 18269 break; 18270 case MODIFY_EXPR: 18271 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR 18272 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR) 18273 { 18274 tree rhs = TREE_OPERAND (incr, 1); 18275 tree lhs = RECUR (TREE_OPERAND (incr, 0)); 18276 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0)); 18277 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1)); 18278 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs, 18279 build2 (TREE_CODE (rhs), TREE_TYPE (decl), 18280 rhs0, rhs1)); 18281 } 18282 else 18283 incr = RECUR (incr); 18284 break; 18285 case MODOP_EXPR: 18286 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR 18287 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR) 18288 { 18289 tree lhs = RECUR (TREE_OPERAND (incr, 0)); 18290 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs, 18291 build2 (TREE_CODE (TREE_OPERAND (incr, 1)), 18292 TREE_TYPE (decl), lhs, 18293 RECUR (TREE_OPERAND (incr, 2)))); 18294 } 18295 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR 18296 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR 18297 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR))) 18298 { 18299 tree rhs = TREE_OPERAND (incr, 2); 18300 tree lhs = RECUR (TREE_OPERAND (incr, 0)); 18301 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0)); 18302 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1)); 18303 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs, 18304 build2 (TREE_CODE (rhs), TREE_TYPE (decl), 18305 rhs0, rhs1)); 18306 } 18307 else 18308 incr = RECUR (incr); 18309 break; 18310 default: 18311 incr = RECUR (incr); 18312 break; 18313 } 18314 18315 if (orig_declv && !OMP_FOR_ORIG_DECLS (t)) 18316 TREE_VEC_ELT (orig_declv, i) = decl; 18317 TREE_VEC_ELT (declv, i) = decl; 18318 TREE_VEC_ELT (initv, i) = init; 18319 TREE_VEC_ELT (condv, i) = cond; 18320 TREE_VEC_ELT (incrv, i) = incr; 18321 return false; 18322 #undef RECUR 18323 } 18324 18325 /* Helper function of tsubst_expr, find OMP_TEAMS inside 18326 of OMP_TARGET's body. */ 18327 18328 static tree 18329 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *) 18330 { 18331 *walk_subtrees = 0; 18332 switch (TREE_CODE (*tp)) 18333 { 18334 case OMP_TEAMS: 18335 return *tp; 18336 case BIND_EXPR: 18337 case STATEMENT_LIST: 18338 *walk_subtrees = 1; 18339 break; 18340 default: 18341 break; 18342 } 18343 return NULL_TREE; 18344 } 18345 18346 /* Helper function for tsubst_expr. For decomposition declaration 18347 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst 18348 also the corresponding decls representing the identifiers 18349 of the decomposition declaration. Return DECL if successful 18350 or error_mark_node otherwise, set *FIRST to the first decl 18351 in the list chained through DECL_CHAIN and *CNT to the number 18352 of such decls. */ 18353 18354 static tree 18355 tsubst_decomp_names (tree decl, tree pattern_decl, tree args, 18356 tsubst_flags_t complain, tree in_decl, cp_decomp *decomp) 18357 { 18358 tree decl2, decl3, prev = decl; 18359 decomp->count = 0; 18360 gcc_assert (DECL_NAME (decl) == NULL_TREE); 18361 for (decl2 = DECL_CHAIN (pattern_decl); 18362 decl2 18363 && VAR_P (decl2) 18364 && DECL_DECOMPOSITION_P (decl2) 18365 && DECL_NAME (decl2); 18366 decl2 = DECL_CHAIN (decl2)) 18367 { 18368 if (TREE_TYPE (decl2) == error_mark_node && decomp->count == 0) 18369 { 18370 gcc_assert (errorcount); 18371 return error_mark_node; 18372 } 18373 decomp->count++; 18374 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl); 18375 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2)); 18376 tree v = DECL_VALUE_EXPR (decl2); 18377 DECL_HAS_VALUE_EXPR_P (decl2) = 0; 18378 SET_DECL_VALUE_EXPR (decl2, NULL_TREE); 18379 decl3 = tsubst (decl2, args, complain, in_decl); 18380 SET_DECL_VALUE_EXPR (decl2, v); 18381 DECL_HAS_VALUE_EXPR_P (decl2) = 1; 18382 if (VAR_P (decl3)) 18383 DECL_TEMPLATE_INSTANTIATED (decl3) = 1; 18384 else 18385 { 18386 gcc_assert (errorcount); 18387 decl = error_mark_node; 18388 continue; 18389 } 18390 maybe_push_decl (decl3); 18391 if (error_operand_p (decl3)) 18392 decl = error_mark_node; 18393 else if (decl != error_mark_node 18394 && DECL_CHAIN (decl3) != prev 18395 && decl != prev) 18396 { 18397 gcc_assert (errorcount); 18398 decl = error_mark_node; 18399 } 18400 else 18401 prev = decl3; 18402 } 18403 decomp->decl = prev; 18404 return decl; 18405 } 18406 18407 /* Return the proper local_specialization for init-capture pack DECL. */ 18408 18409 static tree 18410 lookup_init_capture_pack (tree decl) 18411 { 18412 /* We handle normal pack captures by forwarding to the specialization of the 18413 captured parameter. We can't do that for pack init-captures; we need them 18414 to have their own local_specialization. We created the individual 18415 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them 18416 when we process the DECL_EXPR for the pack init-capture in the template. 18417 So, how do we find them? We don't know the capture proxy pack when 18418 building the individual resulting proxies, and we don't know the 18419 individual proxies when instantiating the pack. What we have in common is 18420 the FIELD_DECL. 18421 18422 So...when we instantiate the FIELD_DECL, we stick the result in 18423 local_specializations. Then at the DECL_EXPR we look up that result, see 18424 how many elements it has, synthesize the names, and look them up. */ 18425 18426 tree cname = DECL_NAME (decl); 18427 tree val = DECL_VALUE_EXPR (decl); 18428 tree field = TREE_OPERAND (val, 1); 18429 gcc_assert (TREE_CODE (field) == FIELD_DECL); 18430 tree fpack = retrieve_local_specialization (field); 18431 if (fpack == error_mark_node) 18432 return error_mark_node; 18433 18434 int len = 1; 18435 tree vec = NULL_TREE; 18436 tree r = NULL_TREE; 18437 if (TREE_CODE (fpack) == TREE_VEC) 18438 { 18439 len = TREE_VEC_LENGTH (fpack); 18440 vec = make_tree_vec (len); 18441 r = make_node (NONTYPE_ARGUMENT_PACK); 18442 ARGUMENT_PACK_ARGS (r) = vec; 18443 } 18444 for (int i = 0; i < len; ++i) 18445 { 18446 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname; 18447 tree elt = lookup_name (ename); 18448 if (vec) 18449 TREE_VEC_ELT (vec, i) = elt; 18450 else 18451 r = elt; 18452 } 18453 return r; 18454 } 18455 18456 /* T is an operand of a template tree being substituted. Return whether 18457 T is dependent such that we should suppress some warnings that would 18458 make sense if the substituted expression were written directly, like 18459 template <int I> bool f() { return I == 2; } 18460 We don't want to warn when instantiating f that comparing two constants 18461 always has the same value. 18462 18463 This is a more limited concept of dependence than instantiation-dependent; 18464 here we don't care whether substitution could fail. */ 18465 18466 static bool 18467 dependent_operand_p (tree t) 18468 { 18469 while (TREE_CODE (t) == IMPLICIT_CONV_EXPR) 18470 t = TREE_OPERAND (t, 0); 18471 ++processing_template_decl; 18472 bool r = (potential_constant_expression (t) 18473 ? value_dependent_expression_p (t) 18474 : type_dependent_expression_p (t)); 18475 --processing_template_decl; 18476 return r; 18477 } 18478 18479 /* A superset of tsubst_expr that also handles statement trees. */ 18480 18481 static tree 18482 tsubst_stmt (tree t, tree args, tsubst_flags_t complain, tree in_decl) 18483 { 18484 #define RETURN(EXP) do { r = (EXP); goto out; } while(0) 18485 #define RECUR(NODE) \ 18486 tsubst_stmt ((NODE), args, complain, in_decl) 18487 18488 tree stmt, tmp; 18489 tree r; 18490 location_t loc; 18491 18492 if (t == NULL_TREE || t == error_mark_node) 18493 return t; 18494 18495 loc = input_location; 18496 if (location_t eloc = cp_expr_location (t)) 18497 input_location = eloc; 18498 if (STATEMENT_CODE_P (TREE_CODE (t))) 18499 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t); 18500 18501 switch (TREE_CODE (t)) 18502 { 18503 case STATEMENT_LIST: 18504 { 18505 for (tree stmt : tsi_range (t)) 18506 RECUR (stmt); 18507 break; 18508 } 18509 18510 case CTOR_INITIALIZER: 18511 finish_mem_initializers (tsubst_initializer_list 18512 (TREE_OPERAND (t, 0), args)); 18513 break; 18514 18515 case RETURN_EXPR: 18516 finish_return_stmt (RECUR (TREE_OPERAND (t, 0))); 18517 break; 18518 18519 case CO_RETURN_EXPR: 18520 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0))); 18521 break; 18522 18523 case EXPR_STMT: 18524 tmp = RECUR (EXPR_STMT_EXPR (t)); 18525 if (EXPR_STMT_STMT_EXPR_RESULT (t)) 18526 finish_stmt_expr_expr (tmp, cur_stmt_expr); 18527 else 18528 finish_expr_stmt (tmp); 18529 break; 18530 18531 case USING_STMT: 18532 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE); 18533 break; 18534 18535 case PRECONDITION_STMT: 18536 case POSTCONDITION_STMT: 18537 gcc_unreachable (); 18538 18539 case ASSERTION_STMT: 18540 { 18541 r = tsubst_contract (NULL_TREE, t, args, complain, in_decl); 18542 if (r != error_mark_node) 18543 add_stmt (r); 18544 RETURN (r); 18545 } 18546 break; 18547 18548 case DECL_EXPR: 18549 { 18550 tree decl, pattern_decl; 18551 tree init; 18552 18553 pattern_decl = decl = DECL_EXPR_DECL (t); 18554 if (TREE_CODE (decl) == LABEL_DECL) 18555 finish_label_decl (DECL_NAME (decl)); 18556 else if (TREE_CODE (decl) == USING_DECL) 18557 { 18558 tree scope = USING_DECL_SCOPE (decl); 18559 if (DECL_DEPENDENT_P (decl)) 18560 { 18561 scope = tsubst (scope, args, complain, in_decl); 18562 if (!MAYBE_CLASS_TYPE_P (scope) 18563 && TREE_CODE (scope) != ENUMERAL_TYPE) 18564 { 18565 if (complain & tf_error) 18566 error_at (DECL_SOURCE_LOCATION (decl), "%qT is not a " 18567 "class, namespace, or enumeration", scope); 18568 return error_mark_node; 18569 } 18570 finish_nonmember_using_decl (scope, DECL_NAME (decl)); 18571 } 18572 else 18573 { 18574 /* This is a non-dependent using-decl, and we'll have 18575 used the names it found during template parsing. We do 18576 not want to do the lookup again, because we might not 18577 find the things we found then. */ 18578 gcc_checking_assert (scope == tsubst (scope, args, 18579 complain, in_decl)); 18580 /* We still need to push the bindings so that we can look up 18581 this name later. */ 18582 push_using_decl_bindings (DECL_NAME (decl), 18583 USING_DECL_DECLS (decl)); 18584 } 18585 } 18586 else if (is_capture_proxy (decl) 18587 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl)) 18588 { 18589 /* We're in tsubst_lambda_expr, we've already inserted a new 18590 capture proxy, so look it up and register it. */ 18591 tree inst; 18592 if (!DECL_PACK_P (decl)) 18593 { 18594 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK, 18595 LOOK_want::HIDDEN_LAMBDA); 18596 gcc_assert (inst != decl && is_capture_proxy (inst)); 18597 } 18598 else if (is_normal_capture_proxy (decl)) 18599 { 18600 inst = (retrieve_local_specialization 18601 (DECL_CAPTURED_VARIABLE (decl))); 18602 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK 18603 || DECL_PACK_P (inst)); 18604 } 18605 else 18606 inst = lookup_init_capture_pack (decl); 18607 18608 register_local_specialization (inst, decl); 18609 break; 18610 } 18611 else if (DECL_PRETTY_FUNCTION_P (decl)) 18612 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl), 18613 DECL_NAME (decl), 18614 true/*DECL_PRETTY_FUNCTION_P (decl)*/); 18615 else if (DECL_IMPLICIT_TYPEDEF_P (decl) 18616 && LAMBDA_TYPE_P (TREE_TYPE (decl))) 18617 /* Don't copy the old closure; we'll create a new one in 18618 tsubst_lambda_expr. */ 18619 break; 18620 else 18621 { 18622 init = DECL_INITIAL (decl); 18623 decl = tsubst (decl, args, complain, in_decl); 18624 if (decl != error_mark_node) 18625 { 18626 /* By marking the declaration as instantiated, we avoid 18627 trying to instantiate it. Since instantiate_decl can't 18628 handle local variables, and since we've already done 18629 all that needs to be done, that's the right thing to 18630 do. */ 18631 if (VAR_P (decl)) 18632 DECL_TEMPLATE_INSTANTIATED (decl) = 1; 18633 if (VAR_P (decl) && !DECL_NAME (decl) 18634 && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) 18635 /* Anonymous aggregates are a special case. */ 18636 finish_anon_union (decl); 18637 else if (is_capture_proxy (DECL_EXPR_DECL (t))) 18638 { 18639 DECL_CONTEXT (decl) = current_function_decl; 18640 if (DECL_NAME (decl) == this_identifier) 18641 { 18642 tree lam = DECL_CONTEXT (current_function_decl); 18643 lam = CLASSTYPE_LAMBDA_EXPR (lam); 18644 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl; 18645 } 18646 insert_capture_proxy (decl); 18647 } 18648 else if (DECL_IMPLICIT_TYPEDEF_P (t)) 18649 /* We already did a pushtag. */; 18650 else if (VAR_OR_FUNCTION_DECL_P (decl) 18651 && DECL_LOCAL_DECL_P (decl)) 18652 { 18653 if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL) 18654 DECL_CONTEXT (decl) = NULL_TREE; 18655 decl = pushdecl (decl); 18656 if (TREE_CODE (decl) == FUNCTION_DECL 18657 && DECL_OMP_DECLARE_REDUCTION_P (decl) 18658 && cp_check_omp_declare_reduction (decl)) 18659 instantiate_body (pattern_decl, args, decl, true); 18660 } 18661 else 18662 { 18663 bool const_init = false; 18664 cp_decomp decomp_d, *decomp = NULL; 18665 tree ndecl = error_mark_node; 18666 tree asmspec_tree = NULL_TREE; 18667 maybe_push_decl (decl); 18668 18669 if (VAR_P (decl) 18670 && DECL_LANG_SPECIFIC (decl) 18671 && DECL_OMP_PRIVATIZED_MEMBER (decl)) 18672 break; 18673 18674 if (VAR_P (decl) 18675 && DECL_DECOMPOSITION_P (decl) 18676 && TREE_TYPE (pattern_decl) != error_mark_node) 18677 { 18678 decomp = &decomp_d; 18679 ndecl = tsubst_decomp_names (decl, pattern_decl, args, 18680 complain, in_decl, decomp); 18681 } 18682 18683 init = tsubst_init (init, decl, args, complain, in_decl); 18684 18685 if (VAR_P (decl)) 18686 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P 18687 (pattern_decl)); 18688 18689 /* In a non-template function, VLA type declarations are 18690 handled in grokdeclarator; for templates, handle them 18691 now. */ 18692 predeclare_vla (decl); 18693 18694 if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl)) 18695 { 18696 tree id = DECL_ASSEMBLER_NAME (pattern_decl); 18697 const char *asmspec = IDENTIFIER_POINTER (id); 18698 gcc_assert (asmspec[0] == '*'); 18699 asmspec_tree 18700 = build_string (IDENTIFIER_LENGTH (id) - 1, 18701 asmspec + 1); 18702 TREE_TYPE (asmspec_tree) = char_array_type_node; 18703 } 18704 18705 cp_finish_decl (decl, init, const_init, asmspec_tree, 0, 18706 decomp); 18707 18708 if (ndecl != error_mark_node) 18709 cp_finish_decomp (ndecl, decomp); 18710 } 18711 } 18712 } 18713 18714 break; 18715 } 18716 18717 case FOR_STMT: 18718 stmt = begin_for_stmt (NULL_TREE, NULL_TREE); 18719 RECUR (FOR_INIT_STMT (t)); 18720 finish_init_stmt (stmt); 18721 tmp = RECUR (FOR_COND (t)); 18722 finish_for_cond (tmp, stmt, false, 0, false); 18723 tmp = RECUR (FOR_EXPR (t)); 18724 finish_for_expr (tmp, stmt); 18725 { 18726 bool prev = note_iteration_stmt_body_start (); 18727 RECUR (FOR_BODY (t)); 18728 note_iteration_stmt_body_end (prev); 18729 } 18730 finish_for_stmt (stmt); 18731 break; 18732 18733 case RANGE_FOR_STMT: 18734 { 18735 /* Construct another range_for, if this is not a final 18736 substitution (for inside a generic lambda of a 18737 template). Otherwise convert to a regular for. */ 18738 tree decl, expr; 18739 stmt = (processing_template_decl 18740 ? begin_range_for_stmt (NULL_TREE, NULL_TREE) 18741 : begin_for_stmt (NULL_TREE, NULL_TREE)); 18742 RECUR (RANGE_FOR_INIT_STMT (t)); 18743 decl = RANGE_FOR_DECL (t); 18744 decl = tsubst (decl, args, complain, in_decl); 18745 maybe_push_decl (decl); 18746 expr = RECUR (RANGE_FOR_EXPR (t)); 18747 18748 cp_decomp decomp_d, *decomp = NULL; 18749 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl)) 18750 { 18751 decomp = &decomp_d; 18752 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args, 18753 complain, in_decl, decomp); 18754 } 18755 18756 tree unroll = RECUR (RANGE_FOR_UNROLL (t)); 18757 if (unroll) 18758 unroll 18759 = cp_check_pragma_unroll (EXPR_LOCATION (RANGE_FOR_UNROLL (t)), 18760 unroll); 18761 if (processing_template_decl) 18762 { 18763 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t); 18764 RANGE_FOR_UNROLL (stmt) = unroll; 18765 RANGE_FOR_NOVECTOR (stmt) = RANGE_FOR_NOVECTOR (t); 18766 finish_range_for_decl (stmt, decl, expr); 18767 if (decomp && decl != error_mark_node) 18768 cp_finish_decomp (decl, decomp); 18769 } 18770 else 18771 stmt = cp_convert_range_for (stmt, decl, expr, decomp, 18772 RANGE_FOR_IVDEP (t), unroll, 18773 RANGE_FOR_NOVECTOR (t)); 18774 18775 bool prev = note_iteration_stmt_body_start (); 18776 RECUR (RANGE_FOR_BODY (t)); 18777 note_iteration_stmt_body_end (prev); 18778 finish_for_stmt (stmt); 18779 } 18780 break; 18781 18782 case WHILE_STMT: 18783 stmt = begin_while_stmt (); 18784 tmp = RECUR (WHILE_COND (t)); 18785 finish_while_stmt_cond (tmp, stmt, false, 0, false); 18786 { 18787 bool prev = note_iteration_stmt_body_start (); 18788 RECUR (WHILE_BODY (t)); 18789 note_iteration_stmt_body_end (prev); 18790 } 18791 finish_while_stmt (stmt); 18792 break; 18793 18794 case DO_STMT: 18795 stmt = begin_do_stmt (); 18796 { 18797 bool prev = note_iteration_stmt_body_start (); 18798 RECUR (DO_BODY (t)); 18799 note_iteration_stmt_body_end (prev); 18800 } 18801 finish_do_body (stmt); 18802 tmp = RECUR (DO_COND (t)); 18803 finish_do_stmt (tmp, stmt, false, 0, false); 18804 break; 18805 18806 case IF_STMT: 18807 stmt = begin_if_stmt (); 18808 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t); 18809 IF_STMT_CONSTEVAL_P (stmt) = IF_STMT_CONSTEVAL_P (t); 18810 if (IF_STMT_CONSTEXPR_P (t)) 18811 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args, complain, in_decl); 18812 { 18813 tree cond = IF_COND (t); 18814 bool was_dep = dependent_operand_p (cond); 18815 cond = RECUR (cond); 18816 warning_sentinel s1(warn_address, was_dep); 18817 tmp = finish_if_stmt_cond (cond, stmt); 18818 } 18819 if (IF_STMT_CONSTEXPR_P (t) 18820 && instantiation_dependent_expression_p (tmp)) 18821 { 18822 /* We're partially instantiating a generic lambda, but the condition 18823 of the constexpr if is still dependent. Don't substitute into the 18824 branches now, just remember the template arguments. */ 18825 do_poplevel (IF_SCOPE (stmt)); 18826 IF_SCOPE (stmt) = NULL_TREE; 18827 IF_COND (stmt) = IF_COND (t); 18828 THEN_CLAUSE (stmt) = THEN_CLAUSE (t); 18829 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t); 18830 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (stmt, args, complain); 18831 add_stmt (stmt); 18832 break; 18833 } 18834 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp)) 18835 /* Don't instantiate the THEN_CLAUSE. */; 18836 else if (IF_STMT_CONSTEVAL_P (t)) 18837 { 18838 bool save_in_consteval_if_p = in_consteval_if_p; 18839 in_consteval_if_p = true; 18840 RECUR (THEN_CLAUSE (t)); 18841 in_consteval_if_p = save_in_consteval_if_p; 18842 } 18843 else 18844 { 18845 tree folded = fold_non_dependent_expr (tmp, complain); 18846 bool inhibit = integer_zerop (folded); 18847 if (inhibit) 18848 ++c_inhibit_evaluation_warnings; 18849 RECUR (THEN_CLAUSE (t)); 18850 if (inhibit) 18851 --c_inhibit_evaluation_warnings; 18852 } 18853 finish_then_clause (stmt); 18854 18855 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp)) 18856 /* Don't instantiate the ELSE_CLAUSE. */; 18857 else if (ELSE_CLAUSE (t)) 18858 { 18859 tree folded = fold_non_dependent_expr (tmp, complain); 18860 bool inhibit = integer_nonzerop (folded); 18861 begin_else_clause (stmt); 18862 if (inhibit) 18863 ++c_inhibit_evaluation_warnings; 18864 RECUR (ELSE_CLAUSE (t)); 18865 if (inhibit) 18866 --c_inhibit_evaluation_warnings; 18867 finish_else_clause (stmt); 18868 } 18869 18870 finish_if_stmt (stmt); 18871 break; 18872 18873 case BIND_EXPR: 18874 if (BIND_EXPR_BODY_BLOCK (t)) 18875 stmt = begin_function_body (); 18876 else 18877 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t) 18878 ? BCS_TRY_BLOCK : 0); 18879 18880 RECUR (BIND_EXPR_BODY (t)); 18881 18882 if (BIND_EXPR_BODY_BLOCK (t)) 18883 finish_function_body (stmt); 18884 else 18885 finish_compound_stmt (stmt); 18886 break; 18887 18888 case BREAK_STMT: 18889 finish_break_stmt (); 18890 break; 18891 18892 case CONTINUE_STMT: 18893 finish_continue_stmt (); 18894 break; 18895 18896 case SWITCH_STMT: 18897 stmt = begin_switch_stmt (); 18898 tmp = RECUR (SWITCH_STMT_COND (t)); 18899 finish_switch_cond (tmp, stmt); 18900 RECUR (SWITCH_STMT_BODY (t)); 18901 finish_switch_stmt (stmt); 18902 break; 18903 18904 case CASE_LABEL_EXPR: 18905 { 18906 tree decl = CASE_LABEL (t); 18907 tree low = RECUR (CASE_LOW (t)); 18908 tree high = RECUR (CASE_HIGH (t)); 18909 tree l = finish_case_label (EXPR_LOCATION (t), low, high); 18910 if (l && TREE_CODE (l) == CASE_LABEL_EXPR) 18911 { 18912 tree label = CASE_LABEL (l); 18913 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl); 18914 if (DECL_ATTRIBUTES (decl) != NULL_TREE) 18915 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0); 18916 } 18917 } 18918 break; 18919 18920 case LABEL_EXPR: 18921 { 18922 tree decl = LABEL_EXPR_LABEL (t); 18923 tree label; 18924 18925 label = finish_label_stmt (DECL_NAME (decl)); 18926 if (TREE_CODE (label) == LABEL_DECL) 18927 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl); 18928 if (DECL_ATTRIBUTES (decl) != NULL_TREE) 18929 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0); 18930 } 18931 break; 18932 18933 case GOTO_EXPR: 18934 tmp = GOTO_DESTINATION (t); 18935 if (TREE_CODE (tmp) != LABEL_DECL) 18936 /* Computed goto's must be tsubst'd into. On the other hand, 18937 non-computed gotos must not be; the identifier in question 18938 will have no binding. */ 18939 tmp = RECUR (tmp); 18940 else 18941 tmp = DECL_NAME (tmp); 18942 finish_goto_stmt (tmp); 18943 break; 18944 18945 case ASM_EXPR: 18946 { 18947 tree string = RECUR (ASM_STRING (t)); 18948 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, 18949 complain, in_decl); 18950 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args, 18951 complain, in_decl); 18952 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, 18953 complain, in_decl); 18954 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args, 18955 complain, in_decl); 18956 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string, 18957 outputs, inputs, clobbers, labels, 18958 ASM_INLINE_P (t)); 18959 tree asm_expr = tmp; 18960 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR) 18961 asm_expr = TREE_OPERAND (asm_expr, 0); 18962 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t); 18963 } 18964 break; 18965 18966 case TRY_BLOCK: 18967 if (CLEANUP_P (t)) 18968 { 18969 stmt = begin_try_block (); 18970 RECUR (TRY_STMTS (t)); 18971 finish_cleanup_try_block (stmt); 18972 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt); 18973 } 18974 else 18975 { 18976 tree compound_stmt = NULL_TREE; 18977 18978 if (FN_TRY_BLOCK_P (t)) 18979 stmt = begin_function_try_block (&compound_stmt); 18980 else 18981 stmt = begin_try_block (); 18982 18983 RECUR (TRY_STMTS (t)); 18984 18985 if (FN_TRY_BLOCK_P (t)) 18986 finish_function_try_block (stmt); 18987 else 18988 finish_try_block (stmt); 18989 18990 RECUR (TRY_HANDLERS (t)); 18991 if (FN_TRY_BLOCK_P (t)) 18992 finish_function_handler_sequence (stmt, compound_stmt); 18993 else 18994 finish_handler_sequence (stmt); 18995 } 18996 break; 18997 18998 case HANDLER: 18999 { 19000 tree decl = HANDLER_PARMS (t); 19001 19002 if (decl) 19003 { 19004 decl = tsubst (decl, args, complain, in_decl); 19005 /* Prevent instantiate_decl from trying to instantiate 19006 this variable. We've already done all that needs to be 19007 done. */ 19008 if (decl != error_mark_node) 19009 DECL_TEMPLATE_INSTANTIATED (decl) = 1; 19010 } 19011 stmt = begin_handler (); 19012 finish_handler_parms (decl, stmt); 19013 RECUR (HANDLER_BODY (t)); 19014 finish_handler (stmt); 19015 } 19016 break; 19017 19018 case TAG_DEFN: 19019 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE); 19020 if (dependent_type_p (tmp)) 19021 /* This is a partial instantiation, try again when full. */ 19022 add_stmt (build_min (TAG_DEFN, tmp)); 19023 else if (CLASS_TYPE_P (tmp)) 19024 { 19025 /* Local classes are not independent templates; they are 19026 instantiated along with their containing function. And this 19027 way we don't have to deal with pushing out of one local class 19028 to instantiate a member of another local class. */ 19029 /* Closures are handled by the LAMBDA_EXPR. */ 19030 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t))); 19031 complete_type (tmp); 19032 tree save_ccp = current_class_ptr; 19033 tree save_ccr = current_class_ref; 19034 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld)) 19035 if ((VAR_P (fld) 19036 || (TREE_CODE (fld) == FUNCTION_DECL 19037 && !DECL_ARTIFICIAL (fld))) 19038 && DECL_TEMPLATE_INSTANTIATION (fld)) 19039 instantiate_decl (fld, /*defer_ok=*/false, 19040 /*expl_inst_class=*/false); 19041 else if (TREE_CODE (fld) == FIELD_DECL) 19042 maybe_instantiate_nsdmi_init (fld, tf_warning_or_error); 19043 current_class_ptr = save_ccp; 19044 current_class_ref = save_ccr; 19045 } 19046 break; 19047 19048 case STATIC_ASSERT: 19049 { 19050 tree condition, message; 19051 19052 ++c_inhibit_evaluation_warnings; 19053 condition = tsubst_expr (STATIC_ASSERT_CONDITION (t), args, 19054 complain, in_decl); 19055 message = tsubst_expr (STATIC_ASSERT_MESSAGE (t), args, 19056 complain, in_decl); 19057 if (TREE_CODE (STATIC_ASSERT_MESSAGE (t)) != STRING_CST 19058 && TREE_CODE (message) == STRING_CST) 19059 message = build1_loc (STATIC_ASSERT_SOURCE_LOCATION (t), 19060 PAREN_EXPR, TREE_TYPE (message), message); 19061 --c_inhibit_evaluation_warnings; 19062 19063 finish_static_assert (condition, message, 19064 STATIC_ASSERT_SOURCE_LOCATION (t), 19065 /*member_p=*/false, /*show_expr_p=*/true); 19066 } 19067 break; 19068 19069 case OACC_KERNELS: 19070 case OACC_PARALLEL: 19071 case OACC_SERIAL: 19072 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC_TARGET, args, 19073 complain, in_decl); 19074 stmt = begin_omp_parallel (); 19075 RECUR (OMP_BODY (t)); 19076 finish_omp_construct (TREE_CODE (t), stmt, tmp); 19077 break; 19078 19079 case OMP_PARALLEL: 19080 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t)); 19081 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args, 19082 complain, in_decl); 19083 if (OMP_PARALLEL_COMBINED (t)) 19084 omp_parallel_combined_clauses = &tmp; 19085 stmt = begin_omp_parallel (); 19086 RECUR (OMP_PARALLEL_BODY (t)); 19087 gcc_assert (omp_parallel_combined_clauses == NULL); 19088 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt)) 19089 = OMP_PARALLEL_COMBINED (t); 19090 pop_omp_privatization_clauses (r); 19091 break; 19092 19093 case OMP_TASK: 19094 if (OMP_TASK_BODY (t) == NULL_TREE) 19095 { 19096 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args, 19097 complain, in_decl); 19098 t = copy_node (t); 19099 OMP_TASK_CLAUSES (t) = tmp; 19100 add_stmt (t); 19101 break; 19102 } 19103 r = push_omp_privatization_clauses (false); 19104 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args, 19105 complain, in_decl); 19106 stmt = begin_omp_task (); 19107 RECUR (OMP_TASK_BODY (t)); 19108 finish_omp_task (tmp, stmt); 19109 pop_omp_privatization_clauses (r); 19110 break; 19111 19112 case OMP_FOR: 19113 case OMP_LOOP: 19114 case OMP_SIMD: 19115 case OMP_DISTRIBUTE: 19116 case OMP_TASKLOOP: 19117 case OACC_LOOP: 19118 { 19119 tree clauses, body, pre_body; 19120 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE; 19121 tree orig_declv = NULL_TREE; 19122 tree incrv = NULL_TREE; 19123 enum c_omp_region_type ort = C_ORT_OMP; 19124 bool any_range_for = false; 19125 int i; 19126 19127 if (TREE_CODE (t) == OACC_LOOP) 19128 ort = C_ORT_ACC; 19129 19130 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE); 19131 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain, 19132 in_decl); 19133 if (OMP_FOR_INIT (t) != NULL_TREE) 19134 { 19135 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); 19136 if (OMP_FOR_ORIG_DECLS (t)) 19137 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); 19138 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); 19139 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); 19140 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); 19141 } 19142 19143 keep_next_level (true); 19144 stmt = begin_omp_structured_block (); 19145 19146 pre_body = push_stmt_list (); 19147 RECUR (OMP_FOR_PRE_BODY (t)); 19148 pre_body = pop_stmt_list (pre_body); 19149 19150 if (OMP_FOR_INIT (t) != NULL_TREE) 19151 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++) 19152 any_range_for 19153 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, 19154 condv, incrv, &clauses, args, 19155 complain, in_decl); 19156 omp_parallel_combined_clauses = NULL; 19157 19158 if (any_range_for) 19159 { 19160 gcc_assert (orig_declv); 19161 body = begin_omp_structured_block (); 19162 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++) 19163 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i) 19164 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST 19165 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i))) 19166 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i), 19167 TREE_VEC_ELT (declv, i)); 19168 } 19169 else 19170 body = push_stmt_list (); 19171 RECUR (OMP_FOR_BODY (t)); 19172 if (any_range_for) 19173 body = finish_omp_structured_block (body); 19174 else 19175 body = pop_stmt_list (body); 19176 19177 if (OMP_FOR_INIT (t) != NULL_TREE) 19178 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, 19179 orig_declv, initv, condv, incrv, body, pre_body, 19180 NULL, clauses); 19181 else 19182 { 19183 t = make_node (TREE_CODE (t)); 19184 TREE_TYPE (t) = void_type_node; 19185 OMP_FOR_BODY (t) = body; 19186 OMP_FOR_PRE_BODY (t) = pre_body; 19187 OMP_FOR_CLAUSES (t) = clauses; 19188 SET_EXPR_LOCATION (t, EXPR_LOCATION (t)); 19189 add_stmt (t); 19190 } 19191 19192 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt), 19193 t)); 19194 pop_omp_privatization_clauses (r); 19195 } 19196 break; 19197 19198 case OMP_SECTIONS: 19199 case OMP_MASKED: 19200 omp_parallel_combined_clauses = NULL; 19201 /* FALLTHRU */ 19202 case OMP_SINGLE: 19203 case OMP_SCOPE: 19204 case OMP_TEAMS: 19205 case OMP_CRITICAL: 19206 case OMP_TASKGROUP: 19207 case OMP_SCAN: 19208 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS 19209 && OMP_TEAMS_COMBINED (t)); 19210 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain, 19211 in_decl); 19212 if (TREE_CODE (t) == OMP_TEAMS) 19213 { 19214 keep_next_level (true); 19215 stmt = begin_omp_structured_block (); 19216 RECUR (OMP_BODY (t)); 19217 stmt = finish_omp_structured_block (stmt); 19218 } 19219 else 19220 { 19221 stmt = push_stmt_list (); 19222 RECUR (OMP_BODY (t)); 19223 stmt = pop_stmt_list (stmt); 19224 } 19225 19226 if (TREE_CODE (t) == OMP_CRITICAL 19227 && tmp != NULL_TREE 19228 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp))) 19229 { 19230 error_at (OMP_CLAUSE_LOCATION (tmp), 19231 "%<#pragma omp critical%> with %<hint%> clause requires " 19232 "a name, except when %<omp_sync_hint_none%> is used"); 19233 RETURN (error_mark_node); 19234 } 19235 t = copy_node (t); 19236 OMP_BODY (t) = stmt; 19237 OMP_CLAUSES (t) = tmp; 19238 add_stmt (t); 19239 pop_omp_privatization_clauses (r); 19240 break; 19241 19242 case OMP_DEPOBJ: 19243 r = RECUR (OMP_DEPOBJ_DEPOBJ (t)); 19244 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node) 19245 { 19246 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID; 19247 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE) 19248 { 19249 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP, 19250 args, complain, in_decl); 19251 if (tmp == NULL_TREE) 19252 tmp = error_mark_node; 19253 } 19254 else 19255 { 19256 kind = (enum omp_clause_depend_kind) 19257 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t)); 19258 tmp = NULL_TREE; 19259 } 19260 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp); 19261 } 19262 else 19263 finish_omp_depobj (EXPR_LOCATION (t), r, 19264 OMP_CLAUSE_DEPEND_INVALID, 19265 OMP_DEPOBJ_CLAUSES (t)); 19266 break; 19267 19268 case OACC_DATA: 19269 case OMP_TARGET_DATA: 19270 case OMP_TARGET: 19271 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), 19272 TREE_CODE (t) == OACC_DATA 19273 ? C_ORT_ACC 19274 : TREE_CODE (t) == OMP_TARGET 19275 ? C_ORT_OMP_TARGET : C_ORT_OMP, 19276 args, complain, in_decl); 19277 keep_next_level (true); 19278 stmt = begin_omp_structured_block (); 19279 19280 RECUR (OMP_BODY (t)); 19281 stmt = finish_omp_structured_block (stmt); 19282 19283 t = copy_node (t); 19284 OMP_BODY (t) = stmt; 19285 OMP_CLAUSES (t) = tmp; 19286 19287 if (TREE_CODE (t) == OMP_TARGET) 19288 finish_omp_target_clauses (EXPR_LOCATION (t), OMP_BODY (t), 19289 &OMP_CLAUSES (t)); 19290 19291 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t)) 19292 { 19293 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL); 19294 if (teams) 19295 /* For combined target teams, ensure the num_teams and 19296 thread_limit clause expressions are evaluated on the host, 19297 before entering the target construct. */ 19298 for (tree c = OMP_TEAMS_CLAUSES (teams); 19299 c; c = OMP_CLAUSE_CHAIN (c)) 19300 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS 19301 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT) 19302 for (int i = 0; 19303 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i) 19304 if (OMP_CLAUSE_OPERAND (c, i) 19305 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST) 19306 { 19307 tree expr = OMP_CLAUSE_OPERAND (c, i); 19308 expr = force_target_expr (TREE_TYPE (expr), expr, 19309 tf_none); 19310 if (expr == error_mark_node) 19311 continue; 19312 tmp = TARGET_EXPR_SLOT (expr); 19313 add_stmt (expr); 19314 OMP_CLAUSE_OPERAND (c, i) = expr; 19315 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c), 19316 OMP_CLAUSE_FIRSTPRIVATE); 19317 OMP_CLAUSE_DECL (tc) = tmp; 19318 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t); 19319 OMP_TARGET_CLAUSES (t) = tc; 19320 } 19321 } 19322 add_stmt (t); 19323 break; 19324 19325 case OACC_DECLARE: 19326 t = copy_node (t); 19327 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args, 19328 complain, in_decl); 19329 OACC_DECLARE_CLAUSES (t) = tmp; 19330 add_stmt (t); 19331 break; 19332 19333 case OMP_TARGET_UPDATE: 19334 case OMP_TARGET_ENTER_DATA: 19335 case OMP_TARGET_EXIT_DATA: 19336 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args, 19337 complain, in_decl); 19338 t = copy_node (t); 19339 OMP_STANDALONE_CLAUSES (t) = tmp; 19340 add_stmt (t); 19341 break; 19342 19343 case OACC_CACHE: 19344 case OACC_ENTER_DATA: 19345 case OACC_EXIT_DATA: 19346 case OACC_UPDATE: 19347 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args, 19348 complain, in_decl); 19349 t = copy_node (t); 19350 OMP_STANDALONE_CLAUSES (t) = tmp; 19351 add_stmt (t); 19352 break; 19353 19354 case OMP_ORDERED: 19355 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args, 19356 complain, in_decl); 19357 if (OMP_BODY (t)) 19358 { 19359 stmt = push_stmt_list (); 19360 RECUR (OMP_BODY (t)); 19361 stmt = pop_stmt_list (stmt); 19362 } 19363 else 19364 stmt = NULL_TREE; 19365 19366 t = copy_node (t); 19367 OMP_BODY (t) = stmt; 19368 OMP_ORDERED_CLAUSES (t) = tmp; 19369 add_stmt (t); 19370 break; 19371 19372 case OMP_MASTER: 19373 case OMP_STRUCTURED_BLOCK: 19374 omp_parallel_combined_clauses = NULL; 19375 /* FALLTHRU */ 19376 case OMP_SECTION: 19377 stmt = push_stmt_list (); 19378 RECUR (OMP_BODY (t)); 19379 stmt = pop_stmt_list (stmt); 19380 19381 t = copy_node (t); 19382 OMP_BODY (t) = stmt; 19383 add_stmt (t); 19384 break; 19385 19386 case OMP_ATOMIC: 19387 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t)); 19388 tmp = NULL_TREE; 19389 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE) 19390 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args, 19391 complain, in_decl); 19392 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR) 19393 { 19394 tree op1 = TREE_OPERAND (t, 1); 19395 tree rhs1 = NULL_TREE; 19396 tree r = NULL_TREE; 19397 tree lhs, rhs; 19398 if (TREE_CODE (op1) == COMPOUND_EXPR) 19399 { 19400 rhs1 = RECUR (TREE_OPERAND (op1, 0)); 19401 op1 = TREE_OPERAND (op1, 1); 19402 } 19403 if (TREE_CODE (op1) == COND_EXPR) 19404 { 19405 gcc_assert (rhs1 == NULL_TREE); 19406 tree c = TREE_OPERAND (op1, 0); 19407 if (TREE_CODE (c) == MODIFY_EXPR) 19408 { 19409 r = RECUR (TREE_OPERAND (c, 0)); 19410 c = TREE_OPERAND (c, 1); 19411 } 19412 gcc_assert (TREE_CODE (c) == EQ_EXPR); 19413 rhs = RECUR (TREE_OPERAND (c, 1)); 19414 lhs = RECUR (TREE_OPERAND (op1, 2)); 19415 rhs1 = RECUR (TREE_OPERAND (op1, 1)); 19416 } 19417 else 19418 { 19419 lhs = RECUR (TREE_OPERAND (op1, 0)); 19420 rhs = RECUR (TREE_OPERAND (op1, 1)); 19421 } 19422 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1), 19423 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, r, 19424 tmp, OMP_ATOMIC_MEMORY_ORDER (t), 19425 OMP_ATOMIC_WEAK (t)); 19426 } 19427 else 19428 { 19429 tree op1 = TREE_OPERAND (t, 1); 19430 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE; 19431 tree rhs1 = NULL_TREE, r = NULL_TREE; 19432 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1)); 19433 enum tree_code opcode = NOP_EXPR; 19434 if (code == OMP_ATOMIC_READ) 19435 { 19436 v = RECUR (TREE_OPERAND (op1, 0)); 19437 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0)); 19438 } 19439 else if (code == OMP_ATOMIC_CAPTURE_OLD 19440 || code == OMP_ATOMIC_CAPTURE_NEW) 19441 { 19442 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1); 19443 v = RECUR (TREE_OPERAND (op1, 0)); 19444 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0)); 19445 if (TREE_CODE (op11) == COMPOUND_EXPR) 19446 { 19447 rhs1 = RECUR (TREE_OPERAND (op11, 0)); 19448 op11 = TREE_OPERAND (op11, 1); 19449 } 19450 if (TREE_CODE (op11) == COND_EXPR) 19451 { 19452 gcc_assert (rhs1 == NULL_TREE); 19453 tree c = TREE_OPERAND (op11, 0); 19454 if (TREE_CODE (c) == MODIFY_EXPR) 19455 { 19456 r = RECUR (TREE_OPERAND (c, 0)); 19457 c = TREE_OPERAND (c, 1); 19458 } 19459 gcc_assert (TREE_CODE (c) == EQ_EXPR); 19460 rhs = RECUR (TREE_OPERAND (c, 1)); 19461 lhs = RECUR (TREE_OPERAND (op11, 2)); 19462 rhs1 = RECUR (TREE_OPERAND (op11, 1)); 19463 } 19464 else 19465 { 19466 lhs = RECUR (TREE_OPERAND (op11, 0)); 19467 rhs = RECUR (TREE_OPERAND (op11, 1)); 19468 } 19469 opcode = TREE_CODE (op11); 19470 if (opcode == MODIFY_EXPR) 19471 opcode = NOP_EXPR; 19472 } 19473 else 19474 { 19475 code = OMP_ATOMIC; 19476 lhs = RECUR (TREE_OPERAND (op1, 0)); 19477 rhs = RECUR (TREE_OPERAND (op1, 1)); 19478 } 19479 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v, 19480 lhs1, rhs1, r, tmp, 19481 OMP_ATOMIC_MEMORY_ORDER (t), OMP_ATOMIC_WEAK (t)); 19482 } 19483 break; 19484 19485 case TRANSACTION_EXPR: 19486 { 19487 int flags = 0; 19488 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0); 19489 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0); 19490 19491 if (TRANSACTION_EXPR_IS_STMT (t)) 19492 { 19493 tree body = TRANSACTION_EXPR_BODY (t); 19494 tree noex = NULL_TREE; 19495 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR) 19496 { 19497 noex = MUST_NOT_THROW_COND (body); 19498 if (noex == NULL_TREE) 19499 noex = boolean_true_node; 19500 body = TREE_OPERAND (body, 0); 19501 } 19502 stmt = begin_transaction_stmt (input_location, NULL, flags); 19503 RECUR (body); 19504 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex)); 19505 } 19506 else 19507 { 19508 stmt = build_transaction_expr (EXPR_LOCATION (t), 19509 RECUR (TRANSACTION_EXPR_BODY (t)), 19510 flags, NULL_TREE); 19511 RETURN (stmt); 19512 } 19513 } 19514 break; 19515 19516 case MUST_NOT_THROW_EXPR: 19517 { 19518 tree op0 = RECUR (TREE_OPERAND (t, 0)); 19519 tree cond = RECUR (MUST_NOT_THROW_COND (t)); 19520 RETURN (build_must_not_throw_expr (op0, cond)); 19521 } 19522 19523 case EXPR_PACK_EXPANSION: 19524 error ("invalid use of pack expansion expression"); 19525 RETURN (error_mark_node); 19526 19527 case NONTYPE_ARGUMENT_PACK: 19528 error ("use %<...%> to expand argument pack"); 19529 RETURN (error_mark_node); 19530 19531 case COMPOUND_EXPR: 19532 tmp = RECUR (TREE_OPERAND (t, 0)); 19533 if (tmp == NULL_TREE) 19534 /* If the first operand was a statement, we're done with it. */ 19535 RETURN (RECUR (TREE_OPERAND (t, 1))); 19536 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp, 19537 RECUR (TREE_OPERAND (t, 1)), 19538 templated_operator_saved_lookups (t), 19539 complain)); 19540 19541 case PREDICT_EXPR: 19542 RETURN (add_stmt (copy_node (t))); 19543 19544 case ANNOTATE_EXPR: 19545 { 19546 /* Although ANNOTATE_EXPR is an expression, it can only appear in 19547 WHILE_COND, DO_COND or FOR_COND expressions, which are tsubsted 19548 using tsubst_stmt rather than tsubst_expr and can contain 19549 DECL_EXPRs. */ 19550 tree op1 = RECUR (TREE_OPERAND (t, 0)); 19551 tree op2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl); 19552 tree op3 = tsubst_expr (TREE_OPERAND (t, 2), args, complain, in_decl); 19553 if (TREE_CODE (op2) == INTEGER_CST 19554 && wi::to_widest (op2) == (int) annot_expr_unroll_kind) 19555 op3 = cp_check_pragma_unroll (EXPR_LOCATION (TREE_OPERAND (t, 2)), 19556 op3); 19557 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR, 19558 TREE_TYPE (op1), op1, op2, op3)); 19559 } 19560 19561 default: 19562 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t))); 19563 19564 RETURN (tsubst_expr (t, args, complain, in_decl)); 19565 } 19566 19567 RETURN (NULL_TREE); 19568 out: 19569 input_location = loc; 19570 return r; 19571 #undef RECUR 19572 #undef RETURN 19573 } 19574 19575 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION 19576 function. For description of the body see comment above 19577 cp_parser_omp_declare_reduction_exprs. */ 19578 19579 static void 19580 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl) 19581 { 19582 if (t == NULL_TREE || t == error_mark_node) 19583 return; 19584 19585 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl); 19586 19587 tree_stmt_iterator tsi; 19588 int i; 19589 tree stmts[7]; 19590 memset (stmts, 0, sizeof stmts); 19591 for (i = 0, tsi = tsi_start (t); 19592 i < 7 && !tsi_end_p (tsi); 19593 i++, tsi_next (&tsi)) 19594 stmts[i] = tsi_stmt (tsi); 19595 gcc_assert (tsi_end_p (tsi)); 19596 19597 if (i >= 3) 19598 { 19599 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR 19600 && TREE_CODE (stmts[1]) == DECL_EXPR); 19601 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]), 19602 args, complain, in_decl); 19603 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]), 19604 args, complain, in_decl); 19605 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we 19606 expect to be pushing it. */ 19607 DECL_CONTEXT (omp_out) = current_function_decl; 19608 DECL_CONTEXT (omp_in) = current_function_decl; 19609 keep_next_level (true); 19610 tree block = begin_omp_structured_block (); 19611 tsubst_stmt (stmts[2], args, complain, in_decl); 19612 block = finish_omp_structured_block (block); 19613 block = maybe_cleanup_point_expr_void (block); 19614 add_decl_expr (omp_out); 19615 copy_warning (omp_out, DECL_EXPR_DECL (stmts[0])); 19616 add_decl_expr (omp_in); 19617 finish_expr_stmt (block); 19618 } 19619 if (i >= 6) 19620 { 19621 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR 19622 && TREE_CODE (stmts[4]) == DECL_EXPR); 19623 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]), 19624 args, complain, in_decl); 19625 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]), 19626 args, complain, in_decl); 19627 DECL_CONTEXT (omp_priv) = current_function_decl; 19628 DECL_CONTEXT (omp_orig) = current_function_decl; 19629 keep_next_level (true); 19630 tree block = begin_omp_structured_block (); 19631 tsubst_stmt (stmts[5], args, complain, in_decl); 19632 block = finish_omp_structured_block (block); 19633 block = maybe_cleanup_point_expr_void (block); 19634 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL); 19635 add_decl_expr (omp_priv); 19636 add_decl_expr (omp_orig); 19637 finish_expr_stmt (block); 19638 if (i == 7) 19639 add_decl_expr (omp_orig); 19640 } 19641 } 19642 19643 /* T is a postfix-expression that is not being used in a function 19644 call. Return the substituted version of T. */ 19645 19646 static tree 19647 tsubst_non_call_postfix_expression (tree t, tree args, 19648 tsubst_flags_t complain, 19649 tree in_decl) 19650 { 19651 if (TREE_CODE (t) == SCOPE_REF) 19652 t = tsubst_qualified_id (t, args, complain, in_decl, 19653 /*done=*/false, /*address_p=*/false); 19654 else 19655 t = tsubst_expr (t, args, complain, in_decl); 19656 19657 return t; 19658 } 19659 19660 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the 19661 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously 19662 dependent init-capture. EXPLICIT_P is true if the original list had 19663 explicit captures. */ 19664 19665 static void 19666 prepend_one_capture (tree field, tree init, tree &list, bool explicit_p, 19667 tsubst_flags_t complain) 19668 { 19669 if (tree auto_node = type_uses_auto (TREE_TYPE (field))) 19670 { 19671 tree type = NULL_TREE; 19672 if (!init) 19673 { 19674 if (complain & tf_error) 19675 error ("empty initializer in lambda init-capture"); 19676 init = error_mark_node; 19677 } 19678 else if (TREE_CODE (init) == TREE_LIST) 19679 init = build_x_compound_expr_from_list (init, ELK_INIT, complain); 19680 if (!type) 19681 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain); 19682 TREE_TYPE (field) = type; 19683 cp_apply_type_quals_to_decl (cp_type_quals (type), field); 19684 } 19685 list = tree_cons (field, init, list); 19686 LAMBDA_CAPTURE_EXPLICIT_P (list) = explicit_p; 19687 } 19688 19689 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current 19690 instantiation context. Instantiating a pack expansion containing a lambda 19691 might result in multiple lambdas all based on the same lambda in the 19692 template. */ 19693 19694 tree 19695 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl) 19696 { 19697 tree oldfn = lambda_function (t); 19698 in_decl = oldfn; 19699 19700 args = add_extra_args (LAMBDA_EXPR_EXTRA_ARGS (t), args, complain, in_decl); 19701 if (processing_template_decl 19702 && (!in_template_context || any_dependent_template_arguments_p (args))) 19703 { 19704 /* Defer templated substitution into a lambda-expr if we lost the 19705 necessary template context. This may happen for a lambda-expr 19706 used as a default template argument. 19707 19708 Defer dependent substitution as well so that we don't prematurely 19709 lower the level of a deduced return type or any other auto or 19710 template parameter belonging to the lambda. */ 19711 t = copy_node (t); 19712 LAMBDA_EXPR_EXTRA_ARGS (t) = NULL_TREE; 19713 LAMBDA_EXPR_EXTRA_ARGS (t) = build_extra_args (t, args, complain); 19714 return t; 19715 } 19716 if (LAMBDA_EXPR_EXTRA_ARGS (t)) 19717 { 19718 /* If we deferred substitution into this lambda, then its original 19719 context (e.g. default template argument context) might be unrelated 19720 to the current context it's embedded in. After add_extra_args though, 19721 the innermost levels of 'args' will correspond to the lambda context, 19722 so get rid of all unrelated levels. */ 19723 tree ctx_parms = DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (oldfn)); 19724 if (generic_lambda_fn_p (oldfn)) 19725 ctx_parms = TREE_CHAIN (ctx_parms); 19726 if (TMPL_ARGS_DEPTH (args) > TMPL_PARMS_DEPTH (ctx_parms)) 19727 args = get_innermost_template_args (args, TMPL_PARMS_DEPTH (ctx_parms)); 19728 } 19729 19730 tree r = build_lambda_expr (); 19731 19732 LAMBDA_EXPR_LOCATION (r) 19733 = LAMBDA_EXPR_LOCATION (t); 19734 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r) 19735 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t); 19736 if (tree ti = LAMBDA_EXPR_REGEN_INFO (t)) 19737 LAMBDA_EXPR_REGEN_INFO (r) 19738 = build_template_info (t, add_to_template_args (TI_ARGS (ti), 19739 preserve_args (args))); 19740 else 19741 LAMBDA_EXPR_REGEN_INFO (r) 19742 = build_template_info (t, preserve_args (args)); 19743 19744 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE 19745 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL); 19746 19747 vec<tree,va_gc>* field_packs = NULL; 19748 unsigned name_independent_cnt = 0; 19749 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap; 19750 cap = TREE_CHAIN (cap)) 19751 { 19752 tree ofield = TREE_PURPOSE (cap); 19753 tree init = TREE_VALUE (cap); 19754 if (PACK_EXPANSION_P (init)) 19755 init = tsubst_pack_expansion (init, args, complain, in_decl); 19756 else 19757 init = tsubst_expr (init, args, complain, in_decl); 19758 19759 if (init == error_mark_node) 19760 return error_mark_node; 19761 19762 if (init && TREE_CODE (init) == TREE_LIST) 19763 init = build_x_compound_expr_from_list (init, ELK_INIT, complain); 19764 19765 if (!processing_template_decl 19766 && init && TREE_CODE (init) != TREE_VEC 19767 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE)) 19768 { 19769 /* For a VLA, simply tsubsting the field type won't work, we need to 19770 go through add_capture again. XXX do we want to do this for all 19771 captures? */ 19772 tree name = (get_identifier 19773 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2)); 19774 tree ftype = TREE_TYPE (ofield); 19775 bool by_ref = (TYPE_REF_P (ftype) 19776 || (TREE_CODE (ftype) == DECLTYPE_TYPE 19777 && DECLTYPE_FOR_REF_CAPTURE (ftype))); 19778 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield), 19779 &name_independent_cnt); 19780 continue; 19781 } 19782 19783 if (PACK_EXPANSION_P (ofield)) 19784 ofield = PACK_EXPANSION_PATTERN (ofield); 19785 tree field = tsubst_decl (ofield, args, complain); 19786 19787 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield)) 19788 { 19789 /* Remember these for when we've pushed local_specializations. */ 19790 vec_safe_push (field_packs, ofield); 19791 vec_safe_push (field_packs, field); 19792 } 19793 19794 if (field == error_mark_node) 19795 return error_mark_node; 19796 19797 if (TREE_CODE (field) == TREE_VEC) 19798 { 19799 int len = TREE_VEC_LENGTH (field); 19800 gcc_assert (TREE_CODE (init) == TREE_VEC 19801 && TREE_VEC_LENGTH (init) == len); 19802 for (int i = 0; i < len; ++i) 19803 prepend_one_capture (TREE_VEC_ELT (field, i), 19804 TREE_VEC_ELT (init, i), 19805 LAMBDA_EXPR_CAPTURE_LIST (r), 19806 LAMBDA_CAPTURE_EXPLICIT_P (cap), 19807 complain); 19808 } 19809 else 19810 { 19811 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r), 19812 LAMBDA_CAPTURE_EXPLICIT_P (cap), complain); 19813 19814 if (id_equal (DECL_NAME (field), "__this")) 19815 LAMBDA_EXPR_THIS_CAPTURE (r) = field; 19816 } 19817 } 19818 19819 tree type = begin_lambda_type (r); 19820 if (type == error_mark_node) 19821 { 19822 gcc_checking_assert (!(complain & tf_error) || seen_error ()); 19823 return error_mark_node; 19824 } 19825 19826 if (LAMBDA_EXPR_EXTRA_SCOPE (t)) 19827 record_lambda_scope (r); 19828 else if (TYPE_NAMESPACE_SCOPE_P (TREE_TYPE (t))) 19829 /* If we're pushed into another scope (PR105652), fix it. */ 19830 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_NAME (type)) 19831 = TYPE_CONTEXT (TREE_TYPE (t)); 19832 record_lambda_scope_discriminator (r); 19833 19834 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */ 19835 determine_visibility (TYPE_NAME (type)); 19836 19837 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r)); 19838 19839 tree oldtmpl = (generic_lambda_fn_p (oldfn) 19840 ? DECL_TI_TEMPLATE (oldfn) 19841 : NULL_TREE); 19842 19843 tree tparms = NULL_TREE; 19844 if (oldtmpl) 19845 tparms = tsubst_template_parms (DECL_TEMPLATE_PARMS (oldtmpl), args, complain); 19846 19847 tree fntype = static_fn_type (oldfn); 19848 19849 tree saved_ctp = current_template_parms; 19850 if (oldtmpl) 19851 { 19852 ++processing_template_decl; 19853 current_template_parms = tparms; 19854 } 19855 fntype = tsubst (fntype, args, complain, in_decl); 19856 if (oldtmpl) 19857 { 19858 current_template_parms = saved_ctp; 19859 --processing_template_decl; 19860 } 19861 19862 if (fntype == error_mark_node) 19863 r = error_mark_node; 19864 else 19865 { 19866 /* The body of a lambda-expression is not a subexpression of the 19867 enclosing expression. Parms are to have DECL_CHAIN tsubsted, 19868 which would be skipped if cp_unevaluated_operand. */ 19869 cp_evaluated ev; 19870 19871 /* Fix the type of 'this'. 19872 For static and xobj member functions we use this to transport the 19873 lambda's closure type. It appears that in the regular case the 19874 object parameter is still pulled off, and then re-added again anyway. 19875 So perhaps we could do something better here? */ 19876 fntype = build_memfn_type (fntype, type, 19877 type_memfn_quals (fntype), 19878 type_memfn_rqual (fntype)); 19879 tree inst = (oldtmpl 19880 ? tsubst_template_decl (oldtmpl, args, complain, 19881 fntype, tparms) 19882 : tsubst_function_decl (oldfn, args, complain, fntype)); 19883 if (inst == error_mark_node) 19884 { 19885 r = error_mark_node; 19886 goto out; 19887 } 19888 finish_member_declaration (inst); 19889 record_lambda_scope_sig_discriminator (r, inst); 19890 19891 tree fn = oldtmpl ? DECL_TEMPLATE_RESULT (inst) : inst; 19892 19893 /* Let finish_function set this. */ 19894 DECL_DECLARED_CONSTEXPR_P (fn) = false; 19895 19896 bool nested = cfun; 19897 if (nested) 19898 push_function_context (); 19899 else 19900 /* Still increment function_depth so that we don't GC in the 19901 middle of an expression. */ 19902 ++function_depth; 19903 19904 local_specialization_stack s (lss_copy); 19905 19906 bool save_in_consteval_if_p = in_consteval_if_p; 19907 in_consteval_if_p = false; 19908 19909 tree body = start_lambda_function (fn, r); 19910 19911 /* Now record them for lookup_init_capture_pack. */ 19912 int fplen = vec_safe_length (field_packs); 19913 for (int i = 0; i < fplen; ) 19914 { 19915 tree pack = (*field_packs)[i++]; 19916 tree inst = (*field_packs)[i++]; 19917 register_local_specialization (inst, pack); 19918 } 19919 release_tree_vector (field_packs); 19920 19921 register_parameter_specializations (oldfn, fn); 19922 19923 if (oldtmpl) 19924 { 19925 /* We might not partially instantiate some parts of the function, so 19926 copy these flags from the original template. */ 19927 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language; 19928 current_function_returns_value = ol->returns_value; 19929 current_function_returns_null = ol->returns_null; 19930 current_function_returns_abnormally = ol->returns_abnormally; 19931 current_function_infinite_loop = ol->infinite_loop; 19932 } 19933 19934 /* [temp.deduct] A lambda-expression appearing in a function type or a 19935 template parameter is not considered part of the immediate context for 19936 the purposes of template argument deduction. */ 19937 complain = tf_warning_or_error; 19938 19939 tree saved = DECL_SAVED_TREE (oldfn); 19940 if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved)) 19941 /* We already have a body block from start_lambda_function, we don't 19942 need another to confuse NRV (91217). */ 19943 saved = BIND_EXPR_BODY (saved); 19944 19945 tsubst_stmt (saved, args, complain, r); 19946 19947 finish_lambda_function (body); 19948 19949 in_consteval_if_p = save_in_consteval_if_p; 19950 19951 if (nested) 19952 pop_function_context (); 19953 else 19954 --function_depth; 19955 19956 /* The capture list was built up in reverse order; fix that now. */ 19957 LAMBDA_EXPR_CAPTURE_LIST (r) 19958 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r)); 19959 19960 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE; 19961 19962 maybe_add_lambda_conv_op (type); 19963 } 19964 19965 out: 19966 finish_struct (type, /*attr*/NULL_TREE); 19967 19968 insert_pending_capture_proxies (); 19969 19970 return r; 19971 } 19972 19973 /* Subroutine of maybe_fold_fn_template_args. */ 19974 19975 static bool 19976 fold_targs_r (tree targs, tsubst_flags_t complain) 19977 { 19978 int len = TREE_VEC_LENGTH (targs); 19979 for (int i = 0; i < len; ++i) 19980 { 19981 tree &elt = TREE_VEC_ELT (targs, i); 19982 if (!elt || TYPE_P (elt) 19983 || TREE_CODE (elt) == TEMPLATE_DECL) 19984 continue; 19985 if (TREE_CODE (elt) == NONTYPE_ARGUMENT_PACK) 19986 { 19987 if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt), complain)) 19988 return false; 19989 } 19990 else if (/* We can only safely preevaluate scalar prvalues. */ 19991 SCALAR_TYPE_P (TREE_TYPE (elt)) 19992 && !glvalue_p (elt) 19993 && !TREE_CONSTANT (elt)) 19994 { 19995 elt = cxx_constant_value (elt, complain); 19996 if (elt == error_mark_node) 19997 return false; 19998 } 19999 } 20000 20001 return true; 20002 } 20003 20004 /* Try to do constant evaluation of any explicit template arguments in FN 20005 before overload resolution, to get any errors only once. Return true iff 20006 we didn't have any problems folding. */ 20007 20008 static bool 20009 maybe_fold_fn_template_args (tree fn, tsubst_flags_t complain) 20010 { 20011 if (processing_template_decl || fn == NULL_TREE) 20012 return true; 20013 if (fn == error_mark_node) 20014 return false; 20015 if (TREE_CODE (fn) == OFFSET_REF 20016 || TREE_CODE (fn) == COMPONENT_REF) 20017 fn = TREE_OPERAND (fn, 1); 20018 if (BASELINK_P (fn)) 20019 fn = BASELINK_FUNCTIONS (fn); 20020 if (TREE_CODE (fn) != TEMPLATE_ID_EXPR) 20021 return true; 20022 tree targs = TREE_OPERAND (fn, 1); 20023 if (targs == NULL_TREE) 20024 return true; 20025 if (targs == error_mark_node) 20026 return false; 20027 return fold_targs_r (targs, complain); 20028 } 20029 20030 /* Helper function for tsubst_expr CALL_EXPR and ARRAY_REF handling. */ 20031 20032 static void 20033 tsubst_call_args (tree t, tree args, tsubst_flags_t complain, 20034 tree in_decl, releasing_vec &call_args) 20035 { 20036 unsigned int nargs = call_expr_nargs (t); 20037 for (unsigned int i = 0; i < nargs; ++i) 20038 { 20039 tree arg = CALL_EXPR_ARG (t, i); 20040 20041 if (!PACK_EXPANSION_P (arg)) 20042 vec_safe_push (call_args, tsubst_expr (arg, args, complain, in_decl)); 20043 else 20044 { 20045 /* Expand the pack expansion and push each entry onto CALL_ARGS. */ 20046 arg = tsubst_pack_expansion (arg, args, complain, in_decl); 20047 if (TREE_CODE (arg) == TREE_VEC) 20048 { 20049 unsigned int len, j; 20050 20051 len = TREE_VEC_LENGTH (arg); 20052 for (j = 0; j < len; ++j) 20053 { 20054 tree value = TREE_VEC_ELT (arg, j); 20055 if (value != NULL_TREE) 20056 value = convert_from_reference (value); 20057 vec_safe_push (call_args, value); 20058 } 20059 } 20060 else 20061 /* A partial substitution. Add one entry. */ 20062 vec_safe_push (call_args, arg); 20063 } 20064 } 20065 } 20066 20067 /* Like tsubst but deals with expressions and performs semantic 20068 analysis. */ 20069 20070 tree 20071 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl) 20072 { 20073 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0) 20074 #define RECUR(NODE) \ 20075 tsubst_expr (NODE, args, complain, in_decl) 20076 20077 tree retval, op1; 20078 location_t save_loc; 20079 20080 if (t == NULL_TREE || t == error_mark_node) 20081 return t; 20082 20083 save_loc = input_location; 20084 if (location_t eloc = cp_expr_location (t)) 20085 input_location = eloc; 20086 20087 /* N3276 decltype magic only applies to calls at the top level or on the 20088 right side of a comma. */ 20089 tsubst_flags_t decltype_flag = (complain & tf_decltype); 20090 complain &= ~tf_decltype; 20091 20092 /* This flag only applies to id-expressions at the top level, and 20093 controls resolution thereof. */ 20094 tsubst_flags_t no_name_lookup_flag = (complain & tf_no_name_lookup); 20095 complain &= ~tf_no_name_lookup; 20096 20097 if (!no_name_lookup_flag) 20098 if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl)) 20099 return d; 20100 20101 switch (TREE_CODE (t)) 20102 { 20103 case USING_DECL: 20104 t = DECL_NAME (t); 20105 /* Fall through. */ 20106 case IDENTIFIER_NODE: 20107 { 20108 tree decl; 20109 cp_id_kind idk; 20110 const char *error_msg; 20111 20112 if (IDENTIFIER_CONV_OP_P (t)) 20113 { 20114 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); 20115 t = make_conv_op_name (new_type); 20116 } 20117 20118 if (no_name_lookup_flag) 20119 RETURN (t); 20120 20121 /* Look up the name. */ 20122 decl = lookup_name (t); 20123 20124 /* By convention, expressions use ERROR_MARK_NODE to indicate 20125 failure, not NULL_TREE. */ 20126 if (decl == NULL_TREE) 20127 decl = error_mark_node; 20128 20129 decl = finish_id_expression (t, decl, NULL_TREE, 20130 &idk, 20131 /*i_c_e_p=*/false, 20132 /*allow_i_c_e_p=*/true, 20133 /*non_i_c_e_p=*/nullptr, 20134 /*template_p=*/false, 20135 /*done=*/true, 20136 /*address_p=*/false, 20137 /*template_arg_p=*/false, 20138 &error_msg, 20139 input_location); 20140 if (error_msg) 20141 error (error_msg); 20142 if (identifier_p (decl)) 20143 { 20144 if (complain & tf_error) 20145 unqualified_name_lookup_error (decl); 20146 decl = error_mark_node; 20147 } 20148 RETURN (decl); 20149 } 20150 20151 case TEMPLATE_ID_EXPR: 20152 { 20153 tree object; 20154 tree templ = TREE_OPERAND (t, 0); 20155 tree targs = TREE_OPERAND (t, 1); 20156 20157 if (no_name_lookup_flag) 20158 templ = tsubst_name (templ, args, complain, in_decl); 20159 else 20160 templ = tsubst_expr (templ, args, complain, in_decl); 20161 20162 if (targs) 20163 targs = tsubst_template_args (targs, args, complain, in_decl); 20164 if (targs == error_mark_node) 20165 RETURN (error_mark_node); 20166 20167 if (TREE_CODE (templ) == SCOPE_REF) 20168 { 20169 tree name = TREE_OPERAND (templ, 1); 20170 tree tid = lookup_template_function (name, targs); 20171 TREE_OPERAND (templ, 1) = tid; 20172 RETURN (templ); 20173 } 20174 20175 if (concept_definition_p (templ)) 20176 { 20177 tree check = build_concept_check (templ, targs, complain); 20178 if (check == error_mark_node) 20179 RETURN (error_mark_node); 20180 20181 tree id = unpack_concept_check (check); 20182 20183 /* If we built a function concept check, return the underlying 20184 template-id. So we can evaluate it as a function call. */ 20185 if (function_concept_p (TREE_OPERAND (id, 0))) 20186 RETURN (id); 20187 20188 RETURN (check); 20189 } 20190 20191 if (variable_template_p (templ)) 20192 { 20193 if (no_name_lookup_flag) 20194 RETURN (lookup_template_variable (templ, targs, complain)); 20195 20196 tree r = lookup_and_finish_template_variable (templ, targs, 20197 complain); 20198 r = convert_from_reference (r); 20199 r = maybe_wrap_with_location (r, EXPR_LOCATION (t)); 20200 RETURN (r); 20201 } 20202 20203 if (TREE_CODE (templ) == COMPONENT_REF) 20204 { 20205 object = TREE_OPERAND (templ, 0); 20206 templ = TREE_OPERAND (templ, 1); 20207 } 20208 else 20209 object = NULL_TREE; 20210 20211 tree tid = lookup_template_function (templ, targs); 20212 protected_set_expr_location (tid, EXPR_LOCATION (t)); 20213 20214 if (object) 20215 RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid), 20216 object, tid, NULL_TREE)); 20217 else if (no_name_lookup_flag) 20218 RETURN (tid); 20219 else if (identifier_p (templ)) 20220 { 20221 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when 20222 name lookup found nothing when parsing the template name. */ 20223 gcc_assert (cxx_dialect >= cxx20 || seen_error ()); 20224 RETURN (tid); 20225 } 20226 else 20227 RETURN (baselink_for_fns (tid)); 20228 } 20229 20230 case INDIRECT_REF: 20231 { 20232 tree r = RECUR (TREE_OPERAND (t, 0)); 20233 20234 if (REFERENCE_REF_P (t)) 20235 { 20236 /* A type conversion to reference type will be enclosed in 20237 such an indirect ref, but the substitution of the cast 20238 will have also added such an indirect ref. */ 20239 r = convert_from_reference (r); 20240 } 20241 else 20242 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR, 20243 templated_operator_saved_lookups (t), 20244 complain|decltype_flag); 20245 20246 if (REF_PARENTHESIZED_P (t)) 20247 r = force_paren_expr (r); 20248 20249 RETURN (r); 20250 } 20251 20252 case MEM_REF: 20253 { 20254 tree op0 = RECUR (TREE_OPERAND (t, 0)); 20255 tree op1 = RECUR (TREE_OPERAND (t, 0)); 20256 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); 20257 RETURN (build2_loc (EXPR_LOCATION (t), MEM_REF, new_type, op0, op1)); 20258 } 20259 20260 case NOP_EXPR: 20261 { 20262 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); 20263 tree op0 = RECUR (TREE_OPERAND (t, 0)); 20264 RETURN (build_nop (type, op0)); 20265 } 20266 20267 case IMPLICIT_CONV_EXPR: 20268 { 20269 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); 20270 if (type == error_mark_node) 20271 RETURN (error_mark_node); 20272 tree expr = RECUR (TREE_OPERAND (t, 0)); 20273 if (dependent_type_p (type) || type_dependent_expression_p (expr)) 20274 { 20275 retval = copy_node (t); 20276 TREE_TYPE (retval) = type; 20277 TREE_OPERAND (retval, 0) = expr; 20278 RETURN (retval); 20279 } 20280 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t)) 20281 { 20282 tree r = convert_nontype_argument (type, expr, complain); 20283 if (r == NULL_TREE) 20284 r = error_mark_node; 20285 RETURN (r); 20286 } 20287 int flags = LOOKUP_IMPLICIT; 20288 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t)) 20289 flags = LOOKUP_NORMAL; 20290 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t)) 20291 flags |= LOOKUP_NO_NARROWING; 20292 RETURN (perform_implicit_conversion_flags (type, expr, complain, 20293 flags)); 20294 } 20295 20296 case CONVERT_EXPR: 20297 { 20298 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); 20299 tree op0 = RECUR (TREE_OPERAND (t, 0)); 20300 if (op0 == error_mark_node) 20301 RETURN (error_mark_node); 20302 RETURN (build1 (CONVERT_EXPR, type, op0)); 20303 } 20304 20305 case CAST_EXPR: 20306 case REINTERPRET_CAST_EXPR: 20307 case CONST_CAST_EXPR: 20308 case DYNAMIC_CAST_EXPR: 20309 case STATIC_CAST_EXPR: 20310 { 20311 tree type; 20312 tree op, r = NULL_TREE; 20313 20314 tsubst_flags_t tcomplain = complain; 20315 if (TREE_CODE (t) == CAST_EXPR) 20316 tcomplain |= tf_tst_ok; 20317 type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl); 20318 20319 op = RECUR (TREE_OPERAND (t, 0)); 20320 20321 warning_sentinel s(warn_useless_cast); 20322 warning_sentinel s2(warn_ignored_qualifiers); 20323 warning_sentinel s3(warn_int_in_bool_context); 20324 switch (TREE_CODE (t)) 20325 { 20326 case CAST_EXPR: 20327 r = build_functional_cast (input_location, type, op, complain); 20328 break; 20329 case REINTERPRET_CAST_EXPR: 20330 r = build_reinterpret_cast (input_location, type, op, complain); 20331 break; 20332 case CONST_CAST_EXPR: 20333 r = build_const_cast (input_location, type, op, complain); 20334 break; 20335 case DYNAMIC_CAST_EXPR: 20336 r = build_dynamic_cast (input_location, type, op, complain); 20337 break; 20338 case STATIC_CAST_EXPR: 20339 r = build_static_cast (input_location, type, op, complain); 20340 if (IMPLICIT_RVALUE_P (t)) 20341 set_implicit_rvalue_p (r); 20342 break; 20343 default: 20344 gcc_unreachable (); 20345 } 20346 20347 RETURN (r); 20348 } 20349 20350 case BIT_CAST_EXPR: 20351 { 20352 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); 20353 tree op0 = RECUR (TREE_OPERAND (t, 0)); 20354 RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain)); 20355 } 20356 20357 case POSTDECREMENT_EXPR: 20358 case POSTINCREMENT_EXPR: 20359 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), 20360 args, complain, in_decl); 20361 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1, 20362 templated_operator_saved_lookups (t), 20363 complain|decltype_flag)); 20364 20365 case BIT_NOT_EXPR: 20366 if (identifier_p (TREE_OPERAND (t, 0))) 20367 { 20368 gcc_checking_assert (no_name_lookup_flag); 20369 RETURN (t); 20370 } 20371 else if (TYPE_P (TREE_OPERAND (t, 0))) 20372 { 20373 gcc_checking_assert (no_name_lookup_flag); 20374 tree op0 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); 20375 RETURN (build_min_nt_loc (EXPR_LOCATION (t), BIT_NOT_EXPR, op0)); 20376 } 20377 /* Fall through. */ 20378 case PREDECREMENT_EXPR: 20379 case PREINCREMENT_EXPR: 20380 case NEGATE_EXPR: 20381 case ABS_EXPR: 20382 case TRUTH_NOT_EXPR: 20383 case UNARY_PLUS_EXPR: /* Unary + */ 20384 case REALPART_EXPR: 20385 case IMAGPART_EXPR: 20386 RETURN (build_x_unary_op (input_location, TREE_CODE (t), 20387 RECUR (TREE_OPERAND (t, 0)), 20388 templated_operator_saved_lookups (t), 20389 complain|decltype_flag)); 20390 20391 case EXCESS_PRECISION_EXPR: 20392 { 20393 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); 20394 tree op0 = RECUR (TREE_OPERAND (t, 0)); 20395 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR) 20396 RETURN (op0); 20397 RETURN (build1_loc (EXPR_LOCATION (t), EXCESS_PRECISION_EXPR, 20398 type, op0)); 20399 } 20400 20401 case FIX_TRUNC_EXPR: 20402 /* convert_like should have created an IMPLICIT_CONV_EXPR. */ 20403 gcc_unreachable (); 20404 20405 case ADDR_EXPR: 20406 op1 = TREE_OPERAND (t, 0); 20407 if (TREE_CODE (op1) == LABEL_DECL) 20408 RETURN (finish_label_address_expr (DECL_NAME (op1), 20409 EXPR_LOCATION (op1))); 20410 if (TREE_CODE (op1) == SCOPE_REF) 20411 op1 = tsubst_qualified_id (op1, args, complain, in_decl, 20412 /*done=*/true, /*address_p=*/true); 20413 else 20414 op1 = tsubst_non_call_postfix_expression (op1, args, complain, 20415 in_decl); 20416 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1, 20417 templated_operator_saved_lookups (t), 20418 complain|decltype_flag)); 20419 20420 case PLUS_EXPR: 20421 case MINUS_EXPR: 20422 case MULT_EXPR: 20423 case TRUNC_DIV_EXPR: 20424 case CEIL_DIV_EXPR: 20425 case FLOOR_DIV_EXPR: 20426 case ROUND_DIV_EXPR: 20427 case EXACT_DIV_EXPR: 20428 case BIT_AND_EXPR: 20429 case BIT_IOR_EXPR: 20430 case BIT_XOR_EXPR: 20431 case TRUNC_MOD_EXPR: 20432 case FLOOR_MOD_EXPR: 20433 case TRUTH_ANDIF_EXPR: 20434 case TRUTH_ORIF_EXPR: 20435 case TRUTH_AND_EXPR: 20436 case TRUTH_OR_EXPR: 20437 case RSHIFT_EXPR: 20438 case LSHIFT_EXPR: 20439 case EQ_EXPR: 20440 case NE_EXPR: 20441 case MAX_EXPR: 20442 case MIN_EXPR: 20443 case LE_EXPR: 20444 case GE_EXPR: 20445 case LT_EXPR: 20446 case GT_EXPR: 20447 case SPACESHIP_EXPR: 20448 case MEMBER_REF: 20449 case DOTSTAR_EXPR: 20450 { 20451 /* If either OP0 or OP1 was value- or type-dependent, suppress 20452 warnings that depend on the range of the types involved. */ 20453 tree op0 = TREE_OPERAND (t, 0); 20454 tree op1 = TREE_OPERAND (t, 1); 20455 const bool was_dep = (dependent_operand_p (op0) 20456 || dependent_operand_p (op1)); 20457 op0 = RECUR (op0); 20458 op1 = RECUR (op1); 20459 20460 warning_sentinel s1(warn_type_limits, was_dep); 20461 warning_sentinel s2(warn_div_by_zero, was_dep); 20462 warning_sentinel s3(warn_logical_op, was_dep); 20463 warning_sentinel s4(warn_tautological_compare, was_dep); 20464 warning_sentinel s5(warn_address, was_dep); 20465 20466 tree r = build_x_binary_op 20467 (input_location, TREE_CODE (t), 20468 op0, 20469 (warning_suppressed_p (TREE_OPERAND (t, 0)) 20470 ? ERROR_MARK 20471 : TREE_CODE (TREE_OPERAND (t, 0))), 20472 op1, 20473 (warning_suppressed_p (TREE_OPERAND (t, 1)) 20474 ? ERROR_MARK 20475 : TREE_CODE (TREE_OPERAND (t, 1))), 20476 templated_operator_saved_lookups (t), 20477 /*overload=*/NULL, 20478 complain|decltype_flag); 20479 if (EXPR_P (r)) 20480 copy_warning (r, t); 20481 20482 RETURN (r); 20483 } 20484 20485 case POINTER_PLUS_EXPR: 20486 { 20487 tree op0 = RECUR (TREE_OPERAND (t, 0)); 20488 if (op0 == error_mark_node) 20489 RETURN (error_mark_node); 20490 tree op1 = RECUR (TREE_OPERAND (t, 1)); 20491 if (op1 == error_mark_node) 20492 RETURN (error_mark_node); 20493 RETURN (fold_build_pointer_plus (op0, op1)); 20494 } 20495 20496 case SCOPE_REF: 20497 if (no_name_lookup_flag) 20498 { 20499 tree op0 = tsubst_scope (TREE_OPERAND (t, 0), args, complain, in_decl); 20500 tree op1 = tsubst_name (TREE_OPERAND (t, 1), args, complain, in_decl); 20501 RETURN (build_qualified_name (/*type=*/NULL_TREE, op0, op1, 20502 QUALIFIED_NAME_IS_TEMPLATE (t))); 20503 } 20504 else 20505 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true, 20506 /*address_p=*/false)); 20507 20508 case BASELINK: 20509 RETURN (tsubst_baselink (t, current_nonlambda_class_type (), 20510 args, complain, in_decl)); 20511 20512 case ARRAY_REF: 20513 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), 20514 args, complain, in_decl); 20515 if (TREE_CODE (TREE_OPERAND (t, 1)) == CALL_EXPR 20516 && (CALL_EXPR_FN (TREE_OPERAND (t, 1)) 20517 == ovl_op_identifier (ARRAY_REF))) 20518 { 20519 tree c = TREE_OPERAND (t, 1); 20520 releasing_vec index_exp_list; 20521 tsubst_call_args (c, args, complain, in_decl, index_exp_list); 20522 20523 tree r; 20524 if (vec_safe_length (index_exp_list) == 1 20525 && !PACK_EXPANSION_P (index_exp_list[0])) 20526 r = grok_array_decl (EXPR_LOCATION (t), op1, 20527 index_exp_list[0], NULL, 20528 complain | decltype_flag); 20529 else 20530 r = grok_array_decl (EXPR_LOCATION (t), op1, 20531 NULL_TREE, &index_exp_list, 20532 complain | decltype_flag); 20533 RETURN (r); 20534 } 20535 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1, 20536 RECUR (TREE_OPERAND (t, 1)), 20537 complain|decltype_flag)); 20538 20539 case OMP_ARRAY_SECTION: 20540 { 20541 tree op0 = RECUR (TREE_OPERAND (t, 0)); 20542 tree op1 = NULL_TREE, op2 = NULL_TREE; 20543 if (op0 == error_mark_node) 20544 RETURN (error_mark_node); 20545 if (TREE_OPERAND (t, 1)) 20546 { 20547 op1 = RECUR (TREE_OPERAND (t, 1)); 20548 if (op1 == error_mark_node) 20549 RETURN (error_mark_node); 20550 } 20551 if (TREE_OPERAND (t, 2)) 20552 { 20553 op2 = RECUR (TREE_OPERAND (t, 2)); 20554 if (op2 == error_mark_node) 20555 RETURN (error_mark_node); 20556 } 20557 RETURN (build_omp_array_section (EXPR_LOCATION (t), op0, op1, op2)); 20558 } 20559 20560 case SIZEOF_EXPR: 20561 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)) 20562 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0))) 20563 { 20564 tree expanded, op = TREE_OPERAND (t, 0); 20565 int len = 0; 20566 20567 if (SIZEOF_EXPR_TYPE_P (t)) 20568 op = TREE_TYPE (op); 20569 20570 ++cp_unevaluated_operand; 20571 ++c_inhibit_evaluation_warnings; 20572 /* We only want to compute the number of arguments. */ 20573 if (PACK_EXPANSION_P (op)) 20574 expanded = tsubst_pack_expansion (op, args, complain, in_decl); 20575 else 20576 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op), 20577 args, complain, in_decl); 20578 --cp_unevaluated_operand; 20579 --c_inhibit_evaluation_warnings; 20580 20581 if (TREE_CODE (expanded) == TREE_VEC) 20582 { 20583 len = TREE_VEC_LENGTH (expanded); 20584 /* Set TREE_USED for the benefit of -Wunused. */ 20585 for (int i = 0; i < len; i++) 20586 if (DECL_P (TREE_VEC_ELT (expanded, i))) 20587 TREE_USED (TREE_VEC_ELT (expanded, i)) = true; 20588 } 20589 20590 if (expanded == error_mark_node) 20591 RETURN (error_mark_node); 20592 else if (PACK_EXPANSION_P (expanded) 20593 || (TREE_CODE (expanded) == TREE_VEC 20594 && pack_expansion_args_count (expanded))) 20595 20596 { 20597 if (PACK_EXPANSION_P (expanded)) 20598 /* OK. */; 20599 else 20600 expanded = make_argument_pack (expanded); 20601 20602 if (TYPE_P (expanded)) 20603 RETURN (cxx_sizeof_or_alignof_type (input_location, 20604 expanded, SIZEOF_EXPR, 20605 false, 20606 complain & tf_error)); 20607 else 20608 RETURN (cxx_sizeof_or_alignof_expr (input_location, 20609 expanded, SIZEOF_EXPR, 20610 false, 20611 complain & tf_error)); 20612 } 20613 else 20614 RETURN (build_int_cst (size_type_node, len)); 20615 } 20616 /* Fall through */ 20617 20618 case ALIGNOF_EXPR: 20619 { 20620 tree r; 20621 20622 op1 = TREE_OPERAND (t, 0); 20623 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t)) 20624 op1 = TREE_TYPE (op1); 20625 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR 20626 && ALIGNOF_EXPR_STD_P (t)); 20627 if (!args) 20628 { 20629 /* When there are no ARGS, we are trying to evaluate a 20630 non-dependent expression from the parser. Trying to do 20631 the substitutions may not work. */ 20632 if (!TYPE_P (op1)) 20633 op1 = TREE_TYPE (op1); 20634 } 20635 else 20636 { 20637 ++cp_unevaluated_operand; 20638 ++c_inhibit_evaluation_warnings; 20639 if (TYPE_P (op1)) 20640 op1 = tsubst (op1, args, complain, in_decl); 20641 else 20642 op1 = tsubst_expr (op1, args, complain, in_decl); 20643 --cp_unevaluated_operand; 20644 --c_inhibit_evaluation_warnings; 20645 } 20646 if (TYPE_P (op1)) 20647 r = cxx_sizeof_or_alignof_type (input_location, 20648 op1, TREE_CODE (t), std_alignof, 20649 complain & tf_error); 20650 else 20651 r = cxx_sizeof_or_alignof_expr (input_location, 20652 op1, TREE_CODE (t), std_alignof, 20653 complain & tf_error); 20654 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node) 20655 { 20656 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1)) 20657 { 20658 if (!processing_template_decl && TYPE_P (op1)) 20659 { 20660 r = build_min (SIZEOF_EXPR, size_type_node, 20661 build1 (NOP_EXPR, op1, error_mark_node)); 20662 SIZEOF_EXPR_TYPE_P (r) = 1; 20663 } 20664 else 20665 r = build_min (SIZEOF_EXPR, size_type_node, op1); 20666 TREE_SIDE_EFFECTS (r) = 0; 20667 TREE_READONLY (r) = 1; 20668 } 20669 SET_EXPR_LOCATION (r, EXPR_LOCATION (t)); 20670 copy_warning (r, t); 20671 } 20672 RETURN (r); 20673 } 20674 20675 case AT_ENCODE_EXPR: 20676 { 20677 op1 = TREE_OPERAND (t, 0); 20678 ++cp_unevaluated_operand; 20679 ++c_inhibit_evaluation_warnings; 20680 op1 = tsubst (op1, args, complain, in_decl); 20681 --cp_unevaluated_operand; 20682 --c_inhibit_evaluation_warnings; 20683 RETURN (objc_build_encode_expr (op1)); 20684 } 20685 20686 case NOEXCEPT_EXPR: 20687 op1 = TREE_OPERAND (t, 0); 20688 ++cp_unevaluated_operand; 20689 ++c_inhibit_evaluation_warnings; 20690 ++cp_noexcept_operand; 20691 op1 = tsubst_expr (op1, args, complain, in_decl); 20692 --cp_unevaluated_operand; 20693 --c_inhibit_evaluation_warnings; 20694 --cp_noexcept_operand; 20695 RETURN (finish_noexcept_expr (op1, complain)); 20696 20697 case MODOP_EXPR: 20698 { 20699 warning_sentinel s(warn_div_by_zero); 20700 tree lhs = RECUR (TREE_OPERAND (t, 0)); 20701 tree rhs = RECUR (TREE_OPERAND (t, 2)); 20702 20703 tree r = build_x_modify_expr 20704 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs, 20705 templated_operator_saved_lookups (t), 20706 complain|decltype_flag); 20707 /* TREE_NO_WARNING must be set if either the expression was 20708 parenthesized or it uses an operator such as >>= rather 20709 than plain assignment. In the former case, it was already 20710 set and must be copied. In the latter case, 20711 build_x_modify_expr sets it and it must not be reset 20712 here. */ 20713 if (warning_suppressed_p (t, OPT_Wparentheses)) 20714 suppress_warning (STRIP_REFERENCE_REF (r), OPT_Wparentheses); 20715 20716 RETURN (r); 20717 } 20718 20719 case ARROW_EXPR: 20720 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), 20721 args, complain, in_decl); 20722 /* Remember that there was a reference to this entity. */ 20723 if (DECL_P (op1) 20724 && !mark_used (op1, complain) && !(complain & tf_error)) 20725 RETURN (error_mark_node); 20726 RETURN (build_x_arrow (input_location, op1, complain)); 20727 20728 case NEW_EXPR: 20729 { 20730 tree placement = RECUR (TREE_OPERAND (t, 0)); 20731 tree init = RECUR (TREE_OPERAND (t, 3)); 20732 vec<tree, va_gc> *placement_vec; 20733 vec<tree, va_gc> *init_vec; 20734 tree ret; 20735 location_t loc = EXPR_LOCATION (t); 20736 20737 if (placement == NULL_TREE) 20738 placement_vec = NULL; 20739 else if (placement == error_mark_node) 20740 RETURN (error_mark_node); 20741 else 20742 { 20743 placement_vec = make_tree_vector (); 20744 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement)) 20745 vec_safe_push (placement_vec, TREE_VALUE (placement)); 20746 } 20747 20748 /* If there was an initializer in the original tree, but it 20749 instantiated to an empty list, then we should pass a 20750 non-NULL empty vector to tell build_new that it was an 20751 empty initializer() rather than no initializer. This can 20752 only happen when the initializer is a pack expansion whose 20753 parameter packs are of length zero. */ 20754 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE) 20755 init_vec = NULL; 20756 else if (init == error_mark_node) 20757 RETURN (error_mark_node); 20758 else 20759 { 20760 init_vec = make_tree_vector (); 20761 if (init == void_node) 20762 gcc_assert (init_vec != NULL); 20763 else 20764 { 20765 for (; init != NULL_TREE; init = TREE_CHAIN (init)) 20766 vec_safe_push (init_vec, TREE_VALUE (init)); 20767 } 20768 } 20769 20770 /* Avoid passing an enclosing decl to valid_array_size_p. */ 20771 in_decl = NULL_TREE; 20772 20773 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); 20774 tree op2 = RECUR (TREE_OPERAND (t, 2)); 20775 ret = build_new (loc, &placement_vec, op1, op2, 20776 &init_vec, NEW_EXPR_USE_GLOBAL (t), 20777 complain); 20778 20779 if (placement_vec != NULL) 20780 release_tree_vector (placement_vec); 20781 if (init_vec != NULL) 20782 release_tree_vector (init_vec); 20783 20784 RETURN (ret); 20785 } 20786 20787 case DELETE_EXPR: 20788 { 20789 tree op0 = RECUR (TREE_OPERAND (t, 0)); 20790 tree op1 = RECUR (TREE_OPERAND (t, 1)); 20791 RETURN (delete_sanity (input_location, op0, op1, 20792 DELETE_EXPR_USE_VEC (t), 20793 DELETE_EXPR_USE_GLOBAL (t), 20794 complain)); 20795 } 20796 20797 case COMPOUND_EXPR: 20798 { 20799 tree op0 = tsubst_expr (TREE_OPERAND (t, 0), args, 20800 complain & ~tf_decltype, in_decl); 20801 RETURN (build_x_compound_expr (EXPR_LOCATION (t), 20802 op0, 20803 RECUR (TREE_OPERAND (t, 1)), 20804 templated_operator_saved_lookups (t), 20805 complain|decltype_flag)); 20806 } 20807 20808 case CALL_EXPR: 20809 { 20810 tree function; 20811 unsigned int nargs; 20812 bool qualified_p; 20813 bool koenig_p; 20814 tree ret; 20815 20816 function = CALL_EXPR_FN (t); 20817 /* Internal function with no arguments. */ 20818 if (function == NULL_TREE && call_expr_nargs (t) == 0) 20819 RETURN (t); 20820 20821 /* When we parsed the expression, we determined whether or 20822 not Koenig lookup should be performed. */ 20823 koenig_p = KOENIG_LOOKUP_P (t); 20824 if (function == NULL_TREE) 20825 { 20826 koenig_p = false; 20827 qualified_p = false; 20828 } 20829 else if (TREE_CODE (function) == SCOPE_REF) 20830 { 20831 qualified_p = true; 20832 function = tsubst_qualified_id (function, args, complain, in_decl, 20833 /*done=*/false, 20834 /*address_p=*/false); 20835 } 20836 else if (CALL_EXPR_STATIC_CHAIN (t) 20837 && TREE_CODE (function) == FUNCTION_DECL 20838 && fndecl_built_in_p (function, BUILT_IN_CLASSIFY_TYPE)) 20839 { 20840 tree type = tsubst (CALL_EXPR_STATIC_CHAIN (t), args, complain, 20841 in_decl); 20842 if (dependent_type_p (type)) 20843 { 20844 ret = build_vl_exp (CALL_EXPR, 4); 20845 CALL_EXPR_FN (ret) = function; 20846 CALL_EXPR_STATIC_CHAIN (ret) = type; 20847 CALL_EXPR_ARG (ret, 0) 20848 = build_min (SIZEOF_EXPR, size_type_node, type); 20849 TREE_TYPE (ret) = integer_type_node; 20850 } 20851 else 20852 ret = build_int_cst (integer_type_node, type_to_class (type)); 20853 RETURN (ret); 20854 } 20855 else if (koenig_p 20856 && (identifier_p (function) 20857 || (TREE_CODE (function) == TEMPLATE_ID_EXPR 20858 && identifier_p (TREE_OPERAND (function, 0))))) 20859 { 20860 /* Do nothing; calling tsubst_expr on an identifier 20861 would incorrectly perform unqualified lookup again. 20862 20863 Note that we can also have an IDENTIFIER_NODE if the earlier 20864 unqualified lookup found a dependent local extern declaration 20865 (as per finish_call_expr); in that case koenig_p will be false 20866 and we do want to do the lookup again to find the substituted 20867 declaration. */ 20868 qualified_p = false; 20869 20870 if (TREE_CODE (function) == TEMPLATE_ID_EXPR) 20871 function = tsubst_name (function, args, complain, in_decl); 20872 } 20873 else 20874 { 20875 if (TREE_CODE (function) == COMPONENT_REF) 20876 { 20877 tree op = TREE_OPERAND (function, 1); 20878 20879 qualified_p = (TREE_CODE (op) == SCOPE_REF 20880 || (BASELINK_P (op) 20881 && BASELINK_QUALIFIED_P (op))); 20882 } 20883 else 20884 qualified_p = false; 20885 20886 if (TREE_CODE (function) == ADDR_EXPR 20887 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) 20888 /* Avoid error about taking the address of a constructor. */ 20889 function = TREE_OPERAND (function, 0); 20890 20891 tsubst_flags_t subcomplain = complain; 20892 if (koenig_p && TREE_CODE (function) == FUNCTION_DECL) 20893 /* When KOENIG_P, we don't want to mark_used the callee before 20894 augmenting the overload set via ADL, so during this initial 20895 substitution we disable mark_used by setting tf_conv (68942). */ 20896 subcomplain |= tf_conv; 20897 function = tsubst_expr (function, args, subcomplain, in_decl); 20898 20899 if (BASELINK_P (function)) 20900 qualified_p = true; 20901 } 20902 20903 nargs = call_expr_nargs (t); 20904 releasing_vec call_args; 20905 tsubst_call_args (t, args, complain, in_decl, call_args); 20906 20907 /* Stripped-down processing for a call in a thunk. Specifically, in 20908 the thunk template for a generic lambda. */ 20909 if (call_from_lambda_thunk_p (t)) 20910 { 20911 /* Now that we've expanded any packs, the number of call args 20912 might be different. */ 20913 unsigned int cargs = call_args->length (); 20914 tree thisarg = NULL_TREE; 20915 if (TREE_CODE (function) == COMPONENT_REF) 20916 { 20917 thisarg = TREE_OPERAND (function, 0); 20918 if (TREE_CODE (thisarg) == INDIRECT_REF) 20919 thisarg = TREE_OPERAND (thisarg, 0); 20920 function = TREE_OPERAND (function, 1); 20921 if (TREE_CODE (function) == BASELINK) 20922 function = BASELINK_FUNCTIONS (function); 20923 } 20924 /* We aren't going to do normal overload resolution, so force the 20925 template-id to resolve. */ 20926 function = resolve_nondeduced_context (function, complain); 20927 for (unsigned i = 0; i < cargs; ++i) 20928 { 20929 /* In a thunk, pass through args directly, without any 20930 conversions. */ 20931 tree arg = (*call_args)[i]; 20932 while (TREE_CODE (arg) != PARM_DECL) 20933 arg = TREE_OPERAND (arg, 0); 20934 (*call_args)[i] = arg; 20935 } 20936 if (thisarg) 20937 { 20938 /* If there are no other args, just push 'this'. */ 20939 if (cargs == 0) 20940 vec_safe_push (call_args, thisarg); 20941 else 20942 { 20943 /* Otherwise, shift the other args over to make room. */ 20944 tree last = (*call_args)[cargs - 1]; 20945 vec_safe_push (call_args, last); 20946 for (int i = cargs - 1; i > 0; --i) 20947 (*call_args)[i] = (*call_args)[i - 1]; 20948 (*call_args)[0] = thisarg; 20949 } 20950 } 20951 ret = build_call_a (function, call_args->length (), 20952 call_args->address ()); 20953 /* The thunk location is not interesting. */ 20954 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION); 20955 CALL_FROM_THUNK_P (ret) = true; 20956 if (CLASS_TYPE_P (TREE_TYPE (ret))) 20957 CALL_EXPR_RETURN_SLOT_OPT (ret) = true; 20958 20959 RETURN (ret); 20960 } 20961 20962 /* We do not perform argument-dependent lookup if normal 20963 lookup finds a non-function, in accordance with the 20964 resolution of DR 218. */ 20965 if (koenig_p 20966 && ((is_overloaded_fn (function) 20967 /* If lookup found a member function, the Koenig lookup is 20968 not appropriate, even if an unqualified-name was used 20969 to denote the function. */ 20970 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function))) 20971 || identifier_p (function) 20972 /* C++20 P0846: Lookup found nothing. */ 20973 || (TREE_CODE (function) == TEMPLATE_ID_EXPR 20974 && identifier_p (TREE_OPERAND (function, 0)))) 20975 /* Only do this when substitution turns a dependent call 20976 into a non-dependent call. */ 20977 && type_dependent_expression_p_push (t) 20978 && !any_type_dependent_arguments_p (call_args)) 20979 function = perform_koenig_lookup (function, call_args, tf_none); 20980 20981 if (function != NULL_TREE 20982 && (identifier_p (function) 20983 || (TREE_CODE (function) == TEMPLATE_ID_EXPR 20984 && identifier_p (TREE_OPERAND (function, 0)) 20985 && !any_dependent_template_arguments_p (TREE_OPERAND 20986 (function, 1)))) 20987 && !any_type_dependent_arguments_p (call_args)) 20988 { 20989 bool template_id_p = (TREE_CODE (function) == TEMPLATE_ID_EXPR); 20990 if (template_id_p) 20991 function = TREE_OPERAND (function, 0); 20992 if (koenig_p && (complain & tf_warning_or_error)) 20993 { 20994 /* For backwards compatibility and good diagnostics, try 20995 the unqualified lookup again if we aren't in SFINAE 20996 context. */ 20997 tree unq = tsubst_expr (function, args, complain, in_decl); 20998 if (unq == error_mark_node) 20999 RETURN (error_mark_node); 21000 21001 if (unq != function) 21002 { 21003 char const *const msg 21004 = G_("%qD was not declared in this scope, " 21005 "and no declarations were found by " 21006 "argument-dependent lookup at the point " 21007 "of instantiation"); 21008 21009 bool in_lambda = (current_class_type 21010 && LAMBDA_TYPE_P (current_class_type)); 21011 /* In a lambda fn, we have to be careful to not 21012 introduce new this captures. Legacy code can't 21013 be using lambdas anyway, so it's ok to be 21014 stricter. Be strict with C++20 template-id ADL too. 21015 And be strict if we're already failing anyway. */ 21016 bool strict = in_lambda || template_id_p || seen_error(); 21017 bool diag = true; 21018 if (strict) 21019 error_at (cp_expr_loc_or_input_loc (t), 21020 msg, function); 21021 else 21022 diag = permerror (cp_expr_loc_or_input_loc (t), 21023 msg, function); 21024 if (diag) 21025 { 21026 tree fn = unq; 21027 21028 if (INDIRECT_REF_P (fn)) 21029 fn = TREE_OPERAND (fn, 0); 21030 if (is_overloaded_fn (fn)) 21031 fn = get_first_fn (fn); 21032 21033 if (!DECL_P (fn)) 21034 /* Can't say anything more. */; 21035 else if (DECL_CLASS_SCOPE_P (fn)) 21036 { 21037 location_t loc = cp_expr_loc_or_input_loc (t); 21038 inform (loc, 21039 "declarations in dependent base %qT are " 21040 "not found by unqualified lookup", 21041 DECL_CLASS_CONTEXT (fn)); 21042 if (current_class_ptr) 21043 inform (loc, 21044 "use %<this->%D%> instead", function); 21045 else 21046 inform (loc, 21047 "use %<%T::%D%> instead", 21048 current_class_name, function); 21049 } 21050 else 21051 inform (DECL_SOURCE_LOCATION (fn), 21052 "%qD declared here, later in the " 21053 "translation unit", fn); 21054 if (strict) 21055 RETURN (error_mark_node); 21056 } 21057 21058 function = unq; 21059 } 21060 } 21061 if (identifier_p (function)) 21062 { 21063 if (complain & tf_error) 21064 unqualified_name_lookup_error (function); 21065 RETURN (error_mark_node); 21066 } 21067 } 21068 21069 /* Remember that there was a reference to this entity. */ 21070 if (function != NULL_TREE 21071 && DECL_P (function) 21072 && !mark_used (function, complain) && !(complain & tf_error)) 21073 RETURN (error_mark_node); 21074 21075 if (!maybe_fold_fn_template_args (function, complain)) 21076 return error_mark_node; 21077 21078 /* Put back tf_decltype for the actual call. */ 21079 complain |= decltype_flag; 21080 21081 if (function == NULL_TREE) 21082 switch (CALL_EXPR_IFN (t)) 21083 { 21084 case IFN_LAUNDER: 21085 gcc_assert (nargs == 1); 21086 if (vec_safe_length (call_args) != 1) 21087 { 21088 error_at (cp_expr_loc_or_input_loc (t), 21089 "wrong number of arguments to " 21090 "%<__builtin_launder%>"); 21091 ret = error_mark_node; 21092 } 21093 else 21094 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t), 21095 (*call_args)[0], complain); 21096 break; 21097 21098 case IFN_VEC_CONVERT: 21099 gcc_assert (nargs == 1); 21100 if (vec_safe_length (call_args) != 1) 21101 { 21102 error_at (cp_expr_loc_or_input_loc (t), 21103 "wrong number of arguments to " 21104 "%<__builtin_convertvector%>"); 21105 ret = error_mark_node; 21106 break; 21107 } 21108 ret = cp_build_vec_convert ((*call_args)[0], input_location, 21109 tsubst (TREE_TYPE (t), args, 21110 complain, in_decl), 21111 complain); 21112 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR) 21113 RETURN (ret); 21114 break; 21115 21116 case IFN_SHUFFLEVECTOR: 21117 { 21118 ret = build_x_shufflevector (input_location, call_args, 21119 complain); 21120 if (ret != error_mark_node) 21121 RETURN (ret); 21122 break; 21123 } 21124 21125 case IFN_ASSUME: 21126 gcc_assert (nargs == 1); 21127 if (vec_safe_length (call_args) != 1) 21128 { 21129 error_at (cp_expr_loc_or_input_loc (t), 21130 "wrong number of arguments to " 21131 "%<assume%> attribute"); 21132 ret = error_mark_node; 21133 } 21134 else 21135 { 21136 tree &arg = (*call_args)[0]; 21137 if (!type_dependent_expression_p (arg)) 21138 arg = contextual_conv_bool (arg, tf_warning_or_error); 21139 if (error_operand_p (arg)) 21140 { 21141 ret = error_mark_node; 21142 break; 21143 } 21144 ret = build_assume_call (EXPR_LOCATION (t), arg); 21145 RETURN (ret); 21146 } 21147 break; 21148 21149 default: 21150 /* Unsupported internal function with arguments. */ 21151 gcc_unreachable (); 21152 } 21153 else if (TREE_CODE (function) == OFFSET_REF 21154 || TREE_CODE (function) == DOTSTAR_EXPR 21155 || TREE_CODE (function) == MEMBER_REF) 21156 ret = build_offset_ref_call_from_tree (function, &call_args, 21157 complain); 21158 else if (concept_check_p (function)) 21159 { 21160 /* FUNCTION is a template-id referring to a concept definition. */ 21161 tree id = unpack_concept_check (function); 21162 tree tmpl = TREE_OPERAND (id, 0); 21163 tree args = TREE_OPERAND (id, 1); 21164 21165 /* Calls to standard and variable concepts should have been 21166 previously diagnosed. */ 21167 gcc_assert (function_concept_p (tmpl)); 21168 21169 /* Ensure the result is wrapped as a call expression. */ 21170 ret = build_concept_check (tmpl, args, tf_warning_or_error); 21171 } 21172 else 21173 ret = finish_call_expr (function, &call_args, 21174 /*disallow_virtual=*/qualified_p, 21175 koenig_p, 21176 complain); 21177 21178 if (ret != error_mark_node) 21179 { 21180 bool op = CALL_EXPR_OPERATOR_SYNTAX (t); 21181 bool ord = CALL_EXPR_ORDERED_ARGS (t); 21182 bool rev = CALL_EXPR_REVERSE_ARGS (t); 21183 if (op || ord || rev) 21184 if (tree call = extract_call_expr (ret)) 21185 { 21186 CALL_EXPR_OPERATOR_SYNTAX (call) = op; 21187 CALL_EXPR_ORDERED_ARGS (call) = ord; 21188 CALL_EXPR_REVERSE_ARGS (call) = rev; 21189 } 21190 if (warning_suppressed_p (t, OPT_Wpessimizing_move)) 21191 /* This also suppresses -Wredundant-move. */ 21192 suppress_warning (ret, OPT_Wpessimizing_move); 21193 } 21194 21195 RETURN (ret); 21196 } 21197 21198 case COND_EXPR: 21199 { 21200 tree cond = RECUR (TREE_OPERAND (t, 0)); 21201 cond = mark_rvalue_use (cond); 21202 tree folded_cond = fold_non_dependent_expr (cond, complain); 21203 tree exp1, exp2; 21204 21205 if (TREE_CODE (folded_cond) == INTEGER_CST) 21206 { 21207 if (integer_zerop (folded_cond)) 21208 { 21209 ++c_inhibit_evaluation_warnings; 21210 exp1 = RECUR (TREE_OPERAND (t, 1)); 21211 --c_inhibit_evaluation_warnings; 21212 exp2 = RECUR (TREE_OPERAND (t, 2)); 21213 } 21214 else 21215 { 21216 exp1 = RECUR (TREE_OPERAND (t, 1)); 21217 ++c_inhibit_evaluation_warnings; 21218 exp2 = RECUR (TREE_OPERAND (t, 2)); 21219 --c_inhibit_evaluation_warnings; 21220 } 21221 cond = folded_cond; 21222 } 21223 else 21224 { 21225 exp1 = RECUR (TREE_OPERAND (t, 1)); 21226 exp2 = RECUR (TREE_OPERAND (t, 2)); 21227 } 21228 21229 warning_sentinel s(warn_duplicated_branches); 21230 RETURN (build_x_conditional_expr (EXPR_LOCATION (t), 21231 cond, exp1, exp2, complain)); 21232 } 21233 21234 case PSEUDO_DTOR_EXPR: 21235 { 21236 tree op0 = RECUR (TREE_OPERAND (t, 0)); 21237 tree op1 = RECUR (TREE_OPERAND (t, 1)); 21238 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl); 21239 RETURN (finish_pseudo_destructor_expr (op0, op1, op2, 21240 input_location)); 21241 } 21242 21243 case TREE_LIST: 21244 RETURN (tsubst_tree_list (t, args, complain, in_decl)); 21245 21246 case COMPONENT_REF: 21247 { 21248 tree object; 21249 tree object_type; 21250 tree member; 21251 tree r; 21252 21253 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), 21254 args, complain, in_decl); 21255 /* Remember that there was a reference to this entity. */ 21256 if (DECL_P (object) 21257 && !mark_used (object, complain) && !(complain & tf_error)) 21258 RETURN (error_mark_node); 21259 object_type = TREE_TYPE (object); 21260 21261 member = TREE_OPERAND (t, 1); 21262 if (BASELINK_P (member)) 21263 member = tsubst_baselink (member, 21264 non_reference (TREE_TYPE (object)), 21265 args, complain, in_decl); 21266 else 21267 member = tsubst_name (member, args, complain, in_decl); 21268 if (member == error_mark_node) 21269 RETURN (error_mark_node); 21270 21271 if (object_type && TYPE_PTRMEMFUNC_P (object_type) 21272 && TREE_CODE (member) == FIELD_DECL) 21273 { 21274 r = build_ptrmemfunc_access_expr (object, DECL_NAME (member)); 21275 RETURN (r); 21276 } 21277 else if (TREE_CODE (member) == FIELD_DECL) 21278 { 21279 r = finish_non_static_data_member (member, object, NULL_TREE, 21280 complain); 21281 if (REF_PARENTHESIZED_P (t)) 21282 r = force_paren_expr (r); 21283 RETURN (r); 21284 } 21285 else if (type_dependent_expression_p (object)) 21286 /* We can't do much here. */; 21287 else if (!CLASS_TYPE_P (object_type)) 21288 { 21289 if (scalarish_type_p (object_type)) 21290 { 21291 tree s = NULL_TREE; 21292 tree dtor = member; 21293 21294 if (TREE_CODE (dtor) == SCOPE_REF) 21295 { 21296 s = TREE_OPERAND (dtor, 0); 21297 dtor = TREE_OPERAND (dtor, 1); 21298 } 21299 if (TREE_CODE (dtor) == BIT_NOT_EXPR) 21300 { 21301 dtor = TREE_OPERAND (dtor, 0); 21302 if (TYPE_P (dtor)) 21303 RETURN (finish_pseudo_destructor_expr 21304 (object, s, dtor, input_location)); 21305 } 21306 } 21307 } 21308 else if (TREE_CODE (member) == SCOPE_REF 21309 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR) 21310 { 21311 /* Lookup the template functions now that we know what the 21312 scope is. */ 21313 tree scope = TREE_OPERAND (member, 0); 21314 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0); 21315 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1); 21316 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL, 21317 /*complain=*/false); 21318 if (BASELINK_P (member)) 21319 { 21320 BASELINK_FUNCTIONS (member) 21321 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member), 21322 args); 21323 member = (adjust_result_of_qualified_name_lookup 21324 (member, BINFO_TYPE (BASELINK_BINFO (member)), 21325 object_type)); 21326 } 21327 else 21328 { 21329 qualified_name_lookup_error (scope, tmpl, member, 21330 input_location); 21331 RETURN (error_mark_node); 21332 } 21333 } 21334 else if (TREE_CODE (member) == SCOPE_REF 21335 && !CLASS_TYPE_P (TREE_OPERAND (member, 0)) 21336 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL) 21337 { 21338 if (complain & tf_error) 21339 { 21340 if (TYPE_P (TREE_OPERAND (member, 0))) 21341 error ("%qT is not a class or namespace", 21342 TREE_OPERAND (member, 0)); 21343 else 21344 error ("%qD is not a class or namespace", 21345 TREE_OPERAND (member, 0)); 21346 } 21347 RETURN (error_mark_node); 21348 } 21349 21350 r = finish_class_member_access_expr (object, member, 21351 /*template_p=*/false, 21352 complain); 21353 if (REF_PARENTHESIZED_P (t)) 21354 r = force_paren_expr (r); 21355 RETURN (r); 21356 } 21357 21358 case THROW_EXPR: 21359 RETURN (build_throw 21360 (input_location, RECUR (TREE_OPERAND (t, 0)), complain)); 21361 21362 case CONSTRUCTOR: 21363 { 21364 vec<constructor_elt, va_gc> *n; 21365 constructor_elt *ce; 21366 unsigned HOST_WIDE_INT idx; 21367 bool process_index_p; 21368 int newlen; 21369 bool need_copy_p = false; 21370 tree r; 21371 21372 tsubst_flags_t tcomplain = complain; 21373 if (COMPOUND_LITERAL_P (t)) 21374 tcomplain |= tf_tst_ok; 21375 tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl); 21376 if (type == error_mark_node) 21377 RETURN (error_mark_node); 21378 21379 /* We do not want to process the index of aggregate 21380 initializers as they are identifier nodes which will be 21381 looked up by digest_init. */ 21382 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type)); 21383 21384 if (null_member_pointer_value_p (t)) 21385 { 21386 gcc_assert (same_type_p (type, TREE_TYPE (t))); 21387 RETURN (t); 21388 } 21389 21390 n = vec_safe_copy (CONSTRUCTOR_ELTS (t)); 21391 newlen = vec_safe_length (n); 21392 FOR_EACH_VEC_SAFE_ELT (n, idx, ce) 21393 { 21394 if (ce->index && process_index_p 21395 /* An identifier index is looked up in the type 21396 being initialized, not the current scope. */ 21397 && TREE_CODE (ce->index) != IDENTIFIER_NODE) 21398 ce->index = RECUR (ce->index); 21399 21400 if (PACK_EXPANSION_P (ce->value)) 21401 { 21402 /* Substitute into the pack expansion. */ 21403 ce->value = tsubst_pack_expansion (ce->value, args, complain, 21404 in_decl); 21405 21406 if (ce->value == error_mark_node 21407 || PACK_EXPANSION_P (ce->value)) 21408 ; 21409 else if (TREE_VEC_LENGTH (ce->value) == 1) 21410 /* Just move the argument into place. */ 21411 ce->value = TREE_VEC_ELT (ce->value, 0); 21412 else 21413 { 21414 /* Update the length of the final CONSTRUCTOR 21415 arguments vector, and note that we will need to 21416 copy.*/ 21417 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1; 21418 need_copy_p = true; 21419 } 21420 } 21421 else 21422 ce->value = RECUR (ce->value); 21423 } 21424 21425 if (need_copy_p) 21426 { 21427 vec<constructor_elt, va_gc> *old_n = n; 21428 21429 vec_alloc (n, newlen); 21430 FOR_EACH_VEC_ELT (*old_n, idx, ce) 21431 { 21432 if (TREE_CODE (ce->value) == TREE_VEC) 21433 { 21434 int i, len = TREE_VEC_LENGTH (ce->value); 21435 for (i = 0; i < len; ++i) 21436 CONSTRUCTOR_APPEND_ELT (n, 0, 21437 TREE_VEC_ELT (ce->value, i)); 21438 } 21439 else 21440 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value); 21441 } 21442 } 21443 21444 r = build_constructor (init_list_type_node, n); 21445 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t); 21446 CONSTRUCTOR_IS_DESIGNATED_INIT (r) 21447 = CONSTRUCTOR_IS_DESIGNATED_INIT (t); 21448 21449 if (TREE_HAS_CONSTRUCTOR (t)) 21450 { 21451 fcl_t cl = fcl_functional; 21452 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t)) 21453 cl = fcl_c99; 21454 RETURN (finish_compound_literal (type, r, complain, cl)); 21455 } 21456 21457 TREE_TYPE (r) = type; 21458 RETURN (r); 21459 } 21460 21461 case TYPEID_EXPR: 21462 { 21463 tree operand_0 = TREE_OPERAND (t, 0); 21464 if (TYPE_P (operand_0)) 21465 { 21466 operand_0 = tsubst (operand_0, args, complain, in_decl); 21467 RETURN (get_typeid (operand_0, complain)); 21468 } 21469 else 21470 { 21471 operand_0 = RECUR (operand_0); 21472 RETURN (build_typeid (operand_0, complain)); 21473 } 21474 } 21475 21476 case FUNCTION_DECL: 21477 case PARM_DECL: 21478 case VAR_DECL: 21479 if (!args) 21480 RETURN (t); 21481 tree r; 21482 if (VAR_OR_FUNCTION_DECL_P (t) 21483 && DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) 21484 r = tsubst_decl (t, args, complain); 21485 else if (VAR_OR_FUNCTION_DECL_P (t) && DECL_LOCAL_DECL_P (t)) 21486 { 21487 /* Local specialization will usually have been created when 21488 we instantiated the DECL_EXPR_DECL. */ 21489 r = retrieve_local_specialization (t); 21490 if (!r) 21491 { 21492 /* We're in a generic lambda referencing a local extern 21493 from an outer block-scope of a non-template. */ 21494 gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl)); 21495 r = t; 21496 } 21497 } 21498 else if (local_variable_p (t) 21499 && ((r = retrieve_local_specialization (t)) 21500 || TREE_CODE (t) == PARM_DECL 21501 || uses_template_parms (DECL_CONTEXT (t)))) 21502 { 21503 if (r == NULL_TREE && TREE_CODE (t) == PARM_DECL) 21504 { 21505 /* We get here for a use of 'this' in an NSDMI. */ 21506 if (DECL_NAME (t) == this_identifier && current_class_ptr) 21507 RETURN (current_class_ptr); 21508 21509 /* This can happen for a parameter name used later in a function 21510 declaration (such as in a late-specified return type). Just 21511 make a dummy decl, since it's only used for its type. */ 21512 gcc_assert (cp_unevaluated_operand); 21513 r = tsubst_decl (t, args, complain); 21514 /* Give it the template pattern as its context; its true context 21515 hasn't been instantiated yet and this is good enough for 21516 mangling. */ 21517 DECL_CONTEXT (r) = DECL_CONTEXT (t); 21518 } 21519 else if (r == NULL_TREE) 21520 { 21521 /* First try name lookup to find the instantiation. */ 21522 r = lookup_name (DECL_NAME (t)); 21523 if (r) 21524 { 21525 if (!VAR_P (r)) 21526 { 21527 /* During error-recovery we may find a non-variable, 21528 even an OVERLOAD: just bail out and avoid ICEs and 21529 duplicate diagnostics (c++/62207). */ 21530 gcc_assert (seen_error ()); 21531 RETURN (error_mark_node); 21532 } 21533 if (!is_capture_proxy (r)) 21534 { 21535 /* Make sure the one we found is the one we want. */ 21536 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t)); 21537 if (ctx != DECL_CONTEXT (r)) 21538 r = NULL_TREE; 21539 } 21540 } 21541 21542 if (r) 21543 /* OK */; 21544 else 21545 { 21546 /* This can happen for a variable used in a 21547 late-specified return type of a local lambda, or for a 21548 local static or constant. Building a new VAR_DECL 21549 should be OK in all those cases. */ 21550 r = tsubst_decl (t, args, complain); 21551 if (local_specializations) 21552 /* Avoid infinite recursion (79640). */ 21553 register_local_specialization (r, t); 21554 if (decl_maybe_constant_var_p (r)) 21555 { 21556 /* We can't call cp_finish_decl, so handle the 21557 initializer by hand. */ 21558 tree init = tsubst_init (DECL_INITIAL (t), r, args, 21559 complain, in_decl); 21560 if (!processing_template_decl) 21561 init = maybe_constant_init (init); 21562 if (processing_template_decl 21563 ? potential_constant_expression (init) 21564 : reduced_constant_expression_p (init)) 21565 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) 21566 = TREE_CONSTANT (r) = true; 21567 DECL_INITIAL (r) = init; 21568 if (tree auto_node = type_uses_auto (TREE_TYPE (r))) 21569 TREE_TYPE (r) 21570 = do_auto_deduction (TREE_TYPE (r), init, auto_node, 21571 complain, adc_variable_type); 21572 } 21573 gcc_assert (cp_unevaluated_operand 21574 || processing_contract_condition 21575 || TREE_STATIC (r) 21576 || decl_constant_var_p (r) 21577 || seen_error ()); 21578 if (!processing_template_decl 21579 && !TREE_STATIC (r)) 21580 r = process_outer_var_ref (r, complain); 21581 } 21582 /* Remember this for subsequent uses. */ 21583 if (local_specializations) 21584 register_local_specialization (r, t); 21585 } 21586 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT) 21587 r = argument_pack_select_arg (r); 21588 } 21589 else 21590 r = t; 21591 if (!mark_used (r, complain)) 21592 RETURN (error_mark_node); 21593 21594 if (!no_name_lookup_flag 21595 && (TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == VAR_DECL)) 21596 { 21597 /* ??? We're doing a subset of finish_id_expression here. */ 21598 if (tree wrap = maybe_get_tls_wrapper_call (r)) 21599 /* Replace an evaluated use of the thread_local variable with 21600 a call to its wrapper. */ 21601 r = wrap; 21602 else if (outer_automatic_var_p (r)) 21603 r = process_outer_var_ref (r, complain); 21604 21605 if (!TYPE_REF_P (TREE_TYPE (t))) 21606 /* If the original type was a reference, we'll be wrapped in 21607 the appropriate INDIRECT_REF. */ 21608 r = convert_from_reference (r); 21609 } 21610 RETURN (r); 21611 21612 case CONST_DECL: 21613 { 21614 tree enum_type; 21615 tree v; 21616 21617 if (DECL_TEMPLATE_PARM_P (t)) 21618 RETURN (RECUR (DECL_INITIAL (t))); 21619 if (!args || !uses_template_parms (DECL_CONTEXT (t))) 21620 RETURN (t); 21621 21622 /* Unfortunately, we cannot just call lookup_name here. 21623 Consider: 21624 21625 template <int I> int f() { 21626 enum E { a = I }; 21627 struct S { void g() { E e = a; } }; 21628 }; 21629 21630 When we instantiate f<7>::S::g(), say, lookup_name is not 21631 clever enough to find f<7>::a. */ 21632 enum_type 21633 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, 21634 /*entering_scope=*/0); 21635 21636 for (v = TYPE_VALUES (enum_type); 21637 v != NULL_TREE; 21638 v = TREE_CHAIN (v)) 21639 if (TREE_PURPOSE (v) == DECL_NAME (t)) 21640 RETURN (TREE_VALUE (v)); 21641 21642 /* We didn't find the name. That should never happen; if 21643 name-lookup found it during preliminary parsing, we 21644 should find it again here during instantiation. */ 21645 gcc_unreachable (); 21646 RETURN (t); 21647 } 21648 21649 case FIELD_DECL: 21650 if (DECL_CONTEXT (t)) 21651 { 21652 tree ctx; 21653 21654 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, 21655 /*entering_scope=*/1); 21656 if (ctx != DECL_CONTEXT (t)) 21657 { 21658 tree r = lookup_field (ctx, DECL_NAME (t), 0, false); 21659 if (!r) 21660 { 21661 if (complain & tf_error) 21662 error ("using invalid field %qD", t); 21663 RETURN (error_mark_node); 21664 } 21665 RETURN (r); 21666 } 21667 } 21668 RETURN (t); 21669 21670 case NAMESPACE_DECL: 21671 case OVERLOAD: 21672 RETURN (t); 21673 21674 case TEMPLATE_DECL: 21675 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) 21676 RETURN (tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)), 21677 args, complain, in_decl)); 21678 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t)) 21679 RETURN (tsubst (t, args, complain, in_decl)); 21680 else if (DECL_CLASS_SCOPE_P (t) 21681 && uses_template_parms (DECL_CONTEXT (t))) 21682 { 21683 /* Template template argument like the following example need 21684 special treatment: 21685 21686 template <template <class> class TT> struct C {}; 21687 template <class T> struct D { 21688 template <class U> struct E {}; 21689 C<E> c; // #1 21690 }; 21691 D<int> d; // #2 21692 21693 We are processing the template argument `E' in #1 for 21694 the template instantiation #2. Originally, `E' is a 21695 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we 21696 have to substitute this with one having context `D<int>'. */ 21697 21698 tree context = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, 21699 in_decl, /*entering_scope=*/true); 21700 RETURN (lookup_field (context, DECL_NAME(t), 0, false)); 21701 } 21702 else 21703 /* Ordinary template template argument. */ 21704 RETURN (t); 21705 21706 case TEMPLATE_PARM_INDEX: 21707 case TYPE_DECL: 21708 RETURN (tsubst (t, args, complain, in_decl)); 21709 21710 case CLEANUP_POINT_EXPR: 21711 /* We shouldn't have built any of these during initial template 21712 generation. Instead, they should be built during instantiation 21713 in response to the saved STMT_IS_FULL_EXPR_P setting. */ 21714 gcc_unreachable (); 21715 21716 case OFFSET_REF: 21717 { 21718 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); 21719 tree op0 = RECUR (TREE_OPERAND (t, 0)); 21720 tree op1 = RECUR (TREE_OPERAND (t, 1)); 21721 r = build2 (OFFSET_REF, type, op0, op1); 21722 PTRMEM_OK_P (r) = PTRMEM_OK_P (t); 21723 if (!mark_used (TREE_OPERAND (r, 1), complain) 21724 && !(complain & tf_error)) 21725 RETURN (error_mark_node); 21726 RETURN (r); 21727 } 21728 21729 case EXPR_PACK_EXPANSION: 21730 error ("invalid use of pack expansion expression"); 21731 RETURN (error_mark_node); 21732 21733 case NONTYPE_ARGUMENT_PACK: 21734 error ("use %<...%> to expand argument pack"); 21735 RETURN (error_mark_node); 21736 21737 case VOID_CST: 21738 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t))); 21739 RETURN (t); 21740 21741 case INTEGER_CST: 21742 case REAL_CST: 21743 case COMPLEX_CST: 21744 case VECTOR_CST: 21745 { 21746 /* Instantiate any typedefs in the type. */ 21747 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); 21748 r = fold_convert (type, t); 21749 gcc_assert (TREE_CODE (r) == TREE_CODE (t)); 21750 RETURN (r); 21751 } 21752 21753 case STRING_CST: 21754 { 21755 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); 21756 r = t; 21757 if (type != TREE_TYPE (t)) 21758 { 21759 r = copy_node (t); 21760 TREE_TYPE (r) = type; 21761 } 21762 RETURN (r); 21763 } 21764 21765 case PTRMEM_CST: 21766 /* These can sometimes show up in a partial instantiation, but never 21767 involve template parms. */ 21768 gcc_assert (!uses_template_parms (t)); 21769 RETURN (t); 21770 21771 case UNARY_LEFT_FOLD_EXPR: 21772 RETURN (tsubst_unary_left_fold (t, args, complain, in_decl)); 21773 case UNARY_RIGHT_FOLD_EXPR: 21774 RETURN (tsubst_unary_right_fold (t, args, complain, in_decl)); 21775 case BINARY_LEFT_FOLD_EXPR: 21776 RETURN (tsubst_binary_left_fold (t, args, complain, in_decl)); 21777 case BINARY_RIGHT_FOLD_EXPR: 21778 RETURN (tsubst_binary_right_fold (t, args, complain, in_decl)); 21779 case PREDICT_EXPR: 21780 RETURN (t); 21781 21782 case DEBUG_BEGIN_STMT: 21783 /* ??? There's no point in copying it for now, but maybe some 21784 day it will contain more information, such as a pointer back 21785 to the containing function, inlined copy or so. */ 21786 RETURN (t); 21787 21788 case CO_YIELD_EXPR: 21789 RETURN (finish_co_yield_expr (input_location, 21790 RECUR (TREE_OPERAND (t, 0)))); 21791 21792 case CO_AWAIT_EXPR: 21793 RETURN (finish_co_await_expr (input_location, 21794 RECUR (TREE_OPERAND (t, 0)))); 21795 21796 case VA_ARG_EXPR: 21797 { 21798 tree op0 = RECUR (TREE_OPERAND (t, 0)); 21799 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); 21800 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type)); 21801 } 21802 21803 case OFFSETOF_EXPR: 21804 { 21805 tree object_ptr 21806 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl); 21807 RETURN (finish_offsetof (object_ptr, 21808 RECUR (TREE_OPERAND (t, 0)), 21809 EXPR_LOCATION (t))); 21810 } 21811 21812 case ADDRESSOF_EXPR: 21813 RETURN (cp_build_addressof (EXPR_LOCATION (t), 21814 RECUR (TREE_OPERAND (t, 0)), complain)); 21815 21816 case TRAIT_EXPR: 21817 { 21818 tree type1 = TRAIT_EXPR_TYPE1 (t); 21819 if (TYPE_P (type1)) 21820 type1 = tsubst (type1, args, complain, in_decl); 21821 else 21822 type1 = tsubst_expr (type1, args, complain, in_decl); 21823 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args, 21824 complain, in_decl); 21825 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t), 21826 TRAIT_EXPR_KIND (t), type1, type2)); 21827 } 21828 21829 case STMT_EXPR: 21830 { 21831 tree old_stmt_expr = cur_stmt_expr; 21832 tree stmt_expr = begin_stmt_expr (); 21833 21834 cur_stmt_expr = stmt_expr; 21835 tsubst_stmt (STMT_EXPR_STMT (t), args, complain, in_decl); 21836 stmt_expr = finish_stmt_expr (stmt_expr, false); 21837 cur_stmt_expr = old_stmt_expr; 21838 21839 /* If the resulting list of expression statement is empty, 21840 fold it further into void_node. */ 21841 if (empty_expr_stmt_p (stmt_expr)) 21842 stmt_expr = void_node; 21843 21844 RETURN (stmt_expr); 21845 } 21846 21847 case LAMBDA_EXPR: 21848 { 21849 if (complain & tf_partial) 21850 { 21851 /* We don't have a full set of template arguments yet; don't touch 21852 the lambda at all. */ 21853 gcc_assert (processing_template_decl); 21854 return t; 21855 } 21856 tree r = tsubst_lambda_expr (t, args, complain, in_decl); 21857 21858 RETURN (build_lambda_object (r)); 21859 } 21860 21861 case TRANSACTION_EXPR: 21862 gcc_checking_assert (!TRANSACTION_EXPR_IS_STMT (t)); 21863 RETURN (tsubst_stmt (t, args, complain, in_decl)); 21864 21865 case PAREN_EXPR: 21866 if (REF_PARENTHESIZED_P (t)) 21867 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0)))); 21868 else 21869 /* Recreate the PAREN_EXPR from __builtin_assoc_barrier. */ 21870 { 21871 tree op0 = RECUR (TREE_OPERAND (t, 0)); 21872 RETURN (build1_loc (input_location, PAREN_EXPR, 21873 TREE_TYPE (op0), op0)); 21874 } 21875 21876 case VEC_PERM_EXPR: 21877 { 21878 tree op0 = RECUR (TREE_OPERAND (t, 0)); 21879 tree op1 = RECUR (TREE_OPERAND (t, 1)); 21880 tree op2 = RECUR (TREE_OPERAND (t, 2)); 21881 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2, 21882 complain)); 21883 } 21884 21885 case REQUIRES_EXPR: 21886 { 21887 complain &= ~tf_warning_or_error; 21888 tree r = tsubst_requires_expr (t, args, complain, in_decl); 21889 RETURN (r); 21890 } 21891 21892 case RANGE_EXPR: 21893 /* No need to substitute further, a RANGE_EXPR will always be built 21894 with constant operands. */ 21895 RETURN (t); 21896 21897 case NON_LVALUE_EXPR: 21898 case VIEW_CONVERT_EXPR: 21899 { 21900 tree op = RECUR (TREE_OPERAND (t, 0)); 21901 21902 if (location_wrapper_p (t)) 21903 /* We need to do this here as well as in tsubst_copy so we get the 21904 other tsubst_copy_and_build semantics for a PARM_DECL operand. */ 21905 RETURN (maybe_wrap_with_location (op, EXPR_LOCATION (t))); 21906 21907 gcc_checking_assert (TREE_CODE (t) == VIEW_CONVERT_EXPR); 21908 if (REF_PARENTHESIZED_P (t)) 21909 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */ 21910 RETURN (finish_parenthesized_expr (op)); 21911 21912 /* Otherwise, we're dealing with a wrapper to make a C++20 template 21913 parameter object const. */ 21914 if (TREE_TYPE (op) == NULL_TREE 21915 || !CP_TYPE_CONST_P (TREE_TYPE (op))) 21916 { 21917 /* The template argument is not const, presumably because 21918 it is still dependent, and so not the const template parm 21919 object. */ 21920 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); 21921 if (TREE_CODE (op) == CONSTRUCTOR 21922 || TREE_CODE (op) == IMPLICIT_CONV_EXPR) 21923 { 21924 /* Don't add a wrapper to these. */ 21925 op = copy_node (op); 21926 TREE_TYPE (op) = type; 21927 } 21928 else 21929 /* Do add a wrapper otherwise (in particular, if op is 21930 another TEMPLATE_PARM_INDEX). */ 21931 op = build1 (VIEW_CONVERT_EXPR, type, op); 21932 } 21933 RETURN (op); 21934 } 21935 21936 default: 21937 /* Handle Objective-C++ constructs, if appropriate. */ 21938 if (tree subst = objcp_tsubst_expr (t, args, complain, in_decl)) 21939 RETURN (subst); 21940 21941 /* We shouldn't get here, but keep going if !flag_checking. */ 21942 if (flag_checking) 21943 gcc_unreachable (); 21944 RETURN (t); 21945 } 21946 21947 #undef RECUR 21948 #undef RETURN 21949 out: 21950 input_location = save_loc; 21951 return retval; 21952 } 21953 21954 /* Verify that the instantiated ARGS are valid. For type arguments, 21955 make sure that the type's linkage is ok. For non-type arguments, 21956 make sure they are constants if they are integral or enumerations. 21957 Emit an error under control of COMPLAIN, and return TRUE on error. */ 21958 21959 static bool 21960 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain) 21961 { 21962 if (dependent_template_arg_p (t)) 21963 return false; 21964 if (ARGUMENT_PACK_P (t)) 21965 { 21966 tree vec = ARGUMENT_PACK_ARGS (t); 21967 int len = TREE_VEC_LENGTH (vec); 21968 bool result = false; 21969 int i; 21970 21971 for (i = 0; i < len; ++i) 21972 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain)) 21973 result = true; 21974 return result; 21975 } 21976 else if (TYPE_P (t)) 21977 { 21978 /* [basic.link]: A name with no linkage (notably, the name 21979 of a class or enumeration declared in a local scope) 21980 shall not be used to declare an entity with linkage. 21981 This implies that names with no linkage cannot be used as 21982 template arguments 21983 21984 DR 757 relaxes this restriction for C++0x. */ 21985 tree nt = (cxx_dialect > cxx98 ? NULL_TREE 21986 : no_linkage_check (t, /*relaxed_p=*/false)); 21987 21988 if (nt) 21989 { 21990 /* DR 488 makes use of a type with no linkage cause 21991 type deduction to fail. */ 21992 if (complain & tf_error) 21993 { 21994 if (TYPE_UNNAMED_P (nt)) 21995 error ("%qT is/uses unnamed type", t); 21996 else 21997 error ("template argument for %qD uses local type %qT", 21998 tmpl, t); 21999 } 22000 return true; 22001 } 22002 /* In order to avoid all sorts of complications, we do not 22003 allow variably-modified types as template arguments. */ 22004 else if (variably_modified_type_p (t, NULL_TREE)) 22005 { 22006 if (complain & tf_error) 22007 error ("%qT is a variably modified type", t); 22008 return true; 22009 } 22010 } 22011 /* Class template and alias template arguments should be OK. */ 22012 else if (DECL_TYPE_TEMPLATE_P (t)) 22013 ; 22014 /* A non-type argument of integral or enumerated type must be a 22015 constant. */ 22016 else if (TREE_TYPE (t) 22017 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)) 22018 && !REFERENCE_REF_P (t) 22019 && !TREE_CONSTANT (t)) 22020 { 22021 if (complain & tf_error) 22022 error ("integral expression %qE is not constant", t); 22023 return true; 22024 } 22025 return false; 22026 } 22027 22028 static bool 22029 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain) 22030 { 22031 int ix, len = DECL_NTPARMS (tmpl); 22032 bool result = false; 22033 22034 for (ix = 0; ix != len; ix++) 22035 { 22036 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain)) 22037 result = true; 22038 } 22039 if (result && (complain & tf_error)) 22040 error (" trying to instantiate %qD", tmpl); 22041 return result; 22042 } 22043 22044 /* Call mark_used on each entity within the non-type template arguments in 22045 ARGS for an instantiation of TMPL, to ensure that each such entity is 22046 considered odr-used (and therefore marked for instantiation) regardless of 22047 whether the specialization was first formed in a template context (which 22048 inhibits mark_used). 22049 22050 This function assumes push_to_top_level has been called beforehand. */ 22051 22052 static void 22053 mark_template_arguments_used (tree tmpl, tree args) 22054 { 22055 /* It suffices to do this only when instantiating a primary template. */ 22056 if (TREE_CODE (tmpl) != TEMPLATE_DECL || !PRIMARY_TEMPLATE_P (tmpl)) 22057 return; 22058 22059 /* We already marked outer arguments when specializing the context. */ 22060 args = INNERMOST_TEMPLATE_ARGS (args); 22061 22062 for (tree arg : tree_vec_range (args)) 22063 { 22064 /* A (pointer/reference to) function or variable NTTP argument. */ 22065 if (TREE_CODE (arg) == ADDR_EXPR 22066 || TREE_CODE (arg) == INDIRECT_REF) 22067 { 22068 while (TREE_CODE (arg) == ADDR_EXPR 22069 || REFERENCE_REF_P (arg) 22070 || CONVERT_EXPR_P (arg)) 22071 arg = TREE_OPERAND (arg, 0); 22072 if (VAR_OR_FUNCTION_DECL_P (arg)) 22073 { 22074 /* Pass tf_none to avoid duplicate diagnostics: if this call 22075 fails then an earlier call to mark_used for this argument 22076 must have also failed and emitted a diagnostic. */ 22077 bool ok = mark_used (arg, tf_none); 22078 gcc_checking_assert (ok || seen_error ()); 22079 } 22080 } 22081 /* A member function pointer. */ 22082 else if (TREE_CODE (arg) == PTRMEM_CST) 22083 { 22084 bool ok = mark_used (PTRMEM_CST_MEMBER (arg), tf_none); 22085 gcc_checking_assert (ok || seen_error ()); 22086 } 22087 /* A class NTTP argument. */ 22088 else if (VAR_P (arg) 22089 && DECL_NTTP_OBJECT_P (arg)) 22090 { 22091 auto mark_used_r = [](tree *tp, int *, void *) { 22092 if (VAR_OR_FUNCTION_DECL_P (*tp)) 22093 { 22094 bool ok = mark_used (*tp, tf_none); 22095 gcc_checking_assert (ok || seen_error ()); 22096 } 22097 return NULL_TREE; 22098 }; 22099 cp_walk_tree_without_duplicates (&DECL_INITIAL (arg), 22100 mark_used_r, nullptr); 22101 } 22102 } 22103 } 22104 22105 /* We're out of SFINAE context now, so generate diagnostics for the access 22106 errors we saw earlier when instantiating D from TMPL and ARGS. */ 22107 22108 static void 22109 recheck_decl_substitution (tree d, tree tmpl, tree args) 22110 { 22111 tree pattern = DECL_TEMPLATE_RESULT (tmpl); 22112 tree type = TREE_TYPE (pattern); 22113 location_t loc = input_location; 22114 22115 push_access_scope (d); 22116 push_deferring_access_checks (dk_no_deferred); 22117 input_location = DECL_SOURCE_LOCATION (pattern); 22118 tsubst (type, args, tf_warning_or_error, d); 22119 input_location = loc; 22120 pop_deferring_access_checks (); 22121 pop_access_scope (d); 22122 } 22123 22124 /* Instantiate the indicated variable, function, or alias template TMPL with 22125 the template arguments in TARG_PTR. */ 22126 22127 tree 22128 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain) 22129 { 22130 auto_timevar tv (TV_TEMPLATE_INST); 22131 22132 tree targ_ptr = orig_args; 22133 tree fndecl; 22134 tree gen_tmpl; 22135 bool access_ok = true; 22136 22137 if (tmpl == error_mark_node) 22138 return error_mark_node; 22139 22140 /* The other flags are not relevant anymore here, especially tf_partial 22141 shouldn't be set. For instance, we may be called while doing a partial 22142 substitution of a template variable, but the type of the variable 22143 template may be auto, in which case we will call do_auto_deduction 22144 in mark_used (which clears tf_partial) and the auto must be properly 22145 reduced at that time for the deduction to work. */ 22146 complain &= tf_warning_or_error; 22147 22148 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); 22149 22150 if (modules_p ()) 22151 lazy_load_pendings (tmpl); 22152 22153 /* If this function is a clone, handle it specially. */ 22154 if (DECL_CLONED_FUNCTION_P (tmpl)) 22155 { 22156 tree spec; 22157 tree clone; 22158 22159 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have 22160 DECL_CLONED_FUNCTION. */ 22161 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl), 22162 targ_ptr, complain); 22163 if (spec == error_mark_node) 22164 return error_mark_node; 22165 22166 /* Look for the clone. */ 22167 FOR_EACH_CLONE (clone, spec) 22168 if (DECL_NAME (clone) == DECL_NAME (tmpl)) 22169 return clone; 22170 /* We should always have found the clone by now. */ 22171 gcc_unreachable (); 22172 return NULL_TREE; 22173 } 22174 22175 if (targ_ptr == error_mark_node) 22176 return error_mark_node; 22177 22178 /* Check to see if we already have this specialization. */ 22179 gen_tmpl = most_general_template (tmpl); 22180 if (TMPL_ARGS_DEPTH (targ_ptr) 22181 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))) 22182 /* targ_ptr only has the innermost template args, so add the outer ones 22183 from tmpl, which could be either a partial instantiation or gen_tmpl (in 22184 the case of a non-dependent call within a template definition). */ 22185 targ_ptr = (add_outermost_template_args 22186 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)), 22187 targ_ptr)); 22188 22189 hashval_t hash = spec_hasher::hash (gen_tmpl, targ_ptr); 22190 tree spec = retrieve_specialization (gen_tmpl, targ_ptr, hash); 22191 22192 gcc_checking_assert (tmpl == gen_tmpl 22193 || ((fndecl 22194 = retrieve_specialization (tmpl, orig_args, 0)) 22195 == spec) 22196 || fndecl == NULL_TREE); 22197 22198 if (spec != NULL_TREE) 22199 { 22200 if (FNDECL_HAS_ACCESS_ERRORS (spec)) 22201 { 22202 if (complain & tf_error) 22203 recheck_decl_substitution (spec, gen_tmpl, targ_ptr); 22204 return error_mark_node; 22205 } 22206 return spec; 22207 } 22208 22209 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr), 22210 complain)) 22211 return error_mark_node; 22212 22213 /* We are building a FUNCTION_DECL, during which the access of its 22214 parameters and return types have to be checked. However this 22215 FUNCTION_DECL which is the desired context for access checking 22216 is not built yet. We solve this chicken-and-egg problem by 22217 deferring all checks until we have the FUNCTION_DECL. */ 22218 push_deferring_access_checks (dk_deferred); 22219 22220 /* Instantiation of the function happens in the context of the function 22221 template, not the context of the overload resolution we're doing. */ 22222 push_to_top_level (); 22223 /* If there are dependent arguments, e.g. because we're doing partial 22224 ordering, make sure processing_template_decl stays set. */ 22225 if (uses_template_parms (targ_ptr)) 22226 ++processing_template_decl; 22227 if (DECL_CLASS_SCOPE_P (gen_tmpl)) 22228 { 22229 tree ctx; 22230 if (!uses_template_parms (DECL_CONTEXT (tmpl))) 22231 /* If the context of the partially instantiated template is 22232 already non-dependent, then we might as well use it. */ 22233 ctx = DECL_CONTEXT (tmpl); 22234 else 22235 ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr, 22236 complain, gen_tmpl, true); 22237 push_nested_class (ctx); 22238 } 22239 22240 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl); 22241 22242 tree partial_ti = NULL_TREE; 22243 fndecl = NULL_TREE; 22244 if (VAR_P (pattern)) 22245 { 22246 /* We need to determine if we're using a partial or explicit 22247 specialization now, because the type of the variable could be 22248 different. */ 22249 tree tid = build2 (TEMPLATE_ID_EXPR, NULL_TREE, tmpl, targ_ptr); 22250 partial_ti = most_specialized_partial_spec (tid, complain); 22251 if (partial_ti == error_mark_node) 22252 pattern = error_mark_node; 22253 else if (partial_ti) 22254 { 22255 tree partial_tmpl = TI_TEMPLATE (partial_ti); 22256 tree partial_args = TI_ARGS (partial_ti); 22257 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl); 22258 fndecl = tsubst_decl (partial_pat, partial_args, complain, 22259 /*use_spec_table=*/false); 22260 } 22261 } 22262 22263 /* Substitute template parameters to obtain the specialization. */ 22264 if (fndecl == NULL_TREE) 22265 fndecl = tsubst_decl (pattern, targ_ptr, complain, /*use_spec_table=*/false); 22266 if (DECL_CLASS_SCOPE_P (gen_tmpl)) 22267 pop_nested_class (); 22268 pop_from_top_level (); 22269 22270 if (fndecl == error_mark_node) 22271 { 22272 pop_deferring_access_checks (); 22273 return error_mark_node; 22274 } 22275 22276 /* Substituting the type might have recursively instantiated this 22277 same alias (c++/117530). */ 22278 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl) 22279 && (spec = retrieve_specialization (gen_tmpl, targ_ptr, hash))) 22280 { 22281 pop_deferring_access_checks (); 22282 return spec; 22283 } 22284 22285 /* The DECL_TI_TEMPLATE should always be the immediate parent 22286 template, not the most general template. */ 22287 DECL_TI_TEMPLATE (fndecl) = tmpl; 22288 DECL_TI_ARGS (fndecl) = targ_ptr; 22289 if (VAR_P (pattern)) 22290 { 22291 /* Now that we we've formed this variable template specialization, 22292 remember the result of most_specialized_partial_spec for it. */ 22293 TI_PARTIAL_INFO (DECL_TEMPLATE_INFO (fndecl)) = partial_ti; 22294 22295 /* And remember if the variable was declared with []. */ 22296 if (TREE_CODE (TREE_TYPE (fndecl)) == ARRAY_TYPE 22297 && TYPE_DOMAIN (TREE_TYPE (fndecl)) == NULL_TREE) 22298 SET_VAR_HAD_UNKNOWN_BOUND (fndecl); 22299 } 22300 22301 fndecl = register_specialization (fndecl, gen_tmpl, targ_ptr, false, hash); 22302 if (fndecl == error_mark_node) 22303 return error_mark_node; 22304 22305 set_instantiating_module (fndecl); 22306 22307 /* Now we know the specialization, compute access previously 22308 deferred. Do no access control for inheriting constructors, 22309 as we already checked access for the inherited constructor. */ 22310 if (!(flag_new_inheriting_ctors 22311 && DECL_INHERITED_CTOR (fndecl))) 22312 { 22313 push_access_scope (fndecl); 22314 if (!perform_deferred_access_checks (complain)) 22315 access_ok = false; 22316 pop_access_scope (fndecl); 22317 } 22318 pop_deferring_access_checks (); 22319 22320 /* If we've just instantiated the main entry point for a function, 22321 instantiate all the alternate entry points as well. We do this 22322 by cloning the instantiation of the main entry point, not by 22323 instantiating the template clones. */ 22324 if (tree chain = DECL_CHAIN (gen_tmpl)) 22325 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain)) 22326 clone_cdtor (fndecl, /*update_methods=*/false); 22327 22328 if (!access_ok) 22329 { 22330 if (!(complain & tf_error)) 22331 { 22332 /* Remember to reinstantiate when we're out of SFINAE so the user 22333 can see the errors. */ 22334 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true; 22335 } 22336 return error_mark_node; 22337 } 22338 22339 return fndecl; 22340 } 22341 22342 /* Instantiate the alias template TMPL with ARGS. Also push a template 22343 instantiation level, which instantiate_template doesn't do because 22344 functions and variables have sufficient context established by the 22345 callers. */ 22346 22347 static tree 22348 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain) 22349 { 22350 if (tmpl == error_mark_node || args == error_mark_node) 22351 return error_mark_node; 22352 22353 args = coerce_template_parms (DECL_TEMPLATE_PARMS (tmpl), 22354 args, tmpl, complain); 22355 if (args == error_mark_node) 22356 return error_mark_node; 22357 22358 /* FIXME check for satisfaction in check_instantiated_args. */ 22359 if (!constraints_satisfied_p (tmpl, args)) 22360 { 22361 if (complain & tf_error) 22362 { 22363 auto_diagnostic_group d; 22364 error ("template constraint failure for %qD", tmpl); 22365 diagnose_constraints (input_location, tmpl, args); 22366 } 22367 return error_mark_node; 22368 } 22369 22370 if (!push_tinst_level (tmpl, args)) 22371 return error_mark_node; 22372 tree r = instantiate_template (tmpl, args, complain); 22373 pop_tinst_level (); 22374 22375 if (tree d = dependent_alias_template_spec_p (TREE_TYPE (r), nt_opaque)) 22376 { 22377 /* An alias template specialization can be dependent 22378 even if its underlying type is not. */ 22379 TYPE_DEPENDENT_P (d) = true; 22380 TYPE_DEPENDENT_P_VALID (d) = true; 22381 /* Sometimes a dependent alias spec is equivalent to its expansion, 22382 sometimes not. So always use structural_comptypes. */ 22383 SET_TYPE_STRUCTURAL_EQUALITY (d); 22384 } 22385 22386 return r; 22387 } 22388 22389 /* PARM is a template parameter pack for FN. Returns true iff 22390 PARM is used in a deducible way in the argument list of FN. */ 22391 22392 static bool 22393 pack_deducible_p (tree parm, tree fn) 22394 { 22395 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn); 22396 for (; t; t = TREE_CHAIN (t)) 22397 { 22398 tree type = TREE_VALUE (t); 22399 tree packs; 22400 if (!PACK_EXPANSION_P (type)) 22401 continue; 22402 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type); 22403 packs; packs = TREE_CHAIN (packs)) 22404 if (template_args_equal (TREE_VALUE (packs), parm)) 22405 { 22406 /* The template parameter pack is used in a function parameter 22407 pack. If this is the end of the parameter list, the 22408 template parameter pack is deducible. */ 22409 if (TREE_CHAIN (t) == void_list_node) 22410 return true; 22411 else 22412 /* Otherwise, not. Well, it could be deduced from 22413 a non-pack parameter, but doing so would end up with 22414 a deduction mismatch, so don't bother. */ 22415 return false; 22416 } 22417 } 22418 /* The template parameter pack isn't used in any function parameter 22419 packs, but it might be used deeper, e.g. tuple<Args...>. */ 22420 return true; 22421 } 22422 22423 /* Subroutine of fn_type_unification: check non-dependent parms for 22424 convertibility. */ 22425 22426 static int 22427 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs, 22428 tree fn, unification_kind_t strict, int flags, 22429 struct conversion **convs, bool explain_p, 22430 bool noninst_only_p) 22431 { 22432 /* Non-constructor methods need to leave a conversion for 'this', which 22433 isn't included in nargs here. */ 22434 unsigned offset = (DECL_IOBJ_MEMBER_FUNCTION_P (fn) 22435 && !DECL_CONSTRUCTOR_P (fn)); 22436 22437 for (unsigned ia = 0; 22438 parms && parms != void_list_node && ia < nargs; ) 22439 { 22440 tree parm = TREE_VALUE (parms); 22441 22442 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION 22443 && (!TREE_CHAIN (parms) 22444 || TREE_CHAIN (parms) == void_list_node)) 22445 /* For a function parameter pack that occurs at the end of the 22446 parameter-declaration-list, the type A of each remaining 22447 argument of the call is compared with the type P of the 22448 declarator-id of the function parameter pack. */ 22449 break; 22450 22451 parms = TREE_CHAIN (parms); 22452 22453 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION) 22454 /* For a function parameter pack that does not occur at the 22455 end of the parameter-declaration-list, the type of the 22456 parameter pack is a non-deduced context. */ 22457 continue; 22458 22459 if (!uses_template_parms (parm)) 22460 { 22461 tree arg = args[ia]; 22462 conversion **conv_p = convs ? &convs[ia+offset] : NULL; 22463 int lflags = conv_flags (ia, nargs, fn, arg, flags); 22464 22465 if (check_non_deducible_conversion (parm, arg, strict, lflags, 22466 conv_p, explain_p, noninst_only_p)) 22467 return 1; 22468 } 22469 22470 ++ia; 22471 } 22472 22473 return 0; 22474 } 22475 22476 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with 22477 NARGS elements of the arguments that are being used when calling 22478 it. TARGS is a vector into which the deduced template arguments 22479 are placed. 22480 22481 Returns either a FUNCTION_DECL for the matching specialization of FN or 22482 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is 22483 true, diagnostics will be printed to explain why it failed. 22484 22485 If FN is a conversion operator, or we are trying to produce a specific 22486 specialization, RETURN_TYPE is the return type desired. 22487 22488 The EXPLICIT_TARGS are explicit template arguments provided via a 22489 template-id. 22490 22491 The parameter STRICT is one of: 22492 22493 DEDUCE_CALL: 22494 We are deducing arguments for a function call, as in 22495 [temp.deduct.call]. If RETURN_TYPE is non-null, we are 22496 deducing arguments for a call to the result of a conversion 22497 function template, as in [over.call.object]. 22498 22499 DEDUCE_CONV: 22500 We are deducing arguments for a conversion function, as in 22501 [temp.deduct.conv]. 22502 22503 DEDUCE_EXACT: 22504 We are deducing arguments when doing an explicit instantiation 22505 as in [temp.explicit], when determining an explicit specialization 22506 as in [temp.expl.spec], or when taking the address of a function 22507 template, as in [temp.deduct.funcaddr]. */ 22508 22509 tree 22510 fn_type_unification (tree fn, 22511 tree explicit_targs, 22512 tree targs, 22513 const tree *args, 22514 unsigned int nargs, 22515 tree return_type, 22516 unification_kind_t strict, 22517 int flags, 22518 struct conversion **convs, 22519 bool explain_p, 22520 bool decltype_p) 22521 { 22522 tree parms; 22523 tree fntype; 22524 tree decl = NULL_TREE; 22525 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none); 22526 bool ok; 22527 static int deduction_depth; 22528 /* type_unification_real will pass back any access checks from default 22529 template argument substitution. */ 22530 vec<deferred_access_check, va_gc> *checks = NULL; 22531 /* We don't have all the template args yet. */ 22532 bool incomplete = true; 22533 22534 tree orig_fn = fn; 22535 if (flag_new_inheriting_ctors) 22536 fn = strip_inheriting_ctors (fn); 22537 22538 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn); 22539 tree r = error_mark_node; 22540 22541 tree full_targs = targs; 22542 if (TMPL_ARGS_DEPTH (targs) 22543 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn))) 22544 full_targs = (add_outermost_template_args 22545 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)), 22546 targs)); 22547 22548 if (decltype_p) 22549 complain |= tf_decltype; 22550 22551 /* In C++0x, it's possible to have a function template whose type depends 22552 on itself recursively. This is most obvious with decltype, but can also 22553 occur with enumeration scope (c++/48969). So we need to catch infinite 22554 recursion and reject the substitution at deduction time; this function 22555 will return error_mark_node for any repeated substitution. 22556 22557 This also catches excessive recursion such as when f<N> depends on 22558 f<N-1> across all integers, and returns error_mark_node for all the 22559 substitutions back up to the initial one. 22560 22561 This is, of course, not reentrant. */ 22562 if (excessive_deduction_depth) 22563 return error_mark_node; 22564 ++deduction_depth; 22565 22566 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL); 22567 22568 fntype = TREE_TYPE (fn); 22569 if (explicit_targs) 22570 { 22571 /* [temp.deduct] 22572 22573 The specified template arguments must match the template 22574 parameters in kind (i.e., type, nontype, template), and there 22575 must not be more arguments than there are parameters; 22576 otherwise type deduction fails. 22577 22578 Nontype arguments must match the types of the corresponding 22579 nontype template parameters, or must be convertible to the 22580 types of the corresponding nontype parameters as specified in 22581 _temp.arg.nontype_, otherwise type deduction fails. 22582 22583 All references in the function type of the function template 22584 to the corresponding template parameters are replaced by the 22585 specified template argument values. If a substitution in a 22586 template parameter or in the function type of the function 22587 template results in an invalid type, type deduction fails. */ 22588 int i, len = TREE_VEC_LENGTH (tparms); 22589 location_t loc = input_location; 22590 incomplete = false; 22591 22592 if (explicit_targs == error_mark_node) 22593 goto fail; 22594 22595 if (TMPL_ARGS_DEPTH (explicit_targs) 22596 < TMPL_ARGS_DEPTH (full_targs)) 22597 explicit_targs = add_outermost_template_args (full_targs, 22598 explicit_targs); 22599 22600 /* Adjust any explicit template arguments before entering the 22601 substitution context. */ 22602 explicit_targs 22603 = (coerce_template_parms (tparms, explicit_targs, fn, 22604 complain|tf_partial, 22605 /*require_all_args=*/false)); 22606 if (explicit_targs == error_mark_node) 22607 goto fail; 22608 22609 /* Substitute the explicit args into the function type. This is 22610 necessary so that, for instance, explicitly declared function 22611 arguments can match null pointed constants. If we were given 22612 an incomplete set of explicit args, we must not do semantic 22613 processing during substitution as we could create partial 22614 instantiations. */ 22615 for (i = 0; i < len; i++) 22616 { 22617 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); 22618 bool parameter_pack = false; 22619 tree targ = TREE_VEC_ELT (explicit_targs, i); 22620 22621 /* Dig out the actual parm. */ 22622 if (TREE_CODE (parm) == TYPE_DECL 22623 || TREE_CODE (parm) == TEMPLATE_DECL) 22624 { 22625 parm = TREE_TYPE (parm); 22626 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm); 22627 } 22628 else if (TREE_CODE (parm) == PARM_DECL) 22629 { 22630 parm = DECL_INITIAL (parm); 22631 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm); 22632 } 22633 22634 if (targ == NULL_TREE) 22635 /* No explicit argument for this template parameter. */ 22636 incomplete = true; 22637 else if (parameter_pack && pack_deducible_p (parm, fn)) 22638 { 22639 /* Mark the argument pack as "incomplete". We could 22640 still deduce more arguments during unification. 22641 We remove this mark in type_unification_real. */ 22642 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1; 22643 ARGUMENT_PACK_EXPLICIT_ARGS (targ) 22644 = ARGUMENT_PACK_ARGS (targ); 22645 22646 /* We have some incomplete argument packs. */ 22647 incomplete = true; 22648 } 22649 } 22650 22651 if (incomplete) 22652 { 22653 if (!push_tinst_level (fn, explicit_targs)) 22654 { 22655 excessive_deduction_depth = true; 22656 goto fail; 22657 } 22658 ++processing_template_decl; 22659 input_location = DECL_SOURCE_LOCATION (fn); 22660 /* Ignore any access checks; we'll see them again in 22661 instantiate_template and they might have the wrong 22662 access path at this point. */ 22663 push_deferring_access_checks (dk_deferred); 22664 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type; 22665 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE); 22666 pop_deferring_access_checks (); 22667 input_location = loc; 22668 --processing_template_decl; 22669 pop_tinst_level (); 22670 22671 if (fntype == error_mark_node) 22672 goto fail; 22673 } 22674 22675 /* Place the explicitly specified arguments in TARGS. */ 22676 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs); 22677 for (i = NUM_TMPL_ARGS (explicit_targs); i--;) 22678 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i); 22679 if (!incomplete && CHECKING_P 22680 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs)) 22681 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT 22682 (targs, NUM_TMPL_ARGS (explicit_targs)); 22683 } 22684 22685 if (return_type && strict != DEDUCE_CALL) 22686 { 22687 tree *new_args = XALLOCAVEC (tree, nargs + 1); 22688 new_args[0] = return_type; 22689 memcpy (new_args + 1, args, nargs * sizeof (tree)); 22690 args = new_args; 22691 ++nargs; 22692 } 22693 22694 if (!incomplete) 22695 goto deduced; 22696 22697 /* Never do unification on the 'this' parameter. */ 22698 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype)); 22699 22700 if (return_type && strict == DEDUCE_CALL) 22701 { 22702 /* We're deducing for a call to the result of a template conversion 22703 function. The parms we really want are in return_type. */ 22704 if (INDIRECT_TYPE_P (return_type)) 22705 return_type = TREE_TYPE (return_type); 22706 parms = TYPE_ARG_TYPES (return_type); 22707 } 22708 else if (return_type) 22709 { 22710 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); 22711 } 22712 22713 /* We allow incomplete unification without an error message here 22714 because the standard doesn't seem to explicitly prohibit it. Our 22715 callers must be ready to deal with unification failures in any 22716 event. */ 22717 22718 /* If we aren't explaining yet, push tinst context so we can see where 22719 any errors (e.g. from class instantiations triggered by instantiation 22720 of default template arguments) come from. If we are explaining, this 22721 context is redundant. */ 22722 if (!explain_p && !push_tinst_level (fn, targs)) 22723 { 22724 excessive_deduction_depth = true; 22725 goto fail; 22726 } 22727 22728 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), 22729 full_targs, parms, args, nargs, /*subr=*/0, 22730 strict, &checks, explain_p); 22731 if (!explain_p) 22732 pop_tinst_level (); 22733 if (!ok) 22734 goto fail; 22735 22736 /* Now that we have bindings for all of the template arguments, 22737 ensure that the arguments deduced for the template template 22738 parameters have compatible template parameter lists. We cannot 22739 check this property before we have deduced all template 22740 arguments, because the template parameter types of a template 22741 template parameter might depend on prior template parameters 22742 deduced after the template template parameter. The following 22743 ill-formed example illustrates this issue: 22744 22745 template<typename T, template<T> class C> void f(C<5>, T); 22746 22747 template<int N> struct X {}; 22748 22749 void g() { 22750 f(X<5>(), 5l); // error: template argument deduction fails 22751 } 22752 22753 The template parameter list of 'C' depends on the template type 22754 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to 22755 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the 22756 time that we deduce 'C'. */ 22757 if (!template_template_parm_bindings_ok_p 22758 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs)) 22759 { 22760 unify_inconsistent_template_template_parameters (explain_p); 22761 goto fail; 22762 } 22763 22764 deduced: 22765 22766 /* As a refinement of CWG2369, check first and foremost non-dependent 22767 conversions that we know are not going to induce template instantiation 22768 (PR99599). */ 22769 if (strict == DEDUCE_CALL 22770 && incomplete 22771 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags, 22772 convs, explain_p, 22773 /*noninst_only_p=*/true)) 22774 goto fail; 22775 22776 /* CWG2369: Check satisfaction before non-deducible conversions. */ 22777 if (!constraints_satisfied_p (fn, targs)) 22778 { 22779 if (explain_p) 22780 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs); 22781 goto fail; 22782 } 22783 22784 /* DR 1391: All parameters have args, now check non-dependent parms for 22785 convertibility. We don't do this if all args were explicitly specified, 22786 as the standard says that we substitute explicit args immediately. */ 22787 if (incomplete 22788 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags, 22789 convs, explain_p, 22790 /*noninst_only_p=*/false)) 22791 goto fail; 22792 22793 /* All is well so far. Now, check: 22794 22795 [temp.deduct] 22796 22797 When all template arguments have been deduced, all uses of 22798 template parameters in nondeduced contexts are replaced with 22799 the corresponding deduced argument values. If the 22800 substitution results in an invalid type, as described above, 22801 type deduction fails. */ 22802 if (!push_tinst_level (fn, targs)) 22803 { 22804 excessive_deduction_depth = true; 22805 goto fail; 22806 } 22807 22808 /* Also collect access checks from the instantiation. */ 22809 reopen_deferring_access_checks (checks); 22810 22811 decl = instantiate_template (fn, targs, complain); 22812 22813 checks = get_deferred_access_checks (); 22814 pop_deferring_access_checks (); 22815 22816 pop_tinst_level (); 22817 22818 if (decl == error_mark_node) 22819 goto fail; 22820 22821 /* Now perform any access checks encountered during substitution. */ 22822 push_access_scope (decl); 22823 ok = perform_access_checks (checks, complain); 22824 pop_access_scope (decl); 22825 if (!ok) 22826 goto fail; 22827 22828 /* If we're looking for an exact match, check that what we got 22829 is indeed an exact match. It might not be if some template 22830 parameters are used in non-deduced contexts. But don't check 22831 for an exact match if we have dependent template arguments; 22832 in that case we're doing partial ordering, and we already know 22833 that we have two candidates that will provide the actual type. */ 22834 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs)) 22835 { 22836 tree substed = TREE_TYPE (decl); 22837 unsigned int i; 22838 22839 tree sarg 22840 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed)); 22841 if (return_type) 22842 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg); 22843 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg)) 22844 if (!same_type_p (args[i], TREE_VALUE (sarg))) 22845 { 22846 unify_type_mismatch (explain_p, args[i], 22847 TREE_VALUE (sarg)); 22848 goto fail; 22849 } 22850 if ((i < nargs || sarg) 22851 /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args 22852 doesn't contain the trailing void, and conv fns are always (). */ 22853 && !DECL_CONV_FN_P (decl)) 22854 { 22855 unsigned nsargs = i + list_length (sarg); 22856 unify_arity (explain_p, nargs, nsargs); 22857 goto fail; 22858 } 22859 } 22860 22861 /* After doing deduction with the inherited constructor, actually return an 22862 instantiation of the inheriting constructor. */ 22863 if (orig_fn != fn) 22864 decl = instantiate_template (orig_fn, targs, complain); 22865 22866 r = decl; 22867 22868 fail: 22869 --deduction_depth; 22870 if (excessive_deduction_depth) 22871 { 22872 if (deduction_depth == 0) 22873 /* Reset once we're all the way out. */ 22874 excessive_deduction_depth = false; 22875 } 22876 22877 return r; 22878 } 22879 22880 /* Returns true iff PARM is a forwarding reference in the context of 22881 template argument deduction for TMPL. */ 22882 22883 static bool 22884 forwarding_reference_p (tree parm, tree tmpl) 22885 { 22886 /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a 22887 cv-unqualified template parameter ..." */ 22888 if (TYPE_REF_P (parm) 22889 && TYPE_REF_IS_RVALUE (parm) 22890 && TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM 22891 && cp_type_quals (TREE_TYPE (parm)) == TYPE_UNQUALIFIED) 22892 { 22893 parm = TREE_TYPE (parm); 22894 /* [temp.deduct.call], "... that does not represent a template parameter 22895 of a class template (during class template argument deduction)." */ 22896 if (tmpl 22897 && deduction_guide_p (tmpl) 22898 && DECL_ARTIFICIAL (tmpl)) 22899 { 22900 /* Since the template parameters of a synthesized guide consist of 22901 the template parameters of the class template followed by those of 22902 the constructor (if any), we can tell if PARM represents a template 22903 parameter of the class template by comparing its index with the 22904 arity of the class template. */ 22905 tree ctmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl))); 22906 if (TEMPLATE_TYPE_IDX (parm) 22907 < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl))) 22908 return false; 22909 } 22910 return true; 22911 } 22912 return false; 22913 } 22914 22915 /* Adjust types before performing type deduction, as described in 22916 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two 22917 sections are symmetric. PARM is the type of a function parameter 22918 or the return type of the conversion function. ARG is the type of 22919 the argument passed to the call, or the type of the value 22920 initialized with the result of the conversion function. 22921 ARG_EXPR is the original argument expression, which may be null. */ 22922 22923 static int 22924 maybe_adjust_types_for_deduction (tree tparms, 22925 unification_kind_t strict, 22926 tree* parm, 22927 tree* arg, 22928 tree arg_expr) 22929 { 22930 int result = 0; 22931 22932 switch (strict) 22933 { 22934 case DEDUCE_CALL: 22935 break; 22936 22937 case DEDUCE_CONV: 22938 /* [temp.deduct.conv] First remove a reference type on parm. 22939 DRs 322 & 976 affected this. */ 22940 if (TYPE_REF_P (*parm)) 22941 *parm = TREE_TYPE (*parm); 22942 22943 /* Swap PARM and ARG throughout the remainder of this 22944 function; the handling is precisely symmetric since PARM 22945 will initialize ARG rather than vice versa. */ 22946 std::swap (parm, arg); 22947 22948 break; 22949 22950 case DEDUCE_EXACT: 22951 /* Core issue #873: Do the DR606 thing (see below) for these cases, 22952 too, but here handle it by stripping the reference from PARM 22953 rather than by adding it to ARG. */ 22954 if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms)) 22955 && TYPE_REF_P (*arg) 22956 && !TYPE_REF_IS_RVALUE (*arg)) 22957 *parm = TREE_TYPE (*parm); 22958 /* Nothing else to do in this case. */ 22959 return 0; 22960 22961 default: 22962 gcc_unreachable (); 22963 } 22964 22965 if (!TYPE_REF_P (*parm)) 22966 { 22967 /* [temp.deduct.call] 22968 22969 If P is not a reference type: 22970 22971 --If A is an array type, the pointer type produced by the 22972 array-to-pointer standard conversion (_conv.array_) is 22973 used in place of A for type deduction; otherwise, 22974 22975 --If A is a function type, the pointer type produced by 22976 the function-to-pointer standard conversion 22977 (_conv.func_) is used in place of A for type deduction; 22978 otherwise, 22979 22980 --If A is a cv-qualified type, the top level 22981 cv-qualifiers of A's type are ignored for type 22982 deduction. */ 22983 if (TREE_CODE (*arg) == ARRAY_TYPE) 22984 *arg = build_pointer_type (TREE_TYPE (*arg)); 22985 else if (TREE_CODE (*arg) == FUNCTION_TYPE) 22986 *arg = build_pointer_type (*arg); 22987 else 22988 *arg = TYPE_MAIN_VARIANT (*arg); 22989 } 22990 22991 /* [temp.deduct.call], "If P is a forwarding reference and the argument is 22992 an lvalue, the type 'lvalue reference to A' is used in place of A for 22993 type deduction." */ 22994 if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms)) 22995 && (arg_expr ? lvalue_p (arg_expr) 22996 /* try_one_overload doesn't provide an arg_expr, but 22997 functions are always lvalues. */ 22998 : TREE_CODE (*arg) == FUNCTION_TYPE)) 22999 *arg = build_reference_type (*arg); 23000 23001 /* [temp.deduct.call] 23002 23003 If P is a cv-qualified type, the top level cv-qualifiers 23004 of P's type are ignored for type deduction. If P is a 23005 reference type, the type referred to by P is used for 23006 type deduction. */ 23007 *parm = TYPE_MAIN_VARIANT (*parm); 23008 if (TYPE_REF_P (*parm)) 23009 { 23010 *parm = TREE_TYPE (*parm); 23011 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL; 23012 } 23013 23014 return result; 23015 } 23016 23017 /* Return true if computing a conversion from FROM to TO might induce template 23018 instantiation. Conversely, if this predicate returns false then computing 23019 the conversion definitely won't induce template instantiation. */ 23020 23021 static bool 23022 conversion_may_instantiate_p (tree to, tree from) 23023 { 23024 to = non_reference (to); 23025 from = non_reference (from); 23026 23027 bool ptr_conv_p = false; 23028 if (TYPE_PTR_P (to) 23029 && TYPE_PTR_P (from)) 23030 { 23031 to = TREE_TYPE (to); 23032 from = TREE_TYPE (from); 23033 ptr_conv_p = true; 23034 } 23035 23036 /* If one of the types is a not-yet-instantiated class template 23037 specialization, then computing the conversion might instantiate 23038 it in order to inspect bases, conversion functions and/or 23039 converting constructors. */ 23040 if ((CLASS_TYPE_P (to) 23041 && !COMPLETE_TYPE_P (to) 23042 && CLASSTYPE_TEMPLATE_INSTANTIATION (to)) 23043 || (CLASS_TYPE_P (from) 23044 && !COMPLETE_TYPE_P (from) 23045 && CLASSTYPE_TEMPLATE_INSTANTIATION (from))) 23046 return true; 23047 23048 /* Converting from one pointer type to another, or between 23049 reference-related types, always yields a standard conversion. */ 23050 if (ptr_conv_p || reference_related_p (to, from)) 23051 return false; 23052 23053 /* Converting to a non-aggregate class type will consider its 23054 user-declared constructors, which might induce instantiation. */ 23055 if (CLASS_TYPE_P (to) 23056 && CLASSTYPE_NON_AGGREGATE (to)) 23057 return true; 23058 23059 /* Similarly, converting from a class type will consider its conversion 23060 functions. */ 23061 if (CLASS_TYPE_P (from) 23062 && TYPE_HAS_CONVERSION (from)) 23063 return true; 23064 23065 /* Otherwise, computing this conversion definitely won't induce 23066 template instantiation. */ 23067 return false; 23068 } 23069 23070 /* Subroutine of fn_type_unification. PARM is a function parameter of a 23071 template which doesn't contain any deducible template parameters; check if 23072 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in 23073 unify_one_argument. */ 23074 23075 static int 23076 check_non_deducible_conversion (tree parm, tree arg, unification_kind_t strict, 23077 int flags, struct conversion **conv_p, 23078 bool explain_p, bool noninst_only_p) 23079 { 23080 tree type; 23081 23082 if (!TYPE_P (arg)) 23083 type = TREE_TYPE (arg); 23084 else 23085 type = arg; 23086 23087 if (same_type_p (parm, type)) 23088 return unify_success (explain_p); 23089 23090 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none); 23091 if (strict == DEDUCE_CONV) 23092 { 23093 if (can_convert_arg (type, parm, NULL_TREE, flags, complain)) 23094 return unify_success (explain_p); 23095 } 23096 else if (strict == DEDUCE_CALL) 23097 { 23098 if (conv_p && *conv_p) 23099 { 23100 /* This conversion was already computed earlier (when 23101 computing only non-instantiating conversions). */ 23102 gcc_checking_assert (!noninst_only_p); 23103 return unify_success (explain_p); 23104 } 23105 23106 if (noninst_only_p 23107 && conversion_may_instantiate_p (parm, type)) 23108 return unify_success (explain_p); 23109 23110 bool ok = false; 23111 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg; 23112 if (conv_p) 23113 /* Avoid recalculating this in add_function_candidate. */ 23114 ok = (*conv_p 23115 = good_conversion (parm, type, conv_arg, flags, complain)); 23116 else 23117 ok = can_convert_arg (parm, type, conv_arg, flags, complain); 23118 if (ok) 23119 return unify_success (explain_p); 23120 } 23121 23122 if (strict == DEDUCE_EXACT) 23123 return unify_type_mismatch (explain_p, parm, arg); 23124 else 23125 return unify_arg_conversion (explain_p, parm, type, arg); 23126 } 23127 23128 static bool uses_deducible_template_parms (tree type); 23129 23130 /* Returns true iff the expression EXPR is one from which a template 23131 argument can be deduced. In other words, if it's an undecorated 23132 use of a template non-type parameter. */ 23133 23134 static bool 23135 deducible_expression (tree expr) 23136 { 23137 /* Strip implicit conversions and implicit INDIRECT_REFs. */ 23138 while (CONVERT_EXPR_P (expr) 23139 || TREE_CODE (expr) == VIEW_CONVERT_EXPR 23140 || (TREE_CODE (expr) == IMPLICIT_CONV_EXPR 23141 && IMPLICIT_CONV_EXPR_FORCED (expr)) 23142 || REFERENCE_REF_P (expr)) 23143 expr = TREE_OPERAND (expr, 0); 23144 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX); 23145 } 23146 23147 /* Returns true iff the array domain DOMAIN uses a template parameter in a 23148 deducible way; that is, if it has a max value of <PARM> - 1. */ 23149 23150 static bool 23151 deducible_array_bound (tree domain) 23152 { 23153 if (domain == NULL_TREE) 23154 return false; 23155 23156 tree max = TYPE_MAX_VALUE (domain); 23157 if (TREE_CODE (max) != MINUS_EXPR) 23158 return false; 23159 23160 return deducible_expression (TREE_OPERAND (max, 0)); 23161 } 23162 23163 /* Returns true iff the template arguments ARGS use a template parameter 23164 in a deducible way. */ 23165 23166 static bool 23167 deducible_template_args (tree args) 23168 { 23169 for (tree elt : tree_vec_range (args)) 23170 { 23171 bool deducible; 23172 if (ARGUMENT_PACK_P (elt)) 23173 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt)); 23174 else 23175 { 23176 if (PACK_EXPANSION_P (elt)) 23177 elt = PACK_EXPANSION_PATTERN (elt); 23178 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM) 23179 deducible = true; 23180 else if (TYPE_P (elt)) 23181 deducible = uses_deducible_template_parms (elt); 23182 else 23183 deducible = deducible_expression (elt); 23184 } 23185 if (deducible) 23186 return true; 23187 } 23188 return false; 23189 } 23190 23191 /* Returns true iff TYPE contains any deducible references to template 23192 parameters, as per 14.8.2.5. */ 23193 23194 static bool 23195 uses_deducible_template_parms (tree type) 23196 { 23197 if (PACK_EXPANSION_P (type)) 23198 type = PACK_EXPANSION_PATTERN (type); 23199 23200 /* T 23201 cv-list T 23202 TT<T> 23203 TT<i> 23204 TT<> */ 23205 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM 23206 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) 23207 return true; 23208 23209 /* T* 23210 T& 23211 T&& */ 23212 if (INDIRECT_TYPE_P (type)) 23213 return uses_deducible_template_parms (TREE_TYPE (type)); 23214 23215 /* T[integer-constant ] 23216 type [i] */ 23217 if (TREE_CODE (type) == ARRAY_TYPE) 23218 return (uses_deducible_template_parms (TREE_TYPE (type)) 23219 || deducible_array_bound (TYPE_DOMAIN (type))); 23220 23221 /* T type ::* 23222 type T::* 23223 T T::* 23224 T (type ::*)() 23225 type (T::*)() 23226 type (type ::*)(T) 23227 type (T::*)(T) 23228 T (type ::*)(T) 23229 T (T::*)() 23230 T (T::*)(T) */ 23231 if (TYPE_PTRMEM_P (type)) 23232 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type)) 23233 || (uses_deducible_template_parms 23234 (TYPE_PTRMEM_POINTED_TO_TYPE (type)))); 23235 23236 /* template-name <T> (where template-name refers to a class template) 23237 template-name <i> (where template-name refers to a class template) */ 23238 if (CLASS_TYPE_P (type) 23239 && CLASSTYPE_TEMPLATE_INFO (type) 23240 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))) 23241 return deducible_template_args (INNERMOST_TEMPLATE_ARGS 23242 (CLASSTYPE_TI_ARGS (type))); 23243 23244 /* type (T) 23245 T() 23246 T(T) */ 23247 if (FUNC_OR_METHOD_TYPE_P (type)) 23248 { 23249 if (uses_deducible_template_parms (TREE_TYPE (type))) 23250 return true; 23251 tree parm = TYPE_ARG_TYPES (type); 23252 if (TREE_CODE (type) == METHOD_TYPE) 23253 parm = TREE_CHAIN (parm); 23254 for (; parm; parm = TREE_CHAIN (parm)) 23255 if (uses_deducible_template_parms (TREE_VALUE (parm))) 23256 return true; 23257 if (flag_noexcept_type 23258 && TYPE_RAISES_EXCEPTIONS (type) 23259 && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type)) 23260 && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type)))) 23261 return true; 23262 } 23263 23264 return false; 23265 } 23266 23267 /* Subroutine of type_unification_real and unify_pack_expansion to 23268 handle unification of a single P/A pair. Parameters are as 23269 for those functions. */ 23270 23271 static int 23272 unify_one_argument (tree tparms, tree targs, tree parm, tree arg, 23273 int subr, unification_kind_t strict, 23274 bool explain_p) 23275 { 23276 tree arg_expr = NULL_TREE; 23277 int arg_strict; 23278 23279 if (arg == error_mark_node || parm == error_mark_node) 23280 return unify_invalid (explain_p); 23281 if (arg == unknown_type_node) 23282 /* We can't deduce anything from this, but we might get all the 23283 template args from other function args. */ 23284 return unify_success (explain_p); 23285 23286 /* Implicit conversions (Clause 4) will be performed on a function 23287 argument to convert it to the type of the corresponding function 23288 parameter if the parameter type contains no template-parameters that 23289 participate in template argument deduction. */ 23290 if (strict != DEDUCE_EXACT 23291 && TYPE_P (parm) && !uses_deducible_template_parms (parm)) 23292 /* For function parameters with no deducible template parameters, 23293 just return. We'll check non-dependent conversions later. */ 23294 return unify_success (explain_p); 23295 23296 switch (strict) 23297 { 23298 case DEDUCE_CALL: 23299 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL 23300 | UNIFY_ALLOW_MORE_CV_QUAL 23301 | UNIFY_ALLOW_DERIVED); 23302 break; 23303 23304 case DEDUCE_CONV: 23305 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL; 23306 break; 23307 23308 case DEDUCE_EXACT: 23309 arg_strict = UNIFY_ALLOW_NONE; 23310 break; 23311 23312 default: 23313 gcc_unreachable (); 23314 } 23315 23316 /* We only do these transformations if this is the top-level 23317 parameter_type_list in a call or declaration matching; in other 23318 situations (nested function declarators, template argument lists) we 23319 won't be comparing a type to an expression, and we don't do any type 23320 adjustments. */ 23321 if (!subr) 23322 { 23323 if (!TYPE_P (arg)) 23324 { 23325 gcc_assert (TREE_TYPE (arg) != NULL_TREE); 23326 if (type_unknown_p (arg)) 23327 { 23328 /* [temp.deduct.type] A template-argument can be 23329 deduced from a pointer to function or pointer 23330 to member function argument if the set of 23331 overloaded functions does not contain function 23332 templates and at most one of a set of 23333 overloaded functions provides a unique 23334 match. */ 23335 resolve_overloaded_unification (tparms, targs, parm, 23336 arg, strict, 23337 arg_strict, explain_p); 23338 /* If a unique match was not found, this is a 23339 non-deduced context, so we still succeed. */ 23340 return unify_success (explain_p); 23341 } 23342 23343 arg_expr = arg; 23344 arg = unlowered_expr_type (arg); 23345 if (arg == error_mark_node) 23346 return unify_invalid (explain_p); 23347 } 23348 23349 arg_strict |= maybe_adjust_types_for_deduction (tparms, strict, 23350 &parm, &arg, arg_expr); 23351 } 23352 else 23353 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL) 23354 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)) 23355 return unify_template_argument_mismatch (explain_p, parm, arg); 23356 23357 /* For deduction from an init-list we need the actual list. */ 23358 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr)) 23359 arg = arg_expr; 23360 return unify (tparms, targs, parm, arg, arg_strict, explain_p); 23361 } 23362 23363 /* for_each_template_parm callback that always returns 0. */ 23364 23365 static int 23366 zero_r (tree, void *) 23367 { 23368 return 0; 23369 } 23370 23371 /* for_each_template_parm any_fn callback to handle deduction of a template 23372 type argument from the type of an array bound. */ 23373 23374 static int 23375 array_deduction_r (tree t, void *data) 23376 { 23377 tree_pair_p d = (tree_pair_p)data; 23378 tree &tparms = d->purpose; 23379 tree &targs = d->value; 23380 23381 if (TREE_CODE (t) == ARRAY_TYPE) 23382 if (tree dom = TYPE_DOMAIN (t)) 23383 if (tree max = TYPE_MAX_VALUE (dom)) 23384 { 23385 if (TREE_CODE (max) == MINUS_EXPR) 23386 max = TREE_OPERAND (max, 0); 23387 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX) 23388 unify (tparms, targs, TREE_TYPE (max), size_type_node, 23389 UNIFY_ALLOW_NONE, /*explain*/false); 23390 } 23391 23392 /* Keep walking. */ 23393 return 0; 23394 } 23395 23396 /* Try to deduce any not-yet-deduced template type arguments from the type of 23397 an array bound. This is handled separately from unify because 14.8.2.5 says 23398 "The type of a type parameter is only deduced from an array bound if it is 23399 not otherwise deduced." */ 23400 23401 static void 23402 try_array_deduction (tree tparms, tree targs, tree parm) 23403 { 23404 tree_pair_s data = { tparms, targs }; 23405 hash_set<tree> visited; 23406 for_each_template_parm (parm, zero_r, &data, &visited, 23407 /*nondeduced*/false, array_deduction_r); 23408 } 23409 23410 /* Most parms like fn_type_unification. 23411 23412 If SUBR is 1, we're being called recursively (to unify the 23413 arguments of a function or method parameter of a function 23414 template). 23415 23416 CHECKS is a pointer to a vector of access checks encountered while 23417 substituting default template arguments. */ 23418 23419 static int 23420 type_unification_real (tree tparms, 23421 tree full_targs, 23422 tree xparms, 23423 const tree *xargs, 23424 unsigned int xnargs, 23425 int subr, 23426 unification_kind_t strict, 23427 vec<deferred_access_check, va_gc> **checks, 23428 bool explain_p) 23429 { 23430 tree parm, arg; 23431 int i; 23432 int ntparms = TREE_VEC_LENGTH (tparms); 23433 int saw_undeduced = 0; 23434 tree parms; 23435 const tree *args; 23436 unsigned int nargs; 23437 unsigned int ia; 23438 23439 gcc_assert (TREE_CODE (tparms) == TREE_VEC); 23440 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST); 23441 gcc_assert (ntparms > 0); 23442 23443 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs); 23444 23445 /* Reset the number of non-defaulted template arguments contained 23446 in TARGS. */ 23447 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE; 23448 23449 again: 23450 parms = xparms; 23451 args = xargs; 23452 nargs = xnargs; 23453 23454 /* Only fn_type_unification cares about terminal void. */ 23455 if (nargs && args[nargs-1] == void_type_node) 23456 --nargs; 23457 23458 ia = 0; 23459 while (parms && parms != void_list_node 23460 && ia < nargs) 23461 { 23462 parm = TREE_VALUE (parms); 23463 23464 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION 23465 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node)) 23466 /* For a function parameter pack that occurs at the end of the 23467 parameter-declaration-list, the type A of each remaining 23468 argument of the call is compared with the type P of the 23469 declarator-id of the function parameter pack. */ 23470 break; 23471 23472 parms = TREE_CHAIN (parms); 23473 23474 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION) 23475 /* For a function parameter pack that does not occur at the 23476 end of the parameter-declaration-list, the type of the 23477 parameter pack is a non-deduced context. */ 23478 continue; 23479 23480 /* [temp.deduct.conv] only applies to the deduction of the return 23481 type, which is always the first argument here. Other arguments 23482 (notably, explicit object parameters) should undergo normal 23483 call-like unification. */ 23484 unification_kind_t kind = strict; 23485 if (strict == DEDUCE_CONV && ia > 0) 23486 kind = DEDUCE_CALL; 23487 23488 arg = args[ia]; 23489 ++ia; 23490 23491 if (unify_one_argument (tparms, full_targs, parm, arg, subr, kind, 23492 explain_p)) 23493 return 1; 23494 } 23495 23496 if (parms 23497 && parms != void_list_node 23498 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) 23499 { 23500 gcc_assert (strict != DEDUCE_CONV); 23501 23502 /* Unify the remaining arguments with the pack expansion type. */ 23503 tree argvec; 23504 tree parmvec = make_tree_vec (1); 23505 23506 /* Allocate a TREE_VEC and copy in all of the arguments */ 23507 argvec = make_tree_vec (nargs - ia); 23508 for (i = 0; ia < nargs; ++ia, ++i) 23509 TREE_VEC_ELT (argvec, i) = args[ia]; 23510 23511 /* Copy the parameter into parmvec. */ 23512 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms); 23513 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict, 23514 /*subr=*/subr, explain_p)) 23515 return 1; 23516 23517 /* Advance to the end of the list of parameters. */ 23518 parms = TREE_CHAIN (parms); 23519 } 23520 23521 /* Fail if we've reached the end of the parm list, and more args 23522 are present, and the parm list isn't variadic. */ 23523 if (ia < nargs && parms == void_list_node) 23524 return unify_too_many_arguments (explain_p, nargs, ia); 23525 /* Fail if parms are left and they don't have default values and 23526 they aren't all deduced as empty packs (c++/57397). This is 23527 consistent with sufficient_parms_p. */ 23528 if (parms && parms != void_list_node 23529 && TREE_PURPOSE (parms) == NULL_TREE) 23530 { 23531 unsigned int count = nargs; 23532 tree p = parms; 23533 bool type_pack_p; 23534 do 23535 { 23536 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION; 23537 if (!type_pack_p) 23538 count++; 23539 p = TREE_CHAIN (p); 23540 } 23541 while (p && p != void_list_node); 23542 if (count != nargs) 23543 return unify_too_few_arguments (explain_p, ia, count, 23544 type_pack_p); 23545 } 23546 23547 if (!subr) 23548 { 23549 tsubst_flags_t complain = (explain_p 23550 ? tf_warning_or_error 23551 : tf_none); 23552 bool tried_array_deduction = (cxx_dialect < cxx17); 23553 23554 for (i = 0; i < ntparms; i++) 23555 { 23556 tree targ = TREE_VEC_ELT (targs, i); 23557 tree tparm = TREE_VEC_ELT (tparms, i); 23558 23559 /* Clear the "incomplete" flags on all argument packs now so that 23560 substituting them into later default arguments works. */ 23561 if (targ && ARGUMENT_PACK_P (targ)) 23562 { 23563 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0; 23564 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE; 23565 } 23566 23567 if (targ || tparm == error_mark_node) 23568 continue; 23569 tparm = TREE_VALUE (tparm); 23570 23571 if (TREE_CODE (tparm) == TYPE_DECL 23572 && !tried_array_deduction) 23573 { 23574 try_array_deduction (tparms, targs, xparms); 23575 tried_array_deduction = true; 23576 if (TREE_VEC_ELT (targs, i)) 23577 continue; 23578 } 23579 23580 /* If this is an undeduced nontype parameter that depends on 23581 a type parameter, try another pass; its type may have been 23582 deduced from a later argument than the one from which 23583 this parameter can be deduced. */ 23584 if (TREE_CODE (tparm) == PARM_DECL 23585 && !is_auto (TREE_TYPE (tparm)) 23586 && uses_template_parms (TREE_TYPE (tparm)) 23587 && saw_undeduced < 2) 23588 { 23589 saw_undeduced = 1; 23590 continue; 23591 } 23592 23593 /* Core issue #226 (C++0x) [temp.deduct]: 23594 23595 If a template argument has not been deduced, its 23596 default template argument, if any, is used. 23597 23598 When we are in C++98 mode, TREE_PURPOSE will either 23599 be NULL_TREE or ERROR_MARK_NODE, so we do not need 23600 to explicitly check cxx_dialect here. */ 23601 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i))) 23602 /* OK, there is a default argument. Wait until after the 23603 conversion check to do substitution. */ 23604 continue; 23605 23606 /* If the type parameter is a parameter pack, then it will 23607 be deduced to an empty parameter pack. */ 23608 if (template_parameter_pack_p (tparm)) 23609 { 23610 tree arg; 23611 23612 if (TREE_CODE (tparm) == PARM_DECL) 23613 { 23614 arg = make_node (NONTYPE_ARGUMENT_PACK); 23615 TREE_CONSTANT (arg) = 1; 23616 } 23617 else 23618 arg = cxx_make_type (TYPE_ARGUMENT_PACK); 23619 23620 ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0); 23621 23622 TREE_VEC_ELT (targs, i) = arg; 23623 continue; 23624 } 23625 23626 return unify_parameter_deduction_failure (explain_p, tparm); 23627 } 23628 23629 /* During partial ordering, we deduce dependent template args. */ 23630 bool any_dependent_targs = false; 23631 23632 /* Now substitute into the default template arguments. */ 23633 for (i = 0; i < ntparms; i++) 23634 { 23635 tree targ = TREE_VEC_ELT (targs, i); 23636 tree tparm = TREE_VEC_ELT (tparms, i); 23637 23638 if (targ) 23639 { 23640 if (!any_dependent_targs && dependent_template_arg_p (targ)) 23641 any_dependent_targs = true; 23642 continue; 23643 } 23644 if (tparm == error_mark_node) 23645 continue; 23646 23647 tree parm = TREE_VALUE (tparm); 23648 tree arg = TREE_PURPOSE (tparm); 23649 reopen_deferring_access_checks (*checks); 23650 location_t save_loc = input_location; 23651 if (DECL_P (parm)) 23652 input_location = DECL_SOURCE_LOCATION (parm); 23653 23654 if (saw_undeduced == 1 23655 && TREE_CODE (parm) == PARM_DECL 23656 && !is_auto (TREE_TYPE (parm)) 23657 && uses_template_parms (TREE_TYPE (parm))) 23658 { 23659 /* The type of this non-type parameter depends on undeduced 23660 parameters. Don't try to use its default argument yet, 23661 since we might deduce an argument for it on the next pass, 23662 but do check whether the arguments we already have cause 23663 substitution failure, so that that happens before we try 23664 later default arguments (78489). */ 23665 ++processing_template_decl; 23666 tree type = tsubst (TREE_TYPE (parm), full_targs, complain, 23667 NULL_TREE); 23668 --processing_template_decl; 23669 if (type == error_mark_node) 23670 arg = error_mark_node; 23671 else 23672 arg = NULL_TREE; 23673 } 23674 else 23675 { 23676 /* Even if the call is happening in template context, getting 23677 here means it's non-dependent, and a default argument is 23678 considered a separate definition under [temp.decls], so we can 23679 do this substitution without processing_template_decl. This 23680 is important if the default argument contains something that 23681 might be instantiation-dependent like access (87480). */ 23682 processing_template_decl_sentinel s (!any_dependent_targs); 23683 tree substed = NULL_TREE; 23684 if (saw_undeduced == 1 && !any_dependent_targs) 23685 { 23686 /* First instatiate in template context, in case we still 23687 depend on undeduced template parameters. */ 23688 ++processing_template_decl; 23689 substed = tsubst_template_arg (arg, full_targs, complain, 23690 NULL_TREE); 23691 --processing_template_decl; 23692 if (substed != error_mark_node 23693 && !uses_template_parms (substed)) 23694 /* We replaced all the tparms, substitute again out of 23695 template context. */ 23696 substed = NULL_TREE; 23697 else 23698 processing_template_decl = 1; 23699 } 23700 if (!substed) 23701 substed = tsubst_template_arg (arg, full_targs, complain, 23702 NULL_TREE); 23703 23704 if (!processing_template_decl || !uses_template_parms (substed)) 23705 arg = convert_template_argument (parm, substed, full_targs, 23706 complain, i, NULL_TREE); 23707 else if (saw_undeduced == 1) 23708 arg = NULL_TREE; 23709 else if (!any_dependent_targs) 23710 arg = error_mark_node; 23711 } 23712 23713 input_location = save_loc; 23714 *checks = get_deferred_access_checks (); 23715 pop_deferring_access_checks (); 23716 23717 if (arg == error_mark_node) 23718 return 1; 23719 else if (arg) 23720 { 23721 TREE_VEC_ELT (targs, i) = arg; 23722 /* The position of the first default template argument, 23723 is also the number of non-defaulted arguments in TARGS. 23724 Record that. */ 23725 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs)) 23726 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i); 23727 } 23728 } 23729 23730 if (saw_undeduced++ == 1) 23731 goto again; 23732 } 23733 23734 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs)) 23735 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs)); 23736 23737 return unify_success (explain_p); 23738 } 23739 23740 /* Subroutine of type_unification_real. Args are like the variables 23741 at the call site. ARG is an overloaded function (or template-id); 23742 we try deducing template args from each of the overloads, and if 23743 only one succeeds, we go with that. Modifies TARGS and returns 23744 true on success. */ 23745 23746 static bool 23747 resolve_overloaded_unification (tree tparms, 23748 tree targs, 23749 tree parm, 23750 tree arg, 23751 unification_kind_t strict, 23752 int sub_strict, 23753 bool explain_p) 23754 { 23755 tree tempargs = copy_node (targs); 23756 int good = 0; 23757 tree goodfn = NULL_TREE; 23758 bool addr_p; 23759 23760 if (TREE_CODE (arg) == ADDR_EXPR) 23761 { 23762 arg = TREE_OPERAND (arg, 0); 23763 addr_p = true; 23764 } 23765 else 23766 addr_p = false; 23767 23768 if (TREE_CODE (arg) == COMPONENT_REF) 23769 /* Handle `&x' where `x' is some static or non-static member 23770 function name. */ 23771 arg = TREE_OPERAND (arg, 1); 23772 23773 if (TREE_CODE (arg) == OFFSET_REF) 23774 arg = TREE_OPERAND (arg, 1); 23775 23776 /* Strip baselink information. */ 23777 if (BASELINK_P (arg)) 23778 arg = BASELINK_FUNCTIONS (arg); 23779 23780 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) 23781 { 23782 /* If we got some explicit template args, we need to plug them into 23783 the affected templates before we try to unify, in case the 23784 explicit args will completely resolve the templates in question. */ 23785 23786 int ok = 0; 23787 tree expl_subargs = TREE_OPERAND (arg, 1); 23788 arg = TREE_OPERAND (arg, 0); 23789 23790 for (lkp_iterator iter (arg); iter; ++iter) 23791 { 23792 tree fn = *iter; 23793 tree subargs, elem; 23794 23795 if (TREE_CODE (fn) != TEMPLATE_DECL) 23796 continue; 23797 23798 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), 23799 expl_subargs, NULL_TREE, tf_none); 23800 if (subargs != error_mark_node 23801 && !any_dependent_template_arguments_p (subargs)) 23802 { 23803 fn = instantiate_template (fn, subargs, tf_none); 23804 if (!constraints_satisfied_p (fn)) 23805 continue; 23806 if (undeduced_auto_decl (fn)) 23807 { 23808 /* Instantiate the function to deduce its return type. */ 23809 ++function_depth; 23810 instantiate_decl (fn, /*defer*/false, /*class*/false); 23811 --function_depth; 23812 } 23813 23814 if (flag_noexcept_type) 23815 maybe_instantiate_noexcept (fn, tf_none); 23816 23817 elem = TREE_TYPE (fn); 23818 if (try_one_overload (tparms, targs, tempargs, parm, 23819 elem, strict, sub_strict, addr_p, explain_p) 23820 && (!goodfn || !same_type_p (goodfn, elem))) 23821 { 23822 goodfn = elem; 23823 ++good; 23824 } 23825 } 23826 else if (subargs) 23827 ++ok; 23828 } 23829 /* If no templates (or more than one) are fully resolved by the 23830 explicit arguments, this template-id is a non-deduced context; it 23831 could still be OK if we deduce all template arguments for the 23832 enclosing call through other arguments. */ 23833 if (good != 1) 23834 good = ok; 23835 } 23836 else if (!OVL_P (arg)) 23837 /* If ARG is, for example, "(0, &f)" then its type will be unknown 23838 -- but the deduction does not succeed because the expression is 23839 not just the function on its own. */ 23840 return false; 23841 else 23842 for (lkp_iterator iter (arg); iter; ++iter) 23843 { 23844 tree fn = *iter; 23845 if (flag_noexcept_type) 23846 maybe_instantiate_noexcept (fn, tf_none); 23847 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn), 23848 strict, sub_strict, addr_p, explain_p) 23849 && (!goodfn || !decls_match (goodfn, fn))) 23850 { 23851 goodfn = fn; 23852 ++good; 23853 } 23854 } 23855 23856 /* [temp.deduct.type] A template-argument can be deduced from a pointer 23857 to function or pointer to member function argument if the set of 23858 overloaded functions does not contain function templates and at most 23859 one of a set of overloaded functions provides a unique match. 23860 23861 So if we found multiple possibilities, we return success but don't 23862 deduce anything. */ 23863 23864 if (good == 1) 23865 { 23866 int i = TREE_VEC_LENGTH (targs); 23867 for (; i--; ) 23868 if (TREE_VEC_ELT (tempargs, i)) 23869 { 23870 tree old = TREE_VEC_ELT (targs, i); 23871 tree new_ = TREE_VEC_ELT (tempargs, i); 23872 if (new_ && old && ARGUMENT_PACK_P (old) 23873 && ARGUMENT_PACK_EXPLICIT_ARGS (old)) 23874 /* Don't forget explicit template arguments in a pack. */ 23875 ARGUMENT_PACK_EXPLICIT_ARGS (new_) 23876 = ARGUMENT_PACK_EXPLICIT_ARGS (old); 23877 TREE_VEC_ELT (targs, i) = new_; 23878 } 23879 } 23880 if (good) 23881 return true; 23882 23883 return false; 23884 } 23885 23886 /* Core DR 115: In contexts where deduction is done and fails, or in 23887 contexts where deduction is not done, if a template argument list is 23888 specified and it, along with any default template arguments, identifies 23889 a single function template specialization, then the template-id is an 23890 lvalue for the function template specialization. */ 23891 23892 tree 23893 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain) 23894 { 23895 tree expr, offset, baselink; 23896 bool addr; 23897 23898 if (!type_unknown_p (orig_expr)) 23899 return orig_expr; 23900 23901 expr = orig_expr; 23902 addr = false; 23903 offset = NULL_TREE; 23904 baselink = NULL_TREE; 23905 23906 if (TREE_CODE (expr) == ADDR_EXPR) 23907 { 23908 expr = TREE_OPERAND (expr, 0); 23909 addr = true; 23910 } 23911 if (TREE_CODE (expr) == OFFSET_REF) 23912 { 23913 offset = expr; 23914 expr = TREE_OPERAND (expr, 1); 23915 } 23916 if (BASELINK_P (expr)) 23917 { 23918 baselink = expr; 23919 expr = BASELINK_FUNCTIONS (expr); 23920 } 23921 23922 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR) 23923 { 23924 int good = 0; 23925 tree goodfn = NULL_TREE; 23926 23927 /* If we got some explicit template args, we need to plug them into 23928 the affected templates before we try to unify, in case the 23929 explicit args will completely resolve the templates in question. */ 23930 23931 tree expl_subargs = TREE_OPERAND (expr, 1); 23932 tree arg = TREE_OPERAND (expr, 0); 23933 tree badfn = NULL_TREE; 23934 tree badargs = NULL_TREE; 23935 23936 for (lkp_iterator iter (arg); iter; ++iter) 23937 { 23938 tree fn = *iter; 23939 tree subargs, elem; 23940 23941 if (TREE_CODE (fn) != TEMPLATE_DECL) 23942 continue; 23943 23944 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), 23945 expl_subargs, NULL_TREE, tf_none); 23946 if (subargs != error_mark_node 23947 && !any_dependent_template_arguments_p (subargs)) 23948 { 23949 elem = instantiate_template (fn, subargs, tf_none); 23950 if (elem == error_mark_node) 23951 { 23952 badfn = fn; 23953 badargs = subargs; 23954 } 23955 else if (elem && (!goodfn || !decls_match (goodfn, elem)) 23956 && constraints_satisfied_p (elem)) 23957 { 23958 goodfn = elem; 23959 ++good; 23960 } 23961 } 23962 } 23963 if (good == 1) 23964 { 23965 mark_used (goodfn); 23966 expr = goodfn; 23967 if (baselink) 23968 expr = build_baselink (BASELINK_BINFO (baselink), 23969 BASELINK_ACCESS_BINFO (baselink), 23970 expr, BASELINK_OPTYPE (baselink)); 23971 if (offset) 23972 { 23973 tree base 23974 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0))); 23975 expr = build_offset_ref (base, expr, addr, complain); 23976 } 23977 if (addr) 23978 expr = cp_build_addr_expr (expr, complain); 23979 return expr; 23980 } 23981 else if (good == 0 && badargs && (complain & tf_error)) 23982 /* There were no good options and at least one bad one, so let the 23983 user know what the problem is. */ 23984 instantiate_template (badfn, badargs, complain); 23985 } 23986 return orig_expr; 23987 } 23988 23989 /* As above, but error out if the expression remains overloaded. */ 23990 23991 tree 23992 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain) 23993 { 23994 exp = resolve_nondeduced_context (exp, complain); 23995 if (type_unknown_p (exp)) 23996 { 23997 if (complain & tf_error) 23998 cxx_incomplete_type_error (exp, TREE_TYPE (exp)); 23999 return error_mark_node; 24000 } 24001 return exp; 24002 } 24003 24004 /* Subroutine of resolve_overloaded_unification; does deduction for a single 24005 overload. Fills TARGS with any deduced arguments, or error_mark_node if 24006 different overloads deduce different arguments for a given parm. 24007 ADDR_P is true if the expression for which deduction is being 24008 performed was of the form "& fn" rather than simply "fn". 24009 24010 Returns 1 on success. */ 24011 24012 static int 24013 try_one_overload (tree tparms, 24014 tree orig_targs, 24015 tree targs, 24016 tree parm, 24017 tree arg, 24018 unification_kind_t strict, 24019 int sub_strict, 24020 bool addr_p, 24021 bool explain_p) 24022 { 24023 int nargs; 24024 tree tempargs; 24025 int i; 24026 24027 if (arg == error_mark_node) 24028 return 0; 24029 24030 /* [temp.deduct.type] A template-argument can be deduced from a pointer 24031 to function or pointer to member function argument if the set of 24032 overloaded functions does not contain function templates and at most 24033 one of a set of overloaded functions provides a unique match. 24034 24035 So if this is a template, just return success. */ 24036 24037 if (uses_template_parms (arg)) 24038 return 1; 24039 24040 if (TREE_CODE (arg) == METHOD_TYPE) 24041 arg = build_ptrmemfunc_type (build_pointer_type (arg)); 24042 else if (addr_p) 24043 arg = build_pointer_type (arg); 24044 24045 sub_strict |= maybe_adjust_types_for_deduction (tparms, strict, 24046 &parm, &arg, NULL_TREE); 24047 24048 /* We don't copy orig_targs for this because if we have already deduced 24049 some template args from previous args, unify would complain when we 24050 try to deduce a template parameter for the same argument, even though 24051 there isn't really a conflict. */ 24052 nargs = TREE_VEC_LENGTH (targs); 24053 tempargs = make_tree_vec (nargs); 24054 24055 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p)) 24056 return 0; 24057 24058 /* First make sure we didn't deduce anything that conflicts with 24059 explicitly specified args. */ 24060 for (i = nargs; i--; ) 24061 { 24062 tree elt = TREE_VEC_ELT (tempargs, i); 24063 tree oldelt = TREE_VEC_ELT (orig_targs, i); 24064 24065 if (!elt) 24066 /*NOP*/; 24067 else if (uses_template_parms (elt)) 24068 /* Since we're unifying against ourselves, we will fill in 24069 template args used in the function parm list with our own 24070 template parms. Discard them. */ 24071 TREE_VEC_ELT (tempargs, i) = NULL_TREE; 24072 else if (oldelt && ARGUMENT_PACK_P (oldelt)) 24073 { 24074 /* Check that the argument at each index of the deduced argument pack 24075 is equivalent to the corresponding explicitly specified argument. 24076 We may have deduced more arguments than were explicitly specified, 24077 and that's OK. */ 24078 24079 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but 24080 that's wrong if we deduce the same argument pack from multiple 24081 function arguments: it's only incomplete the first time. */ 24082 24083 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt); 24084 tree deduced_pack = ARGUMENT_PACK_ARGS (elt); 24085 24086 if (TREE_VEC_LENGTH (deduced_pack) 24087 < TREE_VEC_LENGTH (explicit_pack)) 24088 return 0; 24089 24090 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++) 24091 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j), 24092 TREE_VEC_ELT (deduced_pack, j))) 24093 return 0; 24094 } 24095 else if (oldelt && !template_args_equal (oldelt, elt)) 24096 return 0; 24097 } 24098 24099 for (i = nargs; i--; ) 24100 { 24101 tree elt = TREE_VEC_ELT (tempargs, i); 24102 24103 if (elt) 24104 TREE_VEC_ELT (targs, i) = elt; 24105 } 24106 24107 return 1; 24108 } 24109 24110 /* PARM is a template class (perhaps with unbound template 24111 parameters). ARG is a fully instantiated type. If ARG can be 24112 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and 24113 TARGS are as for unify. */ 24114 24115 static tree 24116 try_class_unification (tree tparms, tree targs, tree parm, tree arg, 24117 bool explain_p) 24118 { 24119 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) 24120 return NULL_TREE; 24121 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) 24122 /* Matches anything. */; 24123 else if (CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm)) 24124 return NULL_TREE; 24125 24126 /* We need to make a new template argument vector for the call to 24127 unify. If we used TARGS, we'd clutter it up with the result of 24128 the attempted unification, even if this class didn't work out. 24129 We also don't want to commit ourselves to all the unifications 24130 we've already done, since unification is supposed to be done on 24131 an argument-by-argument basis. In other words, consider the 24132 following pathological case: 24133 24134 template <int I, int J, int K> 24135 struct S {}; 24136 24137 template <int I, int J> 24138 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; 24139 24140 template <int I, int J, int K> 24141 void f(S<I, J, K>, S<I, I, I>); 24142 24143 void g() { 24144 S<0, 0, 0> s0; 24145 S<0, 1, 2> s2; 24146 24147 f(s0, s2); 24148 } 24149 24150 Now, by the time we consider the unification involving `s2', we 24151 already know that we must have `f<0, 0, 0>'. But, even though 24152 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid 24153 because there are two ways to unify base classes of S<0, 1, 2> 24154 with S<I, I, I>. If we kept the already deduced knowledge, we 24155 would reject the possibility I=1. */ 24156 targs = copy_template_args (targs); 24157 for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs))) 24158 targ = NULL_TREE; 24159 24160 int err; 24161 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) 24162 err = unify_bound_ttp_args (tparms, targs, parm, arg, explain_p); 24163 else 24164 err = unify (tparms, targs, 24165 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)), 24166 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (arg)), 24167 UNIFY_ALLOW_NONE, explain_p); 24168 24169 return err ? NULL_TREE : arg; 24170 } 24171 24172 /* Given a template type PARM and a class type ARG, find the unique 24173 base type in ARG that is an instance of PARM. We do not examine 24174 ARG itself; only its base-classes. If there is not exactly one 24175 appropriate base class, return NULL_TREE. PARM may be the type of 24176 a partial specialization, as well as a plain template type. Used 24177 by unify. */ 24178 24179 static enum template_base_result 24180 get_template_base (tree tparms, tree targs, tree parm, tree arg, 24181 bool explain_p, tree *result) 24182 { 24183 tree rval = NULL_TREE; 24184 tree binfo; 24185 24186 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg))); 24187 24188 binfo = TYPE_BINFO (complete_type (arg)); 24189 if (!binfo) 24190 { 24191 /* The type could not be completed. */ 24192 *result = NULL_TREE; 24193 return tbr_incomplete_type; 24194 } 24195 24196 /* Walk in inheritance graph order. The search order is not 24197 important, and this avoids multiple walks of virtual bases. */ 24198 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo)) 24199 { 24200 tree r = try_class_unification (tparms, targs, parm, 24201 BINFO_TYPE (binfo), explain_p); 24202 24203 if (r) 24204 { 24205 /* If there is more than one satisfactory baseclass, then: 24206 24207 [temp.deduct.call] 24208 24209 If they yield more than one possible deduced A, the type 24210 deduction fails. 24211 24212 applies. */ 24213 if (rval && !same_type_p (r, rval)) 24214 { 24215 /* [temp.deduct.call]/4.3: If there is a class C that is a 24216 (direct or indirect) base class of D and derived (directly or 24217 indirectly) from a class B and that would be a valid deduced 24218 A, the deduced A cannot be B or pointer to B, respectively. */ 24219 if (DERIVED_FROM_P (r, rval)) 24220 /* Ignore r. */ 24221 continue; 24222 else if (DERIVED_FROM_P (rval, r)) 24223 /* Ignore rval. */; 24224 else 24225 { 24226 *result = NULL_TREE; 24227 return tbr_ambiguous_baseclass; 24228 } 24229 } 24230 24231 rval = r; 24232 } 24233 } 24234 24235 *result = rval; 24236 return tbr_success; 24237 } 24238 24239 /* Returns the level of DECL, which declares a template parameter. */ 24240 24241 static int 24242 template_decl_level (tree decl) 24243 { 24244 switch (TREE_CODE (decl)) 24245 { 24246 case TYPE_DECL: 24247 case TEMPLATE_DECL: 24248 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); 24249 24250 case PARM_DECL: 24251 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); 24252 24253 default: 24254 gcc_unreachable (); 24255 } 24256 return 0; 24257 } 24258 24259 /* Decide whether ARG can be unified with PARM, considering only the 24260 cv-qualifiers of each type, given STRICT as documented for unify. 24261 Returns nonzero iff the unification is OK on that basis. */ 24262 24263 static int 24264 check_cv_quals_for_unify (int strict, tree arg, tree parm) 24265 { 24266 int arg_quals = cp_type_quals (arg); 24267 int parm_quals = cp_type_quals (parm); 24268 24269 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM 24270 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) 24271 { 24272 /* Although a CVR qualifier is ignored when being applied to a 24273 substituted template parameter ([8.3.2]/1 for example), that 24274 does not allow us to unify "const T" with "int&" because both 24275 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type]. 24276 It is ok when we're allowing additional CV qualifiers 24277 at the outer level [14.8.2.1]/3,1st bullet. */ 24278 if ((TYPE_REF_P (arg) 24279 || FUNC_OR_METHOD_TYPE_P (arg)) 24280 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))) 24281 return 0; 24282 24283 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM) 24284 && (parm_quals & TYPE_QUAL_RESTRICT)) 24285 return 0; 24286 } 24287 24288 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) 24289 && (arg_quals & parm_quals) != parm_quals) 24290 return 0; 24291 24292 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL)) 24293 && (parm_quals & arg_quals) != arg_quals) 24294 return 0; 24295 24296 return 1; 24297 } 24298 24299 /* Determines the LEVEL and INDEX for the template parameter PARM. */ 24300 void 24301 template_parm_level_and_index (tree parm, int* level, int* index) 24302 { 24303 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM 24304 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM 24305 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) 24306 { 24307 *index = TEMPLATE_TYPE_IDX (parm); 24308 *level = TEMPLATE_TYPE_LEVEL (parm); 24309 } 24310 else 24311 { 24312 *index = TEMPLATE_PARM_IDX (parm); 24313 *level = TEMPLATE_PARM_LEVEL (parm); 24314 } 24315 } 24316 24317 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \ 24318 do { \ 24319 if (unify (TP, TA, P, A, S, EP)) \ 24320 return 1; \ 24321 } while (0) 24322 24323 /* Unifies the remaining arguments in PACKED_ARGS with the pack 24324 expansion at the end of PACKED_PARMS. Returns 0 if the type 24325 deduction succeeds, 1 otherwise. STRICT is the same as in 24326 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a 24327 function call argument list. We'll need to adjust the arguments to make them 24328 types. SUBR tells us if this is from a recursive call to 24329 type_unification_real, or for comparing two template argument 24330 lists. */ 24331 24332 static int 24333 unify_pack_expansion (tree tparms, tree targs, tree packed_parms, 24334 tree packed_args, unification_kind_t strict, 24335 bool subr, bool explain_p) 24336 { 24337 tree parm 24338 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1); 24339 tree pattern = PACK_EXPANSION_PATTERN (parm); 24340 tree pack, packs = NULL_TREE; 24341 int i, start = TREE_VEC_LENGTH (packed_parms) - 1; 24342 24343 /* Add in any args remembered from an earlier partial instantiation. */ 24344 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs); 24345 int levels = TMPL_ARGS_DEPTH (targs); 24346 24347 packed_args = expand_template_argument_pack (packed_args); 24348 24349 int len = TREE_VEC_LENGTH (packed_args); 24350 24351 /* Determine the parameter packs we will be deducing from the 24352 pattern, and record their current deductions. */ 24353 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm); 24354 pack; pack = TREE_CHAIN (pack)) 24355 { 24356 tree parm_pack = TREE_VALUE (pack); 24357 int idx, level; 24358 24359 /* Only template parameter packs can be deduced, not e.g. function 24360 parameter packs or __bases or __integer_pack. */ 24361 if (!TEMPLATE_PARM_P (parm_pack)) 24362 continue; 24363 24364 /* Determine the index and level of this parameter pack. */ 24365 template_parm_level_and_index (parm_pack, &level, &idx); 24366 if (level > levels) 24367 continue; 24368 24369 /* Keep track of the parameter packs and their corresponding 24370 argument packs. */ 24371 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs); 24372 TREE_TYPE (packs) = make_tree_vec (len - start); 24373 } 24374 24375 /* Loop through all of the arguments that have not yet been 24376 unified and unify each with the pattern. */ 24377 for (i = start; i < len; i++) 24378 { 24379 tree parm; 24380 bool any_explicit = false; 24381 tree arg = TREE_VEC_ELT (packed_args, i); 24382 24383 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE 24384 or the element of its argument pack at the current index if 24385 this argument was explicitly specified. */ 24386 for (pack = packs; pack; pack = TREE_CHAIN (pack)) 24387 { 24388 int idx, level; 24389 tree arg, pargs; 24390 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); 24391 24392 arg = NULL_TREE; 24393 if (TREE_VALUE (pack) 24394 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack))) 24395 && (i - start < TREE_VEC_LENGTH (pargs))) 24396 { 24397 any_explicit = true; 24398 arg = TREE_VEC_ELT (pargs, i - start); 24399 } 24400 TMPL_ARG (targs, level, idx) = arg; 24401 } 24402 24403 /* If we had explicit template arguments, substitute them into the 24404 pattern before deduction. */ 24405 if (any_explicit) 24406 { 24407 /* Some arguments might still be unspecified or dependent. */ 24408 bool dependent; 24409 ++processing_template_decl; 24410 dependent = any_dependent_template_arguments_p (targs); 24411 if (!dependent) 24412 --processing_template_decl; 24413 parm = tsubst (pattern, targs, 24414 explain_p ? tf_warning_or_error : tf_none, 24415 NULL_TREE); 24416 if (dependent) 24417 --processing_template_decl; 24418 if (parm == error_mark_node) 24419 return 1; 24420 } 24421 else 24422 parm = pattern; 24423 24424 /* Unify the pattern with the current argument. */ 24425 if (unify_one_argument (tparms, targs, parm, arg, subr, strict, 24426 explain_p)) 24427 return 1; 24428 24429 /* For each parameter pack, collect the deduced value. */ 24430 for (pack = packs; pack; pack = TREE_CHAIN (pack)) 24431 { 24432 int idx, level; 24433 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); 24434 24435 TREE_VEC_ELT (TREE_TYPE (pack), i - start) = 24436 TMPL_ARG (targs, level, idx); 24437 } 24438 } 24439 24440 /* Verify that the results of unification with the parameter packs 24441 produce results consistent with what we've seen before, and make 24442 the deduced argument packs available. */ 24443 for (pack = packs; pack; pack = TREE_CHAIN (pack)) 24444 { 24445 tree old_pack = TREE_VALUE (pack); 24446 tree new_args = TREE_TYPE (pack); 24447 int i, len = TREE_VEC_LENGTH (new_args); 24448 int idx, level; 24449 bool nondeduced_p = false; 24450 24451 /* By default keep the original deduced argument pack. 24452 If necessary, more specific code is going to update the 24453 resulting deduced argument later down in this function. */ 24454 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx); 24455 TMPL_ARG (targs, level, idx) = old_pack; 24456 24457 /* If NEW_ARGS contains any NULL_TREE entries, we didn't 24458 actually deduce anything. */ 24459 for (i = 0; i < len && !nondeduced_p; ++i) 24460 if (TREE_VEC_ELT (new_args, i) == NULL_TREE) 24461 nondeduced_p = true; 24462 if (nondeduced_p) 24463 continue; 24464 24465 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack)) 24466 { 24467 /* If we had fewer function args than explicit template args, 24468 just use the explicits. */ 24469 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); 24470 int explicit_len = TREE_VEC_LENGTH (explicit_args); 24471 if (len < explicit_len) 24472 new_args = explicit_args; 24473 } 24474 24475 if (!old_pack) 24476 { 24477 tree result; 24478 /* Build the deduced *_ARGUMENT_PACK. */ 24479 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX) 24480 { 24481 result = make_node (NONTYPE_ARGUMENT_PACK); 24482 TREE_CONSTANT (result) = 1; 24483 } 24484 else 24485 result = cxx_make_type (TYPE_ARGUMENT_PACK); 24486 24487 ARGUMENT_PACK_ARGS (result) = new_args; 24488 24489 /* Note the deduced argument packs for this parameter 24490 pack. */ 24491 TMPL_ARG (targs, level, idx) = result; 24492 } 24493 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack) 24494 && (ARGUMENT_PACK_ARGS (old_pack) 24495 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack))) 24496 { 24497 /* We only had the explicitly-provided arguments before, but 24498 now we have a complete set of arguments. */ 24499 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); 24500 24501 ARGUMENT_PACK_ARGS (old_pack) = new_args; 24502 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1; 24503 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args; 24504 } 24505 else 24506 { 24507 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE; 24508 tree old_args = ARGUMENT_PACK_ARGS (old_pack); 24509 temp_override<int> ovl (TREE_VEC_LENGTH (old_args)); 24510 /* During template argument deduction for the aggregate deduction 24511 candidate, the number of elements in a trailing parameter pack 24512 is only deduced from the number of remaining function 24513 arguments if it is not otherwise deduced. */ 24514 if (cxx_dialect >= cxx20 24515 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args) 24516 /* FIXME This isn't set properly for partial instantiations. */ 24517 && TPARMS_PRIMARY_TEMPLATE (tparms) 24518 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms))) 24519 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args); 24520 if (!comp_template_args (old_args, new_args, 24521 &bad_old_arg, &bad_new_arg)) 24522 /* Inconsistent unification of this parameter pack. */ 24523 return unify_parameter_pack_inconsistent (explain_p, 24524 bad_old_arg, 24525 bad_new_arg); 24526 } 24527 } 24528 24529 return unify_success (explain_p); 24530 } 24531 24532 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are 24533 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other 24534 parameters and return value are as for unify. */ 24535 24536 static int 24537 unify_array_domain (tree tparms, tree targs, 24538 tree parm_dom, tree arg_dom, 24539 bool explain_p) 24540 { 24541 tree parm_max; 24542 tree arg_max; 24543 bool parm_cst; 24544 bool arg_cst; 24545 24546 /* Our representation of array types uses "N - 1" as the 24547 TYPE_MAX_VALUE for an array with "N" elements, if "N" is 24548 not an integer constant. We cannot unify arbitrarily 24549 complex expressions, so we eliminate the MINUS_EXPRs 24550 here. */ 24551 parm_max = TYPE_MAX_VALUE (parm_dom); 24552 parm_cst = TREE_CODE (parm_max) == INTEGER_CST; 24553 if (!parm_cst) 24554 { 24555 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR); 24556 parm_max = TREE_OPERAND (parm_max, 0); 24557 } 24558 arg_max = TYPE_MAX_VALUE (arg_dom); 24559 arg_cst = TREE_CODE (arg_max) == INTEGER_CST; 24560 if (!arg_cst) 24561 { 24562 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are 24563 trying to unify the type of a variable with the type 24564 of a template parameter. For example: 24565 24566 template <unsigned int N> 24567 void f (char (&) [N]); 24568 int g(); 24569 void h(int i) { 24570 char a[g(i)]; 24571 f(a); 24572 } 24573 24574 Here, the type of the ARG will be "int [g(i)]", and 24575 may be a SAVE_EXPR, etc. */ 24576 if (TREE_CODE (arg_max) != MINUS_EXPR) 24577 return unify_vla_arg (explain_p, arg_dom); 24578 arg_max = TREE_OPERAND (arg_max, 0); 24579 } 24580 24581 /* If only one of the bounds used a MINUS_EXPR, compensate 24582 by adding one to the other bound. */ 24583 if (parm_cst && !arg_cst) 24584 parm_max = fold_build2_loc (input_location, PLUS_EXPR, 24585 integer_type_node, 24586 parm_max, 24587 integer_one_node); 24588 else if (arg_cst && !parm_cst) 24589 arg_max = fold_build2_loc (input_location, PLUS_EXPR, 24590 integer_type_node, 24591 arg_max, 24592 integer_one_node); 24593 24594 return unify (tparms, targs, parm_max, arg_max, 24595 UNIFY_ALLOW_INTEGER, explain_p); 24596 } 24597 24598 /* Returns whether T, a P or A in unify, is a type, template or expression. */ 24599 24600 enum pa_kind_t { pa_type, pa_tmpl, pa_expr }; 24601 24602 static pa_kind_t 24603 pa_kind (tree t) 24604 { 24605 if (PACK_EXPANSION_P (t)) 24606 t = PACK_EXPANSION_PATTERN (t); 24607 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM 24608 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE 24609 || DECL_TYPE_TEMPLATE_P (t)) 24610 return pa_tmpl; 24611 else if (TYPE_P (t)) 24612 return pa_type; 24613 else 24614 return pa_expr; 24615 } 24616 24617 /* Deduce the value of template parameters. TPARMS is the (innermost) 24618 set of template parameters to a template. TARGS is the bindings 24619 for those template parameters, as determined thus far; TARGS may 24620 include template arguments for outer levels of template parameters 24621 as well. PARM is a parameter to a template function, or a 24622 subcomponent of that parameter; ARG is the corresponding argument. 24623 This function attempts to match PARM with ARG in a manner 24624 consistent with the existing assignments in TARGS. If more values 24625 are deduced, then TARGS is updated. 24626 24627 Returns 0 if the type deduction succeeds, 1 otherwise. The 24628 parameter STRICT is a bitwise or of the following flags: 24629 24630 UNIFY_ALLOW_NONE: 24631 Require an exact match between PARM and ARG. 24632 UNIFY_ALLOW_MORE_CV_QUAL: 24633 Allow the deduced ARG to be more cv-qualified (by qualification 24634 conversion) than ARG. 24635 UNIFY_ALLOW_LESS_CV_QUAL: 24636 Allow the deduced ARG to be less cv-qualified than ARG. 24637 UNIFY_ALLOW_DERIVED: 24638 Allow the deduced ARG to be a template base class of ARG, 24639 or a pointer to a template base class of the type pointed to by 24640 ARG. 24641 UNIFY_ALLOW_INTEGER: 24642 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX 24643 case for more information. 24644 UNIFY_ALLOW_OUTER_LEVEL: 24645 This is the outermost level of a deduction. Used to determine validity 24646 of qualification conversions. A valid qualification conversion must 24647 have const qualified pointers leading up to the inner type which 24648 requires additional CV quals, except at the outer level, where const 24649 is not required [conv.qual]. It would be normal to set this flag in 24650 addition to setting UNIFY_ALLOW_MORE_CV_QUAL. 24651 UNIFY_ALLOW_OUTER_MORE_CV_QUAL: 24652 This is the outermost level of a deduction, and PARM can be more CV 24653 qualified at this point. 24654 UNIFY_ALLOW_OUTER_LESS_CV_QUAL: 24655 This is the outermost level of a deduction, and PARM can be less CV 24656 qualified at this point. */ 24657 24658 static int 24659 unify (tree tparms, tree targs, tree parm, tree arg, int strict, 24660 bool explain_p) 24661 { 24662 int idx; 24663 tree targ; 24664 tree tparm; 24665 int strict_in = strict; 24666 tsubst_flags_t complain = (explain_p 24667 ? tf_warning_or_error 24668 : tf_none); 24669 24670 /* I don't think this will do the right thing with respect to types. 24671 But the only case I've seen it in so far has been array bounds, where 24672 signedness is the only information lost, and I think that will be 24673 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to 24674 finish_id_expression_1, and are also OK. */ 24675 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR 24676 || (TREE_CODE (parm) == IMPLICIT_CONV_EXPR 24677 && IMPLICIT_CONV_EXPR_FORCED (parm))) 24678 parm = TREE_OPERAND (parm, 0); 24679 24680 if (arg == error_mark_node) 24681 return unify_invalid (explain_p); 24682 if (arg == unknown_type_node 24683 || arg == init_list_type_node) 24684 /* We can't deduce anything from this, but we might get all the 24685 template args from other function args. */ 24686 return unify_success (explain_p); 24687 24688 if (parm == any_targ_node || arg == any_targ_node) 24689 return unify_success (explain_p); 24690 24691 /* If PARM uses template parameters, then we can't bail out here, 24692 even if ARG == PARM, since we won't record unifications for the 24693 template parameters. We might need them if we're trying to 24694 figure out which of two things is more specialized. */ 24695 if (arg == parm 24696 && (DECL_P (parm) || !uses_template_parms (parm))) 24697 return unify_success (explain_p); 24698 24699 /* Handle init lists early, so the rest of the function can assume 24700 we're dealing with a type. */ 24701 if (BRACE_ENCLOSED_INITIALIZER_P (arg)) 24702 { 24703 tree elttype; 24704 tree orig_parm = parm; 24705 24706 if (!is_std_init_list (parm) 24707 && TREE_CODE (parm) != ARRAY_TYPE) 24708 /* We can only deduce from an initializer list argument if the 24709 parameter is std::initializer_list or an array; otherwise this 24710 is a non-deduced context. */ 24711 return unify_success (explain_p); 24712 24713 if (TREE_CODE (parm) == ARRAY_TYPE) 24714 elttype = TREE_TYPE (parm); 24715 else 24716 { 24717 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0); 24718 /* Deduction is defined in terms of a single type, so just punt 24719 on the (bizarre) std::initializer_list<T...>. */ 24720 if (PACK_EXPANSION_P (elttype)) 24721 return unify_success (explain_p); 24722 } 24723 24724 if (strict != DEDUCE_EXACT 24725 && TYPE_P (elttype) 24726 && !uses_deducible_template_parms (elttype)) 24727 /* If ELTTYPE has no deducible template parms, skip deduction from 24728 the list elements. */; 24729 else 24730 for (auto &e: CONSTRUCTOR_ELTS (arg)) 24731 { 24732 tree elt = e.value; 24733 int elt_strict = strict; 24734 24735 if (elt == error_mark_node) 24736 return unify_invalid (explain_p); 24737 24738 if (!BRACE_ENCLOSED_INITIALIZER_P (elt)) 24739 { 24740 tree type = TREE_TYPE (elt); 24741 if (type == error_mark_node) 24742 return unify_invalid (explain_p); 24743 /* It should only be possible to get here for a call. */ 24744 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL); 24745 elt_strict |= maybe_adjust_types_for_deduction 24746 (tparms, DEDUCE_CALL, &elttype, &type, elt); 24747 elt = type; 24748 } 24749 24750 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict, 24751 explain_p); 24752 } 24753 24754 if (TREE_CODE (parm) == ARRAY_TYPE 24755 && deducible_array_bound (TYPE_DOMAIN (parm))) 24756 { 24757 /* Also deduce from the length of the initializer list. */ 24758 tree max = size_int (CONSTRUCTOR_NELTS (arg)); 24759 tree idx = compute_array_index_type (NULL_TREE, max, tf_none); 24760 if (idx == error_mark_node) 24761 return unify_invalid (explain_p); 24762 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm), 24763 idx, explain_p); 24764 } 24765 24766 /* If the std::initializer_list<T> deduction worked, replace the 24767 deduced A with std::initializer_list<A>. */ 24768 if (orig_parm != parm) 24769 { 24770 idx = TEMPLATE_TYPE_IDX (orig_parm); 24771 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); 24772 targ = listify (targ); 24773 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ; 24774 } 24775 return unify_success (explain_p); 24776 } 24777 24778 /* If parm and arg aren't the same kind of thing (template, type, or 24779 expression), fail early. */ 24780 if (pa_kind (parm) != pa_kind (arg)) 24781 return unify_invalid (explain_p); 24782 24783 /* Immediately reject some pairs that won't unify because of 24784 cv-qualification mismatches. */ 24785 if (TREE_CODE (arg) == TREE_CODE (parm) 24786 && TYPE_P (arg) 24787 /* It is the elements of the array which hold the cv quals of an array 24788 type, and the elements might be template type parms. We'll check 24789 when we recurse. */ 24790 && TREE_CODE (arg) != ARRAY_TYPE 24791 /* We check the cv-qualifiers when unifying with template type 24792 parameters below. We want to allow ARG `const T' to unify with 24793 PARM `T' for example, when computing which of two templates 24794 is more specialized, for example. */ 24795 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM 24796 && !check_cv_quals_for_unify (strict_in, arg, parm)) 24797 return unify_cv_qual_mismatch (explain_p, parm, arg); 24798 24799 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL) 24800 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm) 24801 && !FUNC_OR_METHOD_TYPE_P (parm)) 24802 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL; 24803 /* PMFs recurse at the same level, so don't strip this yet. */ 24804 if (!TYPE_PTRMEMFUNC_P (parm)) 24805 strict &= ~UNIFY_ALLOW_OUTER_LEVEL; 24806 strict &= ~UNIFY_ALLOW_DERIVED; 24807 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL; 24808 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL; 24809 24810 switch (TREE_CODE (parm)) 24811 { 24812 case TYPENAME_TYPE: 24813 case SCOPE_REF: 24814 case UNBOUND_CLASS_TEMPLATE: 24815 /* In a type which contains a nested-name-specifier, template 24816 argument values cannot be deduced for template parameters used 24817 within the nested-name-specifier. */ 24818 return unify_success (explain_p); 24819 24820 case TEMPLATE_TYPE_PARM: 24821 case TEMPLATE_TEMPLATE_PARM: 24822 case BOUND_TEMPLATE_TEMPLATE_PARM: 24823 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); 24824 if (error_operand_p (tparm)) 24825 return unify_invalid (explain_p); 24826 24827 if (TEMPLATE_TYPE_LEVEL (parm) 24828 != template_decl_level (tparm)) 24829 /* The PARM is not one we're trying to unify. Just check 24830 to see if it matches ARG. */ 24831 { 24832 if (TREE_CODE (arg) == TREE_CODE (parm) 24833 && (is_auto (parm) ? is_auto (arg) 24834 : same_type_p (parm, arg))) 24835 return unify_success (explain_p); 24836 else 24837 return unify_type_mismatch (explain_p, parm, arg); 24838 } 24839 idx = TEMPLATE_TYPE_IDX (parm); 24840 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); 24841 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); 24842 if (error_operand_p (tparm)) 24843 return unify_invalid (explain_p); 24844 24845 /* Check for mixed types and values. */ 24846 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM 24847 && TREE_CODE (tparm) != TYPE_DECL) 24848 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM 24849 && TREE_CODE (tparm) != TEMPLATE_DECL)) 24850 gcc_unreachable (); 24851 24852 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) 24853 { 24854 if ((strict_in & UNIFY_ALLOW_DERIVED) 24855 && CLASS_TYPE_P (arg)) 24856 { 24857 /* First try to match ARG directly. */ 24858 tree t = try_class_unification (tparms, targs, parm, arg, 24859 explain_p); 24860 if (!t) 24861 { 24862 /* Otherwise, look for a suitable base of ARG, as below. */ 24863 enum template_base_result r; 24864 r = get_template_base (tparms, targs, parm, arg, 24865 explain_p, &t); 24866 if (!t) 24867 return unify_no_common_base (explain_p, r, parm, arg); 24868 arg = t; 24869 } 24870 } 24871 /* ARG must be constructed from a template class or a template 24872 template parameter. */ 24873 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM 24874 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) 24875 return unify_template_deduction_failure (explain_p, parm, arg); 24876 24877 /* Deduce arguments T, i from TT<T> or TT<i>. */ 24878 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p)) 24879 return 1; 24880 24881 arg = TYPE_TI_TEMPLATE (arg); 24882 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg)) 24883 /* If the template is a template template parameter, use the 24884 TEMPLATE_TEMPLATE_PARM for matching. */ 24885 arg = TREE_TYPE (arg); 24886 24887 /* Fall through to deduce template name. */ 24888 } 24889 24890 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM 24891 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) 24892 { 24893 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */ 24894 24895 /* Simple cases: Value already set, does match or doesn't. */ 24896 if (targ != NULL_TREE && template_args_equal (targ, arg)) 24897 return unify_success (explain_p); 24898 else if (targ) 24899 return unify_inconsistency (explain_p, parm, targ, arg); 24900 } 24901 else 24902 { 24903 /* If PARM is `const T' and ARG is only `int', we don't have 24904 a match unless we are allowing additional qualification. 24905 If ARG is `const int' and PARM is just `T' that's OK; 24906 that binds `const int' to `T'. */ 24907 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL, 24908 arg, parm)) 24909 return unify_cv_qual_mismatch (explain_p, parm, arg); 24910 24911 /* Consider the case where ARG is `const volatile int' and 24912 PARM is `const T'. Then, T should be `volatile int'. */ 24913 arg = cp_build_qualified_type 24914 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none); 24915 if (arg == error_mark_node) 24916 return unify_invalid (explain_p); 24917 24918 /* Simple cases: Value already set, does match or doesn't. */ 24919 if (targ != NULL_TREE && same_type_p (targ, arg)) 24920 return unify_success (explain_p); 24921 else if (targ) 24922 return unify_inconsistency (explain_p, parm, targ, arg); 24923 24924 /* Make sure that ARG is not a variable-sized array. (Note 24925 that were talking about variable-sized arrays (like 24926 `int[n]'), rather than arrays of unknown size (like 24927 `int[]').) We'll get very confused by such a type since 24928 the bound of the array is not constant, and therefore 24929 not mangleable. Besides, such types are not allowed in 24930 ISO C++, so we can do as we please here. We do allow 24931 them for 'auto' deduction, since that isn't ABI-exposed. */ 24932 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE)) 24933 return unify_vla_arg (explain_p, arg); 24934 24935 /* Strip typedefs as in convert_template_argument. */ 24936 arg = canonicalize_type_argument (arg, tf_none); 24937 } 24938 24939 /* If ARG is a parameter pack or an expansion, we cannot unify 24940 against it unless PARM is also a parameter pack. */ 24941 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) 24942 && !template_parameter_pack_p (parm)) 24943 return unify_parameter_pack_mismatch (explain_p, parm, arg); 24944 24945 /* If the argument deduction results is a METHOD_TYPE, 24946 then there is a problem. 24947 METHOD_TYPE doesn't map to any real C++ type the result of 24948 the deduction cannot be of that type. */ 24949 if (TREE_CODE (arg) == METHOD_TYPE) 24950 return unify_method_type_error (explain_p, arg); 24951 24952 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; 24953 return unify_success (explain_p); 24954 24955 case TEMPLATE_PARM_INDEX: 24956 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); 24957 if (error_operand_p (tparm)) 24958 return unify_invalid (explain_p); 24959 24960 if (TEMPLATE_PARM_LEVEL (parm) 24961 != template_decl_level (tparm)) 24962 { 24963 /* The PARM is not one we're trying to unify. Just check 24964 to see if it matches ARG. */ 24965 int result = !(TREE_CODE (arg) == TREE_CODE (parm) 24966 && cp_tree_equal (parm, arg)); 24967 if (result) 24968 unify_expression_unequal (explain_p, parm, arg); 24969 return result; 24970 } 24971 24972 idx = TEMPLATE_PARM_IDX (parm); 24973 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); 24974 24975 if (targ) 24976 { 24977 if ((strict & UNIFY_ALLOW_INTEGER) 24978 && TREE_TYPE (targ) && TREE_TYPE (arg) 24979 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ))) 24980 /* We're deducing from an array bound, the type doesn't matter. 24981 This conversion should match the one below. */ 24982 arg = fold (build_nop (TREE_TYPE (targ), arg)); 24983 int x = !cp_tree_equal (targ, arg); 24984 if (x) 24985 unify_inconsistency (explain_p, parm, targ, arg); 24986 return x; 24987 } 24988 24989 /* [temp.deduct.type] If, in the declaration of a function template 24990 with a non-type template-parameter, the non-type 24991 template-parameter is used in an expression in the function 24992 parameter-list and, if the corresponding template-argument is 24993 deduced, the template-argument type shall match the type of the 24994 template-parameter exactly, except that a template-argument 24995 deduced from an array bound may be of any integral type. 24996 The non-type parameter might use already deduced type parameters. */ 24997 tparm = TREE_TYPE (parm); 24998 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs)) 24999 /* We don't have enough levels of args to do any substitution. This 25000 can happen in the context of -fnew-ttp-matching. */; 25001 else 25002 { 25003 ++processing_template_decl; 25004 tparm = tsubst (tparm, targs, tf_none, NULL_TREE); 25005 --processing_template_decl; 25006 25007 if (tree a = type_uses_auto (tparm)) 25008 { 25009 tparm = do_auto_deduction (tparm, arg, a, 25010 complain, adc_unify, targs, 25011 LOOKUP_NORMAL, 25012 TPARMS_PRIMARY_TEMPLATE (tparms)); 25013 if (tparm == error_mark_node) 25014 return 1; 25015 } 25016 } 25017 25018 if (!TREE_TYPE (arg) 25019 || TREE_CODE (TREE_TYPE (arg)) == DEPENDENT_OPERATOR_TYPE) 25020 /* Template-parameter dependent expression. Just accept it for now. 25021 It will later be processed in convert_template_argument. */ 25022 ; 25023 else if (same_type_ignoring_top_level_qualifiers_p 25024 (non_reference (TREE_TYPE (arg)), 25025 non_reference (tparm))) 25026 /* OK. Ignore top-level quals here because a class-type template 25027 parameter object is const. */; 25028 else if ((strict & UNIFY_ALLOW_INTEGER) 25029 && CP_INTEGRAL_TYPE_P (tparm)) 25030 /* Convert the ARG to the type of PARM; the deduced non-type 25031 template argument must exactly match the types of the 25032 corresponding parameter. This conversion should match the 25033 one above. */ 25034 arg = fold (build_nop (tparm, arg)); 25035 else if (uses_template_parms (tparm)) 25036 { 25037 /* We haven't deduced the type of this parameter yet. */ 25038 if (cxx_dialect >= cxx17 25039 /* We deduce from array bounds in try_array_deduction. */ 25040 && !(strict & UNIFY_ALLOW_INTEGER) 25041 && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs)) 25042 { 25043 /* Deduce it from the non-type argument. As above, ignore 25044 top-level quals here too. */ 25045 tree atype = cv_unqualified (TREE_TYPE (arg)); 25046 RECUR_AND_CHECK_FAILURE (tparms, targs, 25047 tparm, atype, 25048 UNIFY_ALLOW_NONE, explain_p); 25049 /* Now check whether the type of this parameter is still 25050 dependent, and give up if so. */ 25051 ++processing_template_decl; 25052 tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE); 25053 --processing_template_decl; 25054 if (uses_template_parms (tparm)) 25055 return unify_success (explain_p); 25056 } 25057 else 25058 /* Try again later. */ 25059 return unify_success (explain_p); 25060 } 25061 else 25062 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg)); 25063 25064 /* If ARG is a parameter pack or an expansion, we cannot unify 25065 against it unless PARM is also a parameter pack. */ 25066 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg)) 25067 && !TEMPLATE_PARM_PARAMETER_PACK (parm)) 25068 return unify_parameter_pack_mismatch (explain_p, parm, arg); 25069 25070 { 25071 bool removed_attr = false; 25072 arg = strip_typedefs_expr (arg, &removed_attr); 25073 } 25074 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; 25075 return unify_success (explain_p); 25076 25077 case PTRMEM_CST: 25078 { 25079 /* A pointer-to-member constant can be unified only with 25080 another constant. */ 25081 if (TREE_CODE (arg) != PTRMEM_CST) 25082 return unify_ptrmem_cst_mismatch (explain_p, parm, arg); 25083 25084 /* Just unify the class member. It would be useless (and possibly 25085 wrong, depending on the strict flags) to unify also 25086 PTRMEM_CST_CLASS, because we want to be sure that both parm and 25087 arg refer to the same variable, even if through different 25088 classes. For instance: 25089 25090 struct A { int x; }; 25091 struct B : A { }; 25092 25093 Unification of &A::x and &B::x must succeed. */ 25094 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm), 25095 PTRMEM_CST_MEMBER (arg), strict, explain_p); 25096 } 25097 25098 case POINTER_TYPE: 25099 { 25100 if (!TYPE_PTR_P (arg)) 25101 return unify_type_mismatch (explain_p, parm, arg); 25102 25103 /* [temp.deduct.call] 25104 25105 A can be another pointer or pointer to member type that can 25106 be converted to the deduced A via a qualification 25107 conversion (_conv.qual_). 25108 25109 We pass down STRICT here rather than UNIFY_ALLOW_NONE. 25110 This will allow for additional cv-qualification of the 25111 pointed-to types if appropriate. */ 25112 25113 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE) 25114 /* The derived-to-base conversion only persists through one 25115 level of pointers. */ 25116 strict |= (strict_in & UNIFY_ALLOW_DERIVED); 25117 25118 return unify (tparms, targs, TREE_TYPE (parm), 25119 TREE_TYPE (arg), strict, explain_p); 25120 } 25121 25122 case REFERENCE_TYPE: 25123 if (!TYPE_REF_P (arg)) 25124 return unify_type_mismatch (explain_p, parm, arg); 25125 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), 25126 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p); 25127 25128 case ARRAY_TYPE: 25129 if (TREE_CODE (arg) != ARRAY_TYPE) 25130 return unify_type_mismatch (explain_p, parm, arg); 25131 if ((TYPE_DOMAIN (parm) == NULL_TREE) 25132 != (TYPE_DOMAIN (arg) == NULL_TREE)) 25133 return unify_type_mismatch (explain_p, parm, arg); 25134 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), 25135 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p); 25136 if (TYPE_DOMAIN (parm) != NULL_TREE) 25137 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm), 25138 TYPE_DOMAIN (arg), explain_p); 25139 return unify_success (explain_p); 25140 25141 case REAL_TYPE: 25142 case COMPLEX_TYPE: 25143 case VECTOR_TYPE: 25144 case INTEGER_TYPE: 25145 case BOOLEAN_TYPE: 25146 case ENUMERAL_TYPE: 25147 case VOID_TYPE: 25148 case OPAQUE_TYPE: 25149 case NULLPTR_TYPE: 25150 if (TREE_CODE (arg) != TREE_CODE (parm)) 25151 return unify_type_mismatch (explain_p, parm, arg); 25152 25153 /* We have already checked cv-qualification at the top of the 25154 function. */ 25155 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) 25156 return unify_type_mismatch (explain_p, parm, arg); 25157 25158 /* As far as unification is concerned, this wins. Later checks 25159 will invalidate it if necessary. */ 25160 return unify_success (explain_p); 25161 25162 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ 25163 /* Type INTEGER_CST can come from ordinary constant template args. */ 25164 case INTEGER_CST: 25165 case REAL_CST: 25166 if (TREE_TYPE (arg) == NULL_TREE 25167 || !same_type_p (TREE_TYPE (parm), TREE_TYPE (arg))) 25168 return unify_template_argument_mismatch (explain_p, parm, arg); 25169 while (CONVERT_EXPR_P (arg)) 25170 arg = TREE_OPERAND (arg, 0); 25171 25172 if (TREE_CODE (arg) != TREE_CODE (parm)) 25173 return unify_template_argument_mismatch (explain_p, parm, arg); 25174 return (simple_cst_equal (parm, arg) 25175 ? unify_success (explain_p) 25176 : unify_template_argument_mismatch (explain_p, parm, arg)); 25177 25178 case TREE_VEC: 25179 { 25180 int i, len, argslen; 25181 int parm_variadic_p = 0; 25182 25183 if (TREE_CODE (arg) != TREE_VEC) 25184 return unify_template_argument_mismatch (explain_p, parm, arg); 25185 25186 len = TREE_VEC_LENGTH (parm); 25187 argslen = TREE_VEC_LENGTH (arg); 25188 25189 /* Check for pack expansions in the parameters. */ 25190 for (i = 0; i < len; ++i) 25191 { 25192 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i))) 25193 { 25194 if (i == len - 1) 25195 /* We can unify against something with a trailing 25196 parameter pack. */ 25197 parm_variadic_p = 1; 25198 else 25199 /* [temp.deduct.type]/9: If the template argument list of 25200 P contains a pack expansion that is not the last 25201 template argument, the entire template argument list 25202 is a non-deduced context. */ 25203 return unify_success (explain_p); 25204 } 25205 } 25206 25207 /* If we don't have enough arguments to satisfy the parameters 25208 (not counting the pack expression at the end), or we have 25209 too many arguments for a parameter list that doesn't end in 25210 a pack expression, we can't unify. */ 25211 if (parm_variadic_p 25212 ? argslen < len - parm_variadic_p 25213 : argslen != len) 25214 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len); 25215 25216 /* Unify all of the parameters that precede the (optional) 25217 pack expression. */ 25218 for (i = 0; i < len - parm_variadic_p; ++i) 25219 { 25220 RECUR_AND_CHECK_FAILURE (tparms, targs, 25221 TREE_VEC_ELT (parm, i), 25222 TREE_VEC_ELT (arg, i), 25223 UNIFY_ALLOW_NONE, explain_p); 25224 } 25225 if (parm_variadic_p) 25226 return unify_pack_expansion (tparms, targs, parm, arg, 25227 DEDUCE_EXACT, 25228 /*subr=*/true, explain_p); 25229 return unify_success (explain_p); 25230 } 25231 25232 case RECORD_TYPE: 25233 case UNION_TYPE: 25234 if (TREE_CODE (arg) != TREE_CODE (parm)) 25235 return unify_type_mismatch (explain_p, parm, arg); 25236 25237 if (TYPE_PTRMEMFUNC_P (parm)) 25238 { 25239 if (!TYPE_PTRMEMFUNC_P (arg)) 25240 return unify_type_mismatch (explain_p, parm, arg); 25241 25242 return unify (tparms, targs, 25243 TYPE_PTRMEMFUNC_FN_TYPE (parm), 25244 TYPE_PTRMEMFUNC_FN_TYPE (arg), 25245 strict, explain_p); 25246 } 25247 else if (TYPE_PTRMEMFUNC_P (arg)) 25248 return unify_type_mismatch (explain_p, parm, arg); 25249 25250 if (CLASSTYPE_TEMPLATE_INFO (parm)) 25251 { 25252 tree t = NULL_TREE; 25253 25254 if (strict_in & UNIFY_ALLOW_DERIVED) 25255 { 25256 /* First, we try to unify the PARM and ARG directly. */ 25257 t = try_class_unification (tparms, targs, 25258 parm, arg, explain_p); 25259 25260 if (!t) 25261 { 25262 /* Fallback to the special case allowed in 25263 [temp.deduct.call]: 25264 25265 If P is a class, and P has the form 25266 template-id, then A can be a derived class of 25267 the deduced A. Likewise, if P is a pointer to 25268 a class of the form template-id, A can be a 25269 pointer to a derived class pointed to by the 25270 deduced A. */ 25271 enum template_base_result r; 25272 r = get_template_base (tparms, targs, parm, arg, 25273 explain_p, &t); 25274 25275 if (!t) 25276 { 25277 /* Don't give the derived diagnostic if we're 25278 already dealing with the same template. */ 25279 bool same_template 25280 = (CLASSTYPE_TEMPLATE_INFO (arg) 25281 && (CLASSTYPE_TI_TEMPLATE (parm) 25282 == CLASSTYPE_TI_TEMPLATE (arg))); 25283 return unify_no_common_base (explain_p && !same_template, 25284 r, parm, arg); 25285 } 25286 } 25287 } 25288 else if (CLASSTYPE_TEMPLATE_INFO (arg) 25289 && (CLASSTYPE_TI_TEMPLATE (parm) 25290 == CLASSTYPE_TI_TEMPLATE (arg))) 25291 /* Perhaps PARM is something like S<U> and ARG is S<int>. 25292 Then, we should unify `int' and `U'. */ 25293 t = arg; 25294 else 25295 /* There's no chance of unification succeeding. */ 25296 return unify_type_mismatch (explain_p, parm, arg); 25297 25298 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))) 25299 return unify (tparms, targs, 25300 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)), 25301 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (t)), 25302 UNIFY_ALLOW_NONE, explain_p); 25303 else 25304 return unify_success (explain_p); 25305 } 25306 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) 25307 return unify_type_mismatch (explain_p, parm, arg); 25308 return unify_success (explain_p); 25309 25310 case METHOD_TYPE: 25311 case FUNCTION_TYPE: 25312 { 25313 unsigned int nargs; 25314 tree *args; 25315 tree a; 25316 unsigned int i; 25317 25318 if (TREE_CODE (arg) != TREE_CODE (parm)) 25319 return unify_type_mismatch (explain_p, parm, arg); 25320 25321 /* CV qualifications for methods can never be deduced, they must 25322 match exactly. We need to check them explicitly here, 25323 because type_unification_real treats them as any other 25324 cv-qualified parameter. */ 25325 if (TREE_CODE (parm) == METHOD_TYPE 25326 && (!check_cv_quals_for_unify 25327 (UNIFY_ALLOW_NONE, 25328 class_of_this_parm (arg), 25329 class_of_this_parm (parm)))) 25330 return unify_cv_qual_mismatch (explain_p, parm, arg); 25331 if (TREE_CODE (arg) == FUNCTION_TYPE 25332 && type_memfn_quals (parm) != type_memfn_quals (arg)) 25333 return unify_cv_qual_mismatch (explain_p, parm, arg); 25334 if (type_memfn_rqual (parm) != type_memfn_rqual (arg)) 25335 return unify_type_mismatch (explain_p, parm, arg); 25336 25337 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), 25338 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p); 25339 25340 nargs = list_length (TYPE_ARG_TYPES (arg)); 25341 args = XALLOCAVEC (tree, nargs); 25342 for (a = TYPE_ARG_TYPES (arg), i = 0; 25343 a != NULL_TREE && a != void_list_node; 25344 a = TREE_CHAIN (a), ++i) 25345 args[i] = TREE_VALUE (a); 25346 nargs = i; 25347 25348 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), 25349 args, nargs, 1, DEDUCE_EXACT, 25350 NULL, explain_p)) 25351 return 1; 25352 25353 if (flag_noexcept_type) 25354 { 25355 tree pspec = TYPE_RAISES_EXCEPTIONS (parm); 25356 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg)); 25357 if (pspec == NULL_TREE) pspec = noexcept_false_spec; 25358 if (aspec == NULL_TREE) aspec = noexcept_false_spec; 25359 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec) 25360 && uses_template_parms (TREE_PURPOSE (pspec))) 25361 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec), 25362 TREE_PURPOSE (aspec), 25363 UNIFY_ALLOW_NONE, explain_p); 25364 else 25365 { 25366 bool pn = nothrow_spec_p (pspec); 25367 bool an = nothrow_spec_p (aspec); 25368 /* Here "less cv-qual" means the deduced arg (i.e. parm) has 25369 /more/ noexcept, since function pointer conversions are the 25370 reverse of qualification conversions. */ 25371 if (an == pn 25372 || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL)) 25373 || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL))) 25374 /* OK. */; 25375 else 25376 return unify_type_mismatch (explain_p, parm, arg); 25377 } 25378 } 25379 if (flag_tm) 25380 { 25381 /* As for noexcept. */ 25382 bool pn = tx_safe_fn_type_p (parm); 25383 bool an = tx_safe_fn_type_p (arg); 25384 if (an == pn 25385 || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL)) 25386 || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL))) 25387 /* OK. */; 25388 else 25389 return unify_type_mismatch (explain_p, parm, arg); 25390 } 25391 25392 return 0; 25393 } 25394 25395 case OFFSET_TYPE: 25396 /* Unify a pointer to member with a pointer to member function, which 25397 deduces the type of the member as a function type. */ 25398 if (TYPE_PTRMEMFUNC_P (arg)) 25399 { 25400 /* Check top-level cv qualifiers */ 25401 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm)) 25402 return unify_cv_qual_mismatch (explain_p, parm, arg); 25403 25404 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm), 25405 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), 25406 UNIFY_ALLOW_NONE, explain_p); 25407 25408 /* Determine the type of the function we are unifying against. */ 25409 tree fntype = static_fn_type (arg); 25410 25411 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p); 25412 } 25413 25414 if (TREE_CODE (arg) != OFFSET_TYPE) 25415 return unify_type_mismatch (explain_p, parm, arg); 25416 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm), 25417 TYPE_OFFSET_BASETYPE (arg), 25418 UNIFY_ALLOW_NONE, explain_p); 25419 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), 25420 strict, explain_p); 25421 25422 case CONST_DECL: 25423 /* CONST_DECL should already have been folded to its DECL_INITIAL. */ 25424 gcc_unreachable (); 25425 25426 case FIELD_DECL: 25427 case FUNCTION_DECL: 25428 case TEMPLATE_DECL: 25429 /* Matched cases are handled by the ARG == PARM test above. */ 25430 return unify_template_argument_mismatch (explain_p, parm, arg); 25431 25432 case VAR_DECL: 25433 /* We might get a variable as a non-type template argument in parm if the 25434 corresponding parameter is type-dependent. Make any necessary 25435 adjustments based on whether arg is a reference. */ 25436 if (CONSTANT_CLASS_P (arg)) 25437 parm = fold_non_dependent_expr (parm, complain); 25438 else if (REFERENCE_REF_P (arg)) 25439 { 25440 tree sub = TREE_OPERAND (arg, 0); 25441 STRIP_NOPS (sub); 25442 if (TREE_CODE (sub) == ADDR_EXPR) 25443 arg = TREE_OPERAND (sub, 0); 25444 } 25445 /* Now use the normal expression code to check whether they match. */ 25446 goto expr; 25447 25448 case TYPE_ARGUMENT_PACK: 25449 case NONTYPE_ARGUMENT_PACK: 25450 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm), 25451 ARGUMENT_PACK_ARGS (arg), strict, explain_p); 25452 25453 case TYPEOF_TYPE: 25454 case DECLTYPE_TYPE: 25455 case TRAIT_TYPE: 25456 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE, 25457 or TRAIT_TYPE nodes. */ 25458 return unify_success (explain_p); 25459 25460 case ERROR_MARK: 25461 /* Unification fails if we hit an error node. */ 25462 return unify_invalid (explain_p); 25463 25464 case INDIRECT_REF: 25465 if (REFERENCE_REF_P (parm)) 25466 { 25467 bool pexp = PACK_EXPANSION_P (arg); 25468 if (pexp) 25469 arg = PACK_EXPANSION_PATTERN (arg); 25470 if (REFERENCE_REF_P (arg)) 25471 arg = TREE_OPERAND (arg, 0); 25472 if (pexp) 25473 arg = make_pack_expansion (arg, complain); 25474 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg, 25475 strict, explain_p); 25476 } 25477 /* FALLTHRU */ 25478 25479 default: 25480 /* An unresolved overload is a nondeduced context. */ 25481 if (is_overloaded_fn (parm) || type_unknown_p (parm)) 25482 return unify_success (explain_p); 25483 gcc_assert (EXPR_P (parm) 25484 || TREE_CODE (parm) == CONSTRUCTOR 25485 || TREE_CODE (parm) == TRAIT_EXPR); 25486 expr: 25487 /* We must be looking at an expression. This can happen with 25488 something like: 25489 25490 template <int I> 25491 void foo(S<I>, S<I + 2>); 25492 25493 or 25494 25495 template<typename T> 25496 void foo(A<T, T{}>); 25497 25498 This is a "non-deduced context": 25499 25500 [deduct.type] 25501 25502 The non-deduced contexts are: 25503 25504 --A non-type template argument or an array bound in which 25505 a subexpression references a template parameter. 25506 25507 In these cases, we assume deduction succeeded, but don't 25508 actually infer any unifications. */ 25509 25510 if (!uses_template_parms (parm) 25511 && !template_args_equal (parm, arg)) 25512 return unify_expression_unequal (explain_p, parm, arg); 25513 else 25514 return unify_success (explain_p); 25515 } 25516 } 25517 #undef RECUR_AND_CHECK_FAILURE 25518 25519 /* Note that DECL can be defined in this translation unit, if 25521 required. */ 25522 25523 static void 25524 mark_definable (tree decl) 25525 { 25526 tree clone; 25527 DECL_NOT_REALLY_EXTERN (decl) = 1; 25528 FOR_EACH_CLONE (clone, decl) 25529 DECL_NOT_REALLY_EXTERN (clone) = 1; 25530 } 25531 25532 /* Called if RESULT is explicitly instantiated, or is a member of an 25533 explicitly instantiated class. */ 25534 25535 void 25536 mark_decl_instantiated (tree result, int extern_p) 25537 { 25538 SET_DECL_EXPLICIT_INSTANTIATION (result); 25539 25540 /* If this entity has already been written out, it's too late to 25541 make any modifications. */ 25542 if (TREE_ASM_WRITTEN (result)) 25543 return; 25544 25545 /* consteval functions are never emitted. */ 25546 if (TREE_CODE (result) == FUNCTION_DECL 25547 && DECL_IMMEDIATE_FUNCTION_P (result)) 25548 return; 25549 25550 /* For anonymous namespace we don't need to do anything. */ 25551 if (decl_internal_context_p (result)) 25552 { 25553 gcc_assert (!TREE_PUBLIC (result)); 25554 return; 25555 } 25556 25557 if (TREE_CODE (result) != FUNCTION_DECL) 25558 /* The TREE_PUBLIC flag for function declarations will have been 25559 set correctly by tsubst. */ 25560 TREE_PUBLIC (result) = 1; 25561 25562 if (extern_p) 25563 { 25564 DECL_EXTERNAL (result) = 1; 25565 DECL_NOT_REALLY_EXTERN (result) = 0; 25566 } 25567 else 25568 { 25569 mark_definable (result); 25570 mark_needed (result); 25571 /* Always make artificials weak. */ 25572 if (DECL_ARTIFICIAL (result) && flag_weak) 25573 comdat_linkage (result); 25574 /* For WIN32 we also want to put explicit instantiations in 25575 linkonce sections. */ 25576 else if (TREE_PUBLIC (result)) 25577 maybe_make_one_only (result); 25578 if (TREE_CODE (result) == FUNCTION_DECL 25579 && DECL_TEMPLATE_INSTANTIATED (result)) 25580 /* If the function has already been instantiated, clear DECL_EXTERNAL, 25581 since start_preparsed_function wouldn't have if we had an earlier 25582 extern explicit instantiation. */ 25583 DECL_EXTERNAL (result) = 0; 25584 } 25585 25586 /* If EXTERN_P, then this function will not be emitted -- unless 25587 followed by an explicit instantiation, at which point its linkage 25588 will be adjusted. If !EXTERN_P, then this function will be 25589 emitted here. In neither circumstance do we want 25590 import_export_decl to adjust the linkage. */ 25591 DECL_INTERFACE_KNOWN (result) = 1; 25592 } 25593 25594 /* Subroutine of more_specialized_fn: check whether TARGS is missing any 25595 important template arguments. If any are missing, we check whether 25596 they're important by using error_mark_node for substituting into any 25597 args that were used for partial ordering (the ones between ARGS and END) 25598 and seeing if it bubbles up. */ 25599 25600 static bool 25601 check_undeduced_parms (tree targs, tree args, tree end) 25602 { 25603 bool found = false; 25604 for (tree& targ : tree_vec_range (targs)) 25605 if (targ == NULL_TREE) 25606 { 25607 found = true; 25608 targ = error_mark_node; 25609 } 25610 if (found) 25611 { 25612 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE); 25613 if (substed == error_mark_node) 25614 return true; 25615 } 25616 return false; 25617 } 25618 25619 /* Given two function templates PAT1 and PAT2, return: 25620 25621 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. 25622 -1 if PAT2 is more specialized than PAT1. 25623 0 if neither is more specialized. 25624 25625 LEN indicates the number of parameters we should consider 25626 (defaulted parameters should not be considered). 25627 25628 The 1998 std underspecified function template partial ordering, and 25629 DR214 addresses the issue. We take pairs of arguments, one from 25630 each of the templates, and deduce them against each other. One of 25631 the templates will be more specialized if all the *other* 25632 template's arguments deduce against its arguments and at least one 25633 of its arguments *does* *not* deduce against the other template's 25634 corresponding argument. Deduction is done as for class templates. 25635 The arguments used in deduction have reference and top level cv 25636 qualifiers removed. Iff both arguments were originally reference 25637 types *and* deduction succeeds in both directions, an lvalue reference 25638 wins against an rvalue reference and otherwise the template 25639 with the more cv-qualified argument wins for that pairing (if 25640 neither is more cv-qualified, they both are equal). Unlike regular 25641 deduction, after all the arguments have been deduced in this way, 25642 we do *not* verify the deduced template argument values can be 25643 substituted into non-deduced contexts. 25644 25645 The logic can be a bit confusing here, because we look at deduce1 and 25646 targs1 to see if pat2 is at least as specialized, and vice versa; if we 25647 can find template arguments for pat1 to make arg1 look like arg2, that 25648 means that arg2 is at least as specialized as arg1. */ 25649 25650 int 25651 more_specialized_fn (tree pat1, tree pat2, int len) 25652 { 25653 tree decl1 = DECL_TEMPLATE_RESULT (pat1); 25654 tree decl2 = DECL_TEMPLATE_RESULT (pat2); 25655 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1)); 25656 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2)); 25657 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1); 25658 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2); 25659 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1)); 25660 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2)); 25661 tree origs1, origs2; 25662 bool lose1 = false; 25663 bool lose2 = false; 25664 25665 /* C++17 [temp.func.order]/3 (CWG532) 25666 25667 If only one of the function templates M is a non-static member of some 25668 class A, M is considered to have a new first parameter inserted in its 25669 function parameter list. Given cv as the cv-qualifiers of M (if any), the 25670 new parameter is of type "rvalue reference to cv A" if the optional 25671 ref-qualifier of M is && or if M has no ref-qualifier and the first 25672 parameter of the other template has rvalue reference type. Otherwise, the 25673 new parameter is of type "lvalue reference to cv A". */ 25674 25675 if (DECL_STATIC_FUNCTION_P (decl1) || DECL_STATIC_FUNCTION_P (decl2)) 25676 { 25677 /* Note C++20 DR2445 extended the above to static member functions, but 25678 I think the old G++ behavior of just skipping the object 25679 parameter when comparing to a static member function was better, so 25680 let's stick with that for now. This is CWG2834. --jason 2023-12 */ 25681 if (DECL_OBJECT_MEMBER_FUNCTION_P (decl1)) 25682 { 25683 len--; /* LEN is the number of significant arguments for DECL1 */ 25684 args1 = TREE_CHAIN (args1); 25685 } 25686 else if (DECL_OBJECT_MEMBER_FUNCTION_P (decl2)) 25687 args2 = TREE_CHAIN (args2); 25688 } 25689 else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1) 25690 && DECL_IOBJ_MEMBER_FUNCTION_P (decl2)) 25691 { 25692 /* Note DR2445 also (IMO wrongly) removed the "only one" above, which 25693 would break e.g. cpp1y/lambda-generic-variadic5.C. */ 25694 len--; 25695 args1 = TREE_CHAIN (args1); 25696 args2 = TREE_CHAIN (args2); 25697 } 25698 else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1) 25699 || DECL_IOBJ_MEMBER_FUNCTION_P (decl2)) 25700 { 25701 /* The other is a non-member or explicit object member function; 25702 rewrite the implicit object parameter to a reference. */ 25703 tree ns = DECL_IOBJ_MEMBER_FUNCTION_P (decl2) ? decl2 : decl1; 25704 tree &nsargs = ns == decl2 ? args2 : args1; 25705 tree obtype = TREE_TYPE (TREE_VALUE (nsargs)); 25706 25707 nsargs = TREE_CHAIN (nsargs); 25708 25709 cp_ref_qualifier rqual = type_memfn_rqual (TREE_TYPE (ns)); 25710 if (rqual == REF_QUAL_NONE) 25711 { 25712 tree otherfirst = ns == decl1 ? args2 : args1; 25713 otherfirst = TREE_VALUE (otherfirst); 25714 if (TREE_CODE (otherfirst) == REFERENCE_TYPE 25715 && TYPE_REF_IS_RVALUE (otherfirst)) 25716 rqual = REF_QUAL_RVALUE; 25717 } 25718 obtype = cp_build_reference_type (obtype, rqual == REF_QUAL_RVALUE); 25719 nsargs = tree_cons (NULL_TREE, obtype, nsargs); 25720 } 25721 25722 /* If only one is a conversion operator, they are unordered. */ 25723 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2)) 25724 return 0; 25725 25726 /* Consider the return type for a conversion function */ 25727 if (DECL_CONV_FN_P (decl1)) 25728 { 25729 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1); 25730 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2); 25731 len++; 25732 } 25733 25734 processing_template_decl++; 25735 25736 origs1 = args1; 25737 origs2 = args2; 25738 25739 while (len-- 25740 /* Stop when an ellipsis is seen. */ 25741 && args1 != NULL_TREE && args2 != NULL_TREE) 25742 { 25743 tree arg1 = TREE_VALUE (args1); 25744 tree arg2 = TREE_VALUE (args2); 25745 int deduce1, deduce2; 25746 int quals1 = -1; 25747 int quals2 = -1; 25748 int ref1 = 0; 25749 int ref2 = 0; 25750 25751 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION 25752 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION) 25753 { 25754 /* When both arguments are pack expansions, we need only 25755 unify the patterns themselves. */ 25756 arg1 = PACK_EXPANSION_PATTERN (arg1); 25757 arg2 = PACK_EXPANSION_PATTERN (arg2); 25758 25759 /* This is the last comparison we need to do. */ 25760 len = 0; 25761 } 25762 25763 if (TYPE_REF_P (arg1)) 25764 { 25765 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1; 25766 arg1 = TREE_TYPE (arg1); 25767 quals1 = cp_type_quals (arg1); 25768 } 25769 25770 if (TYPE_REF_P (arg2)) 25771 { 25772 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1; 25773 arg2 = TREE_TYPE (arg2); 25774 quals2 = cp_type_quals (arg2); 25775 } 25776 25777 arg1 = TYPE_MAIN_VARIANT (arg1); 25778 arg2 = TYPE_MAIN_VARIANT (arg2); 25779 25780 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION) 25781 { 25782 int i, len2 = remaining_arguments (args2); 25783 tree parmvec = make_tree_vec (1); 25784 tree argvec = make_tree_vec (len2); 25785 tree ta = args2; 25786 25787 /* Setup the parameter vector, which contains only ARG1. */ 25788 TREE_VEC_ELT (parmvec, 0) = arg1; 25789 25790 /* Setup the argument vector, which contains the remaining 25791 arguments. */ 25792 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta)) 25793 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); 25794 25795 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec, 25796 argvec, DEDUCE_EXACT, 25797 /*subr=*/true, /*explain_p=*/false) 25798 == 0); 25799 25800 /* We cannot deduce in the other direction, because ARG1 is 25801 a pack expansion but ARG2 is not. */ 25802 deduce2 = 0; 25803 } 25804 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION) 25805 { 25806 int i, len1 = remaining_arguments (args1); 25807 tree parmvec = make_tree_vec (1); 25808 tree argvec = make_tree_vec (len1); 25809 tree ta = args1; 25810 25811 /* Setup the parameter vector, which contains only ARG1. */ 25812 TREE_VEC_ELT (parmvec, 0) = arg2; 25813 25814 /* Setup the argument vector, which contains the remaining 25815 arguments. */ 25816 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta)) 25817 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); 25818 25819 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec, 25820 argvec, DEDUCE_EXACT, 25821 /*subr=*/true, /*explain_p=*/false) 25822 == 0); 25823 25824 /* We cannot deduce in the other direction, because ARG2 is 25825 a pack expansion but ARG1 is not.*/ 25826 deduce1 = 0; 25827 } 25828 25829 else 25830 { 25831 /* The normal case, where neither argument is a pack 25832 expansion. */ 25833 deduce1 = (unify (tparms1, targs1, arg1, arg2, 25834 UNIFY_ALLOW_NONE, /*explain_p=*/false) 25835 == 0); 25836 deduce2 = (unify (tparms2, targs2, arg2, arg1, 25837 UNIFY_ALLOW_NONE, /*explain_p=*/false) 25838 == 0); 25839 } 25840 25841 /* If we couldn't deduce arguments for tparms1 to make arg1 match 25842 arg2, then arg2 is not as specialized as arg1. */ 25843 if (!deduce1) 25844 lose2 = true; 25845 if (!deduce2) 25846 lose1 = true; 25847 25848 /* "If, for a given type, deduction succeeds in both directions 25849 (i.e., the types are identical after the transformations above) 25850 and both P and A were reference types (before being replaced with 25851 the type referred to above): 25852 - if the type from the argument template was an lvalue reference and 25853 the type from the parameter template was not, the argument type is 25854 considered to be more specialized than the other; otherwise, 25855 - if the type from the argument template is more cv-qualified 25856 than the type from the parameter template (as described above), 25857 the argument type is considered to be more specialized than the other; 25858 otherwise, 25859 - neither type is more specialized than the other." */ 25860 25861 if (deduce1 && deduce2) 25862 { 25863 if (ref1 && ref2 && ref1 != ref2) 25864 { 25865 if (ref1 > ref2) 25866 lose1 = true; 25867 else 25868 lose2 = true; 25869 } 25870 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0) 25871 { 25872 if ((quals1 & quals2) == quals2) 25873 lose2 = true; 25874 if ((quals1 & quals2) == quals1) 25875 lose1 = true; 25876 } 25877 } 25878 25879 if (lose1 && lose2) 25880 /* We've failed to deduce something in either direction. 25881 These must be unordered. */ 25882 break; 25883 25884 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION 25885 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION) 25886 /* We have already processed all of the arguments in our 25887 handing of the pack expansion type. */ 25888 len = 0; 25889 25890 args1 = TREE_CHAIN (args1); 25891 args2 = TREE_CHAIN (args2); 25892 } 25893 25894 /* "In most cases, all template parameters must have values in order for 25895 deduction to succeed, but for partial ordering purposes a template 25896 parameter may remain without a value provided it is not used in the 25897 types being used for partial ordering." 25898 25899 Thus, if we are missing any of the targs1 we need to substitute into 25900 origs1, then pat2 is not as specialized as pat1. This can happen when 25901 there is a nondeduced context. */ 25902 if (!lose2 && check_undeduced_parms (targs1, origs1, args1)) 25903 lose2 = true; 25904 if (!lose1 && check_undeduced_parms (targs2, origs2, args2)) 25905 lose1 = true; 25906 25907 processing_template_decl--; 25908 25909 /* If both deductions succeed, the partial ordering selects the more 25910 constrained template. */ 25911 /* P2113: If the corresponding template-parameters of the 25912 template-parameter-lists are not equivalent ([temp.over.link]) or if 25913 the function parameters that positionally correspond between the two 25914 templates are not of the same type, neither template is more 25915 specialized than the other. */ 25916 if (!lose1 && !lose2 25917 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1), 25918 DECL_TEMPLATE_PARMS (pat2)) 25919 && compparms (origs1, origs2)) 25920 { 25921 int winner = more_constrained (decl1, decl2); 25922 if (winner > 0) 25923 lose2 = true; 25924 else if (winner < 0) 25925 lose1 = true; 25926 } 25927 25928 /* All things being equal, if the next argument is a pack expansion 25929 for one function but not for the other, prefer the 25930 non-variadic function. FIXME this is bogus; see c++/41958. */ 25931 if (lose1 == lose2 25932 && args1 && TREE_VALUE (args1) 25933 && args2 && TREE_VALUE (args2)) 25934 { 25935 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION; 25936 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION; 25937 } 25938 25939 if (lose1 == lose2) 25940 return 0; 25941 else if (!lose1) 25942 return 1; 25943 else 25944 return -1; 25945 } 25946 25947 /* Determine which of two partial specializations of TMPL is more 25948 specialized. 25949 25950 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding 25951 to the first partial specialization. The TREE_PURPOSE is the 25952 innermost set of template parameters for the partial 25953 specialization. PAT2 is similar, but for the second template. 25954 25955 Return 1 if the first partial specialization is more specialized; 25956 -1 if the second is more specialized; 0 if neither is more 25957 specialized. 25958 25959 See [temp.class.order] for information about determining which of 25960 two templates is more specialized. */ 25961 25962 static int 25963 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2) 25964 { 25965 tree targs; 25966 int winner = 0; 25967 bool any_deductions = false; 25968 25969 tree tmpl1 = TREE_VALUE (pat1); 25970 tree tmpl2 = TREE_VALUE (pat2); 25971 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1))); 25972 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2))); 25973 25974 /* Just like what happens for functions, if we are ordering between 25975 different template specializations, we may encounter dependent 25976 types in the arguments, and we need our dependency check functions 25977 to behave correctly. */ 25978 ++processing_template_decl; 25979 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2); 25980 if (targs) 25981 { 25982 --winner; 25983 any_deductions = true; 25984 } 25985 25986 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1); 25987 if (targs) 25988 { 25989 ++winner; 25990 any_deductions = true; 25991 } 25992 --processing_template_decl; 25993 25994 /* If both deductions succeed, the partial ordering selects the more 25995 constrained template. */ 25996 if (!winner && any_deductions) 25997 winner = more_constrained (tmpl1, tmpl2); 25998 25999 /* In the case of a tie where at least one of the templates 26000 has a parameter pack at the end, the template with the most 26001 non-packed parameters wins. */ 26002 if (winner == 0 26003 && any_deductions 26004 && (template_args_variadic_p (TREE_PURPOSE (pat1)) 26005 || template_args_variadic_p (TREE_PURPOSE (pat2)))) 26006 { 26007 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1)); 26008 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2)); 26009 int len1 = TREE_VEC_LENGTH (args1); 26010 int len2 = TREE_VEC_LENGTH (args2); 26011 26012 /* We don't count the pack expansion at the end. */ 26013 if (template_args_variadic_p (TREE_PURPOSE (pat1))) 26014 --len1; 26015 if (template_args_variadic_p (TREE_PURPOSE (pat2))) 26016 --len2; 26017 26018 if (len1 > len2) 26019 return 1; 26020 else if (len1 < len2) 26021 return -1; 26022 } 26023 26024 return winner; 26025 } 26026 26027 /* Return the template arguments that will produce the function signature 26028 DECL from the function template FN, with the explicit template 26029 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must 26030 also match. Return NULL_TREE if no satisfactory arguments could be 26031 found. */ 26032 26033 static tree 26034 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype) 26035 { 26036 int ntparms = DECL_NTPARMS (fn); 26037 tree targs = make_tree_vec (ntparms); 26038 tree decl_type = TREE_TYPE (decl); 26039 tree decl_arg_types; 26040 tree *args; 26041 unsigned int nargs, ix; 26042 tree arg; 26043 26044 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn)); 26045 26046 /* Never do unification on the 'this' parameter. */ 26047 decl_arg_types = skip_artificial_parms_for (decl, 26048 TYPE_ARG_TYPES (decl_type)); 26049 26050 nargs = list_length (decl_arg_types); 26051 args = XALLOCAVEC (tree, nargs); 26052 for (arg = decl_arg_types, ix = 0; 26053 arg != NULL_TREE; 26054 arg = TREE_CHAIN (arg), ++ix) 26055 args[ix] = TREE_VALUE (arg); 26056 26057 if (fn_type_unification (fn, explicit_args, targs, 26058 args, ix, 26059 (check_rettype || DECL_CONV_FN_P (fn) 26060 ? TREE_TYPE (decl_type) : NULL_TREE), 26061 DEDUCE_EXACT, LOOKUP_NORMAL, NULL, 26062 /*explain_p=*/false, 26063 /*decltype*/false) 26064 == error_mark_node) 26065 return NULL_TREE; 26066 26067 return targs; 26068 } 26069 26070 /* Return the innermost template arguments that, when applied to a partial 26071 specialization SPEC_TMPL of TMPL, yield the ARGS. 26072 26073 For example, suppose we have: 26074 26075 template <class T, class U> struct S {}; 26076 template <class T> struct S<T*, int> {}; 26077 26078 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the 26079 partial specialization and the ARGS will be {double*, int}. The resulting 26080 vector will be {double}, indicating that `T' is bound to `double'. */ 26081 26082 static tree 26083 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args) 26084 { 26085 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl); 26086 tree spec_args 26087 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl))); 26088 int i, ntparms = TREE_VEC_LENGTH (tparms); 26089 tree deduced_args; 26090 tree innermost_deduced_args; 26091 26092 innermost_deduced_args = make_tree_vec (ntparms); 26093 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) 26094 { 26095 deduced_args = copy_node (args); 26096 SET_TMPL_ARGS_LEVEL (deduced_args, 26097 TMPL_ARGS_DEPTH (deduced_args), 26098 innermost_deduced_args); 26099 } 26100 else 26101 deduced_args = innermost_deduced_args; 26102 26103 bool tried_array_deduction = (cxx_dialect < cxx17); 26104 again: 26105 if (unify (tparms, deduced_args, 26106 INNERMOST_TEMPLATE_ARGS (spec_args), 26107 INNERMOST_TEMPLATE_ARGS (args), 26108 UNIFY_ALLOW_NONE, /*explain_p=*/false)) 26109 return NULL_TREE; 26110 26111 for (i = 0; i < ntparms; ++i) 26112 if (! TREE_VEC_ELT (innermost_deduced_args, i)) 26113 { 26114 if (!tried_array_deduction) 26115 { 26116 try_array_deduction (tparms, innermost_deduced_args, 26117 INNERMOST_TEMPLATE_ARGS (spec_args)); 26118 tried_array_deduction = true; 26119 if (TREE_VEC_ELT (innermost_deduced_args, i)) 26120 goto again; 26121 } 26122 return NULL_TREE; 26123 } 26124 26125 if (!push_tinst_level (spec_tmpl, deduced_args)) 26126 { 26127 excessive_deduction_depth = true; 26128 return NULL_TREE; 26129 } 26130 26131 /* Verify that nondeduced template arguments agree with the type 26132 obtained from argument deduction. 26133 26134 For example: 26135 26136 struct A { typedef int X; }; 26137 template <class T, class U> struct C {}; 26138 template <class T> struct C<T, typename T::X> {}; 26139 26140 Then with the instantiation `C<A, int>', we can deduce that 26141 `T' is `A' but unify () does not check whether `typename T::X' 26142 is `int'. */ 26143 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE); 26144 26145 if (spec_args != error_mark_node) 26146 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), 26147 INNERMOST_TEMPLATE_ARGS (spec_args), 26148 tmpl, tf_none, false); 26149 26150 pop_tinst_level (); 26151 26152 if (spec_args == error_mark_node 26153 /* We only need to check the innermost arguments; the other 26154 arguments will always agree. */ 26155 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args), 26156 INNERMOST_TEMPLATE_ARGS (args))) 26157 return NULL_TREE; 26158 26159 /* Now that we have bindings for all of the template arguments, 26160 ensure that the arguments deduced for the template template 26161 parameters have compatible template parameter lists. See the use 26162 of template_template_parm_bindings_ok_p in fn_type_unification 26163 for more information. */ 26164 if (!template_template_parm_bindings_ok_p (tparms, deduced_args)) 26165 return NULL_TREE; 26166 26167 return deduced_args; 26168 } 26169 26170 // Compare two function templates T1 and T2 by deducing bindings 26171 // from one against the other. If both deductions succeed, compare 26172 // constraints to see which is more constrained. 26173 static int 26174 more_specialized_inst (tree t1, tree t2) 26175 { 26176 int fate = 0; 26177 int count = 0; 26178 26179 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true)) 26180 { 26181 --fate; 26182 ++count; 26183 } 26184 26185 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true)) 26186 { 26187 ++fate; 26188 ++count; 26189 } 26190 26191 // If both deductions succeed, then one may be more constrained. 26192 if (count == 2 && fate == 0) 26193 fate = more_constrained (t1, t2); 26194 26195 return fate; 26196 } 26197 26198 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL. 26199 Return the TREE_LIST node with the most specialized template, if 26200 any. If there is no most specialized template, the error_mark_node 26201 is returned. 26202 26203 Note that this function does not look at, or modify, the 26204 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node 26205 returned is one of the elements of INSTANTIATIONS, callers may 26206 store information in the TREE_PURPOSE or TREE_TYPE of the nodes, 26207 and retrieve it from the value returned. */ 26208 26209 tree 26210 most_specialized_instantiation (tree templates) 26211 { 26212 tree fn, champ; 26213 26214 ++processing_template_decl; 26215 26216 champ = templates; 26217 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn)) 26218 { 26219 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn)); 26220 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)); 26221 if (fate == -1) 26222 champ = fn; 26223 else if (!fate) 26224 { 26225 /* Equally specialized, move to next function. If there 26226 is no next function, nothing's most specialized. */ 26227 fn = TREE_CHAIN (fn); 26228 champ = fn; 26229 if (!fn) 26230 break; 26231 } 26232 } 26233 26234 if (champ) 26235 /* Now verify that champ is better than everything earlier in the 26236 instantiation list. */ 26237 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) { 26238 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1) 26239 { 26240 champ = NULL_TREE; 26241 break; 26242 } 26243 } 26244 26245 processing_template_decl--; 26246 26247 if (!champ) 26248 return error_mark_node; 26249 26250 return champ; 26251 } 26252 26253 /* If DECL is a specialization of some template, return the most 26254 general such template. Otherwise, returns NULL_TREE. 26255 26256 For example, given: 26257 26258 template <class T> struct S { template <class U> void f(U); }; 26259 26260 if TMPL is `template <class U> void S<int>::f(U)' this will return 26261 the full template. This function will not trace past partial 26262 specializations, however. For example, given in addition: 26263 26264 template <class T> struct S<T*> { template <class U> void f(U); }; 26265 26266 if TMPL is `template <class U> void S<int*>::f(U)' this will return 26267 `template <class T> template <class U> S<T*>::f(U)'. */ 26268 26269 tree 26270 most_general_template (const_tree decl) 26271 { 26272 if (TREE_CODE (decl) != TEMPLATE_DECL) 26273 { 26274 if (tree tinfo = get_template_info (decl)) 26275 decl = TI_TEMPLATE (tinfo); 26276 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a 26277 template friend, or a FIELD_DECL for a capture pack. */ 26278 if (TREE_CODE (decl) != TEMPLATE_DECL) 26279 return NULL_TREE; 26280 } 26281 26282 if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl)) 26283 return DECL_TI_TEMPLATE (DECL_TEMPLATE_RESULT (decl)); 26284 26285 /* Look for more and more general templates. */ 26286 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)) 26287 { 26288 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases. 26289 (See cp-tree.h for details.) */ 26290 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) 26291 break; 26292 26293 if (CLASS_TYPE_P (TREE_TYPE (decl)) 26294 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl))) 26295 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) 26296 break; 26297 26298 /* Stop if we run into an explicitly specialized class template. */ 26299 if (!DECL_NAMESPACE_SCOPE_P (decl) 26300 && DECL_CONTEXT (decl) 26301 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl))) 26302 break; 26303 26304 decl = DECL_TI_TEMPLATE (decl); 26305 } 26306 26307 return CONST_CAST_TREE (decl); 26308 } 26309 26310 /* Return the most specialized of the template partial specializations 26311 which can produce TARGET, a specialization of some class or variable 26312 template. The value returned is a TEMPLATE_INFO; the TI_TEMPLATE is a 26313 TEMPLATE_DECL node corresponding to the partial specialization, while 26314 the TI_ARGS is the set of template arguments that must be substituted 26315 into the template pattern in order to generate TARGET. The result is 26316 cached in the TI_PARTIAL_INFO of the corresponding TEMPLATE_INFO unless 26317 RECHECKING is true. 26318 26319 If the choice of partial specialization is ambiguous, a diagnostic 26320 is issued, and the error_mark_node is returned. If there are no 26321 partial specializations matching TARGET, then NULL_TREE is 26322 returned, indicating that the primary template should be used. */ 26323 26324 tree 26325 most_specialized_partial_spec (tree target, tsubst_flags_t complain, 26326 bool rechecking /* = false */) 26327 { 26328 tree tinfo = NULL_TREE; 26329 tree tmpl, args, decl; 26330 if (TYPE_P (target)) 26331 { 26332 tinfo = CLASSTYPE_TEMPLATE_INFO (target); 26333 tmpl = TI_TEMPLATE (tinfo); 26334 args = TI_ARGS (tinfo); 26335 decl = TYPE_NAME (target); 26336 } 26337 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR) 26338 { 26339 tmpl = TREE_OPERAND (target, 0); 26340 args = TREE_OPERAND (target, 1); 26341 decl = DECL_TEMPLATE_RESULT (tmpl); 26342 } 26343 else if (VAR_P (target)) 26344 { 26345 tinfo = DECL_TEMPLATE_INFO (target); 26346 tmpl = TI_TEMPLATE (tinfo); 26347 args = TI_ARGS (tinfo); 26348 decl = target; 26349 } 26350 else 26351 gcc_unreachable (); 26352 26353 if (!PRIMARY_TEMPLATE_P (tmpl)) 26354 return NULL_TREE; 26355 26356 if (!rechecking 26357 && tinfo 26358 && (VAR_P (target) || COMPLETE_TYPE_P (target))) 26359 return TI_PARTIAL_INFO (tinfo); 26360 26361 tree main_tmpl = most_general_template (tmpl); 26362 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); 26363 if (!specs) 26364 /* There are no partial specializations of this template. */ 26365 return NULL_TREE; 26366 26367 push_access_scope_guard pas (decl); 26368 deferring_access_check_sentinel acs (dk_no_deferred); 26369 26370 /* For determining which partial specialization to use, only the 26371 innermost args are interesting. */ 26372 tree outer_args = NULL_TREE; 26373 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) 26374 { 26375 outer_args = strip_innermost_template_args (args, 1); 26376 args = INNERMOST_TEMPLATE_ARGS (args); 26377 } 26378 26379 /* The caller hasn't called push_to_top_level yet, but we need 26380 get_partial_spec_bindings to be done in non-template context so that we'll 26381 fully resolve everything. */ 26382 processing_template_decl_sentinel ptds; 26383 26384 tree list = NULL_TREE; 26385 for (tree t = specs; t; t = TREE_CHAIN (t)) 26386 { 26387 const tree ospec_tmpl = TREE_VALUE (t); 26388 26389 tree spec_tmpl; 26390 if (outer_args) 26391 { 26392 /* Substitute in the template args from the enclosing class. */ 26393 ++processing_template_decl; 26394 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE); 26395 --processing_template_decl; 26396 if (spec_tmpl == error_mark_node) 26397 return error_mark_node; 26398 } 26399 else 26400 spec_tmpl = ospec_tmpl; 26401 26402 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args); 26403 if (spec_args) 26404 { 26405 if (outer_args) 26406 spec_args = add_to_template_args (outer_args, spec_args); 26407 26408 /* Keep the candidate only if its constraints are satisfied. */ 26409 if (constraints_satisfied_p (ospec_tmpl, spec_args)) 26410 list = tree_cons (spec_args, ospec_tmpl, list); 26411 } 26412 } 26413 26414 if (! list) 26415 return NULL_TREE; 26416 26417 tree champ = list; 26418 bool ambiguous_p = false; 26419 for (tree t = TREE_CHAIN (list); t; t = TREE_CHAIN (t)) 26420 { 26421 int fate = more_specialized_partial_spec (tmpl, champ, t); 26422 if (fate == 1) 26423 ; 26424 else 26425 { 26426 if (fate == 0) 26427 { 26428 t = TREE_CHAIN (t); 26429 if (! t) 26430 { 26431 ambiguous_p = true; 26432 break; 26433 } 26434 } 26435 champ = t; 26436 } 26437 } 26438 26439 if (!ambiguous_p) 26440 for (tree t = list; t && t != champ; t = TREE_CHAIN (t)) 26441 { 26442 int fate = more_specialized_partial_spec (tmpl, champ, t); 26443 if (fate != 1) 26444 { 26445 ambiguous_p = true; 26446 break; 26447 } 26448 } 26449 26450 if (ambiguous_p) 26451 { 26452 const char *str; 26453 char *spaces = NULL; 26454 if (!(complain & tf_error)) 26455 return error_mark_node; 26456 if (TYPE_P (target)) 26457 error ("ambiguous template instantiation for %q#T", target); 26458 else 26459 error ("ambiguous template instantiation for %q#D", target); 26460 str = ngettext ("candidate is:", "candidates are:", list_length (list)); 26461 for (tree t = list; t; t = TREE_CHAIN (t)) 26462 { 26463 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t)); 26464 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)), 26465 "%s %#qS", spaces ? spaces : str, subst); 26466 spaces = spaces ? spaces : get_spaces (str); 26467 } 26468 free (spaces); 26469 return error_mark_node; 26470 } 26471 26472 tree result = build_template_info (TREE_VALUE (champ), TREE_PURPOSE (champ)); 26473 if (!rechecking && tinfo) 26474 TI_PARTIAL_INFO (tinfo) = result; 26475 return result; 26476 } 26477 26478 /* Explicitly instantiate DECL. */ 26479 26480 void 26481 do_decl_instantiation (tree decl, tree storage) 26482 { 26483 tree result = NULL_TREE; 26484 int extern_p = 0; 26485 26486 if (!decl || decl == error_mark_node) 26487 /* An error occurred, for which grokdeclarator has already issued 26488 an appropriate message. */ 26489 return; 26490 else if (! DECL_LANG_SPECIFIC (decl)) 26491 { 26492 error ("explicit instantiation of non-template %q#D", decl); 26493 return; 26494 } 26495 else if (DECL_DECLARED_CONCEPT_P (decl)) 26496 { 26497 if (VAR_P (decl)) 26498 error ("explicit instantiation of variable concept %q#D", decl); 26499 else 26500 error ("explicit instantiation of function concept %q#D", decl); 26501 return; 26502 } 26503 26504 bool var_templ = (DECL_TEMPLATE_INFO (decl) 26505 && variable_template_p (DECL_TI_TEMPLATE (decl))); 26506 26507 if (VAR_P (decl) && !var_templ) 26508 { 26509 /* There is an asymmetry here in the way VAR_DECLs and 26510 FUNCTION_DECLs are handled by grokdeclarator. In the case of 26511 the latter, the DECL we get back will be marked as a 26512 template instantiation, and the appropriate 26513 DECL_TEMPLATE_INFO will be set up. This does not happen for 26514 VAR_DECLs so we do the lookup here. Probably, grokdeclarator 26515 should handle VAR_DECLs as it currently handles 26516 FUNCTION_DECLs. */ 26517 if (!DECL_CLASS_SCOPE_P (decl)) 26518 { 26519 error ("%qD is not a static data member of a class template", decl); 26520 return; 26521 } 26522 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false); 26523 if (!result || !VAR_P (result)) 26524 { 26525 error ("no matching template for %qD found", decl); 26526 return; 26527 } 26528 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl))) 26529 { 26530 error ("type %qT for explicit instantiation %qD does not match " 26531 "declared type %qT", TREE_TYPE (result), decl, 26532 TREE_TYPE (decl)); 26533 return; 26534 } 26535 } 26536 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ) 26537 { 26538 error ("explicit instantiation of %q#D", decl); 26539 return; 26540 } 26541 else 26542 result = decl; 26543 26544 /* Check for various error cases. Note that if the explicit 26545 instantiation is valid the RESULT will currently be marked as an 26546 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set 26547 until we get here. */ 26548 26549 if (DECL_TEMPLATE_SPECIALIZATION (result)) 26550 { 26551 /* DR 259 [temp.spec]. 26552 26553 Both an explicit instantiation and a declaration of an explicit 26554 specialization shall not appear in a program unless the explicit 26555 instantiation follows a declaration of the explicit specialization. 26556 26557 For a given set of template parameters, if an explicit 26558 instantiation of a template appears after a declaration of an 26559 explicit specialization for that template, the explicit 26560 instantiation has no effect. */ 26561 return; 26562 } 26563 else if (DECL_EXPLICIT_INSTANTIATION (result)) 26564 { 26565 /* [temp.spec] 26566 26567 No program shall explicitly instantiate any template more 26568 than once. 26569 26570 We check DECL_NOT_REALLY_EXTERN so as not to complain when 26571 the first instantiation was `extern' and the second is not, 26572 and EXTERN_P for the opposite case. */ 26573 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p) 26574 permerror (input_location, "duplicate explicit instantiation of %q#D", result); 26575 /* If an "extern" explicit instantiation follows an ordinary 26576 explicit instantiation, the template is instantiated. */ 26577 if (extern_p) 26578 return; 26579 } 26580 else if (!DECL_IMPLICIT_INSTANTIATION (result)) 26581 { 26582 error ("no matching template for %qD found", result); 26583 return; 26584 } 26585 else if (!DECL_TEMPLATE_INFO (result)) 26586 { 26587 permerror (input_location, "explicit instantiation of non-template %q#D", result); 26588 return; 26589 } 26590 26591 if (storage == NULL_TREE) 26592 ; 26593 else if (storage == ridpointers[(int) RID_EXTERN]) 26594 { 26595 if (cxx_dialect == cxx98) 26596 pedwarn (input_location, OPT_Wpedantic, 26597 "ISO C++ 1998 forbids the use of %<extern%> on explicit " 26598 "instantiations"); 26599 extern_p = 1; 26600 } 26601 else 26602 error ("storage class %qD applied to template instantiation", storage); 26603 26604 check_explicit_instantiation_namespace (result); 26605 mark_decl_instantiated (result, extern_p); 26606 if (! extern_p) 26607 instantiate_decl (result, /*defer_ok=*/true, 26608 /*expl_inst_class_mem_p=*/false); 26609 } 26610 26611 static void 26612 mark_class_instantiated (tree t, int extern_p) 26613 { 26614 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); 26615 SET_CLASSTYPE_INTERFACE_KNOWN (t); 26616 CLASSTYPE_INTERFACE_ONLY (t) = extern_p; 26617 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; 26618 if (! extern_p) 26619 { 26620 CLASSTYPE_DEBUG_REQUESTED (t) = 1; 26621 rest_of_type_compilation (t, 1); 26622 } 26623 } 26624 26625 /* Perform an explicit instantiation of template class T. STORAGE, if 26626 non-null, is the RID for extern, inline or static. COMPLAIN is 26627 nonzero if this is called from the parser, zero if called recursively, 26628 since the standard is unclear (as detailed below). */ 26629 26630 void 26631 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain) 26632 { 26633 if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t))) 26634 { 26635 if (tree ti = TYPE_TEMPLATE_INFO (t)) 26636 error ("explicit instantiation of non-class template %qD", 26637 TI_TEMPLATE (ti)); 26638 else 26639 error ("explicit instantiation of non-template type %qT", t); 26640 return; 26641 } 26642 26643 complete_type (t); 26644 26645 if (!COMPLETE_TYPE_P (t)) 26646 { 26647 if (complain & tf_error) 26648 error ("explicit instantiation of %q#T before definition of template", 26649 t); 26650 return; 26651 } 26652 26653 /* At most one of these will be true. */ 26654 bool extern_p = false; 26655 bool nomem_p = false; 26656 bool static_p = false; 26657 26658 if (storage != NULL_TREE) 26659 { 26660 if (storage == ridpointers[(int) RID_EXTERN]) 26661 { 26662 if (cxx_dialect == cxx98) 26663 pedwarn (input_location, OPT_Wpedantic, 26664 "ISO C++ 1998 forbids the use of %<extern%> on " 26665 "explicit instantiations"); 26666 } 26667 else 26668 pedwarn (input_location, OPT_Wpedantic, 26669 "ISO C++ forbids the use of %qE" 26670 " on explicit instantiations", storage); 26671 26672 if (storage == ridpointers[(int) RID_INLINE]) 26673 nomem_p = true; 26674 else if (storage == ridpointers[(int) RID_EXTERN]) 26675 extern_p = true; 26676 else if (storage == ridpointers[(int) RID_STATIC]) 26677 static_p = true; 26678 else 26679 error ("storage class %qD applied to template instantiation", 26680 storage); 26681 } 26682 26683 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) 26684 /* DR 259 [temp.spec]. 26685 26686 Both an explicit instantiation and a declaration of an explicit 26687 specialization shall not appear in a program unless the 26688 explicit instantiation follows a declaration of the explicit 26689 specialization. 26690 26691 For a given set of template parameters, if an explicit 26692 instantiation of a template appears after a declaration of an 26693 explicit specialization for that template, the explicit 26694 instantiation has no effect. */ 26695 return; 26696 26697 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t)) 26698 { 26699 /* We've already instantiated the template. */ 26700 26701 /* [temp.spec] 26702 26703 No program shall explicitly instantiate any template more 26704 than once. 26705 26706 If EXTERN_P then this is ok. */ 26707 if (!extern_p && (complain & tf_error)) 26708 permerror (input_location, 26709 "duplicate explicit instantiation of %q#T", t); 26710 26711 return; 26712 } 26713 26714 check_explicit_instantiation_namespace (TYPE_NAME (t)); 26715 mark_class_instantiated (t, extern_p); 26716 26717 if (nomem_p) 26718 return; 26719 26720 /* In contrast to implicit instantiation, where only the 26721 declarations, and not the definitions, of members are 26722 instantiated, we have here: 26723 26724 [temp.explicit] 26725 26726 An explicit instantiation that names a class template 26727 specialization is also an explicit instantiation of the same 26728 kind (declaration or definition) of each of its members (not 26729 including members inherited from base classes and members 26730 that are templates) that has not been previously explicitly 26731 specialized in the translation unit containing the explicit 26732 instantiation, provided that the associated constraints, if 26733 any, of that member are satisfied by the template arguments 26734 of the explicit instantiation. */ 26735 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld)) 26736 if ((VAR_P (fld) 26737 || (TREE_CODE (fld) == FUNCTION_DECL 26738 && !static_p 26739 && user_provided_p (fld))) 26740 && DECL_TEMPLATE_INSTANTIATION (fld) 26741 && constraints_satisfied_p (fld)) 26742 { 26743 mark_decl_instantiated (fld, extern_p); 26744 if (! extern_p) 26745 instantiate_decl (fld, /*defer_ok=*/true, 26746 /*expl_inst_class_mem_p=*/true); 26747 } 26748 else if (DECL_IMPLICIT_TYPEDEF_P (fld)) 26749 { 26750 tree type = TREE_TYPE (fld); 26751 26752 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type) 26753 && !uses_template_parms (CLASSTYPE_TI_ARGS (type))) 26754 do_type_instantiation (type, storage, 0); 26755 } 26756 } 26757 26758 /* Given a function DECL, which is a specialization of TMPL, modify 26759 DECL to be a re-instantiation of TMPL with the same template 26760 arguments. TMPL should be the template into which tsubst'ing 26761 should occur for DECL, not the most general template. 26762 26763 One reason for doing this is a scenario like this: 26764 26765 template <class T> 26766 void f(const T&, int i); 26767 26768 void g() { f(3, 7); } 26769 26770 template <class T> 26771 void f(const T& t, const int i) { } 26772 26773 Note that when the template is first instantiated, with 26774 instantiate_template, the resulting DECL will have no name for the 26775 first parameter, and the wrong type for the second. So, when we go 26776 to instantiate the DECL, we regenerate it. */ 26777 26778 static void 26779 regenerate_decl_from_template (tree decl, tree tmpl, tree args) 26780 { 26781 /* The arguments used to instantiate DECL, from the most general 26782 template. */ 26783 tree code_pattern = DECL_TEMPLATE_RESULT (tmpl); 26784 26785 /* Make sure that we can see identifiers, and compute access correctly. */ 26786 push_access_scope (decl); 26787 26788 if (TREE_CODE (decl) == FUNCTION_DECL) 26789 { 26790 tree specs; 26791 int args_depth; 26792 int parms_depth; 26793 26794 /* Don't bother with this for unique friends that can't be redeclared and 26795 might change type if regenerated (PR69836). */ 26796 if (DECL_UNIQUE_FRIEND_P (decl)) 26797 goto done; 26798 26799 /* Use the source location of the definition. */ 26800 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl); 26801 26802 args_depth = TMPL_ARGS_DEPTH (args); 26803 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); 26804 if (args_depth > parms_depth) 26805 args = get_innermost_template_args (args, parms_depth); 26806 26807 /* Instantiate a dynamic exception-specification. noexcept will be 26808 handled below. */ 26809 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern))) 26810 if (TREE_VALUE (raises)) 26811 { 26812 specs = tsubst_exception_specification (TREE_TYPE (code_pattern), 26813 args, tf_error, NULL_TREE, 26814 /*defer_ok*/false); 26815 if (specs && specs != error_mark_node) 26816 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), 26817 specs); 26818 } 26819 26820 /* Merge parameter declarations. */ 26821 if (tree pattern_parm 26822 = skip_artificial_parms_for (code_pattern, 26823 DECL_ARGUMENTS (code_pattern))) 26824 { 26825 tree *p = &DECL_ARGUMENTS (decl); 26826 for (int skip = num_artificial_parms_for (decl); skip; --skip) 26827 p = &DECL_CHAIN (*p); 26828 *p = tsubst_decl (pattern_parm, args, tf_error); 26829 for (tree t = *p; t; t = DECL_CHAIN (t)) 26830 DECL_CONTEXT (t) = decl; 26831 } 26832 26833 if (DECL_CONTRACTS (decl)) 26834 { 26835 /* If we're regenerating a specialization, the contracts will have 26836 been copied from the most general template. Replace those with 26837 the ones from the actual specialization. */ 26838 tree tmpl = DECL_TI_TEMPLATE (decl); 26839 if (DECL_TEMPLATE_SPECIALIZATION (tmpl)) 26840 { 26841 remove_contract_attributes (decl); 26842 copy_contract_attributes (decl, code_pattern); 26843 } 26844 26845 tsubst_contract_attributes (decl, args, tf_warning_or_error, code_pattern); 26846 } 26847 26848 /* Merge additional specifiers from the CODE_PATTERN. */ 26849 if (DECL_DECLARED_INLINE_P (code_pattern) 26850 && !DECL_DECLARED_INLINE_P (decl)) 26851 DECL_DECLARED_INLINE_P (decl) = 1; 26852 26853 maybe_instantiate_noexcept (decl, tf_error); 26854 } 26855 else if (VAR_P (decl)) 26856 { 26857 start_lambda_scope (decl); 26858 DECL_INITIAL (decl) = 26859 tsubst_init (DECL_INITIAL (code_pattern), decl, args, 26860 tf_error, DECL_TI_TEMPLATE (decl)); 26861 finish_lambda_scope (); 26862 if (VAR_HAD_UNKNOWN_BOUND (decl)) 26863 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args, 26864 tf_error, DECL_TI_TEMPLATE (decl)); 26865 } 26866 else 26867 gcc_unreachable (); 26868 26869 done: 26870 pop_access_scope (decl); 26871 } 26872 26873 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be 26874 substituted to get DECL. */ 26875 26876 tree 26877 template_for_substitution (tree decl) 26878 { 26879 tree tmpl = DECL_TI_TEMPLATE (decl); 26880 26881 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern 26882 for the instantiation. This is not always the most general 26883 template. Consider, for example: 26884 26885 template <class T> 26886 struct S { template <class U> void f(); 26887 template <> void f<int>(); }; 26888 26889 and an instantiation of S<double>::f<int>. We want TD to be the 26890 specialization S<T>::f<int>, not the more general S<T>::f<U>. */ 26891 while (/* An instantiation cannot have a definition, so we need a 26892 more general template. */ 26893 DECL_TEMPLATE_INSTANTIATION (tmpl) 26894 /* We must also deal with friend templates. Given: 26895 26896 template <class T> struct S { 26897 template <class U> friend void f() {}; 26898 }; 26899 26900 S<int>::f<U> say, is not an instantiation of S<T>::f<U>, 26901 so far as the language is concerned, but that's still 26902 where we get the pattern for the instantiation from. On 26903 other hand, if the definition comes outside the class, say: 26904 26905 template <class T> struct S { 26906 template <class U> friend void f(); 26907 }; 26908 template <class U> friend void f() {} 26909 26910 we don't need to look any further. That's what the check for 26911 DECL_INITIAL is for. */ 26912 || (TREE_CODE (decl) == FUNCTION_DECL 26913 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl) 26914 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)))) 26915 { 26916 /* The present template, TD, should not be a definition. If it 26917 were a definition, we should be using it! Note that we 26918 cannot restructure the loop to just keep going until we find 26919 a template with a definition, since that might go too far if 26920 a specialization was declared, but not defined. */ 26921 26922 /* Fetch the more general template. */ 26923 tmpl = DECL_TI_TEMPLATE (tmpl); 26924 } 26925 26926 return tmpl; 26927 } 26928 26929 /* Returns true if we need to instantiate this template instance even if we 26930 know we aren't going to emit it. */ 26931 26932 bool 26933 always_instantiate_p (tree decl) 26934 { 26935 /* We always instantiate inline functions so that we can inline them. An 26936 explicit instantiation declaration prohibits implicit instantiation of 26937 non-inline functions. With high levels of optimization, we would 26938 normally inline non-inline functions -- but we're not allowed to do 26939 that for "extern template" functions. Therefore, we check 26940 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */ 26941 return ((TREE_CODE (decl) == FUNCTION_DECL 26942 && (DECL_DECLARED_INLINE_P (decl) 26943 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl))))) 26944 /* And we need to instantiate static data members so that 26945 their initializers are available in integral constant 26946 expressions. */ 26947 || (VAR_P (decl) 26948 && decl_maybe_constant_var_p (decl))); 26949 } 26950 26951 /* If FN has a noexcept-specifier that hasn't been instantiated yet, 26952 instantiate it now, modifying TREE_TYPE (fn). Returns false on 26953 error, true otherwise. */ 26954 26955 bool 26956 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain) 26957 { 26958 if (fn == error_mark_node) 26959 return false; 26960 26961 /* Don't instantiate a noexcept-specification from template context. */ 26962 if (processing_template_decl 26963 && (!flag_noexcept_type || type_dependent_expression_p (fn))) 26964 return true; 26965 26966 tree fntype = TREE_TYPE (fn); 26967 tree spec = TYPE_RAISES_EXCEPTIONS (fntype); 26968 26969 if ((!spec || UNEVALUATED_NOEXCEPT_SPEC_P (spec)) 26970 && DECL_MAYBE_DELETED (fn)) 26971 { 26972 if (fn == current_function_decl) 26973 /* We're in start_preparsed_function, keep going. */ 26974 return true; 26975 26976 ++function_depth; 26977 maybe_synthesize_method (fn); 26978 --function_depth; 26979 return !DECL_DELETED_FN (fn); 26980 } 26981 26982 if (!spec || !TREE_PURPOSE (spec)) 26983 return true; 26984 26985 tree noex = TREE_PURPOSE (spec); 26986 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT 26987 && TREE_CODE (noex) != DEFERRED_PARSE) 26988 return true; 26989 26990 tree orig_fn = NULL_TREE; 26991 /* For a member friend template we can get a TEMPLATE_DECL. Let's use 26992 its FUNCTION_DECL for the rest of this function -- push_access_scope 26993 doesn't accept TEMPLATE_DECLs. */ 26994 if (DECL_FUNCTION_TEMPLATE_P (fn)) 26995 { 26996 orig_fn = fn; 26997 fn = DECL_TEMPLATE_RESULT (fn); 26998 } 26999 27000 if (DECL_CLONED_FUNCTION_P (fn)) 27001 { 27002 tree prime = DECL_CLONED_FUNCTION (fn); 27003 if (!maybe_instantiate_noexcept (prime, complain)) 27004 return false; 27005 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime)); 27006 } 27007 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT) 27008 { 27009 static hash_set<tree>* fns = new hash_set<tree>; 27010 bool added = false; 27011 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE) 27012 { 27013 spec = get_defaulted_eh_spec (fn, complain); 27014 if (spec == error_mark_node) 27015 /* This might have failed because of an unparsed DMI, so 27016 let's try again later. */ 27017 return false; 27018 } 27019 else if (!(added = !fns->add (fn))) 27020 { 27021 /* If hash_set::add returns true, the element was already there. */ 27022 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex), 27023 DECL_SOURCE_LOCATION (fn)); 27024 error_at (loc, 27025 "exception specification of %qD depends on itself", 27026 fn); 27027 spec = noexcept_false_spec; 27028 } 27029 else if (push_tinst_level (fn)) 27030 { 27031 const bool push_to_top = maybe_push_to_top_level (fn); 27032 push_access_scope (fn); 27033 push_deferring_access_checks (dk_no_deferred); 27034 input_location = DECL_SOURCE_LOCATION (fn); 27035 27036 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn) 27037 && !DECL_LOCAL_DECL_P (fn)) 27038 { 27039 /* If needed, set current_class_ptr for the benefit of 27040 tsubst_copy/PARM_DECL. */ 27041 tree this_parm = DECL_ARGUMENTS (fn); 27042 current_class_ptr = NULL_TREE; 27043 current_class_ref = cp_build_fold_indirect_ref (this_parm); 27044 current_class_ptr = this_parm; 27045 } 27046 27047 /* If this function is represented by a TEMPLATE_DECL, then 27048 the deferred noexcept-specification might still contain 27049 dependent types, even after substitution. And we need the 27050 dependency check functions to work in build_noexcept_spec. */ 27051 if (orig_fn) 27052 ++processing_template_decl; 27053 27054 /* Do deferred instantiation of the noexcept-specifier. */ 27055 noex = tsubst_expr (DEFERRED_NOEXCEPT_PATTERN (noex), 27056 DEFERRED_NOEXCEPT_ARGS (noex), 27057 tf_warning_or_error, fn); 27058 /* Build up the noexcept-specification. */ 27059 spec = build_noexcept_spec (noex, tf_warning_or_error); 27060 27061 if (orig_fn) 27062 --processing_template_decl; 27063 27064 pop_deferring_access_checks (); 27065 pop_access_scope (fn); 27066 pop_tinst_level (); 27067 maybe_pop_from_top_level (push_to_top); 27068 } 27069 else 27070 spec = noexcept_false_spec; 27071 27072 if (added) 27073 fns->remove (fn); 27074 } 27075 27076 if (spec == error_mark_node) 27077 { 27078 /* This failed with a hard error, so let's go with false. */ 27079 gcc_assert (seen_error ()); 27080 spec = noexcept_false_spec; 27081 } 27082 27083 TREE_TYPE (fn) = build_exception_variant (fntype, spec); 27084 if (orig_fn) 27085 TREE_TYPE (orig_fn) = TREE_TYPE (fn); 27086 27087 return true; 27088 } 27089 27090 /* We're starting to process the function INST, an instantiation of PATTERN; 27091 add their parameters to local_specializations. */ 27092 27093 void 27094 register_parameter_specializations (tree pattern, tree inst) 27095 { 27096 tree tmpl_parm = DECL_ARGUMENTS (pattern); 27097 tree spec_parm = DECL_ARGUMENTS (inst); 27098 if (DECL_IOBJ_MEMBER_FUNCTION_P (inst)) 27099 { 27100 register_local_specialization (spec_parm, tmpl_parm); 27101 spec_parm = skip_artificial_parms_for (inst, spec_parm); 27102 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm); 27103 } 27104 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm)) 27105 { 27106 if (!DECL_PACK_P (tmpl_parm)) 27107 { 27108 register_local_specialization (spec_parm, tmpl_parm); 27109 spec_parm = DECL_CHAIN (spec_parm); 27110 } 27111 else 27112 { 27113 /* Register the (value) argument pack as a specialization of 27114 TMPL_PARM, then move on. */ 27115 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm); 27116 register_local_specialization (argpack, tmpl_parm); 27117 } 27118 } 27119 gcc_assert (!spec_parm); 27120 } 27121 27122 /* Instantiate the body of D using PATTERN with ARGS. We have 27123 already determined PATTERN is the correct template to use. 27124 NESTED_P is true if this is a nested function, in which case 27125 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */ 27126 27127 static void 27128 instantiate_body (tree pattern, tree args, tree d, bool nested_p) 27129 { 27130 tree td = NULL_TREE; 27131 tree code_pattern = pattern; 27132 27133 if (!nested_p) 27134 { 27135 td = pattern; 27136 code_pattern = DECL_TEMPLATE_RESULT (td); 27137 } 27138 else 27139 /* Only OMP reductions are nested. */ 27140 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)); 27141 27142 vec<tree> omp_privatization_save; 27143 if (current_function_decl) 27144 save_omp_privatization_clauses (omp_privatization_save); 27145 27146 bool push_to_top = maybe_push_to_top_level (d); 27147 27148 mark_template_arguments_used (pattern, args); 27149 27150 if (VAR_P (d)) 27151 { 27152 /* The variable might be a lambda's extra scope, and that 27153 lambda's visibility depends on D's. */ 27154 maybe_commonize_var (d); 27155 determine_visibility (d); 27156 } 27157 27158 /* Mark D as instantiated so that recursive calls to 27159 instantiate_decl do not try to instantiate it again. */ 27160 DECL_TEMPLATE_INSTANTIATED (d) = 1; 27161 27162 if (td) 27163 /* Regenerate the declaration in case the template has been modified 27164 by a subsequent redeclaration. */ 27165 regenerate_decl_from_template (d, td, args); 27166 27167 /* We already set the file and line above. Reset them now in case 27168 they changed as a result of calling regenerate_decl_from_template. */ 27169 input_location = DECL_SOURCE_LOCATION (d); 27170 27171 if (VAR_P (d)) 27172 { 27173 /* Clear out DECL_RTL; whatever was there before may not be right 27174 since we've reset the type of the declaration. */ 27175 SET_DECL_RTL (d, NULL); 27176 DECL_IN_AGGR_P (d) = 0; 27177 27178 /* The initializer is placed in DECL_INITIAL by 27179 regenerate_decl_from_template so we don't need to 27180 push/pop_access_scope again here. Pull it out so that 27181 cp_finish_decl can process it. */ 27182 bool const_init = false; 27183 tree init = DECL_INITIAL (d); 27184 DECL_INITIAL (d) = NULL_TREE; 27185 DECL_INITIALIZED_P (d) = 0; 27186 27187 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the 27188 initializer. That function will defer actual emission until 27189 we have a chance to determine linkage. */ 27190 DECL_EXTERNAL (d) = 0; 27191 27192 /* Enter the scope of D so that access-checking works correctly. */ 27193 bool enter_context = DECL_CLASS_SCOPE_P (d); 27194 if (enter_context) 27195 push_nested_class (DECL_CONTEXT (d)); 27196 27197 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern); 27198 cp_finish_decl (d, init, const_init, NULL_TREE, 0); 27199 27200 if (enter_context) 27201 pop_nested_class (); 27202 } 27203 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern)) 27204 synthesize_method (d); 27205 else if (TREE_CODE (d) == FUNCTION_DECL) 27206 { 27207 /* Set up the list of local specializations. */ 27208 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy); 27209 tree block = NULL_TREE; 27210 27211 /* Set up context. */ 27212 if (nested_p) 27213 block = push_stmt_list (); 27214 else 27215 { 27216 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED); 27217 27218 perform_instantiation_time_access_checks (code_pattern, args); 27219 } 27220 27221 /* Create substitution entries for the parameters. */ 27222 register_parameter_specializations (code_pattern, d); 27223 27224 /* Substitute into the body of the function. */ 27225 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)) 27226 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args, 27227 tf_warning_or_error, d); 27228 else 27229 { 27230 tsubst_stmt (DECL_SAVED_TREE (code_pattern), args, 27231 tf_warning_or_error, DECL_TI_TEMPLATE (d)); 27232 27233 /* Set the current input_location to the end of the function 27234 so that finish_function knows where we are. */ 27235 input_location 27236 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus; 27237 27238 /* Remember if we saw an infinite loop in the template. */ 27239 current_function_infinite_loop 27240 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop; 27241 } 27242 27243 /* Finish the function. */ 27244 if (nested_p) 27245 DECL_SAVED_TREE (d) = pop_stmt_list (block); 27246 else 27247 { 27248 d = finish_function (/*inline_p=*/false); 27249 expand_or_defer_fn (d); 27250 } 27251 27252 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)) 27253 cp_check_omp_declare_reduction (d); 27254 } 27255 27256 /* We're not deferring instantiation any more. */ 27257 if (!nested_p) 27258 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; 27259 27260 maybe_pop_from_top_level (push_to_top); 27261 27262 if (current_function_decl) 27263 restore_omp_privatization_clauses (omp_privatization_save); 27264 } 27265 27266 /* Produce the definition of D, a _DECL generated from a template. If 27267 DEFER_OK is true, then we don't have to actually do the 27268 instantiation now; we just have to do it sometime. Normally it is 27269 an error if this is an explicit instantiation but D is undefined. 27270 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly 27271 instantiated class template. */ 27272 27273 tree 27274 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p) 27275 { 27276 tree tmpl = DECL_TI_TEMPLATE (d); 27277 tree gen_args; 27278 tree args; 27279 tree td; 27280 tree code_pattern; 27281 tree spec; 27282 tree gen_tmpl; 27283 bool pattern_defined; 27284 location_t saved_loc = input_location; 27285 bool external_p; 27286 bool deleted_p; 27287 27288 /* This function should only be used to instantiate templates for 27289 functions and static member variables. */ 27290 gcc_assert (VAR_OR_FUNCTION_DECL_P (d)); 27291 27292 /* A concept is never instantiated. */ 27293 gcc_assert (!DECL_DECLARED_CONCEPT_P (d)); 27294 27295 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d)); 27296 27297 if (modules_p ()) 27298 /* We may have a pending instantiation of D itself. */ 27299 lazy_load_pendings (d); 27300 27301 /* Variables are never deferred; if instantiation is required, they 27302 are instantiated right away. That allows for better code in the 27303 case that an expression refers to the value of the variable -- 27304 if the variable has a constant value the referring expression can 27305 take advantage of that fact. */ 27306 if (VAR_P (d)) 27307 defer_ok = false; 27308 27309 /* Don't instantiate cloned functions. Instead, instantiate the 27310 functions they cloned. */ 27311 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) 27312 d = DECL_CLONED_FUNCTION (d); 27313 27314 if (DECL_TEMPLATE_INSTANTIATED (d) 27315 || TREE_TYPE (d) == error_mark_node 27316 || (TREE_CODE (d) == FUNCTION_DECL 27317 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d)) 27318 || DECL_TEMPLATE_SPECIALIZATION (d)) 27319 /* D has already been instantiated or explicitly specialized, so 27320 there's nothing for us to do here. 27321 27322 It might seem reasonable to check whether or not D is an explicit 27323 instantiation, and, if so, stop here. But when an explicit 27324 instantiation is deferred until the end of the compilation, 27325 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do 27326 the instantiation. */ 27327 return d; 27328 27329 /* Check to see whether we know that this template will be 27330 instantiated in some other file, as with "extern template" 27331 extension. */ 27332 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d)); 27333 27334 /* In general, we do not instantiate such templates. */ 27335 if (external_p && !always_instantiate_p (d)) 27336 return d; 27337 27338 gen_tmpl = most_general_template (tmpl); 27339 gen_args = DECL_TI_ARGS (d); 27340 27341 /* We should already have the extra args. */ 27342 gcc_checking_assert (tmpl == gen_tmpl 27343 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) 27344 == TMPL_ARGS_DEPTH (gen_args))); 27345 /* And what's in the hash table should match D. */ 27346 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) 27347 == d 27348 || spec == NULL_TREE); 27349 27350 /* This needs to happen before any tsubsting. */ 27351 if (! push_tinst_level (d)) 27352 return d; 27353 27354 auto_timevar tv (TV_TEMPLATE_INST); 27355 27356 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern 27357 for the instantiation. */ 27358 td = template_for_substitution (d); 27359 args = gen_args; 27360 27361 if (variable_template_specialization_p (d)) 27362 { 27363 /* Look up an explicit specialization, if any. */ 27364 tree partial_ti = most_specialized_partial_spec (d, tf_warning_or_error); 27365 if (partial_ti && partial_ti != error_mark_node) 27366 { 27367 td = TI_TEMPLATE (partial_ti); 27368 args = TI_ARGS (partial_ti); 27369 } 27370 } 27371 27372 code_pattern = DECL_TEMPLATE_RESULT (td); 27373 27374 /* We should never be trying to instantiate a member of a class 27375 template or partial specialization. */ 27376 gcc_assert (d != code_pattern); 27377 27378 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d)) 27379 || DECL_TEMPLATE_SPECIALIZATION (td)) 27380 /* In the case of a friend template whose definition is provided 27381 outside the class, we may have too many arguments. Drop the 27382 ones we don't need. The same is true for specializations. */ 27383 args = get_innermost_template_args 27384 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td))); 27385 27386 if (TREE_CODE (d) == FUNCTION_DECL) 27387 { 27388 deleted_p = DECL_DELETED_FN (code_pattern); 27389 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE 27390 && DECL_INITIAL (code_pattern) != error_mark_node) 27391 || DECL_DEFAULTED_FN (code_pattern) 27392 || deleted_p); 27393 } 27394 else 27395 { 27396 deleted_p = false; 27397 if (DECL_CLASS_SCOPE_P (code_pattern)) 27398 pattern_defined = ! DECL_IN_AGGR_P (code_pattern); 27399 else 27400 pattern_defined = ! DECL_EXTERNAL (code_pattern); 27401 } 27402 27403 /* We may be in the middle of deferred access check. Disable it now. */ 27404 push_deferring_access_checks (dk_no_deferred); 27405 27406 /* Unless an explicit instantiation directive has already determined 27407 the linkage of D, remember that a definition is available for 27408 this entity. */ 27409 if (pattern_defined 27410 && !DECL_INTERFACE_KNOWN (d) 27411 && !DECL_NOT_REALLY_EXTERN (d)) 27412 mark_definable (d); 27413 27414 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern); 27415 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern); 27416 input_location = DECL_SOURCE_LOCATION (d); 27417 27418 /* If D is a member of an explicitly instantiated class template, 27419 and no definition is available, treat it like an implicit 27420 instantiation. */ 27421 if (!pattern_defined && expl_inst_class_mem_p 27422 && DECL_EXPLICIT_INSTANTIATION (d)) 27423 { 27424 /* Leave linkage flags alone on instantiations with anonymous 27425 visibility. */ 27426 if (TREE_PUBLIC (d)) 27427 { 27428 DECL_NOT_REALLY_EXTERN (d) = 0; 27429 DECL_INTERFACE_KNOWN (d) = 0; 27430 } 27431 SET_DECL_IMPLICIT_INSTANTIATION (d); 27432 } 27433 27434 /* Defer all other templates, unless we have been explicitly 27435 forbidden from doing so. */ 27436 if (/* If there is no definition, we cannot instantiate the 27437 template. */ 27438 ! pattern_defined 27439 /* If it's OK to postpone instantiation, do so. */ 27440 || defer_ok 27441 /* If this is a static data member that will be defined 27442 elsewhere, we don't want to instantiate the entire data 27443 member, but we do want to instantiate the initializer so that 27444 we can substitute that elsewhere. */ 27445 || (external_p && VAR_P (d)) 27446 /* Handle here a deleted function too, avoid generating 27447 its body (c++/61080). */ 27448 || deleted_p) 27449 { 27450 /* The definition of the static data member is now required so 27451 we must substitute the initializer. */ 27452 if (VAR_P (d) 27453 && !DECL_INITIAL (d) 27454 && DECL_INITIAL (code_pattern)) 27455 { 27456 tree ns; 27457 tree init; 27458 bool const_init = false; 27459 bool enter_context = DECL_CLASS_SCOPE_P (d); 27460 27461 ns = decl_namespace_context (d); 27462 push_nested_namespace (ns); 27463 if (enter_context) 27464 push_nested_class (DECL_CONTEXT (d)); 27465 init = tsubst_expr (DECL_INITIAL (code_pattern), 27466 args, 27467 tf_warning_or_error, NULL_TREE); 27468 /* If instantiating the initializer involved instantiating this 27469 again, don't call cp_finish_decl twice. */ 27470 if (!DECL_INITIAL (d)) 27471 { 27472 /* Make sure the initializer is still constant, in case of 27473 circular dependency (template/instantiate6.C). */ 27474 const_init 27475 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern); 27476 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init, 27477 /*asmspec_tree=*/NULL_TREE, 0); 27478 } 27479 if (enter_context) 27480 pop_nested_class (); 27481 pop_nested_namespace (ns); 27482 } 27483 27484 /* We restore the source position here because it's used by 27485 add_pending_template. */ 27486 input_location = saved_loc; 27487 27488 if (at_eof && !pattern_defined 27489 && DECL_EXPLICIT_INSTANTIATION (d) 27490 && DECL_NOT_REALLY_EXTERN (d)) 27491 /* [temp.explicit] 27492 27493 The definition of a non-exported function template, a 27494 non-exported member function template, or a non-exported 27495 member function or static data member of a class template 27496 shall be present in every translation unit in which it is 27497 explicitly instantiated. */ 27498 permerror (input_location, "explicit instantiation of %qD " 27499 "but no definition available", d); 27500 27501 /* If we're in unevaluated context, we just wanted to get the 27502 constant value; this isn't an odr use, so don't queue 27503 a full instantiation. */ 27504 if (!cp_unevaluated_operand 27505 /* ??? Historically, we have instantiated inline functions, even 27506 when marked as "extern template". */ 27507 && !(external_p && VAR_P (d))) 27508 add_pending_template (d); 27509 } 27510 else 27511 { 27512 set_instantiating_module (d); 27513 if (variable_template_p (gen_tmpl)) 27514 note_vague_linkage_variable (d); 27515 instantiate_body (td, args, d, false); 27516 } 27517 27518 pop_deferring_access_checks (); 27519 pop_tinst_level (); 27520 input_location = saved_loc; 27521 27522 return d; 27523 } 27524 27525 /* Run through the list of templates that we wish we could 27526 instantiate, and instantiate any we can. RETRIES is the 27527 number of times we retry pending template instantiation. */ 27528 27529 void 27530 instantiate_pending_templates (int retries) 27531 { 27532 int reconsider; 27533 location_t saved_loc = input_location; 27534 27535 /* Instantiating templates may trigger vtable generation. This in turn 27536 may require further template instantiations. We place a limit here 27537 to avoid infinite loop. */ 27538 if (pending_templates && retries >= max_tinst_depth) 27539 { 27540 tree decl = pending_templates->tinst->maybe_get_node (); 27541 27542 fatal_error (input_location, 27543 "template instantiation depth exceeds maximum of %d" 27544 " instantiating %q+D, possibly from virtual table generation" 27545 " (use %<-ftemplate-depth=%> to increase the maximum)", 27546 max_tinst_depth, decl); 27547 if (TREE_CODE (decl) == FUNCTION_DECL) 27548 /* Pretend that we defined it. */ 27549 DECL_INITIAL (decl) = error_mark_node; 27550 return; 27551 } 27552 27553 do 27554 { 27555 struct pending_template **t = &pending_templates; 27556 struct pending_template *last = NULL; 27557 reconsider = 0; 27558 while (*t) 27559 { 27560 tree instantiation = reopen_tinst_level ((*t)->tinst); 27561 bool complete = false; 27562 27563 if (TYPE_P (instantiation)) 27564 { 27565 if (!COMPLETE_TYPE_P (instantiation)) 27566 { 27567 instantiate_class_template (instantiation); 27568 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) 27569 for (tree fld = TYPE_FIELDS (instantiation); 27570 fld; fld = TREE_CHAIN (fld)) 27571 if ((VAR_P (fld) 27572 || (TREE_CODE (fld) == FUNCTION_DECL 27573 && !DECL_ARTIFICIAL (fld))) 27574 && DECL_TEMPLATE_INSTANTIATION (fld)) 27575 instantiate_decl (fld, 27576 /*defer_ok=*/false, 27577 /*expl_inst_class_mem_p=*/false); 27578 27579 if (COMPLETE_TYPE_P (instantiation)) 27580 reconsider = 1; 27581 } 27582 27583 complete = COMPLETE_TYPE_P (instantiation); 27584 } 27585 else 27586 { 27587 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation) 27588 && !DECL_TEMPLATE_INSTANTIATED (instantiation)) 27589 { 27590 instantiation 27591 = instantiate_decl (instantiation, 27592 /*defer_ok=*/false, 27593 /*expl_inst_class_mem_p=*/false); 27594 if (DECL_TEMPLATE_INSTANTIATED (instantiation)) 27595 reconsider = 1; 27596 } 27597 27598 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation) 27599 || DECL_TEMPLATE_INSTANTIATED (instantiation)); 27600 } 27601 27602 if (complete) 27603 { 27604 /* If INSTANTIATION has been instantiated, then we don't 27605 need to consider it again in the future. */ 27606 struct pending_template *drop = *t; 27607 *t = (*t)->next; 27608 set_refcount_ptr (drop->tinst); 27609 pending_template_freelist ().free (drop); 27610 } 27611 else 27612 { 27613 last = *t; 27614 t = &(*t)->next; 27615 } 27616 tinst_depth = 0; 27617 set_refcount_ptr (current_tinst_level); 27618 } 27619 last_pending_template = last; 27620 } 27621 while (reconsider); 27622 27623 input_location = saved_loc; 27624 } 27625 27626 /* Substitute ARGVEC into T, which is a list of initializers for 27627 either base class or a non-static data member. The TREE_PURPOSEs 27628 are DECLs, and the TREE_VALUEs are the initializer values. Used by 27629 instantiate_decl. */ 27630 27631 static tree 27632 tsubst_initializer_list (tree t, tree argvec) 27633 { 27634 tree inits = NULL_TREE; 27635 tree target_ctor = error_mark_node; 27636 27637 for (; t; t = TREE_CHAIN (t)) 27638 { 27639 tree decl; 27640 tree init; 27641 tree expanded_bases = NULL_TREE; 27642 tree expanded_arguments = NULL_TREE; 27643 int i, len = 1; 27644 27645 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION) 27646 { 27647 tree expr; 27648 tree arg; 27649 27650 /* Expand the base class expansion type into separate base 27651 classes. */ 27652 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec, 27653 tf_warning_or_error, 27654 NULL_TREE); 27655 if (expanded_bases == error_mark_node) 27656 continue; 27657 27658 /* We'll be building separate TREE_LISTs of arguments for 27659 each base. */ 27660 len = TREE_VEC_LENGTH (expanded_bases); 27661 expanded_arguments = make_tree_vec (len); 27662 for (i = 0; i < len; i++) 27663 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE; 27664 27665 /* Build a dummy EXPR_PACK_EXPANSION that will be used to 27666 expand each argument in the TREE_VALUE of t. */ 27667 expr = make_node (EXPR_PACK_EXPANSION); 27668 PACK_EXPANSION_LOCAL_P (expr) = true; 27669 PACK_EXPANSION_PARAMETER_PACKS (expr) = 27670 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t)); 27671 27672 if (TREE_VALUE (t) == void_type_node) 27673 /* VOID_TYPE_NODE is used to indicate 27674 value-initialization. */ 27675 { 27676 for (i = 0; i < len; i++) 27677 TREE_VEC_ELT (expanded_arguments, i) = void_type_node; 27678 } 27679 else 27680 { 27681 /* Substitute parameter packs into each argument in the 27682 TREE_LIST. */ 27683 in_base_initializer = 1; 27684 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg)) 27685 { 27686 tree expanded_exprs; 27687 27688 /* Expand the argument. */ 27689 tree value; 27690 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION) 27691 value = TREE_VALUE (arg); 27692 else 27693 { 27694 value = expr; 27695 PACK_EXPANSION_PATTERN (value) = TREE_VALUE (arg); 27696 } 27697 expanded_exprs 27698 = tsubst_pack_expansion (value, argvec, 27699 tf_warning_or_error, 27700 NULL_TREE); 27701 if (expanded_exprs == error_mark_node) 27702 continue; 27703 27704 /* Prepend each of the expanded expressions to the 27705 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */ 27706 for (i = 0; i < len; i++) 27707 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION) 27708 for (int j = 0; j < TREE_VEC_LENGTH (expanded_exprs); j++) 27709 TREE_VEC_ELT (expanded_arguments, i) 27710 = tree_cons (NULL_TREE, 27711 TREE_VEC_ELT (expanded_exprs, j), 27712 TREE_VEC_ELT (expanded_arguments, i)); 27713 else 27714 TREE_VEC_ELT (expanded_arguments, i) 27715 = tree_cons (NULL_TREE, 27716 TREE_VEC_ELT (expanded_exprs, i), 27717 TREE_VEC_ELT (expanded_arguments, i)); 27718 } 27719 in_base_initializer = 0; 27720 27721 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS, 27722 since we built them backwards. */ 27723 for (i = 0; i < len; i++) 27724 { 27725 TREE_VEC_ELT (expanded_arguments, i) = 27726 nreverse (TREE_VEC_ELT (expanded_arguments, i)); 27727 } 27728 } 27729 } 27730 27731 for (i = 0; i < len; ++i) 27732 { 27733 if (expanded_bases) 27734 { 27735 decl = TREE_VEC_ELT (expanded_bases, i); 27736 decl = expand_member_init (decl); 27737 init = TREE_VEC_ELT (expanded_arguments, i); 27738 } 27739 else 27740 { 27741 tree tmp; 27742 if (TYPE_P (TREE_PURPOSE (t))) 27743 decl = tsubst (TREE_PURPOSE (t), argvec, 27744 tf_warning_or_error, NULL_TREE); 27745 else 27746 decl = tsubst_expr (TREE_PURPOSE (t), argvec, 27747 tf_warning_or_error, NULL_TREE); 27748 27749 decl = expand_member_init (decl); 27750 if (decl && !DECL_P (decl)) 27751 in_base_initializer = 1; 27752 27753 init = TREE_VALUE (t); 27754 tmp = init; 27755 if (init != void_type_node) 27756 init = tsubst_expr (init, argvec, 27757 tf_warning_or_error, NULL_TREE); 27758 if (init == NULL_TREE && tmp != NULL_TREE) 27759 /* If we had an initializer but it instantiated to nothing, 27760 value-initialize the object. This will only occur when 27761 the initializer was a pack expansion where the parameter 27762 packs used in that expansion were of length zero. */ 27763 init = void_type_node; 27764 in_base_initializer = 0; 27765 } 27766 27767 if (target_ctor != error_mark_node 27768 && init != error_mark_node) 27769 { 27770 error ("mem-initializer for %qD follows constructor delegation", 27771 decl); 27772 return inits; 27773 } 27774 /* Look for a target constructor. */ 27775 if (init != error_mark_node 27776 && decl && CLASS_TYPE_P (decl) 27777 && same_type_p (decl, current_class_type)) 27778 { 27779 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS); 27780 if (inits) 27781 { 27782 error ("constructor delegation follows mem-initializer for %qD", 27783 TREE_PURPOSE (inits)); 27784 continue; 27785 } 27786 target_ctor = init; 27787 } 27788 27789 if (decl) 27790 { 27791 init = build_tree_list (decl, init); 27792 /* Carry over the dummy TREE_TYPE node containing the source 27793 location. */ 27794 TREE_TYPE (init) = TREE_TYPE (t); 27795 TREE_CHAIN (init) = inits; 27796 inits = init; 27797 } 27798 } 27799 } 27800 return inits; 27801 } 27802 27803 /* Instantiate an enumerated type. TAG is the template type, NEWTAG 27804 is the instantiation (which should have been created with 27805 start_enum) and ARGS are the template arguments to use. */ 27806 27807 static void 27808 tsubst_enum (tree tag, tree newtag, tree args) 27809 { 27810 tree e; 27811 27812 if (SCOPED_ENUM_P (newtag)) 27813 begin_scope (sk_scoped_enum, newtag); 27814 27815 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) 27816 { 27817 tree value; 27818 tree decl = TREE_VALUE (e); 27819 27820 /* Note that in a template enum, the TREE_VALUE is the 27821 CONST_DECL, not the corresponding INTEGER_CST. */ 27822 value = tsubst_expr (DECL_INITIAL (decl), 27823 args, tf_warning_or_error, NULL_TREE); 27824 27825 /* Give this enumeration constant the correct access. */ 27826 set_current_access_from_decl (decl); 27827 27828 /* Actually build the enumerator itself. Here we're assuming that 27829 enumerators can't have dependent attributes. */ 27830 tree newdecl = build_enumerator (DECL_NAME (decl), value, newtag, 27831 DECL_ATTRIBUTES (decl), 27832 DECL_SOURCE_LOCATION (decl)); 27833 /* Attribute deprecated without an argument isn't sticky: it'll 27834 melt into a tree flag, so we need to propagate the flag here, 27835 since we just created a new enumerator. */ 27836 TREE_DEPRECATED (newdecl) = TREE_DEPRECATED (decl); 27837 TREE_UNAVAILABLE (newdecl) = TREE_UNAVAILABLE (decl); 27838 } 27839 27840 if (SCOPED_ENUM_P (newtag)) 27841 finish_scope (); 27842 27843 finish_enum_value_list (newtag); 27844 finish_enum (newtag); 27845 27846 DECL_SOURCE_LOCATION (TYPE_NAME (newtag)) 27847 = DECL_SOURCE_LOCATION (TYPE_NAME (tag)); 27848 TREE_DEPRECATED (newtag) = TREE_DEPRECATED (tag); 27849 TREE_UNAVAILABLE (newtag) = TREE_UNAVAILABLE (tag); 27850 } 27851 27852 /* DECL is a FUNCTION_DECL that is a template specialization. Return 27853 its type -- but without substituting the innermost set of template 27854 arguments. So, innermost set of template parameters will appear in 27855 the type. */ 27856 27857 tree 27858 get_mostly_instantiated_function_type (tree decl) 27859 { 27860 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */ 27861 return TREE_TYPE (DECL_TI_TEMPLATE (decl)); 27862 } 27863 27864 /* Return truthvalue if we're processing a template different from 27865 the last one involved in diagnostics. */ 27866 bool 27867 problematic_instantiation_changed (void) 27868 { 27869 return current_tinst_level != last_error_tinst_level; 27870 } 27871 27872 /* Remember current template involved in diagnostics. */ 27873 void 27874 record_last_problematic_instantiation (void) 27875 { 27876 set_refcount_ptr (last_error_tinst_level, current_tinst_level); 27877 } 27878 27879 struct tinst_level * 27880 current_instantiation (void) 27881 { 27882 return current_tinst_level; 27883 } 27884 27885 /* Return TRUE if current_function_decl is being instantiated, false 27886 otherwise. */ 27887 27888 bool 27889 instantiating_current_function_p (void) 27890 { 27891 return (current_instantiation () 27892 && (current_instantiation ()->maybe_get_node () 27893 == current_function_decl)); 27894 } 27895 27896 /* [temp.param] Check that template non-type parm TYPE is of an allowable 27897 type. Return false for ok, true for disallowed. Issue error and 27898 inform messages under control of COMPLAIN. */ 27899 27900 static bool 27901 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain) 27902 { 27903 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) 27904 return false; 27905 else if (TYPE_PTR_P (type)) 27906 return false; 27907 else if (TYPE_REF_P (type) 27908 && !TYPE_REF_IS_RVALUE (type)) 27909 return false; 27910 else if (TYPE_PTRMEM_P (type)) 27911 return false; 27912 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) 27913 { 27914 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20) 27915 { 27916 if (complain & tf_error) 27917 error ("non-type template parameters of deduced class type only " 27918 "available with %<-std=c++20%> or %<-std=gnu++20%>"); 27919 return true; 27920 } 27921 return false; 27922 } 27923 else if (TREE_CODE (type) == NULLPTR_TYPE) 27924 return false; 27925 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM 27926 && cxx_dialect < cxx11) 27927 /* Fall through; before C++11 alias templates, a bound ttp 27928 always instantiates into a class type. */; 27929 else if (WILDCARD_TYPE_P (type)) 27930 /* Any other wildcard type not already handled above is allowed. */ 27931 return false; 27932 else if (TREE_CODE (type) == COMPLEX_TYPE) 27933 /* Fall through. */; 27934 else if (VOID_TYPE_P (type)) 27935 /* Fall through. */; 27936 else if (cxx_dialect >= cxx20) 27937 { 27938 if (dependent_type_p (type)) 27939 return false; 27940 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)) 27941 return true; 27942 if (structural_type_p (type)) 27943 return false; 27944 if (complain & tf_error) 27945 { 27946 auto_diagnostic_group d; 27947 error ("%qT is not a valid type for a template non-type " 27948 "parameter because it is not structural", type); 27949 structural_type_p (type, true); 27950 } 27951 return true; 27952 } 27953 else if (CLASS_TYPE_P (type)) 27954 { 27955 if (complain & tf_error) 27956 error ("non-type template parameters of class type only available " 27957 "with %<-std=c++20%> or %<-std=gnu++20%>"); 27958 return true; 27959 } 27960 27961 if (complain & tf_error) 27962 { 27963 if (type == error_mark_node) 27964 inform (input_location, "invalid template non-type parameter"); 27965 else 27966 error ("%q#T is not a valid type for a template non-type parameter", 27967 type); 27968 } 27969 return true; 27970 } 27971 27972 /* Returns true iff the noexcept-specifier for TYPE is value-dependent. */ 27973 27974 static bool 27975 value_dependent_noexcept_spec_p (tree type) 27976 { 27977 if (tree spec = TYPE_RAISES_EXCEPTIONS (type)) 27978 if (tree noex = TREE_PURPOSE (spec)) 27979 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't 27980 affect overload resolution and treating it as dependent breaks 27981 things. Same for an unparsed noexcept expression. */ 27982 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT 27983 && TREE_CODE (noex) != DEFERRED_PARSE 27984 && value_dependent_expression_p (noex)) 27985 return true; 27986 27987 return false; 27988 } 27989 27990 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type]. 27991 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/ 27992 27993 static bool 27994 dependent_type_p_r (tree type) 27995 { 27996 tree scope; 27997 27998 /* [temp.dep.type] 27999 28000 A type is dependent if it is: 28001 28002 -- a template parameter. Template template parameters are types 28003 for us (since TYPE_P holds true for them) so we handle 28004 them here. */ 28005 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM 28006 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) 28007 return true; 28008 /* -- a qualified-id with a nested-name-specifier which contains a 28009 class-name that names a dependent type or whose unqualified-id 28010 names a dependent type. */ 28011 if (TREE_CODE (type) == TYPENAME_TYPE) 28012 return true; 28013 28014 /* An alias template specialization can be dependent even if the 28015 resulting type is not. */ 28016 if (dependent_alias_template_spec_p (type, nt_transparent)) 28017 return true; 28018 28019 /* -- a cv-qualified type where the cv-unqualified type is 28020 dependent. 28021 No code is necessary for this bullet; the code below handles 28022 cv-qualified types, and we don't want to strip aliases with 28023 TYPE_MAIN_VARIANT because of DR 1558. */ 28024 /* -- a compound type constructed from any dependent type. */ 28025 if (TYPE_PTRMEM_P (type)) 28026 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type)) 28027 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE 28028 (type))); 28029 else if (INDIRECT_TYPE_P (type)) 28030 return dependent_type_p (TREE_TYPE (type)); 28031 else if (FUNC_OR_METHOD_TYPE_P (type)) 28032 { 28033 tree arg_type; 28034 28035 if (dependent_type_p (TREE_TYPE (type))) 28036 return true; 28037 for (arg_type = TYPE_ARG_TYPES (type); 28038 arg_type; 28039 arg_type = TREE_CHAIN (arg_type)) 28040 if (dependent_type_p (TREE_VALUE (arg_type))) 28041 return true; 28042 if (cxx_dialect >= cxx17 28043 && value_dependent_noexcept_spec_p (type)) 28044 /* A value-dependent noexcept-specifier makes the type dependent. */ 28045 return true; 28046 return false; 28047 } 28048 /* -- an array type constructed from any dependent type or whose 28049 size is specified by a constant expression that is 28050 value-dependent. 28051 28052 We checked for type- and value-dependence of the bounds in 28053 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */ 28054 if (TREE_CODE (type) == ARRAY_TYPE) 28055 { 28056 if (TYPE_DOMAIN (type) 28057 && dependent_type_p (TYPE_DOMAIN (type))) 28058 return true; 28059 return dependent_type_p (TREE_TYPE (type)); 28060 } 28061 28062 /* -- a template-id in which either the template name is a template 28063 parameter ... */ 28064 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) 28065 return true; 28066 /* ... or any of the template arguments is a dependent type or 28067 an expression that is type-dependent or value-dependent. */ 28068 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type) 28069 && (any_dependent_template_arguments_p 28070 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type))))) 28071 return true; 28072 28073 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and TRAIT_TYPEs are 28074 dependent; if the argument of the `typeof' expression is not 28075 type-dependent, then it should already been have resolved. */ 28076 if (TREE_CODE (type) == TYPEOF_TYPE 28077 || TREE_CODE (type) == DECLTYPE_TYPE 28078 || TREE_CODE (type) == TRAIT_TYPE) 28079 return true; 28080 28081 /* A template argument pack is dependent if any of its packed 28082 arguments are. */ 28083 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK) 28084 { 28085 tree args = ARGUMENT_PACK_ARGS (type); 28086 for (tree arg : tree_vec_range (args)) 28087 if (dependent_template_arg_p (arg)) 28088 return true; 28089 } 28090 28091 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must 28092 be template parameters. */ 28093 if (TREE_CODE (type) == TYPE_PACK_EXPANSION) 28094 return true; 28095 28096 if (TREE_CODE (type) == DEPENDENT_OPERATOR_TYPE) 28097 return true; 28098 28099 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type))) 28100 return true; 28101 28102 /* The standard does not specifically mention types that are local 28103 to template functions or local classes, but they should be 28104 considered dependent too. For example: 28105 28106 template <int I> void f() { 28107 enum E { a = I }; 28108 S<sizeof (E)> s; 28109 } 28110 28111 The size of `E' cannot be known until the value of `I' has been 28112 determined. Therefore, `E' must be considered dependent. */ 28113 scope = TYPE_CONTEXT (type); 28114 if (scope && TYPE_P (scope)) 28115 return dependent_type_p (scope); 28116 /* Don't use type_dependent_expression_p here, as it can lead 28117 to infinite recursion trying to determine whether a lambda 28118 nested in a lambda is dependent (c++/47687). */ 28119 else if (scope && TREE_CODE (scope) == FUNCTION_DECL 28120 && DECL_LANG_SPECIFIC (scope) 28121 && DECL_TEMPLATE_INFO (scope) 28122 && (any_dependent_template_arguments_p 28123 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope))))) 28124 return true; 28125 28126 /* Other types are non-dependent. */ 28127 return false; 28128 } 28129 28130 /* Returns TRUE if TYPE is dependent, in the sense of 28131 [temp.dep.type]. Note that a NULL type is considered dependent. */ 28132 28133 bool 28134 dependent_type_p (tree type) 28135 { 28136 /* If there are no template parameters in scope, then there can't be 28137 any dependent types. */ 28138 if (!processing_template_decl) 28139 { 28140 /* If we are not processing a template, then nobody should be 28141 providing us with a dependent type. */ 28142 gcc_assert (type); 28143 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type)); 28144 return false; 28145 } 28146 28147 /* If the type is NULL, we have not computed a type for the entity 28148 in question; in that case, the type is dependent. */ 28149 if (!type) 28150 return true; 28151 28152 /* Erroneous types can be considered non-dependent. */ 28153 if (type == error_mark_node) 28154 return false; 28155 28156 /* If we have not already computed the appropriate value for TYPE, 28157 do so now. */ 28158 if (!TYPE_DEPENDENT_P_VALID (type)) 28159 { 28160 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type); 28161 TYPE_DEPENDENT_P_VALID (type) = 1; 28162 } 28163 28164 return TYPE_DEPENDENT_P (type); 28165 } 28166 28167 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any 28168 lookup. In other words, a dependent type that is not the current 28169 instantiation. */ 28170 28171 bool 28172 dependent_scope_p (tree scope) 28173 { 28174 return (scope && TYPE_P (scope) && dependent_type_p (scope) 28175 && !currently_open_class (scope)); 28176 } 28177 28178 /* True if we might find more declarations in SCOPE during instantiation than 28179 we can when parsing the template. */ 28180 28181 bool 28182 dependentish_scope_p (tree scope) 28183 { 28184 return dependent_scope_p (scope) || any_dependent_bases_p (scope); 28185 } 28186 28187 /* T is a SCOPE_REF. Return whether it represents a non-static member of 28188 an unknown base of 'this' (and is therefore instantiation-dependent). */ 28189 28190 static bool 28191 unknown_base_ref_p (tree t) 28192 { 28193 if (!current_class_ptr) 28194 return false; 28195 28196 tree mem = TREE_OPERAND (t, 1); 28197 if (shared_member_p (mem)) 28198 return false; 28199 28200 tree cur = current_nonlambda_class_type (); 28201 if (!any_dependent_bases_p (cur)) 28202 return false; 28203 28204 tree ctx = TREE_OPERAND (t, 0); 28205 if (DERIVED_FROM_P (ctx, cur)) 28206 return false; 28207 28208 return true; 28209 } 28210 28211 /* T is a SCOPE_REF; return whether we need to consider it 28212 instantiation-dependent so that we can check access at instantiation 28213 time even though we know which member it resolves to. */ 28214 28215 static bool 28216 instantiation_dependent_scope_ref_p (tree t) 28217 { 28218 if (DECL_P (TREE_OPERAND (t, 1)) 28219 && CLASS_TYPE_P (TREE_OPERAND (t, 0)) 28220 && !dependent_scope_p (TREE_OPERAND (t, 0)) 28221 && !unknown_base_ref_p (t) 28222 && accessible_in_template_p (TREE_OPERAND (t, 0), 28223 TREE_OPERAND (t, 1))) 28224 return false; 28225 else 28226 return true; 28227 } 28228 28229 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of 28230 [temp.dep.constexpr]. EXPRESSION is already known to be a constant 28231 expression. */ 28232 28233 /* Note that this predicate is not appropriate for general expressions; 28234 only constant expressions (that satisfy potential_constant_expression) 28235 can be tested for value dependence. */ 28236 28237 bool 28238 value_dependent_expression_p (tree expression) 28239 { 28240 if (!processing_template_decl || expression == NULL_TREE) 28241 return false; 28242 28243 /* A type-dependent expression is also value-dependent. */ 28244 if (type_dependent_expression_p (expression)) 28245 return true; 28246 28247 switch (TREE_CODE (expression)) 28248 { 28249 case BASELINK: 28250 /* A dependent member function of the current instantiation. */ 28251 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression))); 28252 28253 case FUNCTION_DECL: 28254 /* A dependent member function of the current instantiation. */ 28255 if (DECL_CLASS_SCOPE_P (expression) 28256 && dependent_type_p (DECL_CONTEXT (expression))) 28257 return true; 28258 break; 28259 28260 case IDENTIFIER_NODE: 28261 /* A name that has not been looked up -- must be dependent. */ 28262 return true; 28263 28264 case TEMPLATE_PARM_INDEX: 28265 /* A non-type template parm. */ 28266 return true; 28267 28268 case CONST_DECL: 28269 /* A non-type template parm. */ 28270 if (DECL_TEMPLATE_PARM_P (expression)) 28271 return true; 28272 return value_dependent_expression_p (DECL_INITIAL (expression)); 28273 28274 case VAR_DECL: 28275 /* A constant with literal type and is initialized 28276 with an expression that is value-dependent. */ 28277 if (DECL_DEPENDENT_INIT_P (expression)) 28278 return true; 28279 if (DECL_HAS_VALUE_EXPR_P (expression)) 28280 { 28281 tree value_expr = DECL_VALUE_EXPR (expression); 28282 if (value_dependent_expression_p (value_expr) 28283 /* __PRETTY_FUNCTION__ inside a template function is dependent 28284 on the name of the function. */ 28285 || (DECL_PRETTY_FUNCTION_P (expression) 28286 /* It might be used in a template, but not a template 28287 function, in which case its DECL_VALUE_EXPR will be 28288 "top level". */ 28289 && value_expr == error_mark_node)) 28290 return true; 28291 } 28292 else if (TYPE_REF_P (TREE_TYPE (expression))) 28293 /* FIXME cp_finish_decl doesn't fold reference initializers. */ 28294 return true; 28295 /* We have a constexpr variable and we're processing a template. When 28296 there's lifetime extension involved (for which finish_compound_literal 28297 used to create a temporary), we'll not be able to evaluate the 28298 variable until instantiating, so pretend it's value-dependent. */ 28299 else if (DECL_DECLARED_CONSTEXPR_P (expression) 28300 && !TREE_CONSTANT (expression)) 28301 return true; 28302 return false; 28303 28304 case DYNAMIC_CAST_EXPR: 28305 case STATIC_CAST_EXPR: 28306 case CONST_CAST_EXPR: 28307 case REINTERPRET_CAST_EXPR: 28308 case CAST_EXPR: 28309 case IMPLICIT_CONV_EXPR: 28310 /* These expressions are value-dependent if the type to which 28311 the cast occurs is dependent or the expression being casted 28312 is value-dependent. */ 28313 { 28314 tree type = TREE_TYPE (expression); 28315 28316 if (dependent_type_p (type)) 28317 return true; 28318 28319 /* A functional cast has a list of operands. */ 28320 expression = TREE_OPERAND (expression, 0); 28321 if (!expression) 28322 { 28323 /* If there are no operands, it must be an expression such 28324 as "int()". This should not happen for aggregate types 28325 because it would form non-constant expressions. */ 28326 gcc_assert (cxx_dialect >= cxx11 28327 || INTEGRAL_OR_ENUMERATION_TYPE_P (type)); 28328 28329 return false; 28330 } 28331 28332 if (TREE_CODE (expression) == TREE_LIST) 28333 return any_value_dependent_elements_p (expression); 28334 28335 if (TREE_CODE (type) == REFERENCE_TYPE 28336 && has_value_dependent_address (expression)) 28337 return true; 28338 28339 return value_dependent_expression_p (expression); 28340 } 28341 28342 case SIZEOF_EXPR: 28343 if (SIZEOF_EXPR_TYPE_P (expression)) 28344 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0))); 28345 /* FALLTHRU */ 28346 case ALIGNOF_EXPR: 28347 case TYPEID_EXPR: 28348 /* A `sizeof' expression is value-dependent if the operand is 28349 type-dependent or is a pack expansion. */ 28350 expression = TREE_OPERAND (expression, 0); 28351 if (PACK_EXPANSION_P (expression)) 28352 return true; 28353 else if (TYPE_P (expression)) 28354 return dependent_type_p (expression); 28355 return instantiation_dependent_uneval_expression_p (expression); 28356 28357 case AT_ENCODE_EXPR: 28358 /* An 'encode' expression is value-dependent if the operand is 28359 type-dependent. */ 28360 expression = TREE_OPERAND (expression, 0); 28361 return dependent_type_p (expression); 28362 28363 case NOEXCEPT_EXPR: 28364 expression = TREE_OPERAND (expression, 0); 28365 return instantiation_dependent_uneval_expression_p (expression); 28366 28367 case SCOPE_REF: 28368 /* All instantiation-dependent expressions should also be considered 28369 value-dependent. */ 28370 return instantiation_dependent_scope_ref_p (expression); 28371 28372 case COMPONENT_REF: 28373 return (value_dependent_expression_p (TREE_OPERAND (expression, 0)) 28374 || value_dependent_expression_p (TREE_OPERAND (expression, 1))); 28375 28376 case NONTYPE_ARGUMENT_PACK: 28377 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument 28378 is value-dependent. */ 28379 for (tree arg : tree_vec_range (ARGUMENT_PACK_ARGS (expression))) 28380 if (value_dependent_expression_p (arg)) 28381 return true; 28382 return false; 28383 28384 case TRAIT_EXPR: 28385 { 28386 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))) 28387 return true; 28388 28389 tree type2 = TRAIT_EXPR_TYPE2 (expression); 28390 if (!type2) 28391 return false; 28392 28393 if (TREE_CODE (type2) != TREE_VEC) 28394 return dependent_type_p (type2); 28395 28396 for (tree arg : tree_vec_range (type2)) 28397 if (dependent_type_p (arg)) 28398 return true; 28399 28400 return false; 28401 } 28402 28403 case MODOP_EXPR: 28404 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0))) 28405 || (value_dependent_expression_p (TREE_OPERAND (expression, 2)))); 28406 28407 case ARRAY_REF: 28408 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0))) 28409 || (value_dependent_expression_p (TREE_OPERAND (expression, 1)))); 28410 28411 case ADDR_EXPR: 28412 { 28413 tree op = TREE_OPERAND (expression, 0); 28414 return (value_dependent_expression_p (op) 28415 || has_value_dependent_address (op)); 28416 } 28417 28418 case REQUIRES_EXPR: 28419 /* Treat all requires-expressions as value-dependent so 28420 we don't try to fold them. */ 28421 return true; 28422 28423 case TYPE_REQ: 28424 return dependent_type_p (TREE_OPERAND (expression, 0)); 28425 28426 case CALL_EXPR: 28427 { 28428 if (value_dependent_expression_p (CALL_EXPR_FN (expression))) 28429 return true; 28430 tree fn = get_callee_fndecl (expression); 28431 int i, nargs; 28432 nargs = call_expr_nargs (expression); 28433 for (i = 0; i < nargs; ++i) 28434 { 28435 tree op = CALL_EXPR_ARG (expression, i); 28436 /* In a call to a constexpr member function, look through the 28437 implicit ADDR_EXPR on the object argument so that it doesn't 28438 cause the call to be considered value-dependent. We also 28439 look through it in potential_constant_expression. */ 28440 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn) 28441 && DECL_IOBJ_MEMBER_FUNCTION_P (fn) 28442 && TREE_CODE (op) == ADDR_EXPR) 28443 op = TREE_OPERAND (op, 0); 28444 if (value_dependent_expression_p (op)) 28445 return true; 28446 } 28447 return false; 28448 } 28449 28450 case TEMPLATE_ID_EXPR: 28451 return concept_definition_p (TREE_OPERAND (expression, 0)) 28452 && any_dependent_template_arguments_p (TREE_OPERAND (expression, 1)); 28453 28454 case CONSTRUCTOR: 28455 { 28456 unsigned ix; 28457 tree val; 28458 if (dependent_type_p (TREE_TYPE (expression))) 28459 return true; 28460 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val) 28461 if (value_dependent_expression_p (val)) 28462 return true; 28463 return false; 28464 } 28465 28466 case STMT_EXPR: 28467 /* Treat a GNU statement expression as dependent to avoid crashing 28468 under instantiate_non_dependent_expr; it can't be constant. */ 28469 return true; 28470 28471 case NEW_EXPR: 28472 case VEC_NEW_EXPR: 28473 /* The second operand is a type, which type_dependent_expression_p 28474 (and therefore value_dependent_expression_p) doesn't want to see. */ 28475 return (value_dependent_expression_p (TREE_OPERAND (expression, 0)) 28476 || value_dependent_expression_p (TREE_OPERAND (expression, 2)) 28477 || value_dependent_expression_p (TREE_OPERAND (expression, 3))); 28478 28479 default: 28480 /* A constant expression is value-dependent if any subexpression is 28481 value-dependent. */ 28482 switch (TREE_CODE_CLASS (TREE_CODE (expression))) 28483 { 28484 case tcc_reference: 28485 case tcc_unary: 28486 case tcc_comparison: 28487 case tcc_binary: 28488 case tcc_expression: 28489 case tcc_vl_exp: 28490 { 28491 int i, len = cp_tree_operand_length (expression); 28492 28493 for (i = 0; i < len; i++) 28494 { 28495 tree t = TREE_OPERAND (expression, i); 28496 28497 /* In some cases, some of the operands may be missing. 28498 (For example, in the case of PREDECREMENT_EXPR, the 28499 amount to increment by may be missing.) That doesn't 28500 make the expression dependent. */ 28501 if (t && value_dependent_expression_p (t)) 28502 return true; 28503 } 28504 } 28505 break; 28506 default: 28507 break; 28508 } 28509 break; 28510 } 28511 28512 /* The expression is not value-dependent. */ 28513 return false; 28514 } 28515 28516 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of 28517 [temp.dep.expr]. Note that an expression with no type is 28518 considered dependent. Other parts of the compiler arrange for an 28519 expression with type-dependent subexpressions to have no type, so 28520 this function doesn't have to be fully recursive. */ 28521 28522 bool 28523 type_dependent_expression_p (tree expression) 28524 { 28525 if (!processing_template_decl) 28526 return false; 28527 28528 if (expression == NULL_TREE || expression == error_mark_node) 28529 return false; 28530 28531 gcc_checking_assert (!TYPE_P (expression)); 28532 28533 STRIP_ANY_LOCATION_WRAPPER (expression); 28534 28535 /* An unresolved name is always dependent. */ 28536 if (identifier_p (expression) 28537 || TREE_CODE (expression) == USING_DECL 28538 || TREE_CODE (expression) == WILDCARD_DECL) 28539 return true; 28540 28541 /* A lambda-expression in template context is dependent. dependent_type_p is 28542 true for a lambda in the scope of a class or function template, but that 28543 doesn't cover all template contexts, like a default template argument. */ 28544 if (TREE_CODE (expression) == LAMBDA_EXPR) 28545 return true; 28546 28547 /* A fold expression is type-dependent. */ 28548 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR 28549 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR 28550 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR 28551 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR) 28552 return true; 28553 28554 /* Some expression forms are never type-dependent. */ 28555 if (TREE_CODE (expression) == SIZEOF_EXPR 28556 || TREE_CODE (expression) == ALIGNOF_EXPR 28557 || TREE_CODE (expression) == AT_ENCODE_EXPR 28558 || TREE_CODE (expression) == NOEXCEPT_EXPR 28559 || TREE_CODE (expression) == TRAIT_EXPR 28560 || TREE_CODE (expression) == TYPEID_EXPR 28561 || TREE_CODE (expression) == DELETE_EXPR 28562 || TREE_CODE (expression) == VEC_DELETE_EXPR 28563 || TREE_CODE (expression) == THROW_EXPR 28564 || TREE_CODE (expression) == REQUIRES_EXPR) 28565 return false; 28566 28567 /* The types of these expressions depends only on the type to which 28568 the cast occurs. */ 28569 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR 28570 || TREE_CODE (expression) == STATIC_CAST_EXPR 28571 || TREE_CODE (expression) == CONST_CAST_EXPR 28572 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR 28573 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR 28574 || TREE_CODE (expression) == CAST_EXPR) 28575 return dependent_type_p (TREE_TYPE (expression)); 28576 28577 /* The types of these expressions depends only on the type created 28578 by the expression. */ 28579 if (TREE_CODE (expression) == NEW_EXPR 28580 || TREE_CODE (expression) == VEC_NEW_EXPR) 28581 { 28582 /* For NEW_EXPR tree nodes created inside a template, either 28583 the object type itself or a TREE_LIST may appear as the 28584 operand 1. */ 28585 tree type = TREE_OPERAND (expression, 1); 28586 if (TREE_CODE (type) == TREE_LIST) 28587 /* This is an array type. We need to check array dimensions 28588 as well. */ 28589 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) 28590 || value_dependent_expression_p 28591 (TREE_OPERAND (TREE_VALUE (type), 1)); 28592 /* Array type whose dimension has to be deduced. */ 28593 else if (TREE_CODE (type) == ARRAY_TYPE 28594 && TREE_OPERAND (expression, 2) == NULL_TREE) 28595 return true; 28596 else 28597 return dependent_type_p (type); 28598 } 28599 28600 if (TREE_CODE (expression) == SCOPE_REF) 28601 { 28602 tree scope = TREE_OPERAND (expression, 0); 28603 tree name = TREE_OPERAND (expression, 1); 28604 28605 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it 28606 contains an identifier associated by name lookup with one or more 28607 declarations declared with a dependent type, or...a 28608 nested-name-specifier or qualified-id that names a member of an 28609 unknown specialization. */ 28610 return (type_dependent_expression_p (name) 28611 || dependent_scope_p (scope)); 28612 } 28613 28614 if (TREE_CODE (expression) == TEMPLATE_DECL 28615 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression)) 28616 return uses_outer_template_parms (expression); 28617 28618 if (TREE_CODE (expression) == STMT_EXPR) 28619 expression = stmt_expr_value_expr (expression); 28620 28621 if (BRACE_ENCLOSED_INITIALIZER_P (expression)) 28622 { 28623 for (auto &elt : CONSTRUCTOR_ELTS (expression)) 28624 if (type_dependent_expression_p (elt.value)) 28625 return true; 28626 return false; 28627 } 28628 28629 /* A static data member of the current instantiation with incomplete 28630 array type is type-dependent, as the definition and specializations 28631 can have different bounds. */ 28632 if (VAR_P (expression) 28633 && DECL_CLASS_SCOPE_P (expression) 28634 && dependent_type_p (DECL_CONTEXT (expression)) 28635 && VAR_HAD_UNKNOWN_BOUND (expression)) 28636 return true; 28637 28638 /* An array of unknown bound depending on a variadic parameter, eg: 28639 28640 template<typename... Args> 28641 void foo (Args... args) 28642 { 28643 int arr[] = { args... }; 28644 } 28645 28646 template<int... vals> 28647 void bar () 28648 { 28649 int arr[] = { vals... }; 28650 } 28651 28652 If the array has no length and has an initializer, it must be that 28653 we couldn't determine its length in cp_complete_array_type because 28654 it is dependent. */ 28655 if (((VAR_P (expression) && DECL_INITIAL (expression)) 28656 || COMPOUND_LITERAL_P (expression)) 28657 && TREE_TYPE (expression) != NULL_TREE 28658 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE 28659 && !TYPE_DOMAIN (TREE_TYPE (expression))) 28660 return true; 28661 28662 /* Pull a FUNCTION_DECL out of a BASELINK if we can. */ 28663 if (BASELINK_P (expression)) 28664 { 28665 if (BASELINK_OPTYPE (expression) 28666 && dependent_type_p (BASELINK_OPTYPE (expression))) 28667 return true; 28668 expression = BASELINK_FUNCTIONS (expression); 28669 } 28670 28671 /* A function or variable template-id is type-dependent if it has any 28672 dependent template arguments. */ 28673 if (VAR_OR_FUNCTION_DECL_P (expression) 28674 && DECL_LANG_SPECIFIC (expression) 28675 && DECL_TEMPLATE_INFO (expression)) 28676 { 28677 /* Consider the innermost template arguments, since those are the ones 28678 that come from the template-id; the template arguments for the 28679 enclosing class do not make it type-dependent unless they are used in 28680 the type of the decl. */ 28681 if (instantiates_primary_template_p (expression) 28682 && (any_dependent_template_arguments_p 28683 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression))))) 28684 return true; 28685 } 28686 28687 /* Otherwise, if the function decl isn't from a dependent scope, it can't be 28688 type-dependent. Checking this is important for functions with auto return 28689 type, which looks like a dependent type. */ 28690 if (TREE_CODE (expression) == FUNCTION_DECL 28691 && !(DECL_CLASS_SCOPE_P (expression) 28692 && dependent_type_p (DECL_CONTEXT (expression))) 28693 && !(DECL_LANG_SPECIFIC (expression) 28694 && DECL_UNIQUE_FRIEND_P (expression) 28695 && (!DECL_FRIEND_CONTEXT (expression) 28696 || dependent_type_p (DECL_FRIEND_CONTEXT (expression)))) 28697 && !DECL_LOCAL_DECL_P (expression)) 28698 { 28699 gcc_assert (!dependent_type_p (TREE_TYPE (expression)) 28700 || undeduced_auto_decl (expression)); 28701 return false; 28702 } 28703 28704 /* Otherwise, its constraints could still depend on outer template parameters 28705 from its (dependent) scope. */ 28706 if (TREE_CODE (expression) == FUNCTION_DECL 28707 /* As an optimization, check this cheaper sufficient condition first. 28708 (At this point we've established that we're looking at a member of 28709 a dependent class, so it makes sense to start treating say undeduced 28710 auto as dependent.) */ 28711 && !dependent_type_p (TREE_TYPE (expression)) 28712 && uses_outer_template_parms_in_constraints (expression)) 28713 return true; 28714 28715 /* Always dependent, on the number of arguments if nothing else. */ 28716 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION) 28717 return true; 28718 28719 if (TREE_TYPE (expression) == unknown_type_node) 28720 { 28721 if (TREE_CODE (expression) == ADDR_EXPR) 28722 return type_dependent_expression_p (TREE_OPERAND (expression, 0)); 28723 if (TREE_CODE (expression) == COMPONENT_REF 28724 || TREE_CODE (expression) == OFFSET_REF) 28725 { 28726 if (type_dependent_object_expression_p (TREE_OPERAND (expression, 0))) 28727 return true; 28728 expression = TREE_OPERAND (expression, 1); 28729 if (identifier_p (expression)) 28730 return false; 28731 } 28732 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */ 28733 if (TREE_CODE (expression) == SCOPE_REF) 28734 return false; 28735 28736 if (BASELINK_P (expression)) 28737 { 28738 if (BASELINK_OPTYPE (expression) 28739 && dependent_type_p (BASELINK_OPTYPE (expression))) 28740 return true; 28741 expression = BASELINK_FUNCTIONS (expression); 28742 } 28743 28744 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR) 28745 { 28746 tree args = TREE_OPERAND (expression, 1); 28747 if (any_dependent_template_arguments_p (args)) 28748 return true; 28749 /* Arguments of a function template-id aren't necessarily coerced 28750 yet so we must conservatively assume that the address (and not 28751 just value) of the argument matters as per [temp.dep.temp]/3. */ 28752 for (tree arg : tree_vec_range (args)) 28753 if (has_value_dependent_address (arg)) 28754 return true; 28755 expression = TREE_OPERAND (expression, 0); 28756 if (identifier_p (expression)) 28757 return true; 28758 } 28759 28760 gcc_assert (OVL_P (expression)); 28761 28762 for (lkp_iterator iter (expression); iter; ++iter) 28763 if (type_dependent_expression_p (*iter)) 28764 return true; 28765 28766 return false; 28767 } 28768 28769 /* The type of a non-type template parm declared with a placeholder type 28770 depends on the corresponding template argument, even though 28771 placeholders are not normally considered dependent. */ 28772 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX 28773 && is_auto (TREE_TYPE (expression))) 28774 return true; 28775 28776 gcc_assert (TREE_CODE (expression) != TYPE_DECL); 28777 28778 /* Dependent type attributes might not have made it from the decl to 28779 the type yet. */ 28780 if (DECL_P (expression) 28781 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression))) 28782 return true; 28783 28784 return (dependent_type_p (TREE_TYPE (expression))); 28785 } 28786 28787 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is 28788 type-dependent if the expression refers to a member of the current 28789 instantiation and the type of the referenced member is dependent, or the 28790 class member access expression refers to a member of an unknown 28791 specialization. 28792 28793 This function returns true if the OBJECT in such a class member access 28794 expression is of an unknown specialization. */ 28795 28796 bool 28797 type_dependent_object_expression_p (tree object) 28798 { 28799 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still 28800 dependent. */ 28801 if (TREE_CODE (object) == IDENTIFIER_NODE) 28802 return true; 28803 tree scope = TREE_TYPE (object); 28804 return (!scope || dependent_scope_p (scope)); 28805 } 28806 28807 /* walk_tree callback function for instantiation_dependent_expression_p, 28808 below. Returns non-zero if a dependent subexpression is found. */ 28809 28810 static tree 28811 instantiation_dependent_r (tree *tp, int *walk_subtrees, 28812 void * /*data*/) 28813 { 28814 if (TYPE_P (*tp)) 28815 { 28816 /* We don't have to worry about decltype currently because decltype 28817 of an instantiation-dependent expr is a dependent type. This 28818 might change depending on the resolution of DR 1172. */ 28819 *walk_subtrees = false; 28820 return NULL_TREE; 28821 } 28822 enum tree_code code = TREE_CODE (*tp); 28823 switch (code) 28824 { 28825 /* Don't treat an argument list as dependent just because it has no 28826 TREE_TYPE. */ 28827 case TREE_LIST: 28828 case TREE_VEC: 28829 case NONTYPE_ARGUMENT_PACK: 28830 return NULL_TREE; 28831 28832 case TEMPLATE_PARM_INDEX: 28833 if (dependent_type_p (TREE_TYPE (*tp))) 28834 return *tp; 28835 if (TEMPLATE_PARM_PARAMETER_PACK (*tp)) 28836 return *tp; 28837 /* We'll check value-dependence separately. */ 28838 return NULL_TREE; 28839 28840 /* Handle expressions with type operands. */ 28841 case SIZEOF_EXPR: 28842 case ALIGNOF_EXPR: 28843 case TYPEID_EXPR: 28844 case AT_ENCODE_EXPR: 28845 { 28846 tree op = TREE_OPERAND (*tp, 0); 28847 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp)) 28848 op = TREE_TYPE (op); 28849 if (TYPE_P (op)) 28850 { 28851 if (dependent_type_p (op)) 28852 return *tp; 28853 else 28854 { 28855 *walk_subtrees = false; 28856 return NULL_TREE; 28857 } 28858 } 28859 break; 28860 } 28861 28862 case COMPONENT_REF: 28863 if (identifier_p (TREE_OPERAND (*tp, 1))) 28864 /* In a template, finish_class_member_access_expr creates a 28865 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't 28866 type-dependent, so that we can check access control at 28867 instantiation time (PR 42277). See also Core issue 1273. */ 28868 return *tp; 28869 break; 28870 28871 case SCOPE_REF: 28872 if (instantiation_dependent_scope_ref_p (*tp)) 28873 return *tp; 28874 else 28875 break; 28876 28877 /* Treat statement-expressions as dependent. */ 28878 case BIND_EXPR: 28879 return *tp; 28880 28881 /* Treat requires-expressions as dependent. */ 28882 case REQUIRES_EXPR: 28883 return *tp; 28884 28885 case CONSTRUCTOR: 28886 if (CONSTRUCTOR_IS_DEPENDENT (*tp)) 28887 return *tp; 28888 break; 28889 28890 case TEMPLATE_DECL: 28891 case FUNCTION_DECL: 28892 /* Before C++17, a noexcept-specifier isn't part of the function type 28893 so it doesn't affect type dependence, but we still want to consider it 28894 for instantiation dependence. */ 28895 if (cxx_dialect < cxx17 28896 && DECL_DECLARES_FUNCTION_P (*tp) 28897 && value_dependent_noexcept_spec_p (TREE_TYPE (*tp))) 28898 return *tp; 28899 break; 28900 28901 default: 28902 break; 28903 } 28904 28905 if (type_dependent_expression_p (*tp)) 28906 return *tp; 28907 else 28908 return NULL_TREE; 28909 } 28910 28911 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the 28912 sense defined by the ABI: 28913 28914 "An expression is instantiation-dependent if it is type-dependent 28915 or value-dependent, or it has a subexpression that is type-dependent 28916 or value-dependent." 28917 28918 Except don't actually check value-dependence for unevaluated expressions, 28919 because in sizeof(i) we don't care about the value of i. Checking 28920 type-dependence will in turn check value-dependence of array bounds/template 28921 arguments as needed. */ 28922 28923 bool 28924 instantiation_dependent_uneval_expression_p (tree expression) 28925 { 28926 tree result; 28927 28928 if (!processing_template_decl) 28929 return false; 28930 28931 if (expression == error_mark_node) 28932 return false; 28933 28934 result = cp_walk_tree_without_duplicates (&expression, 28935 instantiation_dependent_r, NULL); 28936 return result != NULL_TREE; 28937 } 28938 28939 /* As above, but also check value-dependence of the expression as a whole. */ 28940 28941 bool 28942 instantiation_dependent_expression_p (tree expression) 28943 { 28944 return (instantiation_dependent_uneval_expression_p (expression) 28945 || (processing_template_decl 28946 && potential_constant_expression (expression) 28947 && value_dependent_expression_p (expression))); 28948 } 28949 28950 /* Like type_dependent_expression_p, but it also works while not processing 28951 a template definition, i.e. during substitution or mangling. */ 28952 28953 bool 28954 type_dependent_expression_p_push (tree expr) 28955 { 28956 bool b; 28957 ++processing_template_decl; 28958 b = type_dependent_expression_p (expr); 28959 --processing_template_decl; 28960 return b; 28961 } 28962 28963 /* Returns TRUE if ARGS contains a type-dependent expression. */ 28964 28965 bool 28966 any_type_dependent_arguments_p (const vec<tree, va_gc> *args) 28967 { 28968 if (!processing_template_decl || !args) 28969 return false; 28970 28971 for (tree arg : *args) 28972 if (type_dependent_expression_p (arg)) 28973 return true; 28974 28975 return false; 28976 } 28977 28978 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are 28979 expressions) contains any type-dependent expressions. */ 28980 28981 bool 28982 any_type_dependent_elements_p (const_tree list) 28983 { 28984 for (; list; list = TREE_CHAIN (list)) 28985 if (type_dependent_expression_p (TREE_VALUE (list))) 28986 return true; 28987 28988 return false; 28989 } 28990 28991 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are 28992 expressions) contains any value-dependent expressions. */ 28993 28994 bool 28995 any_value_dependent_elements_p (const_tree list) 28996 { 28997 for (; list; list = TREE_CHAIN (list)) 28998 if (value_dependent_expression_p (TREE_VALUE (list))) 28999 return true; 29000 29001 return false; 29002 } 29003 29004 /* Returns TRUE if the ARG (a template argument) is dependent. */ 29005 29006 bool 29007 dependent_template_arg_p (tree arg) 29008 { 29009 if (!processing_template_decl) 29010 return false; 29011 29012 /* Assume a template argument that was wrongly written by the user 29013 is dependent. This is consistent with what 29014 any_dependent_template_arguments_p [that calls this function] 29015 does. */ 29016 if (!arg || arg == error_mark_node) 29017 return true; 29018 29019 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT) 29020 arg = argument_pack_select_arg (arg); 29021 29022 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) 29023 return true; 29024 if (TREE_CODE (arg) == TEMPLATE_DECL) 29025 { 29026 if (DECL_TEMPLATE_PARM_P (arg)) 29027 return true; 29028 /* A member template of a dependent class is not necessarily 29029 type-dependent, but it is a dependent template argument because it 29030 will be a member of an unknown specialization to that template. */ 29031 tree scope = CP_DECL_CONTEXT (arg); 29032 return TYPE_P (scope) && dependent_type_p (scope); 29033 } 29034 else if (ARGUMENT_PACK_P (arg)) 29035 { 29036 tree args = ARGUMENT_PACK_ARGS (arg); 29037 for (tree arg : tree_vec_range (args)) 29038 if (dependent_template_arg_p (arg)) 29039 return true; 29040 return false; 29041 } 29042 else if (TYPE_P (arg)) 29043 return dependent_type_p (arg); 29044 else 29045 return value_dependent_expression_p (arg); 29046 } 29047 29048 /* Identify any expressions that use function parms. */ 29049 29050 static tree 29051 find_parm_usage_r (tree *tp, int *walk_subtrees, void*) 29052 { 29053 tree t = *tp; 29054 if (TREE_CODE (t) == PARM_DECL) 29055 { 29056 *walk_subtrees = 0; 29057 return t; 29058 } 29059 return NULL_TREE; 29060 } 29061 29062 /* Returns true if a type specialization formed using the template 29063 arguments ARGS needs to use structural equality. */ 29064 29065 bool 29066 any_template_arguments_need_structural_equality_p (tree args) 29067 { 29068 int i; 29069 int j; 29070 29071 if (!args) 29072 return false; 29073 if (args == error_mark_node) 29074 return true; 29075 29076 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) 29077 { 29078 tree level = TMPL_ARGS_LEVEL (args, i + 1); 29079 for (j = 0; j < TREE_VEC_LENGTH (level); ++j) 29080 { 29081 tree arg = TREE_VEC_ELT (level, j); 29082 tree packed_args = NULL_TREE; 29083 int k, len = 1; 29084 29085 if (ARGUMENT_PACK_P (arg)) 29086 { 29087 /* Look inside the argument pack. */ 29088 packed_args = ARGUMENT_PACK_ARGS (arg); 29089 len = TREE_VEC_LENGTH (packed_args); 29090 } 29091 29092 for (k = 0; k < len; ++k) 29093 { 29094 if (packed_args) 29095 arg = TREE_VEC_ELT (packed_args, k); 29096 29097 if (error_operand_p (arg)) 29098 return true; 29099 else if (TREE_CODE (arg) == TEMPLATE_DECL) 29100 continue; 29101 else if (arg == any_targ_node) 29102 /* An any_targ_node argument (added by add_defaults_to_ttp) 29103 makes the corresponding specialization not canonicalizable, 29104 since template_args_equal always return true for it. We 29105 may see this when called from bind_template_template_parm. */ 29106 return true; 29107 /* Checking current_function_decl because this structural 29108 comparison is only necessary for redeclaration. */ 29109 else if (!current_function_decl 29110 && dependent_template_arg_p (arg) 29111 && (cp_walk_tree_without_duplicates 29112 (&arg, find_parm_usage_r, NULL))) 29113 /* The identity of a class template specialization that uses 29114 a function parameter depends on the identity of the function. 29115 And if this specialization appeared in the trailing return 29116 type thereof, we don't know the identity of the function 29117 (e.g. if it's a redeclaration or a new function) until we 29118 form its signature and go through duplicate_decls. Thus 29119 it's unsafe to decide on a canonical type now (which depends 29120 on the DECL_CONTEXT of the function parameter, which can get 29121 mutated after the fact by duplicate_decls), so just require 29122 structural equality in this case (PR52830). */ 29123 return true; 29124 else if (TYPE_P (arg) 29125 && TYPE_STRUCTURAL_EQUALITY_P (arg) 29126 && dependent_alias_template_spec_p (arg, nt_transparent)) 29127 /* Require structural equality for specializations written 29128 in terms of a dependent alias template specialization. */ 29129 return true; 29130 else if (CLASS_TYPE_P (arg) 29131 && TYPE_TEMPLATE_INFO (arg) 29132 && TYPE_STRUCTURAL_EQUALITY_P (arg)) 29133 /* Require structural equality for specializations written 29134 in terms of a class template specialization that itself 29135 needs structural equality. */ 29136 return true; 29137 } 29138 } 29139 } 29140 29141 return false; 29142 } 29143 29144 /* Returns true if ARGS (a collection of template arguments) contains 29145 any dependent arguments. */ 29146 29147 bool 29148 any_dependent_template_arguments_p (const_tree args) 29149 { 29150 if (args == error_mark_node) 29151 return true; 29152 if (!processing_template_decl || !args) 29153 return false; 29154 29155 for (int i = 0, depth = TMPL_ARGS_DEPTH (args); i < depth; ++i) 29156 { 29157 const_tree level = TMPL_ARGS_LEVEL (args, i + 1); 29158 for (tree arg : tree_vec_range (CONST_CAST_TREE (level))) 29159 if (dependent_template_arg_p (arg)) 29160 return true; 29161 } 29162 29163 return false; 29164 } 29165 29166 /* Returns true if ARGS contains any errors. */ 29167 29168 bool 29169 any_erroneous_template_args_p (const_tree args) 29170 { 29171 int i; 29172 int j; 29173 29174 if (args == error_mark_node) 29175 return true; 29176 29177 if (args && TREE_CODE (args) != TREE_VEC) 29178 { 29179 if (tree ti = get_template_info (args)) 29180 args = TI_ARGS (ti); 29181 else 29182 args = NULL_TREE; 29183 } 29184 29185 if (!args) 29186 return false; 29187 29188 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) 29189 { 29190 const_tree level = TMPL_ARGS_LEVEL (args, i + 1); 29191 for (j = 0; j < TREE_VEC_LENGTH (level); ++j) 29192 if (error_operand_p (TREE_VEC_ELT (level, j))) 29193 return true; 29194 } 29195 29196 return false; 29197 } 29198 29199 /* Returns TRUE if the template TMPL is type-dependent. */ 29200 29201 bool 29202 dependent_template_p (tree tmpl) 29203 { 29204 if (TREE_CODE (tmpl) == OVERLOAD) 29205 { 29206 for (lkp_iterator iter (tmpl); iter; ++iter) 29207 if (dependent_template_p (*iter)) 29208 return true; 29209 return false; 29210 } 29211 29212 /* Template template parameters are dependent. */ 29213 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl) 29214 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM) 29215 return true; 29216 /* So are names that have not been looked up. */ 29217 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl)) 29218 return true; 29219 return false; 29220 } 29221 29222 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */ 29223 29224 bool 29225 dependent_template_id_p (tree tmpl, tree args) 29226 { 29227 return (dependent_template_p (tmpl) 29228 || any_dependent_template_arguments_p (args)); 29229 } 29230 29231 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors 29232 are dependent. */ 29233 29234 bool 29235 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv) 29236 { 29237 int i; 29238 29239 if (!processing_template_decl) 29240 return false; 29241 29242 for (i = 0; i < TREE_VEC_LENGTH (declv); i++) 29243 { 29244 tree decl = TREE_VEC_ELT (declv, i); 29245 tree init = TREE_VEC_ELT (initv, i); 29246 tree cond = TREE_VEC_ELT (condv, i); 29247 tree incr = TREE_VEC_ELT (incrv, i); 29248 29249 if (type_dependent_expression_p (decl) 29250 || TREE_CODE (decl) == SCOPE_REF) 29251 return true; 29252 29253 if (init && type_dependent_expression_p (init)) 29254 return true; 29255 29256 if (cond == global_namespace) 29257 return true; 29258 29259 if (type_dependent_expression_p (cond)) 29260 return true; 29261 29262 if (COMPARISON_CLASS_P (cond) 29263 && (type_dependent_expression_p (TREE_OPERAND (cond, 0)) 29264 || type_dependent_expression_p (TREE_OPERAND (cond, 1)))) 29265 return true; 29266 29267 if (TREE_CODE (incr) == MODOP_EXPR) 29268 { 29269 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)) 29270 || type_dependent_expression_p (TREE_OPERAND (incr, 2))) 29271 return true; 29272 } 29273 else if (type_dependent_expression_p (incr)) 29274 return true; 29275 else if (TREE_CODE (incr) == MODIFY_EXPR) 29276 { 29277 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))) 29278 return true; 29279 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1))) 29280 { 29281 tree t = TREE_OPERAND (incr, 1); 29282 if (type_dependent_expression_p (TREE_OPERAND (t, 0)) 29283 || type_dependent_expression_p (TREE_OPERAND (t, 1))) 29284 return true; 29285 29286 /* If this loop has a class iterator with != comparison 29287 with increment other than i++/++i/i--/--i, make sure the 29288 increment is constant. */ 29289 if (CLASS_TYPE_P (TREE_TYPE (decl)) 29290 && TREE_CODE (cond) == NE_EXPR) 29291 { 29292 if (TREE_OPERAND (t, 0) == decl) 29293 t = TREE_OPERAND (t, 1); 29294 else 29295 t = TREE_OPERAND (t, 0); 29296 if (TREE_CODE (t) != INTEGER_CST) 29297 return true; 29298 } 29299 } 29300 } 29301 } 29302 29303 return false; 29304 } 29305 29306 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the 29307 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if 29308 no such TYPE can be found. Note that this function peers inside 29309 uninstantiated templates and therefore should be used only in 29310 extremely limited situations. ONLY_CURRENT_P restricts this 29311 peering to the currently open classes hierarchy (which is required 29312 when comparing types). */ 29313 29314 tree 29315 resolve_typename_type (tree type, bool only_current_p) 29316 { 29317 tree scope; 29318 tree name; 29319 tree decl; 29320 int quals; 29321 tree pushed_scope; 29322 tree result; 29323 29324 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE); 29325 29326 scope = TYPE_CONTEXT (type); 29327 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */ 29328 gcc_checking_assert (uses_template_parms (scope)); 29329 29330 /* Usually the non-qualified identifier of a TYPENAME_TYPE is 29331 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a 29332 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL 29333 representing the typedef. In that case TYPE_IDENTIFIER (type) is 29334 not the non-qualified identifier of the TYPENAME_TYPE anymore. 29335 So by getting the TYPE_IDENTIFIER of the _main declaration_ of 29336 the TYPENAME_TYPE instead, we avoid messing up with a possible 29337 typedef variant case. */ 29338 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type)); 29339 29340 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve 29341 it first before we can figure out what NAME refers to. */ 29342 if (TREE_CODE (scope) == TYPENAME_TYPE) 29343 { 29344 if (TYPENAME_IS_RESOLVING_P (scope)) 29345 /* Given a class template A with a dependent base with nested type C, 29346 typedef typename A::C::C C will land us here, as trying to resolve 29347 the initial A::C leads to the local C typedef, which leads back to 29348 A::C::C. So we break the recursion now. */ 29349 return type; 29350 else 29351 scope = resolve_typename_type (scope, only_current_p); 29352 } 29353 /* If we don't know what SCOPE refers to, then we cannot resolve the 29354 TYPENAME_TYPE. */ 29355 if (!CLASS_TYPE_P (scope)) 29356 return type; 29357 /* If this is a typedef, we don't want to look inside (c++/11987). */ 29358 if (typedef_variant_p (type)) 29359 return type; 29360 /* If SCOPE isn't the template itself, it will not have a valid 29361 TYPE_FIELDS list. */ 29362 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope))) 29363 /* scope is either the template itself or a compatible instantiation 29364 like X<T>, so look up the name in the original template. */ 29365 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope); 29366 /* If scope has no fields, it can't be a current instantiation. Check this 29367 before currently_open_class to avoid infinite recursion (71515). */ 29368 if (!TYPE_FIELDS (scope)) 29369 return type; 29370 /* If the SCOPE is not the current instantiation, there's no reason 29371 to look inside it. */ 29372 if (only_current_p && !currently_open_class (scope)) 29373 return type; 29374 /* Enter the SCOPE so that name lookup will be resolved as if we 29375 were in the class definition. In particular, SCOPE will no 29376 longer be considered a dependent type. */ 29377 pushed_scope = push_scope (scope); 29378 /* Look up the declaration. */ 29379 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true, 29380 tf_warning_or_error); 29381 29382 result = NULL_TREE; 29383 29384 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to 29385 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */ 29386 tree fullname = TYPENAME_TYPE_FULLNAME (type); 29387 if (!decl) 29388 /*nop*/; 29389 else if (identifier_p (fullname) 29390 && TREE_CODE (decl) == TYPE_DECL) 29391 { 29392 result = TREE_TYPE (decl); 29393 if (result == error_mark_node) 29394 result = NULL_TREE; 29395 } 29396 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR 29397 && DECL_CLASS_TEMPLATE_P (decl)) 29398 { 29399 /* Obtain the template and the arguments. */ 29400 tree tmpl = TREE_OPERAND (fullname, 0); 29401 if (TREE_CODE (tmpl) == IDENTIFIER_NODE) 29402 { 29403 /* We get here with a plain identifier because a previous tentative 29404 parse of the nested-name-specifier as part of a ptr-operator saw 29405 ::template X<A>. The use of ::template is necessary in a 29406 ptr-operator, but wrong in a declarator-id. 29407 29408 [temp.names]: In a qualified-id of a declarator-id, the keyword 29409 template shall not appear at the top level. */ 29410 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic, 29411 "keyword %<template%> not allowed in declarator-id"); 29412 tmpl = decl; 29413 } 29414 tree args = TREE_OPERAND (fullname, 1); 29415 /* Instantiate the template. */ 29416 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE, 29417 /*entering_scope=*/true, 29418 tf_error | tf_user); 29419 if (result == error_mark_node) 29420 result = NULL_TREE; 29421 } 29422 29423 /* Leave the SCOPE. */ 29424 if (pushed_scope) 29425 pop_scope (pushed_scope); 29426 29427 /* If we failed to resolve it, return the original typename. */ 29428 if (!result) 29429 return type; 29430 29431 /* If lookup found a typename type, resolve that too. */ 29432 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result)) 29433 { 29434 /* Ill-formed programs can cause infinite recursion here, so we 29435 must catch that. */ 29436 TYPENAME_IS_RESOLVING_P (result) = 1; 29437 result = resolve_typename_type (result, only_current_p); 29438 TYPENAME_IS_RESOLVING_P (result) = 0; 29439 } 29440 29441 /* Qualify the resulting type. */ 29442 quals = cp_type_quals (type); 29443 if (quals) 29444 result = cp_build_qualified_type (result, cp_type_quals (result) | quals); 29445 29446 return result; 29447 } 29448 29449 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a 29450 TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms, 29451 by default. If set_canonical is true, we set TYPE_CANONICAL on it. */ 29452 29453 static tree 29454 make_auto_1 (tree name, bool set_canonical, int level = -1) 29455 { 29456 if (level == -1) 29457 level = current_template_depth + 1; 29458 tree au = cxx_make_type (TEMPLATE_TYPE_PARM); 29459 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au); 29460 TYPE_STUB_DECL (au) = TYPE_NAME (au); 29461 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index 29462 (0, level, level, TYPE_NAME (au), NULL_TREE); 29463 if (set_canonical) 29464 TYPE_CANONICAL (au) = canonical_type_parameter (au); 29465 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1; 29466 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au)); 29467 if (name == decltype_auto_identifier) 29468 AUTO_IS_DECLTYPE (au) = true; 29469 29470 return au; 29471 } 29472 29473 tree 29474 make_decltype_auto (void) 29475 { 29476 return make_auto_1 (decltype_auto_identifier, true); 29477 } 29478 29479 tree 29480 make_auto (void) 29481 { 29482 return make_auto_1 (auto_identifier, true); 29483 } 29484 29485 /* Return a C++17 deduction placeholder for class template TMPL. 29486 There are represented as an 'auto' with the special level 0 and 29487 CLASS_PLACEHOLDER_TEMPLATE set. */ 29488 29489 tree 29490 make_template_placeholder (tree tmpl) 29491 { 29492 tree t = make_auto_1 (auto_identifier, false, /*level=*/0); 29493 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl; 29494 /* Our canonical type depends on the placeholder. */ 29495 TYPE_CANONICAL (t) = canonical_type_parameter (t); 29496 return t; 29497 } 29498 29499 /* True iff T is a C++17 class template deduction placeholder. */ 29500 29501 bool 29502 template_placeholder_p (tree t) 29503 { 29504 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t); 29505 } 29506 29507 /* Return an auto for an explicit cast expression auto(x). 29508 Like CTAD placeholders, these have level 0 so that they're 29509 not accidentally replaced via tsubst and are always directly 29510 resolved via do_auto_deduction. */ 29511 29512 tree 29513 make_cast_auto () 29514 { 29515 return make_auto_1 (auto_identifier, true, /*level=*/0); 29516 } 29517 29518 /* Make a "constrained auto" type-specifier. This is an auto or 29519 decltype(auto) type with constraints that must be associated after 29520 deduction. The constraint is formed from the given concept CON 29521 and its optional sequence of template arguments ARGS. 29522 29523 TYPE must be the result of make_auto_type or make_decltype_auto_type. */ 29524 29525 static tree 29526 make_constrained_placeholder_type (tree type, tree con, tree args) 29527 { 29528 /* Build the constraint. */ 29529 tree tmpl = DECL_TI_TEMPLATE (con); 29530 tree expr = tmpl; 29531 if (TREE_CODE (con) == FUNCTION_DECL) 29532 expr = ovl_make (tmpl); 29533 ++processing_template_decl; 29534 expr = build_concept_check (expr, type, args, tf_warning_or_error); 29535 --processing_template_decl; 29536 29537 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type) 29538 = build_tree_list (current_template_parms, expr); 29539 29540 /* Our canonical type depends on the constraint. */ 29541 TYPE_CANONICAL (type) = canonical_type_parameter (type); 29542 29543 /* Attach the constraint to the type declaration. */ 29544 return TYPE_NAME (type); 29545 } 29546 29547 /* Make a "constrained auto" type-specifier. */ 29548 29549 tree 29550 make_constrained_auto (tree con, tree args) 29551 { 29552 tree type = make_auto_1 (auto_identifier, false); 29553 return make_constrained_placeholder_type (type, con, args); 29554 } 29555 29556 /* Make a "constrained decltype(auto)" type-specifier. */ 29557 29558 tree 29559 make_constrained_decltype_auto (tree con, tree args) 29560 { 29561 tree type = make_auto_1 (decltype_auto_identifier, false); 29562 return make_constrained_placeholder_type (type, con, args); 29563 } 29564 29565 /* Returns true if the placeholder type constraint T has any dependent 29566 (explicit) template arguments. */ 29567 29568 static bool 29569 placeholder_type_constraint_dependent_p (tree t) 29570 { 29571 tree id = unpack_concept_check (t); 29572 tree args = TREE_OPERAND (id, 1); 29573 tree first = TREE_VEC_ELT (args, 0); 29574 if (ARGUMENT_PACK_P (first)) 29575 { 29576 args = expand_template_argument_pack (args); 29577 first = TREE_VEC_ELT (args, 0); 29578 } 29579 gcc_checking_assert (TREE_CODE (first) == WILDCARD_DECL 29580 || is_auto (first)); 29581 for (int i = 1; i < TREE_VEC_LENGTH (args); ++i) 29582 if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) 29583 return true; 29584 return false; 29585 } 29586 29587 /* Build and return a concept definition. Like other templates, the 29588 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the 29589 the TEMPLATE_DECL. */ 29590 29591 tree 29592 finish_concept_definition (cp_expr id, tree init, tree attrs) 29593 { 29594 gcc_assert (identifier_p (id)); 29595 gcc_assert (processing_template_decl); 29596 29597 location_t loc = id.get_location(); 29598 29599 /* A concept-definition shall not have associated constraints. */ 29600 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)) 29601 { 29602 error_at (loc, "a concept cannot be constrained"); 29603 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE; 29604 } 29605 29606 /* A concept-definition shall appear in namespace scope. Templates 29607 aren't allowed in block scope, so we only need to check for class 29608 scope. */ 29609 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ())) 29610 { 29611 error_at (loc, "concept %qE not in namespace scope", *id); 29612 return error_mark_node; 29613 } 29614 29615 if (current_template_depth > 1) 29616 { 29617 error_at (loc, "concept %qE has multiple template parameter lists", *id); 29618 return error_mark_node; 29619 } 29620 29621 /* Initially build the concept declaration; its type is bool. */ 29622 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node); 29623 DECL_CONTEXT (decl) = current_scope (); 29624 DECL_INITIAL (decl) = init; 29625 29626 if (attrs) 29627 cplus_decl_attributes (&decl, attrs, 0); 29628 29629 set_originating_module (decl, false); 29630 29631 /* Push the enclosing template. */ 29632 return push_template_decl (decl); 29633 } 29634 29635 /* Given type ARG, return std::initializer_list<ARG>. */ 29636 29637 static tree 29638 listify (tree arg) 29639 { 29640 tree std_init_list = lookup_qualified_name (std_node, init_list_identifier); 29641 29642 if (std_init_list == error_mark_node 29643 || !DECL_CLASS_TEMPLATE_P (std_init_list)) 29644 { 29645 gcc_rich_location richloc (input_location); 29646 maybe_add_include_fixit (&richloc, "<initializer_list>", false); 29647 error_at (&richloc, 29648 "deducing from brace-enclosed initializer list" 29649 " requires %<#include <initializer_list>%>"); 29650 29651 return error_mark_node; 29652 } 29653 tree argvec = make_tree_vec (1); 29654 TREE_VEC_ELT (argvec, 0) = arg; 29655 29656 return lookup_template_class (std_init_list, argvec, NULL_TREE, 29657 NULL_TREE, 0, tf_warning_or_error); 29658 } 29659 29660 /* Replace auto in TYPE with std::initializer_list<auto>. */ 29661 29662 static tree 29663 listify_autos (tree type, tree auto_node) 29664 { 29665 tree init_auto = listify (strip_top_quals (auto_node)); 29666 tree argvec = make_tree_vec (1); 29667 TREE_VEC_ELT (argvec, 0) = init_auto; 29668 if (processing_template_decl) 29669 argvec = add_to_template_args (current_template_args (), argvec); 29670 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE); 29671 } 29672 29673 /* Hash traits for hashing possibly constrained 'auto' 29674 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */ 29675 29676 struct auto_hash : default_hash_traits<tree> 29677 { 29678 static inline hashval_t hash (tree); 29679 static inline bool equal (tree, tree); 29680 }; 29681 29682 /* Hash the 'auto' T. */ 29683 29684 inline hashval_t 29685 auto_hash::hash (tree t) 29686 { 29687 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t))) 29688 /* Matching constrained-type-specifiers denote the same template 29689 parameter, so hash the constraint. */ 29690 return hash_placeholder_constraint (c); 29691 else 29692 /* But unconstrained autos are all separate, so just hash the pointer. */ 29693 return iterative_hash_object (t, 0); 29694 } 29695 29696 /* Compare two 'auto's. */ 29697 29698 inline bool 29699 auto_hash::equal (tree t1, tree t2) 29700 { 29701 if (t1 == t2) 29702 return true; 29703 29704 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1); 29705 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2); 29706 29707 /* Two unconstrained autos are distinct. */ 29708 if (!c1 || !c2) 29709 return false; 29710 29711 return equivalent_placeholder_constraints (c1, c2); 29712 } 29713 29714 /* for_each_template_parm callback for extract_autos: if t is a (possibly 29715 constrained) auto, add it to the vector. */ 29716 29717 static int 29718 extract_autos_r (tree t, void *data) 29719 { 29720 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data; 29721 if (is_auto (t) && !template_placeholder_p (t)) 29722 { 29723 /* All the autos were built with index 0; fix that up now. */ 29724 tree *p = hash.find_slot (t, INSERT); 29725 int idx; 29726 if (*p) 29727 /* If this is a repeated constrained-type-specifier, use the index we 29728 chose before. */ 29729 idx = TEMPLATE_TYPE_IDX (*p); 29730 else 29731 { 29732 /* Otherwise this is new, so use the current count. */ 29733 *p = t; 29734 idx = hash.elements () - 1; 29735 } 29736 if (idx != TEMPLATE_TYPE_IDX (t)) 29737 { 29738 gcc_checking_assert (TEMPLATE_TYPE_IDX (t) == 0); 29739 gcc_checking_assert (TYPE_CANONICAL (t) != t); 29740 TEMPLATE_TYPE_IDX (t) = idx; 29741 TYPE_CANONICAL (t) = canonical_type_parameter (t); 29742 } 29743 } 29744 29745 /* Always keep walking. */ 29746 return 0; 29747 } 29748 29749 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which 29750 says they can appear anywhere in the type. */ 29751 29752 static tree 29753 extract_autos (tree type) 29754 { 29755 hash_set<tree> visited; 29756 hash_table<auto_hash> hash (2); 29757 29758 for_each_template_parm (type, extract_autos_r, &hash, &visited, true); 29759 29760 tree tree_vec = make_tree_vec (hash.elements()); 29761 for (tree elt : hash) 29762 { 29763 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt)); 29764 TREE_VEC_ELT (tree_vec, i) 29765 = build_tree_list (NULL_TREE, TYPE_NAME (elt)); 29766 } 29767 29768 return tree_vec; 29769 } 29770 29771 /* The stem for deduction guide names. */ 29772 const char *const dguide_base = "__dguide_"; 29773 29774 /* Return the name for a deduction guide for class template TMPL. */ 29775 29776 tree 29777 dguide_name (tree tmpl) 29778 { 29779 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl)); 29780 tree tname = TYPE_IDENTIFIER (type); 29781 char *buf = (char *) alloca (1 + strlen (dguide_base) 29782 + IDENTIFIER_LENGTH (tname)); 29783 memcpy (buf, dguide_base, strlen (dguide_base)); 29784 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname), 29785 IDENTIFIER_LENGTH (tname) + 1); 29786 tree dname = get_identifier (buf); 29787 TREE_TYPE (dname) = type; 29788 return dname; 29789 } 29790 29791 /* True if NAME is the name of a deduction guide. */ 29792 29793 bool 29794 dguide_name_p (tree name) 29795 { 29796 return (TREE_CODE (name) == IDENTIFIER_NODE 29797 && TREE_TYPE (name) 29798 && startswith (IDENTIFIER_POINTER (name), dguide_base)); 29799 } 29800 29801 /* True if FN is a deduction guide. */ 29802 29803 bool 29804 deduction_guide_p (const_tree fn) 29805 { 29806 if (DECL_P (fn)) 29807 if (tree name = DECL_NAME (fn)) 29808 return dguide_name_p (name); 29809 return false; 29810 } 29811 29812 /* True if FN is the copy deduction guide, i.e. A(A)->A. */ 29813 29814 bool 29815 copy_guide_p (const_tree fn) 29816 { 29817 gcc_assert (deduction_guide_p (fn)); 29818 if (!DECL_ARTIFICIAL (fn)) 29819 return false; 29820 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn)); 29821 return (TREE_CHAIN (parms) == void_list_node 29822 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn)))); 29823 } 29824 29825 /* True if FN is a guide generated from a constructor template. */ 29826 29827 bool 29828 template_guide_p (const_tree fn) 29829 { 29830 gcc_assert (deduction_guide_p (fn)); 29831 if (!DECL_ARTIFICIAL (fn)) 29832 return false; 29833 tree tmpl = DECL_TI_TEMPLATE (fn); 29834 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl)) 29835 return PRIMARY_TEMPLATE_P (org); 29836 return false; 29837 } 29838 29839 /* True if FN is an aggregate initialization guide or the copy deduction 29840 guide. */ 29841 29842 bool 29843 builtin_guide_p (const_tree fn) 29844 { 29845 if (!deduction_guide_p (fn)) 29846 return false; 29847 if (!DECL_ARTIFICIAL (fn)) 29848 /* Explicitly declared. */ 29849 return false; 29850 if (DECL_ABSTRACT_ORIGIN (fn)) 29851 /* Derived from a constructor. */ 29852 return false; 29853 return true; 29854 } 29855 29856 /* True if FN is a C++23 inherited guide. */ 29857 29858 bool 29859 inherited_guide_p (const_tree fn) 29860 { 29861 gcc_assert (deduction_guide_p (fn)); 29862 return LANG_DECL_FN_CHECK (fn)->context != NULL_TREE; 29863 } 29864 29865 /* Set the base class BASE from which the transformed guide FN 29866 was inherited as part of C++23 inherited CTAD. */ 29867 29868 static void 29869 set_inherited_guide_context (const_tree fn, tree base) 29870 { 29871 gcc_assert (deduction_guide_p (fn)); 29872 LANG_DECL_FN_CHECK (fn)->context = base; 29873 } 29874 29875 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at 29876 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type 29877 template parameter types. Note that the handling of template template 29878 parameters relies on current_template_parms being set appropriately for the 29879 new template. */ 29880 29881 static tree 29882 rewrite_template_parm (tree olddecl, unsigned index, unsigned level, 29883 tree tsubst_args, tsubst_flags_t complain) 29884 { 29885 if (olddecl == error_mark_node) 29886 return error_mark_node; 29887 29888 tree oldidx = get_template_parm_index (olddecl); 29889 29890 tree newtype; 29891 if (TREE_CODE (olddecl) == TYPE_DECL 29892 || TREE_CODE (olddecl) == TEMPLATE_DECL) 29893 { 29894 tree oldtype = TREE_TYPE (olddecl); 29895 newtype = cxx_make_type (TREE_CODE (oldtype)); 29896 TYPE_MAIN_VARIANT (newtype) = newtype; 29897 } 29898 else 29899 { 29900 newtype = TREE_TYPE (olddecl); 29901 if (type_uses_auto (newtype)) 29902 { 29903 // Substitute once to fix references to other template parameters. 29904 newtype = tsubst (newtype, tsubst_args, 29905 complain|tf_partial, NULL_TREE); 29906 // Now substitute again to reduce the level of the auto. 29907 newtype = tsubst (newtype, current_template_args (), 29908 complain, NULL_TREE); 29909 } 29910 else 29911 newtype = tsubst (newtype, tsubst_args, 29912 complain, NULL_TREE); 29913 } 29914 29915 tree newdecl 29916 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl), 29917 DECL_NAME (olddecl), newtype); 29918 SET_DECL_TEMPLATE_PARM_P (newdecl); 29919 29920 tree newidx; 29921 if (TREE_CODE (olddecl) == TYPE_DECL 29922 || TREE_CODE (olddecl) == TEMPLATE_DECL) 29923 { 29924 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype) 29925 = build_template_parm_index (index, level, level, 29926 newdecl, newtype); 29927 TEMPLATE_PARM_PARAMETER_PACK (newidx) 29928 = TEMPLATE_PARM_PARAMETER_PACK (oldidx); 29929 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl; 29930 29931 if (TREE_CODE (olddecl) == TEMPLATE_DECL) 29932 { 29933 tree newresult 29934 = build_lang_decl_loc (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL, 29935 DECL_NAME (olddecl), newtype); 29936 DECL_ARTIFICIAL (newresult) = true; 29937 DECL_TEMPLATE_RESULT (newdecl) = newresult; 29938 // First create a copy (ttargs) of tsubst_args with an 29939 // additional level for the template template parameter's own 29940 // template parameters (ttparms). 29941 tree ttparms = (INNERMOST_TEMPLATE_PARMS 29942 (DECL_TEMPLATE_PARMS (olddecl))); 29943 const int depth = TMPL_ARGS_DEPTH (tsubst_args); 29944 tree ttargs = make_tree_vec (depth + 1); 29945 for (int i = 0; i < depth; ++i) 29946 TREE_VEC_ELT (ttargs, i) = TMPL_ARGS_LEVEL (tsubst_args, i + 1); 29947 TREE_VEC_ELT (ttargs, depth) 29948 = template_parms_level_to_args (ttparms); 29949 // Substitute ttargs into ttparms to fix references to 29950 // other template parameters. 29951 ttparms = tsubst_template_parms_level (ttparms, ttargs, 29952 complain|tf_partial); 29953 // Now substitute again with args based on tparms, to reduce 29954 // the level of the ttparms. 29955 ttargs = current_template_args (); 29956 ttparms = tsubst_template_parms_level (ttparms, ttargs, 29957 complain); 29958 // Finally, tack the adjusted parms onto tparms. 29959 ttparms = tree_cons (size_int (level + 1), ttparms, 29960 copy_node (current_template_parms)); 29961 // As with all template template parms, the parameter list captured 29962 // by this template template parm that corresponds to its own level 29963 // should be empty. This avoids infinite recursion when structurally 29964 // comparing two such rewritten template template parms (PR102479). 29965 gcc_assert (!TREE_VEC_LENGTH 29966 (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl))))); 29967 gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms)) == level); 29968 TREE_VALUE (TREE_CHAIN (ttparms)) = make_tree_vec (0); 29969 // All done. 29970 DECL_TEMPLATE_PARMS (newdecl) = ttparms; 29971 DECL_TEMPLATE_INFO (newresult) 29972 = build_template_info (newdecl, template_parms_to_args (ttparms)); 29973 } 29974 29975 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl))) 29976 SET_TYPE_STRUCTURAL_EQUALITY (newtype); 29977 else 29978 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype); 29979 } 29980 else 29981 { 29982 tree oldconst = TEMPLATE_PARM_DECL (oldidx); 29983 tree newconst 29984 = build_decl (DECL_SOURCE_LOCATION (oldconst), 29985 TREE_CODE (oldconst), 29986 DECL_NAME (oldconst), newtype); 29987 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl) 29988 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true; 29989 SET_DECL_TEMPLATE_PARM_P (newconst); 29990 newidx = build_template_parm_index (index, level, level, 29991 newconst, newtype); 29992 TEMPLATE_PARM_PARAMETER_PACK (newidx) 29993 = TEMPLATE_PARM_PARAMETER_PACK (oldidx); 29994 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx; 29995 } 29996 29997 return newdecl; 29998 } 29999 30000 /* As rewrite_template_parm, but for the whole TREE_LIST representing a 30001 template parameter. */ 30002 30003 static tree 30004 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level, 30005 tree targs, unsigned targs_index, tsubst_flags_t complain) 30006 { 30007 tree olddecl = TREE_VALUE (oldelt); 30008 tree newdecl = rewrite_template_parm (olddecl, index, level, 30009 targs, complain); 30010 if (newdecl == error_mark_node) 30011 return error_mark_node; 30012 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt), 30013 targs, complain, NULL_TREE); 30014 tree list = build_tree_list (newdef, newdecl); 30015 TEMPLATE_PARM_CONSTRAINTS (list) 30016 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt), 30017 targs, complain, NULL_TREE); 30018 int depth = TMPL_ARGS_DEPTH (targs); 30019 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list); 30020 return list; 30021 } 30022 30023 /* Returns a C++17 class deduction guide template based on the constructor 30024 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default 30025 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an 30026 aggregate initialization guide. OUTER_ARGS are the template arguments 30027 for the enclosing scope of the class. */ 30028 30029 static tree 30030 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain) 30031 { 30032 tree tparms, targs, fparms, fargs, ci; 30033 bool memtmpl = false; 30034 bool explicit_p; 30035 location_t loc; 30036 tree fn_tmpl = NULL_TREE; 30037 30038 if (outer_args) 30039 { 30040 ++processing_template_decl; 30041 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type)); 30042 --processing_template_decl; 30043 } 30044 30045 if (!DECL_DECLARES_FUNCTION_P (ctor)) 30046 { 30047 if (TYPE_P (ctor)) 30048 { 30049 bool copy_p = TYPE_REF_P (ctor); 30050 if (copy_p) 30051 fparms = tree_cons (NULL_TREE, type, void_list_node); 30052 else 30053 fparms = void_list_node; 30054 } 30055 else if (TREE_CODE (ctor) == TREE_LIST) 30056 fparms = ctor; 30057 else 30058 gcc_unreachable (); 30059 30060 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type); 30061 tparms = DECL_TEMPLATE_PARMS (ctmpl); 30062 targs = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); 30063 ci = NULL_TREE; 30064 fargs = NULL_TREE; 30065 loc = DECL_SOURCE_LOCATION (ctmpl); 30066 explicit_p = false; 30067 } 30068 else 30069 { 30070 ++processing_template_decl; 30071 bool ok = true; 30072 30073 complain |= tf_dguide; 30074 30075 fn_tmpl 30076 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor 30077 : DECL_TI_TEMPLATE (ctor)); 30078 if (outer_args) 30079 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor); 30080 ctor = DECL_TEMPLATE_RESULT (fn_tmpl); 30081 30082 tparms = DECL_TEMPLATE_PARMS (fn_tmpl); 30083 /* If type is a member class template, DECL_TI_ARGS (ctor) will have 30084 fully specialized args for the enclosing class. Strip those off, as 30085 the deduction guide won't have those template parameters. */ 30086 targs = get_innermost_template_args (DECL_TI_ARGS (ctor), 30087 TMPL_PARMS_DEPTH (tparms)); 30088 /* Discard the 'this' parameter. */ 30089 fparms = FUNCTION_ARG_CHAIN (ctor); 30090 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor)); 30091 ci = get_constraints (ctor); 30092 loc = DECL_SOURCE_LOCATION (ctor); 30093 explicit_p = DECL_NONCONVERTING_P (ctor); 30094 30095 if (PRIMARY_TEMPLATE_P (fn_tmpl)) 30096 { 30097 memtmpl = true; 30098 30099 /* For a member template constructor, we need to flatten the two 30100 template parameter lists into one, and then adjust the function 30101 signature accordingly. This gets...complicated. */ 30102 tree save_parms = current_template_parms; 30103 30104 /* For a member template we should have two levels of parms/args, one 30105 for the class and one for the constructor. We stripped 30106 specialized args for further enclosing classes above. */ 30107 const int depth = 2; 30108 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth); 30109 30110 /* Template args for translating references to the two-level template 30111 parameters into references to the one-level template parameters we 30112 are creating. */ 30113 tree tsubst_args = copy_node (targs); 30114 TMPL_ARGS_LEVEL (tsubst_args, depth) 30115 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth)); 30116 30117 /* Template parms for the constructor template. */ 30118 tree ftparms = TREE_VALUE (tparms); 30119 unsigned flen = TREE_VEC_LENGTH (ftparms); 30120 /* Template parms for the class template. */ 30121 tparms = TREE_CHAIN (tparms); 30122 tree ctparms = TREE_VALUE (tparms); 30123 unsigned clen = TREE_VEC_LENGTH (ctparms); 30124 /* Template parms for the deduction guide start as a copy of the 30125 template parms for the class. We set current_template_parms for 30126 lookup_template_class_1. */ 30127 current_template_parms = tparms = copy_node (tparms); 30128 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen); 30129 for (unsigned i = 0; i < clen; ++i) 30130 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i); 30131 30132 /* Now we need to rewrite the constructor parms to append them to the 30133 class parms. */ 30134 for (unsigned i = 0; i < flen; ++i) 30135 { 30136 unsigned index = i + clen; 30137 unsigned level = 1; 30138 tree oldelt = TREE_VEC_ELT (ftparms, i); 30139 tree newelt 30140 = rewrite_tparm_list (oldelt, index, level, 30141 tsubst_args, i, complain); 30142 if (newelt == error_mark_node) 30143 ok = false; 30144 TREE_VEC_ELT (new_vec, index) = newelt; 30145 } 30146 30147 /* Now we have a final set of template parms to substitute into the 30148 function signature. */ 30149 targs = template_parms_to_args (tparms); 30150 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE, 30151 complain, ctor); 30152 if (fparms == error_mark_node) 30153 ok = false; 30154 if (ci) 30155 { 30156 if (outer_args) 30157 /* FIXME: We'd like to avoid substituting outer template 30158 arguments into the constraint ahead of time, but the 30159 construction of tsubst_args assumes that outer arguments 30160 are already substituted in. */ 30161 ci = tsubst_constraint_info (ci, outer_args, complain, ctor); 30162 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor); 30163 } 30164 30165 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if 30166 cp_unevaluated_operand. */ 30167 cp_evaluated ev; 30168 fargs = tsubst (fargs, tsubst_args, complain, ctor); 30169 current_template_parms = save_parms; 30170 } 30171 else 30172 { 30173 /* Substitute in the same arguments to rewrite class members into 30174 references to members of an unknown specialization. */ 30175 cp_evaluated ev; 30176 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor); 30177 fargs = tsubst (fargs, targs, complain, ctor); 30178 if (ci) 30179 { 30180 if (outer_args) 30181 /* FIXME: As above. */ 30182 ci = tsubst_constraint_info (ci, outer_args, complain, ctor); 30183 ci = tsubst_constraint_info (ci, targs, complain, ctor); 30184 } 30185 } 30186 30187 --processing_template_decl; 30188 if (!ok) 30189 return error_mark_node; 30190 } 30191 30192 if (!memtmpl) 30193 { 30194 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */ 30195 tparms = copy_node (tparms); 30196 INNERMOST_TEMPLATE_PARMS (tparms) 30197 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms)); 30198 } 30199 30200 tree fntype = build_function_type (type, fparms); 30201 tree ded_fn = build_lang_decl_loc (loc, 30202 FUNCTION_DECL, 30203 dguide_name (type), fntype); 30204 DECL_ARGUMENTS (ded_fn) = fargs; 30205 DECL_ARTIFICIAL (ded_fn) = true; 30206 DECL_NONCONVERTING_P (ded_fn) = explicit_p; 30207 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false); 30208 DECL_ARTIFICIAL (ded_tmpl) = true; 30209 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs); 30210 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl; 30211 if (DECL_P (ctor)) 30212 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl; 30213 if (ci) 30214 set_constraints (ded_tmpl, ci); 30215 30216 return ded_tmpl; 30217 } 30218 30219 /* Add to LIST the member types for the reshaped initializer CTOR. */ 30220 30221 static tree 30222 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE) 30223 { 30224 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor); 30225 tree idx, val; unsigned i; 30226 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val) 30227 { 30228 tree ftype = elt ? elt : TREE_TYPE (idx); 30229 if (BRACE_ENCLOSED_INITIALIZER_P (val) 30230 && CONSTRUCTOR_BRACES_ELIDED_P (val)) 30231 { 30232 tree subelt = NULL_TREE; 30233 if (TREE_CODE (ftype) == ARRAY_TYPE) 30234 subelt = TREE_TYPE (ftype); 30235 list = collect_ctor_idx_types (val, list, subelt); 30236 continue; 30237 } 30238 tree arg = NULL_TREE; 30239 if (i == v->length() - 1 30240 && PACK_EXPANSION_P (ftype)) 30241 /* Give the trailing pack expansion parameter a default argument to 30242 match aggregate initialization behavior, even if we deduce the 30243 length of the pack separately to more than we have initializers. */ 30244 arg = build_constructor (init_list_type_node, NULL); 30245 /* if ei is of array type and xi is a braced-init-list or string literal, 30246 Ti is an rvalue reference to the declared type of ei */ 30247 STRIP_ANY_LOCATION_WRAPPER (val); 30248 if (TREE_CODE (ftype) == ARRAY_TYPE 30249 && (BRACE_ENCLOSED_INITIALIZER_P (val) 30250 || TREE_CODE (val) == STRING_CST)) 30251 { 30252 if (TREE_CODE (val) == STRING_CST) 30253 ftype = cp_build_qualified_type 30254 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST); 30255 ftype = (cp_build_reference_type 30256 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val))); 30257 } 30258 list = tree_cons (arg, ftype, list); 30259 } 30260 30261 return list; 30262 } 30263 30264 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */ 30265 30266 static bool 30267 is_spec_or_derived (tree etype, tree tmpl) 30268 { 30269 if (!etype || !CLASS_TYPE_P (etype)) 30270 return false; 30271 30272 etype = cv_unqualified (etype); 30273 tree type = TREE_TYPE (tmpl); 30274 tree tparms = (INNERMOST_TEMPLATE_PARMS 30275 (DECL_TEMPLATE_PARMS (tmpl))); 30276 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms)); 30277 int err = unify (tparms, targs, type, etype, 30278 UNIFY_ALLOW_DERIVED, /*explain*/false); 30279 ggc_free (targs); 30280 return !err; 30281 } 30282 30283 /* Return a C++20 aggregate deduction candidate for TYPE initialized from 30284 INIT. */ 30285 30286 static tree 30287 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args) 30288 { 30289 if (cxx_dialect < cxx20) 30290 return NULL_TREE; 30291 30292 if (init == NULL_TREE) 30293 return NULL_TREE; 30294 30295 if (DECL_ALIAS_TEMPLATE_P (tmpl)) 30296 { 30297 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); 30298 tree tinfo = get_template_info (under); 30299 if (tree guide = maybe_aggr_guide (TI_TEMPLATE (tinfo), init, args)) 30300 return alias_ctad_tweaks (tmpl, guide); 30301 return NULL_TREE; 30302 } 30303 30304 /* We might be creating a guide for a class member template, e.g., 30305 30306 template<typename U> struct A { 30307 template<typename T> struct B { T t; }; 30308 }; 30309 30310 At this point, A will have been instantiated. Below, we need to 30311 use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types. */ 30312 const bool member_template_p 30313 = (DECL_TEMPLATE_INFO (tmpl) 30314 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl))); 30315 tree type = TREE_TYPE (tmpl); 30316 tree template_type = (member_template_p 30317 ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl)) 30318 : type); 30319 if (!CP_AGGREGATE_TYPE_P (template_type)) 30320 return NULL_TREE; 30321 30322 /* No aggregate candidate for copy-initialization. */ 30323 if (args->length() == 1) 30324 { 30325 tree val = (*args)[0]; 30326 if (is_spec_or_derived (TREE_TYPE (val), tmpl)) 30327 return NULL_TREE; 30328 } 30329 30330 /* If we encounter a problem, we just won't add the candidate. */ 30331 tsubst_flags_t complain = tf_none; 30332 30333 tree parms = NULL_TREE; 30334 if (BRACE_ENCLOSED_INITIALIZER_P (init)) 30335 { 30336 init = reshape_init (template_type, init, complain); 30337 if (init == error_mark_node) 30338 return NULL_TREE; 30339 parms = collect_ctor_idx_types (init, parms); 30340 } 30341 else if (TREE_CODE (init) == TREE_LIST) 30342 { 30343 int len = list_length (init); 30344 for (tree binfo : BINFO_BASE_BINFOS (TYPE_BINFO (template_type))) 30345 { 30346 if (!len) 30347 break; 30348 parms = tree_cons (NULL_TREE, BINFO_TYPE (binfo), parms); 30349 --len; 30350 } 30351 for (tree field = TYPE_FIELDS (template_type); 30352 len; 30353 --len, field = DECL_CHAIN (field)) 30354 { 30355 field = next_aggregate_field (field); 30356 if (!field) 30357 return NULL_TREE; 30358 tree ftype = finish_decltype_type (field, true, complain); 30359 parms = tree_cons (NULL_TREE, ftype, parms); 30360 } 30361 } 30362 else 30363 /* Aggregate initialization doesn't apply to an initializer expression. */ 30364 return NULL_TREE; 30365 30366 /* If we're creating a deduction guide for a member class template, 30367 we've used the original template pattern type for the reshape_init 30368 above; this is done because we want PARMS to be a template parameter 30369 type, something that can be deduced when used as a function template 30370 parameter. At this point the outer class template has already been 30371 partially instantiated (we deferred the deduction until the enclosing 30372 scope is non-dependent). Therefore we have to partially instantiate 30373 PARMS, so that its template level is properly reduced and we don't get 30374 mismatches when deducing types using the guide with PARMS. */ 30375 if (member_template_p) 30376 { 30377 ++processing_template_decl; 30378 parms = tsubst (parms, outer_template_args (tmpl), complain, init); 30379 --processing_template_decl; 30380 } 30381 30382 if (parms) 30383 { 30384 tree last = parms; 30385 parms = nreverse (parms); 30386 TREE_CHAIN (last) = void_list_node; 30387 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain); 30388 return guide; 30389 } 30390 30391 return NULL_TREE; 30392 } 30393 30394 /* UGUIDES are the deduction guides for the underlying template of alias 30395 template TMPL; adjust them to be deduction guides for TMPL. 30396 30397 This routine also handles C++23 inherited CTAD, in which case TMPL is a 30398 TREE_LIST representing a synthetic alias template whose TREE_PURPOSE is 30399 the template parameter list of the alias template (equivalently, of the 30400 derived class) and TREE_VALUE the defining-type-id (equivalently, the 30401 base whose guides we're inheriting). UGUIDES are the base's guides. */ 30402 30403 static tree 30404 alias_ctad_tweaks (tree tmpl, tree uguides) 30405 { 30406 /* [over.match.class.deduct]: When resolving a placeholder for a deduced 30407 class type (9.2.8.2) where the template-name names an alias template A, 30408 the defining-type-id of A must be of the form 30409 30410 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id 30411 30412 as specified in 9.2.8.2. The guides of A are the set of functions or 30413 function templates formed as follows. For each function or function 30414 template f in the guides of the template named by the simple-template-id 30415 of the defining-type-id, the template arguments of the return type of f 30416 are deduced from the defining-type-id of A according to the process in 30417 13.10.2.5 with the exception that deduction does not fail if not all 30418 template arguments are deduced. Let g denote the result of substituting 30419 these deductions into f. If substitution succeeds, form a function or 30420 function template f' with the following properties and add it to the set 30421 of guides of A: 30422 30423 * The function type of f' is the function type of g. 30424 30425 * If f is a function template, f' is a function template whose template 30426 parameter list consists of all the template parameters of A (including 30427 their default template arguments) that appear in the above deductions or 30428 (recursively) in their default template arguments, followed by the 30429 template parameters of f that were not deduced (including their default 30430 template arguments), otherwise f' is not a function template. 30431 30432 * The associated constraints (13.5.2) are the conjunction of the 30433 associated constraints of g and a constraint that is satisfied if and only 30434 if the arguments of A are deducible (see below) from the return type. 30435 30436 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to 30437 be so as well. 30438 30439 * If f was generated from a deduction-guide (12.4.1.8), then f' is 30440 considered to be so as well. 30441 30442 * The explicit-specifier of f' is the explicit-specifier of g (if 30443 any). */ 30444 30445 enum { alias, inherited } ctad_kind; 30446 tree atype, fullatparms, utype, name; 30447 if (TREE_CODE (tmpl) == TEMPLATE_DECL) 30448 { 30449 ctad_kind = alias; 30450 atype = TREE_TYPE (tmpl); 30451 fullatparms = DECL_TEMPLATE_PARMS (tmpl); 30452 utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); 30453 name = dguide_name (tmpl); 30454 } 30455 else 30456 { 30457 ctad_kind = inherited; 30458 atype = NULL_TREE; 30459 fullatparms = TREE_PURPOSE (tmpl); 30460 utype = TREE_VALUE (tmpl); 30461 name = dguide_name (TPARMS_PRIMARY_TEMPLATE 30462 (INNERMOST_TEMPLATE_PARMS (fullatparms))); 30463 } 30464 30465 tsubst_flags_t complain = tf_none; 30466 tree aguides = NULL_TREE; 30467 tree atparms = INNERMOST_TEMPLATE_PARMS (fullatparms); 30468 unsigned natparms = TREE_VEC_LENGTH (atparms); 30469 for (tree f : lkp_range (uguides)) 30470 { 30471 tree in_decl = f; 30472 location_t loc = DECL_SOURCE_LOCATION (f); 30473 tree ret = TREE_TYPE (TREE_TYPE (f)); 30474 tree fprime = f; 30475 if (TREE_CODE (f) == TEMPLATE_DECL) 30476 { 30477 processing_template_decl_sentinel ptds (/*reset*/false); 30478 ++processing_template_decl; 30479 30480 /* Deduce template arguments for f from the type-id of A. */ 30481 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f)); 30482 unsigned len = TREE_VEC_LENGTH (ftparms); 30483 tree targs = make_tree_vec (len); 30484 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false); 30485 if (err) 30486 /* CWG2664: Discard any deductions, still build the guide. */ 30487 for (unsigned i = 0; i < len; ++i) 30488 TREE_VEC_ELT (targs, i) = NULL_TREE; 30489 30490 /* The number of parms for f' is the number of parms of A used in 30491 the deduced arguments plus non-deduced parms of f. */ 30492 unsigned ndlen = 0; 30493 unsigned j; 30494 for (unsigned i = 0; i < len; ++i) 30495 if (TREE_VEC_ELT (targs, i) == NULL_TREE) 30496 ++ndlen; 30497 find_template_parameter_info ftpi (fullatparms); 30498 ftpi.find_in_recursive (targs); 30499 unsigned nusedatparms = ftpi.num_found (); 30500 unsigned nfparms = nusedatparms + ndlen; 30501 tree gtparms = make_tree_vec (nfparms); 30502 30503 /* Set current_template_parms as in build_deduction_guide. */ 30504 auto ctp = make_temp_override (current_template_parms); 30505 current_template_parms = copy_node (fullatparms); 30506 TREE_VALUE (current_template_parms) = gtparms; 30507 30508 j = 0; 30509 unsigned level = 1; 30510 30511 /* First copy over the used parms of A. */ 30512 tree atargs = make_tree_vec (natparms); 30513 for (unsigned i = 0; i < natparms; ++i) 30514 { 30515 tree elt = TREE_VEC_ELT (atparms, i); 30516 if (ftpi.found (elt)) 30517 { 30518 unsigned index = j++; 30519 tree nelt = rewrite_tparm_list (elt, index, level, 30520 atargs, i, complain); 30521 TREE_VEC_ELT (gtparms, index) = nelt; 30522 } 30523 } 30524 gcc_checking_assert (j == nusedatparms); 30525 30526 /* Adjust the deduced template args for f to refer to the A parms 30527 with their new indexes. */ 30528 if (nusedatparms && nusedatparms != natparms) 30529 targs = tsubst_template_args (targs, atargs, complain, in_decl); 30530 30531 /* Now rewrite the non-deduced parms of f. */ 30532 for (unsigned i = 0; ndlen && i < len; ++i) 30533 if (TREE_VEC_ELT (targs, i) == NULL_TREE) 30534 { 30535 --ndlen; 30536 unsigned index = j++; 30537 tree oldlist = TREE_VEC_ELT (ftparms, i); 30538 tree list = rewrite_tparm_list (oldlist, index, level, 30539 targs, i, complain); 30540 TREE_VEC_ELT (gtparms, index) = list; 30541 } 30542 gtparms = build_tree_list (size_one_node, gtparms); 30543 30544 /* Substitute the deduced arguments plus the rewritten template 30545 parameters into f to get g. This covers the type, copyness, 30546 guideness, and explicit-specifier. */ 30547 tree g; 30548 { 30549 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped 30550 if cp_unevaluated_operand. */ 30551 cp_evaluated ev; 30552 g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain, 30553 /*use_spec_table=*/false); 30554 } 30555 if (g == error_mark_node) 30556 continue; 30557 DECL_NAME (g) = name; 30558 if (nfparms == 0) 30559 { 30560 /* The targs are all non-dependent, so g isn't a template. */ 30561 fprime = g; 30562 ret = TREE_TYPE (TREE_TYPE (fprime)); 30563 goto non_template; 30564 } 30565 DECL_USE_TEMPLATE (g) = 0; 30566 fprime = build_template_decl (g, gtparms, false); 30567 DECL_TEMPLATE_RESULT (fprime) = g; 30568 TREE_TYPE (fprime) = TREE_TYPE (g); 30569 tree gtargs = template_parms_to_args (gtparms); 30570 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs); 30571 DECL_PRIMARY_TEMPLATE (fprime) = fprime; 30572 30573 /* Substitute the associated constraints. */ 30574 tree ci = get_constraints (f); 30575 if (ci) 30576 { 30577 if (tree outer_targs = outer_template_args (f)) 30578 ci = tsubst_constraint_info (ci, outer_targs, complain, in_decl); 30579 ci = tsubst_constraint_info (ci, targs, complain, in_decl); 30580 } 30581 if (ci == error_mark_node) 30582 continue; 30583 30584 /* Add a constraint that the return type matches the instantiation of 30585 A with the same template arguments. */ 30586 ret = TREE_TYPE (TREE_TYPE (fprime)); 30587 if (ctad_kind == alias 30588 && (!same_type_p (atype, ret) 30589 /* FIXME this should mean they don't compare as equivalent. */ 30590 || dependent_alias_template_spec_p (atype, nt_opaque))) 30591 { 30592 tree same = finish_trait_expr (loc, CPTK_IS_DEDUCIBLE, tmpl, ret); 30593 ci = append_constraint (ci, same); 30594 } 30595 30596 if (ci) 30597 { 30598 remove_constraints (fprime); 30599 set_constraints (fprime, ci); 30600 } 30601 } 30602 else 30603 { 30604 /* For a non-template deduction guide, if the arguments of A aren't 30605 deducible from the return type, don't add the candidate. */ 30606 non_template: 30607 if (ctad_kind == alias 30608 && !type_targs_deducible_from (tmpl, ret)) 30609 continue; 30610 } 30611 30612 /* Rewrite the return type of the inherited guide in terms of the 30613 derived class. This is specified as replacing the return type R 30614 with typename CC<R>::type where the partially specialized CC maps a 30615 base class specialization to a specialization of the derived class 30616 having such a base (inducing substitution failure if no such derived 30617 class exists). 30618 30619 As specified this mapping would be done at instantiation time using 30620 non-dependent template arguments, but we do it ahead of time using 30621 the generic arguments. This seems to be good enough since generic 30622 deduction should succeed only if concrete deduction would. */ 30623 if (ctad_kind == inherited) 30624 { 30625 processing_template_decl_sentinel ptds (/*reset*/false); 30626 if (TREE_CODE (fprime) == TEMPLATE_DECL) 30627 ++processing_template_decl; 30628 30629 tree targs = type_targs_deducible_from (tmpl, ret); 30630 if (!targs) 30631 continue; 30632 30633 if (TREE_CODE (f) != TEMPLATE_DECL) 30634 fprime = copy_decl (fprime); 30635 tree fntype = TREE_TYPE (fprime); 30636 ret = lookup_template_class (TPARMS_PRIMARY_TEMPLATE (atparms), targs, 30637 in_decl, NULL_TREE, false, complain); 30638 fntype = build_function_type (ret, TYPE_ARG_TYPES (fntype)); 30639 TREE_TYPE (fprime) = fntype; 30640 if (TREE_CODE (fprime) == TEMPLATE_DECL) 30641 TREE_TYPE (DECL_TEMPLATE_RESULT (fprime)) = fntype; 30642 set_inherited_guide_context (fprime, utype); 30643 } 30644 30645 aguides = lookup_add (fprime, aguides); 30646 } 30647 30648 return aguides; 30649 } 30650 30651 /* CTOR is a using-decl inheriting the constructors of some base of the class 30652 template TMPL; adjust the base's guides be deduction guides for TMPL. */ 30653 30654 static tree 30655 inherited_ctad_tweaks (tree tmpl, tree ctor, tsubst_flags_t complain) 30656 { 30657 /* [over.match.class.deduct]: In addition, if C is defined and inherits 30658 constructors ([namespace.udecl]) from a direct base class denoted in the 30659 base-specifier-list by a class-or-decltype B, let A be an alias template 30660 whose template parameter list is that of C and whose defining-type-id is 30661 B. If A is a deducible template ([dcl.type.simple]), the set contains the 30662 guides of A with the return type R of each guide replaced with typename 30663 CC::type given a class template 30664 30665 template <typename> class CC; 30666 30667 whose primary template is not defined and with a single partial 30668 specialization whose template parameter list is that of A and whose 30669 template argument list is a specialization of A with the template argument 30670 list of A ([temp.dep.type]) having a member typedef type designating a 30671 template specialization with the template argument list of A but with C as 30672 the template. */ 30673 30674 tree scope = USING_DECL_SCOPE (ctor); 30675 if (TREE_CODE (scope) == TYPENAME_TYPE 30676 && (TYPE_IDENTIFIER (TYPE_CONTEXT (scope)) 30677 == TYPENAME_TYPE_FULLNAME (scope))) 30678 /* Recognize using B<T>::B::B as an inherited constructor. */ 30679 /* FIXME: Also recognize using C::B::B? We might have to call 30680 resolve_typename_type for that. */ 30681 scope = TYPE_CONTEXT (scope); 30682 if (!CLASS_TYPE_P (scope) 30683 || !CLASSTYPE_TEMPLATE_INFO (scope) 30684 || !PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))) 30685 return NULL_TREE; 30686 30687 tree t = build_tree_list (DECL_TEMPLATE_PARMS (tmpl), scope); 30688 bool any_dguides_p; 30689 tree uguides = deduction_guides_for (CLASSTYPE_TI_TEMPLATE (scope), 30690 any_dguides_p, complain); 30691 return alias_ctad_tweaks (t, uguides); 30692 } 30693 30694 /* If template arguments for TMPL can be deduced from TYPE, return 30695 the deduced arguments, otherwise return NULL_TREE. 30696 Used to implement CPTK_IS_DEDUCIBLE for alias CTAD according to 30697 [over.match.class.deduct]. 30698 30699 This check is specified in terms of partial specialization, so the behavior 30700 should be parallel to that of get_partial_spec_bindings. */ 30701 30702 tree 30703 type_targs_deducible_from (tree tmpl, tree type) 30704 { 30705 tree tparms, ttype; 30706 if (TREE_CODE (tmpl) == TEMPLATE_DECL) 30707 { 30708 /* If tmpl is a class template, this is trivial: it's deducible if 30709 TYPE is a specialization of TMPL. */ 30710 if (DECL_CLASS_TEMPLATE_P (tmpl)) 30711 { 30712 if (CLASS_TYPE_P (type) 30713 && CLASSTYPE_TEMPLATE_INFO (type) 30714 && CLASSTYPE_TI_TEMPLATE (type) == tmpl) 30715 return INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); 30716 else 30717 return NULL_TREE; 30718 } 30719 30720 /* Otherwise it's an alias template. */ 30721 tparms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); 30722 ttype = TREE_TYPE (tmpl); 30723 } 30724 else 30725 { 30726 /* TMPL is a synthetic alias template represented as a TREE_LIST as 30727 per alias_ctad_tweaks. */ 30728 tparms = INNERMOST_TEMPLATE_PARMS (TREE_PURPOSE (tmpl)); 30729 ttype = TREE_VALUE (tmpl); 30730 tmpl = TI_TEMPLATE (TYPE_TEMPLATE_INFO_MAYBE_ALIAS (ttype)); 30731 } 30732 30733 int len = TREE_VEC_LENGTH (tparms); 30734 tree targs = make_tree_vec (len); 30735 bool tried_array_deduction = (cxx_dialect < cxx17); 30736 30737 again: 30738 if (unify (tparms, targs, ttype, type, 30739 UNIFY_ALLOW_NONE, false)) 30740 return NULL_TREE; 30741 30742 /* We don't fail on an undeduced targ the second time through (like 30743 get_partial_spec_bindings) because we're going to try defaults. */ 30744 for (int i = 0; i < len; ++i) 30745 if (! TREE_VEC_ELT (targs, i)) 30746 { 30747 tree tparm = TREE_VEC_ELT (tparms, i); 30748 tparm = TREE_VALUE (tparm); 30749 30750 if (!tried_array_deduction 30751 && TREE_CODE (tparm) == TYPE_DECL) 30752 { 30753 try_array_deduction (tparms, targs, ttype); 30754 tried_array_deduction = true; 30755 if (TREE_VEC_ELT (targs, i)) 30756 goto again; 30757 } 30758 /* If the type parameter is a parameter pack, then it will be deduced 30759 to an empty parameter pack. This is another case that doesn't model 30760 well as partial specialization. */ 30761 if (template_parameter_pack_p (tparm)) 30762 { 30763 tree arg; 30764 if (TREE_CODE (tparm) == PARM_DECL) 30765 { 30766 arg = make_node (NONTYPE_ARGUMENT_PACK); 30767 TREE_CONSTANT (arg) = 1; 30768 } 30769 else 30770 arg = cxx_make_type (TYPE_ARGUMENT_PACK); 30771 ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0); 30772 TREE_VEC_ELT (targs, i) = arg; 30773 } 30774 } 30775 30776 /* Maybe add in default template args. This seems like a flaw in the 30777 specification in terms of partial specialization, since it says the 30778 partial specialization has the template parameter list of A, but a 30779 partial specialization can't have default targs. */ 30780 targs = coerce_template_parms (tparms, targs, tmpl, tf_none); 30781 if (targs == error_mark_node) 30782 return NULL_TREE; 30783 30784 /* I believe we don't need the template_template_parm_bindings_ok_p call 30785 because coerce_template_parms did coerce_template_template_parms. */ 30786 30787 if (!constraints_satisfied_p (tmpl, targs)) 30788 return NULL_TREE; 30789 30790 return targs; 30791 } 30792 30793 /* Return artificial deduction guides built from the constructors of class 30794 template TMPL. */ 30795 30796 static tree 30797 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain) 30798 { 30799 tree outer_args = outer_template_args (tmpl); 30800 tree type = TREE_TYPE (most_general_template (tmpl)); 30801 30802 tree cands = NULL_TREE; 30803 30804 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter) 30805 { 30806 /* We handle C++23 inherited CTAD below. */ 30807 if (iter.using_p ()) 30808 continue; 30809 30810 tree guide = build_deduction_guide (type, *iter, outer_args, complain); 30811 cands = lookup_add (guide, cands); 30812 } 30813 30814 if (cxx_dialect >= cxx23) 30815 /* FIXME: CLASSTYPE_CONSTRUCTORS doesn't contain inherited constructors if 30816 e.g. the class also has a user-defined constructor. So instead iterate 30817 over TYPE_FIELDS manually to robustly find all relevant using-decls. */ 30818 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 30819 if (TREE_CODE (field) == USING_DECL 30820 && DECL_NAME (field) == ctor_identifier) 30821 { 30822 tree uguides = inherited_ctad_tweaks (tmpl, field, complain); 30823 if (uguides) 30824 cands = lookup_add (uguides, cands); 30825 } 30826 30827 /* Add implicit default constructor deduction guide. */ 30828 if (!TYPE_HAS_USER_CONSTRUCTOR (type)) 30829 { 30830 tree guide = build_deduction_guide (type, type, outer_args, 30831 complain); 30832 cands = lookup_add (guide, cands); 30833 } 30834 30835 /* Add copy guide. */ 30836 { 30837 tree gtype = build_reference_type (type); 30838 tree guide = build_deduction_guide (type, gtype, outer_args, 30839 complain); 30840 cands = lookup_add (guide, cands); 30841 } 30842 30843 return cands; 30844 } 30845 30846 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache; 30847 30848 /* Return the non-aggregate deduction guides for deducible template TMPL. The 30849 aggregate candidate is added separately because it depends on the 30850 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction 30851 guide. */ 30852 30853 static tree 30854 deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain) 30855 { 30856 tree guides = NULL_TREE; 30857 if (DECL_ALIAS_TEMPLATE_P (tmpl)) 30858 { 30859 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); 30860 tree tinfo = get_template_info (under); 30861 guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p, 30862 complain); 30863 } 30864 else 30865 { 30866 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), 30867 dguide_name (tmpl), 30868 LOOK_want::NORMAL, /*complain*/false); 30869 if (guides == error_mark_node) 30870 guides = NULL_TREE; 30871 else 30872 any_dguides_p = true; 30873 } 30874 30875 /* Cache the deduction guides for a template. We also remember the result of 30876 lookup, and rebuild everything if it changes; should be very rare. */ 30877 /* FIXME: Also rebuild if this is a class template that inherits guides from a 30878 base class, and lookup for the latter changed. */ 30879 tree_pair_p cache = NULL; 30880 if (tree_pair_p &r 30881 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl)) 30882 { 30883 cache = r; 30884 if (cache->purpose == guides) 30885 return cache->value; 30886 } 30887 else 30888 { 30889 r = cache = ggc_cleared_alloc<tree_pair_s> (); 30890 cache->purpose = guides; 30891 } 30892 30893 tree cands = NULL_TREE; 30894 if (DECL_ALIAS_TEMPLATE_P (tmpl)) 30895 cands = alias_ctad_tweaks (tmpl, guides); 30896 else 30897 { 30898 cands = ctor_deduction_guides_for (tmpl, complain); 30899 for (ovl_iterator it (guides); it; ++it) 30900 cands = lookup_add (*it, cands); 30901 } 30902 30903 cache->value = cands; 30904 return cands; 30905 } 30906 30907 /* Return whether TMPL is a (class template argument-) deducible template. */ 30908 30909 bool 30910 ctad_template_p (tree tmpl) 30911 { 30912 /* A deducible template is either a class template or is an alias template 30913 whose defining-type-id is of the form 30914 30915 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id 30916 30917 where the nested-name-specifier (if any) is non-dependent and the 30918 template-name of the simple-template-id names a deducible template. */ 30919 30920 if (DECL_CLASS_TEMPLATE_P (tmpl) 30921 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)) 30922 return true; 30923 if (!DECL_ALIAS_TEMPLATE_P (tmpl)) 30924 return false; 30925 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); 30926 if (tree tinfo = get_template_info (orig)) 30927 return ctad_template_p (TI_TEMPLATE (tinfo)); 30928 return false; 30929 } 30930 30931 /* Deduce template arguments for the class template placeholder PTYPE for 30932 template TMPL based on the initializer INIT, and return the resulting 30933 type. */ 30934 30935 static tree 30936 do_class_deduction (tree ptype, tree tmpl, tree init, tree outer_targs, 30937 int flags, tsubst_flags_t complain) 30938 { 30939 /* We should have handled this in the caller. */ 30940 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)) 30941 return ptype; 30942 30943 /* If the class was erroneous, don't try to deduce, because that 30944 can generate a lot of diagnostic. */ 30945 if (TREE_TYPE (tmpl) 30946 && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl)) 30947 && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl))) 30948 return ptype; 30949 30950 /* Wait until the enclosing scope is non-dependent. */ 30951 if (DECL_CLASS_SCOPE_P (tmpl) 30952 && dependent_type_p (DECL_CONTEXT (tmpl))) 30953 return ptype; 30954 30955 /* Initializing one placeholder from another. */ 30956 if (init 30957 && (TREE_CODE (init) == TEMPLATE_PARM_INDEX 30958 || (TREE_CODE (init) == EXPR_PACK_EXPANSION 30959 && (TREE_CODE (PACK_EXPANSION_PATTERN (init)) 30960 == TEMPLATE_PARM_INDEX))) 30961 && is_auto (TREE_TYPE (init)) 30962 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl) 30963 return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype)); 30964 30965 if (!ctad_template_p (tmpl)) 30966 { 30967 if (complain & tf_error) 30968 error ("non-deducible template %qT used without template arguments", tmpl); 30969 return error_mark_node; 30970 } 30971 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl)) 30972 { 30973 if (complain & tf_error) 30974 { 30975 /* Be permissive with equivalent alias templates. */ 30976 tree u = get_underlying_template (tmpl); 30977 diagnostic_t dk = (u == tmpl) ? DK_ERROR : DK_PEDWARN; 30978 bool complained 30979 = emit_diagnostic (dk, input_location, 0, 30980 "alias template deduction only available " 30981 "with %<-std=c++20%> or %<-std=gnu++20%>"); 30982 if (u == tmpl) 30983 return error_mark_node; 30984 else if (complained) 30985 { 30986 inform (input_location, "use %qD directly instead", u); 30987 tmpl = u; 30988 } 30989 } 30990 else 30991 return error_mark_node; 30992 } 30993 30994 /* Wait until the initializer is non-dependent. */ 30995 if (type_dependent_expression_p (init)) 30996 return ptype; 30997 30998 if (outer_targs) 30999 { 31000 int args_depth = TMPL_ARGS_DEPTH (outer_targs); 31001 int parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); 31002 if (parms_depth > 1) 31003 { 31004 /* Substitute outer arguments into this CTAD template from the 31005 current instantiation. */ 31006 int want = std::min (args_depth, parms_depth - 1); 31007 outer_targs = strip_innermost_template_args (outer_targs, 31008 args_depth - want); 31009 tmpl = tsubst (tmpl, outer_targs, complain, NULL_TREE); 31010 if (tmpl == error_mark_node) 31011 return error_mark_node; 31012 } 31013 } 31014 31015 /* Don't bother with the alias rules for an equivalent template. */ 31016 tmpl = get_underlying_template (tmpl); 31017 31018 tree type = TREE_TYPE (tmpl); 31019 31020 bool try_list_cand = false; 31021 bool list_init_p = false; 31022 31023 releasing_vec rv_args = NULL; 31024 vec<tree,va_gc> *&args = *&rv_args; 31025 if (init == NULL_TREE) 31026 args = make_tree_vector (); 31027 else if (BRACE_ENCLOSED_INITIALIZER_P (init)) 31028 { 31029 list_init_p = true; 31030 try_list_cand = true; 31031 if (CONSTRUCTOR_NELTS (init) == 1 31032 && !CONSTRUCTOR_IS_DESIGNATED_INIT (init)) 31033 { 31034 /* As an exception, the first phase in 16.3.1.7 (considering the 31035 initializer list as a single argument) is omitted if the 31036 initializer list consists of a single expression of type cv U, 31037 where U is a specialization of C or a class derived from a 31038 specialization of C. */ 31039 tree elt = CONSTRUCTOR_ELT (init, 0)->value; 31040 if (is_spec_or_derived (TREE_TYPE (elt), tmpl)) 31041 try_list_cand = false; 31042 } 31043 if (try_list_cand || is_std_init_list (type)) 31044 args = make_tree_vector_single (init); 31045 else 31046 args = make_tree_vector_from_ctor (init); 31047 } 31048 else if (TREE_CODE (init) == TREE_LIST) 31049 args = make_tree_vector_from_list (init); 31050 else 31051 args = make_tree_vector_single (init); 31052 31053 /* Do this now to avoid problems with erroneous args later on. */ 31054 args = resolve_args (args, complain); 31055 if (args == NULL) 31056 return error_mark_node; 31057 31058 bool any_dguides_p = false; 31059 tree cands = deduction_guides_for (tmpl, any_dguides_p, complain); 31060 if (cands == error_mark_node) 31061 return error_mark_node; 31062 31063 /* Prune explicit deduction guides in copy-initialization context (but 31064 not copy-list-initialization). */ 31065 bool elided = false; 31066 if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING)) 31067 { 31068 for (lkp_iterator iter (cands); !elided && iter; ++iter) 31069 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter))) 31070 elided = true; 31071 31072 if (elided) 31073 { 31074 /* Found a nonconverting guide, prune the candidates. */ 31075 tree pruned = NULL_TREE; 31076 for (lkp_iterator iter (cands); iter; ++iter) 31077 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter))) 31078 pruned = lookup_add (*iter, pruned); 31079 31080 cands = pruned; 31081 } 31082 } 31083 31084 if (!any_dguides_p) 31085 if (tree guide = maybe_aggr_guide (tmpl, init, args)) 31086 cands = lookup_add (guide, cands); 31087 31088 tree fndecl = error_mark_node; 31089 31090 /* If this is list-initialization and the class has a list guide, first 31091 try deducing from the list as a single argument, as [over.match.list]. */ 31092 if (try_list_cand) 31093 { 31094 tree list_cands = NULL_TREE; 31095 for (tree dg : lkp_range (cands)) 31096 if (is_list_ctor (dg)) 31097 list_cands = lookup_add (dg, list_cands); 31098 if (list_cands) 31099 fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none); 31100 if (fndecl == error_mark_node) 31101 { 31102 /* That didn't work, now try treating the list as a sequence of 31103 arguments. */ 31104 release_tree_vector (args); 31105 args = make_tree_vector_from_ctor (init); 31106 args = resolve_args (args, complain); 31107 if (args == NULL) 31108 return error_mark_node; 31109 } 31110 } 31111 31112 if (elided && !cands) 31113 { 31114 error ("cannot deduce template arguments for copy-initialization" 31115 " of %qT, as it has no non-explicit deduction guides or " 31116 "user-declared constructors", type); 31117 return error_mark_node; 31118 } 31119 else if (!cands && fndecl == error_mark_node) 31120 { 31121 error ("cannot deduce template arguments of %qT, as it has no viable " 31122 "deduction guides", type); 31123 return error_mark_node; 31124 } 31125 31126 if (fndecl == error_mark_node) 31127 fndecl = perform_dguide_overload_resolution (cands, args, tf_none); 31128 31129 if (fndecl == error_mark_node) 31130 { 31131 if (complain & tf_warning_or_error) 31132 { 31133 error ("class template argument deduction failed:"); 31134 perform_dguide_overload_resolution (cands, args, complain); 31135 if (elided) 31136 inform (input_location, "explicit deduction guides not considered " 31137 "for copy-initialization"); 31138 } 31139 return error_mark_node; 31140 } 31141 /* [over.match.list]/1: In copy-list-initialization, if an explicit 31142 constructor is chosen, the initialization is ill-formed. */ 31143 else if (flags & LOOKUP_ONLYCONVERTING) 31144 { 31145 if (DECL_NONCONVERTING_P (fndecl)) 31146 { 31147 if (complain & tf_warning_or_error) 31148 { 31149 // TODO: Pass down location from cp_finish_decl. 31150 error ("class template argument deduction for %qT failed: " 31151 "explicit deduction guide selected in " 31152 "copy-list-initialization", type); 31153 inform (DECL_SOURCE_LOCATION (fndecl), 31154 "explicit deduction guide declared here"); 31155 31156 } 31157 return error_mark_node; 31158 } 31159 } 31160 31161 /* If CTAD succeeded but the type doesn't have any explicit deduction 31162 guides, this deduction might not be what the user intended. */ 31163 if (fndecl != error_mark_node && !any_dguides_p && (complain & tf_warning)) 31164 { 31165 if ((!DECL_IN_SYSTEM_HEADER (fndecl) 31166 || global_dc->m_warn_system_headers) 31167 && warning (OPT_Wctad_maybe_unsupported, 31168 "%qT may not intend to support class template argument " 31169 "deduction", type)) 31170 inform (input_location, "add a deduction guide to suppress this " 31171 "warning"); 31172 } 31173 31174 return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl)), 31175 cp_type_quals (ptype)); 31176 } 31177 31178 /* Return true if INIT is an unparenthesized id-expression or an 31179 unparenthesized class member access. Used for the argument of 31180 decltype(auto). */ 31181 31182 bool 31183 unparenthesized_id_or_class_member_access_p (tree init) 31184 { 31185 STRIP_ANY_LOCATION_WRAPPER (init); 31186 31187 /* We need to be able to tell '(r)' and 'r' apart (when it's of 31188 reference type). Only the latter is an id-expression. */ 31189 if (REFERENCE_REF_P (init) 31190 && !REF_PARENTHESIZED_P (init)) 31191 init = TREE_OPERAND (init, 0); 31192 return (DECL_P (init) 31193 || ((TREE_CODE (init) == COMPONENT_REF 31194 || TREE_CODE (init) == SCOPE_REF) 31195 && !REF_PARENTHESIZED_P (init))); 31196 } 31197 31198 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced 31199 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. 31200 The CONTEXT determines the context in which auto deduction is performed 31201 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags. 31202 31203 OUTER_TARGS is used during template argument deduction (context == adc_unify) 31204 to properly substitute the result. It's also used in the adc_unify and 31205 adc_requirement contexts to communicate the necessary template arguments 31206 to satisfaction. OUTER_TARGS is ignored in other contexts. 31207 31208 Additionally for adc_unify contexts TMPL is the template for which TYPE 31209 is a template parameter type. 31210 31211 For partial-concept-ids, extra args from OUTER_TARGS, TMPL and the current 31212 scope may be appended to the list of deduced template arguments prior to 31213 determining constraint satisfaction as appropriate. */ 31214 31215 tree 31216 do_auto_deduction (tree type, tree init, tree auto_node, 31217 tsubst_flags_t complain /* = tf_warning_or_error */, 31218 auto_deduction_context context /* = adc_unspecified */, 31219 tree outer_targs /* = NULL_TREE */, 31220 int flags /* = LOOKUP_NORMAL */, 31221 tree tmpl /* = NULL_TREE */) 31222 { 31223 if (type == error_mark_node || init == error_mark_node) 31224 return error_mark_node; 31225 31226 if (init && type_dependent_expression_p (init) 31227 && context != adc_unify) 31228 /* Defining a subset of type-dependent expressions that we can deduce 31229 from ahead of time isn't worth the trouble. */ 31230 return type; 31231 31232 /* Similarly, we can't deduce from another undeduced decl. */ 31233 if (init && undeduced_auto_decl (init)) 31234 return type; 31235 31236 /* We may be doing a partial substitution, but we still want to replace 31237 auto_node. */ 31238 complain &= ~tf_partial; 31239 31240 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)) 31241 { 31242 /* We don't recurse here because we can't deduce from a nested 31243 initializer_list. */ 31244 if (CONSTRUCTOR_ELTS (init)) 31245 for (constructor_elt &elt : CONSTRUCTOR_ELTS (init)) 31246 elt.value = resolve_nondeduced_context (elt.value, complain); 31247 } 31248 else if (init) 31249 init = resolve_nondeduced_context (init, complain); 31250 31251 /* In C++23, we must deduce the type to int&& for code like 31252 decltype(auto) f(int&& x) { return (x); } 31253 or 31254 auto&& f(int x) { return x; } 31255 so we use treat_lvalue_as_rvalue_p. But don't do it for 31256 decltype(auto) f(int x) { return x; } 31257 where we should deduce 'int' rather than 'int&&'; transmogrifying 31258 INIT to an rvalue would break that. */ 31259 tree r; 31260 if (cxx_dialect >= cxx23 31261 && context == adc_return_type 31262 && (!AUTO_IS_DECLTYPE (auto_node) 31263 || !unparenthesized_id_or_class_member_access_p (init)) 31264 && (r = treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init), 31265 /*return*/true))) 31266 init = r; 31267 31268 if (tree ctmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node)) 31269 /* C++17 class template argument deduction. */ 31270 return do_class_deduction (type, ctmpl, init, outer_targs, flags, complain); 31271 31272 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE) 31273 /* Nothing we can do with this, even in deduction context. */ 31274 return type; 31275 31276 location_t loc = cp_expr_loc_or_input_loc (init); 31277 31278 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto 31279 with either a new invented type template parameter U or, if the 31280 initializer is a braced-init-list (8.5.4), with 31281 std::initializer_list<U>. */ 31282 if (BRACE_ENCLOSED_INITIALIZER_P (init)) 31283 { 31284 if (!DIRECT_LIST_INIT_P (init)) 31285 type = listify_autos (type, auto_node); 31286 else if (CONSTRUCTOR_NELTS (init) == 1) 31287 init = CONSTRUCTOR_ELT (init, 0)->value; 31288 else 31289 { 31290 if (complain & tf_warning_or_error) 31291 { 31292 if (permerror (loc, "direct-list-initialization of " 31293 "%<auto%> requires exactly one element")) 31294 inform (loc, 31295 "for deduction to %<std::initializer_list%>, use copy-" 31296 "list-initialization (i.e. add %<=%> before the %<{%>)"); 31297 } 31298 type = listify_autos (type, auto_node); 31299 } 31300 } 31301 31302 if (type == error_mark_node || init == error_mark_node) 31303 return error_mark_node; 31304 31305 tree targs; 31306 if (context == adc_decomp_type 31307 && auto_node == type 31308 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE) 31309 { 31310 /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers 31311 and initializer has array type, deduce cv-qualified array type. */ 31312 targs = make_tree_vec (1); 31313 TREE_VEC_ELT (targs, 0) = TREE_TYPE (init); 31314 } 31315 else if (AUTO_IS_DECLTYPE (auto_node)) 31316 { 31317 const bool id = unparenthesized_id_or_class_member_access_p (init); 31318 tree deduced = finish_decltype_type (init, id, complain); 31319 deduced = canonicalize_type_argument (deduced, complain); 31320 if (deduced == error_mark_node) 31321 return error_mark_node; 31322 targs = make_tree_vec (1); 31323 TREE_VEC_ELT (targs, 0) = deduced; 31324 } 31325 else 31326 { 31327 if (error_operand_p (init)) 31328 return error_mark_node; 31329 31330 tree parms = build_tree_list (NULL_TREE, type); 31331 tree tparms; 31332 31333 if (flag_concepts_ts) 31334 tparms = extract_autos (type); 31335 else 31336 { 31337 tparms = make_tree_vec (1); 31338 TREE_VEC_ELT (tparms, 0) 31339 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node)); 31340 } 31341 31342 targs = make_tree_vec (TREE_VEC_LENGTH (tparms)); 31343 int val = type_unification_real (tparms, targs, parms, &init, 1, 0, 31344 DEDUCE_CALL, 31345 NULL, /*explain_p=*/false); 31346 if (val > 0) 31347 { 31348 if (processing_template_decl) 31349 /* Try again at instantiation time. */ 31350 return type; 31351 if (type && type != error_mark_node 31352 && (complain & tf_error)) 31353 /* If type is error_mark_node a diagnostic must have been 31354 emitted by now. Also, having a mention to '<type error>' 31355 in the diagnostic is not really useful to the user. */ 31356 { 31357 if (cfun 31358 && FNDECL_USED_AUTO (current_function_decl) 31359 && (auto_node 31360 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl)) 31361 && LAMBDA_FUNCTION_P (current_function_decl)) 31362 error_at (loc, "unable to deduce lambda return type from %qE", 31363 init); 31364 else 31365 error_at (loc, "unable to deduce %qT from %qE", type, init); 31366 type_unification_real (tparms, targs, parms, &init, 1, 0, 31367 DEDUCE_CALL, 31368 NULL, /*explain_p=*/true); 31369 } 31370 return error_mark_node; 31371 } 31372 } 31373 31374 /* Check any placeholder constraints against the deduced type. */ 31375 if (processing_template_decl && context == adc_unify) 31376 /* Constraints will be checked after deduction. */; 31377 else if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))) 31378 { 31379 if (processing_template_decl) 31380 { 31381 gcc_checking_assert (context == adc_variable_type 31382 || context == adc_return_type 31383 || context == adc_decomp_type); 31384 gcc_checking_assert (!type_dependent_expression_p (init)); 31385 /* If the constraint is dependent, we need to wait until 31386 instantiation time to resolve the placeholder. */ 31387 if (placeholder_type_constraint_dependent_p (constr)) 31388 return type; 31389 } 31390 31391 if (context == adc_return_type 31392 || context == adc_variable_type 31393 || context == adc_decomp_type) 31394 if (tree fn = current_function_decl) 31395 if (DECL_TEMPLATE_INFO (fn) || LAMBDA_FUNCTION_P (fn)) 31396 { 31397 outer_targs = DECL_TEMPLATE_INFO (fn) 31398 ? DECL_TI_ARGS (fn) : NULL_TREE; 31399 if (LAMBDA_FUNCTION_P (fn)) 31400 { 31401 /* As in satisfy_declaration_constraints. */ 31402 tree regen_args = lambda_regenerating_args (fn); 31403 if (outer_targs) 31404 outer_targs = add_to_template_args (regen_args, outer_targs); 31405 else 31406 outer_targs = regen_args; 31407 } 31408 } 31409 31410 tree full_targs = outer_targs; 31411 if (context == adc_unify && tmpl) 31412 full_targs = add_outermost_template_args (tmpl, full_targs); 31413 full_targs = add_to_template_args (full_targs, targs); 31414 31415 /* HACK: Compensate for callers not always communicating all levels of 31416 outer template arguments by filling in the outermost missing levels 31417 with dummy levels before checking satisfaction. We'll still crash 31418 if the constraint depends on a template argument belonging to one of 31419 these missing levels, but this hack otherwise allows us to handle a 31420 large subset of possible constraints (including all non-dependent 31421 constraints). */ 31422 if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node) 31423 - TMPL_ARGS_DEPTH (full_targs))) 31424 { 31425 tree dummy_levels = make_tree_vec (missing_levels); 31426 for (int i = 0; i < missing_levels; ++i) 31427 TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0); 31428 full_targs = add_to_template_args (dummy_levels, full_targs); 31429 } 31430 31431 if (!constraints_satisfied_p (auto_node, full_targs)) 31432 { 31433 if (complain & tf_warning_or_error) 31434 { 31435 auto_diagnostic_group d; 31436 switch (context) 31437 { 31438 case adc_unspecified: 31439 case adc_unify: 31440 error_at (loc, "placeholder constraints not satisfied"); 31441 break; 31442 case adc_variable_type: 31443 case adc_decomp_type: 31444 error_at (loc, "deduced initializer does not satisfy " 31445 "placeholder constraints"); 31446 break; 31447 case adc_return_type: 31448 error_at (loc, "deduced return type does not satisfy " 31449 "placeholder constraints"); 31450 break; 31451 case adc_requirement: 31452 error_at (loc, "deduced expression type does not satisfy " 31453 "placeholder constraints"); 31454 break; 31455 } 31456 diagnose_constraints (loc, auto_node, full_targs); 31457 } 31458 return error_mark_node; 31459 } 31460 } 31461 31462 if (TEMPLATE_TYPE_LEVEL (auto_node) == 0) 31463 { 31464 /* Substitute this level-less auto via tsubst by temporarily 31465 overriding its level to 1. */ 31466 TEMPLATE_TYPE_LEVEL (auto_node) = 1; 31467 type = tsubst (type, targs, complain, NULL_TREE); 31468 TEMPLATE_TYPE_LEVEL (auto_node) = 0; 31469 return type; 31470 } 31471 31472 if (TEMPLATE_TYPE_LEVEL (auto_node) == 1) 31473 /* The outer template arguments are already substituted into type 31474 (but we still may have used them for constraint checking above). */; 31475 else if (context == adc_unify) 31476 targs = add_to_template_args (outer_targs, targs); 31477 else if (processing_template_decl) 31478 targs = add_to_template_args (current_template_args (), targs); 31479 return tsubst (type, targs, complain, NULL_TREE); 31480 } 31481 31482 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the 31483 result. */ 31484 31485 tree 31486 splice_late_return_type (tree type, tree late_return_type) 31487 { 31488 if (late_return_type) 31489 { 31490 gcc_assert (is_auto (type) || seen_error ()); 31491 return late_return_type; 31492 } 31493 31494 if (tree auto_node = find_type_usage (type, is_auto)) 31495 if (TEMPLATE_TYPE_LEVEL (auto_node) <= current_template_depth) 31496 { 31497 /* In an abbreviated function template we didn't know we were dealing 31498 with a function template when we saw the auto return type, so rebuild 31499 the return type using an auto with the correct level. */ 31500 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), false); 31501 tree auto_vec = make_tree_vec (1); 31502 TREE_VEC_ELT (auto_vec, 0) = new_auto; 31503 tree targs = add_outermost_template_args (current_template_args (), 31504 auto_vec); 31505 /* Also rebuild the constraint info in terms of the new auto. */ 31506 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)) 31507 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto) 31508 = build_tree_list (current_template_parms, 31509 tsubst_constraint (TREE_VALUE (ci), targs, 31510 tf_none, NULL_TREE)); 31511 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto); 31512 return tsubst (type, targs, tf_none, NULL_TREE); 31513 } 31514 return type; 31515 } 31516 31517 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or 31518 'decltype(auto)' or a deduced class template. */ 31519 31520 bool 31521 is_auto (const_tree type) 31522 { 31523 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM 31524 && (TYPE_IDENTIFIER (type) == auto_identifier 31525 || TYPE_IDENTIFIER (type) == decltype_auto_identifier)) 31526 return true; 31527 else 31528 return false; 31529 } 31530 31531 /* for_each_template_parm callback for type_uses_auto. */ 31532 31533 int 31534 is_auto_r (tree tp, void */*data*/) 31535 { 31536 return is_auto (tp); 31537 } 31538 31539 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains 31540 a use of `auto'. Returns NULL_TREE otherwise. */ 31541 31542 tree 31543 type_uses_auto (tree type) 31544 { 31545 if (type == NULL_TREE) 31546 return NULL_TREE; 31547 31548 /* For parameter packs, check the contents of the pack. */ 31549 if (PACK_EXPANSION_P (type)) 31550 type = PACK_EXPANSION_PATTERN (type); 31551 31552 if (flag_concepts_ts) 31553 { 31554 /* The Concepts TS allows multiple autos in one type-specifier; just 31555 return the first one we find, do_auto_deduction will collect all of 31556 them. */ 31557 if (uses_template_parms (type)) 31558 return for_each_template_parm (type, is_auto_r, /*data*/NULL, 31559 /*visited*/NULL, /*nondeduced*/false); 31560 else 31561 return NULL_TREE; 31562 } 31563 else 31564 return find_type_usage (type, is_auto); 31565 } 31566 31567 /* Report ill-formed occurrences of auto types in ARGUMENTS. If 31568 concepts are enabled, auto is acceptable in template arguments, but 31569 only when TEMPL identifies a template class. Return TRUE if any 31570 such errors were reported. */ 31571 31572 bool 31573 check_auto_in_tmpl_args (tree tmpl, tree args) 31574 { 31575 if (!flag_concepts_ts) 31576 /* Only the concepts TS allows 'auto' as a type-id; it'd otherwise 31577 have already been rejected by the parser more generally. */ 31578 return false; 31579 31580 /* If there were previous errors, nevermind. */ 31581 if (!args || TREE_CODE (args) != TREE_VEC) 31582 return false; 31583 31584 /* If TMPL is an identifier, we're parsing and we can't tell yet 31585 whether TMPL is supposed to be a type, a function or a variable. 31586 We'll only be able to tell during template substitution, so we 31587 expect to be called again then. If concepts are enabled and we 31588 know we have a type, we're ok. */ 31589 if (identifier_p (tmpl) 31590 || (DECL_P (tmpl) 31591 && (DECL_TYPE_TEMPLATE_P (tmpl) 31592 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))) 31593 return false; 31594 31595 /* Quickly search for any occurrences of auto; usually there won't 31596 be any, and then we'll avoid allocating the vector. */ 31597 if (!type_uses_auto (args)) 31598 return false; 31599 31600 bool errors = false; 31601 31602 tree vec = extract_autos (args); 31603 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++) 31604 { 31605 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i)); 31606 error_at (DECL_SOURCE_LOCATION (xauto), 31607 "invalid use of %qT in template argument", xauto); 31608 errors = true; 31609 } 31610 31611 return errors; 31612 } 31613 31614 /* Recursively walk over && expressions searching for EXPR. Return a reference 31615 to that expression. */ 31616 31617 static tree *find_template_requirement (tree *t, tree key) 31618 { 31619 if (*t == key) 31620 return t; 31621 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR) 31622 { 31623 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key)) 31624 return p; 31625 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key)) 31626 return p; 31627 } 31628 return 0; 31629 } 31630 31631 /* Convert the generic type parameters in PARM that match the types given in the 31632 range [START_IDX, END_IDX) from the current_template_parms into generic type 31633 packs. */ 31634 31635 tree 31636 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx) 31637 { 31638 tree current = current_template_parms; 31639 int depth = TMPL_PARMS_DEPTH (current); 31640 current = INNERMOST_TEMPLATE_PARMS (current); 31641 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current)); 31642 31643 for (int i = 0; i < start_idx; ++i) 31644 TREE_VEC_ELT (replacement, i) 31645 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i))); 31646 31647 for (int i = start_idx; i < end_idx; ++i) 31648 { 31649 /* Create a distinct parameter pack type from the current parm and add it 31650 to the replacement args to tsubst below into the generic function 31651 parameter. */ 31652 tree node = TREE_VEC_ELT (current, i); 31653 tree o = TREE_TYPE (TREE_VALUE (node)); 31654 tree t = copy_type (o); 31655 TEMPLATE_TYPE_PARM_INDEX (t) 31656 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o), 31657 t, 0, 0, tf_none); 31658 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t; 31659 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t); 31660 TYPE_MAIN_VARIANT (t) = t; 31661 TEMPLATE_TYPE_PARAMETER_PACK (t) = true; 31662 TYPE_CANONICAL (t) = canonical_type_parameter (t); 31663 TREE_VEC_ELT (replacement, i) = t; 31664 31665 /* Replace the current template parameter with new pack. */ 31666 TREE_VALUE (node) = TREE_CHAIN (t); 31667 31668 /* Surgically adjust the associated constraint of adjusted parameter 31669 and it's corresponding contribution to the current template 31670 requirements. */ 31671 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node)) 31672 { 31673 tree id = unpack_concept_check (constr); 31674 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t; 31675 /* Use UNKNOWN_LOCATION so write_template_args can tell the 31676 difference between this and a fold the user wrote. */ 31677 location_t loc = UNKNOWN_LOCATION; 31678 tree fold = finish_left_unary_fold_expr (loc, constr, 31679 TRUTH_ANDIF_EXPR); 31680 TEMPLATE_PARM_CONSTRAINTS (node) = fold; 31681 31682 /* If there was a constraint, we also need to replace that in 31683 the template requirements, which we've already built. */ 31684 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms); 31685 reqs = find_template_requirement (reqs, constr); 31686 *reqs = fold; 31687 } 31688 } 31689 31690 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i) 31691 TREE_VEC_ELT (replacement, i) 31692 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i))); 31693 31694 /* If there are more levels then build up the replacement with the outer 31695 template parms. */ 31696 if (depth > 1) 31697 replacement = add_to_template_args (template_parms_to_args 31698 (TREE_CHAIN (current_template_parms)), 31699 replacement); 31700 31701 return tsubst (parm, replacement, tf_none, NULL_TREE); 31702 } 31703 31704 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from 31705 0..N-1. */ 31706 31707 void 31708 declare_integer_pack (void) 31709 { 31710 tree ipfn = push_library_fn (get_identifier ("__integer_pack"), 31711 build_function_type_list (integer_type_node, 31712 integer_type_node, 31713 NULL_TREE), 31714 NULL_TREE, ECF_CONST); 31715 DECL_DECLARED_CONSTEXPR_P (ipfn) = true; 31716 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND, 31717 CP_BUILT_IN_INTEGER_PACK); 31718 } 31719 31720 /* Walk the decl or type specialization table calling FN on each 31721 entry. */ 31722 31723 void 31724 walk_specializations (bool decls_p, 31725 void (*fn) (bool decls_p, spec_entry *entry, void *data), 31726 void *data) 31727 { 31728 spec_hash_table *table = decls_p ? decl_specializations 31729 : type_specializations; 31730 spec_hash_table::iterator end (table->end ()); 31731 for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter) 31732 fn (decls_p, *iter, data); 31733 } 31734 31735 /* Lookup the specialization of *ELT, in the decl or type 31736 specialization table. Return the SPEC that's already there, or 31737 NULL if nothing. */ 31738 31739 tree 31740 match_mergeable_specialization (bool decl_p, spec_entry *elt) 31741 { 31742 hash_table<spec_hasher> *specializations 31743 = decl_p ? decl_specializations : type_specializations; 31744 hashval_t hash = spec_hasher::hash (elt); 31745 auto *slot = specializations->find_slot_with_hash (elt, hash, NO_INSERT); 31746 31747 if (slot) 31748 return (*slot)->spec; 31749 31750 return NULL_TREE; 31751 } 31752 31753 /* Return flags encoding whether SPEC is on the instantiation and/or 31754 specialization lists of TMPL. */ 31755 31756 unsigned 31757 get_mergeable_specialization_flags (tree tmpl, tree decl) 31758 { 31759 unsigned flags = 0; 31760 31761 for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl); 31762 inst; inst = TREE_CHAIN (inst)) 31763 if (TREE_VALUE (inst) == decl) 31764 { 31765 flags |= 1; 31766 break; 31767 } 31768 31769 if (CLASS_TYPE_P (TREE_TYPE (decl)) 31770 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)) 31771 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2) 31772 /* Only need to search if DECL is a partial specialization. */ 31773 for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); 31774 part; part = TREE_CHAIN (part)) 31775 if (TREE_VALUE (part) == decl) 31776 { 31777 flags |= 2; 31778 break; 31779 } 31780 31781 return flags; 31782 } 31783 31784 /* Add a new specialization described by SPEC. DECL is the 31785 maybe-template decl and FLAGS is as returned from 31786 get_mergeable_specialization_flags. */ 31787 31788 void 31789 add_mergeable_specialization (bool decl_p, spec_entry *elt, tree decl, 31790 unsigned flags) 31791 { 31792 hashval_t hash = spec_hasher::hash (elt); 31793 if (decl_p) 31794 { 31795 auto *slot = decl_specializations->find_slot_with_hash (elt, hash, INSERT); 31796 31797 gcc_checking_assert (!*slot); 31798 auto entry = ggc_alloc<spec_entry> (); 31799 *entry = *elt; 31800 *slot = entry; 31801 } 31802 else 31803 { 31804 auto *slot = type_specializations->find_slot_with_hash (elt, hash, INSERT); 31805 31806 /* We don't distinguish different constrained partial type 31807 specializations, so there could be duplicates. Everything else 31808 must be new. */ 31809 if (!(flags & 2 && *slot)) 31810 { 31811 gcc_checking_assert (!*slot); 31812 31813 auto entry = ggc_alloc<spec_entry> (); 31814 *entry = *elt; 31815 *slot = entry; 31816 } 31817 } 31818 31819 if (flags & 1) 31820 DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl) 31821 = tree_cons (elt->args, decl, DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl)); 31822 31823 if (flags & 2) 31824 { 31825 /* A partial specialization. */ 31826 tree cons = tree_cons (elt->args, decl, 31827 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl)); 31828 TREE_TYPE (cons) = decl_p ? TREE_TYPE (elt->spec) : elt->spec; 31829 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl) = cons; 31830 } 31831 } 31832 31833 /* Set up the hash tables for template instantiations. */ 31834 31835 void 31836 init_template_processing (void) 31837 { 31838 decl_specializations = hash_table<spec_hasher>::create_ggc (37); 31839 type_specializations = hash_table<spec_hasher>::create_ggc (37); 31840 31841 if (cxx_dialect >= cxx11) 31842 declare_integer_pack (); 31843 } 31844 31845 /* Print stats about the template hash tables for -fstats. */ 31846 31847 void 31848 print_template_statistics (void) 31849 { 31850 fprintf (stderr, "decl_specializations: size " HOST_SIZE_T_PRINT_DEC ", " 31851 HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n", 31852 (fmt_size_t) decl_specializations->size (), 31853 (fmt_size_t) decl_specializations->elements (), 31854 decl_specializations->collisions ()); 31855 fprintf (stderr, "type_specializations: size " HOST_SIZE_T_PRINT_DEC ", " 31856 HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n", 31857 (fmt_size_t) type_specializations->size (), 31858 (fmt_size_t) type_specializations->elements (), 31859 type_specializations->collisions ()); 31860 } 31861 31862 #if CHECKING_P 31863 31864 namespace selftest { 31865 31866 /* Verify that type_dependent_expression_p () works correctly, even 31867 in the presence of location wrapper nodes. */ 31868 31869 static void 31870 test_type_dependent_expression_p () 31871 { 31872 location_t loc = BUILTINS_LOCATION; 31873 31874 tree name = get_identifier ("foo"); 31875 31876 /* If no templates are involved, nothing is type-dependent. */ 31877 gcc_assert (!processing_template_decl); 31878 ASSERT_FALSE (type_dependent_expression_p (name)); 31879 31880 ++processing_template_decl; 31881 31882 /* Within a template, an unresolved name is always type-dependent. */ 31883 ASSERT_TRUE (type_dependent_expression_p (name)); 31884 31885 /* Ensure it copes with NULL_TREE and errors. */ 31886 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE)); 31887 ASSERT_FALSE (type_dependent_expression_p (error_mark_node)); 31888 31889 /* A USING_DECL in a template should be type-dependent, even if wrapped 31890 with a location wrapper (PR c++/83799). */ 31891 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE); 31892 TREE_TYPE (using_decl) = integer_type_node; 31893 ASSERT_TRUE (type_dependent_expression_p (using_decl)); 31894 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc); 31895 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl)); 31896 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl)); 31897 31898 --processing_template_decl; 31899 } 31900 31901 /* Run all of the selftests within this file. */ 31902 31903 void 31904 cp_pt_cc_tests () 31905 { 31906 test_type_dependent_expression_p (); 31907 } 31908 31909 } // namespace selftest 31910 31911 #endif /* #if CHECKING_P */ 31912 31913 #include "gt-cp-pt.h" 31914