Add qdf24xx base tuning support.
[official-gcc.git] / gcc / cp / constraint.cc
blobaf7a593a4f66b05cbccb03d00244d02e47060145
1 /* Processing rules for constraints.
2 Copyright (C) 2013-2016 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 /* Given the predicate constraint T from a constrained-type-specifier, extract
1357 its TMPL and ARGS. FIXME why do we need two different forms of
1358 constrained-type-specifier? */
1360 void
1361 placeholder_extract_concept_and_args (tree t, tree &tmpl, tree &args)
1363 if (TREE_CODE (t) == TYPE_DECL)
1365 /* A constrained parameter. */
1366 tmpl = DECL_TI_TEMPLATE (CONSTRAINED_PARM_CONCEPT (t));
1367 args = CONSTRAINED_PARM_EXTRA_ARGS (t);
1368 return;
1371 gcc_assert (TREE_CODE (t) == PRED_CONSTR);
1372 t = PRED_CONSTR_EXPR (t);
1373 gcc_assert (TREE_CODE (t) == CALL_EXPR
1374 || TREE_CODE (t) == TEMPLATE_ID_EXPR
1375 || VAR_P (t));
1377 if (TREE_CODE (t) == CALL_EXPR)
1378 t = CALL_EXPR_FN (t);
1379 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
1381 tmpl = TREE_OPERAND (t, 0);
1382 if (TREE_CODE (tmpl) == OVERLOAD)
1384 gcc_assert (OVL_CHAIN (tmpl) == NULL_TREE);
1385 tmpl = OVL_FUNCTION (tmpl);
1387 args = TREE_OPERAND (t, 1);
1389 else if (DECL_P (t))
1391 tmpl = DECL_TI_TEMPLATE (t);
1392 args = DECL_TI_ARGS (t);
1394 else
1395 gcc_unreachable ();
1398 /* Returns true iff the placeholders C1 and C2 are equivalent. C1
1399 and C2 can be either PRED_CONSTR_EXPR or TEMPLATE_TYPE_PARM. */
1401 bool
1402 equivalent_placeholder_constraints (tree c1, tree c2)
1404 if (c1 && TREE_CODE (c1) == TEMPLATE_TYPE_PARM)
1405 /* A constrained auto. */
1406 c1 = PLACEHOLDER_TYPE_CONSTRAINTS (c1);
1407 if (c2 && TREE_CODE (c2) == TEMPLATE_TYPE_PARM)
1408 c2 = PLACEHOLDER_TYPE_CONSTRAINTS (c2);
1410 if (c1 == c2)
1411 return true;
1412 if (!c1 || !c2)
1413 return false;
1415 tree t1, t2, a1, a2;
1416 placeholder_extract_concept_and_args (c1, t1, a1);
1417 placeholder_extract_concept_and_args (c2, t2, a2);
1419 if (t1 != t2)
1420 return false;
1422 /* Skip the first argument to avoid infinite recursion on the
1423 placeholder auto itself. */
1424 bool skip1 = (TREE_CODE (c1) == PRED_CONSTR);
1425 bool skip2 = (TREE_CODE (c2) == PRED_CONSTR);
1427 int len1 = (a1 ? TREE_VEC_LENGTH (a1) : 0) - skip1;
1428 int len2 = (a2 ? TREE_VEC_LENGTH (a2) : 0) - skip2;
1430 if (len1 != len2)
1431 return false;
1433 for (int i = 0; i < len1; ++i)
1434 if (!cp_tree_equal (TREE_VEC_ELT (a1, i + skip1),
1435 TREE_VEC_ELT (a2, i + skip2)))
1436 return false;
1437 return true;
1440 /* Return a hash value for the placeholder PRED_CONSTR C. */
1442 hashval_t
1443 hash_placeholder_constraint (tree c)
1445 tree t, a;
1446 placeholder_extract_concept_and_args (c, t, a);
1448 /* Like hash_tmpl_and_args, but skip the first argument. */
1449 hashval_t val = iterative_hash_object (DECL_UID (t), 0);
1451 for (int i = TREE_VEC_LENGTH (a)-1; i > 0; --i)
1452 val = iterative_hash_template_arg (TREE_VEC_ELT (a, i), val);
1454 return val;
1457 /*---------------------------------------------------------------------------
1458 Constraint substitution
1459 ---------------------------------------------------------------------------*/
1461 /* The following functions implement substitution rules for constraints.
1462 Substitution without checking constraints happens only in the
1463 instantiation of class templates. For example:
1465 template<C1 T> struct S {
1466 void f(T) requires C2<T>;
1467 void g(T) requires T::value;
1470 S<int> s; // error instantiating S<int>::g(T)
1472 When we instantiate S, we substitute into its member declarations,
1473 including their constraints. However, those constraints are not
1474 checked. Substituting int into C2<T> yields C2<int>, and substituting
1475 into T::value yields a substitution failure, making the program
1476 ill-formed.
1478 Note that we only ever substitute into the associated constraints
1479 of a declaration. That is, substitution is defined only for predicate
1480 constraints and conjunctions. */
1482 /* Substitute into the predicate constraints. Returns error_mark_node
1483 if the substitution into the expression fails. */
1484 tree
1485 tsubst_predicate_constraint (tree t, tree args,
1486 tsubst_flags_t complain, tree in_decl)
1488 tree expr = PRED_CONSTR_EXPR (t);
1489 ++processing_template_decl;
1490 tree result = tsubst_expr (expr, args, complain, in_decl, false);
1491 --processing_template_decl;
1492 return build_nt (PRED_CONSTR, result);
1495 /* Substitute into the conjunction of constraints. Returns
1496 error_mark_node if substitution into either operand fails. */
1497 tree
1498 tsubst_conjunction (tree t, tree args,
1499 tsubst_flags_t complain, tree in_decl)
1501 tree t0 = TREE_OPERAND (t, 0);
1502 tree r0 = tsubst_constraint (t0, args, complain, in_decl);
1503 tree t1 = TREE_OPERAND (t, 1);
1504 tree r1 = tsubst_constraint (t1, args, complain, in_decl);
1505 return build_nt (CONJ_CONSTR, r0, r1);
1508 /* Substitute ARGS into the constraint T. */
1509 tree
1510 tsubst_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1512 if (t == NULL_TREE)
1513 return t;
1514 if (TREE_CODE (t) == CONJ_CONSTR)
1515 return tsubst_conjunction (t, args, complain, in_decl);
1516 else if (TREE_CODE (t) == PRED_CONSTR)
1517 return tsubst_predicate_constraint (t, args, complain, in_decl);
1518 else
1519 gcc_unreachable ();
1520 return error_mark_node;
1523 namespace {
1525 /* A subroutine of tsubst_constraint_variables. Register local
1526 specializations for each of parameter in PARMS and its
1527 corresponding substituted constraint variable in VARS.
1528 Returns VARS. */
1529 tree
1530 declare_constraint_vars (tree parms, tree vars)
1532 tree s = vars;
1533 for (tree t = parms; t; t = DECL_CHAIN (t))
1535 if (DECL_PACK_P (t))
1537 tree pack = extract_fnparm_pack (t, &s);
1538 register_local_specialization (pack, t);
1540 else
1542 register_local_specialization (s, t);
1543 s = DECL_CHAIN (s);
1546 return vars;
1549 /* A subroutine of tsubst_parameterized_constraint. Substitute ARGS
1550 into the parameter list T, producing a sequence of constraint
1551 variables, declared in the current scope.
1553 Note that the caller must establish a local specialization stack
1554 prior to calling this function since this substitution will
1555 declare the substituted parameters. */
1556 tree
1557 tsubst_constraint_variables (tree t, tree args,
1558 tsubst_flags_t complain, tree in_decl)
1560 /* Clear cp_unevaluated_operand across tsubst so that we get a proper chain
1561 of PARM_DECLs. */
1562 int saved_unevaluated_operand = cp_unevaluated_operand;
1563 cp_unevaluated_operand = 0;
1564 tree vars = tsubst (t, args, complain, in_decl);
1565 cp_unevaluated_operand = saved_unevaluated_operand;
1566 if (vars == error_mark_node)
1567 return error_mark_node;
1568 return declare_constraint_vars (t, vars);
1571 /* Substitute ARGS into the simple requirement T. Note that
1572 substitution may result in an ill-formed expression without
1573 causing the program to be ill-formed. In such cases, the
1574 requirement wraps an error_mark_node. */
1575 inline tree
1576 tsubst_simple_requirement (tree t, tree args,
1577 tsubst_flags_t complain, tree in_decl)
1579 ++processing_template_decl;
1580 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1581 --processing_template_decl;
1582 return finish_simple_requirement (expr);
1585 /* Substitute ARGS into the type requirement T. Note that
1586 substitution may result in an ill-formed type without
1587 causing the program to be ill-formed. In such cases, the
1588 requirement wraps an error_mark_node. */
1590 inline tree
1591 tsubst_type_requirement (tree t, tree args,
1592 tsubst_flags_t complain, tree in_decl)
1594 ++processing_template_decl;
1595 tree type = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
1596 --processing_template_decl;
1597 return finish_type_requirement (type);
1600 /* Substitute args into the compound requirement T. If substituting
1601 into either the expression or the type fails, the corresponding
1602 operands in the resulting node will be error_mark_node. This
1603 preserves a requirement for the purpose of partial ordering, but
1604 it will never be satisfied. */
1606 tree
1607 tsubst_compound_requirement (tree t, tree args,
1608 tsubst_flags_t complain, tree in_decl)
1610 ++processing_template_decl;
1611 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1612 tree type = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
1613 --processing_template_decl;
1614 bool noexcept_p = COMPOUND_REQ_NOEXCEPT_P (t);
1615 return finish_compound_requirement (expr, type, noexcept_p);
1618 /* Substitute ARGS into the nested requirement T. */
1620 tree
1621 tsubst_nested_requirement (tree t, tree args,
1622 tsubst_flags_t complain, tree in_decl)
1624 ++processing_template_decl;
1625 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1626 --processing_template_decl;
1627 return finish_nested_requirement (expr);
1630 inline tree
1631 tsubst_requirement (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1633 switch (TREE_CODE (t))
1635 case SIMPLE_REQ:
1636 return tsubst_simple_requirement (t, args, complain, in_decl);
1637 case TYPE_REQ:
1638 return tsubst_type_requirement (t, args, complain, in_decl);
1639 case COMPOUND_REQ:
1640 return tsubst_compound_requirement (t, args, complain, in_decl);
1641 case NESTED_REQ:
1642 return tsubst_nested_requirement (t, args, complain, in_decl);
1643 default:
1644 gcc_unreachable ();
1646 return error_mark_node;
1649 /* Substitute ARGS into the list of requirements T. Note that
1650 substitution failures here result in ill-formed programs. */
1652 tree
1653 tsubst_requirement_body (tree t, tree args,
1654 tsubst_flags_t complain, tree in_decl)
1656 tree r = NULL_TREE;
1657 while (t)
1659 tree e = tsubst_requirement (TREE_VALUE (t), args, complain, in_decl);
1660 if (e == error_mark_node)
1661 return error_mark_node;
1662 r = tree_cons (NULL_TREE, e, r);
1663 t = TREE_CHAIN (t);
1665 return r;
1668 } /* namespace */
1670 /* Substitute ARGS into the requires expression T. Note that this
1671 results in the re-declaration of local parameters when
1672 substituting through the parameter list. If either substitution
1673 fails, the program is ill-formed. */
1675 tree
1676 tsubst_requires_expr (tree t, tree args,
1677 tsubst_flags_t complain, tree in_decl)
1679 local_specialization_stack stack;
1681 tree parms = TREE_OPERAND (t, 0);
1682 if (parms)
1684 parms = tsubst_constraint_variables (parms, args, complain, in_decl);
1685 if (parms == error_mark_node)
1686 return error_mark_node;
1689 tree reqs = TREE_OPERAND (t, 1);
1690 reqs = tsubst_requirement_body (reqs, args, complain, in_decl);
1691 if (reqs == error_mark_node)
1692 return error_mark_node;
1694 return finish_requires_expr (parms, reqs);
1697 /* Substitute ARGS into the constraint information CI, producing a new
1698 constraint record. */
1699 tree
1700 tsubst_constraint_info (tree t, tree args,
1701 tsubst_flags_t complain, tree in_decl)
1703 if (!t || t == error_mark_node || !check_constraint_info (t))
1704 return NULL_TREE;
1706 tree tmpl_constr = NULL_TREE;
1707 if (tree r = CI_TEMPLATE_REQS (t))
1708 tmpl_constr = tsubst_constraint (r, args, complain, in_decl);
1710 tree decl_constr = NULL_TREE;
1711 if (tree r = CI_DECLARATOR_REQS (t))
1712 decl_constr = tsubst_constraint (r, args, complain, in_decl);
1714 return build_constraints (tmpl_constr, decl_constr);
1718 /*---------------------------------------------------------------------------
1719 Constraint satisfaction
1720 ---------------------------------------------------------------------------*/
1722 /* The following functions determine if a constraint, when
1723 substituting template arguments, is satisfied. For convenience,
1724 satisfaction reduces a constraint to either true or false (and
1725 nothing else). */
1727 namespace {
1729 tree satisfy_constraint_1 (tree, tree, tsubst_flags_t, tree);
1731 /* Check the constraint pack expansion. */
1733 tree
1734 satisfy_pack_expansion (tree t, tree args,
1735 tsubst_flags_t complain, tree in_decl)
1737 /* Get the vector of satisfaction results.
1738 gen_elem_of_pack_expansion_instantiation will check that each element of
1739 the expansion is satisfied. */
1740 tree exprs = tsubst_pack_expansion (t, args, complain, in_decl);
1741 if (exprs == error_mark_node)
1742 return boolean_false_node;
1743 int n = TREE_VEC_LENGTH (exprs);
1745 for (int i = 0; i < n; ++i)
1746 if (TREE_VEC_ELT (exprs, i) != boolean_true_node)
1747 return boolean_false_node;
1748 return boolean_true_node;
1751 /* A predicate constraint is satisfied if its expression evaluates
1752 to true. If substitution into that node fails, the constraint
1753 is not satisfied ([temp.constr.pred]).
1755 Note that a predicate constraint is a constraint expression
1756 of type bool. If neither of those are true, the program is
1757 ill-formed; they are not SFINAE'able errors. */
1759 tree
1760 satisfy_predicate_constraint (tree t, tree args,
1761 tsubst_flags_t complain, tree in_decl)
1763 tree original = TREE_OPERAND (t, 0);
1765 /* We should never have a naked pack expansion in a predicate constraint. */
1766 gcc_assert (TREE_CODE (original) != EXPR_PACK_EXPANSION);
1768 tree expr = tsubst_expr (original, args, complain, in_decl, false);
1769 if (expr == error_mark_node)
1770 return boolean_false_node;
1772 /* A predicate constraint shall have type bool. In some
1773 cases, substitution gives us const-qualified bool, which
1774 is also acceptable. */
1775 tree type = cv_unqualified (TREE_TYPE (expr));
1776 if (!same_type_p (type, boolean_type_node))
1778 error_at (EXPR_LOC_OR_LOC (expr, input_location),
1779 "constraint %qE does not have type %qT",
1780 expr, boolean_type_node);
1781 return boolean_false_node;
1784 tree value = cxx_constant_value (expr);
1785 return value;
1788 /* Check an expression constraint. The constraint is satisfied if
1789 substitution succeeds ([temp.constr.expr]).
1791 Note that the expression is unevaluated. */
1793 tree
1794 satisfy_expression_constraint (tree t, tree args,
1795 tsubst_flags_t complain, tree in_decl)
1797 cp_unevaluated guard;
1798 deferring_access_check_sentinel deferring;
1800 tree expr = EXPR_CONSTR_EXPR (t);
1801 tree check = tsubst_expr (expr, args, complain, in_decl, false);
1802 if (check == error_mark_node)
1803 return boolean_false_node;
1804 if (!perform_deferred_access_checks (tf_none))
1805 return boolean_false_node;
1807 return boolean_true_node;
1810 /* Check a type constraint. The constraint is satisfied if
1811 substitution succeeds. */
1813 inline tree
1814 satisfy_type_constraint (tree t, tree args,
1815 tsubst_flags_t complain, tree in_decl)
1817 deferring_access_check_sentinel deferring;
1818 tree type = TYPE_CONSTR_TYPE (t);
1819 gcc_assert (TYPE_P (type) || type == error_mark_node);
1820 tree check = tsubst (type, args, complain, in_decl);
1821 if (error_operand_p (check))
1822 return boolean_false_node;
1823 if (!perform_deferred_access_checks (complain))
1824 return boolean_false_node;
1826 return boolean_true_node;
1829 /* Check an implicit conversion constraint. */
1831 tree
1832 satisfy_implicit_conversion_constraint (tree t, tree args,
1833 tsubst_flags_t complain, tree in_decl)
1835 /* Don't tsubst as if we're processing a template. If we try
1836 to we can end up generating template-like expressions
1837 (e.g., modop-exprs) that aren't properly typed. */
1838 tree expr =
1839 tsubst_expr (ICONV_CONSTR_EXPR (t), args, complain, in_decl, false);
1840 if (expr == error_mark_node)
1841 return boolean_false_node;
1843 /* Get the transformed target type. */
1844 tree type = tsubst (ICONV_CONSTR_TYPE (t), args, complain, in_decl);
1845 if (type == error_mark_node)
1846 return boolean_false_node;
1848 /* Attempt the conversion as a direct initialization
1849 of the form TYPE <unspecified> = EXPR. */
1850 tree conv =
1851 perform_direct_initialization_if_possible (type, expr, false, complain);
1852 if (conv == NULL_TREE || conv == error_mark_node)
1853 return boolean_false_node;
1854 else
1855 return boolean_true_node;
1858 /* Check an argument deduction constraint. */
1860 tree
1861 satisfy_argument_deduction_constraint (tree t, tree args,
1862 tsubst_flags_t complain, tree in_decl)
1864 /* Substitute through the expression. */
1865 tree expr = DEDUCT_CONSTR_EXPR (t);
1866 tree init = tsubst_expr (expr, args, complain, in_decl, false);
1867 if (expr == error_mark_node)
1868 return boolean_false_node;
1870 /* Perform auto or decltype(auto) deduction to get the result. */
1871 tree pattern = DEDUCT_CONSTR_PATTERN (t);
1872 tree placeholder = DEDUCT_CONSTR_PLACEHOLDER (t);
1873 tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (placeholder);
1874 tree type_canonical = TYPE_CANONICAL (placeholder);
1875 PLACEHOLDER_TYPE_CONSTRAINTS (placeholder)
1876 = tsubst_constraint (constr, args, complain|tf_partial, in_decl);
1877 TYPE_CANONICAL (placeholder) = NULL_TREE;
1878 tree type = do_auto_deduction (pattern, init, placeholder,
1879 complain, adc_requirement);
1880 PLACEHOLDER_TYPE_CONSTRAINTS (placeholder) = constr;
1881 TYPE_CANONICAL (placeholder) = type_canonical;
1882 if (type == error_mark_node)
1883 return boolean_false_node;
1885 return boolean_true_node;
1888 /* Check an exception constraint. An exception constraint for an
1889 expression e is satisfied when noexcept(e) is true. */
1891 tree
1892 satisfy_exception_constraint (tree t, tree args,
1893 tsubst_flags_t complain, tree in_decl)
1895 tree expr = EXCEPT_CONSTR_EXPR (t);
1896 tree check = tsubst_expr (expr, args, complain, in_decl, false);
1897 if (check == error_mark_node)
1898 return boolean_false_node;
1900 if (expr_noexcept_p (check, complain))
1901 return boolean_true_node;
1902 else
1903 return boolean_false_node;
1906 /* Check a parameterized constraint. */
1908 tree
1909 satisfy_parameterized_constraint (tree t, tree args,
1910 tsubst_flags_t complain, tree in_decl)
1912 local_specialization_stack stack;
1913 tree parms = PARM_CONSTR_PARMS (t);
1914 tree vars = tsubst_constraint_variables (parms, args, complain, in_decl);
1915 if (vars == error_mark_node)
1916 return boolean_false_node;
1917 tree constr = PARM_CONSTR_OPERAND (t);
1918 return satisfy_constraint_1 (constr, args, complain, in_decl);
1921 /* Check that the conjunction of constraints is satisfied. Note
1922 that if left operand is not satisfied, the right operand
1923 is not checked.
1925 FIXME: Check that this wouldn't result in a user-defined
1926 operator. Note that this error is partially diagnosed in
1927 satisfy_predicate_constraint. It would be nice to diagnose
1928 the overload, but I don't think it's strictly necessary. */
1930 tree
1931 satisfy_conjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1933 tree t0 = satisfy_constraint_1 (TREE_OPERAND (t, 0), args, complain, in_decl);
1934 if (t0 == boolean_false_node)
1935 return t0;
1936 tree t1 = satisfy_constraint_1 (TREE_OPERAND (t, 1), args, complain, in_decl);
1937 if (t1 == boolean_false_node)
1938 return t1;
1939 return boolean_true_node;
1942 /* Check that the disjunction of constraints is satisfied. Note
1943 that if the left operand is satisfied, the right operand is not
1944 checked. */
1946 tree
1947 satisfy_disjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1949 tree t0 = satisfy_constraint_1 (TREE_OPERAND (t, 0), args, complain, in_decl);
1950 if (t0 == boolean_true_node)
1951 return boolean_true_node;
1952 tree t1 = satisfy_constraint_1 (TREE_OPERAND (t, 1), args, complain, in_decl);
1953 if (t1 == boolean_true_node)
1954 return boolean_true_node;
1955 return boolean_false_node;
1958 /* Dispatch to an appropriate satisfaction routine depending on the
1959 tree code of T. */
1961 tree
1962 satisfy_constraint_1 (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1964 gcc_assert (!processing_template_decl);
1966 if (!t)
1967 return boolean_false_node;
1969 if (t == error_mark_node)
1970 return boolean_false_node;
1972 switch (TREE_CODE (t))
1974 case PRED_CONSTR:
1975 return satisfy_predicate_constraint (t, args, complain, in_decl);
1977 case EXPR_CONSTR:
1978 return satisfy_expression_constraint (t, args, complain, in_decl);
1980 case TYPE_CONSTR:
1981 return satisfy_type_constraint (t, args, complain, in_decl);
1983 case ICONV_CONSTR:
1984 return satisfy_implicit_conversion_constraint (t, args, complain, in_decl);
1986 case DEDUCT_CONSTR:
1987 return satisfy_argument_deduction_constraint (t, args, complain, in_decl);
1989 case EXCEPT_CONSTR:
1990 return satisfy_exception_constraint (t, args, complain, in_decl);
1992 case PARM_CONSTR:
1993 return satisfy_parameterized_constraint (t, args, complain, in_decl);
1995 case CONJ_CONSTR:
1996 return satisfy_conjunction (t, args, complain, in_decl);
1998 case DISJ_CONSTR:
1999 return satisfy_disjunction (t, args, complain, in_decl);
2001 case EXPR_PACK_EXPANSION:
2002 return satisfy_pack_expansion (t, args, complain, in_decl);
2004 default:
2005 gcc_unreachable ();
2007 return boolean_false_node;
2010 /* Check that the constraint is satisfied, according to the rules
2011 for that constraint. Note that each satisfy_* function returns
2012 true or false, depending on whether it is satisfied or not. */
2014 tree
2015 satisfy_constraint (tree t, tree args)
2017 /* Turn off template processing. Constraint satisfaction only applies
2018 to non-dependent terms, so we want full checking here. */
2019 processing_template_decl_sentinel sentinel (true);
2020 /* Avoid early exit in tsubst and tsubst_copy from null args; since earlier
2021 substitution was done with processing_template_decl forced on, there will
2022 be expressions that still need semantic processing, possibly buried in
2023 decltype or a template argument. */
2024 if (args == NULL_TREE)
2025 args = make_tree_vec (1);
2026 return satisfy_constraint_1 (t, args, tf_none, NULL_TREE);
2029 /* Check the associated constraints in CI against the given
2030 ARGS, returning true when the constraints are satisfied
2031 and false otherwise. */
2033 tree
2034 satisfy_associated_constraints (tree ci, tree args)
2036 /* If there are no constraints then this is trivially satisfied. */
2037 if (!ci)
2038 return boolean_true_node;
2040 /* If any arguments depend on template parameters, we can't
2041 check constraints. */
2042 if (args && uses_template_parms (args))
2043 return boolean_true_node;
2045 /* Invalid requirements cannot be satisfied. */
2046 if (!valid_constraints_p (ci))
2047 return boolean_false_node;
2049 return satisfy_constraint (CI_NORMALIZED_CONSTRAINTS (ci), args);
2052 } /* namespace */
2054 /* Evaluate the given constraint, returning boolean_true_node
2055 if the constraint is satisfied and boolean_false_node
2056 otherwise. */
2058 tree
2059 evaluate_constraints (tree constr, tree args)
2061 gcc_assert (constraint_p (constr));
2062 return satisfy_constraint (normalize_constraint (constr), args);
2065 /* Evaluate the function concept FN by substituting its own args
2066 into its definition and evaluating that as the result. Returns
2067 boolean_true_node if the constraints are satisfied and
2068 boolean_false_node otherwise. */
2070 tree
2071 evaluate_function_concept (tree fn, tree args)
2073 ++processing_template_decl;
2074 /* We lift using DECL_TI_ARGS because we want to delay producing
2075 non-dependent expressions until we're doing satisfaction. We can't just
2076 go without any substitution because we need to lower the level of 'auto's
2077 in type deduction constraints. */
2078 tree constr = transform_expression (lift_function_definition
2079 (fn, DECL_TI_ARGS (fn)));
2080 --processing_template_decl;
2081 return satisfy_constraint (constr, args);
2084 /* Evaluate the variable concept VAR by substituting its own args into
2085 its initializer and checking the resulting constraint. Returns
2086 boolean_true_node if the constraints are satisfied and
2087 boolean_false_node otherwise. */
2089 tree
2090 evaluate_variable_concept (tree decl, tree args)
2092 ++processing_template_decl;
2093 tree constr = transform_expression (lift_variable_initializer
2094 (decl, DECL_TI_ARGS (decl)));
2095 --processing_template_decl;
2096 return satisfy_constraint (constr, args);
2099 /* Evaluate the given expression as if it were a predicate
2100 constraint. Returns boolean_true_node if the constraint
2101 is satisfied and boolean_false_node otherwise. */
2103 tree
2104 evaluate_constraint_expression (tree expr, tree args)
2106 ++processing_template_decl;
2107 tree constr = transform_expression (lift_expression (expr));
2108 --processing_template_decl;
2109 return satisfy_constraint (constr, args);
2112 /* Returns true if the DECL's constraints are satisfied.
2113 This is used in cases where a declaration is formed but
2114 before it is used (e.g., overload resolution). */
2116 bool
2117 constraints_satisfied_p (tree decl)
2119 /* Get the constraints to check for satisfaction. This depends
2120 on whether we're looking at a template specialization or not. */
2121 tree ci;
2122 tree args = NULL_TREE;
2123 if (tree ti = DECL_TEMPLATE_INFO (decl))
2125 tree tmpl = TI_TEMPLATE (ti);
2126 ci = get_constraints (tmpl);
2127 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2128 args = get_innermost_template_args (TI_ARGS (ti), depth);
2130 else
2132 ci = get_constraints (decl);
2135 tree eval = satisfy_associated_constraints (ci, args);
2136 return eval == boolean_true_node;
2139 /* Returns true if the constraints are satisfied by ARGS.
2140 Here, T can be either a constraint or a constrained
2141 declaration. */
2143 bool
2144 constraints_satisfied_p (tree t, tree args)
2146 tree eval;
2147 if (constraint_p (t))
2148 eval = evaluate_constraints (t, args);
2149 else
2150 eval = satisfy_associated_constraints (get_constraints (t), args);
2151 return eval == boolean_true_node;
2154 namespace
2157 /* Normalize EXPR and determine if the resulting constraint is
2158 satisfied by ARGS. Returns true if and only if the constraint
2159 is satisfied. This is used extensively by diagnostics to
2160 determine causes for failure. */
2162 inline bool
2163 constraint_expression_satisfied_p (tree expr, tree args)
2165 return evaluate_constraint_expression (expr, args) == boolean_true_node;
2168 } /* namespace */
2171 /*---------------------------------------------------------------------------
2172 Semantic analysis of requires-expressions
2173 ---------------------------------------------------------------------------*/
2175 /* Finish a requires expression for the given PARMS (possibly
2176 null) and the non-empty sequence of requirements. */
2177 tree
2178 finish_requires_expr (tree parms, tree reqs)
2180 /* Modify the declared parameters by removing their context
2181 so they don't refer to the enclosing scope and explicitly
2182 indicating that they are constraint variables. */
2183 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
2185 DECL_CONTEXT (parm) = NULL_TREE;
2186 CONSTRAINT_VAR_P (parm) = true;
2189 /* Build the node. */
2190 tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs);
2191 TREE_SIDE_EFFECTS (r) = false;
2192 TREE_CONSTANT (r) = true;
2193 return r;
2196 /* Construct a requirement for the validity of EXPR. */
2197 tree
2198 finish_simple_requirement (tree expr)
2200 return build_nt (SIMPLE_REQ, expr);
2203 /* Construct a requirement for the validity of TYPE. */
2204 tree
2205 finish_type_requirement (tree type)
2207 return build_nt (TYPE_REQ, type);
2210 /* Construct a requirement for the validity of EXPR, along with
2211 its properties. if TYPE is non-null, then it specifies either
2212 an implicit conversion or argument deduction constraint,
2213 depending on whether any placeholders occur in the type name.
2214 NOEXCEPT_P is true iff the noexcept keyword was specified. */
2215 tree
2216 finish_compound_requirement (tree expr, tree type, bool noexcept_p)
2218 tree req = build_nt (COMPOUND_REQ, expr, type);
2219 COMPOUND_REQ_NOEXCEPT_P (req) = noexcept_p;
2220 return req;
2223 /* Finish a nested requirement. */
2224 tree
2225 finish_nested_requirement (tree expr)
2227 return build_nt (NESTED_REQ, expr);
2230 // Check that FN satisfies the structural requirements of a
2231 // function concept definition.
2232 tree
2233 check_function_concept (tree fn)
2235 // Check that the function is comprised of only a single
2236 // return statement.
2237 tree body = DECL_SAVED_TREE (fn);
2238 if (TREE_CODE (body) == BIND_EXPR)
2239 body = BIND_EXPR_BODY (body);
2241 // Sometimes a function call results in the creation of clean up
2242 // points. Allow these to be preserved in the body of the
2243 // constraint, as we might actually need them for some constexpr
2244 // evaluations.
2245 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
2246 body = TREE_OPERAND (body, 0);
2248 /* Check that the definition is written correctly. */
2249 if (TREE_CODE (body) != RETURN_EXPR)
2251 location_t loc = DECL_SOURCE_LOCATION (fn);
2252 if (TREE_CODE (body) == STATEMENT_LIST && !STATEMENT_LIST_HEAD (body))
2253 error_at (loc, "definition of concept %qD is empty", fn);
2254 else
2255 error_at (loc, "definition of concept %qD has multiple statements", fn);
2258 return NULL_TREE;
2262 // Check that a constrained friend declaration function declaration,
2263 // FN, is admissible. This is the case only when the declaration depends
2264 // on template parameters and does not declare a specialization.
2265 void
2266 check_constrained_friend (tree fn, tree reqs)
2268 if (fn == error_mark_node)
2269 return;
2270 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
2272 // If there are not constraints, this cannot be an error.
2273 if (!reqs)
2274 return;
2276 // Constrained friend functions that don't depend on template
2277 // arguments are effectively meaningless.
2278 if (!uses_template_parms (TREE_TYPE (fn)))
2280 error_at (location_of (fn),
2281 "constrained friend does not depend on template parameters");
2282 return;
2286 /*---------------------------------------------------------------------------
2287 Equivalence of constraints
2288 ---------------------------------------------------------------------------*/
2290 /* Returns true when A and B are equivalent constraints. */
2291 bool
2292 equivalent_constraints (tree a, tree b)
2294 gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2295 gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2296 return cp_tree_equal (a, b);
2299 /* Returns true if the template declarations A and B have equivalent
2300 constraints. This is the case when A's constraints subsume B's and
2301 when B's also constrain A's. */
2302 bool
2303 equivalently_constrained (tree d1, tree d2)
2305 gcc_assert (TREE_CODE (d1) == TREE_CODE (d2));
2306 return equivalent_constraints (get_constraints (d1), get_constraints (d2));
2309 /*---------------------------------------------------------------------------
2310 Partial ordering of constraints
2311 ---------------------------------------------------------------------------*/
2313 /* Returns true when the the constraints in A subsume those in B. */
2314 bool
2315 subsumes_constraints (tree a, tree b)
2317 gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2318 gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2319 return subsumes (a, b);
2322 /* Returns true when the the constraints in A subsume those in B, but
2323 the constraints in B do not subsume the constraints in A. */
2325 bool
2326 strictly_subsumes (tree a, tree b)
2328 return subsumes (a, b) && !subsumes (b, a);
2331 /* Determines which of the declarations, A or B, is more constrained.
2332 That is, which declaration's constraints subsume but are not subsumed
2333 by the other's?
2335 Returns 1 if A is more constrained than B, -1 if B is more constrained
2336 than A, and 0 otherwise. */
2338 more_constrained (tree d1, tree d2)
2340 tree c1 = get_constraints (d1);
2341 tree c2 = get_constraints (d2);
2342 int winner = 0;
2343 if (subsumes_constraints (c1, c2))
2344 ++winner;
2345 if (subsumes_constraints (c2, c1))
2346 --winner;
2347 return winner;
2350 /* Returns true if D1 is at least as constrained as D2. That is, the
2351 associated constraints of D1 subsume those of D2, or both declarations
2352 are unconstrained. */
2353 bool
2354 at_least_as_constrained (tree d1, tree d2)
2356 tree c1 = get_constraints (d1);
2357 tree c2 = get_constraints (d2);
2358 return subsumes_constraints (c1, c2);
2362 /*---------------------------------------------------------------------------
2363 Constraint diagnostics
2364 ---------------------------------------------------------------------------*/
2366 /* The diagnosis of constraints performs a combination of
2367 normalization and satisfaction testing. We recursively
2368 walk through the conjunction (or disjunctions) of associated
2369 constraints, testing each sub-expression in turn.
2371 We currently restrict diagnostics to just the top-level
2372 conjunctions within the associated constraints. A fully
2373 recursive walk is possible, but it can generate a lot
2374 of errors. */
2377 namespace {
2379 void diagnose_expression (location_t, tree, tree);
2380 void diagnose_constraint (location_t, tree, tree);
2382 /* Diagnose a conjunction of constraints. */
2383 void
2384 diagnose_logical_operation (location_t loc, tree t, tree args)
2386 diagnose_expression (loc, TREE_OPERAND (t, 0), args);
2387 diagnose_expression (loc, TREE_OPERAND (t, 0), args);
2390 /* Determine if the trait expression T is satisfied by ARGS.
2391 Emit a precise diagnostic if it is not. */
2392 void
2393 diagnose_trait_expression (location_t loc, tree t, tree args)
2395 if (constraint_expression_satisfied_p (t, args))
2396 return;
2398 /* Rebuild the trait expression so we can diagnose the
2399 specific failure. */
2400 ++processing_template_decl;
2401 tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
2402 --processing_template_decl;
2404 tree t1 = TRAIT_EXPR_TYPE1 (expr);
2405 tree t2 = TRAIT_EXPR_TYPE2 (expr);
2406 switch (TRAIT_EXPR_KIND (t))
2408 case CPTK_HAS_NOTHROW_ASSIGN:
2409 inform (loc, " %qT is not nothrow copy assignable", t1);
2410 break;
2411 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
2412 inform (loc, " %qT is not nothrow default constructible", t1);
2413 break;
2414 case CPTK_HAS_NOTHROW_COPY:
2415 inform (loc, " %qT is not nothrow copy constructible", t1);
2416 break;
2417 case CPTK_HAS_TRIVIAL_ASSIGN:
2418 inform (loc, " %qT is not trivially copy assignable", t1);
2419 break;
2420 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
2421 inform (loc, " %qT is not trivially default constructible", t1);
2422 break;
2423 case CPTK_HAS_TRIVIAL_COPY:
2424 inform (loc, " %qT is not trivially copy constructible", t1);
2425 break;
2426 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
2427 inform (loc, " %qT is not trivially destructible", t1);
2428 break;
2429 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
2430 inform (loc, " %qT does not have a virtual destructor", t1);
2431 break;
2432 case CPTK_IS_ABSTRACT:
2433 inform (loc, " %qT is not an abstract class", t1);
2434 break;
2435 case CPTK_IS_BASE_OF:
2436 inform (loc, " %qT is not a base of %qT", t1, t2);
2437 break;
2438 case CPTK_IS_CLASS:
2439 inform (loc, " %qT is not a class", t1);
2440 break;
2441 case CPTK_IS_EMPTY:
2442 inform (loc, " %qT is not an empty class", t1);
2443 break;
2444 case CPTK_IS_ENUM:
2445 inform (loc, " %qT is not an enum", t1);
2446 break;
2447 case CPTK_IS_FINAL:
2448 inform (loc, " %qT is not a final class", t1);
2449 break;
2450 case CPTK_IS_LITERAL_TYPE:
2451 inform (loc, " %qT is not a literal type", t1);
2452 break;
2453 case CPTK_IS_POD:
2454 inform (loc, " %qT is not a POD type", t1);
2455 break;
2456 case CPTK_IS_POLYMORPHIC:
2457 inform (loc, " %qT is not a polymorphic type", t1);
2458 break;
2459 case CPTK_IS_SAME_AS:
2460 inform (loc, " %qT is not the same as %qT", t1, t2);
2461 break;
2462 case CPTK_IS_STD_LAYOUT:
2463 inform (loc, " %qT is not an standard layout type", t1);
2464 break;
2465 case CPTK_IS_TRIVIAL:
2466 inform (loc, " %qT is not a trivial type", t1);
2467 break;
2468 case CPTK_IS_UNION:
2469 inform (loc, " %qT is not a union", t1);
2470 break;
2471 default:
2472 gcc_unreachable ();
2476 /* Determine if the call expression T, when normalized as a constraint,
2477 is satisfied by ARGS.
2479 TODO: If T is refers to a concept, We could recursively analyze
2480 its definition to identify the exact failure, but that could
2481 emit a *lot* of error messages (defeating the purpose of
2482 improved diagnostics). Consider adding a flag to control the
2483 depth of diagnostics. */
2484 void
2485 diagnose_call_expression (location_t loc, tree t, tree args)
2487 if (constraint_expression_satisfied_p (t, args))
2488 return;
2490 /* Rebuild the expression for the purpose of diagnostics. */
2491 ++processing_template_decl;
2492 tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
2493 --processing_template_decl;
2495 /* If the function call is known to be a concept check, then
2496 diagnose it differently (i.e., we may recurse). */
2497 if (resolve_constraint_check (t))
2498 inform (loc, " concept %qE was not satisfied", expr);
2499 else
2500 inform (loc, " %qE evaluated to false", expr);
2503 /* Determine if the template-id T, when normalized as a constraint
2504 is satisfied by ARGS. */
2505 void
2506 diagnose_template_id (location_t loc, tree t, tree args)
2508 /* Check for invalid template-ids. */
2509 if (!variable_template_p (TREE_OPERAND (t, 0)))
2511 inform (loc, " invalid constraint %qE", t);
2512 return;
2515 if (constraint_expression_satisfied_p (t, args))
2516 return;
2518 /* Rebuild the expression for the purpose of diagnostics. */
2519 ++processing_template_decl;
2520 tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
2521 --processing_template_decl;
2523 tree var = DECL_TEMPLATE_RESULT (TREE_OPERAND (t, 0));
2524 if (DECL_DECLARED_CONCEPT_P (var))
2525 inform (loc, " concept %qE was not satisfied", expr);
2526 else
2527 inform (loc, " %qE evaluated to false", expr);
2530 /* Determine if the requires-expression, when normalized as a
2531 constraint is satisfied by ARGS.
2533 TODO: Build sets of expressions, types, and constraints
2534 based on the requirements in T and emit specific diagnostics
2535 for those. */
2536 void
2537 diagnose_requires_expression (location_t loc, tree t, tree args)
2539 if (constraint_expression_satisfied_p (t, args))
2540 return;
2541 inform (loc, "requirements not satisfied");
2544 void
2545 diagnose_pack_expansion (location_t loc, tree t, tree args)
2547 if (constraint_expression_satisfied_p (t, args))
2548 return;
2550 /* Make sure that we don't have naked packs that we don't expect. */
2551 if (!same_type_p (TREE_TYPE (t), boolean_type_node))
2553 inform (loc, "invalid pack expansion in constraint %qE", t);
2554 return;
2557 inform (loc, " in the expansion of %qE", t);
2559 /* Get the vector of expanded arguments. Note that n must not
2560 be 0 since this constraint is not satisfied. */
2561 ++processing_template_decl;
2562 tree exprs = tsubst_pack_expansion (t, args, tf_none, NULL_TREE);
2563 --processing_template_decl;
2564 if (exprs == error_mark_node)
2566 /* TODO: This error message could be better. */
2567 inform (loc, " substitution failure occurred during expansion");
2568 return;
2571 /* Check each expanded constraint separately. */
2572 int n = TREE_VEC_LENGTH (exprs);
2573 for (int i = 0; i < n; ++i)
2575 tree expr = TREE_VEC_ELT (exprs, i);
2576 if (!constraint_expression_satisfied_p (expr, args))
2577 inform (loc, " %qE was not satisfied", expr);
2581 /* Diagnose an expression that would be characterized as
2582 a predicate constraint. */
2583 void
2584 diagnose_other_expression (location_t loc, tree t, tree args)
2586 if (constraint_expression_satisfied_p (t, args))
2587 return;
2588 inform (loc, " %qE evaluated to false", t);
2591 void
2592 diagnose_expression (location_t loc, tree t, tree args)
2594 switch (TREE_CODE (t))
2596 case TRUTH_ANDIF_EXPR:
2597 diagnose_logical_operation (loc, t, args);
2598 break;
2600 case TRUTH_ORIF_EXPR:
2601 diagnose_logical_operation (loc, t, args);
2602 break;
2604 case CALL_EXPR:
2605 diagnose_call_expression (loc, t, args);
2606 break;
2608 case TEMPLATE_ID_EXPR:
2609 diagnose_template_id (loc, t, args);
2610 break;
2612 case REQUIRES_EXPR:
2613 diagnose_requires_expression (loc, t, args);
2614 break;
2616 case TRAIT_EXPR:
2617 diagnose_trait_expression (loc, t, args);
2618 break;
2620 case EXPR_PACK_EXPANSION:
2621 diagnose_pack_expansion (loc, t, args);
2622 break;
2624 default:
2625 diagnose_other_expression (loc, t, args);
2626 break;
2630 inline void
2631 diagnose_predicate_constraint (location_t loc, tree t, tree args)
2633 diagnose_expression (loc, PRED_CONSTR_EXPR (t), args);
2636 inline void
2637 diagnose_conjunction (location_t loc, tree t, tree args)
2639 diagnose_constraint (loc, TREE_OPERAND (t, 0), args);
2640 diagnose_constraint (loc, TREE_OPERAND (t, 1), args);
2643 /* Diagnose the constraint T for the given ARGS. This is only
2644 ever invoked on the associated constraints, so we can
2645 only have conjunctions of predicate constraints. */
2646 void
2647 diagnose_constraint (location_t loc, tree t, tree args)
2649 switch (TREE_CODE (t))
2651 case CONJ_CONSTR:
2652 diagnose_conjunction (loc, t, args);
2653 break;
2655 case PRED_CONSTR:
2656 diagnose_predicate_constraint (loc, t, args);
2657 break;
2659 default:
2660 gcc_unreachable ();
2661 break;
2665 /* Diagnose the reason(s) why ARGS do not satisfy the constraints
2666 of declaration DECL. */
2668 void
2669 diagnose_declaration_constraints (location_t loc, tree decl, tree args)
2671 inform (loc, " constraints not satisfied");
2673 /* Constraints are attached to the template. */
2674 if (tree ti = DECL_TEMPLATE_INFO (decl))
2676 decl = TI_TEMPLATE (ti);
2677 if (!args)
2678 args = TI_ARGS (ti);
2681 /* Check that the constraints are actually valid. */
2682 tree ci = get_constraints (decl);
2683 if (!valid_constraints_p (ci))
2685 inform (loc, " invalid constraints");
2686 return;
2689 /* Recursively diagnose the associated constraints. */
2690 diagnose_constraint (loc, CI_ASSOCIATED_CONSTRAINTS (ci), args);
2693 } // namespace
2695 /* Emit diagnostics detailing the failure ARGS to satisfy the
2696 constraints of T. Here, T can be either a constraint
2697 or a declaration. */
2699 void
2700 diagnose_constraints (location_t loc, tree t, tree args)
2702 if (constraint_p (t))
2703 diagnose_constraint (loc, t, args);
2704 else
2705 diagnose_declaration_constraints (loc, t, args);