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