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 : 50838904 : processing_constraint_expression_sentinel::
56 : : processing_constraint_expression_sentinel ()
57 : : {
58 : 50838904 : ++scope_chain->x_processing_constraint;
59 : 50838904 : }
60 : :
61 : 50838904 : processing_constraint_expression_sentinel::
62 : : ~processing_constraint_expression_sentinel ()
63 : : {
64 : 50838904 : --scope_chain->x_processing_constraint;
65 : 50838904 : }
66 : :
67 : : bool
68 : 2562905 : processing_constraint_expression_p ()
69 : : {
70 : 2562905 : 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 : 328331728 : subst_info (tsubst_flags_t cmp, tree in)
82 : 328331728 : : complain (cmp), in_decl (in)
83 : : { }
84 : :
85 : : /* True if we should not diagnose errors. */
86 : 906139304 : bool quiet () const
87 : : {
88 : 906139304 : return !(complain & tf_warning_or_error);
89 : : }
90 : :
91 : : /* True if we should diagnose errors. */
92 : 695167659 : bool noisy () const
93 : : {
94 : 700889808 : 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 : 306579046 : sat_info (tsubst_flags_t cmp, tree in, bool diag_unsat = false)
132 : 306579046 : : subst_info (cmp, in), diagnose_unsatisfaction (diag_unsat)
133 : : {
134 : 306579046 : if (diagnose_unsatisfaction_p ())
135 : 0 : gcc_checking_assert (noisy ());
136 : 2859887 : }
137 : :
138 : : /* True if we should diagnose the cause of satisfaction failure.
139 : : Implies noisy(). */
140 : : bool
141 : 309165075 : diagnose_unsatisfaction_p () const
142 : : {
143 : 2859887 : 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 : 8923052 : known_non_bool_p (tree t)
156 : : {
157 : 8923052 : return (t && !WILDCARD_TYPE_P (t) && TREE_CODE (t) != BOOLEAN_TYPE);
158 : : }
159 : :
160 : : static bool
161 : 8923052 : check_constraint_atom (cp_expr expr)
162 : : {
163 : 8923052 : 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 : 2560518 : check_constraint_operands (location_t, cp_expr lhs, cp_expr rhs)
175 : : {
176 : 2560518 : 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 : 2560524 : finish_constraint_binary_op (location_t loc,
183 : : tree_code code,
184 : : cp_expr lhs,
185 : : cp_expr rhs)
186 : : {
187 : 2560524 : gcc_assert (processing_constraint_expression_p ());
188 : 2560524 : if (lhs == error_mark_node || rhs == error_mark_node)
189 : 6 : return error_mark_node;
190 : 2560518 : if (!check_constraint_operands (loc, lhs, rhs))
191 : 0 : return error_mark_node;
192 : 2560518 : cp_expr expr
193 : 2560518 : = build_min_nt_loc (loc, code, lhs.get_value (), rhs.get_value ());
194 : 2560518 : expr.set_range (lhs.get_start (), rhs.get_finish ());
195 : 2560518 : return expr;
196 : : }
197 : :
198 : : cp_expr
199 : 196117 : finish_constraint_or_expr (location_t loc, cp_expr lhs, cp_expr rhs)
200 : : {
201 : 196117 : return finish_constraint_binary_op (loc, TRUTH_ORIF_EXPR, lhs, rhs);
202 : : }
203 : :
204 : : cp_expr
205 : 2364407 : finish_constraint_and_expr (location_t loc, cp_expr lhs, cp_expr rhs)
206 : : {
207 : 2364407 : return finish_constraint_binary_op (loc, TRUTH_ANDIF_EXPR, lhs, rhs);
208 : : }
209 : :
210 : : cp_expr
211 : 3802040 : finish_constraint_primary_expr (cp_expr expr)
212 : : {
213 : 3802040 : if (expr == error_mark_node)
214 : 24 : return error_mark_node;
215 : 3802016 : if (!check_constraint_atom (expr))
216 : 10 : return cp_expr (error_mark_node, expr.get_location ());
217 : 3802006 : return expr;
218 : : }
219 : :
220 : : /* Combine two constraint-expressions with a logical-and. */
221 : :
222 : : tree
223 : 45723376 : combine_constraint_expressions (tree lhs, tree rhs)
224 : : {
225 : 45723376 : processing_constraint_expression_sentinel pce;
226 : 45723376 : if (!lhs)
227 : : return rhs;
228 : 6451035 : 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 : 1407104 : return finish_constraint_and_expr (UNKNOWN_LOCATION, lhs, rhs);
233 : 45723376 : }
234 : :
235 : : /* Extract the TEMPLATE_DECL from a concept check. */
236 : :
237 : : tree
238 : 6410146 : get_concept_check_template (tree t)
239 : : {
240 : 6410146 : gcc_assert (concept_check_p (t));
241 : 6410146 : 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 : 5837121 : get_concept_definition (tree decl)
252 : : {
253 : 5837121 : gcc_assert (TREE_CODE (decl) == CONCEPT_DECL);
254 : 5837121 : 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 : 6327941 : map_arguments (tree parms, tree args)
298 : : {
299 : 17451374 : for (tree p = parms; p; p = TREE_CHAIN (p))
300 : 11123433 : if (args)
301 : : {
302 : 9748482 : int level;
303 : 9748482 : int index;
304 : 9748482 : template_parm_level_and_index (TREE_VALUE (p), &level, &index);
305 : 9748482 : TREE_PURPOSE (p) = TMPL_ARG (args, level, index);
306 : : }
307 : : else
308 : 1374951 : TREE_PURPOSE (p) = template_parm_to_arg (p);
309 : :
310 : 6327941 : 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 : 6327941 : build_parameter_mapping (tree expr, tree args, tree ctx_parms)
318 : : {
319 : 6327941 : tree parms = find_template_parameters (expr, ctx_parms);
320 : 6327941 : tree map = map_arguments (parms, args);
321 : 6327941 : 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 : 20044968 : parameter_mapping_equivalent_p (tree t1, tree t2)
329 : : {
330 : 20044968 : tree map1 = ATOMIC_CONSTR_MAP (t1);
331 : 20044968 : tree map2 = ATOMIC_CONSTR_MAP (t2);
332 : 44611270 : while (map1 && map2)
333 : : {
334 : 31524120 : gcc_checking_assert (TREE_VALUE (map1) == TREE_VALUE (map2));
335 : 31524120 : tree arg1 = TREE_PURPOSE (map1);
336 : 31524120 : tree arg2 = TREE_PURPOSE (map2);
337 : 31524120 : if (!template_args_equal (arg1, arg2))
338 : : return false;
339 : 24566302 : map1 = TREE_CHAIN (map1);
340 : 24566302 : map2 = TREE_CHAIN (map2);
341 : : }
342 : 13087150 : 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 : 2228814 : explicit norm_info (bool diag)
351 : 2228814 : : norm_info (NULL_TREE, diag)
352 : : {}
353 : :
354 : : /* Construct a top-level context for DECL. */
355 : :
356 : 3126231 : norm_info (tree in_decl, bool diag)
357 : 3126231 : : subst_info (tf_warning_or_error, in_decl),
358 : 3126231 : generate_diagnostics (diag)
359 : : {
360 : 897417 : if (in_decl)
361 : : {
362 : 738971 : initial_parms = DECL_TEMPLATE_PARMS (in_decl);
363 : 738971 : if (generate_diagnostics)
364 : 1190 : context = build_tree_list (NULL_TREE, in_decl);
365 : : }
366 : : else
367 : 158446 : initial_parms = current_template_parms;
368 : 897417 : }
369 : :
370 : 5769976 : void update_context (tree expr, tree args)
371 : : {
372 : 5769976 : if (generate_diagnostics)
373 : : {
374 : 5961 : tree map = build_parameter_mapping (expr, args, ctx_parms ());
375 : 5961 : context = tree_cons (map, expr, context);
376 : : }
377 : 5769976 : in_decl = get_concept_check_template (expr);
378 : 5769976 : }
379 : :
380 : : /* Returns the template parameters that are in scope for the current
381 : : normalization context. */
382 : :
383 : 6327941 : tree ctx_parms ()
384 : : {
385 : 6327941 : if (in_decl)
386 : 6217072 : return DECL_TEMPLATE_PARMS (in_decl);
387 : : else
388 : 110869 : 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 : 6560639 : normalize_logical_operation (tree t, tree args, tree_code c, norm_info info)
416 : : {
417 : 6560639 : tree t0 = normalize_expression (TREE_OPERAND (t, 0), args, info);
418 : 6560639 : tree t1 = normalize_expression (TREE_OPERAND (t, 1), args, info);
419 : :
420 : : /* Build a new info object for the constraint. */
421 : 6560639 : tree ci = (info.generate_diagnostics
422 : 6560639 : ? build_tree_list (t, info.context) : NULL_TREE);
423 : :
424 : 6560639 : 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 : 56861336 : static hashval_t hash (norm_entry *e)
443 : : {
444 : 56861336 : ++comparing_specializations;
445 : 56861336 : hashval_t val = iterative_hash_template_arg (e->tmpl, 0);
446 : 56861336 : val = iterative_hash_template_arg (e->args, val);
447 : 56861336 : --comparing_specializations;
448 : 56861336 : return val;
449 : : }
450 : :
451 : 65881985 : static bool equal (norm_entry *e1, norm_entry *e2)
452 : : {
453 : 65881985 : ++comparing_specializations;
454 : 65881985 : bool eq = e1->tmpl == e2->tmpl
455 : 65881985 : && template_args_equal (e1->args, e2->args);
456 : 65881985 : --comparing_specializations;
457 : 65881985 : 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 : 7305456 : normalize_concept_check (tree check, tree args, norm_info info)
468 : : {
469 : 7305456 : gcc_assert (concept_check_p (check));
470 : 7305456 : tree tmpl = TREE_OPERAND (check, 0);
471 : 7305456 : tree targs = TREE_OPERAND (check, 1);
472 : :
473 : : /* Substitute through the arguments of the concept check. */
474 : 7305456 : if (args)
475 : 5423004 : targs = tsubst_template_args (targs, args, info.complain, info.in_decl);
476 : 7305456 : if (targs == error_mark_node)
477 : : return error_mark_node;
478 : 7305456 : if (template_args_equal (targs, generic_targs_for (tmpl)))
479 : : /* Canonicalize generic arguments as NULL_TREE, as an optimization. */
480 : 949231 : targs = NULL_TREE;
481 : :
482 : : /* Build the substitution for the concept definition. */
483 : 7305456 : tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl));
484 : 7305456 : if (targs && args)
485 : : /* As an optimization, coerce the arguments only if necessary
486 : : (i.e. if they were substituted). */
487 : 5322348 : targs = coerce_template_parms (parms, targs, tmpl, tf_none);
488 : 7305456 : if (targs == error_mark_node)
489 : : return error_mark_node;
490 : :
491 : 7305456 : if (!norm_cache)
492 : 18444 : norm_cache = hash_table<norm_hasher>::create_ggc (31);
493 : 7305456 : norm_entry *entry = nullptr;
494 : 7305456 : if (!info.generate_diagnostics)
495 : : {
496 : : /* Cache the normal form of the substituted concept-id (when not
497 : : diagnosing). */
498 : 7299495 : norm_entry elt = {tmpl, targs, NULL_TREE};
499 : 7299495 : norm_entry **slot = norm_cache->find_slot (&elt, INSERT);
500 : 7299495 : if (*slot)
501 : 1535480 : return (*slot)->norm;
502 : 5764015 : entry = ggc_alloc<norm_entry> ();
503 : 5764015 : *entry = elt;
504 : 5764015 : *slot = entry;
505 : : }
506 : :
507 : 5769976 : tree def = get_concept_definition (DECL_TEMPLATE_RESULT (tmpl));
508 : 5769976 : info.update_context (check, args);
509 : 5769976 : tree norm = normalize_expression (def, targs, info);
510 : 5769976 : if (entry)
511 : 5764015 : 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 : 47619018 : static hashval_t hash (tree t)
520 : : {
521 : 47619018 : ++comparing_specializations;
522 : 47619018 : hashval_t val = hash_atomic_constraint (t);
523 : 47619018 : --comparing_specializations;
524 : 47619018 : return val;
525 : : }
526 : :
527 : 43607976 : static bool equal (tree t1, tree t2)
528 : : {
529 : 43607976 : ++comparing_specializations;
530 : 43607976 : bool eq = atomic_constraints_identical_p (t1, t2);
531 : 43607976 : --comparing_specializations;
532 : 43607976 : 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 : 13627436 : normalize_atom (tree t, tree args, norm_info info)
544 : : {
545 : : /* Concept checks are not atomic. */
546 : 13627436 : if (concept_check_p (t))
547 : 7305456 : return normalize_concept_check (t, args, info);
548 : :
549 : : /* Build the parameter mapping for the atom. */
550 : 6321980 : tree map = build_parameter_mapping (t, args, info.ctx_parms ());
551 : :
552 : : /* Build a new info object for the atom. */
553 : 6321980 : tree ci = build_tree_list (t, info.context);
554 : :
555 : 6321980 : 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 : 12533224 : if (info.in_decl && concept_definition_p (info.in_decl))
563 : 5726943 : ATOMIC_CONSTR_EXPR_FROM_CONCEPT_P (atom) = true;
564 : :
565 : 6321980 : 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 : 6315068 : if (!atom_cache)
570 : 22411 : atom_cache = hash_table<atom_hasher>::create_ggc (31);
571 : 6315068 : tree *slot = atom_cache->find_slot (atom, INSERT);
572 : 6315068 : 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 : 6144516 : if (map)
581 : : {
582 : 6143937 : tree targets = make_tree_vec (list_length (map));
583 : 6143937 : int i = 0;
584 : 17072224 : for (tree node = map; node; node = TREE_CHAIN (node))
585 : : {
586 : 10928287 : tree target = TREE_PURPOSE (node);
587 : 10928287 : TREE_VEC_ELT (targets, i++) = target;
588 : : }
589 : 6143937 : tree target_parms = find_template_parameters (targets,
590 : : info.initial_parms);
591 : 6143937 : TREE_TYPE (map) = target_parms;
592 : : }
593 : :
594 : 6144516 : *slot = atom;
595 : : }
596 : : return atom;
597 : : }
598 : :
599 : : /* Returns the normal form of an expression. */
600 : :
601 : : static tree
602 : 20188190 : normalize_expression (tree t, tree args, norm_info info)
603 : : {
604 : 20188190 : if (!t)
605 : : return NULL_TREE;
606 : :
607 : 20188190 : if (t == error_mark_node)
608 : : return error_mark_node;
609 : :
610 : 20188075 : switch (TREE_CODE (t))
611 : : {
612 : 6276407 : case TRUTH_ANDIF_EXPR:
613 : 6276407 : return normalize_logical_operation (t, args, CONJ_CONSTR, info);
614 : 284232 : case TRUTH_ORIF_EXPR:
615 : 284232 : return normalize_logical_operation (t, args, DISJ_CONSTR, info);
616 : 13627436 : default:
617 : 13627436 : 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 : 1296936 : get_normalized_constraints (tree t, norm_info info)
627 : : {
628 : 1296936 : auto_timevar time (TV_CONSTRAINT_NORM);
629 : 1296936 : return normalize_expression (t, NULL_TREE, info);
630 : 1296936 : }
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 : 830276 : get_normalized_constraints_from_info (tree ci, tree in_decl, bool diag = false)
638 : : {
639 : 830276 : if (ci == NULL_TREE)
640 : : return NULL_TREE;
641 : :
642 : : /* Substitution errors during normalization are fatal. */
643 : 830272 : ++processing_template_decl;
644 : 830272 : norm_info info (in_decl, diag);
645 : 1660544 : tree t = get_normalized_constraints (CI_ASSOCIATED_CONSTRAINTS (ci), info);
646 : 830272 : --processing_template_decl;
647 : :
648 : 830272 : return t;
649 : : }
650 : :
651 : : /* Returns the normalized constraints for the declaration D. */
652 : :
653 : : static tree
654 : 113996032 : get_normalized_constraints_from_decl (tree d, bool diag = false)
655 : : {
656 : 113996032 : tree tmpl;
657 : 113996032 : tree decl;
658 : :
659 : : /* For inherited constructors, consider the original declaration;
660 : : it has the correct template information attached. */
661 : 113996032 : d = strip_inheriting_ctors (d);
662 : :
663 : 113996032 : 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 : 362923 : tree lambda = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (d));
669 : 362923 : lambda = most_general_lambda (lambda);
670 : 362923 : d = lambda_function (lambda);
671 : : }
672 : :
673 : 113996032 : if (TREE_CODE (d) == TEMPLATE_DECL)
674 : : {
675 : 84092229 : tmpl = d;
676 : 84092229 : decl = DECL_TEMPLATE_RESULT (tmpl);
677 : : }
678 : : else
679 : : {
680 : 29903803 : if (tree ti = DECL_TEMPLATE_INFO (d))
681 : 20332379 : tmpl = TI_TEMPLATE (ti);
682 : : else
683 : : tmpl = NULL_TREE;
684 : 20332379 : 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 : 104424608 : if (tmpl && DECL_LANG_SPECIFIC (tmpl)
705 : 124756972 : && (!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 : 6344876 : || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
715 : 3172438 : < TMPL_ARGS_DEPTH (DECL_TI_ARGS (tmpl)))))
716 : 101252223 : tmpl = most_general_template (tmpl);
717 : :
718 : 113996032 : d = tmpl ? tmpl : decl;
719 : :
720 : : /* If we're not diagnosing errors, use cached constraints, if any. */
721 : 113996032 : if (!diag)
722 : 227885663 : if (tree *p = hash_map_safe_get (normalized_map, d))
723 : 94018558 : return *p;
724 : :
725 : 19977474 : tree norm = NULL_TREE;
726 : 19977474 : if (tree ci = get_constraints (d))
727 : : {
728 : 671739 : push_access_scope_guard pas (decl);
729 : 671739 : norm = get_normalized_constraints_from_info (ci, tmpl, diag);
730 : 671739 : }
731 : :
732 : 19977474 : if (!diag)
733 : 19976502 : hash_map_safe_put<hm_ggc> (normalized_map, d, norm);
734 : :
735 : 19977474 : return norm;
736 : : }
737 : :
738 : : /* Returns the normal form of TMPL's definition. */
739 : :
740 : : static tree
741 : 610141 : normalize_concept_definition (tree tmpl, bool diag)
742 : : {
743 : 610141 : if (!norm_cache)
744 : 648 : norm_cache = hash_table<norm_hasher>::create_ggc (31);
745 : 610141 : norm_entry entry = {tmpl, NULL_TREE, NULL_TREE};
746 : :
747 : 610141 : if (!diag)
748 : 609923 : if (norm_entry *found = norm_cache->find (&entry))
749 : 542996 : return found->norm;
750 : :
751 : 67145 : gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
752 : 67145 : tree def = get_concept_definition (DECL_TEMPLATE_RESULT (tmpl));
753 : 67145 : ++processing_template_decl;
754 : 67145 : norm_info info (tmpl, diag);
755 : 67145 : tree norm = get_normalized_constraints (def, info);
756 : 67145 : --processing_template_decl;
757 : :
758 : 67145 : if (!diag)
759 : : {
760 : 66927 : norm_entry **slot = norm_cache->find_slot (&entry, INSERT);
761 : 66927 : entry.norm = norm;
762 : 66927 : *slot = ggc_alloc<norm_entry> ();
763 : 66927 : **slot = entry;
764 : : }
765 : :
766 : : return norm;
767 : : }
768 : :
769 : : /* Normalize an EXPR as a constraint. */
770 : :
771 : : static tree
772 : 2228814 : normalize_constraint_expression (tree expr, norm_info info)
773 : : {
774 : 2228814 : if (!expr || expr == error_mark_node)
775 : : return expr;
776 : :
777 : 2228814 : if (!info.generate_diagnostics)
778 : 4457232 : if (tree *p = hash_map_safe_get (normalized_map, expr))
779 : 1829295 : return *p;
780 : :
781 : 399519 : ++processing_template_decl;
782 : 399519 : tree norm = get_normalized_constraints (expr, info);
783 : 399519 : --processing_template_decl;
784 : :
785 : 399519 : if (!info.generate_diagnostics)
786 : 399368 : 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 : 88114210 : atomic_constraints_identical_p (tree t1, tree t2)
797 : : {
798 : 88114210 : gcc_assert (TREE_CODE (t1) == ATOMIC_CONSTR);
799 : 88114210 : gcc_assert (TREE_CODE (t2) == ATOMIC_CONSTR);
800 : :
801 : 88114210 : if (ATOMIC_CONSTR_EXPR (t1) != ATOMIC_CONSTR_EXPR (t2))
802 : : return false;
803 : :
804 : 20044968 : 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 : 4396304 : constraints_equivalent_p (tree t1, tree t2)
815 : : {
816 : 4396304 : gcc_assert (CONSTR_P (t1));
817 : 4396304 : gcc_assert (CONSTR_P (t2));
818 : :
819 : 4396304 : if (TREE_CODE (t1) != TREE_CODE (t2))
820 : : return false;
821 : :
822 : 4345227 : switch (TREE_CODE (t1))
823 : : {
824 : 2194133 : case CONJ_CONSTR:
825 : 2194133 : case DISJ_CONSTR:
826 : 2194133 : if (!constraints_equivalent_p (TREE_OPERAND (t1, 0),
827 : 2194133 : TREE_OPERAND (t2, 0)))
828 : : return false;
829 : 2125047 : if (!constraints_equivalent_p (TREE_OPERAND (t1, 1),
830 : 2125047 : TREE_OPERAND (t2, 1)))
831 : : return false;
832 : : break;
833 : 2151094 : case ATOMIC_CONSTR:
834 : 2151094 : 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 : 400737918 : hash_atomic_constraint (tree t)
847 : : {
848 : 400737918 : gcc_assert (TREE_CODE (t) == ATOMIC_CONSTR);
849 : :
850 : : /* Hash the identity of the expression. */
851 : 400737918 : hashval_t val = htab_hash_pointer (ATOMIC_CONSTR_EXPR (t));
852 : :
853 : : /* Hash the targets of the parameter map. */
854 : 400737918 : tree p = ATOMIC_CONSTR_MAP (t);
855 : 1042272431 : while (p)
856 : : {
857 : 641534513 : val = iterative_hash_template_arg (TREE_PURPOSE (p), val);
858 : 641534513 : p = TREE_CHAIN (p);
859 : : }
860 : :
861 : 400737918 : return val;
862 : : }
863 : :
864 : : namespace inchash
865 : : {
866 : :
867 : : static void
868 : 23853768 : add_constraint (tree t, hash& h)
869 : : {
870 : 47490254 : h.add_int (TREE_CODE (t));
871 : 47490254 : switch (TREE_CODE (t))
872 : : {
873 : 23636486 : case CONJ_CONSTR:
874 : 23636486 : case DISJ_CONSTR:
875 : 23636486 : add_constraint (TREE_OPERAND (t, 0), h);
876 : 23636486 : add_constraint (TREE_OPERAND (t, 1), h);
877 : 23636486 : break;
878 : 23853768 : case ATOMIC_CONSTR:
879 : 23853768 : h.merge_hash (hash_atomic_constraint (t));
880 : 23853768 : break;
881 : 0 : default:
882 : 0 : gcc_unreachable ();
883 : : }
884 : 23853768 : }
885 : :
886 : : }
887 : :
888 : : /* Computes a hash code for the constraint T. */
889 : :
890 : : hashval_t
891 : 217282 : iterative_hash_constraint (tree t, hashval_t val)
892 : : {
893 : 217282 : gcc_assert (CONSTR_P (t));
894 : 217282 : inchash::hash h (val);
895 : 217282 : inchash::add_constraint (t, h);
896 : 217282 : 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 : 32794845 : current_template_constraints (void)
908 : : {
909 : 32794845 : if (!current_template_parms)
910 : : return NULL_TREE;
911 : 32794842 : tree tmpl_constr = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
912 : 32794842 : 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 : 16457941 : associate_classtype_constraints (tree type)
921 : : {
922 : 16457941 : 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 : 16457246 : if (!current_template_parms)
927 : : return type;
928 : :
929 : 13761890 : if (CLASSTYPE_IS_TEMPLATE (type) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
930 : : {
931 : 13471344 : tree decl = TYPE_STUB_DECL (type);
932 : 13471344 : 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 : 13471344 : if (tree orig_ci = get_constraints (decl))
939 : : {
940 : 234418 : if (int extra_levels = (TMPL_PARMS_DEPTH (current_template_parms)
941 : 1460251 : - 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 : 234418 : 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 : 234418 : return type;
963 : : }
964 : 13236926 : 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 : 5448132 : build_constraint_info ()
973 : : {
974 : 5448132 : 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 : 79688427 : build_constraints (tree tr, tree dr)
987 : : {
988 : 79688427 : if (!tr && !dr)
989 : : return NULL_TREE;
990 : :
991 : 5448132 : tree_constraint_info* ci = build_constraint_info ();
992 : 5448132 : ci->template_reqs = tr;
993 : 5448132 : ci->declarator_reqs = dr;
994 : 5448132 : ci->associated_constr = combine_constraint_expressions (tr, dr);
995 : :
996 : 5448132 : return (tree)ci;
997 : : }
998 : :
999 : : /* Add constraint RHS to the end of CONSTRAINT_INFO ci. */
1000 : :
1001 : : tree
1002 : 60 : append_constraint (tree ci, tree rhs)
1003 : : {
1004 : 60 : tree tr = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
1005 : 20 : tree dr = ci ? CI_DECLARATOR_REQS (ci) : NULL_TREE;
1006 : 60 : dr = combine_constraint_expressions (dr, rhs);
1007 : 60 : if (ci)
1008 : : {
1009 : 10 : CI_DECLARATOR_REQS (ci) = dr;
1010 : 10 : tree ac = combine_constraint_expressions (tr, dr);
1011 : 20 : CI_ASSOCIATED_CONSTRAINTS (ci) = ac;
1012 : : }
1013 : : else
1014 : 50 : ci = build_constraints (tr, dr);
1015 : 60 : 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 : 449929446 : get_constraints (const_tree t)
1027 : : {
1028 : 449929446 : if (!flag_concepts)
1029 : : return NULL_TREE;
1030 : 204521927 : if (!decl_constraints)
1031 : : return NULL_TREE;
1032 : :
1033 : 201087929 : gcc_assert (DECL_P (t));
1034 : 201087929 : if (TREE_CODE (t) == TEMPLATE_DECL)
1035 : 49218237 : t = DECL_TEMPLATE_RESULT (t);
1036 : 201087929 : tree* found = decl_constraints->get (CONST_CAST_TREE (t));
1037 : 201087929 : if (found)
1038 : 20062643 : 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 : 78345896 : set_constraints (tree t, tree ci)
1050 : : {
1051 : 78345896 : if (!ci)
1052 : : return;
1053 : 9964185 : gcc_assert (t && flag_concepts);
1054 : 9964185 : if (TREE_CODE (t) == TEMPLATE_DECL)
1055 : 88664 : t = DECL_TEMPLATE_RESULT (t);
1056 : 9964185 : bool found = hash_map_safe_put<hm_ggc> (decl_constraints, t, ci);
1057 : 9964185 : gcc_assert (!found);
1058 : : }
1059 : :
1060 : : /* Remove the associated constraints of the declaration T. */
1061 : :
1062 : : void
1063 : 3185161 : remove_constraints (tree t)
1064 : : {
1065 : 3185161 : gcc_checking_assert (DECL_P (t));
1066 : 3185161 : if (TREE_CODE (t) == TEMPLATE_DECL)
1067 : 78 : t = DECL_TEMPLATE_RESULT (t);
1068 : :
1069 : 3185161 : if (decl_constraints)
1070 : 3055566 : decl_constraints->remove (t);
1071 : 3185161 : }
1072 : :
1073 : : /* If DECL is a friend, substitute into REQS to produce requirements suitable
1074 : : for declaration matching. */
1075 : :
1076 : : tree
1077 : 17862958 : maybe_substitute_reqs_for (tree reqs, const_tree decl)
1078 : : {
1079 : 17862958 : if (reqs == NULL_TREE)
1080 : : return NULL_TREE;
1081 : :
1082 : 1114471 : decl = STRIP_TEMPLATE (decl);
1083 : 1114471 : if (DECL_UNIQUE_FRIEND_P (decl) && DECL_TEMPLATE_INFO (decl))
1084 : : {
1085 : 67440 : tree tmpl = DECL_TI_TEMPLATE (decl);
1086 : 67440 : tree outer_args = outer_template_args (decl);
1087 : 67440 : processing_template_decl_sentinel s;
1088 : 67440 : if (PRIMARY_TEMPLATE_P (tmpl)
1089 : 67440 : || uses_template_parms (outer_args))
1090 : 67440 : ++processing_template_decl;
1091 : 67440 : reqs = tsubst_constraint (reqs, outer_args,
1092 : : tf_warning_or_error, NULL_TREE);
1093 : 67440 : }
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 : 124260300 : get_trailing_function_requirements (tree t)
1102 : : {
1103 : 124260300 : tree ci = get_constraints (t);
1104 : 124260300 : if (!ci)
1105 : : return NULL_TREE;
1106 : 7702066 : 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 : 10817263 : build_concept_check_arguments (tree arg, tree rest)
1114 : : {
1115 : 10817263 : gcc_assert (!rest || TREE_CODE (rest) == TREE_VEC);
1116 : 10817263 : tree args;
1117 : 10817263 : if (arg)
1118 : : {
1119 : 8103757 : int n = rest ? TREE_VEC_LENGTH (rest) : 0;
1120 : 5500340 : args = make_tree_vec (n + 1);
1121 : 5500340 : TREE_VEC_ELT (args, 0) = arg;
1122 : 5500340 : if (rest)
1123 : 5666215 : for (int i = 0; i < n; ++i)
1124 : 3062798 : TREE_VEC_ELT (args, i + 1) = TREE_VEC_ELT (rest, i);
1125 : 2603417 : int def = rest ? GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (rest) : 0;
1126 : 5500340 : SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, def + 1);
1127 : : }
1128 : : else
1129 : : args = rest;
1130 : 10817263 : return args;
1131 : : }
1132 : :
1133 : : /* Construct an expression that checks TMPL using ARGS. */
1134 : :
1135 : : tree
1136 : 5316923 : build_concept_check (tree tmpl, tree args, tsubst_flags_t complain)
1137 : : {
1138 : 5316923 : 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 : 10817263 : build_concept_check (tree tmpl, tree arg, tree rest, tsubst_flags_t complain)
1145 : : {
1146 : 10817263 : if (TREE_DEPRECATED (DECL_TEMPLATE_RESULT (tmpl)))
1147 : 9 : warn_deprecated_use (DECL_TEMPLATE_RESULT (tmpl), NULL_TREE);
1148 : :
1149 : 10817263 : tree parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
1150 : 10817263 : tree args = build_concept_check_arguments (arg, rest);
1151 : 10817263 : args = coerce_template_parms (parms, args, tmpl, complain);
1152 : 10817263 : if (args == error_mark_node)
1153 : : return error_mark_node;
1154 : 10218796 : 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 : 4471880 : 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 : 4471880 : build_concept_id (tree expr)
1170 : : {
1171 : 4471880 : gcc_assert (TREE_CODE (expr) == TEMPLATE_ID_EXPR);
1172 : 4471880 : tree id = build_concept_id (TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
1173 : 4471880 : protected_set_expr_location (id, cp_expr_location (expr));
1174 : 4471880 : 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 : 3058332 : build_type_constraint (tree decl, tree args, tsubst_flags_t complain)
1185 : : {
1186 : 3058332 : tree proto = template_parm_to_arg (concept_prototype_parameter (decl));
1187 : 3058332 : ++processing_template_decl;
1188 : 3058332 : tree check = build_concept_check (decl, proto, args, complain);
1189 : 3058332 : --processing_template_decl;
1190 : 3058332 : 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 : 2098917 : build_constrained_parameter (tree cnc, tree proto, tree args)
1202 : : {
1203 : 2098917 : tree name = DECL_NAME (cnc);
1204 : 2098917 : tree type = TREE_TYPE (proto);
1205 : 2098917 : tree decl = build_decl (input_location, TYPE_DECL, name, type);
1206 : 2098917 : CONSTRAINED_PARM_PROTOTYPE (decl) = proto;
1207 : 2098917 : CONSTRAINED_PARM_CONCEPT (decl) = cnc;
1208 : 2098917 : CONSTRAINED_PARM_EXTRA_ARGS (decl) = args;
1209 : 2098917 : 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 : 145677606 : finish_shorthand_constraint (tree decl, tree constr)
1222 : : {
1223 : : /* No requirements means no constraints. */
1224 : 145677606 : if (!constr)
1225 : : return NULL_TREE;
1226 : :
1227 : 2098893 : if (error_operand_p (constr))
1228 : : return NULL_TREE;
1229 : :
1230 : 2098893 : tree proto = CONSTRAINED_PARM_PROTOTYPE (constr);
1231 : 2098893 : tree con = CONSTRAINED_PARM_CONCEPT (constr);
1232 : 2098893 : tree args = CONSTRAINED_PARM_EXTRA_ARGS (constr);
1233 : :
1234 : 2098893 : bool variadic_concept_p = template_parameter_pack_p (proto);
1235 : 2098893 : bool declared_pack_p = template_parameter_pack_p (decl);
1236 : 2098893 : 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 : 2098893 : tree arg = template_parm_to_arg (decl);
1241 : 2098893 : if (apply_to_each_p && declared_pack_p)
1242 : 14887 : arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
1243 : :
1244 : : /* Build the concept constraint-expression. */
1245 : 2098893 : tree tmpl = DECL_TI_TEMPLATE (con);
1246 : 2098893 : 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 : 2098893 : if (apply_to_each_p && declared_pack_p)
1252 : 14887 : 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 : 21103075 : get_shorthand_constraints (tree parms)
1264 : : {
1265 : 21103075 : tree result = NULL_TREE;
1266 : 21103075 : parms = INNERMOST_TEMPLATE_PARMS (parms);
1267 : 59889940 : for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
1268 : : {
1269 : 38786865 : tree parm = TREE_VEC_ELT (parms, i);
1270 : 38786865 : tree constr = TEMPLATE_PARM_CONSTRAINTS (parm);
1271 : 38786865 : result = combine_constraint_expressions (result, constr);
1272 : : }
1273 : 21103075 : 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 : 445895527 : equivalent_placeholder_constraints (tree c1, tree c2)
1281 : : {
1282 : 445895527 : if (c1 && TREE_CODE (c1) == TEMPLATE_TYPE_PARM)
1283 : : /* A constrained auto. */
1284 : 445895527 : c1 = PLACEHOLDER_TYPE_CONSTRAINTS (c1);
1285 : 445895527 : if (c2 && TREE_CODE (c2) == TEMPLATE_TYPE_PARM)
1286 : 445895527 : c2 = PLACEHOLDER_TYPE_CONSTRAINTS (c2);
1287 : :
1288 : 445895527 : if (c1 == c2)
1289 : : return true;
1290 : 1653328 : if (!c1 || !c2)
1291 : : return false;
1292 : 1386959 : 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 : 1386959 : gcc_assert (concept_check_p (c1) && concept_check_p (c2));
1299 : 1386959 : tree t1 = TREE_OPERAND (c1, 0);
1300 : 1386959 : tree a1 = TREE_OPERAND (c1, 1);
1301 : 1386959 : tree t2 = TREE_OPERAND (c2, 0);
1302 : 1386959 : tree a2 = TREE_OPERAND (c2, 1);
1303 : :
1304 : 1386959 : if (t1 != t2)
1305 : : return false;
1306 : :
1307 : 675985 : int len1 = TREE_VEC_LENGTH (a1);
1308 : 675985 : int len2 = TREE_VEC_LENGTH (a2);
1309 : 675985 : 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 : 964725 : for (int i = 1; i < len1; ++i)
1315 : 395311 : if (!template_args_equal (TREE_VEC_ELT (a1, i),
1316 : 395311 : 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 : 76250959 : iterative_hash_placeholder_constraint (tree c, hashval_t val)
1325 : : {
1326 : 76250959 : gcc_assert (concept_check_p (c));
1327 : 76250959 : tree t = TREE_OPERAND (c, 0);
1328 : 76250959 : tree a = TREE_OPERAND (c, 1);
1329 : :
1330 : : /* Like hash_tmpl_and_args, but skip the first argument. */
1331 : 76250959 : val = iterative_hash_object (DECL_UID (t), val);
1332 : :
1333 : 116719759 : for (int i = TREE_VEC_LENGTH (a)-1; i > 0; --i)
1334 : 40468800 : val = iterative_hash_template_arg (TREE_VEC_ELT (a, i), val);
1335 : :
1336 : 76250959 : return val;
1337 : : }
1338 : :
1339 : : /* Substitute through the expression of a simple requirement or
1340 : : compound requirement. */
1341 : :
1342 : : static tree
1343 : 3364985 : tsubst_valid_expression_requirement (tree t, tree args, sat_info info)
1344 : : {
1345 : 3364985 : tsubst_flags_t quiet = info.complain & ~tf_warning_or_error;
1346 : 3364985 : tree r = tsubst_expr (t, args, quiet, info.in_decl);
1347 : 3362285 : if (r != error_mark_node
1348 : 3362285 : && (processing_template_decl
1349 : 3181481 : || convert_to_void (r, ICV_STATEMENT, quiet) != error_mark_node))
1350 : 3182787 : return r;
1351 : :
1352 : 179498 : if (info.diagnose_unsatisfaction_p ())
1353 : : {
1354 : 230 : location_t loc = cp_expr_loc_or_input_loc (t);
1355 : 230 : 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 : 222 : inform (loc, "the required expression %qE is invalid", t);
1366 : : }
1367 : 179268 : 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 : 179498 : return error_mark_node;
1374 : : }
1375 : :
1376 : :
1377 : : /* Substitute through the simple requirement. */
1378 : :
1379 : : static tree
1380 : 1200769 : tsubst_simple_requirement (tree t, tree args, sat_info info)
1381 : : {
1382 : 1200769 : tree t0 = TREE_OPERAND (t, 0);
1383 : 1200769 : tree expr = tsubst_valid_expression_requirement (t0, args, info);
1384 : 1198069 : if (expr == error_mark_node)
1385 : : return error_mark_node;
1386 : 1048543 : if (processing_template_decl)
1387 : 202 : return finish_simple_requirement (EXPR_LOCATION (t), expr);
1388 : 1048341 : 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 : 3009198 : tsubst_type_requirement_1 (tree t, tree args, sat_info info, location_t loc)
1396 : : {
1397 : 3009198 : tsubst_flags_t quiet = info.complain & ~tf_warning_or_error;
1398 : 3009198 : tree r = tsubst (t, args, quiet, info.in_decl);
1399 : 3009198 : if (r != error_mark_node)
1400 : : return r;
1401 : :
1402 : 108440 : 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 : 108419 : else if (info.noisy ())
1414 : 0 : tsubst (t, args, info.complain, info.in_decl);
1415 : :
1416 : 108440 : return error_mark_node;
1417 : : }
1418 : :
1419 : :
1420 : : /* Substitute through the type requirement. */
1421 : :
1422 : : static tree
1423 : 874972 : tsubst_type_requirement (tree t, tree args, sat_info info)
1424 : : {
1425 : 874972 : tree t0 = TREE_OPERAND (t, 0);
1426 : 874972 : tree type = tsubst_type_requirement_1 (t0, args, info, EXPR_LOCATION (t));
1427 : 874972 : if (type == error_mark_node)
1428 : : return error_mark_node;
1429 : 766532 : if (processing_template_decl)
1430 : 8 : return finish_type_requirement (EXPR_LOCATION (t), type);
1431 : 766524 : return boolean_true_node;
1432 : : }
1433 : :
1434 : : /* True if TYPE can be deduced from EXPR. */
1435 : :
1436 : : static bool
1437 : 2071144 : 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 : 2071144 : expr = force_paren_expr_uneval (expr);
1443 : :
1444 : 2071144 : tree deduced_type = do_auto_deduction (type, expr, placeholder,
1445 : : info.complain, adc_requirement,
1446 : : /*outer_targs=*/args);
1447 : :
1448 : 2071144 : return deduced_type != error_mark_node;
1449 : : }
1450 : :
1451 : : /* True if EXPR can not be converted to TYPE. */
1452 : :
1453 : : static bool
1454 : 40 : expression_convertible_p (tree expr, tree type, subst_info info)
1455 : : {
1456 : 40 : tree conv =
1457 : 40 : perform_direct_initialization_if_possible (type, expr, false,
1458 : : info.complain);
1459 : 40 : if (conv == error_mark_node)
1460 : : return false;
1461 : 8 : 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 : 2164216 : tsubst_compound_requirement (tree t, tree args, sat_info info)
1478 : : {
1479 : 2164216 : tree t0 = TREE_OPERAND (t, 0);
1480 : 2164216 : tree t1 = TREE_OPERAND (t, 1);
1481 : 2164216 : tree expr = tsubst_valid_expression_requirement (t0, args, info);
1482 : 2164216 : if (expr == error_mark_node)
1483 : : return error_mark_node;
1484 : :
1485 : 2134244 : location_t loc = cp_expr_loc_or_input_loc (expr);
1486 : :
1487 : 2134244 : subst_info quiet (info.complain & ~tf_warning_or_error, info.in_decl);
1488 : :
1489 : : /* Check the noexcept condition. */
1490 : 2134244 : bool noexcept_p = COMPOUND_REQ_NOEXCEPT_P (t);
1491 : 61969 : if (noexcept_p && !processing_template_decl
1492 : 2196210 : && !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 : 2134226 : tree type = tsubst_type_requirement_1 (t1, args, info, EXPR_LOCATION (t));
1502 : 2134226 : if (type == error_mark_node)
1503 : : return error_mark_node;
1504 : :
1505 : : /* Check expression against the result type. */
1506 : 2134226 : if (type && !processing_template_decl)
1507 : : {
1508 : 2071166 : if (tree placeholder = type_uses_auto (type))
1509 : : {
1510 : 2071132 : if (!type_deducible_p (expr, type, placeholder, args, quiet))
1511 : : {
1512 : 1063 : 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 : 1063 : return error_mark_node;
1527 : : }
1528 : : }
1529 : 34 : else if (!expression_convertible_p (expr, type, quiet))
1530 : : {
1531 : 28 : if (info.diagnose_unsatisfaction_p ())
1532 : : {
1533 : 6 : if (diagnosing_failed_constraint::replay_errors_p ())
1534 : : {
1535 : 6 : inform (loc, "cannot convert %qE to %qT because", t0, type);
1536 : : /* Further explain the reason for the error. */
1537 : 6 : expression_convertible_p (expr, type, info);
1538 : : }
1539 : : else
1540 : 0 : inform (loc, "cannot convert %qE to %qT", t0, type);
1541 : : }
1542 : 28 : return error_mark_node;
1543 : : }
1544 : : }
1545 : :
1546 : 2133135 : if (processing_template_decl)
1547 : 1114 : return finish_compound_requirement (EXPR_LOCATION (t),
1548 : 1114 : expr, type, noexcept_p);
1549 : 2132021 : return boolean_true_node;
1550 : : }
1551 : :
1552 : : /* Substitute through the nested requirement. */
1553 : :
1554 : : static tree
1555 : 156990 : tsubst_nested_requirement (tree t, tree args, sat_info info)
1556 : : {
1557 : 156990 : 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 : 156990 : sat_info quiet (info.complain & ~tf_warning_or_error, info.in_decl);
1567 : 156990 : tree result = constraint_satisfaction_value (t, args, quiet);
1568 : 156990 : if (result == boolean_true_node)
1569 : : return boolean_true_node;
1570 : :
1571 : 461 : if (result == boolean_false_node
1572 : 461 : && info.diagnose_unsatisfaction_p ())
1573 : : {
1574 : 58 : tree expr = TREE_OPERAND (t, 0);
1575 : 58 : location_t loc = cp_expr_location (t);
1576 : 58 : if (diagnosing_failed_constraint::replay_errors_p ())
1577 : : {
1578 : : /* Replay the substitution error. */
1579 : 9 : inform (loc, "nested requirement %qE is not satisfied, because", expr);
1580 : 9 : constraint_satisfaction_value (t, args, info);
1581 : : }
1582 : : else
1583 : 49 : inform (loc, "nested requirement %qE is not satisfied", expr);
1584 : : }
1585 : :
1586 : 461 : return error_mark_node;
1587 : : }
1588 : :
1589 : : /* Substitute ARGS into the requirement T. */
1590 : :
1591 : : static tree
1592 : 4396947 : tsubst_requirement (tree t, tree args, sat_info info)
1593 : : {
1594 : 4396947 : iloc_sentinel loc_s (cp_expr_location (t));
1595 : 4396947 : switch (TREE_CODE (t))
1596 : : {
1597 : 1200769 : case SIMPLE_REQ:
1598 : 1200769 : return tsubst_simple_requirement (t, args, info);
1599 : 874972 : case TYPE_REQ:
1600 : 874972 : return tsubst_type_requirement (t, args, info);
1601 : 2164216 : case COMPOUND_REQ:
1602 : 2164216 : return tsubst_compound_requirement (t, args, info);
1603 : 156990 : case NESTED_REQ:
1604 : 156990 : return tsubst_nested_requirement (t, args, info);
1605 : 0 : default:
1606 : 0 : break;
1607 : : }
1608 : 0 : gcc_unreachable ();
1609 : 4394247 : }
1610 : :
1611 : : static tree
1612 : 1447769 : declare_constraint_vars (tree parms, tree vars)
1613 : : {
1614 : 1447769 : tree s = vars;
1615 : 3738173 : for (tree t = parms; t; t = DECL_CHAIN (t))
1616 : : {
1617 : 2290404 : if (DECL_PACK_P (t))
1618 : : {
1619 : 690 : tree pack = extract_fnparm_pack (t, &s);
1620 : 690 : register_local_specialization (pack, t);
1621 : : }
1622 : : else
1623 : : {
1624 : 2289714 : register_local_specialization (s, t);
1625 : 2289714 : s = DECL_CHAIN (s);
1626 : : }
1627 : : }
1628 : 1447769 : 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 : 1447819 : check_constraint_variables (tree t, tree args, subst_info info)
1637 : : {
1638 : 1447819 : tree types = NULL_TREE;
1639 : 1447819 : tree p = t;
1640 : 3738279 : while (p && !VOID_TYPE_P (p))
1641 : : {
1642 : 2290460 : types = tree_cons (NULL_TREE, TREE_TYPE (p), types);
1643 : 2290460 : p = TREE_CHAIN (p);
1644 : : }
1645 : 1447819 : types = chainon (nreverse (types), void_list_node);
1646 : 1447819 : 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 : 1447819 : tsubst_constraint_variables (tree t, tree args, subst_info info)
1659 : : {
1660 : : /* Perform a trial substitution to check for type errors. */
1661 : 1447819 : tree parms = check_constraint_variables (t, args, info);
1662 : 1447819 : 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 : 1447769 : int saved_unevaluated_operand = cp_unevaluated_operand;
1668 : 1447769 : cp_unevaluated_operand = 0;
1669 : 1447769 : tree vars = tsubst (t, args, info.complain, info.in_decl);
1670 : 1447769 : cp_unevaluated_operand = saved_unevaluated_operand;
1671 : 1447769 : if (vars == error_mark_node)
1672 : : return error_mark_node;
1673 : 1447769 : 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 : 2703313 : tsubst_requires_expr (tree t, tree args, sat_info info)
1695 : : {
1696 : 2703313 : local_specialization_stack stack (lss_copy);
1697 : :
1698 : : /* We need to check access during the substitution. */
1699 : 2703313 : deferring_access_check_sentinel acs (dk_no_deferred);
1700 : :
1701 : : /* A requires-expression is an unevaluated context. */
1702 : 2703313 : cp_unevaluated u;
1703 : :
1704 : 2703313 : args = add_extra_args (REQUIRES_EXPR_EXTRA_ARGS (t), args,
1705 : : info.complain, info.in_decl);
1706 : 2703313 : if (processing_template_decl
1707 : 2703313 : && !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 : 1287 : t = copy_node (t);
1719 : 1287 : REQUIRES_EXPR_EXTRA_ARGS (t) = NULL_TREE;
1720 : 1287 : REQUIRES_EXPR_EXTRA_ARGS (t) = build_extra_args (t, args, info.complain);
1721 : 1287 : return t;
1722 : : }
1723 : :
1724 : 2702026 : tree parms = REQUIRES_EXPR_PARMS (t);
1725 : 2702026 : if (parms)
1726 : : {
1727 : 1447819 : parms = tsubst_constraint_variables (parms, args, info);
1728 : 1447819 : if (parms == error_mark_node)
1729 : 50 : return boolean_false_node;
1730 : : }
1731 : :
1732 : 2701976 : tree result = boolean_true_node;
1733 : 2701976 : if (processing_template_decl)
1734 : 1094 : result = NULL_TREE;
1735 : 6807068 : for (tree reqs = REQUIRES_EXPR_REQS (t); reqs; reqs = TREE_CHAIN (reqs))
1736 : : {
1737 : 4396947 : tree req = TREE_VALUE (reqs);
1738 : 4396947 : req = tsubst_requirement (req, args, info);
1739 : 4394247 : if (req == error_mark_node)
1740 : : {
1741 : 289508 : result = boolean_false_node;
1742 : 289508 : if (info.diagnose_unsatisfaction_p ())
1743 : : /* Keep going so that we diagnose all failed requirements. */;
1744 : : else
1745 : : break;
1746 : : }
1747 : 4104739 : else if (processing_template_decl)
1748 : 1324 : result = tree_cons (NULL_TREE, req, result);
1749 : : }
1750 : 2699276 : if (processing_template_decl && result != boolean_false_node)
1751 : 1094 : result = finish_requires_expr (EXPR_LOCATION (t), parms, nreverse (result));
1752 : : return result;
1753 : 2700613 : }
1754 : :
1755 : : /* Public wrapper for the above. */
1756 : :
1757 : : tree
1758 : 2702897 : tsubst_requires_expr (tree t, tree args,
1759 : : tsubst_flags_t complain, tree in_decl)
1760 : : {
1761 : 2702897 : sat_info info (complain, in_decl);
1762 : 2702897 : 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 : 576075 : tsubst_constraint_info (tree t, tree args,
1770 : : tsubst_flags_t complain, tree in_decl)
1771 : : {
1772 : 576075 : if (!t || t == error_mark_node || !check_constraint_info (t))
1773 : : return NULL_TREE;
1774 : :
1775 : 86193 : tree tr = tsubst_constraint (CI_TEMPLATE_REQS (t), args, complain, in_decl);
1776 : 172386 : tree dr = tsubst_constraint (CI_DECLARATOR_REQS (t), args, complain, in_decl);
1777 : 86193 : 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 : 16492214 : tsubst_parameter_mapping (tree map, tree args, subst_info info)
1786 : : {
1787 : 16492214 : if (!map)
1788 : : return NULL_TREE;
1789 : :
1790 : 16491608 : tsubst_flags_t complain = info.complain;
1791 : 16491608 : tree in_decl = info.in_decl;
1792 : :
1793 : 16491608 : tree result = NULL_TREE;
1794 : 45391533 : for (tree p = map; p; p = TREE_CHAIN (p))
1795 : : {
1796 : 28902043 : if (p == error_mark_node)
1797 : : return error_mark_node;
1798 : 28902043 : tree parm = TREE_VALUE (p);
1799 : 28902043 : tree arg = TREE_PURPOSE (p);
1800 : 28902043 : tree new_arg;
1801 : 28902043 : if (ARGUMENT_PACK_P (arg))
1802 : 1131382 : new_arg = tsubst_argument_pack (arg, args, complain, in_decl);
1803 : : else
1804 : : {
1805 : 27770661 : new_arg = tsubst_template_arg (arg, args, complain, in_decl);
1806 : 27770661 : if (TYPE_P (new_arg))
1807 : 27710409 : new_arg = canonicalize_type_argument (new_arg, complain);
1808 : : }
1809 : 28902043 : if (TREE_CODE (new_arg) == TYPE_ARGUMENT_PACK)
1810 : : {
1811 : 1131021 : tree pack_args = ARGUMENT_PACK_ARGS (new_arg);
1812 : 2282025 : for (tree& pack_arg : tree_vec_range (pack_args))
1813 : 1151004 : if (TYPE_P (pack_arg))
1814 : 1151004 : pack_arg = canonicalize_type_argument (pack_arg, complain);
1815 : : }
1816 : 28902043 : if (new_arg == error_mark_node)
1817 : : return error_mark_node;
1818 : :
1819 : 28899925 : result = tree_cons (new_arg, parm, result);
1820 : : }
1821 : 16489490 : 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_for_satisfaction. 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 : : /* Called whenever a type completion (or return type deduction) failure occurs
1849 : : that definitely affects the meaning of the program, by e.g. inducing
1850 : : substitution failure. */
1851 : :
1852 : : void
1853 : 4029 : note_failed_type_completion_for_satisfaction (tree t)
1854 : : {
1855 : 4029 : if (satisfying_constraint)
1856 : : {
1857 : 183 : gcc_checking_assert ((TYPE_P (t) && !COMPLETE_TYPE_P (t))
1858 : : || (DECL_P (t) && undeduced_auto_decl (t)));
1859 : 183 : vec_safe_push (failed_type_completions, t);
1860 : : }
1861 : 4029 : }
1862 : :
1863 : : /* Returns true if the range [BEGIN, END) of elements within the
1864 : : failed_type_completions vector contains a complete type (or a
1865 : : declaration with a non-placeholder return type). */
1866 : :
1867 : : static bool
1868 : 200811540 : some_type_complete_p (int begin, int end)
1869 : : {
1870 : 200812757 : for (int i = begin; i < end; i++)
1871 : : {
1872 : 1262 : tree t = (*failed_type_completions)[i];
1873 : 1262 : if (TYPE_P (t) && COMPLETE_TYPE_P (t))
1874 : : return true;
1875 : 1235 : if (DECL_P (t) && !undeduced_auto_decl (t))
1876 : : return true;
1877 : : }
1878 : : return false;
1879 : : }
1880 : :
1881 : : /* Hash functions and data types for satisfaction cache entries. */
1882 : :
1883 : : struct GTY((for_user)) sat_entry
1884 : : {
1885 : : /* The relevant ATOMIC_CONSTR. */
1886 : : tree atom;
1887 : :
1888 : : /* The relevant template arguments. */
1889 : : tree args;
1890 : :
1891 : : /* The result of satisfaction of ATOM+ARGS.
1892 : : This is either boolean_true_node, boolean_false_node or error_mark_node,
1893 : : where error_mark_node indicates ill-formed satisfaction.
1894 : : It's set to NULL_TREE while computing satisfaction of ATOM+ARGS for
1895 : : the first time. */
1896 : : tree result;
1897 : :
1898 : : /* The value of input_location when satisfaction of ATOM+ARGS was first
1899 : : performed. */
1900 : : location_t location;
1901 : :
1902 : : /* The range of elements appended to the failed_type_completions vector
1903 : : during computation of this satisfaction result, encoded as a begin/end
1904 : : pair of offsets. */
1905 : : int ftc_begin, ftc_end;
1906 : :
1907 : : /* True if we want to diagnose the above instability when it's detected.
1908 : : We don't always want to do so, in order to avoid emitting duplicate
1909 : : diagnostics in some cases. */
1910 : : bool diagnose_instability;
1911 : :
1912 : : /* True if we're in the middle of computing this satisfaction result.
1913 : : Used during both quiet and noisy satisfaction to detect self-recursive
1914 : : satisfaction. */
1915 : : bool evaluating;
1916 : : };
1917 : :
1918 : : struct sat_hasher : ggc_ptr_hash<sat_entry>
1919 : : {
1920 : 1331376992 : static hashval_t hash (sat_entry *e)
1921 : : {
1922 : 1331376992 : auto cso = make_temp_override (comparing_specializations);
1923 : 1331376992 : ++comparing_specializations;
1924 : :
1925 : 1331376992 : if (ATOMIC_CONSTR_MAP_INSTANTIATED_P (e->atom))
1926 : : {
1927 : : /* Atoms with instantiated mappings are built during satisfaction.
1928 : : They live only inside the sat_cache, and we build one to query
1929 : : the cache with each time we instantiate a mapping. */
1930 : 329265132 : gcc_assert (!e->args);
1931 : 329265132 : return hash_atomic_constraint (e->atom);
1932 : : }
1933 : :
1934 : : /* Atoms with uninstantiated mappings are built during normalization.
1935 : : Since normalize_atom caches the atoms it returns, we can assume
1936 : : pointer-based identity for fast hashing and comparison. Even if this
1937 : : assumption is violated, that's okay, we'll just get a cache miss. */
1938 : 1002111860 : hashval_t value = htab_hash_pointer (e->atom);
1939 : :
1940 : 1002111860 : if (tree map = ATOMIC_CONSTR_MAP (e->atom))
1941 : : /* Only the parameters that are used in the targets of the mapping
1942 : : affect the satisfaction value of the atom. So we consider only
1943 : : the arguments for these parameters, and ignore the rest. */
1944 : 1002105693 : for (tree target_parms = TREE_TYPE (map);
1945 : 2242618181 : target_parms;
1946 : 1240512488 : target_parms = TREE_CHAIN (target_parms))
1947 : : {
1948 : 1240512488 : int level, index;
1949 : 1240512488 : tree parm = TREE_VALUE (target_parms);
1950 : 1240512488 : template_parm_level_and_index (parm, &level, &index);
1951 : 1240512488 : tree arg = TMPL_ARG (e->args, level, index);
1952 : 1240512488 : value = iterative_hash_template_arg (arg, value);
1953 : : }
1954 : : return value;
1955 : 1331376992 : }
1956 : :
1957 : 1449052321 : static bool equal (sat_entry *e1, sat_entry *e2)
1958 : : {
1959 : 1449052321 : auto cso = make_temp_override (comparing_specializations);
1960 : 1449052321 : ++comparing_specializations;
1961 : :
1962 : 1449052321 : if (ATOMIC_CONSTR_MAP_INSTANTIATED_P (e1->atom)
1963 : 1449052321 : != ATOMIC_CONSTR_MAP_INSTANTIATED_P (e2->atom))
1964 : : return false;
1965 : :
1966 : : /* See sat_hasher::hash. */
1967 : 1055613835 : if (ATOMIC_CONSTR_MAP_INSTANTIATED_P (e1->atom))
1968 : : {
1969 : 42355140 : gcc_assert (!e1->args && !e2->args);
1970 : 42355140 : return atomic_constraints_identical_p (e1->atom, e2->atom);
1971 : : }
1972 : :
1973 : 1013258695 : if (e1->atom != e2->atom)
1974 : : return false;
1975 : :
1976 : 169616548 : if (tree map = ATOMIC_CONSTR_MAP (e1->atom))
1977 : 169614112 : for (tree target_parms = TREE_TYPE (map);
1978 : 349699928 : target_parms;
1979 : 180085816 : target_parms = TREE_CHAIN (target_parms))
1980 : : {
1981 : 181865192 : int level, index;
1982 : 181865192 : tree parm = TREE_VALUE (target_parms);
1983 : 181865192 : template_parm_level_and_index (parm, &level, &index);
1984 : 181865192 : tree arg1 = TMPL_ARG (e1->args, level, index);
1985 : 181865192 : tree arg2 = TMPL_ARG (e2->args, level, index);
1986 : 181865192 : if (!template_args_equal (arg1, arg2))
1987 : 1779376 : return false;
1988 : : }
1989 : : return true;
1990 : 1449052321 : }
1991 : : };
1992 : :
1993 : : /* Cache the result of satisfy_atom. */
1994 : : static GTY((deletable)) hash_table<sat_hasher> *sat_cache;
1995 : :
1996 : : /* Cache the result of satisfy_declaration_constraints. */
1997 : : static GTY((deletable)) hash_map<tree, tree> *decl_satisfied_cache;
1998 : :
1999 : : /* A tool used by satisfy_atom to help manage satisfaction caching and to
2000 : : diagnose "unstable" satisfaction values. We insert into the cache only
2001 : : when performing satisfaction quietly. */
2002 : :
2003 : : struct satisfaction_cache
2004 : : {
2005 : : satisfaction_cache (tree, tree, sat_info);
2006 : : tree get ();
2007 : : tree save (tree);
2008 : :
2009 : : sat_entry *entry;
2010 : : sat_info info;
2011 : : int ftc_begin;
2012 : : };
2013 : :
2014 : : /* Constructor for the satisfaction_cache class. We're performing satisfaction
2015 : : of ATOM+ARGS according to INFO. */
2016 : :
2017 : 200811582 : satisfaction_cache
2018 : 200811582 : ::satisfaction_cache (tree atom, tree args, sat_info info)
2019 : 200811582 : : entry(nullptr), info(info), ftc_begin(-1)
2020 : : {
2021 : 200811582 : if (!sat_cache)
2022 : 18764 : sat_cache = hash_table<sat_hasher>::create_ggc (31);
2023 : :
2024 : : /* When noisy, we query the satisfaction cache in order to diagnose
2025 : : "unstable" satisfaction values. */
2026 : 200811582 : if (info.noisy ())
2027 : : {
2028 : : /* When noisy, constraints have been re-normalized, and that breaks the
2029 : : pointer-based identity assumption of sat_cache (for atoms with
2030 : : uninstantiated mappings). So undo this re-normalization by looking in
2031 : : the atom_cache for the corresponding atom that was used during quiet
2032 : : satisfaction. */
2033 : 11023 : if (!ATOMIC_CONSTR_MAP_INSTANTIATED_P (atom))
2034 : : {
2035 : 5524 : if (tree found = atom_cache->find (atom))
2036 : 5524 : atom = found;
2037 : : else
2038 : : /* The lookup should always succeed, but if it fails then let's
2039 : : just leave 'entry' empty, effectively disabling the cache. */
2040 : 0 : return;
2041 : : }
2042 : : }
2043 : :
2044 : : /* Look up or create the corresponding satisfaction entry. */
2045 : 200811582 : sat_entry elt;
2046 : 200811582 : elt.atom = atom;
2047 : 200811582 : elt.args = args;
2048 : 200811582 : sat_entry **slot = sat_cache->find_slot (&elt, INSERT);
2049 : 200811582 : if (*slot)
2050 : 178602460 : entry = *slot;
2051 : 22209122 : else if (info.quiet ())
2052 : : {
2053 : 22209116 : entry = ggc_alloc<sat_entry> ();
2054 : 22209116 : entry->atom = atom;
2055 : 22209116 : entry->args = args;
2056 : 22209116 : entry->result = NULL_TREE;
2057 : 22209116 : entry->location = input_location;
2058 : 22209116 : entry->ftc_begin = entry->ftc_end = -1;
2059 : 22209116 : entry->diagnose_instability = false;
2060 : 22209116 : if (ATOMIC_CONSTR_MAP_INSTANTIATED_P (atom))
2061 : : /* We always want to diagnose instability of an atom with an
2062 : : instantiated parameter mapping. For atoms with an uninstantiated
2063 : : mapping, we set this flag (in satisfy_atom) only if substitution
2064 : : into its mapping previously failed. */
2065 : 5723620 : entry->diagnose_instability = true;
2066 : 22209116 : entry->evaluating = false;
2067 : 22209116 : *slot = entry;
2068 : : }
2069 : : else
2070 : : {
2071 : : /* We're evaluating this atom for the first time, and doing so noisily.
2072 : : This shouldn't happen outside of error recovery situations involving
2073 : : unstable satisfaction. Let's just leave 'entry' empty, effectively
2074 : : disabling the cache, and remove the empty slot. */
2075 : 6 : gcc_checking_assert (seen_error ());
2076 : : /* Appease hash_table::check_complete_insertion. */
2077 : 6 : *slot = ggc_alloc<sat_entry> ();
2078 : 6 : sat_cache->clear_slot (slot);
2079 : : }
2080 : : }
2081 : :
2082 : : /* Returns the cached satisfaction result if we have one and we're not
2083 : : recomputing the satisfaction result from scratch. Otherwise returns
2084 : : NULL_TREE. */
2085 : :
2086 : : tree
2087 : 200811582 : satisfaction_cache::get ()
2088 : : {
2089 : 200811582 : if (!entry)
2090 : : return NULL_TREE;
2091 : :
2092 : 200811576 : if (entry->evaluating)
2093 : : {
2094 : : /* If we get here, it means satisfaction is self-recursive. */
2095 : 36 : gcc_checking_assert (!entry->result || seen_error ());
2096 : 36 : if (info.noisy ())
2097 : 18 : error_at (EXPR_LOCATION (ATOMIC_CONSTR_EXPR (entry->atom)),
2098 : : "satisfaction of atomic constraint %qE depends on itself",
2099 : 18 : entry->atom);
2100 : 36 : return error_mark_node;
2101 : : }
2102 : :
2103 : : /* This satisfaction result is "potentially unstable" if a type for which
2104 : : type completion failed during its earlier computation is now complete. */
2105 : 200811540 : bool maybe_unstable = some_type_complete_p (entry->ftc_begin,
2106 : : entry->ftc_end);
2107 : :
2108 : 200811540 : if (info.noisy () || maybe_unstable || !entry->result)
2109 : : {
2110 : : /* We're computing the satisfaction result from scratch. */
2111 : 22220145 : entry->evaluating = true;
2112 : 22220145 : ftc_begin = vec_safe_length (failed_type_completions);
2113 : 22220145 : return NULL_TREE;
2114 : : }
2115 : : else
2116 : : return entry->result;
2117 : : }
2118 : :
2119 : : /* RESULT is the computed satisfaction result. If RESULT differs from the
2120 : : previously cached result, this routine issues an appropriate error.
2121 : : Otherwise, when evaluating quietly, updates the cache appropriately. */
2122 : :
2123 : : tree
2124 : 22214751 : satisfaction_cache::save (tree result)
2125 : : {
2126 : 22214751 : if (!entry)
2127 : : return result;
2128 : :
2129 : 22214745 : gcc_checking_assert (entry->evaluating);
2130 : 22214745 : entry->evaluating = false;
2131 : :
2132 : 22214745 : if (entry->result && result != entry->result)
2133 : : {
2134 : 36 : if (info.quiet ())
2135 : : /* Return error_mark_node to force satisfaction to get replayed
2136 : : noisily. */
2137 : 15 : return error_mark_node;
2138 : : else
2139 : : {
2140 : 21 : if (entry->diagnose_instability)
2141 : : {
2142 : 12 : auto_diagnostic_group d;
2143 : 12 : error_at (EXPR_LOCATION (ATOMIC_CONSTR_EXPR (entry->atom)),
2144 : : "satisfaction value of atomic constraint %qE changed "
2145 : 12 : "from %qE to %qE", entry->atom, entry->result, result);
2146 : 12 : inform (entry->location,
2147 : : "satisfaction value first evaluated to %qE from here",
2148 : 12 : entry->result);
2149 : 12 : }
2150 : : /* For sake of error recovery, allow this latest satisfaction result
2151 : : to prevail. */
2152 : 21 : entry->result = result;
2153 : 21 : return result;
2154 : : }
2155 : : }
2156 : :
2157 : 22214709 : if (info.quiet ())
2158 : : {
2159 : 22203731 : entry->result = result;
2160 : : /* Store into this entry the list of relevant failed type completions
2161 : : that occurred during (re)computation of the satisfaction result. */
2162 : 22203731 : gcc_checking_assert (ftc_begin != -1);
2163 : 22203731 : entry->ftc_begin = ftc_begin;
2164 : 22422512 : entry->ftc_end = vec_safe_length (failed_type_completions);
2165 : : }
2166 : :
2167 : : return result;
2168 : : }
2169 : :
2170 : : /* Substitute ARGS into constraint-expression T during instantiation of
2171 : : a member of a class template. */
2172 : :
2173 : : tree
2174 : 1148227 : tsubst_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
2175 : : {
2176 : : /* We also don't want to evaluate concept-checks when substituting the
2177 : : constraint-expressions of a declaration. */
2178 : 1148227 : processing_constraint_expression_sentinel s;
2179 : 1148227 : cp_unevaluated u;
2180 : 1148227 : tree expr = tsubst_expr (t, args, complain, in_decl);
2181 : 2296454 : return expr;
2182 : 1148227 : }
2183 : :
2184 : : static tree satisfy_constraint_r (tree, tree, sat_info info);
2185 : :
2186 : : /* Compute the satisfaction of a conjunction. */
2187 : :
2188 : : static tree
2189 : 160328392 : satisfy_conjunction (tree t, tree args, sat_info info)
2190 : : {
2191 : 160328392 : tree lhs = satisfy_constraint_r (TREE_OPERAND (t, 0), args, info);
2192 : 160328392 : if (lhs == error_mark_node || lhs == boolean_false_node)
2193 : : return lhs;
2194 : 158224349 : return satisfy_constraint_r (TREE_OPERAND (t, 1), args, info);
2195 : : }
2196 : :
2197 : : /* The current depth at which we're replaying an error during recursive
2198 : : diagnosis of a constraint satisfaction failure. */
2199 : :
2200 : : static int current_constraint_diagnosis_depth;
2201 : :
2202 : : /* Whether CURRENT_CONSTRAINT_DIAGNOSIS_DEPTH has ever exceeded
2203 : : CONCEPTS_DIAGNOSTICS_MAX_DEPTH during recursive diagnosis of a constraint
2204 : : satisfaction error. */
2205 : :
2206 : : static bool concepts_diagnostics_max_depth_exceeded_p;
2207 : :
2208 : : /* Recursive subroutine of collect_operands_of_disjunction. T is a normalized
2209 : : subexpression of a constraint (composed of CONJ_CONSTRs and DISJ_CONSTRs)
2210 : : and E is the corresponding unnormalized subexpression (composed of
2211 : : TRUTH_ANDIF_EXPRs and TRUTH_ORIF_EXPRs). */
2212 : :
2213 : : static void
2214 : 17 : collect_operands_of_disjunction_r (tree t, tree e,
2215 : : auto_vec<tree_pair> *operands)
2216 : : {
2217 : 27 : if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
2218 : : {
2219 : 10 : collect_operands_of_disjunction_r (TREE_OPERAND (t, 0),
2220 : 10 : TREE_OPERAND (e, 0), operands);
2221 : 10 : collect_operands_of_disjunction_r (TREE_OPERAND (t, 1),
2222 : 10 : TREE_OPERAND (e, 1), operands);
2223 : : }
2224 : : else
2225 : : {
2226 : 17 : tree_pair p = std::make_pair (t, e);
2227 : 17 : operands->safe_push (p);
2228 : : }
2229 : 17 : }
2230 : :
2231 : : /* Recursively collect the normalized and unnormalized operands of the
2232 : : disjunction T and append them to OPERANDS in order. */
2233 : :
2234 : : static void
2235 : 7 : collect_operands_of_disjunction (tree t, auto_vec<tree_pair> *operands)
2236 : : {
2237 : 7 : collect_operands_of_disjunction_r (t, CONSTR_EXPR (t), operands);
2238 : 7 : }
2239 : :
2240 : : /* Compute the satisfaction of a disjunction. */
2241 : :
2242 : : static tree
2243 : 15578296 : satisfy_disjunction (tree t, tree args, sat_info info)
2244 : : {
2245 : : /* Evaluate each operand with unsatisfaction diagnostics disabled. */
2246 : 15578296 : sat_info sub = info;
2247 : 15578296 : sub.diagnose_unsatisfaction = false;
2248 : :
2249 : 15578296 : tree lhs = satisfy_constraint_r (TREE_OPERAND (t, 0), args, sub);
2250 : 15578296 : if (lhs == boolean_true_node || lhs == error_mark_node)
2251 : : return lhs;
2252 : :
2253 : 7129772 : tree rhs = satisfy_constraint_r (TREE_OPERAND (t, 1), args, sub);
2254 : 7129772 : if (rhs == boolean_true_node || rhs == error_mark_node)
2255 : : return rhs;
2256 : :
2257 : : /* Both branches evaluated to false. Explain the satisfaction failure in
2258 : : each branch. */
2259 : 997863 : if (info.diagnose_unsatisfaction_p ())
2260 : : {
2261 : 33 : diagnosing_failed_constraint failure (t, args, info.noisy ());
2262 : 33 : cp_expr disj_expr = CONSTR_EXPR (t);
2263 : 33 : inform (disj_expr.get_location (),
2264 : : "no operand of the disjunction is satisfied");
2265 : 33 : if (diagnosing_failed_constraint::replay_errors_p ())
2266 : : {
2267 : 7 : auto_diagnostic_nesting_level sentinel;
2268 : : /* Replay the error in each branch of the disjunction. */
2269 : 7 : auto_vec<tree_pair> operands;
2270 : 7 : collect_operands_of_disjunction (t, &operands);
2271 : 24 : for (unsigned i = 0; i < operands.length (); i++)
2272 : : {
2273 : 17 : tree norm_op = operands[i].first;
2274 : 17 : tree op = operands[i].second;
2275 : 17 : location_t loc = make_location (cp_expr_location (op),
2276 : : disj_expr.get_start (),
2277 : : disj_expr.get_finish ());
2278 : 17 : inform (loc, "the operand %qE is unsatisfied because", op);
2279 : 17 : auto_diagnostic_nesting_level sentinel;
2280 : 17 : satisfy_constraint_r (norm_op, args, info);
2281 : 17 : }
2282 : 7 : }
2283 : 33 : }
2284 : :
2285 : 997863 : return boolean_false_node;
2286 : : }
2287 : :
2288 : : /* Ensures that T is a truth value and not (accidentally, as sometimes
2289 : : happens) an integer value. */
2290 : :
2291 : : tree
2292 : 5722102 : satisfaction_value (tree t)
2293 : : {
2294 : 5722102 : if (t == error_mark_node || t == boolean_true_node || t == boolean_false_node)
2295 : : return t;
2296 : :
2297 : 1 : gcc_assert (TREE_CODE (t) == INTEGER_CST
2298 : : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t),
2299 : : boolean_type_node));
2300 : 1 : if (integer_zerop (t))
2301 : 0 : return boolean_false_node;
2302 : : else
2303 : 1 : return boolean_true_node;
2304 : : }
2305 : :
2306 : : /* Build a new template argument vector corresponding to the parameter
2307 : : mapping of the atomic constraint T, using arguments from ARGS. */
2308 : :
2309 : : static tree
2310 : 5729131 : get_mapped_args (tree t, tree args)
2311 : : {
2312 : 5729131 : tree map = ATOMIC_CONSTR_MAP (t);
2313 : :
2314 : : /* No map, no arguments. */
2315 : 5729131 : if (!map)
2316 : : return NULL_TREE;
2317 : :
2318 : : /* Determine the depth of the resulting argument vector. */
2319 : 5728525 : int depth;
2320 : 5728525 : if (ATOMIC_CONSTR_EXPR_FROM_CONCEPT_P (t))
2321 : : /* The expression of this atomic constraint comes from a concept
2322 : : definition, whose template depth is always one, so the resulting
2323 : : argument vector will also have depth one. */
2324 : : depth = 1;
2325 : : else
2326 : : /* Otherwise, the expression of this atomic constraint comes from
2327 : : the context of the constrained entity, whose template depth is that
2328 : : of ARGS. */
2329 : 2874236 : depth = TMPL_ARGS_DEPTH (args);
2330 : :
2331 : : /* Place each argument at its corresponding position in the argument
2332 : : list. Note that the list will be sparse (not all arguments supplied),
2333 : : but instantiation is guaranteed to only use the parameters in the
2334 : : mapping, so null arguments would never be used. */
2335 : 5728525 : auto_vec< vec<tree> > lists (depth);
2336 : 5728525 : lists.quick_grow_cleared (depth);
2337 : 14640941 : for (tree p = map; p; p = TREE_CHAIN (p))
2338 : : {
2339 : 8912416 : int level;
2340 : 8912416 : int index;
2341 : 8912416 : template_parm_level_and_index (TREE_VALUE (p), &level, &index);
2342 : :
2343 : : /* Insert the argument into its corresponding position. */
2344 : 8912416 : vec<tree> &list = lists[level - 1];
2345 : 11895770 : if (index >= (int)list.length ())
2346 : 8288783 : list.safe_grow_cleared (index + 1, /*exact=*/false);
2347 : 8912416 : list[index] = TREE_PURPOSE (p);
2348 : : }
2349 : :
2350 : : /* Build the new argument list. */
2351 : 5728525 : args = make_tree_vec (lists.length ());
2352 : 23548608 : for (unsigned i = 0; i != lists.length (); ++i)
2353 : : {
2354 : 6045779 : vec<tree> &list = lists[i];
2355 : 6045779 : tree level = make_tree_vec (list.length ());
2356 : 15065457 : for (unsigned j = 0; j < list.length (); ++j)
2357 : 9019678 : TREE_VEC_ELT (level, j) = list[j];
2358 : 6045779 : SET_TMPL_ARGS_LEVEL (args, i + 1, level);
2359 : 6045779 : list.release ();
2360 : : }
2361 : 5728525 : SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, 0);
2362 : :
2363 : 5728525 : if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
2364 : 5728525 : && TMPL_ARGS_DEPTH (args) == 1)
2365 : : {
2366 : : /* Get rid of the redundant outer TREE_VEC. */
2367 : 5411528 : tree level = TMPL_ARGS_LEVEL (args, 1);
2368 : 5411528 : ggc_free (args);
2369 : 5411528 : args = level;
2370 : : }
2371 : :
2372 : 5728525 : return args;
2373 : 5728525 : }
2374 : :
2375 : : static void diagnose_atomic_constraint (tree, tree, tree, sat_info);
2376 : :
2377 : : /* Compute the satisfaction of an atomic constraint. */
2378 : :
2379 : : static tree
2380 : 184322671 : satisfy_atom (tree t, tree args, sat_info info)
2381 : : {
2382 : : /* In case there is a diagnostic, we want to establish the context
2383 : : prior to printing errors. If no errors occur, this context is
2384 : : removed before returning. */
2385 : 184322671 : diagnosing_failed_constraint failure (t, args, info.noisy ());
2386 : :
2387 : 184322671 : satisfaction_cache cache (t, args, info);
2388 : 184322671 : if (tree r = cache.get ())
2389 : : return r;
2390 : :
2391 : : /* Perform substitution quietly. */
2392 : 16491020 : subst_info quiet (tf_none, NULL_TREE);
2393 : :
2394 : : /* Instantiate the parameter mapping. */
2395 : 16491020 : tree map = tsubst_parameter_mapping (ATOMIC_CONSTR_MAP (t), args, quiet);
2396 : 16491020 : if (map == error_mark_node)
2397 : : {
2398 : : /* If instantiation of the parameter mapping fails, the constraint is
2399 : : not satisfied. Replay the substitution. */
2400 : 2109 : if (info.diagnose_unsatisfaction_p ())
2401 : 7 : tsubst_parameter_mapping (ATOMIC_CONSTR_MAP (t), args, info);
2402 : 2109 : if (info.quiet ())
2403 : : /* Since instantiation of the parameter mapping failed, we
2404 : : want to diagnose potential instability of this satisfaction
2405 : : result. */
2406 : 2102 : cache.entry->diagnose_instability = true;
2407 : 2109 : return cache.save (boolean_false_node);
2408 : : }
2409 : :
2410 : : /* Now build a new atom using the instantiated mapping. We use
2411 : : this atom as a second key to the satisfaction cache, and we
2412 : : also pass it to diagnose_atomic_constraint so that diagnostics
2413 : : which refer to the atom display the instantiated mapping. */
2414 : 16488911 : t = copy_node (t);
2415 : 16488911 : ATOMIC_CONSTR_MAP (t) = map;
2416 : 16488911 : gcc_assert (!ATOMIC_CONSTR_MAP_INSTANTIATED_P (t));
2417 : 16488911 : ATOMIC_CONSTR_MAP_INSTANTIATED_P (t) = true;
2418 : 16488911 : satisfaction_cache inst_cache (t, /*args=*/NULL_TREE, info);
2419 : 16488911 : if (tree r = inst_cache.get ())
2420 : : {
2421 : 10759780 : cache.entry->location = inst_cache.entry->location;
2422 : 10759780 : return cache.save (r);
2423 : : }
2424 : :
2425 : : /* Rebuild the argument vector from the parameter mapping. */
2426 : 5729131 : args = get_mapped_args (t, args);
2427 : :
2428 : : /* Apply the parameter mapping (i.e., just substitute). */
2429 : 5729131 : tree expr = ATOMIC_CONSTR_EXPR (t);
2430 : 5729131 : tree result = tsubst_expr (expr, args, quiet.complain, quiet.in_decl);
2431 : 5726431 : if (result == error_mark_node)
2432 : : {
2433 : : /* If substitution results in an invalid type or expression, the
2434 : : constraint is not satisfied. Replay the substitution. */
2435 : 4282 : if (info.diagnose_unsatisfaction_p ())
2436 : 19 : tsubst_expr (expr, args, info.complain, info.in_decl);
2437 : 4282 : return cache.save (inst_cache.save (boolean_false_node));
2438 : : }
2439 : :
2440 : : /* [17.4.1.2] ... lvalue-to-rvalue conversion is performed as necessary,
2441 : : and EXPR shall be a constant expression of type bool. */
2442 : 5722149 : result = force_rvalue (result, info.complain);
2443 : 5722149 : if (result == error_mark_node)
2444 : 0 : return cache.save (inst_cache.save (error_mark_node));
2445 : 5722149 : if (!same_type_p (TREE_TYPE (result), boolean_type_node))
2446 : : {
2447 : 47 : if (info.noisy ())
2448 : 26 : diagnose_atomic_constraint (t, args, result, info);
2449 : 47 : return cache.save (inst_cache.save (error_mark_node));
2450 : : }
2451 : :
2452 : : /* Compute the value of the constraint. */
2453 : 5722102 : if (info.noisy ())
2454 : : {
2455 : 5445 : iloc_sentinel ils (EXPR_LOCATION (result));
2456 : 5445 : result = cxx_constant_value (result);
2457 : 5445 : }
2458 : : else
2459 : : {
2460 : 5716657 : result = maybe_constant_value (result, NULL_TREE, mce_true);
2461 : 5716657 : if (!TREE_CONSTANT (result))
2462 : 18 : result = error_mark_node;
2463 : : }
2464 : 5722102 : result = satisfaction_value (result);
2465 : 5722102 : if (result == boolean_false_node && info.diagnose_unsatisfaction_p ())
2466 : 1127 : diagnose_atomic_constraint (t, args, result, info);
2467 : :
2468 : 5722102 : return cache.save (inst_cache.save (result));
2469 : 184319971 : }
2470 : :
2471 : : /* Determine if the normalized constraint T is satisfied.
2472 : : Returns boolean_true_node if the expression/constraint is
2473 : : satisfied, boolean_false_node if not, and error_mark_node
2474 : : if there was an error evaluating the constraint.
2475 : :
2476 : : The parameter mapping of atomic constraints is simply the
2477 : : set of template arguments that will be substituted into
2478 : : the expression, regardless of template parameters appearing
2479 : : within. Whether a template argument is used in the atomic
2480 : : constraint only matters for subsumption. */
2481 : :
2482 : : static tree
2483 : 360229496 : satisfy_constraint_r (tree t, tree args, sat_info info)
2484 : : {
2485 : 360229496 : if (t == error_mark_node)
2486 : : return error_mark_node;
2487 : :
2488 : 360229359 : switch (TREE_CODE (t))
2489 : : {
2490 : 160328392 : case CONJ_CONSTR:
2491 : 160328392 : return satisfy_conjunction (t, args, info);
2492 : 15578296 : case DISJ_CONSTR:
2493 : 15578296 : return satisfy_disjunction (t, args, info);
2494 : 184322671 : case ATOMIC_CONSTR:
2495 : 184322671 : return satisfy_atom (t, args, info);
2496 : 0 : default:
2497 : 0 : gcc_unreachable ();
2498 : : }
2499 : : }
2500 : :
2501 : : /* Check that the normalized constraint T is satisfied for ARGS. */
2502 : :
2503 : : static tree
2504 : 18968670 : satisfy_normalized_constraints (tree t, tree args, sat_info info)
2505 : : {
2506 : 18968670 : auto_timevar time (TV_CONSTRAINT_SAT);
2507 : :
2508 : 18968670 : auto ovr = make_temp_override (satisfying_constraint, true);
2509 : :
2510 : : /* Turn off template processing. Constraint satisfaction only applies
2511 : : to non-dependent terms, so we want to ensure full checking here. */
2512 : 18968670 : processing_template_decl_sentinel proc (true);
2513 : :
2514 : : /* We need to check access during satisfaction. */
2515 : 18968670 : deferring_access_check_sentinel acs (dk_no_deferred);
2516 : :
2517 : : /* Constraints are unevaluated operands. */
2518 : 18968670 : cp_unevaluated u;
2519 : :
2520 : 18968670 : return satisfy_constraint_r (t, args, info);
2521 : 18965970 : }
2522 : :
2523 : : /* Return the normal form of the constraints on the placeholder 'auto'
2524 : : type T. */
2525 : :
2526 : : static tree
2527 : 2071806 : normalize_placeholder_type_constraints (tree t, bool diag)
2528 : : {
2529 : 2071806 : gcc_assert (is_auto (t));
2530 : 2071806 : tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t);
2531 : 2071806 : if (!ci)
2532 : : return NULL_TREE;
2533 : :
2534 : 2071806 : tree constr = TREE_VALUE (ci);
2535 : : /* The TREE_PURPOSE contains the set of template parameters that were in
2536 : : scope for this placeholder type; use them as the initial template
2537 : : parameters for normalization. */
2538 : 2071806 : tree initial_parms = TREE_PURPOSE (ci);
2539 : :
2540 : : /* The 'auto' itself is used as the first argument in its own constraints,
2541 : : and its level is one greater than its template depth. So in order to
2542 : : capture all used template parameters, we need to add an extra level of
2543 : : template parameters to the context; a dummy level suffices. */
2544 : 2071806 : initial_parms
2545 : 4143357 : = tree_cons (size_int (initial_parms
2546 : : ? TMPL_PARMS_DEPTH (initial_parms) + 1 : 1),
2547 : : make_tree_vec (0), initial_parms);
2548 : :
2549 : 2071806 : norm_info info (diag);
2550 : 2071806 : info.initial_parms = initial_parms;
2551 : 2071806 : return normalize_constraint_expression (constr, info);
2552 : : }
2553 : :
2554 : : /* Evaluate the constraints of T using ARGS, returning a satisfaction value.
2555 : : Here, T can be a concept-id, nested-requirement, placeholder 'auto', or
2556 : : requires-expression. */
2557 : :
2558 : : static tree
2559 : 2839073 : satisfy_nondeclaration_constraints (tree t, tree args, sat_info info)
2560 : : {
2561 : 2839073 : if (t == error_mark_node)
2562 : : return error_mark_node;
2563 : :
2564 : : /* Handle REQUIRES_EXPR directly, bypassing satisfaction. */
2565 : 2839071 : if (TREE_CODE (t) == REQUIRES_EXPR)
2566 : : {
2567 : 116 : auto ovr = make_temp_override (current_constraint_diagnosis_depth);
2568 : 116 : if (info.noisy ())
2569 : 16 : ++current_constraint_diagnosis_depth;
2570 : 116 : return tsubst_requires_expr (t, args, info);
2571 : 116 : }
2572 : :
2573 : : /* Get the normalized constraints. */
2574 : 2838955 : tree norm;
2575 : 2838955 : if (concept_check_p (t))
2576 : : {
2577 : 610141 : gcc_assert (!args);
2578 : 610141 : args = TREE_OPERAND (t, 1);
2579 : 610141 : tree tmpl = get_concept_check_template (t);
2580 : 610141 : norm = normalize_concept_definition (tmpl, info.noisy ());
2581 : : }
2582 : 2228814 : else if (TREE_CODE (t) == NESTED_REQ)
2583 : : {
2584 : 157008 : norm_info ninfo (info.noisy ());
2585 : : /* The TREE_TYPE contains the set of template parameters that were in
2586 : : scope for this nested requirement; use them as the initial template
2587 : : parameters for normalization. */
2588 : 157008 : ninfo.initial_parms = TREE_TYPE (t);
2589 : 157008 : norm = normalize_constraint_expression (TREE_OPERAND (t, 0), ninfo);
2590 : : }
2591 : 2071806 : else if (is_auto (t))
2592 : : {
2593 : 2071806 : norm = normalize_placeholder_type_constraints (t, info.noisy ());
2594 : 2071806 : if (!norm)
2595 : 0 : return boolean_true_node;
2596 : : }
2597 : : else
2598 : 0 : gcc_unreachable ();
2599 : :
2600 : : /* Perform satisfaction. */
2601 : 2838955 : return satisfy_normalized_constraints (norm, args, info);
2602 : : }
2603 : :
2604 : : /* Evaluate the associated constraints of the template specialization T
2605 : : according to INFO, returning a satisfaction value. */
2606 : :
2607 : : static tree
2608 : 194896472 : satisfy_declaration_constraints (tree t, sat_info info)
2609 : : {
2610 : 194896472 : gcc_assert (DECL_P (t) && TREE_CODE (t) != TEMPLATE_DECL);
2611 : 194896472 : const tree saved_t = t;
2612 : :
2613 : : /* For inherited constructors, consider the original declaration;
2614 : : it has the correct template information attached. */
2615 : 194896472 : t = strip_inheriting_ctors (t);
2616 : 194896472 : tree inh_ctor_targs = NULL_TREE;
2617 : 194896472 : if (t != saved_t)
2618 : 70622 : if (tree ti = DECL_TEMPLATE_INFO (saved_t))
2619 : : /* The inherited constructor points to an instantiation of a constructor
2620 : : template; remember its template arguments. */
2621 : 9194 : inh_ctor_targs = TI_ARGS (ti);
2622 : :
2623 : : /* Update the declaration for diagnostics. */
2624 : 194896472 : info.in_decl = t;
2625 : :
2626 : 194896472 : if (info.quiet ())
2627 : 389751396 : if (tree *result = hash_map_safe_get (decl_satisfied_cache, saved_t))
2628 : 166540732 : return *result;
2629 : :
2630 : 28355740 : tree args = NULL_TREE;
2631 : 28355740 : if (tree ti = DECL_TEMPLATE_INFO (t))
2632 : : {
2633 : : /* The initial parameter mapping is the complete set of
2634 : : template arguments substituted into the declaration. */
2635 : 18867212 : args = TI_ARGS (ti);
2636 : 18867212 : if (inh_ctor_targs)
2637 : 3250 : args = add_outermost_template_args (args, inh_ctor_targs);
2638 : : }
2639 : :
2640 : 28355740 : if (regenerated_lambda_fn_p (t))
2641 : : {
2642 : : /* The TI_ARGS of a regenerated lambda contains only the innermost
2643 : : set of template arguments. Augment this with the outer template
2644 : : arguments that were used to regenerate the lambda. */
2645 : 358454 : gcc_assert (!args || TMPL_ARGS_DEPTH (args) == 1);
2646 : 232275 : tree regen_args = lambda_regenerating_args (t);
2647 : 232275 : if (args)
2648 : 126179 : args = add_to_template_args (regen_args, args);
2649 : : else
2650 : : args = regen_args;
2651 : : }
2652 : :
2653 : : /* If the innermost arguments are dependent, or if the outer arguments
2654 : : are dependent and are needed by the constraints, we can't check
2655 : : satisfaction yet so pretend they're satisfied for now. */
2656 : 28355740 : if (uses_template_parms (args)
2657 : 28355740 : && ((DECL_TEMPLATE_INFO (t)
2658 : 64095 : && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))
2659 : 36064 : && (TMPL_ARGS_DEPTH (args) == 1
2660 : 10077 : || uses_template_parms (INNERMOST_TEMPLATE_ARGS (args))))
2661 : 56167 : || uses_outer_template_parms_in_constraints (t)))
2662 : 7977 : return boolean_true_node;
2663 : :
2664 : : /* Get the normalized constraints. */
2665 : 28347763 : tree norm = get_normalized_constraints_from_decl (t, info.noisy ());
2666 : :
2667 : 28347763 : unsigned ftc_count = vec_safe_length (failed_type_completions);
2668 : :
2669 : 28347763 : tree result = boolean_true_node;
2670 : 28347763 : if (norm)
2671 : : {
2672 : 709883 : if (!push_tinst_level (t))
2673 : : return result;
2674 : 709645 : push_to_top_level ();
2675 : 709645 : push_access_scope (t);
2676 : 709645 : result = satisfy_normalized_constraints (norm, args, info);
2677 : 709645 : pop_access_scope (t);
2678 : 709645 : pop_from_top_level ();
2679 : 709645 : pop_tinst_level ();
2680 : : }
2681 : :
2682 : : /* True if this satisfaction is (heuristically) potentially unstable, i.e.
2683 : : if its result may depend on where in the program it was performed. */
2684 : 28347525 : bool maybe_unstable_satisfaction = false;
2685 : 28476161 : if (ftc_count != vec_safe_length (failed_type_completions))
2686 : : /* Type completion failure occurred during satisfaction. The satisfaction
2687 : : result may (or may not) materially depend on the completeness of a type,
2688 : : so we consider it potentially unstable. */
2689 : : maybe_unstable_satisfaction = true;
2690 : :
2691 : 28347525 : if (maybe_unstable_satisfaction)
2692 : : /* Don't cache potentially unstable satisfaction, to allow satisfy_atom
2693 : : to check the stability the next time around. */;
2694 : 28347525 : else if (info.quiet ())
2695 : 28347450 : hash_map_safe_put<hm_ggc> (decl_satisfied_cache, saved_t, result);
2696 : :
2697 : 28347525 : return result;
2698 : : }
2699 : :
2700 : : /* Evaluate the associated constraints of the template T using ARGS as the
2701 : : innermost set of template arguments and according to INFO, returning a
2702 : : satisfaction value. */
2703 : :
2704 : : static tree
2705 : 106140613 : satisfy_declaration_constraints (tree t, tree args, sat_info info)
2706 : : {
2707 : : /* Update the declaration for diagnostics. */
2708 : 106140613 : info.in_decl = t;
2709 : :
2710 : 106140613 : gcc_assert (TREE_CODE (t) == TEMPLATE_DECL);
2711 : :
2712 : 106140613 : if (regenerated_lambda_fn_p (t))
2713 : : {
2714 : : /* As in the two-parameter version of this function. */
2715 : 261332 : gcc_assert (TMPL_ARGS_DEPTH (args) == 1);
2716 : 130666 : tree lambda = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t));
2717 : 130666 : tree outer_args = TI_ARGS (LAMBDA_EXPR_REGEN_INFO (lambda));
2718 : 130666 : args = add_to_template_args (outer_args, args);
2719 : : }
2720 : : else
2721 : 106009947 : args = add_outermost_template_args (t, args);
2722 : :
2723 : : /* If the innermost arguments are dependent, or if the outer arguments
2724 : : are dependent and are needed by the constraints, we can't check
2725 : : satisfaction yet so pretend they're satisfied for now. */
2726 : 106140613 : if (uses_template_parms (args)
2727 : 134654304 : && (TMPL_ARGS_DEPTH (args) == 1
2728 : 1897834 : || uses_template_parms (INNERMOST_TEMPLATE_ARGS (args))
2729 : 119136 : || uses_outer_template_parms_in_constraints (t)))
2730 : 28394555 : return boolean_true_node;
2731 : :
2732 : 77746058 : tree result = boolean_true_node;
2733 : 77746058 : if (tree norm = get_normalized_constraints_from_decl (t, info.noisy ()))
2734 : : {
2735 : 15420070 : if (!push_tinst_level (t, args))
2736 : : return result;
2737 : 15420070 : tree pattern = DECL_TEMPLATE_RESULT (t);
2738 : 15420070 : push_to_top_level ();
2739 : 15420070 : push_access_scope (pattern);
2740 : 15420070 : result = satisfy_normalized_constraints (norm, args, info);
2741 : 15417370 : pop_access_scope (pattern);
2742 : 15417370 : pop_from_top_level ();
2743 : 15417370 : pop_tinst_level ();
2744 : : }
2745 : :
2746 : : return result;
2747 : : }
2748 : :
2749 : : /* A wrapper around satisfy_declaration_constraints and
2750 : : satisfy_nondeclaration_constraints which additionally replays
2751 : : quiet ill-formed satisfaction noisily, so that ill-formed
2752 : : satisfaction always gets diagnosed. */
2753 : :
2754 : : static tree
2755 : 303876158 : constraint_satisfaction_value (tree t, tree args, sat_info info)
2756 : : {
2757 : 303876158 : tree r;
2758 : 303876158 : if (DECL_P (t))
2759 : : {
2760 : 301037085 : if (args)
2761 : 106140613 : r = satisfy_declaration_constraints (t, args, info);
2762 : : else
2763 : 194896472 : r = satisfy_declaration_constraints (t, info);
2764 : : }
2765 : : else
2766 : 2839073 : r = satisfy_nondeclaration_constraints (t, args, info);
2767 : 287 : if (r == error_mark_node && info.quiet ()
2768 : 303873589 : && !(DECL_P (t) && warning_suppressed_p (t)))
2769 : : {
2770 : : /* Replay the error noisily. */
2771 : 131 : sat_info noisy (tf_warning_or_error, info.in_decl);
2772 : 131 : constraint_satisfaction_value (t, args, noisy);
2773 : 131 : if (DECL_P (t) && !args)
2774 : : /* Avoid giving these errors again. */
2775 : 0 : suppress_warning (t);
2776 : : }
2777 : 303873458 : return r;
2778 : : }
2779 : :
2780 : : /* True iff the result of satisfying T using ARGS is BOOLEAN_TRUE_NODE
2781 : : and false otherwise, even in the case of errors.
2782 : :
2783 : : Here, T can be:
2784 : : - a template declaration
2785 : : - a template specialization (in which case ARGS must be empty)
2786 : : - a concept-id (in which case ARGS must be empty)
2787 : : - a nested-requirement
2788 : : - a placeholder 'auto'
2789 : : - a requires-expression. */
2790 : :
2791 : : bool
2792 : 402358005 : constraints_satisfied_p (tree t, tree args/*= NULL_TREE */)
2793 : : {
2794 : 402358005 : if (!flag_concepts)
2795 : : return true;
2796 : :
2797 : 303107787 : sat_info quiet (tf_none, NULL_TREE);
2798 : 303107787 : return constraint_satisfaction_value (t, args, quiet) == boolean_true_node;
2799 : : }
2800 : :
2801 : : /* Evaluate a concept check of the form C<ARGS>. This is only used for the
2802 : : evaluation of template-ids as id-expressions. */
2803 : :
2804 : : tree
2805 : 609923 : evaluate_concept_check (tree check)
2806 : : {
2807 : 609923 : if (check == error_mark_node)
2808 : : return error_mark_node;
2809 : :
2810 : 609923 : gcc_assert (concept_check_p (check));
2811 : :
2812 : : /* Check for satisfaction without diagnostics. */
2813 : 609923 : sat_info quiet (tf_none, NULL_TREE);
2814 : 609923 : return constraint_satisfaction_value (check, /*args=*/NULL_TREE, quiet);
2815 : : }
2816 : :
2817 : : /* Evaluate the requires-expression T, returning either boolean_true_node
2818 : : or boolean_false_node. This is used during folding and constexpr
2819 : : evaluation. */
2820 : :
2821 : : tree
2822 : 100 : evaluate_requires_expr (tree t)
2823 : : {
2824 : 100 : gcc_assert (TREE_CODE (t) == REQUIRES_EXPR);
2825 : 100 : sat_info quiet (tf_none, NULL_TREE);
2826 : 100 : return constraint_satisfaction_value (t, /*args=*/NULL_TREE, quiet);
2827 : : }
2828 : :
2829 : : /*---------------------------------------------------------------------------
2830 : : Semantic analysis of requires-expressions
2831 : : ---------------------------------------------------------------------------*/
2832 : :
2833 : : /* Finish a requires expression for the given PARMS (possibly
2834 : : null) and the non-empty sequence of requirements. */
2835 : :
2836 : : tree
2837 : 659253 : finish_requires_expr (location_t loc, tree parms, tree reqs)
2838 : : {
2839 : : /* Build the node. */
2840 : 659253 : tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs, NULL_TREE);
2841 : 659253 : TREE_SIDE_EFFECTS (r) = false;
2842 : 659253 : TREE_CONSTANT (r) = true;
2843 : 659253 : SET_EXPR_LOCATION (r, loc);
2844 : 659253 : return r;
2845 : : }
2846 : :
2847 : : /* Construct a requirement for the validity of EXPR. */
2848 : :
2849 : : tree
2850 : 359654 : finish_simple_requirement (location_t loc, tree expr)
2851 : : {
2852 : 359654 : tree r = build_nt (SIMPLE_REQ, expr);
2853 : 359654 : SET_EXPR_LOCATION (r, loc);
2854 : 359654 : return r;
2855 : : }
2856 : :
2857 : : /* Construct a requirement for the validity of TYPE. */
2858 : :
2859 : : tree
2860 : 166057 : finish_type_requirement (location_t loc, tree type)
2861 : : {
2862 : 166057 : tree r = build_nt (TYPE_REQ, type);
2863 : 166057 : SET_EXPR_LOCATION (r, loc);
2864 : 166057 : return r;
2865 : : }
2866 : :
2867 : : /* Construct a requirement for the validity of EXPR, along with
2868 : : its properties. If TYPE is non-null, then it specifies either
2869 : : an implicit conversion or argument deduction constraint,
2870 : : depending on whether any placeholders occur in the type name.
2871 : : NOEXCEPT_P is true iff the noexcept keyword was specified. */
2872 : :
2873 : : tree
2874 : 341864 : finish_compound_requirement (location_t loc, tree expr, tree type, bool noexcept_p)
2875 : : {
2876 : 341864 : tree req = build_nt (COMPOUND_REQ, expr, type);
2877 : 341864 : SET_EXPR_LOCATION (req, loc);
2878 : 341864 : COMPOUND_REQ_NOEXCEPT_P (req) = noexcept_p;
2879 : 341864 : return req;
2880 : : }
2881 : :
2882 : : /* Finish a nested requirement. */
2883 : :
2884 : : tree
2885 : 36477 : finish_nested_requirement (location_t loc, tree expr)
2886 : : {
2887 : : /* Build the requirement, saving the set of in-scope template
2888 : : parameters as its type. */
2889 : 36477 : tree r = build1 (NESTED_REQ, current_template_parms, expr);
2890 : 36477 : SET_EXPR_LOCATION (r, loc);
2891 : 36477 : return r;
2892 : : }
2893 : :
2894 : : /*---------------------------------------------------------------------------
2895 : : Equivalence of constraints
2896 : : ---------------------------------------------------------------------------*/
2897 : :
2898 : : /* Returns true when A and B are equivalent constraints. */
2899 : : bool
2900 : 13734759 : equivalent_constraints (tree a, tree b)
2901 : : {
2902 : 13734759 : gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2903 : 13734759 : gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2904 : 13734759 : return cp_tree_equal (a, b);
2905 : : }
2906 : :
2907 : : /* Returns true if the template declarations A and B have equivalent
2908 : : constraints. This is the case when A's constraints subsume B's and
2909 : : when B's also constrain A's. */
2910 : : bool
2911 : 1157416 : equivalently_constrained (tree d1, tree d2)
2912 : : {
2913 : 1157416 : gcc_assert (TREE_CODE (d1) == TREE_CODE (d2));
2914 : 1157416 : return equivalent_constraints (get_constraints (d1), get_constraints (d2));
2915 : : }
2916 : :
2917 : : /*---------------------------------------------------------------------------
2918 : : Partial ordering of constraints
2919 : : ---------------------------------------------------------------------------*/
2920 : :
2921 : : /* Returns true when the constraints in CI strictly subsume
2922 : : the associated constraints of TMPL. */
2923 : :
2924 : : bool
2925 : 158450 : strictly_subsumes (tree ci, tree tmpl)
2926 : : {
2927 : 158450 : tree n1 = get_normalized_constraints_from_info (ci, NULL_TREE);
2928 : 158450 : tree n2 = get_normalized_constraints_from_decl (tmpl);
2929 : :
2930 : 158450 : return subsumes (n1, n2) && !subsumes (n2, n1);
2931 : : }
2932 : :
2933 : : /* Returns true when the template template parameter constraints in CI
2934 : : subsume the associated constraints of the template template argument
2935 : : TMPL. */
2936 : :
2937 : : bool
2938 : 87 : ttp_subsumes (tree ci, tree tmpl)
2939 : : {
2940 : 87 : tree n1 = get_normalized_constraints_from_info (ci, tmpl);
2941 : 87 : tree n2 = get_normalized_constraints_from_decl (tmpl);
2942 : :
2943 : 87 : return subsumes (n1, n2);
2944 : : }
2945 : :
2946 : : /* Determines which of the declarations, A or B, is more constrained.
2947 : : That is, which declaration's constraints subsume but are not subsumed
2948 : : by the other's?
2949 : :
2950 : : Returns 1 if D1 is more constrained than D2, -1 if D2 is more constrained
2951 : : than D1, and 0 otherwise. */
2952 : :
2953 : : int
2954 : 719145 : more_constrained (tree d1, tree d2)
2955 : : {
2956 : 719145 : tree n1 = get_normalized_constraints_from_decl (d1);
2957 : 719145 : tree n2 = get_normalized_constraints_from_decl (d2);
2958 : :
2959 : 719145 : int winner = 0;
2960 : 719145 : if (subsumes (n1, n2))
2961 : 713567 : ++winner;
2962 : 719145 : if (subsumes (n2, n1))
2963 : 571461 : --winner;
2964 : 719145 : return winner;
2965 : : }
2966 : :
2967 : : /* Return whether D1 is at least as constrained as D2. */
2968 : :
2969 : : bool
2970 : 3152692 : at_least_as_constrained (tree d1, tree d2)
2971 : : {
2972 : 3152692 : tree n1 = get_normalized_constraints_from_decl (d1);
2973 : 3152692 : tree n2 = get_normalized_constraints_from_decl (d2);
2974 : :
2975 : 3152692 : return subsumes (n1, n2);
2976 : : }
2977 : :
2978 : : /*---------------------------------------------------------------------------
2979 : : Constraint diagnostics
2980 : : ---------------------------------------------------------------------------*/
2981 : :
2982 : : /* Returns the best location to diagnose a constraint error. */
2983 : :
2984 : : static location_t
2985 : 1153 : get_constraint_error_location (tree t)
2986 : : {
2987 : 1153 : if (location_t loc = cp_expr_location (t))
2988 : : return loc;
2989 : :
2990 : : /* If we have a specific location give it. */
2991 : 1153 : tree expr = CONSTR_EXPR (t);
2992 : 1153 : if (location_t loc = cp_expr_location (expr))
2993 : : return loc;
2994 : :
2995 : : /* If the constraint is normalized from a requires-clause, give
2996 : : the location as that of the constrained declaration. */
2997 : 76 : tree cxt = CONSTR_CONTEXT (t);
2998 : 76 : tree src = cxt ? TREE_VALUE (cxt) : NULL_TREE;
2999 : 75 : if (!src)
3000 : : /* TODO: This only happens for constrained non-template declarations. */
3001 : : ;
3002 : 75 : else if (DECL_P (src))
3003 : 60 : return DECL_SOURCE_LOCATION (src);
3004 : : /* Otherwise, give the location as the defining concept. */
3005 : 15 : else if (concept_check_p (src))
3006 : : {
3007 : 15 : tree tmpl = TREE_OPERAND (src, 0);
3008 : 15 : return DECL_SOURCE_LOCATION (tmpl);
3009 : : }
3010 : :
3011 : 1 : return input_location;
3012 : : }
3013 : :
3014 : : /* Emit a diagnostic for a failed trait. */
3015 : :
3016 : : static void
3017 : 370 : diagnose_trait_expr (tree expr, tree args)
3018 : : {
3019 : 370 : location_t loc = cp_expr_location (expr);
3020 : :
3021 : : /* Build a "fake" version of the instantiated trait, so we can
3022 : : get the instantiated types from result. */
3023 : 370 : ++processing_template_decl;
3024 : 370 : expr = tsubst_expr (expr, args, tf_none, NULL_TREE);
3025 : 370 : --processing_template_decl;
3026 : :
3027 : 370 : tree t1 = TRAIT_EXPR_TYPE1 (expr);
3028 : 370 : tree t2 = TRAIT_EXPR_TYPE2 (expr);
3029 : 370 : if (t2 && TREE_CODE (t2) == TREE_VEC)
3030 : : {
3031 : : /* Convert the TREE_VEC of arguments into a TREE_LIST, since we can't
3032 : : directly print a TREE_VEC but we can a TREE_LIST via the E format
3033 : : specifier. */
3034 : 27 : tree list = NULL_TREE;
3035 : 54 : for (tree t : tree_vec_range (t2))
3036 : 27 : list = tree_cons (NULL_TREE, t, list);
3037 : 27 : t2 = nreverse (list);
3038 : : }
3039 : 370 : switch (TRAIT_EXPR_KIND (expr))
3040 : : {
3041 : 4 : case CPTK_HAS_NOTHROW_ASSIGN:
3042 : 4 : inform (loc, " %qT is not nothrow copy assignable", t1);
3043 : 4 : break;
3044 : 4 : case CPTK_HAS_NOTHROW_CONSTRUCTOR:
3045 : 4 : inform (loc, " %qT is not nothrow default constructible", t1);
3046 : 4 : break;
3047 : 4 : case CPTK_HAS_NOTHROW_COPY:
3048 : 4 : inform (loc, " %qT is not nothrow copy constructible", t1);
3049 : 4 : break;
3050 : 4 : case CPTK_HAS_TRIVIAL_ASSIGN:
3051 : 4 : inform (loc, " %qT is not trivially copy assignable", t1);
3052 : 4 : break;
3053 : 4 : case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
3054 : 4 : inform (loc, " %qT is not trivially default constructible", t1);
3055 : 4 : break;
3056 : 4 : case CPTK_HAS_TRIVIAL_COPY:
3057 : 4 : inform (loc, " %qT is not trivially copy constructible", t1);
3058 : 4 : break;
3059 : 4 : case CPTK_HAS_TRIVIAL_DESTRUCTOR:
3060 : 4 : inform (loc, " %qT is not trivially destructible", t1);
3061 : 4 : break;
3062 : 3 : case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
3063 : 3 : inform (loc, " %qT does not have unique object representations", t1);
3064 : 3 : break;
3065 : 4 : case CPTK_HAS_VIRTUAL_DESTRUCTOR:
3066 : 4 : inform (loc, " %qT does not have a virtual destructor", t1);
3067 : 4 : break;
3068 : 4 : case CPTK_IS_ABSTRACT:
3069 : 4 : inform (loc, " %qT is not an abstract class", t1);
3070 : 4 : break;
3071 : 3 : case CPTK_IS_AGGREGATE:
3072 : 3 : inform (loc, " %qT is not an aggregate", t1);
3073 : 3 : break;
3074 : 0 : case CPTK_IS_ARRAY:
3075 : 0 : inform (loc, " %qT is not an array", t1);
3076 : 0 : break;
3077 : 3 : case CPTK_IS_ASSIGNABLE:
3078 : 3 : inform (loc, " %qT is not assignable from %qT", t1, t2);
3079 : 3 : break;
3080 : 2 : case CPTK_IS_BASE_OF:
3081 : 2 : inform (loc, " %qT is not a base of %qT", t1, t2);
3082 : 2 : break;
3083 : 0 : case CPTK_IS_BOUNDED_ARRAY:
3084 : 0 : inform (loc, " %qT is not a bounded array", t1);
3085 : 0 : break;
3086 : 93 : case CPTK_IS_CLASS:
3087 : 93 : inform (loc, " %qT is not a class", t1);
3088 : 93 : break;
3089 : 0 : case CPTK_IS_CONST:
3090 : 0 : inform (loc, " %qT is not a const type", t1);
3091 : 0 : break;
3092 : 9 : case CPTK_IS_CONSTRUCTIBLE:
3093 : 9 : if (!t2)
3094 : 3 : inform (loc, " %qT is not default constructible", t1);
3095 : : else
3096 : 6 : inform (loc, " %qT is not constructible from %qE", t1, t2);
3097 : : break;
3098 : 0 : case CPTK_IS_CONVERTIBLE:
3099 : 0 : inform (loc, " %qT is not convertible from %qE", t2, t1);
3100 : 0 : break;
3101 : 4 : case CPTK_IS_EMPTY:
3102 : 4 : inform (loc, " %qT is not an empty class", t1);
3103 : 4 : break;
3104 : 0 : case CPTK_IS_ENUM:
3105 : 0 : inform (loc, " %qT is not an enum", t1);
3106 : 0 : break;
3107 : 4 : case CPTK_IS_FINAL:
3108 : 4 : inform (loc, " %qT is not a final class", t1);
3109 : 4 : break;
3110 : 0 : case CPTK_IS_FUNCTION:
3111 : 0 : inform (loc, " %qT is not a function", t1);
3112 : 0 : break;
3113 : 0 : case CPTK_IS_INVOCABLE:
3114 : 0 : if (!t2)
3115 : 0 : inform (loc, " %qT is not invocable", t1);
3116 : : else
3117 : 0 : inform (loc, " %qT is not invocable by %qE", t1, t2);
3118 : : break;
3119 : 0 : case CPTK_IS_LAYOUT_COMPATIBLE:
3120 : 0 : inform (loc, " %qT is not layout compatible with %qT", t1, t2);
3121 : 0 : break;
3122 : 0 : case CPTK_IS_LITERAL_TYPE:
3123 : 0 : inform (loc, " %qT is not a literal type", t1);
3124 : 0 : break;
3125 : 0 : case CPTK_IS_MEMBER_FUNCTION_POINTER:
3126 : 0 : inform (loc, " %qT is not a member function pointer", t1);
3127 : 0 : break;
3128 : 0 : case CPTK_IS_MEMBER_OBJECT_POINTER:
3129 : 0 : inform (loc, " %qT is not a member object pointer", t1);
3130 : 0 : break;
3131 : 0 : case CPTK_IS_MEMBER_POINTER:
3132 : 0 : inform (loc, " %qT is not a member pointer", t1);
3133 : 0 : break;
3134 : 3 : case CPTK_IS_NOTHROW_ASSIGNABLE:
3135 : 3 : inform (loc, " %qT is not nothrow assignable from %qT", t1, t2);
3136 : 3 : break;
3137 : 9 : case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
3138 : 9 : if (!t2)
3139 : 3 : inform (loc, " %qT is not nothrow default constructible", t1);
3140 : : else
3141 : 6 : inform (loc, " %qT is not nothrow constructible from %qE", t1, t2);
3142 : : break;
3143 : 0 : case CPTK_IS_NOTHROW_CONVERTIBLE:
3144 : 0 : inform (loc, " %qT is not nothrow convertible from %qE", t2, t1);
3145 : 0 : break;
3146 : 0 : case CPTK_IS_NOTHROW_INVOCABLE:
3147 : 0 : if (!t2)
3148 : 0 : inform (loc, " %qT is not nothrow invocable", t1);
3149 : : else
3150 : 0 : inform (loc, " %qT is not nothrow invocable by %qE", t1, t2);
3151 : : break;
3152 : 0 : case CPTK_IS_OBJECT:
3153 : 0 : inform (loc, " %qT is not an object type", t1);
3154 : 0 : break;
3155 : 0 : case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
3156 : 0 : inform (loc, " %qT is not pointer-interconvertible base of %qT",
3157 : : t1, t2);
3158 : 0 : break;
3159 : 4 : case CPTK_IS_POD:
3160 : 4 : inform (loc, " %qT is not a POD type", t1);
3161 : 4 : break;
3162 : 0 : case CPTK_IS_POINTER:
3163 : 0 : inform (loc, " %qT is not a pointer", t1);
3164 : 0 : break;
3165 : 4 : case CPTK_IS_POLYMORPHIC:
3166 : 4 : inform (loc, " %qT is not a polymorphic type", t1);
3167 : 4 : break;
3168 : 0 : case CPTK_IS_REFERENCE:
3169 : 0 : inform (loc, " %qT is not a reference", t1);
3170 : 0 : break;
3171 : 145 : case CPTK_IS_SAME:
3172 : 145 : inform (loc, " %qT is not the same as %qT", t1, t2);
3173 : 145 : break;
3174 : 0 : case CPTK_IS_SCOPED_ENUM:
3175 : 0 : inform (loc, " %qT is not a scoped enum", t1);
3176 : 0 : break;
3177 : 4 : case CPTK_IS_STD_LAYOUT:
3178 : 4 : inform (loc, " %qT is not an standard layout type", t1);
3179 : 4 : break;
3180 : 4 : case CPTK_IS_TRIVIAL:
3181 : 4 : inform (loc, " %qT is not a trivial type", t1);
3182 : 4 : break;
3183 : 3 : case CPTK_IS_TRIVIALLY_ASSIGNABLE:
3184 : 3 : inform (loc, " %qT is not trivially assignable from %qT", t1, t2);
3185 : 3 : break;
3186 : 9 : case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
3187 : 9 : if (!t2)
3188 : 3 : inform (loc, " %qT is not trivially default constructible", t1);
3189 : : else
3190 : 6 : inform (loc, " %qT is not trivially constructible from %qE", t1, t2);
3191 : : break;
3192 : 3 : case CPTK_IS_TRIVIALLY_COPYABLE:
3193 : 3 : inform (loc, " %qT is not trivially copyable", t1);
3194 : 3 : break;
3195 : 0 : case CPTK_IS_UNBOUNDED_ARRAY:
3196 : 0 : inform (loc, " %qT is not an unbounded array", t1);
3197 : 0 : break;
3198 : 4 : case CPTK_IS_UNION:
3199 : 4 : inform (loc, " %qT is not a union", t1);
3200 : 4 : break;
3201 : 0 : case CPTK_IS_VIRTUAL_BASE_OF:
3202 : 0 : inform (loc, " %qT is not a virtual base of %qT", t1, t2);
3203 : 0 : break;
3204 : 0 : case CPTK_IS_VOLATILE:
3205 : 0 : inform (loc, " %qT is not a volatile type", t1);
3206 : 0 : break;
3207 : 0 : case CPTK_RANK:
3208 : 0 : inform (loc, " %qT cannot yield a rank", t1);
3209 : 0 : break;
3210 : 0 : case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
3211 : 0 : inform (loc, " %qT is not a reference that binds to a temporary "
3212 : : "object of type %qT (direct-initialization)", t1, t2);
3213 : 0 : break;
3214 : 0 : case CPTK_REF_CONVERTS_FROM_TEMPORARY:
3215 : 0 : inform (loc, " %qT is not a reference that binds to a temporary "
3216 : : "object of type %qT (copy-initialization)", t1, t2);
3217 : 0 : break;
3218 : 21 : case CPTK_IS_DEDUCIBLE:
3219 : 21 : inform (loc, " %qD is not deducible from %qT", t1, t2);
3220 : 21 : break;
3221 : : #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
3222 : : case CPTK_##CODE:
3223 : : #include "cp-trait.def"
3224 : : #undef DEFTRAIT_TYPE
3225 : : /* Type-yielding traits aren't expressions. */
3226 : 0 : gcc_unreachable ();
3227 : : /* We deliberately omit the default case so that when adding a new
3228 : : trait we'll get reminded (by way of a warning) to handle it here. */
3229 : : }
3230 : 370 : }
3231 : :
3232 : : /* Diagnose a substitution failure in the atomic constraint T using ARGS. */
3233 : :
3234 : : static void
3235 : 1153 : diagnose_atomic_constraint (tree t, tree args, tree result, sat_info info)
3236 : : {
3237 : : /* If the constraint is already ill-formed, we've previously diagnosed
3238 : : the reason. We should still say why the constraints aren't satisfied. */
3239 : 1153 : if (t == error_mark_node)
3240 : : {
3241 : 0 : location_t loc;
3242 : 0 : if (info.in_decl)
3243 : 0 : loc = DECL_SOURCE_LOCATION (info.in_decl);
3244 : : else
3245 : 0 : loc = input_location;
3246 : 0 : inform (loc, "invalid constraints");
3247 : 0 : return;
3248 : : }
3249 : :
3250 : 1153 : location_t loc = get_constraint_error_location (t);
3251 : 1153 : iloc_sentinel loc_s (loc);
3252 : :
3253 : : /* Generate better diagnostics for certain kinds of expressions. */
3254 : 1153 : tree expr = ATOMIC_CONSTR_EXPR (t);
3255 : 1153 : STRIP_ANY_LOCATION_WRAPPER (expr);
3256 : 1153 : switch (TREE_CODE (expr))
3257 : : {
3258 : 370 : case TRAIT_EXPR:
3259 : 370 : diagnose_trait_expr (expr, args);
3260 : 370 : break;
3261 : 300 : case REQUIRES_EXPR:
3262 : 300 : gcc_checking_assert (info.diagnose_unsatisfaction_p ());
3263 : : /* Clear in_decl before replaying the substitution to avoid emitting
3264 : : seemingly unhelpful "in declaration ..." notes that follow some
3265 : : substitution failure error messages. */
3266 : 300 : info.in_decl = NULL_TREE;
3267 : 300 : tsubst_requires_expr (expr, args, info);
3268 : 300 : break;
3269 : 483 : default:
3270 : 483 : if (!same_type_p (TREE_TYPE (result), boolean_type_node))
3271 : 26 : error_at (loc, "constraint %qE has type %qT, not %<bool%>",
3272 : 26 : t, TREE_TYPE (result));
3273 : : else
3274 : 457 : inform (loc, "the expression %qE evaluated to %<false%>", t);
3275 : : }
3276 : 1153 : }
3277 : :
3278 : : GTY(()) tree current_failed_constraint;
3279 : :
3280 : 184322704 : diagnosing_failed_constraint::
3281 : 184322704 : diagnosing_failed_constraint (tree t, tree args, bool diag)
3282 : 184322704 : : diagnosing_error (diag)
3283 : : {
3284 : 184322704 : if (diagnosing_error)
3285 : : {
3286 : 5557 : current_failed_constraint
3287 : 5557 : = tree_cons (args, t, current_failed_constraint);
3288 : 5557 : ++current_constraint_diagnosis_depth;
3289 : : }
3290 : 184322704 : }
3291 : :
3292 : 184320004 : diagnosing_failed_constraint::
3293 : : ~diagnosing_failed_constraint ()
3294 : : {
3295 : 184320004 : if (diagnosing_error)
3296 : : {
3297 : 5557 : --current_constraint_diagnosis_depth;
3298 : 5557 : if (current_failed_constraint)
3299 : 4288 : current_failed_constraint = TREE_CHAIN (current_failed_constraint);
3300 : : }
3301 : :
3302 : 184320004 : }
3303 : :
3304 : : /* Whether we are allowed to replay an error that underlies a constraint failure
3305 : : at the current diagnosis depth. */
3306 : :
3307 : : bool
3308 : 386 : diagnosing_failed_constraint::replay_errors_p ()
3309 : : {
3310 : 386 : if (current_constraint_diagnosis_depth >= concepts_diagnostics_max_depth)
3311 : : {
3312 : 344 : concepts_diagnostics_max_depth_exceeded_p = true;
3313 : 344 : return false;
3314 : : }
3315 : : else
3316 : : return true;
3317 : : }
3318 : :
3319 : : /* Emit diagnostics detailing the failure ARGS to satisfy the constraints
3320 : : of T. Here, T and ARGS are as in constraints_satisfied_p. */
3321 : :
3322 : : void
3323 : 1218 : diagnose_constraints (location_t loc, tree t, tree args)
3324 : : {
3325 : 1218 : inform (loc, "constraints not satisfied");
3326 : :
3327 : 1218 : if (concepts_diagnostics_max_depth == 0)
3328 : 0 : return;
3329 : :
3330 : 1218 : auto_diagnostic_nesting_level sentinel;
3331 : :
3332 : : /* Replay satisfaction, but diagnose unsatisfaction. */
3333 : 1218 : sat_info noisy (tf_warning_or_error, NULL_TREE, /*diag_unsat=*/true);
3334 : 1218 : constraint_satisfaction_value (t, args, noisy);
3335 : :
3336 : 1218 : static bool suggested_p;
3337 : 1218 : if (concepts_diagnostics_max_depth_exceeded_p
3338 : 335 : && current_constraint_diagnosis_depth == 0
3339 : 332 : && !suggested_p)
3340 : : {
3341 : 129 : inform (UNKNOWN_LOCATION,
3342 : : "set %qs to at least %d for more detail",
3343 : : "-fconcepts-diagnostics-depth=",
3344 : 129 : concepts_diagnostics_max_depth + 1);
3345 : 129 : suggested_p = true;
3346 : : }
3347 : 1218 : }
3348 : :
3349 : : #include "gt-cp-constraint.h"
|