Line data Source code
1 : /* Processing rules for constraints.
2 : Copyright (C) 2013-2026 Free Software Foundation, Inc.
3 : Contributed by Andrew Sutton (andrew.n.sutton@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 215077744 : processing_constraint_expression_sentinel::
56 : processing_constraint_expression_sentinel ()
57 : {
58 215077744 : ++scope_chain->x_processing_constraint;
59 215077744 : }
60 :
61 215077744 : processing_constraint_expression_sentinel::
62 : ~processing_constraint_expression_sentinel ()
63 : {
64 215077744 : --scope_chain->x_processing_constraint;
65 215077744 : }
66 :
67 : bool
68 9352737 : processing_constraint_expression_p ()
69 : {
70 9352737 : 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 1162467896 : subst_info (tsubst_flags_t cmp, tree in)
82 1162467896 : : complain (cmp), in_decl (in)
83 : { }
84 :
85 : /* True if we should not diagnose errors. */
86 2630611460 : bool quiet () const
87 : {
88 2630611460 : return !(complain & tf_warning_or_error);
89 : }
90 :
91 : /* True if we should diagnose errors. */
92 1922362387 : bool noisy () const
93 : {
94 1949722345 : 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 1084324145 : sat_info (tsubst_flags_t cmp, tree in, bool diag_unsat = false)
132 1084324145 : : subst_info (cmp, in), diagnose_unsatisfaction (diag_unsat)
133 : {
134 1084324145 : if (diagnose_unsatisfaction_p ())
135 0 : gcc_checking_assert (noisy ());
136 12618018 : }
137 :
138 : /* True if we should diagnose the cause of satisfaction failure.
139 : Implies noisy(). */
140 : bool
141 1093548368 : diagnose_unsatisfaction_p () const
142 : {
143 12618018 : 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 31870886 : known_non_bool_p (tree t)
156 : {
157 31870886 : return (t && !WILDCARD_TYPE_P (t) && TREE_CODE (t) != BOOLEAN_TYPE);
158 : }
159 :
160 : static bool
161 31870886 : check_constraint_atom (cp_expr expr)
162 : {
163 31870886 : if (known_non_bool_p (TREE_TYPE (expr)))
164 : {
165 10 : error_at (expr.get_location (),
166 : "constraint expression does not have type %<bool%>");
167 10 : return false;
168 : }
169 :
170 : return true;
171 : }
172 :
173 : static bool
174 9346606 : check_constraint_operands (location_t, cp_expr lhs, cp_expr rhs)
175 : {
176 9346606 : return check_constraint_atom (lhs) && check_constraint_atom (rhs);
177 : }
178 :
179 : /* Validate the semantic properties of the constraint expression. */
180 :
181 : static cp_expr
182 9346612 : finish_constraint_binary_op (location_t loc,
183 : tree_code code,
184 : cp_expr lhs,
185 : cp_expr rhs)
186 : {
187 9346612 : gcc_assert (processing_constraint_expression_p ());
188 9346612 : if (lhs == error_mark_node || rhs == error_mark_node)
189 6 : return error_mark_node;
190 9346606 : if (!check_constraint_operands (loc, lhs, rhs))
191 0 : return error_mark_node;
192 9346606 : cp_expr expr
193 9346606 : = build_min_nt_loc (loc, code, lhs.get_value (), rhs.get_value ());
194 9346606 : expr.set_range (lhs.get_start (), rhs.get_finish ());
195 9346606 : return expr;
196 : }
197 :
198 : cp_expr
199 472163 : finish_constraint_or_expr (location_t loc, cp_expr lhs, cp_expr rhs)
200 : {
201 472163 : return finish_constraint_binary_op (loc, TRUTH_ORIF_EXPR, lhs, rhs);
202 : }
203 :
204 : cp_expr
205 8874449 : finish_constraint_and_expr (location_t loc, cp_expr lhs, cp_expr rhs)
206 : {
207 8874449 : return finish_constraint_binary_op (loc, TRUTH_ANDIF_EXPR, lhs, rhs);
208 : }
209 :
210 : cp_expr
211 13177706 : finish_constraint_primary_expr (cp_expr expr)
212 : {
213 13177706 : if (expr == error_mark_node)
214 32 : return error_mark_node;
215 13177674 : if (!check_constraint_atom (expr))
216 10 : return cp_expr (error_mark_node, expr.get_location ());
217 13177664 : return expr;
218 : }
219 :
220 : /* Combine two constraint-expressions with a logical-and. */
221 :
222 : tree
223 198151864 : combine_constraint_expressions (tree lhs, tree rhs)
224 : {
225 198151864 : processing_constraint_expression_sentinel pce;
226 198151864 : if (!lhs)
227 : return rhs;
228 27233963 : if (!rhs)
229 : return lhs;
230 : /* Use UNKNOWN_LOCATION so write_template_args can tell the difference
231 : between this and a && the user wrote. */
232 5564315 : return finish_constraint_and_expr (UNKNOWN_LOCATION, lhs, rhs);
233 198151864 : }
234 :
235 : /* Extract the TEMPLATE_DECL from a concept check. */
236 :
237 : tree
238 24229775 : get_concept_check_template (tree t)
239 : {
240 24229775 : gcc_assert (concept_check_p (t));
241 24229775 : return TREE_OPERAND (t, 0);
242 : }
243 :
244 : /*---------------------------------------------------------------------------
245 : Expansion of concept definitions
246 : ---------------------------------------------------------------------------*/
247 :
248 : /* Returns the definition of a concept. */
249 :
250 : static tree
251 21717976 : get_concept_definition (tree decl)
252 : {
253 21717976 : gcc_assert (TREE_CODE (decl) == CONCEPT_DECL);
254 21717976 : return DECL_INITIAL (decl);
255 : }
256 :
257 : /*---------------------------------------------------------------------------
258 : Normalization of expressions
259 :
260 : This set of functions will transform an expression into a constraint
261 : in a sequence of steps.
262 : ---------------------------------------------------------------------------*/
263 :
264 : void
265 0 : debug_parameter_mapping (tree map)
266 : {
267 0 : for (tree p = map; p; p = TREE_CHAIN (p))
268 : {
269 0 : tree parm = TREE_VALUE (p);
270 0 : tree arg = TREE_PURPOSE (p);
271 0 : if (TYPE_P (parm))
272 0 : verbatim ("MAP %qD TO %qT", TEMPLATE_TYPE_DECL (parm), arg);
273 : else
274 0 : verbatim ("MAP %qD TO %qE", TEMPLATE_PARM_DECL (parm), arg);
275 : // debug_tree (parm);
276 : // debug_tree (arg);
277 : }
278 0 : }
279 :
280 : void
281 0 : debug_argument_list (tree args)
282 : {
283 0 : for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
284 : {
285 0 : tree arg = TREE_VEC_ELT (args, i);
286 0 : if (TYPE_P (arg))
287 0 : verbatim ("argument %qT", arg);
288 : else
289 0 : verbatim ("argument %qE", arg);
290 : }
291 0 : }
292 :
293 : /* Associate each parameter in PARMS with its corresponding template
294 : argument in ARGS. */
295 :
296 : static tree
297 24599664 : map_arguments (tree parms, tree args)
298 : {
299 67393214 : for (tree p = parms; p; p = TREE_CHAIN (p))
300 42793550 : if (args)
301 : {
302 36584995 : int level;
303 36584995 : int index;
304 36584995 : template_parm_level_and_index (TREE_VALUE (p), &level, &index);
305 36584995 : TREE_PURPOSE (p) = TMPL_ARG (args, level, index);
306 : }
307 : else
308 6208555 : TREE_PURPOSE (p) = template_parm_to_arg (p);
309 :
310 24599664 : return parms;
311 : }
312 :
313 : /* Build the parameter mapping for EXPR using ARGS, where CTX_PARMS
314 : are the template parameters in scope for EXPR. */
315 :
316 : static tree
317 24599664 : build_parameter_mapping (tree expr, tree args, tree ctx_parms)
318 : {
319 24599664 : tree parms = find_template_parameters (expr, ctx_parms);
320 24599664 : tree map = map_arguments (parms, args);
321 24599664 : return map;
322 : }
323 :
324 : /* True if the parameter mappings of two atomic constraints formed
325 : from the same expression are equivalent. */
326 :
327 : static bool
328 57719045 : parameter_mapping_equivalent_p (tree t1, tree t2)
329 : {
330 57719045 : tree map1 = ATOMIC_CONSTR_MAP (t1);
331 57719045 : tree map2 = ATOMIC_CONSTR_MAP (t2);
332 120995032 : while (map1 && map2)
333 : {
334 87674392 : gcc_checking_assert (TREE_VALUE (map1) == TREE_VALUE (map2));
335 87674392 : tree arg1 = TREE_PURPOSE (map1);
336 87674392 : tree arg2 = TREE_PURPOSE (map2);
337 87674392 : if (!template_args_equal (arg1, arg2))
338 : return false;
339 63275987 : map1 = TREE_CHAIN (map1);
340 63275987 : map2 = TREE_CHAIN (map2);
341 : }
342 33320640 : gcc_checking_assert (!map1 && !map2);
343 : return true;
344 : }
345 :
346 : /* Provides additional context for normalization. */
347 :
348 : struct norm_info : subst_info
349 : {
350 9378687 : explicit norm_info (bool diag)
351 9378687 : : norm_info (NULL_TREE, diag)
352 : {}
353 :
354 : /* Construct a top-level context for DECL. */
355 :
356 12991926 : norm_info (tree in_decl, bool diag)
357 12991926 : : subst_info (tf_warning_or_error|tf_partial, in_decl),
358 12991926 : generate_diagnostics (diag)
359 : {
360 3613239 : if (in_decl)
361 : {
362 2924469 : initial_parms = DECL_TEMPLATE_PARMS (in_decl);
363 2924469 : if (generate_diagnostics)
364 1352 : context = build_tree_list (NULL_TREE, in_decl);
365 : }
366 : else
367 688770 : initial_parms = current_template_parms;
368 3613239 : }
369 :
370 21376875 : void update_context (tree expr, tree args)
371 : {
372 21376875 : if (generate_diagnostics)
373 : {
374 3435 : tree map = build_parameter_mapping (expr, args, ctx_parms ());
375 3435 : context = tree_cons (map, expr, context);
376 : }
377 21376875 : in_decl = get_concept_check_template (expr);
378 21376875 : }
379 :
380 : /* Returns the template parameters that are in scope for the current
381 : normalization context. */
382 :
383 24599664 : tree ctx_parms ()
384 : {
385 24599664 : if (in_decl)
386 24120268 : return DECL_TEMPLATE_PARMS (in_decl);
387 : else
388 479396 : return initial_parms;
389 : }
390 :
391 : /* Provides information about the source of a constraint. This is a
392 : TREE_LIST whose VALUE is either a concept check or a constrained
393 : declaration. The PURPOSE, for concept checks is a parameter mapping
394 : for that check. */
395 :
396 : tree context = NULL_TREE;
397 :
398 : /* The declaration whose constraints we're normalizing. The targets
399 : of the parameter mapping of each atom will be in terms of the
400 : template parameters of ORIG_DECL. */
401 :
402 : tree initial_parms = NULL_TREE;
403 :
404 : /* Whether to build diagnostic information during normalization. */
405 :
406 : bool generate_diagnostics;
407 : };
408 :
409 : static tree normalize_expression (tree, tree, norm_info);
410 :
411 : /* Transform a logical-or or logical-and expression into either
412 : a conjunction or disjunction. */
413 :
414 : static tree
415 24467610 : normalize_logical_operation (tree t, tree args, tree_code c, norm_info info)
416 : {
417 24467610 : tree t0 = normalize_expression (TREE_OPERAND (t, 0), args, info);
418 24467610 : tree t1 = normalize_expression (TREE_OPERAND (t, 1), args, info);
419 :
420 : /* Build a new info object for the constraint. */
421 24467610 : tree ci = (info.generate_diagnostics
422 24467610 : ? build_tree_list (t, info.context) : NULL_TREE);
423 :
424 24467610 : return build2 (c, ci, t0, t1);
425 : }
426 :
427 : /* Data types and hash functions for caching the normal form of a concept-id.
428 : This essentially memoizes calls to normalize_concept_check. */
429 :
430 : struct GTY((for_user)) norm_entry
431 : {
432 : /* The CONCEPT_DECL of the concept-id. */
433 : tree tmpl;
434 : /* The arguments of the concept-id. */
435 : tree args;
436 : /* The normal form of the concept-id. */
437 : tree norm;
438 : };
439 :
440 : struct norm_hasher : ggc_ptr_hash<norm_entry>
441 : {
442 207980579 : static hashval_t hash (norm_entry *e)
443 : {
444 207980579 : ++comparing_specializations;
445 207980579 : hashval_t val = iterative_hash_template_arg (e->tmpl, 0);
446 207980579 : val = iterative_hash_template_arg (e->args, val);
447 207980579 : --comparing_specializations;
448 207980579 : return val;
449 : }
450 :
451 232010710 : static bool equal (norm_entry *e1, norm_entry *e2)
452 : {
453 232010710 : ++comparing_specializations;
454 232010710 : bool eq = e1->tmpl == e2->tmpl
455 232010710 : && template_args_equal (e1->args, e2->args);
456 232010710 : --comparing_specializations;
457 232010710 : return eq;
458 : }
459 : };
460 :
461 : static GTY((deletable)) hash_table<norm_hasher> *norm_cache;
462 :
463 : /* Normalize the concept check CHECK where ARGS are the
464 : arguments to be substituted into CHECK's arguments. */
465 :
466 : static tree
467 26549838 : normalize_concept_check (tree check, tree args, norm_info info)
468 : {
469 26549838 : gcc_assert (concept_check_p (check));
470 26549838 : tree tmpl = TREE_OPERAND (check, 0);
471 26549838 : tree targs = TREE_OPERAND (check, 1);
472 :
473 : /* Substitute through the arguments of the concept check. */
474 26549838 : if (args)
475 19340112 : targs = tsubst_template_args (targs, args, info.complain, info.in_decl);
476 26549838 : if (targs == error_mark_node)
477 : return error_mark_node;
478 26549838 : if (template_args_equal (targs, generic_targs_for (tmpl)))
479 : /* Canonicalize generic arguments as NULL_TREE, as an optimization. */
480 3475351 : targs = NULL_TREE;
481 :
482 : /* Build the substitution for the concept definition. */
483 26549838 : tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl));
484 26549838 : if (targs && args)
485 : /* As an optimization, coerce the arguments only if necessary
486 : (i.e. if they were substituted). */
487 18944059 : targs = coerce_template_parms (parms, targs, tmpl, tf_none);
488 26549838 : if (targs == error_mark_node)
489 : return error_mark_node;
490 :
491 26549838 : if (!norm_cache)
492 82863 : norm_cache = hash_table<norm_hasher>::create_ggc (31);
493 26549838 : norm_entry *entry = nullptr;
494 26549838 : if (!info.generate_diagnostics)
495 : {
496 : /* Cache the normal form of the substituted concept-id (when not
497 : diagnosing). */
498 26546403 : norm_entry elt = {tmpl, targs, NULL_TREE};
499 26546403 : norm_entry **slot = norm_cache->find_slot (&elt, INSERT);
500 26546403 : if (*slot)
501 5172963 : return (*slot)->norm;
502 21373440 : entry = ggc_alloc<norm_entry> ();
503 21373440 : *entry = elt;
504 21373440 : *slot = entry;
505 : }
506 :
507 21376875 : tree def = get_concept_definition (DECL_TEMPLATE_RESULT (tmpl));
508 21376875 : info.update_context (check, args);
509 21376875 : tree norm = normalize_expression (def, targs, info);
510 21376875 : if (entry)
511 21373440 : entry->norm = norm;
512 : return norm;
513 : }
514 :
515 : /* A structural hasher for ATOMIC_CONSTRs. */
516 :
517 : struct atom_hasher : default_hash_traits<tree>
518 : {
519 188145477 : static hashval_t hash (tree t)
520 : {
521 188145477 : ++comparing_specializations;
522 188145477 : hashval_t val = hash_atomic_constraint (t);
523 188145477 : --comparing_specializations;
524 188145477 : return val;
525 : }
526 :
527 169881064 : static bool equal (tree t1, tree t2)
528 : {
529 169881064 : ++comparing_specializations;
530 169881064 : bool eq = atomic_constraints_identical_p (t1, t2);
531 169881064 : --comparing_specializations;
532 169881064 : return eq;
533 : }
534 : };
535 :
536 : /* Used by normalize_atom to cache ATOMIC_CONSTRs. */
537 :
538 : static GTY((deletable)) hash_table<atom_hasher> *atom_cache;
539 :
540 : /* The normal form of an atom is an atomic constraint. */
541 :
542 : static tree
543 51146067 : normalize_atom (tree t, tree args, norm_info info)
544 : {
545 : /* Concept checks are not atomic. */
546 51146067 : if (concept_check_p (t))
547 26549838 : return normalize_concept_check (t, args, info);
548 :
549 : /* Build the parameter mapping for the atom. */
550 24596229 : tree map = build_parameter_mapping (t, args, info.ctx_parms ());
551 :
552 : /* Build a new info object for the atom. */
553 24596229 : tree ci = build_tree_list (t, info.context);
554 :
555 24596229 : tree atom = build1 (ATOMIC_CONSTR, ci, map);
556 :
557 : /* Remember whether the expression of this atomic constraint belongs to
558 : a concept definition by inspecting in_decl, which should always be set
559 : in this case either by norm_info::update_context (when recursing into a
560 : concept-id during normalization) or by normalize_concept_definition
561 : (when starting out with a concept-id). */
562 48713194 : if (info.in_decl && concept_definition_p (info.in_decl))
563 21903089 : ATOMIC_CONSTR_EXPR_FROM_CONCEPT_P (atom) = true;
564 :
565 24596229 : if (!info.generate_diagnostics)
566 : {
567 : /* Cache the ATOMIC_CONSTRs that we return, so that sat_hasher::equal
568 : later can cheaply compare two atoms using just pointer equality. */
569 24591719 : if (!atom_cache)
570 92641 : atom_cache = hash_table<atom_hasher>::create_ggc (31);
571 24591719 : tree *slot = atom_cache->find_slot (atom, INSERT);
572 24591719 : if (*slot)
573 : return *slot;
574 :
575 : /* Find all template parameters used in the targets of the parameter
576 : mapping, and store a list of them in the TREE_TYPE of the mapping.
577 : This list will be used by sat_hasher to determine the subset of
578 : supplied template arguments that the satisfaction value of the atom
579 : depends on. */
580 23925752 : if (map)
581 : {
582 23924962 : tree targets = make_tree_vec (list_length (map));
583 23924962 : int i = 0;
584 65989021 : for (tree node = map; node; node = TREE_CHAIN (node))
585 : {
586 42064059 : tree target = TREE_PURPOSE (node);
587 42064059 : TREE_VEC_ELT (targets, i++) = target;
588 : }
589 23924962 : tree target_parms = find_template_parameters (targets,
590 : info.initial_parms);
591 23924962 : TREE_TYPE (map) = target_parms;
592 : }
593 :
594 23925752 : *slot = atom;
595 : }
596 : return atom;
597 : }
598 :
599 : /* Returns the normal form of an expression. */
600 :
601 : static tree
602 75613800 : normalize_expression (tree t, tree args, norm_info info)
603 : {
604 75613800 : if (!t)
605 : return NULL_TREE;
606 :
607 75613800 : if (t == error_mark_node)
608 : return error_mark_node;
609 :
610 75613677 : switch (TREE_CODE (t))
611 : {
612 23598255 : case TRUTH_ANDIF_EXPR:
613 23598255 : return normalize_logical_operation (t, args, CONJ_CONSTR, info);
614 869355 : case TRUTH_ORIF_EXPR:
615 869355 : return normalize_logical_operation (t, args, DISJ_CONSTR, info);
616 51146067 : default:
617 51146067 : return normalize_atom (t, args, info);
618 : }
619 : }
620 :
621 : /* Cache of the normalized form of constraints. Marked as deletable because it
622 : can all be recalculated. */
623 : static GTY((deletable)) hash_map<tree,tree> *normalized_map;
624 :
625 : static tree
626 5301705 : get_normalized_constraints (tree t, norm_info info)
627 : {
628 5301705 : auto_timevar time (TV_CONSTRAINT_NORM);
629 5301705 : return normalize_expression (t, NULL_TREE, info);
630 5301705 : }
631 :
632 : /* Returns the normalized constraints from a constraint-info object
633 : or NULL_TREE if the constraints are null. IN_DECL provides the
634 : declaration to which the constraints belong. */
635 :
636 : static tree
637 3272146 : get_normalized_constraints_from_info (tree ci, tree in_decl, bool diag = false)
638 : {
639 3272146 : if (ci == NULL_TREE)
640 : return NULL_TREE;
641 :
642 : /* Substitution errors during normalization are fatal. */
643 3272138 : ++processing_template_decl;
644 3272138 : norm_info info (in_decl, diag);
645 6544276 : tree t = get_normalized_constraints (CI_ASSOCIATED_CONSTRAINTS (ci), info);
646 3272138 : --processing_template_decl;
647 :
648 3272138 : return t;
649 : }
650 :
651 : /* Returns the normalized constraints for the declaration D. */
652 :
653 : static tree
654 422961709 : get_normalized_constraints_from_decl (tree d, bool diag = false)
655 : {
656 422961709 : tree tmpl;
657 422961709 : tree decl;
658 :
659 : /* For inherited constructors, consider the original declaration;
660 : it has the correct template information attached. */
661 422961709 : d = strip_inheriting_ctors (d);
662 :
663 422961709 : if (regenerated_lambda_fn_p (d))
664 : {
665 : /* If this lambda was regenerated, DECL_TEMPLATE_PARMS doesn't contain
666 : all in-scope template parameters, but the lambda from which it was
667 : ultimately regenerated does, so use that instead. */
668 484791 : tree lambda = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (d));
669 484791 : lambda = most_general_lambda (lambda);
670 484791 : d = lambda_function (lambda);
671 : }
672 :
673 422961709 : if (TREE_CODE (d) == TEMPLATE_DECL)
674 : {
675 298019149 : tmpl = d;
676 298019149 : decl = DECL_TEMPLATE_RESULT (tmpl);
677 : }
678 : else
679 : {
680 124942560 : if (tree ti = DECL_TEMPLATE_INFO (d))
681 85688548 : tmpl = TI_TEMPLATE (ti);
682 : else
683 : tmpl = NULL_TREE;
684 85688548 : decl = d;
685 : }
686 :
687 : /* Get the most general template for the declaration, and compute
688 : arguments from that. This ensures that the arguments used for
689 : normalization are always template parameters and not arguments
690 : used for outer specializations. For example:
691 :
692 : template<typename T>
693 : struct S {
694 : template<typename U> requires C<T, U> void f(U);
695 : };
696 :
697 : S<int>::f(0);
698 :
699 : When we normalize the requirements for S<int>::f, we want the
700 : arguments to be {T, U}, not {int, U}. One reason for this is that
701 : accepting the latter causes the template parameter level of U
702 : to be reduced in a way that makes it overly difficult substitute
703 : concrete arguments (i.e., eventually {int, int} during satisfaction. */
704 383707697 : if (tmpl && DECL_LANG_SPECIFIC (tmpl)
705 469396230 : && (!DECL_TEMPLATE_SPECIALIZATION (tmpl)
706 : /* DECL_TEMPLATE_SPECIALIZATION means TMPL is either a partial
707 : specialization, or an explicit specialization of a member
708 : template. In the former case all is well: TMPL's constraints
709 : are in terms of its parameters. But in the latter case TMPL's
710 : parameters are partially instantiated whereas its constraints
711 : aren't, so we need to instead use (the parameters of) the most
712 : general template. The following test distinguishes between a
713 : partial specialization and such an explicit specialization. */
714 27064028 : || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
715 13532014 : < TMPL_ARGS_DEPTH (DECL_TI_ARGS (tmpl)))))
716 370176880 : tmpl = most_general_template (tmpl);
717 :
718 422961709 : d = tmpl ? tmpl : decl;
719 :
720 : /* If we're not diagnosing errors, use cached constraints, if any. */
721 422961709 : if (!diag)
722 845768512 : if (tree *p = hash_map_safe_get (normalized_map, d))
723 339431435 : return *p;
724 :
725 83530274 : tree norm = NULL_TREE;
726 83530274 : if (tree ci = get_constraints (d))
727 : {
728 2583281 : push_access_scope_guard pas (decl);
729 2583281 : norm = get_normalized_constraints_from_info (ci, tmpl, diag);
730 2583281 : }
731 :
732 83530274 : if (!diag)
733 83529232 : hash_map_safe_put<hm_ggc> (normalized_map, d, norm);
734 :
735 83530274 : return norm;
736 : }
737 :
738 : /* Returns the normal form of TMPL's definition. */
739 :
740 : static tree
741 2712672 : normalize_concept_definition (tree tmpl, bool diag)
742 : {
743 2712672 : if (!norm_cache)
744 2531 : norm_cache = hash_table<norm_hasher>::create_ggc (31);
745 2712672 : norm_entry entry = {tmpl, NULL_TREE, NULL_TREE};
746 :
747 2712672 : if (!diag)
748 2712362 : if (norm_entry *found = norm_cache->find (&entry))
749 2371571 : return found->norm;
750 :
751 341101 : gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
752 341101 : tree def = get_concept_definition (DECL_TEMPLATE_RESULT (tmpl));
753 341101 : ++processing_template_decl;
754 341101 : norm_info info (tmpl, diag);
755 341101 : tree norm = get_normalized_constraints (def, info);
756 341101 : --processing_template_decl;
757 :
758 341101 : if (!diag)
759 : {
760 340791 : norm_entry **slot = norm_cache->find_slot (&entry, INSERT);
761 340791 : entry.norm = norm;
762 340791 : *slot = ggc_alloc<norm_entry> ();
763 340791 : **slot = entry;
764 : }
765 :
766 : return norm;
767 : }
768 :
769 : /* Normalize an EXPR as a constraint. */
770 :
771 : static tree
772 9378687 : normalize_constraint_expression (tree expr, norm_info info)
773 : {
774 9378687 : if (!expr || expr == error_mark_node)
775 : return expr;
776 :
777 9378687 : if (!info.generate_diagnostics)
778 18756968 : if (tree *p = hash_map_safe_get (normalized_map, expr))
779 7690221 : return *p;
780 :
781 1688466 : ++processing_template_decl;
782 1688466 : tree norm = get_normalized_constraints (expr, info);
783 1688466 : --processing_template_decl;
784 :
785 1688466 : if (!info.generate_diagnostics)
786 1688313 : hash_map_safe_put<hm_ggc> (normalized_map, expr, norm);
787 :
788 : return norm;
789 : }
790 :
791 : /* 17.4.1.2p2. Two constraints are identical if they are formed
792 : from the same expression and the targets of the parameter mapping
793 : are equivalent. */
794 :
795 : bool
796 330782379 : atomic_constraints_identical_p (tree t1, tree t2)
797 : {
798 330782379 : gcc_assert (TREE_CODE (t1) == ATOMIC_CONSTR);
799 330782379 : gcc_assert (TREE_CODE (t2) == ATOMIC_CONSTR);
800 :
801 330782379 : if (ATOMIC_CONSTR_EXPR (t1) != ATOMIC_CONSTR_EXPR (t2))
802 : return false;
803 :
804 57719045 : if (!parameter_mapping_equivalent_p (t1, t2))
805 : return false;
806 :
807 : return true;
808 : }
809 :
810 : /* True if T1 and T2 are equivalent, meaning they have the same syntactic
811 : structure and all corresponding constraints are identical. */
812 :
813 : bool
814 7776243 : constraints_equivalent_p (tree t1, tree t2)
815 : {
816 7776243 : gcc_assert (CONSTR_P (t1));
817 7776243 : gcc_assert (CONSTR_P (t2));
818 :
819 7776243 : if (TREE_CODE (t1) != TREE_CODE (t2))
820 : return false;
821 :
822 7582872 : switch (TREE_CODE (t1))
823 : {
824 3873058 : case CONJ_CONSTR:
825 3873058 : case DISJ_CONSTR:
826 3873058 : if (!constraints_equivalent_p (TREE_OPERAND (t1, 0),
827 3873058 : TREE_OPERAND (t2, 0)))
828 : return false;
829 3640513 : if (!constraints_equivalent_p (TREE_OPERAND (t1, 1),
830 3640513 : TREE_OPERAND (t2, 1)))
831 : return false;
832 : break;
833 3709814 : case ATOMIC_CONSTR:
834 3709814 : if (!atomic_constraints_identical_p (t1, t2))
835 : return false;
836 : break;
837 : default:
838 : gcc_unreachable ();
839 : }
840 : return true;
841 : }
842 :
843 : /* Compute the hash value for T. */
844 :
845 : hashval_t
846 1283493914 : hash_atomic_constraint (tree t)
847 : {
848 1283493914 : gcc_assert (TREE_CODE (t) == ATOMIC_CONSTR);
849 :
850 : /* Hash the identity of the expression. */
851 1283493914 : hashval_t val = htab_hash_pointer (ATOMIC_CONSTR_EXPR (t));
852 :
853 : /* Hash the targets of the parameter map. */
854 1283493914 : tree p = ATOMIC_CONSTR_MAP (t);
855 3341537697 : while (p)
856 : {
857 2058043783 : val = iterative_hash_template_arg (TREE_PURPOSE (p), val);
858 2058043783 : p = TREE_CHAIN (p);
859 : }
860 :
861 1283493914 : return val;
862 : }
863 :
864 : namespace inchash
865 : {
866 :
867 : static void
868 70221679 : add_constraint (tree t, hash& h)
869 : {
870 139703964 : h.add_int (TREE_CODE (t));
871 139703964 : switch (TREE_CODE (t))
872 : {
873 69482285 : case CONJ_CONSTR:
874 69482285 : case DISJ_CONSTR:
875 69482285 : add_constraint (TREE_OPERAND (t, 0), h);
876 69482285 : add_constraint (TREE_OPERAND (t, 1), h);
877 69482285 : break;
878 70221679 : case ATOMIC_CONSTR:
879 70221679 : h.merge_hash (hash_atomic_constraint (t));
880 70221679 : break;
881 0 : default:
882 0 : gcc_unreachable ();
883 : }
884 70221679 : }
885 :
886 : }
887 :
888 : /* Computes a hash code for the constraint T. */
889 :
890 : hashval_t
891 739394 : iterative_hash_constraint (tree t, hashval_t val)
892 : {
893 739394 : gcc_assert (CONSTR_P (t));
894 739394 : inchash::hash h (val);
895 739394 : inchash::add_constraint (t, h);
896 739394 : return h.end ();
897 : }
898 :
899 : // -------------------------------------------------------------------------- //
900 : // Constraint Semantic Processing
901 : //
902 : // The following functions are called by the parser and substitution rules
903 : // to create and evaluate constraint-related nodes.
904 :
905 : // The constraints associated with the current template parameters.
906 : tree
907 107262316 : current_template_constraints (void)
908 : {
909 107262316 : if (!current_template_parms)
910 : return NULL_TREE;
911 107262313 : tree tmpl_constr = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
912 107262313 : return build_constraints (tmpl_constr, NULL_TREE);
913 : }
914 :
915 : /* If the recently parsed TYPE declares or defines a template or
916 : template specialization, get its corresponding constraints from the
917 : current template parameters and bind them to TYPE's declaration. */
918 :
919 : tree
920 40261058 : associate_classtype_constraints (tree type)
921 : {
922 40261058 : if (!type || type == error_mark_node || !CLASS_TYPE_P (type))
923 : return type;
924 :
925 : /* An explicit class template specialization has no template parameters. */
926 40260288 : if (!current_template_parms)
927 : return type;
928 :
929 29322246 : if (CLASSTYPE_IS_TEMPLATE (type) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
930 : {
931 28128888 : tree decl = TYPE_STUB_DECL (type);
932 28128888 : tree ci = current_template_constraints ();
933 :
934 : /* An implicitly instantiated member template declaration already
935 : has associated constraints. If it is defined outside of its
936 : class, then we need match these constraints against those of
937 : original declaration. */
938 28128888 : if (tree orig_ci = get_constraints (decl))
939 : {
940 1288593 : if (int extra_levels = (TMPL_PARMS_DEPTH (current_template_parms)
941 7945367 : - TMPL_ARGS_DEPTH (TYPE_TI_ARGS (type))))
942 : {
943 : /* If there is a discrepancy between the current template depth
944 : and the template depth of the original declaration, then we
945 : must be redeclaring a class template as part of a friend
946 : declaration within another class template. Before matching
947 : constraints, we need to reduce the template parameter level
948 : within the current constraints via substitution. */
949 9 : tree outer_gtargs = template_parms_to_args (current_template_parms);
950 9 : TREE_VEC_LENGTH (outer_gtargs) = extra_levels;
951 9 : ci = tsubst_constraint_info (ci, outer_gtargs, tf_none, NULL_TREE);
952 : }
953 1288593 : if (!equivalent_constraints (ci, orig_ci))
954 : {
955 6 : auto_diagnostic_group d;
956 6 : error ("%qT does not match original declaration", type);
957 6 : tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
958 6 : location_t loc = DECL_SOURCE_LOCATION (tmpl);
959 6 : inform (loc, "original template declaration here");
960 : /* Fall through, so that we define the type anyway. */
961 6 : }
962 1288593 : return type;
963 : }
964 26840295 : set_constraints (decl, ci);
965 : }
966 : return type;
967 : }
968 :
969 : /* Create an empty constraint info block. */
970 :
971 : static inline tree_constraint_info*
972 22225509 : build_constraint_info ()
973 : {
974 22225509 : return (tree_constraint_info *)make_node (CONSTRAINT_INFO);
975 : }
976 :
977 : /* Build a constraint-info object that contains the associated constraints
978 : of a declaration. This also includes the declaration's template
979 : requirements (TREQS) and any trailing requirements for a function
980 : declarator (DREQS). Note that both TREQS and DREQS must be constraints.
981 :
982 : If the declaration has neither template nor declaration requirements
983 : this returns NULL_TREE, indicating an unconstrained declaration. */
984 :
985 : tree
986 315079341 : build_constraints (tree tr, tree dr)
987 : {
988 315079341 : if (!tr && !dr)
989 : return NULL_TREE;
990 :
991 22225509 : tree_constraint_info* ci = build_constraint_info ();
992 22225509 : ci->template_reqs = tr;
993 22225509 : ci->declarator_reqs = dr;
994 22225509 : ci->associated_constr = combine_constraint_expressions (tr, dr);
995 :
996 22225509 : return (tree)ci;
997 : }
998 :
999 : /* Add constraint RHS to the end of CONSTRAINT_INFO ci. */
1000 :
1001 : tree
1002 79 : append_constraint (tree ci, tree rhs)
1003 : {
1004 79 : tree tr = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
1005 24 : tree dr = ci ? CI_DECLARATOR_REQS (ci) : NULL_TREE;
1006 79 : dr = combine_constraint_expressions (dr, rhs);
1007 79 : if (ci)
1008 : {
1009 12 : CI_DECLARATOR_REQS (ci) = dr;
1010 12 : tree ac = combine_constraint_expressions (tr, dr);
1011 24 : CI_ASSOCIATED_CONSTRAINTS (ci) = ac;
1012 : }
1013 : else
1014 67 : ci = build_constraints (tr, dr);
1015 79 : return ci;
1016 : }
1017 :
1018 : /* A mapping from declarations to constraint information. */
1019 :
1020 : static GTY ((cache)) decl_tree_cache_map *decl_constraints;
1021 :
1022 : /* Returns the template constraints of declaration T. If T is not
1023 : constrained, return NULL_TREE. Note that T must be non-null. */
1024 :
1025 : tree
1026 1047920028 : get_constraints (const_tree t)
1027 : {
1028 1047920028 : if (!flag_concepts)
1029 : return NULL_TREE;
1030 1040056840 : if (!decl_constraints)
1031 : return NULL_TREE;
1032 :
1033 1026807305 : gcc_assert (DECL_P (t));
1034 1026807305 : if (TREE_CODE (t) == TEMPLATE_DECL)
1035 200179478 : t = DECL_TEMPLATE_RESULT (t);
1036 1026807305 : tree* found = decl_constraints->get (const_cast<tree> (t));
1037 1026807305 : if (found)
1038 100478131 : return *found;
1039 : else
1040 : return NULL_TREE;
1041 : }
1042 :
1043 : /* Associate the given constraint information CI with the declaration
1044 : T. If T is a template, then the constraints are associated with
1045 : its underlying declaration. Don't build associations if CI is
1046 : NULL_TREE. */
1047 :
1048 : void
1049 301988098 : set_constraints (tree t, tree ci)
1050 : {
1051 301988098 : if (!ci)
1052 : return;
1053 37756199 : gcc_assert (t && flag_concepts);
1054 37756199 : if (TREE_CODE (t) == TEMPLATE_DECL)
1055 91041 : t = DECL_TEMPLATE_RESULT (t);
1056 37756199 : bool found = hash_map_safe_put<hm_ggc> (decl_constraints, t, ci);
1057 37756199 : gcc_assert (!found);
1058 : }
1059 :
1060 : /* Remove the associated constraints of the declaration T. */
1061 :
1062 : void
1063 15701933 : remove_constraints (tree t)
1064 : {
1065 15701933 : gcc_checking_assert (DECL_P (t));
1066 15701933 : if (TREE_CODE (t) == TEMPLATE_DECL)
1067 186 : t = DECL_TEMPLATE_RESULT (t);
1068 :
1069 15701933 : if (decl_constraints)
1070 15076468 : decl_constraints->remove (t);
1071 15701933 : }
1072 :
1073 : /* If DECL is a friend, substitute into REQS to produce requirements suitable
1074 : for declaration matching. */
1075 :
1076 : tree
1077 140217629 : maybe_substitute_reqs_for (tree reqs, const_tree decl)
1078 : {
1079 140217629 : if (reqs == NULL_TREE)
1080 : return NULL_TREE;
1081 :
1082 10760075 : decl = STRIP_TEMPLATE (decl);
1083 10760075 : if (DECL_UNIQUE_FRIEND_P (decl) && DECL_TEMPLATE_INFO (decl))
1084 : {
1085 195799 : tree tmpl = DECL_TI_TEMPLATE (decl);
1086 195799 : tree outer_args = outer_template_args (decl);
1087 195799 : processing_template_decl_sentinel s;
1088 195799 : if (PRIMARY_TEMPLATE_P (tmpl)
1089 195799 : || uses_template_parms (outer_args))
1090 195799 : ++processing_template_decl;
1091 195799 : reqs = tsubst_constraint (reqs, outer_args,
1092 : tf_warning_or_error, NULL_TREE);
1093 195799 : }
1094 : return reqs;
1095 : }
1096 :
1097 : /* Returns the trailing requires clause of the declarator of
1098 : a template declaration T or NULL_TREE if none. */
1099 :
1100 : tree
1101 329253055 : get_trailing_function_requirements (tree t)
1102 : {
1103 329253055 : tree ci = get_constraints (t);
1104 329253055 : if (!ci)
1105 : return NULL_TREE;
1106 62690612 : return CI_DECLARATOR_REQS (ci);
1107 : }
1108 :
1109 : /* Construct a sequence of template arguments by prepending
1110 : ARG to REST. Either ARG or REST may be null. */
1111 :
1112 : static tree
1113 42946339 : build_concept_check_arguments (tree arg, tree rest)
1114 : {
1115 42946339 : gcc_assert (!rest || TREE_CODE (rest) == TREE_VEC);
1116 42946339 : tree args;
1117 42946339 : if (arg)
1118 : {
1119 33185329 : int n = rest ? TREE_VEC_LENGTH (rest) : 0;
1120 22599157 : args = make_tree_vec (n + 1);
1121 22599157 : TREE_VEC_ELT (args, 0) = arg;
1122 22599157 : if (rest)
1123 22873474 : for (int i = 0; i < n; ++i)
1124 12287302 : TREE_VEC_ELT (args, i + 1) = TREE_VEC_ELT (rest, i);
1125 10586172 : int def = rest ? GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (rest) : 0;
1126 22599157 : SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, def + 1);
1127 : }
1128 : else
1129 : args = rest;
1130 42946339 : return args;
1131 : }
1132 :
1133 : /* Construct an expression that checks TMPL using ARGS. */
1134 :
1135 : tree
1136 20347182 : build_concept_check (tree tmpl, tree args, tsubst_flags_t complain)
1137 : {
1138 20347182 : return build_concept_check (tmpl, NULL_TREE, args, complain);
1139 : }
1140 :
1141 : /* Construct an expression that checks the concept given by TMPL. */
1142 :
1143 : tree
1144 42946339 : build_concept_check (tree tmpl, tree arg, tree rest, tsubst_flags_t complain)
1145 : {
1146 42946339 : if (TREE_DEPRECATED (DECL_TEMPLATE_RESULT (tmpl)))
1147 9 : warn_deprecated_use (DECL_TEMPLATE_RESULT (tmpl), NULL_TREE);
1148 :
1149 42946339 : tree parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
1150 42946339 : tree args = build_concept_check_arguments (arg, rest);
1151 42946339 : args = coerce_template_parms (parms, args, tmpl, complain);
1152 42946339 : if (args == error_mark_node)
1153 : return error_mark_node;
1154 40339503 : return build2 (TEMPLATE_ID_EXPR, boolean_type_node, tmpl, args);
1155 : }
1156 :
1157 : /* Build a template-id that can participate in a concept check. */
1158 :
1159 : static tree
1160 17038810 : build_concept_id (tree decl, tree args)
1161 : {
1162 0 : return build_concept_check (decl, args, tf_warning_or_error);
1163 : }
1164 :
1165 : /* Build a template-id that can participate in a concept check, preserving
1166 : the source location of the original template-id. */
1167 :
1168 : tree
1169 17038810 : build_concept_id (tree expr)
1170 : {
1171 17038810 : gcc_assert (TREE_CODE (expr) == TEMPLATE_ID_EXPR);
1172 17038810 : tree id = build_concept_id (TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
1173 17038810 : protected_set_expr_location (id, cp_expr_location (expr));
1174 17038810 : return id;
1175 : }
1176 :
1177 : /* Build as template-id with a placeholder that can be used as a
1178 : type constraint.
1179 :
1180 : Note that this will diagnose errors if the initial concept check
1181 : cannot be built. */
1182 :
1183 : tree
1184 12640677 : build_type_constraint (tree decl, tree args, tsubst_flags_t complain)
1185 : {
1186 12640677 : tree proto = template_parm_to_arg (concept_prototype_parameter (decl));
1187 12640677 : ++processing_template_decl;
1188 12640677 : tree check = build_concept_check (decl, proto, args, complain);
1189 12640677 : --processing_template_decl;
1190 12640677 : return check;
1191 : }
1192 :
1193 : /* Returns a TYPE_DECL that contains sufficient information to
1194 : build a template parameter of the same kind as PROTO and
1195 : constrained by the concept declaration CNC. Note that PROTO
1196 : is the first template parameter of CNC.
1197 :
1198 : If specified, ARGS provides additional arguments to the
1199 : constraint check. */
1200 : tree
1201 8434525 : build_constrained_parameter (tree cnc, tree proto, tree args)
1202 : {
1203 8434525 : tree name = DECL_NAME (cnc);
1204 8434525 : tree type = TREE_TYPE (proto);
1205 8434525 : tree decl = build_decl (input_location, TYPE_DECL, name, type);
1206 8434525 : CONSTRAINED_PARM_PROTOTYPE (decl) = proto;
1207 8434525 : CONSTRAINED_PARM_CONCEPT (decl) = cnc;
1208 8434525 : CONSTRAINED_PARM_EXTRA_ARGS (decl) = args;
1209 8434525 : return decl;
1210 : }
1211 :
1212 : /* Create a constraint expression for the given DECL that evaluates the
1213 : requirements specified by CONSTR, a TYPE_DECL that contains all the
1214 : information necessary to build the requirements (see finish_concept_name
1215 : for the layout of that TYPE_DECL).
1216 :
1217 : Note that the constraints are neither reduced nor decomposed. That is
1218 : done only after the requires clause has been parsed (or not). */
1219 :
1220 : tree
1221 173713223 : finish_shorthand_constraint (tree decl, tree constr, bool is_non_type)
1222 : {
1223 : /* No requirements means no constraints. */
1224 173713223 : if (!constr)
1225 : return NULL_TREE;
1226 :
1227 8434610 : if (error_operand_p (constr))
1228 : return NULL_TREE;
1229 :
1230 8434610 : tree proto, con, args;
1231 8434610 : if (is_non_type)
1232 : {
1233 : /* This function should not see constrained auto&, auto* NTTPs, and a
1234 : simple constrained auto NTTP type should by now have been replaced
1235 : by ordinary auto; see finish_constrained_parameter. */
1236 109 : gcc_checking_assert (is_auto (TREE_TYPE (decl))
1237 : && !is_constrained_auto (TREE_TYPE (decl)));
1238 109 : gcc_checking_assert (TREE_CODE (constr) == TEMPLATE_ID_EXPR);
1239 109 : tree tmpl = TREE_OPERAND (constr, 0);
1240 109 : proto = concept_prototype_parameter (tmpl);
1241 109 : con = DECL_TEMPLATE_RESULT (tmpl);
1242 109 : args = TREE_OPERAND (constr, 1);
1243 : }
1244 : else
1245 : {
1246 8434501 : proto = CONSTRAINED_PARM_PROTOTYPE (constr);
1247 8434501 : con = CONSTRAINED_PARM_CONCEPT (constr);
1248 8434501 : args = CONSTRAINED_PARM_EXTRA_ARGS (constr);
1249 : }
1250 :
1251 8434610 : bool variadic_concept_p = template_parameter_pack_p (proto);
1252 8434610 : bool declared_pack_p = template_parameter_pack_p (decl);
1253 8434610 : bool apply_to_each_p = (cxx_dialect >= cxx20) ? true : !variadic_concept_p;
1254 :
1255 : /* Get the argument and overload used for the requirement
1256 : and adjust it if we're going to expand later. */
1257 8434610 : tree arg = template_parm_to_arg (decl);
1258 8434610 : if (apply_to_each_p && declared_pack_p)
1259 22473 : arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
1260 :
1261 : /* Build the concept constraint-expression. */
1262 8434610 : tree tmpl = DECL_TI_TEMPLATE (con);
1263 8434610 : tree check;
1264 8434610 : if (is_non_type)
1265 : {
1266 109 : arg = finish_decltype_type (arg, /*id_expr=*/true, tf_warning_or_error);
1267 109 : if (ARGUMENT_PACK_P (TREE_VEC_ELT (args, 0)))
1268 5 : args = expand_template_argument_pack (args);
1269 : else
1270 104 : args = copy_template_args (args);
1271 109 : TREE_VEC_ELT (args, 0) = arg;
1272 109 : check = build_concept_check (tmpl, args, tf_warning_or_error);
1273 : }
1274 : else
1275 8434501 : check = build_concept_check (tmpl, arg, args, tf_warning_or_error);
1276 :
1277 : /* Make the check a fold-expression if needed.
1278 : Use UNKNOWN_LOCATION so write_template_args can tell the
1279 : difference between this and a fold the user wrote. */
1280 8434610 : if (apply_to_each_p && declared_pack_p)
1281 22473 : check = finish_left_unary_fold_expr (UNKNOWN_LOCATION,
1282 : check, TRUTH_ANDIF_EXPR);
1283 :
1284 : return check;
1285 : }
1286 :
1287 : /* Returns a conjunction of shorthand requirements for the template
1288 : parameter list PARMS. Note that the requirements are stored in
1289 : the TYPE of each tree node. */
1290 :
1291 : tree
1292 91816785 : get_shorthand_constraints (tree parms)
1293 : {
1294 91816785 : tree result = NULL_TREE;
1295 91816785 : parms = INNERMOST_TEMPLATE_PARMS (parms);
1296 262101205 : for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
1297 : {
1298 170284420 : tree parm = TREE_VEC_ELT (parms, i);
1299 170284420 : tree constr = TEMPLATE_PARM_CONSTRAINTS (parm);
1300 170284420 : result = combine_constraint_expressions (result, constr);
1301 : }
1302 91816785 : return result;
1303 : }
1304 :
1305 : /* Returns true iff the placeholders C1 and C2 are equivalent. C1
1306 : and C2 can be either TEMPLATE_TYPE_PARM or template-ids. */
1307 :
1308 : bool
1309 612127471 : equivalent_placeholder_constraints (tree c1, tree c2)
1310 : {
1311 612127471 : if (c1 && TREE_CODE (c1) == TEMPLATE_TYPE_PARM)
1312 : /* A constrained auto. */
1313 612127471 : c1 = PLACEHOLDER_TYPE_CONSTRAINTS (c1);
1314 612127471 : if (c2 && TREE_CODE (c2) == TEMPLATE_TYPE_PARM)
1315 612127471 : c2 = PLACEHOLDER_TYPE_CONSTRAINTS (c2);
1316 :
1317 612127471 : if (c1 == c2)
1318 : return true;
1319 6748629 : if (!c1 || !c2)
1320 : return false;
1321 5880852 : if (c1 == error_mark_node || c2 == error_mark_node)
1322 : /* We get here during satisfaction; when a deduction constraint
1323 : fails, substitution can produce an error_mark_node for the
1324 : placeholder constraints. */
1325 : return false;
1326 :
1327 5880852 : gcc_assert (concept_check_p (c1) && concept_check_p (c2));
1328 5880852 : tree t1 = TREE_OPERAND (c1, 0);
1329 5880852 : tree a1 = TREE_OPERAND (c1, 1);
1330 5880852 : tree t2 = TREE_OPERAND (c2, 0);
1331 5880852 : tree a2 = TREE_OPERAND (c2, 1);
1332 :
1333 5880852 : if (t1 != t2)
1334 : return false;
1335 :
1336 2897338 : int len1 = TREE_VEC_LENGTH (a1);
1337 2897338 : int len2 = TREE_VEC_LENGTH (a2);
1338 2897338 : if (len1 != len2)
1339 : return false;
1340 :
1341 : /* Skip the first argument so we don't infinitely recurse.
1342 : Also, they may differ in template parameter index. */
1343 4056985 : for (int i = 1; i < len1; ++i)
1344 1734540 : if (!template_args_equal (TREE_VEC_ELT (a1, i),
1345 1734540 : TREE_VEC_ELT (a2, i)))
1346 : return false;
1347 : return true;
1348 : }
1349 :
1350 : /* Return a hash value for the placeholder ATOMIC_CONSTR C. */
1351 :
1352 : hashval_t
1353 269536385 : iterative_hash_placeholder_constraint (tree c, hashval_t val)
1354 : {
1355 269536385 : gcc_assert (concept_check_p (c));
1356 269536385 : tree t = TREE_OPERAND (c, 0);
1357 269536385 : tree a = TREE_OPERAND (c, 1);
1358 :
1359 : /* Like hash_tmpl_and_args, but skip the first argument. */
1360 269536385 : val = iterative_hash_object (DECL_UID (t), val);
1361 :
1362 488978878 : for (int i = TREE_VEC_LENGTH (a)-1; i > 0; --i)
1363 219442493 : val = iterative_hash_template_arg (TREE_VEC_ELT (a, i), val);
1364 :
1365 269536385 : return val;
1366 : }
1367 :
1368 : /* Substitute through the expression of a simple requirement or
1369 : compound requirement. */
1370 :
1371 : static tree
1372 13663034 : tsubst_valid_expression_requirement (tree t, tree args, sat_info info)
1373 : {
1374 13663034 : tsubst_flags_t quiet = info.complain & ~tf_warning_or_error;
1375 13663034 : tree r = tsubst_expr (t, args, quiet, info.in_decl);
1376 13660334 : if (r != error_mark_node
1377 13660334 : && (processing_template_decl
1378 13120135 : || convert_to_void (r, ICV_STATEMENT, quiet) != error_mark_node))
1379 13123837 : return r;
1380 :
1381 536497 : if (info.diagnose_unsatisfaction_p ())
1382 : {
1383 195 : location_t loc = cp_expr_loc_or_input_loc (t);
1384 195 : if (diagnosing_failed_constraint::replay_errors_p ())
1385 : {
1386 10 : inform (loc, "the required expression %qE is invalid, because", t);
1387 10 : auto_diagnostic_nesting_level sentinel;
1388 10 : if (r == error_mark_node)
1389 8 : tsubst_expr (t, args, info.complain, info.in_decl);
1390 : else
1391 2 : convert_to_void (r, ICV_STATEMENT, info.complain);
1392 10 : }
1393 : else
1394 185 : inform (loc, "the required expression %qE is invalid", t);
1395 : }
1396 536302 : else if (info.noisy ())
1397 : {
1398 0 : r = tsubst_expr (t, args, info.complain, info.in_decl);
1399 0 : convert_to_void (r, ICV_STATEMENT, info.complain);
1400 : }
1401 :
1402 536497 : return error_mark_node;
1403 : }
1404 :
1405 :
1406 : /* Substitute through the simple requirement. */
1407 :
1408 : static tree
1409 4750136 : tsubst_simple_requirement (tree t, tree args, sat_info info)
1410 : {
1411 4750136 : tree t0 = TREE_OPERAND (t, 0);
1412 4750136 : tree expr = tsubst_valid_expression_requirement (t0, args, info);
1413 4747436 : if (expr == error_mark_node)
1414 : return error_mark_node;
1415 4315901 : if (processing_template_decl)
1416 394 : return finish_simple_requirement (EXPR_LOCATION (t), expr);
1417 4315507 : return boolean_true_node;
1418 : }
1419 :
1420 : /* Subroutine of tsubst_type_requirement that performs the actual substitution
1421 : and diagnosing. Also used by tsubst_compound_requirement. */
1422 :
1423 : static tree
1424 13151820 : tsubst_type_requirement_1 (tree t, tree args, sat_info info, location_t loc)
1425 : {
1426 13151820 : tsubst_flags_t quiet = info.complain & ~tf_warning_or_error;
1427 13151820 : tree r = tsubst (t, args, quiet, info.in_decl);
1428 13151820 : if (r != error_mark_node)
1429 : return r;
1430 :
1431 391112 : if (info.diagnose_unsatisfaction_p ())
1432 : {
1433 25 : if (diagnosing_failed_constraint::replay_errors_p ())
1434 : {
1435 : /* Replay the substitution error. */
1436 4 : inform (loc, "the required type %qT is invalid, because", t);
1437 4 : tsubst (t, args, info.complain, info.in_decl);
1438 : }
1439 : else
1440 21 : inform (loc, "the required type %qT is invalid", t);
1441 : }
1442 391087 : else if (info.noisy ())
1443 0 : tsubst (t, args, info.complain, info.in_decl);
1444 :
1445 391112 : return error_mark_node;
1446 : }
1447 :
1448 :
1449 : /* Substitute through the type requirement. */
1450 :
1451 : static tree
1452 4343899 : tsubst_type_requirement (tree t, tree args, sat_info info)
1453 : {
1454 4343899 : tree t0 = TREE_OPERAND (t, 0);
1455 4343899 : tree type = tsubst_type_requirement_1 (t0, args, info, EXPR_LOCATION (t));
1456 4343899 : if (type == error_mark_node)
1457 : return error_mark_node;
1458 3952787 : if (processing_template_decl)
1459 4 : return finish_type_requirement (EXPR_LOCATION (t), type);
1460 3952783 : return boolean_true_node;
1461 : }
1462 :
1463 : /* True if TYPE can be deduced from EXPR. */
1464 :
1465 : static bool
1466 8547455 : type_deducible_p (tree expr, tree type, tree placeholder, tree args,
1467 : subst_info info)
1468 : {
1469 : /* Make sure deduction is performed against ( EXPR ), so that
1470 : references are preserved in the result. */
1471 8547455 : expr = force_paren_expr_uneval (expr);
1472 :
1473 8547455 : tree deduced_type = do_auto_deduction (type, expr, placeholder,
1474 : info.complain, adc_requirement,
1475 : /*outer_targs=*/args);
1476 :
1477 8547455 : return deduced_type != error_mark_node;
1478 : }
1479 :
1480 : /* True if EXPR can not be converted to TYPE. */
1481 :
1482 : static bool
1483 5 : expression_convertible_p (tree expr, tree type, subst_info info)
1484 : {
1485 5 : tree conv =
1486 5 : perform_direct_initialization_if_possible (type, expr, false,
1487 : info.complain);
1488 5 : if (conv == error_mark_node)
1489 : return false;
1490 0 : if (conv == NULL_TREE)
1491 : {
1492 0 : if (info.complain & tf_error)
1493 : {
1494 0 : location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
1495 0 : error_at (loc, "cannot convert %qE to %qT", expr, type);
1496 : }
1497 0 : return false;
1498 : }
1499 : return true;
1500 : }
1501 :
1502 :
1503 : /* Substitute through the compound requirement. */
1504 :
1505 : static tree
1506 8912898 : tsubst_compound_requirement (tree t, tree args, sat_info info)
1507 : {
1508 8912898 : tree t0 = TREE_OPERAND (t, 0);
1509 8912898 : tree t1 = TREE_OPERAND (t, 1);
1510 8912898 : tree expr = tsubst_valid_expression_requirement (t0, args, info);
1511 8912898 : if (expr == error_mark_node)
1512 : return error_mark_node;
1513 :
1514 8807936 : location_t loc = cp_expr_loc_or_input_loc (expr);
1515 :
1516 8807936 : subst_info quiet (info.complain & ~tf_warning_or_error, info.in_decl);
1517 :
1518 : /* Check the noexcept condition. */
1519 8807936 : bool noexcept_p = COMPOUND_REQ_NOEXCEPT_P (t);
1520 257156 : if (noexcept_p && !processing_template_decl
1521 9065089 : && !expr_noexcept_p (expr, quiet.complain))
1522 : {
1523 20 : if (info.diagnose_unsatisfaction_p ())
1524 5 : inform (loc, "%qE is not %<noexcept%>", expr);
1525 : else
1526 15 : return error_mark_node;
1527 : }
1528 :
1529 : /* Substitute through the type expression, if any. */
1530 8807921 : tree type = tsubst_type_requirement_1 (t1, args, info, EXPR_LOCATION (t));
1531 8807921 : if (type == error_mark_node)
1532 : return error_mark_node;
1533 :
1534 : /* Check expression against the result type. */
1535 8807921 : if (type && !processing_template_decl)
1536 : {
1537 8547448 : if (tree placeholder = type_uses_auto (type))
1538 : {
1539 8547443 : if (!type_deducible_p (expr, type, placeholder, args, quiet))
1540 : {
1541 22745 : if (info.diagnose_unsatisfaction_p ())
1542 : {
1543 40 : if (diagnosing_failed_constraint::replay_errors_p ())
1544 : {
1545 12 : inform (loc,
1546 : "%qE does not satisfy return-type-requirement, "
1547 : "because", t0);
1548 : /* Further explain the reason for the error. */
1549 12 : type_deducible_p (expr, type, placeholder, args, info);
1550 : }
1551 : else
1552 28 : inform (loc,
1553 : "%qE does not satisfy return-type-requirement", t0);
1554 : }
1555 22745 : return error_mark_node;
1556 : }
1557 : }
1558 5 : else if (!expression_convertible_p (expr, type, quiet))
1559 : {
1560 5 : if (info.diagnose_unsatisfaction_p ())
1561 : {
1562 0 : if (diagnosing_failed_constraint::replay_errors_p ())
1563 : {
1564 0 : inform (loc, "cannot convert %qE to %qT because", t0, type);
1565 : /* Further explain the reason for the error. */
1566 0 : expression_convertible_p (expr, type, info);
1567 : }
1568 : else
1569 0 : inform (loc, "cannot convert %qE to %qT", t0, type);
1570 : }
1571 5 : return error_mark_node;
1572 : }
1573 : }
1574 :
1575 8785171 : if (processing_template_decl)
1576 3322 : return finish_compound_requirement (EXPR_LOCATION (t),
1577 3322 : expr, type, noexcept_p);
1578 8781849 : return boolean_true_node;
1579 : }
1580 :
1581 : /* Substitute through the nested requirement. */
1582 :
1583 : static tree
1584 830729 : tsubst_nested_requirement (tree t, tree args, sat_info info)
1585 : {
1586 830729 : if (processing_template_decl)
1587 : {
1588 0 : tree req = TREE_OPERAND (t, 0);
1589 0 : req = tsubst_constraint (req, args, info.complain, info.in_decl);
1590 0 : if (req == error_mark_node)
1591 : return error_mark_node;
1592 0 : return finish_nested_requirement (EXPR_LOCATION (t), req);
1593 : }
1594 :
1595 830729 : sat_info quiet (info.complain & ~tf_warning_or_error, info.in_decl);
1596 830729 : tree result = constraint_satisfaction_value (t, args, quiet);
1597 830729 : if (result == boolean_true_node)
1598 : return boolean_true_node;
1599 :
1600 31958 : if (result == boolean_false_node
1601 31958 : && info.diagnose_unsatisfaction_p ())
1602 : {
1603 77 : tree expr = TREE_OPERAND (t, 0);
1604 77 : location_t loc = cp_expr_location (t);
1605 77 : if (diagnosing_failed_constraint::replay_errors_p ())
1606 : {
1607 : /* Replay the substitution error. */
1608 27 : inform (loc, "nested requirement %qE is not satisfied, because", expr);
1609 27 : constraint_satisfaction_value (t, args, info);
1610 : }
1611 : else
1612 50 : inform (loc, "nested requirement %qE is not satisfied", expr);
1613 : }
1614 :
1615 31958 : return error_mark_node;
1616 : }
1617 :
1618 : /* Substitute ARGS into the requirement T. */
1619 :
1620 : static tree
1621 18837662 : tsubst_requirement (tree t, tree args, sat_info info)
1622 : {
1623 18837662 : iloc_sentinel loc_s (cp_expr_location (t));
1624 18837662 : switch (TREE_CODE (t))
1625 : {
1626 4750136 : case SIMPLE_REQ:
1627 4750136 : return tsubst_simple_requirement (t, args, info);
1628 4343899 : case TYPE_REQ:
1629 4343899 : return tsubst_type_requirement (t, args, info);
1630 8912898 : case COMPOUND_REQ:
1631 8912898 : return tsubst_compound_requirement (t, args, info);
1632 830729 : case NESTED_REQ:
1633 830729 : return tsubst_nested_requirement (t, args, info);
1634 0 : default:
1635 0 : break;
1636 : }
1637 0 : gcc_unreachable ();
1638 18834962 : }
1639 :
1640 : static tree
1641 5423731 : declare_constraint_vars (tree parms, tree vars)
1642 : {
1643 5423731 : tree s = vars;
1644 13964043 : for (tree t = parms; t; t = DECL_CHAIN (t))
1645 : {
1646 8540312 : if (DECL_PACK_P (t))
1647 : {
1648 3752 : tree pack = extract_fnparm_pack (t, &s);
1649 3752 : register_local_specialization (pack, t);
1650 : }
1651 : else
1652 : {
1653 8536560 : register_local_specialization (s, t);
1654 8536560 : s = DECL_CHAIN (s);
1655 : }
1656 : }
1657 5423731 : return vars;
1658 : }
1659 :
1660 : /* Substitute through as if checking function parameter types. This
1661 : will diagnose common parameter type errors. Returns error_mark_node
1662 : if an error occurred. */
1663 :
1664 : static tree
1665 5423773 : check_constraint_variables (tree t, tree args, subst_info info)
1666 : {
1667 5423773 : tree types = NULL_TREE;
1668 5423773 : tree p = t;
1669 13964134 : while (p && !VOID_TYPE_P (p))
1670 : {
1671 8540361 : types = tree_cons (NULL_TREE, TREE_TYPE (p), types);
1672 8540361 : p = TREE_CHAIN (p);
1673 : }
1674 5423773 : types = chainon (nreverse (types), void_list_node);
1675 5423773 : return tsubst_function_parms (types, args, info.complain, info.in_decl);
1676 : }
1677 :
1678 : /* A subroutine of tsubst_parameterized_constraint. Substitute ARGS
1679 : into the parameter list T, producing a sequence of constraint
1680 : variables, declared in the current scope.
1681 :
1682 : Note that the caller must establish a local specialization stack
1683 : prior to calling this function since this substitution will
1684 : declare the substituted parameters. */
1685 :
1686 : static tree
1687 5423773 : tsubst_constraint_variables (tree t, tree args, subst_info info)
1688 : {
1689 : /* Perform a trial substitution to check for type errors. */
1690 5423773 : tree parms = check_constraint_variables (t, args, info);
1691 5423773 : if (parms == error_mark_node)
1692 : return error_mark_node;
1693 :
1694 : /* Clear cp_unevaluated_operand across tsubst so that we get a proper chain
1695 : of PARM_DECLs. */
1696 5423731 : int saved_unevaluated_operand = cp_unevaluated_operand;
1697 5423731 : cp_unevaluated_operand = 0;
1698 5423731 : tree vars = tsubst (t, args, info.complain, info.in_decl);
1699 5423731 : cp_unevaluated_operand = saved_unevaluated_operand;
1700 5423731 : if (vars == error_mark_node)
1701 : return error_mark_node;
1702 5423731 : return declare_constraint_vars (t, vars);
1703 : }
1704 :
1705 : /* Substitute ARGS into the requires-expression T. [8.4.7]p6. The
1706 : substitution of template arguments into a requires-expression
1707 : may result in the formation of invalid types or expressions
1708 : in its requirements ... In such cases, the expression evaluates
1709 : to false; it does not cause the program to be ill-formed.
1710 :
1711 : When substituting through a REQUIRES_EXPR as part of template
1712 : instantiation, we call this routine with info.quiet() true.
1713 :
1714 : When evaluating a REQUIRES_EXPR that appears outside a template in
1715 : cp_parser_requires_expression, we call this routine with
1716 : info.noisy() true.
1717 :
1718 : Finally, when diagnosing unsatisfaction from diagnose_atomic_constraint
1719 : and when diagnosing a false REQUIRES_EXPR via diagnose_constraints,
1720 : we call this routine with info.diagnose_unsatisfaction_p() true. */
1721 :
1722 : static tree
1723 11787767 : tsubst_requires_expr (tree t, tree args, sat_info info)
1724 : {
1725 11787767 : local_specialization_stack stack (lss_copy);
1726 :
1727 : /* We need to check access during the substitution. */
1728 11787767 : deferring_access_check_sentinel acs (dk_no_deferred);
1729 :
1730 : /* A requires-expression is an unevaluated context. */
1731 11787767 : cp_unevaluated u;
1732 :
1733 11787767 : args = add_extra_args (REQUIRES_EXPR_EXTRA_ARGS (t), args,
1734 : info.complain, info.in_decl);
1735 11787767 : if (processing_template_decl
1736 11787767 : && !processing_constraint_expression_p ())
1737 : {
1738 : /* We're partially instantiating a generic lambda. Substituting into
1739 : this requires-expression now may cause its requirements to get
1740 : checked out of order, so instead just remember the template
1741 : arguments and wait until we can substitute them all at once.
1742 :
1743 : Except if this requires-expr is part of associated constraints
1744 : that we're substituting into directly (for e.g. declaration
1745 : matching or dguide constraint rewriting), in which case we need
1746 : to partially substitute. */
1747 3164 : t = copy_node (t);
1748 3164 : REQUIRES_EXPR_EXTRA_ARGS (t) = NULL_TREE;
1749 3164 : REQUIRES_EXPR_EXTRA_ARGS (t) = build_extra_args (t, args, info.complain);
1750 3164 : return t;
1751 : }
1752 :
1753 11784603 : tree parms = REQUIRES_EXPR_PARMS (t);
1754 11784603 : if (parms)
1755 : {
1756 5423773 : parms = tsubst_constraint_variables (parms, args, info);
1757 5423773 : if (parms == error_mark_node)
1758 42 : return boolean_false_node;
1759 : }
1760 :
1761 11784561 : tree result = boolean_true_node;
1762 11784561 : if (processing_template_decl)
1763 2961 : result = NULL_TREE;
1764 29637528 : for (tree reqs = REQUIRES_EXPR_REQS (t); reqs; reqs = TREE_CHAIN (reqs))
1765 : {
1766 18837662 : tree req = TREE_VALUE (reqs);
1767 18837662 : req = tsubst_requirement (req, args, info);
1768 18834962 : if (req == error_mark_node)
1769 : {
1770 982332 : result = boolean_false_node;
1771 982332 : if (info.diagnose_unsatisfaction_p ())
1772 : /* Keep going so that we diagnose all failed requirements. */;
1773 : else
1774 : break;
1775 : }
1776 17852630 : else if (processing_template_decl)
1777 3720 : result = tree_cons (NULL_TREE, req, result);
1778 : }
1779 11781861 : if (processing_template_decl && result != boolean_false_node)
1780 2961 : result = finish_requires_expr (REQUIRES_EXPR_LOCATION (t), parms,
1781 : nreverse (result));
1782 : return result;
1783 11785067 : }
1784 :
1785 : /* Public wrapper for the above. */
1786 :
1787 : tree
1788 11787289 : tsubst_requires_expr (tree t, tree args,
1789 : tsubst_flags_t complain, tree in_decl)
1790 : {
1791 11787289 : sat_info info (complain, in_decl);
1792 11787289 : return tsubst_requires_expr (t, args, info);
1793 : }
1794 :
1795 : /* Substitute ARGS into the constraint information CI, producing a new
1796 : constraint record. */
1797 :
1798 : tree
1799 638779 : tsubst_constraint_info (tree t, tree args,
1800 : tsubst_flags_t complain, tree in_decl)
1801 : {
1802 638779 : if (!t || t == error_mark_node || !check_constraint_info (t))
1803 : return NULL_TREE;
1804 :
1805 84514 : tree tr = tsubst_constraint (CI_TEMPLATE_REQS (t), args, complain, in_decl);
1806 169028 : tree dr = tsubst_constraint (CI_DECLARATOR_REQS (t), args, complain, in_decl);
1807 84514 : return build_constraints (tr, dr);
1808 : }
1809 :
1810 : /* Substitute through a parameter mapping, in order to get the actual
1811 : arguments used to instantiate an atomic constraint. This may fail
1812 : if the substitution into arguments produces something ill-formed. */
1813 :
1814 : static tree
1815 56343897 : tsubst_parameter_mapping (tree map, tree args, subst_info info)
1816 : {
1817 56343897 : if (!map)
1818 : return NULL_TREE;
1819 :
1820 56343044 : tsubst_flags_t complain = info.complain;
1821 56343044 : tree in_decl = info.in_decl;
1822 :
1823 56343044 : tree result = NULL_TREE;
1824 152660656 : for (tree p = map; p; p = TREE_CHAIN (p))
1825 : {
1826 96330871 : if (p == error_mark_node)
1827 : return error_mark_node;
1828 96330871 : tree parm = TREE_VALUE (p);
1829 96330871 : tree arg = TREE_PURPOSE (p);
1830 96330871 : tree new_arg;
1831 96330871 : if (ARGUMENT_PACK_P (arg))
1832 3276384 : new_arg = tsubst_argument_pack (arg, args, complain, in_decl);
1833 : else
1834 : {
1835 93054487 : new_arg = tsubst_template_arg (arg, args, complain, in_decl);
1836 93054487 : if (TYPE_P (new_arg))
1837 92820468 : new_arg = canonicalize_type_argument (new_arg, complain);
1838 : }
1839 96330871 : if (TREE_CODE (new_arg) == TYPE_ARGUMENT_PACK)
1840 : {
1841 3257312 : tree pack_args = ARGUMENT_PACK_ARGS (new_arg);
1842 6710221 : for (tree& pack_arg : tree_vec_range (pack_args))
1843 3452909 : if (TYPE_P (pack_arg))
1844 3452909 : pack_arg = canonicalize_type_argument (pack_arg, complain);
1845 : }
1846 96330871 : if (new_arg == error_mark_node)
1847 : return error_mark_node;
1848 :
1849 96317612 : result = tree_cons (new_arg, parm, result);
1850 : }
1851 56329785 : return nreverse (result);
1852 : }
1853 :
1854 : tree
1855 1121 : tsubst_parameter_mapping (tree map, tree args, tsubst_flags_t complain, tree in_decl)
1856 : {
1857 1121 : return tsubst_parameter_mapping (map, args, subst_info (complain, in_decl));
1858 : }
1859 :
1860 : /*---------------------------------------------------------------------------
1861 : Constraint satisfaction
1862 : ---------------------------------------------------------------------------*/
1863 :
1864 : /* True if we are currently satisfying a constraint. */
1865 :
1866 : static bool satisfying_constraint;
1867 :
1868 : /* A vector of incomplete types (and of declarations with undeduced return type),
1869 : appended to by note_failed_type_completion. The
1870 : satisfaction caches use this in order to keep track of "potentially unstable"
1871 : satisfaction results.
1872 :
1873 : Since references to entries in this vector are stored only in the
1874 : GC-deletable sat_cache, it's safe to make this deletable as well. */
1875 :
1876 : static GTY((deletable)) vec<tree, va_gc> *failed_type_completions;
1877 :
1878 : /* A map of where types were found to be incomplete in SFINAE context, for
1879 : warning if they are later completed. */
1880 :
1881 : static GTY((cache)) hash_map<tree, location_t, decl_location_traits> *failed_completions_map;
1882 :
1883 : /* Called whenever a type completion (or return type deduction) failure occurs
1884 : that definitely affects the meaning of the program, by e.g. inducing
1885 : substitution failure. */
1886 :
1887 : void
1888 6995 : note_failed_type_completion (tree t, tsubst_flags_t complain)
1889 : {
1890 6995 : if (dependent_template_arg_p (t))
1891 : return;
1892 :
1893 6958 : gcc_checking_assert ((TYPE_P (t) && !COMPLETE_TYPE_P (t))
1894 : || (DECL_P (t) && undeduced_auto_decl (t)));
1895 :
1896 6958 : if (satisfying_constraint)
1897 193 : vec_safe_push (failed_type_completions, t);
1898 :
1899 6958 : if (TYPE_P (t))
1900 : {
1901 4217 : if (!CLASS_TYPE_P (t))
1902 : return;
1903 3806 : t = TYPE_MAIN_DECL (t);
1904 : }
1905 6547 : if (!(complain & tf_error)
1906 12461 : && warning_enabled_at (DECL_SOURCE_LOCATION (t),
1907 5914 : OPT_Wsfinae_incomplete_))
1908 : {
1909 5912 : if (warn_sfinae_incomplete > 1)
1910 : {
1911 0 : if (TREE_CODE (t) == TYPE_DECL)
1912 0 : warning (OPT_Wsfinae_incomplete_,
1913 0 : "failed to complete %qT in SFINAE context", TREE_TYPE (t));
1914 : else
1915 0 : warning (OPT_Wsfinae_incomplete_,
1916 : "failed to deduce %qD in SFINAE context", t);
1917 : }
1918 5912 : if (!failed_completions_map)
1919 126 : failed_completions_map
1920 126 : = hash_map<tree, location_t, decl_location_traits>::create_ggc ();
1921 5912 : failed_completions_map->put (t, input_location);
1922 : }
1923 : }
1924 :
1925 : /* If T was previously found to be incomplete in SFINAE context, return the
1926 : location where that happened, otherwise UNKNOWN_LOCATION. */
1927 :
1928 : location_t
1929 53470168 : failed_completion_location (tree t)
1930 : {
1931 53470168 : if (failed_completions_map)
1932 : {
1933 111443 : if (TYPE_P (t))
1934 96057 : t = TYPE_MAIN_DECL (t);
1935 111443 : if (location_t *p = failed_completions_map->get (t))
1936 17 : return *p;
1937 : }
1938 : return UNKNOWN_LOCATION;
1939 : }
1940 :
1941 : /* Returns true if the range [BEGIN, END) of elements within the
1942 : failed_type_completions vector contains a complete type (or a
1943 : declaration with a non-placeholder return type). */
1944 :
1945 : static bool
1946 522045996 : some_type_complete_p (int begin, int end)
1947 : {
1948 522046599 : for (int i = begin; i < end; i++)
1949 : {
1950 666 : tree t = (*failed_type_completions)[i];
1951 666 : if (TYPE_P (t) && COMPLETE_TYPE_P (t))
1952 : return true;
1953 621 : if (DECL_P (t) && !undeduced_auto_decl (t))
1954 : return true;
1955 : }
1956 : return false;
1957 : }
1958 :
1959 : /* Hash functions and data types for satisfaction cache entries. */
1960 :
1961 : struct GTY((for_user)) sat_entry
1962 : {
1963 : /* The relevant ATOMIC_CONSTR. */
1964 : tree atom;
1965 :
1966 : /* The relevant template arguments. */
1967 : tree args;
1968 :
1969 : /* The result of satisfaction of ATOM+ARGS.
1970 : This is either boolean_true_node, boolean_false_node or error_mark_node,
1971 : where error_mark_node indicates ill-formed satisfaction.
1972 : It's set to NULL_TREE while computing satisfaction of ATOM+ARGS for
1973 : the first time. */
1974 : tree result;
1975 :
1976 : /* For a !ATOMIC_CONSTR_MAP_INSTANTIATED_P atom, this conveniently points to
1977 : the entry for the corresponding atom after instantiating its mapping. */
1978 : sat_entry *inst_entry;
1979 :
1980 : /* The value of input_location when satisfaction of ATOM+ARGS was first
1981 : performed. */
1982 : location_t location;
1983 :
1984 : /* The range of elements appended to the failed_type_completions vector
1985 : during computation of this satisfaction result, encoded as a begin/end
1986 : pair of offsets. */
1987 : int ftc_begin, ftc_end;
1988 :
1989 : /* True if we want to diagnose the above instability when it's detected.
1990 : We don't always want to do so, in order to avoid emitting duplicate
1991 : diagnostics in some cases. */
1992 : bool diagnose_instability;
1993 :
1994 : /* True if we're in the middle of computing this satisfaction result.
1995 : Used during both quiet and noisy satisfaction to detect self-recursive
1996 : satisfaction. */
1997 : bool evaluating;
1998 : };
1999 :
2000 : struct sat_hasher : ggc_ptr_hash<sat_entry>
2001 : {
2002 3443022541 : static hashval_t hash (sat_entry *e)
2003 : {
2004 3443022541 : auto cso = make_temp_override (comparing_specializations);
2005 3443022541 : ++comparing_specializations;
2006 :
2007 3443022541 : if (ATOMIC_CONSTR_MAP_INSTANTIATED_P (e->atom))
2008 : {
2009 : /* Atoms with instantiated mappings are built during satisfaction.
2010 : They live only inside the sat_cache, and we build one to query
2011 : the cache with each time we instantiate a mapping. */
2012 1025126758 : gcc_assert (!e->args);
2013 1025126758 : return hash_atomic_constraint (e->atom);
2014 : }
2015 :
2016 : /* Atoms with uninstantiated mappings are built during normalization.
2017 : Since normalize_atom caches the atoms it returns, we can assume
2018 : pointer-based identity for fast hashing and comparison. Even if this
2019 : assumption is violated, that's okay, we'll just get a cache miss. */
2020 2417895783 : hashval_t value = htab_hash_pointer (e->atom);
2021 :
2022 2417895783 : if (tree map = ATOMIC_CONSTR_MAP (e->atom))
2023 : /* Only the parameters that are used in the targets of the mapping
2024 : affect the satisfaction value of the atom. So we consider only
2025 : the arguments for these parameters, and ignore the rest. */
2026 2417881902 : for (tree target_parms = TREE_TYPE (map);
2027 5413462636 : target_parms;
2028 2995580734 : target_parms = TREE_CHAIN (target_parms))
2029 : {
2030 2995580734 : int level, index;
2031 2995580734 : tree parm = TREE_VALUE (target_parms);
2032 2995580734 : template_parm_level_and_index (parm, &level, &index);
2033 2995580734 : tree arg = TMPL_ARG (e->args, level, index);
2034 2995580734 : value = iterative_hash_template_arg (arg, value);
2035 : }
2036 : return value;
2037 3443022541 : }
2038 :
2039 3705552191 : static bool equal (sat_entry *e1, sat_entry *e2)
2040 : {
2041 3705552191 : auto cso = make_temp_override (comparing_specializations);
2042 3705552191 : ++comparing_specializations;
2043 :
2044 3705552191 : if (ATOMIC_CONSTR_MAP_INSTANTIATED_P (e1->atom)
2045 3705552191 : != ATOMIC_CONSTR_MAP_INSTANTIATED_P (e2->atom))
2046 : return false;
2047 :
2048 : /* See sat_hasher::hash. */
2049 2524717216 : if (ATOMIC_CONSTR_MAP_INSTANTIATED_P (e1->atom))
2050 : {
2051 157191501 : gcc_assert (!e1->args && !e2->args);
2052 157191501 : return atomic_constraints_identical_p (e1->atom, e2->atom);
2053 : }
2054 :
2055 2367525715 : if (e1->atom != e2->atom)
2056 : return false;
2057 :
2058 416495099 : if (tree map = ATOMIC_CONSTR_MAP (e1->atom))
2059 416493509 : for (tree target_parms = TREE_TYPE (map);
2060 854762679 : target_parms;
2061 438269170 : target_parms = TREE_CHAIN (target_parms))
2062 : {
2063 445386767 : int level, index;
2064 445386767 : tree parm = TREE_VALUE (target_parms);
2065 445386767 : template_parm_level_and_index (parm, &level, &index);
2066 445386767 : tree arg1 = TMPL_ARG (e1->args, level, index);
2067 445386767 : tree arg2 = TMPL_ARG (e2->args, level, index);
2068 445386767 : if (!template_args_equal (arg1, arg2))
2069 7117597 : return false;
2070 : }
2071 : return true;
2072 3705552191 : }
2073 : };
2074 :
2075 : /* Cache the result of satisfy_atom. */
2076 : static GTY((deletable)) hash_table<sat_hasher> *sat_cache;
2077 :
2078 : /* Cache the result of satisfy_declaration_constraints. */
2079 : static GTY((deletable)) hash_map<tree, tree> *decl_satisfied_cache;
2080 :
2081 : /* A tool used by satisfy_atom to help manage satisfaction caching and to
2082 : diagnose "unstable" satisfaction values. We insert into the cache only
2083 : when performing satisfaction quietly. */
2084 :
2085 : struct satisfaction_cache
2086 : {
2087 : satisfaction_cache (tree, tree, sat_info);
2088 : tree get ();
2089 : tree save (tree);
2090 :
2091 : sat_entry *entry;
2092 : sat_info info;
2093 : int ftc_begin;
2094 : };
2095 :
2096 : /* Constructor for the satisfaction_cache class. We're performing satisfaction
2097 : of ATOM+ARGS according to INFO. */
2098 :
2099 522046038 : satisfaction_cache
2100 522046038 : ::satisfaction_cache (tree atom, tree args, sat_info info)
2101 522046038 : : entry(nullptr), info(info), ftc_begin(-1)
2102 : {
2103 522046038 : if (!sat_cache)
2104 74556 : sat_cache = hash_table<sat_hasher>::create_ggc (31);
2105 :
2106 : /* When noisy, we query the satisfaction cache in order to diagnose
2107 : "unstable" satisfaction values. */
2108 522046038 : if (info.noisy ())
2109 : {
2110 : /* When noisy, constraints have been re-normalized, and that breaks the
2111 : pointer-based identity assumption of sat_cache (for atoms with
2112 : uninstantiated mappings). So undo this re-normalization by looking in
2113 : the atom_cache for the corresponding atom that was used during quiet
2114 : satisfaction. */
2115 7474 : if (!ATOMIC_CONSTR_MAP_INSTANTIATED_P (atom))
2116 : {
2117 3750 : if (tree found = atom_cache->find (atom))
2118 3750 : atom = found;
2119 : else
2120 : /* The lookup should always succeed, but if it fails then let's
2121 : just leave 'entry' empty, effectively disabling the cache. */
2122 0 : return;
2123 : }
2124 : }
2125 :
2126 : /* Look up or create the corresponding satisfaction entry. */
2127 522046038 : sat_entry elt;
2128 522046038 : elt.atom = atom;
2129 522046038 : elt.args = args;
2130 522046038 : sat_entry **slot = sat_cache->find_slot (&elt, INSERT);
2131 522046038 : if (*slot)
2132 438336661 : entry = *slot;
2133 83709377 : else if (info.quiet ())
2134 : {
2135 83709371 : entry = ggc_alloc<sat_entry> ();
2136 83709371 : entry->atom = atom;
2137 83709371 : entry->args = args;
2138 83709371 : entry->result = NULL_TREE;
2139 83709371 : entry->inst_entry = nullptr;
2140 83709371 : entry->location = input_location;
2141 83709371 : entry->ftc_begin = entry->ftc_end = -1;
2142 83709371 : entry->diagnose_instability = false;
2143 83709371 : if (ATOMIC_CONSTR_MAP_INSTANTIATED_P (atom))
2144 : /* We always want to diagnose instability of an atom with an
2145 : instantiated parameter mapping. For atoms with an uninstantiated
2146 : mapping, we set this flag (in satisfy_atom) only if substitution
2147 : into its mapping previously failed. */
2148 27370359 : entry->diagnose_instability = true;
2149 83709371 : entry->evaluating = false;
2150 83709371 : *slot = entry;
2151 : }
2152 : else
2153 : {
2154 : /* We're evaluating this atom for the first time, and doing so noisily.
2155 : This shouldn't happen outside of error recovery situations involving
2156 : unstable satisfaction. Let's just leave 'entry' empty, effectively
2157 : disabling the cache, and remove the empty slot. */
2158 6 : gcc_checking_assert (seen_error ());
2159 : /* Appease hash_table::check_complete_insertion. */
2160 6 : *slot = ggc_alloc<sat_entry> ();
2161 6 : sat_cache->clear_slot (slot);
2162 : }
2163 : }
2164 :
2165 : /* Returns the cached satisfaction result if we have one and we're not
2166 : recomputing the satisfaction result from scratch. Otherwise returns
2167 : NULL_TREE. */
2168 :
2169 : tree
2170 522046038 : satisfaction_cache::get ()
2171 : {
2172 522046038 : if (!entry)
2173 : return NULL_TREE;
2174 :
2175 522046032 : if (entry->evaluating)
2176 : {
2177 : /* If we get here, it means satisfaction is self-recursive. */
2178 36 : gcc_checking_assert (!entry->result || seen_error ());
2179 : /* Prefer printing the instantiated mapping. */
2180 36 : tree atom = entry->inst_entry ? entry->inst_entry->atom : entry->atom;
2181 36 : if (info.noisy ())
2182 18 : error_at (cp_expr_location (ATOMIC_CONSTR_EXPR (atom)),
2183 : "satisfaction of atomic constraint %qE depends on itself",
2184 : atom);
2185 36 : return error_mark_node;
2186 : }
2187 :
2188 : /* This satisfaction result is "potentially unstable" if a type for which
2189 : type completion failed during its earlier computation is now complete. */
2190 522045996 : bool maybe_unstable = some_type_complete_p (entry->ftc_begin,
2191 : entry->ftc_end);
2192 :
2193 522045996 : if (info.noisy () || maybe_unstable || !entry->result)
2194 : {
2195 : /* We're computing the satisfaction result from scratch. */
2196 83716863 : entry->evaluating = true;
2197 83716863 : ftc_begin = vec_safe_length (failed_type_completions);
2198 83716863 : return NULL_TREE;
2199 : }
2200 : else
2201 : return entry->result;
2202 : }
2203 :
2204 : /* RESULT is the computed satisfaction result. If RESULT differs from the
2205 : previously cached result, this routine issues an appropriate error.
2206 : Otherwise, when evaluating quietly, updates the cache appropriately. */
2207 :
2208 : tree
2209 83711469 : satisfaction_cache::save (tree result)
2210 : {
2211 83711469 : if (!entry)
2212 : return result;
2213 :
2214 83711463 : gcc_checking_assert (entry->evaluating);
2215 83711463 : entry->evaluating = false;
2216 :
2217 83711463 : if (entry->result && result != entry->result)
2218 : {
2219 42 : if (info.quiet ())
2220 : /* Return error_mark_node to force satisfaction to get replayed
2221 : noisily. */
2222 21 : return error_mark_node;
2223 : else
2224 : {
2225 21 : if (entry->diagnose_instability)
2226 : {
2227 12 : auto_diagnostic_group d;
2228 12 : error_at (cp_expr_location (ATOMIC_CONSTR_EXPR (entry->atom)),
2229 : "satisfaction value of atomic constraint %qE changed "
2230 12 : "from %qE to %qE", entry->atom, entry->result, result);
2231 12 : inform (entry->location,
2232 : "satisfaction value first evaluated to %qE from here",
2233 12 : entry->result);
2234 12 : }
2235 : /* For sake of error recovery, allow this latest satisfaction result
2236 : to prevail. */
2237 21 : entry->result = result;
2238 21 : return result;
2239 : }
2240 : }
2241 :
2242 83711421 : if (info.quiet ())
2243 : {
2244 83703992 : entry->result = result;
2245 : /* Store into this entry the list of relevant failed type completions
2246 : that occurred during (re)computation of the satisfaction result. */
2247 83703992 : gcc_checking_assert (ftc_begin != -1);
2248 83703992 : entry->ftc_begin = ftc_begin;
2249 83892175 : entry->ftc_end = vec_safe_length (failed_type_completions);
2250 : }
2251 :
2252 : return result;
2253 : }
2254 :
2255 : /* Substitute ARGS into constraint-expression T during instantiation of
2256 : a member of a class template. */
2257 :
2258 : tree
2259 1667691 : tsubst_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
2260 : {
2261 : /* We also don't want to evaluate concept-checks when substituting the
2262 : constraint-expressions of a declaration. */
2263 1667691 : processing_constraint_expression_sentinel s;
2264 1667691 : cp_unevaluated u;
2265 1667691 : tree expr = tsubst_expr (t, args, complain, in_decl);
2266 3335382 : return expr;
2267 1667691 : }
2268 :
2269 : static tree satisfy_constraint_r (tree, tree, sat_info info);
2270 :
2271 : /* Compute the satisfaction of a conjunction. */
2272 :
2273 : static tree
2274 392152596 : satisfy_conjunction (tree t, tree args, sat_info info)
2275 : {
2276 392152596 : tree lhs = satisfy_constraint_r (TREE_OPERAND (t, 0), args, info);
2277 392152596 : if (lhs == error_mark_node || lhs == boolean_false_node)
2278 : return lhs;
2279 384166303 : return satisfy_constraint_r (TREE_OPERAND (t, 1), args, info);
2280 : }
2281 :
2282 : /* The current depth at which we're replaying an error during recursive
2283 : diagnosis of a constraint satisfaction failure. */
2284 :
2285 : static int current_constraint_diagnosis_depth;
2286 :
2287 : /* Whether CURRENT_CONSTRAINT_DIAGNOSIS_DEPTH has ever exceeded
2288 : CONCEPTS_DIAGNOSTICS_MAX_DEPTH during recursive diagnosis of a constraint
2289 : satisfaction error. */
2290 :
2291 : static bool concepts_diagnostics_max_depth_exceeded_p;
2292 :
2293 : /* Recursive subroutine of collect_operands_of_disjunction. T is a normalized
2294 : subexpression of a constraint (composed of CONJ_CONSTRs and DISJ_CONSTRs)
2295 : and E is the corresponding unnormalized subexpression (composed of
2296 : TRUTH_ANDIF_EXPRs and TRUTH_ORIF_EXPRs). */
2297 :
2298 : static void
2299 22 : collect_operands_of_disjunction_r (tree t, tree e,
2300 : auto_vec<tree_pair> *operands)
2301 : {
2302 36 : if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
2303 : {
2304 14 : collect_operands_of_disjunction_r (TREE_OPERAND (t, 0),
2305 14 : TREE_OPERAND (e, 0), operands);
2306 14 : collect_operands_of_disjunction_r (TREE_OPERAND (t, 1),
2307 14 : TREE_OPERAND (e, 1), operands);
2308 : }
2309 : else
2310 : {
2311 22 : tree_pair p = std::make_pair (t, e);
2312 22 : operands->safe_push (p);
2313 : }
2314 22 : }
2315 :
2316 : /* Recursively collect the normalized and unnormalized operands of the
2317 : disjunction T and append them to OPERANDS in order. */
2318 :
2319 : static void
2320 8 : collect_operands_of_disjunction (tree t, auto_vec<tree_pair> *operands)
2321 : {
2322 8 : collect_operands_of_disjunction_r (t, CONSTR_EXPR (t), operands);
2323 8 : }
2324 :
2325 : /* Compute the satisfaction of a disjunction. */
2326 :
2327 : static tree
2328 40906424 : satisfy_disjunction (tree t, tree args, sat_info info)
2329 : {
2330 : /* Evaluate each operand with unsatisfaction diagnostics disabled. */
2331 40906424 : sat_info sub = info;
2332 40906424 : sub.diagnose_unsatisfaction = false;
2333 :
2334 40906424 : tree lhs = satisfy_constraint_r (TREE_OPERAND (t, 0), args, sub);
2335 40906424 : if (lhs == boolean_true_node || lhs == error_mark_node)
2336 : return lhs;
2337 :
2338 20233578 : tree rhs = satisfy_constraint_r (TREE_OPERAND (t, 1), args, sub);
2339 20233578 : if (rhs == boolean_true_node || rhs == error_mark_node)
2340 : return rhs;
2341 :
2342 : /* Both branches evaluated to false. Explain the satisfaction failure in
2343 : each branch. */
2344 3623384 : if (info.diagnose_unsatisfaction_p ())
2345 : {
2346 69 : diagnosing_failed_constraint failure (t, args, info.noisy ());
2347 69 : cp_expr disj_expr = CONSTR_EXPR (t);
2348 69 : inform (disj_expr.get_location (),
2349 : "no operand of the disjunction is satisfied");
2350 69 : if (diagnosing_failed_constraint::replay_errors_p ())
2351 : {
2352 8 : auto_diagnostic_nesting_level sentinel;
2353 : /* Replay the error in each branch of the disjunction. */
2354 8 : auto_vec<tree_pair> operands;
2355 8 : collect_operands_of_disjunction (t, &operands);
2356 30 : for (unsigned i = 0; i < operands.length (); i++)
2357 : {
2358 22 : tree norm_op = operands[i].first;
2359 22 : tree op = operands[i].second;
2360 22 : location_t loc = make_location (cp_expr_location (op),
2361 : disj_expr.get_start (),
2362 : disj_expr.get_finish ());
2363 22 : inform (loc, "the operand %qE is unsatisfied because", op);
2364 22 : auto_diagnostic_nesting_level sentinel;
2365 22 : satisfy_constraint_r (norm_op, args, info);
2366 22 : }
2367 8 : }
2368 69 : }
2369 :
2370 3623384 : return boolean_false_node;
2371 : }
2372 :
2373 : /* Ensures that T is a truth value and not (accidentally, as sometimes
2374 : happens) an integer value. */
2375 :
2376 : tree
2377 27359900 : satisfaction_value (tree t)
2378 : {
2379 27359900 : if (t == error_mark_node || t == boolean_true_node || t == boolean_false_node)
2380 : return t;
2381 :
2382 2 : gcc_assert (TREE_CODE (t) == INTEGER_CST
2383 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t),
2384 : boolean_type_node));
2385 2 : if (integer_zerop (t))
2386 0 : return boolean_false_node;
2387 : else
2388 2 : return boolean_true_node;
2389 : }
2390 :
2391 : /* Build a new template argument vector corresponding to the parameter
2392 : mapping of the atomic constraint T, using arguments from ARGS. */
2393 :
2394 : static tree
2395 27374101 : get_mapped_args (tree t, tree args)
2396 : {
2397 27374101 : tree map = ATOMIC_CONSTR_MAP (t);
2398 :
2399 : /* No map, no arguments. */
2400 27374101 : if (!map)
2401 : return NULL_TREE;
2402 :
2403 : /* Determine the depth of the resulting argument vector. */
2404 27373248 : int depth;
2405 27373248 : if (ATOMIC_CONSTR_EXPR_FROM_CONCEPT_P (t))
2406 : /* The expression of this atomic constraint comes from a concept
2407 : definition, whose template depth is always one, so the resulting
2408 : argument vector will also have depth one. */
2409 : depth = 1;
2410 : else
2411 : /* Otherwise, the expression of this atomic constraint comes from
2412 : the context of the constrained entity, whose template depth is that
2413 : of ARGS. */
2414 20845274 : depth = TMPL_ARGS_DEPTH (args);
2415 :
2416 : /* Place each argument at its corresponding position in the argument
2417 : list. Note that the list will be sparse (not all arguments supplied),
2418 : but instantiation is guaranteed to only use the parameters in the
2419 : mapping, so null arguments would never be used. */
2420 27373248 : auto_vec< vec<tree> > lists (depth);
2421 27373248 : lists.quick_grow_cleared (depth);
2422 69838245 : for (tree p = map; p; p = TREE_CHAIN (p))
2423 : {
2424 42464997 : int level;
2425 42464997 : int index;
2426 42464997 : template_parm_level_and_index (TREE_VALUE (p), &level, &index);
2427 :
2428 : /* Insert the argument into its corresponding position. */
2429 42464997 : vec<tree> &list = lists[level - 1];
2430 56760896 : if (index >= (int)list.length ())
2431 39122477 : list.safe_grow_cleared (index + 1, /*exact=*/false);
2432 42464997 : list[index] = TREE_PURPOSE (p);
2433 : }
2434 :
2435 : /* Build the new argument list. */
2436 27373248 : args = make_tree_vec (lists.length ());
2437 111974282 : for (unsigned i = 0; i != lists.length (); ++i)
2438 : {
2439 28613893 : vec<tree> &list = lists[i];
2440 28613893 : tree level = make_tree_vec (list.length ());
2441 72090340 : for (unsigned j = 0; j < list.length (); ++j)
2442 43476447 : TREE_VEC_ELT (level, j) = list[j];
2443 28613893 : SET_TMPL_ARGS_LEVEL (args, i + 1, level);
2444 28613893 : list.release ();
2445 : }
2446 27373248 : SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, 0);
2447 :
2448 27373248 : if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
2449 27373248 : && TMPL_ARGS_DEPTH (args) == 1)
2450 : {
2451 : /* Get rid of the redundant outer TREE_VEC. */
2452 26140532 : tree level = TMPL_ARGS_LEVEL (args, 1);
2453 26140532 : ggc_free (args);
2454 26140532 : args = level;
2455 : }
2456 :
2457 27373248 : return args;
2458 27373248 : }
2459 :
2460 : static void diagnose_atomic_constraint (tree, tree, tree, sat_info);
2461 :
2462 : /* Compute the satisfaction of an atomic constraint. */
2463 :
2464 : static tree
2465 465716517 : satisfy_atom (tree t, tree args, sat_info info)
2466 : {
2467 : /* In case there is a diagnostic, we want to establish the context
2468 : prior to printing errors. If no errors occur, this context is
2469 : removed before returning. */
2470 465716517 : diagnosing_failed_constraint failure (t, args, info.noisy ());
2471 :
2472 465716517 : satisfaction_cache cache (t, args, info);
2473 465716517 : if (tree r = cache.get ())
2474 : return r;
2475 :
2476 : /* Perform substitution quietly. */
2477 56342768 : subst_info quiet (tf_none, NULL_TREE);
2478 :
2479 : /* Instantiate the parameter mapping. */
2480 56342768 : tree map = tsubst_parameter_mapping (ATOMIC_CONSTR_MAP (t), args, quiet);
2481 56342768 : if (map == error_mark_node)
2482 : {
2483 : /* If instantiation of the parameter mapping fails, the constraint is
2484 : not satisfied. Replay the substitution. */
2485 13247 : if (info.diagnose_unsatisfaction_p ())
2486 8 : tsubst_parameter_mapping (ATOMIC_CONSTR_MAP (t), args, info);
2487 13247 : if (info.quiet ())
2488 : /* Since instantiation of the parameter mapping failed, we
2489 : want to diagnose potential instability of this satisfaction
2490 : result. */
2491 13239 : cache.entry->diagnose_instability = true;
2492 13247 : return cache.save (boolean_false_node);
2493 : }
2494 :
2495 : /* Now build a new atom using the instantiated mapping. We use
2496 : this atom as a second key to the satisfaction cache, and we
2497 : also pass it to diagnose_atomic_constraint so that diagnostics
2498 : which refer to the atom display the instantiated mapping. */
2499 56329521 : t = copy_node (t);
2500 56329521 : ATOMIC_CONSTR_MAP (t) = map;
2501 56329521 : gcc_assert (!ATOMIC_CONSTR_MAP_INSTANTIATED_P (t));
2502 56329521 : ATOMIC_CONSTR_MAP_INSTANTIATED_P (t) = true;
2503 56329521 : satisfaction_cache inst_cache (t, /*args=*/NULL_TREE, info);
2504 56329521 : if (cache.entry && inst_cache.entry)
2505 56329518 : cache.entry->inst_entry = inst_cache.entry;
2506 56329521 : if (tree r = inst_cache.get ())
2507 : {
2508 28955420 : cache.entry->location = inst_cache.entry->location;
2509 28955420 : return cache.save (r);
2510 : }
2511 :
2512 : /* Rebuild the argument vector from the parameter mapping. */
2513 27374101 : args = get_mapped_args (t, args);
2514 :
2515 : /* Apply the parameter mapping (i.e., just substitute). */
2516 27374101 : tree expr = ATOMIC_CONSTR_EXPR (t);
2517 27374101 : tree result = tsubst_expr (expr, args, quiet.complain, quiet.in_decl);
2518 27371401 : if (result == error_mark_node)
2519 : {
2520 : /* If substitution results in an invalid type or expression, the
2521 : constraint is not satisfied. Replay the substitution. */
2522 11443 : if (info.diagnose_unsatisfaction_p ())
2523 20 : tsubst_expr (expr, args, info.complain, info.in_decl);
2524 11443 : return cache.save (inst_cache.save (boolean_false_node));
2525 : }
2526 :
2527 : /* [17.4.1.2] ... lvalue-to-rvalue conversion is performed as necessary,
2528 : and EXPR shall be a constant expression of type bool. */
2529 27359958 : result = force_rvalue (result, info.complain);
2530 27359958 : if (result == error_mark_node)
2531 0 : return cache.save (inst_cache.save (error_mark_node));
2532 27359958 : tree substituted = result;
2533 27359958 : if (!same_type_p (TREE_TYPE (result), boolean_type_node))
2534 : {
2535 58 : if (info.noisy ())
2536 32 : diagnose_atomic_constraint (t, args, substituted, info);
2537 58 : return cache.save (inst_cache.save (error_mark_node));
2538 : }
2539 :
2540 : /* Compute the value of the constraint. */
2541 27359900 : if (info.noisy ())
2542 : {
2543 3663 : iloc_sentinel ils (EXPR_LOCATION (result));
2544 3663 : result = cxx_constant_value (result);
2545 3663 : }
2546 : else
2547 : {
2548 27356237 : result = maybe_constant_value (result, NULL_TREE, mce_true);
2549 27356237 : if (!TREE_CONSTANT (result))
2550 22 : result = error_mark_node;
2551 : }
2552 27359900 : result = satisfaction_value (result);
2553 27359900 : if (result == boolean_false_node && info.diagnose_unsatisfaction_p ())
2554 1234 : diagnose_atomic_constraint (t, args, substituted, info);
2555 :
2556 27359900 : return cache.save (inst_cache.save (result));
2557 465713817 : }
2558 :
2559 : /* Determine if the normalized constraint T is satisfied.
2560 : Returns boolean_true_node if the expression/constraint is
2561 : satisfied, boolean_false_node if not, and error_mark_node
2562 : if there was an error evaluating the constraint.
2563 :
2564 : The parameter mapping of atomic constraints is simply the
2565 : set of template arguments that will be substituted into
2566 : the expression, regardless of template parameters appearing
2567 : within. Whether a template argument is used in the atomic
2568 : constraint only matters for subsumption. */
2569 :
2570 : static tree
2571 898775684 : satisfy_constraint_r (tree t, tree args, sat_info info)
2572 : {
2573 898775684 : if (t == error_mark_node)
2574 : return error_mark_node;
2575 :
2576 898775537 : switch (TREE_CODE (t))
2577 : {
2578 392152596 : case CONJ_CONSTR:
2579 392152596 : return satisfy_conjunction (t, args, info);
2580 40906424 : case DISJ_CONSTR:
2581 40906424 : return satisfy_disjunction (t, args, info);
2582 465716517 : case ATOMIC_CONSTR:
2583 465716517 : return satisfy_atom (t, args, info);
2584 0 : default:
2585 0 : gcc_unreachable ();
2586 : }
2587 : }
2588 :
2589 : /* Check that the normalized constraint T is satisfied for ARGS. */
2590 :
2591 : static tree
2592 61316761 : satisfy_normalized_constraints (tree t, tree args, sat_info info)
2593 : {
2594 61316761 : auto_timevar time (TV_CONSTRAINT_SAT);
2595 :
2596 61316761 : auto ovr = make_temp_override (satisfying_constraint, true);
2597 :
2598 : /* Turn off template processing. Constraint satisfaction only applies
2599 : to non-dependent terms, so we want to ensure full checking here. */
2600 61316761 : processing_template_decl_sentinel proc (true);
2601 :
2602 : /* We need to check access during satisfaction. */
2603 61316761 : deferring_access_check_sentinel acs (dk_no_deferred);
2604 :
2605 : /* Constraints are unevaluated operands. */
2606 61316761 : cp_unevaluated u;
2607 :
2608 61316761 : return satisfy_constraint_r (t, args, info);
2609 61314061 : }
2610 :
2611 : /* Return the normal form of the constraints on the placeholder 'auto'
2612 : type T. */
2613 :
2614 : static tree
2615 8547919 : normalize_placeholder_type_constraints (tree t, bool diag)
2616 : {
2617 8547919 : gcc_assert (is_auto (t));
2618 8547919 : tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t);
2619 8547919 : if (!ci)
2620 : return NULL_TREE;
2621 :
2622 8547919 : tree constr = TREE_VALUE (ci);
2623 : /* The TREE_PURPOSE contains the set of template parameters that were in
2624 : scope for this placeholder type; use them as the initial template
2625 : parameters for normalization. */
2626 8547919 : tree initial_parms = TREE_PURPOSE (ci);
2627 :
2628 : /* The 'auto' itself is used as the first argument in its own constraints,
2629 : and its level is one greater than its template depth. So in order to
2630 : capture all used template parameters, we need to add an extra level of
2631 : template parameters to the context; a dummy level suffices. */
2632 8547919 : initial_parms
2633 17095574 : = tree_cons (size_int (initial_parms
2634 : ? TMPL_PARMS_DEPTH (initial_parms) + 1 : 1),
2635 : make_tree_vec (0), initial_parms);
2636 :
2637 8547919 : norm_info info (diag);
2638 8547919 : info.initial_parms = initial_parms;
2639 8547919 : return normalize_constraint_expression (constr, info);
2640 : }
2641 :
2642 : /* Evaluate the constraints of T using ARGS, returning a satisfaction value.
2643 : Here, T can be a concept-id, nested-requirement, placeholder 'auto', or
2644 : requires-expression. */
2645 :
2646 : static tree
2647 12091552 : satisfy_nondeclaration_constraints (tree t, tree args, sat_info info)
2648 : {
2649 12091552 : if (t == error_mark_node)
2650 : return error_mark_node;
2651 :
2652 : /* Handle REQUIRES_EXPR directly, bypassing satisfaction. */
2653 12091548 : if (TREE_CODE (t) == REQUIRES_EXPR)
2654 : {
2655 189 : auto ovr = make_temp_override (current_constraint_diagnosis_depth);
2656 189 : if (info.noisy ())
2657 35 : ++current_constraint_diagnosis_depth;
2658 189 : return tsubst_requires_expr (t, args, info);
2659 189 : }
2660 :
2661 : /* Get the normalized constraints. */
2662 12091359 : tree norm;
2663 12091359 : if (concept_check_p (t))
2664 : {
2665 2712672 : gcc_assert (!args);
2666 2712672 : args = TREE_OPERAND (t, 1);
2667 2712672 : tree tmpl = get_concept_check_template (t);
2668 2712672 : norm = normalize_concept_definition (tmpl, info.noisy ());
2669 : }
2670 9378687 : else if (TREE_CODE (t) == NESTED_REQ)
2671 : {
2672 830768 : norm_info ninfo (info.noisy ());
2673 : /* The TREE_TYPE contains the set of template parameters that were in
2674 : scope for this nested requirement; use them as the initial template
2675 : parameters for normalization. */
2676 830768 : ninfo.initial_parms = TREE_TYPE (t);
2677 830768 : norm = normalize_constraint_expression (TREE_OPERAND (t, 0), ninfo);
2678 : }
2679 8547919 : else if (is_auto (t))
2680 : {
2681 8547919 : norm = normalize_placeholder_type_constraints (t, info.noisy ());
2682 8547919 : if (!norm)
2683 0 : return boolean_true_node;
2684 : }
2685 : else
2686 0 : gcc_unreachable ();
2687 :
2688 : /* Perform satisfaction. */
2689 12091359 : return satisfy_normalized_constraints (norm, args, info);
2690 : }
2691 :
2692 : /* Evaluate the associated constraints of the template specialization T
2693 : according to INFO, returning a satisfaction value. */
2694 :
2695 : static tree
2696 660737221 : satisfy_declaration_constraints (tree t, sat_info info)
2697 : {
2698 660737221 : gcc_assert (DECL_P (t) && TREE_CODE (t) != TEMPLATE_DECL);
2699 660737221 : const tree saved_t = t;
2700 :
2701 : /* For inherited constructors, consider the original declaration;
2702 : it has the correct template information attached. */
2703 660737221 : t = strip_inheriting_ctors (t);
2704 660737221 : tree inh_ctor_targs = NULL_TREE;
2705 660737221 : if (t != saved_t)
2706 324616 : if (tree ti = DECL_TEMPLATE_INFO (saved_t))
2707 : /* The inherited constructor points to an instantiation of a constructor
2708 : template; remember its template arguments. */
2709 34410 : inh_ctor_targs = TI_ARGS (ti);
2710 :
2711 : /* Update the declaration for diagnostics. */
2712 660737221 : info.in_decl = t;
2713 :
2714 660737221 : if (info.quiet ())
2715 1321327506 : if (tree *result = hash_map_safe_get (decl_satisfied_cache, saved_t))
2716 540802125 : return *result;
2717 :
2718 119935096 : tree args = NULL_TREE;
2719 119935096 : if (tree ti = DECL_TEMPLATE_INFO (t))
2720 : {
2721 : /* The initial parameter mapping is the complete set of
2722 : template arguments substituted into the declaration. */
2723 81276470 : args = TI_ARGS (ti);
2724 81276470 : if (inh_ctor_targs)
2725 14466 : args = add_outermost_template_args (args, inh_ctor_targs);
2726 : }
2727 :
2728 119935096 : if (regenerated_lambda_fn_p (t))
2729 : {
2730 : /* The TI_ARGS of a regenerated lambda contains only the innermost
2731 : set of template arguments. Augment this with the outer template
2732 : arguments that were used to regenerate the lambda. */
2733 450202 : gcc_assert (!args || TMPL_ARGS_DEPTH (args) == 1);
2734 316802 : tree regen_args = lambda_regenerating_args (t);
2735 316802 : if (args)
2736 133400 : args = add_to_template_args (regen_args, args);
2737 : else
2738 : args = regen_args;
2739 : }
2740 :
2741 : /* If the innermost arguments are dependent, or if the outer arguments
2742 : are dependent and are needed by the constraints, we can't check
2743 : satisfaction yet so pretend they're satisfied for now. */
2744 119935096 : if (uses_template_parms (args)
2745 119935096 : && ((DECL_TEMPLATE_INFO (t)
2746 326204 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))
2747 103752 : && (TMPL_ARGS_DEPTH (args) == 1
2748 24775 : || uses_template_parms (INNERMOST_TEMPLATE_ARGS (args))))
2749 299146 : || uses_outer_template_parms_in_constraints (t)))
2750 27140 : return boolean_true_node;
2751 :
2752 : /* Get the normalized constraints. */
2753 119907956 : tree norm = get_normalized_constraints_from_decl (t, info.noisy ());
2754 :
2755 119907956 : unsigned ftc_count = vec_safe_length (failed_type_completions);
2756 :
2757 119907956 : tree result = boolean_true_node;
2758 119907956 : if (norm)
2759 : {
2760 2908108 : if (!push_tinst_level (t))
2761 : return result;
2762 2907197 : push_to_top_level ();
2763 2907197 : push_access_scope (t);
2764 2907197 : result = satisfy_normalized_constraints (norm, args, info);
2765 2907197 : pop_access_scope (t);
2766 2907197 : pop_from_top_level ();
2767 2907197 : pop_tinst_level ();
2768 : }
2769 :
2770 : /* True if this satisfaction is (heuristically) potentially unstable, i.e.
2771 : if its result may depend on where in the program it was performed. */
2772 119907045 : bool maybe_unstable_satisfaction = false;
2773 119995958 : if (ftc_count != vec_safe_length (failed_type_completions))
2774 : /* Type completion failure occurred during satisfaction. The satisfaction
2775 : result may (or may not) materially depend on the completeness of a type,
2776 : so we consider it potentially unstable. */
2777 : maybe_unstable_satisfaction = true;
2778 :
2779 119907045 : if (maybe_unstable_satisfaction)
2780 : /* Don't cache potentially unstable satisfaction, to allow satisfy_atom
2781 : to check the stability the next time around. */;
2782 119907045 : else if (info.quiet ())
2783 119906955 : hash_map_safe_put<hm_ggc> (decl_satisfied_cache, saved_t, result);
2784 :
2785 119907045 : return result;
2786 : }
2787 :
2788 : /* Evaluate the associated constraints of the template T using ARGS as the
2789 : innermost set of template arguments and according to INFO, returning a
2790 : satisfaction value. */
2791 :
2792 : static tree
2793 399708110 : satisfy_declaration_constraints (tree t, tree args, sat_info info)
2794 : {
2795 399708110 : tree orig_args = args;
2796 :
2797 : /* Update the declaration for diagnostics. */
2798 399708110 : info.in_decl = t;
2799 :
2800 399708110 : gcc_assert (TREE_CODE (t) == TEMPLATE_DECL);
2801 :
2802 399708110 : if (regenerated_lambda_fn_p (t))
2803 : {
2804 : /* As in the two-parameter version of this function. */
2805 336040 : gcc_assert (TMPL_ARGS_DEPTH (args) == 1);
2806 168020 : tree lambda = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t));
2807 168020 : tree outer_args = TI_ARGS (LAMBDA_EXPR_REGEN_INFO (lambda));
2808 168020 : args = add_to_template_args (outer_args, args);
2809 : }
2810 : else
2811 399540090 : args = add_outermost_template_args (t, args);
2812 :
2813 : /* If the innermost arguments are dependent, or if the outer arguments
2814 : are dependent and are needed by the constraints, we can't check
2815 : satisfaction yet so pretend they're satisfied for now. */
2816 399708110 : if (uses_template_parms (args)
2817 520158966 : && (TMPL_ARGS_DEPTH (args) == 1
2818 7723456 : || uses_template_parms (INNERMOST_TEMPLATE_ARGS (args))
2819 368222 : || uses_outer_template_parms_in_constraints (t)))
2820 120082634 : return boolean_true_node;
2821 :
2822 279625476 : tree result = boolean_true_node;
2823 279625476 : if (tree norm = get_normalized_constraints_from_decl (t, info.noisy ()))
2824 : {
2825 46318205 : if (!push_tinst_level (t, orig_args))
2826 : return result;
2827 46318205 : tree pattern = DECL_TEMPLATE_RESULT (t);
2828 46318205 : push_to_top_level ();
2829 46318205 : push_access_scope (pattern);
2830 46318205 : {
2831 : /* For reconstruct_lambda_capture_pack. */
2832 47381127 : local_specialization_stack lss (LAMBDA_FUNCTION_P (t)
2833 92636410 : ? lss_blank : lss_nop);
2834 46318205 : result = satisfy_normalized_constraints (norm, args, info);
2835 46315505 : }
2836 46315505 : pop_access_scope (pattern);
2837 46315505 : pop_from_top_level ();
2838 46315505 : pop_tinst_level ();
2839 : }
2840 :
2841 : return result;
2842 : }
2843 :
2844 : /* A wrapper around satisfy_declaration_constraints and
2845 : satisfy_nondeclaration_constraints which additionally replays
2846 : quiet ill-formed satisfaction noisily, so that ill-formed
2847 : satisfaction always gets diagnosed. */
2848 :
2849 : static tree
2850 1072536883 : constraint_satisfaction_value (tree t, tree args, sat_info info)
2851 : {
2852 1072536883 : tree r;
2853 1072536883 : if (DECL_P (t))
2854 : {
2855 1060445331 : if (args)
2856 399708110 : r = satisfy_declaration_constraints (t, args, info);
2857 : else
2858 660737221 : r = satisfy_declaration_constraints (t, info);
2859 : }
2860 : else
2861 12091552 : r = satisfy_nondeclaration_constraints (t, args, info);
2862 330 : if (r == error_mark_node && info.quiet ()
2863 1072534336 : && !(DECL_P (t) && warning_suppressed_p (t)))
2864 : {
2865 : /* Replay the error noisily. */
2866 153 : sat_info noisy (tf_warning_or_error, info.in_decl);
2867 153 : constraint_satisfaction_value (t, args, noisy);
2868 153 : if (DECL_P (t) && !args)
2869 : /* Avoid giving these errors again. */
2870 0 : suppress_warning (t);
2871 : }
2872 1072534183 : return r;
2873 : }
2874 :
2875 : /* True iff the result of satisfying T using ARGS is BOOLEAN_TRUE_NODE
2876 : and false otherwise, even in the case of errors.
2877 :
2878 : Here, T can be:
2879 : - a template declaration
2880 : - a template specialization (in which case ARGS must be empty)
2881 : - a concept-id (in which case ARGS must be empty)
2882 : - a nested-requirement
2883 : - a placeholder 'auto'
2884 : - a requires-expression. */
2885 :
2886 : bool
2887 1070719765 : constraints_satisfied_p (tree t, tree args/*= NULL_TREE */)
2888 : {
2889 1070719765 : if (!flag_concepts)
2890 : return true;
2891 :
2892 1068992096 : sat_info quiet (tf_none, NULL_TREE);
2893 1068992096 : return constraint_satisfaction_value (t, args, quiet) == boolean_true_node;
2894 : }
2895 :
2896 : /* Evaluate a concept check of the form C<ARGS>. This is only used for the
2897 : evaluation of template-ids as id-expressions. */
2898 :
2899 : tree
2900 2712362 : evaluate_concept_check (tree check)
2901 : {
2902 2712362 : if (check == error_mark_node)
2903 : return error_mark_node;
2904 :
2905 2712362 : gcc_assert (concept_check_p (check));
2906 :
2907 : /* We don't want any declarations instantiated from a concept evaluation
2908 : to enter the binding table for the current scope, such as lambdas, so
2909 : leave that scope. But maintain the access context (PR104111). */
2910 2712362 : tree scope = current_scope ();
2911 2712362 : if (CLASS_TYPE_P (scope))
2912 639328 : scope = TYPE_MAIN_DECL (scope);
2913 2073034 : else if (TREE_CODE (scope) != FUNCTION_DECL)
2914 183682 : scope = NULL_TREE;
2915 :
2916 639328 : push_to_top_level ();
2917 2712362 : if (scope)
2918 2528680 : push_access_scope (scope);
2919 :
2920 : /* Check for satisfaction without diagnostics. */
2921 2712362 : sat_info quiet (tf_none, NULL_TREE);
2922 2712362 : tree r = constraint_satisfaction_value (check, /*args=*/NULL_TREE, quiet);
2923 :
2924 2712362 : if (scope)
2925 2528680 : pop_access_scope (scope);
2926 2712362 : pop_from_top_level ();
2927 2712362 : return r;
2928 : }
2929 :
2930 : /* Evaluate the requires-expression T, returning either boolean_true_node
2931 : or boolean_false_node. This is used during folding and constexpr
2932 : evaluation. */
2933 :
2934 : tree
2935 154 : evaluate_requires_expr (tree t)
2936 : {
2937 154 : gcc_assert (TREE_CODE (t) == REQUIRES_EXPR);
2938 154 : sat_info quiet (tf_none, NULL_TREE);
2939 154 : return constraint_satisfaction_value (t, /*args=*/NULL_TREE, quiet);
2940 : }
2941 :
2942 : /*---------------------------------------------------------------------------
2943 : Semantic analysis of requires-expressions
2944 : ---------------------------------------------------------------------------*/
2945 :
2946 : /* Finish a requires expression for the given PARMS (possibly
2947 : null) and the non-empty sequence of requirements. */
2948 :
2949 : tree
2950 3030493 : finish_requires_expr (location_t loc, tree parms, tree reqs)
2951 : {
2952 : /* Build the node. */
2953 3030493 : tree r = make_node (REQUIRES_EXPR);
2954 3030493 : TREE_TYPE (r) = boolean_type_node;
2955 3030493 : REQUIRES_EXPR_PARMS (r) = parms;
2956 3030493 : REQUIRES_EXPR_REQS (r) = reqs;
2957 3030493 : REQUIRES_EXPR_LOCATION (r) = loc;
2958 3030493 : TREE_SIDE_EFFECTS (r) = false;
2959 3030493 : TREE_CONSTANT (r) = true;
2960 3030493 : return r;
2961 : }
2962 :
2963 : /* Construct a requirement for the validity of EXPR. */
2964 :
2965 : tree
2966 1609926 : finish_simple_requirement (location_t loc, tree expr)
2967 : {
2968 1609926 : tree r = build_nt (SIMPLE_REQ, expr);
2969 1609926 : SET_EXPR_LOCATION (r, loc);
2970 1609926 : return r;
2971 : }
2972 :
2973 : /* Construct a requirement for the validity of TYPE. */
2974 :
2975 : tree
2976 800500 : finish_type_requirement (location_t loc, tree type)
2977 : {
2978 800500 : tree r = build_nt (TYPE_REQ, type);
2979 800500 : SET_EXPR_LOCATION (r, loc);
2980 800500 : return r;
2981 : }
2982 :
2983 : /* Construct a requirement for the validity of EXPR, along with
2984 : its properties. If TYPE is non-null, then it specifies either
2985 : an implicit conversion or argument deduction constraint,
2986 : depending on whether any placeholders occur in the type name.
2987 : NOEXCEPT_P is true iff the noexcept keyword was specified. */
2988 :
2989 : tree
2990 1515400 : finish_compound_requirement (location_t loc, tree expr, tree type, bool noexcept_p)
2991 : {
2992 1515400 : tree req = build_nt (COMPOUND_REQ, expr, type);
2993 1515400 : SET_EXPR_LOCATION (req, loc);
2994 1515400 : COMPOUND_REQ_NOEXCEPT_P (req) = noexcept_p;
2995 1515400 : return req;
2996 : }
2997 :
2998 : /* Finish a nested requirement. */
2999 :
3000 : tree
3001 258904 : finish_nested_requirement (location_t loc, tree expr)
3002 : {
3003 : /* Build the requirement, saving the set of in-scope template
3004 : parameters as its type. */
3005 258904 : tree r = build1 (NESTED_REQ, current_template_parms, expr);
3006 258904 : SET_EXPR_LOCATION (r, loc);
3007 258904 : return r;
3008 : }
3009 :
3010 : /*---------------------------------------------------------------------------
3011 : Equivalence of constraints
3012 : ---------------------------------------------------------------------------*/
3013 :
3014 : /* Returns true when A and B are equivalent constraints. */
3015 : bool
3016 29907560 : equivalent_constraints (tree a, tree b)
3017 : {
3018 29907560 : gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
3019 29907560 : gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
3020 29907560 : return cp_tree_equal (a, b);
3021 : }
3022 :
3023 : /* Returns true if the template declarations A and B have equivalent
3024 : constraints. This is the case when A's constraints subsume B's and
3025 : when B's also constrain A's. */
3026 : bool
3027 146 : equivalently_constrained (tree d1, tree d2)
3028 : {
3029 146 : gcc_assert (TREE_CODE (d1) == TREE_CODE (d2));
3030 146 : return equivalent_constraints (get_constraints (d1), get_constraints (d2));
3031 : }
3032 :
3033 : /*---------------------------------------------------------------------------
3034 : Partial ordering of constraints
3035 : ---------------------------------------------------------------------------*/
3036 :
3037 : /* Returns true when the constraints in CI strictly subsume
3038 : the associated constraints of TMPL. */
3039 :
3040 : bool
3041 688778 : strictly_subsumes (tree ci, tree tmpl)
3042 : {
3043 688778 : tree n1 = get_normalized_constraints_from_info (ci, NULL_TREE);
3044 688778 : tree n2 = get_normalized_constraints_from_decl (tmpl);
3045 :
3046 688778 : return subsumes (n1, n2) && !subsumes (n2, n1);
3047 : }
3048 :
3049 : /* Returns true when the template template parameter constraints in CI
3050 : subsume the associated constraints of the template template argument
3051 : TMPL. */
3052 :
3053 : bool
3054 87 : ttp_subsumes (tree ci, tree tmpl)
3055 : {
3056 87 : tree n1 = get_normalized_constraints_from_info (ci, tmpl);
3057 87 : tree n2 = get_normalized_constraints_from_decl (tmpl);
3058 :
3059 87 : return subsumes (n1, n2);
3060 : }
3061 :
3062 : /* Determines which of the declarations, A or B, is more constrained.
3063 : That is, which declaration's constraints subsume but are not subsumed
3064 : by the other's?
3065 :
3066 : Returns 1 if D1 is more constrained than D2, -1 if D2 is more constrained
3067 : than D1, and 0 otherwise. */
3068 :
3069 : int
3070 2441405 : more_constrained (tree d1, tree d2)
3071 : {
3072 2441405 : tree n1 = get_normalized_constraints_from_decl (d1);
3073 2441405 : tree n2 = get_normalized_constraints_from_decl (d2);
3074 :
3075 2441405 : int winner = 0;
3076 2441405 : if (subsumes (n1, n2))
3077 2129516 : ++winner;
3078 2441405 : if (subsumes (n2, n1))
3079 1773153 : --winner;
3080 2441405 : return winner;
3081 : }
3082 :
3083 : /* Return whether D1 is at least as constrained as D2. */
3084 :
3085 : bool
3086 8928301 : at_least_as_constrained (tree d1, tree d2)
3087 : {
3088 8928301 : tree n1 = get_normalized_constraints_from_decl (d1);
3089 8928301 : tree n2 = get_normalized_constraints_from_decl (d2);
3090 :
3091 8928301 : return subsumes (n1, n2);
3092 : }
3093 :
3094 : /*---------------------------------------------------------------------------
3095 : Constraint diagnostics
3096 : ---------------------------------------------------------------------------*/
3097 :
3098 : /* Returns the best location to diagnose a constraint error. */
3099 :
3100 : static location_t
3101 1266 : get_constraint_error_location (tree t)
3102 : {
3103 1266 : if (location_t loc = cp_expr_location (t))
3104 : return loc;
3105 :
3106 : /* If we have a specific location give it. */
3107 1266 : tree expr = CONSTR_EXPR (t);
3108 1266 : if (location_t loc = cp_expr_location (expr))
3109 : return loc;
3110 :
3111 : /* If the constraint is normalized from a requires-clause, give
3112 : the location as that of the constrained declaration. */
3113 80 : tree cxt = CONSTR_CONTEXT (t);
3114 80 : tree src = cxt ? TREE_VALUE (cxt) : NULL_TREE;
3115 75 : if (!src)
3116 : /* TODO: This only happens for constrained non-template declarations. */
3117 : ;
3118 75 : else if (DECL_P (src))
3119 60 : return DECL_SOURCE_LOCATION (src);
3120 : /* Otherwise, give the location as the defining concept. */
3121 15 : else if (concept_check_p (src))
3122 : {
3123 15 : tree tmpl = TREE_OPERAND (src, 0);
3124 15 : return DECL_SOURCE_LOCATION (tmpl);
3125 : }
3126 :
3127 5 : return input_location;
3128 : }
3129 :
3130 : /* Emit a diagnostic for a failed trait. */
3131 :
3132 : void
3133 809 : diagnose_trait_expr (location_t loc, tree expr, tree args)
3134 : {
3135 : /* Build a "fake" version of the instantiated trait, so we can
3136 : get the instantiated types from result. */
3137 809 : ++processing_template_decl;
3138 809 : expr = tsubst_expr (expr, args, tf_none, NULL_TREE);
3139 809 : --processing_template_decl;
3140 :
3141 809 : tree t1 = TRAIT_EXPR_TYPE1 (expr);
3142 809 : tree t2 = TRAIT_EXPR_TYPE2 (expr);
3143 809 : gcc_checking_assert (t1 != error_mark_node && t2 != error_mark_node);
3144 :
3145 809 : iloc_sentinel ils (loc);
3146 :
3147 : /* For traits intrinsically about the properties of user-defined types,
3148 : decl_loc will point to the declaration of that type. */
3149 809 : location_t decl_loc = location_of (t1);
3150 809 : if (decl_loc == input_location)
3151 523 : decl_loc = loc;
3152 :
3153 809 : switch (TRAIT_EXPR_KIND (expr))
3154 : {
3155 4 : case CPTK_HAS_NOTHROW_ASSIGN:
3156 4 : inform (decl_loc, "%qT is not nothrow copy assignable", t1);
3157 4 : break;
3158 4 : case CPTK_HAS_NOTHROW_CONSTRUCTOR:
3159 4 : inform (decl_loc, "%qT is not nothrow default constructible", t1);
3160 4 : break;
3161 4 : case CPTK_HAS_NOTHROW_COPY:
3162 4 : inform (decl_loc, "%qT is not nothrow copy constructible", t1);
3163 4 : break;
3164 4 : case CPTK_HAS_TRIVIAL_ASSIGN:
3165 4 : inform (decl_loc, "%qT is not trivially copy assignable", t1);
3166 4 : break;
3167 4 : case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
3168 4 : inform (decl_loc, "%qT is not trivially default constructible", t1);
3169 4 : break;
3170 4 : case CPTK_HAS_TRIVIAL_COPY:
3171 4 : inform (decl_loc, "%qT is not trivially copy constructible", t1);
3172 4 : break;
3173 4 : case CPTK_HAS_TRIVIAL_DESTRUCTOR:
3174 4 : inform (decl_loc, "%qT is not trivially destructible", t1);
3175 4 : break;
3176 30 : case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
3177 30 : inform (decl_loc, "%qT does not have unique object "
3178 : "representations, because", t1);
3179 30 : type_has_unique_obj_representations (t1, /*explain=*/true);
3180 30 : break;
3181 19 : case CPTK_HAS_VIRTUAL_DESTRUCTOR:
3182 19 : {
3183 19 : location_t dtor_loc = decl_loc;
3184 19 : if (NON_UNION_CLASS_TYPE_P (t1))
3185 6 : if (tree dtor = CLASSTYPE_DESTRUCTOR (t1))
3186 3 : dtor_loc = DECL_SOURCE_LOCATION (dtor);
3187 19 : inform (dtor_loc, "%qT does not have a virtual destructor", t1);
3188 : }
3189 19 : break;
3190 4 : case CPTK_IS_ABSTRACT:
3191 4 : inform (decl_loc, "%qT is not an abstract class", t1);
3192 4 : break;
3193 3 : case CPTK_IS_AGGREGATE:
3194 3 : inform (decl_loc, "%qT is not an aggregate", t1);
3195 3 : break;
3196 0 : case CPTK_IS_ARRAY:
3197 0 : inform (loc, "%qT is not an array", t1);
3198 0 : break;
3199 15 : case CPTK_IS_ASSIGNABLE:
3200 15 : inform (loc, "%qT is not assignable from %qT, because", t1, t2);
3201 15 : is_xible (MODIFY_EXPR, t1, t2, /*explain=*/true);
3202 15 : break;
3203 1 : case CPTK_IS_BASE_OF:
3204 1 : inform (location_of (t2), "%qT is not a base of %qT", t1, t2);
3205 1 : break;
3206 0 : case CPTK_IS_BOUNDED_ARRAY:
3207 0 : inform (loc, "%qT is not a bounded array", t1);
3208 0 : break;
3209 103 : case CPTK_IS_CLASS:
3210 103 : inform (decl_loc, "%qT is not a class", t1);
3211 103 : break;
3212 0 : case CPTK_IS_CONST:
3213 0 : inform (loc, "%qT is not a const type", t1);
3214 0 : break;
3215 141 : case CPTK_IS_CONSTRUCTIBLE:
3216 141 : if (!TREE_VEC_LENGTH (t2))
3217 76 : inform (loc, "%qT is not default constructible, because", t1);
3218 : else
3219 65 : inform (loc, "%qT is not constructible from %qT, because", t1, t2);
3220 141 : is_xible (INIT_EXPR, t1, t2, /*explain=*/true);
3221 141 : break;
3222 17 : case CPTK_IS_CONVERTIBLE:
3223 : /* The errors produced here all seem to mention "convertible" in the
3224 : diagnostic, so an extra inform here appears redundant. */
3225 17 : is_convertible (t1, t2, /*explain=*/true);
3226 17 : break;
3227 12 : case CPTK_IS_DESTRUCTIBLE:
3228 12 : inform (loc, "%qT is not destructible, because", t1);
3229 12 : is_xible (BIT_NOT_EXPR, t1, NULL_TREE, /*explain=*/true);
3230 12 : break;
3231 4 : case CPTK_IS_EMPTY:
3232 4 : inform (decl_loc, "%qT is not an empty class", t1);
3233 4 : break;
3234 0 : case CPTK_IS_ENUM:
3235 0 : inform (decl_loc, "%qT is not an enum", t1);
3236 0 : break;
3237 4 : case CPTK_IS_FINAL:
3238 4 : inform (decl_loc, "%qT is not a final class", t1);
3239 4 : break;
3240 0 : case CPTK_IS_FUNCTION:
3241 0 : inform (loc, "%qT is not a function", t1);
3242 0 : break;
3243 8 : case CPTK_IS_IMPLICIT_LIFETIME:
3244 8 : inform (decl_loc, "%qT is not an implicit-lifetime type", t1);
3245 8 : break;
3246 20 : case CPTK_IS_INVOCABLE:
3247 20 : {
3248 20 : if (!TREE_VEC_LENGTH (t2))
3249 11 : inform (loc, "%qT is not invocable, because", t1);
3250 : else
3251 9 : inform (loc, "%qT is not invocable by %qT, because", t1, t2);
3252 20 : build_invoke (t1, t2, tf_error);
3253 : }
3254 20 : break;
3255 24 : case CPTK_IS_LAYOUT_COMPATIBLE:
3256 24 : inform (loc, "%qT is not layout compatible with %qT, because", t1, t2);
3257 24 : layout_compatible_type_p (t1, t2, /*explain=*/true);
3258 24 : break;
3259 0 : case CPTK_IS_LITERAL_TYPE:
3260 0 : inform (decl_loc, "%qT is not a literal type", t1);
3261 0 : break;
3262 0 : case CPTK_IS_MEMBER_FUNCTION_POINTER:
3263 0 : inform (loc, "%qT is not a member function pointer", t1);
3264 0 : break;
3265 0 : case CPTK_IS_MEMBER_OBJECT_POINTER:
3266 0 : inform (loc, "%qT is not a member object pointer", t1);
3267 0 : break;
3268 0 : case CPTK_IS_MEMBER_POINTER:
3269 0 : inform (loc, "%qT is not a member pointer", t1);
3270 0 : break;
3271 6 : case CPTK_IS_NOTHROW_ASSIGNABLE:
3272 6 : inform (loc, "%qT is not nothrow assignable from %qT, because", t1, t2);
3273 6 : is_nothrow_xible (MODIFY_EXPR, t1, t2, /*explain=*/true);
3274 6 : break;
3275 15 : case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
3276 15 : if (!TREE_VEC_LENGTH (t2))
3277 6 : inform (loc, "%qT is not nothrow default constructible, because", t1);
3278 : else
3279 9 : inform (loc, "%qT is not nothrow constructible from %qT, because",
3280 : t1, t2);
3281 15 : is_nothrow_xible (INIT_EXPR, t1, t2, /*explain=*/true);
3282 15 : break;
3283 6 : case CPTK_IS_NOTHROW_CONVERTIBLE:
3284 6 : inform (loc, "%qT is not nothrow convertible from %qT, because", t1, t2);
3285 6 : is_nothrow_convertible (t1, t2, /*explain=*/true);
3286 6 : break;
3287 8 : case CPTK_IS_NOTHROW_DESTRUCTIBLE:
3288 8 : inform (loc, "%qT is not nothrow destructible, because", t1);
3289 8 : is_nothrow_xible (BIT_NOT_EXPR, t1, NULL_TREE, /*explain=*/true);
3290 8 : break;
3291 9 : case CPTK_IS_NOTHROW_INVOCABLE:
3292 9 : {
3293 9 : if (!TREE_VEC_LENGTH (t2))
3294 6 : inform (loc, "%qT is not nothrow invocable, because", t1);
3295 : else
3296 3 : inform (loc, "%qT is not nothrow invocable by %qT, because", t1, t2);
3297 9 : tree call = build_invoke (t1, t2, tf_error);
3298 9 : if (call != error_mark_node)
3299 9 : explain_not_noexcept (call);
3300 : }
3301 : break;
3302 48 : case CPTK_IS_OBJECT:
3303 48 : inform (loc, "%qT is not an object type", t1);
3304 48 : break;
3305 15 : case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
3306 15 : inform (location_of (t2),
3307 : "%qT is not a pointer-interconvertible base of %qT, because",
3308 : t1, t2);
3309 15 : pointer_interconvertible_base_of_p (t1, t2, /*explain=*/true);
3310 15 : break;
3311 4 : case CPTK_IS_POD:
3312 4 : inform (loc, "%qT is not a POD type", t1);
3313 4 : break;
3314 0 : case CPTK_IS_POINTER:
3315 0 : inform (loc, "%qT is not a pointer", t1);
3316 0 : break;
3317 4 : case CPTK_IS_POLYMORPHIC:
3318 4 : inform (decl_loc, "%qT is not a polymorphic type", t1);
3319 4 : break;
3320 0 : case CPTK_IS_REFERENCE:
3321 0 : inform (loc, "%qT is not a reference", t1);
3322 0 : break;
3323 188 : case CPTK_IS_SAME:
3324 188 : inform (loc, "%q#T is not the same as %q#T", t1, t2);
3325 188 : break;
3326 0 : case CPTK_IS_SCOPED_ENUM:
3327 0 : inform (decl_loc, "%qT is not a scoped enum", t1);
3328 0 : break;
3329 4 : case CPTK_IS_STD_LAYOUT:
3330 4 : inform (decl_loc, "%qT is not a standard layout type", t1);
3331 4 : break;
3332 4 : case CPTK_IS_TRIVIAL:
3333 4 : inform (decl_loc, "%qT is not a trivial type", t1);
3334 4 : break;
3335 6 : case CPTK_IS_TRIVIALLY_ASSIGNABLE:
3336 6 : inform (loc, "%qT is not trivially assignable from %qT, because", t1, t2);
3337 6 : is_trivially_xible (MODIFY_EXPR, t1, t2, /*explain=*/true);
3338 6 : break;
3339 15 : case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
3340 15 : if (!TREE_VEC_LENGTH (t2))
3341 6 : inform (loc, "%qT is not trivially default constructible, because", t1);
3342 : else
3343 9 : inform (loc, "%qT is not trivially constructible from %qT, because",
3344 : t1, t2);
3345 15 : is_trivially_xible (INIT_EXPR, t1, t2, /*explain=*/true);
3346 15 : break;
3347 7 : case CPTK_IS_TRIVIALLY_COPYABLE:
3348 7 : inform (decl_loc, "%qT is not trivially copyable", t1);
3349 7 : break;
3350 6 : case CPTK_IS_TRIVIALLY_DESTRUCTIBLE:
3351 6 : inform (loc, "%qT is not trivially destructible, because", t1);
3352 6 : is_trivially_xible (BIT_NOT_EXPR, t1, NULL_TREE, /*explain=*/true);
3353 6 : break;
3354 0 : case CPTK_IS_UNBOUNDED_ARRAY:
3355 0 : inform (loc, "%qT is not an unbounded array", t1);
3356 0 : break;
3357 4 : case CPTK_IS_UNION:
3358 4 : inform (decl_loc, "%qT is not a union", t1);
3359 4 : break;
3360 6 : case CPTK_IS_VIRTUAL_BASE_OF:
3361 6 : inform (location_of (t2), "%qT is not a virtual base of %qT", t1, t2);
3362 6 : break;
3363 0 : case CPTK_IS_VOLATILE:
3364 0 : inform (loc, "%qT is not a volatile type", t1);
3365 0 : break;
3366 0 : case CPTK_IS_STRUCTURAL:
3367 0 : inform (decl_loc, "%qT is not a structural type", t1);
3368 0 : structural_type_p (t1, /*explain=*/true);
3369 0 : break;
3370 0 : case CPTK_RANK:
3371 0 : inform (loc, "%qT cannot yield a rank", t1);
3372 0 : break;
3373 0 : case CPTK_TYPE_ORDER:
3374 0 : inform (loc, "%qT and %qT cannot be ordered", t1, t2);
3375 0 : break;
3376 0 : case CPTK_STRUCTURED_BINDING_SIZE:
3377 0 : inform (loc, "%qT is not destructurable", t1);
3378 0 : break;
3379 0 : case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
3380 0 : inform (loc, "%qT is not a reference that binds to a temporary "
3381 : "object of type %qT (direct-initialization)", t1, t2);
3382 0 : break;
3383 0 : case CPTK_REF_CONVERTS_FROM_TEMPORARY:
3384 0 : inform (loc, "%qT is not a reference that binds to a temporary "
3385 : "object of type %qT (copy-initialization)", t1, t2);
3386 0 : break;
3387 21 : case CPTK_IS_DEDUCIBLE:
3388 21 : inform (loc, "%qD is not deducible from %qT", t1, t2);
3389 21 : break;
3390 : #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
3391 : case CPTK_##CODE:
3392 : #include "cp-trait.def"
3393 : #undef DEFTRAIT_TYPE
3394 : /* Type-yielding traits aren't expressions. */
3395 0 : gcc_unreachable ();
3396 : /* We deliberately omit the default case so that when adding a new
3397 : trait we'll get reminded (by way of a warning) to handle it here. */
3398 : }
3399 809 : }
3400 :
3401 : /* Attempt to detect if this is a standard type trait, defined in terms
3402 : of a compiler builtin (above). If so, this will allow us to provide
3403 : more helpful diagnostics. */
3404 :
3405 : bool
3406 2223 : maybe_diagnose_standard_trait (location_t loc, tree expr)
3407 : {
3408 2223 : gcc_assert (TREE_CODE (expr) != TRAIT_EXPR);
3409 2223 : expr = tree_strip_nop_conversions (expr);
3410 :
3411 : /* TODO: in some cases it would be possible to provide more helpful
3412 : diagnostics for negations of traits, e.g. '!is_same_v<T1, T2>'. */
3413 :
3414 2223 : tree args = NULL_TREE;
3415 2223 : if (VAR_P (expr) && DECL_LANG_SPECIFIC (expr) && DECL_USE_TEMPLATE (expr))
3416 : {
3417 914 : tree tinfo = DECL_TEMPLATE_INFO (expr);
3418 914 : if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) && TI_PARTIAL_INFO (tinfo))
3419 12 : tinfo = TI_PARTIAL_INFO (tinfo);
3420 902 : else if (DECL_TEMPLATE_SPECIALIZATION (expr))
3421 : /* In an explicit specialisation we no longer know what the original
3422 : initializer looked like. */
3423 : tinfo = NULL_TREE;
3424 :
3425 905 : if (tinfo)
3426 : {
3427 905 : expr = DECL_INITIAL (DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo)));
3428 905 : args = TI_ARGS (tinfo);
3429 : }
3430 : }
3431 :
3432 2223 : if (expr && TREE_CODE (expr) == TRAIT_EXPR)
3433 : {
3434 436 : diagnose_trait_expr (loc, expr, args);
3435 436 : return true;
3436 : }
3437 :
3438 : return false;
3439 : }
3440 :
3441 : /* Diagnose a substitution failure in the atomic constraint T using ARGS. */
3442 :
3443 : static void
3444 1266 : diagnose_atomic_constraint (tree t, tree args, tree substituted, sat_info info)
3445 : {
3446 : /* If the constraint is already ill-formed, we've previously diagnosed
3447 : the reason. We should still say why the constraints aren't satisfied. */
3448 1266 : if (t == error_mark_node)
3449 : {
3450 0 : location_t loc;
3451 0 : if (info.in_decl)
3452 0 : loc = DECL_SOURCE_LOCATION (info.in_decl);
3453 : else
3454 0 : loc = input_location;
3455 0 : inform (loc, "invalid constraints");
3456 0 : return;
3457 : }
3458 :
3459 1266 : location_t loc = get_constraint_error_location (t);
3460 1266 : iloc_sentinel loc_s (loc);
3461 :
3462 : /* Generate better diagnostics for certain kinds of expressions. */
3463 1266 : tree expr = ATOMIC_CONSTR_EXPR (t);
3464 1266 : STRIP_ANY_LOCATION_WRAPPER (expr);
3465 :
3466 1266 : if (TREE_CODE (expr) == REQUIRES_EXPR)
3467 : {
3468 289 : gcc_checking_assert (info.diagnose_unsatisfaction_p ());
3469 : /* Clear in_decl before replaying the substitution to avoid emitting
3470 : seemingly unhelpful "in declaration ..." notes that follow some
3471 : substitution failure error messages. */
3472 289 : info.in_decl = NULL_TREE;
3473 289 : tsubst_requires_expr (expr, args, info);
3474 : }
3475 977 : else if (!same_type_p (TREE_TYPE (substituted), boolean_type_node))
3476 32 : error_at (loc, "constraint %qE has type %qT, not %<bool%>",
3477 32 : t, TREE_TYPE (substituted));
3478 : else
3479 : {
3480 945 : inform (loc, "the expression %qE evaluated to %<false%>", t);
3481 945 : if (TREE_CODE (expr) == TRAIT_EXPR)
3482 373 : diagnose_trait_expr (loc, expr, args);
3483 : else
3484 572 : maybe_diagnose_standard_trait (loc, substituted);
3485 : }
3486 1266 : }
3487 :
3488 : GTY(()) tree current_failed_constraint;
3489 :
3490 465716586 : diagnosing_failed_constraint::
3491 465716586 : diagnosing_failed_constraint (tree t, tree args, bool diag)
3492 465716586 : : diagnosing_error (diag)
3493 : {
3494 465716586 : if (diagnosing_error)
3495 : {
3496 3819 : current_failed_constraint
3497 3819 : = tree_cons (args, t, current_failed_constraint);
3498 3819 : ++current_constraint_diagnosis_depth;
3499 : }
3500 465716586 : }
3501 :
3502 465713886 : diagnosing_failed_constraint::
3503 : ~diagnosing_failed_constraint ()
3504 : {
3505 465713886 : if (diagnosing_error)
3506 : {
3507 3819 : --current_constraint_diagnosis_depth;
3508 3819 : if (current_failed_constraint)
3509 2399 : current_failed_constraint = TREE_CHAIN (current_failed_constraint);
3510 : }
3511 :
3512 465713886 : }
3513 :
3514 : /* Whether we are allowed to replay an error that underlies a constraint failure
3515 : at the current diagnosis depth. */
3516 :
3517 : bool
3518 406 : diagnosing_failed_constraint::replay_errors_p ()
3519 : {
3520 406 : if (current_constraint_diagnosis_depth >= concepts_diagnostics_max_depth)
3521 : {
3522 345 : concepts_diagnostics_max_depth_exceeded_p = true;
3523 345 : return false;
3524 : }
3525 : else
3526 : return true;
3527 : }
3528 :
3529 : /* Emit diagnostics detailing the failure ARGS to satisfy the constraints
3530 : of T. Here, T and ARGS are as in constraints_satisfied_p. */
3531 :
3532 : void
3533 1362 : diagnose_constraints (location_t loc, tree t, tree args)
3534 : {
3535 1362 : inform (loc, "constraints not satisfied");
3536 :
3537 1362 : if (concepts_diagnostics_max_depth == 0)
3538 0 : return;
3539 :
3540 1362 : auto_diagnostic_nesting_level sentinel;
3541 :
3542 : /* Replay satisfaction, but diagnose unsatisfaction. */
3543 1362 : sat_info noisy (tf_warning_or_error, NULL_TREE, /*diag_unsat=*/true);
3544 1362 : constraint_satisfaction_value (t, args, noisy);
3545 :
3546 1362 : static bool suggested_p;
3547 1362 : if (concepts_diagnostics_max_depth_exceeded_p
3548 354 : && current_constraint_diagnosis_depth == 0
3549 351 : && !suggested_p)
3550 : {
3551 141 : inform (UNKNOWN_LOCATION,
3552 : "set %qs to at least %d for more detail",
3553 : "-fconcepts-diagnostics-depth=",
3554 141 : concepts_diagnostics_max_depth + 1);
3555 141 : suggested_p = true;
3556 : }
3557 1362 : }
3558 :
3559 : #include "gt-cp-constraint.h"
|