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