1 /* Processing rules for constraints.
2 Copyright (C) 2013-2018 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)
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/>. */
23 #include "coretypes.h"
29 #include "double-int.h"
36 #include "stringpool.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"
47 #include "type-utils.h"
49 /*---------------------------------------------------------------------------
50 Operations on constraints
51 ---------------------------------------------------------------------------*/
53 /* Returns true if C is a constraint tree code. Note that ERROR_MARK
54 is a valid constraint. */
57 constraint_p (tree_code c
)
59 return ((PRED_CONSTR
<= c
&& c
<= DISJ_CONSTR
)
60 || c
== EXPR_PACK_EXPANSION
64 /* Returns true if T is a constraint. Note that error_mark_node
65 is a valid constraint. */
70 return constraint_p (TREE_CODE (t
));
73 /* Returns the conjunction of two constraints A and B. Note that
74 conjoining a non-null constraint with NULL_TREE is an identity
75 operation. That is, for non-null A,
77 conjoin_constraints(a, NULL_TREE) == a
81 conjoin_constraints (NULL_TREE, a) == a
83 If both A and B are NULL_TREE, the result is also NULL_TREE. */
86 conjoin_constraints (tree a
, tree b
)
88 gcc_assert (a
? constraint_p (a
) : true);
89 gcc_assert (b
? constraint_p (b
) : true);
91 return b
? build_nt (CONJ_CONSTR
, a
, b
) : a
;
98 /* Transform the vector of expressions in the T into a conjunction
99 of requirements. T must be a TREE_VEC. */
102 conjoin_constraints (tree t
)
104 gcc_assert (TREE_CODE (t
) == TREE_VEC
);
106 for (int i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
107 r
= conjoin_constraints (r
, TREE_VEC_ELT (t
, i
));
111 /* Returns true if T is a call expression to a function
115 function_concept_check_p (tree t
)
117 gcc_assert (TREE_CODE (t
) == CALL_EXPR
);
118 tree fn
= CALL_EXPR_FN (t
);
120 && TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
122 tree f1
= OVL_FIRST (TREE_OPERAND (fn
, 0));
123 if (TREE_CODE (f1
) == TEMPLATE_DECL
124 && DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (f1
)))
130 /* Returns true if any of the arguments in the template
131 argument list is a wildcard or wildcard pack. */
134 contains_wildcard_p (tree args
)
136 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
138 tree arg
= TREE_VEC_ELT (args
, i
);
139 if (TREE_CODE (arg
) == WILDCARD_DECL
)
145 /* Build a new call expression, but don't actually generate a
146 new function call. We just want the tree, not the semantics. */
149 build_call_check (tree id
)
151 ++processing_template_decl
;
152 vec
<tree
, va_gc
> *fargs
= make_tree_vector();
153 tree call
= finish_call_expr (id
, &fargs
, false, false, tf_none
);
154 release_tree_vector (fargs
);
155 --processing_template_decl
;
159 /* Build an expression that will check a variable concept. If any
160 argument contains a wildcard, don't try to finish the variable
161 template because we can't substitute into a non-existent
165 build_variable_check (tree id
)
167 gcc_assert (TREE_CODE (id
) == TEMPLATE_ID_EXPR
);
168 if (contains_wildcard_p (TREE_OPERAND (id
, 1)))
171 ++processing_template_decl
;
172 tree var
= finish_template_variable (id
);
173 --processing_template_decl
;
177 /*---------------------------------------------------------------------------
178 Resolution of qualified concept names
179 ---------------------------------------------------------------------------*/
181 /* This facility is used to resolve constraint checks from
182 requirement expressions. A constraint check is a call to
183 a function template declared with the keyword 'concept'.
185 The result of resolution is a pair (a TREE_LIST) whose value
186 is the matched declaration, and whose purpose contains the
187 coerced template arguments that can be substituted into the
190 // Given an overload set OVL, try to find a unique definition that can be
191 // instantiated by the template arguments ARGS.
193 // This function is not called for arbitrary call expressions. In particular,
194 // the call expression must be written with explicit template arguments
195 // and no function arguments. For example:
199 // If a single match is found, this returns a TREE_LIST whose VALUE
200 // is the constraint function (not the template), and its PURPOSE is
201 // the complete set of arguments substituted into the parameter list.
203 resolve_constraint_check (tree ovl
, tree args
)
206 tree cands
= NULL_TREE
;
207 for (lkp_iterator
iter (ovl
); iter
; ++iter
)
209 // Get the next template overload.
211 if (TREE_CODE (tmpl
) != TEMPLATE_DECL
)
214 // Don't try to deduce checks for non-concepts. We often
215 // end up trying to resolve constraints in functional casts
216 // as part of a postfix-expression. We can save time and
217 // headaches by not instantiating those declarations.
219 // NOTE: This masks a potential error, caused by instantiating
220 // non-deduced contexts using placeholder arguments.
221 tree fn
= DECL_TEMPLATE_RESULT (tmpl
);
222 if (DECL_ARGUMENTS (fn
))
224 if (!DECL_DECLARED_CONCEPT_P (fn
))
227 // Remember the candidate if we can deduce a substitution.
228 ++processing_template_decl
;
229 tree parms
= TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl
));
230 if (tree subst
= coerce_template_parms (parms
, args
, tmpl
))
232 if (subst
== error_mark_node
)
235 cands
= tree_cons (subst
, fn
, cands
);
237 --processing_template_decl
;
241 /* We either had no candidates or failed deductions. */
242 return nerrs
? error_mark_node
: NULL_TREE
;
243 else if (TREE_CHAIN (cands
))
244 /* There are multiple candidates. */
245 return error_mark_node
;
250 // Determine if the the call expression CALL is a constraint check, and
251 // return the concept declaration and arguments being checked. If CALL
252 // does not denote a constraint check, return NULL.
254 resolve_constraint_check (tree call
)
256 gcc_assert (TREE_CODE (call
) == CALL_EXPR
);
258 // A constraint check must be only a template-id expression. If
259 // it's a call to a base-link, its function(s) should be a
260 // template-id expression. If this is not a template-id, then it
261 // cannot be a concept-check.
262 tree target
= CALL_EXPR_FN (call
);
263 if (BASELINK_P (target
))
264 target
= BASELINK_FUNCTIONS (target
);
265 if (TREE_CODE (target
) != TEMPLATE_ID_EXPR
)
268 // Get the overload set and template arguments and try to
269 // resolve the target.
270 tree ovl
= TREE_OPERAND (target
, 0);
272 /* This is a function call of a variable concept... ill-formed. */
273 if (TREE_CODE (ovl
) == TEMPLATE_DECL
)
275 error_at (location_of (call
),
276 "function call of variable concept %qE", call
);
277 return error_mark_node
;
280 tree args
= TREE_OPERAND (target
, 1);
281 return resolve_constraint_check (ovl
, args
);
284 /* Returns a pair containing the checked variable concept
285 and its associated prototype parameter. The result
286 is a TREE_LIST whose TREE_VALUE is the variable concept
287 and whose TREE_PURPOSE is the prototype parameter. */
290 resolve_variable_concept_check (tree id
)
292 tree tmpl
= TREE_OPERAND (id
, 0);
293 tree args
= TREE_OPERAND (id
, 1);
295 if (!variable_concept_p (tmpl
))
298 /* Make sure that we have the right parameters before
299 assuming that it works. Note that failing to deduce
300 will result in diagnostics. */
301 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
302 ++processing_template_decl
;
303 tree result
= coerce_template_parms (parms
, args
, tmpl
);
304 --processing_template_decl
;
305 if (result
!= error_mark_node
)
307 tree decl
= DECL_TEMPLATE_RESULT (tmpl
);
308 return build_tree_list (result
, decl
);
311 return error_mark_node
;
315 /* Given a call expression or template-id expression to
316 a concept EXPR possibly including a wildcard, deduce
317 the concept being checked and the prototype parameter.
318 Returns true if the constraint and prototype can be
319 deduced and false otherwise. Note that the CHECK and
320 PROTO arguments are set to NULL_TREE if this returns
324 deduce_constrained_parameter (tree expr
, tree
& check
, tree
& proto
)
326 tree info
= NULL_TREE
;
327 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
328 info
= resolve_variable_concept_check (expr
);
329 else if (TREE_CODE (expr
) == CALL_EXPR
)
330 info
= resolve_constraint_check (expr
);
334 if (info
&& info
!= error_mark_node
)
336 check
= TREE_VALUE (info
);
337 tree arg
= TREE_VEC_ELT (TREE_PURPOSE (info
), 0);
338 if (ARGUMENT_PACK_P (arg
))
339 arg
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg
), 0);
340 proto
= TREE_TYPE (arg
);
343 check
= proto
= NULL_TREE
;
347 // Given a call expression or template-id expression to a concept, EXPR,
348 // deduce the concept being checked and return the template arguments.
349 // Returns NULL_TREE if deduction fails.
351 deduce_concept_introduction (tree expr
)
353 tree info
= NULL_TREE
;
354 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
355 info
= resolve_variable_concept_check (expr
);
356 else if (TREE_CODE (expr
) == CALL_EXPR
)
357 info
= resolve_constraint_check (expr
);
361 if (info
&& info
!= error_mark_node
)
362 return TREE_PURPOSE (info
);
368 /*---------------------------------------------------------------------------
369 Constraint implication learning
370 ---------------------------------------------------------------------------*/
372 /* The implication context determines how we memoize concept checks.
373 Given two checks C1 and C2, the direction of implication depends
374 on whether we are learning implications of a conjunction or disjunction.
377 template<typename T> concept bool C = ...;
378 template<typenaem T> concept bool D = C<T> && true;
380 From this, we can learn that D<T> implies C<T>. We cannot learn,
381 without further testing, that C<T> does not imply D<T>. If, for
382 example, C<T> were defined as true, then these constraints would
383 be logically equivalent.
385 In rare cases, we may start with a logical equivalence. For example:
387 template<typename T> concept bool C = ...;
388 template<typename T> concept bool D = C<T>;
390 Here, we learn that C<T> implies D<T> and vice versa. */
392 enum implication_context
394 conjunction_cxt
, /* C1 implies C2. */
395 disjunction_cxt
, /* C2 implies C1. */
396 equivalence_cxt
/* C1 implies C2, C2 implies C1. */
399 void learn_implications(tree
, tree
, implication_context
);
402 learn_implication (tree parent
, tree child
, implication_context cxt
)
406 case conjunction_cxt
:
407 save_subsumption_result (parent
, child
, true);
409 case disjunction_cxt
:
410 save_subsumption_result (child
, parent
, true);
412 case equivalence_cxt
:
413 save_subsumption_result (parent
, child
, true);
414 save_subsumption_result (child
, parent
, true);
420 learn_logical_operation (tree parent
, tree constr
, implication_context cxt
)
422 learn_implications (parent
, TREE_OPERAND (constr
, 0), cxt
);
423 learn_implications (parent
, TREE_OPERAND (constr
, 1), cxt
);
427 learn_implications (tree parent
, tree constr
, implication_context cxt
)
429 switch (TREE_CODE (constr
))
432 return learn_implication (parent
, constr
, cxt
);
435 if (cxt
== disjunction_cxt
)
437 return learn_logical_operation (parent
, constr
, cxt
);
440 if (cxt
== conjunction_cxt
)
442 return learn_logical_operation (parent
, constr
, cxt
);
449 /* Quickly scan the top-level constraints of CONSTR to learn and
450 cache logical relations between concepts. The search does not
451 include conjunctions of disjunctions or vice versa. */
454 learn_implications (tree tmpl
, tree args
, tree constr
)
456 /* Don't memoize relations between non-dependent arguemnts. It's not
458 if (!uses_template_parms (args
))
461 /* Build a check constraint for the purpose of caching. */
462 tree parent
= build_nt (CHECK_CONSTR
, tmpl
, args
);
464 /* Start learning based on the kind of the top-level contraint. */
465 if (TREE_CODE (constr
) == CONJ_CONSTR
)
466 return learn_logical_operation (parent
, constr
, conjunction_cxt
);
467 else if (TREE_CODE (constr
) == DISJ_CONSTR
)
468 return learn_logical_operation (parent
, constr
, disjunction_cxt
);
469 else if (TREE_CODE (constr
) == CHECK_CONSTR
)
470 /* This is the rare concept alias case. */
471 return learn_implication (parent
, constr
, equivalence_cxt
);
474 /*---------------------------------------------------------------------------
475 Expansion of concept definitions
476 ---------------------------------------------------------------------------*/
478 /* Returns the expression of a function concept. */
481 get_returned_expression (tree fn
)
483 /* Extract the body of the function minus the return expression. */
484 tree body
= DECL_SAVED_TREE (fn
);
486 return error_mark_node
;
487 if (TREE_CODE (body
) == BIND_EXPR
)
488 body
= BIND_EXPR_BODY (body
);
489 if (TREE_CODE (body
) != RETURN_EXPR
)
490 return error_mark_node
;
492 return TREE_OPERAND (body
, 0);
495 /* Returns the initializer of a variable concept. */
498 get_variable_initializer (tree var
)
500 tree init
= DECL_INITIAL (var
);
502 return error_mark_node
;
506 /* Returns the definition of a variable or function concept. */
509 get_concept_definition (tree decl
)
512 return get_variable_initializer (decl
);
513 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
514 return get_returned_expression (decl
);
518 int expansion_level
= 0;
520 struct expanding_concept_sentinel
522 expanding_concept_sentinel ()
527 ~expanding_concept_sentinel()
536 /* Returns true when a concept is being expanded. */
541 return expansion_level
> 0;
544 /* Expand a concept declaration (not a template) and its arguments to
545 a constraint defined by the concept's initializer or definition. */
548 expand_concept (tree decl
, tree args
)
550 expanding_concept_sentinel sentinel
;
552 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
553 decl
= DECL_TEMPLATE_RESULT (decl
);
554 tree tmpl
= DECL_TI_TEMPLATE (decl
);
556 /* Check for a previous specialization. */
557 if (tree spec
= get_concept_expansion (tmpl
, args
))
560 /* Substitute the arguments to form a new definition expression. */
561 tree def
= get_concept_definition (decl
);
563 ++processing_template_decl
;
564 tree result
= tsubst_expr (def
, args
, tf_none
, NULL_TREE
, true);
565 --processing_template_decl
;
566 if (result
== error_mark_node
)
567 return error_mark_node
;
569 /* And lastly, normalize it, check for implications, and save
570 the specialization for later. */
571 tree norm
= normalize_expression (result
);
572 learn_implications (tmpl
, args
, norm
);
573 return save_concept_expansion (tmpl
, args
, norm
);
577 /*---------------------------------------------------------------------------
578 Stepwise normalization of expressions
580 This set of functions will transform an expression into a constraint
581 in a sequence of steps. Normalization does not not look into concept
583 ---------------------------------------------------------------------------*/
585 /* Transform a logical-or or logical-and expression into either
586 a conjunction or disjunction. */
589 normalize_logical_operation (tree t
, tree_code c
)
591 tree t0
= normalize_expression (TREE_OPERAND (t
, 0));
592 tree t1
= normalize_expression (TREE_OPERAND (t
, 1));
593 return build_nt (c
, t0
, t1
);
596 /* A simple requirement T introduces an expression constraint
597 for its expression. */
600 normalize_simple_requirement (tree t
)
602 return build_nt (EXPR_CONSTR
, TREE_OPERAND (t
, 0));
605 /* A type requirement T introduce a type constraint for its type. */
608 normalize_type_requirement (tree t
)
610 return build_nt (TYPE_CONSTR
, TREE_OPERAND (t
, 0));
613 /* A compound requirement T introduces a conjunction of constraints
614 depending on its form. The conjunction always includes an
615 expression constraint for the expression of the requirement.
616 If a trailing return type was specified, the conjunction includes
617 either an implicit conversion constraint or an argument deduction
618 constraint. If the noexcept specifier is present, the conjunction
619 includes an exception constraint. */
622 normalize_compound_requirement (tree t
)
624 tree expr
= TREE_OPERAND (t
, 0);
625 tree constr
= build_nt (EXPR_CONSTR
, TREE_OPERAND (t
, 0));
627 /* If a type is given, append an implicit conversion or
628 argument deduction constraint. */
629 if (tree type
= TREE_OPERAND (t
, 1))
632 /* TODO: We should be extracting a list of auto nodes
633 from type_uses_auto, not a single node */
634 if (tree placeholder
= type_uses_auto (type
))
635 type_constr
= build_nt (DEDUCT_CONSTR
, expr
, type
, placeholder
);
637 type_constr
= build_nt (ICONV_CONSTR
, expr
, type
);
638 constr
= conjoin_constraints (constr
, type_constr
);
641 /* If noexcept is present, append an exception constraint. */
642 if (COMPOUND_REQ_NOEXCEPT_P (t
))
644 tree except
= build_nt (EXCEPT_CONSTR
, expr
);
645 constr
= conjoin_constraints (constr
, except
);
651 /* A nested requirement T introduces a conjunction of constraints
652 corresponding to its constraint-expression.
654 If the result of transforming T is error_mark_node, the resulting
655 constraint is a predicate constraint whose operand is also
656 error_mark_node. This preserves the constraint structure, but
657 will guarantee that the constraint is never satisfied. */
660 normalize_nested_requirement (tree t
)
662 return normalize_expression (TREE_OPERAND (t
, 0));
665 /* Transform a requirement T into one or more constraints. */
668 normalize_requirement (tree t
)
670 switch (TREE_CODE (t
))
673 return normalize_simple_requirement (t
);
676 return normalize_type_requirement (t
);
679 return normalize_compound_requirement (t
);
682 return normalize_nested_requirement (t
);
687 return error_mark_node
;
690 /* Transform a sequence of requirements into a conjunction of
694 normalize_requirements (tree t
)
696 tree result
= NULL_TREE
;
697 for (; t
; t
= TREE_CHAIN (t
))
699 tree constr
= normalize_requirement (TREE_VALUE (t
));
700 result
= conjoin_constraints (result
, constr
);
705 /* The normal form of a requires-expression is a parameterized
706 constraint having the same parameters and a conjunction of
707 constraints representing the normal form of requirements. */
710 normalize_requires_expression (tree t
)
712 tree operand
= normalize_requirements (TREE_OPERAND (t
, 1));
713 if (tree parms
= TREE_OPERAND (t
, 0))
714 return build_nt (PARM_CONSTR
, parms
, operand
);
719 /* For a template-id referring to a variable concept, returns
720 a check constraint. Otherwise, returns a predicate constraint. */
723 normalize_template_id_expression (tree t
)
725 if (tree info
= resolve_variable_concept_check (t
))
727 if (info
== error_mark_node
)
729 /* We get this when the template arguments don't match
730 the variable concept. */
731 error ("invalid reference to concept %qE", t
);
732 return error_mark_node
;
735 tree decl
= TREE_VALUE (info
);
736 tree args
= TREE_PURPOSE (info
);
737 return build_nt (CHECK_CONSTR
, decl
, args
);
740 /* Check that we didn't refer to a function concept like a variable. */
741 tree fn
= OVL_FIRST (TREE_OPERAND (t
, 0));
742 if (TREE_CODE (fn
) == TEMPLATE_DECL
743 && DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (fn
)))
745 error_at (location_of (t
),
746 "invalid reference to function concept %qD", fn
);
747 return error_mark_node
;
750 return build_nt (PRED_CONSTR
, t
);
753 /* For a call expression to a function concept, returns a check
754 constraint. Otherwise, returns a predicate constraint. */
757 normalize_call_expression (tree t
)
759 /* Try to resolve this function call as a concept. If not, then
760 it can be returned as a predicate constraint. */
761 tree check
= resolve_constraint_check (t
);
763 return build_nt (PRED_CONSTR
, t
);
764 if (check
== error_mark_node
)
766 /* TODO: Improve diagnostics. We could report why the reference
768 error ("invalid reference to concept %qE", t
);
769 return error_mark_node
;
772 tree fn
= TREE_VALUE (check
);
773 tree args
= TREE_PURPOSE (check
);
774 return build_nt (CHECK_CONSTR
, fn
, args
);
777 /* If T is a call to an overloaded && or || operator, diagnose that
778 as a non-SFINAEable error. Returns true if an error is emitted.
780 TODO: It would be better to diagnose this at the point of definition,
781 if possible. Perhaps we should immediately do a first-pass normalization
782 of a concept definition to catch obvious non-dependent errors like
786 check_for_logical_overloads (tree t
)
788 if (TREE_CODE (t
) != CALL_EXPR
)
791 tree fn
= CALL_EXPR_FN (t
);
793 /* For member calls, try extracting the function from the
795 if (TREE_CODE (fn
) == COMPONENT_REF
)
797 fn
= TREE_OPERAND (fn
, 1);
798 if (TREE_CODE (fn
) == BASELINK
)
799 fn
= BASELINK_FUNCTIONS (fn
);
802 if (TREE_CODE (fn
) != FUNCTION_DECL
)
805 if (DECL_OVERLOADED_OPERATOR_P (fn
))
807 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
808 error_at (loc
, "constraint %qE, uses overloaded operator", t
);
815 /* The normal form of an atom depends on the expression. The normal
816 form of a function call to a function concept is a check constraint
817 for that concept. The normal form of a reference to a variable
818 concept is a check constraint for that concept. Otherwise, the
819 constraint is a predicate constraint. */
822 normalize_atom (tree t
)
824 /* We can get constraints pushed down through pack expansions, so
826 if (constraint_p (t
))
829 tree type
= TREE_TYPE (t
);
830 if (!type
|| type_unknown_p (t
) || TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
832 else if (!dependent_type_p (type
))
834 if (check_for_logical_overloads (t
))
835 return error_mark_node
;
837 type
= cv_unqualified (type
);
838 if (!same_type_p (type
, boolean_type_node
))
840 error ("predicate constraint %q+E does not have type %<bool%>", t
);
841 return error_mark_node
;
845 if (TREE_CODE (t
) == TEMPLATE_ID_EXPR
)
846 return normalize_template_id_expression (t
);
847 if (TREE_CODE (t
) == CALL_EXPR
)
848 return normalize_call_expression (t
);
849 return build_nt (PRED_CONSTR
, t
);
852 /* Push down the pack expansion EXP into the leaves of the constraint PAT. */
855 push_down_pack_expansion (tree exp
, tree pat
)
857 switch (TREE_CODE (pat
))
862 pat
= copy_node (pat
);
863 TREE_OPERAND (pat
, 0)
864 = push_down_pack_expansion (exp
, TREE_OPERAND (pat
, 0));
865 TREE_OPERAND (pat
, 1)
866 = push_down_pack_expansion (exp
, TREE_OPERAND (pat
, 1));
871 exp
= copy_node (exp
);
872 SET_PACK_EXPANSION_PATTERN (exp
, pat
);
878 /* Transform a pack expansion into a constraint. First we transform the
879 pattern of the pack expansion, then we push the pack expansion down into the
880 leaves of the constraint so that partial ordering will work. */
883 normalize_pack_expansion (tree t
)
885 tree pat
= normalize_expression (PACK_EXPANSION_PATTERN (t
));
886 return push_down_pack_expansion (t
, pat
);
889 /* Transform an expression into a constraint. */
892 normalize_any_expression (tree t
)
894 switch (TREE_CODE (t
))
896 case TRUTH_ANDIF_EXPR
:
897 return normalize_logical_operation (t
, CONJ_CONSTR
);
899 case TRUTH_ORIF_EXPR
:
900 return normalize_logical_operation (t
, DISJ_CONSTR
);
903 return normalize_requires_expression (t
);
906 return normalize_expression (BIND_EXPR_BODY (t
));
908 case EXPR_PACK_EXPANSION
:
909 return normalize_pack_expansion (t
);
912 /* All other constraints are atomic. */
913 return normalize_atom (t
);
917 /* Transform a statement into an expression. */
919 normalize_any_statement (tree t
)
921 switch (TREE_CODE (t
))
924 return normalize_expression (TREE_OPERAND (t
, 0));
928 return error_mark_node
;
931 /* Reduction rules for the declaration T. */
934 normalize_any_declaration (tree t
)
936 switch (TREE_CODE (t
))
939 return normalize_atom (t
);
943 return error_mark_node
;
946 /* Returns the normal form of a constraint expression. */
949 normalize_expression (tree t
)
954 if (t
== error_mark_node
)
955 return error_mark_node
;
957 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
963 return normalize_any_expression (t
);
966 return normalize_any_statement (t
);
968 case tcc_declaration
:
969 return normalize_any_declaration (t
);
971 case tcc_exceptional
:
975 /* These are all atomic predicate constraints. */
976 return normalize_atom (t
);
979 /* Unhandled node kind. */
982 return error_mark_node
;
986 /*---------------------------------------------------------------------------
987 Constraint normalization
988 ---------------------------------------------------------------------------*/
990 tree
normalize_constraint (tree
);
992 /* The normal form of the disjunction T0 /\ T1 is the conjunction
993 of the normal form of T0 and the normal form of T1. */
996 normalize_conjunction (tree t
)
998 tree t0
= normalize_constraint (TREE_OPERAND (t
, 0));
999 tree t1
= normalize_constraint (TREE_OPERAND (t
, 1));
1000 return build_nt (CONJ_CONSTR
, t0
, t1
);
1003 /* The normal form of the disjunction T0 \/ T1 is the disjunction
1004 of the normal form of T0 and the normal form of T1. */
1007 normalize_disjunction (tree t
)
1009 tree t0
= normalize_constraint (TREE_OPERAND (t
, 0));
1010 tree t1
= normalize_constraint (TREE_OPERAND (t
, 1));
1011 return build_nt (DISJ_CONSTR
, t0
, t1
);
1014 /* A predicate constraint is normalized in two stages. First all
1015 references specializations of concepts are replaced by their
1016 substituted definitions. Then, the resulting expression is
1017 transformed into a constraint by transforming && expressions
1018 into conjunctions and || into disjunctions. */
1021 normalize_predicate_constraint (tree t
)
1023 ++processing_template_decl
;
1024 tree expr
= PRED_CONSTR_EXPR (t
);
1025 tree constr
= normalize_expression (expr
);
1026 --processing_template_decl
;
1030 /* The normal form of a parameterized constraint is the normal
1031 form of its operand. */
1034 normalize_parameterized_constraint (tree t
)
1036 tree parms
= PARM_CONSTR_PARMS (t
);
1037 tree operand
= normalize_constraint (PARM_CONSTR_OPERAND (t
));
1038 return build_nt (PARM_CONSTR
, parms
, operand
);
1041 /* Normalize the constraint T by reducing it so that it is
1042 comprised of only conjunctions and disjunctions of atomic
1046 normalize_constraint (tree t
)
1051 if (t
== error_mark_node
)
1054 switch (TREE_CODE (t
))
1057 return normalize_conjunction (t
);
1060 return normalize_disjunction (t
);
1063 return normalize_predicate_constraint (t
);
1066 return normalize_parameterized_constraint (t
);
1073 /* These constraints are defined to be atomic. */
1077 /* CONSTR was not a constraint. */
1080 return error_mark_node
;
1085 // -------------------------------------------------------------------------- //
1086 // Constraint Semantic Processing
1088 // The following functions are called by the parser and substitution rules
1089 // to create and evaluate constraint-related nodes.
1091 // The constraints associated with the current template parameters.
1093 current_template_constraints (void)
1095 if (!current_template_parms
)
1097 tree tmpl_constr
= TEMPLATE_PARM_CONSTRAINTS (current_template_parms
);
1098 return build_constraints (tmpl_constr
, NULL_TREE
);
1101 // If the recently parsed TYPE declares or defines a template or template
1102 // specialization, get its corresponding constraints from the current
1103 // template parameters and bind them to TYPE's declaration.
1105 associate_classtype_constraints (tree type
)
1107 if (!type
|| type
== error_mark_node
|| TREE_CODE (type
) != RECORD_TYPE
)
1110 // An explicit class template specialization has no template
1112 if (!current_template_parms
)
1115 if (CLASSTYPE_IS_TEMPLATE (type
) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1117 tree decl
= TYPE_STUB_DECL (type
);
1118 tree ci
= current_template_constraints ();
1120 // An implicitly instantiated member template declaration already
1121 // has associated constraints. If it is defined outside of its
1122 // class, then we need match these constraints against those of
1123 // original declaration.
1124 if (tree orig_ci
= get_constraints (decl
))
1126 if (!equivalent_constraints (ci
, orig_ci
))
1128 // FIXME: Improve diagnostics.
1129 error ("%qT does not match any declaration", type
);
1130 return error_mark_node
;
1134 set_constraints (decl
, ci
);
1141 // Create an empty constraint info block.
1142 inline tree_constraint_info
*
1143 build_constraint_info ()
1145 return (tree_constraint_info
*)make_node (CONSTRAINT_INFO
);
1150 /* Build a constraint-info object that contains the associated constraints
1151 of a declaration. This also includes the declaration's template
1152 requirements (TREQS) and any trailing requirements for a function
1153 declarator (DREQS). Note that both TREQS and DREQS must be constraints.
1155 If the declaration has neither template nor declaration requirements
1156 this returns NULL_TREE, indicating an unconstrained declaration. */
1159 build_constraints (tree tmpl_reqs
, tree decl_reqs
)
1161 gcc_assert (tmpl_reqs
? constraint_p (tmpl_reqs
) : true);
1162 gcc_assert (decl_reqs
? constraint_p (decl_reqs
) : true);
1164 if (!tmpl_reqs
&& !decl_reqs
)
1167 tree_constraint_info
* ci
= build_constraint_info ();
1168 ci
->template_reqs
= tmpl_reqs
;
1169 ci
->declarator_reqs
= decl_reqs
;
1170 ci
->associated_constr
= conjoin_constraints (tmpl_reqs
, decl_reqs
);
1177 /* Construct a sequence of template arguments by prepending
1178 ARG to REST. Either ARG or REST may be null. */
1180 build_concept_check_arguments (tree arg
, tree rest
)
1182 gcc_assert (rest
? TREE_CODE (rest
) == TREE_VEC
: true);
1186 int n
= rest
? TREE_VEC_LENGTH (rest
) : 0;
1187 args
= make_tree_vec (n
+ 1);
1188 TREE_VEC_ELT (args
, 0) = arg
;
1190 for (int i
= 0; i
< n
; ++i
)
1191 TREE_VEC_ELT (args
, i
+ 1) = TREE_VEC_ELT (rest
, i
);
1192 int def
= rest
? GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (rest
) : 0;
1193 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
, def
+ 1);
1197 gcc_assert (rest
!= NULL_TREE
);
1205 /* Construct an expression that checks the concept given by
1206 TARGET. The TARGET must be:
1208 - an OVERLOAD referring to one or more function concepts
1209 - a BASELINK referring to an overload set of the above, or
1210 - a TEMPLTATE_DECL referring to a variable concept.
1212 ARG and REST are the explicit template arguments for the
1213 eventual concept check. */
1215 build_concept_check (tree target
, tree arg
, tree rest
)
1217 tree args
= build_concept_check_arguments (arg
, rest
);
1218 if (variable_template_p (target
))
1219 return build_variable_check (lookup_template_variable (target
, args
));
1221 return build_call_check (lookup_template_function (target
, args
));
1225 /* Returns a TYPE_DECL that contains sufficient information to
1226 build a template parameter of the same kind as PROTO and
1227 constrained by the concept declaration CNC. Note that PROTO
1228 is the first template parameter of CNC.
1230 If specified, ARGS provides additional arguments to the
1231 constraint check. */
1233 build_constrained_parameter (tree cnc
, tree proto
, tree args
)
1235 tree name
= DECL_NAME (cnc
);
1236 tree type
= TREE_TYPE (proto
);
1237 tree decl
= build_decl (input_location
, TYPE_DECL
, name
, type
);
1238 CONSTRAINED_PARM_PROTOTYPE (decl
) = proto
;
1239 CONSTRAINED_PARM_CONCEPT (decl
) = cnc
;
1240 CONSTRAINED_PARM_EXTRA_ARGS (decl
) = args
;
1244 /* Create a constraint expression for the given DECL that
1245 evaluates the requirements specified by CONSTR, a TYPE_DECL
1246 that contains all the information necessary to build the
1247 requirements (see finish_concept_name for the layout of
1250 Note that the constraints are neither reduced nor decomposed.
1251 That is done only after the requires clause has been parsed
1254 This will always return a CHECK_CONSTR. */
1256 finish_shorthand_constraint (tree decl
, tree constr
)
1258 /* No requirements means no constraints. */
1262 tree proto
= CONSTRAINED_PARM_PROTOTYPE (constr
);
1263 tree con
= CONSTRAINED_PARM_CONCEPT (constr
);
1264 tree args
= CONSTRAINED_PARM_EXTRA_ARGS (constr
);
1266 /* If the parameter declaration is variadic, but the concept
1267 is not then we need to apply the concept to every element
1269 bool is_proto_pack
= template_parameter_pack_p (proto
);
1270 bool is_decl_pack
= template_parameter_pack_p (decl
);
1271 bool apply_to_all_p
= is_decl_pack
&& !is_proto_pack
;
1273 /* Get the argument and overload used for the requirement
1274 and adjust it if we're going to expand later. */
1275 tree arg
= template_parm_to_arg (build_tree_list (NULL_TREE
, decl
));
1277 arg
= PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg
), 0));
1279 /* Build the concept check. If it the constraint needs to be
1280 applied to all elements of the parameter pack, then make
1281 the constraint an expansion. */
1282 tree tmpl
= DECL_TI_TEMPLATE (con
);
1283 tree check
= VAR_P (con
) ? tmpl
: ovl_make (tmpl
);
1284 check
= build_concept_check (check
, arg
, args
);
1286 /* Make the check a pack expansion if needed.
1288 FIXME: We should be making a fold expression. */
1291 check
= make_pack_expansion (check
);
1292 TREE_TYPE (check
) = boolean_type_node
;
1295 return normalize_expression (check
);
1298 /* Returns a conjunction of shorthand requirements for the template
1299 parameter list PARMS. Note that the requirements are stored in
1300 the TYPE of each tree node. */
1302 get_shorthand_constraints (tree parms
)
1304 tree result
= NULL_TREE
;
1305 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
1306 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
1308 tree parm
= TREE_VEC_ELT (parms
, i
);
1309 tree constr
= TEMPLATE_PARM_CONSTRAINTS (parm
);
1310 result
= conjoin_constraints (result
, constr
);
1315 // Returns and chains a new parameter for PARAMETER_LIST which will conform
1316 // to the prototype given by SRC_PARM. The new parameter will have its
1317 // identifier and location set according to IDENT and PARM_LOC respectively.
1319 process_introduction_parm (tree parameter_list
, tree src_parm
)
1321 // If we have a pack, we should have a single pack argument which is the
1322 // placeholder we want to look at.
1323 bool is_parameter_pack
= ARGUMENT_PACK_P (src_parm
);
1324 if (is_parameter_pack
)
1325 src_parm
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (src_parm
), 0);
1327 // At this point we should have a wildcard, but we want to
1328 // grab the associated decl from it. Also grab the stored
1329 // identifier and location that should be chained to it in
1331 gcc_assert (TREE_CODE (src_parm
) == WILDCARD_DECL
);
1333 tree ident
= DECL_NAME (src_parm
);
1334 location_t parm_loc
= DECL_SOURCE_LOCATION (src_parm
);
1336 // If we expect a pack and the deduced template is not a pack, or if the
1337 // template is using a pack and we didn't declare a pack, throw an error.
1338 if (is_parameter_pack
!= WILDCARD_PACK_P (src_parm
))
1340 error_at (parm_loc
, "cannot match pack for introduced parameter");
1341 tree err_parm
= build_tree_list (error_mark_node
, error_mark_node
);
1342 return chainon (parameter_list
, err_parm
);
1345 src_parm
= TREE_TYPE (src_parm
);
1349 if (TREE_CODE (src_parm
) == TYPE_DECL
)
1351 is_non_type
= false;
1352 parm
= finish_template_type_parm (class_type_node
, ident
);
1354 else if (TREE_CODE (src_parm
) == TEMPLATE_DECL
)
1356 is_non_type
= false;
1357 begin_template_parm_list ();
1358 current_template_parms
= DECL_TEMPLATE_PARMS (src_parm
);
1359 end_template_parm_list ();
1360 parm
= finish_template_template_parm (class_type_node
, ident
);
1366 // Since we don't have a declarator, so we can copy the source
1367 // parameter and change the name and eventually the location.
1368 parm
= copy_decl (src_parm
);
1369 DECL_NAME (parm
) = ident
;
1372 // Wrap in a TREE_LIST for process_template_parm. Introductions do not
1373 // retain the defaults from the source template.
1374 parm
= build_tree_list (NULL_TREE
, parm
);
1376 return process_template_parm (parameter_list
, parm_loc
, parm
,
1377 is_non_type
, is_parameter_pack
);
1380 /* Associates a constraint check to the current template based
1381 on the introduction parameters. INTRO_LIST must be a TREE_VEC
1382 of WILDCARD_DECLs containing a chained PARM_DECL which
1383 contains the identifier as well as the source location.
1384 TMPL_DECL is the decl for the concept being used. If we
1385 take a concept, C, this will form a check in the form of
1386 C<INTRO_LIST> filling in any extra arguments needed by the
1389 Returns NULL_TREE if no concept could be matched and
1390 error_mark_node if an error occurred when matching. */
1392 finish_template_introduction (tree tmpl_decl
, tree intro_list
)
1394 /* Deduce the concept check. */
1395 tree expr
= build_concept_check (tmpl_decl
, NULL_TREE
, intro_list
);
1396 if (expr
== error_mark_node
)
1399 tree parms
= deduce_concept_introduction (expr
);
1403 /* Build template parameter scope for introduction. */
1404 tree parm_list
= NULL_TREE
;
1405 begin_template_parm_list ();
1406 int nargs
= MIN (TREE_VEC_LENGTH (parms
), TREE_VEC_LENGTH (intro_list
));
1407 for (int n
= 0; n
< nargs
; ++n
)
1408 parm_list
= process_introduction_parm (parm_list
, TREE_VEC_ELT (parms
, n
));
1409 parm_list
= end_template_parm_list (parm_list
);
1410 for (int i
= 0; i
< TREE_VEC_LENGTH (parm_list
); ++i
)
1411 if (TREE_VALUE (TREE_VEC_ELT (parm_list
, i
)) == error_mark_node
)
1413 end_template_decl ();
1414 return error_mark_node
;
1417 /* Build a concept check for our constraint. */
1418 tree check_args
= make_tree_vec (TREE_VEC_LENGTH (parms
));
1420 for (; n
< TREE_VEC_LENGTH (parm_list
); ++n
)
1422 tree parm
= TREE_VEC_ELT (parm_list
, n
);
1423 TREE_VEC_ELT (check_args
, n
) = template_parm_to_arg (parm
);
1425 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (check_args
, n
);
1427 /* If the template expects more parameters we should be able
1428 to use the defaults from our deduced concept. */
1429 for (; n
< TREE_VEC_LENGTH (parms
); ++n
)
1430 TREE_VEC_ELT (check_args
, n
) = TREE_VEC_ELT (parms
, n
);
1432 /* Associate the constraint. */
1433 tree check
= build_concept_check (tmpl_decl
, NULL_TREE
, check_args
);
1434 tree constr
= normalize_expression (check
);
1435 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = constr
;
1441 /* Given the predicate constraint T from a constrained-type-specifier, extract
1442 its TMPL and ARGS. FIXME why do we need two different forms of
1443 constrained-type-specifier? */
1446 placeholder_extract_concept_and_args (tree t
, tree
&tmpl
, tree
&args
)
1448 if (TREE_CODE (t
) == TYPE_DECL
)
1450 /* A constrained parameter. Build a constraint check
1451 based on the prototype parameter and then extract the
1452 arguments from that. */
1453 tree proto
= CONSTRAINED_PARM_PROTOTYPE (t
);
1454 tree check
= finish_shorthand_constraint (proto
, t
);
1455 placeholder_extract_concept_and_args (check
, tmpl
, args
);
1459 if (TREE_CODE (t
) == CHECK_CONSTR
)
1461 tree decl
= CHECK_CONSTR_CONCEPT (t
);
1462 tmpl
= DECL_TI_TEMPLATE (decl
);
1463 args
= CHECK_CONSTR_ARGS (t
);
1470 /* Returns true iff the placeholders C1 and C2 are equivalent. C1
1471 and C2 can be either CHECK_CONSTR or TEMPLATE_TYPE_PARM. */
1474 equivalent_placeholder_constraints (tree c1
, tree c2
)
1476 if (c1
&& TREE_CODE (c1
) == TEMPLATE_TYPE_PARM
)
1477 /* A constrained auto. */
1478 c1
= PLACEHOLDER_TYPE_CONSTRAINTS (c1
);
1479 if (c2
&& TREE_CODE (c2
) == TEMPLATE_TYPE_PARM
)
1480 c2
= PLACEHOLDER_TYPE_CONSTRAINTS (c2
);
1486 if (c1
== error_mark_node
|| c2
== error_mark_node
)
1487 /* We get here during satisfaction; when a deduction constraint
1488 fails, substitution can produce an error_mark_node for the
1489 placeholder constraints. */
1492 tree t1
, t2
, a1
, a2
;
1493 placeholder_extract_concept_and_args (c1
, t1
, a1
);
1494 placeholder_extract_concept_and_args (c2
, t2
, a2
);
1499 int len1
= TREE_VEC_LENGTH (a1
);
1500 int len2
= TREE_VEC_LENGTH (a2
);
1504 /* Skip the first argument so we don't infinitely recurse.
1505 Also, they may differ in template parameter index. */
1506 for (int i
= 1; i
< len1
; ++i
)
1508 tree t1
= TREE_VEC_ELT (a1
, i
);
1509 tree t2
= TREE_VEC_ELT (a2
, i
);
1510 if (!template_args_equal (t1
, t2
))
1516 /* Return a hash value for the placeholder PRED_CONSTR C. */
1519 hash_placeholder_constraint (tree c
)
1522 placeholder_extract_concept_and_args (c
, t
, a
);
1524 /* Like hash_tmpl_and_args, but skip the first argument. */
1525 hashval_t val
= iterative_hash_object (DECL_UID (t
), 0);
1527 for (int i
= TREE_VEC_LENGTH (a
)-1; i
> 0; --i
)
1528 val
= iterative_hash_template_arg (TREE_VEC_ELT (a
, i
), val
);
1533 /*---------------------------------------------------------------------------
1534 Constraint substitution
1535 ---------------------------------------------------------------------------*/
1537 /* The following functions implement substitution rules for constraints.
1538 Substitution without checking constraints happens only in the
1539 instantiation of class templates. For example:
1541 template<C1 T> struct S {
1542 void f(T) requires C2<T>;
1543 void g(T) requires T::value;
1546 S<int> s; // error instantiating S<int>::g(T)
1548 When we instantiate S, we substitute into its member declarations,
1549 including their constraints. However, those constraints are not
1550 checked. Substituting int into C2<T> yields C2<int>, and substituting
1551 into T::value yields a substitution failure, making the program
1554 Note that we only ever substitute into the associated constraints
1555 of a declaration. That is, substitution is defined only for predicate
1556 constraints and conjunctions. */
1558 /* Substitute into the predicate constraints. Returns error_mark_node
1559 if the substitution into the expression fails. */
1561 tsubst_predicate_constraint (tree t
, tree args
,
1562 tsubst_flags_t complain
, tree in_decl
)
1564 tree expr
= PRED_CONSTR_EXPR (t
);
1565 ++processing_template_decl
;
1566 tree result
= tsubst_expr (expr
, args
, complain
, in_decl
, false);
1567 --processing_template_decl
;
1568 return build_nt (PRED_CONSTR
, result
);
1571 /* Substitute into a check constraint. */
1574 tsubst_check_constraint (tree t
, tree args
,
1575 tsubst_flags_t complain
, tree in_decl
)
1577 tree decl
= CHECK_CONSTR_CONCEPT (t
);
1578 tree tmpl
= DECL_TI_TEMPLATE (decl
);
1579 tree targs
= CHECK_CONSTR_ARGS (t
);
1581 /* Substitute through by building an template-id expression
1582 and then substituting into that. */
1583 tree expr
= build_nt (TEMPLATE_ID_EXPR
, tmpl
, targs
);
1584 ++processing_template_decl
;
1585 tree result
= tsubst_expr (expr
, args
, complain
, in_decl
, false);
1586 --processing_template_decl
;
1588 if (result
== error_mark_node
)
1589 return error_mark_node
;
1591 /* Extract the results and rebuild the check constraint. */
1592 decl
= DECL_TEMPLATE_RESULT (TREE_OPERAND (result
, 0));
1593 args
= TREE_OPERAND (result
, 1);
1595 return build_nt (CHECK_CONSTR
, decl
, args
);
1598 /* Substitute into the conjunction of constraints. Returns
1599 error_mark_node if substitution into either operand fails. */
1602 tsubst_logical_operator (tree t
, tree args
,
1603 tsubst_flags_t complain
, tree in_decl
)
1605 tree t0
= TREE_OPERAND (t
, 0);
1606 tree r0
= tsubst_constraint (t0
, args
, complain
, in_decl
);
1607 if (r0
== error_mark_node
)
1608 return error_mark_node
;
1609 tree t1
= TREE_OPERAND (t
, 1);
1610 tree r1
= tsubst_constraint (t1
, args
, complain
, in_decl
);
1611 if (r1
== error_mark_node
)
1612 return error_mark_node
;
1613 return build_nt (TREE_CODE (t
), r0
, r1
);
1618 /* Substitute ARGS into the expression constraint T. */
1621 tsubst_expr_constr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
1623 cp_unevaluated guard
;
1624 tree expr
= EXPR_CONSTR_EXPR (t
);
1625 tree ret
= tsubst_expr (expr
, args
, complain
, in_decl
, false);
1626 if (ret
== error_mark_node
)
1627 return error_mark_node
;
1628 return build_nt (EXPR_CONSTR
, ret
);
1631 /* Substitute ARGS into the type constraint T. */
1634 tsubst_type_constr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
1636 tree type
= TYPE_CONSTR_TYPE (t
);
1637 tree ret
= tsubst (type
, args
, complain
, in_decl
);
1638 if (ret
== error_mark_node
)
1639 return error_mark_node
;
1640 return build_nt (TYPE_CONSTR
, ret
);
1643 /* Substitute ARGS into the implicit conversion constraint T. */
1646 tsubst_implicit_conversion_constr (tree t
, tree args
, tsubst_flags_t complain
,
1649 cp_unevaluated guard
;
1650 tree expr
= ICONV_CONSTR_EXPR (t
);
1651 tree type
= ICONV_CONSTR_TYPE (t
);
1652 tree new_expr
= tsubst_expr (expr
, args
, complain
, in_decl
, false);
1653 if (new_expr
== error_mark_node
)
1654 return error_mark_node
;
1655 tree new_type
= tsubst (type
, args
, complain
, in_decl
);
1656 if (new_type
== error_mark_node
)
1657 return error_mark_node
;
1658 return build_nt (ICONV_CONSTR
, new_expr
, new_type
);
1661 /* Substitute ARGS into the argument deduction constraint T. */
1664 tsubst_argument_deduction_constr (tree t
, tree args
, tsubst_flags_t complain
,
1667 cp_unevaluated guard
;
1668 tree expr
= DEDUCT_CONSTR_EXPR (t
);
1669 tree pattern
= DEDUCT_CONSTR_PATTERN (t
);
1670 tree autos
= DEDUCT_CONSTR_PLACEHOLDER(t
);
1671 tree new_expr
= tsubst_expr (expr
, args
, complain
, in_decl
, false);
1672 if (new_expr
== error_mark_node
)
1673 return error_mark_node
;
1674 /* It seems like substituting through the pattern will not affect the
1675 placeholders. We should (?) be able to reuse the existing list
1676 without any problems. If not, then we probably want to create a
1677 new list of placeholders and then instantiate the pattern using
1679 tree new_pattern
= tsubst (pattern
, args
, complain
, in_decl
);
1680 if (new_pattern
== error_mark_node
)
1681 return error_mark_node
;
1682 return build_nt (DEDUCT_CONSTR
, new_expr
, new_pattern
, autos
);
1685 /* Substitute ARGS into the exception constraint T. */
1688 tsubst_exception_constr (tree t
, tree args
, tsubst_flags_t complain
,
1691 cp_unevaluated guard
;
1692 tree expr
= EXCEPT_CONSTR_EXPR (t
);
1693 tree ret
= tsubst_expr (expr
, args
, complain
, in_decl
, false);
1694 if (ret
== error_mark_node
)
1695 return error_mark_node
;
1696 return build_nt (EXCEPT_CONSTR
, ret
);
1699 /* A subroutine of tsubst_constraint_variables. Register local
1700 specializations for each of parameter in PARMS and its
1701 corresponding substituted constraint variable in VARS.
1705 declare_constraint_vars (tree parms
, tree vars
)
1708 for (tree t
= parms
; t
; t
= DECL_CHAIN (t
))
1710 if (DECL_PACK_P (t
))
1712 tree pack
= extract_fnparm_pack (t
, &s
);
1713 register_local_specialization (pack
, t
);
1717 register_local_specialization (s
, t
);
1724 /* A subroutine of tsubst_parameterized_constraint. Substitute ARGS
1725 into the parameter list T, producing a sequence of constraint
1726 variables, declared in the current scope.
1728 Note that the caller must establish a local specialization stack
1729 prior to calling this function since this substitution will
1730 declare the substituted parameters. */
1733 tsubst_constraint_variables (tree t
, tree args
,
1734 tsubst_flags_t complain
, tree in_decl
)
1736 /* Clear cp_unevaluated_operand across tsubst so that we get a proper chain
1738 int saved_unevaluated_operand
= cp_unevaluated_operand
;
1739 cp_unevaluated_operand
= 0;
1740 tree vars
= tsubst (t
, args
, complain
, in_decl
);
1741 cp_unevaluated_operand
= saved_unevaluated_operand
;
1742 if (vars
== error_mark_node
)
1743 return error_mark_node
;
1744 return declare_constraint_vars (t
, vars
);
1747 /* Substitute ARGS into the parameterized constraint T. */
1750 tsubst_parameterized_constraint (tree t
, tree args
,
1751 tsubst_flags_t complain
, tree in_decl
)
1753 local_specialization_stack stack
;
1754 tree vars
= tsubst_constraint_variables (PARM_CONSTR_PARMS (t
),
1755 args
, complain
, in_decl
);
1756 if (vars
== error_mark_node
)
1757 return error_mark_node
;
1758 tree expr
= tsubst_constraint (PARM_CONSTR_OPERAND (t
), args
,
1760 if (expr
== error_mark_node
)
1761 return error_mark_node
;
1762 return build_nt (PARM_CONSTR
, vars
, expr
);
1765 /* Substitute ARGS into the simple requirement T. Note that
1766 substitution may result in an ill-formed expression without
1767 causing the program to be ill-formed. In such cases, the
1768 requirement wraps an error_mark_node. */
1771 tsubst_simple_requirement (tree t
, tree args
,
1772 tsubst_flags_t complain
, tree in_decl
)
1774 ++processing_template_decl
;
1775 tree expr
= tsubst_expr (TREE_OPERAND (t
, 0), args
, complain
, in_decl
, false);
1776 --processing_template_decl
;
1777 return finish_simple_requirement (expr
);
1780 /* Substitute ARGS into the type requirement T. Note that
1781 substitution may result in an ill-formed type without
1782 causing the program to be ill-formed. In such cases, the
1783 requirement wraps an error_mark_node. */
1786 tsubst_type_requirement (tree t
, tree args
,
1787 tsubst_flags_t complain
, tree in_decl
)
1789 ++processing_template_decl
;
1790 tree type
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
1791 --processing_template_decl
;
1792 return finish_type_requirement (type
);
1795 /* Substitute args into the compound requirement T. If substituting
1796 into either the expression or the type fails, the corresponding
1797 operands in the resulting node will be error_mark_node. This
1798 preserves a requirement for the purpose of partial ordering, but
1799 it will never be satisfied. */
1802 tsubst_compound_requirement (tree t
, tree args
,
1803 tsubst_flags_t complain
, tree in_decl
)
1805 ++processing_template_decl
;
1806 tree expr
= tsubst_expr (TREE_OPERAND (t
, 0), args
, complain
, in_decl
, false);
1807 tree type
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
1808 --processing_template_decl
;
1809 bool noexcept_p
= COMPOUND_REQ_NOEXCEPT_P (t
);
1810 return finish_compound_requirement (expr
, type
, noexcept_p
);
1813 /* Substitute ARGS into the nested requirement T. */
1816 tsubst_nested_requirement (tree t
, tree args
,
1817 tsubst_flags_t complain
, tree in_decl
)
1819 ++processing_template_decl
;
1820 tree expr
= tsubst_expr (TREE_OPERAND (t
, 0), args
, complain
, in_decl
, false);
1821 --processing_template_decl
;
1822 return finish_nested_requirement (expr
);
1825 /* Substitute ARGS into the requirement T. */
1828 tsubst_requirement (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
1830 switch (TREE_CODE (t
))
1833 return tsubst_simple_requirement (t
, args
, complain
, in_decl
);
1835 return tsubst_type_requirement (t
, args
, complain
, in_decl
);
1837 return tsubst_compound_requirement (t
, args
, complain
, in_decl
);
1839 return tsubst_nested_requirement (t
, args
, complain
, in_decl
);
1843 return error_mark_node
;
1846 /* Substitute ARGS into the list of requirements T. Note that
1847 substitution failures here result in ill-formed programs. */
1850 tsubst_requirement_body (tree t
, tree args
,
1851 tsubst_flags_t complain
, tree in_decl
)
1856 tree e
= tsubst_requirement (TREE_VALUE (t
), args
, complain
, in_decl
);
1857 if (e
== error_mark_node
)
1858 return error_mark_node
;
1859 r
= tree_cons (NULL_TREE
, e
, r
);
1862 /* Ensure that the order of constraints is the same as the original. */
1863 return nreverse (r
);
1868 /* Substitute ARGS into the requires expression T. Note that this
1869 results in the re-declaration of local parameters when
1870 substituting through the parameter list. If either substitution
1871 fails, the program is ill-formed. */
1874 tsubst_requires_expr (tree t
, tree args
,
1875 tsubst_flags_t complain
, tree in_decl
)
1877 local_specialization_stack stack
;
1879 tree parms
= TREE_OPERAND (t
, 0);
1882 parms
= tsubst_constraint_variables (parms
, args
, complain
, in_decl
);
1883 if (parms
== error_mark_node
)
1884 return error_mark_node
;
1887 tree reqs
= TREE_OPERAND (t
, 1);
1888 reqs
= tsubst_requirement_body (reqs
, args
, complain
, in_decl
);
1889 if (reqs
== error_mark_node
)
1890 return error_mark_node
;
1892 return finish_requires_expr (parms
, reqs
);
1895 /* Substitute ARGS into the constraint information CI, producing a new
1896 constraint record. */
1899 tsubst_constraint_info (tree t
, tree args
,
1900 tsubst_flags_t complain
, tree in_decl
)
1902 if (!t
|| t
== error_mark_node
|| !check_constraint_info (t
))
1905 tree tmpl_constr
= NULL_TREE
;
1906 if (tree r
= CI_TEMPLATE_REQS (t
))
1907 tmpl_constr
= tsubst_constraint (r
, args
, complain
, in_decl
);
1909 tree decl_constr
= NULL_TREE
;
1910 if (tree r
= CI_DECLARATOR_REQS (t
))
1911 decl_constr
= tsubst_constraint (r
, args
, complain
, in_decl
);
1913 return build_constraints (tmpl_constr
, decl_constr
);
1916 /* Substitute ARGS into the constraint T. */
1919 tsubst_constraint (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
1923 switch (TREE_CODE (t
))
1926 return tsubst_predicate_constraint (t
, args
, complain
, in_decl
);
1928 return tsubst_check_constraint (t
, args
, complain
, in_decl
);
1931 return tsubst_logical_operator (t
, args
, complain
, in_decl
);
1933 return tsubst_parameterized_constraint (t
, args
, complain
, in_decl
);
1935 return tsubst_expr_constr (t
, args
, complain
, in_decl
);
1937 return tsubst_type_constr (t
, args
, complain
, in_decl
);
1939 return tsubst_implicit_conversion_constr (t
, args
, complain
, in_decl
);
1941 return tsubst_argument_deduction_constr (t
, args
, complain
, in_decl
);
1943 return tsubst_exception_constr (t
, args
, complain
, in_decl
);
1947 return error_mark_node
;
1950 /*---------------------------------------------------------------------------
1951 Constraint satisfaction
1952 ---------------------------------------------------------------------------*/
1954 /* The following functions determine if a constraint, when
1955 substituting template arguments, is satisfied. For convenience,
1956 satisfaction reduces a constraint to either true or false (and
1961 tree
satisfy_constraint_1 (tree
, tree
, tsubst_flags_t
, tree
);
1963 /* Check the constraint pack expansion. */
1966 satisfy_pack_expansion (tree t
, tree args
,
1967 tsubst_flags_t complain
, tree in_decl
)
1969 /* Get the vector of satisfaction results.
1970 gen_elem_of_pack_expansion_instantiation will check that each element of
1971 the expansion is satisfied. */
1972 tree exprs
= tsubst_pack_expansion (t
, args
, complain
, in_decl
);
1974 if (exprs
== error_mark_node
)
1975 return boolean_false_node
;
1977 /* TODO: It might be better to normalize each expanded term
1978 and evaluate them separately. That would provide better
1979 opportunities for diagnostics. */
1980 for (int i
= 0; i
< TREE_VEC_LENGTH (exprs
); ++i
)
1981 if (TREE_VEC_ELT (exprs
, i
) != boolean_true_node
)
1982 return boolean_false_node
;
1983 return boolean_true_node
;
1986 /* A predicate constraint is satisfied if its expression evaluates
1987 to true. If substitution into that node fails, the constraint
1988 is not satisfied ([temp.constr.pred]).
1990 Note that a predicate constraint is a constraint expression
1991 of type bool. If neither of those are true, the program is
1992 ill-formed; they are not SFINAE'able errors. */
1995 satisfy_predicate_constraint (tree t
, tree args
,
1996 tsubst_flags_t complain
, tree in_decl
)
1998 tree expr
= TREE_OPERAND (t
, 0);
2000 /* We should never have a naked pack expansion in a predicate constraint. */
2001 gcc_assert (TREE_CODE (expr
) != EXPR_PACK_EXPANSION
);
2003 /* If substitution into the expression fails, the constraint
2004 is not satisfied. */
2005 expr
= tsubst_expr (expr
, args
, complain
, in_decl
, false);
2006 if (expr
== error_mark_node
)
2007 return boolean_false_node
;
2009 /* A predicate constraint shall have type bool. In some
2010 cases, substitution gives us const-qualified bool, which
2011 is also acceptable. */
2012 tree type
= cv_unqualified (TREE_TYPE (expr
));
2013 if (!same_type_p (type
, boolean_type_node
))
2015 error_at (EXPR_LOC_OR_LOC (expr
, input_location
),
2016 "constraint %qE does not have type %qT",
2017 expr
, boolean_type_node
);
2018 return boolean_false_node
;
2021 return cxx_constant_value (expr
);
2024 /* A concept check constraint like C<CARGS> is satisfied if substituting ARGS
2025 into CARGS succeeds and C is satisfied for the resulting arguments. */
2028 satisfy_check_constraint (tree t
, tree args
,
2029 tsubst_flags_t complain
, tree in_decl
)
2031 tree decl
= CHECK_CONSTR_CONCEPT (t
);
2032 tree tmpl
= DECL_TI_TEMPLATE (decl
);
2033 tree cargs
= CHECK_CONSTR_ARGS (t
);
2035 /* Instantiate the concept check arguments. */
2036 tree targs
= tsubst (cargs
, args
, tf_none
, NULL_TREE
);
2037 if (targs
== error_mark_node
)
2038 return boolean_false_node
;
2040 /* Search for a previous value. */
2041 if (tree prev
= lookup_concept_satisfaction (tmpl
, targs
))
2044 /* Expand the concept; failure here implies non-satisfaction. */
2045 tree def
= expand_concept (decl
, targs
);
2046 if (def
== error_mark_node
)
2047 return memoize_concept_satisfaction (tmpl
, args
, boolean_false_node
);
2049 /* Recursively satisfy the constraint. */
2050 tree result
= satisfy_constraint_1 (def
, targs
, complain
, in_decl
);
2051 return memoize_concept_satisfaction (tmpl
, targs
, result
);
2054 /* Check an expression constraint. The constraint is satisfied if
2055 substitution succeeds ([temp.constr.expr]).
2057 Note that the expression is unevaluated. */
2060 satisfy_expression_constraint (tree t
, tree args
,
2061 tsubst_flags_t complain
, tree in_decl
)
2063 cp_unevaluated guard
;
2064 deferring_access_check_sentinel deferring
;
2066 tree expr
= EXPR_CONSTR_EXPR (t
);
2067 tree check
= tsubst_expr (expr
, args
, complain
, in_decl
, false);
2068 if (check
== error_mark_node
)
2069 return boolean_false_node
;
2070 if (!perform_deferred_access_checks (tf_none
))
2071 return boolean_false_node
;
2072 return boolean_true_node
;
2075 /* Check a type constraint. The constraint is satisfied if
2076 substitution succeeds. */
2079 satisfy_type_constraint (tree t
, tree args
,
2080 tsubst_flags_t complain
, tree in_decl
)
2082 deferring_access_check_sentinel deferring
;
2083 tree type
= TYPE_CONSTR_TYPE (t
);
2084 gcc_assert (TYPE_P (type
) || type
== error_mark_node
);
2085 tree check
= tsubst (type
, args
, complain
, in_decl
);
2086 if (error_operand_p (check
))
2087 return boolean_false_node
;
2088 if (!perform_deferred_access_checks (complain
))
2089 return boolean_false_node
;
2090 return boolean_true_node
;
2093 /* Check an implicit conversion constraint. */
2096 satisfy_implicit_conversion_constraint (tree t
, tree args
,
2097 tsubst_flags_t complain
, tree in_decl
)
2099 /* Don't tsubst as if we're processing a template. If we try
2100 to we can end up generating template-like expressions
2101 (e.g., modop-exprs) that aren't properly typed. */
2103 tsubst_expr (ICONV_CONSTR_EXPR (t
), args
, complain
, in_decl
, false);
2104 if (expr
== error_mark_node
)
2105 return boolean_false_node
;
2107 /* Get the transformed target type. */
2108 tree type
= tsubst (ICONV_CONSTR_TYPE (t
), args
, complain
, in_decl
);
2109 if (type
== error_mark_node
)
2110 return boolean_false_node
;
2112 /* Attempt the conversion as a direct initialization
2113 of the form TYPE <unspecified> = EXPR. */
2115 perform_direct_initialization_if_possible (type
, expr
, false, complain
);
2116 if (conv
== NULL_TREE
|| conv
== error_mark_node
)
2117 return boolean_false_node
;
2119 return boolean_true_node
;
2122 /* Check an argument deduction constraint. */
2125 satisfy_argument_deduction_constraint (tree t
, tree args
,
2126 tsubst_flags_t complain
, tree in_decl
)
2128 /* Substitute through the expression. */
2129 tree expr
= DEDUCT_CONSTR_EXPR (t
);
2130 tree init
= tsubst_expr (expr
, args
, complain
, in_decl
, false);
2131 if (expr
== error_mark_node
)
2132 return boolean_false_node
;
2134 /* Perform auto or decltype(auto) deduction to get the result. */
2135 tree pattern
= DEDUCT_CONSTR_PATTERN (t
);
2136 tree placeholder
= DEDUCT_CONSTR_PLACEHOLDER (t
);
2137 tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (placeholder
);
2138 tree type_canonical
= TYPE_CANONICAL (placeholder
);
2139 PLACEHOLDER_TYPE_CONSTRAINTS (placeholder
)
2140 = tsubst_constraint (constr
, args
, complain
|tf_partial
, in_decl
);
2141 TYPE_CANONICAL (placeholder
) = NULL_TREE
;
2142 tree type
= do_auto_deduction (pattern
, init
, placeholder
,
2143 complain
, adc_requirement
);
2144 PLACEHOLDER_TYPE_CONSTRAINTS (placeholder
) = constr
;
2145 TYPE_CANONICAL (placeholder
) = type_canonical
;
2146 if (type
== error_mark_node
)
2147 return boolean_false_node
;
2149 return boolean_true_node
;
2152 /* Check an exception constraint. An exception constraint for an
2153 expression e is satisfied when noexcept(e) is true. */
2156 satisfy_exception_constraint (tree t
, tree args
,
2157 tsubst_flags_t complain
, tree in_decl
)
2159 tree expr
= EXCEPT_CONSTR_EXPR (t
);
2160 tree check
= tsubst_expr (expr
, args
, complain
, in_decl
, false);
2161 if (check
== error_mark_node
)
2162 return boolean_false_node
;
2164 if (expr_noexcept_p (check
, complain
))
2165 return boolean_true_node
;
2167 return boolean_false_node
;
2170 /* Check a parameterized constraint. */
2173 satisfy_parameterized_constraint (tree t
, tree args
,
2174 tsubst_flags_t complain
, tree in_decl
)
2176 local_specialization_stack stack
;
2177 tree parms
= PARM_CONSTR_PARMS (t
);
2178 tree vars
= tsubst_constraint_variables (parms
, args
, complain
, in_decl
);
2179 if (vars
== error_mark_node
)
2180 return boolean_false_node
;
2181 tree constr
= PARM_CONSTR_OPERAND (t
);
2182 return satisfy_constraint_1 (constr
, args
, complain
, in_decl
);
2185 /* Check that the conjunction of constraints is satisfied. Note
2186 that if left operand is not satisfied, the right operand
2189 FIXME: Check that this wouldn't result in a user-defined
2190 operator. Note that this error is partially diagnosed in
2191 satisfy_predicate_constraint. It would be nice to diagnose
2192 the overload, but I don't think it's strictly necessary. */
2195 satisfy_conjunction (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
2197 tree t0
= satisfy_constraint_1 (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
2198 if (t0
== boolean_false_node
)
2199 return boolean_false_node
;
2200 return satisfy_constraint_1 (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
2203 /* Check that the disjunction of constraints is satisfied. Note
2204 that if the left operand is satisfied, the right operand is not
2208 satisfy_disjunction (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
2210 tree t0
= satisfy_constraint_1 (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
2211 if (t0
== boolean_true_node
)
2212 return boolean_true_node
;
2213 return satisfy_constraint_1 (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
2216 /* Dispatch to an appropriate satisfaction routine depending on the
2220 satisfy_constraint_1 (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
2222 gcc_assert (!processing_template_decl
);
2225 return boolean_false_node
;
2227 if (t
== error_mark_node
)
2228 return boolean_false_node
;
2230 switch (TREE_CODE (t
))
2233 return satisfy_predicate_constraint (t
, args
, complain
, in_decl
);
2236 return satisfy_check_constraint (t
, args
, complain
, in_decl
);
2239 return satisfy_expression_constraint (t
, args
, complain
, in_decl
);
2242 return satisfy_type_constraint (t
, args
, complain
, in_decl
);
2245 return satisfy_implicit_conversion_constraint (t
, args
, complain
, in_decl
);
2248 return satisfy_argument_deduction_constraint (t
, args
, complain
, in_decl
);
2251 return satisfy_exception_constraint (t
, args
, complain
, in_decl
);
2254 return satisfy_parameterized_constraint (t
, args
, complain
, in_decl
);
2257 return satisfy_conjunction (t
, args
, complain
, in_decl
);
2260 return satisfy_disjunction (t
, args
, complain
, in_decl
);
2262 case EXPR_PACK_EXPANSION
:
2263 return satisfy_pack_expansion (t
, args
, complain
, in_decl
);
2268 return boolean_false_node
;
2271 /* Check that the constraint is satisfied, according to the rules
2272 for that constraint. Note that each satisfy_* function returns
2273 true or false, depending on whether it is satisfied or not. */
2276 satisfy_constraint (tree t
, tree args
)
2278 auto_timevar
time (TV_CONSTRAINT_SAT
);
2280 /* Turn off template processing. Constraint satisfaction only applies
2281 to non-dependent terms, so we want to ensure full checking here. */
2282 processing_template_decl_sentinel
proc (true);
2284 /* Avoid early exit in tsubst and tsubst_copy from null args; since earlier
2285 substitution was done with processing_template_decl forced on, there will
2286 be expressions that still need semantic processing, possibly buried in
2287 decltype or a template argument. */
2288 if (args
== NULL_TREE
)
2289 args
= make_tree_vec (1);
2291 return satisfy_constraint_1 (t
, args
, tf_none
, NULL_TREE
);
2294 /* Check the associated constraints in CI against the given
2295 ARGS, returning true when the constraints are satisfied
2296 and false otherwise. */
2299 satisfy_associated_constraints (tree ci
, tree args
)
2301 /* If there are no constraints then this is trivially satisfied. */
2303 return boolean_true_node
;
2305 /* If any arguments depend on template parameters, we can't
2306 check constraints. */
2307 if (args
&& uses_template_parms (args
))
2308 return boolean_true_node
;
2310 /* Check if we've seen a previous result. */
2311 if (tree prev
= lookup_constraint_satisfaction (ci
, args
))
2314 /* Actually test for satisfaction. */
2315 tree result
= satisfy_constraint (CI_ASSOCIATED_CONSTRAINTS (ci
), args
);
2316 return memoize_constraint_satisfaction (ci
, args
, result
);
2321 /* Evaluate the given constraint, returning boolean_true_node
2322 if the constraint is satisfied and boolean_false_node
2326 evaluate_constraints (tree constr
, tree args
)
2328 gcc_assert (constraint_p (constr
));
2329 return satisfy_constraint (constr
, args
);
2332 /* Evaluate the function concept FN by substituting its own args
2333 into its definition and evaluating that as the result. Returns
2334 boolean_true_node if the constraints are satisfied and
2335 boolean_false_node otherwise. */
2338 evaluate_function_concept (tree fn
, tree args
)
2340 tree constr
= build_nt (CHECK_CONSTR
, fn
, args
);
2341 return satisfy_constraint (constr
, args
);
2344 /* Evaluate the variable concept VAR by substituting its own args into
2345 its initializer and checking the resulting constraint. Returns
2346 boolean_true_node if the constraints are satisfied and
2347 boolean_false_node otherwise. */
2350 evaluate_variable_concept (tree var
, tree args
)
2352 tree constr
= build_nt (CHECK_CONSTR
, var
, args
);
2353 return satisfy_constraint (constr
, args
);
2356 /* Evaluate the given expression as if it were a predicate
2357 constraint. Returns boolean_true_node if the constraint
2358 is satisfied and boolean_false_node otherwise. */
2361 evaluate_constraint_expression (tree expr
, tree args
)
2363 tree constr
= normalize_expression (expr
);
2364 return satisfy_constraint (constr
, args
);
2367 /* Returns true if the DECL's constraints are satisfied.
2368 This is used in cases where a declaration is formed but
2369 before it is used (e.g., overload resolution). */
2372 constraints_satisfied_p (tree decl
)
2374 /* Get the constraints to check for satisfaction. This depends
2375 on whether we're looking at a template specialization or not. */
2377 tree args
= NULL_TREE
;
2378 if (tree ti
= DECL_TEMPLATE_INFO (decl
))
2380 tree tmpl
= TI_TEMPLATE (ti
);
2381 ci
= get_constraints (tmpl
);
2382 int depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2383 args
= get_innermost_template_args (TI_ARGS (ti
), depth
);
2387 ci
= get_constraints (decl
);
2390 tree eval
= satisfy_associated_constraints (ci
, args
);
2391 return eval
== boolean_true_node
;
2394 /* Returns true if the constraints are satisfied by ARGS.
2395 Here, T can be either a constraint or a constrained
2399 constraints_satisfied_p (tree t
, tree args
)
2402 if (constraint_p (t
))
2403 eval
= evaluate_constraints (t
, args
);
2405 eval
= satisfy_associated_constraints (get_constraints (t
), args
);
2406 return eval
== boolean_true_node
;
2412 /* Normalize EXPR and determine if the resulting constraint is
2413 satisfied by ARGS. Returns true if and only if the constraint
2414 is satisfied. This is used extensively by diagnostics to
2415 determine causes for failure. */
2418 constraint_expression_satisfied_p (tree expr
, tree args
)
2420 return evaluate_constraint_expression (expr
, args
) == boolean_true_node
;
2425 /*---------------------------------------------------------------------------
2426 Semantic analysis of requires-expressions
2427 ---------------------------------------------------------------------------*/
2429 /* Finish a requires expression for the given PARMS (possibly
2430 null) and the non-empty sequence of requirements. */
2432 finish_requires_expr (tree parms
, tree reqs
)
2434 /* Modify the declared parameters by removing their context
2435 so they don't refer to the enclosing scope and explicitly
2436 indicating that they are constraint variables. */
2437 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
2439 DECL_CONTEXT (parm
) = NULL_TREE
;
2440 CONSTRAINT_VAR_P (parm
) = true;
2443 /* Build the node. */
2444 tree r
= build_min (REQUIRES_EXPR
, boolean_type_node
, parms
, reqs
);
2445 TREE_SIDE_EFFECTS (r
) = false;
2446 TREE_CONSTANT (r
) = true;
2450 /* Construct a requirement for the validity of EXPR. */
2452 finish_simple_requirement (tree expr
)
2454 return build_nt (SIMPLE_REQ
, expr
);
2457 /* Construct a requirement for the validity of TYPE. */
2459 finish_type_requirement (tree type
)
2461 return build_nt (TYPE_REQ
, type
);
2464 /* Construct a requirement for the validity of EXPR, along with
2465 its properties. if TYPE is non-null, then it specifies either
2466 an implicit conversion or argument deduction constraint,
2467 depending on whether any placeholders occur in the type name.
2468 NOEXCEPT_P is true iff the noexcept keyword was specified. */
2470 finish_compound_requirement (tree expr
, tree type
, bool noexcept_p
)
2472 tree req
= build_nt (COMPOUND_REQ
, expr
, type
);
2473 COMPOUND_REQ_NOEXCEPT_P (req
) = noexcept_p
;
2477 /* Finish a nested requirement. */
2479 finish_nested_requirement (tree expr
)
2481 return build_nt (NESTED_REQ
, expr
);
2484 // Check that FN satisfies the structural requirements of a
2485 // function concept definition.
2487 check_function_concept (tree fn
)
2489 // Check that the function is comprised of only a single
2490 // return statement.
2491 tree body
= DECL_SAVED_TREE (fn
);
2492 if (TREE_CODE (body
) == BIND_EXPR
)
2493 body
= BIND_EXPR_BODY (body
);
2495 // Sometimes a function call results in the creation of clean up
2496 // points. Allow these to be preserved in the body of the
2497 // constraint, as we might actually need them for some constexpr
2499 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
2500 body
= TREE_OPERAND (body
, 0);
2502 /* Check that the definition is written correctly. */
2503 if (TREE_CODE (body
) != RETURN_EXPR
)
2505 location_t loc
= DECL_SOURCE_LOCATION (fn
);
2506 if (TREE_CODE (body
) == STATEMENT_LIST
&& !STATEMENT_LIST_HEAD (body
))
2509 /* The definition was probably erroneous, not empty. */;
2511 error_at (loc
, "definition of concept %qD is empty", fn
);
2514 error_at (loc
, "definition of concept %qD has multiple statements", fn
);
2521 // Check that a constrained friend declaration function declaration,
2522 // FN, is admissible. This is the case only when the declaration depends
2523 // on template parameters and does not declare a specialization.
2525 check_constrained_friend (tree fn
, tree reqs
)
2527 if (fn
== error_mark_node
)
2529 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
2531 // If there are not constraints, this cannot be an error.
2535 // Constrained friend functions that don't depend on template
2536 // arguments are effectively meaningless.
2537 if (!uses_template_parms (TREE_TYPE (fn
)))
2539 error_at (location_of (fn
),
2540 "constrained friend does not depend on template parameters");
2545 /*---------------------------------------------------------------------------
2546 Equivalence of constraints
2547 ---------------------------------------------------------------------------*/
2549 /* Returns true when A and B are equivalent constraints. */
2551 equivalent_constraints (tree a
, tree b
)
2553 gcc_assert (!a
|| TREE_CODE (a
) == CONSTRAINT_INFO
);
2554 gcc_assert (!b
|| TREE_CODE (b
) == CONSTRAINT_INFO
);
2555 return cp_tree_equal (a
, b
);
2558 /* Returns true if the template declarations A and B have equivalent
2559 constraints. This is the case when A's constraints subsume B's and
2560 when B's also constrain A's. */
2562 equivalently_constrained (tree d1
, tree d2
)
2564 gcc_assert (TREE_CODE (d1
) == TREE_CODE (d2
));
2565 return equivalent_constraints (get_constraints (d1
), get_constraints (d2
));
2568 /*---------------------------------------------------------------------------
2569 Partial ordering of constraints
2570 ---------------------------------------------------------------------------*/
2572 /* Returns true when the the constraints in A subsume those in B. */
2575 subsumes_constraints (tree a
, tree b
)
2577 gcc_assert (!a
|| TREE_CODE (a
) == CONSTRAINT_INFO
);
2578 gcc_assert (!b
|| TREE_CODE (b
) == CONSTRAINT_INFO
);
2579 return subsumes (a
, b
);
2582 /* Returns true when the the constraints in A subsume those in B, but
2583 the constraints in B do not subsume the constraints in A. */
2586 strictly_subsumes (tree a
, tree b
)
2588 return subsumes (a
, b
) && !subsumes (b
, a
);
2591 /* Determines which of the declarations, A or B, is more constrained.
2592 That is, which declaration's constraints subsume but are not subsumed
2595 Returns 1 if A is more constrained than B, -1 if B is more constrained
2596 than A, and 0 otherwise. */
2599 more_constrained (tree d1
, tree d2
)
2601 tree c1
= get_constraints (d1
);
2602 tree c2
= get_constraints (d2
);
2604 if (subsumes_constraints (c1
, c2
))
2606 if (subsumes_constraints (c2
, c1
))
2611 /* Returns true if D1 is at least as constrained as D2. That is, the
2612 associated constraints of D1 subsume those of D2, or both declarations
2613 are unconstrained. */
2616 at_least_as_constrained (tree d1
, tree d2
)
2618 tree c1
= get_constraints (d1
);
2619 tree c2
= get_constraints (d2
);
2620 return subsumes_constraints (c1
, c2
);
2624 /*---------------------------------------------------------------------------
2625 Constraint diagnostics
2627 FIXME: Normalize expressions into constraints before evaluating them.
2628 This should be the general pattern for all such diagnostics.
2629 ---------------------------------------------------------------------------*/
2631 /* The number of detailed constraint failures. */
2633 int constraint_errors
= 0;
2635 /* Do not generate errors after diagnosing this number of constraint
2638 FIXME: This is a really arbitrary number. Provide better control of
2639 constraint diagnostics with a command line option. */
2641 int constraint_thresh
= 20;
2644 /* Returns true if we should elide the diagnostic for a constraint failure.
2645 This is the case when the number of errors has exceeded the pre-configured
2649 elide_constraint_failure_p ()
2651 bool ret
= constraint_thresh
<= constraint_errors
;
2652 ++constraint_errors
;
2656 /* Returns the number of undiagnosed errors. */
2659 undiagnosed_constraint_failures ()
2661 return constraint_errors
- constraint_thresh
;
2664 /* The diagnosis of constraints performs a combination of normalization
2665 and satisfaction testing. We recursively walk through the conjunction or
2666 disjunction of associated constraints, testing each sub-constraint in
2671 void diagnose_constraint (location_t
, tree
, tree
, tree
);
2673 /* Emit a specific diagnostics for a failed trait. */
2676 diagnose_trait_expression (location_t loc
, tree
, tree cur
, tree args
)
2678 if (constraint_expression_satisfied_p (cur
, args
))
2680 if (elide_constraint_failure_p())
2683 tree expr
= PRED_CONSTR_EXPR (cur
);
2684 ++processing_template_decl
;
2685 expr
= tsubst_expr (expr
, args
, tf_none
, NULL_TREE
, false);
2686 --processing_template_decl
;
2688 tree t1
= TRAIT_EXPR_TYPE1 (expr
);
2689 tree t2
= TRAIT_EXPR_TYPE2 (expr
);
2690 switch (TRAIT_EXPR_KIND (expr
))
2692 case CPTK_HAS_NOTHROW_ASSIGN
:
2693 inform (loc
, " %qT is not nothrow copy assignable", t1
);
2695 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
2696 inform (loc
, " %qT is not nothrow default constructible", t1
);
2698 case CPTK_HAS_NOTHROW_COPY
:
2699 inform (loc
, " %qT is not nothrow copy constructible", t1
);
2701 case CPTK_HAS_TRIVIAL_ASSIGN
:
2702 inform (loc
, " %qT is not trivially copy assignable", t1
);
2704 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
2705 inform (loc
, " %qT is not trivially default constructible", t1
);
2707 case CPTK_HAS_TRIVIAL_COPY
:
2708 inform (loc
, " %qT is not trivially copy constructible", t1
);
2710 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
2711 inform (loc
, " %qT is not trivially destructible", t1
);
2713 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
2714 inform (loc
, " %qT does not have a virtual destructor", t1
);
2716 case CPTK_IS_ABSTRACT
:
2717 inform (loc
, " %qT is not an abstract class", t1
);
2719 case CPTK_IS_BASE_OF
:
2720 inform (loc
, " %qT is not a base of %qT", t1
, t2
);
2723 inform (loc
, " %qT is not a class", t1
);
2726 inform (loc
, " %qT is not an empty class", t1
);
2729 inform (loc
, " %qT is not an enum", t1
);
2732 inform (loc
, " %qT is not a final class", t1
);
2734 case CPTK_IS_LITERAL_TYPE
:
2735 inform (loc
, " %qT is not a literal type", t1
);
2738 inform (loc
, " %qT is not a POD type", t1
);
2740 case CPTK_IS_POLYMORPHIC
:
2741 inform (loc
, " %qT is not a polymorphic type", t1
);
2743 case CPTK_IS_SAME_AS
:
2744 inform (loc
, " %qT is not the same as %qT", t1
, t2
);
2746 case CPTK_IS_STD_LAYOUT
:
2747 inform (loc
, " %qT is not an standard layout type", t1
);
2749 case CPTK_IS_TRIVIAL
:
2750 inform (loc
, " %qT is not a trivial type", t1
);
2753 inform (loc
, " %qT is not a union", t1
);
2760 /* Diagnose the expression of a predicate constraint. */
2763 diagnose_other_expression (location_t loc
, tree
, tree cur
, tree args
)
2765 if (constraint_expression_satisfied_p (cur
, args
))
2767 if (elide_constraint_failure_p())
2769 inform (loc
, "%qE evaluated to false", cur
);
2772 /* Do our best to infer meaning from predicates. */
2775 diagnose_predicate_constraint (location_t loc
, tree orig
, tree cur
, tree args
)
2777 if (TREE_CODE (PRED_CONSTR_EXPR (cur
)) == TRAIT_EXPR
)
2778 diagnose_trait_expression (loc
, orig
, cur
, args
);
2780 diagnose_other_expression (loc
, orig
, cur
, args
);
2783 /* Diagnose a failed pack expansion, possibly containing constraints. */
2786 diagnose_pack_expansion (location_t loc
, tree
, tree cur
, tree args
)
2788 if (constraint_expression_satisfied_p (cur
, args
))
2790 if (elide_constraint_failure_p())
2793 /* Make sure that we don't have naked packs that we don't expect. */
2794 if (!same_type_p (TREE_TYPE (cur
), boolean_type_node
))
2796 inform (loc
, "invalid pack expansion in constraint %qE", cur
);
2800 inform (loc
, "in the expansion of %qE", cur
);
2802 /* Get the vector of expanded arguments. Note that n must not
2803 be 0 since this constraint is not satisfied. */
2804 ++processing_template_decl
;
2805 tree exprs
= tsubst_pack_expansion (cur
, args
, tf_none
, NULL_TREE
);
2806 --processing_template_decl
;
2807 if (exprs
== error_mark_node
)
2809 /* TODO: This error message could be better. */
2810 inform (loc
, " substitution failure occurred during expansion");
2814 /* Check each expanded constraint separately. */
2815 int n
= TREE_VEC_LENGTH (exprs
);
2816 for (int i
= 0; i
< n
; ++i
)
2818 tree expr
= TREE_VEC_ELT (exprs
, i
);
2819 if (!constraint_expression_satisfied_p (expr
, args
))
2820 inform (loc
, " %qE was not satisfied", expr
);
2824 /* Diagnose a potentially unsatisfied concept check constraint DECL<CARGS>.
2825 Parameters are as for diagnose_constraint. */
2828 diagnose_check_constraint (location_t loc
, tree orig
, tree cur
, tree args
)
2830 if (constraints_satisfied_p (cur
, args
))
2833 tree decl
= CHECK_CONSTR_CONCEPT (cur
);
2834 tree cargs
= CHECK_CONSTR_ARGS (cur
);
2835 tree tmpl
= DECL_TI_TEMPLATE (decl
);
2836 tree check
= build_nt (CHECK_CONSTR
, decl
, cargs
);
2838 /* Instantiate the concept check arguments. */
2839 tree targs
= tsubst (cargs
, args
, tf_none
, NULL_TREE
);
2840 if (targs
== error_mark_node
)
2842 if (elide_constraint_failure_p ())
2844 inform (loc
, "invalid use of the concept %qE", check
);
2845 tsubst (cargs
, args
, tf_warning_or_error
, NULL_TREE
);
2849 tree sub
= build_tree_list (tmpl
, targs
);
2850 /* Update to the expanded definitions. */
2851 cur
= expand_concept (decl
, targs
);
2852 if (cur
== error_mark_node
)
2854 if (elide_constraint_failure_p ())
2856 inform (loc
, "in the expansion of concept %<%E %S%>", check
, sub
);
2857 cur
= get_concept_definition (decl
);
2858 tsubst_expr (cur
, targs
, tf_warning_or_error
, NULL_TREE
, false);
2862 orig
= get_concept_definition (CHECK_CONSTR_CONCEPT (orig
));
2863 orig
= normalize_expression (orig
);
2865 location_t dloc
= DECL_SOURCE_LOCATION (decl
);
2866 inform (dloc
, "within %qS", sub
);
2867 diagnose_constraint (dloc
, orig
, cur
, targs
);
2870 /* Diagnose a potentially unsatisfied conjunction or disjunction. Parameters
2871 are as for diagnose_constraint. */
2874 diagnose_logical_constraint (location_t loc
, tree orig
, tree cur
, tree args
)
2876 tree t0
= TREE_OPERAND (cur
, 0);
2877 tree t1
= TREE_OPERAND (cur
, 1);
2878 if (!constraints_satisfied_p (t0
, args
))
2879 diagnose_constraint (loc
, TREE_OPERAND (orig
, 0), t0
, args
);
2880 else if (TREE_CODE (orig
) == TRUTH_ORIF_EXPR
)
2882 if (!constraints_satisfied_p (t1
, args
))
2883 diagnose_constraint (loc
, TREE_OPERAND (orig
, 1), t1
, args
);
2886 /* Diagnose a potential expression constraint failure. */
2889 diagnose_expression_constraint (location_t loc
, tree orig
, tree cur
, tree args
)
2891 if (constraints_satisfied_p (cur
, args
))
2893 if (elide_constraint_failure_p())
2896 tree expr
= EXPR_CONSTR_EXPR (orig
);
2897 inform (loc
, "the required expression %qE would be ill-formed", expr
);
2899 // TODO: We should have a flag that controls this substitution.
2900 // I'm finding it very useful for resolving concept check errors.
2902 // inform (input_location, "==== BEGIN DUMP ====");
2903 // tsubst_expr (EXPR_CONSTR_EXPR (orig), args, tf_warning_or_error, NULL_TREE, false);
2904 // inform (input_location, "==== END DUMP ====");
2907 /* Diagnose a potentially failed type constraint. */
2910 diagnose_type_constraint (location_t loc
, tree orig
, tree cur
, tree args
)
2912 if (constraints_satisfied_p (cur
, args
))
2914 if (elide_constraint_failure_p())
2917 tree type
= TYPE_CONSTR_TYPE (orig
);
2918 inform (loc
, "the required type %qT would be ill-formed", type
);
2921 /* Diagnose a potentially unsatisfied conversion constraint. */
2924 diagnose_implicit_conversion_constraint (location_t loc
, tree orig
, tree cur
,
2927 if (constraints_satisfied_p (cur
, args
))
2930 /* The expression and type will previously have been substituted into,
2931 and therefore may already be an error. Also, we will have already
2932 diagnosed substitution failures into an expression since this must be
2933 part of a compound requirement. */
2934 tree expr
= ICONV_CONSTR_EXPR (cur
);
2935 if (error_operand_p (expr
))
2938 /* Don't elide a previously diagnosed failure. */
2939 if (elide_constraint_failure_p())
2942 tree type
= ICONV_CONSTR_TYPE (cur
);
2943 if (error_operand_p (type
))
2945 inform (loc
, "substitution into type %qT failed",
2946 ICONV_CONSTR_TYPE (orig
));
2950 inform(loc
, "%qE is not implicitly convertible to %qT", expr
, type
);
2953 /* Diagnose an argument deduction constraint. */
2956 diagnose_argument_deduction_constraint (location_t loc
, tree orig
, tree cur
,
2959 if (constraints_satisfied_p (cur
, args
))
2962 /* The expression and type will previously have been substituted into,
2963 and therefore may already be an error. Also, we will have already
2964 diagnosed substution failures into an expression since this must be
2965 part of a compound requirement. */
2966 tree expr
= DEDUCT_CONSTR_EXPR (cur
);
2967 if (error_operand_p (expr
))
2970 /* Don't elide a previously diagnosed failure. */
2971 if (elide_constraint_failure_p ())
2974 tree pattern
= DEDUCT_CONSTR_PATTERN (cur
);
2975 if (error_operand_p (pattern
))
2977 inform (loc
, "substitution into type %qT failed",
2978 DEDUCT_CONSTR_PATTERN (orig
));
2982 inform (loc
, "unable to deduce placeholder type %qT from %qE",
2986 /* Diagnose an exception constraint. */
2989 diagnose_exception_constraint (location_t loc
, tree orig
, tree cur
, tree args
)
2991 if (constraints_satisfied_p (cur
, args
))
2993 if (elide_constraint_failure_p ())
2996 /* Rebuild a noexcept expression. */
2997 tree expr
= EXCEPT_CONSTR_EXPR (cur
);
2998 if (error_operand_p (expr
))
3001 inform (loc
, "%qE evaluated to false", EXCEPT_CONSTR_EXPR (orig
));
3004 /* Diagnose a potentially unsatisfied parameterized constraint. */
3007 diagnose_parameterized_constraint (location_t loc
, tree orig
, tree cur
,
3010 if (constraints_satisfied_p (cur
, args
))
3013 local_specialization_stack stack
;
3014 tree parms
= PARM_CONSTR_PARMS (cur
);
3015 tree vars
= tsubst_constraint_variables (parms
, args
, tf_warning_or_error
,
3017 if (vars
== error_mark_node
)
3019 if (elide_constraint_failure_p ())
3022 /* TODO: Check which variable failed and use orig to diagnose
3023 that substitution error. */
3024 inform (loc
, "failed to instantiate constraint variables");
3028 /* TODO: It would be better write these in a list. */
3031 inform (loc
, " with %q#D", vars
);
3032 vars
= TREE_CHAIN (vars
);
3034 orig
= PARM_CONSTR_OPERAND (orig
);
3035 cur
= PARM_CONSTR_OPERAND (cur
);
3036 return diagnose_constraint (loc
, orig
, cur
, args
);
3039 /* Diagnose the constraint CUR for the given ARGS. This is only ever invoked
3040 on the associated constraints, so we can only have conjunctions of
3041 predicate constraints. The ORIGinal (dependent) constructs follow
3042 the current constraints to enable better diagnostics. Note that ORIG
3043 and CUR must be the same kinds of node, except when CUR is an error. */
3046 diagnose_constraint (location_t loc
, tree orig
, tree cur
, tree args
)
3048 switch (TREE_CODE (cur
))
3051 diagnose_expression_constraint (loc
, orig
, cur
, args
);
3055 diagnose_type_constraint (loc
, orig
, cur
, args
);
3059 diagnose_implicit_conversion_constraint (loc
, orig
, cur
, args
);
3063 diagnose_argument_deduction_constraint (loc
, orig
, cur
, args
);
3067 diagnose_exception_constraint (loc
, orig
, cur
, args
);
3072 diagnose_logical_constraint (loc
, orig
, cur
, args
);
3076 diagnose_predicate_constraint (loc
, orig
, cur
, args
);
3080 diagnose_parameterized_constraint (loc
, orig
, cur
, args
);
3084 diagnose_check_constraint (loc
, orig
, cur
, args
);
3087 case EXPR_PACK_EXPANSION
:
3088 diagnose_pack_expansion (loc
, orig
, cur
, args
);
3092 /* TODO: Can we improve the diagnostic with the original? */
3093 inform (input_location
, "ill-formed constraint");
3102 /* Diagnose the reason(s) why ARGS do not satisfy the constraints
3103 of declaration DECL. */
3106 diagnose_declaration_constraints (location_t loc
, tree decl
, tree args
)
3108 inform (loc
, " constraints not satisfied");
3110 /* Constraints are attached to the template. */
3111 if (tree ti
= DECL_TEMPLATE_INFO (decl
))
3113 decl
= TI_TEMPLATE (ti
);
3115 args
= TI_ARGS (ti
);
3118 /* Recursively diagnose the associated constraints. */
3119 tree ci
= get_constraints (decl
);
3120 tree t
= CI_ASSOCIATED_CONSTRAINTS (ci
);
3121 diagnose_constraint (loc
, t
, t
, args
);
3126 /* Emit diagnostics detailing the failure ARGS to satisfy the
3127 constraints of T. Here, T can be either a constraint
3128 or a declaration. */
3131 diagnose_constraints (location_t loc
, tree t
, tree args
)
3133 constraint_errors
= 0;
3135 if (constraint_p (t
))
3136 diagnose_constraint (loc
, t
, t
, args
);
3137 else if (DECL_P (t
))
3138 diagnose_declaration_constraints (loc
, t
, args
);
3142 /* Note the number of elided failures. */
3143 int n
= undiagnosed_constraint_failures ();
3145 inform (loc
, "... and %d more constraint errors not shown", n
);