1 /* Processing rules for constraints.
2 Copyright (C) 2013-2020 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 static tree
satisfaction_value (tree t
);
51 /* When we're parsing or substuting a constraint expression, we have slightly
52 different expression semantics. In particular, we don't want to reduce a
53 concept-id to a satisfaction value. */
55 processing_constraint_expression_sentinel::
56 processing_constraint_expression_sentinel ()
58 ++scope_chain
->x_processing_constraint
;
61 processing_constraint_expression_sentinel::
62 ~processing_constraint_expression_sentinel ()
64 --scope_chain
->x_processing_constraint
;
68 processing_constraint_expression_p ()
70 return scope_chain
->x_processing_constraint
!= 0;
73 /*---------------------------------------------------------------------------
74 Constraint expressions
75 ---------------------------------------------------------------------------*/
77 /* Information provided to substitution. */
81 subst_info (tsubst_flags_t cmp
, tree in
)
82 : complain (cmp
), in_decl (in
)
85 /* True if we should not diagnose errors. */
88 return complain
== tf_none
;
91 /* True if we should diagnose errors. */
97 tsubst_flags_t complain
;
101 static tree
satisfy_constraint (tree
, tree
, subst_info
);
103 /* True if T is known to be some type other than bool. Note that this
104 is false for dependent types and errors. */
107 known_non_bool_p (tree t
)
109 return (t
&& !WILDCARD_TYPE_P (t
) && TREE_CODE (t
) != BOOLEAN_TYPE
);
113 check_constraint_atom (cp_expr expr
)
115 if (known_non_bool_p (TREE_TYPE (expr
)))
117 error_at (expr
.get_location (),
118 "constraint expression does not have type %<bool%>");
122 /* Check that we're using function concepts correctly. */
123 if (concept_check_p (expr
))
125 tree id
= unpack_concept_check (expr
);
126 tree tmpl
= TREE_OPERAND (id
, 0);
127 if (OVL_P (tmpl
) && TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
129 error_at (EXPR_LOC_OR_LOC (expr
, input_location
),
130 "function concept must be called");
139 check_constraint_operands (location_t
, cp_expr lhs
, cp_expr rhs
)
141 return check_constraint_atom (lhs
) && check_constraint_atom (rhs
);
144 /* Validate the semantic properties of the constraint expression. */
147 finish_constraint_binary_op (location_t loc
,
152 gcc_assert (processing_constraint_expression_p ());
153 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
154 return error_mark_node
;
155 if (!check_constraint_operands (loc
, lhs
, rhs
))
156 return error_mark_node
;
158 cp_expr expr
= build_x_binary_op (loc
, code
,
159 lhs
, TREE_CODE (lhs
),
160 rhs
, TREE_CODE (rhs
),
162 /* When either operand is dependent, the overload set may be non-empty. */
163 if (expr
== error_mark_node
)
164 return error_mark_node
;
165 expr
.set_location (loc
);
166 expr
.set_range (lhs
.get_start (), rhs
.get_finish ());
171 finish_constraint_or_expr (location_t loc
, cp_expr lhs
, cp_expr rhs
)
173 return finish_constraint_binary_op (loc
, TRUTH_ORIF_EXPR
, lhs
, rhs
);
177 finish_constraint_and_expr (location_t loc
, cp_expr lhs
, cp_expr rhs
)
179 return finish_constraint_binary_op (loc
, TRUTH_ANDIF_EXPR
, lhs
, rhs
);
183 finish_constraint_primary_expr (cp_expr expr
)
185 if (expr
== error_mark_node
)
186 return error_mark_node
;
187 if (!check_constraint_atom (expr
))
188 return cp_expr (error_mark_node
, expr
.get_location ());
192 /* Combine two constraint-expressions with a logical-and. */
195 combine_constraint_expressions (tree lhs
, tree rhs
)
197 processing_constraint_expression_sentinel pce
;
202 return finish_constraint_and_expr (input_location
, lhs
, rhs
);
205 /* Extract the template-id from a concept check. For standard and variable
206 checks, this is simply T. For function concept checks, this is the
210 unpack_concept_check (tree t
)
212 gcc_assert (concept_check_p (t
));
214 if (TREE_CODE (t
) == CALL_EXPR
)
215 t
= CALL_EXPR_FN (t
);
217 gcc_assert (TREE_CODE (t
) == TEMPLATE_ID_EXPR
);
221 /* Extract the TEMPLATE_DECL from a concept check. */
224 get_concept_check_template (tree t
)
226 tree id
= unpack_concept_check (t
);
227 tree tmpl
= TREE_OPERAND (id
, 0);
229 tmpl
= OVL_FIRST (tmpl
);
233 /* Returns true if any of the arguments in the template argument list is
234 a wildcard or wildcard pack. */
237 contains_wildcard_p (tree args
)
239 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
241 tree arg
= TREE_VEC_ELT (args
, i
);
242 if (TREE_CODE (arg
) == WILDCARD_DECL
)
248 /*---------------------------------------------------------------------------
249 Resolution of qualified concept names
250 ---------------------------------------------------------------------------*/
252 /* This facility is used to resolve constraint checks from requirement
253 expressions. A constraint check is a call to a function template declared
254 with the keyword 'concept'.
256 The result of resolution is a pair (a TREE_LIST) whose value is the
257 matched declaration, and whose purpose contains the coerced template
258 arguments that can be substituted into the call. */
260 /* Given an overload set OVL, try to find a unique definition that can be
261 instantiated by the template arguments ARGS.
263 This function is not called for arbitrary call expressions. In particular,
264 the call expression must be written with explicit template arguments
265 and no function arguments. For example:
269 If a single match is found, this returns a TREE_LIST whose VALUE
270 is the constraint function (not the template), and its PURPOSE is
271 the complete set of arguments substituted into the parameter list. */
274 resolve_function_concept_overload (tree ovl
, tree args
)
277 tree cands
= NULL_TREE
;
278 for (lkp_iterator
iter (ovl
); iter
; ++iter
)
281 if (TREE_CODE (tmpl
) != TEMPLATE_DECL
)
284 /* Don't try to deduce checks for non-concepts. We often end up trying
285 to resolve constraints in functional casts as part of a
286 postfix-expression. We can save time and headaches by not
287 instantiating those declarations.
289 NOTE: This masks a potential error, caused by instantiating
290 non-deduced contexts using placeholder arguments. */
291 tree fn
= DECL_TEMPLATE_RESULT (tmpl
);
292 if (DECL_ARGUMENTS (fn
))
294 if (!DECL_DECLARED_CONCEPT_P (fn
))
297 /* Remember the candidate if we can deduce a substitution. */
298 ++processing_template_decl
;
299 tree parms
= TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl
));
300 if (tree subst
= coerce_template_parms (parms
, args
, tmpl
))
302 if (subst
== error_mark_node
)
305 cands
= tree_cons (subst
, fn
, cands
);
307 --processing_template_decl
;
311 /* We either had no candidates or failed deductions. */
312 return nerrs
? error_mark_node
: NULL_TREE
;
313 else if (TREE_CHAIN (cands
))
314 /* There are multiple candidates. */
315 return error_mark_node
;
320 /* Determine if the call expression CALL is a constraint check, and
321 return the concept declaration and arguments being checked. If CALL
322 does not denote a constraint check, return NULL. */
325 resolve_function_concept_check (tree call
)
327 gcc_assert (TREE_CODE (call
) == CALL_EXPR
);
329 /* A constraint check must be only a template-id expression.
330 If it's a call to a base-link, its function(s) should be a
331 template-id expression. If this is not a template-id, then
332 it cannot be a concept-check. */
333 tree target
= CALL_EXPR_FN (call
);
334 if (BASELINK_P (target
))
335 target
= BASELINK_FUNCTIONS (target
);
336 if (TREE_CODE (target
) != TEMPLATE_ID_EXPR
)
339 /* Get the overload set and template arguments and try to
340 resolve the target. */
341 tree ovl
= TREE_OPERAND (target
, 0);
343 /* This is a function call of a variable concept... ill-formed. */
344 if (TREE_CODE (ovl
) == TEMPLATE_DECL
)
346 error_at (location_of (call
),
347 "function call of variable concept %qE", call
);
348 return error_mark_node
;
351 tree args
= TREE_OPERAND (target
, 1);
352 return resolve_function_concept_overload (ovl
, args
);
355 /* Returns a pair containing the checked concept and its associated
356 prototype parameter. The result is a TREE_LIST whose TREE_VALUE
357 is the concept (non-template) and whose TREE_PURPOSE contains
358 the converted template arguments, including the deduced prototype
359 parameter (in position 0). */
362 resolve_concept_check (tree check
)
364 gcc_assert (concept_check_p (check
));
365 tree id
= unpack_concept_check (check
);
366 tree tmpl
= TREE_OPERAND (id
, 0);
368 /* If this is an overloaded function concept, perform overload
369 resolution (this only happens when deducing prototype parameters
370 and template introductions). */
371 if (TREE_CODE (tmpl
) == OVERLOAD
)
373 if (OVL_CHAIN (tmpl
))
374 return resolve_function_concept_check (check
);
375 tmpl
= OVL_FIRST (tmpl
);
378 tree args
= TREE_OPERAND (id
, 1);
379 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
380 ++processing_template_decl
;
381 tree result
= coerce_template_parms (parms
, args
, tmpl
);
382 --processing_template_decl
;
383 if (result
== error_mark_node
)
384 return error_mark_node
;
385 return build_tree_list (result
, DECL_TEMPLATE_RESULT (tmpl
));
388 /* Given a call expression or template-id expression to a concept EXPR
389 possibly including a wildcard, deduce the concept being checked and
390 the prototype parameter. Returns true if the constraint and prototype
391 can be deduced and false otherwise. Note that the CHECK and PROTO
392 arguments are set to NULL_TREE if this returns false. */
395 deduce_constrained_parameter (tree expr
, tree
& check
, tree
& proto
)
397 tree info
= resolve_concept_check (expr
);
398 if (info
&& info
!= error_mark_node
)
400 check
= TREE_VALUE (info
);
401 tree arg
= TREE_VEC_ELT (TREE_PURPOSE (info
), 0);
402 if (ARGUMENT_PACK_P (arg
))
403 arg
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg
), 0);
404 proto
= TREE_TYPE (arg
);
408 check
= proto
= NULL_TREE
;
412 /* Given a call expression or template-id expression to a concept, EXPR,
413 deduce the concept being checked and return the template arguments.
414 Returns NULL_TREE if deduction fails. */
416 deduce_concept_introduction (tree check
)
418 tree info
= resolve_concept_check (check
);
419 if (info
&& info
!= error_mark_node
)
420 return TREE_PURPOSE (info
);
424 /* Build a constrained placeholder type where SPEC is a type-constraint.
425 SPEC can be anything were concept_definition_p is true.
427 If DECLTYPE_P is true, then the placeholder is decltype(auto).
429 Returns a pair whose FIRST is the concept being checked and whose
430 SECOND is the prototype parameter. */
433 finish_type_constraints (tree spec
, tree args
, tsubst_flags_t complain
)
435 gcc_assert (concept_definition_p (spec
));
437 /* Build an initial concept check. */
438 tree check
= build_type_constraint (spec
, args
, complain
);
439 if (check
== error_mark_node
)
440 return std::make_pair (error_mark_node
, NULL_TREE
);
442 /* Extract the concept and prototype parameter from the check. */
445 if (!deduce_constrained_parameter (check
, con
, proto
))
446 return std::make_pair (error_mark_node
, NULL_TREE
);
448 return std::make_pair (con
, proto
);
451 /*---------------------------------------------------------------------------
452 Expansion of concept definitions
453 ---------------------------------------------------------------------------*/
455 /* Returns the expression of a function concept. */
458 get_returned_expression (tree fn
)
460 /* Extract the body of the function minus the return expression. */
461 tree body
= DECL_SAVED_TREE (fn
);
463 return error_mark_node
;
464 if (TREE_CODE (body
) == BIND_EXPR
)
465 body
= BIND_EXPR_BODY (body
);
466 if (TREE_CODE (body
) != RETURN_EXPR
)
467 return error_mark_node
;
469 return TREE_OPERAND (body
, 0);
472 /* Returns the initializer of a variable concept. */
475 get_variable_initializer (tree var
)
477 tree init
= DECL_INITIAL (var
);
479 return error_mark_node
;
480 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
481 && CONSTRUCTOR_NELTS (init
) == 1)
482 init
= CONSTRUCTOR_ELT (init
, 0)->value
;
486 /* Returns the definition of a variable or function concept. */
489 get_concept_definition (tree decl
)
491 if (TREE_CODE (decl
) == OVERLOAD
)
492 decl
= OVL_FIRST (decl
);
494 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
495 decl
= DECL_TEMPLATE_RESULT (decl
);
497 if (TREE_CODE (decl
) == CONCEPT_DECL
)
498 return DECL_INITIAL (decl
);
500 return get_variable_initializer (decl
);
501 if (TREE_CODE (decl
) == FUNCTION_DECL
)
502 return get_returned_expression (decl
);
506 /*---------------------------------------------------------------------------
507 Normalization of expressions
509 This set of functions will transform an expression into a constraint
510 in a sequence of steps.
511 ---------------------------------------------------------------------------*/
514 debug_parameter_mapping (tree map
)
516 for (tree p
= map
; p
; p
= TREE_CHAIN (p
))
518 tree parm
= TREE_VALUE (p
);
519 tree arg
= TREE_PURPOSE (p
);
521 verbatim ("MAP %qD TO %qT", TEMPLATE_TYPE_DECL (parm
), arg
);
523 verbatim ("MAP %qD TO %qE", TEMPLATE_PARM_DECL (parm
), arg
);
524 // debug_tree (parm);
530 debug_argument_list (tree args
)
532 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
534 tree arg
= TREE_VEC_ELT (args
, i
);
536 verbatim ("ARG %qT", arg
);
538 verbatim ("ARG %qE", arg
);
542 /* Associate each parameter in PARMS with its corresponding template
546 map_arguments (tree parms
, tree args
)
548 for (tree p
= parms
; p
; p
= TREE_CHAIN (p
))
553 template_parm_level_and_index (TREE_VALUE (p
), &level
, &index
);
554 TREE_PURPOSE (p
) = TMPL_ARG (args
, level
, index
);
557 TREE_PURPOSE (p
) = template_parm_to_arg (p
);
562 /* Build the parameter mapping for EXPR using ARGS. */
565 build_parameter_mapping (tree expr
, tree args
, tree decl
)
567 tree ctx_parms
= NULL_TREE
;
570 gcc_assert (TREE_CODE (decl
) == TEMPLATE_DECL
);
571 ctx_parms
= DECL_TEMPLATE_PARMS (decl
);
573 else if (current_template_parms
)
575 /* TODO: This should probably be the only case, but because the
576 point of declaration of concepts is currently set after the
577 initializer, the template parameter lists are not available
578 when normalizing concept definitions, hence the case above. */
579 ctx_parms
= current_template_parms
;
582 tree parms
= find_template_parameters (expr
, ctx_parms
);
583 tree map
= map_arguments (parms
, args
);
587 /* True if the parameter mappings of two atomic constraints are equivalent. */
590 parameter_mapping_equivalent_p (tree t1
, tree t2
)
592 tree map1
= ATOMIC_CONSTR_MAP (t1
);
593 tree map2
= ATOMIC_CONSTR_MAP (t2
);
596 tree arg1
= TREE_PURPOSE (map1
);
597 tree arg2
= TREE_PURPOSE (map2
);
598 if (!template_args_equal (arg1
, arg2
))
600 map1
= TREE_CHAIN (map1
);
601 map2
= TREE_CHAIN (map2
);
606 /* Provides additional context for normalization. */
608 struct norm_info
: subst_info
610 explicit norm_info (tsubst_flags_t complain
)
611 : subst_info (tf_warning_or_error
| complain
, NULL_TREE
),
615 /* Construct a top-level context for DECL. */
617 norm_info (tree in_decl
, tsubst_flags_t complain
)
618 : subst_info (tf_warning_or_error
| complain
, in_decl
),
619 context (make_context (in_decl
))
622 bool generate_diagnostics() const
624 return complain
& tf_norm
;
627 tree
make_context(tree in_decl
)
629 if (generate_diagnostics ())
630 return build_tree_list (NULL_TREE
, in_decl
);
634 void update_context(tree expr
, tree args
)
636 if (generate_diagnostics ())
638 tree map
= build_parameter_mapping (expr
, args
, in_decl
);
639 context
= tree_cons (map
, expr
, context
);
641 in_decl
= get_concept_check_template (expr
);
644 /* Provides information about the source of a constraint. This is a
645 TREE_LIST whose VALUE is either a concept check or a constrained
646 declaration. The PURPOSE, for concept checks is a parameter mapping
652 static tree
normalize_expression (tree
, tree
, norm_info
);
654 /* Transform a logical-or or logical-and expression into either
655 a conjunction or disjunction. */
658 normalize_logical_operation (tree t
, tree args
, tree_code c
, norm_info info
)
660 tree t0
= normalize_expression (TREE_OPERAND (t
, 0), args
, info
);
661 tree t1
= normalize_expression (TREE_OPERAND (t
, 1), args
, info
);
663 /* Build a new info object for the constraint. */
664 tree ci
= info
.generate_diagnostics()
665 ? build_tree_list (t
, info
.context
)
668 return build2 (c
, ci
, t0
, t1
);
672 normalize_concept_check (tree check
, tree args
, norm_info info
)
674 tree id
= unpack_concept_check (check
);
675 tree tmpl
= TREE_OPERAND (id
, 0);
676 tree targs
= TREE_OPERAND (id
, 1);
678 /* A function concept is wrapped in an overload. */
679 if (TREE_CODE (tmpl
) == OVERLOAD
)
681 /* TODO: Can we diagnose this error during parsing? */
682 if (TREE_CODE (check
) == TEMPLATE_ID_EXPR
)
683 error_at (EXPR_LOC_OR_LOC (check
, input_location
),
684 "function concept must be called");
685 tmpl
= OVL_FIRST (tmpl
);
688 /* Substitute through the arguments of the concept check. */
690 targs
= tsubst_template_args (targs
, args
, info
.complain
, info
.in_decl
);
691 if (targs
== error_mark_node
)
692 return error_mark_node
;
694 /* Build the substitution for the concept definition. */
695 tree parms
= TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl
));
696 /* Turn on template processing; coercing non-type template arguments
697 will automatically assume they're non-dependent. */
698 ++processing_template_decl
;
699 tree subst
= coerce_template_parms (parms
, targs
, tmpl
);
700 --processing_template_decl
;
701 if (subst
== error_mark_node
)
702 return error_mark_node
;
704 /* The concept may have been ill-formed. */
705 tree def
= get_concept_definition (DECL_TEMPLATE_RESULT (tmpl
));
706 if (def
== error_mark_node
)
707 return error_mark_node
;
709 info
.update_context (check
, args
);
710 return normalize_expression (def
, subst
, info
);
713 /* The normal form of an atom depends on the expression. The normal
714 form of a function call to a function concept is a check constraint
715 for that concept. The normal form of a reference to a variable
716 concept is a check constraint for that concept. Otherwise, the
717 constraint is a predicate constraint. */
720 normalize_atom (tree t
, tree args
, norm_info info
)
722 /* Concept checks are not atomic. */
723 if (concept_check_p (t
))
724 return normalize_concept_check (t
, args
, info
);
726 /* Build the parameter mapping for the atom. */
727 tree map
= build_parameter_mapping (t
, args
, info
.in_decl
);
729 /* Build a new info object for the atom. */
730 tree ci
= build_tree_list (t
, info
.context
);
732 return build1 (ATOMIC_CONSTR
, ci
, map
);
735 /* Returns the normal form of an expression. */
738 normalize_expression (tree t
, tree args
, norm_info info
)
743 if (t
== error_mark_node
)
744 return error_mark_node
;
746 switch (TREE_CODE (t
))
748 case TRUTH_ANDIF_EXPR
:
749 return normalize_logical_operation (t
, args
, CONJ_CONSTR
, info
);
750 case TRUTH_ORIF_EXPR
:
751 return normalize_logical_operation (t
, args
, DISJ_CONSTR
, info
);
753 return normalize_atom (t
, args
, info
);
757 /* Cache of the normalized form of constraints. Marked as deletable because it
758 can all be recalculated. */
759 static GTY((deletable
)) hash_map
<tree
,tree
> *normalized_map
;
762 get_normalized_constraints (tree t
, norm_info info
)
764 auto_timevar
time (TV_CONSTRAINT_NORM
);
765 return normalize_expression (t
, NULL_TREE
, info
);
768 /* Returns the normalized constraints from a constraint-info object
769 or NULL_TREE if the constraints are null. IN_DECL provides the
770 declaration to which the constraints belong. */
773 get_normalized_constraints_from_info (tree ci
, tree in_decl
, bool diag
= false)
778 /* Substitution errors during normalization are fatal. */
779 ++processing_template_decl
;
780 norm_info
info (in_decl
, diag
? tf_norm
: tf_none
);
781 tree t
= get_normalized_constraints (CI_ASSOCIATED_CONSTRAINTS (ci
), info
);
782 --processing_template_decl
;
787 /* Returns the normalized constraints for the declaration D. */
790 get_normalized_constraints_from_decl (tree d
, bool diag
= false)
795 /* For inherited constructors, consider the original declaration;
796 it has the correct template information attached. */
797 d
= strip_inheriting_ctors (d
);
799 if (TREE_CODE (d
) == TEMPLATE_DECL
)
802 decl
= DECL_TEMPLATE_RESULT (tmpl
);
806 if (tree ti
= DECL_TEMPLATE_INFO (d
))
807 tmpl
= TI_TEMPLATE (ti
);
813 /* Get the most general template for the declaration, and compute
814 arguments from that. This ensures that the arguments used for
815 normalization are always template parameters and not arguments
816 used for outer specializations. For example:
820 template<typename U> requires C<T, U> void f(U);
825 When we normalize the requirements for S<int>::f, we want the
826 arguments to be {T, U}, not {int, U}. One reason for this is that
827 accepting the latter causes the template parameter level of U
828 to be reduced in a way that makes it overly difficult substitute
829 concrete arguments (i.e., eventually {int, int} during satisfaction. */
832 if (DECL_LANG_SPECIFIC(tmpl
) && !DECL_TEMPLATE_SPECIALIZATION (tmpl
))
833 tmpl
= most_general_template (tmpl
);
836 /* If we're not diagnosing errors, use cached constraints, if any. */
838 if (tree
*p
= hash_map_safe_get (normalized_map
, tmpl
))
841 push_nested_class_guard
pncs (DECL_CONTEXT (d
));
843 tree ci
= get_constraints (decl
);
844 tree norm
= get_normalized_constraints_from_info (ci
, tmpl
, diag
);
847 hash_map_safe_put
<hm_ggc
> (normalized_map
, tmpl
, norm
);
852 /* Returns the normal form of TMPL's definition. */
855 normalize_concept_definition (tree tmpl
, bool diag
= false)
858 if (tree
*p
= hash_map_safe_get (normalized_map
, tmpl
))
861 gcc_assert (concept_definition_p (tmpl
));
863 tmpl
= OVL_FIRST (tmpl
);
864 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
865 tree def
= get_concept_definition (DECL_TEMPLATE_RESULT (tmpl
));
866 ++processing_template_decl
;
867 norm_info
info (tmpl
, diag
? tf_norm
: tf_none
);
868 tree norm
= get_normalized_constraints (def
, info
);
869 --processing_template_decl
;
872 hash_map_safe_put
<hm_ggc
> (normalized_map
, tmpl
, norm
);
877 /* Returns the normal form of TMPL's requirements. */
880 normalize_template_requirements (tree tmpl
, bool diag
= false)
882 return get_normalized_constraints_from_decl (tmpl
, diag
);
885 /* Returns the normal form of TMPL's requirements. */
888 normalize_nontemplate_requirements (tree decl
, bool diag
= false)
890 return get_normalized_constraints_from_decl (decl
, diag
);
893 /* Normalize an EXPR as a constraint. */
896 normalize_constraint_expression (tree expr
, bool diag
)
898 if (!expr
|| expr
== error_mark_node
)
900 ++processing_template_decl
;
901 norm_info
info (diag
? tf_norm
: tf_none
);
902 tree norm
= get_normalized_constraints (expr
, info
);
903 --processing_template_decl
;
907 /* 17.4.1.2p2. Two constraints are identical if they are formed
908 from the same expression and the targets of the parameter mapping
912 atomic_constraints_identical_p (tree t1
, tree t2
)
914 gcc_assert (TREE_CODE (t1
) == ATOMIC_CONSTR
);
915 gcc_assert (TREE_CODE (t2
) == ATOMIC_CONSTR
);
917 if (ATOMIC_CONSTR_EXPR (t1
) != ATOMIC_CONSTR_EXPR (t2
))
920 if (!parameter_mapping_equivalent_p (t1
, t2
))
926 /* True if T1 and T2 are equivalent, meaning they have the same syntactic
927 structure and all corresponding constraints are identical. */
930 constraints_equivalent_p (tree t1
, tree t2
)
932 gcc_assert (CONSTR_P (t1
));
933 gcc_assert (CONSTR_P (t2
));
935 if (TREE_CODE (t1
) != TREE_CODE (t2
))
938 switch (TREE_CODE (t1
))
942 if (!constraints_equivalent_p (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0)))
944 if (!constraints_equivalent_p (TREE_OPERAND (t1
, 1), TREE_OPERAND (t2
, 1)))
948 if (!atomic_constraints_identical_p(t1
, t2
))
957 /* Compute the hash value for T. */
960 hash_atomic_constraint (tree t
)
962 gcc_assert (TREE_CODE (t
) == ATOMIC_CONSTR
);
964 /* Hash the identity of the expression. */
965 hashval_t val
= htab_hash_pointer (ATOMIC_CONSTR_EXPR (t
));
967 /* Hash the targets of the parameter map. */
968 tree p
= ATOMIC_CONSTR_MAP (t
);
971 val
= iterative_hash_template_arg (TREE_PURPOSE (p
), val
);
982 add_constraint (tree t
, hash
& h
)
984 h
.add_int(TREE_CODE (t
));
985 switch (TREE_CODE (t
))
989 add_constraint (TREE_OPERAND (t
, 0), h
);
990 add_constraint (TREE_OPERAND (t
, 1), h
);
993 h
.merge_hash (hash_atomic_constraint (t
));
1002 /* Computes a hash code for the constraint T. */
1005 iterative_hash_constraint (tree t
, hashval_t val
)
1007 gcc_assert (CONSTR_P (t
));
1008 inchash::hash
h (val
);
1009 inchash::add_constraint (t
, h
);
1013 // -------------------------------------------------------------------------- //
1014 // Constraint Semantic Processing
1016 // The following functions are called by the parser and substitution rules
1017 // to create and evaluate constraint-related nodes.
1019 // The constraints associated with the current template parameters.
1021 current_template_constraints (void)
1023 if (!current_template_parms
)
1025 tree tmpl_constr
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
1026 return build_constraints (tmpl_constr
, NULL_TREE
);
1029 /* If the recently parsed TYPE declares or defines a template or
1030 template specialization, get its corresponding constraints from the
1031 current template parameters and bind them to TYPE's declaration. */
1034 associate_classtype_constraints (tree type
)
1036 if (!type
|| type
== error_mark_node
|| !CLASS_TYPE_P (type
))
1039 /* An explicit class template specialization has no template parameters. */
1040 if (!current_template_parms
)
1043 if (CLASSTYPE_IS_TEMPLATE (type
) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1045 tree decl
= TYPE_STUB_DECL (type
);
1046 tree ci
= current_template_constraints ();
1048 /* An implicitly instantiated member template declaration already
1049 has associated constraints. If it is defined outside of its
1050 class, then we need match these constraints against those of
1051 original declaration. */
1052 if (tree orig_ci
= get_constraints (decl
))
1054 if (int extra_levels
= (TMPL_PARMS_DEPTH (current_template_parms
)
1055 - TMPL_ARGS_DEPTH (TYPE_TI_ARGS (type
))))
1057 /* If there is a discrepancy between the current template depth
1058 and the template depth of the original declaration, then we
1059 must be redeclaring a class template as part of a friend
1060 declaration within another class template. Before matching
1061 constraints, we need to reduce the template parameter level
1062 within the current constraints via substitution. */
1063 tree outer_gtargs
= template_parms_to_args (current_template_parms
);
1064 TREE_VEC_LENGTH (outer_gtargs
) = extra_levels
;
1065 ci
= tsubst_constraint_info (ci
, outer_gtargs
, tf_none
, NULL_TREE
);
1067 if (!equivalent_constraints (ci
, orig_ci
))
1069 error ("%qT does not match original declaration", type
);
1070 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1071 location_t loc
= DECL_SOURCE_LOCATION (tmpl
);
1072 inform (loc
, "original template declaration here");
1073 /* Fall through, so that we define the type anyway. */
1077 set_constraints (decl
, ci
);
1082 /* Create an empty constraint info block. */
1084 static inline tree_constraint_info
*
1085 build_constraint_info ()
1087 return (tree_constraint_info
*)make_node (CONSTRAINT_INFO
);
1090 /* Build a constraint-info object that contains the associated constraints
1091 of a declaration. This also includes the declaration's template
1092 requirements (TREQS) and any trailing requirements for a function
1093 declarator (DREQS). Note that both TREQS and DREQS must be constraints.
1095 If the declaration has neither template nor declaration requirements
1096 this returns NULL_TREE, indicating an unconstrained declaration. */
1099 build_constraints (tree tr
, tree dr
)
1104 tree_constraint_info
* ci
= build_constraint_info ();
1105 ci
->template_reqs
= tr
;
1106 ci
->declarator_reqs
= dr
;
1107 ci
->associated_constr
= combine_constraint_expressions (tr
, dr
);
1112 /* Add constraint RHS to the end of CONSTRAINT_INFO ci. */
1115 append_constraint (tree ci
, tree rhs
)
1117 tree tr
= ci
? CI_TEMPLATE_REQS (ci
) : NULL_TREE
;
1118 tree dr
= ci
? CI_DECLARATOR_REQS (ci
) : NULL_TREE
;
1119 dr
= combine_constraint_expressions (dr
, rhs
);
1122 CI_DECLARATOR_REQS (ci
) = dr
;
1123 tree ac
= combine_constraint_expressions (tr
, dr
);
1124 CI_ASSOCIATED_CONSTRAINTS (ci
) = ac
;
1127 ci
= build_constraints (tr
, dr
);
1131 /* A mapping from declarations to constraint information. */
1133 static GTY ((cache
)) decl_tree_cache_map
*decl_constraints
;
1135 /* Returns the template constraints of declaration T. If T is not
1136 constrained, return NULL_TREE. Note that T must be non-null. */
1139 get_constraints (const_tree t
)
1143 if (!decl_constraints
)
1146 gcc_assert (DECL_P (t
));
1147 if (TREE_CODE (t
) == TEMPLATE_DECL
)
1148 t
= DECL_TEMPLATE_RESULT (t
);
1149 tree
* found
= decl_constraints
->get (CONST_CAST_TREE (t
));
1156 /* Associate the given constraint information CI with the declaration
1157 T. If T is a template, then the constraints are associated with
1158 its underlying declaration. Don't build associations if CI is
1162 set_constraints (tree t
, tree ci
)
1166 gcc_assert (t
&& flag_concepts
);
1167 if (TREE_CODE (t
) == TEMPLATE_DECL
)
1168 t
= DECL_TEMPLATE_RESULT (t
);
1169 bool found
= hash_map_safe_put
<hm_ggc
> (decl_constraints
, t
, ci
);
1170 gcc_assert (!found
);
1173 /* Remove the associated constraints of the declaration T. */
1176 remove_constraints (tree t
)
1178 gcc_checking_assert (DECL_P (t
));
1179 if (TREE_CODE (t
) == TEMPLATE_DECL
)
1180 t
= DECL_TEMPLATE_RESULT (t
);
1182 if (decl_constraints
)
1183 decl_constraints
->remove (t
);
1186 /* If DECL is a friend, substitute into REQS to produce requirements suitable
1187 for declaration matching. */
1190 maybe_substitute_reqs_for (tree reqs
, const_tree decl_
)
1192 if (reqs
== NULL_TREE
)
1195 tree decl
= CONST_CAST_TREE (decl_
);
1196 tree result
= STRIP_TEMPLATE (decl
);
1198 if (DECL_UNIQUE_FRIEND_P (result
))
1201 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
1202 tmpl
= DECL_TI_TEMPLATE (result
);
1204 tree gargs
= generic_targs_for (tmpl
);
1205 processing_template_decl_sentinel s
;
1206 if (uses_template_parms (gargs
))
1207 ++processing_template_decl
;
1208 reqs
= tsubst_constraint (reqs
, gargs
,
1209 tf_warning_or_error
, NULL_TREE
);
1214 /* Returns the template-head requires clause for the template
1215 declaration T or NULL_TREE if none. */
1218 get_template_head_requirements (tree t
)
1220 tree ci
= get_constraints (t
);
1223 return CI_TEMPLATE_REQS (ci
);
1226 /* Returns the trailing requires clause of the declarator of
1227 a template declaration T or NULL_TREE if none. */
1230 get_trailing_function_requirements (tree t
)
1232 tree ci
= get_constraints (t
);
1235 return CI_DECLARATOR_REQS (ci
);
1238 /* Construct a sequence of template arguments by prepending
1239 ARG to REST. Either ARG or REST may be null. */
1241 build_concept_check_arguments (tree arg
, tree rest
)
1243 gcc_assert (rest
? TREE_CODE (rest
) == TREE_VEC
: true);
1247 int n
= rest
? TREE_VEC_LENGTH (rest
) : 0;
1248 args
= make_tree_vec (n
+ 1);
1249 TREE_VEC_ELT (args
, 0) = arg
;
1251 for (int i
= 0; i
< n
; ++i
)
1252 TREE_VEC_ELT (args
, i
+ 1) = TREE_VEC_ELT (rest
, i
);
1253 int def
= rest
? GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (rest
) : 0;
1254 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
, def
+ 1);
1258 gcc_assert (rest
!= NULL_TREE
);
1264 /* Builds an id-expression of the form `C<Args...>()` where C is a function
1268 build_function_check (tree tmpl
, tree args
, tsubst_flags_t
/*complain*/)
1270 if (TREE_CODE (tmpl
) == TEMPLATE_DECL
)
1272 /* If we just got a template, wrap it in an overload so it looks like any
1273 other template-id. */
1274 tmpl
= ovl_make (tmpl
);
1275 TREE_TYPE (tmpl
) = boolean_type_node
;
1278 /* Perform function concept resolution now so we always have a single
1279 function of the overload set (even if we started with only one; the
1280 resolution function converts template arguments). Note that we still
1281 wrap this in an overload set so we don't upset other parts of the
1282 compiler that expect template-ids referring to function concepts
1283 to have an overload set. */
1284 tree info
= resolve_function_concept_overload (tmpl
, args
);
1285 if (info
== error_mark_node
)
1286 return error_mark_node
;
1289 error ("no matching concepts for %qE", tmpl
);
1290 return error_mark_node
;
1292 args
= TREE_PURPOSE (info
);
1293 tmpl
= DECL_TI_TEMPLATE (TREE_VALUE (info
));
1295 /* Rebuild the singleton overload set; mark the type bool. */
1296 tmpl
= ovl_make (tmpl
, NULL_TREE
);
1297 TREE_TYPE (tmpl
) = boolean_type_node
;
1299 /* Build the id-expression around the overload set. */
1300 tree id
= build2 (TEMPLATE_ID_EXPR
, boolean_type_node
, tmpl
, args
);
1302 /* Finally, build the call expression around the overload. */
1303 ++processing_template_decl
;
1304 vec
<tree
, va_gc
> *fargs
= make_tree_vector ();
1305 tree call
= build_min_nt_call_vec (id
, fargs
);
1306 TREE_TYPE (call
) = boolean_type_node
;
1307 release_tree_vector (fargs
);
1308 --processing_template_decl
;
1313 /* Builds an id-expression of the form `C<Args...>` where C is a variable
1317 build_variable_check (tree tmpl
, tree args
, tsubst_flags_t complain
)
1319 gcc_assert (variable_concept_p (tmpl
));
1320 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
1321 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
1322 args
= coerce_template_parms (parms
, args
, tmpl
, complain
);
1323 if (args
== error_mark_node
)
1324 return error_mark_node
;
1325 return build2 (TEMPLATE_ID_EXPR
, boolean_type_node
, tmpl
, args
);
1328 /* Builds an id-expression of the form `C<Args...>` where C is a standard
1332 build_standard_check (tree tmpl
, tree args
, tsubst_flags_t complain
)
1334 gcc_assert (standard_concept_p (tmpl
));
1335 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
1336 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
1337 args
= coerce_template_parms (parms
, args
, tmpl
, complain
);
1338 if (args
== error_mark_node
)
1339 return error_mark_node
;
1340 return build2 (TEMPLATE_ID_EXPR
, boolean_type_node
, tmpl
, args
);
1343 /* Construct an expression that checks TARGET using ARGS. */
1346 build_concept_check (tree target
, tree args
, tsubst_flags_t complain
)
1348 return build_concept_check (target
, NULL_TREE
, args
, complain
);
1351 /* Construct an expression that checks the concept given by DECL. If
1352 concept_definition_p (DECL) is false, this returns null. */
1355 build_concept_check (tree decl
, tree arg
, tree rest
, tsubst_flags_t complain
)
1357 if (arg
== NULL_TREE
&& rest
== NULL_TREE
)
1359 tree id
= build_nt (TEMPLATE_ID_EXPR
, decl
, rest
);
1360 error ("invalid use concept %qE", id
);
1361 return error_mark_node
;
1364 tree args
= build_concept_check_arguments (arg
, rest
);
1366 if (standard_concept_p (decl
))
1367 return build_standard_check (decl
, args
, complain
);
1368 if (variable_concept_p (decl
))
1369 return build_variable_check (decl
, args
, complain
);
1370 if (function_concept_p (decl
))
1371 return build_function_check (decl
, args
, complain
);
1373 return error_mark_node
;
1376 /* Build a template-id that can participate in a concept check. */
1379 build_concept_id (tree decl
, tree args
)
1381 tree check
= build_concept_check (decl
, args
, tf_warning_or_error
);
1382 if (check
== error_mark_node
)
1383 return error_mark_node
;
1384 return unpack_concept_check (check
);
1387 /* Build a template-id that can participate in a concept check, preserving
1388 the source location of the original template-id. */
1391 build_concept_id (tree expr
)
1393 gcc_assert (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
);
1394 tree id
= build_concept_id (TREE_OPERAND (expr
, 0), TREE_OPERAND (expr
, 1));
1395 protected_set_expr_location (id
, cp_expr_location (expr
));
1399 /* Build as template-id with a placeholder that can be used as a
1402 Note that this will diagnose errors if the initial concept check
1406 build_type_constraint (tree decl
, tree args
, tsubst_flags_t complain
)
1408 tree wildcard
= build_nt (WILDCARD_DECL
);
1409 ++processing_template_decl
;
1410 tree check
= build_concept_check (decl
, wildcard
, args
, complain
);
1411 --processing_template_decl
;
1412 if (check
== error_mark_node
)
1413 return error_mark_node
;
1414 return unpack_concept_check (check
);
1417 /* Returns a TYPE_DECL that contains sufficient information to
1418 build a template parameter of the same kind as PROTO and
1419 constrained by the concept declaration CNC. Note that PROTO
1420 is the first template parameter of CNC.
1422 If specified, ARGS provides additional arguments to the
1423 constraint check. */
1425 build_constrained_parameter (tree cnc
, tree proto
, tree args
)
1427 tree name
= DECL_NAME (cnc
);
1428 tree type
= TREE_TYPE (proto
);
1429 tree decl
= build_decl (input_location
, TYPE_DECL
, name
, type
);
1430 CONSTRAINED_PARM_PROTOTYPE (decl
) = proto
;
1431 CONSTRAINED_PARM_CONCEPT (decl
) = cnc
;
1432 CONSTRAINED_PARM_EXTRA_ARGS (decl
) = args
;
1436 /* Create a constraint expression for the given DECL that evaluates the
1437 requirements specified by CONSTR, a TYPE_DECL that contains all the
1438 information necessary to build the requirements (see finish_concept_name
1439 for the layout of that TYPE_DECL).
1441 Note that the constraints are neither reduced nor decomposed. That is
1442 done only after the requires clause has been parsed (or not). */
1445 finish_shorthand_constraint (tree decl
, tree constr
)
1447 /* No requirements means no constraints. */
1451 if (error_operand_p (constr
))
1454 tree proto
= CONSTRAINED_PARM_PROTOTYPE (constr
);
1455 tree con
= CONSTRAINED_PARM_CONCEPT (constr
);
1456 tree args
= CONSTRAINED_PARM_EXTRA_ARGS (constr
);
1458 /* The TS lets use shorthand to constrain a pack of arguments, but the
1461 For the TS, consider:
1463 template<C... Ts> struct s;
1465 If C is variadic (and because Ts is a pack), we associate the
1466 constraint C<Ts...>. In all other cases, we associate
1467 the constraint (C<Ts> && ...).
1469 The standard behavior cannot be overridden by -fconcepts-ts. */
1470 bool variadic_concept_p
= template_parameter_pack_p (proto
);
1471 bool declared_pack_p
= template_parameter_pack_p (decl
);
1472 bool apply_to_each_p
= (cxx_dialect
>= cxx20
) ? true : !variadic_concept_p
;
1474 /* Get the argument and overload used for the requirement
1475 and adjust it if we're going to expand later. */
1476 tree arg
= template_parm_to_arg (decl
);
1477 if (apply_to_each_p
&& declared_pack_p
)
1478 arg
= PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg
), 0));
1480 /* Build the concept constraint-expression. */
1481 tree tmpl
= DECL_TI_TEMPLATE (con
);
1483 if (TREE_CODE (con
) == FUNCTION_DECL
)
1484 check
= ovl_make (tmpl
);
1485 check
= build_concept_check (check
, arg
, args
, tf_warning_or_error
);
1487 /* Make the check a fold-expression if needed. */
1488 if (apply_to_each_p
&& declared_pack_p
)
1489 check
= finish_left_unary_fold_expr (check
, TRUTH_ANDIF_EXPR
);
1494 /* Returns a conjunction of shorthand requirements for the template
1495 parameter list PARMS. Note that the requirements are stored in
1496 the TYPE of each tree node. */
1499 get_shorthand_constraints (tree parms
)
1501 tree result
= NULL_TREE
;
1502 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
1503 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
1505 tree parm
= TREE_VEC_ELT (parms
, i
);
1506 tree constr
= TEMPLATE_PARM_CONSTRAINTS (parm
);
1507 result
= combine_constraint_expressions (result
, constr
);
1512 /* Get the deduced wildcard from a DEDUCED placeholder. If the deduced
1513 wildcard is a pack, return the first argument of that pack. */
1516 get_deduced_wildcard (tree wildcard
)
1518 if (ARGUMENT_PACK_P (wildcard
))
1519 wildcard
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (wildcard
), 0);
1520 gcc_assert (TREE_CODE (wildcard
) == WILDCARD_DECL
);
1524 /* Returns the prototype parameter for the nth deduced wildcard. */
1527 get_introduction_prototype (tree wildcards
, int index
)
1529 return TREE_TYPE (get_deduced_wildcard (TREE_VEC_ELT (wildcards
, index
)));
1532 /* Introduce a type template parameter. */
1535 introduce_type_template_parameter (tree wildcard
, bool& non_type_p
)
1538 return finish_template_type_parm (class_type_node
, DECL_NAME (wildcard
));
1541 /* Introduce a template template parameter. */
1544 introduce_template_template_parameter (tree wildcard
, bool& non_type_p
)
1547 begin_template_parm_list ();
1548 current_template_parms
= DECL_TEMPLATE_PARMS (TREE_TYPE (wildcard
));
1549 end_template_parm_list ();
1550 return finish_template_template_parm (class_type_node
, DECL_NAME (wildcard
));
1553 /* Introduce a template non-type parameter. */
1556 introduce_nontype_template_parameter (tree wildcard
, bool& non_type_p
)
1559 tree parm
= copy_decl (TREE_TYPE (wildcard
));
1560 DECL_NAME (parm
) = DECL_NAME (wildcard
);
1564 /* Introduce a single template parameter. */
1567 build_introduced_template_parameter (tree wildcard
, bool& non_type_p
)
1569 tree proto
= TREE_TYPE (wildcard
);
1572 if (TREE_CODE (proto
) == TYPE_DECL
)
1573 parm
= introduce_type_template_parameter (wildcard
, non_type_p
);
1574 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
1575 parm
= introduce_template_template_parameter (wildcard
, non_type_p
);
1577 parm
= introduce_nontype_template_parameter (wildcard
, non_type_p
);
1579 /* Wrap in a TREE_LIST for process_template_parm. Note that introduced
1580 parameters do not retain the defaults from the source parameter. */
1581 return build_tree_list (NULL_TREE
, parm
);
1584 /* Introduce a single template parameter. */
1587 introduce_template_parameter (tree parms
, tree wildcard
)
1589 gcc_assert (!ARGUMENT_PACK_P (wildcard
));
1590 tree proto
= TREE_TYPE (wildcard
);
1591 location_t loc
= DECL_SOURCE_LOCATION (wildcard
);
1593 /* Diagnose the case where we have C{...Args}. */
1594 if (WILDCARD_PACK_P (wildcard
))
1596 tree id
= DECL_NAME (wildcard
);
1597 error_at (loc
, "%qE cannot be introduced with an ellipsis %<...%>", id
);
1598 inform (DECL_SOURCE_LOCATION (proto
), "prototype declared here");
1602 tree parm
= build_introduced_template_parameter (wildcard
, non_type_p
);
1603 return process_template_parm (parms
, loc
, parm
, non_type_p
, false);
1606 /* Introduce a template parameter pack. */
1609 introduce_template_parameter_pack (tree parms
, tree wildcard
)
1612 tree parm
= build_introduced_template_parameter (wildcard
, non_type_p
);
1613 location_t loc
= DECL_SOURCE_LOCATION (wildcard
);
1614 return process_template_parm (parms
, loc
, parm
, non_type_p
, true);
1617 /* Introduce the nth template parameter. */
1620 introduce_template_parameter (tree parms
, tree wildcards
, int& index
)
1622 tree deduced
= TREE_VEC_ELT (wildcards
, index
++);
1623 return introduce_template_parameter (parms
, deduced
);
1626 /* Introduce either a template parameter pack or a list of template
1630 introduce_template_parameters (tree parms
, tree wildcards
, int& index
)
1632 /* If the prototype was a parameter, we better have deduced an
1633 argument pack, and that argument must be the last deduced value
1634 in the wildcard vector. */
1635 tree deduced
= TREE_VEC_ELT (wildcards
, index
++);
1636 gcc_assert (ARGUMENT_PACK_P (deduced
));
1637 gcc_assert (index
== TREE_VEC_LENGTH (wildcards
));
1639 /* Introduce each element in the pack. */
1640 tree args
= ARGUMENT_PACK_ARGS (deduced
);
1641 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
1643 tree arg
= TREE_VEC_ELT (args
, i
);
1644 if (WILDCARD_PACK_P (arg
))
1645 parms
= introduce_template_parameter_pack (parms
, arg
);
1647 parms
= introduce_template_parameter (parms
, arg
);
1653 /* Builds the template parameter list PARMS by chaining introduced
1654 parameters from the WILDCARD vector. INDEX is the position of
1655 the current parameter. */
1658 process_introduction_parms (tree parms
, tree wildcards
, int& index
)
1660 tree proto
= get_introduction_prototype (wildcards
, index
);
1661 if (template_parameter_pack_p (proto
))
1662 return introduce_template_parameters (parms
, wildcards
, index
);
1664 return introduce_template_parameter (parms
, wildcards
, index
);
1667 /* Ensure that all template parameters have been introduced for the concept
1668 named in CHECK. If not, emit a diagnostic.
1670 Note that implicitly introducing a parameter with a default argument
1671 creates a case where a parameter is declared, but unnamed, making
1672 it unusable in the definition. */
1675 check_introduction_list (tree intros
, tree check
)
1677 check
= unpack_concept_check (check
);
1678 tree tmpl
= TREE_OPERAND (check
, 0);
1680 tmpl
= OVL_FIRST (tmpl
);
1682 tree parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
1683 if (TREE_VEC_LENGTH (intros
) < TREE_VEC_LENGTH (parms
))
1685 error_at (input_location
, "all template parameters of %qD must "
1686 "be introduced", tmpl
);
1693 /* Associates a constraint check to the current template based on the
1694 introduction parameters. INTRO_LIST must be a TREE_VEC of WILDCARD_DECLs
1695 containing a chained PARM_DECL which contains the identifier as well as
1696 the source location. TMPL_DECL is the decl for the concept being used.
1697 If we take a concept, C, this will form a check in the form of
1698 C<INTRO_LIST> filling in any extra arguments needed by the defaults
1701 Returns NULL_TREE if no concept could be matched and error_mark_node if
1702 an error occurred when matching. */
1705 finish_template_introduction (tree tmpl_decl
,
1707 location_t intro_loc
)
1709 /* Build a concept check to deduce the actual parameters. */
1710 tree expr
= build_concept_check (tmpl_decl
, intro_list
, tf_none
);
1711 if (expr
== error_mark_node
)
1713 error_at (intro_loc
, "cannot deduce template parameters from "
1714 "introduction list");
1715 return error_mark_node
;
1718 if (!check_introduction_list (intro_list
, expr
))
1719 return error_mark_node
;
1721 tree parms
= deduce_concept_introduction (expr
);
1725 /* Build template parameter scope for introduction. */
1726 tree parm_list
= NULL_TREE
;
1727 begin_template_parm_list ();
1728 int nargs
= MIN (TREE_VEC_LENGTH (parms
), TREE_VEC_LENGTH (intro_list
));
1729 for (int n
= 0; n
< nargs
; )
1730 parm_list
= process_introduction_parms (parm_list
, parms
, n
);
1731 parm_list
= end_template_parm_list (parm_list
);
1733 /* Update the number of arguments to reflect the number of deduced
1734 template parameter introductions. */
1735 nargs
= TREE_VEC_LENGTH (parm_list
);
1737 /* Determine if any errors occurred during matching. */
1738 for (int i
= 0; i
< TREE_VEC_LENGTH (parm_list
); ++i
)
1739 if (TREE_VALUE (TREE_VEC_ELT (parm_list
, i
)) == error_mark_node
)
1741 end_template_decl ();
1742 return error_mark_node
;
1745 /* Build a concept check for our constraint. */
1746 tree check_args
= make_tree_vec (nargs
);
1748 for (; n
< TREE_VEC_LENGTH (parm_list
); ++n
)
1750 tree parm
= TREE_VEC_ELT (parm_list
, n
);
1751 TREE_VEC_ELT (check_args
, n
) = template_parm_to_arg (parm
);
1753 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (check_args
, n
);
1755 /* If the template expects more parameters we should be able
1756 to use the defaults from our deduced concept. */
1757 for (; n
< TREE_VEC_LENGTH (parms
); ++n
)
1758 TREE_VEC_ELT (check_args
, n
) = TREE_VEC_ELT (parms
, n
);
1760 /* Associate the constraint. */
1761 tree check
= build_concept_check (tmpl_decl
,
1763 tf_warning_or_error
);
1764 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = check
;
1770 /* Given the concept check T from a constrained-type-specifier, extract
1771 its TMPL and ARGS. FIXME why do we need two different forms of
1772 constrained-type-specifier? */
1775 placeholder_extract_concept_and_args (tree t
, tree
&tmpl
, tree
&args
)
1777 if (concept_check_p (t
))
1779 t
= unpack_concept_check (t
);
1780 tmpl
= TREE_OPERAND (t
, 0);
1781 if (TREE_CODE (tmpl
) == OVERLOAD
)
1782 tmpl
= OVL_FIRST (tmpl
);
1783 args
= TREE_OPERAND (t
, 1);
1787 if (TREE_CODE (t
) == TYPE_DECL
)
1789 /* A constrained parameter. Build a constraint check
1790 based on the prototype parameter and then extract the
1791 arguments from that. */
1792 tree proto
= CONSTRAINED_PARM_PROTOTYPE (t
);
1793 tree check
= finish_shorthand_constraint (proto
, t
);
1794 placeholder_extract_concept_and_args (check
, tmpl
, args
);
1799 /* Returns true iff the placeholders C1 and C2 are equivalent. C1
1800 and C2 can be either TEMPLATE_TYPE_PARM or template-ids. */
1803 equivalent_placeholder_constraints (tree c1
, tree c2
)
1805 if (c1
&& TREE_CODE (c1
) == TEMPLATE_TYPE_PARM
)
1806 /* A constrained auto. */
1807 c1
= PLACEHOLDER_TYPE_CONSTRAINTS (c1
);
1808 if (c2
&& TREE_CODE (c2
) == TEMPLATE_TYPE_PARM
)
1809 c2
= PLACEHOLDER_TYPE_CONSTRAINTS (c2
);
1815 if (c1
== error_mark_node
|| c2
== error_mark_node
)
1816 /* We get here during satisfaction; when a deduction constraint
1817 fails, substitution can produce an error_mark_node for the
1818 placeholder constraints. */
1821 tree t1
, t2
, a1
, a2
;
1822 placeholder_extract_concept_and_args (c1
, t1
, a1
);
1823 placeholder_extract_concept_and_args (c2
, t2
, a2
);
1828 int len1
= TREE_VEC_LENGTH (a1
);
1829 int len2
= TREE_VEC_LENGTH (a2
);
1833 /* Skip the first argument so we don't infinitely recurse.
1834 Also, they may differ in template parameter index. */
1835 for (int i
= 1; i
< len1
; ++i
)
1837 tree t1
= TREE_VEC_ELT (a1
, i
);
1838 tree t2
= TREE_VEC_ELT (a2
, i
);
1839 if (!template_args_equal (t1
, t2
))
1845 /* Return a hash value for the placeholder ATOMIC_CONSTR C. */
1848 hash_placeholder_constraint (tree c
)
1851 placeholder_extract_concept_and_args (c
, t
, a
);
1853 /* Like hash_tmpl_and_args, but skip the first argument. */
1854 hashval_t val
= iterative_hash_object (DECL_UID (t
), 0);
1856 for (int i
= TREE_VEC_LENGTH (a
)-1; i
> 0; --i
)
1857 val
= iterative_hash_template_arg (TREE_VEC_ELT (a
, i
), val
);
1862 /* Substitute through the simple requirement. */
1865 tsubst_valid_expression_requirement (tree t
, tree args
, subst_info info
)
1867 tree r
= tsubst_expr (t
, args
, info
.complain
, info
.in_decl
, false);
1868 if (convert_to_void (r
, ICV_STATEMENT
, info
.complain
) == error_mark_node
)
1869 return error_mark_node
;
1874 /* Substitute through the simple requirement. */
1877 tsubst_simple_requirement (tree t
, tree args
, subst_info info
)
1879 tree t0
= TREE_OPERAND (t
, 0);
1880 tree expr
= tsubst_valid_expression_requirement (t0
, args
, info
);
1881 if (expr
== error_mark_node
)
1882 return error_mark_node
;
1883 return finish_simple_requirement (EXPR_LOCATION (t
), expr
);
1886 /* Substitute through the type requirement. */
1889 tsubst_type_requirement (tree t
, tree args
, subst_info info
)
1891 tree t0
= TREE_OPERAND (t
, 0);
1892 tree type
= tsubst (t0
, args
, info
.complain
, info
.in_decl
);
1893 if (type
== error_mark_node
)
1894 return error_mark_node
;
1895 return finish_type_requirement (EXPR_LOCATION (t
), type
);
1898 /* True if TYPE can be deduced from EXPR. */
1901 type_deducible_p (tree expr
, tree type
, tree placeholder
, tree args
,
1904 /* Make sure deduction is performed against ( EXPR ), so that
1905 references are preserved in the result. */
1906 expr
= force_paren_expr_uneval (expr
);
1908 /* Replace the constraints with the instantiated constraints. This
1909 substitutes args into any template parameters in the trailing
1911 tree saved_constr
= PLACEHOLDER_TYPE_CONSTRAINTS (placeholder
);
1913 = tsubst_constraint (saved_constr
,
1915 info
.complain
| tf_partial
,
1918 if (subst_constr
== error_mark_node
)
1921 PLACEHOLDER_TYPE_CONSTRAINTS (placeholder
) = subst_constr
;
1923 /* Temporarily unlink the canonical type. */
1924 tree saved_type
= TYPE_CANONICAL (placeholder
);
1925 TYPE_CANONICAL (placeholder
) = NULL_TREE
;
1928 = do_auto_deduction (type
,
1934 PLACEHOLDER_TYPE_CONSTRAINTS (placeholder
) = saved_constr
;
1935 TYPE_CANONICAL (placeholder
) = saved_type
;
1937 if (deduced_type
== error_mark_node
)
1943 /* True if EXPR can not be converted to TYPE. */
1946 expression_convertible_p (tree expr
, tree type
, subst_info info
)
1949 perform_direct_initialization_if_possible (type
, expr
, false,
1951 if (conv
== error_mark_node
)
1953 if (conv
== NULL_TREE
)
1955 if (info
.complain
& tf_error
)
1957 location_t loc
= EXPR_LOC_OR_LOC (expr
, input_location
);
1958 error_at (loc
, "cannot convert %qE to %qT", expr
, type
);
1966 /* Substitute through the compound requirement. */
1969 tsubst_compound_requirement (tree t
, tree args
, subst_info info
)
1971 tree t0
= TREE_OPERAND (t
, 0);
1972 tree t1
= TREE_OPERAND (t
, 1);
1973 tree expr
= tsubst_valid_expression_requirement (t0
, args
, info
);
1974 if (expr
== error_mark_node
)
1975 return error_mark_node
;
1977 /* Check the noexcept condition. */
1978 bool noexcept_p
= COMPOUND_REQ_NOEXCEPT_P (t
);
1979 if (noexcept_p
&& !expr_noexcept_p (expr
, tf_none
))
1980 return error_mark_node
;
1982 /* Substitute through the type expression, if any. */
1983 tree type
= tsubst (t1
, args
, info
.complain
, info
.in_decl
);
1984 if (type
== error_mark_node
)
1985 return error_mark_node
;
1987 subst_info
quiet (tf_none
, info
.in_decl
);
1989 /* Check expression against the result type. */
1992 if (tree placeholder
= type_uses_auto (type
))
1994 if (!type_deducible_p (expr
, type
, placeholder
, args
, quiet
))
1995 return error_mark_node
;
1997 else if (!expression_convertible_p (expr
, type
, quiet
))
1998 return error_mark_node
;
2001 return finish_compound_requirement (EXPR_LOCATION (t
),
2002 expr
, type
, noexcept_p
);
2006 tsubst_nested_requirement (tree t
, tree args
, subst_info info
)
2008 /* Ensure that we're in an evaluation context prior to satisfaction. */
2009 tree norm
= TREE_TYPE (t
);
2010 tree result
= satisfy_constraint (norm
, args
, info
);
2011 if (result
== error_mark_node
&& info
.quiet ())
2013 subst_info
noisy (tf_warning_or_error
, info
.in_decl
);
2014 satisfy_constraint (norm
, args
, noisy
);
2016 if (result
!= boolean_true_node
)
2017 return error_mark_node
;
2021 /* Substitute ARGS into the requirement T. */
2024 tsubst_requirement (tree t
, tree args
, subst_info info
)
2026 iloc_sentinel
loc_s (cp_expr_location (t
));
2027 switch (TREE_CODE (t
))
2030 return tsubst_simple_requirement (t
, args
, info
);
2032 return tsubst_type_requirement (t
, args
, info
);
2034 return tsubst_compound_requirement (t
, args
, info
);
2036 return tsubst_nested_requirement (t
, args
, info
);
2043 /* Substitute ARGS into the list of requirements T. Note that
2044 substitution failures here result in ill-formed programs. */
2047 tsubst_requirement_body (tree t
, tree args
, subst_info info
)
2049 tree result
= NULL_TREE
;
2052 tree req
= tsubst_requirement (TREE_VALUE (t
), args
, info
);
2053 if (req
== error_mark_node
)
2054 return error_mark_node
;
2055 result
= tree_cons (NULL_TREE
, req
, result
);
2058 return nreverse (result
);
2062 declare_constraint_vars (tree parms
, tree vars
)
2065 for (tree t
= parms
; t
; t
= DECL_CHAIN (t
))
2067 if (DECL_PACK_P (t
))
2069 tree pack
= extract_fnparm_pack (t
, &s
);
2070 register_local_specialization (pack
, t
);
2074 register_local_specialization (s
, t
);
2081 /* Substitute through as if checking function parameter types. This
2082 will diagnose common parameter type errors. Returns error_mark_node
2083 if an error occurred. */
2086 check_constaint_variables (tree t
, tree args
, subst_info info
)
2088 tree types
= NULL_TREE
;
2090 while (p
&& !VOID_TYPE_P (p
))
2092 types
= tree_cons (NULL_TREE
, TREE_TYPE (p
), types
);
2095 types
= chainon (nreverse (types
), void_list_node
);
2096 return tsubst_function_parms (types
, args
, info
.complain
, info
.in_decl
);
2099 /* A subroutine of tsubst_parameterized_constraint. Substitute ARGS
2100 into the parameter list T, producing a sequence of constraint
2101 variables, declared in the current scope.
2103 Note that the caller must establish a local specialization stack
2104 prior to calling this function since this substitution will
2105 declare the substituted parameters. */
2108 tsubst_constraint_variables (tree t
, tree args
, subst_info info
)
2110 /* Perform a trial substitution to check for type errors. */
2111 tree parms
= check_constaint_variables (t
, args
, info
);
2112 if (parms
== error_mark_node
)
2113 return error_mark_node
;
2115 /* Clear cp_unevaluated_operand across tsubst so that we get a proper chain
2117 int saved_unevaluated_operand
= cp_unevaluated_operand
;
2118 cp_unevaluated_operand
= 0;
2119 tree vars
= tsubst (t
, args
, info
.complain
, info
.in_decl
);
2120 cp_unevaluated_operand
= saved_unevaluated_operand
;
2121 if (vars
== error_mark_node
)
2122 return error_mark_node
;
2123 return declare_constraint_vars (t
, vars
);
2126 /* Substitute ARGS into the requires-expression T. [8.4.7]p6. The
2127 substitution of template arguments into a requires-expression
2128 may result in the formation of invalid types or expressions
2129 in its requirements ... In such cases, the expression evaluates
2130 to false; it does not cause the program to be ill-formed.
2132 However, there are cases where substitution must produce a
2133 new requires-expression, that is not a template constraint.
2136 template<typename T>
2138 template<typename U>
2139 static constexpr bool var = requires (U u) { T::fn(u); };
2142 In the instantiation of X<Y> (assuming Y defines fn), then the
2143 instantiated requires-expression would include Y::fn(u). If any
2144 substitution in the requires-expression fails, we can immediately
2145 fold the expression to false, as would be the case e.g., when
2146 instantiation X<int>. */
2149 tsubst_requires_expr (tree t
, tree args
,
2150 tsubst_flags_t complain
, tree in_decl
)
2152 local_specialization_stack
stack (lss_copy
);
2154 subst_info
info (complain
, in_decl
);
2156 /* A requires-expression is an unevaluated context. */
2159 args
= add_extra_args (REQUIRES_EXPR_EXTRA_ARGS (t
), args
);
2160 if (processing_template_decl
)
2162 /* We're partially instantiating a generic lambda. Substituting into
2163 this requires-expression now may cause its requirements to get
2164 checked out of order, so instead just remember the template
2165 arguments and wait until we can substitute them all at once. */
2167 REQUIRES_EXPR_EXTRA_ARGS (t
) = build_extra_args (t
, args
, complain
);
2171 tree parms
= REQUIRES_EXPR_PARMS (t
);
2174 parms
= tsubst_constraint_variables (parms
, args
, info
);
2175 if (parms
== error_mark_node
)
2176 return boolean_false_node
;
2179 tree reqs
= REQUIRES_EXPR_REQS (t
);
2180 reqs
= tsubst_requirement_body (reqs
, args
, info
);
2181 if (reqs
== error_mark_node
)
2182 return boolean_false_node
;
2184 return boolean_true_node
;
2187 /* Substitute ARGS into the constraint information CI, producing a new
2188 constraint record. */
2191 tsubst_constraint_info (tree t
, tree args
,
2192 tsubst_flags_t complain
, tree in_decl
)
2194 if (!t
|| t
== error_mark_node
|| !check_constraint_info (t
))
2197 tree tr
= tsubst_constraint (CI_TEMPLATE_REQS (t
), args
, complain
, in_decl
);
2198 tree dr
= tsubst_constraint (CI_DECLARATOR_REQS (t
), args
, complain
, in_decl
);
2199 return build_constraints (tr
, dr
);
2202 /* Substitute through a parameter mapping, in order to get the actual
2203 arguments used to instantiate an atomic constraint. This may fail
2204 if the substitution into arguments produces something ill-formed. */
2207 tsubst_parameter_mapping (tree map
, tree args
, subst_info info
)
2212 tsubst_flags_t complain
= info
.complain
;
2213 tree in_decl
= info
.in_decl
;
2215 tree result
= NULL_TREE
;
2216 for (tree p
= map
; p
; p
= TREE_CHAIN (p
))
2218 if (p
== error_mark_node
)
2219 return error_mark_node
;
2220 tree parm
= TREE_VALUE (p
);
2221 tree arg
= TREE_PURPOSE (p
);
2222 tree new_arg
= NULL_TREE
;
2225 /* If a template parameter is declared with a placeholder, we can
2226 get those in the argument list if decltype is applied to the
2227 placeholder. For example:
2230 requires C<decltype(T)>
2233 The normalized argument for C will be an auto type, so we'll
2234 need to deduce the actual argument from the corresponding
2235 initializer (whatever argument is provided for T), and use
2236 that result in the instantiated parameter mapping. */
2237 if (tree auto_node
= type_uses_auto (arg
))
2241 template_parm_level_and_index (parm
, &level
, &index
);
2242 tree init
= TMPL_ARG (args
, level
, index
);
2243 new_arg
= do_auto_deduction (arg
, init
, auto_node
,
2244 complain
, adc_variable_type
,
2248 else if (ARGUMENT_PACK_P (arg
))
2249 new_arg
= tsubst_argument_pack (arg
, args
, complain
, in_decl
);
2252 new_arg
= tsubst_template_arg (arg
, args
, complain
, in_decl
);
2253 if (TYPE_P (new_arg
))
2254 new_arg
= canonicalize_type_argument (new_arg
, complain
);
2256 if (new_arg
== error_mark_node
)
2257 return error_mark_node
;
2259 result
= tree_cons (new_arg
, parm
, result
);
2261 return nreverse (result
);
2265 tsubst_parameter_mapping (tree map
, tree args
, tsubst_flags_t complain
, tree in_decl
)
2267 return tsubst_parameter_mapping (map
, args
, subst_info (complain
, in_decl
));
2270 /*---------------------------------------------------------------------------
2271 Constraint satisfaction
2272 ---------------------------------------------------------------------------*/
2274 /* Hash functions for satisfaction entries. */
2276 struct GTY((for_user
)) sat_entry
2283 struct sat_hasher
: ggc_ptr_hash
<sat_entry
>
2285 static hashval_t
hash (sat_entry
*e
)
2287 hashval_t value
= hash_atomic_constraint (e
->constr
);
2288 return iterative_hash_template_arg (e
->args
, value
);
2291 static bool equal (sat_entry
*e1
, sat_entry
*e2
)
2293 if (!atomic_constraints_identical_p (e1
->constr
, e2
->constr
))
2295 return template_args_equal (e1
->args
, e2
->args
);
2299 /* Cache the result of satisfy_atom. */
2300 static GTY((deletable
)) hash_table
<sat_hasher
> *sat_cache
;
2302 /* Cache the result of constraint_satisfaction_value. */
2303 static GTY((deletable
)) hash_map
<tree
, tree
> *decl_satisfied_cache
;
2306 get_satisfaction (tree constr
, tree args
)
2310 sat_entry elt
= { constr
, args
, NULL_TREE
};
2311 sat_entry
* found
= sat_cache
->find (&elt
);
2313 return found
->result
;
2319 save_satisfaction (tree constr
, tree args
, tree result
)
2322 sat_cache
= hash_table
<sat_hasher
>::create_ggc (31);
2323 sat_entry elt
= {constr
, args
, result
};
2324 sat_entry
** slot
= sat_cache
->find_slot (&elt
, INSERT
);
2325 sat_entry
* entry
= ggc_alloc
<sat_entry
> ();
2331 clear_satisfaction_cache ()
2334 sat_cache
->empty ();
2335 if (decl_satisfied_cache
)
2336 decl_satisfied_cache
->empty ();
2339 /* A tool to help manage satisfaction caching in satisfy_constraint_r.
2340 Note the cache is only used when not diagnosing errors. */
2342 struct satisfaction_cache
2344 satisfaction_cache (tree constr
, tree args
, tsubst_flags_t complain
)
2345 : constr(constr
), args(args
), complain(complain
)
2350 if (complain
== tf_none
)
2351 return get_satisfaction (constr
, args
);
2355 tree
save (tree result
)
2357 if (complain
== tf_none
)
2358 save_satisfaction (constr
, args
, result
);
2364 tsubst_flags_t complain
;
2367 static int satisfying_constraint
= 0;
2369 /* Returns true if we are currently satisfying a constraint.
2371 This is used to guard against recursive calls to evaluate_concept_check
2372 during template argument substitution.
2374 TODO: Do we need this now that we fully normalize prior to evaluation?
2378 satisfying_constraint_p ()
2380 return satisfying_constraint
;
2383 /* Substitute ARGS into constraint-expression T during instantiation of
2384 a member of a class template. */
2387 tsubst_constraint (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
2389 /* We also don't want to evaluate concept-checks when substituting the
2390 constraint-expressions of a declaration. */
2391 processing_constraint_expression_sentinel s
;
2392 tree expr
= tsubst_expr (t
, args
, complain
, in_decl
, false);
2396 static tree
satisfy_constraint_r (tree
, tree
, subst_info info
);
2398 /* Compute the satisfaction of a conjunction. */
2401 satisfy_conjunction (tree t
, tree args
, subst_info info
)
2403 tree lhs
= satisfy_constraint_r (TREE_OPERAND (t
, 0), args
, info
);
2404 if (lhs
== error_mark_node
|| lhs
== boolean_false_node
)
2406 return satisfy_constraint_r (TREE_OPERAND (t
, 1), args
, info
);
2409 /* The current depth at which we're replaying an error during recursive
2410 diagnosis of a constraint satisfaction failure. */
2412 static int current_constraint_diagnosis_depth
;
2414 /* Whether CURRENT_CONSTRAINT_DIAGNOSIS_DEPTH has ever exceeded
2415 CONCEPTS_DIAGNOSTICS_MAX_DEPTH during recursive diagnosis of a constraint
2416 satisfaction error. */
2418 static bool concepts_diagnostics_max_depth_exceeded_p
;
2420 /* Recursive subroutine of collect_operands_of_disjunction. T is a normalized
2421 subexpression of a constraint (composed of CONJ_CONSTRs and DISJ_CONSTRs)
2422 and E is the corresponding unnormalized subexpression (composed of
2423 TRUTH_ANDIF_EXPRs and TRUTH_ORIF_EXPRs). */
2426 collect_operands_of_disjunction_r (tree t
, tree e
,
2427 auto_vec
<tree_pair
> *operands
)
2429 if (TREE_CODE (e
) == TRUTH_ORIF_EXPR
)
2431 collect_operands_of_disjunction_r (TREE_OPERAND (t
, 0),
2432 TREE_OPERAND (e
, 0), operands
);
2433 collect_operands_of_disjunction_r (TREE_OPERAND (t
, 1),
2434 TREE_OPERAND (e
, 1), operands
);
2438 tree_pair p
= std::make_pair (t
, e
);
2439 operands
->safe_push (p
);
2443 /* Recursively collect the normalized and unnormalized operands of the
2444 disjunction T and append them to OPERANDS in order. */
2447 collect_operands_of_disjunction (tree t
, auto_vec
<tree_pair
> *operands
)
2449 collect_operands_of_disjunction_r (t
, CONSTR_EXPR (t
), operands
);
2452 /* Compute the satisfaction of a disjunction. */
2455 satisfy_disjunction (tree t
, tree args
, subst_info info
)
2457 /* Evaluate the operands quietly. */
2458 subst_info
quiet (tf_none
, NULL_TREE
);
2460 /* Register the constraint for diagnostics, if needed. */
2461 diagnosing_failed_constraint
failure (t
, args
, info
.noisy ());
2463 tree lhs
= satisfy_constraint_r (TREE_OPERAND (t
, 0), args
, quiet
);
2464 if (lhs
== boolean_true_node
)
2465 return boolean_true_node
;
2466 tree rhs
= satisfy_constraint_r (TREE_OPERAND (t
, 1), args
, quiet
);
2467 if (rhs
!= boolean_true_node
&& info
.noisy ())
2469 cp_expr disj_expr
= CONSTR_EXPR (t
);
2470 inform (disj_expr
.get_location (),
2471 "no operand of the disjunction is satisfied");
2472 if (diagnosing_failed_constraint::replay_errors_p ())
2474 /* Replay the error in each branch of the disjunction. */
2475 auto_vec
<tree_pair
> operands
;
2476 collect_operands_of_disjunction (t
, &operands
);
2477 for (unsigned i
= 0; i
< operands
.length (); i
++)
2479 tree norm_op
= operands
[i
].first
;
2480 tree op
= operands
[i
].second
;
2481 location_t loc
= make_location (cp_expr_location (op
),
2482 disj_expr
.get_start (),
2483 disj_expr
.get_finish ());
2484 inform (loc
, "the operand %qE is unsatisfied because", op
);
2485 satisfy_constraint_r (norm_op
, args
, info
);
2492 /* Ensures that T is a truth value and not (accidentally, as sometimes
2493 happens) an integer value. */
2496 satisfaction_value (tree t
)
2498 if (t
== error_mark_node
|| t
== boolean_true_node
|| t
== boolean_false_node
)
2501 gcc_assert (TREE_CODE (t
) == INTEGER_CST
2502 && same_type_p (TREE_TYPE (t
), boolean_type_node
));
2503 if (integer_zerop (t
))
2504 return boolean_false_node
;
2506 return boolean_true_node
;
2509 /* Build a new template argument list with template arguments corresponding
2510 to the parameters used in an atomic constraint. */
2513 get_mapped_args (tree map
)
2515 /* No map, no arguments. */
2519 /* Find the mapped parameter with the highest level. */
2521 for (tree p
= map
; p
; p
= TREE_CHAIN (p
))
2525 template_parm_level_and_index (TREE_VALUE (p
), &level
, &index
);
2530 /* Place each argument at its corresponding position in the argument
2531 list. Note that the list will be sparse (not all arguments supplied),
2532 but instantiation is guaranteed to only use the parameters in the
2533 mapping, so null arguments would never be used. */
2534 auto_vec
< vec
<tree
> > lists (count
);
2535 lists
.quick_grow_cleared (count
);
2536 for (tree p
= map
; p
; p
= TREE_CHAIN (p
))
2540 template_parm_level_and_index (TREE_VALUE (p
), &level
, &index
);
2542 /* Insert the argument into its corresponding position. */
2543 vec
<tree
> &list
= lists
[level
- 1];
2544 if (index
>= (int)list
.length ())
2545 list
.safe_grow_cleared (index
+ 1, true);
2546 list
[index
] = TREE_PURPOSE (p
);
2549 /* Build the new argument list. */
2550 tree args
= make_tree_vec (lists
.length ());
2551 for (unsigned i
= 0; i
!= lists
.length (); ++i
)
2553 vec
<tree
> &list
= lists
[i
];
2554 tree level
= make_tree_vec (list
.length ());
2555 for (unsigned j
= 0; j
< list
.length(); ++j
)
2556 TREE_VEC_ELT (level
, j
) = list
[j
];
2557 SET_TMPL_ARGS_LEVEL (args
, i
+ 1, level
);
2560 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
, 0);
2565 static void diagnose_atomic_constraint (tree
, tree
, tree
, subst_info
);
2567 /* Compute the satisfaction of an atomic constraint. */
2570 satisfy_atom (tree t
, tree args
, subst_info info
)
2572 satisfaction_cache
cache (t
, args
, info
.complain
);
2573 if (tree r
= cache
.get ())
2576 /* Perform substitution quietly. */
2577 subst_info
quiet (tf_none
, NULL_TREE
);
2579 /* In case there is a diagnostic, we want to establish the context
2580 prior to printing errors. If no errors occur, this context is
2581 removed before returning. */
2582 diagnosing_failed_constraint
failure (t
, args
, info
.noisy ());
2584 /* Instantiate the parameter mapping. */
2585 tree map
= tsubst_parameter_mapping (ATOMIC_CONSTR_MAP (t
), args
, quiet
);
2586 if (map
== error_mark_node
)
2588 /* If instantiation of the parameter mapping fails, the program
2591 tsubst_parameter_mapping (ATOMIC_CONSTR_MAP (t
), args
, info
);
2592 return cache
.save (boolean_false_node
);
2595 /* Rebuild the argument vector from the parameter mapping. */
2596 args
= get_mapped_args (map
);
2598 /* Apply the parameter mapping (i.e., just substitute). */
2599 tree expr
= ATOMIC_CONSTR_EXPR (t
);
2600 tree result
= tsubst_expr (expr
, args
, quiet
.complain
, quiet
.in_decl
, false);
2601 if (result
== error_mark_node
)
2603 /* If substitution results in an invalid type or expression, the constraint
2604 is not satisfied. Replay the substitution. */
2606 tsubst_expr (expr
, args
, info
.complain
, info
.in_decl
, false);
2607 return cache
.save (boolean_false_node
);
2610 /* [17.4.1.2] ... lvalue-to-rvalue conversion is performed as necessary,
2611 and EXPR shall be a constant expression of type bool. */
2612 result
= force_rvalue (result
, info
.complain
);
2613 if (result
== error_mark_node
)
2614 return cache
.save (error_mark_node
);
2615 if (!same_type_p (TREE_TYPE (result
), boolean_type_node
))
2618 diagnose_atomic_constraint (t
, map
, result
, info
);
2619 return cache
.save (error_mark_node
);
2622 /* Compute the value of the constraint. */
2624 result
= cxx_constant_value (result
);
2627 result
= maybe_constant_value (result
, NULL_TREE
,
2628 /*manifestly_const_eval=*/true);
2629 if (!TREE_CONSTANT (result
))
2630 result
= error_mark_node
;
2632 result
= satisfaction_value (result
);
2633 if (result
== boolean_false_node
&& info
.noisy ())
2634 diagnose_atomic_constraint (t
, map
, result
, info
);
2636 return cache
.save (result
);
2639 /* Determine if the normalized constraint T is satisfied.
2640 Returns boolean_true_node if the expression/constraint is
2641 satisfied, boolean_false_node if not, and error_mark_node
2642 if the there was an error evaluating the constraint.
2644 The parameter mapping of atomic constraints is simply the
2645 set of template arguments that will be substituted into
2646 the expression, regardless of template parameters appearing
2647 withing. Whether a template argument is used in the atomic
2648 constraint only matters for subsumption. */
2651 satisfy_constraint_r (tree t
, tree args
, subst_info info
)
2653 if (t
== error_mark_node
)
2654 return error_mark_node
;
2656 switch (TREE_CODE (t
))
2659 return satisfy_conjunction (t
, args
, info
);
2661 return satisfy_disjunction (t
, args
, info
);
2663 return satisfy_atom (t
, args
, info
);
2669 /* Check that the normalized constraint T is satisfied for ARGS. */
2672 satisfy_constraint (tree t
, tree args
, subst_info info
)
2674 auto_timevar
time (TV_CONSTRAINT_SAT
);
2676 /* Turn off template processing. Constraint satisfaction only applies
2677 to non-dependent terms, so we want to ensure full checking here. */
2678 processing_template_decl_sentinel
proc (true);
2680 /* We need to check access during satisfaction. */
2681 deferring_access_check_sentinel
acs (dk_no_deferred
);
2683 return satisfy_constraint_r (t
, args
, info
);
2686 /* Check the normalized constraints T against ARGS, returning a satisfaction
2687 value (either true, false, or error). */
2690 satisfy_associated_constraints (tree t
, tree args
, subst_info info
)
2692 /* If there are no constraints then this is trivially satisfied. */
2694 return boolean_true_node
;
2696 /* If any arguments depend on template parameters, we can't
2697 check constraints. Pretend they're satisfied for now. */
2698 if (args
&& uses_template_parms (args
))
2699 return boolean_true_node
;
2701 return satisfy_constraint (t
, args
, info
);
2704 /* Evaluate EXPR as a constraint expression using ARGS, returning a
2705 satisfaction value. */
2708 satisfy_constraint_expression (tree t
, tree args
, subst_info info
)
2710 if (t
== error_mark_node
)
2711 return error_mark_node
;
2713 gcc_assert (EXPR_P (t
));
2715 /* Get the normalized constraints. */
2717 if (args
== NULL_TREE
&& concept_check_p (t
))
2719 tree id
= unpack_concept_check (t
);
2720 args
= TREE_OPERAND (id
, 1);
2721 tree tmpl
= get_concept_check_template (id
);
2722 norm
= normalize_concept_definition (tmpl
, info
.noisy ());
2725 norm
= normalize_constraint_expression (t
, info
.noisy ());
2727 /* Perform satisfaction. */
2728 return satisfy_constraint (norm
, args
, info
);
2731 /* Used only to evaluate requires-expressions during constant expression
2735 satisfy_constraint_expression (tree expr
)
2737 subst_info
info (tf_none
, NULL_TREE
);
2738 return satisfy_constraint_expression (expr
, NULL_TREE
, info
);
2742 satisfy_declaration_constraints (tree t
, subst_info info
)
2744 gcc_assert (DECL_P (t
));
2745 const tree saved_t
= t
;
2747 /* For inherited constructors, consider the original declaration;
2748 it has the correct template information attached. */
2749 t
= strip_inheriting_ctors (t
);
2750 tree inh_ctor_targs
= NULL_TREE
;
2752 if (tree ti
= DECL_TEMPLATE_INFO (saved_t
))
2753 /* The inherited constructor points to an instantiation of a constructor
2754 template; remember its template arguments. */
2755 inh_ctor_targs
= TI_ARGS (ti
);
2757 /* Update the declaration for diagnostics. */
2761 if (tree
*result
= hash_map_safe_get (decl_satisfied_cache
, saved_t
))
2764 /* Get the normalized constraints. */
2765 tree norm
= NULL_TREE
;
2766 tree args
= NULL_TREE
;
2767 if (tree ti
= DECL_TEMPLATE_INFO (t
))
2769 tree tmpl
= TI_TEMPLATE (ti
);
2770 norm
= normalize_template_requirements (tmpl
, info
.noisy ());
2772 /* The initial parameter mapping is the complete set of
2773 template arguments substituted into the declaration. */
2774 args
= TI_ARGS (ti
);
2776 args
= add_outermost_template_args (args
, inh_ctor_targs
);
2780 /* These should be empty until we allow constraints on non-templates. */
2781 norm
= normalize_nontemplate_requirements (t
, info
.noisy ());
2784 tree result
= boolean_true_node
;
2787 if (!push_tinst_level (t
))
2789 push_access_scope (t
);
2790 result
= satisfy_associated_constraints (norm
, args
, info
);
2791 pop_access_scope (t
);
2796 hash_map_safe_put
<hm_ggc
> (decl_satisfied_cache
, saved_t
, result
);
2802 satisfy_declaration_constraints (tree t
, tree args
, subst_info info
)
2804 /* Update the declaration for diagnostics. */
2807 gcc_assert (TREE_CODE (t
) == TEMPLATE_DECL
);
2809 args
= add_outermost_template_args (t
, args
);
2811 tree result
= boolean_true_node
;
2812 if (tree norm
= normalize_template_requirements (t
, info
.noisy ()))
2814 if (!push_tinst_level (t
, args
))
2816 tree pattern
= DECL_TEMPLATE_RESULT (t
);
2817 push_access_scope (pattern
);
2818 result
= satisfy_associated_constraints (norm
, args
, info
);
2819 pop_access_scope (pattern
);
2827 constraint_satisfaction_value (tree t
, tsubst_flags_t complain
)
2829 subst_info
info (complain
, NULL_TREE
);
2832 r
= satisfy_declaration_constraints (t
, info
);
2834 r
= satisfy_constraint_expression (t
, NULL_TREE
, info
);
2835 if (r
== error_mark_node
&& info
.quiet ()
2836 && !(DECL_P (t
) && TREE_NO_WARNING (t
)))
2838 constraint_satisfaction_value (t
, tf_warning_or_error
);
2840 /* Avoid giving these errors again. */
2841 TREE_NO_WARNING (t
) = true;
2847 constraint_satisfaction_value (tree t
, tree args
, tsubst_flags_t complain
)
2849 subst_info
info (complain
, NULL_TREE
);
2852 r
= satisfy_declaration_constraints (t
, args
, info
);
2854 r
= satisfy_constraint_expression (t
, args
, info
);
2855 if (r
== error_mark_node
&& info
.quiet ())
2856 constraint_satisfaction_value (t
, args
, tf_warning_or_error
);
2860 /* True iff the result of satisfying T is BOOLEAN_TRUE_NODE and false
2861 otherwise, even in the case of errors. */
2864 constraints_satisfied_p (tree t
)
2869 return constraint_satisfaction_value (t
, tf_none
) == boolean_true_node
;
2872 /* True iff the result of satisfying T with ARGS is BOOLEAN_TRUE_NODE
2873 and false otherwise, even in the case of errors. */
2876 constraints_satisfied_p (tree t
, tree args
)
2881 return constraint_satisfaction_value (t
, args
, tf_none
) == boolean_true_node
;
2884 /* Evaluate a concept check of the form C<ARGS>. This is only used for the
2885 evaluation of template-ids as id-expressions. */
2888 evaluate_concept_check (tree check
, tsubst_flags_t complain
)
2890 if (check
== error_mark_node
)
2891 return error_mark_node
;
2893 gcc_assert (concept_check_p (check
));
2895 /* Check for satisfaction without diagnostics. */
2896 subst_info
quiet (tf_none
, NULL_TREE
);
2897 tree result
= satisfy_constraint_expression (check
, NULL_TREE
, quiet
);
2898 if (result
== error_mark_node
&& (complain
& tf_error
))
2900 /* Replay the error with re-normalized requirements. */
2901 subst_info
noisy (tf_warning_or_error
, NULL_TREE
);
2902 satisfy_constraint_expression (check
, NULL_TREE
, noisy
);
2907 /*---------------------------------------------------------------------------
2908 Semantic analysis of requires-expressions
2909 ---------------------------------------------------------------------------*/
2911 /* Finish a requires expression for the given PARMS (possibly
2912 null) and the non-empty sequence of requirements. */
2915 finish_requires_expr (location_t loc
, tree parms
, tree reqs
)
2917 /* Modify the declared parameters by removing their context
2918 so they don't refer to the enclosing scope and explicitly
2919 indicating that they are constraint variables. */
2920 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
2922 DECL_CONTEXT (parm
) = NULL_TREE
;
2923 CONSTRAINT_VAR_P (parm
) = true;
2926 /* Build the node. */
2927 tree r
= build_min (REQUIRES_EXPR
, boolean_type_node
, parms
, reqs
, NULL_TREE
);
2928 TREE_SIDE_EFFECTS (r
) = false;
2929 TREE_CONSTANT (r
) = true;
2930 SET_EXPR_LOCATION (r
, loc
);
2934 /* Construct a requirement for the validity of EXPR. */
2937 finish_simple_requirement (location_t loc
, tree expr
)
2939 tree r
= build_nt (SIMPLE_REQ
, expr
);
2940 SET_EXPR_LOCATION (r
, loc
);
2944 /* Construct a requirement for the validity of TYPE. */
2947 finish_type_requirement (location_t loc
, tree type
)
2949 tree r
= build_nt (TYPE_REQ
, type
);
2950 SET_EXPR_LOCATION (r
, loc
);
2954 /* Construct a requirement for the validity of EXPR, along with
2955 its properties. if TYPE is non-null, then it specifies either
2956 an implicit conversion or argument deduction constraint,
2957 depending on whether any placeholders occur in the type name.
2958 NOEXCEPT_P is true iff the noexcept keyword was specified. */
2961 finish_compound_requirement (location_t loc
, tree expr
, tree type
, bool noexcept_p
)
2963 tree req
= build_nt (COMPOUND_REQ
, expr
, type
);
2964 SET_EXPR_LOCATION (req
, loc
);
2965 COMPOUND_REQ_NOEXCEPT_P (req
) = noexcept_p
;
2969 /* Finish a nested requirement. */
2972 finish_nested_requirement (location_t loc
, tree expr
)
2974 tree norm
= normalize_constraint_expression (expr
, false);
2976 /* Build the constraint, saving its normalization as its type. */
2977 tree r
= build1 (NESTED_REQ
, norm
, expr
);
2978 SET_EXPR_LOCATION (r
, loc
);
2982 /* Check that FN satisfies the structural requirements of a
2983 function concept definition. */
2985 check_function_concept (tree fn
)
2987 /* Check that the function is comprised of only a return statement. */
2988 tree body
= DECL_SAVED_TREE (fn
);
2989 if (TREE_CODE (body
) == BIND_EXPR
)
2990 body
= BIND_EXPR_BODY (body
);
2992 /* Sometimes a function call results in the creation of clean up
2993 points. Allow these to be preserved in the body of the
2994 constraint, as we might actually need them for some constexpr
2996 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
2997 body
= TREE_OPERAND (body
, 0);
2999 /* Check that the definition is written correctly. */
3000 if (TREE_CODE (body
) != RETURN_EXPR
)
3002 location_t loc
= DECL_SOURCE_LOCATION (fn
);
3003 if (TREE_CODE (body
) == STATEMENT_LIST
&& !STATEMENT_LIST_HEAD (body
))
3006 /* The definition was probably erroneous, not empty. */;
3008 error_at (loc
, "definition of concept %qD is empty", fn
);
3011 error_at (loc
, "definition of concept %qD has multiple statements", fn
);
3018 // Check that a constrained friend declaration function declaration,
3019 // FN, is admissible. This is the case only when the declaration depends
3020 // on template parameters and does not declare a specialization.
3022 check_constrained_friend (tree fn
, tree reqs
)
3024 if (fn
== error_mark_node
)
3026 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
3028 // If there are not constraints, this cannot be an error.
3032 // Constrained friend functions that don't depend on template
3033 // arguments are effectively meaningless.
3034 if (!uses_template_parms (TREE_TYPE (fn
)))
3036 error_at (location_of (fn
),
3037 "constrained friend does not depend on template parameters");
3042 /*---------------------------------------------------------------------------
3043 Equivalence of constraints
3044 ---------------------------------------------------------------------------*/
3046 /* Returns true when A and B are equivalent constraints. */
3048 equivalent_constraints (tree a
, tree b
)
3050 gcc_assert (!a
|| TREE_CODE (a
) == CONSTRAINT_INFO
);
3051 gcc_assert (!b
|| TREE_CODE (b
) == CONSTRAINT_INFO
);
3052 return cp_tree_equal (a
, b
);
3055 /* Returns true if the template declarations A and B have equivalent
3056 constraints. This is the case when A's constraints subsume B's and
3057 when B's also constrain A's. */
3059 equivalently_constrained (tree d1
, tree d2
)
3061 gcc_assert (TREE_CODE (d1
) == TREE_CODE (d2
));
3062 return equivalent_constraints (get_constraints (d1
), get_constraints (d2
));
3065 /*---------------------------------------------------------------------------
3066 Partial ordering of constraints
3067 ---------------------------------------------------------------------------*/
3069 /* Returns true when the constraints in A subsume those in B. */
3072 subsumes_constraints (tree a
, tree b
)
3074 gcc_assert (!a
|| TREE_CODE (a
) == CONSTRAINT_INFO
);
3075 gcc_assert (!b
|| TREE_CODE (b
) == CONSTRAINT_INFO
);
3076 return subsumes (a
, b
);
3079 /* Returns true when the constraints in CI strictly subsume
3080 the associated constraints of TMPL. */
3083 strictly_subsumes (tree ci
, tree tmpl
)
3085 tree n1
= get_normalized_constraints_from_info (ci
, NULL_TREE
);
3086 tree n2
= get_normalized_constraints_from_decl (tmpl
);
3088 return subsumes (n1
, n2
) && !subsumes (n2
, n1
);
3091 /* Returns true when the constraints in CI subsume the
3092 associated constraints of TMPL. */
3095 weakly_subsumes (tree ci
, tree tmpl
)
3097 tree n1
= get_normalized_constraints_from_info (ci
, NULL_TREE
);
3098 tree n2
= get_normalized_constraints_from_decl (tmpl
);
3100 return subsumes (n1
, n2
);
3103 /* Determines which of the declarations, A or B, is more constrained.
3104 That is, which declaration's constraints subsume but are not subsumed
3107 Returns 1 if D1 is more constrained than D2, -1 if D2 is more constrained
3108 than D1, and 0 otherwise. */
3111 more_constrained (tree d1
, tree d2
)
3113 tree n1
= get_normalized_constraints_from_decl (d1
);
3114 tree n2
= get_normalized_constraints_from_decl (d2
);
3117 if (subsumes (n1
, n2
))
3119 if (subsumes (n2
, n1
))
3124 /* Return whether D1 is at least as constrained as D2. */
3127 at_least_as_constrained (tree d1
, tree d2
)
3129 tree n1
= get_normalized_constraints_from_decl (d1
);
3130 tree n2
= get_normalized_constraints_from_decl (d2
);
3132 return subsumes (n1
, n2
);
3135 /*---------------------------------------------------------------------------
3136 Constraint diagnostics
3137 ---------------------------------------------------------------------------*/
3139 /* Returns the best location to diagnose a constraint error. */
3142 get_constraint_error_location (tree t
)
3144 if (location_t loc
= cp_expr_location (t
))
3147 /* If we have a specific location give it. */
3148 tree expr
= CONSTR_EXPR (t
);
3149 if (location_t loc
= cp_expr_location (expr
))
3152 /* If the constraint is normalized from a requires-clause, give
3153 the location as that of the constrained declaration. */
3154 tree cxt
= CONSTR_CONTEXT (t
);
3155 tree src
= cxt
? TREE_VALUE (cxt
) : NULL_TREE
;
3157 /* TODO: This only happens for constrained non-template declarations. */
3159 else if (DECL_P (src
))
3160 return DECL_SOURCE_LOCATION (src
);
3161 /* Otherwise, give the location as the defining concept. */
3162 else if (concept_check_p (src
))
3164 tree id
= unpack_concept_check (src
);
3165 tree tmpl
= TREE_OPERAND (id
, 0);
3167 tmpl
= OVL_FIRST (tmpl
);
3168 return DECL_SOURCE_LOCATION (tmpl
);
3171 return input_location
;
3174 /* Emit a diagnostic for a failed trait. */
3177 diagnose_trait_expr (tree expr
, tree map
)
3179 location_t loc
= cp_expr_location (expr
);
3180 tree args
= get_mapped_args (map
);
3182 /* Build a "fake" version of the instantiated trait, so we can
3183 get the instantiated types from result. */
3184 ++processing_template_decl
;
3185 expr
= tsubst_expr (expr
, args
, tf_none
, NULL_TREE
, false);
3186 --processing_template_decl
;
3188 tree t1
= TRAIT_EXPR_TYPE1 (expr
);
3189 tree t2
= TRAIT_EXPR_TYPE2 (expr
);
3190 switch (TRAIT_EXPR_KIND (expr
))
3192 case CPTK_HAS_NOTHROW_ASSIGN
:
3193 inform (loc
, " %qT is not %<nothrow%> copy assignable", t1
);
3195 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
3196 inform (loc
, " %qT is not %<nothrow%> default constructible", t1
);
3198 case CPTK_HAS_NOTHROW_COPY
:
3199 inform (loc
, " %qT is not %<nothrow%> copy constructible", t1
);
3201 case CPTK_HAS_TRIVIAL_ASSIGN
:
3202 inform (loc
, " %qT is not trivially copy assignable", t1
);
3204 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
3205 inform (loc
, " %qT is not trivially default constructible", t1
);
3207 case CPTK_HAS_TRIVIAL_COPY
:
3208 inform (loc
, " %qT is not trivially copy constructible", t1
);
3210 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
3211 inform (loc
, " %qT is not trivially destructible", t1
);
3213 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
3214 inform (loc
, " %qT does not have a virtual destructor", t1
);
3216 case CPTK_IS_ABSTRACT
:
3217 inform (loc
, " %qT is not an abstract class", t1
);
3219 case CPTK_IS_BASE_OF
:
3220 inform (loc
, " %qT is not a base of %qT", t1
, t2
);
3223 inform (loc
, " %qT is not a class", t1
);
3226 inform (loc
, " %qT is not an empty class", t1
);
3229 inform (loc
, " %qT is not an enum", t1
);
3232 inform (loc
, " %qT is not a final class", t1
);
3234 case CPTK_IS_LITERAL_TYPE
:
3235 inform (loc
, " %qT is not a literal type", t1
);
3238 inform (loc
, " %qT is not a POD type", t1
);
3240 case CPTK_IS_POLYMORPHIC
:
3241 inform (loc
, " %qT is not a polymorphic type", t1
);
3243 case CPTK_IS_SAME_AS
:
3244 inform (loc
, " %qT is not the same as %qT", t1
, t2
);
3246 case CPTK_IS_STD_LAYOUT
:
3247 inform (loc
, " %qT is not an standard layout type", t1
);
3249 case CPTK_IS_TRIVIAL
:
3250 inform (loc
, " %qT is not a trivial type", t1
);
3253 inform (loc
, " %qT is not a union", t1
);
3261 diagnose_valid_expression (tree expr
, tree args
, tree in_decl
)
3263 tree result
= tsubst_expr (expr
, args
, tf_none
, in_decl
, false);
3264 if (result
!= error_mark_node
3265 && convert_to_void (result
, ICV_STATEMENT
, tf_none
) != error_mark_node
)
3268 location_t loc
= cp_expr_loc_or_input_loc (expr
);
3269 if (diagnosing_failed_constraint::replay_errors_p ())
3271 /* Replay the substitution error. */
3272 inform (loc
, "the required expression %qE is invalid, because", expr
);
3273 if (result
== error_mark_node
)
3274 tsubst_expr (expr
, args
, tf_error
, in_decl
, false);
3276 convert_to_void (result
, ICV_STATEMENT
, tf_error
);
3279 inform (loc
, "the required expression %qE is invalid", expr
);
3281 return error_mark_node
;
3285 diagnose_valid_type (tree type
, tree args
, tree in_decl
)
3287 tree result
= tsubst (type
, args
, tf_none
, in_decl
);
3288 if (result
!= error_mark_node
)
3291 location_t loc
= cp_expr_loc_or_input_loc (type
);
3292 if (diagnosing_failed_constraint::replay_errors_p ())
3294 /* Replay the substitution error. */
3295 inform (loc
, "the required type %qT is invalid, because", type
);
3296 tsubst (type
, args
, tf_error
, in_decl
);
3299 inform (loc
, "the required type %qT is invalid", type
);
3301 return error_mark_node
;
3305 diagnose_simple_requirement (tree req
, tree args
, tree in_decl
)
3307 diagnose_valid_expression (TREE_OPERAND (req
, 0), args
, in_decl
);
3311 diagnose_compound_requirement (tree req
, tree args
, tree in_decl
)
3313 tree expr
= TREE_OPERAND (req
, 0);
3314 expr
= diagnose_valid_expression (expr
, args
, in_decl
);
3315 if (expr
== error_mark_node
)
3318 location_t loc
= cp_expr_loc_or_input_loc (expr
);
3320 /* Check the noexcept condition. */
3321 if (COMPOUND_REQ_NOEXCEPT_P (req
) && !expr_noexcept_p (expr
, tf_none
))
3322 inform (loc
, "%qE is not %<noexcept%>", expr
);
3324 tree type
= TREE_OPERAND (req
, 1);
3325 type
= diagnose_valid_type (type
, args
, in_decl
);
3326 if (type
== error_mark_node
)
3331 subst_info
quiet (tf_none
, in_decl
);
3332 subst_info
noisy (tf_error
, in_decl
);
3334 /* Check the expression against the result type. */
3335 if (tree placeholder
= type_uses_auto (type
))
3337 if (!type_deducible_p (expr
, type
, placeholder
, args
, quiet
))
3339 tree orig_expr
= TREE_OPERAND (req
, 0);
3340 if (diagnosing_failed_constraint::replay_errors_p ())
3343 "%qE does not satisfy return-type-requirement, "
3344 "because", orig_expr
);
3345 /* Further explain the reason for the error. */
3346 type_deducible_p (expr
, type
, placeholder
, args
, noisy
);
3349 inform (loc
, "%qE does not satisfy return-type-requirement",
3353 else if (!expression_convertible_p (expr
, type
, quiet
))
3355 tree orig_expr
= TREE_OPERAND (req
, 0);
3356 if (diagnosing_failed_constraint::replay_errors_p ())
3358 inform (loc
, "cannot convert %qE to %qT because", orig_expr
, type
);
3359 /* Further explain the reason for the error. */
3360 expression_convertible_p (expr
, type
, noisy
);
3363 inform (loc
, "cannot convert %qE to %qT", orig_expr
, type
);
3369 diagnose_type_requirement (tree req
, tree args
, tree in_decl
)
3371 tree type
= TREE_OPERAND (req
, 0);
3372 diagnose_valid_type (type
, args
, in_decl
);
3376 diagnose_nested_requirement (tree req
, tree args
)
3378 /* Quietly check for satisfaction first. We can elaborate details
3380 tree norm
= TREE_TYPE (req
);
3381 subst_info
info (tf_none
, NULL_TREE
);
3382 tree result
= satisfy_constraint (norm
, args
, info
);
3383 if (result
== boolean_true_node
)
3386 tree expr
= TREE_OPERAND (req
, 0);
3387 location_t loc
= cp_expr_location (expr
);
3388 if (diagnosing_failed_constraint::replay_errors_p ())
3390 /* Replay the substitution error. */
3391 inform (loc
, "nested requirement %qE is not satisfied, because", expr
);
3392 subst_info
noisy (tf_warning_or_error
, NULL_TREE
);
3393 satisfy_constraint_expression (expr
, args
, noisy
);
3396 inform (loc
, "nested requirement %qE is not satisfied", expr
);
3401 diagnose_requirement (tree req
, tree args
, tree in_decl
)
3403 iloc_sentinel
loc_s (cp_expr_location (req
));
3404 switch (TREE_CODE (req
))
3407 return diagnose_simple_requirement (req
, args
, in_decl
);
3409 return diagnose_compound_requirement (req
, args
, in_decl
);
3411 return diagnose_type_requirement (req
, args
, in_decl
);
3413 return diagnose_nested_requirement (req
, args
);
3420 diagnose_requires_expr (tree expr
, tree map
, tree in_decl
)
3422 local_specialization_stack
stack (lss_copy
);
3423 tree parms
= TREE_OPERAND (expr
, 0);
3424 tree body
= TREE_OPERAND (expr
, 1);
3425 tree args
= get_mapped_args (map
);
3428 subst_info
info (tf_warning_or_error
, NULL_TREE
);
3429 tree vars
= tsubst_constraint_variables (parms
, args
, info
);
3430 if (vars
== error_mark_node
)
3436 tree req
= TREE_VALUE (p
);
3437 diagnose_requirement (req
, args
, in_decl
);
3442 /* Diagnose a substitution failure in the atomic constraint T when applied
3443 with the instantiated parameter mapping MAP. */
3446 diagnose_atomic_constraint (tree t
, tree map
, tree result
, subst_info info
)
3448 /* If the constraint is already ill-formed, we've previously diagnosed
3449 the reason. We should still say why the constraints aren't satisfied. */
3450 if (t
== error_mark_node
)
3454 loc
= DECL_SOURCE_LOCATION (info
.in_decl
);
3456 loc
= input_location
;
3457 inform (loc
, "invalid constraints");
3461 location_t loc
= get_constraint_error_location (t
);
3462 iloc_sentinel
loc_s (loc
);
3464 /* Generate better diagnostics for certain kinds of expressions. */
3465 tree expr
= ATOMIC_CONSTR_EXPR (t
);
3466 STRIP_ANY_LOCATION_WRAPPER (expr
);
3467 switch (TREE_CODE (expr
))
3470 diagnose_trait_expr (expr
, map
);
3473 diagnose_requires_expr (expr
, map
, info
.in_decl
);
3476 tree a
= copy_node (t
);
3477 ATOMIC_CONSTR_MAP (a
) = map
;
3478 if (!same_type_p (TREE_TYPE (result
), boolean_type_node
))
3479 error_at (loc
, "constraint %qE has type %qT, not %<bool%>",
3480 a
, TREE_TYPE (result
));
3482 inform (loc
, "the expression %qE evaluated to %<false%>", a
);
3487 GTY(()) tree current_failed_constraint
;
3489 diagnosing_failed_constraint::
3490 diagnosing_failed_constraint (tree t
, tree args
, bool diag
)
3491 : diagnosing_error (diag
)
3493 if (diagnosing_error
)
3495 current_failed_constraint
3496 = tree_cons (args
, t
, current_failed_constraint
);
3497 ++current_constraint_diagnosis_depth
;
3501 diagnosing_failed_constraint::
3502 ~diagnosing_failed_constraint ()
3504 if (diagnosing_error
)
3506 --current_constraint_diagnosis_depth
;
3507 if (current_failed_constraint
)
3508 current_failed_constraint
= TREE_CHAIN (current_failed_constraint
);
3513 /* Whether we are allowed to replay an error that underlies a constraint failure
3514 at the current diagnosis depth. */
3517 diagnosing_failed_constraint::replay_errors_p ()
3519 if (current_constraint_diagnosis_depth
>= concepts_diagnostics_max_depth
)
3521 concepts_diagnostics_max_depth_exceeded_p
= true;
3528 /* Emit diagnostics detailing the failure ARGS to satisfy the constraints
3529 of T. Here, T can be either a constraint or a declaration. */
3532 diagnose_constraints (location_t loc
, tree t
, tree args
)
3534 inform (loc
, "constraints not satisfied");
3536 if (concepts_diagnostics_max_depth
== 0)
3539 /* Replay satisfaction, but diagnose errors. */
3541 constraint_satisfaction_value (t
, tf_warning_or_error
);
3543 constraint_satisfaction_value (t
, args
, tf_warning_or_error
);
3545 static bool suggested_p
;
3546 if (concepts_diagnostics_max_depth_exceeded_p
3547 && current_constraint_diagnosis_depth
== 0
3550 inform (UNKNOWN_LOCATION
,
3551 "set %qs to at least %d for more detail",
3552 "-fconcepts-diagnostics-depth=",
3553 concepts_diagnostics_max_depth
+ 1);
3558 #include "gt-cp-constraint.h"