2015-03-20 Andrew Sutton <andrew.n.sutton@gmail.com>
[official-gcc.git] / gcc / cp / constraint.cc
blob11239720d9edcf82f6950e2d67ca1016b807354a
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 /*---------------------------------------------------------------------------
111 Resolution of qualified concept names
112 ---------------------------------------------------------------------------*/
114 /* This facility is used to resolve constraint checks from
115 requirement expressions. A constraint check is a call to
116 a function template declared with the keyword 'concept'.
118 The result of resolution is a pair (a TREE_LIST) whose value
119 is the matched declaration, and whose purpose contains the
120 coerced template arguments that can be substituted into the
121 call. */
123 // Given an overload set OVL, try to find a unique definition that can be
124 // instantiated by the template arguments ARGS.
126 // This function is not called for arbitrary call expressions. In particular,
127 // the call expression must be written with explicit template arguments
128 // and no function arguments. For example:
130 // f<T, U>()
132 // If a single match is found, this returns a TREE_LIST whose VALUE
133 // is the constraint function (not the template), and its PURPOSE is
134 // the complete set of arguments substituted into the parameter list.
135 static tree
136 resolve_constraint_check (tree ovl, tree args)
138 tree cands = NULL_TREE;
139 for (tree p = ovl; p != NULL_TREE; p = OVL_NEXT (p))
141 // Get the next template overload.
142 tree tmpl = OVL_CURRENT (p);
143 if (TREE_CODE (tmpl) != TEMPLATE_DECL)
144 continue;
146 // Don't try to deduce checks for non-concept-like. We often
147 // end up trying to resolve constraints in functional casts
148 // as part of a post-fix expression. We can save time and
149 // headaches by not instantiating those declarations.
151 // NOTE: This masks a potential error, caused by instantiating
152 // non-deduced contexts using placeholder arguments.
153 tree fn = DECL_TEMPLATE_RESULT (tmpl);
154 if (DECL_ARGUMENTS (fn))
155 continue;
156 if (!DECL_DECLARED_CONCEPT_P (fn))
157 continue;
159 // Remember the candidate if we can deduce a substitution.
160 ++processing_template_decl;
161 tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl));
162 if (tree subst = coerce_template_parms (parms, args, tmpl))
163 if (subst != error_mark_node)
164 cands = tree_cons (subst, fn, cands);
165 --processing_template_decl;
168 // If we didn't find a unique candidate, then this is
169 // not a constraint check.
170 if (!cands || TREE_CHAIN (cands))
171 return NULL_TREE;
173 // Constraints must be declared concepts.
174 tree decl = TREE_VALUE (cands);
175 if (!DECL_DECLARED_CONCEPT_P (decl))
176 return NULL_TREE;
178 return cands;
181 // Determine if the the call expression CALL is a constraint check, and
182 // return the concept declaration and arguments being checked. If CALL
183 // does not denote a constraint check, return NULL.
184 tree
185 resolve_constraint_check (tree call)
187 gcc_assert (TREE_CODE (call) == CALL_EXPR);
189 // A constraint check must be only a template-id expression. If
190 // it's a call to a base-link, its function(s) should be a
191 // template-id expression. If this is not a template-id, then it
192 // cannot be a concept-check.
193 tree target = CALL_EXPR_FN (call);
194 if (BASELINK_P (target))
195 target = BASELINK_FUNCTIONS (target);
196 if (TREE_CODE (target) != TEMPLATE_ID_EXPR)
197 return NULL_TREE;
199 // Get the overload set and template arguments and try to
200 // resolve the target.
201 tree ovl = TREE_OPERAND (target, 0);
203 /* This is a function call of a variable concept... ill-formed. */
204 if (TREE_CODE (ovl) == TEMPLATE_DECL)
206 error ("function call of variable concept %qE", call);
207 return error_mark_node;
210 tree args = TREE_OPERAND (target, 1);
211 return resolve_constraint_check (ovl, args);
214 // Given a call expression or template-id expression to a concept, EXPR,
215 // possibly including a placeholder argument, deduce the concept being checked
216 // and the prototype parameter. Returns true if the constraint and prototype
217 // can be deduced and false otherwise. Note that the CHECK and PROTO arguments
218 // are set to NULL_TREE if this returns false.
219 bool
220 deduce_constrained_parameter (tree expr, tree& check, tree& proto)
222 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
224 // Get the check and prototype parameter from the variable template.
225 tree decl = TREE_OPERAND (expr, 0);
226 tree parms = DECL_TEMPLATE_PARMS (decl);
228 check = DECL_TEMPLATE_RESULT (decl);
229 proto = TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), 0));
230 return true;
232 else if (TREE_CODE (expr) == CALL_EXPR)
234 // Resolve the constraint check to deduce the prototype parameter.
235 tree info = resolve_constraint_check (expr);
236 if (info && info != error_mark_node)
238 // Get function and argument from the resolved check expression and
239 // the prototype parameter. Note that if the first argument was a
240 // pack, we need to extract the first element ot get the prototype.
241 check = TREE_VALUE (info);
242 tree arg = TREE_VEC_ELT (TREE_PURPOSE (info), 0);
243 if (ARGUMENT_PACK_P (arg))
244 arg = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0);
245 proto = TREE_TYPE (arg);
246 return true;
248 check = proto = NULL_TREE;
249 return false;
251 else
252 gcc_unreachable ();
255 // Given a call expression or template-id expression to a concept, EXPR,
256 // deduce the concept being checked and return the template arguments.
257 // Returns NULL_TREE if deduction fails.
258 static tree
259 deduce_concept_introduction (tree expr)
261 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
263 // Get the parameters from the template expression.
264 tree decl = TREE_OPERAND (expr, 0);
265 tree args = TREE_OPERAND (expr, 1);
266 tree var = DECL_TEMPLATE_RESULT (decl);
267 tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (decl));
269 parms = coerce_template_parms (parms, args, var);
270 // Check that we are returned a proper set of arguments.
271 if (parms == error_mark_node)
272 return NULL_TREE;
273 return parms;
275 else if (TREE_CODE (expr) == CALL_EXPR)
277 // Resolve the constraint check and return arguments.
278 tree info = resolve_constraint_check (expr);
279 if (info && info != error_mark_node)
280 return TREE_PURPOSE (info);
281 return NULL_TREE;
283 else
284 gcc_unreachable ();
287 namespace {
289 /*---------------------------------------------------------------------------
290 Lifting of concept definitions
291 ---------------------------------------------------------------------------*/
293 tree lift_expression (tree);
295 /* If the tree T has operands, then lift any concepts out of them. */
296 tree
297 lift_operands (tree t)
299 if (int n = tree_operand_length (t))
301 t = copy_node (t);
302 for (int i = 0; i < n; ++i)
303 TREE_OPERAND (t, i) = lift_expression (TREE_OPERAND (t, i));
305 return t;
308 /* Recursively lift all operands of the function call. Also, check
309 that the call target is not accidentally a variable concept
310 since that's ill-formed. */
311 tree
312 lift_function_call (tree t)
314 gcc_assert (TREE_CODE (t) == CALL_EXPR);
315 tree target = CALL_EXPR_FN (t);
316 if (VAR_P (target)) {
317 error ("%qE cannot be used as a function", target);
318 return error_mark_node;
320 return lift_operands (t);
323 /* Inline a function (concept) definition by substituting
324 ARGS into its body. */
325 tree
326 lift_function_definition (tree fn, tree args)
328 /* Extract the body of the function minus the return expression. */
329 tree body = DECL_SAVED_TREE (fn);
330 if (!body)
331 return error_mark_node;
332 if (TREE_CODE (body) == BIND_EXPR)
333 body = BIND_EXPR_BODY (body);
334 if (TREE_CODE (body) != RETURN_EXPR)
335 return error_mark_node;
337 body = TREE_OPERAND (body, 0);
339 /* Substitute template arguments to produce our inline expression. */
340 tree result = tsubst_expr (body, args, tf_none, NULL_TREE, false);
341 if (result == error_mark_node)
342 return error_mark_node;
344 return lift_expression (result);
347 /* Inline a reference to a function concept. */
348 tree
349 lift_call_expression (tree t)
351 /* Try to resolve this function call as a concept. If not, then
352 it can be returned as-is. */
353 tree check = resolve_constraint_check (t);
354 if (!check)
355 return lift_function_call (t);
356 if (check == error_mark_node)
357 return error_mark_node;
359 tree fn = TREE_VALUE (check);
360 tree args = TREE_PURPOSE (check);
361 return lift_function_definition (fn, args);
364 tree
365 lift_variable_initializer (tree var, tree args)
367 /* Extract the body from the variable initializer. */
368 tree init = DECL_INITIAL (var);
369 if (!init)
370 return error_mark_node;
372 /* Substitute the arguments to form our new inline expression. */
373 tree result = tsubst_expr (init, args, tf_none, NULL_TREE, false);
374 if (result == error_mark_node)
375 return error_mark_node;
377 return lift_expression (result);
380 /* Inline a reference to a variable concept. */
381 tree
382 lift_variable_concept (tree t)
384 tree tmpl = TREE_OPERAND (t, 0);
385 tree args = TREE_OPERAND (t, 1);
386 tree decl = DECL_TEMPLATE_RESULT (tmpl);
387 gcc_assert (DECL_DECLARED_CONCEPT_P (decl));
388 return lift_variable_initializer (decl, args);
391 /* Determine if a template-id is a variable concept and inline.
393 TODO: I don't think that we get template-ids for variable
394 templates any more. They tend to be transformed into var-decl
395 specializations when an id-expression is parsed.
397 tree
398 lift_template_id (tree t)
400 if (variable_concept_p (TREE_OPERAND (t, 0)))
401 return lift_variable_concept (t);
403 /* Check that we didn't refer to a function concept like
404 a variable.
406 TODO: Add a note on how to fix this. */
407 tree tmpl = TREE_OPERAND (t, 0);
408 if (TREE_CODE (tmpl) == OVERLOAD)
410 tree fn = OVL_FUNCTION (tmpl);
411 if (TREE_CODE (fn) == TEMPLATE_DECL
412 && DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (fn)))
414 error ("invalid reference to function concept %qD", fn);
415 return error_mark_node;
419 return t;
422 /* If the variable declaration is a specialization of a concept
423 declaration, then inline it's initializer. */
424 tree
425 lift_variable (tree t)
427 if (tree ti = DECL_TEMPLATE_INFO (t))
429 tree tmpl = TI_TEMPLATE (ti);
430 tree args = TI_ARGS (ti);
431 tree decl = DECL_TEMPLATE_RESULT (tmpl);
432 if (DECL_DECLARED_CONCEPT_P (decl))
433 return lift_variable_initializer (decl, args);
435 return t;
438 /* Lift any constraints appearing in a nested requirement of
439 a requires-expression. */
440 tree
441 lift_requires_expression (tree t)
443 tree parms = TREE_OPERAND (t, 0);
444 tree reqs = TREE_OPERAND (t, 1);
445 tree result = NULL_TREE;
446 for (; reqs != NULL_TREE; reqs = TREE_CHAIN (reqs))
448 tree req = TREE_VALUE (reqs);
449 if (TREE_CODE (req) == NESTED_REQ)
451 tree expr = lift_expression (TREE_OPERAND (req, 0));
452 req = finish_nested_requirement (expr);
454 result = tree_cons (NULL_TREE, req, result);
456 return finish_requires_expr (parms, result);
459 /* Inline references to specializations of concepts. */
460 tree
461 lift_expression (tree t)
463 if (t == NULL_TREE)
464 return NULL_TREE;
466 if (t == error_mark_node)
467 return error_mark_node;
469 /* Concepts can be referred to by call or variable. All other
470 nodes are preserved. */
471 switch (TREE_CODE (t))
473 case CALL_EXPR:
474 return lift_call_expression (t);
476 case TEMPLATE_ID_EXPR:
477 return lift_template_id (t);
479 case VAR_DECL:
480 return lift_variable (t);
482 case REQUIRES_EXPR:
483 return lift_requires_expression (t);
485 case EXPR_PACK_EXPANSION:
486 /* Defer the lifting of pack expansions until constraint
487 evaluation. We know that we're not going to be doing
488 anything else with this predicate constraint until
489 we expand it. */
490 return t;
492 case TREE_LIST:
494 t = copy_node (t);
495 TREE_VALUE (t) = lift_expression (TREE_VALUE (t));
496 TREE_CHAIN (t) = lift_expression (TREE_CHAIN (t));
497 return t;
500 default:
501 return lift_operands (t);
505 /*---------------------------------------------------------------------------
506 Constraint normalization
507 ---------------------------------------------------------------------------*/
509 tree transform_expression (tree);
511 /* Check that the logical-or or logical-and expression does
512 not result in a call to a user-defined user-defined operator
513 (temp.constr.op). Returns true if the logical operator is
514 admissible and false otherwise. */
515 bool
516 check_logical_expr (tree t)
518 /* We can't do much for type dependent expressions. */
519 if (type_dependent_expression_p (t))
520 return true;
522 /* Resolve the logical operator. Note that template processing is
523 disabled so we get the actual call or target expression back.
524 not_processing_template_sentinel sentinel. */
525 tree arg1 = TREE_OPERAND (t, 0);
526 tree arg2 = TREE_OPERAND (t, 1);
527 tree ovl = NULL_TREE;
528 tree expr = build_new_op (input_location, TREE_CODE (t), LOOKUP_NORMAL,
529 arg1, arg2, /*arg3*/NULL_TREE,
530 &ovl, tf_none);
531 if (TREE_CODE (expr) != TREE_CODE (t))
533 error ("user-defined operator %qs in constraint %qE",
534 operator_name_info[TREE_CODE (t)].name, t);
535 return false;
537 return true;
540 /* Transform a logical-or or logical-and expression into either
541 a conjunction or disjunction. */
542 tree
543 xform_logical (tree t, tree_code c)
545 if (!check_logical_expr (t))
546 return error_mark_node;
547 tree t0 = transform_expression (TREE_OPERAND (t, 0));
548 tree t1 = transform_expression (TREE_OPERAND (t, 1));
549 return build_nt (c, t0, t1);
552 /* A simple requirement T introduces an expression constraint
553 for its expression. */
554 inline tree
555 xform_simple_requirement (tree t)
557 return build_nt (EXPR_CONSTR, TREE_OPERAND (t, 0));
560 /* A type requirement T introduce a type constraint for its
561 type. */
562 inline tree
563 xform_type_requirement (tree t)
565 return build_nt (TYPE_CONSTR, TREE_OPERAND (t, 0));
568 /* A compound requirement T introduces a conjunction of constraints
569 depending on its form. The conjunction always includes
570 an expression constraint for the expression of the requirement.
571 If a trailing return type was specified, the conjunction includes
572 either an implicit conversion constraint or an argument
573 deduction constraint. If the noexcept specifier is present, the
574 conjunction includes an exception constraint. */
575 tree
576 xform_compound_requirement (tree t)
578 tree expr = TREE_OPERAND (t, 0);
579 tree constr = build_nt (EXPR_CONSTR, TREE_OPERAND (t, 0));
581 /* If a type is given, append an implicit conversion or
582 argument deduction constraint.
584 FIXME: Handle argument deduction constraints. */
585 if (tree type = TREE_OPERAND (t, 1))
587 tree iconv = build_nt (ICONV_CONSTR, expr, type);
588 constr = conjoin_constraints (constr, iconv);
591 /* If noexcept is present, append an exception constraint. */
592 if (COMPOUND_REQ_NOEXCEPT_P (t))
594 tree except = build_nt (EXCEPT_CONSTR, expr);
595 constr = conjoin_constraints (constr, except);
598 return constr;
601 /* A nested requirement T introduces a conjunction of constraints
602 corresponding to its constraint-expression. */
603 inline tree
604 xform_nested_requirement (tree t)
606 return transform_expression (TREE_OPERAND (t, 0));
609 /* Transform a requirement T into one or more constraints. */
610 tree
611 xform_requirement (tree t)
613 switch (TREE_CODE (t))
615 case SIMPLE_REQ:
616 return xform_simple_requirement (t);
618 case TYPE_REQ:
619 return xform_type_requirement (t);
621 case COMPOUND_REQ:
622 return xform_compound_requirement (t);
624 case NESTED_REQ:
625 return xform_nested_requirement (t);
627 default:
628 gcc_unreachable ();
630 return error_mark_node;
633 /* Transform a sequence of requirements into a conjunction of
634 constraints. */
635 tree
636 xform_requirements (tree t)
638 tree result = NULL_TREE;
639 for (; t; t = TREE_CHAIN (t)) {
640 tree constr = xform_requirement (TREE_VALUE (t));
641 result = conjoin_constraints (result, constr);
643 return result;
646 /* Transform a requires-expression into a parameterized constraint. */
647 tree
648 xform_requires_expr (tree t)
650 tree operand = xform_requirements (TREE_OPERAND (t, 1));
651 if (tree parms = TREE_OPERAND (t, 0))
652 return build_nt (PARM_CONSTR, parms, operand);
653 else
654 return operand;
657 /* Transform an expression into an atomic predicate constraint.
658 After substitution, the expression of a predicate constraint shall
659 have type bool (temp.constr.pred). For non-type- dependent
660 expressions, we can check that now. */
661 tree
662 xform_atomic (tree t)
664 if (!type_dependent_expression_p (t))
666 tree type = cv_unqualified (TREE_TYPE (t));
667 if (!same_type_p (type, boolean_type_node))
669 error ("predicate constraint %qE does not have type %<bool%>", t);
670 return error_mark_node;
673 return build_nt (PRED_CONSTR, t);
676 /* Transform an expression into a constraint. */
677 tree
678 xform_expr (tree t)
680 switch (TREE_CODE (t))
682 case TRUTH_ANDIF_EXPR:
683 return xform_logical (t, CONJ_CONSTR);
685 case TRUTH_ORIF_EXPR:
686 return xform_logical (t, DISJ_CONSTR);
688 case REQUIRES_EXPR:
689 return xform_requires_expr (t);
691 case BIND_EXPR:
692 return transform_expression (BIND_EXPR_BODY (t));
694 default:
695 /* All other constraints are atomic. */
696 return xform_atomic (t);
700 /* Transform a statement into an expression. */
701 tree
702 xform_stmt (tree t)
704 switch (TREE_CODE (t))
706 case RETURN_EXPR:
707 return transform_expression (TREE_OPERAND (t, 0));
708 default:
709 gcc_unreachable ();
711 return error_mark_node;
714 // Reduction rules for the declaration T.
715 tree
716 xform_decl (tree t)
718 switch (TREE_CODE (t))
720 case VAR_DECL:
721 return xform_atomic (t);
722 default:
723 gcc_unreachable ();
725 return error_mark_node;
728 /* Transform an exceptional node into a constraint. */
729 tree
730 xform_misc (tree t)
732 switch (TREE_CODE (t))
734 case TRAIT_EXPR:
735 return xform_atomic (t);
736 case CONSTRUCTOR:
737 return xform_atomic (t);
738 default:
739 gcc_unreachable ();
741 return error_mark_node;
745 /* Transform a lifted expression into a constraint. This either
746 returns a constraint, or it returns error_mark_node when
747 a constraint cannot be formed. */
748 tree
749 transform_expression (tree t)
751 if (!t)
752 return NULL_TREE;
754 if (t == error_mark_node)
755 return error_mark_node;
757 switch (TREE_CODE_CLASS (TREE_CODE (t)))
759 case tcc_unary:
760 case tcc_binary:
761 case tcc_expression:
762 case tcc_vl_exp:
763 return xform_expr (t);
765 case tcc_statement:
766 return xform_stmt (t);
768 case tcc_declaration:
769 return xform_decl (t);
771 case tcc_exceptional:
772 return xform_misc (t);
774 case tcc_constant:
775 case tcc_reference:
776 case tcc_comparison:
777 /* These are atomic predicate constraints. */
778 return xform_atomic (t);
780 default:
781 /* Unhandled node kind. */
782 gcc_unreachable ();
784 return error_mark_node;
787 /*---------------------------------------------------------------------------
788 Constraint normalization
789 ---------------------------------------------------------------------------*/
791 tree normalize_constraint (tree);
793 /* The normal form of the disjunction T0 /\ T1 is the conjunction
794 of the normal form of T0 and the normal form of T1 */
795 inline tree
796 normalize_conjunction (tree t)
798 tree t0 = normalize_constraint (TREE_OPERAND (t, 0));
799 tree t1 = normalize_constraint (TREE_OPERAND (t, 1));
800 return build_nt (CONJ_CONSTR, t0, t1);
803 /* The normal form of the disjunction T0 \/ T1 is the disjunction
804 of the normal form of T0 and the normal form of T1 */
805 inline tree
806 normalize_disjunction (tree t)
808 tree t0 = normalize_constraint (TREE_OPERAND (t, 0));
809 tree t1 = normalize_constraint (TREE_OPERAND (t, 1));
810 return build_nt (DISJ_CONSTR, t0, t1);
813 /* A predicate constraint is normalized in two stages. First all
814 references specializations of concepts are replaced by their
815 substituted definitions. Then, the resulting expression is
816 transformed into a constraint by transforming && expressions
817 into conjunctions and || into disjunctions. */
818 tree
819 normalize_predicate_constraint (tree t)
821 tree expr = PRED_CONSTR_EXPR (t);
822 tree lifted = lift_expression (expr);
823 tree constr = transform_expression (lifted);
824 return constr;
827 /* The normal form of a parameterized constraint is the normal
828 form of its operand. */
829 tree
830 normalize_parameterized_constraint (tree t)
832 tree parms = PARM_CONSTR_PARMS (t);
833 tree operand = normalize_constraint (PARM_CONSTR_OPERAND (t));
834 return build_nt (PARM_CONSTR, parms, operand);
837 /* Normalize the constraint T by reducing it so that it is
838 comprised of only conjunctions and disjunctions of atomic
839 constraints. */
840 tree
841 normalize_constraint (tree t)
843 if (!t)
844 return NULL_TREE;
846 switch (TREE_CODE (t))
848 case CONJ_CONSTR:
849 return normalize_conjunction (t);
851 case DISJ_CONSTR:
852 return normalize_disjunction (t);
854 case PRED_CONSTR:
855 return normalize_predicate_constraint (t);
857 case PARM_CONSTR:
858 return normalize_parameterized_constraint (t);
860 case EXPR_CONSTR:
861 case TYPE_CONSTR:
862 case ICONV_CONSTR:
863 case DEDUCT_CONSTR:
864 case EXCEPT_CONSTR:
865 /* These constraints are defined to be atomic. */
866 return t;
868 default:
869 /* CONSTR was not a constraint. */
870 gcc_unreachable();
872 return error_mark_node;
875 } /* namespace */
878 // -------------------------------------------------------------------------- //
879 // Constraint Semantic Processing
881 // The following functions are called by the parser and substitution rules
882 // to create and evaluate constraint-related nodes.
885 // If the recently parsed TYPE declares or defines a template or template
886 // specialization, get its corresponding constraints from the current
887 // template parameters and bind them to TYPE's declaration.
888 tree
889 associate_classtype_constraints (tree type)
891 if (!type || type == error_mark_node || TREE_CODE (type) != RECORD_TYPE)
892 return type;
894 // An explicit class template specialization has no template
895 // parameters.
896 if (!current_template_parms)
897 return type;
899 if (CLASSTYPE_IS_TEMPLATE (type) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
901 tree decl = TYPE_STUB_DECL (type);
902 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
903 tree ci = build_constraints (reqs, NULL_TREE);
905 // An implicitly instantiated member template declaration already
906 // has associated constraints. If it is defined outside of its
907 // class, then we need match these constraints against those of
908 // original declaration.
909 if (tree orig_ci = get_constraints (decl))
911 if (!equivalent_constraints (ci, orig_ci))
913 // FIXME: Improve diagnostics.
914 error ("%qT does not match any declaration", type);
915 return error_mark_node;
917 return type;
919 set_constraints (decl, ci);
921 return type;
924 namespace {
926 // Create an empty constraint info block.
927 inline tree_constraint_info*
928 build_constraint_info ()
930 return (tree_constraint_info *)make_node (CONSTRAINT_INFO);
933 } // namespace
935 /* Build a constraint-info object that contains the
936 associated condstraints of a declaration. This also
937 includes the declaration's template requirements (TREQS)
938 and any trailing requirements for a function declarator
939 (DREQS). Note that both TREQS and DREQS must be constraints.
941 If the declaration has neither template nor declaration
942 requirements this returns NULL_TREE, indicating an
943 unconstrained declaration. */
944 tree
945 build_constraints (tree tmpl_reqs, tree decl_reqs)
947 gcc_assert (tmpl_reqs ? constraint_p (tmpl_reqs) : true);
948 gcc_assert (decl_reqs ? constraint_p (decl_reqs) : true);
950 if (!tmpl_reqs && !decl_reqs)
951 return NULL_TREE;
953 tree_constraint_info* ci = build_constraint_info ();
954 ci->template_reqs = tmpl_reqs;
955 ci->declarator_reqs = decl_reqs;
956 ci->associated_constr = conjoin_constraints (tmpl_reqs, decl_reqs);
958 ++processing_template_decl;
959 ci->normalized_constr = normalize_constraint (ci->associated_constr);
960 --processing_template_decl;
962 ci->assumptions = decompose_assumptions (ci->normalized_constr);
963 return (tree)ci;
966 namespace {
967 // Build a new call expression, but don't actually generate a new
968 // function call. We just want the tree, not the semantics.
969 inline tree
970 build_call_check (tree id)
972 ++processing_template_decl;
973 vec<tree, va_gc> *fargs = make_tree_vector();
974 tree call = finish_call_expr (id, &fargs, false, false, tf_none);
975 --processing_template_decl;
976 return call;
978 } // namespace
980 // Construct a concept check for the given TARGET. The target may be
981 // an overload set or a baselink referring to an overload set. Template
982 // arguments to the target are given by ARG and REST. If the target is
983 // a function (overload set or baselink referring to an overload set),
984 // then this builds the call expression TARGET<ARG, REST>(). If REST is
985 // NULL_TREE, then the resulting check is just TARGET<ARG>(). If ARG is
986 // NULL_TREE, then the resulting check is TARGET<REST>().
987 tree
988 build_concept_check (tree target, tree arg, tree rest)
990 gcc_assert (rest ? TREE_CODE (rest) == TREE_VEC : true);
992 // Build a template-id that acts as the call target using TARGET as
993 // the template and ARG as the only explicit argument.
994 int n = rest ? TREE_VEC_LENGTH (rest) : 0;
995 tree targs;
996 if (arg)
998 targs = make_tree_vec (n + 1);
999 TREE_VEC_ELT (targs, 0) = arg;
1000 if (rest)
1001 for (int i = 0; i < n; ++i)
1002 TREE_VEC_ELT (targs, i + 1) = TREE_VEC_ELT (rest, i);
1003 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, n + 1);
1005 else
1007 gcc_assert (rest != NULL_TREE);
1008 targs = rest;
1011 if (variable_template_p (target))
1012 return lookup_template_variable (target, targs);
1013 else
1014 return build_call_check (lookup_template_function (target, targs));
1017 // Returns a TYPE_DECL that contains sufficient information to build
1018 // a template parameter of the same kind as PROTO and constrained
1019 // by the concept declaration FN. PROTO is saved as the initializer of
1020 // the new type decl, and the constraining function is saved in
1021 // DECL_SIZE_UNIT.
1023 // If specified, ARGS provides additional arguments to the constraint
1024 // check. These are stored in the DECL_SIZE field.
1025 tree
1026 build_constrained_parameter (tree fn, tree proto, tree args)
1028 tree name = DECL_NAME (fn);
1029 tree type = TREE_TYPE (proto);
1030 tree decl = build_decl (input_location, TYPE_DECL, name, type);
1031 DECL_INITIAL (decl) = proto; // Describing parameter
1032 DECL_SIZE_UNIT (decl) = fn; // Constraining function declaration
1033 DECL_SIZE (decl) = args; // Extra template arguments.
1034 return decl;
1037 /* Create a constraint expression for the given DECL that
1038 evaluates the requirements specified by CONSTR, a TYPE_DECL
1039 that contains all the information necessary to build the
1040 requirements (see finish_concept_name for the layout of
1041 that TYPE_DECL).
1043 Note that the constraints are neither reduced nor decomposed.
1044 That is done only after the requires clause has been parsed
1045 (or not). */
1046 tree
1047 finish_shorthand_constraint (tree decl, tree constr)
1049 // No requirements means no constraints.
1050 if (!constr)
1051 return NULL_TREE;
1053 tree proto = DECL_INITIAL (constr); // The prototype declaration
1054 tree con = DECL_SIZE_UNIT (constr); // The concept declaration
1055 tree args = DECL_SIZE (constr); // Extra template arguments
1057 // If the parameter declaration is variadic, but the concept is not
1058 // then we need to apply the concept to every element in the pack.
1059 bool is_proto_pack = template_parameter_pack_p (proto);
1060 bool is_decl_pack = template_parameter_pack_p (decl);
1061 bool apply_to_all_p = is_decl_pack && !is_proto_pack;
1063 // Get the argument and overload used for the requirement. Adjust
1064 // if we're going to expand later.
1065 tree arg = template_parm_to_arg (build_tree_list (NULL_TREE, decl));
1066 if (apply_to_all_p)
1067 arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
1069 // Build the concept check. If it the constraint needs to be applied
1070 // to all elements of the parameter pack, then make the constraint
1071 // an expansion.
1072 tree check;
1073 if (TREE_CODE (con) == VAR_DECL)
1075 check = build_concept_check (DECL_TI_TEMPLATE (con), arg, args);
1077 else
1079 tree ovl = build_overload (DECL_TI_TEMPLATE (con), NULL_TREE);
1080 check = build_concept_check (ovl, arg, args);
1082 if (apply_to_all_p)
1084 check = make_pack_expansion (check);
1086 // Set the type to indicate that this expansion will get special
1087 // treatment during instantiation.
1089 // TODO: Maybe this should be a different kind of node... one that
1090 // has all the same properties as a pack expansion, but has a definite
1091 // expansion when instantiated as part of an expression.
1093 // As of now, this is a hack.
1094 TREE_TYPE (check) = boolean_type_node;
1097 return make_predicate_constraint (check);
1100 /* Returns a conjunction of shorthand requirements for the template
1101 parameter list PARMS. Note that the requirements are stored in
1102 the TYPE of each tree node. */
1103 tree
1104 get_shorthand_constraints (tree parms)
1106 tree result = NULL_TREE;
1107 parms = INNERMOST_TEMPLATE_PARMS (parms);
1108 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
1110 tree parm = TREE_VEC_ELT (parms, i);
1111 tree constr = TEMPLATE_PARM_CONSTRAINTS (parm);
1112 result = conjoin_constraints (result, constr);
1114 return result;
1117 // Returns and chains a new parameter for PARAMETER_LIST which will conform
1118 // to the prototype given by SRC_PARM. The new parameter will have its
1119 // identifier and location set according to IDENT and PARM_LOC respectively.
1120 static tree
1121 process_introduction_parm (tree parameter_list, tree src_parm)
1123 // If we have a pack, we should have a single pack argument which is the
1124 // placeholder we want to look at.
1125 bool is_parameter_pack = ARGUMENT_PACK_P (src_parm);
1126 if (is_parameter_pack)
1127 src_parm = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (src_parm), 0);
1129 // At this point we should have a INTRODUCED_PARM_DECL, but we want to grab
1130 // the associated decl from it. Also grab the stored identifier and location
1131 // that should be chained to it in a PARM_DECL.
1132 gcc_assert (TREE_CODE (src_parm) == INTRODUCED_PARM_DECL);
1134 tree ident = DECL_NAME (src_parm);
1135 location_t parm_loc = DECL_SOURCE_LOCATION (src_parm);
1137 // If we expect a pack and the deduced template is not a pack, or if the
1138 // template is using a pack and we didn't declare a pack, throw an error.
1139 if (is_parameter_pack != INTRODUCED_PACK_P (src_parm))
1141 error_at (parm_loc, "cannot match pack for introduced parameter");
1142 tree err_parm = build_tree_list (error_mark_node, error_mark_node);
1143 return chainon (parameter_list, err_parm);
1146 src_parm = TREE_TYPE (src_parm);
1148 tree parm;
1149 bool is_non_type;
1150 if (TREE_CODE (src_parm) == TYPE_DECL)
1152 is_non_type = false;
1153 parm = finish_template_type_parm (class_type_node, ident);
1155 else if (TREE_CODE (src_parm) == TEMPLATE_DECL)
1157 is_non_type = false;
1158 current_template_parms = DECL_TEMPLATE_PARMS (src_parm);
1159 parm = finish_template_template_parm (class_type_node, ident);
1161 else
1163 is_non_type = true;
1165 // Since we don't have a declarator, so we can copy the source
1166 // parameter and change the name and eventually the location.
1167 parm = copy_decl (src_parm);
1168 DECL_NAME (parm) = ident;
1171 // Wrap in a TREE_LIST for process_template_parm. Introductions do not
1172 // retain the defaults from the source template.
1173 parm = build_tree_list (NULL_TREE, parm);
1175 return process_template_parm (parameter_list, parm_loc, parm,
1176 is_non_type, is_parameter_pack);
1179 /* Associates a constraint check to the current template based
1180 on the introduction parameters. INTRO_LIST should be a TREE_VEC
1181 of INTRODUCED_PARM_DECLs containing a chained PARM_DECL which
1182 contains the identifier as well as the source location.
1183 TMPL_DECL is the decl for the concept being used. If we take
1184 some concept, C, this will form a check in the form of
1185 C<INTRO_LIST> filling in any extra arguments needed by the
1186 defaults deduced.
1188 Returns NULL_TREE if no concept could be matched and
1189 error_mark_node if an error occurred when matching.
1191 tree
1192 finish_template_introduction (tree tmpl_decl, tree intro_list)
1194 /* Deduce the concept check. */
1195 tree expr = build_concept_check (tmpl_decl, NULL_TREE, intro_list);
1196 if (expr == error_mark_node)
1197 return NULL_TREE;
1199 tree parms = deduce_concept_introduction (expr);
1200 if (!parms)
1201 return NULL_TREE;
1203 /* Build template parameter scope for introduction. */
1204 tree parm_list = NULL_TREE;
1205 begin_template_parm_list ();
1206 int nargs = MIN (TREE_VEC_LENGTH (parms), TREE_VEC_LENGTH (intro_list));
1207 for (int n = 0; n < nargs; ++n)
1208 parm_list = process_introduction_parm (parm_list, TREE_VEC_ELT (parms, n));
1209 parm_list = end_template_parm_list (parm_list);
1210 for (int i = 0; i < TREE_VEC_LENGTH (parm_list); ++i)
1211 if (TREE_VALUE (TREE_VEC_ELT (parm_list, i)) == error_mark_node)
1213 end_template_decl ();
1214 return error_mark_node;
1217 /* Build a concept check for our constraint. */
1218 tree check_args = make_tree_vec (TREE_VEC_LENGTH (parms));
1219 int n = 0;
1220 for (; n < TREE_VEC_LENGTH (parm_list); ++n)
1222 tree parm = TREE_VEC_ELT (parm_list, n);
1223 TREE_VEC_ELT (check_args, n) = template_parm_to_arg (parm);
1226 /* If the template expects more parameters we should be able
1227 to use the defaults from our deduced concept. */
1228 for (; n < TREE_VEC_LENGTH (parms); ++n)
1229 TREE_VEC_ELT (check_args, n) = TREE_VEC_ELT (parms, n);
1231 /* Associate the constraint. */
1232 tree check = build_concept_check (tmpl_decl, NULL_TREE, check_args);
1233 tree constr = make_predicate_constraint (check);
1234 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = constr;
1236 return parm_list;
1239 /*---------------------------------------------------------------------------
1240 Constraint substitution
1241 ---------------------------------------------------------------------------*/
1243 tree tsubst_constraint (tree, tree, tsubst_flags_t, tree);
1245 /* The following functions implement substitution rules for constraints.
1246 Substitution without checking constraints happens only in the
1247 instantiation of class templates. For example:
1249 template<C1 T> struct S {
1250 void f(T) requires C2<T>;
1251 void g(T) requires T::value;
1254 S<int> s; // error instantiating S<int>::g(T)
1256 When we instantiate S, we substitute into its member declarations,
1257 including their constraints. However, those constraints are not
1258 checked. Substituting int into C2<T> yields C2<int>, and substituting
1259 into T::value yields a substitution failure, making the program
1260 ill-formed.
1262 Note that we only ever substitute into the associated constraints
1263 of a declaration. That is, substitute is defined only for predicate
1264 constraints and conjunctions. */
1266 /* Substitute into the predicate constraints. Returns error_mark_node
1267 if the substitution into the expression fails. */
1268 tree
1269 tsubst_predicate_constraint (tree t, tree args,
1270 tsubst_flags_t complain, tree in_decl)
1272 tree expr = PRED_CONSTR_EXPR (t);
1273 ++processing_template_decl;
1274 tree result = tsubst_expr (expr, args, complain, in_decl, false);
1275 --processing_template_decl;
1276 return build_nt (PRED_CONSTR, result);
1279 /* Substitute into the conjunction of constraints. Returns
1280 error_mark_node if substitution into either operand fails. */
1281 tree
1282 tsubst_conjunction (tree t, tree args,
1283 tsubst_flags_t complain, tree in_decl)
1285 tree t0 = TREE_OPERAND (t, 0);
1286 tree r0 = tsubst_constraint (t0, args, complain, in_decl);
1287 tree t1 = TREE_OPERAND (t, 1);
1288 tree r1 = tsubst_constraint (t1, args, complain, in_decl);
1289 return build_nt (CONJ_CONSTR, r0, r1);
1292 /* Substitute ARGS into the constraint T. */
1293 tree
1294 tsubst_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1296 if (TREE_CODE (t) == CONJ_CONSTR)
1297 return tsubst_conjunction (t, args, complain, in_decl);
1298 else if (TREE_CODE (t) == PRED_CONSTR)
1299 return tsubst_predicate_constraint (t, args, complain, in_decl);
1300 else
1301 gcc_unreachable ();
1302 return error_mark_node;
1305 namespace {
1307 /* A subroutine of tsubst_constraint_variables. In an unevaluated
1308 context, the substitution of PARM_DECLs are not properly chained
1309 during substitution. Do that here. */
1310 tree
1311 fixup_constraint_vars (tree parms)
1313 if (!parms)
1314 return parms;
1316 tree p = TREE_CHAIN (parms);
1317 tree q = parms;
1318 while (p && TREE_VALUE (p) != void_type_node)
1320 DECL_CHAIN (TREE_VALUE (q)) = TREE_VALUE (p);
1321 q = p;
1322 p = TREE_CHAIN (p);
1324 return parms;
1327 /* A subroutine of tsubst_constraint_variables. Register local
1328 specializations for each of parameter in PARMS and its
1329 corresponding substituted constraint variable in VARS.
1330 Returns VARS. */
1331 tree
1332 declare_constraint_vars (tree parms, tree vars)
1334 tree s = TREE_VALUE (vars);
1335 for (tree p = parms; p && !VOID_TYPE_P (TREE_VALUE (p)); p = TREE_CHAIN (p))
1337 tree t = TREE_VALUE (p);
1338 if (DECL_PACK_P (t))
1340 tree pack = extract_fnparm_pack (t, &s);
1341 register_local_specialization (pack, t);
1343 else
1345 register_local_specialization (s, t);
1346 s = TREE_CHAIN (s);
1349 return vars;
1352 /* A subroutine of tsubst_parameterized_constraint. Substitute ARGS
1353 into the parameter list T, producing a sequence of constraint
1354 variables, declared in the current scope.
1356 Note that the caller must establish a local specialization stack
1357 prior to calling this function since this substitution will
1358 declare the substituted parameters. */
1359 tree
1360 tsubst_constraint_variables (tree t, tree args,
1361 tsubst_flags_t complain, tree in_decl)
1363 tree vars = tsubst (t, args, complain, in_decl);
1364 if (vars == error_mark_node)
1365 return error_mark_node;
1366 return declare_constraint_vars (t, fixup_constraint_vars (vars));
1369 /* Substitute ARGS into the simple requirement T. Note that
1370 substitution may result in an ill-formed expression without
1371 causing the program to be ill-formed. In such cases, the
1372 requirement wraps an error_mark_node. */
1373 inline tree
1374 tsubst_simple_requirement (tree t, tree args,
1375 tsubst_flags_t complain, tree in_decl)
1377 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1378 return finish_simple_requirement (expr);
1381 /* Substitute ARGS into the type requirement T. Note that
1382 substitution may result in an ill-formed type without
1383 causing the program to be ill-formed. In such cases, the
1384 requirement wraps an error_mark_node. */
1385 inline tree
1386 tsubst_type_requirement (tree t, tree args,
1387 tsubst_flags_t complain, tree in_decl)
1389 tree type = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
1390 return finish_type_requirement (type);
1393 /* Substitute args into the compound requirement T. If substituting
1394 into either the expression or the type fails, the corresponding
1395 operands in the resulting node will be error_mark_node. This
1396 preserves a requirement for the purpose of partial ordering, but
1397 it will never be satisfied. */
1398 tree
1399 tsubst_compound_requirement (tree t, tree args,
1400 tsubst_flags_t complain, tree in_decl)
1402 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1403 tree type = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
1404 bool noexcept_p = COMPOUND_REQ_NOEXCEPT_P (t);
1405 return finish_compound_requirement (expr, type, noexcept_p);
1408 /* Substitute ARGS into the nested requirement T. */
1409 tree
1410 tsubst_nested_requirement (tree t, tree args,
1411 tsubst_flags_t complain, tree in_decl)
1413 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1414 return finish_nested_requirement (expr);
1417 inline tree
1418 tsubst_requirement (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1420 switch (TREE_CODE (t))
1422 case SIMPLE_REQ:
1423 return tsubst_simple_requirement (t, args, complain, in_decl);
1424 case TYPE_REQ:
1425 return tsubst_type_requirement (t, args, complain, in_decl);
1426 case COMPOUND_REQ:
1427 return tsubst_compound_requirement (t, args, complain, in_decl);
1428 case NESTED_REQ:
1429 return tsubst_nested_requirement (t, args, complain, in_decl);
1430 default:
1431 gcc_unreachable ();
1433 return error_mark_node;
1436 /* Substitute ARGS into the list of requirements T. Note that
1437 substitution failures here result in ill-formed programs. */
1438 tree
1439 tsubst_requirement_body (tree t, tree args,
1440 tsubst_flags_t complain, tree in_decl)
1442 tree r = NULL_TREE;
1443 while (t)
1445 tree e = tsubst_requirement (TREE_VALUE (t), args, complain, in_decl);
1446 if (e == error_mark_node)
1447 return error_mark_node;
1448 r = tree_cons (NULL_TREE, e, r);
1449 t = TREE_CHAIN (t);
1451 return r;
1454 } /* namespace */
1456 /* Substitute ARGS into the requires expression T. Note that this
1457 results in the re-declaration of local parameters when
1458 substituting through the parameter list. If either substitution
1459 fails, the program is ill-formed. */
1460 tree
1461 tsubst_requires_expr (tree t, tree args,
1462 tsubst_flags_t complain, tree in_decl)
1464 local_specialization_stack stack;
1466 tree parms = TREE_OPERAND (t, 0);
1467 if (parms)
1469 parms = tsubst_constraint_variables (parms, args, complain, in_decl);
1470 if (parms == error_mark_node)
1471 return error_mark_node;
1474 tree reqs = TREE_OPERAND (t, 1);
1475 reqs = tsubst_requirement_body (reqs, args, complain, in_decl);
1476 if (reqs == error_mark_node)
1477 return error_mark_node;
1479 return finish_requires_expr (parms, reqs);
1482 /* Substitute ARGS into the constraint information CI, producing a new
1483 constraint record. */
1484 tree
1485 tsubst_constraint_info (tree t, tree args,
1486 tsubst_flags_t complain, tree in_decl)
1488 if (!t || t == error_mark_node || !check_constraint_info (t))
1489 return NULL_TREE;
1491 tree tmpl_constr = NULL_TREE;
1492 if (tree r = CI_TEMPLATE_REQS (t))
1493 tmpl_constr = tsubst_constraint (r, args, complain, in_decl);
1495 tree decl_constr = NULL_TREE;
1496 if (tree r = CI_DECLARATOR_REQS (t))
1497 decl_constr = tsubst_constraint (r, args, complain, in_decl);
1499 return build_constraints (tmpl_constr, decl_constr);
1503 /*---------------------------------------------------------------------------
1504 Constraint satisfaction
1505 ---------------------------------------------------------------------------*/
1507 /* The following functions determine if a constraint, when
1508 substituting template arguments, is satisfied. For convenience,
1509 satisfaction reduces a constraint to either true or false (and
1510 nothing else). */
1512 namespace {
1514 tree check_constraint (tree, tree, tsubst_flags_t, tree);
1516 /* Check the pack expansion by first transforming it into a
1517 conjunction of constraints. */
1518 tree
1519 check_pack_expansion (tree t, tree args,
1520 tsubst_flags_t complain, tree in_decl)
1522 /* Check that somebody isn't arbitrarily expanding packs
1523 as part of a predicate. Note that packs are normally
1524 untyped, however, we use the type field as a hack to
1525 indicate folded boolean expressions generated from a
1526 shorthand constraint. This check should disappear with
1527 fold expressions. */
1528 if (!same_type_p (TREE_TYPE (t), boolean_type_node))
1530 error ("invalid pack expansion in constraint %qE", t);
1531 return boolean_false_node;
1534 /* Get the vector of expanded arguments. */
1535 tree exprs = tsubst_pack_expansion (t, args, complain, in_decl);
1536 if (exprs == error_mark_node)
1537 return boolean_false_node;
1538 int n = TREE_VEC_LENGTH (exprs);
1540 /* An empty expansion is inherently satisfied. */
1541 if (n == 0)
1542 return boolean_true_node;
1544 /* Build a conjunction of constraints from the resulting
1545 expansions and then check that. */
1546 tree result = NULL_TREE;
1547 for (int i = 0; i < n; ++i)
1549 tree expr = TREE_VEC_ELT (exprs, i);
1550 tree constr = transform_expression (expr);
1551 result = conjoin_constraints (result, constr);
1553 return check_constraint (result, args, complain, in_decl);
1556 /* A predicate constraint is satisfied if its expression evaluates
1557 to true. If substitution into that node fails, the constraint
1558 is not satisfied ([temp.constr.pred]).
1560 Note that a predicate constraint is a constraint expression
1561 of type bool. If neither of those are true, the program is
1562 ill-formed; they are not SFINAE'able errors. */
1563 tree
1564 check_predicate_constraint (tree t, tree args,
1565 tsubst_flags_t complain, tree in_decl)
1567 tree original = TREE_OPERAND (t, 0);
1569 /* Pack expansions are not transformed during normalization,
1570 which means we might have requires-expressions.
1572 FIXME: We should never have a naked pack expansion in a
1573 predicate constraint. When the fold-expression branch is
1574 integrated, this same logical will apply to that fold. */
1575 if (TREE_CODE (original) == EXPR_PACK_EXPANSION)
1576 return check_pack_expansion (original, args, complain, in_decl);
1578 tree expr = tsubst_expr (original, args, complain, in_decl, false);
1579 if (expr == error_mark_node)
1580 return boolean_false_node;
1582 tree result = fold_non_dependent_expr (expr);
1583 if (result == error_mark_node)
1584 return boolean_false_node;
1586 /* A predicate constraint shall have type bool. In some
1587 cases, substitution gives us const-qualified bool, which
1588 is also acceptable. */
1589 tree type = cv_unqualified (TREE_TYPE (result));
1590 if (!same_type_p (type, boolean_type_node))
1592 error_at (EXPR_LOC_OR_LOC (t, input_location),
1593 "constraint %qE does not have type %qT",
1594 result, boolean_type_node);
1595 return boolean_false_node;
1598 return cxx_constant_value (result);
1601 /* Check an expression constraint. The constraint is satisfied if
1602 substitution succeeds ([temp.constr.expr]).
1604 Note that the expression is unevaluated. */
1605 tree
1606 check_expression_constraint (tree t, tree args,
1607 tsubst_flags_t complain, tree in_decl)
1609 cp_unevaluated guard;
1610 tree expr = EXPR_CONSTR_EXPR (t);
1611 tree check = tsubst_expr (expr, args, complain, in_decl, false);
1612 if (check == error_mark_node)
1613 return boolean_false_node;
1614 return boolean_true_node;
1617 /* Check a type constraint. The constraint is satisfied if
1618 substitution succeeds. */
1619 inline tree
1620 check_type_constraint (tree t, tree args,
1621 tsubst_flags_t complain, tree in_decl)
1623 tree type = TYPE_CONSTR_TYPE (t);
1624 tree check = tsubst (type, args, complain, in_decl);
1625 if (check == error_mark_node)
1626 return boolean_false_node;
1627 return boolean_true_node;
1630 /* Check an implicit conversion constraint. */
1631 tree
1632 check_implicit_conversion_constraint (tree t, tree args,
1633 tsubst_flags_t complain, tree in_decl)
1635 tree expr = ICONV_CONSTR_EXPR (t);
1637 /* Don't tsubst as if we're processing a template. If we try
1638 to we can end up generating template-like expressions
1639 (e.g., modop-exprs) that aren't properly typed. */
1640 int saved_template_decl = processing_template_decl;
1641 processing_template_decl = 0;
1642 tree arg = tsubst_expr (expr, args, complain, in_decl, false);
1643 processing_template_decl = saved_template_decl;
1645 if (arg == error_mark_node)
1646 return boolean_false_node;
1647 tree from = TREE_TYPE (arg);
1649 tree type = ICONV_CONSTR_TYPE (t);
1650 tree to = tsubst (type, args, complain, in_decl);
1651 if (to == error_mark_node)
1652 return boolean_false_node;
1654 if (can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain))
1655 return boolean_true_node;
1656 else
1657 return boolean_false_node;
1660 /* Check an argument deduction constraint.
1662 TODO: Implement me. We need generalized auto for this to work. */
1663 tree
1664 check_argument_deduction_constraint (tree /*t*/, tree /*args*/,
1665 tsubst_flags_t /*complain*/,
1666 tree /*in_decl*/)
1668 gcc_unreachable ();
1669 return boolean_false_node;
1672 /* Check an exception constraint. An exception constraint for an
1673 expression e is satisfied when noexcept(e) is true. */
1674 tree
1675 check_exception_constraint (tree t, tree args,
1676 tsubst_flags_t complain, tree in_decl)
1678 tree expr = EXCEPT_CONSTR_EXPR (t);
1679 tree check = tsubst_expr (expr, args, complain, in_decl, false);
1680 if (check == error_mark_node)
1681 return boolean_false_node;
1683 if (expr_noexcept_p (check, complain))
1684 return boolean_true_node;
1685 else
1686 return boolean_false_node;
1689 /* Check a parameterized constraint. */
1690 tree
1691 check_parameterized_constraint (tree t, tree args,
1692 tsubst_flags_t complain, tree in_decl)
1694 local_specialization_stack stack;
1695 tree parms = PARM_CONSTR_PARMS (t);
1696 tree vars = tsubst_constraint_variables (parms, args, complain, in_decl);
1697 if (vars == error_mark_node)
1698 return boolean_false_node;
1699 tree constr = PARM_CONSTR_OPERAND (t);
1700 return check_constraint (constr, args, complain, in_decl);
1703 /* Check that the conjunction of constraints is satisfied. Note
1704 that if left operand is not satisfied, the right operand
1705 is not checked.
1707 FIXME: Check that this wouldn't result in a user-defined
1708 operator. Note that this error is partially diagnosed in
1709 check_predicate_constriant. It would be nice to diagnose
1710 the overload, but I don't think it's strictly necessary. */
1711 tree
1712 check_conjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1714 tree t0 = check_constraint (TREE_OPERAND (t, 0), args, complain, in_decl);
1715 if (t0 == boolean_false_node)
1716 return t0;
1717 tree t1 = check_constraint (TREE_OPERAND (t, 1), args, complain, in_decl);
1718 if (t1 == boolean_false_node)
1719 return t1;
1720 return boolean_true_node;
1723 /* Check that the disjunction of constraints is satisfied. Note
1724 that if the left operand is satisfied, the right operand is not
1725 checked. */
1726 tree
1727 check_disjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1729 tree t0 = check_constraint (TREE_OPERAND (t, 0), args, complain, in_decl);
1730 if (t0 == boolean_true_node)
1731 return t0;
1732 tree t1 = check_constraint (TREE_OPERAND (t, 1), args, complain, in_decl);
1733 if (t1 == boolean_true_node)
1734 return t0;
1735 return boolean_false_node;
1738 /* Check that the constraint is satisfied, according to the rules
1739 for that constraint. Note that each check_* function returns
1740 true or false, depending on whether it is satisfied or not. */
1741 tree
1742 check_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1744 if (!t)
1745 return boolean_false_node;
1747 if (t == error_mark_node)
1748 return boolean_false_node;
1750 switch (TREE_CODE (t))
1752 case PRED_CONSTR:
1753 return check_predicate_constraint (t, args, complain, in_decl);
1755 case EXPR_CONSTR:
1756 return check_expression_constraint (t, args, complain, in_decl);
1758 case TYPE_CONSTR:
1759 return check_type_constraint (t, args, complain, in_decl);
1761 case ICONV_CONSTR:
1762 return check_implicit_conversion_constraint (t, args, complain, in_decl);
1764 case DEDUCT_CONSTR:
1765 return check_argument_deduction_constraint (t, args, complain, in_decl);
1767 case EXCEPT_CONSTR:
1768 return check_exception_constraint (t, args, complain, in_decl);
1770 case PARM_CONSTR:
1771 return check_parameterized_constraint (t, args, complain, in_decl);
1773 case CONJ_CONSTR:
1774 return check_conjunction (t, args, complain, in_decl);
1776 case DISJ_CONSTR:
1777 return check_disjunction (t, args, complain, in_decl);
1779 default:
1780 gcc_unreachable ();
1782 return boolean_false_node;
1785 } /* namespace */
1787 /* Check the constraints in CI against the given ARGS, returning
1788 true when the constraints are satisfied and false otherwise.
1790 Note that this is the only place that we instantiate the
1791 constraints. */
1792 bool
1793 check_constraints (tree ci, tree args)
1795 /* If there are no constraints then this is trivially satisfied. */
1796 if (!ci)
1797 return true;
1799 /* If any arguments depend on template parameters, we can't
1800 check constraints. */
1801 if (args && uses_template_parms (args))
1802 return true;
1804 /* Invalid requirements cannot be satisfied. */
1805 if (!valid_constraints_p (ci))
1806 return false;
1808 tree constr = CI_NORMALIZED_CONSTRAINTS (ci);
1809 tree result = check_constraint (constr, args, tf_none, NULL_TREE);
1810 return result == boolean_true_node;
1813 /* Returns true if the declaration's constraints are satisfied.
1814 This is used in cases where a declaration is formed but
1815 before it is used (e.g., overload resolution). */
1816 bool
1817 constraints_satisfied_p (tree decl)
1819 /* Get the constraints to check for satisfaction. This depends
1820 on whether we're looking at a template specialization or not. */
1821 tree ci = NULL_TREE;
1822 tree args = NULL_TREE;
1823 if (tree ti = DECL_TEMPLATE_INFO (decl))
1825 ci = get_constraints (TI_TEMPLATE (ti));
1826 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
1828 else
1830 ci = get_constraints (decl);
1833 return check_constraints (ci, args);
1836 /* Returns true if the template's constrains are satisfied
1837 by the given arguments. This is used in cases where a
1838 declaration cannot be formed unless the constraints are
1839 checked (e.g., the use of a template-id that names a
1840 class). */
1841 bool
1842 constraints_satisfied_p (tree tmpl, tree args)
1844 return check_constraints (get_constraints (tmpl), args);
1847 /* Evaluate the function concept FN by substituting ARGS into its
1848 definition and evaluating that as the result. Returns
1849 boolean_true_node if the constraints are satisfied and
1850 boolean_false_node otherwise. */
1851 tree
1852 evaluate_function_concept (tree fn, tree args)
1854 ++processing_template_decl;
1855 tree constr = transform_expression (lift_function_definition (fn, args));
1856 --processing_template_decl;
1857 return check_constraint (constr, args, tf_none, NULL_TREE);
1860 /* Evaluate the variable concept VAR by substituting ARGS into
1861 its initializer and checking the resulting constraint. Returns
1862 boolean_true_node if the constraints are satisfied and
1863 boolean_false_node otherwise. */
1864 tree
1865 evaluate_variable_concept (tree decl, tree args)
1867 ++processing_template_decl;
1868 tree constr = transform_expression (lift_variable_initializer (decl, args));
1869 --processing_template_decl;
1870 return check_constraint (constr, args, tf_none, NULL_TREE);
1873 /* Evaluate the given expression as if it were a predicate
1874 constraint. Returns boolean_true_node if the constraint
1875 is satisfied and boolean_false_node otherwise. */
1876 tree
1877 evaluate_constraint_expression (tree expr, tree args)
1879 ++processing_template_decl;
1880 tree constr = transform_expression (lift_expression (expr));
1881 --processing_template_decl;
1882 return check_constraint (constr, args, tf_none, NULL_TREE);
1885 /* Normalize EXPR and determine if the resulting constraint is
1886 satisfied by ARGS. Returns true if and only if the constraint
1887 is satisfied. */
1888 bool
1889 check_constraint_expression (tree expr, tree args)
1891 return evaluate_constraint_expression (expr, args) == boolean_true_node;
1894 /*---------------------------------------------------------------------------
1895 Semantic analysis of requires-expressions
1896 ---------------------------------------------------------------------------*/
1898 /* Finish a requires expression for the given PARMS (possibly
1899 null) and the non-empty sequence of requirements. */
1900 tree
1901 finish_requires_expr (tree parms, tree reqs)
1903 /* Modify the declared parameters by removing their context
1904 so they don't refer to the enclosing scope. */
1905 for (tree p = parms; p && !VOID_TYPE_P (TREE_VALUE (p)); p = TREE_CHAIN (p))
1906 DECL_CONTEXT (TREE_VALUE (p)) = NULL_TREE;
1908 /* Build the node. */
1909 tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs);
1910 TREE_SIDE_EFFECTS (r) = false;
1911 TREE_CONSTANT (r) = true;
1912 return r;
1915 /* Construct a requirement for the validity of EXPR. */
1916 tree
1917 finish_simple_requirement (tree expr)
1919 return build_nt (SIMPLE_REQ, expr);
1922 /* Construct a requirement for the validity of TYPE. */
1923 tree
1924 finish_type_requirement (tree type)
1926 return build_nt (TYPE_REQ, type);
1929 /* Construct a requirement for the validity of EXPR, along with
1930 its properties. if TYPE is non-null, then it specifies either
1931 an implicit conversion or argument deduction constraint,
1932 depending on whether any placeholders occur in the type name.
1933 NOEXCEPT_P is true iff the noexcept keyword was specified. */
1934 tree
1935 finish_compound_requirement (tree expr, tree type, bool noexcept_p)
1937 tree req = build_nt (COMPOUND_REQ, expr, type);
1938 COMPOUND_REQ_NOEXCEPT_P (req) = noexcept_p;
1939 return req;
1942 /* Finish a nested requirement. */
1943 tree
1944 finish_nested_requirement (tree expr)
1946 return build_nt (NESTED_REQ, expr);
1949 // Check that FN satisfies the structural requirements of a
1950 // function concept definition.
1951 tree
1952 check_function_concept (tree fn)
1954 // Check that the function is comprised of only a single
1955 // return statement.
1956 tree body = DECL_SAVED_TREE (fn);
1957 if (TREE_CODE (body) == BIND_EXPR)
1958 body = BIND_EXPR_BODY (body);
1960 // Sometimes a function call results in the creation of clean up
1961 // points. Allow these to be preserved in the body of the
1962 // constraint, as we might actually need them for some constexpr
1963 // evaluations.
1964 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
1965 body = TREE_OPERAND (body, 0);
1967 /* Check that the definition is written correctly. */
1968 if (TREE_CODE (body) != RETURN_EXPR)
1970 location_t loc = DECL_SOURCE_LOCATION (fn);
1971 if (TREE_CODE (body) == STATEMENT_LIST && !STATEMENT_LIST_HEAD (body))
1972 error_at (loc, "definition of concept %qD is empty", fn);
1973 else
1974 error_at (loc, "definition of concept %qD has multiple statements", fn);
1977 return NULL_TREE;
1981 // Check that a constrained friend declaration function declaration,
1982 // FN, is admissible. This is the case only when the declaration depends
1983 // on template parameters and does not declare a specialization.
1984 void
1985 check_constrained_friend (tree fn, tree reqs)
1987 if (fn == error_mark_node)
1988 return;
1989 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
1991 // If there are not constraints, this cannot be an error.
1992 if (!reqs)
1993 return;
1995 // Constrained friend functions that don't depend on template
1996 // arguments are effectively meaningless.
1997 tree parms = DECL_ARGUMENTS (fn);
1998 tree result = TREE_TYPE (TREE_TYPE (fn));
1999 if (!(parms && uses_template_parms (parms)) && !uses_template_parms (result))
2001 error ("constrained friend does not depend on template parameters");
2002 return;
2006 /*---------------------------------------------------------------------------
2007 Equivalence of constraints
2008 ---------------------------------------------------------------------------*/
2010 /* Returns true when A and B are equivalent constraints. */
2011 bool
2012 equivalent_constraints (tree a, tree b)
2014 gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2015 gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2016 return cp_tree_equal (a, b);
2019 /* Returns true if the template declarations A and B have equivalent
2020 constraints. This is the case when A's constraints subsume B's and
2021 when B's also constrain A's. */
2022 bool
2023 equivalently_constrained (tree d1, tree d2)
2025 gcc_assert (TREE_CODE (d1) == TREE_CODE (d2));
2026 return equivalent_constraints (get_constraints (d1), get_constraints (d2));
2029 /*---------------------------------------------------------------------------
2030 Partial ordering of constraints
2031 ---------------------------------------------------------------------------*/
2033 /* Returns true when the the constraints in A subsume those in B. */
2034 bool
2035 subsumes_constraints (tree a, tree b)
2037 gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2038 gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2039 return subsumes (a, b);
2042 /* Determines which of the declarations, A or B, is more constrained.
2043 That is, which declaration's constraints subsume but are not subsumed
2044 by the other's?
2046 Returns 1 if A is more constrained than B, -1 if B is more constrained
2047 than A, and 0 otherwise. */
2049 more_constrained (tree d1, tree d2)
2051 tree c1 = get_constraints (d1);
2052 tree c2 = get_constraints (d2);
2053 int winner = 0;
2054 if (subsumes_constraints (c1, c2))
2055 ++winner;
2056 if (subsumes_constraints (c2, c1))
2057 --winner;
2058 return winner;
2061 /* Returns true if D1 is at least as constrained as D2. That is, the
2062 associated constraints of D1 subsume those of D2, or both declarations
2063 are unconstrained. */
2064 bool
2065 at_least_as_constrained (tree d1, tree d2)
2067 tree c1 = get_constraints (d1);
2068 tree c2 = get_constraints (d2);
2069 return subsumes_constraints (c1, c2);
2073 /*---------------------------------------------------------------------------
2074 Constraint diagnostics
2075 ---------------------------------------------------------------------------*/
2077 /* The diagnosis of constraints performs a combination of
2078 normalization and satisfaction testing. We recursively
2079 walk through the conjunction (or disjunctions) of associated
2080 constraints, testing each sub-expression in turn.
2082 We currently restrict diagnostics to just the top-level
2083 conjunctions within the associated constraints. A fully
2084 recursive walk is possible, but it can generate a lot
2085 of errors. */
2088 namespace {
2090 void diagnose_expression (location_t, tree, tree);
2091 void diagnose_constraint (location_t, tree, tree);
2093 /* Diagnose a conjunction of constraints. */
2094 void
2095 diagnose_logical_operation (location_t loc, tree t, tree args)
2097 diagnose_expression (loc, TREE_OPERAND (t, 0), args);
2098 diagnose_expression (loc, TREE_OPERAND (t, 0), args);
2101 /* Determine if the trait expression T is satisfied by ARGS.
2102 Emit a precise diagnostic if it is not. */
2103 void
2104 diagnose_trait_expression (location_t loc, tree t, tree args)
2106 if (check_constraint_expression (t, args))
2107 return;
2109 /* Rebuild the trait expression so we can diagnose the
2110 specific failure. */
2111 ++processing_template_decl;
2112 tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
2113 --processing_template_decl;
2115 tree t1 = TRAIT_EXPR_TYPE1 (expr);
2116 tree t2 = TRAIT_EXPR_TYPE2 (expr);
2117 switch (TRAIT_EXPR_KIND (t))
2119 case CPTK_HAS_NOTHROW_ASSIGN:
2120 inform (loc, " %qT is not nothrow copy assignable", t1);
2121 break;
2122 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
2123 inform (loc, " %qT is not nothrow default constructible", t1);
2124 break;
2125 case CPTK_HAS_NOTHROW_COPY:
2126 inform (loc, " %qT is not nothrow copy constructible", t1);
2127 break;
2128 case CPTK_HAS_TRIVIAL_ASSIGN:
2129 inform (loc, " %qT is not trivially copy assignable", t1);
2130 break;
2131 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
2132 inform (loc, " %qT is not trivially default constructible", t1);
2133 break;
2134 case CPTK_HAS_TRIVIAL_COPY:
2135 inform (loc, " %qT is not trivially copy constructible", t1);
2136 break;
2137 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
2138 inform (loc, " %qT is not trivially destructible", t1);
2139 break;
2140 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
2141 inform (loc, " %qT does not have a virtual destructor", t1);
2142 break;
2143 case CPTK_IS_ABSTRACT:
2144 inform (loc, " %qT is not an abstract class", t1);
2145 break;
2146 case CPTK_IS_BASE_OF:
2147 inform (loc, " %qT is not a base of %qT", t1, t2);
2148 break;
2149 case CPTK_IS_CLASS:
2150 inform (loc, " %qT is not a class", t1);
2151 break;
2152 case CPTK_IS_CONVERTIBLE_TO:
2153 inform (loc, " %qT is not convertible to %qT", t1, t2);
2154 break;
2155 case CPTK_IS_EMPTY:
2156 inform (loc, " %qT is not an empty class", t1);
2157 break;
2158 case CPTK_IS_ENUM:
2159 inform (loc, " %qT is not an enum", t1);
2160 break;
2161 case CPTK_IS_FINAL:
2162 inform (loc, " %qT is not a final class", t1);
2163 break;
2164 case CPTK_IS_LITERAL_TYPE:
2165 inform (loc, " %qT is not a literal type", t1);
2166 break;
2167 case CPTK_IS_POD:
2168 inform (loc, " %qT is not a POD type", t1);
2169 break;
2170 case CPTK_IS_POLYMORPHIC:
2171 inform (loc, " %qT is not a polymorphic type", t1);
2172 break;
2173 case CPTK_IS_SAME_AS:
2174 inform (loc, " %qT is not the same as %qT", t1, t2);
2175 break;
2176 case CPTK_IS_STD_LAYOUT:
2177 inform (loc, " %qT is not an standard layout type", t1);
2178 break;
2179 case CPTK_IS_TRIVIAL:
2180 inform (loc, " %qT is not a trivial type", t1);
2181 break;
2182 case CPTK_IS_UNION:
2183 inform (loc, " %qT is not a union", t1);
2184 break;
2185 default:
2186 gcc_unreachable ();
2190 /* Determine if the call expression T, when normalized as a constraint,
2191 is satisfied by ARGS.
2193 TODO: If T is refers to a concept, We could recursively analyze
2194 its definition to identify the exact failure, but that could
2195 emit a *lot* of error messages (defeating the purpose of
2196 improved diagnostics). Consider adding a flag to control the
2197 depth of diagnostics. */
2198 void
2199 diagnose_call_expression (location_t loc, tree t, tree args)
2201 if (check_constraint_expression (t, args))
2202 return;
2204 /* Rebuild the expression for the purpose of diagnostics. */
2205 ++processing_template_decl;
2206 tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
2207 --processing_template_decl;
2209 /* If the function call is known to be a concept check, then
2210 diagnose it differently (i.e., we may recurse). */
2211 if (resolve_constraint_check (t))
2212 inform (loc, " concept %qE was not satisfied", expr);
2213 else
2214 inform (loc, " %qE evaluated to false", expr);
2217 /* Determine if the template-id T, when normalized as a constraint
2218 is satisfied by ARGS. */
2219 void
2220 diagnose_template_id (location_t loc, tree t, tree args)
2222 /* Check for invalid template-ids. */
2223 if (!variable_template_p (TREE_OPERAND (t, 0)))
2225 inform (loc, " invalid constraint %qE", t);
2226 return;
2229 if (check_constraint_expression (t, args))
2230 return;
2232 /* Rebuild the expression for the purpose of diagnostics. */
2233 ++processing_template_decl;
2234 tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
2235 --processing_template_decl;
2237 tree var = DECL_TEMPLATE_RESULT (TREE_OPERAND (t, 0));
2238 if (DECL_DECLARED_CONCEPT_P (var))
2239 inform (loc, " concept %qE was not satisfied", expr);
2240 else
2241 inform (loc, " %qE evaluated to false", expr);
2244 /* Determine if the requires-expression, when normalized as a
2245 constraint is satisfied by ARGS.
2247 TODO: Build sets of expressions, types, and constraints
2248 based on the requirements in T and emit specific diagnostics
2249 for those. */
2250 void
2251 diagnose_requires_expression (location_t loc, tree t, tree args)
2253 if (check_constraint_expression (t, args))
2254 return;
2255 inform (loc, "requirements not satisfied");
2258 void
2259 diagnose_pack_expansion (location_t loc, tree t, tree args)
2261 if (check_constraint_expression (t, args))
2262 return;
2264 /* Make sure that we don't have naked packs that we don't expect. */
2265 if (!same_type_p (TREE_TYPE (t), boolean_type_node))
2267 inform (loc, "invalid pack expansion in constraint %qE", t);
2268 return;
2271 inform (loc, " in the expansion of %qE", t);
2273 /* Get the vector of expanded arguments. Note that n must not
2274 be 0 since this constraint is not satisfied. */
2275 ++processing_template_decl;
2276 tree exprs = tsubst_pack_expansion (t, args, tf_none, NULL_TREE);
2277 --processing_template_decl;
2278 if (exprs == error_mark_node)
2280 /* TODO: This error message could be better. */
2281 inform (loc, " substitution failure occurred during expansion");
2282 return;
2285 /* Check each expanded constraint separately. */
2286 int n = TREE_VEC_LENGTH (exprs);
2287 for (int i = 0; i < n; ++i)
2289 tree expr = TREE_VEC_ELT (exprs, i);
2290 if (!check_constraint_expression (expr, args))
2291 inform (loc, " %qE was not satisfied", expr);
2295 /* Diagnose an expression that would be characterized as
2296 a predicate constraint. */
2297 void
2298 diagnose_other_expression (location_t loc, tree t, tree args)
2300 if (check_constraint_expression (t, args))
2301 return;
2302 inform (loc, " %qE evaluated to false", t);
2305 void
2306 diagnose_expression (location_t loc, tree t, tree args)
2308 switch (TREE_CODE (t))
2310 case TRUTH_ANDIF_EXPR:
2311 diagnose_logical_operation (loc, t, args);
2312 break;
2314 case TRUTH_ORIF_EXPR:
2315 diagnose_logical_operation (loc, t, args);
2316 break;
2318 case CALL_EXPR:
2319 diagnose_call_expression (loc, t, args);
2320 break;
2322 case TEMPLATE_ID_EXPR:
2323 diagnose_template_id (loc, t, args);
2324 break;
2326 case REQUIRES_EXPR:
2327 diagnose_requires_expression (loc, t, args);
2328 break;
2330 case TRAIT_EXPR:
2331 diagnose_trait_expression (loc, t, args);
2332 break;
2334 case EXPR_PACK_EXPANSION:
2335 diagnose_pack_expansion (loc, t, args);
2336 break;
2338 default:
2339 diagnose_other_expression (loc, t, args);
2340 break;
2344 inline void
2345 diagnose_predicate_constraint (location_t loc, tree t, tree args)
2347 diagnose_expression (loc, PRED_CONSTR_EXPR (t), args);
2350 inline void
2351 diagnose_conjunction (location_t loc, tree t, tree args)
2353 diagnose_constraint (loc, TREE_OPERAND (t, 0), args);
2354 diagnose_constraint (loc, TREE_OPERAND (t, 1), args);
2357 /* Diagnose the constraint T for the given ARGS. This is only
2358 ever invoked on the associated constraints, so we can
2359 only have conjunctions of predicate constraints. */
2360 void
2361 diagnose_constraint (location_t loc, tree t, tree args)
2363 switch (TREE_CODE (t))
2365 case CONJ_CONSTR:
2366 diagnose_conjunction (loc, t, args);
2367 break;
2369 case PRED_CONSTR:
2370 diagnose_predicate_constraint (loc, t, args);
2371 break;
2373 default:
2374 gcc_unreachable ();
2375 break;
2379 } // namespace
2381 /* Emit diagnostics detailing the failure ARGS to satisfy the constraints
2382 of the template declaration, DECL. */
2383 void
2384 diagnose_constraints (location_t loc, tree decl, tree args)
2386 inform (loc, " constraints not satisfied");
2388 /* Constraints are attached to the template. */
2389 if (tree ti = DECL_TEMPLATE_INFO (decl)) {
2390 decl = TI_TEMPLATE (ti);
2391 args = TI_ARGS (ti);
2394 /* Check that the constraints are actually valid. */
2395 tree ci = get_constraints (decl);
2396 if (!valid_constraints_p (ci))
2398 inform (loc, " invalid constraints");
2399 return;
2402 /* Recursively diagnose the associated constraints. */
2403 diagnose_constraint (loc, CI_ASSOCIATED_CONSTRAINTS (ci), args);