Daily bump.
[official-gcc.git] / gcc / cp / constraint.cc
blobcb82535af49fe68313b1a4108d66f687afdcf009
1 /* Processing rules for constraints.
2 Copyright (C) 2013-2015 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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "stringpool.h"
36 #include "attribs.h"
37 #include "intl.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "cp-objcp-common.h"
43 #include "tree-inline.h"
44 #include "decl.h"
45 #include "toplev.h"
46 #include "type-utils.h"
48 /*---------------------------------------------------------------------------
49 Operations on constraints
50 ---------------------------------------------------------------------------*/
52 /* Returns true if C is a constraint tree code. Note that ERROR_MARK
53 is a valid constraint. */
55 static inline bool
56 constraint_p (tree_code c)
58 return (PRED_CONSTR <= c && c <= DISJ_CONSTR) || c == ERROR_MARK;
61 /* Returns true if T is a constraint. Note that error_mark_node
62 is a valid constraint. */
64 bool
65 constraint_p (tree t)
67 return constraint_p (TREE_CODE (t));
70 /* Make a predicate constraint from the given expression. */
72 tree
73 make_predicate_constraint (tree expr)
75 return build_nt (PRED_CONSTR, expr);
78 /* Returns the conjunction of two constraints A and B. Note that
79 conjoining a non-null constraint with NULL_TREE is an identity
80 operation. That is, for non-null A,
82 conjoin_constraints(a, NULL_TREE) == a
84 and
86 conjoin_constraints (NULL_TREE, a) == a
88 If both A and B are NULL_TREE, the result is also NULL_TREE. */
90 tree
91 conjoin_constraints (tree a, tree b)
93 gcc_assert (a ? constraint_p (a) : true);
94 gcc_assert (b ? constraint_p (b) : true);
95 if (a)
96 return b ? build_nt (CONJ_CONSTR, a, b) : a;
97 else if (b)
98 return b;
99 else
100 return NULL_TREE;
103 /* Transform the vector of expressions in the T into a conjunction
104 of requirements. T must be a TREE_VEC. */
106 tree
107 conjoin_constraints (tree t)
109 gcc_assert (TREE_CODE (t) == TREE_VEC);
110 tree r = NULL_TREE;
111 for (int i = 0; i < TREE_VEC_LENGTH (t); ++i)
112 r = conjoin_constraints (r, TREE_VEC_ELT (t, i));
113 return r;
116 /* Returns true if T is a call expression to a function
117 concept. */
119 bool
120 function_concept_check_p (tree t)
122 gcc_assert (TREE_CODE (t) == CALL_EXPR);
123 tree fn = CALL_EXPR_FN (t);
124 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR
125 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
127 tree f1 = get_first_fn (fn);
128 if (TREE_CODE (f1) == TEMPLATE_DECL
129 && DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (f1)))
130 return true;
132 return false;
135 /*---------------------------------------------------------------------------
136 Resolution of qualified concept names
137 ---------------------------------------------------------------------------*/
139 /* This facility is used to resolve constraint checks from
140 requirement expressions. A constraint check is a call to
141 a function template declared with the keyword 'concept'.
143 The result of resolution is a pair (a TREE_LIST) whose value
144 is the matched declaration, and whose purpose contains the
145 coerced template arguments that can be substituted into the
146 call. */
148 // Given an overload set OVL, try to find a unique definition that can be
149 // instantiated by the template arguments ARGS.
151 // This function is not called for arbitrary call expressions. In particular,
152 // the call expression must be written with explicit template arguments
153 // and no function arguments. For example:
155 // f<T, U>()
157 // If a single match is found, this returns a TREE_LIST whose VALUE
158 // is the constraint function (not the template), and its PURPOSE is
159 // the complete set of arguments substituted into the parameter list.
160 static tree
161 resolve_constraint_check (tree ovl, tree args)
163 tree cands = NULL_TREE;
164 for (tree p = ovl; p != NULL_TREE; p = OVL_NEXT (p))
166 // Get the next template overload.
167 tree tmpl = OVL_CURRENT (p);
168 if (TREE_CODE (tmpl) != TEMPLATE_DECL)
169 continue;
171 // Don't try to deduce checks for non-concepts. We often
172 // end up trying to resolve constraints in functional casts
173 // as part of a postfix-expression. We can save time and
174 // headaches by not instantiating those declarations.
176 // NOTE: This masks a potential error, caused by instantiating
177 // non-deduced contexts using placeholder arguments.
178 tree fn = DECL_TEMPLATE_RESULT (tmpl);
179 if (DECL_ARGUMENTS (fn))
180 continue;
181 if (!DECL_DECLARED_CONCEPT_P (fn))
182 continue;
184 // Remember the candidate if we can deduce a substitution.
185 ++processing_template_decl;
186 tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl));
187 if (tree subst = coerce_template_parms (parms, args, tmpl))
188 if (subst != error_mark_node)
189 cands = tree_cons (subst, fn, cands);
190 --processing_template_decl;
193 // If we didn't find a unique candidate, then this is
194 // not a constraint check.
195 if (!cands || TREE_CHAIN (cands))
196 return NULL_TREE;
198 return cands;
201 // Determine if the the call expression CALL is a constraint check, and
202 // return the concept declaration and arguments being checked. If CALL
203 // does not denote a constraint check, return NULL.
204 tree
205 resolve_constraint_check (tree call)
207 gcc_assert (TREE_CODE (call) == CALL_EXPR);
209 // A constraint check must be only a template-id expression. If
210 // it's a call to a base-link, its function(s) should be a
211 // template-id expression. If this is not a template-id, then it
212 // cannot be a concept-check.
213 tree target = CALL_EXPR_FN (call);
214 if (BASELINK_P (target))
215 target = BASELINK_FUNCTIONS (target);
216 if (TREE_CODE (target) != TEMPLATE_ID_EXPR)
217 return NULL_TREE;
219 // Get the overload set and template arguments and try to
220 // resolve the target.
221 tree ovl = TREE_OPERAND (target, 0);
223 /* This is a function call of a variable concept... ill-formed. */
224 if (TREE_CODE (ovl) == TEMPLATE_DECL)
226 error_at (location_of (call),
227 "function call of variable concept %qE", call);
228 return error_mark_node;
231 tree args = TREE_OPERAND (target, 1);
232 return resolve_constraint_check (ovl, args);
235 /* Returns a pair containing the checked variable concept
236 and its associated prototype parameter. The result
237 is a TREE_LIST whose TREE_VALUE is the variable concept
238 and whose TREE_PURPOSE is the prototype parameter. */
240 tree
241 resolve_variable_concept_check (tree id)
243 tree tmpl = TREE_OPERAND (id, 0);
244 tree args = TREE_OPERAND (id, 1);
246 if (!variable_concept_p (tmpl))
247 return NULL_TREE;
249 /* Make sure that we have the right parameters before
250 assuming that it works. Note that failing to deduce
251 will result in diagnostics. */
252 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
253 tree result = coerce_template_parms (parms, args, tmpl);
254 if (result != error_mark_node)
256 tree decl = DECL_TEMPLATE_RESULT (tmpl);
257 return build_tree_list (result, decl);
259 else
260 return NULL_TREE;
264 /* Given a call expression or template-id expression to
265 a concept EXPR possibly including a wildcard, deduce
266 the concept being checked and the prototype parameter.
267 Returns true if the constraint and prototype can be
268 deduced and false otherwise. Note that the CHECK and
269 PROTO arguments are set to NULL_TREE if this returns
270 false. */
272 bool
273 deduce_constrained_parameter (tree expr, tree& check, tree& proto)
275 tree info = NULL_TREE;
276 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
277 info = resolve_variable_concept_check (expr);
278 else if (TREE_CODE (expr) == CALL_EXPR)
279 info = resolve_constraint_check (expr);
280 else
281 gcc_unreachable ();
283 if (info && info != error_mark_node)
285 check = TREE_VALUE (info);
286 tree arg = TREE_VEC_ELT (TREE_PURPOSE (info), 0);
287 if (ARGUMENT_PACK_P (arg))
288 arg = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0);
289 proto = TREE_TYPE (arg);
290 return true;
292 check = proto = NULL_TREE;
293 return false;
296 // Given a call expression or template-id expression to a concept, EXPR,
297 // deduce the concept being checked and return the template arguments.
298 // Returns NULL_TREE if deduction fails.
299 static tree
300 deduce_concept_introduction (tree expr)
302 tree info = NULL_TREE;
303 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
304 info = resolve_variable_concept_check (expr);
305 else if (TREE_CODE (expr) == CALL_EXPR)
306 info = resolve_constraint_check (expr);
307 else
308 gcc_unreachable ();
310 if (info && info != error_mark_node)
311 return TREE_PURPOSE (info);
312 return NULL_TREE;
315 namespace {
317 /*---------------------------------------------------------------------------
318 Lifting of concept definitions
319 ---------------------------------------------------------------------------*/
321 /* Part of constraint normalization. Whenever we find a reference to
322 a variable concept or a call to a function concept, we lift or
323 inline that concept's definition into the constraint. This ensures
324 that constraints are always checked in the immediate instantiation
325 context. */
327 tree lift_expression (tree);
329 /* If the tree T has operands, then lift any concepts out of them. */
330 tree
331 lift_operands (tree t)
333 if (int n = tree_operand_length (t))
335 t = copy_node (t);
336 for (int i = 0; i < n; ++i)
337 TREE_OPERAND (t, i) = lift_expression (TREE_OPERAND (t, i));
339 return t;
342 /* Recursively lift all operands of the function call. Also, check
343 that the call target is not accidentally a variable concept
344 since that's ill-formed. */
345 tree
346 lift_function_call (tree t)
348 gcc_assert (TREE_CODE (t) == CALL_EXPR);
349 gcc_assert (!VAR_P (CALL_EXPR_FN (t)));
350 return lift_operands (t);
353 /* Inline a function (concept) definition by substituting
354 ARGS into its body. */
355 tree
356 lift_function_definition (tree fn, tree args)
358 /* Extract the body of the function minus the return expression. */
359 tree body = DECL_SAVED_TREE (fn);
360 if (!body)
361 return error_mark_node;
362 if (TREE_CODE (body) == BIND_EXPR)
363 body = BIND_EXPR_BODY (body);
364 if (TREE_CODE (body) != RETURN_EXPR)
365 return error_mark_node;
367 body = TREE_OPERAND (body, 0);
369 /* Substitute template arguments to produce our inline expression. */
370 tree result = tsubst_expr (body, args, tf_none, NULL_TREE, false);
371 if (result == error_mark_node)
372 return error_mark_node;
374 return lift_expression (result);
377 /* Inline a reference to a function concept. */
378 tree
379 lift_call_expression (tree t)
381 /* Try to resolve this function call as a concept. If not, then
382 it can be returned as-is. */
383 tree check = resolve_constraint_check (t);
384 if (!check)
385 return lift_function_call (t);
386 if (check == error_mark_node)
387 return error_mark_node;
389 tree fn = TREE_VALUE (check);
390 tree args = TREE_PURPOSE (check);
391 return lift_function_definition (fn, args);
394 tree
395 lift_variable_initializer (tree var, tree args)
397 /* Extract the body from the variable initializer. */
398 tree init = DECL_INITIAL (var);
399 if (!init)
400 return error_mark_node;
402 /* Substitute the arguments to form our new inline expression. */
403 tree result = tsubst_expr (init, args, tf_none, NULL_TREE, false);
404 if (result == error_mark_node)
405 return error_mark_node;
407 return lift_expression (result);
410 /* Determine if a template-id is a variable concept and inline. */
412 tree
413 lift_template_id (tree t)
415 if (tree info = resolve_variable_concept_check (t))
417 tree decl = TREE_VALUE (info);
418 tree args = TREE_PURPOSE (info);
419 return lift_variable_initializer (decl, args);
422 /* Check that we didn't refer to a function concept like
423 a variable.
425 TODO: Add a note on how to fix this. */
426 tree tmpl = TREE_OPERAND (t, 0);
427 if (TREE_CODE (tmpl) == OVERLOAD)
429 tree fn = OVL_FUNCTION (tmpl);
430 if (TREE_CODE (fn) == TEMPLATE_DECL
431 && DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (fn)))
433 error_at (location_of (t),
434 "invalid reference to function concept %qD", fn);
435 return error_mark_node;
439 return t;
442 /* Lift any constraints appearing in a nested requirement of
443 a requires-expression. */
444 tree
445 lift_requires_expression (tree t)
447 tree parms = TREE_OPERAND (t, 0);
448 tree reqs = TREE_OPERAND (t, 1);
449 tree result = NULL_TREE;
450 for (; reqs != NULL_TREE; reqs = TREE_CHAIN (reqs))
452 tree req = TREE_VALUE (reqs);
453 if (TREE_CODE (req) == NESTED_REQ)
455 tree expr = lift_expression (TREE_OPERAND (req, 0));
456 req = finish_nested_requirement (expr);
458 result = tree_cons (NULL_TREE, req, result);
460 return finish_requires_expr (parms, result);
463 /* Inline references to specializations of concepts. */
464 tree
465 lift_expression (tree t)
467 if (t == NULL_TREE)
468 return NULL_TREE;
470 if (t == error_mark_node)
471 return error_mark_node;
473 /* Concepts can be referred to by call or variable. All other
474 nodes are preserved. */
475 switch (TREE_CODE (t))
477 case CALL_EXPR:
478 return lift_call_expression (t);
480 case TEMPLATE_ID_EXPR:
481 return lift_template_id (t);
483 case REQUIRES_EXPR:
484 return lift_requires_expression (t);
486 case EXPR_PACK_EXPANSION:
487 /* Use copy_node rather than make_pack_expansion so that
488 PACK_EXPANSION_PARAMETER_PACKS stays the same. */
489 t = copy_node (t);
490 SET_PACK_EXPANSION_PATTERN
491 (t, lift_expression (PACK_EXPANSION_PATTERN (t)));
492 return t;
494 case TREE_LIST:
496 t = copy_node (t);
497 TREE_VALUE (t) = lift_expression (TREE_VALUE (t));
498 TREE_CHAIN (t) = lift_expression (TREE_CHAIN (t));
499 return t;
502 default:
503 return lift_operands (t);
507 /*---------------------------------------------------------------------------
508 Transformation of expressions into constraints
509 ---------------------------------------------------------------------------*/
511 /* Part of constraint normalization. The following functions rewrite
512 expressions as constraints. */
514 tree transform_expression (tree);
516 /* Check that the logical-or or logical-and expression does
517 not result in a call to a user-defined user-defined operator
518 (temp.constr.op). Returns true if the logical operator is
519 admissible and false otherwise. */
521 bool
522 check_logical_expr (tree t)
524 /* We can't do much for type dependent expressions. */
525 if (type_dependent_expression_p (t))
526 return true;
528 /* Resolve the logical operator. Note that template processing is
529 disabled so we get the actual call or target expression back.
530 not_processing_template_sentinel sentinel.
532 TODO: This check is actually subsumed by the requirement that
533 constraint operands have type bool. I'm not sure we need it
534 unless we allow conversions. */
535 tree arg1 = TREE_OPERAND (t, 0);
536 tree arg2 = TREE_OPERAND (t, 1);
537 tree ovl = NULL_TREE;
538 tree expr = build_x_binary_op (EXPR_LOC_OR_LOC (arg2, input_location),
539 TREE_CODE (t),
540 arg1, TREE_CODE (arg1),
541 arg2, TREE_CODE (arg2),
542 &ovl,
543 tf_none);
544 if (TREE_CODE (expr) != TREE_CODE (t))
546 error ("user-defined operator %qs in constraint %q+E",
547 operator_name_info[TREE_CODE (t)].name, t);
548 return false;
550 return true;
553 /* Transform a logical-or or logical-and expression into either
554 a conjunction or disjunction. */
556 tree
557 xform_logical (tree t, tree_code c)
559 if (!check_logical_expr (t))
560 return error_mark_node;
561 tree t0 = transform_expression (TREE_OPERAND (t, 0));
562 tree t1 = transform_expression (TREE_OPERAND (t, 1));
563 return build_nt (c, t0, t1);
566 /* A simple requirement T introduces an expression constraint
567 for its expression. */
569 inline tree
570 xform_simple_requirement (tree t)
572 return build_nt (EXPR_CONSTR, TREE_OPERAND (t, 0));
575 /* A type requirement T introduce a type constraint for its type. */
577 inline tree
578 xform_type_requirement (tree t)
580 return build_nt (TYPE_CONSTR, TREE_OPERAND (t, 0));
583 /* A compound requirement T introduces a conjunction of constraints
584 depending on its form. The conjunction always includes an
585 expression constraint for the expression of the requirement.
586 If a trailing return type was specified, the conjunction includes
587 either an implicit conversion constraint or an argument deduction
588 constraint. If the noexcept specifier is present, the conjunction
589 includes an exception constraint. */
591 tree
592 xform_compound_requirement (tree t)
594 tree expr = TREE_OPERAND (t, 0);
595 tree constr = build_nt (EXPR_CONSTR, TREE_OPERAND (t, 0));
597 /* If a type is given, append an implicit conversion or
598 argument deduction constraint. */
599 if (tree type = TREE_OPERAND (t, 1))
601 tree type_constr;
602 /* TODO: We should be extracting a list of auto nodes
603 from type_uses_auto, not a single node */
604 if (tree placeholder = type_uses_auto (type))
605 type_constr = build_nt (DEDUCT_CONSTR, expr, type, placeholder);
606 else
607 type_constr = build_nt (ICONV_CONSTR, expr, type);
608 constr = conjoin_constraints (constr, type_constr);
611 /* If noexcept is present, append an exception constraint. */
612 if (COMPOUND_REQ_NOEXCEPT_P (t))
614 tree except = build_nt (EXCEPT_CONSTR, expr);
615 constr = conjoin_constraints (constr, except);
618 return constr;
621 /* A nested requirement T introduces a conjunction of constraints
622 corresponding to its constraint-expression.
624 If the result of transforming T is error_mark_node, the resulting
625 constraint is a predicate constraint whose operand is also
626 error_mark_node. This preserves the constraint structure, but
627 will guarantee that the constraint is never satisfied. */
629 inline tree
630 xform_nested_requirement (tree t)
632 return transform_expression (TREE_OPERAND (t, 0));
635 /* Transform a requirement T into one or more constraints. */
637 tree
638 xform_requirement (tree t)
640 switch (TREE_CODE (t))
642 case SIMPLE_REQ:
643 return xform_simple_requirement (t);
645 case TYPE_REQ:
646 return xform_type_requirement (t);
648 case COMPOUND_REQ:
649 return xform_compound_requirement (t);
651 case NESTED_REQ:
652 return xform_nested_requirement (t);
654 default:
655 gcc_unreachable ();
657 return error_mark_node;
660 /* Transform a sequence of requirements into a conjunction of
661 constraints. */
663 tree
664 xform_requirements (tree t)
666 tree result = NULL_TREE;
667 for (; t; t = TREE_CHAIN (t))
669 tree constr = xform_requirement (TREE_VALUE (t));
670 result = conjoin_constraints (result, constr);
672 return result;
675 /* Transform a requires-expression into a parameterized constraint. */
677 tree
678 xform_requires_expr (tree t)
680 tree operand = xform_requirements (TREE_OPERAND (t, 1));
681 if (tree parms = TREE_OPERAND (t, 0))
682 return build_nt (PARM_CONSTR, parms, operand);
683 else
684 return operand;
687 /* Transform an expression into an atomic predicate constraint.
688 After substitution, the expression of a predicate constraint
689 shall have type bool (temp.constr.pred). For non-type-dependent
690 expressions, we can check that now. */
692 tree
693 xform_atomic (tree t)
695 if (TREE_TYPE (t) && !type_dependent_expression_p (t))
697 tree type = cv_unqualified (TREE_TYPE (t));
698 if (!same_type_p (type, boolean_type_node))
700 error ("predicate constraint %q+E does not have type %<bool%>", t);
701 return error_mark_node;
704 return build_nt (PRED_CONSTR, t);
707 /* Push down the pack expansion EXP into the leaves of the constraint PAT. */
709 tree
710 push_down_pack_expansion (tree exp, tree pat)
712 switch (TREE_CODE (pat))
714 case CONJ_CONSTR:
715 case DISJ_CONSTR:
717 pat = copy_node (pat);
718 TREE_OPERAND (pat, 0)
719 = push_down_pack_expansion (exp, TREE_OPERAND (pat, 0));
720 TREE_OPERAND (pat, 1)
721 = push_down_pack_expansion (exp, TREE_OPERAND (pat, 1));
722 return pat;
724 default:
726 exp = copy_node (exp);
727 SET_PACK_EXPANSION_PATTERN (exp, pat);
728 return exp;
733 /* Transform a pack expansion into a constraint. First we transform the
734 pattern of the pack expansion, then we push the pack expansion down into the
735 leaves of the constraint so that partial ordering will work. */
737 tree
738 xform_pack_expansion (tree t)
740 tree pat = transform_expression (PACK_EXPANSION_PATTERN (t));
741 return push_down_pack_expansion (t, pat);
744 /* Transform an expression into a constraint. */
746 tree
747 xform_expr (tree t)
749 switch (TREE_CODE (t))
751 case TRUTH_ANDIF_EXPR:
752 return xform_logical (t, CONJ_CONSTR);
754 case TRUTH_ORIF_EXPR:
755 return xform_logical (t, DISJ_CONSTR);
757 case REQUIRES_EXPR:
758 return xform_requires_expr (t);
760 case BIND_EXPR:
761 return transform_expression (BIND_EXPR_BODY (t));
763 case EXPR_PACK_EXPANSION:
764 return xform_pack_expansion (t);
766 default:
767 /* All other constraints are atomic. */
768 return xform_atomic (t);
772 /* Transform a statement into an expression. */
774 tree
775 xform_stmt (tree t)
777 switch (TREE_CODE (t))
779 case RETURN_EXPR:
780 return transform_expression (TREE_OPERAND (t, 0));
781 default:
782 gcc_unreachable ();
784 return error_mark_node;
787 /* Reduction rules for the declaration T. */
789 tree
790 xform_decl (tree t)
792 switch (TREE_CODE (t))
794 case VAR_DECL:
795 return xform_atomic (t);
796 default:
797 gcc_unreachable ();
799 return error_mark_node;
802 /* Transform a lifted expression into a constraint. This either
803 returns a constraint, or it returns error_mark_node when
804 a constraint cannot be formed. */
806 tree
807 transform_expression (tree t)
809 if (!t)
810 return NULL_TREE;
812 if (t == error_mark_node)
813 return error_mark_node;
815 switch (TREE_CODE_CLASS (TREE_CODE (t)))
817 case tcc_unary:
818 case tcc_binary:
819 case tcc_expression:
820 case tcc_vl_exp:
821 return xform_expr (t);
823 case tcc_statement:
824 return xform_stmt (t);
826 case tcc_declaration:
827 return xform_decl (t);
829 case tcc_exceptional:
830 case tcc_constant:
831 case tcc_reference:
832 case tcc_comparison:
833 /* These are all atomic predicate constraints. */
834 return xform_atomic (t);
836 default:
837 /* Unhandled node kind. */
838 gcc_unreachable ();
840 return error_mark_node;
843 /*---------------------------------------------------------------------------
844 Constraint normalization
845 ---------------------------------------------------------------------------*/
847 tree normalize_constraint (tree);
849 /* The normal form of the disjunction T0 /\ T1 is the conjunction
850 of the normal form of T0 and the normal form of T1. */
852 inline tree
853 normalize_conjunction (tree t)
855 tree t0 = normalize_constraint (TREE_OPERAND (t, 0));
856 tree t1 = normalize_constraint (TREE_OPERAND (t, 1));
857 return build_nt (CONJ_CONSTR, t0, t1);
860 /* The normal form of the disjunction T0 \/ T1 is the disjunction
861 of the normal form of T0 and the normal form of T1. */
863 inline tree
864 normalize_disjunction (tree t)
866 tree t0 = normalize_constraint (TREE_OPERAND (t, 0));
867 tree t1 = normalize_constraint (TREE_OPERAND (t, 1));
868 return build_nt (DISJ_CONSTR, t0, t1);
871 /* A predicate constraint is normalized in two stages. First all
872 references specializations of concepts are replaced by their
873 substituted definitions. Then, the resulting expression is
874 transformed into a constraint by transforming && expressions
875 into conjunctions and || into disjunctions. */
877 tree
878 normalize_predicate_constraint (tree t)
880 ++processing_template_decl;
881 tree expr = PRED_CONSTR_EXPR (t);
882 tree lifted = lift_expression (expr);
883 tree constr = transform_expression (lifted);
884 --processing_template_decl;
885 return constr;
888 /* The normal form of a parameterized constraint is the normal
889 form of its operand. */
891 tree
892 normalize_parameterized_constraint (tree t)
894 tree parms = PARM_CONSTR_PARMS (t);
895 tree operand = normalize_constraint (PARM_CONSTR_OPERAND (t));
896 return build_nt (PARM_CONSTR, parms, operand);
899 /* Normalize the constraint T by reducing it so that it is
900 comprised of only conjunctions and disjunctions of atomic
901 constraints. */
903 tree
904 normalize_constraint (tree t)
906 if (!t)
907 return NULL_TREE;
909 if (t == error_mark_node)
910 return t;
912 switch (TREE_CODE (t))
914 case CONJ_CONSTR:
915 return normalize_conjunction (t);
917 case DISJ_CONSTR:
918 return normalize_disjunction (t);
920 case PRED_CONSTR:
921 return normalize_predicate_constraint (t);
923 case PARM_CONSTR:
924 return normalize_parameterized_constraint (t);
926 case EXPR_CONSTR:
927 case TYPE_CONSTR:
928 case ICONV_CONSTR:
929 case DEDUCT_CONSTR:
930 case EXCEPT_CONSTR:
931 /* These constraints are defined to be atomic. */
932 return t;
934 default:
935 /* CONSTR was not a constraint. */
936 gcc_unreachable();
938 return error_mark_node;
941 } /* namespace */
944 // -------------------------------------------------------------------------- //
945 // Constraint Semantic Processing
947 // The following functions are called by the parser and substitution rules
948 // to create and evaluate constraint-related nodes.
950 // The constraints associated with the current template parameters.
951 tree
952 current_template_constraints (void)
954 if (!current_template_parms)
955 return NULL_TREE;
956 tree tmpl_constr = TEMPLATE_PARM_CONSTRAINTS (current_template_parms);
957 return build_constraints (tmpl_constr, NULL_TREE);
960 // If the recently parsed TYPE declares or defines a template or template
961 // specialization, get its corresponding constraints from the current
962 // template parameters and bind them to TYPE's declaration.
963 tree
964 associate_classtype_constraints (tree type)
966 if (!type || type == error_mark_node || TREE_CODE (type) != RECORD_TYPE)
967 return type;
969 // An explicit class template specialization has no template
970 // parameters.
971 if (!current_template_parms)
972 return type;
974 if (CLASSTYPE_IS_TEMPLATE (type) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
976 tree decl = TYPE_STUB_DECL (type);
977 tree ci = current_template_constraints ();
979 // An implicitly instantiated member template declaration already
980 // has associated constraints. If it is defined outside of its
981 // class, then we need match these constraints against those of
982 // original declaration.
983 if (tree orig_ci = get_constraints (decl))
985 if (!equivalent_constraints (ci, orig_ci))
987 // FIXME: Improve diagnostics.
988 error ("%qT does not match any declaration", type);
989 return error_mark_node;
991 return type;
993 set_constraints (decl, ci);
995 return type;
998 namespace {
1000 // Create an empty constraint info block.
1001 inline tree_constraint_info*
1002 build_constraint_info ()
1004 return (tree_constraint_info *)make_node (CONSTRAINT_INFO);
1007 } // namespace
1009 /* Build a constraint-info object that contains the associated constraints
1010 of a declaration. This also includes the declaration's template
1011 requirements (TREQS) and any trailing requirements for a function
1012 declarator (DREQS). Note that both TREQS and DREQS must be constraints.
1014 If the declaration has neither template nor declaration requirements
1015 this returns NULL_TREE, indicating an unconstrained declaration. */
1017 tree
1018 build_constraints (tree tmpl_reqs, tree decl_reqs)
1020 gcc_assert (tmpl_reqs ? constraint_p (tmpl_reqs) : true);
1021 gcc_assert (decl_reqs ? constraint_p (decl_reqs) : true);
1023 if (!tmpl_reqs && !decl_reqs)
1024 return NULL_TREE;
1026 tree_constraint_info* ci = build_constraint_info ();
1027 ci->template_reqs = tmpl_reqs;
1028 ci->declarator_reqs = decl_reqs;
1029 ci->associated_constr = conjoin_constraints (tmpl_reqs, decl_reqs);
1031 ++processing_template_decl;
1032 ci->normalized_constr = normalize_constraint (ci->associated_constr);
1033 --processing_template_decl;
1035 ci->assumptions = decompose_assumptions (ci->normalized_constr);
1036 return (tree)ci;
1039 namespace {
1041 /* Returns true if any of the arguments in the template
1042 argument list is a wildcard or wildcard pack. */
1043 bool
1044 contains_wildcard_p (tree args)
1046 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1048 tree arg = TREE_VEC_ELT (args, i);
1049 if (TREE_CODE (arg) == WILDCARD_DECL)
1050 return true;
1052 return false;
1055 /* Build a new call expression, but don't actually generate
1056 a new function call. We just want the tree, not the
1057 semantics. */
1058 inline tree
1059 build_call_check (tree id)
1061 ++processing_template_decl;
1062 vec<tree, va_gc> *fargs = make_tree_vector();
1063 tree call = finish_call_expr (id, &fargs, false, false, tf_none);
1064 release_tree_vector (fargs);
1065 --processing_template_decl;
1066 return call;
1069 /* Build an expression that will check a variable concept. If any
1070 argument contains a wildcard, don't try to finish the variable
1071 template because we can't substitute into a non-existent
1072 declaration. */
1073 tree
1074 build_variable_check (tree id)
1076 gcc_assert (TREE_CODE (id) == TEMPLATE_ID_EXPR);
1077 if (contains_wildcard_p (TREE_OPERAND (id, 1)))
1078 return id;
1080 ++processing_template_decl;
1081 tree var = finish_template_variable (id);
1082 --processing_template_decl;
1083 return var;
1086 /* Construct a sequence of template arguments by prepending
1087 ARG to REST. Either ARG or REST may be null. */
1088 tree
1089 build_concept_check_arguments (tree arg, tree rest)
1091 gcc_assert (rest ? TREE_CODE (rest) == TREE_VEC : true);
1092 tree args;
1093 if (arg)
1095 int n = rest ? TREE_VEC_LENGTH (rest) : 0;
1096 args = make_tree_vec (n + 1);
1097 TREE_VEC_ELT (args, 0) = arg;
1098 if (rest)
1099 for (int i = 0; i < n; ++i)
1100 TREE_VEC_ELT (args, i + 1) = TREE_VEC_ELT (rest, i);
1101 int def = rest ? GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (rest) : 0;
1102 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, def + 1);
1104 else
1106 gcc_assert (rest != NULL_TREE);
1107 args = rest;
1109 return args;
1112 } // namespace
1114 /* Construct an expression that checks the concept given by
1115 TARGET. The TARGET must be:
1117 - an OVERLOAD referring to one or more function concepts
1118 - a BASELINK referring to an overload set of the above, or
1119 - a TEMPLTATE_DECL referring to a variable concept.
1121 ARG and REST are the explicit template arguments for the
1122 eventual concept check. */
1123 tree
1124 build_concept_check (tree target, tree arg, tree rest)
1126 tree args = build_concept_check_arguments (arg, rest);
1127 if (variable_template_p (target))
1128 return build_variable_check (lookup_template_variable (target, args));
1129 else
1130 return build_call_check (lookup_template_function (target, args));
1134 /* Returns a TYPE_DECL that contains sufficient information to
1135 build a template parameter of the same kind as PROTO and
1136 constrained by the concept declaration CNC. Note that PROTO
1137 is the first template parameter of CNC.
1139 If specified, ARGS provides additional arguments to the
1140 constraint check. */
1141 tree
1142 build_constrained_parameter (tree cnc, tree proto, tree args)
1144 tree name = DECL_NAME (cnc);
1145 tree type = TREE_TYPE (proto);
1146 tree decl = build_decl (input_location, TYPE_DECL, name, type);
1147 CONSTRAINED_PARM_PROTOTYPE (decl) = proto;
1148 CONSTRAINED_PARM_CONCEPT (decl) = cnc;
1149 CONSTRAINED_PARM_EXTRA_ARGS (decl) = args;
1150 return decl;
1153 /* Create a constraint expression for the given DECL that
1154 evaluates the requirements specified by CONSTR, a TYPE_DECL
1155 that contains all the information necessary to build the
1156 requirements (see finish_concept_name for the layout of
1157 that TYPE_DECL).
1159 Note that the constraints are neither reduced nor decomposed.
1160 That is done only after the requires clause has been parsed
1161 (or not). */
1162 tree
1163 finish_shorthand_constraint (tree decl, tree constr)
1165 /* No requirements means no constraints. */
1166 if (!constr)
1167 return NULL_TREE;
1169 tree proto = CONSTRAINED_PARM_PROTOTYPE (constr);
1170 tree con = CONSTRAINED_PARM_CONCEPT (constr);
1171 tree args = CONSTRAINED_PARM_EXTRA_ARGS (constr);
1173 /* If the parameter declaration is variadic, but the concept
1174 is not then we need to apply the concept to every element
1175 in the pack. */
1176 bool is_proto_pack = template_parameter_pack_p (proto);
1177 bool is_decl_pack = template_parameter_pack_p (decl);
1178 bool apply_to_all_p = is_decl_pack && !is_proto_pack;
1180 /* Get the argument and overload used for the requirement
1181 and adjust it if we're going to expand later. */
1182 tree arg = template_parm_to_arg (build_tree_list (NULL_TREE, decl));
1183 if (apply_to_all_p)
1184 arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
1186 /* Build the concept check. If it the constraint needs to be
1187 applied to all elements of the parameter pack, then make
1188 the constraint an expansion. */
1189 tree check;
1190 tree tmpl = DECL_TI_TEMPLATE (con);
1191 if (TREE_CODE (con) == VAR_DECL)
1193 check = build_concept_check (tmpl, arg, args);
1195 else
1197 tree ovl = build_overload (tmpl, NULL_TREE);
1198 check = build_concept_check (ovl, arg, args);
1201 /* Make the check a pack expansion if needed.
1203 FIXME: We should be making a fold expression. */
1204 if (apply_to_all_p)
1206 check = make_pack_expansion (check);
1207 TREE_TYPE (check) = boolean_type_node;
1210 return make_predicate_constraint (check);
1213 /* Returns a conjunction of shorthand requirements for the template
1214 parameter list PARMS. Note that the requirements are stored in
1215 the TYPE of each tree node. */
1216 tree
1217 get_shorthand_constraints (tree parms)
1219 tree result = NULL_TREE;
1220 parms = INNERMOST_TEMPLATE_PARMS (parms);
1221 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
1223 tree parm = TREE_VEC_ELT (parms, i);
1224 tree constr = TEMPLATE_PARM_CONSTRAINTS (parm);
1225 result = conjoin_constraints (result, constr);
1227 return result;
1230 // Returns and chains a new parameter for PARAMETER_LIST which will conform
1231 // to the prototype given by SRC_PARM. The new parameter will have its
1232 // identifier and location set according to IDENT and PARM_LOC respectively.
1233 static tree
1234 process_introduction_parm (tree parameter_list, tree src_parm)
1236 // If we have a pack, we should have a single pack argument which is the
1237 // placeholder we want to look at.
1238 bool is_parameter_pack = ARGUMENT_PACK_P (src_parm);
1239 if (is_parameter_pack)
1240 src_parm = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (src_parm), 0);
1242 // At this point we should have a wildcard, but we want to
1243 // grab the associated decl from it. Also grab the stored
1244 // identifier and location that should be chained to it in
1245 // a PARM_DECL.
1246 gcc_assert (TREE_CODE (src_parm) == WILDCARD_DECL);
1248 tree ident = DECL_NAME (src_parm);
1249 location_t parm_loc = DECL_SOURCE_LOCATION (src_parm);
1251 // If we expect a pack and the deduced template is not a pack, or if the
1252 // template is using a pack and we didn't declare a pack, throw an error.
1253 if (is_parameter_pack != WILDCARD_PACK_P (src_parm))
1255 error_at (parm_loc, "cannot match pack for introduced parameter");
1256 tree err_parm = build_tree_list (error_mark_node, error_mark_node);
1257 return chainon (parameter_list, err_parm);
1260 src_parm = TREE_TYPE (src_parm);
1262 tree parm;
1263 bool is_non_type;
1264 if (TREE_CODE (src_parm) == TYPE_DECL)
1266 is_non_type = false;
1267 parm = finish_template_type_parm (class_type_node, ident);
1269 else if (TREE_CODE (src_parm) == TEMPLATE_DECL)
1271 is_non_type = false;
1272 begin_template_parm_list ();
1273 current_template_parms = DECL_TEMPLATE_PARMS (src_parm);
1274 end_template_parm_list ();
1275 parm = finish_template_template_parm (class_type_node, ident);
1277 else
1279 is_non_type = true;
1281 // Since we don't have a declarator, so we can copy the source
1282 // parameter and change the name and eventually the location.
1283 parm = copy_decl (src_parm);
1284 DECL_NAME (parm) = ident;
1287 // Wrap in a TREE_LIST for process_template_parm. Introductions do not
1288 // retain the defaults from the source template.
1289 parm = build_tree_list (NULL_TREE, parm);
1291 return process_template_parm (parameter_list, parm_loc, parm,
1292 is_non_type, is_parameter_pack);
1295 /* Associates a constraint check to the current template based
1296 on the introduction parameters. INTRO_LIST must be a TREE_VEC
1297 of WILDCARD_DECLs containing a chained PARM_DECL which
1298 contains the identifier as well as the source location.
1299 TMPL_DECL is the decl for the concept being used. If we
1300 take a concept, C, this will form a check in the form of
1301 C<INTRO_LIST> filling in any extra arguments needed by the
1302 defaults deduced.
1304 Returns NULL_TREE if no concept could be matched and
1305 error_mark_node if an error occurred when matching. */
1306 tree
1307 finish_template_introduction (tree tmpl_decl, tree intro_list)
1309 /* Deduce the concept check. */
1310 tree expr = build_concept_check (tmpl_decl, NULL_TREE, intro_list);
1311 if (expr == error_mark_node)
1312 return NULL_TREE;
1314 tree parms = deduce_concept_introduction (expr);
1315 if (!parms)
1316 return NULL_TREE;
1318 /* Build template parameter scope for introduction. */
1319 tree parm_list = NULL_TREE;
1320 begin_template_parm_list ();
1321 int nargs = MIN (TREE_VEC_LENGTH (parms), TREE_VEC_LENGTH (intro_list));
1322 for (int n = 0; n < nargs; ++n)
1323 parm_list = process_introduction_parm (parm_list, TREE_VEC_ELT (parms, n));
1324 parm_list = end_template_parm_list (parm_list);
1325 for (int i = 0; i < TREE_VEC_LENGTH (parm_list); ++i)
1326 if (TREE_VALUE (TREE_VEC_ELT (parm_list, i)) == error_mark_node)
1328 end_template_decl ();
1329 return error_mark_node;
1332 /* Build a concept check for our constraint. */
1333 tree check_args = make_tree_vec (TREE_VEC_LENGTH (parms));
1334 int n = 0;
1335 for (; n < TREE_VEC_LENGTH (parm_list); ++n)
1337 tree parm = TREE_VEC_ELT (parm_list, n);
1338 TREE_VEC_ELT (check_args, n) = template_parm_to_arg (parm);
1340 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (check_args, n);
1342 /* If the template expects more parameters we should be able
1343 to use the defaults from our deduced concept. */
1344 for (; n < TREE_VEC_LENGTH (parms); ++n)
1345 TREE_VEC_ELT (check_args, n) = TREE_VEC_ELT (parms, n);
1347 /* Associate the constraint. */
1348 tree check = build_concept_check (tmpl_decl, NULL_TREE, check_args);
1349 tree constr = make_predicate_constraint (check);
1350 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = constr;
1352 return parm_list;
1356 /* Make a "constrained auto" type-specifier. This is an
1357 auto type with constraints that must be associated after
1358 deduction. The constraint is formed from the given
1359 CONC and its optional sequence of arguments, which are
1360 non-null if written as partial-concept-id. */
1361 tree
1362 make_constrained_auto (tree con, tree args)
1364 tree type = make_auto();
1366 /* Build the constraint. */
1367 tree tmpl = DECL_TI_TEMPLATE (con);
1368 tree expr;
1369 if (VAR_P (con))
1370 expr = build_concept_check (tmpl, type, args);
1371 else
1372 expr = build_concept_check (build_overload (tmpl, NULL_TREE), type, args);
1374 tree constr = make_predicate_constraint (expr);
1375 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
1377 /* Attach the constraint to the type declaration. */
1378 tree decl = TYPE_NAME (type);
1379 return decl;
1383 /*---------------------------------------------------------------------------
1384 Constraint substitution
1385 ---------------------------------------------------------------------------*/
1387 /* The following functions implement substitution rules for constraints.
1388 Substitution without checking constraints happens only in the
1389 instantiation of class templates. For example:
1391 template<C1 T> struct S {
1392 void f(T) requires C2<T>;
1393 void g(T) requires T::value;
1396 S<int> s; // error instantiating S<int>::g(T)
1398 When we instantiate S, we substitute into its member declarations,
1399 including their constraints. However, those constraints are not
1400 checked. Substituting int into C2<T> yields C2<int>, and substituting
1401 into T::value yields a substitution failure, making the program
1402 ill-formed.
1404 Note that we only ever substitute into the associated constraints
1405 of a declaration. That is, substitution is defined only for predicate
1406 constraints and conjunctions. */
1408 /* Substitute into the predicate constraints. Returns error_mark_node
1409 if the substitution into the expression fails. */
1410 tree
1411 tsubst_predicate_constraint (tree t, tree args,
1412 tsubst_flags_t complain, tree in_decl)
1414 tree expr = PRED_CONSTR_EXPR (t);
1415 ++processing_template_decl;
1416 tree result = tsubst_expr (expr, args, complain, in_decl, false);
1417 --processing_template_decl;
1418 return build_nt (PRED_CONSTR, result);
1421 /* Substitute into the conjunction of constraints. Returns
1422 error_mark_node if substitution into either operand fails. */
1423 tree
1424 tsubst_conjunction (tree t, tree args,
1425 tsubst_flags_t complain, tree in_decl)
1427 tree t0 = TREE_OPERAND (t, 0);
1428 tree r0 = tsubst_constraint (t0, args, complain, in_decl);
1429 tree t1 = TREE_OPERAND (t, 1);
1430 tree r1 = tsubst_constraint (t1, args, complain, in_decl);
1431 return build_nt (CONJ_CONSTR, r0, r1);
1434 /* Substitute ARGS into the constraint T. */
1435 tree
1436 tsubst_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1438 if (t == NULL_TREE)
1439 return t;
1440 if (TREE_CODE (t) == CONJ_CONSTR)
1441 return tsubst_conjunction (t, args, complain, in_decl);
1442 else if (TREE_CODE (t) == PRED_CONSTR)
1443 return tsubst_predicate_constraint (t, args, complain, in_decl);
1444 else
1445 gcc_unreachable ();
1446 return error_mark_node;
1449 namespace {
1451 /* A subroutine of tsubst_constraint_variables. Register local
1452 specializations for each of parameter in PARMS and its
1453 corresponding substituted constraint variable in VARS.
1454 Returns VARS. */
1455 tree
1456 declare_constraint_vars (tree parms, tree vars)
1458 tree s = vars;
1459 for (tree t = parms; t; t = DECL_CHAIN (t))
1461 if (DECL_PACK_P (t))
1463 tree pack = extract_fnparm_pack (t, &s);
1464 register_local_specialization (pack, t);
1466 else
1468 register_local_specialization (s, t);
1469 s = DECL_CHAIN (s);
1472 return vars;
1475 /* A subroutine of tsubst_parameterized_constraint. Substitute ARGS
1476 into the parameter list T, producing a sequence of constraint
1477 variables, declared in the current scope.
1479 Note that the caller must establish a local specialization stack
1480 prior to calling this function since this substitution will
1481 declare the substituted parameters. */
1482 tree
1483 tsubst_constraint_variables (tree t, tree args,
1484 tsubst_flags_t complain, tree in_decl)
1486 /* Clear cp_unevaluated_operand across tsubst so that we get a proper chain
1487 of PARM_DECLs. */
1488 int saved_unevaluated_operand = cp_unevaluated_operand;
1489 cp_unevaluated_operand = 0;
1490 tree vars = tsubst (t, args, complain, in_decl);
1491 cp_unevaluated_operand = saved_unevaluated_operand;
1492 if (vars == error_mark_node)
1493 return error_mark_node;
1494 return declare_constraint_vars (t, vars);
1497 /* Substitute ARGS into the simple requirement T. Note that
1498 substitution may result in an ill-formed expression without
1499 causing the program to be ill-formed. In such cases, the
1500 requirement wraps an error_mark_node. */
1501 inline tree
1502 tsubst_simple_requirement (tree t, tree args,
1503 tsubst_flags_t complain, tree in_decl)
1505 ++processing_template_decl;
1506 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1507 --processing_template_decl;
1508 return finish_simple_requirement (expr);
1511 /* Substitute ARGS into the type requirement T. Note that
1512 substitution may result in an ill-formed type without
1513 causing the program to be ill-formed. In such cases, the
1514 requirement wraps an error_mark_node. */
1516 inline tree
1517 tsubst_type_requirement (tree t, tree args,
1518 tsubst_flags_t complain, tree in_decl)
1520 ++processing_template_decl;
1521 tree type = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
1522 --processing_template_decl;
1523 return finish_type_requirement (type);
1526 /* Substitute args into the compound requirement T. If substituting
1527 into either the expression or the type fails, the corresponding
1528 operands in the resulting node will be error_mark_node. This
1529 preserves a requirement for the purpose of partial ordering, but
1530 it will never be satisfied. */
1532 tree
1533 tsubst_compound_requirement (tree t, tree args,
1534 tsubst_flags_t complain, tree in_decl)
1536 ++processing_template_decl;
1537 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1538 tree type = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
1539 --processing_template_decl;
1540 bool noexcept_p = COMPOUND_REQ_NOEXCEPT_P (t);
1541 return finish_compound_requirement (expr, type, noexcept_p);
1544 /* Substitute ARGS into the nested requirement T. */
1546 tree
1547 tsubst_nested_requirement (tree t, tree args,
1548 tsubst_flags_t complain, tree in_decl)
1550 ++processing_template_decl;
1551 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1552 --processing_template_decl;
1553 return finish_nested_requirement (expr);
1556 inline tree
1557 tsubst_requirement (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1559 switch (TREE_CODE (t))
1561 case SIMPLE_REQ:
1562 return tsubst_simple_requirement (t, args, complain, in_decl);
1563 case TYPE_REQ:
1564 return tsubst_type_requirement (t, args, complain, in_decl);
1565 case COMPOUND_REQ:
1566 return tsubst_compound_requirement (t, args, complain, in_decl);
1567 case NESTED_REQ:
1568 return tsubst_nested_requirement (t, args, complain, in_decl);
1569 default:
1570 gcc_unreachable ();
1572 return error_mark_node;
1575 /* Substitute ARGS into the list of requirements T. Note that
1576 substitution failures here result in ill-formed programs. */
1578 tree
1579 tsubst_requirement_body (tree t, tree args,
1580 tsubst_flags_t complain, tree in_decl)
1582 tree r = NULL_TREE;
1583 while (t)
1585 tree e = tsubst_requirement (TREE_VALUE (t), args, complain, in_decl);
1586 if (e == error_mark_node)
1587 return error_mark_node;
1588 r = tree_cons (NULL_TREE, e, r);
1589 t = TREE_CHAIN (t);
1591 return r;
1594 } /* namespace */
1596 /* Substitute ARGS into the requires expression T. Note that this
1597 results in the re-declaration of local parameters when
1598 substituting through the parameter list. If either substitution
1599 fails, the program is ill-formed. */
1601 tree
1602 tsubst_requires_expr (tree t, tree args,
1603 tsubst_flags_t complain, tree in_decl)
1605 local_specialization_stack stack;
1607 tree parms = TREE_OPERAND (t, 0);
1608 if (parms)
1610 parms = tsubst_constraint_variables (parms, args, complain, in_decl);
1611 if (parms == error_mark_node)
1612 return error_mark_node;
1615 tree reqs = TREE_OPERAND (t, 1);
1616 reqs = tsubst_requirement_body (reqs, args, complain, in_decl);
1617 if (reqs == error_mark_node)
1618 return error_mark_node;
1620 return finish_requires_expr (parms, reqs);
1623 /* Substitute ARGS into the constraint information CI, producing a new
1624 constraint record. */
1625 tree
1626 tsubst_constraint_info (tree t, tree args,
1627 tsubst_flags_t complain, tree in_decl)
1629 if (!t || t == error_mark_node || !check_constraint_info (t))
1630 return NULL_TREE;
1632 tree tmpl_constr = NULL_TREE;
1633 if (tree r = CI_TEMPLATE_REQS (t))
1634 tmpl_constr = tsubst_constraint (r, args, complain, in_decl);
1636 tree decl_constr = NULL_TREE;
1637 if (tree r = CI_DECLARATOR_REQS (t))
1638 decl_constr = tsubst_constraint (r, args, complain, in_decl);
1640 return build_constraints (tmpl_constr, decl_constr);
1644 /*---------------------------------------------------------------------------
1645 Constraint satisfaction
1646 ---------------------------------------------------------------------------*/
1648 /* The following functions determine if a constraint, when
1649 substituting template arguments, is satisfied. For convenience,
1650 satisfaction reduces a constraint to either true or false (and
1651 nothing else). */
1653 namespace {
1655 tree satisfy_constraint_1 (tree, tree, tsubst_flags_t, tree);
1657 /* Check the constraint pack expansion. */
1659 tree
1660 satisfy_pack_expansion (tree t, tree args,
1661 tsubst_flags_t complain, tree in_decl)
1663 /* Get the vector of satisfaction results.
1664 gen_elem_of_pack_expansion_instantiation will check that each element of
1665 the expansion is satisfied. */
1666 tree exprs = tsubst_pack_expansion (t, args, complain, in_decl);
1667 if (exprs == error_mark_node)
1668 return boolean_false_node;
1669 int n = TREE_VEC_LENGTH (exprs);
1671 for (int i = 0; i < n; ++i)
1672 if (TREE_VEC_ELT (exprs, i) != boolean_true_node)
1673 return boolean_false_node;
1674 return boolean_true_node;
1677 /* A predicate constraint is satisfied if its expression evaluates
1678 to true. If substitution into that node fails, the constraint
1679 is not satisfied ([temp.constr.pred]).
1681 Note that a predicate constraint is a constraint expression
1682 of type bool. If neither of those are true, the program is
1683 ill-formed; they are not SFINAE'able errors. */
1685 tree
1686 satisfy_predicate_constraint (tree t, tree args,
1687 tsubst_flags_t complain, tree in_decl)
1689 tree original = TREE_OPERAND (t, 0);
1691 /* We should never have a naked pack expansion in a predicate constraint. */
1692 gcc_assert (TREE_CODE (original) != EXPR_PACK_EXPANSION);
1694 tree expr = tsubst_expr (original, args, complain, in_decl, false);
1695 if (expr == error_mark_node)
1696 return boolean_false_node;
1698 /* A predicate constraint shall have type bool. In some
1699 cases, substitution gives us const-qualified bool, which
1700 is also acceptable. */
1701 tree type = cv_unqualified (TREE_TYPE (expr));
1702 if (!same_type_p (type, boolean_type_node))
1704 error_at (EXPR_LOC_OR_LOC (expr, input_location),
1705 "constraint %qE does not have type %qT",
1706 expr, boolean_type_node);
1707 return boolean_false_node;
1710 tree value = cxx_constant_value (expr);
1711 return value;
1714 /* Check an expression constraint. The constraint is satisfied if
1715 substitution succeeds ([temp.constr.expr]).
1717 Note that the expression is unevaluated. */
1719 tree
1720 satisfy_expression_constraint (tree t, tree args,
1721 tsubst_flags_t complain, tree in_decl)
1723 cp_unevaluated guard;
1724 deferring_access_check_sentinel deferring;
1726 tree expr = EXPR_CONSTR_EXPR (t);
1727 tree check = tsubst_expr (expr, args, complain, in_decl, false);
1728 if (check == error_mark_node)
1729 return boolean_false_node;
1730 if (!perform_deferred_access_checks (tf_none))
1731 return boolean_false_node;
1733 return boolean_true_node;
1736 /* Check a type constraint. The constraint is satisfied if
1737 substitution succeeds. */
1739 inline tree
1740 satisfy_type_constraint (tree t, tree args,
1741 tsubst_flags_t complain, tree in_decl)
1743 deferring_access_check_sentinel deferring;
1744 tree type = TYPE_CONSTR_TYPE (t);
1745 gcc_assert (TYPE_P (type) || type == error_mark_node);
1746 tree check = tsubst (type, args, complain, in_decl);
1747 if (error_operand_p (check))
1748 return boolean_false_node;
1749 if (!perform_deferred_access_checks (complain))
1750 return boolean_false_node;
1752 return boolean_true_node;
1755 /* Check an implicit conversion constraint. */
1757 tree
1758 satisfy_implicit_conversion_constraint (tree t, tree args,
1759 tsubst_flags_t complain, tree in_decl)
1761 /* Don't tsubst as if we're processing a template. If we try
1762 to we can end up generating template-like expressions
1763 (e.g., modop-exprs) that aren't properly typed. */
1764 tree expr =
1765 tsubst_expr (ICONV_CONSTR_EXPR (t), args, complain, in_decl, false);
1766 if (expr == error_mark_node)
1767 return boolean_false_node;
1769 /* Get the transformed target type. */
1770 tree type = tsubst (ICONV_CONSTR_TYPE (t), args, complain, in_decl);
1771 if (type == error_mark_node)
1772 return boolean_false_node;
1774 /* Attempt the conversion as a direct initialization
1775 of the form TYPE <unspecified> = EXPR. */
1776 tree conv =
1777 perform_direct_initialization_if_possible (type, expr, false, complain);
1778 if (conv == NULL_TREE || conv == error_mark_node)
1779 return boolean_false_node;
1780 else
1781 return boolean_true_node;
1784 /* Check an argument deduction constraint. */
1786 tree
1787 satisfy_argument_deduction_constraint (tree t, tree args,
1788 tsubst_flags_t complain, tree in_decl)
1790 /* Substitute through the expression. */
1791 tree expr = DEDUCT_CONSTR_EXPR (t);
1792 tree init = tsubst_expr (expr, args, complain, in_decl, false);
1793 if (expr == error_mark_node)
1794 return boolean_false_node;
1796 /* Perform auto or decltype(auto) deduction to get the result. */
1797 tree pattern = DEDUCT_CONSTR_PATTERN (t);
1798 tree placeholder = DEDUCT_CONSTR_PLACEHOLDER (t);
1799 tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (placeholder);
1800 PLACEHOLDER_TYPE_CONSTRAINTS (placeholder)
1801 = tsubst_constraint (constr, args, complain|tf_partial, in_decl);
1802 tree type = do_auto_deduction (pattern, init, placeholder,
1803 complain, adc_requirement);
1804 PLACEHOLDER_TYPE_CONSTRAINTS (placeholder) = constr;
1805 if (type == error_mark_node)
1806 return boolean_false_node;
1808 return boolean_true_node;
1811 /* Check an exception constraint. An exception constraint for an
1812 expression e is satisfied when noexcept(e) is true. */
1814 tree
1815 satisfy_exception_constraint (tree t, tree args,
1816 tsubst_flags_t complain, tree in_decl)
1818 tree expr = EXCEPT_CONSTR_EXPR (t);
1819 tree check = tsubst_expr (expr, args, complain, in_decl, false);
1820 if (check == error_mark_node)
1821 return boolean_false_node;
1823 if (expr_noexcept_p (check, complain))
1824 return boolean_true_node;
1825 else
1826 return boolean_false_node;
1829 /* Check a parameterized constraint. */
1831 tree
1832 satisfy_parameterized_constraint (tree t, tree args,
1833 tsubst_flags_t complain, tree in_decl)
1835 local_specialization_stack stack;
1836 tree parms = PARM_CONSTR_PARMS (t);
1837 tree vars = tsubst_constraint_variables (parms, args, complain, in_decl);
1838 if (vars == error_mark_node)
1839 return boolean_false_node;
1840 tree constr = PARM_CONSTR_OPERAND (t);
1841 return satisfy_constraint_1 (constr, args, complain, in_decl);
1844 /* Check that the conjunction of constraints is satisfied. Note
1845 that if left operand is not satisfied, the right operand
1846 is not checked.
1848 FIXME: Check that this wouldn't result in a user-defined
1849 operator. Note that this error is partially diagnosed in
1850 satisfy_predicate_constraint. It would be nice to diagnose
1851 the overload, but I don't think it's strictly necessary. */
1853 tree
1854 satisfy_conjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1856 tree t0 = satisfy_constraint_1 (TREE_OPERAND (t, 0), args, complain, in_decl);
1857 if (t0 == boolean_false_node)
1858 return t0;
1859 tree t1 = satisfy_constraint_1 (TREE_OPERAND (t, 1), args, complain, in_decl);
1860 if (t1 == boolean_false_node)
1861 return t1;
1862 return boolean_true_node;
1865 /* Check that the disjunction of constraints is satisfied. Note
1866 that if the left operand is satisfied, the right operand is not
1867 checked. */
1869 tree
1870 satisfy_disjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1872 tree t0 = satisfy_constraint_1 (TREE_OPERAND (t, 0), args, complain, in_decl);
1873 if (t0 == boolean_true_node)
1874 return boolean_true_node;
1875 tree t1 = satisfy_constraint_1 (TREE_OPERAND (t, 1), args, complain, in_decl);
1876 if (t1 == boolean_true_node)
1877 return boolean_true_node;
1878 return boolean_false_node;
1881 /* Dispatch to an appropriate satisfaction routine depending on the
1882 tree code of T. */
1884 tree
1885 satisfy_constraint_1 (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1887 gcc_assert (!processing_template_decl);
1889 if (!t)
1890 return boolean_false_node;
1892 if (t == error_mark_node)
1893 return boolean_false_node;
1895 switch (TREE_CODE (t))
1897 case PRED_CONSTR:
1898 return satisfy_predicate_constraint (t, args, complain, in_decl);
1900 case EXPR_CONSTR:
1901 return satisfy_expression_constraint (t, args, complain, in_decl);
1903 case TYPE_CONSTR:
1904 return satisfy_type_constraint (t, args, complain, in_decl);
1906 case ICONV_CONSTR:
1907 return satisfy_implicit_conversion_constraint (t, args, complain, in_decl);
1909 case DEDUCT_CONSTR:
1910 return satisfy_argument_deduction_constraint (t, args, complain, in_decl);
1912 case EXCEPT_CONSTR:
1913 return satisfy_exception_constraint (t, args, complain, in_decl);
1915 case PARM_CONSTR:
1916 return satisfy_parameterized_constraint (t, args, complain, in_decl);
1918 case CONJ_CONSTR:
1919 return satisfy_conjunction (t, args, complain, in_decl);
1921 case DISJ_CONSTR:
1922 return satisfy_disjunction (t, args, complain, in_decl);
1924 case EXPR_PACK_EXPANSION:
1925 return satisfy_pack_expansion (t, args, complain, in_decl);
1927 default:
1928 gcc_unreachable ();
1930 return boolean_false_node;
1933 /* Check that the constraint is satisfied, according to the rules
1934 for that constraint. Note that each satisfy_* function returns
1935 true or false, depending on whether it is satisfied or not. */
1937 tree
1938 satisfy_constraint (tree t, tree args)
1940 /* Turn off template processing. Constraint satisfaction only applies
1941 to non-dependent terms, so we want full checking here. */
1942 processing_template_decl_sentinel sentinel (true);
1943 /* Avoid early exit in tsubst and tsubst_copy from null args; since earlier
1944 substitution was done with processing_template_decl forced on, there will
1945 be expressions that still need semantic processing, possibly buried in
1946 decltype or a template argument. */
1947 if (args == NULL_TREE)
1948 args = make_tree_vec (1);
1949 return satisfy_constraint_1 (t, args, tf_none, NULL_TREE);
1952 /* Check the associated constraints in CI against the given
1953 ARGS, returning true when the constraints are satisfied
1954 and false otherwise. */
1956 tree
1957 satisfy_associated_constraints (tree ci, tree args)
1959 /* If there are no constraints then this is trivially satisfied. */
1960 if (!ci)
1961 return boolean_true_node;
1963 /* If any arguments depend on template parameters, we can't
1964 check constraints. */
1965 if (args && uses_template_parms (args))
1966 return boolean_true_node;
1968 /* Invalid requirements cannot be satisfied. */
1969 if (!valid_constraints_p (ci))
1970 return boolean_false_node;
1972 return satisfy_constraint (CI_NORMALIZED_CONSTRAINTS (ci), args);
1975 } /* namespace */
1977 /* Evaluate the given constraint, returning boolean_true_node
1978 if the constraint is satisfied and boolean_false_node
1979 otherwise. */
1981 tree
1982 evaluate_constraints (tree constr, tree args)
1984 gcc_assert (constraint_p (constr));
1985 return satisfy_constraint (normalize_constraint (constr), args);
1988 /* Evaluate the function concept FN by substituting its own args
1989 into its definition and evaluating that as the result. Returns
1990 boolean_true_node if the constraints are satisfied and
1991 boolean_false_node otherwise. */
1993 tree
1994 evaluate_function_concept (tree fn, tree args)
1996 ++processing_template_decl;
1997 /* We lift using DECL_TI_ARGS because we want to delay producing
1998 non-dependent expressions until we're doing satisfaction. We can't just
1999 go without any substitution because we need to lower the level of 'auto's
2000 in type deduction constraints. */
2001 tree constr = transform_expression (lift_function_definition
2002 (fn, DECL_TI_ARGS (fn)));
2003 --processing_template_decl;
2004 return satisfy_constraint (constr, args);
2007 /* Evaluate the variable concept VAR by substituting its own args into
2008 its initializer and checking the resulting constraint. Returns
2009 boolean_true_node if the constraints are satisfied and
2010 boolean_false_node otherwise. */
2012 tree
2013 evaluate_variable_concept (tree decl, tree args)
2015 ++processing_template_decl;
2016 tree constr = transform_expression (lift_variable_initializer
2017 (decl, DECL_TI_ARGS (decl)));
2018 --processing_template_decl;
2019 return satisfy_constraint (constr, args);
2022 /* Evaluate the given expression as if it were a predicate
2023 constraint. Returns boolean_true_node if the constraint
2024 is satisfied and boolean_false_node otherwise. */
2026 tree
2027 evaluate_constraint_expression (tree expr, tree args)
2029 ++processing_template_decl;
2030 tree constr = transform_expression (lift_expression (expr));
2031 --processing_template_decl;
2032 return satisfy_constraint (constr, args);
2035 /* Returns true if the DECL's constraints are satisfied.
2036 This is used in cases where a declaration is formed but
2037 before it is used (e.g., overload resolution). */
2039 bool
2040 constraints_satisfied_p (tree decl)
2042 /* Get the constraints to check for satisfaction. This depends
2043 on whether we're looking at a template specialization or not. */
2044 tree ci;
2045 tree args = NULL_TREE;
2046 if (tree ti = DECL_TEMPLATE_INFO (decl))
2048 ci = get_constraints (TI_TEMPLATE (ti));
2049 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
2051 else
2053 ci = get_constraints (decl);
2056 tree eval = satisfy_associated_constraints (ci, args);
2057 return eval == boolean_true_node;
2060 /* Returns true if the constraints are satisfied by ARGS.
2061 Here, T can be either a constraint or a constrained
2062 declaration. */
2064 bool
2065 constraints_satisfied_p (tree t, tree args)
2067 tree eval;
2068 if (constraint_p (t))
2069 eval = evaluate_constraints (t, args);
2070 else
2071 eval = satisfy_associated_constraints (get_constraints (t), args);
2072 return eval == boolean_true_node;
2075 namespace
2078 /* Normalize EXPR and determine if the resulting constraint is
2079 satisfied by ARGS. Returns true if and only if the constraint
2080 is satisfied. This is used extensively by diagnostics to
2081 determine causes for failure. */
2083 inline bool
2084 constraint_expression_satisfied_p (tree expr, tree args)
2086 return evaluate_constraint_expression (expr, args) == boolean_true_node;
2089 } /* namespace */
2092 /*---------------------------------------------------------------------------
2093 Semantic analysis of requires-expressions
2094 ---------------------------------------------------------------------------*/
2096 /* Finish a requires expression for the given PARMS (possibly
2097 null) and the non-empty sequence of requirements. */
2098 tree
2099 finish_requires_expr (tree parms, tree reqs)
2101 /* Modify the declared parameters by removing their context
2102 so they don't refer to the enclosing scope and explicitly
2103 indicating that they are constraint variables. */
2104 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
2106 DECL_CONTEXT (parm) = NULL_TREE;
2107 CONSTRAINT_VAR_P (parm) = true;
2110 /* Build the node. */
2111 tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs);
2112 TREE_SIDE_EFFECTS (r) = false;
2113 TREE_CONSTANT (r) = true;
2114 return r;
2117 /* Construct a requirement for the validity of EXPR. */
2118 tree
2119 finish_simple_requirement (tree expr)
2121 return build_nt (SIMPLE_REQ, expr);
2124 /* Construct a requirement for the validity of TYPE. */
2125 tree
2126 finish_type_requirement (tree type)
2128 return build_nt (TYPE_REQ, type);
2131 /* Construct a requirement for the validity of EXPR, along with
2132 its properties. if TYPE is non-null, then it specifies either
2133 an implicit conversion or argument deduction constraint,
2134 depending on whether any placeholders occur in the type name.
2135 NOEXCEPT_P is true iff the noexcept keyword was specified. */
2136 tree
2137 finish_compound_requirement (tree expr, tree type, bool noexcept_p)
2139 tree req = build_nt (COMPOUND_REQ, expr, type);
2140 COMPOUND_REQ_NOEXCEPT_P (req) = noexcept_p;
2141 return req;
2144 /* Finish a nested requirement. */
2145 tree
2146 finish_nested_requirement (tree expr)
2148 return build_nt (NESTED_REQ, expr);
2151 // Check that FN satisfies the structural requirements of a
2152 // function concept definition.
2153 tree
2154 check_function_concept (tree fn)
2156 // Check that the function is comprised of only a single
2157 // return statement.
2158 tree body = DECL_SAVED_TREE (fn);
2159 if (TREE_CODE (body) == BIND_EXPR)
2160 body = BIND_EXPR_BODY (body);
2162 // Sometimes a function call results in the creation of clean up
2163 // points. Allow these to be preserved in the body of the
2164 // constraint, as we might actually need them for some constexpr
2165 // evaluations.
2166 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
2167 body = TREE_OPERAND (body, 0);
2169 /* Check that the definition is written correctly. */
2170 if (TREE_CODE (body) != RETURN_EXPR)
2172 location_t loc = DECL_SOURCE_LOCATION (fn);
2173 if (TREE_CODE (body) == STATEMENT_LIST && !STATEMENT_LIST_HEAD (body))
2174 error_at (loc, "definition of concept %qD is empty", fn);
2175 else
2176 error_at (loc, "definition of concept %qD has multiple statements", fn);
2179 return NULL_TREE;
2183 // Check that a constrained friend declaration function declaration,
2184 // FN, is admissible. This is the case only when the declaration depends
2185 // on template parameters and does not declare a specialization.
2186 void
2187 check_constrained_friend (tree fn, tree reqs)
2189 if (fn == error_mark_node)
2190 return;
2191 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
2193 // If there are not constraints, this cannot be an error.
2194 if (!reqs)
2195 return;
2197 // Constrained friend functions that don't depend on template
2198 // arguments are effectively meaningless.
2199 if (!uses_template_parms (TREE_TYPE (fn)))
2201 error_at (location_of (fn),
2202 "constrained friend does not depend on template parameters");
2203 return;
2207 /*---------------------------------------------------------------------------
2208 Equivalence of constraints
2209 ---------------------------------------------------------------------------*/
2211 /* Returns true when A and B are equivalent constraints. */
2212 bool
2213 equivalent_constraints (tree a, tree b)
2215 gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2216 gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2217 return cp_tree_equal (a, b);
2220 /* Returns true if the template declarations A and B have equivalent
2221 constraints. This is the case when A's constraints subsume B's and
2222 when B's also constrain A's. */
2223 bool
2224 equivalently_constrained (tree d1, tree d2)
2226 gcc_assert (TREE_CODE (d1) == TREE_CODE (d2));
2227 return equivalent_constraints (get_constraints (d1), get_constraints (d2));
2230 /*---------------------------------------------------------------------------
2231 Partial ordering of constraints
2232 ---------------------------------------------------------------------------*/
2234 /* Returns true when the the constraints in A subsume those in B. */
2235 bool
2236 subsumes_constraints (tree a, tree b)
2238 gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2239 gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2240 return subsumes (a, b);
2243 /* Determines which of the declarations, A or B, is more constrained.
2244 That is, which declaration's constraints subsume but are not subsumed
2245 by the other's?
2247 Returns 1 if A is more constrained than B, -1 if B is more constrained
2248 than A, and 0 otherwise. */
2250 more_constrained (tree d1, tree d2)
2252 tree c1 = get_constraints (d1);
2253 tree c2 = get_constraints (d2);
2254 int winner = 0;
2255 if (subsumes_constraints (c1, c2))
2256 ++winner;
2257 if (subsumes_constraints (c2, c1))
2258 --winner;
2259 return winner;
2262 /* Returns true if D1 is at least as constrained as D2. That is, the
2263 associated constraints of D1 subsume those of D2, or both declarations
2264 are unconstrained. */
2265 bool
2266 at_least_as_constrained (tree d1, tree d2)
2268 tree c1 = get_constraints (d1);
2269 tree c2 = get_constraints (d2);
2270 return subsumes_constraints (c1, c2);
2274 /*---------------------------------------------------------------------------
2275 Constraint diagnostics
2276 ---------------------------------------------------------------------------*/
2278 /* The diagnosis of constraints performs a combination of
2279 normalization and satisfaction testing. We recursively
2280 walk through the conjunction (or disjunctions) of associated
2281 constraints, testing each sub-expression in turn.
2283 We currently restrict diagnostics to just the top-level
2284 conjunctions within the associated constraints. A fully
2285 recursive walk is possible, but it can generate a lot
2286 of errors. */
2289 namespace {
2291 void diagnose_expression (location_t, tree, tree);
2292 void diagnose_constraint (location_t, tree, tree);
2294 /* Diagnose a conjunction of constraints. */
2295 void
2296 diagnose_logical_operation (location_t loc, tree t, tree args)
2298 diagnose_expression (loc, TREE_OPERAND (t, 0), args);
2299 diagnose_expression (loc, TREE_OPERAND (t, 0), args);
2302 /* Determine if the trait expression T is satisfied by ARGS.
2303 Emit a precise diagnostic if it is not. */
2304 void
2305 diagnose_trait_expression (location_t loc, tree t, tree args)
2307 if (constraint_expression_satisfied_p (t, args))
2308 return;
2310 /* Rebuild the trait expression so we can diagnose the
2311 specific failure. */
2312 ++processing_template_decl;
2313 tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
2314 --processing_template_decl;
2316 tree t1 = TRAIT_EXPR_TYPE1 (expr);
2317 tree t2 = TRAIT_EXPR_TYPE2 (expr);
2318 switch (TRAIT_EXPR_KIND (t))
2320 case CPTK_HAS_NOTHROW_ASSIGN:
2321 inform (loc, " %qT is not nothrow copy assignable", t1);
2322 break;
2323 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
2324 inform (loc, " %qT is not nothrow default constructible", t1);
2325 break;
2326 case CPTK_HAS_NOTHROW_COPY:
2327 inform (loc, " %qT is not nothrow copy constructible", t1);
2328 break;
2329 case CPTK_HAS_TRIVIAL_ASSIGN:
2330 inform (loc, " %qT is not trivially copy assignable", t1);
2331 break;
2332 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
2333 inform (loc, " %qT is not trivially default constructible", t1);
2334 break;
2335 case CPTK_HAS_TRIVIAL_COPY:
2336 inform (loc, " %qT is not trivially copy constructible", t1);
2337 break;
2338 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
2339 inform (loc, " %qT is not trivially destructible", t1);
2340 break;
2341 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
2342 inform (loc, " %qT does not have a virtual destructor", t1);
2343 break;
2344 case CPTK_IS_ABSTRACT:
2345 inform (loc, " %qT is not an abstract class", t1);
2346 break;
2347 case CPTK_IS_BASE_OF:
2348 inform (loc, " %qT is not a base of %qT", t1, t2);
2349 break;
2350 case CPTK_IS_CLASS:
2351 inform (loc, " %qT is not a class", t1);
2352 break;
2353 case CPTK_IS_EMPTY:
2354 inform (loc, " %qT is not an empty class", t1);
2355 break;
2356 case CPTK_IS_ENUM:
2357 inform (loc, " %qT is not an enum", t1);
2358 break;
2359 case CPTK_IS_FINAL:
2360 inform (loc, " %qT is not a final class", t1);
2361 break;
2362 case CPTK_IS_LITERAL_TYPE:
2363 inform (loc, " %qT is not a literal type", t1);
2364 break;
2365 case CPTK_IS_POD:
2366 inform (loc, " %qT is not a POD type", t1);
2367 break;
2368 case CPTK_IS_POLYMORPHIC:
2369 inform (loc, " %qT is not a polymorphic type", t1);
2370 break;
2371 case CPTK_IS_SAME_AS:
2372 inform (loc, " %qT is not the same as %qT", t1, t2);
2373 break;
2374 case CPTK_IS_STD_LAYOUT:
2375 inform (loc, " %qT is not an standard layout type", t1);
2376 break;
2377 case CPTK_IS_TRIVIAL:
2378 inform (loc, " %qT is not a trivial type", t1);
2379 break;
2380 case CPTK_IS_UNION:
2381 inform (loc, " %qT is not a union", t1);
2382 break;
2383 default:
2384 gcc_unreachable ();
2388 /* Determine if the call expression T, when normalized as a constraint,
2389 is satisfied by ARGS.
2391 TODO: If T is refers to a concept, We could recursively analyze
2392 its definition to identify the exact failure, but that could
2393 emit a *lot* of error messages (defeating the purpose of
2394 improved diagnostics). Consider adding a flag to control the
2395 depth of diagnostics. */
2396 void
2397 diagnose_call_expression (location_t loc, tree t, tree args)
2399 if (constraint_expression_satisfied_p (t, args))
2400 return;
2402 /* Rebuild the expression for the purpose of diagnostics. */
2403 ++processing_template_decl;
2404 tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
2405 --processing_template_decl;
2407 /* If the function call is known to be a concept check, then
2408 diagnose it differently (i.e., we may recurse). */
2409 if (resolve_constraint_check (t))
2410 inform (loc, " concept %qE was not satisfied", expr);
2411 else
2412 inform (loc, " %qE evaluated to false", expr);
2415 /* Determine if the template-id T, when normalized as a constraint
2416 is satisfied by ARGS. */
2417 void
2418 diagnose_template_id (location_t loc, tree t, tree args)
2420 /* Check for invalid template-ids. */
2421 if (!variable_template_p (TREE_OPERAND (t, 0)))
2423 inform (loc, " invalid constraint %qE", t);
2424 return;
2427 if (constraint_expression_satisfied_p (t, args))
2428 return;
2430 /* Rebuild the expression for the purpose of diagnostics. */
2431 ++processing_template_decl;
2432 tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
2433 --processing_template_decl;
2435 tree var = DECL_TEMPLATE_RESULT (TREE_OPERAND (t, 0));
2436 if (DECL_DECLARED_CONCEPT_P (var))
2437 inform (loc, " concept %qE was not satisfied", expr);
2438 else
2439 inform (loc, " %qE evaluated to false", expr);
2442 /* Determine if the requires-expression, when normalized as a
2443 constraint is satisfied by ARGS.
2445 TODO: Build sets of expressions, types, and constraints
2446 based on the requirements in T and emit specific diagnostics
2447 for those. */
2448 void
2449 diagnose_requires_expression (location_t loc, tree t, tree args)
2451 if (constraint_expression_satisfied_p (t, args))
2452 return;
2453 inform (loc, "requirements not satisfied");
2456 void
2457 diagnose_pack_expansion (location_t loc, tree t, tree args)
2459 if (constraint_expression_satisfied_p (t, args))
2460 return;
2462 /* Make sure that we don't have naked packs that we don't expect. */
2463 if (!same_type_p (TREE_TYPE (t), boolean_type_node))
2465 inform (loc, "invalid pack expansion in constraint %qE", t);
2466 return;
2469 inform (loc, " in the expansion of %qE", t);
2471 /* Get the vector of expanded arguments. Note that n must not
2472 be 0 since this constraint is not satisfied. */
2473 ++processing_template_decl;
2474 tree exprs = tsubst_pack_expansion (t, args, tf_none, NULL_TREE);
2475 --processing_template_decl;
2476 if (exprs == error_mark_node)
2478 /* TODO: This error message could be better. */
2479 inform (loc, " substitution failure occurred during expansion");
2480 return;
2483 /* Check each expanded constraint separately. */
2484 int n = TREE_VEC_LENGTH (exprs);
2485 for (int i = 0; i < n; ++i)
2487 tree expr = TREE_VEC_ELT (exprs, i);
2488 if (!constraint_expression_satisfied_p (expr, args))
2489 inform (loc, " %qE was not satisfied", expr);
2493 /* Diagnose an expression that would be characterized as
2494 a predicate constraint. */
2495 void
2496 diagnose_other_expression (location_t loc, tree t, tree args)
2498 if (constraint_expression_satisfied_p (t, args))
2499 return;
2500 inform (loc, " %qE evaluated to false", t);
2503 void
2504 diagnose_expression (location_t loc, tree t, tree args)
2506 switch (TREE_CODE (t))
2508 case TRUTH_ANDIF_EXPR:
2509 diagnose_logical_operation (loc, t, args);
2510 break;
2512 case TRUTH_ORIF_EXPR:
2513 diagnose_logical_operation (loc, t, args);
2514 break;
2516 case CALL_EXPR:
2517 diagnose_call_expression (loc, t, args);
2518 break;
2520 case TEMPLATE_ID_EXPR:
2521 diagnose_template_id (loc, t, args);
2522 break;
2524 case REQUIRES_EXPR:
2525 diagnose_requires_expression (loc, t, args);
2526 break;
2528 case TRAIT_EXPR:
2529 diagnose_trait_expression (loc, t, args);
2530 break;
2532 case EXPR_PACK_EXPANSION:
2533 diagnose_pack_expansion (loc, t, args);
2534 break;
2536 default:
2537 diagnose_other_expression (loc, t, args);
2538 break;
2542 inline void
2543 diagnose_predicate_constraint (location_t loc, tree t, tree args)
2545 diagnose_expression (loc, PRED_CONSTR_EXPR (t), args);
2548 inline void
2549 diagnose_conjunction (location_t loc, tree t, tree args)
2551 diagnose_constraint (loc, TREE_OPERAND (t, 0), args);
2552 diagnose_constraint (loc, TREE_OPERAND (t, 1), args);
2555 /* Diagnose the constraint T for the given ARGS. This is only
2556 ever invoked on the associated constraints, so we can
2557 only have conjunctions of predicate constraints. */
2558 void
2559 diagnose_constraint (location_t loc, tree t, tree args)
2561 switch (TREE_CODE (t))
2563 case CONJ_CONSTR:
2564 diagnose_conjunction (loc, t, args);
2565 break;
2567 case PRED_CONSTR:
2568 diagnose_predicate_constraint (loc, t, args);
2569 break;
2571 default:
2572 gcc_unreachable ();
2573 break;
2577 /* Diagnose the reason(s) why ARGS do not satisfy the constraints
2578 of declaration DECL. */
2580 void
2581 diagnose_declaration_constraints (location_t loc, tree decl, tree args)
2583 inform (loc, " constraints not satisfied");
2585 /* Constraints are attached to the template. */
2586 if (tree ti = DECL_TEMPLATE_INFO (decl))
2588 decl = TI_TEMPLATE (ti);
2589 if (!args)
2590 args = TI_ARGS (ti);
2593 /* Check that the constraints are actually valid. */
2594 tree ci = get_constraints (decl);
2595 if (!valid_constraints_p (ci))
2597 inform (loc, " invalid constraints");
2598 return;
2601 /* Recursively diagnose the associated constraints. */
2602 diagnose_constraint (loc, CI_ASSOCIATED_CONSTRAINTS (ci), args);
2605 } // namespace
2607 /* Emit diagnostics detailing the failure ARGS to satisfy the
2608 constraints of T. Here, T can be either a constraint
2609 or a declaration. */
2611 void
2612 diagnose_constraints (location_t loc, tree t, tree args)
2614 if (constraint_p (t))
2615 diagnose_constraint (loc, t, args);
2616 else
2617 diagnose_declaration_constraints (loc, t, args);