2017-03-06 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / cp / constraint.cc
blobe91b11679bb329bf02ae0a34f863d939919275de
1 /* Processing rules for constraints.
2 Copyright (C) 2013-2017 Free Software Foundation, Inc.
3 Contributed by Andrew Sutton (andrew.n.sutton@gmail.com)
5 This file is part of GCC.
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.
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.
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/>. */
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"
49 /*---------------------------------------------------------------------------
50 Operations on constraints
51 ---------------------------------------------------------------------------*/
53 /* Returns true if C is a constraint tree code. Note that ERROR_MARK
54 is a valid constraint. */
56 static inline bool
57 constraint_p (tree_code c)
59 return ((PRED_CONSTR <= c && c <= DISJ_CONSTR)
60 || c == EXPR_PACK_EXPANSION
61 || c == ERROR_MARK);
64 /* Returns true if T is a constraint. Note that error_mark_node
65 is a valid constraint. */
67 bool
68 constraint_p (tree t)
70 return constraint_p (TREE_CODE (t));
73 /* Returns the conjunction of two constraints A and B. Note that
74 conjoining a non-null constraint with NULL_TREE is an identity
75 operation. That is, for non-null A,
77 conjoin_constraints(a, NULL_TREE) == a
79 and
81 conjoin_constraints (NULL_TREE, a) == a
83 If both A and B are NULL_TREE, the result is also NULL_TREE. */
85 tree
86 conjoin_constraints (tree a, tree b)
88 gcc_assert (a ? constraint_p (a) : true);
89 gcc_assert (b ? constraint_p (b) : true);
90 if (a)
91 return b ? build_nt (CONJ_CONSTR, a, b) : a;
92 else if (b)
93 return b;
94 else
95 return NULL_TREE;
98 /* Transform the vector of expressions in the T into a conjunction
99 of requirements. T must be a TREE_VEC. */
101 tree
102 conjoin_constraints (tree t)
104 gcc_assert (TREE_CODE (t) == TREE_VEC);
105 tree r = NULL_TREE;
106 for (int i = 0; i < TREE_VEC_LENGTH (t); ++i)
107 r = conjoin_constraints (r, TREE_VEC_ELT (t, i));
108 return r;
111 /* Returns true if T is a call expression to a function
112 concept. */
114 bool
115 function_concept_check_p (tree t)
117 gcc_assert (TREE_CODE (t) == CALL_EXPR);
118 tree fn = CALL_EXPR_FN (t);
119 if (fn != NULL_TREE
120 && TREE_CODE (fn) == TEMPLATE_ID_EXPR
121 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
123 tree f1 = get_first_fn (fn);
124 if (TREE_CODE (f1) == TEMPLATE_DECL
125 && DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (f1)))
126 return true;
128 return false;
131 /* Returns true if any of the arguments in the template
132 argument list is a wildcard or wildcard pack. */
134 bool
135 contains_wildcard_p (tree args)
137 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
139 tree arg = TREE_VEC_ELT (args, i);
140 if (TREE_CODE (arg) == WILDCARD_DECL)
141 return true;
143 return false;
146 /* Build a new call expression, but don't actually generate a
147 new function call. We just want the tree, not the semantics. */
149 inline tree
150 build_call_check (tree id)
152 ++processing_template_decl;
153 vec<tree, va_gc> *fargs = make_tree_vector();
154 tree call = finish_call_expr (id, &fargs, false, false, tf_none);
155 release_tree_vector (fargs);
156 --processing_template_decl;
157 return call;
160 /* Build an expression that will check a variable concept. If any
161 argument contains a wildcard, don't try to finish the variable
162 template because we can't substitute into a non-existent
163 declaration. */
165 tree
166 build_variable_check (tree id)
168 gcc_assert (TREE_CODE (id) == TEMPLATE_ID_EXPR);
169 if (contains_wildcard_p (TREE_OPERAND (id, 1)))
170 return id;
172 ++processing_template_decl;
173 tree var = finish_template_variable (id);
174 --processing_template_decl;
175 return var;
178 /*---------------------------------------------------------------------------
179 Resolution of qualified concept names
180 ---------------------------------------------------------------------------*/
182 /* This facility is used to resolve constraint checks from
183 requirement expressions. A constraint check is a call to
184 a function template declared with the keyword 'concept'.
186 The result of resolution is a pair (a TREE_LIST) whose value
187 is the matched declaration, and whose purpose contains the
188 coerced template arguments that can be substituted into the
189 call. */
191 // Given an overload set OVL, try to find a unique definition that can be
192 // instantiated by the template arguments ARGS.
194 // This function is not called for arbitrary call expressions. In particular,
195 // the call expression must be written with explicit template arguments
196 // and no function arguments. For example:
198 // f<T, U>()
200 // If a single match is found, this returns a TREE_LIST whose VALUE
201 // is the constraint function (not the template), and its PURPOSE is
202 // the complete set of arguments substituted into the parameter list.
203 static tree
204 resolve_constraint_check (tree ovl, tree args)
206 int nerrs = 0;
207 tree cands = NULL_TREE;
208 for (tree p = ovl; p != NULL_TREE; p = OVL_NEXT (p))
210 // Get the next template overload.
211 tree tmpl = OVL_CURRENT (p);
212 if (TREE_CODE (tmpl) != TEMPLATE_DECL)
213 continue;
215 // Don't try to deduce checks for non-concepts. We often
216 // end up trying to resolve constraints in functional casts
217 // as part of a postfix-expression. We can save time and
218 // headaches by not instantiating those declarations.
220 // NOTE: This masks a potential error, caused by instantiating
221 // non-deduced contexts using placeholder arguments.
222 tree fn = DECL_TEMPLATE_RESULT (tmpl);
223 if (DECL_ARGUMENTS (fn))
224 continue;
225 if (!DECL_DECLARED_CONCEPT_P (fn))
226 continue;
228 // Remember the candidate if we can deduce a substitution.
229 ++processing_template_decl;
230 tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl));
231 if (tree subst = coerce_template_parms (parms, args, tmpl))
233 if (subst == error_mark_node)
234 ++nerrs;
235 else
236 cands = tree_cons (subst, fn, cands);
238 --processing_template_decl;
241 if (!cands)
242 /* We either had no candidates or failed deductions. */
243 return nerrs ? error_mark_node : NULL_TREE;
244 else if (TREE_CHAIN (cands))
245 /* There are multiple candidates. */
246 return error_mark_node;
248 return cands;
251 // Determine if the the call expression CALL is a constraint check, and
252 // return the concept declaration and arguments being checked. If CALL
253 // does not denote a constraint check, return NULL.
254 tree
255 resolve_constraint_check (tree call)
257 gcc_assert (TREE_CODE (call) == CALL_EXPR);
259 // A constraint check must be only a template-id expression. If
260 // it's a call to a base-link, its function(s) should be a
261 // template-id expression. If this is not a template-id, then it
262 // cannot be a concept-check.
263 tree target = CALL_EXPR_FN (call);
264 if (BASELINK_P (target))
265 target = BASELINK_FUNCTIONS (target);
266 if (TREE_CODE (target) != TEMPLATE_ID_EXPR)
267 return NULL_TREE;
269 // Get the overload set and template arguments and try to
270 // resolve the target.
271 tree ovl = TREE_OPERAND (target, 0);
273 /* This is a function call of a variable concept... ill-formed. */
274 if (TREE_CODE (ovl) == TEMPLATE_DECL)
276 error_at (location_of (call),
277 "function call of variable concept %qE", call);
278 return error_mark_node;
281 tree args = TREE_OPERAND (target, 1);
282 return resolve_constraint_check (ovl, args);
285 /* Returns a pair containing the checked variable concept
286 and its associated prototype parameter. The result
287 is a TREE_LIST whose TREE_VALUE is the variable concept
288 and whose TREE_PURPOSE is the prototype parameter. */
290 tree
291 resolve_variable_concept_check (tree id)
293 tree tmpl = TREE_OPERAND (id, 0);
294 tree args = TREE_OPERAND (id, 1);
296 if (!variable_concept_p (tmpl))
297 return NULL_TREE;
299 /* Make sure that we have the right parameters before
300 assuming that it works. Note that failing to deduce
301 will result in diagnostics. */
302 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
303 ++processing_template_decl;
304 tree result = coerce_template_parms (parms, args, tmpl);
305 --processing_template_decl;
306 if (result != error_mark_node)
308 tree decl = DECL_TEMPLATE_RESULT (tmpl);
309 return build_tree_list (result, decl);
311 else
312 return error_mark_node;
316 /* Given a call expression or template-id expression to
317 a concept EXPR possibly including a wildcard, deduce
318 the concept being checked and the prototype parameter.
319 Returns true if the constraint and prototype can be
320 deduced and false otherwise. Note that the CHECK and
321 PROTO arguments are set to NULL_TREE if this returns
322 false. */
324 bool
325 deduce_constrained_parameter (tree expr, tree& check, tree& proto)
327 tree info = NULL_TREE;
328 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
329 info = resolve_variable_concept_check (expr);
330 else if (TREE_CODE (expr) == CALL_EXPR)
331 info = resolve_constraint_check (expr);
332 else
333 gcc_unreachable ();
335 if (info && info != error_mark_node)
337 check = TREE_VALUE (info);
338 tree arg = TREE_VEC_ELT (TREE_PURPOSE (info), 0);
339 if (ARGUMENT_PACK_P (arg))
340 arg = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0);
341 proto = TREE_TYPE (arg);
342 return true;
344 check = proto = NULL_TREE;
345 return false;
348 // Given a call expression or template-id expression to a concept, EXPR,
349 // deduce the concept being checked and return the template arguments.
350 // Returns NULL_TREE if deduction fails.
351 static tree
352 deduce_concept_introduction (tree expr)
354 tree info = NULL_TREE;
355 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
356 info = resolve_variable_concept_check (expr);
357 else if (TREE_CODE (expr) == CALL_EXPR)
358 info = resolve_constraint_check (expr);
359 else
360 gcc_unreachable ();
362 if (info && info != error_mark_node)
363 return TREE_PURPOSE (info);
364 return NULL_TREE;
367 namespace {
369 /*---------------------------------------------------------------------------
370 Constraint implication learning
371 ---------------------------------------------------------------------------*/
373 /* The implication context determines how we memoize concept checks.
374 Given two checks C1 and C2, the direction of implication depends
375 on whether we are learning implications of a conjunction or disjunction.
376 For example:
378 template<typename T> concept bool C = ...;
379 template<typenaem T> concept bool D = C<T> && true;
381 From this, we can learn that D<T> implies C<T>. We cannot learn,
382 without further testing, that C<T> does not imply D<T>. If, for
383 example, C<T> were defined as true, then these constraints would
384 be logically equivalent.
386 In rare cases, we may start with a logical equivalence. For example:
388 template<typename T> concept bool C = ...;
389 template<typename T> concept bool D = C<T>;
391 Here, we learn that C<T> implies D<T> and vice versa. */
393 enum implication_context
395 conjunction_cxt, /* C1 implies C2. */
396 disjunction_cxt, /* C2 implies C1. */
397 equivalence_cxt /* C1 implies C2, C2 implies C1. */
400 void learn_implications(tree, tree, implication_context);
402 void
403 learn_implication (tree parent, tree child, implication_context cxt)
405 switch (cxt)
407 case conjunction_cxt:
408 save_subsumption_result (parent, child, true);
409 break;
410 case disjunction_cxt:
411 save_subsumption_result (child, parent, true);
412 break;
413 case equivalence_cxt:
414 save_subsumption_result (parent, child, true);
415 save_subsumption_result (child, parent, true);
416 break;
420 void
421 learn_logical_operation (tree parent, tree constr, implication_context cxt)
423 learn_implications (parent, TREE_OPERAND (constr, 0), cxt);
424 learn_implications (parent, TREE_OPERAND (constr, 1), cxt);
427 void
428 learn_implications (tree parent, tree constr, implication_context cxt)
430 switch (TREE_CODE (constr))
432 case CHECK_CONSTR:
433 return learn_implication (parent, constr, cxt);
435 case CONJ_CONSTR:
436 if (cxt == disjunction_cxt)
437 return;
438 return learn_logical_operation (parent, constr, cxt);
440 case DISJ_CONSTR:
441 if (cxt == conjunction_cxt)
442 return;
443 return learn_logical_operation (parent, constr, cxt);
445 default:
446 break;
450 /* Quickly scan the top-level constraints of CONSTR to learn and
451 cache logical relations between concepts. The search does not
452 include conjunctions of disjunctions or vice versa. */
454 void
455 learn_implications (tree tmpl, tree args, tree constr)
457 /* Don't memoize relations between non-dependent arguemnts. It's not
458 helpful. */
459 if (!uses_template_parms (args))
460 return;
462 /* Build a check constraint for the purpose of caching. */
463 tree parent = build_nt (CHECK_CONSTR, tmpl, args);
465 /* Start learning based on the kind of the top-level contraint. */
466 if (TREE_CODE (constr) == CONJ_CONSTR)
467 return learn_logical_operation (parent, constr, conjunction_cxt);
468 else if (TREE_CODE (constr) == DISJ_CONSTR)
469 return learn_logical_operation (parent, constr, disjunction_cxt);
470 else if (TREE_CODE (constr) == CHECK_CONSTR)
471 /* This is the rare concept alias case. */
472 return learn_implication (parent, constr, equivalence_cxt);
475 /*---------------------------------------------------------------------------
476 Expansion of concept definitions
477 ---------------------------------------------------------------------------*/
479 /* Returns the expression of a function concept. */
481 tree
482 get_returned_expression (tree fn)
484 /* Extract the body of the function minus the return expression. */
485 tree body = DECL_SAVED_TREE (fn);
486 if (!body)
487 return error_mark_node;
488 if (TREE_CODE (body) == BIND_EXPR)
489 body = BIND_EXPR_BODY (body);
490 if (TREE_CODE (body) != RETURN_EXPR)
491 return error_mark_node;
493 return TREE_OPERAND (body, 0);
496 /* Returns the initializer of a variable concept. */
498 tree
499 get_variable_initializer (tree var)
501 tree init = DECL_INITIAL (var);
502 if (!init)
503 return error_mark_node;
504 return init;
507 /* Returns the definition of a variable or function concept. */
509 tree
510 get_concept_definition (tree decl)
512 if (VAR_P (decl))
513 return get_variable_initializer (decl);
514 else if (TREE_CODE (decl) == FUNCTION_DECL)
515 return get_returned_expression (decl);
516 gcc_unreachable ();
519 int expansion_level = 0;
521 struct expanding_concept_sentinel
523 expanding_concept_sentinel ()
525 ++expansion_level;
528 ~expanding_concept_sentinel()
530 --expansion_level;
535 } /* namespace */
537 /* Returns true when a concept is being expanded. */
539 bool
540 expanding_concept()
542 return expansion_level > 0;
545 /* Expand a concept declaration (not a template) and its arguments to
546 a constraint defined by the concept's initializer or definition. */
548 tree
549 expand_concept (tree decl, tree args)
551 expanding_concept_sentinel sentinel;
553 if (TREE_CODE (decl) == TEMPLATE_DECL)
554 decl = DECL_TEMPLATE_RESULT (decl);
555 tree tmpl = DECL_TI_TEMPLATE (decl);
557 /* Check for a previous specialization. */
558 if (tree spec = get_concept_expansion (tmpl, args))
559 return spec;
561 /* Substitute the arguments to form a new definition expression. */
562 tree def = get_concept_definition (decl);
564 ++processing_template_decl;
565 tree result = tsubst_expr (def, args, tf_none, NULL_TREE, true);
566 --processing_template_decl;
567 if (result == error_mark_node)
568 return error_mark_node;
570 /* And lastly, normalize it, check for implications, and save
571 the specialization for later. */
572 tree norm = normalize_expression (result);
573 learn_implications (tmpl, args, norm);
574 return save_concept_expansion (tmpl, args, norm);
578 /*---------------------------------------------------------------------------
579 Stepwise normalization of expressions
581 This set of functions will transform an expression into a constraint
582 in a sequence of steps. Normalization does not not look into concept
583 definitions.
584 ---------------------------------------------------------------------------*/
586 /* Transform a logical-or or logical-and expression into either
587 a conjunction or disjunction. */
589 tree
590 normalize_logical_operation (tree t, tree_code c)
592 tree t0 = normalize_expression (TREE_OPERAND (t, 0));
593 tree t1 = normalize_expression (TREE_OPERAND (t, 1));
594 return build_nt (c, t0, t1);
597 /* A simple requirement T introduces an expression constraint
598 for its expression. */
600 inline tree
601 normalize_simple_requirement (tree t)
603 return build_nt (EXPR_CONSTR, TREE_OPERAND (t, 0));
606 /* A type requirement T introduce a type constraint for its type. */
608 inline tree
609 normalize_type_requirement (tree t)
611 return build_nt (TYPE_CONSTR, TREE_OPERAND (t, 0));
614 /* A compound requirement T introduces a conjunction of constraints
615 depending on its form. The conjunction always includes an
616 expression constraint for the expression of the requirement.
617 If a trailing return type was specified, the conjunction includes
618 either an implicit conversion constraint or an argument deduction
619 constraint. If the noexcept specifier is present, the conjunction
620 includes an exception constraint. */
622 tree
623 normalize_compound_requirement (tree t)
625 tree expr = TREE_OPERAND (t, 0);
626 tree constr = build_nt (EXPR_CONSTR, TREE_OPERAND (t, 0));
628 /* If a type is given, append an implicit conversion or
629 argument deduction constraint. */
630 if (tree type = TREE_OPERAND (t, 1))
632 tree type_constr;
633 /* TODO: We should be extracting a list of auto nodes
634 from type_uses_auto, not a single node */
635 if (tree placeholder = type_uses_auto (type))
636 type_constr = build_nt (DEDUCT_CONSTR, expr, type, placeholder);
637 else
638 type_constr = build_nt (ICONV_CONSTR, expr, type);
639 constr = conjoin_constraints (constr, type_constr);
642 /* If noexcept is present, append an exception constraint. */
643 if (COMPOUND_REQ_NOEXCEPT_P (t))
645 tree except = build_nt (EXCEPT_CONSTR, expr);
646 constr = conjoin_constraints (constr, except);
649 return constr;
652 /* A nested requirement T introduces a conjunction of constraints
653 corresponding to its constraint-expression.
655 If the result of transforming T is error_mark_node, the resulting
656 constraint is a predicate constraint whose operand is also
657 error_mark_node. This preserves the constraint structure, but
658 will guarantee that the constraint is never satisfied. */
660 inline tree
661 normalize_nested_requirement (tree t)
663 return normalize_expression (TREE_OPERAND (t, 0));
666 /* Transform a requirement T into one or more constraints. */
668 tree
669 normalize_requirement (tree t)
671 switch (TREE_CODE (t))
673 case SIMPLE_REQ:
674 return normalize_simple_requirement (t);
676 case TYPE_REQ:
677 return normalize_type_requirement (t);
679 case COMPOUND_REQ:
680 return normalize_compound_requirement (t);
682 case NESTED_REQ:
683 return normalize_nested_requirement (t);
685 default:
686 gcc_unreachable ();
688 return error_mark_node;
691 /* Transform a sequence of requirements into a conjunction of
692 constraints. */
694 tree
695 normalize_requirements (tree t)
697 tree result = NULL_TREE;
698 for (; t; t = TREE_CHAIN (t))
700 tree constr = normalize_requirement (TREE_VALUE (t));
701 result = conjoin_constraints (result, constr);
703 return result;
706 /* The normal form of a requires-expression is a parameterized
707 constraint having the same parameters and a conjunction of
708 constraints representing the normal form of requirements. */
710 tree
711 normalize_requires_expression (tree t)
713 tree operand = normalize_requirements (TREE_OPERAND (t, 1));
714 if (tree parms = TREE_OPERAND (t, 0))
715 return build_nt (PARM_CONSTR, parms, operand);
716 else
717 return operand;
720 /* For a template-id referring to a variable concept, returns
721 a check constraint. Otherwise, returns a predicate constraint. */
723 tree
724 normalize_template_id_expression (tree t)
726 if (tree info = resolve_variable_concept_check (t))
728 if (info == error_mark_node)
730 /* We get this when the template arguments don't match
731 the variable concept. */
732 error ("invalid reference to concept %qE", t);
733 return error_mark_node;
736 tree decl = TREE_VALUE (info);
737 tree args = TREE_PURPOSE (info);
738 return build_nt (CHECK_CONSTR, decl, args);
741 /* Check that we didn't refer to a function concept like a variable. */
742 tree tmpl = TREE_OPERAND (t, 0);
743 if (TREE_CODE (tmpl) == OVERLOAD)
745 tree fn = OVL_FUNCTION (tmpl);
746 if (TREE_CODE (fn) == TEMPLATE_DECL
747 && DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (fn)))
749 error_at (location_of (t),
750 "invalid reference to function concept %qD", fn);
751 return error_mark_node;
755 return build_nt (PRED_CONSTR, t);
758 /* For a call expression to a function concept, returns a check
759 constraint. Otherwise, returns a predicate constraint. */
761 tree
762 normalize_call_expression (tree t)
764 /* Try to resolve this function call as a concept. If not, then
765 it can be returned as a predicate constraint. */
766 tree check = resolve_constraint_check (t);
767 if (!check)
768 return build_nt (PRED_CONSTR, t);
769 if (check == error_mark_node)
771 /* TODO: Improve diagnostics. We could report why the reference
772 is invalid. */
773 error ("invalid reference to concept %qE", t);
774 return error_mark_node;
777 tree fn = TREE_VALUE (check);
778 tree args = TREE_PURPOSE (check);
779 return build_nt (CHECK_CONSTR, fn, args);
782 /* If T is a call to an overloaded && or || operator, diagnose that
783 as a non-SFINAEable error. Returns true if an error is emitted.
785 TODO: It would be better to diagnose this at the point of definition,
786 if possible. Perhaps we should immediately do a first-pass normalization
787 of a concept definition to catch obvious non-dependent errors like
788 this. */
790 bool
791 check_for_logical_overloads (tree t)
793 if (TREE_CODE (t) != CALL_EXPR)
794 return false;
796 tree fn = CALL_EXPR_FN (t);
798 /* For member calls, try extracting the function from the
799 component ref. */
800 if (TREE_CODE (fn) == COMPONENT_REF)
802 fn = TREE_OPERAND (fn, 1);
803 if (TREE_CODE (fn) == BASELINK)
804 fn = BASELINK_FUNCTIONS (fn);
807 if (TREE_CODE (fn) != FUNCTION_DECL)
808 return false;
810 if (DECL_OVERLOADED_OPERATOR_P (fn))
812 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
813 error_at (loc, "constraint %qE, uses overloaded operator", t);
814 return true;
817 return false;
820 /* The normal form of an atom depends on the expression. The normal
821 form of a function call to a function concept is a check constraint
822 for that concept. The normal form of a reference to a variable
823 concept is a check constraint for that concept. Otherwise, the
824 constraint is a predicate constraint. */
826 tree
827 normalize_atom (tree t)
829 /* We can get constraints pushed down through pack expansions, so
830 just return them. */
831 if (constraint_p (t))
832 return t;
834 tree type = TREE_TYPE (t);
835 if (!type || type_unknown_p (t) || TREE_CODE (type) == TEMPLATE_TYPE_PARM)
837 else if (!dependent_type_p (type))
839 if (check_for_logical_overloads (t))
840 return error_mark_node;
842 type = cv_unqualified (type);
843 if (!same_type_p (type, boolean_type_node))
845 error ("predicate constraint %q+E does not have type %<bool%>", t);
846 return error_mark_node;
850 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
851 return normalize_template_id_expression (t);
852 if (TREE_CODE (t) == CALL_EXPR)
853 return normalize_call_expression (t);
854 return build_nt (PRED_CONSTR, t);
857 /* Push down the pack expansion EXP into the leaves of the constraint PAT. */
859 tree
860 push_down_pack_expansion (tree exp, tree pat)
862 switch (TREE_CODE (pat))
864 case CONJ_CONSTR:
865 case DISJ_CONSTR:
867 pat = copy_node (pat);
868 TREE_OPERAND (pat, 0)
869 = push_down_pack_expansion (exp, TREE_OPERAND (pat, 0));
870 TREE_OPERAND (pat, 1)
871 = push_down_pack_expansion (exp, TREE_OPERAND (pat, 1));
872 return pat;
874 default:
876 exp = copy_node (exp);
877 SET_PACK_EXPANSION_PATTERN (exp, pat);
878 return exp;
883 /* Transform a pack expansion into a constraint. First we transform the
884 pattern of the pack expansion, then we push the pack expansion down into the
885 leaves of the constraint so that partial ordering will work. */
887 tree
888 normalize_pack_expansion (tree t)
890 tree pat = normalize_expression (PACK_EXPANSION_PATTERN (t));
891 return push_down_pack_expansion (t, pat);
894 /* Transform an expression into a constraint. */
896 tree
897 normalize_any_expression (tree t)
899 switch (TREE_CODE (t))
901 case TRUTH_ANDIF_EXPR:
902 return normalize_logical_operation (t, CONJ_CONSTR);
904 case TRUTH_ORIF_EXPR:
905 return normalize_logical_operation (t, DISJ_CONSTR);
907 case REQUIRES_EXPR:
908 return normalize_requires_expression (t);
910 case BIND_EXPR:
911 return normalize_expression (BIND_EXPR_BODY (t));
913 case EXPR_PACK_EXPANSION:
914 return normalize_pack_expansion (t);
916 default:
917 /* All other constraints are atomic. */
918 return normalize_atom (t);
922 /* Transform a statement into an expression. */
923 tree
924 normalize_any_statement (tree t)
926 switch (TREE_CODE (t))
928 case RETURN_EXPR:
929 return normalize_expression (TREE_OPERAND (t, 0));
930 default:
931 gcc_unreachable ();
933 return error_mark_node;
936 /* Reduction rules for the declaration T. */
938 tree
939 normalize_any_declaration (tree t)
941 switch (TREE_CODE (t))
943 case VAR_DECL:
944 return normalize_atom (t);
945 default:
946 gcc_unreachable ();
948 return error_mark_node;
951 /* Returns the normal form of a constraint expression. */
953 tree
954 normalize_expression (tree t)
956 if (!t)
957 return NULL_TREE;
959 if (t == error_mark_node)
960 return error_mark_node;
962 switch (TREE_CODE_CLASS (TREE_CODE (t)))
964 case tcc_unary:
965 case tcc_binary:
966 case tcc_expression:
967 case tcc_vl_exp:
968 return normalize_any_expression (t);
970 case tcc_statement:
971 return normalize_any_statement (t);
973 case tcc_declaration:
974 return normalize_any_declaration (t);
976 case tcc_exceptional:
977 case tcc_constant:
978 case tcc_reference:
979 case tcc_comparison:
980 /* These are all atomic predicate constraints. */
981 return normalize_atom (t);
983 default:
984 /* Unhandled node kind. */
985 gcc_unreachable ();
987 return error_mark_node;
991 /*---------------------------------------------------------------------------
992 Constraint normalization
993 ---------------------------------------------------------------------------*/
995 tree normalize_constraint (tree);
997 /* The normal form of the disjunction T0 /\ T1 is the conjunction
998 of the normal form of T0 and the normal form of T1. */
1000 inline tree
1001 normalize_conjunction (tree t)
1003 tree t0 = normalize_constraint (TREE_OPERAND (t, 0));
1004 tree t1 = normalize_constraint (TREE_OPERAND (t, 1));
1005 return build_nt (CONJ_CONSTR, t0, t1);
1008 /* The normal form of the disjunction T0 \/ T1 is the disjunction
1009 of the normal form of T0 and the normal form of T1. */
1011 inline tree
1012 normalize_disjunction (tree t)
1014 tree t0 = normalize_constraint (TREE_OPERAND (t, 0));
1015 tree t1 = normalize_constraint (TREE_OPERAND (t, 1));
1016 return build_nt (DISJ_CONSTR, t0, t1);
1019 /* A predicate constraint is normalized in two stages. First all
1020 references specializations of concepts are replaced by their
1021 substituted definitions. Then, the resulting expression is
1022 transformed into a constraint by transforming && expressions
1023 into conjunctions and || into disjunctions. */
1025 tree
1026 normalize_predicate_constraint (tree t)
1028 ++processing_template_decl;
1029 tree expr = PRED_CONSTR_EXPR (t);
1030 tree constr = normalize_expression (expr);
1031 --processing_template_decl;
1032 return constr;
1035 /* The normal form of a parameterized constraint is the normal
1036 form of its operand. */
1038 tree
1039 normalize_parameterized_constraint (tree t)
1041 tree parms = PARM_CONSTR_PARMS (t);
1042 tree operand = normalize_constraint (PARM_CONSTR_OPERAND (t));
1043 return build_nt (PARM_CONSTR, parms, operand);
1046 /* Normalize the constraint T by reducing it so that it is
1047 comprised of only conjunctions and disjunctions of atomic
1048 constraints. */
1050 tree
1051 normalize_constraint (tree t)
1053 if (!t)
1054 return NULL_TREE;
1056 if (t == error_mark_node)
1057 return t;
1059 switch (TREE_CODE (t))
1061 case CONJ_CONSTR:
1062 return normalize_conjunction (t);
1064 case DISJ_CONSTR:
1065 return normalize_disjunction (t);
1067 case PRED_CONSTR:
1068 return normalize_predicate_constraint (t);
1070 case PARM_CONSTR:
1071 return normalize_parameterized_constraint (t);
1073 case EXPR_CONSTR:
1074 case TYPE_CONSTR:
1075 case ICONV_CONSTR:
1076 case DEDUCT_CONSTR:
1077 case EXCEPT_CONSTR:
1078 /* These constraints are defined to be atomic. */
1079 return t;
1081 default:
1082 /* CONSTR was not a constraint. */
1083 gcc_unreachable();
1085 return error_mark_node;
1090 // -------------------------------------------------------------------------- //
1091 // Constraint Semantic Processing
1093 // The following functions are called by the parser and substitution rules
1094 // to create and evaluate constraint-related nodes.
1096 // The constraints associated with the current template parameters.
1097 tree
1098 current_template_constraints (void)
1100 if (!current_template_parms)
1101 return NULL_TREE;
1102 tree tmpl_constr = TEMPLATE_PARM_CONSTRAINTS (current_template_parms);
1103 return build_constraints (tmpl_constr, NULL_TREE);
1106 // If the recently parsed TYPE declares or defines a template or template
1107 // specialization, get its corresponding constraints from the current
1108 // template parameters and bind them to TYPE's declaration.
1109 tree
1110 associate_classtype_constraints (tree type)
1112 if (!type || type == error_mark_node || TREE_CODE (type) != RECORD_TYPE)
1113 return type;
1115 // An explicit class template specialization has no template
1116 // parameters.
1117 if (!current_template_parms)
1118 return type;
1120 if (CLASSTYPE_IS_TEMPLATE (type) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1122 tree decl = TYPE_STUB_DECL (type);
1123 tree ci = current_template_constraints ();
1125 // An implicitly instantiated member template declaration already
1126 // has associated constraints. If it is defined outside of its
1127 // class, then we need match these constraints against those of
1128 // original declaration.
1129 if (tree orig_ci = get_constraints (decl))
1131 if (!equivalent_constraints (ci, orig_ci))
1133 // FIXME: Improve diagnostics.
1134 error ("%qT does not match any declaration", type);
1135 return error_mark_node;
1137 return type;
1139 set_constraints (decl, ci);
1141 return type;
1144 namespace {
1146 // Create an empty constraint info block.
1147 inline tree_constraint_info*
1148 build_constraint_info ()
1150 return (tree_constraint_info *)make_node (CONSTRAINT_INFO);
1153 } // namespace
1155 /* Build a constraint-info object that contains the associated constraints
1156 of a declaration. This also includes the declaration's template
1157 requirements (TREQS) and any trailing requirements for a function
1158 declarator (DREQS). Note that both TREQS and DREQS must be constraints.
1160 If the declaration has neither template nor declaration requirements
1161 this returns NULL_TREE, indicating an unconstrained declaration. */
1163 tree
1164 build_constraints (tree tmpl_reqs, tree decl_reqs)
1166 gcc_assert (tmpl_reqs ? constraint_p (tmpl_reqs) : true);
1167 gcc_assert (decl_reqs ? constraint_p (decl_reqs) : true);
1169 if (!tmpl_reqs && !decl_reqs)
1170 return NULL_TREE;
1172 tree_constraint_info* ci = build_constraint_info ();
1173 ci->template_reqs = tmpl_reqs;
1174 ci->declarator_reqs = decl_reqs;
1175 ci->associated_constr = conjoin_constraints (tmpl_reqs, decl_reqs);
1177 return (tree)ci;
1180 namespace {
1182 /* Construct a sequence of template arguments by prepending
1183 ARG to REST. Either ARG or REST may be null. */
1184 tree
1185 build_concept_check_arguments (tree arg, tree rest)
1187 gcc_assert (rest ? TREE_CODE (rest) == TREE_VEC : true);
1188 tree args;
1189 if (arg)
1191 int n = rest ? TREE_VEC_LENGTH (rest) : 0;
1192 args = make_tree_vec (n + 1);
1193 TREE_VEC_ELT (args, 0) = arg;
1194 if (rest)
1195 for (int i = 0; i < n; ++i)
1196 TREE_VEC_ELT (args, i + 1) = TREE_VEC_ELT (rest, i);
1197 int def = rest ? GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (rest) : 0;
1198 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, def + 1);
1200 else
1202 gcc_assert (rest != NULL_TREE);
1203 args = rest;
1205 return args;
1208 } // namespace
1210 /* Construct an expression that checks the concept given by
1211 TARGET. The TARGET must be:
1213 - an OVERLOAD referring to one or more function concepts
1214 - a BASELINK referring to an overload set of the above, or
1215 - a TEMPLTATE_DECL referring to a variable concept.
1217 ARG and REST are the explicit template arguments for the
1218 eventual concept check. */
1219 tree
1220 build_concept_check (tree target, tree arg, tree rest)
1222 tree args = build_concept_check_arguments (arg, rest);
1223 if (variable_template_p (target))
1224 return build_variable_check (lookup_template_variable (target, args));
1225 else
1226 return build_call_check (lookup_template_function (target, args));
1230 /* Returns a TYPE_DECL that contains sufficient information to
1231 build a template parameter of the same kind as PROTO and
1232 constrained by the concept declaration CNC. Note that PROTO
1233 is the first template parameter of CNC.
1235 If specified, ARGS provides additional arguments to the
1236 constraint check. */
1237 tree
1238 build_constrained_parameter (tree cnc, tree proto, tree args)
1240 tree name = DECL_NAME (cnc);
1241 tree type = TREE_TYPE (proto);
1242 tree decl = build_decl (input_location, TYPE_DECL, name, type);
1243 CONSTRAINED_PARM_PROTOTYPE (decl) = proto;
1244 CONSTRAINED_PARM_CONCEPT (decl) = cnc;
1245 CONSTRAINED_PARM_EXTRA_ARGS (decl) = args;
1246 return decl;
1249 /* Create a constraint expression for the given DECL that
1250 evaluates the requirements specified by CONSTR, a TYPE_DECL
1251 that contains all the information necessary to build the
1252 requirements (see finish_concept_name for the layout of
1253 that TYPE_DECL).
1255 Note that the constraints are neither reduced nor decomposed.
1256 That is done only after the requires clause has been parsed
1257 (or not).
1259 This will always return a CHECK_CONSTR. */
1260 tree
1261 finish_shorthand_constraint (tree decl, tree constr)
1263 /* No requirements means no constraints. */
1264 if (!constr)
1265 return NULL_TREE;
1267 tree proto = CONSTRAINED_PARM_PROTOTYPE (constr);
1268 tree con = CONSTRAINED_PARM_CONCEPT (constr);
1269 tree args = CONSTRAINED_PARM_EXTRA_ARGS (constr);
1271 /* If the parameter declaration is variadic, but the concept
1272 is not then we need to apply the concept to every element
1273 in the pack. */
1274 bool is_proto_pack = template_parameter_pack_p (proto);
1275 bool is_decl_pack = template_parameter_pack_p (decl);
1276 bool apply_to_all_p = is_decl_pack && !is_proto_pack;
1278 /* Get the argument and overload used for the requirement
1279 and adjust it if we're going to expand later. */
1280 tree arg = template_parm_to_arg (build_tree_list (NULL_TREE, decl));
1281 if (apply_to_all_p)
1282 arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
1284 /* Build the concept check. If it the constraint needs to be
1285 applied to all elements of the parameter pack, then make
1286 the constraint an expansion. */
1287 tree check;
1288 tree tmpl = DECL_TI_TEMPLATE (con);
1289 if (VAR_P (con))
1290 check = build_concept_check (tmpl, arg, args);
1291 else
1293 tree ovl = build_overload (tmpl, NULL_TREE);
1294 check = build_concept_check (ovl, arg, args);
1297 /* Make the check a pack expansion if needed.
1299 FIXME: We should be making a fold expression. */
1300 if (apply_to_all_p)
1302 check = make_pack_expansion (check);
1303 TREE_TYPE (check) = boolean_type_node;
1306 return normalize_expression (check);
1309 /* Returns a conjunction of shorthand requirements for the template
1310 parameter list PARMS. Note that the requirements are stored in
1311 the TYPE of each tree node. */
1312 tree
1313 get_shorthand_constraints (tree parms)
1315 tree result = NULL_TREE;
1316 parms = INNERMOST_TEMPLATE_PARMS (parms);
1317 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
1319 tree parm = TREE_VEC_ELT (parms, i);
1320 tree constr = TEMPLATE_PARM_CONSTRAINTS (parm);
1321 result = conjoin_constraints (result, constr);
1323 return result;
1326 // Returns and chains a new parameter for PARAMETER_LIST which will conform
1327 // to the prototype given by SRC_PARM. The new parameter will have its
1328 // identifier and location set according to IDENT and PARM_LOC respectively.
1329 static tree
1330 process_introduction_parm (tree parameter_list, tree src_parm)
1332 // If we have a pack, we should have a single pack argument which is the
1333 // placeholder we want to look at.
1334 bool is_parameter_pack = ARGUMENT_PACK_P (src_parm);
1335 if (is_parameter_pack)
1336 src_parm = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (src_parm), 0);
1338 // At this point we should have a wildcard, but we want to
1339 // grab the associated decl from it. Also grab the stored
1340 // identifier and location that should be chained to it in
1341 // a PARM_DECL.
1342 gcc_assert (TREE_CODE (src_parm) == WILDCARD_DECL);
1344 tree ident = DECL_NAME (src_parm);
1345 location_t parm_loc = DECL_SOURCE_LOCATION (src_parm);
1347 // If we expect a pack and the deduced template is not a pack, or if the
1348 // template is using a pack and we didn't declare a pack, throw an error.
1349 if (is_parameter_pack != WILDCARD_PACK_P (src_parm))
1351 error_at (parm_loc, "cannot match pack for introduced parameter");
1352 tree err_parm = build_tree_list (error_mark_node, error_mark_node);
1353 return chainon (parameter_list, err_parm);
1356 src_parm = TREE_TYPE (src_parm);
1358 tree parm;
1359 bool is_non_type;
1360 if (TREE_CODE (src_parm) == TYPE_DECL)
1362 is_non_type = false;
1363 parm = finish_template_type_parm (class_type_node, ident);
1365 else if (TREE_CODE (src_parm) == TEMPLATE_DECL)
1367 is_non_type = false;
1368 begin_template_parm_list ();
1369 current_template_parms = DECL_TEMPLATE_PARMS (src_parm);
1370 end_template_parm_list ();
1371 parm = finish_template_template_parm (class_type_node, ident);
1373 else
1375 is_non_type = true;
1377 // Since we don't have a declarator, so we can copy the source
1378 // parameter and change the name and eventually the location.
1379 parm = copy_decl (src_parm);
1380 DECL_NAME (parm) = ident;
1383 // Wrap in a TREE_LIST for process_template_parm. Introductions do not
1384 // retain the defaults from the source template.
1385 parm = build_tree_list (NULL_TREE, parm);
1387 return process_template_parm (parameter_list, parm_loc, parm,
1388 is_non_type, is_parameter_pack);
1391 /* Associates a constraint check to the current template based
1392 on the introduction parameters. INTRO_LIST must be a TREE_VEC
1393 of WILDCARD_DECLs containing a chained PARM_DECL which
1394 contains the identifier as well as the source location.
1395 TMPL_DECL is the decl for the concept being used. If we
1396 take a concept, C, this will form a check in the form of
1397 C<INTRO_LIST> filling in any extra arguments needed by the
1398 defaults deduced.
1400 Returns NULL_TREE if no concept could be matched and
1401 error_mark_node if an error occurred when matching. */
1402 tree
1403 finish_template_introduction (tree tmpl_decl, tree intro_list)
1405 /* Deduce the concept check. */
1406 tree expr = build_concept_check (tmpl_decl, NULL_TREE, intro_list);
1407 if (expr == error_mark_node)
1408 return NULL_TREE;
1410 tree parms = deduce_concept_introduction (expr);
1411 if (!parms)
1412 return NULL_TREE;
1414 /* Build template parameter scope for introduction. */
1415 tree parm_list = NULL_TREE;
1416 begin_template_parm_list ();
1417 int nargs = MIN (TREE_VEC_LENGTH (parms), TREE_VEC_LENGTH (intro_list));
1418 for (int n = 0; n < nargs; ++n)
1419 parm_list = process_introduction_parm (parm_list, TREE_VEC_ELT (parms, n));
1420 parm_list = end_template_parm_list (parm_list);
1421 for (int i = 0; i < TREE_VEC_LENGTH (parm_list); ++i)
1422 if (TREE_VALUE (TREE_VEC_ELT (parm_list, i)) == error_mark_node)
1424 end_template_decl ();
1425 return error_mark_node;
1428 /* Build a concept check for our constraint. */
1429 tree check_args = make_tree_vec (TREE_VEC_LENGTH (parms));
1430 int n = 0;
1431 for (; n < TREE_VEC_LENGTH (parm_list); ++n)
1433 tree parm = TREE_VEC_ELT (parm_list, n);
1434 TREE_VEC_ELT (check_args, n) = template_parm_to_arg (parm);
1436 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (check_args, n);
1438 /* If the template expects more parameters we should be able
1439 to use the defaults from our deduced concept. */
1440 for (; n < TREE_VEC_LENGTH (parms); ++n)
1441 TREE_VEC_ELT (check_args, n) = TREE_VEC_ELT (parms, n);
1443 /* Associate the constraint. */
1444 tree check = build_concept_check (tmpl_decl, NULL_TREE, check_args);
1445 tree constr = normalize_expression (check);
1446 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = constr;
1448 return parm_list;
1452 /* Given the predicate constraint T from a constrained-type-specifier, extract
1453 its TMPL and ARGS. FIXME why do we need two different forms of
1454 constrained-type-specifier? */
1456 void
1457 placeholder_extract_concept_and_args (tree t, tree &tmpl, tree &args)
1459 if (TREE_CODE (t) == TYPE_DECL)
1461 /* A constrained parameter. Build a constraint check
1462 based on the prototype parameter and then extract the
1463 arguments from that. */
1464 tree proto = CONSTRAINED_PARM_PROTOTYPE (t);
1465 tree check = finish_shorthand_constraint (proto, t);
1466 placeholder_extract_concept_and_args (check, tmpl, args);
1467 return;
1470 if (TREE_CODE (t) == CHECK_CONSTR)
1472 tree decl = CHECK_CONSTR_CONCEPT (t);
1473 tmpl = DECL_TI_TEMPLATE (decl);
1474 args = CHECK_CONSTR_ARGS (t);
1475 return;
1478 gcc_unreachable ();
1481 /* Returns true iff the placeholders C1 and C2 are equivalent. C1
1482 and C2 can be either CHECK_CONSTR or TEMPLATE_TYPE_PARM. */
1484 bool
1485 equivalent_placeholder_constraints (tree c1, tree c2)
1487 if (c1 && TREE_CODE (c1) == TEMPLATE_TYPE_PARM)
1488 /* A constrained auto. */
1489 c1 = PLACEHOLDER_TYPE_CONSTRAINTS (c1);
1490 if (c2 && TREE_CODE (c2) == TEMPLATE_TYPE_PARM)
1491 c2 = PLACEHOLDER_TYPE_CONSTRAINTS (c2);
1493 if (c1 == c2)
1494 return true;
1495 if (!c1 || !c2)
1496 return false;
1497 if (c1 == error_mark_node || c2 == error_mark_node)
1498 /* We get here during satisfaction; when a deduction constraint
1499 fails, substitution can produce an error_mark_node for the
1500 placeholder constraints. */
1501 return false;
1503 tree t1, t2, a1, a2;
1504 placeholder_extract_concept_and_args (c1, t1, a1);
1505 placeholder_extract_concept_and_args (c2, t2, a2);
1507 if (t1 != t2)
1508 return false;
1510 int len1 = TREE_VEC_LENGTH (a1);
1511 int len2 = TREE_VEC_LENGTH (a2);
1512 if (len1 != len2)
1513 return false;
1515 /* Skip the first argument so we don't infinitely recurse.
1516 Also, they may differ in template parameter index. */
1517 for (int i = 1; i < len1; ++i)
1519 tree t1 = TREE_VEC_ELT (a1, i);
1520 tree t2 = TREE_VEC_ELT (a2, i);
1521 if (!template_args_equal (t1, t2))
1522 return false;
1524 return true;
1527 /* Return a hash value for the placeholder PRED_CONSTR C. */
1529 hashval_t
1530 hash_placeholder_constraint (tree c)
1532 tree t, a;
1533 placeholder_extract_concept_and_args (c, t, a);
1535 /* Like hash_tmpl_and_args, but skip the first argument. */
1536 hashval_t val = iterative_hash_object (DECL_UID (t), 0);
1538 for (int i = TREE_VEC_LENGTH (a)-1; i > 0; --i)
1539 val = iterative_hash_template_arg (TREE_VEC_ELT (a, i), val);
1541 return val;
1544 /*---------------------------------------------------------------------------
1545 Constraint substitution
1546 ---------------------------------------------------------------------------*/
1548 /* The following functions implement substitution rules for constraints.
1549 Substitution without checking constraints happens only in the
1550 instantiation of class templates. For example:
1552 template<C1 T> struct S {
1553 void f(T) requires C2<T>;
1554 void g(T) requires T::value;
1557 S<int> s; // error instantiating S<int>::g(T)
1559 When we instantiate S, we substitute into its member declarations,
1560 including their constraints. However, those constraints are not
1561 checked. Substituting int into C2<T> yields C2<int>, and substituting
1562 into T::value yields a substitution failure, making the program
1563 ill-formed.
1565 Note that we only ever substitute into the associated constraints
1566 of a declaration. That is, substitution is defined only for predicate
1567 constraints and conjunctions. */
1569 /* Substitute into the predicate constraints. Returns error_mark_node
1570 if the substitution into the expression fails. */
1571 tree
1572 tsubst_predicate_constraint (tree t, tree args,
1573 tsubst_flags_t complain, tree in_decl)
1575 tree expr = PRED_CONSTR_EXPR (t);
1576 ++processing_template_decl;
1577 tree result = tsubst_expr (expr, args, complain, in_decl, false);
1578 --processing_template_decl;
1579 return build_nt (PRED_CONSTR, result);
1582 /* Substitute into a check constraint. */
1584 tree
1585 tsubst_check_constraint (tree t, tree args,
1586 tsubst_flags_t complain, tree in_decl)
1588 tree decl = CHECK_CONSTR_CONCEPT (t);
1589 tree tmpl = DECL_TI_TEMPLATE (decl);
1590 tree targs = CHECK_CONSTR_ARGS (t);
1592 /* Substitute through by building an template-id expression
1593 and then substituting into that. */
1594 tree expr = build_nt(TEMPLATE_ID_EXPR, tmpl, targs);
1595 ++processing_template_decl;
1596 tree result = tsubst_expr (expr, args, complain, in_decl, false);
1597 --processing_template_decl;
1599 if (result == error_mark_node)
1600 return error_mark_node;
1602 /* Extract the results and rebuild the check constraint. */
1603 decl = DECL_TEMPLATE_RESULT (TREE_OPERAND (result, 0));
1604 args = TREE_OPERAND (result, 1);
1606 return build_nt (CHECK_CONSTR, decl, args);
1609 /* Substitute into the conjunction of constraints. Returns
1610 error_mark_node if substitution into either operand fails. */
1612 tree
1613 tsubst_logical_operator (tree t, tree args,
1614 tsubst_flags_t complain, tree in_decl)
1616 tree t0 = TREE_OPERAND (t, 0);
1617 tree r0 = tsubst_constraint (t0, args, complain, in_decl);
1618 if (r0 == error_mark_node)
1619 return error_mark_node;
1620 tree t1 = TREE_OPERAND (t, 1);
1621 tree r1 = tsubst_constraint (t1, args, complain, in_decl);
1622 if (r1 == error_mark_node)
1623 return error_mark_node;
1624 return build_nt (TREE_CODE (t), r0, r1);
1627 namespace {
1629 /* Substitute ARGS into the expression constraint T. */
1631 tree
1632 tsubst_expr_constr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1634 cp_unevaluated guard;
1635 tree expr = EXPR_CONSTR_EXPR (t);
1636 tree ret = tsubst_expr (expr, args, complain, in_decl, false);
1637 if (ret == error_mark_node)
1638 return error_mark_node;
1639 return build_nt (EXPR_CONSTR, ret);
1642 /* Substitute ARGS into the type constraint T. */
1644 tree
1645 tsubst_type_constr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1647 tree type = TYPE_CONSTR_TYPE (t);
1648 tree ret = tsubst (type, args, complain, in_decl);
1649 if (ret == error_mark_node)
1650 return error_mark_node;
1651 return build_nt (TYPE_CONSTR, ret);
1654 /* Substitute ARGS into the implicit conversion constraint T. */
1656 tree
1657 tsubst_implicit_conversion_constr (tree t, tree args, tsubst_flags_t complain,
1658 tree in_decl)
1660 cp_unevaluated guard;
1661 tree expr = ICONV_CONSTR_EXPR (t);
1662 tree type = ICONV_CONSTR_TYPE (t);
1663 tree new_expr = tsubst_expr (expr, args, complain, in_decl, false);
1664 if (new_expr == error_mark_node)
1665 return error_mark_node;
1666 tree new_type = tsubst (type, args, complain, in_decl);
1667 if (new_type == error_mark_node)
1668 return error_mark_node;
1669 return build_nt (ICONV_CONSTR, new_expr, new_type);
1672 /* Substitute ARGS into the argument deduction constraint T. */
1674 tree
1675 tsubst_argument_deduction_constr (tree t, tree args, tsubst_flags_t complain,
1676 tree in_decl)
1678 cp_unevaluated guard;
1679 tree expr = DEDUCT_CONSTR_EXPR (t);
1680 tree pattern = DEDUCT_CONSTR_PATTERN (t);
1681 tree autos = DEDUCT_CONSTR_PLACEHOLDER(t);
1682 tree new_expr = tsubst_expr (expr, args, complain, in_decl, false);
1683 if (new_expr == error_mark_node)
1684 return error_mark_node;
1685 /* It seems like substituting through the pattern will not affect the
1686 placeholders. We should (?) be able to reuse the existing list
1687 without any problems. If not, then we probably want to create a
1688 new list of placeholders and then instantiate the pattern using
1689 those. */
1690 tree new_pattern = tsubst (pattern, args, complain, in_decl);
1691 if (new_pattern == error_mark_node)
1692 return error_mark_node;
1693 return build_nt (DEDUCT_CONSTR, new_expr, new_pattern, autos);
1696 /* Substitute ARGS into the exception constraint T. */
1698 tree
1699 tsubst_exception_constr (tree t, tree args, tsubst_flags_t complain,
1700 tree in_decl)
1702 cp_unevaluated guard;
1703 tree expr = EXCEPT_CONSTR_EXPR (t);
1704 tree ret = tsubst_expr (expr, args, complain, in_decl, false);
1705 if (ret == error_mark_node)
1706 return error_mark_node;
1707 return build_nt (EXCEPT_CONSTR, ret);
1710 /* A subroutine of tsubst_constraint_variables. Register local
1711 specializations for each of parameter in PARMS and its
1712 corresponding substituted constraint variable in VARS.
1713 Returns VARS. */
1715 tree
1716 declare_constraint_vars (tree parms, tree vars)
1718 tree s = vars;
1719 for (tree t = parms; t; t = DECL_CHAIN (t))
1721 if (DECL_PACK_P (t))
1723 tree pack = extract_fnparm_pack (t, &s);
1724 register_local_specialization (pack, t);
1726 else
1728 register_local_specialization (s, t);
1729 s = DECL_CHAIN (s);
1732 return vars;
1735 /* A subroutine of tsubst_parameterized_constraint. Substitute ARGS
1736 into the parameter list T, producing a sequence of constraint
1737 variables, declared in the current scope.
1739 Note that the caller must establish a local specialization stack
1740 prior to calling this function since this substitution will
1741 declare the substituted parameters. */
1743 tree
1744 tsubst_constraint_variables (tree t, tree args,
1745 tsubst_flags_t complain, tree in_decl)
1747 /* Clear cp_unevaluated_operand across tsubst so that we get a proper chain
1748 of PARM_DECLs. */
1749 int saved_unevaluated_operand = cp_unevaluated_operand;
1750 cp_unevaluated_operand = 0;
1751 tree vars = tsubst (t, args, complain, in_decl);
1752 cp_unevaluated_operand = saved_unevaluated_operand;
1753 if (vars == error_mark_node)
1754 return error_mark_node;
1755 return declare_constraint_vars (t, vars);
1758 /* Substitute ARGS into the parameterized constraint T. */
1760 tree
1761 tsubst_parameterized_constraint (tree t, tree args,
1762 tsubst_flags_t complain, tree in_decl)
1764 local_specialization_stack stack;
1765 tree vars = tsubst_constraint_variables (PARM_CONSTR_PARMS (t),
1766 args, complain, in_decl);
1767 if (vars == error_mark_node)
1768 return error_mark_node;
1769 tree expr = tsubst_constraint (PARM_CONSTR_OPERAND (t), args,
1770 complain, in_decl);
1771 if (expr == error_mark_node)
1772 return error_mark_node;
1773 return build_nt (PARM_CONSTR, vars, expr);
1776 /* Substitute ARGS into the simple requirement T. Note that
1777 substitution may result in an ill-formed expression without
1778 causing the program to be ill-formed. In such cases, the
1779 requirement wraps an error_mark_node. */
1781 inline tree
1782 tsubst_simple_requirement (tree t, tree args,
1783 tsubst_flags_t complain, tree in_decl)
1785 ++processing_template_decl;
1786 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1787 --processing_template_decl;
1788 return finish_simple_requirement (expr);
1791 /* Substitute ARGS into the type requirement T. Note that
1792 substitution may result in an ill-formed type without
1793 causing the program to be ill-formed. In such cases, the
1794 requirement wraps an error_mark_node. */
1796 inline tree
1797 tsubst_type_requirement (tree t, tree args,
1798 tsubst_flags_t complain, tree in_decl)
1800 ++processing_template_decl;
1801 tree type = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
1802 --processing_template_decl;
1803 return finish_type_requirement (type);
1806 /* Substitute args into the compound requirement T. If substituting
1807 into either the expression or the type fails, the corresponding
1808 operands in the resulting node will be error_mark_node. This
1809 preserves a requirement for the purpose of partial ordering, but
1810 it will never be satisfied. */
1812 tree
1813 tsubst_compound_requirement (tree t, tree args,
1814 tsubst_flags_t complain, tree in_decl)
1816 ++processing_template_decl;
1817 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1818 tree type = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
1819 --processing_template_decl;
1820 bool noexcept_p = COMPOUND_REQ_NOEXCEPT_P (t);
1821 return finish_compound_requirement (expr, type, noexcept_p);
1824 /* Substitute ARGS into the nested requirement T. */
1826 tree
1827 tsubst_nested_requirement (tree t, tree args,
1828 tsubst_flags_t complain, tree in_decl)
1830 ++processing_template_decl;
1831 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1832 --processing_template_decl;
1833 return finish_nested_requirement (expr);
1836 /* Substitute ARGS into the requirement T. */
1838 inline tree
1839 tsubst_requirement (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1841 switch (TREE_CODE (t))
1843 case SIMPLE_REQ:
1844 return tsubst_simple_requirement (t, args, complain, in_decl);
1845 case TYPE_REQ:
1846 return tsubst_type_requirement (t, args, complain, in_decl);
1847 case COMPOUND_REQ:
1848 return tsubst_compound_requirement (t, args, complain, in_decl);
1849 case NESTED_REQ:
1850 return tsubst_nested_requirement (t, args, complain, in_decl);
1851 default:
1852 gcc_unreachable ();
1854 return error_mark_node;
1857 /* Substitute ARGS into the list of requirements T. Note that
1858 substitution failures here result in ill-formed programs. */
1860 tree
1861 tsubst_requirement_body (tree t, tree args,
1862 tsubst_flags_t complain, tree in_decl)
1864 tree r = NULL_TREE;
1865 while (t)
1867 tree e = tsubst_requirement (TREE_VALUE (t), args, complain, in_decl);
1868 if (e == error_mark_node)
1869 return error_mark_node;
1870 r = tree_cons (NULL_TREE, e, r);
1871 t = TREE_CHAIN (t);
1873 /* Ensure that the order of constraints is the same as the original. */
1874 return nreverse (r);
1877 } /* namespace */
1879 /* Substitute ARGS into the requires expression T. Note that this
1880 results in the re-declaration of local parameters when
1881 substituting through the parameter list. If either substitution
1882 fails, the program is ill-formed. */
1884 tree
1885 tsubst_requires_expr (tree t, tree args,
1886 tsubst_flags_t complain, tree in_decl)
1888 local_specialization_stack stack;
1890 tree parms = TREE_OPERAND (t, 0);
1891 if (parms)
1893 parms = tsubst_constraint_variables (parms, args, complain, in_decl);
1894 if (parms == error_mark_node)
1895 return error_mark_node;
1898 tree reqs = TREE_OPERAND (t, 1);
1899 reqs = tsubst_requirement_body (reqs, args, complain, in_decl);
1900 if (reqs == error_mark_node)
1901 return error_mark_node;
1903 return finish_requires_expr (parms, reqs);
1906 /* Substitute ARGS into the constraint information CI, producing a new
1907 constraint record. */
1909 tree
1910 tsubst_constraint_info (tree t, tree args,
1911 tsubst_flags_t complain, tree in_decl)
1913 if (!t || t == error_mark_node || !check_constraint_info (t))
1914 return NULL_TREE;
1916 tree tmpl_constr = NULL_TREE;
1917 if (tree r = CI_TEMPLATE_REQS (t))
1918 tmpl_constr = tsubst_constraint (r, args, complain, in_decl);
1920 tree decl_constr = NULL_TREE;
1921 if (tree r = CI_DECLARATOR_REQS (t))
1922 decl_constr = tsubst_constraint (r, args, complain, in_decl);
1924 return build_constraints (tmpl_constr, decl_constr);
1927 /* Substitute ARGS into the constraint T. */
1929 tree
1930 tsubst_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1932 if (t == NULL_TREE)
1933 return t;
1934 switch (TREE_CODE (t))
1936 case PRED_CONSTR:
1937 return tsubst_predicate_constraint (t, args, complain, in_decl);
1938 case CHECK_CONSTR:
1939 return tsubst_check_constraint (t, args, complain, in_decl);
1940 case CONJ_CONSTR:
1941 case DISJ_CONSTR:
1942 return tsubst_logical_operator (t, args, complain, in_decl);
1943 case PARM_CONSTR:
1944 return tsubst_parameterized_constraint (t, args, complain, in_decl);
1945 case EXPR_CONSTR:
1946 return tsubst_expr_constr (t, args, complain, in_decl);
1947 case TYPE_CONSTR:
1948 return tsubst_type_constr (t, args, complain, in_decl);
1949 case ICONV_CONSTR:
1950 return tsubst_implicit_conversion_constr (t, args, complain, in_decl);
1951 case DEDUCT_CONSTR:
1952 return tsubst_argument_deduction_constr (t, args, complain, in_decl);
1953 case EXCEPT_CONSTR:
1954 return tsubst_exception_constr (t, args, complain, in_decl);
1955 default:
1956 gcc_unreachable ();
1958 return error_mark_node;
1961 /*---------------------------------------------------------------------------
1962 Constraint satisfaction
1963 ---------------------------------------------------------------------------*/
1965 /* The following functions determine if a constraint, when
1966 substituting template arguments, is satisfied. For convenience,
1967 satisfaction reduces a constraint to either true or false (and
1968 nothing else). */
1970 namespace {
1972 tree satisfy_constraint_1 (tree, tree, tsubst_flags_t, tree);
1974 /* Check the constraint pack expansion. */
1976 tree
1977 satisfy_pack_expansion (tree t, tree args,
1978 tsubst_flags_t complain, tree in_decl)
1980 /* Get the vector of satisfaction results.
1981 gen_elem_of_pack_expansion_instantiation will check that each element of
1982 the expansion is satisfied. */
1983 tree exprs = tsubst_pack_expansion (t, args, complain, in_decl);
1985 if (exprs == error_mark_node)
1986 return boolean_false_node;
1988 /* TODO: It might be better to normalize each expanded term
1989 and evaluate them separately. That would provide better
1990 opportunities for diagnostics. */
1991 for (int i = 0; i < TREE_VEC_LENGTH (exprs); ++i)
1992 if (TREE_VEC_ELT (exprs, i) != boolean_true_node)
1993 return boolean_false_node;
1994 return boolean_true_node;
1997 /* A predicate constraint is satisfied if its expression evaluates
1998 to true. If substitution into that node fails, the constraint
1999 is not satisfied ([temp.constr.pred]).
2001 Note that a predicate constraint is a constraint expression
2002 of type bool. If neither of those are true, the program is
2003 ill-formed; they are not SFINAE'able errors. */
2005 tree
2006 satisfy_predicate_constraint (tree t, tree args,
2007 tsubst_flags_t complain, tree in_decl)
2009 tree expr = TREE_OPERAND (t, 0);
2011 /* We should never have a naked pack expansion in a predicate constraint. */
2012 gcc_assert (TREE_CODE (expr) != EXPR_PACK_EXPANSION);
2014 /* If substitution into the expression fails, the constraint
2015 is not satisfied. */
2016 expr = tsubst_expr (expr, args, complain, in_decl, false);
2017 if (expr == error_mark_node)
2018 return boolean_false_node;
2020 /* A predicate constraint shall have type bool. In some
2021 cases, substitution gives us const-qualified bool, which
2022 is also acceptable. */
2023 tree type = cv_unqualified (TREE_TYPE (expr));
2024 if (!same_type_p (type, boolean_type_node))
2026 error_at (EXPR_LOC_OR_LOC (expr, input_location),
2027 "constraint %qE does not have type %qT",
2028 expr, boolean_type_node);
2029 return boolean_false_node;
2032 return cxx_constant_value (expr);
2035 /* A concept check constraint like C<CARGS> is satisfied if substituting ARGS
2036 into CARGS succeeds and C is satisfied for the resulting arguments. */
2038 tree
2039 satisfy_check_constraint (tree t, tree args,
2040 tsubst_flags_t complain, tree in_decl)
2042 tree decl = CHECK_CONSTR_CONCEPT (t);
2043 tree tmpl = DECL_TI_TEMPLATE (decl);
2044 tree cargs = CHECK_CONSTR_ARGS (t);
2046 /* Instantiate the concept check arguments. */
2047 tree targs = tsubst (cargs, args, tf_none, NULL_TREE);
2048 if (targs == error_mark_node)
2049 return boolean_false_node;
2051 /* Search for a previous value. */
2052 if (tree prev = lookup_concept_satisfaction (tmpl, targs))
2053 return prev;
2055 /* Expand the concept; failure here implies non-satisfaction. */
2056 tree def = expand_concept (decl, targs);
2057 if (def == error_mark_node)
2058 return memoize_concept_satisfaction (tmpl, args, boolean_false_node);
2060 /* Recursively satisfy the constraint. */
2061 tree result = satisfy_constraint_1 (def, targs, complain, in_decl);
2062 return memoize_concept_satisfaction (tmpl, targs, result);
2065 /* Check an expression constraint. The constraint is satisfied if
2066 substitution succeeds ([temp.constr.expr]).
2068 Note that the expression is unevaluated. */
2070 tree
2071 satisfy_expression_constraint (tree t, tree args,
2072 tsubst_flags_t complain, tree in_decl)
2074 cp_unevaluated guard;
2075 deferring_access_check_sentinel deferring;
2077 tree expr = EXPR_CONSTR_EXPR (t);
2078 tree check = tsubst_expr (expr, args, complain, in_decl, false);
2079 if (check == error_mark_node)
2080 return boolean_false_node;
2081 if (!perform_deferred_access_checks (tf_none))
2082 return boolean_false_node;
2083 return boolean_true_node;
2086 /* Check a type constraint. The constraint is satisfied if
2087 substitution succeeds. */
2089 inline tree
2090 satisfy_type_constraint (tree t, tree args,
2091 tsubst_flags_t complain, tree in_decl)
2093 deferring_access_check_sentinel deferring;
2094 tree type = TYPE_CONSTR_TYPE (t);
2095 gcc_assert (TYPE_P (type) || type == error_mark_node);
2096 tree check = tsubst (type, args, complain, in_decl);
2097 if (error_operand_p (check))
2098 return boolean_false_node;
2099 if (!perform_deferred_access_checks (complain))
2100 return boolean_false_node;
2101 return boolean_true_node;
2104 /* Check an implicit conversion constraint. */
2106 tree
2107 satisfy_implicit_conversion_constraint (tree t, tree args,
2108 tsubst_flags_t complain, tree in_decl)
2110 /* Don't tsubst as if we're processing a template. If we try
2111 to we can end up generating template-like expressions
2112 (e.g., modop-exprs) that aren't properly typed. */
2113 tree expr =
2114 tsubst_expr (ICONV_CONSTR_EXPR (t), args, complain, in_decl, false);
2115 if (expr == error_mark_node)
2116 return boolean_false_node;
2118 /* Get the transformed target type. */
2119 tree type = tsubst (ICONV_CONSTR_TYPE (t), args, complain, in_decl);
2120 if (type == error_mark_node)
2121 return boolean_false_node;
2123 /* Attempt the conversion as a direct initialization
2124 of the form TYPE <unspecified> = EXPR. */
2125 tree conv =
2126 perform_direct_initialization_if_possible (type, expr, false, complain);
2127 if (conv == NULL_TREE || conv == error_mark_node)
2128 return boolean_false_node;
2129 else
2130 return boolean_true_node;
2133 /* Check an argument deduction constraint. */
2135 tree
2136 satisfy_argument_deduction_constraint (tree t, tree args,
2137 tsubst_flags_t complain, tree in_decl)
2139 /* Substitute through the expression. */
2140 tree expr = DEDUCT_CONSTR_EXPR (t);
2141 tree init = tsubst_expr (expr, args, complain, in_decl, false);
2142 if (expr == error_mark_node)
2143 return boolean_false_node;
2145 /* Perform auto or decltype(auto) deduction to get the result. */
2146 tree pattern = DEDUCT_CONSTR_PATTERN (t);
2147 tree placeholder = DEDUCT_CONSTR_PLACEHOLDER (t);
2148 tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (placeholder);
2149 tree type_canonical = TYPE_CANONICAL (placeholder);
2150 PLACEHOLDER_TYPE_CONSTRAINTS (placeholder)
2151 = tsubst_constraint (constr, args, complain|tf_partial, in_decl);
2152 TYPE_CANONICAL (placeholder) = NULL_TREE;
2153 tree type = do_auto_deduction (pattern, init, placeholder,
2154 complain, adc_requirement);
2155 PLACEHOLDER_TYPE_CONSTRAINTS (placeholder) = constr;
2156 TYPE_CANONICAL (placeholder) = type_canonical;
2157 if (type == error_mark_node)
2158 return boolean_false_node;
2160 return boolean_true_node;
2163 /* Check an exception constraint. An exception constraint for an
2164 expression e is satisfied when noexcept(e) is true. */
2166 tree
2167 satisfy_exception_constraint (tree t, tree args,
2168 tsubst_flags_t complain, tree in_decl)
2170 tree expr = EXCEPT_CONSTR_EXPR (t);
2171 tree check = tsubst_expr (expr, args, complain, in_decl, false);
2172 if (check == error_mark_node)
2173 return boolean_false_node;
2175 if (expr_noexcept_p (check, complain))
2176 return boolean_true_node;
2177 else
2178 return boolean_false_node;
2181 /* Check a parameterized constraint. */
2183 tree
2184 satisfy_parameterized_constraint (tree t, tree args,
2185 tsubst_flags_t complain, tree in_decl)
2187 local_specialization_stack stack;
2188 tree parms = PARM_CONSTR_PARMS (t);
2189 tree vars = tsubst_constraint_variables (parms, args, complain, in_decl);
2190 if (vars == error_mark_node)
2191 return boolean_false_node;
2192 tree constr = PARM_CONSTR_OPERAND (t);
2193 return satisfy_constraint_1 (constr, args, complain, in_decl);
2196 /* Check that the conjunction of constraints is satisfied. Note
2197 that if left operand is not satisfied, the right operand
2198 is not checked.
2200 FIXME: Check that this wouldn't result in a user-defined
2201 operator. Note that this error is partially diagnosed in
2202 satisfy_predicate_constraint. It would be nice to diagnose
2203 the overload, but I don't think it's strictly necessary. */
2205 tree
2206 satisfy_conjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
2208 tree t0 = satisfy_constraint_1 (TREE_OPERAND (t, 0), args, complain, in_decl);
2209 if (t0 == boolean_false_node)
2210 return boolean_false_node;
2211 return satisfy_constraint_1 (TREE_OPERAND (t, 1), args, complain, in_decl);
2214 /* Check that the disjunction of constraints is satisfied. Note
2215 that if the left operand is satisfied, the right operand is not
2216 checked. */
2218 tree
2219 satisfy_disjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
2221 tree t0 = satisfy_constraint_1 (TREE_OPERAND (t, 0), args, complain, in_decl);
2222 if (t0 == boolean_true_node)
2223 return boolean_true_node;
2224 return satisfy_constraint_1 (TREE_OPERAND (t, 1), args, complain, in_decl);
2227 /* Dispatch to an appropriate satisfaction routine depending on the
2228 tree code of T. */
2230 tree
2231 satisfy_constraint_1 (tree t, tree args, tsubst_flags_t complain, tree in_decl)
2233 gcc_assert (!processing_template_decl);
2235 if (!t)
2236 return boolean_false_node;
2238 if (t == error_mark_node)
2239 return boolean_false_node;
2241 switch (TREE_CODE (t))
2243 case PRED_CONSTR:
2244 return satisfy_predicate_constraint (t, args, complain, in_decl);
2246 case CHECK_CONSTR:
2247 return satisfy_check_constraint (t, args, complain, in_decl);
2249 case EXPR_CONSTR:
2250 return satisfy_expression_constraint (t, args, complain, in_decl);
2252 case TYPE_CONSTR:
2253 return satisfy_type_constraint (t, args, complain, in_decl);
2255 case ICONV_CONSTR:
2256 return satisfy_implicit_conversion_constraint (t, args, complain, in_decl);
2258 case DEDUCT_CONSTR:
2259 return satisfy_argument_deduction_constraint (t, args, complain, in_decl);
2261 case EXCEPT_CONSTR:
2262 return satisfy_exception_constraint (t, args, complain, in_decl);
2264 case PARM_CONSTR:
2265 return satisfy_parameterized_constraint (t, args, complain, in_decl);
2267 case CONJ_CONSTR:
2268 return satisfy_conjunction (t, args, complain, in_decl);
2270 case DISJ_CONSTR:
2271 return satisfy_disjunction (t, args, complain, in_decl);
2273 case EXPR_PACK_EXPANSION:
2274 return satisfy_pack_expansion (t, args, complain, in_decl);
2276 default:
2277 gcc_unreachable ();
2279 return boolean_false_node;
2282 /* Check that the constraint is satisfied, according to the rules
2283 for that constraint. Note that each satisfy_* function returns
2284 true or false, depending on whether it is satisfied or not. */
2286 tree
2287 satisfy_constraint (tree t, tree args)
2289 auto_timevar time (TV_CONSTRAINT_SAT);
2291 /* Turn off template processing. Constraint satisfaction only applies
2292 to non-dependent terms, so we want to ensure full checking here. */
2293 processing_template_decl_sentinel proc (true);
2295 /* Avoid early exit in tsubst and tsubst_copy from null args; since earlier
2296 substitution was done with processing_template_decl forced on, there will
2297 be expressions that still need semantic processing, possibly buried in
2298 decltype or a template argument. */
2299 if (args == NULL_TREE)
2300 args = make_tree_vec (1);
2302 return satisfy_constraint_1 (t, args, tf_none, NULL_TREE);
2305 /* Check the associated constraints in CI against the given
2306 ARGS, returning true when the constraints are satisfied
2307 and false otherwise. */
2309 tree
2310 satisfy_associated_constraints (tree ci, tree args)
2312 /* If there are no constraints then this is trivially satisfied. */
2313 if (!ci)
2314 return boolean_true_node;
2316 /* If any arguments depend on template parameters, we can't
2317 check constraints. */
2318 if (args && uses_template_parms (args))
2319 return boolean_true_node;
2321 /* Check if we've seen a previous result. */
2322 if (tree prev = lookup_constraint_satisfaction (ci, args))
2323 return prev;
2325 /* Actually test for satisfaction. */
2326 tree result = satisfy_constraint (CI_ASSOCIATED_CONSTRAINTS (ci), args);
2327 return memoize_constraint_satisfaction (ci, args, result);
2330 } /* namespace */
2332 /* Evaluate the given constraint, returning boolean_true_node
2333 if the constraint is satisfied and boolean_false_node
2334 otherwise. */
2336 tree
2337 evaluate_constraints (tree constr, tree args)
2339 gcc_assert (constraint_p (constr));
2340 return satisfy_constraint (constr, args);
2343 /* Evaluate the function concept FN by substituting its own args
2344 into its definition and evaluating that as the result. Returns
2345 boolean_true_node if the constraints are satisfied and
2346 boolean_false_node otherwise. */
2348 tree
2349 evaluate_function_concept (tree fn, tree args)
2351 tree constr = build_nt (CHECK_CONSTR, fn, args);
2352 return satisfy_constraint (constr, args);
2355 /* Evaluate the variable concept VAR by substituting its own args into
2356 its initializer and checking the resulting constraint. Returns
2357 boolean_true_node if the constraints are satisfied and
2358 boolean_false_node otherwise. */
2360 tree
2361 evaluate_variable_concept (tree var, tree args)
2363 tree constr = build_nt (CHECK_CONSTR, var, args);
2364 return satisfy_constraint (constr, args);
2367 /* Evaluate the given expression as if it were a predicate
2368 constraint. Returns boolean_true_node if the constraint
2369 is satisfied and boolean_false_node otherwise. */
2371 tree
2372 evaluate_constraint_expression (tree expr, tree args)
2374 tree constr = normalize_expression (expr);
2375 return satisfy_constraint (constr, args);
2378 /* Returns true if the DECL's constraints are satisfied.
2379 This is used in cases where a declaration is formed but
2380 before it is used (e.g., overload resolution). */
2382 bool
2383 constraints_satisfied_p (tree decl)
2385 /* Get the constraints to check for satisfaction. This depends
2386 on whether we're looking at a template specialization or not. */
2387 tree ci;
2388 tree args = NULL_TREE;
2389 if (tree ti = DECL_TEMPLATE_INFO (decl))
2391 tree tmpl = TI_TEMPLATE (ti);
2392 ci = get_constraints (tmpl);
2393 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2394 args = get_innermost_template_args (TI_ARGS (ti), depth);
2396 else
2398 ci = get_constraints (decl);
2401 tree eval = satisfy_associated_constraints (ci, args);
2402 return eval == boolean_true_node;
2405 /* Returns true if the constraints are satisfied by ARGS.
2406 Here, T can be either a constraint or a constrained
2407 declaration. */
2409 bool
2410 constraints_satisfied_p (tree t, tree args)
2412 tree eval;
2413 if (constraint_p (t))
2414 eval = evaluate_constraints (t, args);
2415 else
2416 eval = satisfy_associated_constraints (get_constraints (t), args);
2417 return eval == boolean_true_node;
2420 namespace
2423 /* Normalize EXPR and determine if the resulting constraint is
2424 satisfied by ARGS. Returns true if and only if the constraint
2425 is satisfied. This is used extensively by diagnostics to
2426 determine causes for failure. */
2428 inline bool
2429 constraint_expression_satisfied_p (tree expr, tree args)
2431 return evaluate_constraint_expression (expr, args) == boolean_true_node;
2434 } /* namespace */
2436 /*---------------------------------------------------------------------------
2437 Semantic analysis of requires-expressions
2438 ---------------------------------------------------------------------------*/
2440 /* Finish a requires expression for the given PARMS (possibly
2441 null) and the non-empty sequence of requirements. */
2442 tree
2443 finish_requires_expr (tree parms, tree reqs)
2445 /* Modify the declared parameters by removing their context
2446 so they don't refer to the enclosing scope and explicitly
2447 indicating that they are constraint variables. */
2448 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
2450 DECL_CONTEXT (parm) = NULL_TREE;
2451 CONSTRAINT_VAR_P (parm) = true;
2454 /* Build the node. */
2455 tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs);
2456 TREE_SIDE_EFFECTS (r) = false;
2457 TREE_CONSTANT (r) = true;
2458 return r;
2461 /* Construct a requirement for the validity of EXPR. */
2462 tree
2463 finish_simple_requirement (tree expr)
2465 return build_nt (SIMPLE_REQ, expr);
2468 /* Construct a requirement for the validity of TYPE. */
2469 tree
2470 finish_type_requirement (tree type)
2472 return build_nt (TYPE_REQ, type);
2475 /* Construct a requirement for the validity of EXPR, along with
2476 its properties. if TYPE is non-null, then it specifies either
2477 an implicit conversion or argument deduction constraint,
2478 depending on whether any placeholders occur in the type name.
2479 NOEXCEPT_P is true iff the noexcept keyword was specified. */
2480 tree
2481 finish_compound_requirement (tree expr, tree type, bool noexcept_p)
2483 tree req = build_nt (COMPOUND_REQ, expr, type);
2484 COMPOUND_REQ_NOEXCEPT_P (req) = noexcept_p;
2485 return req;
2488 /* Finish a nested requirement. */
2489 tree
2490 finish_nested_requirement (tree expr)
2492 return build_nt (NESTED_REQ, expr);
2495 // Check that FN satisfies the structural requirements of a
2496 // function concept definition.
2497 tree
2498 check_function_concept (tree fn)
2500 // Check that the function is comprised of only a single
2501 // return statement.
2502 tree body = DECL_SAVED_TREE (fn);
2503 if (TREE_CODE (body) == BIND_EXPR)
2504 body = BIND_EXPR_BODY (body);
2506 // Sometimes a function call results in the creation of clean up
2507 // points. Allow these to be preserved in the body of the
2508 // constraint, as we might actually need them for some constexpr
2509 // evaluations.
2510 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
2511 body = TREE_OPERAND (body, 0);
2513 /* Check that the definition is written correctly. */
2514 if (TREE_CODE (body) != RETURN_EXPR)
2516 location_t loc = DECL_SOURCE_LOCATION (fn);
2517 if (TREE_CODE (body) == STATEMENT_LIST && !STATEMENT_LIST_HEAD (body))
2518 error_at (loc, "definition of concept %qD is empty", fn);
2519 else
2520 error_at (loc, "definition of concept %qD has multiple statements", fn);
2523 return NULL_TREE;
2527 // Check that a constrained friend declaration function declaration,
2528 // FN, is admissible. This is the case only when the declaration depends
2529 // on template parameters and does not declare a specialization.
2530 void
2531 check_constrained_friend (tree fn, tree reqs)
2533 if (fn == error_mark_node)
2534 return;
2535 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
2537 // If there are not constraints, this cannot be an error.
2538 if (!reqs)
2539 return;
2541 // Constrained friend functions that don't depend on template
2542 // arguments are effectively meaningless.
2543 if (!uses_template_parms (TREE_TYPE (fn)))
2545 error_at (location_of (fn),
2546 "constrained friend does not depend on template parameters");
2547 return;
2551 /*---------------------------------------------------------------------------
2552 Equivalence of constraints
2553 ---------------------------------------------------------------------------*/
2555 /* Returns true when A and B are equivalent constraints. */
2556 bool
2557 equivalent_constraints (tree a, tree b)
2559 gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2560 gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2561 return cp_tree_equal (a, b);
2564 /* Returns true if the template declarations A and B have equivalent
2565 constraints. This is the case when A's constraints subsume B's and
2566 when B's also constrain A's. */
2567 bool
2568 equivalently_constrained (tree d1, tree d2)
2570 gcc_assert (TREE_CODE (d1) == TREE_CODE (d2));
2571 return equivalent_constraints (get_constraints (d1), get_constraints (d2));
2574 /*---------------------------------------------------------------------------
2575 Partial ordering of constraints
2576 ---------------------------------------------------------------------------*/
2578 /* Returns true when the the constraints in A subsume those in B. */
2580 bool
2581 subsumes_constraints (tree a, tree b)
2583 gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2584 gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2585 return subsumes (a, b);
2588 /* Returns true when the the constraints in A subsume those in B, but
2589 the constraints in B do not subsume the constraints in A. */
2591 bool
2592 strictly_subsumes (tree a, tree b)
2594 return subsumes (a, b) && !subsumes (b, a);
2597 /* Determines which of the declarations, A or B, is more constrained.
2598 That is, which declaration's constraints subsume but are not subsumed
2599 by the other's?
2601 Returns 1 if A is more constrained than B, -1 if B is more constrained
2602 than A, and 0 otherwise. */
2605 more_constrained (tree d1, tree d2)
2607 tree c1 = get_constraints (d1);
2608 tree c2 = get_constraints (d2);
2609 int winner = 0;
2610 if (subsumes_constraints (c1, c2))
2611 ++winner;
2612 if (subsumes_constraints (c2, c1))
2613 --winner;
2614 return winner;
2617 /* Returns true if D1 is at least as constrained as D2. That is, the
2618 associated constraints of D1 subsume those of D2, or both declarations
2619 are unconstrained. */
2621 bool
2622 at_least_as_constrained (tree d1, tree d2)
2624 tree c1 = get_constraints (d1);
2625 tree c2 = get_constraints (d2);
2626 return subsumes_constraints (c1, c2);
2630 /*---------------------------------------------------------------------------
2631 Constraint diagnostics
2633 FIXME: Normalize expressions into constraints before evaluating them.
2634 This should be the general pattern for all such diagnostics.
2635 ---------------------------------------------------------------------------*/
2637 /* The number of detailed constraint failures. */
2639 int constraint_errors = 0;
2641 /* Do not generate errors after diagnosing this number of constraint
2642 failures.
2644 FIXME: This is a really arbitrary number. Provide better control of
2645 constraint diagnostics with a command line option. */
2647 int constraint_thresh = 20;
2650 /* Returns true if we should elide the diagnostic for a constraint failure.
2651 This is the case when the number of errors has exceeded the pre-configured
2652 threshold. */
2654 inline bool
2655 elide_constraint_failure_p ()
2657 bool ret = constraint_thresh <= constraint_errors;
2658 ++constraint_errors;
2659 return ret;
2662 /* Returns the number of undiagnosed errors. */
2664 inline int
2665 undiagnosed_constraint_failures ()
2667 return constraint_errors - constraint_thresh;
2670 /* The diagnosis of constraints performs a combination of normalization
2671 and satisfaction testing. We recursively walk through the conjunction or
2672 disjunction of associated constraints, testing each sub-constraint in
2673 turn. */
2675 namespace {
2677 void diagnose_constraint (location_t, tree, tree, tree);
2679 /* Emit a specific diagnostics for a failed trait. */
2681 void
2682 diagnose_trait_expression (location_t loc, tree, tree cur, tree args)
2684 if (constraint_expression_satisfied_p (cur, args))
2685 return;
2686 if (elide_constraint_failure_p())
2687 return;
2689 tree expr = PRED_CONSTR_EXPR (cur);
2690 ++processing_template_decl;
2691 expr = tsubst_expr (expr, args, tf_none, NULL_TREE, false);
2692 --processing_template_decl;
2694 tree t1 = TRAIT_EXPR_TYPE1 (expr);
2695 tree t2 = TRAIT_EXPR_TYPE2 (expr);
2696 switch (TRAIT_EXPR_KIND (expr))
2698 case CPTK_HAS_NOTHROW_ASSIGN:
2699 inform (loc, " %qT is not nothrow copy assignable", t1);
2700 break;
2701 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
2702 inform (loc, " %qT is not nothrow default constructible", t1);
2703 break;
2704 case CPTK_HAS_NOTHROW_COPY:
2705 inform (loc, " %qT is not nothrow copy constructible", t1);
2706 break;
2707 case CPTK_HAS_TRIVIAL_ASSIGN:
2708 inform (loc, " %qT is not trivially copy assignable", t1);
2709 break;
2710 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
2711 inform (loc, " %qT is not trivially default constructible", t1);
2712 break;
2713 case CPTK_HAS_TRIVIAL_COPY:
2714 inform (loc, " %qT is not trivially copy constructible", t1);
2715 break;
2716 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
2717 inform (loc, " %qT is not trivially destructible", t1);
2718 break;
2719 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
2720 inform (loc, " %qT does not have a virtual destructor", t1);
2721 break;
2722 case CPTK_IS_ABSTRACT:
2723 inform (loc, " %qT is not an abstract class", t1);
2724 break;
2725 case CPTK_IS_BASE_OF:
2726 inform (loc, " %qT is not a base of %qT", t1, t2);
2727 break;
2728 case CPTK_IS_CLASS:
2729 inform (loc, " %qT is not a class", t1);
2730 break;
2731 case CPTK_IS_EMPTY:
2732 inform (loc, " %qT is not an empty class", t1);
2733 break;
2734 case CPTK_IS_ENUM:
2735 inform (loc, " %qT is not an enum", t1);
2736 break;
2737 case CPTK_IS_FINAL:
2738 inform (loc, " %qT is not a final class", t1);
2739 break;
2740 case CPTK_IS_LITERAL_TYPE:
2741 inform (loc, " %qT is not a literal type", t1);
2742 break;
2743 case CPTK_IS_POD:
2744 inform (loc, " %qT is not a POD type", t1);
2745 break;
2746 case CPTK_IS_POLYMORPHIC:
2747 inform (loc, " %qT is not a polymorphic type", t1);
2748 break;
2749 case CPTK_IS_SAME_AS:
2750 inform (loc, " %qT is not the same as %qT", t1, t2);
2751 break;
2752 case CPTK_IS_STD_LAYOUT:
2753 inform (loc, " %qT is not an standard layout type", t1);
2754 break;
2755 case CPTK_IS_TRIVIAL:
2756 inform (loc, " %qT is not a trivial type", t1);
2757 break;
2758 case CPTK_IS_UNION:
2759 inform (loc, " %qT is not a union", t1);
2760 break;
2761 default:
2762 gcc_unreachable ();
2766 /* Diagnose the expression of a predicate constraint. */
2768 void
2769 diagnose_other_expression (location_t loc, tree, tree cur, tree args)
2771 if (constraint_expression_satisfied_p (cur, args))
2772 return;
2773 if (elide_constraint_failure_p())
2774 return;
2775 inform (loc, "%qE evaluated to false", cur);
2778 /* Do our best to infer meaning from predicates. */
2780 inline void
2781 diagnose_predicate_constraint (location_t loc, tree orig, tree cur, tree args)
2783 if (TREE_CODE (PRED_CONSTR_EXPR (cur)) == TRAIT_EXPR)
2784 diagnose_trait_expression (loc, orig, cur, args);
2785 else
2786 diagnose_other_expression (loc, orig, cur, args);
2789 /* Diagnose a failed pack expansion, possibly containing constraints. */
2791 void
2792 diagnose_pack_expansion (location_t loc, tree, tree cur, tree args)
2794 if (constraint_expression_satisfied_p (cur, args))
2795 return;
2796 if (elide_constraint_failure_p())
2797 return;
2799 /* Make sure that we don't have naked packs that we don't expect. */
2800 if (!same_type_p (TREE_TYPE (cur), boolean_type_node))
2802 inform (loc, "invalid pack expansion in constraint %qE", cur);
2803 return;
2806 inform (loc, "in the expansion of %qE", cur);
2808 /* Get the vector of expanded arguments. Note that n must not
2809 be 0 since this constraint is not satisfied. */
2810 ++processing_template_decl;
2811 tree exprs = tsubst_pack_expansion (cur, args, tf_none, NULL_TREE);
2812 --processing_template_decl;
2813 if (exprs == error_mark_node)
2815 /* TODO: This error message could be better. */
2816 inform (loc, " substitution failure occurred during expansion");
2817 return;
2820 /* Check each expanded constraint separately. */
2821 int n = TREE_VEC_LENGTH (exprs);
2822 for (int i = 0; i < n; ++i)
2824 tree expr = TREE_VEC_ELT (exprs, i);
2825 if (!constraint_expression_satisfied_p (expr, args))
2826 inform (loc, " %qE was not satisfied", expr);
2830 /* Diagnose a potentially unsatisfied concept check constraint DECL<CARGS>.
2831 Parameters are as for diagnose_constraint. */
2833 void
2834 diagnose_check_constraint (location_t loc, tree orig, tree cur, tree args)
2836 if (constraints_satisfied_p (cur, args))
2837 return;
2839 tree decl = CHECK_CONSTR_CONCEPT (cur);
2840 tree cargs = CHECK_CONSTR_ARGS (cur);
2841 tree tmpl = DECL_TI_TEMPLATE (decl);
2842 tree check = build_nt (CHECK_CONSTR, decl, cargs);
2844 /* Instantiate the concept check arguments. */
2845 tree targs = tsubst (cargs, args, tf_none, NULL_TREE);
2846 if (targs == error_mark_node)
2848 if (elide_constraint_failure_p ())
2849 return;
2850 inform (loc, "invalid use of the concept %qE", check);
2851 tsubst (cargs, args, tf_warning_or_error, NULL_TREE);
2852 return;
2855 tree sub = build_tree_list (tmpl, targs);
2856 /* Update to the expanded definitions. */
2857 cur = expand_concept (decl, targs);
2858 if (cur == error_mark_node)
2860 if (elide_constraint_failure_p ())
2861 return;
2862 inform (loc, "in the expansion of concept %qE %S", check, sub);
2863 cur = get_concept_definition (decl);
2864 tsubst_expr (cur, targs, tf_warning_or_error, NULL_TREE, false);
2865 return;
2868 orig = get_concept_definition (CHECK_CONSTR_CONCEPT (orig));
2869 orig = normalize_expression (orig);
2871 location_t dloc = DECL_SOURCE_LOCATION (decl);
2872 inform (dloc, "within %qS", sub);
2873 diagnose_constraint (dloc, orig, cur, targs);
2876 /* Diagnose a potentially unsatisfied conjunction or disjunction. Parameters
2877 are as for diagnose_constraint. */
2879 void
2880 diagnose_logical_constraint (location_t loc, tree orig, tree cur, tree args)
2882 tree t0 = TREE_OPERAND (cur, 0);
2883 tree t1 = TREE_OPERAND (cur, 1);
2884 if (!constraints_satisfied_p (t0, args))
2885 diagnose_constraint (loc, TREE_OPERAND (orig, 0), t0, args);
2886 else if (TREE_CODE (orig) == TRUTH_ORIF_EXPR)
2887 return;
2888 if (!constraints_satisfied_p (t1, args))
2889 diagnose_constraint (loc, TREE_OPERAND (orig, 1), t1, args);
2892 /* Diagnose a potential expression constraint failure. */
2894 void
2895 diagnose_expression_constraint (location_t loc, tree orig, tree cur, tree args)
2897 if (constraints_satisfied_p (cur, args))
2898 return;
2899 if (elide_constraint_failure_p())
2900 return;
2902 tree expr = EXPR_CONSTR_EXPR (orig);
2903 inform (loc, "the required expression %qE would be ill-formed", expr);
2905 // TODO: We should have a flag that controls this substitution.
2906 // I'm finding it very useful for resolving concept check errors.
2908 // inform (input_location, "==== BEGIN DUMP ====");
2909 // tsubst_expr (EXPR_CONSTR_EXPR (orig), args, tf_warning_or_error, NULL_TREE, false);
2910 // inform (input_location, "==== END DUMP ====");
2913 /* Diagnose a potentially failed type constraint. */
2915 void
2916 diagnose_type_constraint (location_t loc, tree orig, tree cur, tree args)
2918 if (constraints_satisfied_p (cur, args))
2919 return;
2920 if (elide_constraint_failure_p())
2921 return;
2923 tree type = TYPE_CONSTR_TYPE (orig);
2924 inform (loc, "the required type %qT would be ill-formed", type);
2927 /* Diagnose a potentially unsatisfied conversion constraint. */
2929 void
2930 diagnose_implicit_conversion_constraint (location_t loc, tree orig, tree cur,
2931 tree args)
2933 if (constraints_satisfied_p (cur, args))
2934 return;
2936 /* The expression and type will previously have been substituted into,
2937 and therefore may already be an error. Also, we will have already
2938 diagnosed substitution failures into an expression since this must be
2939 part of a compound requirement. */
2940 tree expr = ICONV_CONSTR_EXPR (cur);
2941 if (error_operand_p (expr))
2942 return;
2944 /* Don't elide a previously diagnosed failure. */
2945 if (elide_constraint_failure_p())
2946 return;
2948 tree type = ICONV_CONSTR_TYPE (cur);
2949 if (error_operand_p (type))
2951 inform (loc, "substitution into type %qT failed",
2952 ICONV_CONSTR_TYPE (orig));
2953 return;
2956 inform(loc, "%qE is not implicitly convertible to %qT", expr, type);
2959 /* Diagnose an argument deduction constraint. */
2961 void
2962 diagnose_argument_deduction_constraint (location_t loc, tree orig, tree cur,
2963 tree args)
2965 if (constraints_satisfied_p (cur, args))
2966 return;
2968 /* The expression and type will previously have been substituted into,
2969 and therefore may already be an error. Also, we will have already
2970 diagnosed substution failures into an expression since this must be
2971 part of a compound requirement. */
2972 tree expr = DEDUCT_CONSTR_EXPR (cur);
2973 if (error_operand_p (expr))
2974 return;
2976 /* Don't elide a previously diagnosed failure. */
2977 if (elide_constraint_failure_p ())
2978 return;
2980 tree pattern = DEDUCT_CONSTR_PATTERN (cur);
2981 if (error_operand_p (pattern))
2983 inform (loc, "substitution into type %qT failed",
2984 DEDUCT_CONSTR_PATTERN (orig));
2985 return;
2988 inform (loc, "unable to deduce placeholder type %qT from %qE",
2989 pattern, expr);
2992 /* Diagnose an exception constraint. */
2994 void
2995 diagnose_exception_constraint (location_t loc, tree orig, tree cur, tree args)
2997 if (constraints_satisfied_p (cur, args))
2998 return;
2999 if (elide_constraint_failure_p ())
3000 return;
3002 /* Rebuild a noexcept expression. */
3003 tree expr = EXCEPT_CONSTR_EXPR (cur);
3004 if (error_operand_p (expr))
3005 return;
3007 inform (loc, "%qE evaluated to false", EXCEPT_CONSTR_EXPR (orig));
3010 /* Diagnose a potentially unsatisfied parameterized constraint. */
3012 void
3013 diagnose_parameterized_constraint (location_t loc, tree orig, tree cur,
3014 tree args)
3016 if (constraints_satisfied_p (cur, args))
3017 return;
3019 local_specialization_stack stack;
3020 tree parms = PARM_CONSTR_PARMS (cur);
3021 tree vars = tsubst_constraint_variables (parms, args, tf_warning_or_error,
3022 NULL_TREE);
3023 if (vars == error_mark_node)
3025 if (elide_constraint_failure_p ())
3026 return;
3028 /* TODO: Check which variable failed and use orig to diagnose
3029 that substitution error. */
3030 inform (loc, "failed to instantiate constraint variables");
3031 return;
3034 /* TODO: It would be better write these in a list. */
3035 while (vars)
3037 inform (loc, " with %q#D", vars);
3038 vars = TREE_CHAIN (vars);
3040 orig = PARM_CONSTR_OPERAND (orig);
3041 cur = PARM_CONSTR_OPERAND (cur);
3042 return diagnose_constraint (loc, orig, cur, args);
3045 /* Diagnose the constraint CUR for the given ARGS. This is only ever invoked
3046 on the associated constraints, so we can only have conjunctions of
3047 predicate constraints. The ORIGinal (dependent) constructs follow
3048 the current constraints to enable better diagnostics. Note that ORIG
3049 and CUR must be the same kinds of node, except when CUR is an error. */
3051 void
3052 diagnose_constraint (location_t loc, tree orig, tree cur, tree args)
3054 switch (TREE_CODE (cur))
3056 case EXPR_CONSTR:
3057 diagnose_expression_constraint (loc, orig, cur, args);
3058 break;
3060 case TYPE_CONSTR:
3061 diagnose_type_constraint (loc, orig, cur, args);
3062 break;
3064 case ICONV_CONSTR:
3065 diagnose_implicit_conversion_constraint (loc, orig, cur, args);
3066 break;
3068 case DEDUCT_CONSTR:
3069 diagnose_argument_deduction_constraint (loc, orig, cur, args);
3070 break;
3072 case EXCEPT_CONSTR:
3073 diagnose_exception_constraint (loc, orig, cur, args);
3074 break;
3076 case CONJ_CONSTR:
3077 case DISJ_CONSTR:
3078 diagnose_logical_constraint (loc, orig, cur, args);
3079 break;
3081 case PRED_CONSTR:
3082 diagnose_predicate_constraint (loc, orig, cur, args);
3083 break;
3085 case PARM_CONSTR:
3086 diagnose_parameterized_constraint (loc, orig, cur, args);
3087 break;
3089 case CHECK_CONSTR:
3090 diagnose_check_constraint (loc, orig, cur, args);
3091 break;
3093 case EXPR_PACK_EXPANSION:
3094 diagnose_pack_expansion (loc, orig, cur, args);
3095 break;
3097 case ERROR_MARK:
3098 /* TODO: Can we improve the diagnostic with the original? */
3099 inform (input_location, "ill-formed constraint");
3100 break;
3102 default:
3103 gcc_unreachable ();
3104 break;
3108 /* Diagnose the reason(s) why ARGS do not satisfy the constraints
3109 of declaration DECL. */
3111 void
3112 diagnose_declaration_constraints (location_t loc, tree decl, tree args)
3114 inform (loc, " constraints not satisfied");
3116 /* Constraints are attached to the template. */
3117 if (tree ti = DECL_TEMPLATE_INFO (decl))
3119 decl = TI_TEMPLATE (ti);
3120 if (!args)
3121 args = TI_ARGS (ti);
3124 /* Recursively diagnose the associated constraints. */
3125 tree ci = get_constraints (decl);
3126 tree t = CI_ASSOCIATED_CONSTRAINTS (ci);
3127 diagnose_constraint (loc, t, t, args);
3130 } // namespace
3132 /* Emit diagnostics detailing the failure ARGS to satisfy the
3133 constraints of T. Here, T can be either a constraint
3134 or a declaration. */
3136 void
3137 diagnose_constraints (location_t loc, tree t, tree args)
3139 constraint_errors = 0;
3141 if (constraint_p (t))
3142 diagnose_constraint (loc, t, t, args);
3143 else if (DECL_P (t))
3144 diagnose_declaration_constraints (loc, t, args);
3145 else
3146 gcc_unreachable ();
3148 /* Note the number of elided failures. */
3149 int n = undiagnosed_constraint_failures ();
3150 if (n > 0)
3151 inform (loc, "... and %d more constraint errors not shown", n);