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