2015-05-19 Andrew Sutton <andrew.n.sutton@gmail.com>
[official-gcc.git] / gcc / cp / constraint.cc
blobe35c436a3d09afffdb1f2c61c7e04b255f6e1731
1 /* Processing rules for constraints.
2 Copyright (C) 2013-2014 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 "print-tree.h"
36 #include "stringpool.h"
37 #include "attribs.h"
38 #include "intl.h"
39 #include "flags.h"
40 #include "cp-tree.h"
41 #include "c-family/c-common.h"
42 #include "c-family/c-objc.h"
43 #include "cp-objcp-common.h"
44 #include "tree-inline.h"
45 #include "decl.h"
46 #include "toplev.h"
47 #include "type-utils.h"
49 /*---------------------------------------------------------------------------
50 Operations on constraints
51 ---------------------------------------------------------------------------*/
53 /* Returns true if C is a constraint tree code. */
54 static inline bool
55 constraint_p (tree_code c)
57 return PRED_CONSTR <= c && c <= DISJ_CONSTR;
60 /* Returns true if T is a constraint. */
61 bool
62 constraint_p (tree t)
64 return constraint_p (TREE_CODE (t));
67 /* Make a predicate constraint from the given expression. */
68 tree
69 make_predicate_constraint (tree expr)
71 return build_nt (PRED_CONSTR, expr);
74 /* Returns the conjunction of two constraints A and B. Note that
75 conjoining a non-null constraint with NULL_TREE is an identity
76 operation. That is, for non-null A,
78 conjoin_constraints(a, NULL_TREE) == a
80 and
82 conjoin_constraints (NULL_TREE, a) == a
84 If both A and B are NULL_TREE, the result is also NULL_TREE. */
85 tree
86 conjoin_constraints (tree a, tree b)
88 gcc_assert (a ? constraint_p (a) : true);
89 gcc_assert (b ? constraint_p (b) : true);
90 if (a)
91 return b ? build_nt (CONJ_CONSTR, a, b) : a;
92 else if (b)
93 return b;
94 else
95 return NULL_TREE;
98 /* Transform the vector of expressions in the T into a conjunction
99 of requirements. T must be a TREE_VEC. */
100 tree
101 conjoin_constraints (tree t)
103 gcc_assert (TREE_CODE (t) == TREE_VEC);
104 tree r = NULL_TREE;
105 for (int i = 0; i < TREE_VEC_LENGTH (t); ++i)
106 r = conjoin_constraints (r, TREE_VEC_ELT (t, i));
107 return r;
110 /* Returns true if T is a call expression to a function
111 concept. */
112 bool
113 function_concept_check_p (tree t)
115 gcc_assert (TREE_CODE (t) == CALL_EXPR);
116 tree fn = CALL_EXPR_FN (t);
117 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR
118 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
120 tree f1 = get_first_fn (fn);
121 if (TREE_CODE (f1) == TEMPLATE_DECL
122 && DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (f1)))
123 return true;
125 return false;
128 /*---------------------------------------------------------------------------
129 Resolution of qualified concept names
130 ---------------------------------------------------------------------------*/
132 /* This facility is used to resolve constraint checks from
133 requirement expressions. A constraint check is a call to
134 a function template declared with the keyword 'concept'.
136 The result of resolution is a pair (a TREE_LIST) whose value
137 is the matched declaration, and whose purpose contains the
138 coerced template arguments that can be substituted into the
139 call. */
141 // Given an overload set OVL, try to find a unique definition that can be
142 // instantiated by the template arguments ARGS.
144 // This function is not called for arbitrary call expressions. In particular,
145 // the call expression must be written with explicit template arguments
146 // and no function arguments. For example:
148 // f<T, U>()
150 // If a single match is found, this returns a TREE_LIST whose VALUE
151 // is the constraint function (not the template), and its PURPOSE is
152 // the complete set of arguments substituted into the parameter list.
153 static tree
154 resolve_constraint_check (tree ovl, tree args)
156 tree cands = NULL_TREE;
157 for (tree p = ovl; p != NULL_TREE; p = OVL_NEXT (p))
159 // Get the next template overload.
160 tree tmpl = OVL_CURRENT (p);
161 if (TREE_CODE (tmpl) != TEMPLATE_DECL)
162 continue;
164 // Don't try to deduce checks for non-concept-like. We often
165 // end up trying to resolve constraints in functional casts
166 // as part of a post-fix expression. We can save time and
167 // headaches by not instantiating those declarations.
169 // NOTE: This masks a potential error, caused by instantiating
170 // non-deduced contexts using placeholder arguments.
171 tree fn = DECL_TEMPLATE_RESULT (tmpl);
172 if (DECL_ARGUMENTS (fn))
173 continue;
174 if (!DECL_DECLARED_CONCEPT_P (fn))
175 continue;
177 // Remember the candidate if we can deduce a substitution.
178 ++processing_template_decl;
179 tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl));
180 if (tree subst = coerce_template_parms (parms, args, tmpl))
181 if (subst != error_mark_node)
182 cands = tree_cons (subst, fn, cands);
183 --processing_template_decl;
186 // If we didn't find a unique candidate, then this is
187 // not a constraint check.
188 if (!cands || TREE_CHAIN (cands))
189 return NULL_TREE;
191 // Constraints must be declared concepts.
192 tree decl = TREE_VALUE (cands);
193 if (!DECL_DECLARED_CONCEPT_P (decl))
194 return NULL_TREE;
196 return cands;
199 // Determine if the the call expression CALL is a constraint check, and
200 // return the concept declaration and arguments being checked. If CALL
201 // does not denote a constraint check, return NULL.
202 tree
203 resolve_constraint_check (tree call)
205 gcc_assert (TREE_CODE (call) == CALL_EXPR);
207 // A constraint check must be only a template-id expression. If
208 // it's a call to a base-link, its function(s) should be a
209 // template-id expression. If this is not a template-id, then it
210 // cannot be a concept-check.
211 tree target = CALL_EXPR_FN (call);
212 if (BASELINK_P (target))
213 target = BASELINK_FUNCTIONS (target);
214 if (TREE_CODE (target) != TEMPLATE_ID_EXPR)
215 return NULL_TREE;
217 // Get the overload set and template arguments and try to
218 // resolve the target.
219 tree ovl = TREE_OPERAND (target, 0);
221 /* This is a function call of a variable concept... ill-formed. */
222 if (TREE_CODE (ovl) == TEMPLATE_DECL)
224 error ("function call of variable concept %qE", call);
225 return error_mark_node;
228 tree args = TREE_OPERAND (target, 1);
229 return resolve_constraint_check (ovl, args);
232 /* Returns a pair containing the checked variable concept
233 and its associated prototype parameter. The result
234 is a TREE_LIST whose TREE_VALUE is the variable concept
235 and whose TREE_PURPOSE is the prototype parameter. */
237 tree
238 resolve_variable_concept_check (tree id)
240 tree tmpl = TREE_OPERAND (id, 0);
241 tree args = TREE_OPERAND (id, 1);
243 if (!variable_concept_p (tmpl))
244 return NULL_TREE;
246 /* Make sure that we have the right parameters before
247 assuming that it works. Note that failing to deduce
248 will result in diagnostics. */
249 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
250 tree result = coerce_template_parms (parms, args, tmpl);
251 if (result != error_mark_node)
253 tree decl = DECL_TEMPLATE_RESULT (tmpl);
254 tree proto = TREE_VALUE (TREE_VEC_ELT (parms, 0));
255 return build_tree_list (proto, decl);
257 else
258 return NULL_TREE;
262 /* Given a call expression or template-id expression to
263 a concept EXPR possibly including a wildcard, deduce
264 the concept being checked and the prototype parameter.
265 Returns true if the constraint and prototype can be
266 deduced and false otherwise. Note that the CHECK and
267 PROTO arguments are set to NULL_TREE if this returns
268 false. */
270 bool
271 deduce_constrained_parameter (tree expr, tree& check, tree& proto)
273 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
275 if (tree info = resolve_variable_concept_check (expr))
277 /* FIXME: What if the prototype is a pack?
278 Do the same as below? */
279 check = TREE_VALUE (info);
280 proto = TREE_PURPOSE (info);
281 return true;
283 else
285 check = proto = NULL_TREE;
286 return false;
289 else if (TREE_CODE (expr) == CALL_EXPR)
291 tree info = resolve_constraint_check (expr);
292 if (info && info != error_mark_node)
294 /* Get function and argument from the resolved
295 check expression and the prototype parameter.
296 Note that if the first argument was a pack, we
297 need to extract the first element to get the
298 prototype. */
299 check = TREE_VALUE (info);
300 tree arg = TREE_VEC_ELT (TREE_PURPOSE (info), 0);
301 if (ARGUMENT_PACK_P (arg))
302 arg = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0);
303 proto = TREE_TYPE (arg);
304 return true;
306 check = proto = NULL_TREE;
307 return false;
309 else
310 gcc_unreachable ();
313 // Given a call expression or template-id expression to a concept, EXPR,
314 // deduce the concept being checked and return the template arguments.
315 // Returns NULL_TREE if deduction fails.
316 static tree
317 deduce_concept_introduction (tree expr)
319 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
321 // Get the parameters from the template expression.
322 tree decl = TREE_OPERAND (expr, 0);
323 tree args = TREE_OPERAND (expr, 1);
324 tree var = DECL_TEMPLATE_RESULT (decl);
325 tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (decl));
327 parms = coerce_template_parms (parms, args, var);
328 // Check that we are returned a proper set of arguments.
329 if (parms == error_mark_node)
330 return NULL_TREE;
331 return parms;
333 else if (TREE_CODE (expr) == CALL_EXPR)
335 // Resolve the constraint check and return arguments.
336 tree info = resolve_constraint_check (expr);
337 if (info && info != error_mark_node)
338 return TREE_PURPOSE (info);
339 return NULL_TREE;
341 else
342 gcc_unreachable ();
345 namespace {
347 /*---------------------------------------------------------------------------
348 Lifting of concept definitions
349 ---------------------------------------------------------------------------*/
351 /* Part of constraint normalization. Whenever we find a reference to
352 a variable concept or a call to a function concept, we lift or
353 inline that concept's definition into the constraint. This ensures
354 that constraints are always checked in the immediate instantiation
355 context. */
357 tree lift_expression (tree);
359 /* If the tree T has operands, then lift any concepts out of them. */
360 tree
361 lift_operands (tree t)
363 if (int n = tree_operand_length (t))
365 t = copy_node (t);
366 for (int i = 0; i < n; ++i)
367 TREE_OPERAND (t, i) = lift_expression (TREE_OPERAND (t, i));
369 return t;
372 /* Recursively lift all operands of the function call. Also, check
373 that the call target is not accidentally a variable concept
374 since that's ill-formed. */
375 tree
376 lift_function_call (tree t)
378 gcc_assert (TREE_CODE (t) == CALL_EXPR);
379 tree target = CALL_EXPR_FN (t);
380 if (VAR_P (target)) {
381 error ("%qE cannot be used as a function", target);
382 return error_mark_node;
384 return lift_operands (t);
387 /* Inline a function (concept) definition by substituting
388 ARGS into its body. */
389 tree
390 lift_function_definition (tree fn, tree args)
392 /* Extract the body of the function minus the return expression. */
393 tree body = DECL_SAVED_TREE (fn);
394 if (!body)
395 return error_mark_node;
396 if (TREE_CODE (body) == BIND_EXPR)
397 body = BIND_EXPR_BODY (body);
398 if (TREE_CODE (body) != RETURN_EXPR)
399 return error_mark_node;
401 body = TREE_OPERAND (body, 0);
403 /* Substitute template arguments to produce our inline expression. */
404 tree result = tsubst_expr (body, args, tf_none, NULL_TREE, false);
405 if (result == error_mark_node)
406 return error_mark_node;
408 return lift_expression (result);
411 /* Inline a reference to a function concept. */
412 tree
413 lift_call_expression (tree t)
415 /* Try to resolve this function call as a concept. If not, then
416 it can be returned as-is. */
417 tree check = resolve_constraint_check (t);
418 if (!check)
419 return lift_function_call (t);
420 if (check == error_mark_node)
421 return error_mark_node;
423 tree fn = TREE_VALUE (check);
424 tree args = TREE_PURPOSE (check);
425 return lift_function_definition (fn, args);
428 tree
429 lift_variable_initializer (tree var, tree args)
431 /* Extract the body from the variable initializer. */
432 tree init = DECL_INITIAL (var);
433 if (!init)
434 return error_mark_node;
436 /* Substitute the arguments to form our new inline expression. */
437 tree result = tsubst_expr (init, args, tf_none, NULL_TREE, false);
438 if (result == error_mark_node)
439 return error_mark_node;
441 return lift_expression (result);
444 /* Inline a reference to a variable concept. */
445 tree
446 lift_variable_concept (tree t)
448 tree tmpl = TREE_OPERAND (t, 0);
449 tree args = TREE_OPERAND (t, 1);
450 tree decl = DECL_TEMPLATE_RESULT (tmpl);
451 gcc_assert (DECL_DECLARED_CONCEPT_P (decl));
453 /* Convert the template arguments to ensure that they match
454 the parameters of the variable concept. */
455 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
456 args = coerce_template_parms (parms, args, tmpl, tf_warning_or_error);
457 if (args == error_mark_node)
458 return error_mark_node;
460 return lift_variable_initializer (decl, args);
463 /* Determine if a template-id is a variable concept and inline.
465 TODO: I don't think that we get template-ids for variable
466 templates any more. They tend to be transformed into var-decl
467 specializations when an id-expression is parsed.
469 tree
470 lift_template_id (tree t)
472 if (variable_concept_p (TREE_OPERAND (t, 0)))
473 return lift_variable_concept (t);
475 /* Check that we didn't refer to a function concept like
476 a variable.
478 TODO: Add a note on how to fix this. */
479 tree tmpl = TREE_OPERAND (t, 0);
480 if (TREE_CODE (tmpl) == OVERLOAD)
482 tree fn = OVL_FUNCTION (tmpl);
483 if (TREE_CODE (fn) == TEMPLATE_DECL
484 && DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (fn)))
486 error ("invalid reference to function concept %qD", fn);
487 return error_mark_node;
491 return t;
494 /* If the variable declaration is a specialization of a concept
495 declaration, then inline its initializer. */
496 tree
497 lift_variable (tree t)
499 if (tree ti = DECL_TEMPLATE_INFO (t))
501 tree tmpl = TI_TEMPLATE (ti);
502 tree args = TI_ARGS (ti);
503 tree decl = DECL_TEMPLATE_RESULT (tmpl);
504 if (DECL_DECLARED_CONCEPT_P (decl))
505 return lift_variable_initializer (decl, args);
507 return t;
510 /* Lift any constraints appearing in a nested requirement of
511 a requires-expression. */
512 tree
513 lift_requires_expression (tree t)
515 tree parms = TREE_OPERAND (t, 0);
516 tree reqs = TREE_OPERAND (t, 1);
517 tree result = NULL_TREE;
518 for (; reqs != NULL_TREE; reqs = TREE_CHAIN (reqs))
520 tree req = TREE_VALUE (reqs);
521 if (TREE_CODE (req) == NESTED_REQ)
523 tree expr = lift_expression (TREE_OPERAND (req, 0));
524 req = finish_nested_requirement (expr);
526 result = tree_cons (NULL_TREE, req, result);
528 return finish_requires_expr (parms, result);
531 /* Inline references to specializations of concepts. */
532 tree
533 lift_expression (tree t)
535 if (t == NULL_TREE)
536 return NULL_TREE;
538 if (t == error_mark_node)
539 return error_mark_node;
541 /* Concepts can be referred to by call or variable. All other
542 nodes are preserved. */
543 switch (TREE_CODE (t))
545 case CALL_EXPR:
546 return lift_call_expression (t);
548 case TEMPLATE_ID_EXPR:
549 return lift_template_id (t);
551 case VAR_DECL:
552 return lift_variable (t);
554 case REQUIRES_EXPR:
555 return lift_requires_expression (t);
557 case EXPR_PACK_EXPANSION:
558 /* Defer the lifting of pack expansions until constraint
559 evaluation. We know that we're not going to be doing
560 anything else with this predicate constraint until
561 we expand it. */
562 return t;
564 case TREE_LIST:
566 t = copy_node (t);
567 TREE_VALUE (t) = lift_expression (TREE_VALUE (t));
568 TREE_CHAIN (t) = lift_expression (TREE_CHAIN (t));
569 return t;
572 default:
573 return lift_operands (t);
577 /*---------------------------------------------------------------------------
578 Transformation of expressions into constraints
579 ---------------------------------------------------------------------------*/
581 /* Part of constraint normalization. The following functions rewrite
582 expressions as constraints. */
584 tree transform_expression (tree);
586 /* Check that the logical-or or logical-and expression does
587 not result in a call to a user-defined user-defined operator
588 (temp.constr.op). Returns true if the logical operator is
589 admissible and false otherwise. */
591 bool
592 check_logical_expr (tree t)
594 /* We can't do much for type dependent expressions. */
595 if (type_dependent_expression_p (t))
596 return true;
598 /* Resolve the logical operator. Note that template processing is
599 disabled so we get the actual call or target expression back.
600 not_processing_template_sentinel sentinel.
602 TODO: This check is actually subsumed by the requirement that
603 constraint operands have type bool. I'm not sure we need it
604 unless we allow conversions. */
605 tree arg1 = TREE_OPERAND (t, 0);
606 tree arg2 = TREE_OPERAND (t, 1);
607 tree ovl = NULL_TREE;
608 tree expr = build_x_binary_op (EXPR_LOC_OR_LOC (arg2, input_location),
609 TREE_CODE (t),
610 arg1, TREE_CODE (arg1),
611 arg2, TREE_CODE (arg2),
612 &ovl,
613 tf_none);
614 if (TREE_CODE (expr) != TREE_CODE (t))
616 error ("user-defined operator %qs in constraint %qE",
617 operator_name_info[TREE_CODE (t)].name, t);
618 return false;
620 return true;
623 /* Transform a logical-or or logical-and expression into either
624 a conjunction or disjunction. */
626 tree
627 xform_logical (tree t, tree_code c)
629 if (!check_logical_expr (t))
630 return error_mark_node;
631 tree t0 = transform_expression (TREE_OPERAND (t, 0));
632 tree t1 = transform_expression (TREE_OPERAND (t, 1));
633 return build_nt (c, t0, t1);
636 /* A simple requirement T introduces an expression constraint
637 for its expression. */
639 inline tree
640 xform_simple_requirement (tree t)
642 return build_nt (EXPR_CONSTR, TREE_OPERAND (t, 0));
645 /* A type requirement T introduce a type constraint for its type. */
647 inline tree
648 xform_type_requirement (tree t)
650 return build_nt (TYPE_CONSTR, TREE_OPERAND (t, 0));
653 /* A compound requirement T introduces a conjunction of constraints
654 depending on its form. The conjunction always includes an
655 expression constraint for the expression of the requirement.
656 If a trailing return type was specified, the conjunction includes
657 either an implicit conversion constraint or an argument deduction
658 constraint. If the noexcept specifier is present, the conjunction
659 includes an exception constraint. */
661 tree
662 xform_compound_requirement (tree t)
664 tree expr = TREE_OPERAND (t, 0);
665 tree constr = build_nt (EXPR_CONSTR, TREE_OPERAND (t, 0));
667 /* If a type is given, append an implicit conversion or
668 argument deduction constraint. */
669 if (tree type = TREE_OPERAND (t, 1))
671 tree type_constr;
672 /* TODO: We should be extracting a list of auto nodes
673 from type_uses_auto, not a single node */
674 if (tree placeholder = type_uses_auto (type))
675 type_constr = build_nt (DEDUCT_CONSTR, expr, type, placeholder);
676 else
677 type_constr = build_nt (ICONV_CONSTR, expr, type);
678 constr = conjoin_constraints (constr, type_constr);
681 /* If noexcept is present, append an exception constraint. */
682 if (COMPOUND_REQ_NOEXCEPT_P (t))
684 tree except = build_nt (EXCEPT_CONSTR, expr);
685 constr = conjoin_constraints (constr, except);
688 return constr;
691 /* A nested requirement T introduces a conjunction of constraints
692 corresponding to its constraint-expression. */
694 inline tree
695 xform_nested_requirement (tree t)
697 return transform_expression (TREE_OPERAND (t, 0));
700 /* Transform a requirement T into one or more constraints. */
702 tree
703 xform_requirement (tree t)
705 switch (TREE_CODE (t))
707 case SIMPLE_REQ:
708 return xform_simple_requirement (t);
710 case TYPE_REQ:
711 return xform_type_requirement (t);
713 case COMPOUND_REQ:
714 return xform_compound_requirement (t);
716 case NESTED_REQ:
717 return xform_nested_requirement (t);
719 default:
720 gcc_unreachable ();
722 return error_mark_node;
725 /* Transform a sequence of requirements into a conjunction of
726 constraints. */
728 tree
729 xform_requirements (tree t)
731 tree result = NULL_TREE;
732 for (; t; t = TREE_CHAIN (t)) {
733 tree constr = xform_requirement (TREE_VALUE (t));
734 result = conjoin_constraints (result, constr);
736 return result;
739 /* Transform a requires-expression into a parameterized constraint. */
741 tree
742 xform_requires_expr (tree t)
744 tree operand = xform_requirements (TREE_OPERAND (t, 1));
745 if (tree parms = TREE_OPERAND (t, 0))
746 return build_nt (PARM_CONSTR, parms, operand);
747 else
748 return operand;
751 /* Transform an expression into an atomic predicate constraint.
752 After substitution, the expression of a predicate constraint
753 shall have type bool (temp.constr.pred). For non-type-dependent
754 expressions, we can check that now. */
756 tree
757 xform_atomic (tree t)
759 if (TREE_TYPE (t) && !type_dependent_expression_p (t))
761 tree type = cv_unqualified (TREE_TYPE (t));
762 if (!same_type_p (type, boolean_type_node))
764 error ("predicate constraint %qE does not have type %<bool%>", t);
765 return error_mark_node;
768 return build_nt (PRED_CONSTR, t);
771 /* Transform an expression into a constraint. */
773 tree
774 xform_expr (tree t)
776 switch (TREE_CODE (t))
778 case TRUTH_ANDIF_EXPR:
779 return xform_logical (t, CONJ_CONSTR);
781 case TRUTH_ORIF_EXPR:
782 return xform_logical (t, DISJ_CONSTR);
784 case REQUIRES_EXPR:
785 return xform_requires_expr (t);
787 case BIND_EXPR:
788 return transform_expression (BIND_EXPR_BODY (t));
790 default:
791 /* All other constraints are atomic. */
792 return xform_atomic (t);
796 /* Transform a statement into an expression. */
798 tree
799 xform_stmt (tree t)
801 switch (TREE_CODE (t))
803 case RETURN_EXPR:
804 return transform_expression (TREE_OPERAND (t, 0));
805 default:
806 gcc_unreachable ();
808 return error_mark_node;
811 /* Reduction rules for the declaration T. */
813 tree
814 xform_decl (tree t)
816 switch (TREE_CODE (t))
818 case VAR_DECL:
819 return xform_atomic (t);
820 default:
821 gcc_unreachable ();
823 return error_mark_node;
827 /* Transform a lifted expression into a constraint. This either
828 returns a constraint, or it returns error_mark_node when
829 a constraint cannot be formed. */
831 tree
832 transform_expression (tree t)
834 if (!t)
835 return NULL_TREE;
837 if (t == error_mark_node)
838 return error_mark_node;
840 switch (TREE_CODE_CLASS (TREE_CODE (t)))
842 case tcc_unary:
843 case tcc_binary:
844 case tcc_expression:
845 case tcc_vl_exp:
846 return xform_expr (t);
848 case tcc_statement:
849 return xform_stmt (t);
851 case tcc_declaration:
852 return xform_decl (t);
854 case tcc_exceptional:
855 case tcc_constant:
856 case tcc_reference:
857 case tcc_comparison:
858 /* These are all atomic predicate constraints. */
859 return xform_atomic (t);
861 default:
862 /* Unhandled node kind. */
863 gcc_unreachable ();
865 return error_mark_node;
868 /*---------------------------------------------------------------------------
869 Constraint normalization
870 ---------------------------------------------------------------------------*/
872 tree normalize_constraint (tree);
874 /* The normal form of the disjunction T0 /\ T1 is the conjunction
875 of the normal form of T0 and the normal form of T1. */
877 inline tree
878 normalize_conjunction (tree t)
880 tree t0 = normalize_constraint (TREE_OPERAND (t, 0));
881 tree t1 = normalize_constraint (TREE_OPERAND (t, 1));
882 return build_nt (CONJ_CONSTR, t0, t1);
885 /* The normal form of the disjunction T0 \/ T1 is the disjunction
886 of the normal form of T0 and the normal form of T1. */
888 inline tree
889 normalize_disjunction (tree t)
891 tree t0 = normalize_constraint (TREE_OPERAND (t, 0));
892 tree t1 = normalize_constraint (TREE_OPERAND (t, 1));
893 return build_nt (DISJ_CONSTR, t0, t1);
896 /* A predicate constraint is normalized in two stages. First all
897 references specializations of concepts are replaced by their
898 substituted definitions. Then, the resulting expression is
899 transformed into a constraint by transforming && expressions
900 into conjunctions and || into disjunctions. */
902 tree
903 normalize_predicate_constraint (tree t)
905 tree expr = PRED_CONSTR_EXPR (t);
906 tree lifted = lift_expression (expr);
907 tree constr = transform_expression (lifted);
908 return constr;
911 /* The normal form of a parameterized constraint is the normal
912 form of its operand. */
914 tree
915 normalize_parameterized_constraint (tree t)
917 tree parms = PARM_CONSTR_PARMS (t);
918 tree operand = normalize_constraint (PARM_CONSTR_OPERAND (t));
919 return build_nt (PARM_CONSTR, parms, operand);
922 /* Normalize the constraint T by reducing it so that it is
923 comprised of only conjunctions and disjunctions of atomic
924 constraints. */
926 tree
927 normalize_constraint (tree t)
929 if (!t)
930 return NULL_TREE;
932 switch (TREE_CODE (t))
934 case CONJ_CONSTR:
935 return normalize_conjunction (t);
937 case DISJ_CONSTR:
938 return normalize_disjunction (t);
940 case PRED_CONSTR:
941 return normalize_predicate_constraint (t);
943 case PARM_CONSTR:
944 return normalize_parameterized_constraint (t);
946 case EXPR_CONSTR:
947 case TYPE_CONSTR:
948 case ICONV_CONSTR:
949 case DEDUCT_CONSTR:
950 case EXCEPT_CONSTR:
951 /* These constraints are defined to be atomic. */
952 return t;
954 default:
955 /* CONSTR was not a constraint. */
956 gcc_unreachable();
958 return error_mark_node;
961 } /* namespace */
964 // -------------------------------------------------------------------------- //
965 // Constraint Semantic Processing
967 // The following functions are called by the parser and substitution rules
968 // to create and evaluate constraint-related nodes.
971 // If the recently parsed TYPE declares or defines a template or template
972 // specialization, get its corresponding constraints from the current
973 // template parameters and bind them to TYPE's declaration.
974 tree
975 associate_classtype_constraints (tree type)
977 if (!type || type == error_mark_node || TREE_CODE (type) != RECORD_TYPE)
978 return type;
980 // An explicit class template specialization has no template
981 // parameters.
982 if (!current_template_parms)
983 return type;
985 if (CLASSTYPE_IS_TEMPLATE (type) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
987 tree decl = TYPE_STUB_DECL (type);
988 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
989 tree ci = build_constraints (reqs, NULL_TREE);
991 // An implicitly instantiated member template declaration already
992 // has associated constraints. If it is defined outside of its
993 // class, then we need match these constraints against those of
994 // original declaration.
995 if (tree orig_ci = get_constraints (decl))
997 if (!equivalent_constraints (ci, orig_ci))
999 // FIXME: Improve diagnostics.
1000 error ("%qT does not match any declaration", type);
1001 return error_mark_node;
1003 return type;
1005 set_constraints (decl, ci);
1007 return type;
1010 namespace {
1012 // Create an empty constraint info block.
1013 inline tree_constraint_info*
1014 build_constraint_info ()
1016 return (tree_constraint_info *)make_node (CONSTRAINT_INFO);
1019 } // namespace
1021 /* Build a constraint-info object that contains the
1022 associated condstraints of a declaration. This also
1023 includes the declaration's template requirements (TREQS)
1024 and any trailing requirements for a function declarator
1025 (DREQS). Note that both TREQS and DREQS must be constraints.
1027 If the declaration has neither template nor declaration
1028 requirements this returns NULL_TREE, indicating an
1029 unconstrained declaration. */
1030 tree
1031 build_constraints (tree tmpl_reqs, tree decl_reqs)
1033 gcc_assert (tmpl_reqs ? constraint_p (tmpl_reqs) : true);
1034 gcc_assert (decl_reqs ? constraint_p (decl_reqs) : true);
1036 if (!tmpl_reqs && !decl_reqs)
1037 return NULL_TREE;
1039 tree_constraint_info* ci = build_constraint_info ();
1040 ci->template_reqs = tmpl_reqs;
1041 ci->declarator_reqs = decl_reqs;
1042 ci->associated_constr = conjoin_constraints (tmpl_reqs, decl_reqs);
1044 ++processing_template_decl;
1045 ci->normalized_constr = normalize_constraint (ci->associated_constr);
1046 --processing_template_decl;
1048 ci->assumptions = decompose_assumptions (ci->normalized_constr);
1049 return (tree)ci;
1052 namespace {
1054 /* Returns true if any of the arguments in the template
1055 argument list is a wildcard or wildcard pack. */
1056 bool
1057 contains_wildcard_p (tree args)
1059 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i) {
1060 tree arg = TREE_VEC_ELT (args, i);
1061 if (TREE_CODE (arg) == WILDCARD_DECL)
1062 return true;
1064 return false;
1067 /* Build a new call expression, but don't actually generate
1068 a new function call. We just want the tree, not the
1069 semantics. */
1070 inline tree
1071 build_call_check (tree id)
1073 ++processing_template_decl;
1074 vec<tree, va_gc> *fargs = make_tree_vector();
1075 tree call = finish_call_expr (id, &fargs, false, false, tf_none);
1076 --processing_template_decl;
1077 return call;
1080 /* Build an expression that will check a variable concept. If any
1081 argument contains a wildcard, don't try to finish the variable
1082 template because we can't substitute into a non-existent
1083 declaration. */
1084 tree
1085 build_variable_check (tree id)
1087 gcc_assert (TREE_CODE (id) == TEMPLATE_ID_EXPR);
1088 if (contains_wildcard_p (TREE_OPERAND (id, 1)))
1089 return id;
1091 ++processing_template_decl;
1092 tree var = finish_template_variable (id);
1093 --processing_template_decl;
1094 return var;
1097 /* Construct a sequence of template arguments by prepending
1098 ARG to REST. Either ARG or REST may be null. */
1099 tree
1100 build_concept_check_arguments (tree arg, tree rest)
1102 gcc_assert (rest ? TREE_CODE (rest) == TREE_VEC : true);
1103 int n = rest ? TREE_VEC_LENGTH (rest) : 0;
1104 tree args;
1105 if (arg)
1107 args = make_tree_vec (n + 1);
1108 TREE_VEC_ELT (args, 0) = arg;
1109 if (rest)
1110 for (int i = 0; i < n; ++i)
1111 TREE_VEC_ELT (args, i + 1) = TREE_VEC_ELT (rest, i);
1112 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, n + 1);
1114 else
1116 gcc_assert (rest != NULL_TREE);
1117 args = rest;
1119 return args;
1122 /* Construct a template-id that will resolve to a concept
1123 check at some point in the future. This is used to
1124 represent concept checks nested in a pack expansion. */
1125 tree
1126 build_template_id_check (tree target, tree arg, tree rest)
1128 tree args = build_concept_check_arguments (arg, rest);
1129 if (variable_template_p (target))
1130 return lookup_template_variable (target, args);
1131 else
1132 return build_call_check (lookup_template_function (target, args));
1135 } // namespace
1137 /* Construct an expression that checks the concept given by
1138 TARGET. The TARGET must be:
1140 - an OVERLOAD referring to one or more function concepts
1141 - a BASELINK referring to an overload set of the above, or
1142 - a TEMPLTATE_DECL referring to a variable concept.
1144 ARG and REST are the explicit template arguments for the
1145 eventual concept check. */
1146 tree
1147 build_concept_check (tree target, tree arg, tree rest)
1149 tree args = build_concept_check_arguments (arg, rest);
1150 if (variable_template_p (target))
1151 return build_variable_check (lookup_template_variable (target, args));
1152 else
1153 return build_call_check (lookup_template_function (target, args));
1157 /* Returns a TYPE_DECL that contains sufficient information to
1158 build a template parameter of the same kind as PROTO and
1159 constrained by the concept declaration FN. PROTO is saved as
1160 the initializer of the new type decl, and the constraining
1161 function is saved in DECL_SIZE_UNIT.
1163 If specified, ARGS provides additional arguments to the
1164 constraint check. These are stored in the DECL_SIZE field. */
1165 tree
1166 build_constrained_parameter (tree fn, tree proto, tree args)
1168 tree name = DECL_NAME (fn);
1169 tree type = TREE_TYPE (proto);
1170 tree decl = build_decl (input_location, TYPE_DECL, name, type);
1171 DECL_INITIAL (decl) = proto; // Describing parameter
1172 DECL_SIZE_UNIT (decl) = fn; // Constraining function declaration
1173 DECL_SIZE (decl) = args; // Extra template arguments.
1174 return decl;
1177 /* Create a constraint expression for the given DECL that
1178 evaluates the requirements specified by CONSTR, a TYPE_DECL
1179 that contains all the information necessary to build the
1180 requirements (see finish_concept_name for the layout of
1181 that TYPE_DECL).
1183 Note that the constraints are neither reduced nor decomposed.
1184 That is done only after the requires clause has been parsed
1185 (or not). */
1186 tree
1187 finish_shorthand_constraint (tree decl, tree constr)
1189 /* No requirements means no constraints. */
1190 if (!constr)
1191 return NULL_TREE;
1193 tree proto = DECL_INITIAL (constr); /* The prototype declaration */
1194 tree con = DECL_SIZE_UNIT (constr); /* The concept declaration */
1195 tree args = DECL_SIZE (constr); /* Extra template arguments */
1197 /* If the parameter declaration is variadic, but the concept
1198 is not then we need to apply the concept to every element
1199 in the pack. */
1200 bool is_proto_pack = template_parameter_pack_p (proto);
1201 bool is_decl_pack = template_parameter_pack_p (decl);
1202 bool apply_to_all_p = is_decl_pack && !is_proto_pack;
1204 /* Get the argument and overload used for the requirement
1205 and adjust it if we're going to expand later. */
1206 tree arg = template_parm_to_arg (build_tree_list (NULL_TREE, decl));
1207 if (apply_to_all_p)
1208 arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
1210 /* Build the concept check. If it the constraint needs to be
1211 applied to all elements of the parameter pack, then make
1212 the constraint an expansion. */
1213 tree check;
1214 tree tmpl = DECL_TI_TEMPLATE (con);
1215 if (TREE_CODE (con) == VAR_DECL)
1217 if (!apply_to_all_p)
1218 check = build_concept_check (tmpl, arg, args);
1219 else
1220 check = build_template_id_check (tmpl, arg, args);
1222 else
1224 tree ovl = build_overload (tmpl, NULL_TREE);
1225 check = build_concept_check (ovl, arg, args);
1228 /* Make the check a pack expansion if needed.
1230 FIXME: We should be making a fold expression. */
1231 if (apply_to_all_p)
1233 check = make_pack_expansion (check);
1234 TREE_TYPE (check) = boolean_type_node;
1237 return make_predicate_constraint (check);
1240 /* Returns a conjunction of shorthand requirements for the template
1241 parameter list PARMS. Note that the requirements are stored in
1242 the TYPE of each tree node. */
1243 tree
1244 get_shorthand_constraints (tree parms)
1246 tree result = NULL_TREE;
1247 parms = INNERMOST_TEMPLATE_PARMS (parms);
1248 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
1250 tree parm = TREE_VEC_ELT (parms, i);
1251 tree constr = TEMPLATE_PARM_CONSTRAINTS (parm);
1252 result = conjoin_constraints (result, constr);
1254 return result;
1257 // Returns and chains a new parameter for PARAMETER_LIST which will conform
1258 // to the prototype given by SRC_PARM. The new parameter will have its
1259 // identifier and location set according to IDENT and PARM_LOC respectively.
1260 static tree
1261 process_introduction_parm (tree parameter_list, tree src_parm)
1263 // If we have a pack, we should have a single pack argument which is the
1264 // placeholder we want to look at.
1265 bool is_parameter_pack = ARGUMENT_PACK_P (src_parm);
1266 if (is_parameter_pack)
1267 src_parm = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (src_parm), 0);
1269 // At this point we should have a wildcard, but we want to
1270 // grab the associated decl from it. Also grab the stored
1271 // identifier and location that should be chained to it in
1272 // a PARM_DECL.
1273 gcc_assert (TREE_CODE (src_parm) == WILDCARD_DECL);
1275 tree ident = DECL_NAME (src_parm);
1276 location_t parm_loc = DECL_SOURCE_LOCATION (src_parm);
1278 // If we expect a pack and the deduced template is not a pack, or if the
1279 // template is using a pack and we didn't declare a pack, throw an error.
1280 if (is_parameter_pack != WILDCARD_PACK_P (src_parm))
1282 error_at (parm_loc, "cannot match pack for introduced parameter");
1283 tree err_parm = build_tree_list (error_mark_node, error_mark_node);
1284 return chainon (parameter_list, err_parm);
1287 src_parm = TREE_TYPE (src_parm);
1289 tree parm;
1290 bool is_non_type;
1291 if (TREE_CODE (src_parm) == TYPE_DECL)
1293 is_non_type = false;
1294 parm = finish_template_type_parm (class_type_node, ident);
1296 else if (TREE_CODE (src_parm) == TEMPLATE_DECL)
1298 is_non_type = false;
1299 current_template_parms = DECL_TEMPLATE_PARMS (src_parm);
1300 parm = finish_template_template_parm (class_type_node, ident);
1302 else
1304 is_non_type = true;
1306 // Since we don't have a declarator, so we can copy the source
1307 // parameter and change the name and eventually the location.
1308 parm = copy_decl (src_parm);
1309 DECL_NAME (parm) = ident;
1312 // Wrap in a TREE_LIST for process_template_parm. Introductions do not
1313 // retain the defaults from the source template.
1314 parm = build_tree_list (NULL_TREE, parm);
1316 return process_template_parm (parameter_list, parm_loc, parm,
1317 is_non_type, is_parameter_pack);
1320 /* Associates a constraint check to the current template based
1321 on the introduction parameters. INTRO_LIST must be a TREE_VEC
1322 of WILDCARD_DECLs containing a chained PARM_DECL which
1323 contains the identifier as well as the source location.
1324 TMPL_DECL is the decl for the concept being used. If we
1325 take a concept, C, this will form a check in the form of
1326 C<INTRO_LIST> filling in any extra arguments needed by the
1327 defaults deduced.
1329 Returns NULL_TREE if no concept could be matched and
1330 error_mark_node if an error occurred when matching. */
1331 tree
1332 finish_template_introduction (tree tmpl_decl, tree intro_list)
1334 /* Deduce the concept check. */
1335 tree expr = build_concept_check (tmpl_decl, NULL_TREE, intro_list);
1336 if (expr == error_mark_node)
1337 return NULL_TREE;
1339 tree parms = deduce_concept_introduction (expr);
1340 if (!parms)
1341 return NULL_TREE;
1343 /* Build template parameter scope for introduction. */
1344 tree parm_list = NULL_TREE;
1345 begin_template_parm_list ();
1346 int nargs = MIN (TREE_VEC_LENGTH (parms), TREE_VEC_LENGTH (intro_list));
1347 for (int n = 0; n < nargs; ++n)
1348 parm_list = process_introduction_parm (parm_list, TREE_VEC_ELT (parms, n));
1349 parm_list = end_template_parm_list (parm_list);
1350 for (int i = 0; i < TREE_VEC_LENGTH (parm_list); ++i)
1351 if (TREE_VALUE (TREE_VEC_ELT (parm_list, i)) == error_mark_node)
1353 end_template_decl ();
1354 return error_mark_node;
1357 /* Build a concept check for our constraint. */
1358 tree check_args = make_tree_vec (TREE_VEC_LENGTH (parms));
1359 int n = 0;
1360 for (; n < TREE_VEC_LENGTH (parm_list); ++n)
1362 tree parm = TREE_VEC_ELT (parm_list, n);
1363 TREE_VEC_ELT (check_args, n) = template_parm_to_arg (parm);
1366 /* If the template expects more parameters we should be able
1367 to use the defaults from our deduced concept. */
1368 for (; n < TREE_VEC_LENGTH (parms); ++n)
1369 TREE_VEC_ELT (check_args, n) = TREE_VEC_ELT (parms, n);
1371 /* Associate the constraint. */
1372 tree check = build_concept_check (tmpl_decl, NULL_TREE, check_args);
1373 tree constr = make_predicate_constraint (check);
1374 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = constr;
1376 return parm_list;
1380 /* Make a "constrained auto" type-specifier. This is an
1381 auto type with constraints that must be associated after
1382 deduction. The constraint is formed from the given
1383 CONC and its optional sequence of arguments, which are
1384 non-null if written as partial-concept-id. */
1385 tree
1386 make_constrained_auto (tree con, tree args)
1388 tree type = make_auto();
1390 /* Build the constraint. */
1391 tree tmpl = DECL_TI_TEMPLATE (con);
1392 tree expr;
1393 if (VAR_P (con))
1394 expr = build_concept_check (tmpl, type, args);
1395 else
1396 expr = build_concept_check (build_overload (tmpl, NULL_TREE), type, args);
1398 ++processing_template_decl;
1399 tree constr = transform_expression (lift_expression (expr));
1400 --processing_template_decl;
1402 /* Attach the constraint to the type declaration. */
1403 tree decl = TYPE_NAME (type);
1404 DECL_SIZE_UNIT (decl) = constr;
1405 return decl;
1409 /*---------------------------------------------------------------------------
1410 Constraint substitution
1411 ---------------------------------------------------------------------------*/
1413 /* The following functions implement substitution rules for constraints.
1414 Substitution without checking constraints happens only in the
1415 instantiation of class templates. For example:
1417 template<C1 T> struct S {
1418 void f(T) requires C2<T>;
1419 void g(T) requires T::value;
1422 S<int> s; // error instantiating S<int>::g(T)
1424 When we instantiate S, we substitute into its member declarations,
1425 including their constraints. However, those constraints are not
1426 checked. Substituting int into C2<T> yields C2<int>, and substituting
1427 into T::value yields a substitution failure, making the program
1428 ill-formed.
1430 Note that we only ever substitute into the associated constraints
1431 of a declaration. That is, substitution is defined only for predicate
1432 constraints and conjunctions. */
1434 /* Substitute into the predicate constraints. Returns error_mark_node
1435 if the substitution into the expression fails. */
1436 tree
1437 tsubst_predicate_constraint (tree t, tree args,
1438 tsubst_flags_t complain, tree in_decl)
1440 tree expr = PRED_CONSTR_EXPR (t);
1441 ++processing_template_decl;
1442 tree result = tsubst_expr (expr, args, complain, in_decl, false);
1443 --processing_template_decl;
1444 return build_nt (PRED_CONSTR, result);
1447 /* Substitute into the conjunction of constraints. Returns
1448 error_mark_node if substitution into either operand fails. */
1449 tree
1450 tsubst_conjunction (tree t, tree args,
1451 tsubst_flags_t complain, tree in_decl)
1453 tree t0 = TREE_OPERAND (t, 0);
1454 tree r0 = tsubst_constraint (t0, args, complain, in_decl);
1455 tree t1 = TREE_OPERAND (t, 1);
1456 tree r1 = tsubst_constraint (t1, args, complain, in_decl);
1457 return build_nt (CONJ_CONSTR, r0, r1);
1460 /* Substitute ARGS into the constraint T. */
1461 tree
1462 tsubst_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1464 if (TREE_CODE (t) == CONJ_CONSTR)
1465 return tsubst_conjunction (t, args, complain, in_decl);
1466 else if (TREE_CODE (t) == PRED_CONSTR)
1467 return tsubst_predicate_constraint (t, args, complain, in_decl);
1468 else
1469 gcc_unreachable ();
1470 return error_mark_node;
1473 namespace {
1475 /* A subroutine of tsubst_constraint_variables. In an unevaluated
1476 context, the substitution of PARM_DECLs are not properly chained
1477 during substitution. Do that here. */
1478 tree
1479 fixup_constraint_vars (tree parms)
1481 if (!parms)
1482 return parms;
1484 tree p = TREE_CHAIN (parms);
1485 tree q = parms;
1486 while (p && TREE_VALUE (p) != void_type_node)
1488 DECL_CHAIN (TREE_VALUE (q)) = TREE_VALUE (p);
1489 q = p;
1490 p = TREE_CHAIN (p);
1492 return parms;
1495 /* A subroutine of tsubst_constraint_variables. Register local
1496 specializations for each of parameter in PARMS and its
1497 corresponding substituted constraint variable in VARS.
1498 Returns VARS. */
1499 tree
1500 declare_constraint_vars (tree parms, tree vars)
1502 tree s = TREE_VALUE (vars);
1503 for (tree p = parms; p && !VOID_TYPE_P (TREE_VALUE (p)); p = TREE_CHAIN (p))
1505 tree t = TREE_VALUE (p);
1506 CONSTRAINT_VAR_P (t) = true;
1507 if (DECL_PACK_P (t))
1509 tree pack = extract_fnparm_pack (t, &s);
1510 register_local_specialization (pack, t);
1512 else
1514 register_local_specialization (s, t);
1515 s = TREE_CHAIN (s);
1518 return vars;
1521 /* A subroutine of tsubst_parameterized_constraint. Substitute ARGS
1522 into the parameter list T, producing a sequence of constraint
1523 variables, declared in the current scope.
1525 Note that the caller must establish a local specialization stack
1526 prior to calling this function since this substitution will
1527 declare the substituted parameters. */
1528 tree
1529 tsubst_constraint_variables (tree t, tree args,
1530 tsubst_flags_t complain, tree in_decl)
1532 tree vars = tsubst (t, args, complain, in_decl);
1533 if (vars == error_mark_node)
1534 return error_mark_node;
1535 return declare_constraint_vars (t, fixup_constraint_vars (vars));
1538 /* Substitute ARGS into the simple requirement T. Note that
1539 substitution may result in an ill-formed expression without
1540 causing the program to be ill-formed. In such cases, the
1541 requirement wraps an error_mark_node. */
1542 inline tree
1543 tsubst_simple_requirement (tree t, tree args,
1544 tsubst_flags_t complain, tree in_decl)
1546 ++processing_template_decl;
1547 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1548 --processing_template_decl;
1549 return finish_simple_requirement (expr);
1552 /* Substitute ARGS into the type requirement T. Note that
1553 substitution may result in an ill-formed type without
1554 causing the program to be ill-formed. In such cases, the
1555 requirement wraps an error_mark_node. */
1556 inline tree
1557 tsubst_type_requirement (tree t, tree args,
1558 tsubst_flags_t complain, tree in_decl)
1560 ++processing_template_decl;
1561 tree type = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
1562 --processing_template_decl;
1563 return finish_type_requirement (type);
1566 /* Substitute args into the compound requirement T. If substituting
1567 into either the expression or the type fails, the corresponding
1568 operands in the resulting node will be error_mark_node. This
1569 preserves a requirement for the purpose of partial ordering, but
1570 it will never be satisfied. */
1571 tree
1572 tsubst_compound_requirement (tree t, tree args,
1573 tsubst_flags_t complain, tree in_decl)
1575 ++processing_template_decl;
1576 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1577 tree type = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
1578 --processing_template_decl;
1579 bool noexcept_p = COMPOUND_REQ_NOEXCEPT_P (t);
1580 return finish_compound_requirement (expr, type, noexcept_p);
1583 /* Substitute ARGS into the nested requirement T. */
1584 tree
1585 tsubst_nested_requirement (tree t, tree args,
1586 tsubst_flags_t complain, tree in_decl)
1588 ++processing_template_decl;
1589 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1590 --processing_template_decl;
1591 return finish_nested_requirement (expr);
1594 inline tree
1595 tsubst_requirement (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1597 switch (TREE_CODE (t))
1599 case SIMPLE_REQ:
1600 return tsubst_simple_requirement (t, args, complain, in_decl);
1601 case TYPE_REQ:
1602 return tsubst_type_requirement (t, args, complain, in_decl);
1603 case COMPOUND_REQ:
1604 return tsubst_compound_requirement (t, args, complain, in_decl);
1605 case NESTED_REQ:
1606 return tsubst_nested_requirement (t, args, complain, in_decl);
1607 default:
1608 gcc_unreachable ();
1610 return error_mark_node;
1613 /* Substitute ARGS into the list of requirements T. Note that
1614 substitution failures here result in ill-formed programs. */
1615 tree
1616 tsubst_requirement_body (tree t, tree args,
1617 tsubst_flags_t complain, tree in_decl)
1619 tree r = NULL_TREE;
1620 while (t)
1622 tree e = tsubst_requirement (TREE_VALUE (t), args, complain, in_decl);
1623 if (e == error_mark_node)
1624 return error_mark_node;
1625 r = tree_cons (NULL_TREE, e, r);
1626 t = TREE_CHAIN (t);
1628 return r;
1631 } /* namespace */
1633 /* Substitute ARGS into the requires expression T. Note that this
1634 results in the re-declaration of local parameters when
1635 substituting through the parameter list. If either substitution
1636 fails, the program is ill-formed. */
1637 tree
1638 tsubst_requires_expr (tree t, tree args,
1639 tsubst_flags_t complain, tree in_decl)
1641 local_specialization_stack stack;
1643 tree parms = TREE_OPERAND (t, 0);
1644 if (parms)
1646 parms = tsubst_constraint_variables (parms, args, complain, in_decl);
1647 if (parms == error_mark_node)
1648 return error_mark_node;
1651 tree reqs = TREE_OPERAND (t, 1);
1652 reqs = tsubst_requirement_body (reqs, args, complain, in_decl);
1653 if (reqs == error_mark_node)
1654 return error_mark_node;
1656 return finish_requires_expr (parms, reqs);
1659 /* Substitute ARGS into the constraint information CI, producing a new
1660 constraint record. */
1661 tree
1662 tsubst_constraint_info (tree t, tree args,
1663 tsubst_flags_t complain, tree in_decl)
1665 if (!t || t == error_mark_node || !check_constraint_info (t))
1666 return NULL_TREE;
1668 tree tmpl_constr = NULL_TREE;
1669 if (tree r = CI_TEMPLATE_REQS (t))
1670 tmpl_constr = tsubst_constraint (r, args, complain, in_decl);
1672 tree decl_constr = NULL_TREE;
1673 if (tree r = CI_DECLARATOR_REQS (t))
1674 decl_constr = tsubst_constraint (r, args, complain, in_decl);
1676 return build_constraints (tmpl_constr, decl_constr);
1680 /*---------------------------------------------------------------------------
1681 Constraint satisfaction
1682 ---------------------------------------------------------------------------*/
1684 /* The following functions determine if a constraint, when
1685 substituting template arguments, is satisfied. For convenience,
1686 satisfaction reduces a constraint to either true or false (and
1687 nothing else). */
1689 namespace {
1691 tree satisfy_constraint (tree, tree, tsubst_flags_t, tree);
1693 /* Check the pack expansion by first transforming it into a
1694 conjunction of constraints. */
1696 tree
1697 satisfy_pack_expansion (tree t, tree args,
1698 tsubst_flags_t complain, tree in_decl)
1700 /* Check that somebody isn't arbitrarily expanding packs
1701 as part of a predicate. Note that packs are normally
1702 untyped, however, we use the type field as a hack to
1703 indicate folded boolean expressions generated from a
1704 shorthand constraint. This check should disappear with
1705 fold expressions. */
1706 if (!TREE_TYPE (t) || !same_type_p (TREE_TYPE (t), boolean_type_node))
1708 error ("invalid pack expansion in constraint %qE", t);
1709 return boolean_false_node;
1712 /* Get the vector of expanded arguments. */
1713 tree exprs = tsubst_pack_expansion (t, args, complain, in_decl);
1714 if (exprs == error_mark_node)
1715 return boolean_false_node;
1716 int n = TREE_VEC_LENGTH (exprs);
1718 /* An empty expansion is inherently satisfied. */
1719 if (n == 0)
1720 return boolean_true_node;
1722 /* Build a conjunction of constraints from the resulting
1723 expansions and then check that. */
1724 tree result = NULL_TREE;
1725 for (int i = 0; i < n; ++i)
1727 tree expr = TREE_VEC_ELT (exprs, i);
1728 tree constr = transform_expression (expr);
1729 result = conjoin_constraints (result, constr);
1731 return satisfy_constraint (result, args, complain, in_decl);
1734 /* A predicate constraint is satisfied if its expression evaluates
1735 to true. If substitution into that node fails, the constraint
1736 is not satisfied ([temp.constr.pred]).
1738 Note that a predicate constraint is a constraint expression
1739 of type bool. If neither of those are true, the program is
1740 ill-formed; they are not SFINAE'able errors. */
1742 tree
1743 satisfy_predicate_constraint (tree t, tree args,
1744 tsubst_flags_t complain, tree in_decl)
1746 tree original = TREE_OPERAND (t, 0);
1748 /* Pack expansions are not transformed during normalization,
1749 which means we might have requires-expressions.
1751 FIXME: We should never have a naked pack expansion in a
1752 predicate constraint. When the fold-expression branch is
1753 integrated, this same logical will apply to that fold. */
1754 if (TREE_CODE (original) == EXPR_PACK_EXPANSION)
1755 return satisfy_pack_expansion (original, args, complain, in_decl);
1757 tree expr = tsubst_expr (original, args, complain, in_decl, false);
1758 if (expr == error_mark_node)
1759 return boolean_false_node;
1761 tree result = fold_non_dependent_expr (expr);
1762 if (result == error_mark_node)
1763 return boolean_false_node;
1765 /* A predicate constraint shall have type bool. In some
1766 cases, substitution gives us const-qualified bool, which
1767 is also acceptable. */
1768 tree type = cv_unqualified (TREE_TYPE (result));
1769 if (!same_type_p (type, boolean_type_node))
1771 error_at (EXPR_LOC_OR_LOC (t, input_location),
1772 "constraint %qE does not have type %qT",
1773 result, boolean_type_node);
1774 return boolean_false_node;
1777 tree value = cxx_constant_value (result);
1778 return value;
1781 /* Check an expression constraint. The constraint is satisfied if
1782 substitution succeeds ([temp.constr.expr]).
1784 Note that the expression is unevaluated. */
1786 tree
1787 satisfy_expression_constraint (tree t, tree args,
1788 tsubst_flags_t complain, tree in_decl)
1790 cp_unevaluated guard;
1791 deferring_access_check_sentinel deferring;
1793 tree expr = EXPR_CONSTR_EXPR (t);
1794 tree check = tsubst_expr (expr, args, complain, in_decl, false);
1795 if (check == error_mark_node)
1796 return boolean_false_node;
1797 if (!perform_deferred_access_checks (tf_none))
1798 return boolean_false_node;
1800 return boolean_true_node;
1803 /* Check a type constraint. The constraint is satisfied if
1804 substitution succeeds. */
1806 inline tree
1807 satisfy_type_constraint (tree t, tree args,
1808 tsubst_flags_t complain, tree in_decl)
1810 tree type = TYPE_CONSTR_TYPE (t);
1811 tree check = tsubst (type, args, complain, in_decl);
1812 if (check == error_mark_node)
1813 return boolean_false_node;
1815 /* Check any deferred access checks now, and if any fail,
1816 pop them so they aren't diagnosed later. */
1817 if (!perform_deferred_access_checks (tf_none))
1819 pop_deferring_access_checks ();
1820 return boolean_false_node;
1823 return boolean_true_node;
1826 /* Check an implicit conversion constraint. */
1828 tree
1829 satisfy_implicit_conversion_constraint (tree t, tree args,
1830 tsubst_flags_t complain, tree in_decl)
1832 /* Don't tsubst as if we're processing a template. If we try
1833 to we can end up generating template-like expressions
1834 (e.g., modop-exprs) that aren't properly typed. */
1835 int saved_template_decl = processing_template_decl;
1836 processing_template_decl = 0;
1837 tree expr =
1838 tsubst_expr (ICONV_CONSTR_EXPR (t), args, complain, in_decl, false);
1839 processing_template_decl = saved_template_decl;
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 == 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 type = do_auto_deduction (pattern, init, placeholder,
1874 complain, adc_requirement);
1875 if (type == error_mark_node)
1876 return boolean_false_node;
1878 return boolean_true_node;
1881 /* Check an exception constraint. An exception constraint for an
1882 expression e is satisfied when noexcept(e) is true. */
1884 tree
1885 satisfy_exception_constraint (tree t, tree args,
1886 tsubst_flags_t complain, tree in_decl)
1888 tree expr = EXCEPT_CONSTR_EXPR (t);
1889 tree check = tsubst_expr (expr, args, complain, in_decl, false);
1890 if (check == error_mark_node)
1891 return boolean_false_node;
1893 if (expr_noexcept_p (check, complain))
1894 return boolean_true_node;
1895 else
1896 return boolean_false_node;
1899 /* Check a parameterized constraint. */
1901 tree
1902 satisfy_parameterized_constraint (tree t, tree args,
1903 tsubst_flags_t complain, tree in_decl)
1905 local_specialization_stack stack;
1906 tree parms = PARM_CONSTR_PARMS (t);
1907 tree vars = tsubst_constraint_variables (parms, args, complain, in_decl);
1908 if (vars == error_mark_node)
1909 return boolean_false_node;
1910 tree constr = PARM_CONSTR_OPERAND (t);
1911 return satisfy_constraint (constr, args, complain, in_decl);
1914 /* Check that the conjunction of constraints is satisfied. Note
1915 that if left operand is not satisfied, the right operand
1916 is not checked.
1918 FIXME: Check that this wouldn't result in a user-defined
1919 operator. Note that this error is partially diagnosed in
1920 satisfy_predicate_constraint. It would be nice to diagnose
1921 the overload, but I don't think it's strictly necessary. */
1923 tree
1924 satisfy_conjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1926 tree t0 = satisfy_constraint (TREE_OPERAND (t, 0), args, complain, in_decl);
1927 if (t0 == boolean_false_node)
1928 return t0;
1929 tree t1 = satisfy_constraint (TREE_OPERAND (t, 1), args, complain, in_decl);
1930 if (t1 == boolean_false_node)
1931 return t1;
1932 return boolean_true_node;
1935 /* Check that the disjunction of constraints is satisfied. Note
1936 that if the left operand is satisfied, the right operand is not
1937 checked. */
1939 tree
1940 satisfy_disjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1942 tree t0 = satisfy_constraint (TREE_OPERAND (t, 0), args, complain, in_decl);
1943 if (t0 == boolean_true_node)
1944 return boolean_true_node;
1945 tree t1 = satisfy_constraint (TREE_OPERAND (t, 1), args, complain, in_decl);
1946 if (t1 == boolean_true_node)
1947 return boolean_true_node;
1948 return boolean_false_node;
1951 /* Check that the constraint is satisfied, according to the rules
1952 for that constraint. Note that each satisfy_* function returns
1953 true or false, depending on whether it is satisfied or not. */
1955 tree
1956 satisfy_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1958 if (!t)
1959 return boolean_false_node;
1961 if (t == error_mark_node)
1962 return boolean_false_node;
1964 switch (TREE_CODE (t))
1966 case PRED_CONSTR:
1967 return satisfy_predicate_constraint (t, args, complain, in_decl);
1969 case EXPR_CONSTR:
1970 return satisfy_expression_constraint (t, args, complain, in_decl);
1972 case TYPE_CONSTR:
1973 return satisfy_type_constraint (t, args, complain, in_decl);
1975 case ICONV_CONSTR:
1976 return satisfy_implicit_conversion_constraint (t, args, complain, in_decl);
1978 case DEDUCT_CONSTR:
1979 return satisfy_argument_deduction_constraint (t, args, complain, in_decl);
1981 case EXCEPT_CONSTR:
1982 return satisfy_exception_constraint (t, args, complain, in_decl);
1984 case PARM_CONSTR:
1985 return satisfy_parameterized_constraint (t, args, complain, in_decl);
1987 case CONJ_CONSTR:
1988 return satisfy_conjunction (t, args, complain, in_decl);
1990 case DISJ_CONSTR:
1991 return satisfy_disjunction (t, args, complain, in_decl);
1993 default:
1994 gcc_unreachable ();
1996 return boolean_false_node;
1999 /* Same as above but will not emit diagnostics. */
2001 tree
2002 satisfy_constraint (tree t, tree args)
2004 return satisfy_constraint (t, args, tf_none, NULL_TREE);
2007 /* Check the associated constraints in CI against the given
2008 ARGS, returning true when the constraints are satisfied
2009 and false otherwise. */
2011 tree
2012 satisfy_associated_constraints (tree ci, tree args)
2014 /* If there are no constraints then this is trivially satisfied. */
2015 if (!ci)
2016 return boolean_true_node;
2018 /* If any arguments depend on template parameters, we can't
2019 check constraints. */
2020 if (args && uses_template_parms (args))
2021 return boolean_true_node;
2023 /* Invalid requirements cannot be satisfied. */
2024 if (!valid_constraints_p (ci))
2025 return boolean_false_node;
2027 return satisfy_constraint (CI_NORMALIZED_CONSTRAINTS (ci), args);
2030 } /* namespace */
2032 /* Evaluate the given constraint, returning boolean_true_node
2033 if the constraint is satisfied and boolean_false_node
2034 otherwise. */
2036 tree
2037 evaluate_constraints (tree constr, tree args)
2039 gcc_assert (constraint_p (constr));
2040 return satisfy_constraint (normalize_constraint (constr), args);
2043 /* Evaluate the function concept FN by substituting ARGS into its
2044 definition and evaluating that as the result. Returns
2045 boolean_true_node if the constraints are satisfied and
2046 boolean_false_node otherwise. */
2048 tree
2049 evaluate_function_concept (tree fn, tree args)
2051 ++processing_template_decl;
2052 tree constr = transform_expression (lift_function_definition (fn, args));
2053 --processing_template_decl;
2054 return satisfy_constraint (constr, args);
2057 /* Evaluate the variable concept VAR by substituting ARGS into
2058 its initializer and checking the resulting constraint. Returns
2059 boolean_true_node if the constraints are satisfied and
2060 boolean_false_node otherwise. */
2062 tree
2063 evaluate_variable_concept (tree decl, tree args)
2065 ++processing_template_decl;
2066 tree constr = transform_expression (lift_variable_initializer (decl, args));
2067 --processing_template_decl;
2068 return satisfy_constraint (constr, args);
2071 /* Evaluate the given expression as if it were a predicate
2072 constraint. Returns boolean_true_node if the constraint
2073 is satisfied and boolean_false_node otherwise. */
2075 tree
2076 evaluate_constraint_expression (tree expr, tree args)
2078 ++processing_template_decl;
2079 tree constr = transform_expression (lift_expression (expr));
2080 --processing_template_decl;
2081 return satisfy_constraint (constr, args);
2084 /* Returns true if the DECL's constraints are satisfied.
2085 This is used in cases where a declaration is formed but
2086 before it is used (e.g., overload resolution). */
2088 bool
2089 constraints_satisfied_p (tree decl)
2091 /* Get the constraints to check for satisfaction. This depends
2092 on whether we're looking at a template specialization or not. */
2093 tree ci;
2094 tree args = NULL_TREE;
2095 if (tree ti = DECL_TEMPLATE_INFO (decl))
2097 ci = get_constraints (TI_TEMPLATE (ti));
2098 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
2100 else
2102 ci = get_constraints (decl);
2105 tree eval = satisfy_associated_constraints (ci, args);
2106 return eval == boolean_true_node;
2109 /* Returns true if the constraints are satisfied by ARGS.
2110 Here, T can be either a constraint or a constrained
2111 declaration. */
2113 bool
2114 constraints_satisfied_p (tree t, tree args)
2116 tree eval;
2117 if (constraint_p (t))
2118 eval = evaluate_constraints (t, args);
2119 else
2120 eval = satisfy_associated_constraints (get_constraints (t), args);
2121 return eval == boolean_true_node;
2124 namespace
2127 /* Normalize EXPR and determine if the resulting constraint is
2128 satisfied by ARGS. Returns true if and only if the constraint
2129 is satisfied. This is used extensively by diagnostics to
2130 determine causes for failure. */
2132 inline bool
2133 constraint_expression_satisfied_p (tree expr, tree args)
2135 return evaluate_constraint_expression (expr, args) == boolean_true_node;
2138 } /* namespace */
2141 /*---------------------------------------------------------------------------
2142 Semantic analysis of requires-expressions
2143 ---------------------------------------------------------------------------*/
2145 /* Finish a requires expression for the given PARMS (possibly
2146 null) and the non-empty sequence of requirements. */
2147 tree
2148 finish_requires_expr (tree parms, tree reqs)
2150 /* Modify the declared parameters by removing their context
2151 so they don't refer to the enclosing scope and explicitly
2152 indicating that they are constraint variables. */
2153 for (tree p = parms; p && !VOID_TYPE_P (TREE_VALUE (p)); p = TREE_CHAIN (p))
2155 tree parm = TREE_VALUE (p);
2156 DECL_CONTEXT (parm) = NULL_TREE;
2157 CONSTRAINT_VAR_P (parm) = true;
2160 /* Build the node. */
2161 tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs);
2162 TREE_SIDE_EFFECTS (r) = false;
2163 TREE_CONSTANT (r) = true;
2164 return r;
2167 /* Construct a requirement for the validity of EXPR. */
2168 tree
2169 finish_simple_requirement (tree expr)
2171 return build_nt (SIMPLE_REQ, expr);
2174 /* Construct a requirement for the validity of TYPE. */
2175 tree
2176 finish_type_requirement (tree type)
2178 return build_nt (TYPE_REQ, type);
2181 /* Construct a requirement for the validity of EXPR, along with
2182 its properties. if TYPE is non-null, then it specifies either
2183 an implicit conversion or argument deduction constraint,
2184 depending on whether any placeholders occur in the type name.
2185 NOEXCEPT_P is true iff the noexcept keyword was specified. */
2186 tree
2187 finish_compound_requirement (tree expr, tree type, bool noexcept_p)
2189 tree req = build_nt (COMPOUND_REQ, expr, type);
2190 COMPOUND_REQ_NOEXCEPT_P (req) = noexcept_p;
2191 return req;
2194 /* Finish a nested requirement. */
2195 tree
2196 finish_nested_requirement (tree expr)
2198 return build_nt (NESTED_REQ, expr);
2201 // Check that FN satisfies the structural requirements of a
2202 // function concept definition.
2203 tree
2204 check_function_concept (tree fn)
2206 // Check that the function is comprised of only a single
2207 // return statement.
2208 tree body = DECL_SAVED_TREE (fn);
2209 if (TREE_CODE (body) == BIND_EXPR)
2210 body = BIND_EXPR_BODY (body);
2212 // Sometimes a function call results in the creation of clean up
2213 // points. Allow these to be preserved in the body of the
2214 // constraint, as we might actually need them for some constexpr
2215 // evaluations.
2216 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
2217 body = TREE_OPERAND (body, 0);
2219 /* Check that the definition is written correctly. */
2220 if (TREE_CODE (body) != RETURN_EXPR)
2222 location_t loc = DECL_SOURCE_LOCATION (fn);
2223 if (TREE_CODE (body) == STATEMENT_LIST && !STATEMENT_LIST_HEAD (body))
2224 error_at (loc, "definition of concept %qD is empty", fn);
2225 else
2226 error_at (loc, "definition of concept %qD has multiple statements", fn);
2229 return NULL_TREE;
2233 // Check that a constrained friend declaration function declaration,
2234 // FN, is admissible. This is the case only when the declaration depends
2235 // on template parameters and does not declare a specialization.
2236 void
2237 check_constrained_friend (tree fn, tree reqs)
2239 if (fn == error_mark_node)
2240 return;
2241 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
2243 // If there are not constraints, this cannot be an error.
2244 if (!reqs)
2245 return;
2247 // Constrained friend functions that don't depend on template
2248 // arguments are effectively meaningless.
2249 tree parms = DECL_ARGUMENTS (fn);
2250 tree result = TREE_TYPE (TREE_TYPE (fn));
2251 if (!(parms && uses_template_parms (parms)) && !uses_template_parms (result))
2253 error ("constrained friend does not depend on template parameters");
2254 return;
2258 /*---------------------------------------------------------------------------
2259 Equivalence of constraints
2260 ---------------------------------------------------------------------------*/
2262 /* Returns true when A and B are equivalent constraints. */
2263 bool
2264 equivalent_constraints (tree a, tree b)
2266 gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2267 gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2268 return cp_tree_equal (a, b);
2271 /* Returns true if the template declarations A and B have equivalent
2272 constraints. This is the case when A's constraints subsume B's and
2273 when B's also constrain A's. */
2274 bool
2275 equivalently_constrained (tree d1, tree d2)
2277 gcc_assert (TREE_CODE (d1) == TREE_CODE (d2));
2278 return equivalent_constraints (get_constraints (d1), get_constraints (d2));
2281 /*---------------------------------------------------------------------------
2282 Partial ordering of constraints
2283 ---------------------------------------------------------------------------*/
2285 /* Returns true when the the constraints in A subsume those in B. */
2286 bool
2287 subsumes_constraints (tree a, tree b)
2289 gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2290 gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2291 return subsumes (a, b);
2294 /* Determines which of the declarations, A or B, is more constrained.
2295 That is, which declaration's constraints subsume but are not subsumed
2296 by the other's?
2298 Returns 1 if A is more constrained than B, -1 if B is more constrained
2299 than A, and 0 otherwise. */
2301 more_constrained (tree d1, tree d2)
2303 tree c1 = get_constraints (d1);
2304 tree c2 = get_constraints (d2);
2305 int winner = 0;
2306 if (subsumes_constraints (c1, c2))
2307 ++winner;
2308 if (subsumes_constraints (c2, c1))
2309 --winner;
2310 return winner;
2313 /* Returns true if D1 is at least as constrained as D2. That is, the
2314 associated constraints of D1 subsume those of D2, or both declarations
2315 are unconstrained. */
2316 bool
2317 at_least_as_constrained (tree d1, tree d2)
2319 tree c1 = get_constraints (d1);
2320 tree c2 = get_constraints (d2);
2321 return subsumes_constraints (c1, c2);
2325 /*---------------------------------------------------------------------------
2326 Constraint diagnostics
2327 ---------------------------------------------------------------------------*/
2329 /* The diagnosis of constraints performs a combination of
2330 normalization and satisfaction testing. We recursively
2331 walk through the conjunction (or disjunctions) of associated
2332 constraints, testing each sub-expression in turn.
2334 We currently restrict diagnostics to just the top-level
2335 conjunctions within the associated constraints. A fully
2336 recursive walk is possible, but it can generate a lot
2337 of errors. */
2340 namespace {
2342 void diagnose_expression (location_t, tree, tree);
2343 void diagnose_constraint (location_t, tree, tree);
2345 /* Diagnose a conjunction of constraints. */
2346 void
2347 diagnose_logical_operation (location_t loc, tree t, tree args)
2349 diagnose_expression (loc, TREE_OPERAND (t, 0), args);
2350 diagnose_expression (loc, TREE_OPERAND (t, 0), args);
2353 /* Determine if the trait expression T is satisfied by ARGS.
2354 Emit a precise diagnostic if it is not. */
2355 void
2356 diagnose_trait_expression (location_t loc, tree t, tree args)
2358 if (constraint_expression_satisfied_p (t, args))
2359 return;
2361 /* Rebuild the trait expression so we can diagnose the
2362 specific failure. */
2363 ++processing_template_decl;
2364 tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
2365 --processing_template_decl;
2367 tree t1 = TRAIT_EXPR_TYPE1 (expr);
2368 tree t2 = TRAIT_EXPR_TYPE2 (expr);
2369 switch (TRAIT_EXPR_KIND (t))
2371 case CPTK_HAS_NOTHROW_ASSIGN:
2372 inform (loc, " %qT is not nothrow copy assignable", t1);
2373 break;
2374 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
2375 inform (loc, " %qT is not nothrow default constructible", t1);
2376 break;
2377 case CPTK_HAS_NOTHROW_COPY:
2378 inform (loc, " %qT is not nothrow copy constructible", t1);
2379 break;
2380 case CPTK_HAS_TRIVIAL_ASSIGN:
2381 inform (loc, " %qT is not trivially copy assignable", t1);
2382 break;
2383 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
2384 inform (loc, " %qT is not trivially default constructible", t1);
2385 break;
2386 case CPTK_HAS_TRIVIAL_COPY:
2387 inform (loc, " %qT is not trivially copy constructible", t1);
2388 break;
2389 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
2390 inform (loc, " %qT is not trivially destructible", t1);
2391 break;
2392 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
2393 inform (loc, " %qT does not have a virtual destructor", t1);
2394 break;
2395 case CPTK_IS_ABSTRACT:
2396 inform (loc, " %qT is not an abstract class", t1);
2397 break;
2398 case CPTK_IS_BASE_OF:
2399 inform (loc, " %qT is not a base of %qT", t1, t2);
2400 break;
2401 case CPTK_IS_CLASS:
2402 inform (loc, " %qT is not a class", t1);
2403 break;
2404 case CPTK_IS_CONVERTIBLE_TO:
2405 inform (loc, " %qT is not convertible to %qT", t1, t2);
2406 break;
2407 case CPTK_IS_EMPTY:
2408 inform (loc, " %qT is not an empty class", t1);
2409 break;
2410 case CPTK_IS_ENUM:
2411 inform (loc, " %qT is not an enum", t1);
2412 break;
2413 case CPTK_IS_FINAL:
2414 inform (loc, " %qT is not a final class", t1);
2415 break;
2416 case CPTK_IS_LITERAL_TYPE:
2417 inform (loc, " %qT is not a literal type", t1);
2418 break;
2419 case CPTK_IS_POD:
2420 inform (loc, " %qT is not a POD type", t1);
2421 break;
2422 case CPTK_IS_POLYMORPHIC:
2423 inform (loc, " %qT is not a polymorphic type", t1);
2424 break;
2425 case CPTK_IS_SAME_AS:
2426 inform (loc, " %qT is not the same as %qT", t1, t2);
2427 break;
2428 case CPTK_IS_STD_LAYOUT:
2429 inform (loc, " %qT is not an standard layout type", t1);
2430 break;
2431 case CPTK_IS_TRIVIAL:
2432 inform (loc, " %qT is not a trivial type", t1);
2433 break;
2434 case CPTK_IS_UNION:
2435 inform (loc, " %qT is not a union", t1);
2436 break;
2437 default:
2438 gcc_unreachable ();
2442 /* Determine if the call expression T, when normalized as a constraint,
2443 is satisfied by ARGS.
2445 TODO: If T is refers to a concept, We could recursively analyze
2446 its definition to identify the exact failure, but that could
2447 emit a *lot* of error messages (defeating the purpose of
2448 improved diagnostics). Consider adding a flag to control the
2449 depth of diagnostics. */
2450 void
2451 diagnose_call_expression (location_t loc, tree t, tree args)
2453 if (constraint_expression_satisfied_p (t, args))
2454 return;
2456 /* Rebuild the expression for the purpose of diagnostics. */
2457 ++processing_template_decl;
2458 tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
2459 --processing_template_decl;
2461 /* If the function call is known to be a concept check, then
2462 diagnose it differently (i.e., we may recurse). */
2463 if (resolve_constraint_check (t))
2464 inform (loc, " concept %qE was not satisfied", expr);
2465 else
2466 inform (loc, " %qE evaluated to false", expr);
2469 /* Determine if the template-id T, when normalized as a constraint
2470 is satisfied by ARGS. */
2471 void
2472 diagnose_template_id (location_t loc, tree t, tree args)
2474 /* Check for invalid template-ids. */
2475 if (!variable_template_p (TREE_OPERAND (t, 0)))
2477 inform (loc, " invalid constraint %qE", t);
2478 return;
2481 if (constraint_expression_satisfied_p (t, args))
2482 return;
2484 /* Rebuild the expression for the purpose of diagnostics. */
2485 ++processing_template_decl;
2486 tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
2487 --processing_template_decl;
2489 tree var = DECL_TEMPLATE_RESULT (TREE_OPERAND (t, 0));
2490 if (DECL_DECLARED_CONCEPT_P (var))
2491 inform (loc, " concept %qE was not satisfied", expr);
2492 else
2493 inform (loc, " %qE evaluated to false", expr);
2496 /* Determine if the requires-expression, when normalized as a
2497 constraint is satisfied by ARGS.
2499 TODO: Build sets of expressions, types, and constraints
2500 based on the requirements in T and emit specific diagnostics
2501 for those. */
2502 void
2503 diagnose_requires_expression (location_t loc, tree t, tree args)
2505 if (constraint_expression_satisfied_p (t, args))
2506 return;
2507 inform (loc, "requirements not satisfied");
2510 void
2511 diagnose_pack_expansion (location_t loc, tree t, tree args)
2513 if (constraint_expression_satisfied_p (t, args))
2514 return;
2516 /* Make sure that we don't have naked packs that we don't expect. */
2517 if (!same_type_p (TREE_TYPE (t), boolean_type_node))
2519 inform (loc, "invalid pack expansion in constraint %qE", t);
2520 return;
2523 inform (loc, " in the expansion of %qE", t);
2525 /* Get the vector of expanded arguments. Note that n must not
2526 be 0 since this constraint is not satisfied. */
2527 ++processing_template_decl;
2528 tree exprs = tsubst_pack_expansion (t, args, tf_none, NULL_TREE);
2529 --processing_template_decl;
2530 if (exprs == error_mark_node)
2532 /* TODO: This error message could be better. */
2533 inform (loc, " substitution failure occurred during expansion");
2534 return;
2537 /* Check each expanded constraint separately. */
2538 int n = TREE_VEC_LENGTH (exprs);
2539 for (int i = 0; i < n; ++i)
2541 tree expr = TREE_VEC_ELT (exprs, i);
2542 if (!constraint_expression_satisfied_p (expr, args))
2543 inform (loc, " %qE was not satisfied", expr);
2547 /* Diagnose an expression that would be characterized as
2548 a predicate constraint. */
2549 void
2550 diagnose_other_expression (location_t loc, tree t, tree args)
2552 if (constraint_expression_satisfied_p (t, args))
2553 return;
2554 inform (loc, " %qE evaluated to false", t);
2557 void
2558 diagnose_expression (location_t loc, tree t, tree args)
2560 switch (TREE_CODE (t))
2562 case TRUTH_ANDIF_EXPR:
2563 diagnose_logical_operation (loc, t, args);
2564 break;
2566 case TRUTH_ORIF_EXPR:
2567 diagnose_logical_operation (loc, t, args);
2568 break;
2570 case CALL_EXPR:
2571 diagnose_call_expression (loc, t, args);
2572 break;
2574 case TEMPLATE_ID_EXPR:
2575 diagnose_template_id (loc, t, args);
2576 break;
2578 case REQUIRES_EXPR:
2579 diagnose_requires_expression (loc, t, args);
2580 break;
2582 case TRAIT_EXPR:
2583 diagnose_trait_expression (loc, t, args);
2584 break;
2586 case EXPR_PACK_EXPANSION:
2587 diagnose_pack_expansion (loc, t, args);
2588 break;
2590 default:
2591 diagnose_other_expression (loc, t, args);
2592 break;
2596 inline void
2597 diagnose_predicate_constraint (location_t loc, tree t, tree args)
2599 diagnose_expression (loc, PRED_CONSTR_EXPR (t), args);
2602 inline void
2603 diagnose_conjunction (location_t loc, tree t, tree args)
2605 diagnose_constraint (loc, TREE_OPERAND (t, 0), args);
2606 diagnose_constraint (loc, TREE_OPERAND (t, 1), args);
2609 /* Diagnose the constraint T for the given ARGS. This is only
2610 ever invoked on the associated constraints, so we can
2611 only have conjunctions of predicate constraints. */
2612 void
2613 diagnose_constraint (location_t loc, tree t, tree args)
2615 switch (TREE_CODE (t))
2617 case CONJ_CONSTR:
2618 diagnose_conjunction (loc, t, args);
2619 break;
2621 case PRED_CONSTR:
2622 diagnose_predicate_constraint (loc, t, args);
2623 break;
2625 default:
2626 gcc_unreachable ();
2627 break;
2631 /* Diagnose the reason(s) why ARGS do not satisfy the constraints
2632 of declaration DECL. */
2634 void
2635 diagnose_declaration_constraints (location_t loc, tree decl, tree args)
2637 inform (loc, " constraints not satisfied");
2639 /* Constraints are attached to the template. */
2640 if (tree ti = DECL_TEMPLATE_INFO (decl)) {
2641 decl = TI_TEMPLATE (ti);
2642 args = TI_ARGS (ti);
2645 /* Check that the constraints are actually valid. */
2646 tree ci = get_constraints (decl);
2647 if (!valid_constraints_p (ci))
2649 inform (loc, " invalid constraints");
2650 return;
2653 /* Recursively diagnose the associated constraints. */
2654 diagnose_constraint (loc, CI_ASSOCIATED_CONSTRAINTS (ci), args);
2657 } // namespace
2659 /* Emit diagnostics detailing the failure ARGS to satisfy the
2660 constraints of T. Here, T can be either a constraint
2661 or a declaration. */
2663 void
2664 diagnose_constraints (location_t loc, tree t, tree args)
2666 if (constraint_p (t))
2667 diagnose_constraint (loc, t, args);
2668 else
2669 diagnose_declaration_constraints (loc, t, args);