1 /* Processing rules for constraints. 2 Copyright (C) 2013-2024 Free Software Foundation, Inc. 3 Contributed by Andrew Sutton (andrew.n.sutton (at) gmail.com) 4 5 This file is part of GCC. 6 7 GCC is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3, or (at your option) 10 any later version. 11 12 GCC is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with GCC; see the file COPYING3. If not see 19 <http://www.gnu.org/licenses/>. */ 20 21 #include "config.h" 22 #include "system.h" 23 #include "coretypes.h" 24 #include "tm.h" 25 #include "timevar.h" 26 #include "hash-set.h" 27 #include "machmode.h" 28 #include "vec.h" 29 #include "double-int.h" 30 #include "input.h" 31 #include "alias.h" 32 #include "symtab.h" 33 #include "wide-int.h" 34 #include "inchash.h" 35 #include "tree.h" 36 #include "stringpool.h" 37 #include "attribs.h" 38 #include "intl.h" 39 #include "flags.h" 40 #include "cp-tree.h" 41 #include "c-family/c-common.h" 42 #include "c-family/c-objc.h" 43 #include "cp-objcp-common.h" 44 #include "tree-inline.h" 45 #include "decl.h" 46 #include "toplev.h" 47 #include "type-utils.h" 48 49 static tree satisfaction_value (tree t); 50 51 /* When we're parsing or substuting a constraint expression, we have slightly 52 different expression semantics. In particular, we don't want to reduce a 53 concept-id to a satisfaction value. */ 54 55 processing_constraint_expression_sentinel:: 56 processing_constraint_expression_sentinel () 57 { 58 ++scope_chain->x_processing_constraint; 59 } 60 61 processing_constraint_expression_sentinel:: 62 ~processing_constraint_expression_sentinel () 63 { 64 --scope_chain->x_processing_constraint; 65 } 66 67 bool 68 processing_constraint_expression_p () 69 { 70 return scope_chain->x_processing_constraint != 0; 71 } 72 73 /*--------------------------------------------------------------------------- 74 Constraint expressions 75 ---------------------------------------------------------------------------*/ 76 77 /* Information provided to substitution. */ 78 79 struct subst_info 80 { 81 subst_info (tsubst_flags_t cmp, tree in) 82 : complain (cmp), in_decl (in) 83 { } 84 85 /* True if we should not diagnose errors. */ 86 bool quiet() const 87 { 88 return !(complain & tf_warning_or_error); 89 } 90 91 /* True if we should diagnose errors. */ 92 bool noisy() const 93 { 94 return !quiet (); 95 } 96 97 tsubst_flags_t complain; 98 tree in_decl; 99 }; 100 101 /* Provides additional context for satisfaction. 102 103 During satisfaction: 104 - The flag noisy() controls whether to diagnose ill-formed satisfaction, 105 such as the satisfaction value of an atom being non-bool or non-constant. 106 - The flag diagnose_unsatisfaction_p() controls whether to additionally 107 explain why a constraint is not satisfied. 108 - We enter satisfaction with noisy+unsat from diagnose_constraints. 109 - We enter satisfaction with noisy-unsat from the replay inside 110 constraint_satisfaction_value. 111 - We enter satisfaction quietly (both flags cleared) from 112 constraints_satisfied_p. 113 114 During evaluation of a requires-expression: 115 - The flag noisy() controls whether to diagnose ill-formed types and 116 expressions inside its requirements. 117 - The flag diagnose_unsatisfaction_p() controls whether to additionally 118 explain why the requires-expression evaluates to false. 119 - We enter tsubst_requires_expr with noisy+unsat from 120 diagnose_atomic_constraint and potentially from 121 satisfy_nondeclaration_constraints. 122 - We enter tsubst_requires_expr with noisy-unsat from 123 cp_parser_requires_expression when processing a requires-expression that 124 appears outside a template. 125 - We enter tsubst_requires_expr quietly (both flags cleared) when 126 substituting through a requires-expression as part of template 127 instantiation. */ 128 129 struct sat_info : subst_info 130 { 131 sat_info (tsubst_flags_t cmp, tree in, bool diag_unsat = false) 132 : subst_info (cmp, in), diagnose_unsatisfaction (diag_unsat) 133 { 134 if (diagnose_unsatisfaction_p ()) 135 gcc_checking_assert (noisy ()); 136 } 137 138 /* True if we should diagnose the cause of satisfaction failure. 139 Implies noisy(). */ 140 bool 141 diagnose_unsatisfaction_p () const 142 { 143 return diagnose_unsatisfaction; 144 } 145 146 bool diagnose_unsatisfaction; 147 }; 148 149 static tree constraint_satisfaction_value (tree, tree, sat_info); 150 151 /* True if T is known to be some type other than bool. Note that this 152 is false for dependent types and errors. */ 153 154 static inline bool 155 known_non_bool_p (tree t) 156 { 157 return (t && !WILDCARD_TYPE_P (t) && TREE_CODE (t) != BOOLEAN_TYPE); 158 } 159 160 static bool 161 check_constraint_atom (cp_expr expr) 162 { 163 if (known_non_bool_p (TREE_TYPE (expr))) 164 { 165 error_at (expr.get_location (), 166 "constraint expression does not have type %<bool%>"); 167 return false; 168 } 169 170 /* Check that we're using function concepts correctly. */ 171 if (concept_check_p (expr)) 172 { 173 tree id = unpack_concept_check (expr); 174 tree tmpl = TREE_OPERAND (id, 0); 175 if (OVL_P (tmpl) && TREE_CODE (expr) == TEMPLATE_ID_EXPR) 176 { 177 error_at (EXPR_LOC_OR_LOC (expr, input_location), 178 "function concept must be called"); 179 return false; 180 } 181 } 182 183 return true; 184 } 185 186 static bool 187 check_constraint_operands (location_t, cp_expr lhs, cp_expr rhs) 188 { 189 return check_constraint_atom (lhs) && check_constraint_atom (rhs); 190 } 191 192 /* Validate the semantic properties of the constraint expression. */ 193 194 static cp_expr 195 finish_constraint_binary_op (location_t loc, 196 tree_code code, 197 cp_expr lhs, 198 cp_expr rhs) 199 { 200 gcc_assert (processing_constraint_expression_p ()); 201 if (lhs == error_mark_node || rhs == error_mark_node) 202 return error_mark_node; 203 if (!check_constraint_operands (loc, lhs, rhs)) 204 return error_mark_node; 205 cp_expr expr 206 = build_min_nt_loc (loc, code, lhs.get_value (), rhs.get_value ()); 207 expr.set_range (lhs.get_start (), rhs.get_finish ()); 208 return expr; 209 } 210 211 cp_expr 212 finish_constraint_or_expr (location_t loc, cp_expr lhs, cp_expr rhs) 213 { 214 return finish_constraint_binary_op (loc, TRUTH_ORIF_EXPR, lhs, rhs); 215 } 216 217 cp_expr 218 finish_constraint_and_expr (location_t loc, cp_expr lhs, cp_expr rhs) 219 { 220 return finish_constraint_binary_op (loc, TRUTH_ANDIF_EXPR, lhs, rhs); 221 } 222 223 cp_expr 224 finish_constraint_primary_expr (cp_expr expr) 225 { 226 if (expr == error_mark_node) 227 return error_mark_node; 228 if (!check_constraint_atom (expr)) 229 return cp_expr (error_mark_node, expr.get_location ()); 230 return expr; 231 } 232 233 /* Combine two constraint-expressions with a logical-and. */ 234 235 tree 236 combine_constraint_expressions (tree lhs, tree rhs) 237 { 238 processing_constraint_expression_sentinel pce; 239 if (!lhs) 240 return rhs; 241 if (!rhs) 242 return lhs; 243 /* Use UNKNOWN_LOCATION so write_template_args can tell the difference 244 between this and a && the user wrote. */ 245 return finish_constraint_and_expr (UNKNOWN_LOCATION, lhs, rhs); 246 } 247 248 /* Extract the template-id from a concept check. For standard and variable 249 checks, this is simply T. For function concept checks, this is the 250 called function. */ 251 252 tree 253 unpack_concept_check (tree t) 254 { 255 gcc_assert (concept_check_p (t)); 256 257 if (TREE_CODE (t) == CALL_EXPR) 258 t = CALL_EXPR_FN (t); 259 260 gcc_assert (TREE_CODE (t) == TEMPLATE_ID_EXPR); 261 return t; 262 } 263 264 /* Extract the TEMPLATE_DECL from a concept check. */ 265 266 tree 267 get_concept_check_template (tree t) 268 { 269 tree id = unpack_concept_check (t); 270 tree tmpl = TREE_OPERAND (id, 0); 271 if (OVL_P (tmpl)) 272 tmpl = OVL_FIRST (tmpl); 273 return tmpl; 274 } 275 276 /*--------------------------------------------------------------------------- 277 Resolution of qualified concept names 278 ---------------------------------------------------------------------------*/ 279 280 /* This facility is used to resolve constraint checks from requirement 281 expressions. A constraint check is a call to a function template declared 282 with the keyword 'concept'. 283 284 The result of resolution is a pair (a TREE_LIST) whose value is the 285 matched declaration, and whose purpose contains the coerced template 286 arguments that can be substituted into the call. */ 287 288 /* Given an overload set OVL, try to find a unique definition that can be 289 instantiated by the template arguments ARGS. 290 291 This function is not called for arbitrary call expressions. In particular, 292 the call expression must be written with explicit template arguments 293 and no function arguments. For example: 294 295 f<T, U>() 296 297 If a single match is found, this returns a TREE_LIST whose VALUE 298 is the constraint function (not the template), and its PURPOSE is 299 the complete set of arguments substituted into the parameter list. */ 300 301 static tree 302 resolve_function_concept_overload (tree ovl, tree args) 303 { 304 int nerrs = 0; 305 tree cands = NULL_TREE; 306 for (lkp_iterator iter (ovl); iter; ++iter) 307 { 308 tree tmpl = *iter; 309 if (TREE_CODE (tmpl) != TEMPLATE_DECL) 310 continue; 311 312 /* Don't try to deduce checks for non-concepts. We often end up trying 313 to resolve constraints in functional casts as part of a 314 postfix-expression. We can save time and headaches by not 315 instantiating those declarations. 316 317 NOTE: This masks a potential error, caused by instantiating 318 non-deduced contexts using placeholder arguments. */ 319 tree fn = DECL_TEMPLATE_RESULT (tmpl); 320 if (DECL_ARGUMENTS (fn)) 321 continue; 322 if (!DECL_DECLARED_CONCEPT_P (fn)) 323 continue; 324 325 /* Remember the candidate if we can deduce a substitution. */ 326 ++processing_template_decl; 327 tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl)); 328 if (tree subst = coerce_template_parms (parms, args, tmpl, tf_none)) 329 { 330 if (subst == error_mark_node) 331 ++nerrs; 332 else 333 cands = tree_cons (subst, fn, cands); 334 } 335 --processing_template_decl; 336 } 337 338 if (!cands) 339 /* We either had no candidates or failed deductions. */ 340 return nerrs ? error_mark_node : NULL_TREE; 341 else if (TREE_CHAIN (cands)) 342 /* There are multiple candidates. */ 343 return error_mark_node; 344 345 return cands; 346 } 347 348 /* Determine if the call expression CALL is a constraint check, and 349 return the concept declaration and arguments being checked. If CALL 350 does not denote a constraint check, return NULL. */ 351 352 tree 353 resolve_function_concept_check (tree call) 354 { 355 gcc_assert (TREE_CODE (call) == CALL_EXPR); 356 357 /* A constraint check must be only a template-id expression. 358 If it's a call to a base-link, its function(s) should be a 359 template-id expression. If this is not a template-id, then 360 it cannot be a concept-check. */ 361 tree target = CALL_EXPR_FN (call); 362 if (BASELINK_P (target)) 363 target = BASELINK_FUNCTIONS (target); 364 if (TREE_CODE (target) != TEMPLATE_ID_EXPR) 365 return NULL_TREE; 366 367 /* Get the overload set and template arguments and try to 368 resolve the target. */ 369 tree ovl = TREE_OPERAND (target, 0); 370 371 /* This is a function call of a variable concept... ill-formed. */ 372 if (TREE_CODE (ovl) == TEMPLATE_DECL) 373 { 374 error_at (location_of (call), 375 "function call of variable concept %qE", call); 376 return error_mark_node; 377 } 378 379 tree args = TREE_OPERAND (target, 1); 380 return resolve_function_concept_overload (ovl, args); 381 } 382 383 /* Returns a pair containing the checked concept and its associated 384 prototype parameter. The result is a TREE_LIST whose TREE_VALUE 385 is the concept (non-template) and whose TREE_PURPOSE contains 386 the converted template arguments, including the deduced prototype 387 parameter (in position 0). */ 388 389 tree 390 resolve_concept_check (tree check) 391 { 392 gcc_assert (concept_check_p (check)); 393 tree id = unpack_concept_check (check); 394 tree tmpl = TREE_OPERAND (id, 0); 395 396 /* If this is an overloaded function concept, perform overload 397 resolution (this only happens when deducing prototype parameters 398 and template introductions). */ 399 if (TREE_CODE (tmpl) == OVERLOAD) 400 { 401 if (OVL_CHAIN (tmpl)) 402 return resolve_function_concept_check (check); 403 tmpl = OVL_FIRST (tmpl); 404 } 405 406 tree args = TREE_OPERAND (id, 1); 407 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl)); 408 ++processing_template_decl; 409 tree result = coerce_template_parms (parms, args, tmpl, tf_none); 410 --processing_template_decl; 411 if (result == error_mark_node) 412 return error_mark_node; 413 return build_tree_list (result, DECL_TEMPLATE_RESULT (tmpl)); 414 } 415 416 /* Given a call expression or template-id expression to a concept EXPR 417 possibly including a wildcard, deduce the concept being checked and 418 the prototype parameter. Returns true if the constraint and prototype 419 can be deduced and false otherwise. Note that the CHECK and PROTO 420 arguments are set to NULL_TREE if this returns false. */ 421 422 bool 423 deduce_constrained_parameter (tree expr, tree& check, tree& proto) 424 { 425 tree info = resolve_concept_check (expr); 426 if (info && info != error_mark_node) 427 { 428 check = TREE_VALUE (info); 429 tree arg = TREE_VEC_ELT (TREE_PURPOSE (info), 0); 430 if (ARGUMENT_PACK_P (arg)) 431 arg = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0); 432 proto = TREE_TYPE (arg); 433 return true; 434 } 435 436 check = proto = NULL_TREE; 437 return false; 438 } 439 440 /* Given a call expression or template-id expression to a concept, EXPR, 441 deduce the concept being checked and return the template arguments. 442 Returns NULL_TREE if deduction fails. */ 443 static tree 444 deduce_concept_introduction (tree check) 445 { 446 tree info = resolve_concept_check (check); 447 if (info && info != error_mark_node) 448 return TREE_PURPOSE (info); 449 return NULL_TREE; 450 } 451 452 /* Build a constrained placeholder type where SPEC is a type-constraint. 453 SPEC can be anything were concept_definition_p is true. 454 455 Returns a pair whose FIRST is the concept being checked and whose 456 SECOND is the prototype parameter. */ 457 458 tree_pair 459 finish_type_constraints (tree spec, tree args, tsubst_flags_t complain) 460 { 461 gcc_assert (concept_definition_p (spec)); 462 463 /* Build an initial concept check. */ 464 tree check = build_type_constraint (spec, args, complain); 465 if (check == error_mark_node) 466 return std::make_pair (error_mark_node, NULL_TREE); 467 468 /* Extract the concept and prototype parameter from the check. */ 469 tree con; 470 tree proto; 471 if (!deduce_constrained_parameter (check, con, proto)) 472 return std::make_pair (error_mark_node, NULL_TREE); 473 474 return std::make_pair (con, proto); 475 } 476 477 /*--------------------------------------------------------------------------- 478 Expansion of concept definitions 479 ---------------------------------------------------------------------------*/ 480 481 /* Returns the expression of a function concept. */ 482 483 static tree 484 get_returned_expression (tree fn) 485 { 486 /* Extract the body of the function minus the return expression. */ 487 tree body = DECL_SAVED_TREE (fn); 488 if (!body) 489 return error_mark_node; 490 if (TREE_CODE (body) == BIND_EXPR) 491 body = BIND_EXPR_BODY (body); 492 if (TREE_CODE (body) != RETURN_EXPR) 493 return error_mark_node; 494 495 return TREE_OPERAND (body, 0); 496 } 497 498 /* Returns the initializer of a variable concept. */ 499 500 static tree 501 get_variable_initializer (tree var) 502 { 503 tree init = DECL_INITIAL (var); 504 if (!init) 505 return error_mark_node; 506 if (BRACE_ENCLOSED_INITIALIZER_P (init) 507 && CONSTRUCTOR_NELTS (init) == 1) 508 init = CONSTRUCTOR_ELT (init, 0)->value; 509 return init; 510 } 511 512 /* Returns the definition of a variable or function concept. */ 513 514 static tree 515 get_concept_definition (tree decl) 516 { 517 if (TREE_CODE (decl) == OVERLOAD) 518 decl = OVL_FIRST (decl); 519 520 if (TREE_CODE (decl) == TEMPLATE_DECL) 521 decl = DECL_TEMPLATE_RESULT (decl); 522 523 if (TREE_CODE (decl) == CONCEPT_DECL) 524 return DECL_INITIAL (decl); 525 if (VAR_P (decl)) 526 return get_variable_initializer (decl); 527 if (TREE_CODE (decl) == FUNCTION_DECL) 528 return get_returned_expression (decl); 529 gcc_unreachable (); 530 } 531 532 /*--------------------------------------------------------------------------- 533 Normalization of expressions 534 535 This set of functions will transform an expression into a constraint 536 in a sequence of steps. 537 ---------------------------------------------------------------------------*/ 538 539 void 540 debug_parameter_mapping (tree map) 541 { 542 for (tree p = map; p; p = TREE_CHAIN (p)) 543 { 544 tree parm = TREE_VALUE (p); 545 tree arg = TREE_PURPOSE (p); 546 if (TYPE_P (parm)) 547 verbatim ("MAP %qD TO %qT", TEMPLATE_TYPE_DECL (parm), arg); 548 else 549 verbatim ("MAP %qD TO %qE", TEMPLATE_PARM_DECL (parm), arg); 550 // debug_tree (parm); 551 // debug_tree (arg); 552 } 553 } 554 555 void 556 debug_argument_list (tree args) 557 { 558 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i) 559 { 560 tree arg = TREE_VEC_ELT (args, i); 561 if (TYPE_P (arg)) 562 verbatim ("argument %qT", arg); 563 else 564 verbatim ("argument %qE", arg); 565 } 566 } 567 568 /* Associate each parameter in PARMS with its corresponding template 569 argument in ARGS. */ 570 571 static tree 572 map_arguments (tree parms, tree args) 573 { 574 for (tree p = parms; p; p = TREE_CHAIN (p)) 575 if (args) 576 { 577 int level; 578 int index; 579 template_parm_level_and_index (TREE_VALUE (p), &level, &index); 580 TREE_PURPOSE (p) = TMPL_ARG (args, level, index); 581 } 582 else 583 TREE_PURPOSE (p) = template_parm_to_arg (p); 584 585 return parms; 586 } 587 588 /* Build the parameter mapping for EXPR using ARGS, where CTX_PARMS 589 are the template parameters in scope for EXPR. */ 590 591 static tree 592 build_parameter_mapping (tree expr, tree args, tree ctx_parms) 593 { 594 tree parms = find_template_parameters (expr, ctx_parms); 595 tree map = map_arguments (parms, args); 596 return map; 597 } 598 599 /* True if the parameter mappings of two atomic constraints formed 600 from the same expression are equivalent. */ 601 602 static bool 603 parameter_mapping_equivalent_p (tree t1, tree t2) 604 { 605 tree map1 = ATOMIC_CONSTR_MAP (t1); 606 tree map2 = ATOMIC_CONSTR_MAP (t2); 607 while (map1 && map2) 608 { 609 gcc_checking_assert (TREE_VALUE (map1) == TREE_VALUE (map2)); 610 tree arg1 = TREE_PURPOSE (map1); 611 tree arg2 = TREE_PURPOSE (map2); 612 if (!template_args_equal (arg1, arg2)) 613 return false; 614 map1 = TREE_CHAIN (map1); 615 map2 = TREE_CHAIN (map2); 616 } 617 gcc_checking_assert (!map1 && !map2); 618 return true; 619 } 620 621 /* Provides additional context for normalization. */ 622 623 struct norm_info : subst_info 624 { 625 explicit norm_info (tsubst_flags_t cmp) 626 : norm_info (NULL_TREE, cmp) 627 {} 628 629 /* Construct a top-level context for DECL. */ 630 631 norm_info (tree in_decl, tsubst_flags_t complain) 632 : subst_info (tf_warning_or_error | complain, in_decl) 633 { 634 if (in_decl) 635 { 636 initial_parms = DECL_TEMPLATE_PARMS (in_decl); 637 if (generate_diagnostics ()) 638 context = build_tree_list (NULL_TREE, in_decl); 639 } 640 else 641 initial_parms = current_template_parms; 642 } 643 644 bool generate_diagnostics() const 645 { 646 return complain & tf_norm; 647 } 648 649 void update_context(tree expr, tree args) 650 { 651 if (generate_diagnostics ()) 652 { 653 tree map = build_parameter_mapping (expr, args, ctx_parms ()); 654 context = tree_cons (map, expr, context); 655 } 656 in_decl = get_concept_check_template (expr); 657 } 658 659 /* Returns the template parameters that are in scope for the current 660 normalization context. */ 661 662 tree ctx_parms() 663 { 664 if (in_decl) 665 return DECL_TEMPLATE_PARMS (in_decl); 666 else 667 return initial_parms; 668 } 669 670 /* Provides information about the source of a constraint. This is a 671 TREE_LIST whose VALUE is either a concept check or a constrained 672 declaration. The PURPOSE, for concept checks is a parameter mapping 673 for that check. */ 674 675 tree context = NULL_TREE; 676 677 /* The declaration whose constraints we're normalizing. The targets 678 of the parameter mapping of each atom will be in terms of the 679 template parameters of ORIG_DECL. */ 680 681 tree initial_parms = NULL_TREE; 682 }; 683 684 static tree normalize_expression (tree, tree, norm_info); 685 686 /* Transform a logical-or or logical-and expression into either 687 a conjunction or disjunction. */ 688 689 static tree 690 normalize_logical_operation (tree t, tree args, tree_code c, norm_info info) 691 { 692 tree t0 = normalize_expression (TREE_OPERAND (t, 0), args, info); 693 tree t1 = normalize_expression (TREE_OPERAND (t, 1), args, info); 694 695 /* Build a new info object for the constraint. */ 696 tree ci = info.generate_diagnostics() 697 ? build_tree_list (t, info.context) 698 : NULL_TREE; 699 700 return build2 (c, ci, t0, t1); 701 } 702 703 /* Data types and hash functions for caching the normal form of a concept-id. 704 This essentially memoizes calls to normalize_concept_check. */ 705 706 struct GTY((for_user)) norm_entry 707 { 708 /* The CONCEPT_DECL of the concept-id. */ 709 tree tmpl; 710 /* The arguments of the concept-id. */ 711 tree args; 712 /* The normal form of the concept-id. */ 713 tree norm; 714 }; 715 716 struct norm_hasher : ggc_ptr_hash<norm_entry> 717 { 718 static hashval_t hash (norm_entry *e) 719 { 720 ++comparing_specializations; 721 hashval_t val = iterative_hash_template_arg (e->tmpl, 0); 722 val = iterative_hash_template_arg (e->args, val); 723 --comparing_specializations; 724 return val; 725 } 726 727 static bool equal (norm_entry *e1, norm_entry *e2) 728 { 729 ++comparing_specializations; 730 bool eq = e1->tmpl == e2->tmpl 731 && template_args_equal (e1->args, e2->args); 732 --comparing_specializations; 733 return eq; 734 } 735 }; 736 737 static GTY((deletable)) hash_table<norm_hasher> *norm_cache; 738 739 /* Normalize the concept check CHECK where ARGS are the 740 arguments to be substituted into CHECK's arguments. */ 741 742 static tree 743 normalize_concept_check (tree check, tree args, norm_info info) 744 { 745 tree id = unpack_concept_check (check); 746 tree tmpl = TREE_OPERAND (id, 0); 747 tree targs = TREE_OPERAND (id, 1); 748 749 /* A function concept is wrapped in an overload. */ 750 if (TREE_CODE (tmpl) == OVERLOAD) 751 { 752 /* TODO: Can we diagnose this error during parsing? */ 753 if (TREE_CODE (check) == TEMPLATE_ID_EXPR) 754 error_at (EXPR_LOC_OR_LOC (check, input_location), 755 "function concept must be called"); 756 tmpl = OVL_FIRST (tmpl); 757 } 758 759 /* Substitute through the arguments of the concept check. */ 760 if (args) 761 targs = tsubst_template_args (targs, args, info.complain, info.in_decl); 762 if (targs == error_mark_node) 763 return error_mark_node; 764 if (template_args_equal (targs, generic_targs_for (tmpl))) 765 /* Canonicalize generic arguments as NULL_TREE, as an optimization. */ 766 targs = NULL_TREE; 767 768 /* Build the substitution for the concept definition. */ 769 tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl)); 770 if (targs && args) 771 /* As an optimization, coerce the arguments only if necessary 772 (i.e. if they were substituted). */ 773 targs = coerce_template_parms (parms, targs, tmpl, tf_none); 774 if (targs == error_mark_node) 775 return error_mark_node; 776 777 if (!norm_cache) 778 norm_cache = hash_table<norm_hasher>::create_ggc (31); 779 norm_entry *entry = nullptr; 780 if (!info.generate_diagnostics ()) 781 { 782 /* Cache the normal form of the substituted concept-id (when not 783 diagnosing). */ 784 norm_entry elt = {tmpl, targs, NULL_TREE}; 785 norm_entry **slot = norm_cache->find_slot (&elt, INSERT); 786 if (*slot) 787 return (*slot)->norm; 788 entry = ggc_alloc<norm_entry> (); 789 *entry = elt; 790 *slot = entry; 791 } 792 793 tree def = get_concept_definition (DECL_TEMPLATE_RESULT (tmpl)); 794 info.update_context (check, args); 795 tree norm = normalize_expression (def, targs, info); 796 if (entry) 797 entry->norm = norm; 798 return norm; 799 } 800 801 /* Used by normalize_atom to cache ATOMIC_CONSTRs. */ 802 803 static GTY((deletable)) hash_table<atom_hasher> *atom_cache; 804 805 /* The normal form of an atom depends on the expression. The normal 806 form of a function call to a function concept is a check constraint 807 for that concept. The normal form of a reference to a variable 808 concept is a check constraint for that concept. Otherwise, the 809 constraint is a predicate constraint. */ 810 811 static tree 812 normalize_atom (tree t, tree args, norm_info info) 813 { 814 /* Concept checks are not atomic. */ 815 if (concept_check_p (t)) 816 return normalize_concept_check (t, args, info); 817 818 /* Build the parameter mapping for the atom. */ 819 tree map = build_parameter_mapping (t, args, info.ctx_parms ()); 820 821 /* Build a new info object for the atom. */ 822 tree ci = build_tree_list (t, info.context); 823 824 tree atom = build1 (ATOMIC_CONSTR, ci, map); 825 826 /* Remember whether the expression of this atomic constraint belongs to 827 a concept definition by inspecting in_decl, which should always be set 828 in this case either by norm_info::update_context (when recursing into a 829 concept-id during normalization) or by normalize_concept_definition 830 (when starting out with a concept-id). */ 831 if (info.in_decl && concept_definition_p (info.in_decl)) 832 ATOMIC_CONSTR_EXPR_FROM_CONCEPT_P (atom) = true; 833 834 if (!info.generate_diagnostics ()) 835 { 836 /* Cache the ATOMIC_CONSTRs that we return, so that sat_hasher::equal 837 later can cheaply compare two atoms using just pointer equality. */ 838 if (!atom_cache) 839 atom_cache = hash_table<atom_hasher>::create_ggc (31); 840 tree *slot = atom_cache->find_slot (atom, INSERT); 841 if (*slot) 842 return *slot; 843 844 /* Find all template parameters used in the targets of the parameter 845 mapping, and store a list of them in the TREE_TYPE of the mapping. 846 This list will be used by sat_hasher to determine the subset of 847 supplied template arguments that the satisfaction value of the atom 848 depends on. */ 849 if (map) 850 { 851 tree targets = make_tree_vec (list_length (map)); 852 int i = 0; 853 for (tree node = map; node; node = TREE_CHAIN (node)) 854 { 855 tree target = TREE_PURPOSE (node); 856 TREE_VEC_ELT (targets, i++) = target; 857 } 858 tree target_parms = find_template_parameters (targets, 859 info.initial_parms); 860 TREE_TYPE (map) = target_parms; 861 } 862 863 *slot = atom; 864 } 865 return atom; 866 } 867 868 /* Returns the normal form of an expression. */ 869 870 static tree 871 normalize_expression (tree t, tree args, norm_info info) 872 { 873 if (!t) 874 return NULL_TREE; 875 876 if (t == error_mark_node) 877 return error_mark_node; 878 879 switch (TREE_CODE (t)) 880 { 881 case TRUTH_ANDIF_EXPR: 882 return normalize_logical_operation (t, args, CONJ_CONSTR, info); 883 case TRUTH_ORIF_EXPR: 884 return normalize_logical_operation (t, args, DISJ_CONSTR, info); 885 default: 886 return normalize_atom (t, args, info); 887 } 888 } 889 890 /* Cache of the normalized form of constraints. Marked as deletable because it 891 can all be recalculated. */ 892 static GTY((deletable)) hash_map<tree,tree> *normalized_map; 893 894 static tree 895 get_normalized_constraints (tree t, norm_info info) 896 { 897 auto_timevar time (TV_CONSTRAINT_NORM); 898 return normalize_expression (t, NULL_TREE, info); 899 } 900 901 /* Returns the normalized constraints from a constraint-info object 902 or NULL_TREE if the constraints are null. IN_DECL provides the 903 declaration to which the constraints belong. */ 904 905 static tree 906 get_normalized_constraints_from_info (tree ci, tree in_decl, bool diag = false) 907 { 908 if (ci == NULL_TREE) 909 return NULL_TREE; 910 911 /* Substitution errors during normalization are fatal. */ 912 ++processing_template_decl; 913 norm_info info (in_decl, diag ? tf_norm : tf_none); 914 tree t = get_normalized_constraints (CI_ASSOCIATED_CONSTRAINTS (ci), info); 915 --processing_template_decl; 916 917 return t; 918 } 919 920 /* Returns the normalized constraints for the declaration D. */ 921 922 static tree 923 get_normalized_constraints_from_decl (tree d, bool diag = false) 924 { 925 tree tmpl; 926 tree decl; 927 928 /* For inherited constructors, consider the original declaration; 929 it has the correct template information attached. */ 930 d = strip_inheriting_ctors (d); 931 932 if (regenerated_lambda_fn_p (d)) 933 { 934 /* If this lambda was regenerated, DECL_TEMPLATE_PARMS doesn't contain 935 all in-scope template parameters, but the lambda from which it was 936 ultimately regenerated does, so use that instead. */ 937 tree lambda = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (d)); 938 lambda = most_general_lambda (lambda); 939 d = lambda_function (lambda); 940 } 941 942 if (TREE_CODE (d) == TEMPLATE_DECL) 943 { 944 tmpl = d; 945 decl = DECL_TEMPLATE_RESULT (tmpl); 946 } 947 else 948 { 949 if (tree ti = DECL_TEMPLATE_INFO (d)) 950 tmpl = TI_TEMPLATE (ti); 951 else 952 tmpl = NULL_TREE; 953 decl = d; 954 } 955 956 /* Get the most general template for the declaration, and compute 957 arguments from that. This ensures that the arguments used for 958 normalization are always template parameters and not arguments 959 used for outer specializations. For example: 960 961 template<typename T> 962 struct S { 963 template<typename U> requires C<T, U> void f(U); 964 }; 965 966 S<int>::f(0); 967 968 When we normalize the requirements for S<int>::f, we want the 969 arguments to be {T, U}, not {int, U}. One reason for this is that 970 accepting the latter causes the template parameter level of U 971 to be reduced in a way that makes it overly difficult substitute 972 concrete arguments (i.e., eventually {int, int} during satisfaction. */ 973 if (tmpl && DECL_LANG_SPECIFIC (tmpl) 974 && (!DECL_TEMPLATE_SPECIALIZATION (tmpl) 975 /* DECL_TEMPLATE_SPECIALIZATION means TMPL is either a partial 976 specialization, or an explicit specialization of a member 977 template. In the former case all is well: TMPL's constraints 978 are in terms of its parameters. But in the latter case TMPL's 979 parameters are partially instantiated whereas its constraints 980 aren't, so we need to instead use (the parameters of) the most 981 general template. The following test distinguishes between a 982 partial specialization and such an explicit specialization. */ 983 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)) 984 < TMPL_ARGS_DEPTH (DECL_TI_ARGS (tmpl))))) 985 tmpl = most_general_template (tmpl); 986 987 d = tmpl ? tmpl : decl; 988 989 /* If we're not diagnosing errors, use cached constraints, if any. */ 990 if (!diag) 991 if (tree *p = hash_map_safe_get (normalized_map, d)) 992 return *p; 993 994 tree norm = NULL_TREE; 995 if (tree ci = get_constraints (d)) 996 { 997 push_access_scope_guard pas (decl); 998 norm = get_normalized_constraints_from_info (ci, tmpl, diag); 999 } 1000 1001 if (!diag) 1002 hash_map_safe_put<hm_ggc> (normalized_map, d, norm); 1003 1004 return norm; 1005 } 1006 1007 /* Returns the normal form of TMPL's definition. */ 1008 1009 static tree 1010 normalize_concept_definition (tree tmpl, bool diag) 1011 { 1012 if (!norm_cache) 1013 norm_cache = hash_table<norm_hasher>::create_ggc (31); 1014 norm_entry entry = {tmpl, NULL_TREE, NULL_TREE}; 1015 1016 if (!diag) 1017 if (norm_entry *found = norm_cache->find (&entry)) 1018 return found->norm; 1019 1020 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); 1021 tree def = get_concept_definition (DECL_TEMPLATE_RESULT (tmpl)); 1022 ++processing_template_decl; 1023 norm_info info (tmpl, diag ? tf_norm : tf_none); 1024 tree norm = get_normalized_constraints (def, info); 1025 --processing_template_decl; 1026 1027 if (!diag) 1028 { 1029 norm_entry **slot = norm_cache->find_slot (&entry, INSERT); 1030 entry.norm = norm; 1031 *slot = ggc_alloc<norm_entry> (); 1032 **slot = entry; 1033 } 1034 1035 return norm; 1036 } 1037 1038 /* Normalize an EXPR as a constraint. */ 1039 1040 static tree 1041 normalize_constraint_expression (tree expr, norm_info info) 1042 { 1043 if (!expr || expr == error_mark_node) 1044 return expr; 1045 1046 if (!info.generate_diagnostics ()) 1047 if (tree *p = hash_map_safe_get (normalized_map, expr)) 1048 return *p; 1049 1050 ++processing_template_decl; 1051 tree norm = get_normalized_constraints (expr, info); 1052 --processing_template_decl; 1053 1054 if (!info.generate_diagnostics ()) 1055 hash_map_safe_put<hm_ggc> (normalized_map, expr, norm); 1056 1057 return norm; 1058 } 1059 1060 /* 17.4.1.2p2. Two constraints are identical if they are formed 1061 from the same expression and the targets of the parameter mapping 1062 are equivalent. */ 1063 1064 bool 1065 atomic_constraints_identical_p (tree t1, tree t2) 1066 { 1067 gcc_assert (TREE_CODE (t1) == ATOMIC_CONSTR); 1068 gcc_assert (TREE_CODE (t2) == ATOMIC_CONSTR); 1069 1070 if (ATOMIC_CONSTR_EXPR (t1) != ATOMIC_CONSTR_EXPR (t2)) 1071 return false; 1072 1073 if (!parameter_mapping_equivalent_p (t1, t2)) 1074 return false; 1075 1076 return true; 1077 } 1078 1079 /* True if T1 and T2 are equivalent, meaning they have the same syntactic 1080 structure and all corresponding constraints are identical. */ 1081 1082 bool 1083 constraints_equivalent_p (tree t1, tree t2) 1084 { 1085 gcc_assert (CONSTR_P (t1)); 1086 gcc_assert (CONSTR_P (t2)); 1087 1088 if (TREE_CODE (t1) != TREE_CODE (t2)) 1089 return false; 1090 1091 switch (TREE_CODE (t1)) 1092 { 1093 case CONJ_CONSTR: 1094 case DISJ_CONSTR: 1095 if (!constraints_equivalent_p (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))) 1096 return false; 1097 if (!constraints_equivalent_p (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1))) 1098 return false; 1099 break; 1100 case ATOMIC_CONSTR: 1101 if (!atomic_constraints_identical_p(t1, t2)) 1102 return false; 1103 break; 1104 default: 1105 gcc_unreachable (); 1106 } 1107 return true; 1108 } 1109 1110 /* Compute the hash value for T. */ 1111 1112 hashval_t 1113 hash_atomic_constraint (tree t) 1114 { 1115 gcc_assert (TREE_CODE (t) == ATOMIC_CONSTR); 1116 1117 /* Hash the identity of the expression. */ 1118 hashval_t val = htab_hash_pointer (ATOMIC_CONSTR_EXPR (t)); 1119 1120 /* Hash the targets of the parameter map. */ 1121 tree p = ATOMIC_CONSTR_MAP (t); 1122 while (p) 1123 { 1124 val = iterative_hash_template_arg (TREE_PURPOSE (p), val); 1125 p = TREE_CHAIN (p); 1126 } 1127 1128 return val; 1129 } 1130 1131 namespace inchash 1132 { 1133 1134 static void 1135 add_constraint (tree t, hash& h) 1136 { 1137 h.add_int(TREE_CODE (t)); 1138 switch (TREE_CODE (t)) 1139 { 1140 case CONJ_CONSTR: 1141 case DISJ_CONSTR: 1142 add_constraint (TREE_OPERAND (t, 0), h); 1143 add_constraint (TREE_OPERAND (t, 1), h); 1144 break; 1145 case ATOMIC_CONSTR: 1146 h.merge_hash (hash_atomic_constraint (t)); 1147 break; 1148 default: 1149 gcc_unreachable (); 1150 } 1151 } 1152 1153 } 1154 1155 /* Computes a hash code for the constraint T. */ 1156 1157 hashval_t 1158 iterative_hash_constraint (tree t, hashval_t val) 1159 { 1160 gcc_assert (CONSTR_P (t)); 1161 inchash::hash h (val); 1162 inchash::add_constraint (t, h); 1163 return h.end (); 1164 } 1165 1166 // -------------------------------------------------------------------------- // 1167 // Constraint Semantic Processing 1168 // 1169 // The following functions are called by the parser and substitution rules 1170 // to create and evaluate constraint-related nodes. 1171 1172 // The constraints associated with the current template parameters. 1173 tree 1174 current_template_constraints (void) 1175 { 1176 if (!current_template_parms) 1177 return NULL_TREE; 1178 tree tmpl_constr = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms); 1179 return build_constraints (tmpl_constr, NULL_TREE); 1180 } 1181 1182 /* If the recently parsed TYPE declares or defines a template or 1183 template specialization, get its corresponding constraints from the 1184 current template parameters and bind them to TYPE's declaration. */ 1185 1186 tree 1187 associate_classtype_constraints (tree type) 1188 { 1189 if (!type || type == error_mark_node || !CLASS_TYPE_P (type)) 1190 return type; 1191 1192 /* An explicit class template specialization has no template parameters. */ 1193 if (!current_template_parms) 1194 return type; 1195 1196 if (CLASSTYPE_IS_TEMPLATE (type) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) 1197 { 1198 tree decl = TYPE_STUB_DECL (type); 1199 tree ci = current_template_constraints (); 1200 1201 /* An implicitly instantiated member template declaration already 1202 has associated constraints. If it is defined outside of its 1203 class, then we need match these constraints against those of 1204 original declaration. */ 1205 if (tree orig_ci = get_constraints (decl)) 1206 { 1207 if (int extra_levels = (TMPL_PARMS_DEPTH (current_template_parms) 1208 - TMPL_ARGS_DEPTH (TYPE_TI_ARGS (type)))) 1209 { 1210 /* If there is a discrepancy between the current template depth 1211 and the template depth of the original declaration, then we 1212 must be redeclaring a class template as part of a friend 1213 declaration within another class template. Before matching 1214 constraints, we need to reduce the template parameter level 1215 within the current constraints via substitution. */ 1216 tree outer_gtargs = template_parms_to_args (current_template_parms); 1217 TREE_VEC_LENGTH (outer_gtargs) = extra_levels; 1218 ci = tsubst_constraint_info (ci, outer_gtargs, tf_none, NULL_TREE); 1219 } 1220 if (!equivalent_constraints (ci, orig_ci)) 1221 { 1222 error ("%qT does not match original declaration", type); 1223 tree tmpl = CLASSTYPE_TI_TEMPLATE (type); 1224 location_t loc = DECL_SOURCE_LOCATION (tmpl); 1225 inform (loc, "original template declaration here"); 1226 /* Fall through, so that we define the type anyway. */ 1227 } 1228 return type; 1229 } 1230 set_constraints (decl, ci); 1231 } 1232 return type; 1233 } 1234 1235 /* Create an empty constraint info block. */ 1236 1237 static inline tree_constraint_info* 1238 build_constraint_info () 1239 { 1240 return (tree_constraint_info *)make_node (CONSTRAINT_INFO); 1241 } 1242 1243 /* Build a constraint-info object that contains the associated constraints 1244 of a declaration. This also includes the declaration's template 1245 requirements (TREQS) and any trailing requirements for a function 1246 declarator (DREQS). Note that both TREQS and DREQS must be constraints. 1247 1248 If the declaration has neither template nor declaration requirements 1249 this returns NULL_TREE, indicating an unconstrained declaration. */ 1250 1251 tree 1252 build_constraints (tree tr, tree dr) 1253 { 1254 if (!tr && !dr) 1255 return NULL_TREE; 1256 1257 tree_constraint_info* ci = build_constraint_info (); 1258 ci->template_reqs = tr; 1259 ci->declarator_reqs = dr; 1260 ci->associated_constr = combine_constraint_expressions (tr, dr); 1261 1262 return (tree)ci; 1263 } 1264 1265 /* Add constraint RHS to the end of CONSTRAINT_INFO ci. */ 1266 1267 tree 1268 append_constraint (tree ci, tree rhs) 1269 { 1270 tree tr = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE; 1271 tree dr = ci ? CI_DECLARATOR_REQS (ci) : NULL_TREE; 1272 dr = combine_constraint_expressions (dr, rhs); 1273 if (ci) 1274 { 1275 CI_DECLARATOR_REQS (ci) = dr; 1276 tree ac = combine_constraint_expressions (tr, dr); 1277 CI_ASSOCIATED_CONSTRAINTS (ci) = ac; 1278 } 1279 else 1280 ci = build_constraints (tr, dr); 1281 return ci; 1282 } 1283 1284 /* A mapping from declarations to constraint information. */ 1285 1286 static GTY ((cache)) decl_tree_cache_map *decl_constraints; 1287 1288 /* Returns the template constraints of declaration T. If T is not 1289 constrained, return NULL_TREE. Note that T must be non-null. */ 1290 1291 tree 1292 get_constraints (const_tree t) 1293 { 1294 if (!flag_concepts) 1295 return NULL_TREE; 1296 if (!decl_constraints) 1297 return NULL_TREE; 1298 1299 gcc_assert (DECL_P (t)); 1300 if (TREE_CODE (t) == TEMPLATE_DECL) 1301 t = DECL_TEMPLATE_RESULT (t); 1302 tree* found = decl_constraints->get (CONST_CAST_TREE (t)); 1303 if (found) 1304 return *found; 1305 else 1306 return NULL_TREE; 1307 } 1308 1309 /* Associate the given constraint information CI with the declaration 1310 T. If T is a template, then the constraints are associated with 1311 its underlying declaration. Don't build associations if CI is 1312 NULL_TREE. */ 1313 1314 void 1315 set_constraints (tree t, tree ci) 1316 { 1317 if (!ci) 1318 return; 1319 gcc_assert (t && flag_concepts); 1320 if (TREE_CODE (t) == TEMPLATE_DECL) 1321 t = DECL_TEMPLATE_RESULT (t); 1322 bool found = hash_map_safe_put<hm_ggc> (decl_constraints, t, ci); 1323 gcc_assert (!found); 1324 } 1325 1326 /* Remove the associated constraints of the declaration T. */ 1327 1328 void 1329 remove_constraints (tree t) 1330 { 1331 gcc_checking_assert (DECL_P (t)); 1332 if (TREE_CODE (t) == TEMPLATE_DECL) 1333 t = DECL_TEMPLATE_RESULT (t); 1334 1335 if (decl_constraints) 1336 decl_constraints->remove (t); 1337 } 1338 1339 /* If DECL is a friend, substitute into REQS to produce requirements suitable 1340 for declaration matching. */ 1341 1342 tree 1343 maybe_substitute_reqs_for (tree reqs, const_tree decl) 1344 { 1345 if (reqs == NULL_TREE) 1346 return NULL_TREE; 1347 1348 decl = STRIP_TEMPLATE (decl); 1349 if (DECL_UNIQUE_FRIEND_P (decl) && DECL_TEMPLATE_INFO (decl)) 1350 { 1351 tree tmpl = DECL_TI_TEMPLATE (decl); 1352 tree outer_args = outer_template_args (decl); 1353 processing_template_decl_sentinel s; 1354 if (PRIMARY_TEMPLATE_P (tmpl) 1355 || uses_template_parms (outer_args)) 1356 ++processing_template_decl; 1357 reqs = tsubst_constraint (reqs, outer_args, 1358 tf_warning_or_error, NULL_TREE); 1359 } 1360 return reqs; 1361 } 1362 1363 /* Returns the trailing requires clause of the declarator of 1364 a template declaration T or NULL_TREE if none. */ 1365 1366 tree 1367 get_trailing_function_requirements (tree t) 1368 { 1369 tree ci = get_constraints (t); 1370 if (!ci) 1371 return NULL_TREE; 1372 return CI_DECLARATOR_REQS (ci); 1373 } 1374 1375 /* Construct a sequence of template arguments by prepending 1376 ARG to REST. Either ARG or REST may be null. */ 1377 static tree 1378 build_concept_check_arguments (tree arg, tree rest) 1379 { 1380 gcc_assert (rest ? TREE_CODE (rest) == TREE_VEC : true); 1381 tree args; 1382 if (arg) 1383 { 1384 int n = rest ? TREE_VEC_LENGTH (rest) : 0; 1385 args = make_tree_vec (n + 1); 1386 TREE_VEC_ELT (args, 0) = arg; 1387 if (rest) 1388 for (int i = 0; i < n; ++i) 1389 TREE_VEC_ELT (args, i + 1) = TREE_VEC_ELT (rest, i); 1390 int def = rest ? GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (rest) : 0; 1391 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, def + 1); 1392 } 1393 else 1394 { 1395 args = rest; 1396 } 1397 return args; 1398 } 1399 1400 /* Builds an id-expression of the form `C<Args...>()` where C is a function 1401 concept. */ 1402 1403 static tree 1404 build_function_check (tree tmpl, tree args, tsubst_flags_t /*complain*/) 1405 { 1406 if (TREE_CODE (tmpl) == TEMPLATE_DECL) 1407 { 1408 /* If we just got a template, wrap it in an overload so it looks like any 1409 other template-id. */ 1410 tmpl = ovl_make (tmpl); 1411 TREE_TYPE (tmpl) = boolean_type_node; 1412 } 1413 1414 /* Perform function concept resolution now so we always have a single 1415 function of the overload set (even if we started with only one; the 1416 resolution function converts template arguments). Note that we still 1417 wrap this in an overload set so we don't upset other parts of the 1418 compiler that expect template-ids referring to function concepts 1419 to have an overload set. */ 1420 tree info = resolve_function_concept_overload (tmpl, args); 1421 if (info == error_mark_node) 1422 return error_mark_node; 1423 if (!info) 1424 { 1425 error ("no matching concepts for %qE", tmpl); 1426 return error_mark_node; 1427 } 1428 args = TREE_PURPOSE (info); 1429 tmpl = DECL_TI_TEMPLATE (TREE_VALUE (info)); 1430 1431 /* Rebuild the singleton overload set; mark the type bool. */ 1432 tmpl = ovl_make (tmpl, NULL_TREE); 1433 TREE_TYPE (tmpl) = boolean_type_node; 1434 1435 /* Build the id-expression around the overload set. */ 1436 tree id = build2 (TEMPLATE_ID_EXPR, boolean_type_node, tmpl, args); 1437 1438 /* Finally, build the call expression around the overload. */ 1439 ++processing_template_decl; 1440 vec<tree, va_gc> *fargs = make_tree_vector (); 1441 tree call = build_min_nt_call_vec (id, fargs); 1442 TREE_TYPE (call) = boolean_type_node; 1443 release_tree_vector (fargs); 1444 --processing_template_decl; 1445 1446 return call; 1447 } 1448 1449 /* Builds an id-expression of the form `C<Args...>` where C is a variable 1450 concept. */ 1451 1452 static tree 1453 build_variable_check (tree tmpl, tree args, tsubst_flags_t complain) 1454 { 1455 gcc_assert (variable_concept_p (tmpl)); 1456 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); 1457 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl)); 1458 args = coerce_template_parms (parms, args, tmpl, complain); 1459 if (args == error_mark_node) 1460 return error_mark_node; 1461 return build2 (TEMPLATE_ID_EXPR, boolean_type_node, tmpl, args); 1462 } 1463 1464 /* Builds an id-expression of the form `C<Args...>` where C is a standard 1465 concept. */ 1466 1467 static tree 1468 build_standard_check (tree tmpl, tree args, tsubst_flags_t complain) 1469 { 1470 gcc_assert (standard_concept_p (tmpl)); 1471 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); 1472 if (TREE_DEPRECATED (DECL_TEMPLATE_RESULT (tmpl))) 1473 warn_deprecated_use (DECL_TEMPLATE_RESULT (tmpl), NULL_TREE); 1474 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl)); 1475 args = coerce_template_parms (parms, args, tmpl, complain); 1476 if (args == error_mark_node) 1477 return error_mark_node; 1478 return build2 (TEMPLATE_ID_EXPR, boolean_type_node, tmpl, args); 1479 } 1480 1481 /* Construct an expression that checks TARGET using ARGS. */ 1482 1483 tree 1484 build_concept_check (tree target, tree args, tsubst_flags_t complain) 1485 { 1486 return build_concept_check (target, NULL_TREE, args, complain); 1487 } 1488 1489 /* Construct an expression that checks the concept given by DECL. If 1490 concept_definition_p (DECL) is false, this returns null. */ 1491 1492 tree 1493 build_concept_check (tree decl, tree arg, tree rest, tsubst_flags_t complain) 1494 { 1495 tree args = build_concept_check_arguments (arg, rest); 1496 1497 if (standard_concept_p (decl)) 1498 return build_standard_check (decl, args, complain); 1499 if (variable_concept_p (decl)) 1500 return build_variable_check (decl, args, complain); 1501 if (function_concept_p (decl)) 1502 return build_function_check (decl, args, complain); 1503 1504 return error_mark_node; 1505 } 1506 1507 /* Build a template-id that can participate in a concept check. */ 1508 1509 static tree 1510 build_concept_id (tree decl, tree args) 1511 { 1512 tree check = build_concept_check (decl, args, tf_warning_or_error); 1513 if (check == error_mark_node) 1514 return error_mark_node; 1515 return unpack_concept_check (check); 1516 } 1517 1518 /* Build a template-id that can participate in a concept check, preserving 1519 the source location of the original template-id. */ 1520 1521 tree 1522 build_concept_id (tree expr) 1523 { 1524 gcc_assert (TREE_CODE (expr) == TEMPLATE_ID_EXPR); 1525 tree id = build_concept_id (TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1)); 1526 protected_set_expr_location (id, cp_expr_location (expr)); 1527 return id; 1528 } 1529 1530 /* Build as template-id with a placeholder that can be used as a 1531 type constraint. 1532 1533 Note that this will diagnose errors if the initial concept check 1534 cannot be built. */ 1535 1536 tree 1537 build_type_constraint (tree decl, tree args, tsubst_flags_t complain) 1538 { 1539 tree wildcard = build_nt (WILDCARD_DECL); 1540 ++processing_template_decl; 1541 tree check = build_concept_check (decl, wildcard, args, complain); 1542 --processing_template_decl; 1543 if (check == error_mark_node) 1544 return error_mark_node; 1545 return unpack_concept_check (check); 1546 } 1547 1548 /* Returns a TYPE_DECL that contains sufficient information to 1549 build a template parameter of the same kind as PROTO and 1550 constrained by the concept declaration CNC. Note that PROTO 1551 is the first template parameter of CNC. 1552 1553 If specified, ARGS provides additional arguments to the 1554 constraint check. */ 1555 tree 1556 build_constrained_parameter (tree cnc, tree proto, tree args) 1557 { 1558 tree name = DECL_NAME (cnc); 1559 tree type = TREE_TYPE (proto); 1560 tree decl = build_decl (input_location, TYPE_DECL, name, type); 1561 CONSTRAINED_PARM_PROTOTYPE (decl) = proto; 1562 CONSTRAINED_PARM_CONCEPT (decl) = cnc; 1563 CONSTRAINED_PARM_EXTRA_ARGS (decl) = args; 1564 return decl; 1565 } 1566 1567 /* Create a constraint expression for the given DECL that evaluates the 1568 requirements specified by CONSTR, a TYPE_DECL that contains all the 1569 information necessary to build the requirements (see finish_concept_name 1570 for the layout of that TYPE_DECL). 1571 1572 Note that the constraints are neither reduced nor decomposed. That is 1573 done only after the requires clause has been parsed (or not). */ 1574 1575 tree 1576 finish_shorthand_constraint (tree decl, tree constr) 1577 { 1578 /* No requirements means no constraints. */ 1579 if (!constr) 1580 return NULL_TREE; 1581 1582 if (error_operand_p (constr)) 1583 return NULL_TREE; 1584 1585 tree proto = CONSTRAINED_PARM_PROTOTYPE (constr); 1586 tree con = CONSTRAINED_PARM_CONCEPT (constr); 1587 tree args = CONSTRAINED_PARM_EXTRA_ARGS (constr); 1588 1589 /* The TS lets use shorthand to constrain a pack of arguments, but the 1590 standard does not. 1591 1592 For the TS, consider: 1593 1594 template<C... Ts> struct s; 1595 1596 If C is variadic (and because Ts is a pack), we associate the 1597 constraint C<Ts...>. In all other cases, we associate 1598 the constraint (C<Ts> && ...). 1599 1600 The standard behavior cannot be overridden by -fconcepts-ts. */ 1601 bool variadic_concept_p = template_parameter_pack_p (proto); 1602 bool declared_pack_p = template_parameter_pack_p (decl); 1603 bool apply_to_each_p = (cxx_dialect >= cxx20) ? true : !variadic_concept_p; 1604 1605 /* Get the argument and overload used for the requirement 1606 and adjust it if we're going to expand later. */ 1607 tree arg = template_parm_to_arg (decl); 1608 if (apply_to_each_p && declared_pack_p) 1609 arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0)); 1610 1611 /* Build the concept constraint-expression. */ 1612 tree tmpl = DECL_TI_TEMPLATE (con); 1613 tree check = tmpl; 1614 if (TREE_CODE (con) == FUNCTION_DECL) 1615 check = ovl_make (tmpl); 1616 check = build_concept_check (check, arg, args, tf_warning_or_error); 1617 1618 /* Make the check a fold-expression if needed. 1619 Use UNKNOWN_LOCATION so write_template_args can tell the 1620 difference between this and a fold the user wrote. */ 1621 if (apply_to_each_p && declared_pack_p) 1622 check = finish_left_unary_fold_expr (UNKNOWN_LOCATION, 1623 check, TRUTH_ANDIF_EXPR); 1624 1625 return check; 1626 } 1627 1628 /* Returns a conjunction of shorthand requirements for the template 1629 parameter list PARMS. Note that the requirements are stored in 1630 the TYPE of each tree node. */ 1631 1632 tree 1633 get_shorthand_constraints (tree parms) 1634 { 1635 tree result = NULL_TREE; 1636 parms = INNERMOST_TEMPLATE_PARMS (parms); 1637 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i) 1638 { 1639 tree parm = TREE_VEC_ELT (parms, i); 1640 tree constr = TEMPLATE_PARM_CONSTRAINTS (parm); 1641 result = combine_constraint_expressions (result, constr); 1642 } 1643 return result; 1644 } 1645 1646 /* Get the deduced wildcard from a DEDUCED placeholder. If the deduced 1647 wildcard is a pack, return the first argument of that pack. */ 1648 1649 static tree 1650 get_deduced_wildcard (tree wildcard) 1651 { 1652 if (ARGUMENT_PACK_P (wildcard)) 1653 wildcard = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (wildcard), 0); 1654 gcc_assert (TREE_CODE (wildcard) == WILDCARD_DECL); 1655 return wildcard; 1656 } 1657 1658 /* Returns the prototype parameter for the nth deduced wildcard. */ 1659 1660 static tree 1661 get_introduction_prototype (tree wildcards, int index) 1662 { 1663 return TREE_TYPE (get_deduced_wildcard (TREE_VEC_ELT (wildcards, index))); 1664 } 1665 1666 /* Introduce a type template parameter. */ 1667 1668 static tree 1669 introduce_type_template_parameter (tree wildcard, bool& non_type_p) 1670 { 1671 non_type_p = false; 1672 return finish_template_type_parm (class_type_node, DECL_NAME (wildcard)); 1673 } 1674 1675 /* Introduce a template template parameter. */ 1676 1677 static tree 1678 introduce_template_template_parameter (tree wildcard, bool& non_type_p) 1679 { 1680 non_type_p = false; 1681 begin_template_parm_list (); 1682 current_template_parms = DECL_TEMPLATE_PARMS (TREE_TYPE (wildcard)); 1683 end_template_parm_list (); 1684 return finish_template_template_parm (class_type_node, DECL_NAME (wildcard)); 1685 } 1686 1687 /* Introduce a template non-type parameter. */ 1688 1689 static tree 1690 introduce_nontype_template_parameter (tree wildcard, bool& non_type_p) 1691 { 1692 non_type_p = true; 1693 tree parm = copy_decl (TREE_TYPE (wildcard)); 1694 DECL_NAME (parm) = DECL_NAME (wildcard); 1695 return parm; 1696 } 1697 1698 /* Introduce a single template parameter. */ 1699 1700 static tree 1701 build_introduced_template_parameter (tree wildcard, bool& non_type_p) 1702 { 1703 tree proto = TREE_TYPE (wildcard); 1704 1705 tree parm; 1706 if (TREE_CODE (proto) == TYPE_DECL) 1707 parm = introduce_type_template_parameter (wildcard, non_type_p); 1708 else if (TREE_CODE (proto) == TEMPLATE_DECL) 1709 parm = introduce_template_template_parameter (wildcard, non_type_p); 1710 else 1711 parm = introduce_nontype_template_parameter (wildcard, non_type_p); 1712 1713 /* Wrap in a TREE_LIST for process_template_parm. Note that introduced 1714 parameters do not retain the defaults from the source parameter. */ 1715 return build_tree_list (NULL_TREE, parm); 1716 } 1717 1718 /* Introduce a single template parameter. */ 1719 1720 static tree 1721 introduce_template_parameter (tree parms, tree wildcard) 1722 { 1723 gcc_assert (!ARGUMENT_PACK_P (wildcard)); 1724 tree proto = TREE_TYPE (wildcard); 1725 location_t loc = DECL_SOURCE_LOCATION (wildcard); 1726 1727 /* Diagnose the case where we have C{...Args}. */ 1728 if (WILDCARD_PACK_P (wildcard)) 1729 { 1730 tree id = DECL_NAME (wildcard); 1731 error_at (loc, "%qE cannot be introduced with an ellipsis %<...%>", id); 1732 inform (DECL_SOURCE_LOCATION (proto), "prototype declared here"); 1733 } 1734 1735 bool non_type_p; 1736 tree parm = build_introduced_template_parameter (wildcard, non_type_p); 1737 return process_template_parm (parms, loc, parm, non_type_p, false); 1738 } 1739 1740 /* Introduce a template parameter pack. */ 1741 1742 static tree 1743 introduce_template_parameter_pack (tree parms, tree wildcard) 1744 { 1745 bool non_type_p; 1746 tree parm = build_introduced_template_parameter (wildcard, non_type_p); 1747 location_t loc = DECL_SOURCE_LOCATION (wildcard); 1748 return process_template_parm (parms, loc, parm, non_type_p, true); 1749 } 1750 1751 /* Introduce the nth template parameter. */ 1752 1753 static tree 1754 introduce_template_parameter (tree parms, tree wildcards, int& index) 1755 { 1756 tree deduced = TREE_VEC_ELT (wildcards, index++); 1757 return introduce_template_parameter (parms, deduced); 1758 } 1759 1760 /* Introduce either a template parameter pack or a list of template 1761 parameters. */ 1762 1763 static tree 1764 introduce_template_parameters (tree parms, tree wildcards, int& index) 1765 { 1766 /* If the prototype was a parameter, we better have deduced an 1767 argument pack, and that argument must be the last deduced value 1768 in the wildcard vector. */ 1769 tree deduced = TREE_VEC_ELT (wildcards, index++); 1770 gcc_assert (ARGUMENT_PACK_P (deduced)); 1771 gcc_assert (index == TREE_VEC_LENGTH (wildcards)); 1772 1773 /* Introduce each element in the pack. */ 1774 tree args = ARGUMENT_PACK_ARGS (deduced); 1775 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i) 1776 { 1777 tree arg = TREE_VEC_ELT (args, i); 1778 if (WILDCARD_PACK_P (arg)) 1779 parms = introduce_template_parameter_pack (parms, arg); 1780 else 1781 parms = introduce_template_parameter (parms, arg); 1782 } 1783 1784 return parms; 1785 } 1786 1787 /* Builds the template parameter list PARMS by chaining introduced 1788 parameters from the WILDCARD vector. INDEX is the position of 1789 the current parameter. */ 1790 1791 static tree 1792 process_introduction_parms (tree parms, tree wildcards, int& index) 1793 { 1794 tree proto = get_introduction_prototype (wildcards, index); 1795 if (template_parameter_pack_p (proto)) 1796 return introduce_template_parameters (parms, wildcards, index); 1797 else 1798 return introduce_template_parameter (parms, wildcards, index); 1799 } 1800 1801 /* Ensure that all template parameters have been introduced for the concept 1802 named in CHECK. If not, emit a diagnostic. 1803 1804 Note that implicitly introducing a parameter with a default argument 1805 creates a case where a parameter is declared, but unnamed, making 1806 it unusable in the definition. */ 1807 1808 static bool 1809 check_introduction_list (tree intros, tree check) 1810 { 1811 check = unpack_concept_check (check); 1812 tree tmpl = TREE_OPERAND (check, 0); 1813 if (OVL_P (tmpl)) 1814 tmpl = OVL_FIRST (tmpl); 1815 1816 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); 1817 if (TREE_VEC_LENGTH (intros) < TREE_VEC_LENGTH (parms)) 1818 { 1819 error_at (input_location, "all template parameters of %qD must " 1820 "be introduced", tmpl); 1821 return false; 1822 } 1823 1824 return true; 1825 } 1826 1827 /* Associates a constraint check to the current template based on the 1828 introduction parameters. INTRO_LIST must be a TREE_VEC of WILDCARD_DECLs 1829 containing a chained PARM_DECL which contains the identifier as well as 1830 the source location. TMPL_DECL is the decl for the concept being used. 1831 If we take a concept, C, this will form a check in the form of 1832 C<INTRO_LIST> filling in any extra arguments needed by the defaults 1833 deduced. 1834 1835 Returns NULL_TREE if no concept could be matched and error_mark_node if 1836 an error occurred when matching. */ 1837 1838 tree 1839 finish_template_introduction (tree tmpl_decl, 1840 tree intro_list, 1841 location_t intro_loc) 1842 { 1843 /* Build a concept check to deduce the actual parameters. */ 1844 tree expr = build_concept_check (tmpl_decl, intro_list, tf_none); 1845 if (expr == error_mark_node) 1846 { 1847 error_at (intro_loc, "cannot deduce template parameters from " 1848 "introduction list"); 1849 return error_mark_node; 1850 } 1851 1852 if (!check_introduction_list (intro_list, expr)) 1853 return error_mark_node; 1854 1855 tree parms = deduce_concept_introduction (expr); 1856 if (!parms) 1857 return NULL_TREE; 1858 1859 /* Build template parameter scope for introduction. */ 1860 tree parm_list = NULL_TREE; 1861 begin_template_parm_list (); 1862 int nargs = MIN (TREE_VEC_LENGTH (parms), TREE_VEC_LENGTH (intro_list)); 1863 for (int n = 0; n < nargs; ) 1864 parm_list = process_introduction_parms (parm_list, parms, n); 1865 parm_list = end_template_parm_list (parm_list); 1866 1867 /* Update the number of arguments to reflect the number of deduced 1868 template parameter introductions. */ 1869 nargs = TREE_VEC_LENGTH (parm_list); 1870 1871 /* Determine if any errors occurred during matching. */ 1872 for (int i = 0; i < TREE_VEC_LENGTH (parm_list); ++i) 1873 if (TREE_VALUE (TREE_VEC_ELT (parm_list, i)) == error_mark_node) 1874 { 1875 end_template_decl (); 1876 return error_mark_node; 1877 } 1878 1879 /* Build a concept check for our constraint. */ 1880 tree check_args = make_tree_vec (nargs); 1881 int n = 0; 1882 for (; n < TREE_VEC_LENGTH (parm_list); ++n) 1883 { 1884 tree parm = TREE_VEC_ELT (parm_list, n); 1885 TREE_VEC_ELT (check_args, n) = template_parm_to_arg (parm); 1886 } 1887 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (check_args, n); 1888 1889 /* If the template expects more parameters we should be able 1890 to use the defaults from our deduced concept. */ 1891 for (; n < TREE_VEC_LENGTH (parms); ++n) 1892 TREE_VEC_ELT (check_args, n) = TREE_VEC_ELT (parms, n); 1893 1894 /* Associate the constraint. */ 1895 tree check = build_concept_check (tmpl_decl, 1896 check_args, 1897 tf_warning_or_error); 1898 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = check; 1899 1900 return parm_list; 1901 } 1902 1903 1904 /* Given the concept check T from a constrained-type-specifier, extract 1905 its TMPL and ARGS. FIXME why do we need two different forms of 1906 constrained-type-specifier? */ 1907 1908 void 1909 placeholder_extract_concept_and_args (tree t, tree &tmpl, tree &args) 1910 { 1911 if (concept_check_p (t)) 1912 { 1913 t = unpack_concept_check (t); 1914 tmpl = TREE_OPERAND (t, 0); 1915 if (TREE_CODE (tmpl) == OVERLOAD) 1916 tmpl = OVL_FIRST (tmpl); 1917 args = TREE_OPERAND (t, 1); 1918 return; 1919 } 1920 1921 if (TREE_CODE (t) == TYPE_DECL) 1922 { 1923 /* A constrained parameter. Build a constraint check 1924 based on the prototype parameter and then extract the 1925 arguments from that. */ 1926 tree proto = CONSTRAINED_PARM_PROTOTYPE (t); 1927 tree check = finish_shorthand_constraint (proto, t); 1928 placeholder_extract_concept_and_args (check, tmpl, args); 1929 return; 1930 } 1931 } 1932 1933 /* Returns true iff the placeholders C1 and C2 are equivalent. C1 1934 and C2 can be either TEMPLATE_TYPE_PARM or template-ids. */ 1935 1936 bool 1937 equivalent_placeholder_constraints (tree c1, tree c2) 1938 { 1939 if (c1 && TREE_CODE (c1) == TEMPLATE_TYPE_PARM) 1940 /* A constrained auto. */ 1941 c1 = PLACEHOLDER_TYPE_CONSTRAINTS (c1); 1942 if (c2 && TREE_CODE (c2) == TEMPLATE_TYPE_PARM) 1943 c2 = PLACEHOLDER_TYPE_CONSTRAINTS (c2); 1944 1945 if (c1 == c2) 1946 return true; 1947 if (!c1 || !c2) 1948 return false; 1949 if (c1 == error_mark_node || c2 == error_mark_node) 1950 /* We get here during satisfaction; when a deduction constraint 1951 fails, substitution can produce an error_mark_node for the 1952 placeholder constraints. */ 1953 return false; 1954 1955 tree t1, t2, a1, a2; 1956 placeholder_extract_concept_and_args (c1, t1, a1); 1957 placeholder_extract_concept_and_args (c2, t2, a2); 1958 1959 if (t1 != t2) 1960 return false; 1961 1962 int len1 = TREE_VEC_LENGTH (a1); 1963 int len2 = TREE_VEC_LENGTH (a2); 1964 if (len1 != len2) 1965 return false; 1966 1967 /* Skip the first argument so we don't infinitely recurse. 1968 Also, they may differ in template parameter index. */ 1969 for (int i = 1; i < len1; ++i) 1970 { 1971 tree t1 = TREE_VEC_ELT (a1, i); 1972 tree t2 = TREE_VEC_ELT (a2, i); 1973 if (!template_args_equal (t1, t2)) 1974 return false; 1975 } 1976 return true; 1977 } 1978 1979 /* Return a hash value for the placeholder ATOMIC_CONSTR C. */ 1980 1981 hashval_t 1982 hash_placeholder_constraint (tree c) 1983 { 1984 tree t, a; 1985 placeholder_extract_concept_and_args (c, t, a); 1986 1987 /* Like hash_tmpl_and_args, but skip the first argument. */ 1988 hashval_t val = iterative_hash_object (DECL_UID (t), 0); 1989 1990 for (int i = TREE_VEC_LENGTH (a)-1; i > 0; --i) 1991 val = iterative_hash_template_arg (TREE_VEC_ELT (a, i), val); 1992 1993 return val; 1994 } 1995 1996 /* Substitute through the expression of a simple requirement or 1997 compound requirement. */ 1998 1999 static tree 2000 tsubst_valid_expression_requirement (tree t, tree args, sat_info info) 2001 { 2002 tsubst_flags_t quiet = info.complain & ~tf_warning_or_error; 2003 tree r = tsubst_expr (t, args, quiet, info.in_decl); 2004 if (r != error_mark_node 2005 && (processing_template_decl 2006 || convert_to_void (r, ICV_STATEMENT, quiet) != error_mark_node)) 2007 return r; 2008 2009 if (info.diagnose_unsatisfaction_p ()) 2010 { 2011 location_t loc = cp_expr_loc_or_input_loc (t); 2012 if (diagnosing_failed_constraint::replay_errors_p ()) 2013 { 2014 inform (loc, "the required expression %qE is invalid, because", t); 2015 if (r == error_mark_node) 2016 tsubst_expr (t, args, info.complain, info.in_decl); 2017 else 2018 convert_to_void (r, ICV_STATEMENT, info.complain); 2019 } 2020 else 2021 inform (loc, "the required expression %qE is invalid", t); 2022 } 2023 else if (info.noisy ()) 2024 { 2025 r = tsubst_expr (t, args, info.complain, info.in_decl); 2026 convert_to_void (r, ICV_STATEMENT, info.complain); 2027 } 2028 2029 return error_mark_node; 2030 } 2031 2032 2033 /* Substitute through the simple requirement. */ 2034 2035 static tree 2036 tsubst_simple_requirement (tree t, tree args, sat_info info) 2037 { 2038 tree t0 = TREE_OPERAND (t, 0); 2039 tree expr = tsubst_valid_expression_requirement (t0, args, info); 2040 if (expr == error_mark_node) 2041 return error_mark_node; 2042 if (processing_template_decl) 2043 return finish_simple_requirement (EXPR_LOCATION (t), expr); 2044 return boolean_true_node; 2045 } 2046 2047 /* Subroutine of tsubst_type_requirement that performs the actual substitution 2048 and diagnosing. Also used by tsubst_compound_requirement. */ 2049 2050 static tree 2051 tsubst_type_requirement_1 (tree t, tree args, sat_info info, location_t loc) 2052 { 2053 tsubst_flags_t quiet = info.complain & ~tf_warning_or_error; 2054 tree r = tsubst (t, args, quiet, info.in_decl); 2055 if (r != error_mark_node) 2056 return r; 2057 2058 if (info.diagnose_unsatisfaction_p ()) 2059 { 2060 if (diagnosing_failed_constraint::replay_errors_p ()) 2061 { 2062 /* Replay the substitution error. */ 2063 inform (loc, "the required type %qT is invalid, because", t); 2064 tsubst (t, args, info.complain, info.in_decl); 2065 } 2066 else 2067 inform (loc, "the required type %qT is invalid", t); 2068 } 2069 else if (info.noisy ()) 2070 tsubst (t, args, info.complain, info.in_decl); 2071 2072 return error_mark_node; 2073 } 2074 2075 2076 /* Substitute through the type requirement. */ 2077 2078 static tree 2079 tsubst_type_requirement (tree t, tree args, sat_info info) 2080 { 2081 tree t0 = TREE_OPERAND (t, 0); 2082 tree type = tsubst_type_requirement_1 (t0, args, info, EXPR_LOCATION (t)); 2083 if (type == error_mark_node) 2084 return error_mark_node; 2085 if (processing_template_decl) 2086 return finish_type_requirement (EXPR_LOCATION (t), type); 2087 return boolean_true_node; 2088 } 2089 2090 /* True if TYPE can be deduced from EXPR. */ 2091 2092 static bool 2093 type_deducible_p (tree expr, tree type, tree placeholder, tree args, 2094 subst_info info) 2095 { 2096 /* Make sure deduction is performed against ( EXPR ), so that 2097 references are preserved in the result. */ 2098 expr = force_paren_expr_uneval (expr); 2099 2100 tree deduced_type = do_auto_deduction (type, expr, placeholder, 2101 info.complain, adc_requirement, 2102 /*outer_targs=*/args); 2103 2104 return deduced_type != error_mark_node; 2105 } 2106 2107 /* True if EXPR can not be converted to TYPE. */ 2108 2109 static bool 2110 expression_convertible_p (tree expr, tree type, subst_info info) 2111 { 2112 tree conv = 2113 perform_direct_initialization_if_possible (type, expr, false, 2114 info.complain); 2115 if (conv == error_mark_node) 2116 return false; 2117 if (conv == NULL_TREE) 2118 { 2119 if (info.complain & tf_error) 2120 { 2121 location_t loc = EXPR_LOC_OR_LOC (expr, input_location); 2122 error_at (loc, "cannot convert %qE to %qT", expr, type); 2123 } 2124 return false; 2125 } 2126 return true; 2127 } 2128 2129 2130 /* Substitute through the compound requirement. */ 2131 2132 static tree 2133 tsubst_compound_requirement (tree t, tree args, sat_info info) 2134 { 2135 tree t0 = TREE_OPERAND (t, 0); 2136 tree t1 = TREE_OPERAND (t, 1); 2137 tree expr = tsubst_valid_expression_requirement (t0, args, info); 2138 if (expr == error_mark_node) 2139 return error_mark_node; 2140 2141 location_t loc = cp_expr_loc_or_input_loc (expr); 2142 2143 subst_info quiet (info.complain & ~tf_warning_or_error, info.in_decl); 2144 2145 /* Check the noexcept condition. */ 2146 bool noexcept_p = COMPOUND_REQ_NOEXCEPT_P (t); 2147 if (noexcept_p && !processing_template_decl 2148 && !expr_noexcept_p (expr, quiet.complain)) 2149 { 2150 if (info.diagnose_unsatisfaction_p ()) 2151 inform (loc, "%qE is not %<noexcept%>", expr); 2152 else 2153 return error_mark_node; 2154 } 2155 2156 /* Substitute through the type expression, if any. */ 2157 tree type = tsubst_type_requirement_1 (t1, args, info, EXPR_LOCATION (t)); 2158 if (type == error_mark_node) 2159 return error_mark_node; 2160 2161 /* Check expression against the result type. */ 2162 if (type && !processing_template_decl) 2163 { 2164 if (tree placeholder = type_uses_auto (type)) 2165 { 2166 if (!type_deducible_p (expr, type, placeholder, args, quiet)) 2167 { 2168 if (info.diagnose_unsatisfaction_p ()) 2169 { 2170 if (diagnosing_failed_constraint::replay_errors_p ()) 2171 { 2172 inform (loc, 2173 "%qE does not satisfy return-type-requirement, " 2174 "because", t0); 2175 /* Further explain the reason for the error. */ 2176 type_deducible_p (expr, type, placeholder, args, info); 2177 } 2178 else 2179 inform (loc, 2180 "%qE does not satisfy return-type-requirement", t0); 2181 } 2182 return error_mark_node; 2183 } 2184 } 2185 else if (!expression_convertible_p (expr, type, quiet)) 2186 { 2187 if (info.diagnose_unsatisfaction_p ()) 2188 { 2189 if (diagnosing_failed_constraint::replay_errors_p ()) 2190 { 2191 inform (loc, "cannot convert %qE to %qT because", t0, type); 2192 /* Further explain the reason for the error. */ 2193 expression_convertible_p (expr, type, info); 2194 } 2195 else 2196 inform (loc, "cannot convert %qE to %qT", t0, type); 2197 } 2198 return error_mark_node; 2199 } 2200 } 2201 2202 if (processing_template_decl) 2203 return finish_compound_requirement (EXPR_LOCATION (t), 2204 expr, type, noexcept_p); 2205 return boolean_true_node; 2206 } 2207 2208 /* Substitute through the nested requirement. */ 2209 2210 static tree 2211 tsubst_nested_requirement (tree t, tree args, sat_info info) 2212 { 2213 if (processing_template_decl) 2214 { 2215 tree req = TREE_OPERAND (t, 0); 2216 req = tsubst_constraint (req, args, info.complain, info.in_decl); 2217 if (req == error_mark_node) 2218 return error_mark_node; 2219 return finish_nested_requirement (EXPR_LOCATION (t), req); 2220 } 2221 2222 sat_info quiet (info.complain & ~tf_warning_or_error, info.in_decl); 2223 tree result = constraint_satisfaction_value (t, args, quiet); 2224 if (result == boolean_true_node) 2225 return boolean_true_node; 2226 2227 if (result == boolean_false_node 2228 && info.diagnose_unsatisfaction_p ()) 2229 { 2230 tree expr = TREE_OPERAND (t, 0); 2231 location_t loc = cp_expr_location (t); 2232 if (diagnosing_failed_constraint::replay_errors_p ()) 2233 { 2234 /* Replay the substitution error. */ 2235 inform (loc, "nested requirement %qE is not satisfied, because", expr); 2236 constraint_satisfaction_value (t, args, info); 2237 } 2238 else 2239 inform (loc, "nested requirement %qE is not satisfied", expr); 2240 } 2241 2242 return error_mark_node; 2243 } 2244 2245 /* Substitute ARGS into the requirement T. */ 2246 2247 static tree 2248 tsubst_requirement (tree t, tree args, sat_info info) 2249 { 2250 iloc_sentinel loc_s (cp_expr_location (t)); 2251 switch (TREE_CODE (t)) 2252 { 2253 case SIMPLE_REQ: 2254 return tsubst_simple_requirement (t, args, info); 2255 case TYPE_REQ: 2256 return tsubst_type_requirement (t, args, info); 2257 case COMPOUND_REQ: 2258 return tsubst_compound_requirement (t, args, info); 2259 case NESTED_REQ: 2260 return tsubst_nested_requirement (t, args, info); 2261 default: 2262 break; 2263 } 2264 gcc_unreachable (); 2265 } 2266 2267 static tree 2268 declare_constraint_vars (tree parms, tree vars) 2269 { 2270 tree s = vars; 2271 for (tree t = parms; t; t = DECL_CHAIN (t)) 2272 { 2273 if (DECL_PACK_P (t)) 2274 { 2275 tree pack = extract_fnparm_pack (t, &s); 2276 register_local_specialization (pack, t); 2277 } 2278 else 2279 { 2280 register_local_specialization (s, t); 2281 s = DECL_CHAIN (s); 2282 } 2283 } 2284 return vars; 2285 } 2286 2287 /* Substitute through as if checking function parameter types. This 2288 will diagnose common parameter type errors. Returns error_mark_node 2289 if an error occurred. */ 2290 2291 static tree 2292 check_constraint_variables (tree t, tree args, subst_info info) 2293 { 2294 tree types = NULL_TREE; 2295 tree p = t; 2296 while (p && !VOID_TYPE_P (p)) 2297 { 2298 types = tree_cons (NULL_TREE, TREE_TYPE (p), types); 2299 p = TREE_CHAIN (p); 2300 } 2301 types = chainon (nreverse (types), void_list_node); 2302 return tsubst_function_parms (types, args, info.complain, info.in_decl); 2303 } 2304 2305 /* A subroutine of tsubst_parameterized_constraint. Substitute ARGS 2306 into the parameter list T, producing a sequence of constraint 2307 variables, declared in the current scope. 2308 2309 Note that the caller must establish a local specialization stack 2310 prior to calling this function since this substitution will 2311 declare the substituted parameters. */ 2312 2313 static tree 2314 tsubst_constraint_variables (tree t, tree args, subst_info info) 2315 { 2316 /* Perform a trial substitution to check for type errors. */ 2317 tree parms = check_constraint_variables (t, args, info); 2318 if (parms == error_mark_node) 2319 return error_mark_node; 2320 2321 /* Clear cp_unevaluated_operand across tsubst so that we get a proper chain 2322 of PARM_DECLs. */ 2323 int saved_unevaluated_operand = cp_unevaluated_operand; 2324 cp_unevaluated_operand = 0; 2325 tree vars = tsubst (t, args, info.complain, info.in_decl); 2326 cp_unevaluated_operand = saved_unevaluated_operand; 2327 if (vars == error_mark_node) 2328 return error_mark_node; 2329 return declare_constraint_vars (t, vars); 2330 } 2331 2332 /* Substitute ARGS into the requires-expression T. [8.4.7]p6. The 2333 substitution of template arguments into a requires-expression 2334 may result in the formation of invalid types or expressions 2335 in its requirements ... In such cases, the expression evaluates 2336 to false; it does not cause the program to be ill-formed. 2337 2338 When substituting through a REQUIRES_EXPR as part of template 2339 instantiation, we call this routine with info.quiet() true. 2340 2341 When evaluating a REQUIRES_EXPR that appears outside a template in 2342 cp_parser_requires_expression, we call this routine with 2343 info.noisy() true. 2344 2345 Finally, when diagnosing unsatisfaction from diagnose_atomic_constraint 2346 and when diagnosing a false REQUIRES_EXPR via diagnose_constraints, 2347 we call this routine with info.diagnose_unsatisfaction_p() true. */ 2348 2349 static tree 2350 tsubst_requires_expr (tree t, tree args, sat_info info) 2351 { 2352 local_specialization_stack stack (lss_copy); 2353 2354 /* We need to check access during the substitution. */ 2355 deferring_access_check_sentinel acs (dk_no_deferred); 2356 2357 /* A requires-expression is an unevaluated context. */ 2358 cp_unevaluated u; 2359 2360 args = add_extra_args (REQUIRES_EXPR_EXTRA_ARGS (t), args, 2361 info.complain, info.in_decl); 2362 if (processing_template_decl 2363 && !processing_constraint_expression_p ()) 2364 { 2365 /* We're partially instantiating a generic lambda. Substituting into 2366 this requires-expression now may cause its requirements to get 2367 checked out of order, so instead just remember the template 2368 arguments and wait until we can substitute them all at once. 2369 2370 Except if this requires-expr is part of associated constraints 2371 that we're substituting into directly (for e.g. declaration 2372 matching or dguide constraint rewriting), in which case we need 2373 to partially substitute. */ 2374 t = copy_node (t); 2375 REQUIRES_EXPR_EXTRA_ARGS (t) = NULL_TREE; 2376 REQUIRES_EXPR_EXTRA_ARGS (t) = build_extra_args (t, args, info.complain); 2377 return t; 2378 } 2379 2380 tree parms = REQUIRES_EXPR_PARMS (t); 2381 if (parms) 2382 { 2383 parms = tsubst_constraint_variables (parms, args, info); 2384 if (parms == error_mark_node) 2385 return boolean_false_node; 2386 } 2387 2388 tree result = boolean_true_node; 2389 if (processing_template_decl) 2390 result = NULL_TREE; 2391 for (tree reqs = REQUIRES_EXPR_REQS (t); reqs; reqs = TREE_CHAIN (reqs)) 2392 { 2393 tree req = TREE_VALUE (reqs); 2394 req = tsubst_requirement (req, args, info); 2395 if (req == error_mark_node) 2396 { 2397 result = boolean_false_node; 2398 if (info.diagnose_unsatisfaction_p ()) 2399 /* Keep going so that we diagnose all failed requirements. */; 2400 else 2401 break; 2402 } 2403 else if (processing_template_decl) 2404 result = tree_cons (NULL_TREE, req, result); 2405 } 2406 if (processing_template_decl && result != boolean_false_node) 2407 result = finish_requires_expr (EXPR_LOCATION (t), parms, nreverse (result)); 2408 return result; 2409 } 2410 2411 /* Public wrapper for the above. */ 2412 2413 tree 2414 tsubst_requires_expr (tree t, tree args, 2415 tsubst_flags_t complain, tree in_decl) 2416 { 2417 sat_info info (complain, in_decl); 2418 return tsubst_requires_expr (t, args, info); 2419 } 2420 2421 /* Substitute ARGS into the constraint information CI, producing a new 2422 constraint record. */ 2423 2424 tree 2425 tsubst_constraint_info (tree t, tree args, 2426 tsubst_flags_t complain, tree in_decl) 2427 { 2428 if (!t || t == error_mark_node || !check_constraint_info (t)) 2429 return NULL_TREE; 2430 2431 tree tr = tsubst_constraint (CI_TEMPLATE_REQS (t), args, complain, in_decl); 2432 tree dr = tsubst_constraint (CI_DECLARATOR_REQS (t), args, complain, in_decl); 2433 return build_constraints (tr, dr); 2434 } 2435 2436 /* Substitute through a parameter mapping, in order to get the actual 2437 arguments used to instantiate an atomic constraint. This may fail 2438 if the substitution into arguments produces something ill-formed. */ 2439 2440 static tree 2441 tsubst_parameter_mapping (tree map, tree args, subst_info info) 2442 { 2443 if (!map) 2444 return NULL_TREE; 2445 2446 tsubst_flags_t complain = info.complain; 2447 tree in_decl = info.in_decl; 2448 2449 tree result = NULL_TREE; 2450 for (tree p = map; p; p = TREE_CHAIN (p)) 2451 { 2452 if (p == error_mark_node) 2453 return error_mark_node; 2454 tree parm = TREE_VALUE (p); 2455 tree arg = TREE_PURPOSE (p); 2456 tree new_arg; 2457 if (ARGUMENT_PACK_P (arg)) 2458 new_arg = tsubst_argument_pack (arg, args, complain, in_decl); 2459 else 2460 { 2461 new_arg = tsubst_template_arg (arg, args, complain, in_decl); 2462 if (TYPE_P (new_arg)) 2463 new_arg = canonicalize_type_argument (new_arg, complain); 2464 } 2465 if (TREE_CODE (new_arg) == TYPE_ARGUMENT_PACK) 2466 { 2467 tree pack_args = ARGUMENT_PACK_ARGS (new_arg); 2468 for (tree& pack_arg : tree_vec_range (pack_args)) 2469 if (TYPE_P (pack_arg)) 2470 pack_arg = canonicalize_type_argument (pack_arg, complain); 2471 } 2472 if (new_arg == error_mark_node) 2473 return error_mark_node; 2474 2475 result = tree_cons (new_arg, parm, result); 2476 } 2477 return nreverse (result); 2478 } 2479 2480 tree 2481 tsubst_parameter_mapping (tree map, tree args, tsubst_flags_t complain, tree in_decl) 2482 { 2483 return tsubst_parameter_mapping (map, args, subst_info (complain, in_decl)); 2484 } 2485 2486 /*--------------------------------------------------------------------------- 2487 Constraint satisfaction 2488 ---------------------------------------------------------------------------*/ 2489 2490 /* True if we are currently satisfying a constraint. */ 2491 2492 static bool satisfying_constraint; 2493 2494 /* A vector of incomplete types (and of declarations with undeduced return type), 2495 appended to by note_failed_type_completion_for_satisfaction. The 2496 satisfaction caches use this in order to keep track of "potentially unstable" 2497 satisfaction results. 2498 2499 Since references to entries in this vector are stored only in the 2500 GC-deletable sat_cache, it's safe to make this deletable as well. */ 2501 2502 static GTY((deletable)) vec<tree, va_gc> *failed_type_completions; 2503 2504 /* Called whenever a type completion (or return type deduction) failure occurs 2505 that definitely affects the meaning of the program, by e.g. inducing 2506 substitution failure. */ 2507 2508 void 2509 note_failed_type_completion_for_satisfaction (tree t) 2510 { 2511 if (satisfying_constraint) 2512 { 2513 gcc_checking_assert ((TYPE_P (t) && !COMPLETE_TYPE_P (t)) 2514 || (DECL_P (t) && undeduced_auto_decl (t))); 2515 vec_safe_push (failed_type_completions, t); 2516 } 2517 } 2518 2519 /* Returns true if the range [BEGIN, END) of elements within the 2520 failed_type_completions vector contains a complete type (or a 2521 declaration with a non-placeholder return type). */ 2522 2523 static bool 2524 some_type_complete_p (int begin, int end) 2525 { 2526 for (int i = begin; i < end; i++) 2527 { 2528 tree t = (*failed_type_completions)[i]; 2529 if (TYPE_P (t) && COMPLETE_TYPE_P (t)) 2530 return true; 2531 if (DECL_P (t) && !undeduced_auto_decl (t)) 2532 return true; 2533 } 2534 return false; 2535 } 2536 2537 /* Hash functions and data types for satisfaction cache entries. */ 2538 2539 struct GTY((for_user)) sat_entry 2540 { 2541 /* The relevant ATOMIC_CONSTR. */ 2542 tree atom; 2543 2544 /* The relevant template arguments. */ 2545 tree args; 2546 2547 /* The result of satisfaction of ATOM+ARGS. 2548 This is either boolean_true_node, boolean_false_node or error_mark_node, 2549 where error_mark_node indicates ill-formed satisfaction. 2550 It's set to NULL_TREE while computing satisfaction of ATOM+ARGS for 2551 the first time. */ 2552 tree result; 2553 2554 /* The value of input_location when satisfaction of ATOM+ARGS was first 2555 performed. */ 2556 location_t location; 2557 2558 /* The range of elements appended to the failed_type_completions vector 2559 during computation of this satisfaction result, encoded as a begin/end 2560 pair of offsets. */ 2561 int ftc_begin, ftc_end; 2562 2563 /* True if we want to diagnose the above instability when it's detected. 2564 We don't always want to do so, in order to avoid emitting duplicate 2565 diagnostics in some cases. */ 2566 bool diagnose_instability; 2567 2568 /* True if we're in the middle of computing this satisfaction result. 2569 Used during both quiet and noisy satisfaction to detect self-recursive 2570 satisfaction. */ 2571 bool evaluating; 2572 }; 2573 2574 struct sat_hasher : ggc_ptr_hash<sat_entry> 2575 { 2576 static hashval_t hash (sat_entry *e) 2577 { 2578 auto cso = make_temp_override (comparing_specializations); 2579 ++comparing_specializations; 2580 2581 if (ATOMIC_CONSTR_MAP_INSTANTIATED_P (e->atom)) 2582 { 2583 /* Atoms with instantiated mappings are built during satisfaction. 2584 They live only inside the sat_cache, and we build one to query 2585 the cache with each time we instantiate a mapping. */ 2586 gcc_assert (!e->args); 2587 return hash_atomic_constraint (e->atom); 2588 } 2589 2590 /* Atoms with uninstantiated mappings are built during normalization. 2591 Since normalize_atom caches the atoms it returns, we can assume 2592 pointer-based identity for fast hashing and comparison. Even if this 2593 assumption is violated, that's okay, we'll just get a cache miss. */ 2594 hashval_t value = htab_hash_pointer (e->atom); 2595 2596 if (tree map = ATOMIC_CONSTR_MAP (e->atom)) 2597 /* Only the parameters that are used in the targets of the mapping 2598 affect the satisfaction value of the atom. So we consider only 2599 the arguments for these parameters, and ignore the rest. */ 2600 for (tree target_parms = TREE_TYPE (map); 2601 target_parms; 2602 target_parms = TREE_CHAIN (target_parms)) 2603 { 2604 int level, index; 2605 tree parm = TREE_VALUE (target_parms); 2606 template_parm_level_and_index (parm, &level, &index); 2607 tree arg = TMPL_ARG (e->args, level, index); 2608 value = iterative_hash_template_arg (arg, value); 2609 } 2610 return value; 2611 } 2612 2613 static bool equal (sat_entry *e1, sat_entry *e2) 2614 { 2615 auto cso = make_temp_override (comparing_specializations); 2616 ++comparing_specializations; 2617 2618 if (ATOMIC_CONSTR_MAP_INSTANTIATED_P (e1->atom) 2619 != ATOMIC_CONSTR_MAP_INSTANTIATED_P (e2->atom)) 2620 return false; 2621 2622 /* See sat_hasher::hash. */ 2623 if (ATOMIC_CONSTR_MAP_INSTANTIATED_P (e1->atom)) 2624 { 2625 gcc_assert (!e1->args && !e2->args); 2626 return atomic_constraints_identical_p (e1->atom, e2->atom); 2627 } 2628 2629 if (e1->atom != e2->atom) 2630 return false; 2631 2632 if (tree map = ATOMIC_CONSTR_MAP (e1->atom)) 2633 for (tree target_parms = TREE_TYPE (map); 2634 target_parms; 2635 target_parms = TREE_CHAIN (target_parms)) 2636 { 2637 int level, index; 2638 tree parm = TREE_VALUE (target_parms); 2639 template_parm_level_and_index (parm, &level, &index); 2640 tree arg1 = TMPL_ARG (e1->args, level, index); 2641 tree arg2 = TMPL_ARG (e2->args, level, index); 2642 if (!template_args_equal (arg1, arg2)) 2643 return false; 2644 } 2645 return true; 2646 } 2647 }; 2648 2649 /* Cache the result of satisfy_atom. */ 2650 static GTY((deletable)) hash_table<sat_hasher> *sat_cache; 2651 2652 /* Cache the result of satisfy_declaration_constraints. */ 2653 static GTY((deletable)) hash_map<tree, tree> *decl_satisfied_cache; 2654 2655 /* A tool used by satisfy_atom to help manage satisfaction caching and to 2656 diagnose "unstable" satisfaction values. We insert into the cache only 2657 when performing satisfaction quietly. */ 2658 2659 struct satisfaction_cache 2660 { 2661 satisfaction_cache (tree, tree, sat_info); 2662 tree get (); 2663 tree save (tree); 2664 2665 sat_entry *entry; 2666 sat_info info; 2667 int ftc_begin; 2668 }; 2669 2670 /* Constructor for the satisfaction_cache class. We're performing satisfaction 2671 of ATOM+ARGS according to INFO. */ 2672 2673 satisfaction_cache 2674 ::satisfaction_cache (tree atom, tree args, sat_info info) 2675 : entry(nullptr), info(info), ftc_begin(-1) 2676 { 2677 if (!sat_cache) 2678 sat_cache = hash_table<sat_hasher>::create_ggc (31); 2679 2680 /* When noisy, we query the satisfaction cache in order to diagnose 2681 "unstable" satisfaction values. */ 2682 if (info.noisy ()) 2683 { 2684 /* When noisy, constraints have been re-normalized, and that breaks the 2685 pointer-based identity assumption of sat_cache (for atoms with 2686 uninstantiated mappings). So undo this re-normalization by looking in 2687 the atom_cache for the corresponding atom that was used during quiet 2688 satisfaction. */ 2689 if (!ATOMIC_CONSTR_MAP_INSTANTIATED_P (atom)) 2690 { 2691 if (tree found = atom_cache->find (atom)) 2692 atom = found; 2693 else 2694 /* The lookup should always succeed, but if it fails then let's 2695 just leave 'entry' empty, effectively disabling the cache. */ 2696 return; 2697 } 2698 } 2699 2700 /* Look up or create the corresponding satisfaction entry. */ 2701 sat_entry elt; 2702 elt.atom = atom; 2703 elt.args = args; 2704 sat_entry **slot = sat_cache->find_slot (&elt, INSERT); 2705 if (*slot) 2706 entry = *slot; 2707 else if (info.quiet ()) 2708 { 2709 entry = ggc_alloc<sat_entry> (); 2710 entry->atom = atom; 2711 entry->args = args; 2712 entry->result = NULL_TREE; 2713 entry->location = input_location; 2714 entry->ftc_begin = entry->ftc_end = -1; 2715 entry->diagnose_instability = false; 2716 if (ATOMIC_CONSTR_MAP_INSTANTIATED_P (atom)) 2717 /* We always want to diagnose instability of an atom with an 2718 instantiated parameter mapping. For atoms with an uninstantiated 2719 mapping, we set this flag (in satisfy_atom) only if substitution 2720 into its mapping previously failed. */ 2721 entry->diagnose_instability = true; 2722 entry->evaluating = false; 2723 *slot = entry; 2724 } 2725 else 2726 { 2727 /* We're evaluating this atom for the first time, and doing so noisily. 2728 This shouldn't happen outside of error recovery situations involving 2729 unstable satisfaction. Let's just leave 'entry' empty, effectively 2730 disabling the cache, and remove the empty slot. */ 2731 gcc_checking_assert (seen_error ()); 2732 /* Appease hash_table::check_complete_insertion. */ 2733 *slot = ggc_alloc<sat_entry> (); 2734 sat_cache->clear_slot (slot); 2735 } 2736 } 2737 2738 /* Returns the cached satisfaction result if we have one and we're not 2739 recomputing the satisfaction result from scratch. Otherwise returns 2740 NULL_TREE. */ 2741 2742 tree 2743 satisfaction_cache::get () 2744 { 2745 if (!entry) 2746 return NULL_TREE; 2747 2748 if (entry->evaluating) 2749 { 2750 /* If we get here, it means satisfaction is self-recursive. */ 2751 gcc_checking_assert (!entry->result || seen_error ()); 2752 if (info.noisy ()) 2753 error_at (EXPR_LOCATION (ATOMIC_CONSTR_EXPR (entry->atom)), 2754 "satisfaction of atomic constraint %qE depends on itself", 2755 entry->atom); 2756 return error_mark_node; 2757 } 2758 2759 /* This satisfaction result is "potentially unstable" if a type for which 2760 type completion failed during its earlier computation is now complete. */ 2761 bool maybe_unstable = some_type_complete_p (entry->ftc_begin, 2762 entry->ftc_end); 2763 2764 if (info.noisy () || maybe_unstable || !entry->result) 2765 { 2766 /* We're computing the satisfaction result from scratch. */ 2767 entry->evaluating = true; 2768 ftc_begin = vec_safe_length (failed_type_completions); 2769 return NULL_TREE; 2770 } 2771 else 2772 return entry->result; 2773 } 2774 2775 /* RESULT is the computed satisfaction result. If RESULT differs from the 2776 previously cached result, this routine issues an appropriate error. 2777 Otherwise, when evaluating quietly, updates the cache appropriately. */ 2778 2779 tree 2780 satisfaction_cache::save (tree result) 2781 { 2782 if (!entry) 2783 return result; 2784 2785 gcc_checking_assert (entry->evaluating); 2786 entry->evaluating = false; 2787 2788 if (entry->result && result != entry->result) 2789 { 2790 if (info.quiet ()) 2791 /* Return error_mark_node to force satisfaction to get replayed 2792 noisily. */ 2793 return error_mark_node; 2794 else 2795 { 2796 if (entry->diagnose_instability) 2797 { 2798 auto_diagnostic_group d; 2799 error_at (EXPR_LOCATION (ATOMIC_CONSTR_EXPR (entry->atom)), 2800 "satisfaction value of atomic constraint %qE changed " 2801 "from %qE to %qE", entry->atom, entry->result, result); 2802 inform (entry->location, 2803 "satisfaction value first evaluated to %qE from here", 2804 entry->result); 2805 } 2806 /* For sake of error recovery, allow this latest satisfaction result 2807 to prevail. */ 2808 entry->result = result; 2809 return result; 2810 } 2811 } 2812 2813 if (info.quiet ()) 2814 { 2815 entry->result = result; 2816 /* Store into this entry the list of relevant failed type completions 2817 that occurred during (re)computation of the satisfaction result. */ 2818 gcc_checking_assert (ftc_begin != -1); 2819 entry->ftc_begin = ftc_begin; 2820 entry->ftc_end = vec_safe_length (failed_type_completions); 2821 } 2822 2823 return result; 2824 } 2825 2826 /* Substitute ARGS into constraint-expression T during instantiation of 2827 a member of a class template. */ 2828 2829 tree 2830 tsubst_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl) 2831 { 2832 /* We also don't want to evaluate concept-checks when substituting the 2833 constraint-expressions of a declaration. */ 2834 processing_constraint_expression_sentinel s; 2835 cp_unevaluated u; 2836 tree expr = tsubst_expr (t, args, complain, in_decl); 2837 return expr; 2838 } 2839 2840 static tree satisfy_constraint_r (tree, tree, sat_info info); 2841 2842 /* Compute the satisfaction of a conjunction. */ 2843 2844 static tree 2845 satisfy_conjunction (tree t, tree args, sat_info info) 2846 { 2847 tree lhs = satisfy_constraint_r (TREE_OPERAND (t, 0), args, info); 2848 if (lhs == error_mark_node || lhs == boolean_false_node) 2849 return lhs; 2850 return satisfy_constraint_r (TREE_OPERAND (t, 1), args, info); 2851 } 2852 2853 /* The current depth at which we're replaying an error during recursive 2854 diagnosis of a constraint satisfaction failure. */ 2855 2856 static int current_constraint_diagnosis_depth; 2857 2858 /* Whether CURRENT_CONSTRAINT_DIAGNOSIS_DEPTH has ever exceeded 2859 CONCEPTS_DIAGNOSTICS_MAX_DEPTH during recursive diagnosis of a constraint 2860 satisfaction error. */ 2861 2862 static bool concepts_diagnostics_max_depth_exceeded_p; 2863 2864 /* Recursive subroutine of collect_operands_of_disjunction. T is a normalized 2865 subexpression of a constraint (composed of CONJ_CONSTRs and DISJ_CONSTRs) 2866 and E is the corresponding unnormalized subexpression (composed of 2867 TRUTH_ANDIF_EXPRs and TRUTH_ORIF_EXPRs). */ 2868 2869 static void 2870 collect_operands_of_disjunction_r (tree t, tree e, 2871 auto_vec<tree_pair> *operands) 2872 { 2873 if (TREE_CODE (e) == TRUTH_ORIF_EXPR) 2874 { 2875 collect_operands_of_disjunction_r (TREE_OPERAND (t, 0), 2876 TREE_OPERAND (e, 0), operands); 2877 collect_operands_of_disjunction_r (TREE_OPERAND (t, 1), 2878 TREE_OPERAND (e, 1), operands); 2879 } 2880 else 2881 { 2882 tree_pair p = std::make_pair (t, e); 2883 operands->safe_push (p); 2884 } 2885 } 2886 2887 /* Recursively collect the normalized and unnormalized operands of the 2888 disjunction T and append them to OPERANDS in order. */ 2889 2890 static void 2891 collect_operands_of_disjunction (tree t, auto_vec<tree_pair> *operands) 2892 { 2893 collect_operands_of_disjunction_r (t, CONSTR_EXPR (t), operands); 2894 } 2895 2896 /* Compute the satisfaction of a disjunction. */ 2897 2898 static tree 2899 satisfy_disjunction (tree t, tree args, sat_info info) 2900 { 2901 /* Evaluate each operand with unsatisfaction diagnostics disabled. */ 2902 sat_info sub = info; 2903 sub.diagnose_unsatisfaction = false; 2904 2905 tree lhs = satisfy_constraint_r (TREE_OPERAND (t, 0), args, sub); 2906 if (lhs == boolean_true_node || lhs == error_mark_node) 2907 return lhs; 2908 2909 tree rhs = satisfy_constraint_r (TREE_OPERAND (t, 1), args, sub); 2910 if (rhs == boolean_true_node || rhs == error_mark_node) 2911 return rhs; 2912 2913 /* Both branches evaluated to false. Explain the satisfaction failure in 2914 each branch. */ 2915 if (info.diagnose_unsatisfaction_p ()) 2916 { 2917 diagnosing_failed_constraint failure (t, args, info.noisy ()); 2918 cp_expr disj_expr = CONSTR_EXPR (t); 2919 inform (disj_expr.get_location (), 2920 "no operand of the disjunction is satisfied"); 2921 if (diagnosing_failed_constraint::replay_errors_p ()) 2922 { 2923 /* Replay the error in each branch of the disjunction. */ 2924 auto_vec<tree_pair> operands; 2925 collect_operands_of_disjunction (t, &operands); 2926 for (unsigned i = 0; i < operands.length (); i++) 2927 { 2928 tree norm_op = operands[i].first; 2929 tree op = operands[i].second; 2930 location_t loc = make_location (cp_expr_location (op), 2931 disj_expr.get_start (), 2932 disj_expr.get_finish ()); 2933 inform (loc, "the operand %qE is unsatisfied because", op); 2934 satisfy_constraint_r (norm_op, args, info); 2935 } 2936 } 2937 } 2938 2939 return boolean_false_node; 2940 } 2941 2942 /* Ensures that T is a truth value and not (accidentally, as sometimes 2943 happens) an integer value. */ 2944 2945 tree 2946 satisfaction_value (tree t) 2947 { 2948 if (t == error_mark_node || t == boolean_true_node || t == boolean_false_node) 2949 return t; 2950 2951 gcc_assert (TREE_CODE (t) == INTEGER_CST 2952 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t), 2953 boolean_type_node)); 2954 if (integer_zerop (t)) 2955 return boolean_false_node; 2956 else 2957 return boolean_true_node; 2958 } 2959 2960 /* Build a new template argument vector corresponding to the parameter 2961 mapping of the atomic constraint T, using arguments from ARGS. */ 2962 2963 static tree 2964 get_mapped_args (tree t, tree args) 2965 { 2966 tree map = ATOMIC_CONSTR_MAP (t); 2967 2968 /* No map, no arguments. */ 2969 if (!map) 2970 return NULL_TREE; 2971 2972 /* Determine the depth of the resulting argument vector. */ 2973 int depth; 2974 if (ATOMIC_CONSTR_EXPR_FROM_CONCEPT_P (t)) 2975 /* The expression of this atomic constraint comes from a concept definition, 2976 whose template depth is always one, so the resulting argument vector 2977 will also have depth one. */ 2978 depth = 1; 2979 else 2980 /* Otherwise, the expression of this atomic constraint comes from 2981 the context of the constrained entity, whose template depth is that 2982 of ARGS. */ 2983 depth = TMPL_ARGS_DEPTH (args); 2984 2985 /* Place each argument at its corresponding position in the argument 2986 list. Note that the list will be sparse (not all arguments supplied), 2987 but instantiation is guaranteed to only use the parameters in the 2988 mapping, so null arguments would never be used. */ 2989 auto_vec< vec<tree> > lists (depth); 2990 lists.quick_grow_cleared (depth); 2991 for (tree p = map; p; p = TREE_CHAIN (p)) 2992 { 2993 int level; 2994 int index; 2995 template_parm_level_and_index (TREE_VALUE (p), &level, &index); 2996 2997 /* Insert the argument into its corresponding position. */ 2998 vec<tree> &list = lists[level - 1]; 2999 if (index >= (int)list.length ()) 3000 list.safe_grow_cleared (index + 1, /*exact=*/false); 3001 list[index] = TREE_PURPOSE (p); 3002 } 3003 3004 /* Build the new argument list. */ 3005 args = make_tree_vec (lists.length ()); 3006 for (unsigned i = 0; i != lists.length (); ++i) 3007 { 3008 vec<tree> &list = lists[i]; 3009 tree level = make_tree_vec (list.length ()); 3010 for (unsigned j = 0; j < list.length(); ++j) 3011 TREE_VEC_ELT (level, j) = list[j]; 3012 SET_TMPL_ARGS_LEVEL (args, i + 1, level); 3013 list.release (); 3014 } 3015 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, 0); 3016 3017 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args) 3018 && TMPL_ARGS_DEPTH (args) == 1) 3019 { 3020 /* Get rid of the redundant outer TREE_VEC. */ 3021 tree level = TMPL_ARGS_LEVEL (args, 1); 3022 ggc_free (args); 3023 args = level; 3024 } 3025 3026 return args; 3027 } 3028 3029 static void diagnose_atomic_constraint (tree, tree, tree, sat_info); 3030 3031 /* Compute the satisfaction of an atomic constraint. */ 3032 3033 static tree 3034 satisfy_atom (tree t, tree args, sat_info info) 3035 { 3036 /* In case there is a diagnostic, we want to establish the context 3037 prior to printing errors. If no errors occur, this context is 3038 removed before returning. */ 3039 diagnosing_failed_constraint failure (t, args, info.noisy ()); 3040 3041 satisfaction_cache cache (t, args, info); 3042 if (tree r = cache.get ()) 3043 return r; 3044 3045 /* Perform substitution quietly. */ 3046 subst_info quiet (tf_none, NULL_TREE); 3047 3048 /* Instantiate the parameter mapping. */ 3049 tree map = tsubst_parameter_mapping (ATOMIC_CONSTR_MAP (t), args, quiet); 3050 if (map == error_mark_node) 3051 { 3052 /* If instantiation of the parameter mapping fails, the constraint is 3053 not satisfied. Replay the substitution. */ 3054 if (info.diagnose_unsatisfaction_p ()) 3055 tsubst_parameter_mapping (ATOMIC_CONSTR_MAP (t), args, info); 3056 if (info.quiet ()) 3057 /* Since instantiation of the parameter mapping failed, we 3058 want to diagnose potential instability of this satisfaction 3059 result. */ 3060 cache.entry->diagnose_instability = true; 3061 return cache.save (boolean_false_node); 3062 } 3063 3064 /* Now build a new atom using the instantiated mapping. We use 3065 this atom as a second key to the satisfaction cache, and we 3066 also pass it to diagnose_atomic_constraint so that diagnostics 3067 which refer to the atom display the instantiated mapping. */ 3068 t = copy_node (t); 3069 ATOMIC_CONSTR_MAP (t) = map; 3070 gcc_assert (!ATOMIC_CONSTR_MAP_INSTANTIATED_P (t)); 3071 ATOMIC_CONSTR_MAP_INSTANTIATED_P (t) = true; 3072 satisfaction_cache inst_cache (t, /*args=*/NULL_TREE, info); 3073 if (tree r = inst_cache.get ()) 3074 { 3075 cache.entry->location = inst_cache.entry->location; 3076 return cache.save (r); 3077 } 3078 3079 /* Rebuild the argument vector from the parameter mapping. */ 3080 args = get_mapped_args (t, args); 3081 3082 /* Apply the parameter mapping (i.e., just substitute). */ 3083 tree expr = ATOMIC_CONSTR_EXPR (t); 3084 tree result = tsubst_expr (expr, args, quiet.complain, quiet.in_decl); 3085 if (result == error_mark_node) 3086 { 3087 /* If substitution results in an invalid type or expression, the constraint 3088 is not satisfied. Replay the substitution. */ 3089 if (info.diagnose_unsatisfaction_p ()) 3090 tsubst_expr (expr, args, info.complain, info.in_decl); 3091 return cache.save (inst_cache.save (boolean_false_node)); 3092 } 3093 3094 /* [17.4.1.2] ... lvalue-to-rvalue conversion is performed as necessary, 3095 and EXPR shall be a constant expression of type bool. */ 3096 result = force_rvalue (result, info.complain); 3097 if (result == error_mark_node) 3098 return cache.save (inst_cache.save (error_mark_node)); 3099 if (!same_type_p (TREE_TYPE (result), boolean_type_node)) 3100 { 3101 if (info.noisy ()) 3102 diagnose_atomic_constraint (t, args, result, info); 3103 return cache.save (inst_cache.save (error_mark_node)); 3104 } 3105 3106 /* Compute the value of the constraint. */ 3107 if (info.noisy ()) 3108 { 3109 iloc_sentinel ils (EXPR_LOCATION (result)); 3110 result = cxx_constant_value (result); 3111 } 3112 else 3113 { 3114 result = maybe_constant_value (result, NULL_TREE, mce_true); 3115 if (!TREE_CONSTANT (result)) 3116 result = error_mark_node; 3117 } 3118 result = satisfaction_value (result); 3119 if (result == boolean_false_node && info.diagnose_unsatisfaction_p ()) 3120 diagnose_atomic_constraint (t, args, result, info); 3121 3122 return cache.save (inst_cache.save (result)); 3123 } 3124 3125 /* Determine if the normalized constraint T is satisfied. 3126 Returns boolean_true_node if the expression/constraint is 3127 satisfied, boolean_false_node if not, and error_mark_node 3128 if the there was an error evaluating the constraint. 3129 3130 The parameter mapping of atomic constraints is simply the 3131 set of template arguments that will be substituted into 3132 the expression, regardless of template parameters appearing 3133 withing. Whether a template argument is used in the atomic 3134 constraint only matters for subsumption. */ 3135 3136 static tree 3137 satisfy_constraint_r (tree t, tree args, sat_info info) 3138 { 3139 if (t == error_mark_node) 3140 return error_mark_node; 3141 3142 switch (TREE_CODE (t)) 3143 { 3144 case CONJ_CONSTR: 3145 return satisfy_conjunction (t, args, info); 3146 case DISJ_CONSTR: 3147 return satisfy_disjunction (t, args, info); 3148 case ATOMIC_CONSTR: 3149 return satisfy_atom (t, args, info); 3150 default: 3151 gcc_unreachable (); 3152 } 3153 } 3154 3155 /* Check that the normalized constraint T is satisfied for ARGS. */ 3156 3157 static tree 3158 satisfy_normalized_constraints (tree t, tree args, sat_info info) 3159 { 3160 auto_timevar time (TV_CONSTRAINT_SAT); 3161 3162 auto ovr = make_temp_override (satisfying_constraint, true); 3163 3164 /* Turn off template processing. Constraint satisfaction only applies 3165 to non-dependent terms, so we want to ensure full checking here. */ 3166 processing_template_decl_sentinel proc (true); 3167 3168 /* We need to check access during satisfaction. */ 3169 deferring_access_check_sentinel acs (dk_no_deferred); 3170 3171 /* Constraints are unevaluated operands. */ 3172 cp_unevaluated u; 3173 3174 return satisfy_constraint_r (t, args, info); 3175 } 3176 3177 /* Return the normal form of the constraints on the placeholder 'auto' 3178 type T. */ 3179 3180 static tree 3181 normalize_placeholder_type_constraints (tree t, bool diag) 3182 { 3183 gcc_assert (is_auto (t)); 3184 tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t); 3185 if (!ci) 3186 return NULL_TREE; 3187 3188 tree constr = TREE_VALUE (ci); 3189 /* The TREE_PURPOSE contains the set of template parameters that were in 3190 scope for this placeholder type; use them as the initial template 3191 parameters for normalization. */ 3192 tree initial_parms = TREE_PURPOSE (ci); 3193 3194 /* The 'auto' itself is used as the first argument in its own constraints, 3195 and its level is one greater than its template depth. So in order to 3196 capture all used template parameters, we need to add an extra level of 3197 template parameters to the context; a dummy level suffices. */ 3198 initial_parms 3199 = tree_cons (size_int (initial_parms 3200 ? TMPL_PARMS_DEPTH (initial_parms) + 1 : 1), 3201 make_tree_vec (0), initial_parms); 3202 3203 norm_info info (diag ? tf_norm : tf_none); 3204 info.initial_parms = initial_parms; 3205 return normalize_constraint_expression (constr, info); 3206 } 3207 3208 /* Evaluate the constraints of T using ARGS, returning a satisfaction value. 3209 Here, T can be a concept-id, nested-requirement, placeholder 'auto', or 3210 requires-expression. */ 3211 3212 static tree 3213 satisfy_nondeclaration_constraints (tree t, tree args, sat_info info) 3214 { 3215 if (t == error_mark_node) 3216 return error_mark_node; 3217 3218 /* Handle REQUIRES_EXPR directly, bypassing satisfaction. */ 3219 if (TREE_CODE (t) == REQUIRES_EXPR) 3220 { 3221 auto ovr = make_temp_override (current_constraint_diagnosis_depth); 3222 if (info.noisy ()) 3223 ++current_constraint_diagnosis_depth; 3224 return tsubst_requires_expr (t, args, info); 3225 } 3226 3227 /* Get the normalized constraints. */ 3228 tree norm; 3229 if (concept_check_p (t)) 3230 { 3231 gcc_assert (!args); 3232 tree id = unpack_concept_check (t); 3233 args = TREE_OPERAND (id, 1); 3234 tree tmpl = get_concept_check_template (id); 3235 norm = normalize_concept_definition (tmpl, info.noisy ()); 3236 } 3237 else if (TREE_CODE (t) == NESTED_REQ) 3238 { 3239 norm_info ninfo (info.noisy () ? tf_norm : tf_none); 3240 /* The TREE_TYPE contains the set of template parameters that were in 3241 scope for this nested requirement; use them as the initial template 3242 parameters for normalization. */ 3243 ninfo.initial_parms = TREE_TYPE (t); 3244 norm = normalize_constraint_expression (TREE_OPERAND (t, 0), ninfo); 3245 } 3246 else if (is_auto (t)) 3247 { 3248 norm = normalize_placeholder_type_constraints (t, info.noisy ()); 3249 if (!norm) 3250 return boolean_true_node; 3251 } 3252 else 3253 gcc_unreachable (); 3254 3255 /* Perform satisfaction. */ 3256 return satisfy_normalized_constraints (norm, args, info); 3257 } 3258 3259 /* Evaluate the associated constraints of the template specialization T 3260 according to INFO, returning a satisfaction value. */ 3261 3262 static tree 3263 satisfy_declaration_constraints (tree t, sat_info info) 3264 { 3265 gcc_assert (DECL_P (t) && TREE_CODE (t) != TEMPLATE_DECL); 3266 const tree saved_t = t; 3267 3268 /* For inherited constructors, consider the original declaration; 3269 it has the correct template information attached. */ 3270 t = strip_inheriting_ctors (t); 3271 tree inh_ctor_targs = NULL_TREE; 3272 if (t != saved_t) 3273 if (tree ti = DECL_TEMPLATE_INFO (saved_t)) 3274 /* The inherited constructor points to an instantiation of a constructor 3275 template; remember its template arguments. */ 3276 inh_ctor_targs = TI_ARGS (ti); 3277 3278 /* Update the declaration for diagnostics. */ 3279 info.in_decl = t; 3280 3281 if (info.quiet ()) 3282 if (tree *result = hash_map_safe_get (decl_satisfied_cache, saved_t)) 3283 return *result; 3284 3285 tree args = NULL_TREE; 3286 if (tree ti = DECL_TEMPLATE_INFO (t)) 3287 { 3288 /* The initial parameter mapping is the complete set of 3289 template arguments substituted into the declaration. */ 3290 args = TI_ARGS (ti); 3291 if (inh_ctor_targs) 3292 args = add_outermost_template_args (args, inh_ctor_targs); 3293 } 3294 3295 if (regenerated_lambda_fn_p (t)) 3296 { 3297 /* The TI_ARGS of a regenerated lambda contains only the innermost 3298 set of template arguments. Augment this with the outer template 3299 arguments that were used to regenerate the lambda. */ 3300 gcc_assert (!args || TMPL_ARGS_DEPTH (args) == 1); 3301 tree regen_args = lambda_regenerating_args (t); 3302 if (args) 3303 args = add_to_template_args (regen_args, args); 3304 else 3305 args = regen_args; 3306 } 3307 3308 /* If the innermost arguments are dependent, or if the outer arguments 3309 are dependent and are needed by the constraints, we can't check 3310 satisfaction yet so pretend they're satisfied for now. */ 3311 if (uses_template_parms (args) 3312 && ((DECL_TEMPLATE_INFO (t) 3313 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)) 3314 && (TMPL_ARGS_DEPTH (args) == 1 3315 || uses_template_parms (INNERMOST_TEMPLATE_ARGS (args)))) 3316 || uses_outer_template_parms_in_constraints (t))) 3317 return boolean_true_node; 3318 3319 /* Get the normalized constraints. */ 3320 tree norm = get_normalized_constraints_from_decl (t, info.noisy ()); 3321 3322 unsigned ftc_count = vec_safe_length (failed_type_completions); 3323 3324 tree result = boolean_true_node; 3325 if (norm) 3326 { 3327 if (!push_tinst_level (t)) 3328 return result; 3329 push_to_top_level (); 3330 push_access_scope (t); 3331 result = satisfy_normalized_constraints (norm, args, info); 3332 pop_access_scope (t); 3333 pop_from_top_level (); 3334 pop_tinst_level (); 3335 } 3336 3337 /* True if this satisfaction is (heuristically) potentially unstable, i.e. 3338 if its result may depend on where in the program it was performed. */ 3339 bool maybe_unstable_satisfaction = false; 3340 if (ftc_count != vec_safe_length (failed_type_completions)) 3341 /* Type completion failure occurred during satisfaction. The satisfaction 3342 result may (or may not) materially depend on the completeness of a type, 3343 so we consider it potentially unstable. */ 3344 maybe_unstable_satisfaction = true; 3345 3346 if (maybe_unstable_satisfaction) 3347 /* Don't cache potentially unstable satisfaction, to allow satisfy_atom 3348 to check the stability the next time around. */; 3349 else if (info.quiet ()) 3350 hash_map_safe_put<hm_ggc> (decl_satisfied_cache, saved_t, result); 3351 3352 return result; 3353 } 3354 3355 /* Evaluate the associated constraints of the template T using ARGS as the 3356 innermost set of template arguments and according to INFO, returning a 3357 satisfaction value. */ 3358 3359 static tree 3360 satisfy_declaration_constraints (tree t, tree args, sat_info info) 3361 { 3362 tree orig_args = args; 3363 3364 /* Update the declaration for diagnostics. */ 3365 info.in_decl = t; 3366 3367 gcc_assert (TREE_CODE (t) == TEMPLATE_DECL); 3368 3369 if (regenerated_lambda_fn_p (t)) 3370 { 3371 /* As in the two-parameter version of this function. */ 3372 gcc_assert (TMPL_ARGS_DEPTH (args) == 1); 3373 tree lambda = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t)); 3374 tree outer_args = TI_ARGS (LAMBDA_EXPR_REGEN_INFO (lambda)); 3375 args = add_to_template_args (outer_args, args); 3376 } 3377 else 3378 args = add_outermost_template_args (t, args); 3379 3380 /* If the innermost arguments are dependent, or if the outer arguments 3381 are dependent and are needed by the constraints, we can't check 3382 satisfaction yet so pretend they're satisfied for now. */ 3383 if (uses_template_parms (args) 3384 && (TMPL_ARGS_DEPTH (args) == 1 3385 || uses_template_parms (INNERMOST_TEMPLATE_ARGS (args)) 3386 || uses_outer_template_parms_in_constraints (t))) 3387 return boolean_true_node; 3388 3389 tree result = boolean_true_node; 3390 if (tree norm = get_normalized_constraints_from_decl (t, info.noisy ())) 3391 { 3392 if (!push_tinst_level (t, orig_args)) 3393 return result; 3394 tree pattern = DECL_TEMPLATE_RESULT (t); 3395 push_to_top_level (); 3396 push_access_scope (pattern); 3397 result = satisfy_normalized_constraints (norm, args, info); 3398 pop_access_scope (pattern); 3399 pop_from_top_level (); 3400 pop_tinst_level (); 3401 } 3402 3403 return result; 3404 } 3405 3406 /* A wrapper around satisfy_declaration_constraints and 3407 satisfy_nondeclaration_constraints which additionally replays 3408 quiet ill-formed satisfaction noisily, so that ill-formed 3409 satisfaction always gets diagnosed. */ 3410 3411 static tree 3412 constraint_satisfaction_value (tree t, tree args, sat_info info) 3413 { 3414 tree r; 3415 if (DECL_P (t)) 3416 { 3417 if (args) 3418 r = satisfy_declaration_constraints (t, args, info); 3419 else 3420 r = satisfy_declaration_constraints (t, info); 3421 } 3422 else 3423 r = satisfy_nondeclaration_constraints (t, args, info); 3424 if (r == error_mark_node && info.quiet () 3425 && !(DECL_P (t) && warning_suppressed_p (t))) 3426 { 3427 /* Replay the error noisily. */ 3428 sat_info noisy (tf_warning_or_error, info.in_decl); 3429 constraint_satisfaction_value (t, args, noisy); 3430 if (DECL_P (t) && !args) 3431 /* Avoid giving these errors again. */ 3432 suppress_warning (t); 3433 } 3434 return r; 3435 } 3436 3437 /* True iff the result of satisfying T using ARGS is BOOLEAN_TRUE_NODE 3438 and false otherwise, even in the case of errors. 3439 3440 Here, T can be: 3441 - a template declaration 3442 - a template specialization (in which case ARGS must be empty) 3443 - a concept-id (in which case ARGS must be empty) 3444 - a nested-requirement 3445 - a placeholder 'auto' 3446 - a requires-expression. */ 3447 3448 bool 3449 constraints_satisfied_p (tree t, tree args/*= NULL_TREE */) 3450 { 3451 if (!flag_concepts) 3452 return true; 3453 3454 sat_info quiet (tf_none, NULL_TREE); 3455 return constraint_satisfaction_value (t, args, quiet) == boolean_true_node; 3456 } 3457 3458 /* Evaluate a concept check of the form C<ARGS>. This is only used for the 3459 evaluation of template-ids as id-expressions. */ 3460 3461 tree 3462 evaluate_concept_check (tree check) 3463 { 3464 if (check == error_mark_node) 3465 return error_mark_node; 3466 3467 gcc_assert (concept_check_p (check)); 3468 3469 /* Check for satisfaction without diagnostics. */ 3470 sat_info quiet (tf_none, NULL_TREE); 3471 return constraint_satisfaction_value (check, /*args=*/NULL_TREE, quiet); 3472 } 3473 3474 /* Evaluate the requires-expression T, returning either boolean_true_node 3475 or boolean_false_node. This is used during folding and constexpr 3476 evaluation. */ 3477 3478 tree 3479 evaluate_requires_expr (tree t) 3480 { 3481 gcc_assert (TREE_CODE (t) == REQUIRES_EXPR); 3482 sat_info quiet (tf_none, NULL_TREE); 3483 return constraint_satisfaction_value (t, /*args=*/NULL_TREE, quiet); 3484 } 3485 3486 /*--------------------------------------------------------------------------- 3487 Semantic analysis of requires-expressions 3488 ---------------------------------------------------------------------------*/ 3489 3490 /* Finish a requires expression for the given PARMS (possibly 3491 null) and the non-empty sequence of requirements. */ 3492 3493 tree 3494 finish_requires_expr (location_t loc, tree parms, tree reqs) 3495 { 3496 /* Build the node. */ 3497 tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs, NULL_TREE); 3498 TREE_SIDE_EFFECTS (r) = false; 3499 TREE_CONSTANT (r) = true; 3500 SET_EXPR_LOCATION (r, loc); 3501 return r; 3502 } 3503 3504 /* Construct a requirement for the validity of EXPR. */ 3505 3506 tree 3507 finish_simple_requirement (location_t loc, tree expr) 3508 { 3509 tree r = build_nt (SIMPLE_REQ, expr); 3510 SET_EXPR_LOCATION (r, loc); 3511 return r; 3512 } 3513 3514 /* Construct a requirement for the validity of TYPE. */ 3515 3516 tree 3517 finish_type_requirement (location_t loc, tree type) 3518 { 3519 tree r = build_nt (TYPE_REQ, type); 3520 SET_EXPR_LOCATION (r, loc); 3521 return r; 3522 } 3523 3524 /* Construct a requirement for the validity of EXPR, along with 3525 its properties. if TYPE is non-null, then it specifies either 3526 an implicit conversion or argument deduction constraint, 3527 depending on whether any placeholders occur in the type name. 3528 NOEXCEPT_P is true iff the noexcept keyword was specified. */ 3529 3530 tree 3531 finish_compound_requirement (location_t loc, tree expr, tree type, bool noexcept_p) 3532 { 3533 tree req = build_nt (COMPOUND_REQ, expr, type); 3534 SET_EXPR_LOCATION (req, loc); 3535 COMPOUND_REQ_NOEXCEPT_P (req) = noexcept_p; 3536 return req; 3537 } 3538 3539 /* Finish a nested requirement. */ 3540 3541 tree 3542 finish_nested_requirement (location_t loc, tree expr) 3543 { 3544 /* Build the requirement, saving the set of in-scope template 3545 parameters as its type. */ 3546 tree r = build1 (NESTED_REQ, current_template_parms, expr); 3547 SET_EXPR_LOCATION (r, loc); 3548 return r; 3549 } 3550 3551 /* Check that FN satisfies the structural requirements of a 3552 function concept definition. */ 3553 tree 3554 check_function_concept (tree fn) 3555 { 3556 /* Check that the function is comprised of only a return statement. */ 3557 tree body = DECL_SAVED_TREE (fn); 3558 if (TREE_CODE (body) == BIND_EXPR) 3559 body = BIND_EXPR_BODY (body); 3560 3561 /* Sometimes a function call results in the creation of clean up 3562 points. Allow these to be preserved in the body of the 3563 constraint, as we might actually need them for some constexpr 3564 evaluations. */ 3565 if (TREE_CODE (body) == CLEANUP_POINT_EXPR) 3566 body = TREE_OPERAND (body, 0); 3567 3568 /* Check that the definition is written correctly. */ 3569 if (TREE_CODE (body) != RETURN_EXPR) 3570 { 3571 location_t loc = DECL_SOURCE_LOCATION (fn); 3572 if (TREE_CODE (body) == STATEMENT_LIST && !STATEMENT_LIST_HEAD (body)) 3573 { 3574 if (seen_error ()) 3575 /* The definition was probably erroneous, not empty. */; 3576 else 3577 error_at (loc, "definition of concept %qD is empty", fn); 3578 } 3579 else 3580 error_at (loc, "definition of concept %qD has multiple statements", fn); 3581 } 3582 3583 return NULL_TREE; 3584 } 3585 3586 /*--------------------------------------------------------------------------- 3587 Equivalence of constraints 3588 ---------------------------------------------------------------------------*/ 3589 3590 /* Returns true when A and B are equivalent constraints. */ 3591 bool 3592 equivalent_constraints (tree a, tree b) 3593 { 3594 gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO); 3595 gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO); 3596 return cp_tree_equal (a, b); 3597 } 3598 3599 /* Returns true if the template declarations A and B have equivalent 3600 constraints. This is the case when A's constraints subsume B's and 3601 when B's also constrain A's. */ 3602 bool 3603 equivalently_constrained (tree d1, tree d2) 3604 { 3605 gcc_assert (TREE_CODE (d1) == TREE_CODE (d2)); 3606 return equivalent_constraints (get_constraints (d1), get_constraints (d2)); 3607 } 3608 3609 /*--------------------------------------------------------------------------- 3610 Partial ordering of constraints 3611 ---------------------------------------------------------------------------*/ 3612 3613 /* Returns true when the constraints in CI strictly subsume 3614 the associated constraints of TMPL. */ 3615 3616 bool 3617 strictly_subsumes (tree ci, tree tmpl) 3618 { 3619 tree n1 = get_normalized_constraints_from_info (ci, NULL_TREE); 3620 tree n2 = get_normalized_constraints_from_decl (tmpl); 3621 3622 return subsumes (n1, n2) && !subsumes (n2, n1); 3623 } 3624 3625 /* Returns true when the template template parameter constraints in CI 3626 subsume the associated constraints of the template template argument 3627 TMPL. */ 3628 3629 bool 3630 ttp_subsumes (tree ci, tree tmpl) 3631 { 3632 tree n1 = get_normalized_constraints_from_info (ci, tmpl); 3633 tree n2 = get_normalized_constraints_from_decl (tmpl); 3634 3635 return subsumes (n1, n2); 3636 } 3637 3638 /* Determines which of the declarations, A or B, is more constrained. 3639 That is, which declaration's constraints subsume but are not subsumed 3640 by the other's? 3641 3642 Returns 1 if D1 is more constrained than D2, -1 if D2 is more constrained 3643 than D1, and 0 otherwise. */ 3644 3645 int 3646 more_constrained (tree d1, tree d2) 3647 { 3648 tree n1 = get_normalized_constraints_from_decl (d1); 3649 tree n2 = get_normalized_constraints_from_decl (d2); 3650 3651 int winner = 0; 3652 if (subsumes (n1, n2)) 3653 ++winner; 3654 if (subsumes (n2, n1)) 3655 --winner; 3656 return winner; 3657 } 3658 3659 /* Return whether D1 is at least as constrained as D2. */ 3660 3661 bool 3662 at_least_as_constrained (tree d1, tree d2) 3663 { 3664 tree n1 = get_normalized_constraints_from_decl (d1); 3665 tree n2 = get_normalized_constraints_from_decl (d2); 3666 3667 return subsumes (n1, n2); 3668 } 3669 3670 /*--------------------------------------------------------------------------- 3671 Constraint diagnostics 3672 ---------------------------------------------------------------------------*/ 3673 3674 /* Returns the best location to diagnose a constraint error. */ 3675 3676 static location_t 3677 get_constraint_error_location (tree t) 3678 { 3679 if (location_t loc = cp_expr_location (t)) 3680 return loc; 3681 3682 /* If we have a specific location give it. */ 3683 tree expr = CONSTR_EXPR (t); 3684 if (location_t loc = cp_expr_location (expr)) 3685 return loc; 3686 3687 /* If the constraint is normalized from a requires-clause, give 3688 the location as that of the constrained declaration. */ 3689 tree cxt = CONSTR_CONTEXT (t); 3690 tree src = cxt ? TREE_VALUE (cxt) : NULL_TREE; 3691 if (!src) 3692 /* TODO: This only happens for constrained non-template declarations. */ 3693 ; 3694 else if (DECL_P (src)) 3695 return DECL_SOURCE_LOCATION (src); 3696 /* Otherwise, give the location as the defining concept. */ 3697 else if (concept_check_p (src)) 3698 { 3699 tree id = unpack_concept_check (src); 3700 tree tmpl = TREE_OPERAND (id, 0); 3701 if (OVL_P (tmpl)) 3702 tmpl = OVL_FIRST (tmpl); 3703 return DECL_SOURCE_LOCATION (tmpl); 3704 } 3705 3706 return input_location; 3707 } 3708 3709 /* Emit a diagnostic for a failed trait. */ 3710 3711 static void 3712 diagnose_trait_expr (tree expr, tree args) 3713 { 3714 location_t loc = cp_expr_location (expr); 3715 3716 /* Build a "fake" version of the instantiated trait, so we can 3717 get the instantiated types from result. */ 3718 ++processing_template_decl; 3719 expr = tsubst_expr (expr, args, tf_none, NULL_TREE); 3720 --processing_template_decl; 3721 3722 tree t1 = TRAIT_EXPR_TYPE1 (expr); 3723 tree t2 = TRAIT_EXPR_TYPE2 (expr); 3724 if (t2 && TREE_CODE (t2) == TREE_VEC) 3725 { 3726 /* Convert the TREE_VEC of arguments into a TREE_LIST, since we can't 3727 directly print a TREE_VEC but we can a TREE_LIST via the E format 3728 specifier. */ 3729 tree list = NULL_TREE; 3730 for (tree t : tree_vec_range (t2)) 3731 list = tree_cons (NULL_TREE, t, list); 3732 t2 = nreverse (list); 3733 } 3734 switch (TRAIT_EXPR_KIND (expr)) 3735 { 3736 case CPTK_HAS_NOTHROW_ASSIGN: 3737 inform (loc, " %qT is not nothrow copy assignable", t1); 3738 break; 3739 case CPTK_HAS_NOTHROW_CONSTRUCTOR: 3740 inform (loc, " %qT is not nothrow default constructible", t1); 3741 break; 3742 case CPTK_HAS_NOTHROW_COPY: 3743 inform (loc, " %qT is not nothrow copy constructible", t1); 3744 break; 3745 case CPTK_HAS_TRIVIAL_ASSIGN: 3746 inform (loc, " %qT is not trivially copy assignable", t1); 3747 break; 3748 case CPTK_HAS_TRIVIAL_CONSTRUCTOR: 3749 inform (loc, " %qT is not trivially default constructible", t1); 3750 break; 3751 case CPTK_HAS_TRIVIAL_COPY: 3752 inform (loc, " %qT is not trivially copy constructible", t1); 3753 break; 3754 case CPTK_HAS_TRIVIAL_DESTRUCTOR: 3755 inform (loc, " %qT is not trivially destructible", t1); 3756 break; 3757 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS: 3758 inform (loc, " %qT does not have unique object representations", t1); 3759 break; 3760 case CPTK_HAS_VIRTUAL_DESTRUCTOR: 3761 inform (loc, " %qT does not have a virtual destructor", t1); 3762 break; 3763 case CPTK_IS_ABSTRACT: 3764 inform (loc, " %qT is not an abstract class", t1); 3765 break; 3766 case CPTK_IS_AGGREGATE: 3767 inform (loc, " %qT is not an aggregate", t1); 3768 break; 3769 case CPTK_IS_ARRAY: 3770 inform (loc, " %qT is not an array", t1); 3771 break; 3772 case CPTK_IS_ASSIGNABLE: 3773 inform (loc, " %qT is not assignable from %qT", t1, t2); 3774 break; 3775 case CPTK_IS_BASE_OF: 3776 inform (loc, " %qT is not a base of %qT", t1, t2); 3777 break; 3778 case CPTK_IS_BOUNDED_ARRAY: 3779 inform (loc, " %qT is not a bounded array", t1); 3780 break; 3781 case CPTK_IS_CLASS: 3782 inform (loc, " %qT is not a class", t1); 3783 break; 3784 case CPTK_IS_CONSTRUCTIBLE: 3785 if (!t2) 3786 inform (loc, " %qT is not default constructible", t1); 3787 else 3788 inform (loc, " %qT is not constructible from %qE", t1, t2); 3789 break; 3790 case CPTK_IS_CONVERTIBLE: 3791 inform (loc, " %qT is not convertible from %qE", t2, t1); 3792 break; 3793 case CPTK_IS_EMPTY: 3794 inform (loc, " %qT is not an empty class", t1); 3795 break; 3796 case CPTK_IS_ENUM: 3797 inform (loc, " %qT is not an enum", t1); 3798 break; 3799 case CPTK_IS_FINAL: 3800 inform (loc, " %qT is not a final class", t1); 3801 break; 3802 case CPTK_IS_FUNCTION: 3803 inform (loc, " %qT is not a function", t1); 3804 break; 3805 case CPTK_IS_LAYOUT_COMPATIBLE: 3806 inform (loc, " %qT is not layout compatible with %qT", t1, t2); 3807 break; 3808 case CPTK_IS_LITERAL_TYPE: 3809 inform (loc, " %qT is not a literal type", t1); 3810 break; 3811 case CPTK_IS_MEMBER_FUNCTION_POINTER: 3812 inform (loc, " %qT is not a member function pointer", t1); 3813 break; 3814 case CPTK_IS_MEMBER_OBJECT_POINTER: 3815 inform (loc, " %qT is not a member object pointer", t1); 3816 break; 3817 case CPTK_IS_MEMBER_POINTER: 3818 inform (loc, " %qT is not a member pointer", t1); 3819 break; 3820 case CPTK_IS_NOTHROW_ASSIGNABLE: 3821 inform (loc, " %qT is not nothrow assignable from %qT", t1, t2); 3822 break; 3823 case CPTK_IS_NOTHROW_CONSTRUCTIBLE: 3824 if (!t2) 3825 inform (loc, " %qT is not nothrow default constructible", t1); 3826 else 3827 inform (loc, " %qT is not nothrow constructible from %qE", t1, t2); 3828 break; 3829 case CPTK_IS_NOTHROW_CONVERTIBLE: 3830 inform (loc, " %qT is not nothrow convertible from %qE", t2, t1); 3831 break; 3832 case CPTK_IS_OBJECT: 3833 inform (loc, " %qT is not an object type", t1); 3834 break; 3835 case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF: 3836 inform (loc, " %qT is not pointer-interconvertible base of %qT", 3837 t1, t2); 3838 break; 3839 case CPTK_IS_POD: 3840 inform (loc, " %qT is not a POD type", t1); 3841 break; 3842 case CPTK_IS_POLYMORPHIC: 3843 inform (loc, " %qT is not a polymorphic type", t1); 3844 break; 3845 case CPTK_IS_REFERENCE: 3846 inform (loc, " %qT is not a reference", t1); 3847 break; 3848 case CPTK_IS_SAME: 3849 inform (loc, " %qT is not the same as %qT", t1, t2); 3850 break; 3851 case CPTK_IS_SCOPED_ENUM: 3852 inform (loc, " %qT is not a scoped enum", t1); 3853 break; 3854 case CPTK_IS_STD_LAYOUT: 3855 inform (loc, " %qT is not an standard layout type", t1); 3856 break; 3857 case CPTK_IS_TRIVIAL: 3858 inform (loc, " %qT is not a trivial type", t1); 3859 break; 3860 case CPTK_IS_TRIVIALLY_ASSIGNABLE: 3861 inform (loc, " %qT is not trivially assignable from %qT", t1, t2); 3862 break; 3863 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE: 3864 if (!t2) 3865 inform (loc, " %qT is not trivially default constructible", t1); 3866 else 3867 inform (loc, " %qT is not trivially constructible from %qE", t1, t2); 3868 break; 3869 case CPTK_IS_TRIVIALLY_COPYABLE: 3870 inform (loc, " %qT is not trivially copyable", t1); 3871 break; 3872 case CPTK_IS_UNION: 3873 inform (loc, " %qT is not a union", t1); 3874 break; 3875 case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY: 3876 inform (loc, " %qT is not a reference that binds to a temporary " 3877 "object of type %qT (direct-initialization)", t1, t2); 3878 break; 3879 case CPTK_REF_CONVERTS_FROM_TEMPORARY: 3880 inform (loc, " %qT is not a reference that binds to a temporary " 3881 "object of type %qT (copy-initialization)", t1, t2); 3882 break; 3883 case CPTK_IS_DEDUCIBLE: 3884 inform (loc, " %qD is not deducible from %qT", t1, t2); 3885 break; 3886 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \ 3887 case CPTK_##CODE: 3888 #include "cp-trait.def" 3889 #undef DEFTRAIT_TYPE 3890 /* Type-yielding traits aren't expressions. */ 3891 gcc_unreachable (); 3892 /* We deliberately omit the default case so that when adding a new 3893 trait we'll get reminded (by way of a warning) to handle it here. */ 3894 } 3895 } 3896 3897 /* Diagnose a substitution failure in the atomic constraint T using ARGS. */ 3898 3899 static void 3900 diagnose_atomic_constraint (tree t, tree args, tree result, sat_info info) 3901 { 3902 /* If the constraint is already ill-formed, we've previously diagnosed 3903 the reason. We should still say why the constraints aren't satisfied. */ 3904 if (t == error_mark_node) 3905 { 3906 location_t loc; 3907 if (info.in_decl) 3908 loc = DECL_SOURCE_LOCATION (info.in_decl); 3909 else 3910 loc = input_location; 3911 inform (loc, "invalid constraints"); 3912 return; 3913 } 3914 3915 location_t loc = get_constraint_error_location (t); 3916 iloc_sentinel loc_s (loc); 3917 3918 /* Generate better diagnostics for certain kinds of expressions. */ 3919 tree expr = ATOMIC_CONSTR_EXPR (t); 3920 STRIP_ANY_LOCATION_WRAPPER (expr); 3921 switch (TREE_CODE (expr)) 3922 { 3923 case TRAIT_EXPR: 3924 diagnose_trait_expr (expr, args); 3925 break; 3926 case REQUIRES_EXPR: 3927 gcc_checking_assert (info.diagnose_unsatisfaction_p ()); 3928 /* Clear in_decl before replaying the substitution to avoid emitting 3929 seemingly unhelpful "in declaration ..." notes that follow some 3930 substitution failure error messages. */ 3931 info.in_decl = NULL_TREE; 3932 tsubst_requires_expr (expr, args, info); 3933 break; 3934 default: 3935 if (!same_type_p (TREE_TYPE (result), boolean_type_node)) 3936 error_at (loc, "constraint %qE has type %qT, not %<bool%>", 3937 t, TREE_TYPE (result)); 3938 else 3939 inform (loc, "the expression %qE evaluated to %<false%>", t); 3940 } 3941 } 3942 3943 GTY(()) tree current_failed_constraint; 3944 3945 diagnosing_failed_constraint:: 3946 diagnosing_failed_constraint (tree t, tree args, bool diag) 3947 : diagnosing_error (diag) 3948 { 3949 if (diagnosing_error) 3950 { 3951 current_failed_constraint 3952 = tree_cons (args, t, current_failed_constraint); 3953 ++current_constraint_diagnosis_depth; 3954 } 3955 } 3956 3957 diagnosing_failed_constraint:: 3958 ~diagnosing_failed_constraint () 3959 { 3960 if (diagnosing_error) 3961 { 3962 --current_constraint_diagnosis_depth; 3963 if (current_failed_constraint) 3964 current_failed_constraint = TREE_CHAIN (current_failed_constraint); 3965 } 3966 3967 } 3968 3969 /* Whether we are allowed to replay an error that underlies a constraint failure 3970 at the current diagnosis depth. */ 3971 3972 bool 3973 diagnosing_failed_constraint::replay_errors_p () 3974 { 3975 if (current_constraint_diagnosis_depth >= concepts_diagnostics_max_depth) 3976 { 3977 concepts_diagnostics_max_depth_exceeded_p = true; 3978 return false; 3979 } 3980 else 3981 return true; 3982 } 3983 3984 /* Emit diagnostics detailing the failure ARGS to satisfy the constraints 3985 of T. Here, T and ARGS are as in constraints_satisfied_p. */ 3986 3987 void 3988 diagnose_constraints (location_t loc, tree t, tree args) 3989 { 3990 inform (loc, "constraints not satisfied"); 3991 3992 if (concepts_diagnostics_max_depth == 0) 3993 return; 3994 3995 /* Replay satisfaction, but diagnose unsatisfaction. */ 3996 sat_info noisy (tf_warning_or_error, NULL_TREE, /*diag_unsat=*/true); 3997 constraint_satisfaction_value (t, args, noisy); 3998 3999 static bool suggested_p; 4000 if (concepts_diagnostics_max_depth_exceeded_p 4001 && current_constraint_diagnosis_depth == 0 4002 && !suggested_p) 4003 { 4004 inform (UNKNOWN_LOCATION, 4005 "set %qs to at least %d for more detail", 4006 "-fconcepts-diagnostics-depth=", 4007 concepts_diagnostics_max_depth + 1); 4008 suggested_p = true; 4009 } 4010 } 4011 4012 #include "gt-cp-constraint.h" 4013