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_requirements(a, NULL_TREE) == a
68 // If both A and B are NULL_TREE, the result is also NULL_TREE.
70 conjoin_requirements (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_requirements (tree t
)
85 gcc_assert (TREE_CODE (t
) == TREE_VEC
);
87 for (int i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
88 r
= conjoin_requirements (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 be a template-id expression.
185 tree target
= CALL_EXPR_FN (call
);
186 if (TREE_CODE (target
) != TEMPLATE_ID_EXPR
)
189 // Get the overload set and template arguments and try to
190 // resolve the target.
191 tree ovl
= TREE_OPERAND (target
, 0);
192 tree args
= TREE_OPERAND (target
, 1);
193 return resolve_constraint_check (ovl
, args
);
197 // -------------------------------------------------------------------------- //
198 // Requirement Reduction
200 // Reduces a template requirement to a logical formula written in terms of
201 // atomic propositions, returing the new expression. If the expression cannot
202 // be reduced, a NULL_TREE is returned, indicating failure to reduce the
203 // original requirment.
208 static tree
reduce_node (tree
);
209 static tree
reduce_expr (tree
);
210 static tree
reduce_stmt (tree
);
211 static tree
reduce_decl (tree
);
212 static tree
reduce_misc (tree
);
214 static tree
reduce_logical (tree
);
215 static tree
reduce_call (tree
);
216 static tree
reduce_requires (tree
);
217 static tree
reduce_expr_req (tree
);
218 static tree
reduce_type_req (tree
);
219 static tree
reduce_nested_req (tree
);
220 static tree
reduce_template_id (tree
);
221 static tree
reduce_stmt_list (tree
);
223 // Reduce the requirement T into a logical formula written in terms of
224 // atomic propositions.
228 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
234 return reduce_expr (t
);
237 return reduce_stmt (t
);
239 case tcc_declaration
:
240 return reduce_decl (t
);
242 case tcc_exceptional
:
243 return reduce_misc (t
);
245 // These kinds of expressions are atomic.
257 // Reduction rules for the expression node T.
261 switch (TREE_CODE (t
))
263 case TRUTH_ANDIF_EXPR
:
264 case TRUTH_ORIF_EXPR
:
265 return reduce_logical (t
);
268 return reduce_call (t
);
271 return reduce_requires (t
);
274 return reduce_expr_req (t
);
277 return reduce_type_req (t
);
280 return reduce_nested_req (t
);
282 case TEMPLATE_ID_EXPR
:
283 return reduce_template_id (t
);
286 return reduce_node (TREE_VALUE (TREE_OPERAND (t
, 0)));
289 return reduce_node (BIND_EXPR_BODY (t
));
295 // Everything else is atomic.
302 // Reduction rules for the statement T.
306 switch (TREE_CODE (t
))
308 // Reduce the returned expression.
310 return reduce_node (TREE_OPERAND (t
, 0));
312 // These statements do not introduce propositions
313 // in the constraints language. Do not recurse.
324 // Reduction rules for the declaration T.
328 switch (TREE_CODE (t
))
330 // References to var decls are atomic.
340 // Reduction rules for the node T.
344 switch (TREE_CODE (t
))
346 // Errors and traits are atomic.
352 return reduce_stmt_list (t
);
360 // Reduction rules for the binary logical expression T (&& and ||).
362 // Generate a new expression from the reduced operands. If either operand
363 // cannot be reduced, then the resulting expression is null.
365 reduce_logical (tree t
)
367 tree l
= reduce_expr (TREE_OPERAND (t
, 0));
368 tree r
= reduce_expr (TREE_OPERAND (t
, 1));
372 TREE_OPERAND (t
, 0) = l
;
373 TREE_OPERAND (t
, 1) = r
;
380 // Reduction rules for the call expression T.
382 // If T is a call to a constraint instantiate its definition and
383 // recursively reduce its returned expression.
387 // Is the function call actually a constraint check?
388 tree check
= resolve_constraint_check (t
);
392 tree fn
= TREE_VALUE (check
);
393 tree args
= TREE_PURPOSE (check
);
395 // Reduce the body of the function into the constriants language.
396 tree body
= reduce_requirements (DECL_SAVED_TREE (fn
));
399 error ("could not inline requirements from %qD", fn
);
400 return error_mark_node
;
403 // Instantiate the reduced results using the deduced args.
404 tree result
= instantiate_requirements (body
, args
);
405 if (result
== error_mark_node
)
407 error ("could not instantiate requirements from %qD", fn
);
408 return error_mark_node
;
413 // Reduction rules for the template-id T.
415 // It turns out that we often get requirements being written like this:
417 // template<typename T>
421 // Where Foo<T> should actually be written as Foo<T>(). Generate an
422 // error and suggest the improved writing.
424 reduce_template_id (tree t
)
426 vec
<tree
, va_gc
>* args
= NULL
;
427 tree c
= finish_call_expr (t
, &args
, true, false, 0);
428 error_at (EXPR_LOCATION (t
), "invalid requirement");
429 inform (EXPR_LOCATION (t
), "did you mean %qE", c
);
434 // Reduce an expression requirement as a conjunction of its
435 // individual constraints.
437 reduce_expr_req (tree t
)
440 for (tree l
= TREE_OPERAND (t
, 0); l
; l
= TREE_CHAIN (l
))
441 r
= conjoin_requirements (r
, reduce_expr (TREE_VALUE (l
)));
445 // Reduce a type requirement by returing its underlying
448 reduce_type_req (tree t
)
450 return TREE_OPERAND (t
, 0);
453 // Reduce a nested requireemnt by returing its only operand.
455 reduce_nested_req (tree t
)
457 return TREE_OPERAND (t
, 0);
460 // Reduce a requires expr by reducing each requirement in turn,
461 // rewriting the list of requirements so that we end up with a
462 // list of expressions, some of which may be conjunctions.
464 reduce_requires (tree t
)
466 for (tree l
= TREE_OPERAND (t
, 1); l
; l
= TREE_CHAIN (l
))
467 TREE_VALUE (l
) = reduce_expr (TREE_VALUE (l
));
471 // Reduction rules for the statement list STMTS.
473 // Recursively reduce each statement in the list, concatenating each
474 // reduced result into a conjunction of requirements.
476 // A constexpr function may include statements other than a return
477 // statement. The primary purpose of these rules is to filter those
478 // non-return statements from the constraints language.
480 reduce_stmt_list (tree stmts
)
482 tree lhs
= NULL_TREE
;
483 tree_stmt_iterator i
= tsi_start (stmts
);
484 while (!tsi_end_p (i
))
486 if (tree rhs
= reduce_node (tsi_stmt (i
)))
487 lhs
= conjoin_requirements (lhs
, rhs
);
495 // Reduce the requirement REQS into a logical formula written in terms of
496 // atomic propositions.
498 reduce_requirements (tree reqs
)
500 return reduce_node (reqs
);
503 // -------------------------------------------------------------------------- //
504 // Constraint Semantic Processing
506 // The following functions are called by the parser and substitution rules
507 // to create and evaluate constraint-related nodes.
509 // Create a constraint-info node from the specified requirements.
511 make_constraints (tree reqs
)
513 // No requirements == no constraints
517 // Reduce the requirements into a single expression of constraints.
518 tree expr
= reduce_requirements (reqs
);
519 if (expr
== error_mark_node
)
520 return error_mark_node
;
522 // Decompose those expressions into lists of lists of atomic
524 tree assume
= decompose_assumptions (expr
);
526 // Build the constraint info.
527 tree_constraint_info
*cinfo
=
528 (tree_constraint_info
*)make_node (CONSTRAINT_INFO
);
529 cinfo
->spelling
= reqs
;
530 cinfo
->requirements
= expr
;
531 cinfo
->assumptions
= assume
;
535 // Returns the template constraints of declaration T. If T is not a
536 // template, this return NULL_TREE. Note that T must be non-null.
538 get_constraints (tree t
)
540 gcc_assert (DECL_P (t
));
541 if (TREE_CODE (t
) != TEMPLATE_DECL
)
543 if (!DECL_TEMPLATE_INFO (t
))
546 return DECL_CONSTRAINTS (DECL_TI_TEMPLATE (t
));
548 return DECL_CONSTRAINTS (t
);
551 // Returns a conjunction of shorthand requirements for the template
552 // parameter list PARMS. Note that the requirements are stored in
553 // the TYPE of each tree node.
555 get_shorthand_requirements (tree parms
)
557 tree reqs
= NULL_TREE
;
558 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
559 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
561 tree parm
= TREE_VEC_ELT (parms
, i
);
562 reqs
= conjoin_requirements(reqs
, TREE_TYPE (parm
));
567 // Finish the template requirement, EXPR, by translating it into
568 // a constraint information record.
570 finish_template_requirements (tree expr
)
572 if (expr
== error_mark_node
)
575 return make_constraints (expr
);
579 build_requires_expr (tree parms
, tree reqs
)
581 // Modify the declared parameters by removing their context (so they
582 // don't refer to the enclosing scope), and marking them constant (so
583 // we can actually check constexpr properties).
584 for (tree p
= parms
; p
&& !VOID_TYPE_P (TREE_VALUE (p
)); p
= TREE_CHAIN (p
))
586 tree parm
= TREE_VALUE (p
);
587 DECL_CONTEXT (parm
) = NULL_TREE
;
588 TREE_CONSTANT (parm
) = true;
592 tree r
= build_min (REQUIRES_EXPR
, boolean_type_node
, parms
, reqs
);
593 TREE_SIDE_EFFECTS (r
) = false;
594 TREE_CONSTANT (r
) = true;
598 // Evaluate an instantiatd requires expr, returning the truth node
599 // only when all sub-requirements have evaluated to true.
601 eval_requires_expr (tree reqs
)
603 for (tree t
= reqs
; t
; t
= TREE_CHAIN (t
)) {
604 tree r
= TREE_VALUE (t
);
605 r
= fold_non_dependent_expr (r
);
606 r
= maybe_constant_value (r
);
607 if (r
!= boolean_true_node
)
608 return boolean_false_node
;
610 return boolean_true_node
;
613 // Finish a requires expression, returning a node wrapping the parameters,
614 // PARMS, and the list of requirements REQS.
616 finish_requires_expr (tree parms
, tree reqs
)
618 if (processing_template_decl
)
619 return build_requires_expr (parms
, reqs
);
621 return eval_requires_expr (reqs
);
624 // Construct a unary expression that evaluates properties of the
625 // expression or type T, and has a boolean result type.
627 build_check_expr (tree_code c
, tree t
)
629 tree r
= build_min (c
, boolean_type_node
, t
);
630 TREE_SIDE_EFFECTS (r
) = false;
631 TREE_READONLY (r
) = true;
632 TREE_CONSTANT (r
) = true;
636 // Finish a syntax requirement, constructing a list embodying a sequence
637 // of checks for the validity of EXPR and TYPE, the convertibility of
638 // EXPR to TYPE, and the expression properties specified in SPECS.
640 finish_expr_requirement (tree expr
, tree type
, tree specs
)
642 gcc_assert (processing_template_decl
);
644 // Build a list of checks, starting with the valid expression.
645 tree result
= tree_cons (NULL_TREE
, finish_validexpr_expr (expr
), NULL_TREE
);
647 // If a type requirement was provided, build the result type checks.
650 // If the type is dependent, ensure that it can be validly
653 // NOTE: We can also disregard checks that result in the template
655 if (dependent_type_p (type
))
657 tree treq
= finish_type_requirement (type
);
658 result
= tree_cons (NULL_TREE
, treq
, result
);
661 // Ensure that the result of the expression can be converted to
663 tree decl_type
= finish_decltype_type (expr
, false, tf_none
);
664 tree creq
= finish_trait_expr (CPTK_IS_CONVERTIBLE_TO
, decl_type
, type
);
665 result
= tree_cons (NULL_TREE
, creq
, result
);
668 // If constraint specifiers are present, make them part of the
669 // list of constraints.
672 TREE_CHAIN (tree_last (specs
)) = result
;
676 // Finally, construct the syntactic requirement.
677 return build_check_expr (EXPR_REQ
, nreverse (result
));
680 // Finish a simple syntax requirement, returning a node representing
681 // a check that EXPR is a valid expression.
683 finish_expr_requirement (tree expr
)
685 gcc_assert (processing_template_decl
);
686 tree req
= finish_validexpr_expr (expr
);
687 tree reqs
= tree_cons (NULL_TREE
, req
, NULL_TREE
);
688 return build_check_expr (EXPR_REQ
, reqs
);
691 // Finish a type requirement, returning a node representing a check
692 // that TYPE will result in a valid type when instantiated.
694 finish_type_requirement (tree type
)
696 gcc_assert (processing_template_decl
);
697 tree req
= finish_validtype_expr (type
);
698 return build_check_expr (TYPE_REQ
, req
);
702 finish_nested_requirement (tree expr
)
704 gcc_assert (processing_template_decl
);
705 return build_check_expr (NESTED_REQ
, expr
);
708 // Finish a constexpr requirement, returning a node representing a
709 // check that EXPR, when instantiated, may be evaluated at compile time.
711 finish_constexpr_requirement (tree expr
)
713 gcc_assert (processing_template_decl
);
714 return finish_constexpr_expr (expr
);
717 // Finish the noexcept requirement by constructing a noexcept
718 // expression evaluating EXPR.
720 finish_noexcept_requirement (tree expr
)
722 gcc_assert (processing_template_decl
);
723 return finish_noexcept_expr (expr
, tf_none
);
726 // Returns the true or false node depending on the truth value of B.
730 return b
? boolean_true_node
: boolean_false_node
;
733 // Returns a finished validexpr-expr. Returns the true or false node
734 // depending on whether EXPR denotes a valid expression. This is the case
735 // when the expression has been successfully type checked.
737 // When processing a template declaration, the result is an expression
738 // representing the check.
740 finish_validexpr_expr (tree expr
)
742 if (processing_template_decl
)
743 return build_check_expr (VALIDEXPR_EXPR
, expr
);
744 return truth_node (expr
&& expr
!= error_mark_node
);
747 // Returns a finished validtype-expr. Returns the true or false node
748 // depending on whether T denotes a valid type name.
750 // When processing a template declaration, the result is an expression
751 // representing the check.
753 // FIXME: Semantics need to be aligned with the new version of the
754 // specificaiton (i.e., we must be able to invent a function and
755 // perform argument deduction against it).
757 finish_validtype_expr (tree type
)
761 sorry ("%<auto%< not supported in result type constraints\n");
762 return error_mark_node
;
765 if (processing_template_decl
)
766 return build_check_expr (VALIDTYPE_EXPR
, type
);
767 return truth_node (type
&& TYPE_P (type
));
770 // Returns a finished constexpr-expr. Returns the true or false node
771 // depending on whether the expression T may be evaluated at compile
774 // When processing a template declaration, the result is an expression
775 // representing the check.
777 finish_constexpr_expr (tree expr
)
779 if (processing_template_decl
)
780 return build_check_expr (CONSTEXPR_EXPR
, expr
);
782 // TODO: Actually check that the expression can be constexpr
785 // return truth_node (potential_constant_expression (expr));
786 sorry ("constexpr requirement");
790 // Check that a constrained friend declaration function declaration,
791 // FN, is admissable. This is the case only when the declaration depends
792 // on template parameters and does not declare a specialization.
794 check_constrained_friend (tree fn
, tree reqs
)
796 if (fn
== error_mark_node
)
798 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
800 // If there are not constraints, this cannot be an error.
804 // Constrained friend functions that don't depend on template
805 // arguments are effectively meaningless.
806 tree parms
= DECL_ARGUMENTS (fn
);
807 tree result
= TREE_TYPE (TREE_TYPE (fn
));
808 if (!(parms
&& uses_template_parms (parms
)) && !uses_template_parms (result
))
810 error ("constrained friend does not depend on template parameters");
815 // Given an overload set, OVL, and a template argument or placeholder, ARG,
816 // synthesize a call expression that resolves to a concept check of
817 // the expression the form OVL<ARG>().
819 // TODO: Extend this to take a variable concept also.
821 build_concept_check (tree ovl
, tree arg
)
823 gcc_assert (TREE_CODE (ovl
) == OVERLOAD
);
825 // Build a template-id that acts as the call target using OVL as
826 // the template and ARG as the only explicit argument.
827 tree targs
= make_tree_vec (1);
828 TREE_VEC_ELT (targs
, 0) = arg
;
829 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, 1);
830 tree id
= lookup_template_function (ovl
, targs
);
832 // Build a new call expression, but don't actually generate a new
833 // function call. We just want the tree, not the semantics.
834 ++processing_template_decl
;
835 vec
<tree
, va_gc
> *fargs
= make_tree_vector();
836 tree call
= finish_call_expr (id
, &fargs
, false, false, tf_none
);
837 --processing_template_decl
;
842 // Returns a TYPE_DECL that contains sufficient information to build
843 // a template parameter of the same kind as PROTO and constrained
844 // by the concept declaration FN. PROTO is saved as the initializer of
845 // the new type decl, and the constraining function is saved in
848 build_constrained_parameter (tree proto
, tree fn
)
850 tree name
= DECL_NAME (fn
);
851 tree type
= TREE_TYPE (proto
);
852 tree decl
= build_decl (input_location
, TYPE_DECL
, name
, type
);
853 DECL_INITIAL (decl
) = proto
; // Describing parameter
854 DECL_SIZE_UNIT (decl
) = fn
; // Constraining function declaration
858 // Create a requirement expression for the given DECL that evaluates the
859 // requirements specified by CONSTR, a TYPE_DECL that contains all the
860 // information necessary to build the requirements (see finish_concept_name
861 // for the layout of that TYPE_DECL).
863 // Note that the constraints are neither reduced nor decomposed. That is
864 // done only after the requires clause has been parsed (or not).
866 finish_shorthand_requirement (tree decl
, tree constr
)
868 // No requirements means no constraints.
872 tree proto
= DECL_INITIAL (constr
); // The prototype declaration
873 tree con
= DECL_SIZE_UNIT (constr
); // The concept declaration
875 // If the parameter declaration is variadic, but the concept is not
876 // then we need to apply the concept to every element in the pack.
877 bool is_proto_pack
= template_parameter_pack_p (proto
);
878 bool is_decl_pack
= template_parameter_pack_p (decl
);
879 bool apply_to_all_p
= is_decl_pack
&& !is_proto_pack
;
881 // Get the argument and overload used for the requirement. Adjust
882 // if we're going to expand later.
883 tree arg
= template_parm_to_arg (build_tree_list (NULL_TREE
, decl
));
885 arg
= PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg
), 0));
887 // Build the concept check. If it the constraint needs to be applied
888 // to all elements of the parameter pack, then expand make the constraint
890 tree ovl
= build_overload (DECL_TI_TEMPLATE (con
), NULL_TREE
);
891 tree check
= build_concept_check (ovl
, arg
);
894 check
= make_pack_expansion (check
);
896 // Set the type to indicate that this expansion will get special
897 // treatment during instantiation.
899 // TODO: Maybe this should be a different kind of node... one that
900 // has all the same properties as a pack expansion, but has a definite
901 // expansion when instantiated as part of an expression.
903 // As of now, this is a hack.
904 TREE_TYPE (check
) = boolean_type_node
;
910 // -------------------------------------------------------------------------- //
911 // Substitution Rules
913 // The following functions implement substitution rules for constraints.
916 // In an unevaluated context, the substitution of parm decls are not
917 // properly chained during substitution. Do that here.
919 fix_local_parms (tree sparms
)
924 tree p
= TREE_CHAIN (sparms
);
926 while (p
&& TREE_VALUE (p
) != void_type_node
)
928 DECL_CHAIN (TREE_VALUE (q
)) = TREE_VALUE (p
);
935 // Register local specializations for each of tparm and the corresponding
936 // sparm. This is a helper function for tsubst_requires_expr.
938 declare_local_parms (tree tparms
, tree sparms
)
940 tree s
= TREE_VALUE (sparms
);
941 for (tree p
= tparms
; p
&& !VOID_TYPE_P (TREE_VALUE (p
)); p
= TREE_CHAIN (p
))
943 tree t
= TREE_VALUE (p
);
946 tree pack
= extract_fnparm_pack (t
, &s
);
947 register_local_specialization (pack
, t
);
951 register_local_specialization (s
, t
);
957 // Substitute ARGS into the parameter list T, producing a sequence of
958 // local parameters (variables) in the current scope.
960 tsubst_local_parms (tree t
,
962 tsubst_flags_t complain
,
965 tree r
= fix_local_parms (tsubst (t
, args
, complain
, in_decl
));
966 if (r
== error_mark_node
)
967 return error_mark_node
;
969 // Register the instantiated args as local parameters.
971 declare_local_parms (t
, r
);
976 // Substitute ARGS into the requirement body (list of requirements), T.
978 tsubst_requirement_body (tree t
, tree args
, tree in_decl
)
980 cp_unevaluated guard
;
984 // If any substitutions fail, then this is equivalent to
986 tree e
= tsubst_expr (TREE_VALUE (t
), args
, tf_none
, in_decl
, false);
987 if (e
== error_mark_node
)
988 e
= boolean_false_node
;
989 r
= tree_cons (NULL_TREE
, e
, r
);
996 // Substitute ARGS into the requires expression T.
998 tsubst_requires_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
1000 local_specialization_stack stack
;
1001 tree p
= tsubst_local_parms (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
1002 tree r
= tsubst_requirement_body (TREE_OPERAND (t
, 1), args
, in_decl
);
1003 return finish_requires_expr (p
, r
);
1006 // Substitute ARGS into the valid-expr expression T.
1008 tsubst_validexpr_expr (tree t
, tree args
, tree in_decl
)
1010 tree r
= tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1011 return finish_validexpr_expr (r
);
1014 // Substitute ARGS into the valid-type expression T.
1016 tsubst_validtype_expr (tree t
, tree args
, tree in_decl
)
1018 tree r
= tsubst (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
);
1019 return finish_validtype_expr (r
);
1022 // Substitute ARGS into the constexpr expression T.
1024 tsubst_constexpr_expr (tree t
, tree args
, tree in_decl
)
1026 tree r
= tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1027 return finish_constexpr_expr (r
);
1030 // Substitute ARGS into the expr requirement T. Note that a requirement
1031 // node is instantiated from a non-reduced context (e.g., static_assert).
1033 tsubst_expr_req (tree t
, tree args
, tree in_decl
)
1036 for (tree l
= TREE_OPERAND (t
, 0); l
; l
= TREE_CHAIN (l
))
1038 tree e
= tsubst_expr (TREE_VALUE (l
), args
, tf_none
, in_decl
, false);
1039 r
= conjoin_requirements (r
, e
);
1044 // Substitute ARGS into the type requirement T. Note that a requirement
1045 // node is instantiated from a non-reduced context (e.g., static_assert).
1047 tsubst_type_req (tree t
, tree args
, tree in_decl
)
1049 return tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1052 // Substitute ARGS into the nested requirement T. Note that a requirement
1053 // node is instantiated from a non-reduced context (e.g., static_assert).
1055 tsubst_nested_req (tree t
, tree args
, tree in_decl
)
1057 return tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1060 // Substitute the template arguments ARGS into the requirement
1061 // expression REQS. Errors resulting from substitution are not
1064 instantiate_requirements (tree reqs
, tree args
)
1066 return tsubst_expr (reqs
, args
, tf_none
, NULL_TREE
, false);
1069 // -------------------------------------------------------------------------- //
1070 // Constraint Satisfaction
1072 // The following functions are responsible for the instantiation and
1073 // evaluation of constraints.
1076 // Returns true if the requirements expression REQS is satisfied
1077 // and false otherwise. The requirements are checked by simply
1078 // evaluating REQS as a constant expression.
1080 check_requirements (tree reqs
)
1082 // Reduce any remaining TRAIT_EXPR nodes before evaluating.
1083 reqs
= fold_non_dependent_expr (reqs
);
1085 // Requirements are satisfied when REQS evaluates to true.
1086 return cxx_constant_value (reqs
) == boolean_true_node
;
1089 // Returns true if the requirements expression REQS is satisfied
1090 // and false otherwise. The requirements are checked by first
1091 // instantiating REQS and then evaluating it as a constant expression.
1093 check_requirements (tree reqs
, tree args
)
1095 // If any arguments are dependent, then we can't check the
1096 // requirements. Just return true.
1097 if (uses_template_parms (args
))
1100 // Instantiate and evaluate the requirements.
1101 reqs
= instantiate_requirements (reqs
, args
);
1102 if (reqs
== error_mark_node
)
1104 return check_requirements (reqs
);
1108 // Check the instantiated declaration constraints.
1110 check_constraints (tree cinfo
)
1112 // No constraints? Satisfied.
1115 return check_requirements (CI_REQUIREMENTS (cinfo
));
1118 // Check the constraints in CINFO against the given ARGS, returning
1119 // true when the constraints are satisfied and false otherwise.
1121 check_constraints (tree cinfo
, tree args
)
1123 // No constraints? Satisfied.
1127 // Dependent arguments? Satisfied. They won't reduce to true or false.
1128 if (uses_template_parms (args
))
1131 return check_requirements (CI_REQUIREMENTS (cinfo
), args
);
1134 // Check the constraints of the declaration or type T, against
1135 // the specified arguments. Returns true if the constraints are
1136 // satisfied and false otherwise.
1138 check_template_constraints (tree t
, tree args
)
1140 return check_constraints (DECL_CONSTRAINTS (t
), args
);
1143 // -------------------------------------------------------------------------- //
1144 // Constraint Relations
1146 // Interfaces for determining equivalency and ordering of constraints.
1148 // Returns true when A and B are equivalent constraints.
1150 equivalent_constraints (tree a
, tree b
)
1155 return subsumes (a
, b
) && subsumes (b
, a
);
1158 // Returns true if the template declarations A and B have equivalent
1159 // constraints. This is the case when A's constraints subsume B's and
1160 // when B's also constrain A's.
1162 equivalently_constrained (tree a
, tree b
)
1164 gcc_assert (TREE_CODE (a
) == TREE_CODE (b
));
1165 return equivalent_constraints (DECL_CONSTRAINTS (a
), DECL_CONSTRAINTS (b
));
1168 // Returns true when the A contains more atomic properties than B.
1170 more_constraints (tree a
, tree b
)
1172 return subsumes (a
, b
);
1175 // Returns true when the template declaration A's constraints subsume
1176 // those of the template declaration B.
1178 more_constrained (tree a
, tree b
)
1180 gcc_assert (TREE_CODE (a
) == TREE_CODE (b
));
1181 return more_constraints (DECL_CONSTRAINTS (a
), DECL_CONSTRAINTS (b
));
1185 // -------------------------------------------------------------------------- //
1186 // Constraint Diagnostics
1190 void diagnose_node (location_t
, tree
, tree
);
1192 // Diagnose a constraint failure for type trait expressions.
1194 diagnose_trait (location_t loc
, tree t
, tree args
)
1196 if (check_requirements (t
, args
))
1199 ++processing_template_decl
;
1200 tree subst
= instantiate_requirements (t
, args
);
1201 --processing_template_decl
;
1203 if (subst
== error_mark_node
)
1205 inform (input_location
, " substitution failure in %qE", t
);
1209 tree t1
= TRAIT_EXPR_TYPE1 (subst
);
1210 tree t2
= TRAIT_EXPR_TYPE2 (subst
);
1211 switch (TRAIT_EXPR_KIND (t
))
1213 case CPTK_HAS_NOTHROW_ASSIGN
:
1214 inform (loc
, " %qT is not nothrow assignable", t1
);
1216 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
1217 inform (loc
, " %qT is not nothrow constructible", t1
);
1219 case CPTK_HAS_NOTHROW_COPY
:
1220 inform (loc
, " %qT is not nothrow copyable", t1
);
1222 case CPTK_HAS_TRIVIAL_ASSIGN
:
1223 inform (loc
, " %qT is not trivially assignable", t1
);
1225 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
1226 inform (loc
, " %qT is not trivially constructible", t1
);
1228 case CPTK_HAS_TRIVIAL_COPY
:
1229 inform (loc
, " %qT is not trivially copyable", t1
);
1231 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
1232 inform (loc
, " %qT is not trivially destructible", t1
);
1234 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
1235 inform (loc
, " %qT does not have a virtual destructor", t1
);
1237 case CPTK_IS_ABSTRACT
:
1238 inform (loc
, " %qT is not an abstract class", t1
);
1240 case CPTK_IS_BASE_OF
:
1241 inform (loc
, " %qT is not a base of %qT", t1
, t2
);
1244 inform (loc
, " %qT is not a class", t1
);
1246 case CPTK_IS_CONVERTIBLE_TO
:
1247 inform (loc
, " %qT is not convertible to %qT", t1
, t2
);
1250 inform (loc
, " %qT is not an empty class", t1
);
1253 inform (loc
, " %qT is not an enum", t1
);
1256 inform (loc
, " %qT is not a final class", t1
);
1258 case CPTK_IS_LITERAL_TYPE
:
1259 inform (loc
, " %qT is not a literal type", t1
);
1262 inform (loc
, " %qT is not a POD type", t1
);
1264 case CPTK_IS_POLYMORPHIC
:
1265 inform (loc
, " %qT is not a polymorphic type", t1
);
1267 case CPTK_IS_SAME_AS
:
1268 inform (loc
, " %qT is not the same as %qT", t1
, t2
);
1270 case CPTK_IS_STD_LAYOUT
:
1271 inform (loc
, " %qT is not an standard layout type", t1
);
1273 case CPTK_IS_TRIVIAL
:
1274 inform (loc
, " %qT is not a trivial type", t1
);
1277 inform (loc
, " %qT is not a union", t1
);
1284 // Diagnose a failed concept check in concept indicated by T, where
1285 // T is the result of resolve_constraint_check. Recursively analyze
1286 // the nested requiremets for details.
1288 diagnose_check (location_t loc
, tree t
, tree args
)
1290 tree fn
= TREE_VALUE (t
);
1291 tree targs
= TREE_PURPOSE (t
);
1292 tree body
= DECL_SAVED_TREE (fn
);
1296 inform (loc
, " failure in constraint %q#D", DECL_TI_TEMPLATE (fn
));
1298 // Perform a mini-reduction on the constraint.
1299 if (TREE_CODE (body
) == BIND_EXPR
)
1300 body
= BIND_EXPR_BODY (body
);
1301 if (TREE_CODE (body
) == RETURN_EXPR
)
1302 body
= TREE_OPERAND (body
, 0);
1304 // Locally instantiate the body with the call's template args,
1305 // and recursively diagnose.
1306 ++processing_template_decl
;
1307 body
= instantiate_requirements (body
, targs
);
1308 --processing_template_decl
;
1310 diagnose_node (loc
, body
, args
);
1313 // Diagnose constraint failures from the call expression T.
1315 diagnose_call (location_t loc
, tree t
, tree args
)
1317 if (check_requirements (t
, args
))
1320 // If this is a concept, we're going to recurse.
1321 // If it's just a call, then we can emit a simple message.
1322 if (tree check
= resolve_constraint_check (t
))
1323 diagnose_check (loc
, check
, args
);
1325 inform (loc
, " %qE evaluated to false", t
);
1328 // Diagnose specific constraint failures.
1330 diagnose_requires (location_t loc
, tree t
, tree args
)
1332 if (check_requirements (t
, args
))
1335 ++processing_template_decl
;
1336 tree subst
= instantiate_requirements (t
, args
);
1337 --processing_template_decl
;
1339 // Print the header for the requires expression.
1340 tree parms
= TREE_OPERAND (subst
, 0);
1341 if (!VOID_TYPE_P (TREE_VALUE (parms
)))
1342 inform (loc
, " requiring syntax with values %Z", TREE_OPERAND (subst
, 0));
1344 // Create a new local specialization binding for the arguments.
1345 // This lets us instantiate sub-expressions separately from the
1347 local_specialization_stack locals
;
1348 declare_local_parms (TREE_OPERAND (t
, 0), TREE_OPERAND (subst
, 0));
1350 // Iterate over the sub-requirements and try instantiating each.
1351 for (tree l
= TREE_OPERAND (t
, 1); l
; l
= TREE_CHAIN (l
))
1352 diagnose_node (loc
, TREE_VALUE (l
), args
);
1356 diagnose_validexpr (location_t loc
, tree t
, tree args
)
1358 if (check_requirements (t
, args
))
1360 inform (loc
, " %qE is not a valid expression", TREE_OPERAND (t
, 0));
1364 diagnose_validtype (location_t loc
, tree t
, tree args
)
1366 if (check_requirements (t
, args
))
1369 // Substitute into the qualified name.
1370 tree name
= TREE_OPERAND (t
, 0);
1371 if (tree cxt
= TYPE_CONTEXT (name
))
1373 tree id
= TYPE_IDENTIFIER (name
);
1374 cxt
= tsubst (cxt
, args
, tf_none
, NULL_TREE
);
1375 name
= build_qualified_name (NULL_TREE
, cxt
, id
, false);
1376 inform (loc
, " %qE does not name a valid type", name
);
1380 inform (loc
, " %qT does not name a valid type", name
);
1385 diagnose_constexpr (location_t loc
, tree t
, tree args
)
1387 if (check_requirements (t
, args
))
1389 inform (loc
, " %qE is not a constant expression", TREE_OPERAND (t
, 0));
1393 diagnose_noexcept (location_t loc
, tree t
, tree args
)
1395 if (check_requirements (t
, args
))
1397 inform (loc
, " %qE propagates exceptions", TREE_OPERAND (t
, 0));
1400 // Diagnose a constraint failure in the expression T.
1402 diagnose_other (location_t loc
, tree t
, tree args
)
1404 if (check_requirements (t
, args
))
1406 inform (loc
, " %qE evaluated to false", t
);
1409 // Diagnose a constraint failure in the subtree T.
1411 diagnose_node (location_t loc
, tree t
, tree args
)
1413 switch (TREE_CODE (t
))
1415 case TRUTH_ANDIF_EXPR
:
1416 diagnose_node (loc
, TREE_OPERAND (t
, 0), args
);
1417 diagnose_node (loc
, TREE_OPERAND (t
, 1), args
);
1420 case TRUTH_ORIF_EXPR
:
1421 // TODO: Design better diagnostics for dijunctions.
1422 diagnose_other (loc
, t
, args
);
1426 diagnose_trait (loc
, t
, args
);
1430 diagnose_call (loc
, t
, args
);
1434 diagnose_requires (loc
, t
, args
);
1437 case VALIDEXPR_EXPR
:
1438 diagnose_validexpr (loc
, t
, args
);
1441 case VALIDTYPE_EXPR
:
1442 diagnose_validtype (loc
, t
, args
);
1445 case CONSTEXPR_EXPR
:
1446 diagnose_constexpr (loc
, t
, args
);
1450 diagnose_noexcept (loc
, t
, args
);
1454 diagnose_other (loc
, t
, args
);
1459 // Diagnose a constraint failure in the requirements expression REQS.
1461 diagnose_requirements (location_t loc
, tree reqs
, tree args
)
1463 diagnose_node (loc
, reqs
, args
);
1466 // Create a tree node representing the substitution of ARGS into
1467 // the parameters of TMPL. The resulting structure is passed as an
1468 // for diagnosing substitutions.
1470 make_subst (tree tmpl
, tree args
)
1472 tree subst
= tree_cons (NULL_TREE
, args
, NULL_TREE
);
1473 TREE_TYPE (subst
) = DECL_TEMPLATE_PARMS (tmpl
);
1479 // Emit diagnostics detailing the failure ARGS to satisfy the constraints
1480 // of the template declaration, TMPL.
1482 diagnose_constraints (location_t loc
, tree tmpl
, tree args
)
1484 inform (loc
, " constraints not satisfied %S", make_subst (tmpl
, args
));
1486 // Diagnose the constraints by recursively decomposing and
1487 // evaluating the template requirements.
1488 tree reqs
= CI_SPELLING (DECL_CONSTRAINTS (tmpl
));
1489 diagnose_requirements (loc
, reqs
, args
);