Branch data Line data Source code
1 : : /* Processing rules for constraints.
2 : : Copyright (C) 2013-2025 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 : 61827277 : processing_constraint_expression_sentinel::
56 : : processing_constraint_expression_sentinel ()
57 : : {
58 : 61827277 : ++scope_chain->x_processing_constraint;
59 : 61827277 : }
60 : :
61 : 61827277 : processing_constraint_expression_sentinel::
62 : : ~processing_constraint_expression_sentinel ()
63 : : {
64 : 61827277 : --scope_chain->x_processing_constraint;
65 : 61827277 : }
66 : :
67 : : bool
68 : 3276678 : processing_constraint_expression_p ()
69 : : {
70 : 3276678 : 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 : 467713833 : subst_info (tsubst_flags_t cmp, tree in)
82 : 467713833 : : complain (cmp), in_decl (in)
83 : : { }
84 : :
85 : : /* True if we should not diagnose errors. */
86 : 1506569077 : bool quiet () const
87 : : {
88 : 1506569077 : return !(complain & tf_warning_or_error);
89 : : }
90 : :
91 : : /* True if we should diagnose errors. */
92 : 1197146297 : bool noisy () const
93 : : {
94 : 1207521467 : 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 : 430725365 : sat_info (tsubst_flags_t cmp, tree in, bool diag_unsat = false)
132 : 430725365 : : subst_info (cmp, in), diagnose_unsatisfaction (diag_unsat)
133 : : {
134 : 430725365 : if (diagnose_unsatisfaction_p ())
135 : 0 : gcc_checking_assert (noisy ());
136 : 5104931 : }
137 : :
138 : : /* True if we should diagnose the cause of satisfaction failure.
139 : : Implies noisy(). */
140 : : bool
141 : 434624240 : diagnose_unsatisfaction_p () const
142 : : {
143 : 5104931 : 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 : 11312973 : known_non_bool_p (tree t)
156 : : {
157 : 11312973 : return (t && !WILDCARD_TYPE_P (t) && TREE_CODE (t) != BOOLEAN_TYPE);
158 : : }
159 : :
160 : : static bool
161 : 11312973 : check_constraint_atom (cp_expr expr)
162 : : {
163 : 11312973 : 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 : 3273900 : check_constraint_operands (location_t, cp_expr lhs, cp_expr rhs)
175 : : {
176 : 3273900 : 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 : 3273906 : finish_constraint_binary_op (location_t loc,
183 : : tree_code code,
184 : : cp_expr lhs,
185 : : cp_expr rhs)
186 : : {
187 : 3273906 : gcc_assert (processing_constraint_expression_p ());
188 : 3273906 : if (lhs == error_mark_node || rhs == error_mark_node)
189 : 6 : return error_mark_node;
190 : 3273900 : if (!check_constraint_operands (loc, lhs, rhs))
191 : 0 : return error_mark_node;
192 : 3273900 : cp_expr expr
193 : 3273900 : = build_min_nt_loc (loc, code, lhs.get_value (), rhs.get_value ());
194 : 3273900 : expr.set_range (lhs.get_start (), rhs.get_finish ());
195 : 3273900 : return expr;
196 : : }
197 : :
198 : : cp_expr
199 : 151202 : finish_constraint_or_expr (location_t loc, cp_expr lhs, cp_expr rhs)
200 : : {
201 : 151202 : return finish_constraint_binary_op (loc, TRUTH_ORIF_EXPR, lhs, rhs);
202 : : }
203 : :
204 : : cp_expr
205 : 3122704 : finish_constraint_and_expr (location_t loc, cp_expr lhs, cp_expr rhs)
206 : : {
207 : 3122704 : return finish_constraint_binary_op (loc, TRUTH_ANDIF_EXPR, lhs, rhs);
208 : : }
209 : :
210 : : cp_expr
211 : 4765197 : finish_constraint_primary_expr (cp_expr expr)
212 : : {
213 : 4765197 : if (expr == error_mark_node)
214 : 24 : return error_mark_node;
215 : 4765173 : if (!check_constraint_atom (expr))
216 : 10 : return cp_expr (error_mark_node, expr.get_location ());
217 : 4765163 : return expr;
218 : : }
219 : :
220 : : /* Combine two constraint-expressions with a logical-and. */
221 : :
222 : : tree
223 : 55532511 : combine_constraint_expressions (tree lhs, tree rhs)
224 : : {
225 : 55532511 : processing_constraint_expression_sentinel pce;
226 : 55532511 : if (!lhs)
227 : : return rhs;
228 : 8928363 : 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 : 1855579 : return finish_constraint_and_expr (UNKNOWN_LOCATION, lhs, rhs);
233 : 55532511 : }
234 : :
235 : : /* Extract the TEMPLATE_DECL from a concept check. */
236 : :
237 : : tree
238 : 8796433 : get_concept_check_template (tree t)
239 : : {
240 : 8796433 : gcc_assert (concept_check_p (t));
241 : 8796433 : 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 : 7485734 : get_concept_definition (tree decl)
252 : : {
253 : 7485734 : gcc_assert (TREE_CODE (decl) == CONCEPT_DECL);
254 : 7485734 : 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 : 8235840 : map_arguments (tree parms, tree args)
298 : : {
299 : 22668522 : for (tree p = parms; p; p = TREE_CHAIN (p))
300 : 14432682 : if (args)
301 : : {
302 : 12622105 : int level;
303 : 12622105 : int index;
304 : 12622105 : template_parm_level_and_index (TREE_VALUE (p), &level, &index);
305 : 12622105 : TREE_PURPOSE (p) = TMPL_ARG (args, level, index);
306 : : }
307 : : else
308 : 1810577 : TREE_PURPOSE (p) = template_parm_to_arg (p);
309 : :
310 : 8235840 : 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 : 8235840 : build_parameter_mapping (tree expr, tree args, tree ctx_parms)
318 : : {
319 : 8235840 : tree parms = find_template_parameters (expr, ctx_parms);
320 : 8235840 : tree map = map_arguments (parms, args);
321 : 8235840 : 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 : 31771677 : parameter_mapping_equivalent_p (tree t1, tree t2)
329 : : {
330 : 31771677 : tree map1 = ATOMIC_CONSTR_MAP (t1);
331 : 31771677 : tree map2 = ATOMIC_CONSTR_MAP (t2);
332 : 72953047 : while (map1 && map2)
333 : : {
334 : 50905464 : gcc_checking_assert (TREE_VALUE (map1) == TREE_VALUE (map2));
335 : 50905464 : tree arg1 = TREE_PURPOSE (map1);
336 : 50905464 : tree arg2 = TREE_PURPOSE (map2);
337 : 50905464 : if (!template_args_equal (arg1, arg2))
338 : : return false;
339 : 41181370 : map1 = TREE_CHAIN (map1);
340 : 41181370 : map2 = TREE_CHAIN (map2);
341 : : }
342 : 22047583 : 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 : 4018546 : explicit norm_info (bool diag)
351 : 4018546 : : norm_info (NULL_TREE, diag)
352 : : {}
353 : :
354 : : /* Construct a top-level context for DECL. */
355 : :
356 : 5211779 : norm_info (tree in_decl, bool diag)
357 : 5211779 : : subst_info (tf_warning_or_error|tf_partial, in_decl),
358 : 5211779 : generate_diagnostics (diag)
359 : : {
360 : 1193233 : if (in_decl)
361 : : {
362 : 1001568 : initial_parms = DECL_TEMPLATE_PARMS (in_decl);
363 : 1001568 : if (generate_diagnostics)
364 : 1286 : context = build_tree_list (NULL_TREE, in_decl);
365 : : }
366 : : else
367 : 191665 : initial_parms = current_template_parms;
368 : 1193233 : }
369 : :
370 : 7379027 : void update_context (tree expr, tree args)
371 : : {
372 : 7379027 : if (generate_diagnostics)
373 : : {
374 : 5979 : tree map = build_parameter_mapping (expr, args, ctx_parms ());
375 : 5979 : context = tree_cons (map, expr, context);
376 : : }
377 : 7379027 : in_decl = get_concept_check_template (expr);
378 : 7379027 : }
379 : :
380 : : /* Returns the template parameters that are in scope for the current
381 : : normalization context. */
382 : :
383 : 8235840 : tree ctx_parms ()
384 : : {
385 : 8235840 : if (in_decl)
386 : 8116533 : return DECL_TEMPLATE_PARMS (in_decl);
387 : : else
388 : 119307 : 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 : 8430207 : normalize_logical_operation (tree t, tree args, tree_code c, norm_info info)
416 : : {
417 : 8430207 : tree t0 = normalize_expression (TREE_OPERAND (t, 0), args, info);
418 : 8430207 : tree t1 = normalize_expression (TREE_OPERAND (t, 1), args, info);
419 : :
420 : : /* Build a new info object for the constraint. */
421 : 8430207 : tree ci = (info.generate_diagnostics
422 : 8430207 : ? build_tree_list (t, info.context) : NULL_TREE);
423 : :
424 : 8430207 : 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 : 76041771 : static hashval_t hash (norm_entry *e)
443 : : {
444 : 76041771 : ++comparing_specializations;
445 : 76041771 : hashval_t val = iterative_hash_template_arg (e->tmpl, 0);
446 : 76041771 : val = iterative_hash_template_arg (e->args, val);
447 : 76041771 : --comparing_specializations;
448 : 76041771 : return val;
449 : : }
450 : :
451 : 89001788 : static bool equal (norm_entry *e1, norm_entry *e2)
452 : : {
453 : 89001788 : ++comparing_specializations;
454 : 89001788 : bool eq = e1->tmpl == e2->tmpl
455 : 89001788 : && template_args_equal (e1->args, e2->args);
456 : 89001788 : --comparing_specializations;
457 : 89001788 : 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 : 9256613 : normalize_concept_check (tree check, tree args, norm_info info)
468 : : {
469 : 9256613 : gcc_assert (concept_check_p (check));
470 : 9256613 : tree tmpl = TREE_OPERAND (check, 0);
471 : 9256613 : tree targs = TREE_OPERAND (check, 1);
472 : :
473 : : /* Substitute through the arguments of the concept check. */
474 : 9256613 : if (args)
475 : 6929377 : targs = tsubst_template_args (targs, args, info.complain, info.in_decl);
476 : 9256613 : if (targs == error_mark_node)
477 : : return error_mark_node;
478 : 9256613 : if (template_args_equal (targs, generic_targs_for (tmpl)))
479 : : /* Canonicalize generic arguments as NULL_TREE, as an optimization. */
480 : 1147021 : targs = NULL_TREE;
481 : :
482 : : /* Build the substitution for the concept definition. */
483 : 9256613 : tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl));
484 : 9256613 : if (targs && args)
485 : : /* As an optimization, coerce the arguments only if necessary
486 : : (i.e. if they were substituted). */
487 : 6802139 : targs = coerce_template_parms (parms, targs, tmpl, tf_none);
488 : 9256613 : if (targs == error_mark_node)
489 : : return error_mark_node;
490 : :
491 : 9256613 : if (!norm_cache)
492 : 22744 : norm_cache = hash_table<norm_hasher>::create_ggc (31);
493 : 9256613 : norm_entry *entry = nullptr;
494 : 9256613 : if (!info.generate_diagnostics)
495 : : {
496 : : /* Cache the normal form of the substituted concept-id (when not
497 : : diagnosing). */
498 : 9250634 : norm_entry elt = {tmpl, targs, NULL_TREE};
499 : 9250634 : norm_entry **slot = norm_cache->find_slot (&elt, INSERT);
500 : 9250634 : if (*slot)
501 : 1877586 : return (*slot)->norm;
502 : 7373048 : entry = ggc_alloc<norm_entry> ();
503 : 7373048 : *entry = elt;
504 : 7373048 : *slot = entry;
505 : : }
506 : :
507 : 7379027 : tree def = get_concept_definition (DECL_TEMPLATE_RESULT (tmpl));
508 : 7379027 : info.update_context (check, args);
509 : 7379027 : tree norm = normalize_expression (def, targs, info);
510 : 7379027 : if (entry)
511 : 7373048 : 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 : 62565334 : static hashval_t hash (tree t)
520 : : {
521 : 62565334 : ++comparing_specializations;
522 : 62565334 : hashval_t val = hash_atomic_constraint (t);
523 : 62565334 : --comparing_specializations;
524 : 62565334 : return val;
525 : : }
526 : :
527 : 57055577 : static bool equal (tree t1, tree t2)
528 : : {
529 : 57055577 : ++comparing_specializations;
530 : 57055577 : bool eq = atomic_constraints_identical_p (t1, t2);
531 : 57055577 : --comparing_specializations;
532 : 57055577 : 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 : 17486474 : normalize_atom (tree t, tree args, norm_info info)
544 : : {
545 : : /* Concept checks are not atomic. */
546 : 17486474 : if (concept_check_p (t))
547 : 9256613 : return normalize_concept_check (t, args, info);
548 : :
549 : : /* Build the parameter mapping for the atom. */
550 : 8229861 : tree map = build_parameter_mapping (t, args, info.ctx_parms ());
551 : :
552 : : /* Build a new info object for the atom. */
553 : 8229861 : tree ci = build_tree_list (t, info.context);
554 : :
555 : 8229861 : 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 : 16340566 : if (info.in_decl && concept_definition_p (info.in_decl))
563 : 7438310 : ATOMIC_CONSTR_EXPR_FROM_CONCEPT_P (atom) = true;
564 : :
565 : 8229861 : 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 : 8222762 : if (!atom_cache)
570 : 26459 : atom_cache = hash_table<atom_hasher>::create_ggc (31);
571 : 8222762 : tree *slot = atom_cache->find_slot (atom, INSERT);
572 : 8222762 : 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 : 8001043 : if (map)
581 : : {
582 : 8000433 : tree targets = make_tree_vec (list_length (map));
583 : 8000433 : int i = 0;
584 : 22191285 : for (tree node = map; node; node = TREE_CHAIN (node))
585 : : {
586 : 14190852 : tree target = TREE_PURPOSE (node);
587 : 14190852 : TREE_VEC_ELT (targets, i++) = target;
588 : : }
589 : 8000433 : tree target_parms = find_template_parameters (targets,
590 : : info.initial_parms);
591 : 8000433 : TREE_TYPE (map) = target_parms;
592 : : }
593 : :
594 : 8001043 : *slot = atom;
595 : : }
596 : : return atom;
597 : : }
598 : :
599 : : /* Returns the normal form of an expression. */
600 : :
601 : : static tree
602 : 25916796 : normalize_expression (tree t, tree args, norm_info info)
603 : : {
604 : 25916796 : if (!t)
605 : : return NULL_TREE;
606 : :
607 : 25916796 : if (t == error_mark_node)
608 : : return error_mark_node;
609 : :
610 : 25916681 : switch (TREE_CODE (t))
611 : : {
612 : 8189185 : case TRUTH_ANDIF_EXPR:
613 : 8189185 : return normalize_logical_operation (t, args, CONJ_CONSTR, info);
614 : 241022 : case TRUTH_ORIF_EXPR:
615 : 241022 : return normalize_logical_operation (t, args, DISJ_CONSTR, info);
616 : 17486474 : default:
617 : 17486474 : 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 : 1677355 : get_normalized_constraints (tree t, norm_info info)
627 : : {
628 : 1677355 : auto_timevar time (TV_CONSTRAINT_NORM);
629 : 1677355 : return normalize_expression (t, NULL_TREE, info);
630 : 1677355 : }
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 : 1086530 : get_normalized_constraints_from_info (tree ci, tree in_decl, bool diag = false)
638 : : {
639 : 1086530 : if (ci == NULL_TREE)
640 : : return NULL_TREE;
641 : :
642 : : /* Substitution errors during normalization are fatal. */
643 : 1086526 : ++processing_template_decl;
644 : 1086526 : norm_info info (in_decl, diag);
645 : 2173052 : tree t = get_normalized_constraints (CI_ASSOCIATED_CONSTRAINTS (ci), info);
646 : 1086526 : --processing_template_decl;
647 : :
648 : 1086526 : return t;
649 : : }
650 : :
651 : : /* Returns the normalized constraints for the declaration D. */
652 : :
653 : : static tree
654 : 162591394 : get_normalized_constraints_from_decl (tree d, bool diag = false)
655 : : {
656 : 162591394 : tree tmpl;
657 : 162591394 : tree decl;
658 : :
659 : : /* For inherited constructors, consider the original declaration;
660 : : it has the correct template information attached. */
661 : 162591394 : d = strip_inheriting_ctors (d);
662 : :
663 : 162591394 : 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 : 496866 : tree lambda = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (d));
669 : 496866 : lambda = most_general_lambda (lambda);
670 : 496866 : d = lambda_function (lambda);
671 : : }
672 : :
673 : 162591394 : if (TREE_CODE (d) == TEMPLATE_DECL)
674 : : {
675 : 123529949 : tmpl = d;
676 : 123529949 : decl = DECL_TEMPLATE_RESULT (tmpl);
677 : : }
678 : : else
679 : : {
680 : 39061445 : if (tree ti = DECL_TEMPLATE_INFO (d))
681 : 27188057 : tmpl = TI_TEMPLATE (ti);
682 : : else
683 : : tmpl = NULL_TREE;
684 : 27188057 : 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 : 150718006 : if (tmpl && DECL_LANG_SPECIFIC (tmpl)
705 : 177906048 : && (!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 : 10217076 : || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
715 : 5108538 : < TMPL_ARGS_DEPTH (DECL_TI_ARGS (tmpl)))))
716 : 145609521 : tmpl = most_general_template (tmpl);
717 : :
718 : 162591394 : d = tmpl ? tmpl : decl;
719 : :
720 : : /* If we're not diagnosing errors, use cached constraints, if any. */
721 : 162591394 : if (!diag)
722 : 325070049 : if (tree *p = hash_map_safe_get (normalized_map, d))
723 : 137891463 : return *p;
724 : :
725 : 24699931 : tree norm = NULL_TREE;
726 : 24699931 : if (tree ci = get_constraints (d))
727 : : {
728 : 894774 : push_access_scope_guard pas (decl);
729 : 894774 : norm = get_normalized_constraints_from_info (ci, tmpl, diag);
730 : 894774 : }
731 : :
732 : 24699931 : if (!diag)
733 : 24698933 : hash_map_safe_put<hm_ggc> (normalized_map, d, norm);
734 : :
735 : 24699931 : return norm;
736 : : }
737 : :
738 : : /* Returns the normal form of TMPL's definition. */
739 : :
740 : : static tree
741 : 1303758 : normalize_concept_definition (tree tmpl, bool diag)
742 : : {
743 : 1303758 : if (!norm_cache)
744 : 803 : norm_cache = hash_table<norm_hasher>::create_ggc (31);
745 : 1303758 : norm_entry entry = {tmpl, NULL_TREE, NULL_TREE};
746 : :
747 : 1303758 : if (!diag)
748 : 1303470 : if (norm_entry *found = norm_cache->find (&entry))
749 : 1197051 : return found->norm;
750 : :
751 : 106707 : gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
752 : 106707 : tree def = get_concept_definition (DECL_TEMPLATE_RESULT (tmpl));
753 : 106707 : ++processing_template_decl;
754 : 106707 : norm_info info (tmpl, diag);
755 : 106707 : tree norm = get_normalized_constraints (def, info);
756 : 106707 : --processing_template_decl;
757 : :
758 : 106707 : if (!diag)
759 : : {
760 : 106419 : norm_entry **slot = norm_cache->find_slot (&entry, INSERT);
761 : 106419 : entry.norm = norm;
762 : 106419 : *slot = ggc_alloc<norm_entry> ();
763 : 106419 : **slot = entry;
764 : : }
765 : :
766 : : return norm;
767 : : }
768 : :
769 : : /* Normalize an EXPR as a constraint. */
770 : :
771 : : static tree
772 : 4018546 : normalize_constraint_expression (tree expr, norm_info info)
773 : : {
774 : 4018546 : if (!expr || expr == error_mark_node)
775 : : return expr;
776 : :
777 : 4018546 : if (!info.generate_diagnostics)
778 : 8036658 : if (tree *p = hash_map_safe_get (normalized_map, expr))
779 : 3534424 : return *p;
780 : :
781 : 484122 : ++processing_template_decl;
782 : 484122 : tree norm = get_normalized_constraints (expr, info);
783 : 484122 : --processing_template_decl;
784 : :
785 : 484122 : if (!info.generate_diagnostics)
786 : 483953 : 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 : 132445556 : atomic_constraints_identical_p (tree t1, tree t2)
797 : : {
798 : 132445556 : gcc_assert (TREE_CODE (t1) == ATOMIC_CONSTR);
799 : 132445556 : gcc_assert (TREE_CODE (t2) == ATOMIC_CONSTR);
800 : :
801 : 132445556 : if (ATOMIC_CONSTR_EXPR (t1) != ATOMIC_CONSTR_EXPR (t2))
802 : : return false;
803 : :
804 : 31771677 : 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 : 8614923 : constraints_equivalent_p (tree t1, tree t2)
815 : : {
816 : 8614923 : gcc_assert (CONSTR_P (t1));
817 : 8614923 : gcc_assert (CONSTR_P (t2));
818 : :
819 : 8614923 : if (TREE_CODE (t1) != TREE_CODE (t2))
820 : : return false;
821 : :
822 : 8551721 : switch (TREE_CODE (t1))
823 : : {
824 : 4293137 : case CONJ_CONSTR:
825 : 4293137 : case DISJ_CONSTR:
826 : 4293137 : if (!constraints_equivalent_p (TREE_OPERAND (t1, 0),
827 : 4293137 : TREE_OPERAND (t2, 0)))
828 : : return false;
829 : 4205027 : if (!constraints_equivalent_p (TREE_OPERAND (t1, 1),
830 : 4205027 : TREE_OPERAND (t2, 1)))
831 : : return false;
832 : : break;
833 : 4258584 : case ATOMIC_CONSTR:
834 : 4258584 : 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 : 665698585 : hash_atomic_constraint (tree t)
847 : : {
848 : 665698585 : gcc_assert (TREE_CODE (t) == ATOMIC_CONSTR);
849 : :
850 : : /* Hash the identity of the expression. */
851 : 665698585 : hashval_t val = htab_hash_pointer (ATOMIC_CONSTR_EXPR (t));
852 : :
853 : : /* Hash the targets of the parameter map. */
854 : 665698585 : tree p = ATOMIC_CONSTR_MAP (t);
855 : 1711439319 : while (p)
856 : : {
857 : 1045740734 : val = iterative_hash_template_arg (TREE_PURPOSE (p), val);
858 : 1045740734 : p = TREE_CHAIN (p);
859 : : }
860 : :
861 : 665698585 : return val;
862 : : }
863 : :
864 : : namespace inchash
865 : : {
866 : :
867 : : static void
868 : 31410551 : add_constraint (tree t, hash& h)
869 : : {
870 : 62532872 : h.add_int (TREE_CODE (t));
871 : 62532872 : switch (TREE_CODE (t))
872 : : {
873 : 31122321 : case CONJ_CONSTR:
874 : 31122321 : case DISJ_CONSTR:
875 : 31122321 : add_constraint (TREE_OPERAND (t, 0), h);
876 : 31122321 : add_constraint (TREE_OPERAND (t, 1), h);
877 : 31122321 : break;
878 : 31410551 : case ATOMIC_CONSTR:
879 : 31410551 : h.merge_hash (hash_atomic_constraint (t));
880 : 31410551 : break;
881 : 0 : default:
882 : 0 : gcc_unreachable ();
883 : : }
884 : 31410551 : }
885 : :
886 : : }
887 : :
888 : : /* Computes a hash code for the constraint T. */
889 : :
890 : : hashval_t
891 : 288230 : iterative_hash_constraint (tree t, hashval_t val)
892 : : {
893 : 288230 : gcc_assert (CONSTR_P (t));
894 : 288230 : inchash::hash h (val);
895 : 288230 : inchash::add_constraint (t, h);
896 : 288230 : 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 : 37376930 : current_template_constraints (void)
908 : : {
909 : 37376930 : if (!current_template_parms)
910 : : return NULL_TREE;
911 : 37376927 : tree tmpl_constr = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
912 : 37376927 : 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 : 17885382 : associate_classtype_constraints (tree type)
921 : : {
922 : 17885382 : 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 : 17884682 : if (!current_template_parms)
927 : : return type;
928 : :
929 : 14792771 : if (CLASSTYPE_IS_TEMPLATE (type) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
930 : : {
931 : 14447260 : tree decl = TYPE_STUB_DECL (type);
932 : 14447260 : 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 : 14447260 : if (tree orig_ci = get_constraints (decl))
939 : : {
940 : 354850 : if (int extra_levels = (TMPL_PARMS_DEPTH (current_template_parms)
941 : 2190891 : - 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 : 354850 : 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 : 354850 : return type;
963 : : }
964 : 14092410 : 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 : 7629970 : build_constraint_info ()
973 : : {
974 : 7629970 : 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 : 93468470 : build_constraints (tree tr, tree dr)
987 : : {
988 : 93468470 : if (!tr && !dr)
989 : : return NULL_TREE;
990 : :
991 : 7629970 : tree_constraint_info* ci = build_constraint_info ();
992 : 7629970 : ci->template_reqs = tr;
993 : 7629970 : ci->declarator_reqs = dr;
994 : 7629970 : ci->associated_constr = combine_constraint_expressions (tr, dr);
995 : :
996 : 7629970 : return (tree)ci;
997 : : }
998 : :
999 : : /* Add constraint RHS to the end of CONSTRAINT_INFO ci. */
1000 : :
1001 : : tree
1002 : 70 : append_constraint (tree ci, tree rhs)
1003 : : {
1004 : 70 : tree tr = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
1005 : 24 : tree dr = ci ? CI_DECLARATOR_REQS (ci) : NULL_TREE;
1006 : 70 : dr = combine_constraint_expressions (dr, rhs);
1007 : 70 : 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 : 58 : ci = build_constraints (tr, dr);
1015 : 70 : 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 : 573839556 : get_constraints (const_tree t)
1027 : : {
1028 : 573839556 : if (!flag_concepts)
1029 : : return NULL_TREE;
1030 : 294320096 : if (!decl_constraints)
1031 : : return NULL_TREE;
1032 : :
1033 : 290513841 : gcc_assert (DECL_P (t));
1034 : 290513841 : if (TREE_CODE (t) == TEMPLATE_DECL)
1035 : 60440218 : t = DECL_TEMPLATE_RESULT (t);
1036 : 290513841 : tree* found = decl_constraints->get (CONST_CAST_TREE (t));
1037 : 290513841 : if (found)
1038 : 37731633 : 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 : 94104942 : set_constraints (tree t, tree ci)
1050 : : {
1051 : 94104942 : if (!ci)
1052 : : return;
1053 : 14889249 : gcc_assert (t && flag_concepts);
1054 : 14889249 : if (TREE_CODE (t) == TEMPLATE_DECL)
1055 : 105652 : t = DECL_TEMPLATE_RESULT (t);
1056 : 14889249 : bool found = hash_map_safe_put<hm_ggc> (decl_constraints, t, ci);
1057 : 14889249 : gcc_assert (!found);
1058 : : }
1059 : :
1060 : : /* Remove the associated constraints of the declaration T. */
1061 : :
1062 : : void
1063 : 3878297 : remove_constraints (tree t)
1064 : : {
1065 : 3878297 : gcc_checking_assert (DECL_P (t));
1066 : 3878297 : if (TREE_CODE (t) == TEMPLATE_DECL)
1067 : 176 : t = DECL_TEMPLATE_RESULT (t);
1068 : :
1069 : 3878297 : if (decl_constraints)
1070 : 3727288 : decl_constraints->remove (t);
1071 : 3878297 : }
1072 : :
1073 : : /* If DECL is a friend, substitute into REQS to produce requirements suitable
1074 : : for declaration matching. */
1075 : :
1076 : : tree
1077 : 50514561 : maybe_substitute_reqs_for (tree reqs, const_tree decl)
1078 : : {
1079 : 50514561 : if (reqs == NULL_TREE)
1080 : : return NULL_TREE;
1081 : :
1082 : 4450997 : decl = STRIP_TEMPLATE (decl);
1083 : 4450997 : if (DECL_UNIQUE_FRIEND_P (decl) && DECL_TEMPLATE_INFO (decl))
1084 : : {
1085 : 90922 : tree tmpl = DECL_TI_TEMPLATE (decl);
1086 : 90922 : tree outer_args = outer_template_args (decl);
1087 : 90922 : processing_template_decl_sentinel s;
1088 : 90922 : if (PRIMARY_TEMPLATE_P (tmpl)
1089 : 90922 : || uses_template_parms (outer_args))
1090 : 90922 : ++processing_template_decl;
1091 : 90922 : reqs = tsubst_constraint (reqs, outer_args,
1092 : : tf_warning_or_error, NULL_TREE);
1093 : 90922 : }
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 : 206929654 : get_trailing_function_requirements (tree t)
1102 : : {
1103 : 206929654 : tree ci = get_constraints (t);
1104 : 206929654 : if (!ci)
1105 : : return NULL_TREE;
1106 : 29672990 : 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 : 14577507 : build_concept_check_arguments (tree arg, tree rest)
1114 : : {
1115 : 14577507 : gcc_assert (!rest || TREE_CODE (rest) == TREE_VEC);
1116 : 14577507 : tree args;
1117 : 14577507 : if (arg)
1118 : : {
1119 : 10906465 : int n = rest ? TREE_VEC_LENGTH (rest) : 0;
1120 : 7536326 : args = make_tree_vec (n + 1);
1121 : 7536326 : TREE_VEC_ELT (args, 0) = arg;
1122 : 7536326 : if (rest)
1123 : 7307565 : for (int i = 0; i < n; ++i)
1124 : 3937426 : TREE_VEC_ELT (args, i + 1) = TREE_VEC_ELT (rest, i);
1125 : 3370139 : int def = rest ? GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (rest) : 0;
1126 : 7536326 : SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, def + 1);
1127 : : }
1128 : : else
1129 : : args = rest;
1130 : 14577507 : return args;
1131 : : }
1132 : :
1133 : : /* Construct an expression that checks TMPL using ARGS. */
1134 : :
1135 : : tree
1136 : 7041181 : build_concept_check (tree tmpl, tree args, tsubst_flags_t complain)
1137 : : {
1138 : 7041181 : 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 : 14577507 : build_concept_check (tree tmpl, tree arg, tree rest, tsubst_flags_t complain)
1145 : : {
1146 : 14577507 : if (TREE_DEPRECATED (DECL_TEMPLATE_RESULT (tmpl)))
1147 : 9 : warn_deprecated_use (DECL_TEMPLATE_RESULT (tmpl), NULL_TREE);
1148 : :
1149 : 14577507 : tree parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
1150 : 14577507 : tree args = build_concept_check_arguments (arg, rest);
1151 : 14577507 : args = coerce_template_parms (parms, args, tmpl, complain);
1152 : 14577507 : if (args == error_mark_node)
1153 : : return error_mark_node;
1154 : 13773020 : 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 : 5444080 : 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 : 5444080 : build_concept_id (tree expr)
1170 : : {
1171 : 5444080 : gcc_assert (TREE_CODE (expr) == TEMPLATE_ID_EXPR);
1172 : 5444080 : tree id = build_concept_id (TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
1173 : 5444080 : protected_set_expr_location (id, cp_expr_location (expr));
1174 : 5444080 : 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 : 4180813 : build_type_constraint (tree decl, tree args, tsubst_flags_t complain)
1185 : : {
1186 : 4180813 : tree proto = template_parm_to_arg (concept_prototype_parameter (decl));
1187 : 4180813 : ++processing_template_decl;
1188 : 4180813 : tree check = build_concept_check (decl, proto, args, complain);
1189 : 4180813 : --processing_template_decl;
1190 : 4180813 : 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 : 2952043 : build_constrained_parameter (tree cnc, tree proto, tree args)
1202 : : {
1203 : 2952043 : tree name = DECL_NAME (cnc);
1204 : 2952043 : tree type = TREE_TYPE (proto);
1205 : 2952043 : tree decl = build_decl (input_location, TYPE_DECL, name, type);
1206 : 2952043 : CONSTRAINED_PARM_PROTOTYPE (decl) = proto;
1207 : 2952043 : CONSTRAINED_PARM_CONCEPT (decl) = cnc;
1208 : 2952043 : CONSTRAINED_PARM_EXTRA_ARGS (decl) = args;
1209 : 2952043 : 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 : 153486380 : finish_shorthand_constraint (tree decl, tree constr)
1222 : : {
1223 : : /* No requirements means no constraints. */
1224 : 153486380 : if (!constr)
1225 : : return NULL_TREE;
1226 : :
1227 : 2952019 : if (error_operand_p (constr))
1228 : : return NULL_TREE;
1229 : :
1230 : 2952019 : tree proto = CONSTRAINED_PARM_PROTOTYPE (constr);
1231 : 2952019 : tree con = CONSTRAINED_PARM_CONCEPT (constr);
1232 : 2952019 : tree args = CONSTRAINED_PARM_EXTRA_ARGS (constr);
1233 : :
1234 : 2952019 : bool variadic_concept_p = template_parameter_pack_p (proto);
1235 : 2952019 : bool declared_pack_p = template_parameter_pack_p (decl);
1236 : 2952019 : bool apply_to_each_p = (cxx_dialect >= cxx20) ? true : !variadic_concept_p;
1237 : :
1238 : : /* Get the argument and overload used for the requirement
1239 : : and adjust it if we're going to expand later. */
1240 : 2952019 : tree arg = template_parm_to_arg (decl);
1241 : 2952019 : if (apply_to_each_p && declared_pack_p)
1242 : 34860 : arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
1243 : :
1244 : : /* Build the concept constraint-expression. */
1245 : 2952019 : tree tmpl = DECL_TI_TEMPLATE (con);
1246 : 2952019 : tree check = build_concept_check (tmpl, arg, args, tf_warning_or_error);
1247 : :
1248 : : /* Make the check a fold-expression if needed.
1249 : : Use UNKNOWN_LOCATION so write_template_args can tell the
1250 : : difference between this and a fold the user wrote. */
1251 : 2952019 : if (apply_to_each_p && declared_pack_p)
1252 : 34860 : check = finish_left_unary_fold_expr (UNKNOWN_LOCATION,
1253 : : check, TRUTH_ANDIF_EXPR);
1254 : :
1255 : : return check;
1256 : : }
1257 : :
1258 : : /* Returns a conjunction of shorthand requirements for the template
1259 : : parameter list PARMS. Note that the requirements are stored in
1260 : : the TYPE of each tree node. */
1261 : :
1262 : : tree
1263 : 25107494 : get_shorthand_constraints (tree parms)
1264 : : {
1265 : 25107494 : tree result = NULL_TREE;
1266 : 25107494 : parms = INNERMOST_TEMPLATE_PARMS (parms);
1267 : 71124170 : for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
1268 : : {
1269 : 46016676 : tree parm = TREE_VEC_ELT (parms, i);
1270 : 46016676 : tree constr = TEMPLATE_PARM_CONSTRAINTS (parm);
1271 : 46016676 : result = combine_constraint_expressions (result, constr);
1272 : : }
1273 : 25107494 : return result;
1274 : : }
1275 : :
1276 : : /* Returns true iff the placeholders C1 and C2 are equivalent. C1
1277 : : and C2 can be either TEMPLATE_TYPE_PARM or template-ids. */
1278 : :
1279 : : bool
1280 : 493414936 : equivalent_placeholder_constraints (tree c1, tree c2)
1281 : : {
1282 : 493414936 : if (c1 && TREE_CODE (c1) == TEMPLATE_TYPE_PARM)
1283 : : /* A constrained auto. */
1284 : 493414936 : c1 = PLACEHOLDER_TYPE_CONSTRAINTS (c1);
1285 : 493414936 : if (c2 && TREE_CODE (c2) == TEMPLATE_TYPE_PARM)
1286 : 493414936 : c2 = PLACEHOLDER_TYPE_CONSTRAINTS (c2);
1287 : :
1288 : 493414936 : if (c1 == c2)
1289 : : return true;
1290 : 1551757 : if (!c1 || !c2)
1291 : : return false;
1292 : 1364028 : if (c1 == error_mark_node || c2 == error_mark_node)
1293 : : /* We get here during satisfaction; when a deduction constraint
1294 : : fails, substitution can produce an error_mark_node for the
1295 : : placeholder constraints. */
1296 : : return false;
1297 : :
1298 : 1364028 : gcc_assert (concept_check_p (c1) && concept_check_p (c2));
1299 : 1364028 : tree t1 = TREE_OPERAND (c1, 0);
1300 : 1364028 : tree a1 = TREE_OPERAND (c1, 1);
1301 : 1364028 : tree t2 = TREE_OPERAND (c2, 0);
1302 : 1364028 : tree a2 = TREE_OPERAND (c2, 1);
1303 : :
1304 : 1364028 : if (t1 != t2)
1305 : : return false;
1306 : :
1307 : 797081 : int len1 = TREE_VEC_LENGTH (a1);
1308 : 797081 : int len2 = TREE_VEC_LENGTH (a2);
1309 : 797081 : if (len1 != len2)
1310 : : return false;
1311 : :
1312 : : /* Skip the first argument so we don't infinitely recurse.
1313 : : Also, they may differ in template parameter index. */
1314 : 1130613 : for (int i = 1; i < len1; ++i)
1315 : 468549 : if (!template_args_equal (TREE_VEC_ELT (a1, i),
1316 : 468549 : TREE_VEC_ELT (a2, i)))
1317 : : return false;
1318 : : return true;
1319 : : }
1320 : :
1321 : : /* Return a hash value for the placeholder ATOMIC_CONSTR C. */
1322 : :
1323 : : hashval_t
1324 : 82650333 : iterative_hash_placeholder_constraint (tree c, hashval_t val)
1325 : : {
1326 : 82650333 : gcc_assert (concept_check_p (c));
1327 : 82650333 : tree t = TREE_OPERAND (c, 0);
1328 : 82650333 : tree a = TREE_OPERAND (c, 1);
1329 : :
1330 : : /* Like hash_tmpl_and_args, but skip the first argument. */
1331 : 82650333 : val = iterative_hash_object (DECL_UID (t), val);
1332 : :
1333 : 142476931 : for (int i = TREE_VEC_LENGTH (a)-1; i > 0; --i)
1334 : 59826598 : val = iterative_hash_template_arg (TREE_VEC_ELT (a, i), val);
1335 : :
1336 : 82650333 : return val;
1337 : : }
1338 : :
1339 : : /* Substitute through the expression of a simple requirement or
1340 : : compound requirement. */
1341 : :
1342 : : static tree
1343 : 5943276 : tsubst_valid_expression_requirement (tree t, tree args, sat_info info)
1344 : : {
1345 : 5943276 : tsubst_flags_t quiet = info.complain & ~tf_warning_or_error;
1346 : 5943276 : tree r = tsubst_expr (t, args, quiet, info.in_decl);
1347 : 5940576 : if (r != error_mark_node
1348 : 5940576 : && (processing_template_decl
1349 : 5651374 : || convert_to_void (r, ICV_STATEMENT, quiet) != error_mark_node))
1350 : 5652818 : return r;
1351 : :
1352 : 287758 : if (info.diagnose_unsatisfaction_p ())
1353 : : {
1354 : 233 : location_t loc = cp_expr_loc_or_input_loc (t);
1355 : 233 : if (diagnosing_failed_constraint::replay_errors_p ())
1356 : : {
1357 : 8 : inform (loc, "the required expression %qE is invalid, because", t);
1358 : 8 : auto_diagnostic_nesting_level sentinel;
1359 : 8 : if (r == error_mark_node)
1360 : 7 : tsubst_expr (t, args, info.complain, info.in_decl);
1361 : : else
1362 : 1 : convert_to_void (r, ICV_STATEMENT, info.complain);
1363 : 8 : }
1364 : : else
1365 : 225 : inform (loc, "the required expression %qE is invalid", t);
1366 : : }
1367 : 287525 : else if (info.noisy ())
1368 : : {
1369 : 0 : r = tsubst_expr (t, args, info.complain, info.in_decl);
1370 : 0 : convert_to_void (r, ICV_STATEMENT, info.complain);
1371 : : }
1372 : :
1373 : 287758 : return error_mark_node;
1374 : : }
1375 : :
1376 : :
1377 : : /* Substitute through the simple requirement. */
1378 : :
1379 : : static tree
1380 : 2038753 : tsubst_simple_requirement (tree t, tree args, sat_info info)
1381 : : {
1382 : 2038753 : tree t0 = TREE_OPERAND (t, 0);
1383 : 2038753 : tree expr = tsubst_valid_expression_requirement (t0, args, info);
1384 : 2036053 : if (expr == error_mark_node)
1385 : : return error_mark_node;
1386 : 1827181 : if (processing_template_decl)
1387 : 288 : return finish_simple_requirement (EXPR_LOCATION (t), expr);
1388 : 1826893 : return boolean_true_node;
1389 : : }
1390 : :
1391 : : /* Subroutine of tsubst_type_requirement that performs the actual substitution
1392 : : and diagnosing. Also used by tsubst_compound_requirement. */
1393 : :
1394 : : static tree
1395 : 5378104 : tsubst_type_requirement_1 (tree t, tree args, sat_info info, location_t loc)
1396 : : {
1397 : 5378104 : tsubst_flags_t quiet = info.complain & ~tf_warning_or_error;
1398 : 5378104 : tree r = tsubst (t, args, quiet, info.in_decl);
1399 : 5378104 : if (r != error_mark_node)
1400 : : return r;
1401 : :
1402 : 179044 : if (info.diagnose_unsatisfaction_p ())
1403 : : {
1404 : 21 : if (diagnosing_failed_constraint::replay_errors_p ())
1405 : : {
1406 : : /* Replay the substitution error. */
1407 : 0 : inform (loc, "the required type %qT is invalid, because", t);
1408 : 0 : tsubst (t, args, info.complain, info.in_decl);
1409 : : }
1410 : : else
1411 : 21 : inform (loc, "the required type %qT is invalid", t);
1412 : : }
1413 : 179023 : else if (info.noisy ())
1414 : 0 : tsubst (t, args, info.complain, info.in_decl);
1415 : :
1416 : 179044 : return error_mark_node;
1417 : : }
1418 : :
1419 : :
1420 : : /* Substitute through the type requirement. */
1421 : :
1422 : : static tree
1423 : 1552485 : tsubst_type_requirement (tree t, tree args, sat_info info)
1424 : : {
1425 : 1552485 : tree t0 = TREE_OPERAND (t, 0);
1426 : 1552485 : tree type = tsubst_type_requirement_1 (t0, args, info, EXPR_LOCATION (t));
1427 : 1552485 : if (type == error_mark_node)
1428 : : return error_mark_node;
1429 : 1373441 : if (processing_template_decl)
1430 : 8 : return finish_type_requirement (EXPR_LOCATION (t), type);
1431 : 1373433 : return boolean_true_node;
1432 : : }
1433 : :
1434 : : /* True if TYPE can be deduced from EXPR. */
1435 : :
1436 : : static bool
1437 : 3707335 : type_deducible_p (tree expr, tree type, tree placeholder, tree args,
1438 : : subst_info info)
1439 : : {
1440 : : /* Make sure deduction is performed against ( EXPR ), so that
1441 : : references are preserved in the result. */
1442 : 3707335 : expr = force_paren_expr_uneval (expr);
1443 : :
1444 : 3707335 : tree deduced_type = do_auto_deduction (type, expr, placeholder,
1445 : : info.complain, adc_requirement,
1446 : : /*outer_targs=*/args);
1447 : :
1448 : 3707335 : return deduced_type != error_mark_node;
1449 : : }
1450 : :
1451 : : /* True if EXPR can not be converted to TYPE. */
1452 : :
1453 : : static bool
1454 : 10 : expression_convertible_p (tree expr, tree type, subst_info info)
1455 : : {
1456 : 10 : tree conv =
1457 : 10 : perform_direct_initialization_if_possible (type, expr, false,
1458 : : info.complain);
1459 : 10 : if (conv == error_mark_node)
1460 : : return false;
1461 : 0 : if (conv == NULL_TREE)
1462 : : {
1463 : 0 : if (info.complain & tf_error)
1464 : : {
1465 : 0 : location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
1466 : 0 : error_at (loc, "cannot convert %qE to %qT", expr, type);
1467 : : }
1468 : 0 : return false;
1469 : : }
1470 : : return true;
1471 : : }
1472 : :
1473 : :
1474 : : /* Substitute through the compound requirement. */
1475 : :
1476 : : static tree
1477 : 3904523 : tsubst_compound_requirement (tree t, tree args, sat_info info)
1478 : : {
1479 : 3904523 : tree t0 = TREE_OPERAND (t, 0);
1480 : 3904523 : tree t1 = TREE_OPERAND (t, 1);
1481 : 3904523 : tree expr = tsubst_valid_expression_requirement (t0, args, info);
1482 : 3904523 : if (expr == error_mark_node)
1483 : : return error_mark_node;
1484 : :
1485 : 3825637 : location_t loc = cp_expr_loc_or_input_loc (expr);
1486 : :
1487 : 3825637 : subst_info quiet (info.complain & ~tf_warning_or_error, info.in_decl);
1488 : :
1489 : : /* Check the noexcept condition. */
1490 : 3825637 : bool noexcept_p = COMPOUND_REQ_NOEXCEPT_P (t);
1491 : 117095 : if (noexcept_p && !processing_template_decl
1492 : 3942729 : && !expr_noexcept_p (expr, quiet.complain))
1493 : : {
1494 : 22 : if (info.diagnose_unsatisfaction_p ())
1495 : 4 : inform (loc, "%qE is not %<noexcept%>", expr);
1496 : : else
1497 : 18 : return error_mark_node;
1498 : : }
1499 : :
1500 : : /* Substitute through the type expression, if any. */
1501 : 3825619 : tree type = tsubst_type_requirement_1 (t1, args, info, EXPR_LOCATION (t));
1502 : 3825619 : if (type == error_mark_node)
1503 : : return error_mark_node;
1504 : :
1505 : : /* Check expression against the result type. */
1506 : 3825619 : if (type && !processing_template_decl)
1507 : : {
1508 : 3707333 : if (tree placeholder = type_uses_auto (type))
1509 : : {
1510 : 3707323 : if (!type_deducible_p (expr, type, placeholder, args, quiet))
1511 : : {
1512 : 6500 : if (info.diagnose_unsatisfaction_p ())
1513 : : {
1514 : 38 : if (diagnosing_failed_constraint::replay_errors_p ())
1515 : : {
1516 : 12 : inform (loc,
1517 : : "%qE does not satisfy return-type-requirement, "
1518 : : "because", t0);
1519 : : /* Further explain the reason for the error. */
1520 : 12 : type_deducible_p (expr, type, placeholder, args, info);
1521 : : }
1522 : : else
1523 : 26 : inform (loc,
1524 : : "%qE does not satisfy return-type-requirement", t0);
1525 : : }
1526 : 6500 : return error_mark_node;
1527 : : }
1528 : : }
1529 : 10 : else if (!expression_convertible_p (expr, type, quiet))
1530 : : {
1531 : 10 : if (info.diagnose_unsatisfaction_p ())
1532 : : {
1533 : 0 : if (diagnosing_failed_constraint::replay_errors_p ())
1534 : : {
1535 : 0 : inform (loc, "cannot convert %qE to %qT because", t0, type);
1536 : : /* Further explain the reason for the error. */
1537 : 0 : expression_convertible_p (expr, type, info);
1538 : : }
1539 : : else
1540 : 0 : inform (loc, "cannot convert %qE to %qT", t0, type);
1541 : : }
1542 : 10 : return error_mark_node;
1543 : : }
1544 : : }
1545 : :
1546 : 3819109 : if (processing_template_decl)
1547 : 1166 : return finish_compound_requirement (EXPR_LOCATION (t),
1548 : 1166 : expr, type, noexcept_p);
1549 : 3817943 : return boolean_true_node;
1550 : : }
1551 : :
1552 : : /* Substitute through the nested requirement. */
1553 : :
1554 : : static tree
1555 : 310498 : tsubst_nested_requirement (tree t, tree args, sat_info info)
1556 : : {
1557 : 310498 : if (processing_template_decl)
1558 : : {
1559 : 0 : tree req = TREE_OPERAND (t, 0);
1560 : 0 : req = tsubst_constraint (req, args, info.complain, info.in_decl);
1561 : 0 : if (req == error_mark_node)
1562 : : return error_mark_node;
1563 : 0 : return finish_nested_requirement (EXPR_LOCATION (t), req);
1564 : : }
1565 : :
1566 : 310498 : sat_info quiet (info.complain & ~tf_warning_or_error, info.in_decl);
1567 : 310498 : tree result = constraint_satisfaction_value (t, args, quiet);
1568 : 310498 : if (result == boolean_true_node)
1569 : : return boolean_true_node;
1570 : :
1571 : 837 : if (result == boolean_false_node
1572 : 837 : && info.diagnose_unsatisfaction_p ())
1573 : : {
1574 : 76 : tree expr = TREE_OPERAND (t, 0);
1575 : 76 : location_t loc = cp_expr_location (t);
1576 : 76 : if (diagnosing_failed_constraint::replay_errors_p ())
1577 : : {
1578 : : /* Replay the substitution error. */
1579 : 27 : inform (loc, "nested requirement %qE is not satisfied, because", expr);
1580 : 27 : constraint_satisfaction_value (t, args, info);
1581 : : }
1582 : : else
1583 : 49 : inform (loc, "nested requirement %qE is not satisfied", expr);
1584 : : }
1585 : :
1586 : 837 : return error_mark_node;
1587 : : }
1588 : :
1589 : : /* Substitute ARGS into the requirement T. */
1590 : :
1591 : : static tree
1592 : 7806259 : tsubst_requirement (tree t, tree args, sat_info info)
1593 : : {
1594 : 7806259 : iloc_sentinel loc_s (cp_expr_location (t));
1595 : 7806259 : switch (TREE_CODE (t))
1596 : : {
1597 : 2038753 : case SIMPLE_REQ:
1598 : 2038753 : return tsubst_simple_requirement (t, args, info);
1599 : 1552485 : case TYPE_REQ:
1600 : 1552485 : return tsubst_type_requirement (t, args, info);
1601 : 3904523 : case COMPOUND_REQ:
1602 : 3904523 : return tsubst_compound_requirement (t, args, info);
1603 : 310498 : case NESTED_REQ:
1604 : 310498 : return tsubst_nested_requirement (t, args, info);
1605 : 0 : default:
1606 : 0 : break;
1607 : : }
1608 : 0 : gcc_unreachable ();
1609 : 7803559 : }
1610 : :
1611 : : static tree
1612 : 2583300 : declare_constraint_vars (tree parms, tree vars)
1613 : : {
1614 : 2583300 : tree s = vars;
1615 : 6626970 : for (tree t = parms; t; t = DECL_CHAIN (t))
1616 : : {
1617 : 4043670 : if (DECL_PACK_P (t))
1618 : : {
1619 : 1414 : tree pack = extract_fnparm_pack (t, &s);
1620 : 1414 : register_local_specialization (pack, t);
1621 : : }
1622 : : else
1623 : : {
1624 : 4042256 : register_local_specialization (s, t);
1625 : 4042256 : s = DECL_CHAIN (s);
1626 : : }
1627 : : }
1628 : 2583300 : return vars;
1629 : : }
1630 : :
1631 : : /* Substitute through as if checking function parameter types. This
1632 : : will diagnose common parameter type errors. Returns error_mark_node
1633 : : if an error occurred. */
1634 : :
1635 : : static tree
1636 : 2583354 : check_constraint_variables (tree t, tree args, subst_info info)
1637 : : {
1638 : 2583354 : tree types = NULL_TREE;
1639 : 2583354 : tree p = t;
1640 : 6627084 : while (p && !VOID_TYPE_P (p))
1641 : : {
1642 : 4043730 : types = tree_cons (NULL_TREE, TREE_TYPE (p), types);
1643 : 4043730 : p = TREE_CHAIN (p);
1644 : : }
1645 : 2583354 : types = chainon (nreverse (types), void_list_node);
1646 : 2583354 : return tsubst_function_parms (types, args, info.complain, info.in_decl);
1647 : : }
1648 : :
1649 : : /* A subroutine of tsubst_parameterized_constraint. Substitute ARGS
1650 : : into the parameter list T, producing a sequence of constraint
1651 : : variables, declared in the current scope.
1652 : :
1653 : : Note that the caller must establish a local specialization stack
1654 : : prior to calling this function since this substitution will
1655 : : declare the substituted parameters. */
1656 : :
1657 : : static tree
1658 : 2583354 : tsubst_constraint_variables (tree t, tree args, subst_info info)
1659 : : {
1660 : : /* Perform a trial substitution to check for type errors. */
1661 : 2583354 : tree parms = check_constraint_variables (t, args, info);
1662 : 2583354 : if (parms == error_mark_node)
1663 : : return error_mark_node;
1664 : :
1665 : : /* Clear cp_unevaluated_operand across tsubst so that we get a proper chain
1666 : : of PARM_DECLs. */
1667 : 2583300 : int saved_unevaluated_operand = cp_unevaluated_operand;
1668 : 2583300 : cp_unevaluated_operand = 0;
1669 : 2583300 : tree vars = tsubst (t, args, info.complain, info.in_decl);
1670 : 2583300 : cp_unevaluated_operand = saved_unevaluated_operand;
1671 : 2583300 : if (vars == error_mark_node)
1672 : : return error_mark_node;
1673 : 2583300 : return declare_constraint_vars (t, vars);
1674 : : }
1675 : :
1676 : : /* Substitute ARGS into the requires-expression T. [8.4.7]p6. The
1677 : : substitution of template arguments into a requires-expression
1678 : : may result in the formation of invalid types or expressions
1679 : : in its requirements ... In such cases, the expression evaluates
1680 : : to false; it does not cause the program to be ill-formed.
1681 : :
1682 : : When substituting through a REQUIRES_EXPR as part of template
1683 : : instantiation, we call this routine with info.quiet() true.
1684 : :
1685 : : When evaluating a REQUIRES_EXPR that appears outside a template in
1686 : : cp_parser_requires_expression, we call this routine with
1687 : : info.noisy() true.
1688 : :
1689 : : Finally, when diagnosing unsatisfaction from diagnose_atomic_constraint
1690 : : and when diagnosing a false REQUIRES_EXPR via diagnose_constraints,
1691 : : we call this routine with info.diagnose_unsatisfaction_p() true. */
1692 : :
1693 : : static tree
1694 : 4794893 : tsubst_requires_expr (tree t, tree args, sat_info info)
1695 : : {
1696 : 4794893 : local_specialization_stack stack (lss_copy);
1697 : :
1698 : : /* We need to check access during the substitution. */
1699 : 4794893 : deferring_access_check_sentinel acs (dk_no_deferred);
1700 : :
1701 : : /* A requires-expression is an unevaluated context. */
1702 : 4794893 : cp_unevaluated u;
1703 : :
1704 : 4794893 : args = add_extra_args (REQUIRES_EXPR_EXTRA_ARGS (t), args,
1705 : : info.complain, info.in_decl);
1706 : 4794893 : if (processing_template_decl
1707 : 4794893 : && !processing_constraint_expression_p ())
1708 : : {
1709 : : /* We're partially instantiating a generic lambda. Substituting into
1710 : : this requires-expression now may cause its requirements to get
1711 : : checked out of order, so instead just remember the template
1712 : : arguments and wait until we can substitute them all at once.
1713 : :
1714 : : Except if this requires-expr is part of associated constraints
1715 : : that we're substituting into directly (for e.g. declaration
1716 : : matching or dguide constraint rewriting), in which case we need
1717 : : to partially substitute. */
1718 : 1553 : t = copy_node (t);
1719 : 1553 : REQUIRES_EXPR_EXTRA_ARGS (t) = NULL_TREE;
1720 : 1553 : REQUIRES_EXPR_EXTRA_ARGS (t) = build_extra_args (t, args, info.complain);
1721 : 1553 : return t;
1722 : : }
1723 : :
1724 : 4793340 : tree parms = REQUIRES_EXPR_PARMS (t);
1725 : 4793340 : if (parms)
1726 : : {
1727 : 2583354 : parms = tsubst_constraint_variables (parms, args, info);
1728 : 2583354 : if (parms == error_mark_node)
1729 : 54 : return boolean_false_node;
1730 : : }
1731 : :
1732 : 4793286 : tree result = boolean_true_node;
1733 : 4793286 : if (processing_template_decl)
1734 : 1219 : result = NULL_TREE;
1735 : 12123046 : for (tree reqs = REQUIRES_EXPR_REQS (t); reqs; reqs = TREE_CHAIN (reqs))
1736 : : {
1737 : 7806259 : tree req = TREE_VALUE (reqs);
1738 : 7806259 : req = tsubst_requirement (req, args, info);
1739 : 7803559 : if (req == error_mark_node)
1740 : : {
1741 : 474167 : result = boolean_false_node;
1742 : 474167 : if (info.diagnose_unsatisfaction_p ())
1743 : : /* Keep going so that we diagnose all failed requirements. */;
1744 : : else
1745 : : break;
1746 : : }
1747 : 7329392 : else if (processing_template_decl)
1748 : 1462 : result = tree_cons (NULL_TREE, req, result);
1749 : : }
1750 : 4790586 : if (processing_template_decl && result != boolean_false_node)
1751 : 1219 : result = finish_requires_expr (EXPR_LOCATION (t), parms, nreverse (result));
1752 : : return result;
1753 : 4792193 : }
1754 : :
1755 : : /* Public wrapper for the above. */
1756 : :
1757 : : tree
1758 : 4794433 : tsubst_requires_expr (tree t, tree args,
1759 : : tsubst_flags_t complain, tree in_decl)
1760 : : {
1761 : 4794433 : sat_info info (complain, in_decl);
1762 : 4794433 : return tsubst_requires_expr (t, args, info);
1763 : : }
1764 : :
1765 : : /* Substitute ARGS into the constraint information CI, producing a new
1766 : : constraint record. */
1767 : :
1768 : : tree
1769 : 664248 : tsubst_constraint_info (tree t, tree args,
1770 : : tsubst_flags_t complain, tree in_decl)
1771 : : {
1772 : 664248 : if (!t || t == error_mark_node || !check_constraint_info (t))
1773 : : return NULL_TREE;
1774 : :
1775 : 107904 : tree tr = tsubst_constraint (CI_TEMPLATE_REQS (t), args, complain, in_decl);
1776 : 215808 : tree dr = tsubst_constraint (CI_DECLARATOR_REQS (t), args, complain, in_decl);
1777 : 107904 : return build_constraints (tr, dr);
1778 : : }
1779 : :
1780 : : /* Substitute through a parameter mapping, in order to get the actual
1781 : : arguments used to instantiate an atomic constraint. This may fail
1782 : : if the substitution into arguments produces something ill-formed. */
1783 : :
1784 : : static tree
1785 : 27951059 : tsubst_parameter_mapping (tree map, tree args, subst_info info)
1786 : : {
1787 : 27951059 : if (!map)
1788 : : return NULL_TREE;
1789 : :
1790 : 27950397 : tsubst_flags_t complain = info.complain;
1791 : 27950397 : tree in_decl = info.in_decl;
1792 : :
1793 : 27950397 : tree result = NULL_TREE;
1794 : 76304549 : for (tree p = map; p; p = TREE_CHAIN (p))
1795 : : {
1796 : 48357289 : if (p == error_mark_node)
1797 : : return error_mark_node;
1798 : 48357289 : tree parm = TREE_VALUE (p);
1799 : 48357289 : tree arg = TREE_PURPOSE (p);
1800 : 48357289 : tree new_arg;
1801 : 48357289 : if (ARGUMENT_PACK_P (arg))
1802 : 1771213 : new_arg = tsubst_argument_pack (arg, args, complain, in_decl);
1803 : : else
1804 : : {
1805 : 46586076 : new_arg = tsubst_template_arg (arg, args, complain, in_decl);
1806 : 46586076 : if (TYPE_P (new_arg))
1807 : 46497363 : new_arg = canonicalize_type_argument (new_arg, complain);
1808 : : }
1809 : 48357289 : if (TREE_CODE (new_arg) == TYPE_ARGUMENT_PACK)
1810 : : {
1811 : 1769430 : tree pack_args = ARGUMENT_PACK_ARGS (new_arg);
1812 : 3516997 : for (tree& pack_arg : tree_vec_range (pack_args))
1813 : 1747567 : if (TYPE_P (pack_arg))
1814 : 1747567 : pack_arg = canonicalize_type_argument (pack_arg, complain);
1815 : : }
1816 : 48357289 : if (new_arg == error_mark_node)
1817 : : return error_mark_node;
1818 : :
1819 : 48354152 : result = tree_cons (new_arg, parm, result);
1820 : : }
1821 : 27947260 : return nreverse (result);
1822 : : }
1823 : :
1824 : : tree
1825 : 1187 : tsubst_parameter_mapping (tree map, tree args, tsubst_flags_t complain, tree in_decl)
1826 : : {
1827 : 1187 : return tsubst_parameter_mapping (map, args, subst_info (complain, in_decl));
1828 : : }
1829 : :
1830 : : /*---------------------------------------------------------------------------
1831 : : Constraint satisfaction
1832 : : ---------------------------------------------------------------------------*/
1833 : :
1834 : : /* True if we are currently satisfying a constraint. */
1835 : :
1836 : : static bool satisfying_constraint;
1837 : :
1838 : : /* A vector of incomplete types (and of declarations with undeduced return type),
1839 : : appended to by note_failed_type_completion. The
1840 : : satisfaction caches use this in order to keep track of "potentially unstable"
1841 : : satisfaction results.
1842 : :
1843 : : Since references to entries in this vector are stored only in the
1844 : : GC-deletable sat_cache, it's safe to make this deletable as well. */
1845 : :
1846 : : static GTY((deletable)) vec<tree, va_gc> *failed_type_completions;
1847 : :
1848 : : /* A map of where types were found to be incomplete in SFINAE context, for
1849 : : warning if they are later completed. */
1850 : :
1851 : : static GTY((cache)) hash_map<tree, location_t, decl_location_traits> *failed_completions_map;
1852 : :
1853 : : /* Called whenever a type completion (or return type deduction) failure occurs
1854 : : that definitely affects the meaning of the program, by e.g. inducing
1855 : : substitution failure. */
1856 : :
1857 : : void
1858 : 4608 : note_failed_type_completion (tree t, tsubst_flags_t complain)
1859 : : {
1860 : 4608 : if (dependent_template_arg_p (t))
1861 : : return;
1862 : :
1863 : 4569 : gcc_checking_assert ((TYPE_P (t) && !COMPLETE_TYPE_P (t))
1864 : : || (DECL_P (t) && undeduced_auto_decl (t)));
1865 : :
1866 : 4569 : if (satisfying_constraint)
1867 : 207 : vec_safe_push (failed_type_completions, t);
1868 : :
1869 : 4569 : if (TYPE_P (t))
1870 : : {
1871 : 3957 : if (!CLASS_TYPE_P (t))
1872 : : return;
1873 : 3664 : t = TYPE_MAIN_DECL (t);
1874 : : }
1875 : 4276 : if (!(complain & tf_error)
1876 : 7972 : && warning_enabled_at (DECL_SOURCE_LOCATION (t),
1877 : 3696 : OPT_Wsfinae_incomplete_))
1878 : : {
1879 : 3696 : if (warn_sfinae_incomplete > 1)
1880 : : {
1881 : 0 : if (TREE_CODE (t) == TYPE_DECL)
1882 : 0 : warning (OPT_Wsfinae_incomplete_,
1883 : 0 : "failed to complete %qT in SFINAE context", TREE_TYPE (t));
1884 : : else
1885 : 0 : warning (OPT_Wsfinae_incomplete_,
1886 : : "failed to deduce %qD in SFINAE context", t);
1887 : : }
1888 : 3696 : if (!failed_completions_map)
1889 : 102 : failed_completions_map
1890 : 102 : = hash_map<tree, location_t, decl_location_traits>::create_ggc ();
1891 : 3696 : failed_completions_map->put (t, input_location);
1892 : : }
1893 : : }
1894 : :
1895 : : /* If T was previously found to be incomplete in SFINAE context, return the
1896 : : location where that happened, otherwise UNKNOWN_LOCATION. */
1897 : :
1898 : : location_t
1899 : 40144926 : failed_completion_location (tree t)
1900 : : {
1901 : 40144926 : if (failed_completions_map)
1902 : : {
1903 : 84337 : if (TYPE_P (t))
1904 : 69369 : t = TYPE_MAIN_DECL (t);
1905 : 84337 : if (location_t *p = failed_completions_map->get (t))
1906 : 17 : return *p;
1907 : : }
1908 : : return UNKNOWN_LOCATION;
1909 : : }
1910 : :
1911 : : /* Returns true if the range [BEGIN, END) of elements within the
1912 : : failed_type_completions vector contains a complete type (or a
1913 : : declaration with a non-placeholder return type). */
1914 : :
1915 : : static bool
1916 : 355429223 : some_type_complete_p (int begin, int end)
1917 : : {
1918 : 355429838 : for (int i = begin; i < end; i++)
1919 : : {
1920 : 678 : tree t = (*failed_type_completions)[i];
1921 : 678 : if (TYPE_P (t) && COMPLETE_TYPE_P (t))
1922 : : return true;
1923 : 633 : if (DECL_P (t) && !undeduced_auto_decl (t))
1924 : : return true;
1925 : : }
1926 : : return false;
1927 : : }
1928 : :
1929 : : /* Hash functions and data types for satisfaction cache entries. */
1930 : :
1931 : : struct GTY((for_user)) sat_entry
1932 : : {
1933 : : /* The relevant ATOMIC_CONSTR. */
1934 : : tree atom;
1935 : :
1936 : : /* The relevant template arguments. */
1937 : : tree args;
1938 : :
1939 : : /* The result of satisfaction of ATOM+ARGS.
1940 : : This is either boolean_true_node, boolean_false_node or error_mark_node,
1941 : : where error_mark_node indicates ill-formed satisfaction.
1942 : : It's set to NULL_TREE while computing satisfaction of ATOM+ARGS for
1943 : : the first time. */
1944 : : tree result;
1945 : :
1946 : : /* The value of input_location when satisfaction of ATOM+ARGS was first
1947 : : performed. */
1948 : : location_t location;
1949 : :
1950 : : /* The range of elements appended to the failed_type_completions vector
1951 : : during computation of this satisfaction result, encoded as a begin/end
1952 : : pair of offsets. */
1953 : : int ftc_begin, ftc_end;
1954 : :
1955 : : /* True if we want to diagnose the above instability when it's detected.
1956 : : We don't always want to do so, in order to avoid emitting duplicate
1957 : : diagnostics in some cases. */
1958 : : bool diagnose_instability;
1959 : :
1960 : : /* True if we're in the middle of computing this satisfaction result.
1961 : : Used during both quiet and noisy satisfaction to detect self-recursive
1962 : : satisfaction. */
1963 : : bool evaluating;
1964 : : };
1965 : :
1966 : : struct sat_hasher : ggc_ptr_hash<sat_entry>
1967 : : {
1968 : 2313150967 : static hashval_t hash (sat_entry *e)
1969 : : {
1970 : 2313150967 : auto cso = make_temp_override (comparing_specializations);
1971 : 2313150967 : ++comparing_specializations;
1972 : :
1973 : 2313150967 : if (ATOMIC_CONSTR_MAP_INSTANTIATED_P (e->atom))
1974 : : {
1975 : : /* Atoms with instantiated mappings are built during satisfaction.
1976 : : They live only inside the sat_cache, and we build one to query
1977 : : the cache with each time we instantiate a mapping. */
1978 : 571722700 : gcc_assert (!e->args);
1979 : 571722700 : return hash_atomic_constraint (e->atom);
1980 : : }
1981 : :
1982 : : /* Atoms with uninstantiated mappings are built during normalization.
1983 : : Since normalize_atom caches the atoms it returns, we can assume
1984 : : pointer-based identity for fast hashing and comparison. Even if this
1985 : : assumption is violated, that's okay, we'll just get a cache miss. */
1986 : 1741428267 : hashval_t value = htab_hash_pointer (e->atom);
1987 : :
1988 : 1741428267 : if (tree map = ATOMIC_CONSTR_MAP (e->atom))
1989 : : /* Only the parameters that are used in the targets of the mapping
1990 : : affect the satisfaction value of the atom. So we consider only
1991 : : the arguments for these parameters, and ignore the rest. */
1992 : 1741422245 : for (tree target_parms = TREE_TYPE (map);
1993 : 3845117887 : target_parms;
1994 : 2103695642 : target_parms = TREE_CHAIN (target_parms))
1995 : : {
1996 : 2103695642 : int level, index;
1997 : 2103695642 : tree parm = TREE_VALUE (target_parms);
1998 : 2103695642 : template_parm_level_and_index (parm, &level, &index);
1999 : 2103695642 : tree arg = TMPL_ARG (e->args, level, index);
2000 : 2103695642 : value = iterative_hash_template_arg (arg, value);
2001 : : }
2002 : : return value;
2003 : 2313150967 : }
2004 : :
2005 : 2520225327 : static bool equal (sat_entry *e1, sat_entry *e2)
2006 : : {
2007 : 2520225327 : auto cso = make_temp_override (comparing_specializations);
2008 : 2520225327 : ++comparing_specializations;
2009 : :
2010 : 2520225327 : if (ATOMIC_CONSTR_MAP_INSTANTIATED_P (e1->atom)
2011 : 2520225327 : != ATOMIC_CONSTR_MAP_INSTANTIATED_P (e2->atom))
2012 : : return false;
2013 : :
2014 : : /* See sat_hasher::hash. */
2015 : 1838100501 : if (ATOMIC_CONSTR_MAP_INSTANTIATED_P (e1->atom))
2016 : : {
2017 : 71131395 : gcc_assert (!e1->args && !e2->args);
2018 : 71131395 : return atomic_constraints_identical_p (e1->atom, e2->atom);
2019 : : }
2020 : :
2021 : 1766969106 : if (e1->atom != e2->atom)
2022 : : return false;
2023 : :
2024 : 302696176 : if (tree map = ATOMIC_CONSTR_MAP (e1->atom))
2025 : 302694276 : for (tree target_parms = TREE_TYPE (map);
2026 : 621733078 : target_parms;
2027 : 319038802 : target_parms = TREE_CHAIN (target_parms))
2028 : : {
2029 : 322196606 : int level, index;
2030 : 322196606 : tree parm = TREE_VALUE (target_parms);
2031 : 322196606 : template_parm_level_and_index (parm, &level, &index);
2032 : 322196606 : tree arg1 = TMPL_ARG (e1->args, level, index);
2033 : 322196606 : tree arg2 = TMPL_ARG (e2->args, level, index);
2034 : 322196606 : if (!template_args_equal (arg1, arg2))
2035 : 3157804 : return false;
2036 : : }
2037 : : return true;
2038 : 2520225327 : }
2039 : : };
2040 : :
2041 : : /* Cache the result of satisfy_atom. */
2042 : : static GTY((deletable)) hash_table<sat_hasher> *sat_cache;
2043 : :
2044 : : /* Cache the result of satisfy_declaration_constraints. */
2045 : : static GTY((deletable)) hash_map<tree, tree> *decl_satisfied_cache;
2046 : :
2047 : : /* A tool used by satisfy_atom to help manage satisfaction caching and to
2048 : : diagnose "unstable" satisfaction values. We insert into the cache only
2049 : : when performing satisfaction quietly. */
2050 : :
2051 : : struct satisfaction_cache
2052 : : {
2053 : : satisfaction_cache (tree, tree, sat_info);
2054 : : tree get ();
2055 : : tree save (tree);
2056 : :
2057 : : sat_entry *entry;
2058 : : sat_info info;
2059 : : int ftc_begin;
2060 : : };
2061 : :
2062 : : /* Constructor for the satisfaction_cache class. We're performing satisfaction
2063 : : of ATOM+ARGS according to INFO. */
2064 : :
2065 : 355429265 : satisfaction_cache
2066 : 355429265 : ::satisfaction_cache (tree atom, tree args, sat_info info)
2067 : 355429265 : : entry(nullptr), info(info), ftc_begin(-1)
2068 : : {
2069 : 355429265 : if (!sat_cache)
2070 : 21103 : sat_cache = hash_table<sat_hasher>::create_ggc (31);
2071 : :
2072 : : /* When noisy, we query the satisfaction cache in order to diagnose
2073 : : "unstable" satisfaction values. */
2074 : 355429265 : if (info.noisy ())
2075 : : {
2076 : : /* When noisy, constraints have been re-normalized, and that breaks the
2077 : : pointer-based identity assumption of sat_cache (for atoms with
2078 : : uninstantiated mappings). So undo this re-normalization by looking in
2079 : : the atom_cache for the corresponding atom that was used during quiet
2080 : : satisfaction. */
2081 : 11387 : if (!ATOMIC_CONSTR_MAP_INSTANTIATED_P (atom))
2082 : : {
2083 : 5706 : if (tree found = atom_cache->find (atom))
2084 : 5706 : atom = found;
2085 : : else
2086 : : /* The lookup should always succeed, but if it fails then let's
2087 : : just leave 'entry' empty, effectively disabling the cache. */
2088 : 0 : return;
2089 : : }
2090 : : }
2091 : :
2092 : : /* Look up or create the corresponding satisfaction entry. */
2093 : 355429265 : sat_entry elt;
2094 : 355429265 : elt.atom = atom;
2095 : 355429265 : elt.args = args;
2096 : 355429265 : sat_entry **slot = sat_cache->find_slot (&elt, INSERT);
2097 : 355429265 : if (*slot)
2098 : 317107238 : entry = *slot;
2099 : 38322027 : else if (info.quiet ())
2100 : : {
2101 : 38322021 : entry = ggc_alloc<sat_entry> ();
2102 : 38322021 : entry->atom = atom;
2103 : 38322021 : entry->args = args;
2104 : 38322021 : entry->result = NULL_TREE;
2105 : 38322021 : entry->location = input_location;
2106 : 38322021 : entry->ftc_begin = entry->ftc_end = -1;
2107 : 38322021 : entry->diagnose_instability = false;
2108 : 38322021 : if (ATOMIC_CONSTR_MAP_INSTANTIATED_P (atom))
2109 : : /* We always want to diagnose instability of an atom with an
2110 : : instantiated parameter mapping. For atoms with an uninstantiated
2111 : : mapping, we set this flag (in satisfy_atom) only if substitution
2112 : : into its mapping previously failed. */
2113 : 10377868 : entry->diagnose_instability = true;
2114 : 38322021 : entry->evaluating = false;
2115 : 38322021 : *slot = entry;
2116 : : }
2117 : : else
2118 : : {
2119 : : /* We're evaluating this atom for the first time, and doing so noisily.
2120 : : This shouldn't happen outside of error recovery situations involving
2121 : : unstable satisfaction. Let's just leave 'entry' empty, effectively
2122 : : disabling the cache, and remove the empty slot. */
2123 : 6 : gcc_checking_assert (seen_error ());
2124 : : /* Appease hash_table::check_complete_insertion. */
2125 : 6 : *slot = ggc_alloc<sat_entry> ();
2126 : 6 : sat_cache->clear_slot (slot);
2127 : : }
2128 : : }
2129 : :
2130 : : /* Returns the cached satisfaction result if we have one and we're not
2131 : : recomputing the satisfaction result from scratch. Otherwise returns
2132 : : NULL_TREE. */
2133 : :
2134 : : tree
2135 : 355429265 : satisfaction_cache::get ()
2136 : : {
2137 : 355429265 : if (!entry)
2138 : : return NULL_TREE;
2139 : :
2140 : 355429259 : if (entry->evaluating)
2141 : : {
2142 : : /* If we get here, it means satisfaction is self-recursive. */
2143 : 36 : gcc_checking_assert (!entry->result || seen_error ());
2144 : 36 : if (info.noisy ())
2145 : 18 : error_at (EXPR_LOCATION (ATOMIC_CONSTR_EXPR (entry->atom)),
2146 : : "satisfaction of atomic constraint %qE depends on itself",
2147 : 18 : entry->atom);
2148 : 36 : return error_mark_node;
2149 : : }
2150 : :
2151 : : /* This satisfaction result is "potentially unstable" if a type for which
2152 : : type completion failed during its earlier computation is now complete. */
2153 : 355429223 : bool maybe_unstable = some_type_complete_p (entry->ftc_begin,
2154 : : entry->ftc_end);
2155 : :
2156 : 355429223 : if (info.noisy () || maybe_unstable || !entry->result)
2157 : : {
2158 : : /* We're computing the satisfaction result from scratch. */
2159 : 38333426 : entry->evaluating = true;
2160 : 38333426 : ftc_begin = vec_safe_length (failed_type_completions);
2161 : 38333426 : return NULL_TREE;
2162 : : }
2163 : : else
2164 : : return entry->result;
2165 : : }
2166 : :
2167 : : /* RESULT is the computed satisfaction result. If RESULT differs from the
2168 : : previously cached result, this routine issues an appropriate error.
2169 : : Otherwise, when evaluating quietly, updates the cache appropriately. */
2170 : :
2171 : : tree
2172 : 38328032 : satisfaction_cache::save (tree result)
2173 : : {
2174 : 38328032 : if (!entry)
2175 : : return result;
2176 : :
2177 : 38328026 : gcc_checking_assert (entry->evaluating);
2178 : 38328026 : entry->evaluating = false;
2179 : :
2180 : 38328026 : if (entry->result && result != entry->result)
2181 : : {
2182 : 42 : if (info.quiet ())
2183 : : /* Return error_mark_node to force satisfaction to get replayed
2184 : : noisily. */
2185 : 21 : return error_mark_node;
2186 : : else
2187 : : {
2188 : 21 : if (entry->diagnose_instability)
2189 : : {
2190 : 12 : auto_diagnostic_group d;
2191 : 12 : error_at (EXPR_LOCATION (ATOMIC_CONSTR_EXPR (entry->atom)),
2192 : : "satisfaction value of atomic constraint %qE changed "
2193 : 12 : "from %qE to %qE", entry->atom, entry->result, result);
2194 : 12 : inform (entry->location,
2195 : : "satisfaction value first evaluated to %qE from here",
2196 : 12 : entry->result);
2197 : 12 : }
2198 : : /* For sake of error recovery, allow this latest satisfaction result
2199 : : to prevail. */
2200 : 21 : entry->result = result;
2201 : 21 : return result;
2202 : : }
2203 : : }
2204 : :
2205 : 38327984 : if (info.quiet ())
2206 : : {
2207 : 38316642 : entry->result = result;
2208 : : /* Store into this entry the list of relevant failed type completions
2209 : : that occurred during (re)computation of the satisfaction result. */
2210 : 38316642 : gcc_checking_assert (ftc_begin != -1);
2211 : 38316642 : entry->ftc_begin = ftc_begin;
2212 : 38648816 : entry->ftc_end = vec_safe_length (failed_type_completions);
2213 : : }
2214 : :
2215 : : return result;
2216 : : }
2217 : :
2218 : : /* Substitute ARGS into constraint-expression T during instantiation of
2219 : : a member of a class template. */
2220 : :
2221 : : tree
2222 : 1359780 : tsubst_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
2223 : : {
2224 : : /* We also don't want to evaluate concept-checks when substituting the
2225 : : constraint-expressions of a declaration. */
2226 : 1359780 : processing_constraint_expression_sentinel s;
2227 : 1359780 : cp_unevaluated u;
2228 : 1359780 : tree expr = tsubst_expr (t, args, complain, in_decl);
2229 : 2719560 : return expr;
2230 : 1359780 : }
2231 : :
2232 : : static tree satisfy_constraint_r (tree, tree, sat_info info);
2233 : :
2234 : : /* Compute the satisfaction of a conjunction. */
2235 : :
2236 : : static tree
2237 : 285349752 : satisfy_conjunction (tree t, tree args, sat_info info)
2238 : : {
2239 : 285349752 : tree lhs = satisfy_constraint_r (TREE_OPERAND (t, 0), args, info);
2240 : 285349752 : if (lhs == error_mark_node || lhs == boolean_false_node)
2241 : : return lhs;
2242 : 282213326 : return satisfy_constraint_r (TREE_OPERAND (t, 1), args, info);
2243 : : }
2244 : :
2245 : : /* The current depth at which we're replaying an error during recursive
2246 : : diagnosis of a constraint satisfaction failure. */
2247 : :
2248 : : static int current_constraint_diagnosis_depth;
2249 : :
2250 : : /* Whether CURRENT_CONSTRAINT_DIAGNOSIS_DEPTH has ever exceeded
2251 : : CONCEPTS_DIAGNOSTICS_MAX_DEPTH during recursive diagnosis of a constraint
2252 : : satisfaction error. */
2253 : :
2254 : : static bool concepts_diagnostics_max_depth_exceeded_p;
2255 : :
2256 : : /* Recursive subroutine of collect_operands_of_disjunction. T is a normalized
2257 : : subexpression of a constraint (composed of CONJ_CONSTRs and DISJ_CONSTRs)
2258 : : and E is the corresponding unnormalized subexpression (composed of
2259 : : TRUTH_ANDIF_EXPRs and TRUTH_ORIF_EXPRs). */
2260 : :
2261 : : static void
2262 : 17 : collect_operands_of_disjunction_r (tree t, tree e,
2263 : : auto_vec<tree_pair> *operands)
2264 : : {
2265 : 27 : if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
2266 : : {
2267 : 10 : collect_operands_of_disjunction_r (TREE_OPERAND (t, 0),
2268 : 10 : TREE_OPERAND (e, 0), operands);
2269 : 10 : collect_operands_of_disjunction_r (TREE_OPERAND (t, 1),
2270 : 10 : TREE_OPERAND (e, 1), operands);
2271 : : }
2272 : : else
2273 : : {
2274 : 17 : tree_pair p = std::make_pair (t, e);
2275 : 17 : operands->safe_push (p);
2276 : : }
2277 : 17 : }
2278 : :
2279 : : /* Recursively collect the normalized and unnormalized operands of the
2280 : : disjunction T and append them to OPERANDS in order. */
2281 : :
2282 : : static void
2283 : 7 : collect_operands_of_disjunction (tree t, auto_vec<tree_pair> *operands)
2284 : : {
2285 : 7 : collect_operands_of_disjunction_r (t, CONSTR_EXPR (t), operands);
2286 : 7 : }
2287 : :
2288 : : /* Compute the satisfaction of a disjunction. */
2289 : :
2290 : : static tree
2291 : 25686067 : satisfy_disjunction (tree t, tree args, sat_info info)
2292 : : {
2293 : : /* Evaluate each operand with unsatisfaction diagnostics disabled. */
2294 : 25686067 : sat_info sub = info;
2295 : 25686067 : sub.diagnose_unsatisfaction = false;
2296 : :
2297 : 25686067 : tree lhs = satisfy_constraint_r (TREE_OPERAND (t, 0), args, sub);
2298 : 25686067 : if (lhs == boolean_true_node || lhs == error_mark_node)
2299 : : return lhs;
2300 : :
2301 : 11778755 : tree rhs = satisfy_constraint_r (TREE_OPERAND (t, 1), args, sub);
2302 : 11778755 : if (rhs == boolean_true_node || rhs == error_mark_node)
2303 : : return rhs;
2304 : :
2305 : : /* Both branches evaluated to false. Explain the satisfaction failure in
2306 : : each branch. */
2307 : 1234368 : if (info.diagnose_unsatisfaction_p ())
2308 : : {
2309 : 62 : diagnosing_failed_constraint failure (t, args, info.noisy ());
2310 : 62 : cp_expr disj_expr = CONSTR_EXPR (t);
2311 : 62 : inform (disj_expr.get_location (),
2312 : : "no operand of the disjunction is satisfied");
2313 : 62 : if (diagnosing_failed_constraint::replay_errors_p ())
2314 : : {
2315 : 7 : auto_diagnostic_nesting_level sentinel;
2316 : : /* Replay the error in each branch of the disjunction. */
2317 : 7 : auto_vec<tree_pair> operands;
2318 : 7 : collect_operands_of_disjunction (t, &operands);
2319 : 24 : for (unsigned i = 0; i < operands.length (); i++)
2320 : : {
2321 : 17 : tree norm_op = operands[i].first;
2322 : 17 : tree op = operands[i].second;
2323 : 17 : location_t loc = make_location (cp_expr_location (op),
2324 : : disj_expr.get_start (),
2325 : : disj_expr.get_finish ());
2326 : 17 : inform (loc, "the operand %qE is unsatisfied because", op);
2327 : 17 : auto_diagnostic_nesting_level sentinel;
2328 : 17 : satisfy_constraint_r (norm_op, args, info);
2329 : 17 : }
2330 : 7 : }
2331 : 62 : }
2332 : :
2333 : 1234368 : return boolean_false_node;
2334 : : }
2335 : :
2336 : : /* Ensures that T is a truth value and not (accidentally, as sometimes
2337 : : happens) an integer value. */
2338 : :
2339 : : tree
2340 : 10375123 : satisfaction_value (tree t)
2341 : : {
2342 : 10375123 : if (t == error_mark_node || t == boolean_true_node || t == boolean_false_node)
2343 : : return t;
2344 : :
2345 : 1 : gcc_assert (TREE_CODE (t) == INTEGER_CST
2346 : : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t),
2347 : : boolean_type_node));
2348 : 1 : if (integer_zerop (t))
2349 : 0 : return boolean_false_node;
2350 : : else
2351 : 1 : return boolean_true_node;
2352 : : }
2353 : :
2354 : : /* Build a new template argument vector corresponding to the parameter
2355 : : mapping of the atomic constraint T, using arguments from ARGS. */
2356 : :
2357 : : static tree
2358 : 10383567 : get_mapped_args (tree t, tree args)
2359 : : {
2360 : 10383567 : tree map = ATOMIC_CONSTR_MAP (t);
2361 : :
2362 : : /* No map, no arguments. */
2363 : 10383567 : if (!map)
2364 : : return NULL_TREE;
2365 : :
2366 : : /* Determine the depth of the resulting argument vector. */
2367 : 10382905 : int depth;
2368 : 10382905 : if (ATOMIC_CONSTR_EXPR_FROM_CONCEPT_P (t))
2369 : : /* The expression of this atomic constraint comes from a concept
2370 : : definition, whose template depth is always one, so the resulting
2371 : : argument vector will also have depth one. */
2372 : : depth = 1;
2373 : : else
2374 : : /* Otherwise, the expression of this atomic constraint comes from
2375 : : the context of the constrained entity, whose template depth is that
2376 : : of ARGS. */
2377 : 5112398 : depth = TMPL_ARGS_DEPTH (args);
2378 : :
2379 : : /* Place each argument at its corresponding position in the argument
2380 : : list. Note that the list will be sparse (not all arguments supplied),
2381 : : but instantiation is guaranteed to only use the parameters in the
2382 : : mapping, so null arguments would never be used. */
2383 : 10382905 : auto_vec< vec<tree> > lists (depth);
2384 : 10382905 : lists.quick_grow_cleared (depth);
2385 : 26071080 : for (tree p = map; p; p = TREE_CHAIN (p))
2386 : : {
2387 : 15688175 : int level;
2388 : 15688175 : int index;
2389 : 15688175 : template_parm_level_and_index (TREE_VALUE (p), &level, &index);
2390 : :
2391 : : /* Insert the argument into its corresponding position. */
2392 : 15688175 : vec<tree> &list = lists[level - 1];
2393 : 20659967 : if (index >= (int)list.length ())
2394 : 14627402 : list.safe_grow_cleared (index + 1, /*exact=*/false);
2395 : 15688175 : list[index] = TREE_PURPOSE (p);
2396 : : }
2397 : :
2398 : : /* Build the new argument list. */
2399 : 10382905 : args = make_tree_vec (lists.length ());
2400 : 42675646 : for (unsigned i = 0; i != lists.length (); ++i)
2401 : : {
2402 : 10954918 : vec<tree> &list = lists[i];
2403 : 10954918 : tree level = make_tree_vec (list.length ());
2404 : 26788444 : for (unsigned j = 0; j < list.length (); ++j)
2405 : 15833526 : TREE_VEC_ELT (level, j) = list[j];
2406 : 10954918 : SET_TMPL_ARGS_LEVEL (args, i + 1, level);
2407 : 10954918 : list.release ();
2408 : : }
2409 : 10382905 : SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, 0);
2410 : :
2411 : 10382905 : if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
2412 : 10382905 : && TMPL_ARGS_DEPTH (args) == 1)
2413 : : {
2414 : : /* Get rid of the redundant outer TREE_VEC. */
2415 : 9816766 : tree level = TMPL_ARGS_LEVEL (args, 1);
2416 : 9816766 : ggc_free (args);
2417 : 9816766 : args = level;
2418 : : }
2419 : :
2420 : 10382905 : return args;
2421 : 10382905 : }
2422 : :
2423 : : static void diagnose_atomic_constraint (tree, tree, tree, sat_info);
2424 : :
2425 : : /* Compute the satisfaction of an atomic constraint. */
2426 : :
2427 : : static tree
2428 : 327482528 : satisfy_atom (tree t, tree args, sat_info info)
2429 : : {
2430 : : /* In case there is a diagnostic, we want to establish the context
2431 : : prior to printing errors. If no errors occur, this context is
2432 : : removed before returning. */
2433 : 327482528 : diagnosing_failed_constraint failure (t, args, info.noisy ());
2434 : :
2435 : 327482528 : satisfaction_cache cache (t, args, info);
2436 : 327482528 : if (tree r = cache.get ())
2437 : : return r;
2438 : :
2439 : : /* Perform substitution quietly. */
2440 : 27949865 : subst_info quiet (tf_none, NULL_TREE);
2441 : :
2442 : : /* Instantiate the parameter mapping. */
2443 : 27949865 : tree map = tsubst_parameter_mapping (ATOMIC_CONSTR_MAP (t), args, quiet);
2444 : 27949865 : if (map == error_mark_node)
2445 : : {
2446 : : /* If instantiation of the parameter mapping fails, the constraint is
2447 : : not satisfied. Replay the substitution. */
2448 : 3128 : if (info.diagnose_unsatisfaction_p ())
2449 : 7 : tsubst_parameter_mapping (ATOMIC_CONSTR_MAP (t), args, info);
2450 : 3128 : if (info.quiet ())
2451 : : /* Since instantiation of the parameter mapping failed, we
2452 : : want to diagnose potential instability of this satisfaction
2453 : : result. */
2454 : 3121 : cache.entry->diagnose_instability = true;
2455 : 3128 : return cache.save (boolean_false_node);
2456 : : }
2457 : :
2458 : : /* Now build a new atom using the instantiated mapping. We use
2459 : : this atom as a second key to the satisfaction cache, and we
2460 : : also pass it to diagnose_atomic_constraint so that diagnostics
2461 : : which refer to the atom display the instantiated mapping. */
2462 : 27946737 : t = copy_node (t);
2463 : 27946737 : ATOMIC_CONSTR_MAP (t) = map;
2464 : 27946737 : gcc_assert (!ATOMIC_CONSTR_MAP_INSTANTIATED_P (t));
2465 : 27946737 : ATOMIC_CONSTR_MAP_INSTANTIATED_P (t) = true;
2466 : 27946737 : satisfaction_cache inst_cache (t, /*args=*/NULL_TREE, info);
2467 : 27946737 : if (tree r = inst_cache.get ())
2468 : : {
2469 : 17563170 : cache.entry->location = inst_cache.entry->location;
2470 : 17563170 : return cache.save (r);
2471 : : }
2472 : :
2473 : : /* Rebuild the argument vector from the parameter mapping. */
2474 : 10383567 : args = get_mapped_args (t, args);
2475 : :
2476 : : /* Apply the parameter mapping (i.e., just substitute). */
2477 : 10383567 : tree expr = ATOMIC_CONSTR_EXPR (t);
2478 : 10383567 : tree result = tsubst_expr (expr, args, quiet.complain, quiet.in_decl);
2479 : 10380867 : if (result == error_mark_node)
2480 : : {
2481 : : /* If substitution results in an invalid type or expression, the
2482 : : constraint is not satisfied. Replay the substitution. */
2483 : 5697 : if (info.diagnose_unsatisfaction_p ())
2484 : 19 : tsubst_expr (expr, args, info.complain, info.in_decl);
2485 : 5697 : return cache.save (inst_cache.save (boolean_false_node));
2486 : : }
2487 : :
2488 : : /* [17.4.1.2] ... lvalue-to-rvalue conversion is performed as necessary,
2489 : : and EXPR shall be a constant expression of type bool. */
2490 : 10375170 : result = force_rvalue (result, info.complain);
2491 : 10375170 : if (result == error_mark_node)
2492 : 0 : return cache.save (inst_cache.save (error_mark_node));
2493 : 10375170 : tree substituted = result;
2494 : 10375170 : if (!same_type_p (TREE_TYPE (result), boolean_type_node))
2495 : : {
2496 : 47 : if (info.noisy ())
2497 : 26 : diagnose_atomic_constraint (t, args, substituted, info);
2498 : 47 : return cache.save (inst_cache.save (error_mark_node));
2499 : : }
2500 : :
2501 : : /* Compute the value of the constraint. */
2502 : 10375123 : if (info.noisy ())
2503 : : {
2504 : 5627 : iloc_sentinel ils (EXPR_LOCATION (result));
2505 : 5627 : result = cxx_constant_value (result);
2506 : 5627 : }
2507 : : else
2508 : : {
2509 : 10369496 : result = maybe_constant_value (result, NULL_TREE, mce_true);
2510 : 10369496 : if (!TREE_CONSTANT (result))
2511 : 22 : result = error_mark_node;
2512 : : }
2513 : 10375123 : result = satisfaction_value (result);
2514 : 10375123 : if (result == boolean_false_node && info.diagnose_unsatisfaction_p ())
2515 : 1200 : diagnose_atomic_constraint (t, args, substituted, info);
2516 : :
2517 : 10375123 : return cache.save (inst_cache.save (result));
2518 : 327479828 : }
2519 : :
2520 : : /* Determine if the normalized constraint T is satisfied.
2521 : : Returns boolean_true_node if the expression/constraint is
2522 : : satisfied, boolean_false_node if not, and error_mark_node
2523 : : if there was an error evaluating the constraint.
2524 : :
2525 : : The parameter mapping of atomic constraints is simply the
2526 : : set of template arguments that will be substituted into
2527 : : the expression, regardless of template parameters appearing
2528 : : within. Whether a template argument is used in the atomic
2529 : : constraint only matters for subsumption. */
2530 : :
2531 : : static tree
2532 : 638518484 : satisfy_constraint_r (tree t, tree args, sat_info info)
2533 : : {
2534 : 638518484 : if (t == error_mark_node)
2535 : : return error_mark_node;
2536 : :
2537 : 638518347 : switch (TREE_CODE (t))
2538 : : {
2539 : 285349752 : case CONJ_CONSTR:
2540 : 285349752 : return satisfy_conjunction (t, args, info);
2541 : 25686067 : case DISJ_CONSTR:
2542 : 25686067 : return satisfy_disjunction (t, args, info);
2543 : 327482528 : case ATOMIC_CONSTR:
2544 : 327482528 : return satisfy_atom (t, args, info);
2545 : 0 : default:
2546 : 0 : gcc_unreachable ();
2547 : : }
2548 : : }
2549 : :
2550 : : /* Check that the normalized constraint T is satisfied for ARGS. */
2551 : :
2552 : : static tree
2553 : 33490567 : satisfy_normalized_constraints (tree t, tree args, sat_info info)
2554 : : {
2555 : 33490567 : auto_timevar time (TV_CONSTRAINT_SAT);
2556 : :
2557 : 33490567 : auto ovr = make_temp_override (satisfying_constraint, true);
2558 : :
2559 : : /* Turn off template processing. Constraint satisfaction only applies
2560 : : to non-dependent terms, so we want to ensure full checking here. */
2561 : 33490567 : processing_template_decl_sentinel proc (true);
2562 : :
2563 : : /* We need to check access during satisfaction. */
2564 : 33490567 : deferring_access_check_sentinel acs (dk_no_deferred);
2565 : :
2566 : : /* Constraints are unevaluated operands. */
2567 : 33490567 : cp_unevaluated u;
2568 : :
2569 : 33490567 : return satisfy_constraint_r (t, args, info);
2570 : 33487867 : }
2571 : :
2572 : : /* Return the normal form of the constraints on the placeholder 'auto'
2573 : : type T. */
2574 : :
2575 : : static tree
2576 : 3708012 : normalize_placeholder_type_constraints (tree t, bool diag)
2577 : : {
2578 : 3708012 : gcc_assert (is_auto (t));
2579 : 3708012 : tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t);
2580 : 3708012 : if (!ci)
2581 : : return NULL_TREE;
2582 : :
2583 : 3708012 : tree constr = TREE_VALUE (ci);
2584 : : /* The TREE_PURPOSE contains the set of template parameters that were in
2585 : : scope for this placeholder type; use them as the initial template
2586 : : parameters for normalization. */
2587 : 3708012 : tree initial_parms = TREE_PURPOSE (ci);
2588 : :
2589 : : /* The 'auto' itself is used as the first argument in its own constraints,
2590 : : and its level is one greater than its template depth. So in order to
2591 : : capture all used template parameters, we need to add an extra level of
2592 : : template parameters to the context; a dummy level suffices. */
2593 : 3708012 : initial_parms
2594 : 7415757 : = tree_cons (size_int (initial_parms
2595 : : ? TMPL_PARMS_DEPTH (initial_parms) + 1 : 1),
2596 : : make_tree_vec (0), initial_parms);
2597 : :
2598 : 3708012 : norm_info info (diag);
2599 : 3708012 : info.initial_parms = initial_parms;
2600 : 3708012 : return normalize_constraint_expression (constr, info);
2601 : : }
2602 : :
2603 : : /* Evaluate the constraints of T using ARGS, returning a satisfaction value.
2604 : : Here, T can be a concept-id, nested-requirement, placeholder 'auto', or
2605 : : requires-expression. */
2606 : :
2607 : : static tree
2608 : 5322466 : satisfy_nondeclaration_constraints (tree t, tree args, sat_info info)
2609 : : {
2610 : 5322466 : if (t == error_mark_node)
2611 : : return error_mark_node;
2612 : :
2613 : : /* Handle REQUIRES_EXPR directly, bypassing satisfaction. */
2614 : 5322464 : if (TREE_CODE (t) == REQUIRES_EXPR)
2615 : : {
2616 : 160 : auto ovr = make_temp_override (current_constraint_diagnosis_depth);
2617 : 160 : if (info.noisy ())
2618 : 34 : ++current_constraint_diagnosis_depth;
2619 : 160 : return tsubst_requires_expr (t, args, info);
2620 : 160 : }
2621 : :
2622 : : /* Get the normalized constraints. */
2623 : 5322304 : tree norm;
2624 : 5322304 : if (concept_check_p (t))
2625 : : {
2626 : 1303758 : gcc_assert (!args);
2627 : 1303758 : args = TREE_OPERAND (t, 1);
2628 : 1303758 : tree tmpl = get_concept_check_template (t);
2629 : 1303758 : norm = normalize_concept_definition (tmpl, info.noisy ());
2630 : : }
2631 : 4018546 : else if (TREE_CODE (t) == NESTED_REQ)
2632 : : {
2633 : 310534 : norm_info ninfo (info.noisy ());
2634 : : /* The TREE_TYPE contains the set of template parameters that were in
2635 : : scope for this nested requirement; use them as the initial template
2636 : : parameters for normalization. */
2637 : 310534 : ninfo.initial_parms = TREE_TYPE (t);
2638 : 310534 : norm = normalize_constraint_expression (TREE_OPERAND (t, 0), ninfo);
2639 : : }
2640 : 3708012 : else if (is_auto (t))
2641 : : {
2642 : 3708012 : norm = normalize_placeholder_type_constraints (t, info.noisy ());
2643 : 3708012 : if (!norm)
2644 : 0 : return boolean_true_node;
2645 : : }
2646 : : else
2647 : 0 : gcc_unreachable ();
2648 : :
2649 : : /* Perform satisfaction. */
2650 : 5322304 : return satisfy_normalized_constraints (norm, args, info);
2651 : : }
2652 : :
2653 : : /* Evaluate the associated constraints of the template specialization T
2654 : : according to INFO, returning a satisfaction value. */
2655 : :
2656 : : static tree
2657 : 269439575 : satisfy_declaration_constraints (tree t, sat_info info)
2658 : : {
2659 : 269439575 : gcc_assert (DECL_P (t) && TREE_CODE (t) != TEMPLATE_DECL);
2660 : 269439575 : const tree saved_t = t;
2661 : :
2662 : : /* For inherited constructors, consider the original declaration;
2663 : : it has the correct template information attached. */
2664 : 269439575 : t = strip_inheriting_ctors (t);
2665 : 269439575 : tree inh_ctor_targs = NULL_TREE;
2666 : 269439575 : if (t != saved_t)
2667 : 113397 : if (tree ti = DECL_TEMPLATE_INFO (saved_t))
2668 : : /* The inherited constructor points to an instantiation of a constructor
2669 : : template; remember its template arguments. */
2670 : 10698 : inh_ctor_targs = TI_ARGS (ti);
2671 : :
2672 : : /* Update the declaration for diagnostics. */
2673 : 269439575 : info.in_decl = t;
2674 : :
2675 : 269439575 : if (info.quiet ())
2676 : 538832420 : if (tree *result = hash_map_safe_get (decl_satisfied_cache, saved_t))
2677 : 232764099 : return *result;
2678 : :
2679 : 36675476 : tree args = NULL_TREE;
2680 : 36675476 : if (tree ti = DECL_TEMPLATE_INFO (t))
2681 : : {
2682 : : /* The initial parameter mapping is the complete set of
2683 : : template arguments substituted into the declaration. */
2684 : 24879089 : args = TI_ARGS (ti);
2685 : 24879089 : if (inh_ctor_targs)
2686 : 3634 : args = add_outermost_template_args (args, inh_ctor_targs);
2687 : : }
2688 : :
2689 : 36675476 : if (regenerated_lambda_fn_p (t))
2690 : : {
2691 : : /* The TI_ARGS of a regenerated lambda contains only the innermost
2692 : : set of template arguments. Augment this with the outer template
2693 : : arguments that were used to regenerate the lambda. */
2694 : 489227 : gcc_assert (!args || TMPL_ARGS_DEPTH (args) == 1);
2695 : 311678 : tree regen_args = lambda_regenerating_args (t);
2696 : 311678 : if (args)
2697 : 177549 : args = add_to_template_args (regen_args, args);
2698 : : else
2699 : : args = regen_args;
2700 : : }
2701 : :
2702 : : /* If the innermost arguments are dependent, or if the outer arguments
2703 : : are dependent and are needed by the constraints, we can't check
2704 : : satisfaction yet so pretend they're satisfied for now. */
2705 : 36675476 : if (uses_template_parms (args)
2706 : 36675476 : && ((DECL_TEMPLATE_INFO (t)
2707 : 94482 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))
2708 : 42930 : && (TMPL_ARGS_DEPTH (args) == 1
2709 : 11840 : || uses_template_parms (INNERMOST_TEMPLATE_ARGS (args))))
2710 : 84905 : || uses_outer_template_parms_in_constraints (t)))
2711 : 9643 : return boolean_true_node;
2712 : :
2713 : : /* Get the normalized constraints. */
2714 : 36665833 : tree norm = get_normalized_constraints_from_decl (t, info.noisy ());
2715 : :
2716 : 36665833 : unsigned ftc_count = vec_safe_length (failed_type_completions);
2717 : :
2718 : 36665833 : tree result = boolean_true_node;
2719 : 36665833 : if (norm)
2720 : : {
2721 : 1255402 : if (!push_tinst_level (t))
2722 : : return result;
2723 : 1255148 : push_to_top_level ();
2724 : 1255148 : push_access_scope (t);
2725 : 1255148 : result = satisfy_normalized_constraints (norm, args, info);
2726 : 1255148 : pop_access_scope (t);
2727 : 1255148 : pop_from_top_level ();
2728 : 1255148 : pop_tinst_level ();
2729 : : }
2730 : :
2731 : : /* True if this satisfaction is (heuristically) potentially unstable, i.e.
2732 : : if its result may depend on where in the program it was performed. */
2733 : 36665579 : bool maybe_unstable_satisfaction = false;
2734 : 36829893 : if (ftc_count != vec_safe_length (failed_type_completions))
2735 : : /* Type completion failure occurred during satisfaction. The satisfaction
2736 : : result may (or may not) materially depend on the completeness of a type,
2737 : : so we consider it potentially unstable. */
2738 : : maybe_unstable_satisfaction = true;
2739 : :
2740 : 36665579 : if (maybe_unstable_satisfaction)
2741 : : /* Don't cache potentially unstable satisfaction, to allow satisfy_atom
2742 : : to check the stability the next time around. */;
2743 : 36665579 : else if (info.quiet ())
2744 : 36665486 : hash_map_safe_put<hm_ggc> (decl_satisfied_cache, saved_t, result);
2745 : :
2746 : 36665579 : return result;
2747 : : }
2748 : :
2749 : : /* Evaluate the associated constraints of the template T using ARGS as the
2750 : : innermost set of template arguments and according to INFO, returning a
2751 : : satisfaction value. */
2752 : :
2753 : : static tree
2754 : 151168918 : satisfy_declaration_constraints (tree t, tree args, sat_info info)
2755 : : {
2756 : 151168918 : tree orig_args = args;
2757 : :
2758 : : /* Update the declaration for diagnostics. */
2759 : 151168918 : info.in_decl = t;
2760 : :
2761 : 151168918 : gcc_assert (TREE_CODE (t) == TEMPLATE_DECL);
2762 : :
2763 : 151168918 : if (regenerated_lambda_fn_p (t))
2764 : : {
2765 : : /* As in the two-parameter version of this function. */
2766 : 370404 : gcc_assert (TMPL_ARGS_DEPTH (args) == 1);
2767 : 185202 : tree lambda = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t));
2768 : 185202 : tree outer_args = TI_ARGS (LAMBDA_EXPR_REGEN_INFO (lambda));
2769 : 185202 : args = add_to_template_args (outer_args, args);
2770 : : }
2771 : : else
2772 : 150983716 : args = add_outermost_template_args (t, args);
2773 : :
2774 : : /* If the innermost arguments are dependent, or if the outer arguments
2775 : : are dependent and are needed by the constraints, we can't check
2776 : : satisfaction yet so pretend they're satisfied for now. */
2777 : 151168918 : if (uses_template_parms (args)
2778 : 186128301 : && (TMPL_ARGS_DEPTH (args) == 1
2779 : 2264151 : || uses_template_parms (INNERMOST_TEMPLATE_ARGS (args))
2780 : 139482 : || uses_outer_template_parms_in_constraints (t)))
2781 : 34819901 : return boolean_true_node;
2782 : :
2783 : 116349017 : tree result = boolean_true_node;
2784 : 116349017 : if (tree norm = get_normalized_constraints_from_decl (t, info.noisy ()))
2785 : : {
2786 : 26913115 : if (!push_tinst_level (t, orig_args))
2787 : : return result;
2788 : 26913115 : tree pattern = DECL_TEMPLATE_RESULT (t);
2789 : 26913115 : push_to_top_level ();
2790 : 26913115 : push_access_scope (pattern);
2791 : 26913115 : result = satisfy_normalized_constraints (norm, args, info);
2792 : 26910415 : pop_access_scope (pattern);
2793 : 26910415 : pop_from_top_level ();
2794 : 26910415 : pop_tinst_level ();
2795 : : }
2796 : :
2797 : : return result;
2798 : : }
2799 : :
2800 : : /* A wrapper around satisfy_declaration_constraints and
2801 : : satisfy_nondeclaration_constraints which additionally replays
2802 : : quiet ill-formed satisfaction noisily, so that ill-formed
2803 : : satisfaction always gets diagnosed. */
2804 : :
2805 : : static tree
2806 : 425930959 : constraint_satisfaction_value (tree t, tree args, sat_info info)
2807 : : {
2808 : 425930959 : tree r;
2809 : 425930959 : if (DECL_P (t))
2810 : : {
2811 : 420608493 : if (args)
2812 : 151168918 : r = satisfy_declaration_constraints (t, args, info);
2813 : : else
2814 : 269439575 : r = satisfy_declaration_constraints (t, info);
2815 : : }
2816 : : else
2817 : 5322466 : r = satisfy_nondeclaration_constraints (t, args, info);
2818 : 306 : if (r == error_mark_node && info.quiet ()
2819 : 425928401 : && !(DECL_P (t) && warning_suppressed_p (t)))
2820 : : {
2821 : : /* Replay the error noisily. */
2822 : 142 : sat_info noisy (tf_warning_or_error, info.in_decl);
2823 : 142 : constraint_satisfaction_value (t, args, noisy);
2824 : 142 : if (DECL_P (t) && !args)
2825 : : /* Avoid giving these errors again. */
2826 : 0 : suppress_warning (t);
2827 : : }
2828 : 425928259 : return r;
2829 : : }
2830 : :
2831 : : /* True iff the result of satisfying T using ARGS is BOOLEAN_TRUE_NODE
2832 : : and false otherwise, even in the case of errors.
2833 : :
2834 : : Here, T can be:
2835 : : - a template declaration
2836 : : - a template specialization (in which case ARGS must be empty)
2837 : : - a concept-id (in which case ARGS must be empty)
2838 : : - a nested-requirement
2839 : : - a placeholder 'auto'
2840 : : - a requires-expression. */
2841 : :
2842 : : bool
2843 : 526371995 : constraints_satisfied_p (tree t, tree args/*= NULL_TREE */)
2844 : : {
2845 : 526371995 : if (!flag_concepts)
2846 : : return true;
2847 : :
2848 : 424315375 : sat_info quiet (tf_none, NULL_TREE);
2849 : 424315375 : return constraint_satisfaction_value (t, args, quiet) == boolean_true_node;
2850 : : }
2851 : :
2852 : : /* Evaluate a concept check of the form C<ARGS>. This is only used for the
2853 : : evaluation of template-ids as id-expressions. */
2854 : :
2855 : : tree
2856 : 1303470 : evaluate_concept_check (tree check)
2857 : : {
2858 : 1303470 : if (check == error_mark_node)
2859 : : return error_mark_node;
2860 : :
2861 : 1303470 : gcc_assert (concept_check_p (check));
2862 : :
2863 : : /* Check for satisfaction without diagnostics. */
2864 : 1303470 : sat_info quiet (tf_none, NULL_TREE);
2865 : 1303470 : return constraint_satisfaction_value (check, /*args=*/NULL_TREE, quiet);
2866 : : }
2867 : :
2868 : : /* Evaluate the requires-expression T, returning either boolean_true_node
2869 : : or boolean_false_node. This is used during folding and constexpr
2870 : : evaluation. */
2871 : :
2872 : : tree
2873 : 126 : evaluate_requires_expr (tree t)
2874 : : {
2875 : 126 : gcc_assert (TREE_CODE (t) == REQUIRES_EXPR);
2876 : 126 : sat_info quiet (tf_none, NULL_TREE);
2877 : 126 : return constraint_satisfaction_value (t, /*args=*/NULL_TREE, quiet);
2878 : : }
2879 : :
2880 : : /*---------------------------------------------------------------------------
2881 : : Semantic analysis of requires-expressions
2882 : : ---------------------------------------------------------------------------*/
2883 : :
2884 : : /* Finish a requires expression for the given PARMS (possibly
2885 : : null) and the non-empty sequence of requirements. */
2886 : :
2887 : : tree
2888 : 807883 : finish_requires_expr (location_t loc, tree parms, tree reqs)
2889 : : {
2890 : : /* Build the node. */
2891 : 807883 : tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs, NULL_TREE);
2892 : 807883 : TREE_SIDE_EFFECTS (r) = false;
2893 : 807883 : TREE_CONSTANT (r) = true;
2894 : 807883 : SET_EXPR_LOCATION (r, loc);
2895 : 807883 : return r;
2896 : : }
2897 : :
2898 : : /* Construct a requirement for the validity of EXPR. */
2899 : :
2900 : : tree
2901 : 448210 : finish_simple_requirement (location_t loc, tree expr)
2902 : : {
2903 : 448210 : tree r = build_nt (SIMPLE_REQ, expr);
2904 : 448210 : SET_EXPR_LOCATION (r, loc);
2905 : 448210 : return r;
2906 : : }
2907 : :
2908 : : /* Construct a requirement for the validity of TYPE. */
2909 : :
2910 : : tree
2911 : 203275 : finish_type_requirement (location_t loc, tree type)
2912 : : {
2913 : 203275 : tree r = build_nt (TYPE_REQ, type);
2914 : 203275 : SET_EXPR_LOCATION (r, loc);
2915 : 203275 : return r;
2916 : : }
2917 : :
2918 : : /* Construct a requirement for the validity of EXPR, along with
2919 : : its properties. If TYPE is non-null, then it specifies either
2920 : : an implicit conversion or argument deduction constraint,
2921 : : depending on whether any placeholders occur in the type name.
2922 : : NOEXCEPT_P is true iff the noexcept keyword was specified. */
2923 : :
2924 : : tree
2925 : 402121 : finish_compound_requirement (location_t loc, tree expr, tree type, bool noexcept_p)
2926 : : {
2927 : 402121 : tree req = build_nt (COMPOUND_REQ, expr, type);
2928 : 402121 : SET_EXPR_LOCATION (req, loc);
2929 : 402121 : COMPOUND_REQ_NOEXCEPT_P (req) = noexcept_p;
2930 : 402121 : return req;
2931 : : }
2932 : :
2933 : : /* Finish a nested requirement. */
2934 : :
2935 : : tree
2936 : 47786 : finish_nested_requirement (location_t loc, tree expr)
2937 : : {
2938 : : /* Build the requirement, saving the set of in-scope template
2939 : : parameters as its type. */
2940 : 47786 : tree r = build1 (NESTED_REQ, current_template_parms, expr);
2941 : 47786 : SET_EXPR_LOCATION (r, loc);
2942 : 47786 : return r;
2943 : : }
2944 : :
2945 : : /*---------------------------------------------------------------------------
2946 : : Equivalence of constraints
2947 : : ---------------------------------------------------------------------------*/
2948 : :
2949 : : /* Returns true when A and B are equivalent constraints. */
2950 : : bool
2951 : 15127835 : equivalent_constraints (tree a, tree b)
2952 : : {
2953 : 15127835 : gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2954 : 15127835 : gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2955 : 15127835 : return cp_tree_equal (a, b);
2956 : : }
2957 : :
2958 : : /* Returns true if the template declarations A and B have equivalent
2959 : : constraints. This is the case when A's constraints subsume B's and
2960 : : when B's also constrain A's. */
2961 : : bool
2962 : 138 : equivalently_constrained (tree d1, tree d2)
2963 : : {
2964 : 138 : gcc_assert (TREE_CODE (d1) == TREE_CODE (d2));
2965 : 138 : return equivalent_constraints (get_constraints (d1), get_constraints (d2));
2966 : : }
2967 : :
2968 : : /*---------------------------------------------------------------------------
2969 : : Partial ordering of constraints
2970 : : ---------------------------------------------------------------------------*/
2971 : :
2972 : : /* Returns true when the constraints in CI strictly subsume
2973 : : the associated constraints of TMPL. */
2974 : :
2975 : : bool
2976 : 191669 : strictly_subsumes (tree ci, tree tmpl)
2977 : : {
2978 : 191669 : tree n1 = get_normalized_constraints_from_info (ci, NULL_TREE);
2979 : 191669 : tree n2 = get_normalized_constraints_from_decl (tmpl);
2980 : :
2981 : 191669 : return subsumes (n1, n2) && !subsumes (n2, n1);
2982 : : }
2983 : :
2984 : : /* Returns true when the template template parameter constraints in CI
2985 : : subsume the associated constraints of the template template argument
2986 : : TMPL. */
2987 : :
2988 : : bool
2989 : 87 : ttp_subsumes (tree ci, tree tmpl)
2990 : : {
2991 : 87 : tree n1 = get_normalized_constraints_from_info (ci, tmpl);
2992 : 87 : tree n2 = get_normalized_constraints_from_decl (tmpl);
2993 : :
2994 : 87 : return subsumes (n1, n2);
2995 : : }
2996 : :
2997 : : /* Determines which of the declarations, A or B, is more constrained.
2998 : : That is, which declaration's constraints subsume but are not subsumed
2999 : : by the other's?
3000 : :
3001 : : Returns 1 if D1 is more constrained than D2, -1 if D2 is more constrained
3002 : : than D1, and 0 otherwise. */
3003 : :
3004 : : int
3005 : 1116804 : more_constrained (tree d1, tree d2)
3006 : : {
3007 : 1116804 : tree n1 = get_normalized_constraints_from_decl (d1);
3008 : 1116804 : tree n2 = get_normalized_constraints_from_decl (d2);
3009 : :
3010 : 1116804 : int winner = 0;
3011 : 1116804 : if (subsumes (n1, n2))
3012 : 1030837 : ++winner;
3013 : 1116804 : if (subsumes (n2, n1))
3014 : 766932 : --winner;
3015 : 1116804 : return winner;
3016 : : }
3017 : :
3018 : : /* Return whether D1 is at least as constrained as D2. */
3019 : :
3020 : : bool
3021 : 3575590 : at_least_as_constrained (tree d1, tree d2)
3022 : : {
3023 : 3575590 : tree n1 = get_normalized_constraints_from_decl (d1);
3024 : 3575590 : tree n2 = get_normalized_constraints_from_decl (d2);
3025 : :
3026 : 3575590 : return subsumes (n1, n2);
3027 : : }
3028 : :
3029 : : /*---------------------------------------------------------------------------
3030 : : Constraint diagnostics
3031 : : ---------------------------------------------------------------------------*/
3032 : :
3033 : : /* Returns the best location to diagnose a constraint error. */
3034 : :
3035 : : static location_t
3036 : 1226 : get_constraint_error_location (tree t)
3037 : : {
3038 : 1226 : if (location_t loc = cp_expr_location (t))
3039 : : return loc;
3040 : :
3041 : : /* If we have a specific location give it. */
3042 : 1226 : tree expr = CONSTR_EXPR (t);
3043 : 1226 : if (location_t loc = cp_expr_location (expr))
3044 : : return loc;
3045 : :
3046 : : /* If the constraint is normalized from a requires-clause, give
3047 : : the location as that of the constrained declaration. */
3048 : 76 : tree cxt = CONSTR_CONTEXT (t);
3049 : 76 : tree src = cxt ? TREE_VALUE (cxt) : NULL_TREE;
3050 : 75 : if (!src)
3051 : : /* TODO: This only happens for constrained non-template declarations. */
3052 : : ;
3053 : 75 : else if (DECL_P (src))
3054 : 60 : return DECL_SOURCE_LOCATION (src);
3055 : : /* Otherwise, give the location as the defining concept. */
3056 : 15 : else if (concept_check_p (src))
3057 : : {
3058 : 15 : tree tmpl = TREE_OPERAND (src, 0);
3059 : 15 : return DECL_SOURCE_LOCATION (tmpl);
3060 : : }
3061 : :
3062 : 1 : return input_location;
3063 : : }
3064 : :
3065 : : /* Emit a diagnostic for a failed trait. */
3066 : :
3067 : : void
3068 : 747 : diagnose_trait_expr (location_t loc, tree expr, tree args)
3069 : : {
3070 : : /* Build a "fake" version of the instantiated trait, so we can
3071 : : get the instantiated types from result. */
3072 : 747 : ++processing_template_decl;
3073 : 747 : expr = tsubst_expr (expr, args, tf_none, NULL_TREE);
3074 : 747 : --processing_template_decl;
3075 : :
3076 : 747 : tree t1 = TRAIT_EXPR_TYPE1 (expr);
3077 : 747 : tree t2 = TRAIT_EXPR_TYPE2 (expr);
3078 : :
3079 : : /* For traits intrinsically about the properties of user-defined types,
3080 : : decl_loc will point to the declaration of that type. */
3081 : 747 : location_t decl_loc = location_of (t1);
3082 : 747 : if (decl_loc == input_location)
3083 : 519 : decl_loc = loc;
3084 : :
3085 : 747 : switch (TRAIT_EXPR_KIND (expr))
3086 : : {
3087 : 4 : case CPTK_HAS_NOTHROW_ASSIGN:
3088 : 4 : inform (decl_loc, "%qT is not nothrow copy assignable", t1);
3089 : 4 : break;
3090 : 4 : case CPTK_HAS_NOTHROW_CONSTRUCTOR:
3091 : 4 : inform (decl_loc, "%qT is not nothrow default constructible", t1);
3092 : 4 : break;
3093 : 4 : case CPTK_HAS_NOTHROW_COPY:
3094 : 4 : inform (decl_loc, "%qT is not nothrow copy constructible", t1);
3095 : 4 : break;
3096 : 4 : case CPTK_HAS_TRIVIAL_ASSIGN:
3097 : 4 : inform (decl_loc, "%qT is not trivially copy assignable", t1);
3098 : 4 : break;
3099 : 4 : case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
3100 : 4 : inform (decl_loc, "%qT is not trivially default constructible", t1);
3101 : 4 : break;
3102 : 4 : case CPTK_HAS_TRIVIAL_COPY:
3103 : 4 : inform (decl_loc, "%qT is not trivially copy constructible", t1);
3104 : 4 : break;
3105 : 4 : case CPTK_HAS_TRIVIAL_DESTRUCTOR:
3106 : 4 : inform (decl_loc, "%qT is not trivially destructible", t1);
3107 : 4 : break;
3108 : 3 : case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
3109 : 3 : inform (decl_loc, "%qT does not have unique object representations", t1);
3110 : 3 : break;
3111 : 19 : case CPTK_HAS_VIRTUAL_DESTRUCTOR:
3112 : 19 : {
3113 : 19 : location_t dtor_loc = decl_loc;
3114 : 19 : if (NON_UNION_CLASS_TYPE_P (t1))
3115 : 6 : if (tree dtor = CLASSTYPE_DESTRUCTOR (t1))
3116 : 3 : dtor_loc = DECL_SOURCE_LOCATION (dtor);
3117 : 19 : inform (dtor_loc, "%qT does not have a virtual destructor", t1);
3118 : : }
3119 : 19 : break;
3120 : 4 : case CPTK_IS_ABSTRACT:
3121 : 4 : inform (decl_loc, "%qT is not an abstract class", t1);
3122 : 4 : break;
3123 : 3 : case CPTK_IS_AGGREGATE:
3124 : 3 : inform (decl_loc, "%qT is not an aggregate", t1);
3125 : 3 : break;
3126 : 0 : case CPTK_IS_ARRAY:
3127 : 0 : inform (loc, "%qT is not an array", t1);
3128 : 0 : break;
3129 : 21 : case CPTK_IS_ASSIGNABLE:
3130 : 21 : inform (loc, "%qT is not assignable from %qT, because", t1, t2);
3131 : 21 : is_xible (MODIFY_EXPR, t1, t2, /*explain=*/true);
3132 : 21 : break;
3133 : 2 : case CPTK_IS_BASE_OF:
3134 : 2 : inform (decl_loc, "%qT is not a base of %qT", t1, t2);
3135 : 2 : break;
3136 : 0 : case CPTK_IS_BOUNDED_ARRAY:
3137 : 0 : inform (loc, "%qT is not a bounded array", t1);
3138 : 0 : break;
3139 : 109 : case CPTK_IS_CLASS:
3140 : 109 : inform (decl_loc, "%qT is not a class", t1);
3141 : 109 : break;
3142 : 1 : case CPTK_IS_CONST:
3143 : 1 : inform (loc, "%qT is not a const type", t1);
3144 : 1 : break;
3145 : 152 : case CPTK_IS_CONSTRUCTIBLE:
3146 : 152 : if (!TREE_VEC_LENGTH (t2))
3147 : 84 : inform (loc, "%qT is not default constructible, because", t1);
3148 : : else
3149 : 68 : inform (loc, "%qT is not constructible from %qT, because", t1, t2);
3150 : 152 : is_xible (INIT_EXPR, t1, t2, /*explain=*/true);
3151 : 152 : break;
3152 : 11 : case CPTK_IS_CONVERTIBLE:
3153 : : /* The errors produced here all seem to mention "convertible" in the
3154 : : diagnostic, so an extra inform here appears redundant. */
3155 : 11 : is_convertible (t1, t2, /*explain=*/true);
3156 : 11 : break;
3157 : 9 : case CPTK_IS_DESTRUCTIBLE:
3158 : 9 : inform (loc, "%qT is not destructible, because", t1);
3159 : 9 : is_xible (BIT_NOT_EXPR, t1, NULL_TREE, /*explain=*/true);
3160 : 9 : break;
3161 : 4 : case CPTK_IS_EMPTY:
3162 : 4 : inform (decl_loc, "%qT is not an empty class", t1);
3163 : 4 : break;
3164 : 0 : case CPTK_IS_ENUM:
3165 : 0 : inform (decl_loc, "%qT is not an enum", t1);
3166 : 0 : break;
3167 : 4 : case CPTK_IS_FINAL:
3168 : 4 : inform (decl_loc, "%qT is not a final class", t1);
3169 : 4 : break;
3170 : 0 : case CPTK_IS_FUNCTION:
3171 : 0 : inform (loc, "%qT is not a function", t1);
3172 : 0 : break;
3173 : 22 : case CPTK_IS_INVOCABLE:
3174 : 22 : {
3175 : 22 : if (!TREE_VEC_LENGTH (t2))
3176 : 13 : inform (loc, "%qT is not invocable, because", t1);
3177 : : else
3178 : 9 : inform (loc, "%qT is not invocable by %qT, because", t1, t2);
3179 : 22 : build_invoke (t1, t2, tf_error);
3180 : : }
3181 : 22 : break;
3182 : 0 : case CPTK_IS_LAYOUT_COMPATIBLE:
3183 : 0 : inform (loc, "%qT is not layout compatible with %qT", t1, t2);
3184 : 0 : break;
3185 : 0 : case CPTK_IS_LITERAL_TYPE:
3186 : 0 : inform (decl_loc, "%qT is not a literal type", t1);
3187 : 0 : break;
3188 : 0 : case CPTK_IS_MEMBER_FUNCTION_POINTER:
3189 : 0 : inform (loc, "%qT is not a member function pointer", t1);
3190 : 0 : break;
3191 : 0 : case CPTK_IS_MEMBER_OBJECT_POINTER:
3192 : 0 : inform (loc, "%qT is not a member object pointer", t1);
3193 : 0 : break;
3194 : 0 : case CPTK_IS_MEMBER_POINTER:
3195 : 0 : inform (loc, "%qT is not a member pointer", t1);
3196 : 0 : break;
3197 : 6 : case CPTK_IS_NOTHROW_ASSIGNABLE:
3198 : 6 : inform (loc, "%qT is not nothrow assignable from %qT, because", t1, t2);
3199 : 6 : is_nothrow_xible (MODIFY_EXPR, t1, t2, /*explain=*/true);
3200 : 6 : break;
3201 : 15 : case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
3202 : 15 : if (!TREE_VEC_LENGTH (t2))
3203 : 6 : inform (loc, "%qT is not nothrow default constructible, because", t1);
3204 : : else
3205 : 9 : inform (loc, "%qT is not nothrow constructible from %qT, because",
3206 : : t1, t2);
3207 : 15 : is_nothrow_xible (INIT_EXPR, t1, t2, /*explain=*/true);
3208 : 15 : break;
3209 : 6 : case CPTK_IS_NOTHROW_CONVERTIBLE:
3210 : 6 : inform (loc, "%qT is not nothrow convertible from %qT, because", t1, t2);
3211 : 6 : is_nothrow_convertible (t1, t2, /*explain=*/true);
3212 : 6 : break;
3213 : 10 : case CPTK_IS_NOTHROW_DESTRUCTIBLE:
3214 : 10 : inform (loc, "%qT is not nothrow destructible, because", t1);
3215 : 10 : is_nothrow_xible (BIT_NOT_EXPR, t1, NULL_TREE, /*explain=*/true);
3216 : 10 : break;
3217 : 9 : case CPTK_IS_NOTHROW_INVOCABLE:
3218 : 9 : {
3219 : 9 : if (!TREE_VEC_LENGTH (t2))
3220 : 6 : inform (loc, "%qT is not nothrow invocable, because", t1);
3221 : : else
3222 : 3 : inform (loc, "%qT is not nothrow invocable by %qT, because", t1, t2);
3223 : 9 : tree call = build_invoke (t1, t2, tf_error);
3224 : 9 : if (call != error_mark_node)
3225 : 9 : explain_not_noexcept (call);
3226 : : }
3227 : : break;
3228 : 0 : case CPTK_IS_NOTHROW_RELOCATABLE:
3229 : 0 : inform (loc, "%qT is not nothrow relocatable", t1);
3230 : 0 : break;
3231 : 48 : case CPTK_IS_OBJECT:
3232 : 48 : inform (loc, "%qT is not an object type", t1);
3233 : 48 : break;
3234 : 0 : case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
3235 : 0 : inform (decl_loc, "%qT is not a pointer-interconvertible base of %qT",
3236 : : t1, t2);
3237 : 0 : break;
3238 : 4 : case CPTK_IS_POD:
3239 : 4 : inform (loc, "%qT is not a POD type", t1);
3240 : 4 : break;
3241 : 0 : case CPTK_IS_POINTER:
3242 : 0 : inform (loc, "%qT is not a pointer", t1);
3243 : 0 : break;
3244 : 4 : case CPTK_IS_POLYMORPHIC:
3245 : 4 : inform (decl_loc, "%qT is not a polymorphic type", t1);
3246 : 4 : break;
3247 : 0 : case CPTK_IS_REFERENCE:
3248 : 0 : inform (loc, "%qT is not a reference", t1);
3249 : 0 : break;
3250 : 0 : case CPTK_IS_REPLACEABLE:
3251 : 0 : inform (loc, "%qT is not replaceable", t1);
3252 : 0 : break;
3253 : 176 : case CPTK_IS_SAME:
3254 : 176 : inform (loc, "%q#T is not the same as %q#T", t1, t2);
3255 : 176 : break;
3256 : 0 : case CPTK_IS_SCOPED_ENUM:
3257 : 0 : inform (decl_loc, "%qT is not a scoped enum", t1);
3258 : 0 : break;
3259 : 4 : case CPTK_IS_STD_LAYOUT:
3260 : 4 : inform (decl_loc, "%qT is not a standard layout type", t1);
3261 : 4 : break;
3262 : 4 : case CPTK_IS_TRIVIAL:
3263 : 4 : inform (decl_loc, "%qT is not a trivial type", t1);
3264 : 4 : break;
3265 : 6 : case CPTK_IS_TRIVIALLY_ASSIGNABLE:
3266 : 6 : inform (loc, "%qT is not trivially assignable from %qT, because", t1, t2);
3267 : 6 : is_trivially_xible (MODIFY_EXPR, t1, t2, /*explain=*/true);
3268 : 6 : break;
3269 : 15 : case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
3270 : 15 : if (!TREE_VEC_LENGTH (t2))
3271 : 6 : inform (loc, "%qT is not trivially default constructible, because", t1);
3272 : : else
3273 : 9 : inform (loc, "%qT is not trivially constructible from %qT, because",
3274 : : t1, t2);
3275 : 15 : is_trivially_xible (INIT_EXPR, t1, t2, /*explain=*/true);
3276 : 15 : break;
3277 : 11 : case CPTK_IS_TRIVIALLY_COPYABLE:
3278 : 11 : inform (decl_loc, "%qT is not trivially copyable", t1);
3279 : 11 : break;
3280 : 6 : case CPTK_IS_TRIVIALLY_DESTRUCTIBLE:
3281 : 6 : inform (loc, "%qT is not trivially destructible, because", t1);
3282 : 6 : is_trivially_xible (BIT_NOT_EXPR, t1, NULL_TREE, /*explain=*/true);
3283 : 6 : break;
3284 : 0 : case CPTK_IS_TRIVIALLY_RELOCATABLE:
3285 : 0 : inform (loc, "%qT is not trivially relocatable", t1);
3286 : 0 : break;
3287 : 0 : case CPTK_IS_UNBOUNDED_ARRAY:
3288 : 0 : inform (loc, "%qT is not an unbounded array", t1);
3289 : 0 : break;
3290 : 4 : case CPTK_IS_UNION:
3291 : 4 : inform (decl_loc, "%qT is not a union", t1);
3292 : 4 : break;
3293 : 6 : case CPTK_IS_VIRTUAL_BASE_OF:
3294 : 6 : inform (decl_loc, "%qT is not a virtual base of %qT", t1, t2);
3295 : 6 : if (CLASS_TYPE_P (t2))
3296 : 3 : inform (location_of (t2), "%qT declared here", t2);
3297 : : break;
3298 : 0 : case CPTK_IS_VOLATILE:
3299 : 0 : inform (loc, "%qT is not a volatile type", t1);
3300 : 0 : break;
3301 : 0 : case CPTK_RANK:
3302 : 0 : inform (loc, "%qT cannot yield a rank", t1);
3303 : 0 : break;
3304 : 0 : case CPTK_TYPE_ORDER:
3305 : 0 : inform (loc, "%qT and %qT cannot be ordered", t1, t2);
3306 : 0 : break;
3307 : 0 : case CPTK_STRUCTURED_BINDING_SIZE:
3308 : 0 : inform (loc, "%qT is not destructurable", t1);
3309 : 0 : break;
3310 : 0 : case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
3311 : 0 : inform (loc, "%qT is not a reference that binds to a temporary "
3312 : : "object of type %qT (direct-initialization)", t1, t2);
3313 : 0 : break;
3314 : 0 : case CPTK_REF_CONVERTS_FROM_TEMPORARY:
3315 : 0 : inform (loc, "%qT is not a reference that binds to a temporary "
3316 : : "object of type %qT (copy-initialization)", t1, t2);
3317 : 0 : break;
3318 : 21 : case CPTK_IS_DEDUCIBLE:
3319 : 21 : inform (loc, "%qD is not deducible from %qT", t1, t2);
3320 : 21 : break;
3321 : : #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
3322 : : case CPTK_##CODE:
3323 : : #include "cp-trait.def"
3324 : : #undef DEFTRAIT_TYPE
3325 : : /* Type-yielding traits aren't expressions. */
3326 : 0 : gcc_unreachable ();
3327 : : /* We deliberately omit the default case so that when adding a new
3328 : : trait we'll get reminded (by way of a warning) to handle it here. */
3329 : : }
3330 : 747 : }
3331 : :
3332 : : /* Attempt to detect if this is a standard type trait, defined in terms
3333 : : of a compiler builtin (above). If so, this will allow us to provide
3334 : : more helpful diagnostics. */
3335 : :
3336 : : bool
3337 : 1856 : maybe_diagnose_standard_trait (location_t loc, tree expr)
3338 : : {
3339 : 1856 : gcc_assert (TREE_CODE (expr) != TRAIT_EXPR);
3340 : 1856 : expr = tree_strip_nop_conversions (expr);
3341 : :
3342 : : /* TODO: in some cases it would be possible to provide more helpful
3343 : : diagnostics for negations of traits, e.g. '!is_same_v<T1, T2>'. */
3344 : :
3345 : 1856 : tree args = NULL_TREE;
3346 : 1856 : if (VAR_P (expr) && DECL_LANG_SPECIFIC (expr) && DECL_USE_TEMPLATE (expr))
3347 : : {
3348 : 842 : tree tinfo = DECL_TEMPLATE_INFO (expr);
3349 : 842 : if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) && TI_PARTIAL_INFO (tinfo))
3350 : 12 : tinfo = TI_PARTIAL_INFO (tinfo);
3351 : 830 : else if (DECL_TEMPLATE_SPECIALIZATION (expr))
3352 : : /* In an explicit specialisation we no longer know what the original
3353 : : initializer looked like. */
3354 : : tinfo = NULL_TREE;
3355 : :
3356 : 833 : if (tinfo)
3357 : : {
3358 : 833 : expr = DECL_INITIAL (DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo)));
3359 : 833 : args = TI_ARGS (tinfo);
3360 : : }
3361 : : }
3362 : :
3363 : 1856 : if (expr && TREE_CODE (expr) == TRAIT_EXPR)
3364 : : {
3365 : 377 : diagnose_trait_expr (loc, expr, args);
3366 : 377 : return true;
3367 : : }
3368 : :
3369 : : return false;
3370 : : }
3371 : :
3372 : : /* Diagnose a substitution failure in the atomic constraint T using ARGS. */
3373 : :
3374 : : static void
3375 : 1226 : diagnose_atomic_constraint (tree t, tree args, tree substituted, sat_info info)
3376 : : {
3377 : : /* If the constraint is already ill-formed, we've previously diagnosed
3378 : : the reason. We should still say why the constraints aren't satisfied. */
3379 : 1226 : if (t == error_mark_node)
3380 : : {
3381 : 0 : location_t loc;
3382 : 0 : if (info.in_decl)
3383 : 0 : loc = DECL_SOURCE_LOCATION (info.in_decl);
3384 : : else
3385 : 0 : loc = input_location;
3386 : 0 : inform (loc, "invalid constraints");
3387 : 0 : return;
3388 : : }
3389 : :
3390 : 1226 : location_t loc = get_constraint_error_location (t);
3391 : 1226 : iloc_sentinel loc_s (loc);
3392 : :
3393 : : /* Generate better diagnostics for certain kinds of expressions. */
3394 : 1226 : tree expr = ATOMIC_CONSTR_EXPR (t);
3395 : 1226 : STRIP_ANY_LOCATION_WRAPPER (expr);
3396 : :
3397 : 1226 : if (TREE_CODE (expr) == REQUIRES_EXPR)
3398 : : {
3399 : 300 : gcc_checking_assert (info.diagnose_unsatisfaction_p ());
3400 : : /* Clear in_decl before replaying the substitution to avoid emitting
3401 : : seemingly unhelpful "in declaration ..." notes that follow some
3402 : : substitution failure error messages. */
3403 : 300 : info.in_decl = NULL_TREE;
3404 : 300 : tsubst_requires_expr (expr, args, info);
3405 : : }
3406 : 926 : else if (!same_type_p (TREE_TYPE (substituted), boolean_type_node))
3407 : 26 : error_at (loc, "constraint %qE has type %qT, not %<bool%>",
3408 : 26 : t, TREE_TYPE (substituted));
3409 : : else
3410 : : {
3411 : 900 : inform (loc, "the expression %qE evaluated to %<false%>", t);
3412 : 900 : if (TREE_CODE (expr) == TRAIT_EXPR)
3413 : 370 : diagnose_trait_expr (loc, expr, args);
3414 : : else
3415 : 530 : maybe_diagnose_standard_trait (loc, substituted);
3416 : : }
3417 : 1226 : }
3418 : :
3419 : : GTY(()) tree current_failed_constraint;
3420 : :
3421 : 327482590 : diagnosing_failed_constraint::
3422 : 327482590 : diagnosing_failed_constraint (tree t, tree args, bool diag)
3423 : 327482590 : : diagnosing_error (diag)
3424 : : {
3425 : 327482590 : if (diagnosing_error)
3426 : : {
3427 : 5768 : current_failed_constraint
3428 : 5768 : = tree_cons (args, t, current_failed_constraint);
3429 : 5768 : ++current_constraint_diagnosis_depth;
3430 : : }
3431 : 327482590 : }
3432 : :
3433 : 327479890 : diagnosing_failed_constraint::
3434 : : ~diagnosing_failed_constraint ()
3435 : : {
3436 : 327479890 : if (diagnosing_error)
3437 : : {
3438 : 5768 : --current_constraint_diagnosis_depth;
3439 : 5768 : if (current_failed_constraint)
3440 : 4396 : current_failed_constraint = TREE_CHAIN (current_failed_constraint);
3441 : : }
3442 : :
3443 : 327479890 : }
3444 : :
3445 : : /* Whether we are allowed to replay an error that underlies a constraint failure
3446 : : at the current diagnosis depth. */
3447 : :
3448 : : bool
3449 : 430 : diagnosing_failed_constraint::replay_errors_p ()
3450 : : {
3451 : 430 : if (current_constraint_diagnosis_depth >= concepts_diagnostics_max_depth)
3452 : : {
3453 : 376 : concepts_diagnostics_max_depth_exceeded_p = true;
3454 : 376 : return false;
3455 : : }
3456 : : else
3457 : : return true;
3458 : : }
3459 : :
3460 : : /* Emit diagnostics detailing the failure ARGS to satisfy the constraints
3461 : : of T. Here, T and ARGS are as in constraints_satisfied_p. */
3462 : :
3463 : : void
3464 : 1321 : diagnose_constraints (location_t loc, tree t, tree args)
3465 : : {
3466 : 1321 : inform (loc, "constraints not satisfied");
3467 : :
3468 : 1321 : if (concepts_diagnostics_max_depth == 0)
3469 : 0 : return;
3470 : :
3471 : 1321 : auto_diagnostic_nesting_level sentinel;
3472 : :
3473 : : /* Replay satisfaction, but diagnose unsatisfaction. */
3474 : 1321 : sat_info noisy (tf_warning_or_error, NULL_TREE, /*diag_unsat=*/true);
3475 : 1321 : constraint_satisfaction_value (t, args, noisy);
3476 : :
3477 : 1321 : static bool suggested_p;
3478 : 1321 : if (concepts_diagnostics_max_depth_exceeded_p
3479 : 367 : && current_constraint_diagnosis_depth == 0
3480 : 364 : && !suggested_p)
3481 : : {
3482 : 137 : inform (UNKNOWN_LOCATION,
3483 : : "set %qs to at least %d for more detail",
3484 : : "-fconcepts-diagnostics-depth=",
3485 : 137 : concepts_diagnostics_max_depth + 1);
3486 : 137 : suggested_p = true;
3487 : : }
3488 : 1321 : }
3489 : :
3490 : : #include "gt-cp-constraint.h"
|