2015-04-20 Andrew Sutton <andrew.n.sutton@gmail.com>
[official-gcc.git] / gcc / cp / constraint.cc
blobd370e7a7e6b046f92f5317cbfdc75c2bcb009906
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));
389 /* Convert the template arguments to ensure that they match
390 the parameters of the variable concept. */
391 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
392 args = coerce_template_parms (parms, args, tmpl, tf_warning_or_error);
393 if (args == error_mark_node)
394 return error_mark_node;
396 return lift_variable_initializer (decl, args);
399 /* Determine if a template-id is a variable concept and inline.
401 TODO: I don't think that we get template-ids for variable
402 templates any more. They tend to be transformed into var-decl
403 specializations when an id-expression is parsed.
405 tree
406 lift_template_id (tree t)
408 if (variable_concept_p (TREE_OPERAND (t, 0)))
409 return lift_variable_concept (t);
411 /* Check that we didn't refer to a function concept like
412 a variable.
414 TODO: Add a note on how to fix this. */
415 tree tmpl = TREE_OPERAND (t, 0);
416 if (TREE_CODE (tmpl) == OVERLOAD)
418 tree fn = OVL_FUNCTION (tmpl);
419 if (TREE_CODE (fn) == TEMPLATE_DECL
420 && DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (fn)))
422 error ("invalid reference to function concept %qD", fn);
423 return error_mark_node;
427 return t;
430 /* If the variable declaration is a specialization of a concept
431 declaration, then inline it's initializer. */
432 tree
433 lift_variable (tree t)
435 if (tree ti = DECL_TEMPLATE_INFO (t))
437 tree tmpl = TI_TEMPLATE (ti);
438 tree args = TI_ARGS (ti);
439 tree decl = DECL_TEMPLATE_RESULT (tmpl);
440 if (DECL_DECLARED_CONCEPT_P (decl))
441 return lift_variable_initializer (decl, args);
443 return t;
446 /* Lift any constraints appearing in a nested requirement of
447 a requires-expression. */
448 tree
449 lift_requires_expression (tree t)
451 tree parms = TREE_OPERAND (t, 0);
452 tree reqs = TREE_OPERAND (t, 1);
453 tree result = NULL_TREE;
454 for (; reqs != NULL_TREE; reqs = TREE_CHAIN (reqs))
456 tree req = TREE_VALUE (reqs);
457 if (TREE_CODE (req) == NESTED_REQ)
459 tree expr = lift_expression (TREE_OPERAND (req, 0));
460 req = finish_nested_requirement (expr);
462 result = tree_cons (NULL_TREE, req, result);
464 return finish_requires_expr (parms, result);
467 /* Inline references to specializations of concepts. */
468 tree
469 lift_expression (tree t)
471 if (t == NULL_TREE)
472 return NULL_TREE;
474 if (t == error_mark_node)
475 return error_mark_node;
477 /* Concepts can be referred to by call or variable. All other
478 nodes are preserved. */
479 switch (TREE_CODE (t))
481 case CALL_EXPR:
482 return lift_call_expression (t);
484 case TEMPLATE_ID_EXPR:
485 return lift_template_id (t);
487 case VAR_DECL:
488 return lift_variable (t);
490 case REQUIRES_EXPR:
491 return lift_requires_expression (t);
493 case EXPR_PACK_EXPANSION:
494 /* Defer the lifting of pack expansions until constraint
495 evaluation. We know that we're not going to be doing
496 anything else with this predicate constraint until
497 we expand it. */
498 return t;
500 case TREE_LIST:
502 t = copy_node (t);
503 TREE_VALUE (t) = lift_expression (TREE_VALUE (t));
504 TREE_CHAIN (t) = lift_expression (TREE_CHAIN (t));
505 return t;
508 default:
509 return lift_operands (t);
513 /*---------------------------------------------------------------------------
514 Constraint normalization
515 ---------------------------------------------------------------------------*/
517 tree transform_expression (tree);
519 /* Check that the logical-or or logical-and expression does
520 not result in a call to a user-defined user-defined operator
521 (temp.constr.op). Returns true if the logical operator is
522 admissible and false otherwise. */
523 bool
524 check_logical_expr (tree t)
526 /* We can't do much for type dependent expressions. */
527 if (type_dependent_expression_p (t))
528 return true;
530 /* Resolve the logical operator. Note that template processing is
531 disabled so we get the actual call or target expression back.
532 not_processing_template_sentinel sentinel.
534 TODO: This check is actually subsumed by the requirement that
535 constraint operands have type bool. I'm not sure we need it
536 unless we allow conversions. */
537 tree arg1 = TREE_OPERAND (t, 0);
538 tree arg2 = TREE_OPERAND (t, 1);
539 tree ovl = NULL_TREE;
540 tree expr = build_x_binary_op (EXPR_LOC_OR_LOC (arg2, input_location),
541 TREE_CODE (t),
542 arg1, TREE_CODE (arg1),
543 arg2, TREE_CODE (arg2),
544 &ovl,
545 tf_none);
546 if (TREE_CODE (expr) != TREE_CODE (t))
548 error ("user-defined operator %qs in constraint %qE",
549 operator_name_info[TREE_CODE (t)].name, t);
550 return false;
552 return true;
555 /* Transform a logical-or or logical-and expression into either
556 a conjunction or disjunction. */
557 tree
558 xform_logical (tree t, tree_code c)
560 if (!check_logical_expr (t))
561 return error_mark_node;
562 tree t0 = transform_expression (TREE_OPERAND (t, 0));
563 tree t1 = transform_expression (TREE_OPERAND (t, 1));
564 return build_nt (c, t0, t1);
567 /* A simple requirement T introduces an expression constraint
568 for its expression. */
569 inline tree
570 xform_simple_requirement (tree t)
572 return build_nt (EXPR_CONSTR, TREE_OPERAND (t, 0));
575 /* A type requirement T introduce a type constraint for its
576 type. */
577 inline tree
578 xform_type_requirement (tree t)
580 return build_nt (TYPE_CONSTR, TREE_OPERAND (t, 0));
583 /* A compound requirement T introduces a conjunction of constraints
584 depending on its form. The conjunction always includes
585 an expression constraint for the expression of the requirement.
586 If a trailing return type was specified, the conjunction includes
587 either an implicit conversion constraint or an argument
588 deduction constraint. If the noexcept specifier is present, the
589 conjunction includes an exception constraint. */
590 tree
591 xform_compound_requirement (tree t)
593 tree expr = TREE_OPERAND (t, 0);
594 tree constr = build_nt (EXPR_CONSTR, TREE_OPERAND (t, 0));
596 /* If a type is given, append an implicit conversion or
597 argument deduction constraint.
599 FIXME: Handle argument deduction constraints. */
600 if (tree type = TREE_OPERAND (t, 1))
602 tree iconv = build_nt (ICONV_CONSTR, expr, type);
603 constr = conjoin_constraints (constr, iconv);
606 /* If noexcept is present, append an exception constraint. */
607 if (COMPOUND_REQ_NOEXCEPT_P (t))
609 tree except = build_nt (EXCEPT_CONSTR, expr);
610 constr = conjoin_constraints (constr, except);
613 return constr;
616 /* A nested requirement T introduces a conjunction of constraints
617 corresponding to its constraint-expression. */
618 inline tree
619 xform_nested_requirement (tree t)
621 return transform_expression (TREE_OPERAND (t, 0));
624 /* Transform a requirement T into one or more constraints. */
625 tree
626 xform_requirement (tree t)
628 switch (TREE_CODE (t))
630 case SIMPLE_REQ:
631 return xform_simple_requirement (t);
633 case TYPE_REQ:
634 return xform_type_requirement (t);
636 case COMPOUND_REQ:
637 return xform_compound_requirement (t);
639 case NESTED_REQ:
640 return xform_nested_requirement (t);
642 default:
643 gcc_unreachable ();
645 return error_mark_node;
648 /* Transform a sequence of requirements into a conjunction of
649 constraints. */
650 tree
651 xform_requirements (tree t)
653 tree result = NULL_TREE;
654 for (; t; t = TREE_CHAIN (t)) {
655 tree constr = xform_requirement (TREE_VALUE (t));
656 result = conjoin_constraints (result, constr);
658 return result;
661 /* Transform a requires-expression into a parameterized constraint. */
662 tree
663 xform_requires_expr (tree t)
665 tree operand = xform_requirements (TREE_OPERAND (t, 1));
666 if (tree parms = TREE_OPERAND (t, 0))
667 return build_nt (PARM_CONSTR, parms, operand);
668 else
669 return operand;
672 /* Transform an expression into an atomic predicate constraint.
673 After substitution, the expression of a predicate constraint shall
674 have type bool (temp.constr.pred). For non-type- dependent
675 expressions, we can check that now. */
676 tree
677 xform_atomic (tree t)
679 if (!type_dependent_expression_p (t))
681 tree type = cv_unqualified (TREE_TYPE (t));
682 if (!same_type_p (type, boolean_type_node))
684 error ("predicate constraint %qE does not have type %<bool%>", t);
685 return error_mark_node;
688 return build_nt (PRED_CONSTR, t);
691 /* Transform an expression into a constraint. */
692 tree
693 xform_expr (tree t)
695 switch (TREE_CODE (t))
697 case TRUTH_ANDIF_EXPR:
698 return xform_logical (t, CONJ_CONSTR);
700 case TRUTH_ORIF_EXPR:
701 return xform_logical (t, DISJ_CONSTR);
703 case REQUIRES_EXPR:
704 return xform_requires_expr (t);
706 case BIND_EXPR:
707 return transform_expression (BIND_EXPR_BODY (t));
709 default:
710 /* All other constraints are atomic. */
711 return xform_atomic (t);
715 /* Transform a statement into an expression. */
716 tree
717 xform_stmt (tree t)
719 switch (TREE_CODE (t))
721 case RETURN_EXPR:
722 return transform_expression (TREE_OPERAND (t, 0));
723 default:
724 gcc_unreachable ();
726 return error_mark_node;
729 // Reduction rules for the declaration T.
730 tree
731 xform_decl (tree t)
733 switch (TREE_CODE (t))
735 case VAR_DECL:
736 return xform_atomic (t);
737 default:
738 gcc_unreachable ();
740 return error_mark_node;
743 /* Transform an exceptional node into a constraint. */
744 tree
745 xform_misc (tree t)
747 switch (TREE_CODE (t))
749 case TRAIT_EXPR:
750 return xform_atomic (t);
751 case CONSTRUCTOR:
752 return xform_atomic (t);
753 default:
754 gcc_unreachable ();
756 return error_mark_node;
760 /* Transform a lifted expression into a constraint. This either
761 returns a constraint, or it returns error_mark_node when
762 a constraint cannot be formed. */
763 tree
764 transform_expression (tree t)
766 if (!t)
767 return NULL_TREE;
769 if (t == error_mark_node)
770 return error_mark_node;
772 switch (TREE_CODE_CLASS (TREE_CODE (t)))
774 case tcc_unary:
775 case tcc_binary:
776 case tcc_expression:
777 case tcc_vl_exp:
778 return xform_expr (t);
780 case tcc_statement:
781 return xform_stmt (t);
783 case tcc_declaration:
784 return xform_decl (t);
786 case tcc_exceptional:
787 return xform_misc (t);
789 case tcc_constant:
790 case tcc_reference:
791 case tcc_comparison:
792 /* These are atomic predicate constraints. */
793 return xform_atomic (t);
795 default:
796 /* Unhandled node kind. */
797 gcc_unreachable ();
799 return error_mark_node;
802 /*---------------------------------------------------------------------------
803 Constraint normalization
804 ---------------------------------------------------------------------------*/
806 tree normalize_constraint (tree);
808 /* The normal form of the disjunction T0 /\ T1 is the conjunction
809 of the normal form of T0 and the normal form of T1 */
810 inline tree
811 normalize_conjunction (tree t)
813 tree t0 = normalize_constraint (TREE_OPERAND (t, 0));
814 tree t1 = normalize_constraint (TREE_OPERAND (t, 1));
815 return build_nt (CONJ_CONSTR, t0, t1);
818 /* The normal form of the disjunction T0 \/ T1 is the disjunction
819 of the normal form of T0 and the normal form of T1 */
820 inline tree
821 normalize_disjunction (tree t)
823 tree t0 = normalize_constraint (TREE_OPERAND (t, 0));
824 tree t1 = normalize_constraint (TREE_OPERAND (t, 1));
825 return build_nt (DISJ_CONSTR, t0, t1);
828 /* A predicate constraint is normalized in two stages. First all
829 references specializations of concepts are replaced by their
830 substituted definitions. Then, the resulting expression is
831 transformed into a constraint by transforming && expressions
832 into conjunctions and || into disjunctions. */
833 tree
834 normalize_predicate_constraint (tree t)
836 tree expr = PRED_CONSTR_EXPR (t);
837 tree lifted = lift_expression (expr);
838 tree constr = transform_expression (lifted);
839 return constr;
842 /* The normal form of a parameterized constraint is the normal
843 form of its operand. */
844 tree
845 normalize_parameterized_constraint (tree t)
847 tree parms = PARM_CONSTR_PARMS (t);
848 tree operand = normalize_constraint (PARM_CONSTR_OPERAND (t));
849 return build_nt (PARM_CONSTR, parms, operand);
852 /* Normalize the constraint T by reducing it so that it is
853 comprised of only conjunctions and disjunctions of atomic
854 constraints. */
855 tree
856 normalize_constraint (tree t)
858 if (!t)
859 return NULL_TREE;
861 switch (TREE_CODE (t))
863 case CONJ_CONSTR:
864 return normalize_conjunction (t);
866 case DISJ_CONSTR:
867 return normalize_disjunction (t);
869 case PRED_CONSTR:
870 return normalize_predicate_constraint (t);
872 case PARM_CONSTR:
873 return normalize_parameterized_constraint (t);
875 case EXPR_CONSTR:
876 case TYPE_CONSTR:
877 case ICONV_CONSTR:
878 case DEDUCT_CONSTR:
879 case EXCEPT_CONSTR:
880 /* These constraints are defined to be atomic. */
881 return t;
883 default:
884 /* CONSTR was not a constraint. */
885 gcc_unreachable();
887 return error_mark_node;
890 } /* namespace */
893 // -------------------------------------------------------------------------- //
894 // Constraint Semantic Processing
896 // The following functions are called by the parser and substitution rules
897 // to create and evaluate constraint-related nodes.
900 // If the recently parsed TYPE declares or defines a template or template
901 // specialization, get its corresponding constraints from the current
902 // template parameters and bind them to TYPE's declaration.
903 tree
904 associate_classtype_constraints (tree type)
906 if (!type || type == error_mark_node || TREE_CODE (type) != RECORD_TYPE)
907 return type;
909 // An explicit class template specialization has no template
910 // parameters.
911 if (!current_template_parms)
912 return type;
914 if (CLASSTYPE_IS_TEMPLATE (type) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
916 tree decl = TYPE_STUB_DECL (type);
917 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
918 tree ci = build_constraints (reqs, NULL_TREE);
920 // An implicitly instantiated member template declaration already
921 // has associated constraints. If it is defined outside of its
922 // class, then we need match these constraints against those of
923 // original declaration.
924 if (tree orig_ci = get_constraints (decl))
926 if (!equivalent_constraints (ci, orig_ci))
928 // FIXME: Improve diagnostics.
929 error ("%qT does not match any declaration", type);
930 return error_mark_node;
932 return type;
934 set_constraints (decl, ci);
936 return type;
939 namespace {
941 // Create an empty constraint info block.
942 inline tree_constraint_info*
943 build_constraint_info ()
945 return (tree_constraint_info *)make_node (CONSTRAINT_INFO);
948 } // namespace
950 /* Build a constraint-info object that contains the
951 associated condstraints of a declaration. This also
952 includes the declaration's template requirements (TREQS)
953 and any trailing requirements for a function declarator
954 (DREQS). Note that both TREQS and DREQS must be constraints.
956 If the declaration has neither template nor declaration
957 requirements this returns NULL_TREE, indicating an
958 unconstrained declaration. */
959 tree
960 build_constraints (tree tmpl_reqs, tree decl_reqs)
962 gcc_assert (tmpl_reqs ? constraint_p (tmpl_reqs) : true);
963 gcc_assert (decl_reqs ? constraint_p (decl_reqs) : true);
965 if (!tmpl_reqs && !decl_reqs)
966 return NULL_TREE;
968 tree_constraint_info* ci = build_constraint_info ();
969 ci->template_reqs = tmpl_reqs;
970 ci->declarator_reqs = decl_reqs;
971 ci->associated_constr = conjoin_constraints (tmpl_reqs, decl_reqs);
973 ++processing_template_decl;
974 ci->normalized_constr = normalize_constraint (ci->associated_constr);
975 --processing_template_decl;
977 ci->assumptions = decompose_assumptions (ci->normalized_constr);
978 return (tree)ci;
981 namespace {
982 // Build a new call expression, but don't actually generate a new
983 // function call. We just want the tree, not the semantics.
984 inline tree
985 build_call_check (tree id)
987 ++processing_template_decl;
988 vec<tree, va_gc> *fargs = make_tree_vector();
989 tree call = finish_call_expr (id, &fargs, false, false, tf_none);
990 --processing_template_decl;
991 return call;
993 } // namespace
995 // Construct a concept check for the given TARGET. The target may be
996 // an overload set or a baselink referring to an overload set. Template
997 // arguments to the target are given by ARG and REST. If the target is
998 // a function (overload set or baselink referring to an overload set),
999 // then this builds the call expression TARGET<ARG, REST>(). If REST is
1000 // NULL_TREE, then the resulting check is just TARGET<ARG>(). If ARG is
1001 // NULL_TREE, then the resulting check is TARGET<REST>().
1002 tree
1003 build_concept_check (tree target, tree arg, tree rest)
1005 gcc_assert (rest ? TREE_CODE (rest) == TREE_VEC : true);
1007 // Build a template-id that acts as the call target using TARGET as
1008 // the template and ARG as the only explicit argument.
1009 int n = rest ? TREE_VEC_LENGTH (rest) : 0;
1010 tree targs;
1011 if (arg)
1013 targs = make_tree_vec (n + 1);
1014 TREE_VEC_ELT (targs, 0) = arg;
1015 if (rest)
1016 for (int i = 0; i < n; ++i)
1017 TREE_VEC_ELT (targs, i + 1) = TREE_VEC_ELT (rest, i);
1018 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, n + 1);
1020 else
1022 gcc_assert (rest != NULL_TREE);
1023 targs = rest;
1026 if (variable_template_p (target))
1027 return lookup_template_variable (target, targs);
1028 else
1029 return build_call_check (lookup_template_function (target, targs));
1032 // Returns a TYPE_DECL that contains sufficient information to build
1033 // a template parameter of the same kind as PROTO and constrained
1034 // by the concept declaration FN. PROTO is saved as the initializer of
1035 // the new type decl, and the constraining function is saved in
1036 // DECL_SIZE_UNIT.
1038 // If specified, ARGS provides additional arguments to the constraint
1039 // check. These are stored in the DECL_SIZE field.
1040 tree
1041 build_constrained_parameter (tree fn, tree proto, tree args)
1043 tree name = DECL_NAME (fn);
1044 tree type = TREE_TYPE (proto);
1045 tree decl = build_decl (input_location, TYPE_DECL, name, type);
1046 DECL_INITIAL (decl) = proto; // Describing parameter
1047 DECL_SIZE_UNIT (decl) = fn; // Constraining function declaration
1048 DECL_SIZE (decl) = args; // Extra template arguments.
1049 return decl;
1052 /* Create a constraint expression for the given DECL that
1053 evaluates the requirements specified by CONSTR, a TYPE_DECL
1054 that contains all the information necessary to build the
1055 requirements (see finish_concept_name for the layout of
1056 that TYPE_DECL).
1058 Note that the constraints are neither reduced nor decomposed.
1059 That is done only after the requires clause has been parsed
1060 (or not). */
1061 tree
1062 finish_shorthand_constraint (tree decl, tree constr)
1064 // No requirements means no constraints.
1065 if (!constr)
1066 return NULL_TREE;
1068 tree proto = DECL_INITIAL (constr); // The prototype declaration
1069 tree con = DECL_SIZE_UNIT (constr); // The concept declaration
1070 tree args = DECL_SIZE (constr); // Extra template arguments
1072 // If the parameter declaration is variadic, but the concept is not
1073 // then we need to apply the concept to every element in the pack.
1074 bool is_proto_pack = template_parameter_pack_p (proto);
1075 bool is_decl_pack = template_parameter_pack_p (decl);
1076 bool apply_to_all_p = is_decl_pack && !is_proto_pack;
1078 // Get the argument and overload used for the requirement. Adjust
1079 // if we're going to expand later.
1080 tree arg = template_parm_to_arg (build_tree_list (NULL_TREE, decl));
1081 if (apply_to_all_p)
1082 arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
1084 // Build the concept check. If it the constraint needs to be applied
1085 // to all elements of the parameter pack, then make the constraint
1086 // an expansion.
1087 tree check;
1088 if (TREE_CODE (con) == VAR_DECL)
1090 check = build_concept_check (DECL_TI_TEMPLATE (con), arg, args);
1092 else
1094 tree ovl = build_overload (DECL_TI_TEMPLATE (con), NULL_TREE);
1095 check = build_concept_check (ovl, arg, args);
1097 if (apply_to_all_p)
1099 check = make_pack_expansion (check);
1101 // Set the type to indicate that this expansion will get special
1102 // treatment during instantiation.
1104 // TODO: Maybe this should be a different kind of node... one that
1105 // has all the same properties as a pack expansion, but has a definite
1106 // expansion when instantiated as part of an expression.
1108 // As of now, this is a hack.
1109 TREE_TYPE (check) = boolean_type_node;
1112 return make_predicate_constraint (check);
1115 /* Returns a conjunction of shorthand requirements for the template
1116 parameter list PARMS. Note that the requirements are stored in
1117 the TYPE of each tree node. */
1118 tree
1119 get_shorthand_constraints (tree parms)
1121 tree result = NULL_TREE;
1122 parms = INNERMOST_TEMPLATE_PARMS (parms);
1123 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
1125 tree parm = TREE_VEC_ELT (parms, i);
1126 tree constr = TEMPLATE_PARM_CONSTRAINTS (parm);
1127 result = conjoin_constraints (result, constr);
1129 return result;
1132 // Returns and chains a new parameter for PARAMETER_LIST which will conform
1133 // to the prototype given by SRC_PARM. The new parameter will have its
1134 // identifier and location set according to IDENT and PARM_LOC respectively.
1135 static tree
1136 process_introduction_parm (tree parameter_list, tree src_parm)
1138 // If we have a pack, we should have a single pack argument which is the
1139 // placeholder we want to look at.
1140 bool is_parameter_pack = ARGUMENT_PACK_P (src_parm);
1141 if (is_parameter_pack)
1142 src_parm = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (src_parm), 0);
1144 // At this point we should have a INTRODUCED_PARM_DECL, but we want to grab
1145 // the associated decl from it. Also grab the stored identifier and location
1146 // that should be chained to it in a PARM_DECL.
1147 gcc_assert (TREE_CODE (src_parm) == INTRODUCED_PARM_DECL);
1149 tree ident = DECL_NAME (src_parm);
1150 location_t parm_loc = DECL_SOURCE_LOCATION (src_parm);
1152 // If we expect a pack and the deduced template is not a pack, or if the
1153 // template is using a pack and we didn't declare a pack, throw an error.
1154 if (is_parameter_pack != INTRODUCED_PACK_P (src_parm))
1156 error_at (parm_loc, "cannot match pack for introduced parameter");
1157 tree err_parm = build_tree_list (error_mark_node, error_mark_node);
1158 return chainon (parameter_list, err_parm);
1161 src_parm = TREE_TYPE (src_parm);
1163 tree parm;
1164 bool is_non_type;
1165 if (TREE_CODE (src_parm) == TYPE_DECL)
1167 is_non_type = false;
1168 parm = finish_template_type_parm (class_type_node, ident);
1170 else if (TREE_CODE (src_parm) == TEMPLATE_DECL)
1172 is_non_type = false;
1173 current_template_parms = DECL_TEMPLATE_PARMS (src_parm);
1174 parm = finish_template_template_parm (class_type_node, ident);
1176 else
1178 is_non_type = true;
1180 // Since we don't have a declarator, so we can copy the source
1181 // parameter and change the name and eventually the location.
1182 parm = copy_decl (src_parm);
1183 DECL_NAME (parm) = ident;
1186 // Wrap in a TREE_LIST for process_template_parm. Introductions do not
1187 // retain the defaults from the source template.
1188 parm = build_tree_list (NULL_TREE, parm);
1190 return process_template_parm (parameter_list, parm_loc, parm,
1191 is_non_type, is_parameter_pack);
1194 /* Associates a constraint check to the current template based
1195 on the introduction parameters. INTRO_LIST should be a TREE_VEC
1196 of INTRODUCED_PARM_DECLs containing a chained PARM_DECL which
1197 contains the identifier as well as the source location.
1198 TMPL_DECL is the decl for the concept being used. If we take
1199 some concept, C, this will form a check in the form of
1200 C<INTRO_LIST> filling in any extra arguments needed by the
1201 defaults deduced.
1203 Returns NULL_TREE if no concept could be matched and
1204 error_mark_node if an error occurred when matching.
1206 tree
1207 finish_template_introduction (tree tmpl_decl, tree intro_list)
1209 /* Deduce the concept check. */
1210 tree expr = build_concept_check (tmpl_decl, NULL_TREE, intro_list);
1211 if (expr == error_mark_node)
1212 return NULL_TREE;
1214 tree parms = deduce_concept_introduction (expr);
1215 if (!parms)
1216 return NULL_TREE;
1218 /* Build template parameter scope for introduction. */
1219 tree parm_list = NULL_TREE;
1220 begin_template_parm_list ();
1221 int nargs = MIN (TREE_VEC_LENGTH (parms), TREE_VEC_LENGTH (intro_list));
1222 for (int n = 0; n < nargs; ++n)
1223 parm_list = process_introduction_parm (parm_list, TREE_VEC_ELT (parms, n));
1224 parm_list = end_template_parm_list (parm_list);
1225 for (int i = 0; i < TREE_VEC_LENGTH (parm_list); ++i)
1226 if (TREE_VALUE (TREE_VEC_ELT (parm_list, i)) == error_mark_node)
1228 end_template_decl ();
1229 return error_mark_node;
1232 /* Build a concept check for our constraint. */
1233 tree check_args = make_tree_vec (TREE_VEC_LENGTH (parms));
1234 int n = 0;
1235 for (; n < TREE_VEC_LENGTH (parm_list); ++n)
1237 tree parm = TREE_VEC_ELT (parm_list, n);
1238 TREE_VEC_ELT (check_args, n) = template_parm_to_arg (parm);
1241 /* If the template expects more parameters we should be able
1242 to use the defaults from our deduced concept. */
1243 for (; n < TREE_VEC_LENGTH (parms); ++n)
1244 TREE_VEC_ELT (check_args, n) = TREE_VEC_ELT (parms, n);
1246 /* Associate the constraint. */
1247 tree check = build_concept_check (tmpl_decl, NULL_TREE, check_args);
1248 tree constr = make_predicate_constraint (check);
1249 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = constr;
1251 return parm_list;
1254 /*---------------------------------------------------------------------------
1255 Constraint substitution
1256 ---------------------------------------------------------------------------*/
1258 tree tsubst_constraint (tree, tree, tsubst_flags_t, tree);
1260 /* The following functions implement substitution rules for constraints.
1261 Substitution without checking constraints happens only in the
1262 instantiation of class templates. For example:
1264 template<C1 T> struct S {
1265 void f(T) requires C2<T>;
1266 void g(T) requires T::value;
1269 S<int> s; // error instantiating S<int>::g(T)
1271 When we instantiate S, we substitute into its member declarations,
1272 including their constraints. However, those constraints are not
1273 checked. Substituting int into C2<T> yields C2<int>, and substituting
1274 into T::value yields a substitution failure, making the program
1275 ill-formed.
1277 Note that we only ever substitute into the associated constraints
1278 of a declaration. That is, substitute is defined only for predicate
1279 constraints and conjunctions. */
1281 /* Substitute into the predicate constraints. Returns error_mark_node
1282 if the substitution into the expression fails. */
1283 tree
1284 tsubst_predicate_constraint (tree t, tree args,
1285 tsubst_flags_t complain, tree in_decl)
1287 tree expr = PRED_CONSTR_EXPR (t);
1288 ++processing_template_decl;
1289 tree result = tsubst_expr (expr, args, complain, in_decl, false);
1290 --processing_template_decl;
1291 return build_nt (PRED_CONSTR, result);
1294 /* Substitute into the conjunction of constraints. Returns
1295 error_mark_node if substitution into either operand fails. */
1296 tree
1297 tsubst_conjunction (tree t, tree args,
1298 tsubst_flags_t complain, tree in_decl)
1300 tree t0 = TREE_OPERAND (t, 0);
1301 tree r0 = tsubst_constraint (t0, args, complain, in_decl);
1302 tree t1 = TREE_OPERAND (t, 1);
1303 tree r1 = tsubst_constraint (t1, args, complain, in_decl);
1304 return build_nt (CONJ_CONSTR, r0, r1);
1307 /* Substitute ARGS into the constraint T. */
1308 tree
1309 tsubst_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1311 if (TREE_CODE (t) == CONJ_CONSTR)
1312 return tsubst_conjunction (t, args, complain, in_decl);
1313 else if (TREE_CODE (t) == PRED_CONSTR)
1314 return tsubst_predicate_constraint (t, args, complain, in_decl);
1315 else
1316 gcc_unreachable ();
1317 return error_mark_node;
1320 namespace {
1322 /* A subroutine of tsubst_constraint_variables. In an unevaluated
1323 context, the substitution of PARM_DECLs are not properly chained
1324 during substitution. Do that here. */
1325 tree
1326 fixup_constraint_vars (tree parms)
1328 if (!parms)
1329 return parms;
1331 tree p = TREE_CHAIN (parms);
1332 tree q = parms;
1333 while (p && TREE_VALUE (p) != void_type_node)
1335 DECL_CHAIN (TREE_VALUE (q)) = TREE_VALUE (p);
1336 q = p;
1337 p = TREE_CHAIN (p);
1339 return parms;
1342 /* A subroutine of tsubst_constraint_variables. Register local
1343 specializations for each of parameter in PARMS and its
1344 corresponding substituted constraint variable in VARS.
1345 Returns VARS. */
1346 tree
1347 declare_constraint_vars (tree parms, tree vars)
1349 tree s = TREE_VALUE (vars);
1350 for (tree p = parms; p && !VOID_TYPE_P (TREE_VALUE (p)); p = TREE_CHAIN (p))
1352 tree t = TREE_VALUE (p);
1353 CONSTRAINT_VAR_P (t) = true;
1354 if (DECL_PACK_P (t))
1356 tree pack = extract_fnparm_pack (t, &s);
1357 register_local_specialization (pack, t);
1359 else
1361 register_local_specialization (s, t);
1362 s = TREE_CHAIN (s);
1365 return vars;
1368 /* A subroutine of tsubst_parameterized_constraint. Substitute ARGS
1369 into the parameter list T, producing a sequence of constraint
1370 variables, declared in the current scope.
1372 Note that the caller must establish a local specialization stack
1373 prior to calling this function since this substitution will
1374 declare the substituted parameters. */
1375 tree
1376 tsubst_constraint_variables (tree t, tree args,
1377 tsubst_flags_t complain, tree in_decl)
1379 tree vars = tsubst (t, args, complain, in_decl);
1380 if (vars == error_mark_node)
1381 return error_mark_node;
1382 return declare_constraint_vars (t, fixup_constraint_vars (vars));
1385 /* Substitute ARGS into the simple requirement T. Note that
1386 substitution may result in an ill-formed expression without
1387 causing the program to be ill-formed. In such cases, the
1388 requirement wraps an error_mark_node. */
1389 inline tree
1390 tsubst_simple_requirement (tree t, tree args,
1391 tsubst_flags_t complain, tree in_decl)
1393 ++processing_template_decl;
1394 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1395 --processing_template_decl;
1396 return finish_simple_requirement (expr);
1399 /* Substitute ARGS into the type requirement T. Note that
1400 substitution may result in an ill-formed type without
1401 causing the program to be ill-formed. In such cases, the
1402 requirement wraps an error_mark_node. */
1403 inline tree
1404 tsubst_type_requirement (tree t, tree args,
1405 tsubst_flags_t complain, tree in_decl)
1407 ++processing_template_decl;
1408 tree type = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
1409 --processing_template_decl;
1410 return finish_type_requirement (type);
1413 /* Substitute args into the compound requirement T. If substituting
1414 into either the expression or the type fails, the corresponding
1415 operands in the resulting node will be error_mark_node. This
1416 preserves a requirement for the purpose of partial ordering, but
1417 it will never be satisfied. */
1418 tree
1419 tsubst_compound_requirement (tree t, tree args,
1420 tsubst_flags_t complain, tree in_decl)
1422 ++processing_template_decl;
1423 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1424 tree type = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
1425 --processing_template_decl;
1426 bool noexcept_p = COMPOUND_REQ_NOEXCEPT_P (t);
1427 return finish_compound_requirement (expr, type, noexcept_p);
1430 /* Substitute ARGS into the nested requirement T. */
1431 tree
1432 tsubst_nested_requirement (tree t, tree args,
1433 tsubst_flags_t complain, tree in_decl)
1435 ++processing_template_decl;
1436 tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1437 --processing_template_decl;
1438 return finish_nested_requirement (expr);
1441 inline tree
1442 tsubst_requirement (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1444 switch (TREE_CODE (t))
1446 case SIMPLE_REQ:
1447 return tsubst_simple_requirement (t, args, complain, in_decl);
1448 case TYPE_REQ:
1449 return tsubst_type_requirement (t, args, complain, in_decl);
1450 case COMPOUND_REQ:
1451 return tsubst_compound_requirement (t, args, complain, in_decl);
1452 case NESTED_REQ:
1453 return tsubst_nested_requirement (t, args, complain, in_decl);
1454 default:
1455 gcc_unreachable ();
1457 return error_mark_node;
1460 /* Substitute ARGS into the list of requirements T. Note that
1461 substitution failures here result in ill-formed programs. */
1462 tree
1463 tsubst_requirement_body (tree t, tree args,
1464 tsubst_flags_t complain, tree in_decl)
1466 tree r = NULL_TREE;
1467 while (t)
1469 tree e = tsubst_requirement (TREE_VALUE (t), args, complain, in_decl);
1470 if (e == error_mark_node)
1471 return error_mark_node;
1472 r = tree_cons (NULL_TREE, e, r);
1473 t = TREE_CHAIN (t);
1475 return r;
1478 } /* namespace */
1480 /* Substitute ARGS into the requires expression T. Note that this
1481 results in the re-declaration of local parameters when
1482 substituting through the parameter list. If either substitution
1483 fails, the program is ill-formed. */
1484 tree
1485 tsubst_requires_expr (tree t, tree args,
1486 tsubst_flags_t complain, tree in_decl)
1488 local_specialization_stack stack;
1490 tree parms = TREE_OPERAND (t, 0);
1491 if (parms)
1493 parms = tsubst_constraint_variables (parms, args, complain, in_decl);
1494 if (parms == error_mark_node)
1495 return error_mark_node;
1498 tree reqs = TREE_OPERAND (t, 1);
1499 reqs = tsubst_requirement_body (reqs, args, complain, in_decl);
1500 if (reqs == error_mark_node)
1501 return error_mark_node;
1503 return finish_requires_expr (parms, reqs);
1506 /* Substitute ARGS into the constraint information CI, producing a new
1507 constraint record. */
1508 tree
1509 tsubst_constraint_info (tree t, tree args,
1510 tsubst_flags_t complain, tree in_decl)
1512 if (!t || t == error_mark_node || !check_constraint_info (t))
1513 return NULL_TREE;
1515 tree tmpl_constr = NULL_TREE;
1516 if (tree r = CI_TEMPLATE_REQS (t))
1517 tmpl_constr = tsubst_constraint (r, args, complain, in_decl);
1519 tree decl_constr = NULL_TREE;
1520 if (tree r = CI_DECLARATOR_REQS (t))
1521 decl_constr = tsubst_constraint (r, args, complain, in_decl);
1523 return build_constraints (tmpl_constr, decl_constr);
1527 /*---------------------------------------------------------------------------
1528 Constraint satisfaction
1529 ---------------------------------------------------------------------------*/
1531 /* The following functions determine if a constraint, when
1532 substituting template arguments, is satisfied. For convenience,
1533 satisfaction reduces a constraint to either true or false (and
1534 nothing else). */
1536 namespace {
1538 tree check_constraint (tree, tree, tsubst_flags_t, tree);
1540 /* Check the pack expansion by first transforming it into a
1541 conjunction of constraints. */
1542 tree
1543 check_pack_expansion (tree t, tree args,
1544 tsubst_flags_t complain, tree in_decl)
1546 /* Check that somebody isn't arbitrarily expanding packs
1547 as part of a predicate. Note that packs are normally
1548 untyped, however, we use the type field as a hack to
1549 indicate folded boolean expressions generated from a
1550 shorthand constraint. This check should disappear with
1551 fold expressions. */
1552 if (!TREE_TYPE (t) || !same_type_p (TREE_TYPE (t), boolean_type_node))
1554 error ("invalid pack expansion in constraint %qE", t);
1555 return boolean_false_node;
1558 /* Get the vector of expanded arguments. */
1559 tree exprs = tsubst_pack_expansion (t, args, complain, in_decl);
1560 if (exprs == error_mark_node)
1561 return boolean_false_node;
1562 int n = TREE_VEC_LENGTH (exprs);
1564 /* An empty expansion is inherently satisfied. */
1565 if (n == 0)
1566 return boolean_true_node;
1568 /* Build a conjunction of constraints from the resulting
1569 expansions and then check that. */
1570 tree result = NULL_TREE;
1571 for (int i = 0; i < n; ++i)
1573 tree expr = TREE_VEC_ELT (exprs, i);
1574 tree constr = transform_expression (expr);
1575 result = conjoin_constraints (result, constr);
1577 return check_constraint (result, args, complain, in_decl);
1580 /* A predicate constraint is satisfied if its expression evaluates
1581 to true. If substitution into that node fails, the constraint
1582 is not satisfied ([temp.constr.pred]).
1584 Note that a predicate constraint is a constraint expression
1585 of type bool. If neither of those are true, the program is
1586 ill-formed; they are not SFINAE'able errors. */
1587 tree
1588 check_predicate_constraint (tree t, tree args,
1589 tsubst_flags_t complain, tree in_decl)
1591 tree original = TREE_OPERAND (t, 0);
1593 /* Pack expansions are not transformed during normalization,
1594 which means we might have requires-expressions.
1596 FIXME: We should never have a naked pack expansion in a
1597 predicate constraint. When the fold-expression branch is
1598 integrated, this same logical will apply to that fold. */
1599 if (TREE_CODE (original) == EXPR_PACK_EXPANSION)
1600 return check_pack_expansion (original, args, complain, in_decl);
1602 tree expr = tsubst_expr (original, args, complain, in_decl, false);
1603 if (expr == error_mark_node)
1604 return boolean_false_node;
1606 tree result = fold_non_dependent_expr (expr);
1607 if (result == error_mark_node)
1608 return boolean_false_node;
1610 /* A predicate constraint shall have type bool. In some
1611 cases, substitution gives us const-qualified bool, which
1612 is also acceptable. */
1613 tree type = cv_unqualified (TREE_TYPE (result));
1614 if (!same_type_p (type, boolean_type_node))
1616 error_at (EXPR_LOC_OR_LOC (t, input_location),
1617 "constraint %qE does not have type %qT",
1618 result, boolean_type_node);
1619 return boolean_false_node;
1622 return cxx_constant_value (result);
1625 /* Check an expression constraint. The constraint is satisfied if
1626 substitution succeeds ([temp.constr.expr]).
1628 Note that the expression is unevaluated. */
1629 tree
1630 check_expression_constraint (tree t, tree args,
1631 tsubst_flags_t complain, tree in_decl)
1633 cp_unevaluated guard;
1635 /* A sentinel class that ensures that deferred access checks
1636 are popped before a function returns. */
1637 struct deferring_access_check_sentinel
1639 deferring_access_check_sentinel ()
1641 push_deferring_access_checks (dk_deferred);
1643 ~ deferring_access_check_sentinel ()
1645 pop_deferring_access_checks ();
1647 } deferring;
1649 tree expr = EXPR_CONSTR_EXPR (t);
1650 tree check = tsubst_expr (expr, args, complain, in_decl, false);
1651 if (check == error_mark_node)
1652 return boolean_false_node;
1653 if (!perform_deferred_access_checks (tf_none))
1654 return boolean_false_node;
1656 return boolean_true_node;
1659 /* Check a type constraint. The constraint is satisfied if
1660 substitution succeeds. */
1661 inline tree
1662 check_type_constraint (tree t, tree args,
1663 tsubst_flags_t complain, tree in_decl)
1665 tree type = TYPE_CONSTR_TYPE (t);
1666 tree check = tsubst (type, args, complain, in_decl);
1667 if (check == error_mark_node)
1668 return boolean_false_node;
1670 /* Check any deferred access checks now, and if any fail,
1671 pop them so they aren't diagnosed later. */
1672 if (!perform_deferred_access_checks (tf_none))
1674 pop_deferring_access_checks ();
1675 return boolean_false_node;
1678 return boolean_true_node;
1681 /* Check an implicit conversion constraint. */
1682 tree
1683 check_implicit_conversion_constraint (tree t, tree args,
1684 tsubst_flags_t complain, tree in_decl)
1686 /* Don't tsubst as if we're processing a template. If we try
1687 to we can end up generating template-like expresssions
1688 (e.g., modop-exprs) that aren't properly typed. */
1689 int saved_template_decl = processing_template_decl;
1690 processing_template_decl = 0;
1691 tree expr =
1692 tsubst_expr (ICONV_CONSTR_EXPR (t), args, complain, in_decl, false);
1693 processing_template_decl = saved_template_decl;
1694 if (expr == error_mark_node)
1695 return boolean_false_node;
1697 /* Get the transformed target type. */
1698 tree type = tsubst (ICONV_CONSTR_TYPE (t), args, complain, in_decl);
1699 if (type == error_mark_node)
1700 return boolean_false_node;
1702 /* Attempt the conversion as a direct initialization
1703 of the form TYPE <unspecified> = EXPR. */
1704 tree conv =
1705 perform_direct_initialization_if_possible (type, expr, false, complain);
1706 if (conv == error_mark_node)
1707 return boolean_false_node;
1708 else
1709 return boolean_true_node;
1712 /* Check an argument deduction constraint.
1714 TODO: Implement me. We need generalized auto for this to work. */
1715 tree
1716 check_argument_deduction_constraint (tree /*t*/, tree /*args*/,
1717 tsubst_flags_t /*complain*/,
1718 tree /*in_decl*/)
1720 gcc_unreachable ();
1721 return boolean_false_node;
1724 /* Check an exception constraint. An exception constraint for an
1725 expression e is satisfied when noexcept(e) is true. */
1726 tree
1727 check_exception_constraint (tree t, tree args,
1728 tsubst_flags_t complain, tree in_decl)
1730 tree expr = EXCEPT_CONSTR_EXPR (t);
1731 tree check = tsubst_expr (expr, args, complain, in_decl, false);
1732 if (check == error_mark_node)
1733 return boolean_false_node;
1735 if (expr_noexcept_p (check, complain))
1736 return boolean_true_node;
1737 else
1738 return boolean_false_node;
1741 /* Check a parameterized constraint. */
1742 tree
1743 check_parameterized_constraint (tree t, tree args,
1744 tsubst_flags_t complain, tree in_decl)
1746 local_specialization_stack stack;
1747 tree parms = PARM_CONSTR_PARMS (t);
1748 tree vars = tsubst_constraint_variables (parms, args, complain, in_decl);
1749 if (vars == error_mark_node)
1750 return boolean_false_node;
1751 tree constr = PARM_CONSTR_OPERAND (t);
1752 return check_constraint (constr, args, complain, in_decl);
1755 /* Check that the conjunction of constraints is satisfied. Note
1756 that if left operand is not satisfied, the right operand
1757 is not checked.
1759 FIXME: Check that this wouldn't result in a user-defined
1760 operator. Note that this error is partially diagnosed in
1761 check_predicate_constriant. It would be nice to diagnose
1762 the overload, but I don't think it's strictly necessary. */
1763 tree
1764 check_conjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1766 tree t0 = check_constraint (TREE_OPERAND (t, 0), args, complain, in_decl);
1767 if (t0 == boolean_false_node)
1768 return t0;
1769 tree t1 = check_constraint (TREE_OPERAND (t, 1), args, complain, in_decl);
1770 if (t1 == boolean_false_node)
1771 return t1;
1772 return boolean_true_node;
1775 /* Check that the disjunction of constraints is satisfied. Note
1776 that if the left operand is satisfied, the right operand is not
1777 checked. */
1778 tree
1779 check_disjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1781 tree t0 = check_constraint (TREE_OPERAND (t, 0), args, complain, in_decl);
1782 if (t0 == boolean_true_node)
1783 return boolean_true_node;
1784 tree t1 = check_constraint (TREE_OPERAND (t, 1), args, complain, in_decl);
1785 if (t1 == boolean_true_node)
1786 return boolean_true_node;
1787 return boolean_false_node;
1790 /* Check that the constraint is satisfied, according to the rules
1791 for that constraint. Note that each check_* function returns
1792 true or false, depending on whether it is satisfied or not. */
1793 tree
1794 check_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1796 if (!t)
1797 return boolean_false_node;
1799 if (t == error_mark_node)
1800 return boolean_false_node;
1802 switch (TREE_CODE (t))
1804 case PRED_CONSTR:
1805 return check_predicate_constraint (t, args, complain, in_decl);
1807 case EXPR_CONSTR:
1808 return check_expression_constraint (t, args, complain, in_decl);
1810 case TYPE_CONSTR:
1811 return check_type_constraint (t, args, complain, in_decl);
1813 case ICONV_CONSTR:
1814 return check_implicit_conversion_constraint (t, args, complain, in_decl);
1816 case DEDUCT_CONSTR:
1817 return check_argument_deduction_constraint (t, args, complain, in_decl);
1819 case EXCEPT_CONSTR:
1820 return check_exception_constraint (t, args, complain, in_decl);
1822 case PARM_CONSTR:
1823 return check_parameterized_constraint (t, args, complain, in_decl);
1825 case CONJ_CONSTR:
1826 return check_conjunction (t, args, complain, in_decl);
1828 case DISJ_CONSTR:
1829 return check_disjunction (t, args, complain, in_decl);
1831 default:
1832 gcc_unreachable ();
1834 return boolean_false_node;
1837 } /* namespace */
1839 /* Check the constraints in CI against the given ARGS, returning
1840 true when the constraints are satisfied and false otherwise.
1842 Note that this is the only place that we instantiate the
1843 constraints. */
1844 bool
1845 check_constraints (tree ci, tree args)
1847 /* If there are no constraints then this is trivially satisfied. */
1848 if (!ci)
1849 return true;
1851 /* If any arguments depend on template parameters, we can't
1852 check constraints. */
1853 if (args && uses_template_parms (args))
1854 return true;
1856 /* Invalid requirements cannot be satisfied. */
1857 if (!valid_constraints_p (ci))
1858 return false;
1860 tree constr = CI_NORMALIZED_CONSTRAINTS (ci);
1861 tree result = check_constraint (constr, args, tf_none, NULL_TREE);
1862 return result == boolean_true_node;
1865 /* Returns true if the declaration's constraints are satisfied.
1866 This is used in cases where a declaration is formed but
1867 before it is used (e.g., overload resolution). */
1868 bool
1869 constraints_satisfied_p (tree decl)
1871 /* Get the constraints to check for satisfaction. This depends
1872 on whether we're looking at a template specialization or not. */
1873 tree ci = NULL_TREE;
1874 tree args = NULL_TREE;
1875 if (tree ti = DECL_TEMPLATE_INFO (decl))
1877 ci = get_constraints (TI_TEMPLATE (ti));
1878 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
1880 else
1882 ci = get_constraints (decl);
1885 return check_constraints (ci, args);
1888 /* Returns true if the template's constrains are satisfied
1889 by the given arguments. This is used in cases where a
1890 declaration cannot be formed unless the constraints are
1891 checked (e.g., the use of a template-id that names a
1892 class). */
1893 bool
1894 constraints_satisfied_p (tree tmpl, tree args)
1896 return check_constraints (get_constraints (tmpl), args);
1899 /* Evaluate the function concept FN by substituting ARGS into its
1900 definition and evaluating that as the result. Returns
1901 boolean_true_node if the constraints are satisfied and
1902 boolean_false_node otherwise. */
1903 tree
1904 evaluate_function_concept (tree fn, tree args)
1906 ++processing_template_decl;
1907 tree constr = transform_expression (lift_function_definition (fn, args));
1908 --processing_template_decl;
1909 return check_constraint (constr, args, tf_none, NULL_TREE);
1912 /* Evaluate the variable concept VAR by substituting ARGS into
1913 its initializer and checking the resulting constraint. Returns
1914 boolean_true_node if the constraints are satisfied and
1915 boolean_false_node otherwise. */
1916 tree
1917 evaluate_variable_concept (tree decl, tree args)
1919 ++processing_template_decl;
1920 tree constr = transform_expression (lift_variable_initializer (decl, args));
1921 --processing_template_decl;
1922 return check_constraint (constr, args, tf_none, NULL_TREE);
1925 /* Evaluate the given expression as if it were a predicate
1926 constraint. Returns boolean_true_node if the constraint
1927 is satisfied and boolean_false_node otherwise. */
1928 tree
1929 evaluate_constraint_expression (tree expr, tree args)
1931 ++processing_template_decl;
1932 tree constr = transform_expression (lift_expression (expr));
1933 --processing_template_decl;
1934 return check_constraint (constr, args, tf_none, NULL_TREE);
1937 /* Normalize EXPR and determine if the resulting constraint is
1938 satisfied by ARGS. Returns true if and only if the constraint
1939 is satisfied. */
1940 bool
1941 check_constraint_expression (tree expr, tree args)
1943 return evaluate_constraint_expression (expr, args) == boolean_true_node;
1946 /*---------------------------------------------------------------------------
1947 Semantic analysis of requires-expressions
1948 ---------------------------------------------------------------------------*/
1950 /* Finish a requires expression for the given PARMS (possibly
1951 null) and the non-empty sequence of requirements. */
1952 tree
1953 finish_requires_expr (tree parms, tree reqs)
1955 /* Modify the declared parameters by removing their context
1956 so they don't refer to the enclosing scope and explicitly
1957 indicating that they are constraint variables. */
1958 for (tree p = parms; p && !VOID_TYPE_P (TREE_VALUE (p)); p = TREE_CHAIN (p))
1960 tree parm = TREE_VALUE (p);
1961 DECL_CONTEXT (parm) = NULL_TREE;
1962 CONSTRAINT_VAR_P (parm) = true;
1965 /* Build the node. */
1966 tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs);
1967 TREE_SIDE_EFFECTS (r) = false;
1968 TREE_CONSTANT (r) = true;
1969 return r;
1972 /* Construct a requirement for the validity of EXPR. */
1973 tree
1974 finish_simple_requirement (tree expr)
1976 return build_nt (SIMPLE_REQ, expr);
1979 /* Construct a requirement for the validity of TYPE. */
1980 tree
1981 finish_type_requirement (tree type)
1983 return build_nt (TYPE_REQ, type);
1986 /* Construct a requirement for the validity of EXPR, along with
1987 its properties. if TYPE is non-null, then it specifies either
1988 an implicit conversion or argument deduction constraint,
1989 depending on whether any placeholders occur in the type name.
1990 NOEXCEPT_P is true iff the noexcept keyword was specified. */
1991 tree
1992 finish_compound_requirement (tree expr, tree type, bool noexcept_p)
1994 tree req = build_nt (COMPOUND_REQ, expr, type);
1995 COMPOUND_REQ_NOEXCEPT_P (req) = noexcept_p;
1996 return req;
1999 /* Finish a nested requirement. */
2000 tree
2001 finish_nested_requirement (tree expr)
2003 return build_nt (NESTED_REQ, expr);
2006 // Check that FN satisfies the structural requirements of a
2007 // function concept definition.
2008 tree
2009 check_function_concept (tree fn)
2011 // Check that the function is comprised of only a single
2012 // return statement.
2013 tree body = DECL_SAVED_TREE (fn);
2014 if (TREE_CODE (body) == BIND_EXPR)
2015 body = BIND_EXPR_BODY (body);
2017 // Sometimes a function call results in the creation of clean up
2018 // points. Allow these to be preserved in the body of the
2019 // constraint, as we might actually need them for some constexpr
2020 // evaluations.
2021 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
2022 body = TREE_OPERAND (body, 0);
2024 /* Check that the definition is written correctly. */
2025 if (TREE_CODE (body) != RETURN_EXPR)
2027 location_t loc = DECL_SOURCE_LOCATION (fn);
2028 if (TREE_CODE (body) == STATEMENT_LIST && !STATEMENT_LIST_HEAD (body))
2029 error_at (loc, "definition of concept %qD is empty", fn);
2030 else
2031 error_at (loc, "definition of concept %qD has multiple statements", fn);
2034 return NULL_TREE;
2038 // Check that a constrained friend declaration function declaration,
2039 // FN, is admissible. This is the case only when the declaration depends
2040 // on template parameters and does not declare a specialization.
2041 void
2042 check_constrained_friend (tree fn, tree reqs)
2044 if (fn == error_mark_node)
2045 return;
2046 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
2048 // If there are not constraints, this cannot be an error.
2049 if (!reqs)
2050 return;
2052 // Constrained friend functions that don't depend on template
2053 // arguments are effectively meaningless.
2054 tree parms = DECL_ARGUMENTS (fn);
2055 tree result = TREE_TYPE (TREE_TYPE (fn));
2056 if (!(parms && uses_template_parms (parms)) && !uses_template_parms (result))
2058 error ("constrained friend does not depend on template parameters");
2059 return;
2063 /*---------------------------------------------------------------------------
2064 Equivalence of constraints
2065 ---------------------------------------------------------------------------*/
2067 /* Returns true when A and B are equivalent constraints. */
2068 bool
2069 equivalent_constraints (tree a, tree b)
2071 gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2072 gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2073 return cp_tree_equal (a, b);
2076 /* Returns true if the template declarations A and B have equivalent
2077 constraints. This is the case when A's constraints subsume B's and
2078 when B's also constrain A's. */
2079 bool
2080 equivalently_constrained (tree d1, tree d2)
2082 gcc_assert (TREE_CODE (d1) == TREE_CODE (d2));
2083 return equivalent_constraints (get_constraints (d1), get_constraints (d2));
2086 /*---------------------------------------------------------------------------
2087 Partial ordering of constraints
2088 ---------------------------------------------------------------------------*/
2090 /* Returns true when the the constraints in A subsume those in B. */
2091 bool
2092 subsumes_constraints (tree a, tree b)
2094 gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2095 gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2096 return subsumes (a, b);
2099 /* Determines which of the declarations, A or B, is more constrained.
2100 That is, which declaration's constraints subsume but are not subsumed
2101 by the other's?
2103 Returns 1 if A is more constrained than B, -1 if B is more constrained
2104 than A, and 0 otherwise. */
2106 more_constrained (tree d1, tree d2)
2108 tree c1 = get_constraints (d1);
2109 tree c2 = get_constraints (d2);
2110 int winner = 0;
2111 if (subsumes_constraints (c1, c2))
2112 ++winner;
2113 if (subsumes_constraints (c2, c1))
2114 --winner;
2115 return winner;
2118 /* Returns true if D1 is at least as constrained as D2. That is, the
2119 associated constraints of D1 subsume those of D2, or both declarations
2120 are unconstrained. */
2121 bool
2122 at_least_as_constrained (tree d1, tree d2)
2124 tree c1 = get_constraints (d1);
2125 tree c2 = get_constraints (d2);
2126 return subsumes_constraints (c1, c2);
2130 /*---------------------------------------------------------------------------
2131 Constraint diagnostics
2132 ---------------------------------------------------------------------------*/
2134 /* The diagnosis of constraints performs a combination of
2135 normalization and satisfaction testing. We recursively
2136 walk through the conjunction (or disjunctions) of associated
2137 constraints, testing each sub-expression in turn.
2139 We currently restrict diagnostics to just the top-level
2140 conjunctions within the associated constraints. A fully
2141 recursive walk is possible, but it can generate a lot
2142 of errors. */
2145 namespace {
2147 void diagnose_expression (location_t, tree, tree);
2148 void diagnose_constraint (location_t, tree, tree);
2150 /* Diagnose a conjunction of constraints. */
2151 void
2152 diagnose_logical_operation (location_t loc, tree t, tree args)
2154 diagnose_expression (loc, TREE_OPERAND (t, 0), args);
2155 diagnose_expression (loc, TREE_OPERAND (t, 0), args);
2158 /* Determine if the trait expression T is satisfied by ARGS.
2159 Emit a precise diagnostic if it is not. */
2160 void
2161 diagnose_trait_expression (location_t loc, tree t, tree args)
2163 if (check_constraint_expression (t, args))
2164 return;
2166 /* Rebuild the trait expression so we can diagnose the
2167 specific failure. */
2168 ++processing_template_decl;
2169 tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
2170 --processing_template_decl;
2172 tree t1 = TRAIT_EXPR_TYPE1 (expr);
2173 tree t2 = TRAIT_EXPR_TYPE2 (expr);
2174 switch (TRAIT_EXPR_KIND (t))
2176 case CPTK_HAS_NOTHROW_ASSIGN:
2177 inform (loc, " %qT is not nothrow copy assignable", t1);
2178 break;
2179 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
2180 inform (loc, " %qT is not nothrow default constructible", t1);
2181 break;
2182 case CPTK_HAS_NOTHROW_COPY:
2183 inform (loc, " %qT is not nothrow copy constructible", t1);
2184 break;
2185 case CPTK_HAS_TRIVIAL_ASSIGN:
2186 inform (loc, " %qT is not trivially copy assignable", t1);
2187 break;
2188 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
2189 inform (loc, " %qT is not trivially default constructible", t1);
2190 break;
2191 case CPTK_HAS_TRIVIAL_COPY:
2192 inform (loc, " %qT is not trivially copy constructible", t1);
2193 break;
2194 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
2195 inform (loc, " %qT is not trivially destructible", t1);
2196 break;
2197 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
2198 inform (loc, " %qT does not have a virtual destructor", t1);
2199 break;
2200 case CPTK_IS_ABSTRACT:
2201 inform (loc, " %qT is not an abstract class", t1);
2202 break;
2203 case CPTK_IS_BASE_OF:
2204 inform (loc, " %qT is not a base of %qT", t1, t2);
2205 break;
2206 case CPTK_IS_CLASS:
2207 inform (loc, " %qT is not a class", t1);
2208 break;
2209 case CPTK_IS_CONVERTIBLE_TO:
2210 inform (loc, " %qT is not convertible to %qT", t1, t2);
2211 break;
2212 case CPTK_IS_EMPTY:
2213 inform (loc, " %qT is not an empty class", t1);
2214 break;
2215 case CPTK_IS_ENUM:
2216 inform (loc, " %qT is not an enum", t1);
2217 break;
2218 case CPTK_IS_FINAL:
2219 inform (loc, " %qT is not a final class", t1);
2220 break;
2221 case CPTK_IS_LITERAL_TYPE:
2222 inform (loc, " %qT is not a literal type", t1);
2223 break;
2224 case CPTK_IS_POD:
2225 inform (loc, " %qT is not a POD type", t1);
2226 break;
2227 case CPTK_IS_POLYMORPHIC:
2228 inform (loc, " %qT is not a polymorphic type", t1);
2229 break;
2230 case CPTK_IS_SAME_AS:
2231 inform (loc, " %qT is not the same as %qT", t1, t2);
2232 break;
2233 case CPTK_IS_STD_LAYOUT:
2234 inform (loc, " %qT is not an standard layout type", t1);
2235 break;
2236 case CPTK_IS_TRIVIAL:
2237 inform (loc, " %qT is not a trivial type", t1);
2238 break;
2239 case CPTK_IS_UNION:
2240 inform (loc, " %qT is not a union", t1);
2241 break;
2242 default:
2243 gcc_unreachable ();
2247 /* Determine if the call expression T, when normalized as a constraint,
2248 is satisfied by ARGS.
2250 TODO: If T is refers to a concept, We could recursively analyze
2251 its definition to identify the exact failure, but that could
2252 emit a *lot* of error messages (defeating the purpose of
2253 improved diagnostics). Consider adding a flag to control the
2254 depth of diagnostics. */
2255 void
2256 diagnose_call_expression (location_t loc, tree t, tree args)
2258 if (check_constraint_expression (t, args))
2259 return;
2261 /* Rebuild the expression for the purpose of diagnostics. */
2262 ++processing_template_decl;
2263 tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
2264 --processing_template_decl;
2266 /* If the function call is known to be a concept check, then
2267 diagnose it differently (i.e., we may recurse). */
2268 if (resolve_constraint_check (t))
2269 inform (loc, " concept %qE was not satisfied", expr);
2270 else
2271 inform (loc, " %qE evaluated to false", expr);
2274 /* Determine if the template-id T, when normalized as a constraint
2275 is satisfied by ARGS. */
2276 void
2277 diagnose_template_id (location_t loc, tree t, tree args)
2279 /* Check for invalid template-ids. */
2280 if (!variable_template_p (TREE_OPERAND (t, 0)))
2282 inform (loc, " invalid constraint %qE", t);
2283 return;
2286 if (check_constraint_expression (t, args))
2287 return;
2289 /* Rebuild the expression for the purpose of diagnostics. */
2290 ++processing_template_decl;
2291 tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
2292 --processing_template_decl;
2294 tree var = DECL_TEMPLATE_RESULT (TREE_OPERAND (t, 0));
2295 if (DECL_DECLARED_CONCEPT_P (var))
2296 inform (loc, " concept %qE was not satisfied", expr);
2297 else
2298 inform (loc, " %qE evaluated to false", expr);
2301 /* Determine if the requires-expression, when normalized as a
2302 constraint is satisfied by ARGS.
2304 TODO: Build sets of expressions, types, and constraints
2305 based on the requirements in T and emit specific diagnostics
2306 for those. */
2307 void
2308 diagnose_requires_expression (location_t loc, tree t, tree args)
2310 if (check_constraint_expression (t, args))
2311 return;
2312 inform (loc, "requirements not satisfied");
2315 void
2316 diagnose_pack_expansion (location_t loc, tree t, tree args)
2318 if (check_constraint_expression (t, args))
2319 return;
2321 /* Make sure that we don't have naked packs that we don't expect. */
2322 if (!same_type_p (TREE_TYPE (t), boolean_type_node))
2324 inform (loc, "invalid pack expansion in constraint %qE", t);
2325 return;
2328 inform (loc, " in the expansion of %qE", t);
2330 /* Get the vector of expanded arguments. Note that n must not
2331 be 0 since this constraint is not satisfied. */
2332 ++processing_template_decl;
2333 tree exprs = tsubst_pack_expansion (t, args, tf_none, NULL_TREE);
2334 --processing_template_decl;
2335 if (exprs == error_mark_node)
2337 /* TODO: This error message could be better. */
2338 inform (loc, " substitution failure occurred during expansion");
2339 return;
2342 /* Check each expanded constraint separately. */
2343 int n = TREE_VEC_LENGTH (exprs);
2344 for (int i = 0; i < n; ++i)
2346 tree expr = TREE_VEC_ELT (exprs, i);
2347 if (!check_constraint_expression (expr, args))
2348 inform (loc, " %qE was not satisfied", expr);
2352 /* Diagnose an expression that would be characterized as
2353 a predicate constraint. */
2354 void
2355 diagnose_other_expression (location_t loc, tree t, tree args)
2357 if (check_constraint_expression (t, args))
2358 return;
2359 inform (loc, " %qE evaluated to false", t);
2362 void
2363 diagnose_expression (location_t loc, tree t, tree args)
2365 switch (TREE_CODE (t))
2367 case TRUTH_ANDIF_EXPR:
2368 diagnose_logical_operation (loc, t, args);
2369 break;
2371 case TRUTH_ORIF_EXPR:
2372 diagnose_logical_operation (loc, t, args);
2373 break;
2375 case CALL_EXPR:
2376 diagnose_call_expression (loc, t, args);
2377 break;
2379 case TEMPLATE_ID_EXPR:
2380 diagnose_template_id (loc, t, args);
2381 break;
2383 case REQUIRES_EXPR:
2384 diagnose_requires_expression (loc, t, args);
2385 break;
2387 case TRAIT_EXPR:
2388 diagnose_trait_expression (loc, t, args);
2389 break;
2391 case EXPR_PACK_EXPANSION:
2392 diagnose_pack_expansion (loc, t, args);
2393 break;
2395 default:
2396 diagnose_other_expression (loc, t, args);
2397 break;
2401 inline void
2402 diagnose_predicate_constraint (location_t loc, tree t, tree args)
2404 diagnose_expression (loc, PRED_CONSTR_EXPR (t), args);
2407 inline void
2408 diagnose_conjunction (location_t loc, tree t, tree args)
2410 diagnose_constraint (loc, TREE_OPERAND (t, 0), args);
2411 diagnose_constraint (loc, TREE_OPERAND (t, 1), args);
2414 /* Diagnose the constraint T for the given ARGS. This is only
2415 ever invoked on the associated constraints, so we can
2416 only have conjunctions of predicate constraints. */
2417 void
2418 diagnose_constraint (location_t loc, tree t, tree args)
2420 switch (TREE_CODE (t))
2422 case CONJ_CONSTR:
2423 diagnose_conjunction (loc, t, args);
2424 break;
2426 case PRED_CONSTR:
2427 diagnose_predicate_constraint (loc, t, args);
2428 break;
2430 default:
2431 gcc_unreachable ();
2432 break;
2436 } // namespace
2438 /* Emit diagnostics detailing the failure ARGS to satisfy the constraints
2439 of the template declaration, DECL. */
2440 void
2441 diagnose_constraints (location_t loc, tree decl, tree args)
2443 inform (loc, " constraints not satisfied");
2445 /* Constraints are attached to the template. */
2446 if (tree ti = DECL_TEMPLATE_INFO (decl)) {
2447 decl = TI_TEMPLATE (ti);
2448 args = TI_ARGS (ti);
2451 /* Check that the constraints are actually valid. */
2452 tree ci = get_constraints (decl);
2453 if (!valid_constraints_p (ci))
2455 inform (loc, " invalid constraints");
2456 return;
2459 /* Recursively diagnose the associated constraints. */
2460 diagnose_constraint (loc, CI_ASSOCIATED_CONSTRAINTS (ci), args);