1 /* Processing rules for constraints.
2 Copyright (C) 2013 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"
26 #include "print-tree.h"
28 #include "c-family/c-common.h"
29 #include "c-family/c-objc.h"
30 #include "tree-inline.h"
35 #include "diagnostic.h"
37 #include "tree-iterator.h"
43 // -------------------------------------------------------------------------- //
44 // Requirement Construction
46 // Facilities for building and manipulating template requirements.
48 // TODO: Simply assigning boolean_type_node to the result type of the
49 // expression seems right for constraints, but in the long-term we might want
50 // to be more flexible (i.e., allow some form of overload resolution?).
52 // Create a new logical node joining the subexpressions a and b.
54 join_requirements (tree_code c
, tree a
, tree b
)
56 gcc_assert (a
!= NULL_TREE
&& b
!= NULL_TREE
);
57 gcc_assert (c
== TRUTH_ANDIF_EXPR
|| c
== TRUTH_ORIF_EXPR
);
58 return build_min (c
, boolean_type_node
, a
, b
);
61 // Returns the conjunction of two requirements A and B, where A and B are
62 // reduced terms in the constraints language. Note that conjoining a non-null
63 // expression with NULL_TREE is an identity operation. That is, for some
66 // conjoin_constraints(a, NULL_TREE) == a
68 // If both A and B are NULL_TREE, the result is also NULL_TREE.
70 conjoin_constraints (tree a
, tree b
)
73 return b
? join_requirements (TRUTH_ANDIF_EXPR
, a
, b
) : a
;
80 // Transform the list of expressions in the T into a conjunction
81 // of requirements. T must be a TREE_VEC.
83 conjoin_constraints (tree t
)
85 gcc_assert (TREE_CODE (t
) == TREE_VEC
);
87 for (int i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
88 r
= conjoin_constraints (r
, TREE_VEC_ELT (t
, i
));
93 // -------------------------------------------------------------------------- //
94 // Constraint Resolution
96 // This facility is used to resolve constraint checks from requirement
97 // expressions. A constraint check is a call to a function template, declared
100 // The result of resolution is a pair (a list node) whose value is the
101 // matched declaration, and whose purpose contains the coerced template
102 // arguments that can be substituted into the call.
105 // Given an overload set, try to find a unique definition that can be
106 // instantiated by the template arguments.
108 // This function is not called for arbitrary call expressions. In particular,
109 // the call expression must be written with explicit template arguments
110 // and no function arguments. For example:
114 // The overload set will contain only template declarations.
116 // If a single definition is found, this returns a list node whose VALUE
117 // is the constraint function (not the template), and its PURPOSE is
118 // the complete set of arguments substituted into the parameter list.
120 resolve_constraint_check (tree ovl
, tree args
)
122 tree cands
= NULL_TREE
;
123 for (tree p
= ovl
; p
!= NULL_TREE
; p
= OVL_NEXT (p
))
125 // Get the next template overload.
126 tree tmpl
= OVL_CURRENT (p
);
127 if (TREE_CODE (tmpl
) != TEMPLATE_DECL
)
130 // Don't try to deduce checks for non-concept-like. We often
131 // end up trying to resolve constraints in functional casts
132 // as part of a post-fix expression. We can save time and
133 // headaches by not instantiating those declarations.
135 // NOTE: This masks a potential error, caused by instantiating
136 // non-deduced contexts using placeholder arguments.
137 tree fn
= DECL_TEMPLATE_RESULT (tmpl
);
138 if (DECL_ARGUMENTS (fn
))
140 if (!DECL_DECLARED_CONCEPT_P (fn
))
143 // Remember the candidate if we can deduce a substitution.
144 ++processing_template_decl
;
145 tree parms
= TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl
));
146 if (tree subst
= coerce_template_parms (parms
, args
, tmpl
))
147 if (subst
!= error_mark_node
)
148 cands
= tree_cons (subst
, fn
, cands
);
149 --processing_template_decl
;
152 // If we didn't find a unique candidate, then this is
153 // not a constraint check.
154 if (!cands
|| TREE_CHAIN (cands
))
157 // Constraints must be declared concepts.
158 tree decl
= TREE_VALUE (cands
);
159 if (!DECL_DECLARED_CONCEPT_P (decl
))
162 // Concept declarations must have a corresponding definition.
164 // TODO: This should be part of the up-front checking for
165 // a concept declaration.
166 if (!DECL_SAVED_TREE (decl
))
168 error_at (DECL_SOURCE_LOCATION (decl
),
169 "concept %q#D has no definition", decl
);
176 // Determine if the the call expression CALL is a constraint check, and
177 // return the concept declaration and arguments being checked. If CALL
178 // does not denote a constraint check, return NULL.
180 resolve_constraint_check (tree call
)
182 gcc_assert (TREE_CODE (call
) == CALL_EXPR
);
184 // A constraint check must be only a template-id expression. If
185 // it's a call to a base-link, its function(s) should be a
186 // template-id expressson. If this is not a template-id, then it
187 // cannot be a concept-check.
188 tree target
= CALL_EXPR_FN (call
);
189 if (BASELINK_P (target
))
190 target
= BASELINK_FUNCTIONS (target
);
191 if (TREE_CODE (target
) != TEMPLATE_ID_EXPR
)
194 // Get the overload set and template arguments and try to
195 // resolve the target.
196 tree ovl
= TREE_OPERAND (target
, 0);
197 tree args
= TREE_OPERAND (target
, 1);
198 return resolve_constraint_check (ovl
, args
);
201 // Given a call expression or template-id expression to a concept, EXPR,
202 // possibly including a placeholder argument, deduce the concept being checked
203 // and the prototype paraemter. Returns true if the constraint and prototype
204 // can be deduced and false otherwise. Note that the CHECK and PROTO arguments
205 // are set to NULL_TREE if this returns false.
207 deduce_constrained_parameter (tree expr
, tree
& check
, tree
& proto
)
209 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
211 // Get the check and prototype parameter from the variable template.
212 tree decl
= TREE_OPERAND (expr
, 0);
213 tree parms
= DECL_TEMPLATE_PARMS (decl
);
215 check
= DECL_TEMPLATE_RESULT (decl
);
216 proto
= TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), 0));
219 else if (TREE_CODE (expr
) == CALL_EXPR
)
221 // Resolve the constraint check to deduce the prototype parameter.
222 if (tree info
= resolve_constraint_check (expr
))
224 // Get function and argument from the resolved check expression and
225 // the prototype parameter. Note that if the first argument was a
226 // pack, we need to extract the first element ot get the prototype.
227 check
= TREE_VALUE (info
);
228 tree arg
= TREE_VEC_ELT (TREE_PURPOSE (info
), 0);
229 if (ARGUMENT_PACK_P (arg
))
230 arg
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg
), 0);
231 proto
= TREE_TYPE (arg
);
234 check
= proto
= NULL_TREE
;
241 // -------------------------------------------------------------------------- //
242 // Requirement Reduction
244 // Reduces a template requirement to a logical formula written in terms of
245 // atomic propositions, returing the new expression. If the expression cannot
246 // be reduced, a NULL_TREE is returned, indicating failure to reduce the
247 // original requirment.
252 tree
normalize_constraints (tree
);
253 tree
normalize_node (tree
);
254 tree
normalize_expr (tree
);
255 tree
normalize_stmt (tree
);
256 tree
normalize_decl (tree
);
257 tree
normalize_misc (tree
);
258 tree
normalize_logical (tree
);
259 tree
normalize_call (tree
);
260 tree
normalize_requires (tree
);
261 tree
normalize_expr_req (tree
);
262 tree
normalize_type_req (tree
);
263 tree
normalize_nested_req (tree
);
264 tree
normalize_var (tree
);
265 tree
normalize_template_id (tree
);
266 tree
normalize_stmt_list (tree
);
267 tree
normalize_cast (tree
);
268 tree
normalize_atom (tree
);
270 // Reduce the requirement T into a logical formula written in terms of
271 // atomic propositions.
273 normalize_node (tree t
)
275 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
281 return normalize_expr (t
);
284 return normalize_stmt (t
);
286 case tcc_declaration
:
287 return normalize_decl (t
);
289 case tcc_exceptional
:
290 return normalize_misc (t
);
292 // These kinds of expressions are atomic.
304 // Reduction rules for the expression node T.
306 normalize_expr (tree t
)
308 switch (TREE_CODE (t
))
310 case TRUTH_ANDIF_EXPR
:
311 case TRUTH_ORIF_EXPR
:
312 return normalize_logical (t
);
315 return normalize_call (t
);
318 return normalize_requires (t
);
321 return normalize_expr_req (t
);
324 return normalize_type_req (t
);
327 return normalize_nested_req (t
);
329 case TEMPLATE_ID_EXPR
:
330 return normalize_template_id (t
);
333 return normalize_cast (t
);
336 return normalize_node (BIND_EXPR_BODY (t
));
342 // Everything else is atomic.
344 return normalize_atom (t
);
349 // Reduction rules for the statement T.
351 normalize_stmt (tree t
)
353 switch (TREE_CODE (t
))
355 // Reduce the returned expression.
357 return normalize_node (TREE_OPERAND (t
, 0));
359 // These statements do not introduce propositions
360 // in the constraints language. Do not recurse.
371 // Reduction rules for the declaration T.
373 normalize_decl (tree t
)
375 switch (TREE_CODE (t
))
377 // References to var decls are atomic.
387 // Reduction rules for the node T.
389 normalize_misc (tree t
)
391 switch (TREE_CODE (t
))
393 // All of these are atomic.
400 return normalize_stmt_list (t
);
408 // Check that the logical expression is not a user-defined operator.
410 check_logical (tree t
)
412 // We can't do much for type dependent expressions.
413 if (type_dependent_expression_p (t
) || value_dependent_expression_p (t
))
416 // Resolve the logical operator. Note that template processing is
417 // disabled so we get the actual call or target expression back.
418 // not_processing_template_sentinel sentinel;
419 tree arg1
= TREE_OPERAND (t
, 0);
420 tree arg2
= TREE_OPERAND (t
, 1);
422 tree ovl
= NULL_TREE
;
423 tree expr
= build_new_op (input_location
, TREE_CODE (t
), LOOKUP_NORMAL
,
424 arg1
, arg2
, /*arg3*/NULL_TREE
,
426 if (TREE_CODE (expr
) != TREE_CODE (t
))
428 error ("user-defined operator %qs in constraint %qE",
429 operator_name_info
[TREE_CODE (t
)].name
, t
);
436 // Reduction rules for the binary logical expression T (&& and ||).
438 // Generate a new expression from the reduced operands. If either operand
439 // cannot be reduced, then the resulting expression is null.
441 normalize_logical (tree t
)
443 if (!check_logical (t
))
446 tree l
= normalize_expr (TREE_OPERAND (t
, 0));
447 tree r
= normalize_expr (TREE_OPERAND (t
, 1));
450 tree result
= copy_node (t
);
451 SET_EXPR_LOCATION (result
, EXPR_LOCATION (t
));
452 TREE_OPERAND (result
, 0) = l
;
453 TREE_OPERAND (result
, 1) = r
;
460 // Do a cursory investigation of the target in the call expression
461 // with the aim of early diagnosis of ill-formed constraints.
465 tree target
= CALL_EXPR_FN (t
);
466 if (TREE_CODE (target
) != TEMPLATE_ID_EXPR
)
468 tree tmpl
= TREE_OPERAND (target
, 0);
469 if (TREE_CODE (tmpl
) != TEMPLATE_DECL
)
471 tree decl
= DECL_TEMPLATE_RESULT (tmpl
);
472 if (TREE_CODE (decl
) == VAR_DECL
&& DECL_DECLARED_CONCEPT_P (decl
))
474 error ("invalid constraint %qE", t
);
475 inform (input_location
, "did you mean %qE", target
);
481 // Reduction rules for the call expression T.
483 // If T is a call to a constraint instantiate its definition and
484 // recursively reduce its returned expression.
486 normalize_call (tree t
)
491 // Is the function call actually a constraint check? If not, then it's
492 // an atom, and needs to be treated as such.
493 tree check
= resolve_constraint_check (t
);
495 return normalize_atom (t
);
497 tree fn
= TREE_VALUE (check
);
498 tree args
= TREE_PURPOSE (check
);
500 // Reduce the body of the function into the constriants language.
501 tree body
= normalize_constraints (DECL_SAVED_TREE (fn
));
503 return error_mark_node
;
505 // Instantiate the reduced results using the deduced args.
506 tree result
= tsubst_constraint_expr (body
, args
, false);
507 if (result
== error_mark_node
)
508 return error_mark_node
;
513 // Reduction rules for a variable template-id T.
515 // If T is a constraint, instantiate its initializer and recursively reduce its
518 normalize_var (tree t
)
520 tree decl
= DECL_TEMPLATE_RESULT (TREE_OPERAND (t
, 0));
521 if (!DECL_DECLARED_CONCEPT_P (decl
))
524 // Reduce the initializer of the variable into the constriants language.
525 tree body
= normalize_constraints (DECL_INITIAL (decl
));
527 return error_mark_node
;
529 // Instantiate the reduced results.
530 tree result
= tsubst_constraint_expr (body
, TREE_OPERAND (t
, 1), false);
531 if (result
== error_mark_node
)
532 return error_mark_node
;
537 // Reduction rules for the template-id T.
539 // It turns out that we often get requirements being written like this:
541 // template<typename T>
545 // Where Foo<T> should actually be written as Foo<T>(). Generate an
546 // error and suggest the improved writing.
548 normalize_template_id (tree t
)
550 tree tmpl
= TREE_OPERAND (t
, 0);
551 if (variable_concept_p (tmpl
))
552 return normalize_var (t
);
555 // FIXME: input_location is probably wrong, but there's not necessarly
556 // an expr location with the tree.
557 error_at (input_location
, "invalid constraint %qE", t
);
559 vec
<tree
, va_gc
>* args
= NULL
;
560 tree c
= finish_call_expr (t
, &args
, true, false, 0);
561 inform (input_location
, "did you mean %qE", c
);
563 return error_mark_node
;
567 // Reduce an expression requirement as a conjunction of its
568 // individual constraints.
570 normalize_expr_req (tree t
)
573 for (tree l
= TREE_OPERAND (t
, 0); l
; l
= TREE_CHAIN (l
))
574 r
= conjoin_constraints (r
, normalize_expr (TREE_VALUE (l
)));
578 // Reduce a type requirement by returing its underlying
581 normalize_type_req (tree t
)
583 return TREE_OPERAND (t
, 0);
586 // Reduce a nested requireemnt by returing its only operand.
588 normalize_nested_req (tree t
)
590 return TREE_OPERAND (t
, 0);
593 // Reduce a requires expr by reducing each requirement in turn,
594 // rewriting the list of requirements so that we end up with a
595 // list of expressions, some of which may be conjunctions.
597 normalize_requires (tree t
)
599 for (tree l
= TREE_OPERAND (t
, 1); l
; l
= TREE_CHAIN (l
))
600 TREE_VALUE (l
) = normalize_expr (TREE_VALUE (l
));
604 // Reduction rules for the statement list STMTS.
606 // Recursively reduce each statement in the list, concatenating each
607 // reduced result into a conjunction of requirements.
609 // A constexpr function may include statements other than a return
610 // statement. The primary purpose of these rules is to filter those
611 // non-return statements from the constraints language.
613 normalize_stmt_list (tree stmts
)
615 tree lhs
= NULL_TREE
;
616 tree_stmt_iterator i
= tsi_start (stmts
);
617 while (!tsi_end_p (i
))
619 if (tree rhs
= normalize_node (tsi_stmt (i
)))
620 lhs
= conjoin_constraints (lhs
, rhs
);
626 // Normalize a cast expression.
628 normalize_cast (tree t
)
630 // return normalize_node (TREE_VALUE (TREE_OPERAND (t, 0)));
631 return normalize_atom (t
);
634 // Normalize an atomic expression by performing some basic checks.
635 // In particular, if the type is known, it must be convertible to
638 normalize_atom (tree t
)
640 if (!type_dependent_expression_p (t
))
641 if (!can_convert (boolean_type_node
, TREE_TYPE (t
), tf_none
))
643 error ("atomic constraint %qE is not convertible to %<bool%>", t
);
649 // Reduce the requirement REQS into a logical formula written in terms of
650 // atomic propositions.
652 normalize_constraints (tree reqs
)
657 ++processing_template_decl
;
658 tree expr
= normalize_node (reqs
);
659 --processing_template_decl
;
666 // -------------------------------------------------------------------------- //
667 // Constraint Semantic Processing
669 // The following functions are called by the parser and substitution rules
670 // to create and evaluate constraint-related nodes.
672 // Returns the template constraints of declaration T. Note that
673 // T must be non-null.
675 get_constraints (tree t
)
677 gcc_assert (DECL_P (t
));
678 return LANG_DECL_MIN_CHECK (t
)->constraint_info
;
681 // Associate the given constraint information with the declaration. Don't
682 // build associations if ci is NULL_TREE.
684 set_constraints (tree t
, tree ci
)
686 gcc_assert (DECL_P (t
));
687 LANG_DECL_MIN_CHECK (t
)->constraint_info
= ci
;
690 // Returns a conjunction of shorthand requirements for the template
691 // parameter list PARMS. Note that the requirements are stored in
692 // the TYPE of each tree node.
694 get_shorthand_constraints (tree parms
)
696 tree reqs
= NULL_TREE
;
697 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
698 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
700 tree parm
= TREE_VEC_ELT (parms
, i
);
701 reqs
= conjoin_constraints(reqs
, TEMPLATE_PARM_CONSTRAINTS (parm
));
707 // Create an empty constraint into block.
708 inline tree_constraint_info
*
709 build_constraint_info ()
711 return (tree_constraint_info
*)make_node (CONSTRAINT_INFO
);
714 // Create a constraint info object, initialized with the given template
717 init_leading_constraints (tree reqs
)
719 tree_constraint_info
* ci
= build_constraint_info ();
720 ci
->leading_reqs
= reqs
;
724 // Initialize a constraint info object, initialized with the given
725 // trailing requirements.
727 init_trailing_constraints (tree reqs
)
729 tree_constraint_info
* ci
= build_constraint_info ();
730 ci
->trailing_reqs
= reqs
;
734 // Upodate the template requiremnets.
736 update_leading_constraints (tree ci
, tree reqs
) {
737 tree
& current
= CI_LEADING_REQS (ci
);
738 current
= conjoin_constraints (current
, reqs
);
742 // Set the trailing requiremnts to the given expression. Note that
743 // traling requirements cannot be updated once set: no other reqiurements
744 // can be found after parsing a trailing requires-clause.
746 update_trailing_constraints (tree ci
, tree reqs
) {
747 gcc_assert(CI_TRAILING_REQS (ci
) == NULL_TREE
);
748 CI_TRAILING_REQS (ci
) = reqs
;
753 // Return a constraint-info object containing the current template
754 // requirements. If constraints have already been assigned, then these
755 // are appended to the current constraints.
757 // Note that template constraints can be updated by the appearance of
758 // constrained type specifiers in a parameter list. These update the
759 // template requirements after the template header has been parsed.
761 save_leading_constraints (tree reqs
)
763 if (!reqs
|| reqs
== error_mark_node
)
765 else if (!current_template_reqs
)
766 return init_leading_constraints (reqs
);
768 return update_leading_constraints (current_template_reqs
, reqs
);
771 // Return a constraint info object containing saved trailing requirements.
772 // If there are already template requirements, these are added to the
773 // existing requirements. Otherwise, an empty constraint-info object
774 // holding only these trailing requirements is returned.
776 save_trailing_constraints (tree reqs
)
778 if (!reqs
|| reqs
== error_mark_node
)
780 else if (!current_template_reqs
)
781 return init_trailing_constraints (reqs
);
783 return update_trailing_constraints (current_template_reqs
, reqs
);
786 // Finish the template requirements, by computing the associated
787 // constrains (the conjunction of template and trailing requirements),
788 // and then decomposing that into sets of atomic propositions.
790 finish_template_constraints (tree ci
)
792 if (!ci
|| ci
== error_mark_node
)
795 // If these constraints have already been analyzed, don't do it
796 // a second time. This happens when groking a function decl.
797 // before creating its corresponding template.
798 if (CI_ASSUMPTIONS (ci
))
801 // Build and normalize th associated constraints. If any constraints
802 // are ill-formed, this is a hard error.
803 tree r1
= CI_LEADING_REQS (ci
);
804 tree r2
= CI_TRAILING_REQS (ci
);
805 tree reqs
= normalize_constraints (conjoin_constraints (r1
, r2
));
806 CI_ASSOCIATED_REQS (ci
) = reqs
;
808 // If normalization succeeds, decompose those expressions into sets
809 // of atomic constraints. Otherwise, mark this as an error.
811 CI_ASSUMPTIONS (ci
) = decompose_assumptions (reqs
);
813 CI_ASSUMPTIONS (ci
) = error_mark_node
;
817 // Returns true iff cinfo contains a valid constraint expression.
818 // This is the case when the associated requirements can be successfully
819 // decomposed into lists of atomic constraints.
821 valid_requirements_p (tree cinfo
)
824 return CI_ASSUMPTIONS (cinfo
) != error_mark_node
;
828 build_requires_expr (tree parms
, tree reqs
)
830 // Modify the declared parameters by removing their context (so they
831 // don't refer to the enclosing scope), and marking them constant (so
832 // we can actually check constexpr properties).
833 for (tree p
= parms
; p
&& !VOID_TYPE_P (TREE_VALUE (p
)); p
= TREE_CHAIN (p
))
835 tree parm
= TREE_VALUE (p
);
836 DECL_CONTEXT (parm
) = NULL_TREE
;
837 TREE_CONSTANT (parm
) = true;
841 tree r
= build_min (REQUIRES_EXPR
, boolean_type_node
, parms
, reqs
);
842 TREE_SIDE_EFFECTS (r
) = false;
843 TREE_CONSTANT (r
) = true;
847 // Evaluate an instantiatd requires expr, returning the truth node
848 // only when all sub-requirements have evaluated to true.
850 eval_requires_expr (tree reqs
)
852 for (tree t
= reqs
; t
; t
= TREE_CHAIN (t
)) {
853 tree r
= TREE_VALUE (t
);
854 r
= fold_non_dependent_expr (r
);
855 r
= maybe_constant_value (r
);
856 if (r
!= boolean_true_node
)
857 return boolean_false_node
;
859 return boolean_true_node
;
862 // Finish a requires expression, returning a node wrapping the parameters,
863 // PARMS, and the list of requirements REQS.
865 finish_requires_expr (tree parms
, tree reqs
)
867 if (processing_template_decl
)
868 return build_requires_expr (parms
, reqs
);
870 return eval_requires_expr (reqs
);
873 // Construct a unary expression that evaluates properties of the
874 // expression or type T, and has a boolean result type.
876 build_check_expr (tree_code c
, tree t
)
878 tree r
= build_min (c
, boolean_type_node
, t
);
879 TREE_SIDE_EFFECTS (r
) = false;
880 TREE_READONLY (r
) = true;
881 TREE_CONSTANT (r
) = true;
885 // Finish a syntax requirement, constructing a list embodying a sequence
886 // of checks for the validity of EXPR and TYPE, the convertibility of
887 // EXPR to TYPE, and the expression properties specified in SPECS.
889 finish_expr_requirement (tree expr
, tree type
, tree specs
)
891 gcc_assert (processing_template_decl
);
893 // Build a list of checks, starting with the valid expression.
894 tree result
= tree_cons (NULL_TREE
, finish_validexpr_expr (expr
), NULL_TREE
);
896 // If a type requirement was provided, build the result type checks.
899 // If the type is dependent, ensure that it can be validly
902 // NOTE: We can also disregard checks that result in the template
904 if (dependent_type_p (type
))
906 tree treq
= finish_type_requirement (type
);
907 result
= tree_cons (NULL_TREE
, treq
, result
);
910 // Ensure that the result of the expression can be converted to
912 tree decl_type
= finish_decltype_type (expr
, false, tf_none
);
913 tree creq
= finish_trait_expr (CPTK_IS_CONVERTIBLE_TO
, decl_type
, type
);
914 result
= tree_cons (NULL_TREE
, creq
, result
);
917 // If constraint specifiers are present, make them part of the
918 // list of constraints.
921 TREE_CHAIN (tree_last (specs
)) = result
;
925 // Finally, construct the syntactic requirement.
926 return build_check_expr (EXPR_REQ
, nreverse (result
));
929 // Finish a simple syntax requirement, returning a node representing
930 // a check that EXPR is a valid expression.
932 finish_expr_requirement (tree expr
)
934 gcc_assert (processing_template_decl
);
935 tree req
= finish_validexpr_expr (expr
);
936 tree reqs
= tree_cons (NULL_TREE
, req
, NULL_TREE
);
937 return build_check_expr (EXPR_REQ
, reqs
);
940 // Finish a type requirement, returning a node representing a check
941 // that TYPE will result in a valid type when instantiated.
943 finish_type_requirement (tree type
)
945 gcc_assert (processing_template_decl
);
946 tree req
= finish_validtype_expr (type
);
947 return build_check_expr (TYPE_REQ
, req
);
951 finish_nested_requirement (tree expr
)
953 gcc_assert (processing_template_decl
);
954 return build_check_expr (NESTED_REQ
, expr
);
957 // Finish a constexpr requirement, returning a node representing a
958 // check that EXPR, when instantiated, may be evaluated at compile time.
960 finish_constexpr_requirement (tree expr
)
962 gcc_assert (processing_template_decl
);
963 return finish_constexpr_expr (expr
);
966 // Finish the noexcept requirement by constructing a noexcept
967 // expression evaluating EXPR.
969 finish_noexcept_requirement (tree expr
)
971 gcc_assert (processing_template_decl
);
972 return finish_noexcept_expr (expr
, tf_none
);
975 // Returns the true or false node depending on the truth value of B.
979 return b
? boolean_true_node
: boolean_false_node
;
982 // Returns a finished validexpr-expr. Returns the true or false node
983 // depending on whether EXPR denotes a valid expression. This is the case
984 // when the expression has been successfully type checked.
986 // When processing a template declaration, the result is an expression
987 // representing the check.
989 finish_validexpr_expr (tree expr
)
991 if (processing_template_decl
)
992 return build_check_expr (VALIDEXPR_EXPR
, expr
);
993 return truth_node (expr
&& expr
!= error_mark_node
);
996 // Returns a finished validtype-expr. Returns the true or false node
997 // depending on whether T denotes a valid type name.
999 // When processing a template declaration, the result is an expression
1000 // representing the check.
1002 // FIXME: Semantics need to be aligned with the new version of the
1003 // specificaiton (i.e., we must be able to invent a function and
1004 // perform argument deduction against it).
1006 finish_validtype_expr (tree type
)
1010 sorry ("%<auto%< not supported in result type constraints\n");
1011 return error_mark_node
;
1014 if (processing_template_decl
)
1015 return build_check_expr (VALIDTYPE_EXPR
, type
);
1016 return truth_node (type
&& TYPE_P (type
));
1019 // Returns a finished constexpr-expr. Returns the true or false node
1020 // depending on whether the expression T may be evaluated at compile
1023 // When processing a template declaration, the result is an expression
1024 // representing the check.
1026 finish_constexpr_expr (tree expr
)
1028 if (processing_template_decl
)
1029 return build_check_expr (CONSTEXPR_EXPR
, expr
);
1031 // TODO: Actually check that the expression can be constexpr
1034 // return truth_node (potential_constant_expression (expr));
1035 sorry ("constexpr requirement");
1039 // Check that a constrained friend declaration function declaration,
1040 // FN, is admissable. This is the case only when the declaration depends
1041 // on template parameters and does not declare a specialization.
1043 check_constrained_friend (tree fn
, tree reqs
)
1045 if (fn
== error_mark_node
)
1047 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
1049 // If there are not constraints, this cannot be an error.
1053 // Constrained friend functions that don't depend on template
1054 // arguments are effectively meaningless.
1055 tree parms
= DECL_ARGUMENTS (fn
);
1056 tree result
= TREE_TYPE (TREE_TYPE (fn
));
1057 if (!(parms
&& uses_template_parms (parms
)) && !uses_template_parms (result
))
1059 error ("constrained friend does not depend on template parameters");
1065 // Build a new call expression, but don't actually generate a new
1066 // function call. We just want the tree, not the semantics.
1068 build_call_check (tree id
)
1070 ++processing_template_decl
;
1071 vec
<tree
, va_gc
> *fargs
= make_tree_vector();
1072 tree call
= finish_call_expr (id
, &fargs
, false, false, tf_none
);
1073 --processing_template_decl
;
1078 // Construct a concept check for the given TARGET. The target may be
1079 // an overload set or a baselink referring to an overload set. Template
1080 // arguments to the target are given by ARG and REST. If the target is
1081 // a function (overload set or baselink reffering to an overload set),
1082 // then ths builds the call expression TARGET<ARG, REST>(). If REST is
1083 // NULL_TREE, then the resulting check is just TARGET<ARG>().
1085 build_concept_check (tree target
, tree arg
, tree rest
)
1087 gcc_assert (rest
? TREE_CODE (rest
) == TREE_VEC
: true);
1089 // Build a template-id that acts as the call target using TARGET as
1090 // the template and ARG as the only explicit argument.
1091 int n
= rest
? TREE_VEC_LENGTH (rest
) : 0;
1092 tree targs
= make_tree_vec (n
+ 1);
1093 TREE_VEC_ELT (targs
, 0) = arg
;
1095 for (int i
= 0; i
< n
; ++i
)
1096 TREE_VEC_ELT (targs
, i
+ 1) = TREE_VEC_ELT (rest
, i
);
1097 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, n
+ 1);
1098 if (variable_template_p (target
))
1099 return lookup_template_variable (target
, targs
);
1102 tree id
= lookup_template_function (target
, targs
);
1103 return build_call_check (id
);
1107 // Returns a TYPE_DECL that contains sufficient information to build
1108 // a template parameter of the same kind as PROTO and constrained
1109 // by the concept declaration FN. PROTO is saved as the initializer of
1110 // the new type decl, and the constraining function is saved in
1113 // If specified ARGS provides additional arguments to the constraint
1114 // check. These are stored in the DECL_SIZE field.
1116 build_constrained_parameter (tree fn
, tree proto
, tree args
)
1118 tree name
= DECL_NAME (fn
);
1119 tree type
= TREE_TYPE (proto
);
1120 tree decl
= build_decl (input_location
, TYPE_DECL
, name
, type
);
1121 DECL_INITIAL (decl
) = proto
; // Describing parameter
1122 DECL_SIZE_UNIT (decl
) = fn
; // Constraining function declaration
1123 DECL_SIZE (decl
) = args
; // Extra template arguments.
1127 // Create a constraint expression for the given DECL that evaluates the
1128 // requirements specified by CONSTR, a TYPE_DECL that contains all the
1129 // information necessary to build the requirements (see finish_concept_name
1130 // for the layout of that TYPE_DECL).
1132 // Note that the constraints are neither reduced nor decomposed. That is
1133 // done only after the requires clause has been parsed (or not).
1135 finish_shorthand_constraint (tree decl
, tree constr
)
1137 // No requirements means no constraints.
1141 tree proto
= DECL_INITIAL (constr
); // The prototype declaration
1142 tree con
= DECL_SIZE_UNIT (constr
); // The concept declaration
1143 tree args
= DECL_SIZE (constr
); // Extra template arguments
1145 // If the parameter declaration is variadic, but the concept is not
1146 // then we need to apply the concept to every element in the pack.
1147 bool is_proto_pack
= template_parameter_pack_p (proto
);
1148 bool is_decl_pack
= template_parameter_pack_p (decl
);
1149 bool apply_to_all_p
= is_decl_pack
&& !is_proto_pack
;
1151 // Get the argument and overload used for the requirement. Adjust
1152 // if we're going to expand later.
1153 tree arg
= template_parm_to_arg (build_tree_list (NULL_TREE
, decl
));
1155 arg
= PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg
), 0));
1157 // Build the concept check. If it the constraint needs to be applied
1158 // to all elements of the parameter pack, then expand make the constraint
1161 if (TREE_CODE (con
) == VAR_DECL
)
1163 check
= build_concept_check (DECL_TI_TEMPLATE (con
), arg
, args
);
1167 tree ovl
= build_overload (DECL_TI_TEMPLATE (con
), NULL_TREE
);
1168 check
= build_concept_check (ovl
, arg
, args
);
1172 check
= make_pack_expansion (check
);
1174 // Set the type to indicate that this expansion will get special
1175 // treatment during instantiation.
1177 // TODO: Maybe this should be a different kind of node... one that
1178 // has all the same properties as a pack expansion, but has a definite
1179 // expansion when instantiated as part of an expression.
1181 // As of now, this is a hack.
1182 TREE_TYPE (check
) = boolean_type_node
;
1188 // -------------------------------------------------------------------------- //
1189 // Substitution Rules
1191 // The following functions implement substitution rules for constraints.
1194 // In an unevaluated context, the substitution of parm decls are not
1195 // properly chained during substitution. Do that here.
1197 fix_local_parms (tree sparms
)
1202 tree p
= TREE_CHAIN (sparms
);
1204 while (p
&& TREE_VALUE (p
) != void_type_node
)
1206 DECL_CHAIN (TREE_VALUE (q
)) = TREE_VALUE (p
);
1213 // Register local specializations for each of tparm and the corresponding
1214 // sparm. This is a helper function for tsubst_requires_expr.
1216 declare_local_parms (tree tparms
, tree sparms
)
1218 tree s
= TREE_VALUE (sparms
);
1219 for (tree p
= tparms
; p
&& !VOID_TYPE_P (TREE_VALUE (p
)); p
= TREE_CHAIN (p
))
1221 tree t
= TREE_VALUE (p
);
1222 if (DECL_PACK_P (t
))
1224 tree pack
= extract_fnparm_pack (t
, &s
);
1225 register_local_specialization (pack
, t
);
1229 register_local_specialization (s
, t
);
1235 // Substitute ARGS into the parameter list T, producing a sequence of
1236 // local parameters (variables) in the current scope.
1238 tsubst_local_parms (tree t
,
1240 tsubst_flags_t complain
,
1243 tree r
= fix_local_parms (tsubst (t
, args
, complain
, in_decl
));
1244 if (r
== error_mark_node
)
1245 return error_mark_node
;
1247 // Register the instantiated args as local parameters.
1249 declare_local_parms (t
, r
);
1254 // Substitute ARGS into the requirement body (list of requirements), T.
1255 // Note that if any substitutions fail, then this is equivalent to
1258 tsubst_requirement_body (tree t
, tree args
, tree in_decl
)
1263 tree e
= tsubst_expr (TREE_VALUE (t
), args
, tf_none
, in_decl
, false);
1264 if (e
== error_mark_node
)
1265 e
= boolean_false_node
;
1266 r
= tree_cons (NULL_TREE
, e
, r
);
1273 // Substitute ARGS into the requires expression T.
1275 tsubst_requires_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
1277 local_specialization_stack stack
;
1278 tree p
= tsubst_local_parms (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
1279 tree r
= tsubst_requirement_body (TREE_OPERAND (t
, 1), args
, in_decl
);
1280 return finish_requires_expr (p
, r
);
1283 // Substitute ARGS into the valid-expr expression T.
1285 tsubst_validexpr_expr (tree t
, tree args
, tree in_decl
)
1287 tree r
= tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1288 return finish_validexpr_expr (r
);
1291 // Substitute ARGS into the valid-type expression T.
1293 tsubst_validtype_expr (tree t
, tree args
, tree in_decl
)
1295 tree r
= tsubst (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
);
1296 return finish_validtype_expr (r
);
1299 // Substitute ARGS into the constexpr expression T.
1301 tsubst_constexpr_expr (tree t
, tree args
, tree in_decl
)
1303 tree r
= tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1304 return finish_constexpr_expr (r
);
1307 // Substitute ARGS into the expr requirement T. Note that a requirement
1308 // node is instantiated from a non-reduced context (e.g., static_assert).
1310 tsubst_expr_req (tree t
, tree args
, tree in_decl
)
1313 for (tree l
= TREE_OPERAND (t
, 0); l
; l
= TREE_CHAIN (l
))
1315 tree e
= tsubst_expr (TREE_VALUE (l
), args
, tf_none
, in_decl
, false);
1316 r
= conjoin_constraints (r
, e
);
1321 // Substitute ARGS into the type requirement T. Note that a requirement
1322 // node is instantiated from a non-reduced context (e.g., static_assert).
1324 tsubst_type_req (tree t
, tree args
, tree in_decl
)
1326 return tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1329 // Substitute ARGS into the nested requirement T. Note that a requirement
1330 // node is instantiated from a non-reduced context (e.g., static_assert).
1332 tsubst_nested_req (tree t
, tree args
, tree in_decl
)
1334 return tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1337 // Used in various contexts to control substitution. In particular, when
1338 // non-zero, the substitution of NULL arguments into a type will still
1339 // process the type as if passing non-NULL arguments, allowing type
1340 // expressions to be fully elaborated during substitution.
1341 int processing_constraint
;
1343 // Substitute the template arguments ARGS into the requirement
1344 // expression REQS. Errors resulting from substitution are not
1347 // If DO_NOT_FOLD is true, then the requirements are substituted as
1348 // if parsing a template declaration, which causes the resulting expression
1349 // to not be folded.
1351 tsubst_constraint_expr (tree reqs
, tree args
, bool do_not_fold
)
1353 cp_unevaluated guard
;
1354 ++processing_constraint
;
1356 ++processing_template_decl
;
1357 tree r
= tsubst_expr (reqs
, args
, tf_none
, NULL_TREE
, false);
1359 --processing_template_decl
;
1360 --processing_constraint
;
1364 // Substitute into the constraint information, producing a new constraint
1367 tsubst_constraint_info (tree ci
, tree args
)
1369 if (!ci
|| ci
== error_mark_node
)
1372 // Substitute into the various constraint fields.
1373 tree_constraint_info
* result
= build_constraint_info ();
1374 if (tree r
= CI_LEADING_REQS (ci
))
1375 result
->leading_reqs
= tsubst_constraint_expr (r
, args
, true);
1376 if (tree r
= CI_TRAILING_REQS (ci
))
1377 result
->trailing_reqs
= tsubst_constraint_expr (r
, args
, true);
1378 if (tree r
= CI_ASSOCIATED_REQS (ci
))
1379 result
->associated_reqs
= tsubst_constraint_expr (r
, args
, true);
1381 // Re-normalize the constraints to ensure that we haven't picked
1382 // any fatal errors when substituting.
1383 if (!normalize_constraints (result
->associated_reqs
))
1385 result
->associated_reqs
= error_mark_node
;
1386 result
->assumptions
= error_mark_node
;
1390 // Analyze the resulting constraints.
1391 result
->assumptions
= decompose_assumptions (result
->associated_reqs
);
1394 return (tree
)result
;
1397 // -------------------------------------------------------------------------- //
1398 // Constraint Satisfaction
1400 // The following functions are responsible for the instantiation and
1401 // evaluation of constraints.
1404 // Returns true iff the atomic constraint, REQ, is satisfied. This
1405 // is the case when substitution succeeds and the resulting expression
1406 // evaluates to true.
1408 check_satisfied (tree req
, tree args
)
1410 // If any arguments are dependent, then we can't check the
1411 // requirements. Just return true.
1412 if (args
&& uses_template_parms (args
))
1415 // Instantiate and evaluate the requirements.
1416 req
= tsubst_constraint_expr (req
, args
, false);
1417 if (req
== error_mark_node
)
1420 // Reduce any remaining TRAIT_EXPR nodes before evaluating.
1421 req
= fold_non_dependent_expr (req
);
1423 // Requirements are satisfied when REQS evaluates to true.
1424 tree result
= cxx_constant_value (req
);
1425 return result
== boolean_true_node
;
1430 // Check the instantiated declaration constraints.
1432 check_constraints (tree cinfo
)
1434 // No constraints? Satisfied.
1437 // Invalid constraints, not satisfied.
1438 else if (!valid_requirements_p (cinfo
))
1440 // Funnel back into the dependent checking branch. This forces
1441 // one more substitution through the constraints, which removes
1442 // all remaining expressions that are not constant expressions
1443 // (e.g., template-id expressions).
1445 return check_satisfied (CI_ASSOCIATED_REQS (cinfo
), NULL_TREE
);
1448 // Check the constraints in CINFO against the given ARGS, returning
1449 // true when the constraints are satisfied and false otherwise.
1451 check_constraints (tree cinfo
, tree args
)
1453 // If there are no constraints then this is trivally satisfied.
1456 // Invlaid requirements cannot be satisfied.
1457 else if (!valid_requirements_p (cinfo
))
1460 return check_satisfied (CI_ASSOCIATED_REQS (cinfo
), args
);
1464 // Check the constraints of the declaration or type T, against
1465 // the specified arguments. Returns true if the constraints are
1466 // satisfied and false otherwise.
1468 check_template_constraints (tree t
, tree args
)
1470 return check_constraints (get_constraints (t
), args
);
1473 // -------------------------------------------------------------------------- //
1474 // Constraint Relations
1476 // Interfaces for determining equivalency and ordering of constraints.
1478 // Returns true when A and B are equivalent constraints.
1480 equivalent_constraints (tree a
, tree b
)
1482 return cp_tree_equal (a
, b
);
1485 // Returns true if the template declarations A and B have equivalent
1486 // constraints. This is the case when A's constraints subsume B's and
1487 // when B's also constrain A's.
1489 equivalently_constrained (tree a
, tree b
)
1491 gcc_assert (TREE_CODE (a
) == TREE_CODE (b
));
1492 return equivalent_constraints (get_constraints (a
), get_constraints (b
));
1495 // Returns true when the template declaration A's constraints subsume
1496 // those of the template declaration B.
1498 subsumes_constraints (tree a
, tree b
)
1500 gcc_assert (TREE_CODE (a
) == TREE_CODE (b
));
1501 return subsumes (get_constraints (a
), get_constraints (b
));
1504 // Determines which of the templates, A or B, is more constrained.
1505 // That is, which template's constraints subsume but are not subsumed
1508 // Returns 1 if A is more constrained than B, -1 if B is more constrained
1509 // than A, and 0 otherwise.
1511 more_constrained (tree a
, tree b
) {
1513 if (subsumes_constraints (a
, b
))
1515 if (subsumes_constraints (b
, a
))
1521 // -------------------------------------------------------------------------- //
1522 // Constraint Diagnostics
1526 // Given an arbitrary constraint expression, normalize it and
1527 // then check it. We have to normalize so we don't accidentally
1528 // instantiate concept declarations.
1530 check_diagnostic_constraints (tree reqs
, tree args
)
1532 return check_satisfied (normalize_constraints (reqs
), args
);
1535 void diagnose_node (location_t
, tree
, tree
);
1537 // Diagnose a constraint failure for type trait expressions.
1539 diagnose_trait (location_t loc
, tree t
, tree args
)
1541 if (check_diagnostic_constraints (t
, args
))
1544 tree subst
= tsubst_constraint_expr (t
, args
, true);
1546 if (subst
== error_mark_node
)
1548 inform (input_location
, " substitution failure in %qE", t
);
1552 tree t1
= TRAIT_EXPR_TYPE1 (subst
);
1553 tree t2
= TRAIT_EXPR_TYPE2 (subst
);
1554 switch (TRAIT_EXPR_KIND (t
))
1556 case CPTK_HAS_NOTHROW_ASSIGN
:
1557 inform (loc
, " %qT is not nothrow copy assignable", t1
);
1559 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
1560 inform (loc
, " %qT is not nothrow default constructible", t1
);
1562 case CPTK_HAS_NOTHROW_COPY
:
1563 inform (loc
, " %qT is not nothrow copy constructible", t1
);
1565 case CPTK_HAS_TRIVIAL_ASSIGN
:
1566 inform (loc
, " %qT is not trivially copy assignable", t1
);
1568 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
1569 inform (loc
, " %qT is not trivially default constructible", t1
);
1571 case CPTK_HAS_TRIVIAL_COPY
:
1572 inform (loc
, " %qT is not trivially copy constructible", t1
);
1574 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
1575 inform (loc
, " %qT is not trivially destructible", t1
);
1577 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
1578 inform (loc
, " %qT does not have a virtual destructor", t1
);
1580 case CPTK_IS_ABSTRACT
:
1581 inform (loc
, " %qT is not an abstract class", t1
);
1583 case CPTK_IS_BASE_OF
:
1584 inform (loc
, " %qT is not a base of %qT", t1
, t2
);
1587 inform (loc
, " %qT is not a class", t1
);
1589 case CPTK_IS_CONVERTIBLE_TO
:
1590 inform (loc
, " %qT is not convertible to %qT", t1
, t2
);
1593 inform (loc
, " %qT is not an empty class", t1
);
1596 inform (loc
, " %qT is not an enum", t1
);
1599 inform (loc
, " %qT is not a final class", t1
);
1601 case CPTK_IS_LITERAL_TYPE
:
1602 inform (loc
, " %qT is not a literal type", t1
);
1605 inform (loc
, " %qT is not a POD type", t1
);
1607 case CPTK_IS_POLYMORPHIC
:
1608 inform (loc
, " %qT is not a polymorphic type", t1
);
1610 case CPTK_IS_SAME_AS
:
1611 inform (loc
, " %qT is not the same as %qT", t1
, t2
);
1613 case CPTK_IS_STD_LAYOUT
:
1614 inform (loc
, " %qT is not an standard layout type", t1
);
1616 case CPTK_IS_TRIVIAL
:
1617 inform (loc
, " %qT is not a trivial type", t1
);
1620 inform (loc
, " %qT is not a union", t1
);
1627 // Diagnose a failed concept check in concept indicated by T, where
1628 // T is the result of resolve_constraint_check. Recursively analyze
1629 // the nested requiremets for details.
1631 diagnose_check (location_t loc
, tree t
, tree args
)
1633 tree fn
= TREE_VALUE (t
);
1634 tree targs
= TREE_PURPOSE (t
);
1635 tree body
= DECL_SAVED_TREE (fn
);
1639 inform (loc
, " failure in constraint %q#D", DECL_TI_TEMPLATE (fn
));
1641 // Perform a mini-reduction on the constraint.
1642 if (TREE_CODE (body
) == BIND_EXPR
)
1643 body
= BIND_EXPR_BODY (body
);
1644 if (TREE_CODE (body
) == RETURN_EXPR
)
1645 body
= TREE_OPERAND (body
, 0);
1647 // Locally instantiate the body with the call's template args,
1648 // and recursively diagnose.
1649 body
= tsubst_constraint_expr (body
, targs
, true);
1651 diagnose_node (loc
, body
, args
);
1654 // Diagnose constraint failures from the call expression T.
1656 diagnose_call (location_t loc
, tree t
, tree args
)
1658 if (check_diagnostic_constraints (t
, args
))
1661 // If this is a concept, we're going to recurse.
1662 // If it's just a call, then we can emit a simple message.
1663 if (tree check
= resolve_constraint_check (t
))
1664 diagnose_check (loc
, check
, args
);
1666 inform (loc
, " %qE evaluated to false", t
);
1669 // Diagnose constraint failures in a variable concept.
1671 diagnose_var (location_t loc
, tree t
, tree args
)
1673 // If the template-id isn't a variable template, it can't be a
1674 // valid constraint.
1675 if (!variable_template_p (TREE_OPERAND (t
, 0)))
1677 inform (loc
, " invalid constraint %qE", t
);
1681 if (check_diagnostic_constraints (t
, args
))
1684 tree var
= DECL_TEMPLATE_RESULT (TREE_OPERAND (t
, 0));
1685 tree body
= DECL_INITIAL (var
);
1686 tree targs
= TREE_OPERAND (t
, 1);
1687 tree subst
= tsubst_constraint_expr (body
, targs
, true);
1689 inform (loc
, " failure in constraint %q#D", DECL_TI_TEMPLATE (var
));
1691 diagnose_node (loc
, subst
, args
);
1694 // Diagnose specific constraint failures.
1696 diagnose_requires (location_t loc
, tree t
, tree args
)
1698 if (check_diagnostic_constraints (t
, args
))
1701 tree subst
= tsubst_constraint_expr (t
, args
, true);
1703 // Print the header for the requires expression.
1704 tree parms
= TREE_OPERAND (subst
, 0);
1705 if (!VOID_TYPE_P (TREE_VALUE (parms
)))
1706 inform (loc
, " requiring syntax with values %Z", TREE_OPERAND (subst
, 0));
1708 // Create a new local specialization binding for the arguments.
1709 // This lets us instantiate sub-expressions separately from the
1711 local_specialization_stack locals
;
1712 declare_local_parms (TREE_OPERAND (t
, 0), TREE_OPERAND (subst
, 0));
1714 // Iterate over the sub-requirements and try instantiating each.
1715 for (tree l
= TREE_OPERAND (t
, 1); l
; l
= TREE_CHAIN (l
))
1716 diagnose_node (loc
, TREE_VALUE (l
), args
);
1720 diagnose_validexpr (location_t loc
, tree t
, tree args
)
1722 if (check_diagnostic_constraints (t
, args
))
1724 inform (loc
, " %qE is not a valid expression", TREE_OPERAND (t
, 0));
1728 diagnose_validtype (location_t loc
, tree t
, tree args
)
1730 if (check_diagnostic_constraints (t
, args
))
1733 // Substitute into the qualified name.
1734 tree name
= TREE_OPERAND (t
, 0);
1735 if (tree cxt
= TYPE_CONTEXT (name
))
1737 tree id
= TYPE_IDENTIFIER (name
);
1738 cxt
= tsubst (cxt
, args
, tf_none
, NULL_TREE
);
1739 name
= build_qualified_name (NULL_TREE
, cxt
, id
, false);
1740 inform (loc
, " %qE does not name a valid type", name
);
1744 inform (loc
, " %qT does not name a valid type", name
);
1749 diagnose_constexpr (location_t loc
, tree t
, tree args
)
1751 if (check_diagnostic_constraints (t
, args
))
1753 inform (loc
, " %qE is not a constant expression", TREE_OPERAND (t
, 0));
1757 diagnose_noexcept (location_t loc
, tree t
, tree args
)
1759 if (check_diagnostic_constraints (t
, args
))
1761 inform (loc
, " %qE propagates exceptions", TREE_OPERAND (t
, 0));
1764 // Diagnose a constraint failure in the expression T.
1766 diagnose_other (location_t loc
, tree t
, tree args
)
1768 if (check_diagnostic_constraints (t
, args
))
1770 inform (loc
, " %qE evaluated to false", t
);
1773 // Diagnose a constraint failure in the subtree T.
1775 diagnose_node (location_t loc
, tree t
, tree args
)
1777 switch (TREE_CODE (t
))
1779 case TRUTH_ANDIF_EXPR
:
1780 diagnose_node (loc
, TREE_OPERAND (t
, 0), args
);
1781 diagnose_node (loc
, TREE_OPERAND (t
, 1), args
);
1784 case TRUTH_ORIF_EXPR
:
1785 // TODO: Design better diagnostics for dijunctions.
1786 diagnose_other (loc
, t
, args
);
1790 diagnose_trait (loc
, t
, args
);
1794 diagnose_call (loc
, t
, args
);
1798 diagnose_requires (loc
, t
, args
);
1801 case VALIDEXPR_EXPR
:
1802 diagnose_validexpr (loc
, t
, args
);
1805 case VALIDTYPE_EXPR
:
1806 diagnose_validtype (loc
, t
, args
);
1809 case CONSTEXPR_EXPR
:
1810 diagnose_constexpr (loc
, t
, args
);
1814 diagnose_noexcept (loc
, t
, args
);
1817 case TEMPLATE_ID_EXPR
:
1818 diagnose_var (loc
, t
, args
);
1822 diagnose_other (loc
, t
, args
);
1827 // Diagnose a constraint failure in the requirements expression REQS.
1829 diagnose_requirements (location_t loc
, tree reqs
, tree args
)
1831 diagnose_node (loc
, reqs
, args
);
1834 // Create a tree node representing the substitution of ARGS into
1835 // the parameters of TMPL. The resulting structure is passed as an
1836 // for diagnosing substitutions.
1838 make_subst (tree tmpl
, tree args
)
1840 tree subst
= tree_cons (NULL_TREE
, args
, NULL_TREE
);
1841 TREE_TYPE (subst
) = DECL_TEMPLATE_PARMS (tmpl
);
1847 // Emit diagnostics detailing the failure ARGS to satisfy the constraints
1848 // of the template declaration, TMPL.
1850 diagnose_constraints (location_t loc
, tree decl
, tree args
)
1852 tree ci
= get_constraints (decl
);
1854 // If the constraints could not be reduced, then we can't diagnose them.
1855 if (!valid_requirements_p (ci
))
1857 inform (loc
, " invalid constraints");
1861 // If this is a specialization of a template, we want to diagnose
1862 // the dependent constraints. Also update the template arguments.
1863 if (DECL_USE_TEMPLATE (decl
)) {
1864 args
= DECL_TI_ARGS (decl
);
1865 decl
= DECL_TI_TEMPLATE (decl
);
1868 // Otherwise, diagnose the actual failed constraints.
1869 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
1870 inform (loc
, " constraints not satisfied %S", make_subst (decl
, args
));
1872 inform (loc
, " constraints not satisfied");
1874 // Diagnose the constraints by recursively decomposing and
1875 // evaluating the template requirements.
1876 tree reqs
= CI_ASSOCIATED_REQS (get_constraints (decl
));
1877 diagnose_requirements (loc
, reqs
, args
);