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"
27 #include "c-family/c-common.h"
28 #include "c-family/c-objc.h"
29 #include "tree-inline.h"
34 #include "diagnostic.h"
36 #include "tree-iterator.h"
42 // -------------------------------------------------------------------------- //
43 // Requirement Construction
45 // Facilities for building and manipulating template requirements.
47 // TODO: Simply assigning boolean_type_node to the result type of the
48 // expression seems right for constraints, but in the long-term we might want
49 // to be more flexible (i.e., allow some form of overload resolution?).
51 // Create a new logical node joining the subexpressions a and b.
53 join_requirements (tree_code c
, tree a
, tree b
)
55 gcc_assert (a
!= NULL_TREE
&& b
!= NULL_TREE
);
56 gcc_assert (c
== TRUTH_ANDIF_EXPR
|| c
== TRUTH_ORIF_EXPR
);
57 return build_min (c
, boolean_type_node
, a
, b
);
60 // Returns the conjunction of two requirements A and B, where A and B are
61 // reduced terms in the constraints language. Note that conjoining a non-null
62 // expression with NULL_TREE is an identity operation. That is, for some
65 // conjoin_requirements(a, NULL_TREE) == a
67 // If both A and B are NULL_TREE, the result is also NULL_TREE.
69 conjoin_requirements (tree a
, tree b
)
72 return b
? join_requirements (TRUTH_ANDIF_EXPR
, a
, b
) : a
;
79 // Transform the list of expressions in the T into a conjunction
80 // of requirements. T must be a TREE_VEC.
82 conjoin_requirements (tree t
)
84 gcc_assert (TREE_CODE (t
) == TREE_VEC
);
86 for (int i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
87 r
= conjoin_requirements (r
, TREE_VEC_ELT (t
, i
));
92 // -------------------------------------------------------------------------- //
93 // Constraint Resolution
95 // This facility is used to resolve constraint checks from requirement
96 // expressions. A constraint check is a call to a function template, declared
99 // The result of resolution is a pair (a list node) whose value is the
100 // matched declaration, and whose purpose contains the coerced template
101 // arguments that can be substituted into the call.
103 // Given an overload set, try to find a unique definition that can be
104 // instantiated by the template arguments.
106 // This function is not called for arbitrary call expressions. In particular,
107 // the call expression must be written with explicit template arguments
108 // and no function arguments. For example:
112 // The overload set will contain only template declarations.
114 // If a single definition is found, this returns a list node whose VALUE
115 // is the constraint function (not the template), and its PURPOSE is
116 // the complete set of arguments substituted into the parameter list.
118 resolve_constraint_check (tree ovl
, tree args
)
120 tree cands
= NULL_TREE
;
121 for (tree p
= ovl
; p
!= NULL_TREE
; p
= OVL_NEXT (p
))
123 // Get the next template overload.
124 tree tmpl
= OVL_CURRENT (p
);
125 if (TREE_CODE (tmpl
) != TEMPLATE_DECL
)
128 // Don't try to deduce checks for non-concept-like. We often
129 // end up trying to resolve constraints in functional casts
130 // as part of a post-fix expression. We can save time and
131 // headaches by not instantiating those declarations.
133 // NOTE: This masks a potential error, caused by instantiating
134 // non-deduced contexts using placeholder arguments.
135 tree fn
= DECL_TEMPLATE_RESULT (tmpl
);
136 if (DECL_ARGUMENTS (fn
))
138 if (!DECL_DECLARED_CONCEPT_P (fn
))
141 // Remember the candidate if we can deduce a substitution.
142 ++processing_template_decl
;
143 tree parms
= TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl
));
144 if (tree subst
= coerce_template_parms (parms
, args
, tmpl
))
145 if (subst
!= error_mark_node
)
146 cands
= tree_cons (subst
, fn
, cands
);
147 --processing_template_decl
;
150 // If we didn't find a unique candidate, then this is
151 // not a constraint check.
152 if (!cands
|| TREE_CHAIN (cands
))
155 // Constraints must be declared concepts.
156 tree decl
= TREE_VALUE (cands
);
157 if (!DECL_DECLARED_CONCEPT_P (decl
))
160 // Concept declarations must have a corresponding definition.
162 // TODO: This should be part of the up-front checking for
163 // a concept declaration.
164 if (!DECL_SAVED_TREE (decl
))
166 error_at (DECL_SOURCE_LOCATION (decl
),
167 "concept %q#D has no definition", decl
);
174 // Determine if the the call expression CALL is a constraint check, and
175 // return the concept declaration and arguments being checked. If CALL
176 // does not denote a constraint check, return NULL.
178 resolve_constraint_check (tree call
)
180 gcc_assert (TREE_CODE (call
) == CALL_EXPR
);
182 // A constraint check must be only be a template-id expression.
183 tree target
= CALL_EXPR_FN (call
);
184 if (TREE_CODE (target
) != TEMPLATE_ID_EXPR
)
187 // Get the overload set and template arguments and try to
188 // resolve the target.
189 tree ovl
= TREE_OPERAND (target
, 0);
190 tree args
= TREE_OPERAND (target
, 1);
191 return resolve_constraint_check (ovl
, args
);
195 // -------------------------------------------------------------------------- //
196 // Requirement Reduction
198 // Reduces a template requirement to a logical formula written in terms of
199 // atomic propositions, returing the new expression. If the expression cannot
200 // be reduced, a NULL_TREE is returned, indicating failure to reduce the
201 // original requirment.
206 static tree
reduce_node (tree
);
207 static tree
reduce_expr (tree
);
208 static tree
reduce_stmt (tree
);
209 static tree
reduce_decl (tree
);
210 static tree
reduce_misc (tree
);
212 static tree
reduce_logical (tree
);
213 static tree
reduce_call (tree
);
214 static tree
reduce_requires (tree
);
215 static tree
reduce_expr_req (tree
);
216 static tree
reduce_type_req (tree
);
217 static tree
reduce_nested_req (tree
);
218 static tree
reduce_template_id (tree
);
219 static tree
reduce_stmt_list (tree
);
221 // Reduce the requirement T into a logical formula written in terms of
222 // atomic propositions.
226 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
232 return reduce_expr (t
);
235 return reduce_stmt (t
);
237 case tcc_declaration
:
238 return reduce_decl (t
);
240 case tcc_exceptional
:
241 return reduce_misc (t
);
243 // These kinds of expressions are atomic.
255 // Reduction rules for the expression node T.
259 switch (TREE_CODE (t
))
261 case TRUTH_ANDIF_EXPR
:
262 case TRUTH_ORIF_EXPR
:
263 return reduce_logical (t
);
266 return reduce_call (t
);
269 return reduce_requires (t
);
272 return reduce_expr_req (t
);
275 return reduce_type_req (t
);
278 return reduce_nested_req (t
);
280 case TEMPLATE_ID_EXPR
:
281 return reduce_template_id (t
);
284 return reduce_node (TREE_VALUE (TREE_OPERAND (t
, 0)));
287 return reduce_node (BIND_EXPR_BODY (t
));
293 // Everything else is atomic.
300 // Reduction rules for the statement T.
304 switch (TREE_CODE (t
))
306 // Reduce the returned expression.
308 return reduce_node (TREE_OPERAND (t
, 0));
310 // These statements do not introduce propositions
311 // in the constraints language. Do not recurse.
322 // Reduction rules for the declaration T.
326 switch (TREE_CODE (t
))
328 // References to var decls are atomic.
338 // Reduction rules for the node T.
342 switch (TREE_CODE (t
))
344 // Errors and traits are atomic.
350 return reduce_stmt_list (t
);
358 // Reduction rules for the binary logical expression T (&& and ||).
360 // Generate a new expression from the reduced operands. If either operand
361 // cannot be reduced, then the resulting expression is null.
363 reduce_logical (tree t
)
365 tree l
= reduce_expr (TREE_OPERAND (t
, 0));
366 tree r
= reduce_expr (TREE_OPERAND (t
, 1));
370 TREE_OPERAND (t
, 0) = l
;
371 TREE_OPERAND (t
, 1) = r
;
378 // Reduction rules for the call expression T.
380 // If T is a call to a constraint instantiate its definition and
381 // recursively reduce its returned expression.
385 // Is the function call actually a constraint check?
386 tree check
= resolve_constraint_check (t
);
390 tree fn
= TREE_VALUE (check
);
391 tree args
= TREE_PURPOSE (check
);
393 // Reduce the body of the function into the constriants language.
394 tree body
= reduce_requirements (DECL_SAVED_TREE (fn
));
397 error ("could not inline requirements from %qD", fn
);
398 return error_mark_node
;
401 // Instantiate the reduced results using the deduced args.
402 tree result
= instantiate_requirements (body
, args
);
403 if (result
== error_mark_node
)
405 error ("could not instantiate requirements from %qD", fn
);
406 return error_mark_node
;
411 // Reduction rules for the template-id T.
413 // It turns out that we often get requirements being written like this:
415 // template<typename T>
419 // Where Foo<T> should actually be written as Foo<T>(). Generate an
420 // error and suggest the improved writing.
422 reduce_template_id (tree t
)
424 vec
<tree
, va_gc
>* args
= NULL
;
425 tree c
= finish_call_expr (t
, &args
, true, false, 0);
426 error_at (EXPR_LOCATION (t
), "invalid requirement");
427 inform (EXPR_LOCATION (t
), "did you mean %qE", c
);
432 // Reduce an expression requirement as a conjunction of its
433 // individual constraints.
435 reduce_expr_req (tree t
)
438 for (tree l
= TREE_OPERAND (t
, 0); l
; l
= TREE_CHAIN (l
))
439 r
= conjoin_requirements (r
, reduce_expr (TREE_VALUE (l
)));
443 // Reduce a type requirement by returing its underlying
446 reduce_type_req (tree t
)
448 return TREE_OPERAND (t
, 0);
451 // Reduce a nested requireemnt by returing its only operand.
453 reduce_nested_req (tree t
)
455 return TREE_OPERAND (t
, 0);
458 // Reduce a requires expr by reducing each requirement in turn,
459 // rewriting the list of requirements so that we end up with a
460 // list of expressions, some of which may be conjunctions.
462 reduce_requires (tree t
)
464 for (tree l
= TREE_OPERAND (t
, 1); l
; l
= TREE_CHAIN (l
))
465 TREE_VALUE (l
) = reduce_expr (TREE_VALUE (l
));
469 // Reduction rules for the statement list STMTS.
471 // Recursively reduce each statement in the list, concatenating each
472 // reduced result into a conjunction of requirements.
474 // A constexpr function may include statements other than a return
475 // statement. The primary purpose of these rules is to filter those
476 // non-return statements from the constraints language.
478 reduce_stmt_list (tree stmts
)
480 tree lhs
= NULL_TREE
;
481 tree_stmt_iterator i
= tsi_start (stmts
);
482 while (!tsi_end_p (i
))
484 if (tree rhs
= reduce_node (tsi_stmt (i
)))
485 lhs
= conjoin_requirements (lhs
, rhs
);
493 // Reduce the requirement REQS into a logical formula written in terms of
494 // atomic propositions.
496 reduce_requirements (tree reqs
)
498 return reduce_node (reqs
);
501 // -------------------------------------------------------------------------- //
502 // Constraint Semantic Processing
504 // The following functions are called by the parser and substitution rules
505 // to create and evaluate constraint-related nodes.
507 // Create a constraint-info node from the specified requirements.
509 make_constraints (tree reqs
)
511 // No requirements == no constraints
515 // Reduce the requirements into a single expression of constraints.
516 tree expr
= reduce_requirements (reqs
);
517 if (expr
== error_mark_node
)
518 return error_mark_node
;
520 // Decompose those expressions into lists of lists of atomic
522 tree assume
= decompose_assumptions (expr
);
524 // Build the constraint info.
525 tree_constraint_info
*cinfo
=
526 (tree_constraint_info
*)make_node (CONSTRAINT_INFO
);
527 cinfo
->spelling
= reqs
;
528 cinfo
->requirements
= expr
;
529 cinfo
->assumptions
= assume
;
533 // Returns the template constraints of declaration T. If T is not a
534 // template, this return NULL_TREE. Note that T must be non-null.
536 get_constraints (tree t
)
538 gcc_assert (DECL_P (t
));
539 if (TREE_CODE (t
) != TEMPLATE_DECL
)
541 if (!DECL_TEMPLATE_INFO (t
))
544 return DECL_CONSTRAINTS (DECL_TI_TEMPLATE (t
));
546 return DECL_CONSTRAINTS (t
);
549 // Returns a conjunction of shorthand requirements for the template
550 // parameter list PARMS. Note that the requirements are stored in
551 // the TYPE of each tree node.
553 get_shorthand_requirements (tree parms
)
555 tree reqs
= NULL_TREE
;
556 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
557 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
559 tree parm
= TREE_VEC_ELT (parms
, i
);
560 reqs
= conjoin_requirements(reqs
, TREE_TYPE (parm
));
565 // Finish the template requirement, EXPR, by translating it into
566 // a constraint information record.
568 finish_template_requirements (tree expr
)
570 if (expr
== error_mark_node
)
573 return make_constraints (expr
);
577 build_requires_expr (tree parms
, tree reqs
)
579 // Modify the declared parameters by removing their context (so they
580 // don't refer to the enclosing scope), and marking them constant (so
581 // we can actually check constexpr properties).
582 for (tree p
= parms
; p
&& !VOID_TYPE_P (TREE_VALUE (p
)); p
= TREE_CHAIN (p
))
584 tree parm
= TREE_VALUE (p
);
585 DECL_CONTEXT (parm
) = NULL_TREE
;
586 TREE_CONSTANT (parm
) = true;
590 tree r
= build_min (REQUIRES_EXPR
, boolean_type_node
, parms
, reqs
);
591 TREE_SIDE_EFFECTS (r
) = false;
592 TREE_CONSTANT (r
) = true;
596 // Evaluate an instantiatd requires expr, returning the truth node
597 // only when all sub-requirements have evaluated to true.
599 eval_requires_expr (tree reqs
)
601 for (tree t
= reqs
; t
; t
= TREE_CHAIN (t
)) {
602 tree r
= TREE_VALUE (t
);
603 r
= fold_non_dependent_expr (r
);
604 r
= maybe_constant_value (r
);
605 if (r
!= boolean_true_node
)
606 return boolean_false_node
;
608 return boolean_true_node
;
611 // Finish a requires expression, returning a node wrapping the parameters,
612 // PARMS, and the list of requirements REQS.
614 finish_requires_expr (tree parms
, tree reqs
)
616 if (processing_template_decl
)
617 return build_requires_expr (parms
, reqs
);
619 return eval_requires_expr (reqs
);
622 // Construct a unary expression that evaluates properties of the
623 // expression or type T, and has a boolean result type.
625 build_check_expr (tree_code c
, tree t
)
627 tree r
= build_min (c
, boolean_type_node
, t
);
628 TREE_SIDE_EFFECTS (r
) = false;
629 TREE_READONLY (r
) = true;
630 TREE_CONSTANT (r
) = true;
634 // Finish a syntax requirement, constructing a list embodying a sequence
635 // of checks for the validity of EXPR and TYPE, the convertibility of
636 // EXPR to TYPE, and the expression properties specified in SPECS.
638 finish_expr_requirement (tree expr
, tree type
, tree specs
)
640 gcc_assert (processing_template_decl
);
642 // Build a list of checks, starting with the valid expression.
643 tree result
= tree_cons (NULL_TREE
, finish_validexpr_expr (expr
), NULL_TREE
);
645 // If a type requirement was provided, build the result type checks.
648 // If the type is dependent, ensure that it can be validly
651 // NOTE: We can also disregard checks that result in the template
653 if (dependent_type_p (type
))
655 tree treq
= finish_type_requirement (type
);
656 result
= tree_cons (NULL_TREE
, treq
, result
);
659 // Ensure that the result of the expression can be converted to
661 tree decl_type
= finish_decltype_type (expr
, false, tf_none
);
662 tree creq
= finish_trait_expr (CPTK_IS_CONVERTIBLE_TO
, decl_type
, type
);
663 result
= tree_cons (NULL_TREE
, creq
, result
);
666 // If constraint specifiers are present, make them part of the
667 // list of constraints.
670 TREE_CHAIN (tree_last (specs
)) = result
;
674 // Finally, construct the syntactic requirement.
675 return build_check_expr (EXPR_REQ
, nreverse (result
));
678 // Finish a simple syntax requirement, returning a node representing
679 // a check that EXPR is a valid expression.
681 finish_expr_requirement (tree expr
)
683 gcc_assert (processing_template_decl
);
684 tree req
= finish_validexpr_expr (expr
);
685 tree reqs
= tree_cons (NULL_TREE
, req
, NULL_TREE
);
686 return build_check_expr (EXPR_REQ
, reqs
);
689 // Finish a type requirement, returning a node representing a check
690 // that TYPE will result in a valid type when instantiated.
692 finish_type_requirement (tree type
)
694 gcc_assert (processing_template_decl
);
695 tree req
= finish_validtype_expr (type
);
696 return build_check_expr (TYPE_REQ
, req
);
700 finish_nested_requirement (tree expr
)
702 gcc_assert (processing_template_decl
);
703 return build_check_expr (NESTED_REQ
, expr
);
706 // Finish a constexpr requirement, returning a node representing a
707 // check that EXPR, when instantiated, may be evaluated at compile time.
709 finish_constexpr_requirement (tree expr
)
711 gcc_assert (processing_template_decl
);
712 return finish_constexpr_expr (expr
);
715 // Finish the noexcept requirement by constructing a noexcept
716 // expression evaluating EXPR.
718 finish_noexcept_requirement (tree expr
)
720 gcc_assert (processing_template_decl
);
721 return finish_noexcept_expr (expr
, tf_none
);
724 // Returns the true or false node depending on the truth value of B.
728 return b
? boolean_true_node
: boolean_false_node
;
731 // Returns a finished validexpr-expr. Returns the true or false node
732 // depending on whether EXPR denotes a valid expression. This is the case
733 // when the expression has been successfully type checked.
735 // When processing a template declaration, the result is an expression
736 // representing the check.
738 finish_validexpr_expr (tree expr
)
740 if (processing_template_decl
)
741 return build_check_expr (VALIDEXPR_EXPR
, expr
);
742 return truth_node (expr
&& expr
!= error_mark_node
);
745 // Returns a finished validtype-expr. Returns the true or false node
746 // depending on whether T denotes a valid type name.
748 // When processing a template declaration, the result is an expression
749 // representing the check.
751 finish_validtype_expr (tree type
)
753 if (processing_template_decl
)
754 return build_check_expr (VALIDTYPE_EXPR
, type
);
755 return truth_node (type
&& TYPE_P (type
));
758 // Returns a finished constexpr-expr. Returns the true or false node
759 // depending on whether the expression T may be evaluated at compile
762 // When processing a template declaration, the result is an expression
763 // representing the check.
765 finish_constexpr_expr (tree expr
)
767 if (processing_template_decl
)
768 return build_check_expr (CONSTEXPR_EXPR
, expr
);
770 // TODO: Actually check that the expression can be constexpr
773 // return truth_node (potential_constant_expression (expr));
774 sorry ("constexpr requirement");
778 // Check that a constrained friend declaration function declaration,
779 // FN, is admissable. This is the case only when the declaration depends
780 // on template parameters and does not declare a specialization.
782 check_constrained_friend (tree fn
, tree reqs
)
784 if (fn
== error_mark_node
)
786 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
788 // If there are not constraints, this cannot be an error.
792 // Constrained friend functions that don't depend on template
793 // arguments are effectively meaningless.
794 tree parms
= DECL_ARGUMENTS (fn
);
795 tree result
= TREE_TYPE (TREE_TYPE (fn
));
796 if (!(parms
&& uses_template_parms (parms
)) && !uses_template_parms (result
))
798 error ("constrained friend does not depend on template parameters");
803 // Given an overload set, OVL, and a template argument or placeholder, ARG,
804 // synthesize a call expression that resolves to a concept check of
805 // the expression the form OVL<ARG>().
807 build_concept_check (tree ovl
, tree arg
)
809 // Build a template-id that acts as the call target using OVL as
810 // the template and ARG as the only explicit argument.
811 tree targs
= make_tree_vec (1);
812 TREE_VEC_ELT (targs
, 0) = arg
;
813 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, 1);
814 tree id
= lookup_template_function (ovl
, targs
);
816 // Build a new call expression, but don't actually generate a new
817 // function call. We just want the tree, not the semantics.
818 ++processing_template_decl
;
819 vec
<tree
, va_gc
> *fargs
= make_tree_vector();
820 tree call
= finish_call_expr (id
, &fargs
, false, false, tf_none
);
821 --processing_template_decl
;
826 // Returns a TYPE_DECL that contains sufficient information to build
827 // a template parameter of the same kind as PROTO and constrained
828 // by the concept declaration FN. PROTO is saved as the initializer of
829 // the new type decl, and the constraining function is saved in
832 describe_template_parm (tree proto
, tree fn
)
834 tree name
= DECL_NAME (fn
);
835 tree type
= TREE_TYPE (proto
);
836 tree decl
= build_decl (input_location
, TYPE_DECL
, name
, type
);
837 DECL_INITIAL (decl
) = proto
; // Describing parameter
838 DECL_SIZE_UNIT (decl
) = fn
; // Constraining function declaration
842 // If the result is a TYPE_DECL, its DECL_NAME is the name of the
843 // concept (without arguments), its TREE_TYPE refers to the type of the
844 // first template parameter of concept definition (the prototype parameter),
845 // its DECL_INITIAL is the declaration of the prototype parameter, and
846 // its DECL_SIZE_UNIT is the constraining concept declaration.
848 // TODO: A variable template may refer to a concept. The concept-name
849 // could introduce a constrained placeholder type in the terse template
852 finish_concept_name (tree decl
)
854 gcc_assert (TREE_CODE (decl
) == OVERLOAD
);
856 // Try to build a call expression that evaluates the concept. This
857 // can fail if the overload set refers only to non-templates.
858 tree call
= build_concept_check (decl
, build_nt(PLACEHOLDER_EXPR
));
859 if (call
== error_mark_node
)
862 // Resolve the constraint check to deduce the declared parameter.
863 tree check
= resolve_constraint_check (call
);
867 // Get function and argument from the resolved check expression. If
868 // the argument was a pack expansion, then get the first element
870 tree fn
= TREE_VALUE (check
);
871 tree arg
= TREE_VEC_ELT (TREE_PURPOSE (check
), 0);
872 if (ARGUMENT_PACK_P (arg
))
873 arg
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg
), 0);
875 // Get the protyping parameter bound to the placeholder.
876 tree proto
= TREE_TYPE (arg
);
878 // How we process the constrained declaration depends on the scope.
879 // In template scope, we return a "description" that will later be
880 // transformed into a real template parameter by process_template_parm.
881 if (template_parm_scope_p ())
882 return describe_template_parm (proto
, fn
);
884 // NOTE: We may need to be smarter about this since there are lots of
885 // places outside of a template parameter scope we'll want to use
886 // concept names (function arguments, function return types, result
887 // constraints, variable declarations, etc.).
892 // Create a requirement expression for the given DECL that evaluates the
893 // requirements specified by CONSTR, a TYPE_DECL that contains all the
894 // information necessary to build the requirements (see finish_concept_name
895 // for the layout of that TYPE_DECL).
897 // Note that the constraints are neither reduced nor decomposed. That is
898 // done only after the requires clause has been parsed (or not).
900 finish_shorthand_requirement (tree decl
, tree constr
)
902 // No requirements means no constraints.
906 tree proto
= DECL_INITIAL (constr
); // The prototype declaration
907 tree con
= DECL_SIZE_UNIT (constr
); // The concept declaration
909 // If the parameter declaration is variadic, but the concept is not
910 // then we need to apply the concept to every element in the pack.
911 bool is_proto_pack
= template_parameter_pack_p (proto
);
912 bool is_decl_pack
= template_parameter_pack_p (decl
);
913 bool apply_to_all_p
= is_decl_pack
&& !is_proto_pack
;
915 // Get the argument and overload used for the requirement. Adjust
916 // if we're going to expand later.
917 tree arg
= template_parm_to_arg (build_tree_list (NULL_TREE
, decl
));
919 arg
= PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg
), 0));
921 // Build the concept check. If it the constraint needs to be applied
922 // to all elements of the parameter pack, then expand make the constraint
924 tree ovl
= build_overload (DECL_TI_TEMPLATE (con
), NULL_TREE
);
925 tree check
= build_concept_check (ovl
, arg
);
928 check
= make_pack_expansion (check
);
930 // Set the type to indicate that this expansion will get special
931 // treatment during instantiation.
933 // TODO: Maybe this should be a different kind of node... one that
934 // has all the same properties as a pack expansion, but has a definite
935 // expansion when instantiated as part of an expression.
937 // As of now, this is a hack.
938 TREE_TYPE (check
) = boolean_type_node
;
944 // -------------------------------------------------------------------------- //
945 // Substitution Rules
947 // The following functions implement substitution rules for constraints.
950 // In an unevaluated context, the substitution of parm decls are not
951 // properly chained during substitution. Do that here.
953 fix_local_parms (tree sparms
)
958 tree p
= TREE_CHAIN (sparms
);
960 while (p
&& TREE_VALUE (p
) != void_type_node
)
962 DECL_CHAIN (TREE_VALUE (q
)) = TREE_VALUE (p
);
969 // Register local specializations for each of tparm and the corresponding
970 // sparm. This is a helper function for tsubst_requires_expr.
972 declare_local_parms (tree tparms
, tree sparms
)
974 tree s
= TREE_VALUE (sparms
);
975 for (tree p
= tparms
; p
&& !VOID_TYPE_P (TREE_VALUE (p
)); p
= TREE_CHAIN (p
))
977 tree t
= TREE_VALUE (p
);
980 tree pack
= extract_fnparm_pack (t
, &s
);
981 register_local_specialization (pack
, t
);
985 register_local_specialization (s
, t
);
991 // Substitute ARGS into the parameter list T, producing a sequence of
992 // local parameters (variables) in the current scope.
994 tsubst_local_parms (tree t
,
996 tsubst_flags_t complain
,
999 tree r
= fix_local_parms (tsubst (t
, args
, complain
, in_decl
));
1000 if (r
== error_mark_node
)
1001 return error_mark_node
;
1003 // Register the instantiated args as local parameters.
1005 declare_local_parms (t
, r
);
1010 // Substitute ARGS into the requirement body (list of requirements), T.
1012 tsubst_requirement_body (tree t
, tree args
, tree in_decl
)
1014 cp_unevaluated guard
;
1018 // If any substitutions fail, then this is equivalent to
1020 tree e
= tsubst_expr (TREE_VALUE (t
), args
, tf_none
, in_decl
, false);
1021 if (e
== error_mark_node
)
1022 e
= boolean_false_node
;
1023 r
= tree_cons (NULL_TREE
, e
, r
);
1030 // Substitute ARGS into the requires expression T.
1032 tsubst_requires_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
1034 local_specialization_stack stack
;
1035 tree p
= tsubst_local_parms (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
1036 tree r
= tsubst_requirement_body (TREE_OPERAND (t
, 1), args
, in_decl
);
1037 return finish_requires_expr (p
, r
);
1040 // Substitute ARGS into the valid-expr expression T.
1042 tsubst_validexpr_expr (tree t
, tree args
, tree in_decl
)
1044 tree r
= tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1045 return finish_validexpr_expr (r
);
1048 // Substitute ARGS into the valid-type expression T.
1050 tsubst_validtype_expr (tree t
, tree args
, tree in_decl
)
1052 tree r
= tsubst (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
);
1053 return finish_validtype_expr (r
);
1056 // Substitute ARGS into the constexpr expression T.
1058 tsubst_constexpr_expr (tree t
, tree args
, tree in_decl
)
1060 tree r
= tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1061 return finish_constexpr_expr (r
);
1064 // Substitute ARGS into the expr requirement T. Note that a requirement
1065 // node is instantiated from a non-reduced context (e.g., static_assert).
1067 tsubst_expr_req (tree t
, tree args
, tree in_decl
)
1070 for (tree l
= TREE_OPERAND (t
, 0); l
; l
= TREE_CHAIN (l
))
1072 tree e
= tsubst_expr (TREE_VALUE (l
), args
, tf_none
, in_decl
, false);
1073 r
= conjoin_requirements (r
, e
);
1078 // Substitute ARGS into the type requirement T. Note that a requirement
1079 // node is instantiated from a non-reduced context (e.g., static_assert).
1081 tsubst_type_req (tree t
, tree args
, tree in_decl
)
1083 return tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1086 // Substitute ARGS into the nested requirement T. Note that a requirement
1087 // node is instantiated from a non-reduced context (e.g., static_assert).
1089 tsubst_nested_req (tree t
, tree args
, tree in_decl
)
1091 return tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1094 // Substitute the template arguments ARGS into the requirement
1095 // expression REQS. Errors resulting from substitution are not
1098 instantiate_requirements (tree reqs
, tree args
)
1100 return tsubst_expr (reqs
, args
, tf_none
, NULL_TREE
, false);
1103 // -------------------------------------------------------------------------- //
1104 // Constraint Satisfaction
1106 // The following functions are responsible for the instantiation and
1107 // evaluation of constraints.
1110 // Returns true if the requirements expression REQS is satisfied
1111 // and false otherwise. The requirements are checked by simply
1112 // evaluating REQS as a constant expression.
1114 check_requirements (tree reqs
)
1116 // Reduce any remaining TRAIT_EXPR nodes before evaluating.
1117 reqs
= fold_non_dependent_expr (reqs
);
1119 // Requirements are satisfied when REQS evaluates to true.
1120 return cxx_constant_value (reqs
) == boolean_true_node
;
1123 // Returns true if the requirements expression REQS is satisfied
1124 // and false otherwise. The requirements are checked by first
1125 // instantiating REQS and then evaluating it as a constant expression.
1127 check_requirements (tree reqs
, tree args
)
1129 // If any arguments are dependent, then we can't check the
1130 // requirements. Just return true.
1131 if (uses_template_parms (args
))
1134 // Instantiate and evaluate the requirements.
1135 reqs
= instantiate_requirements (reqs
, args
);
1136 if (reqs
== error_mark_node
)
1138 return check_requirements (reqs
);
1142 // Check the instantiated declaration constraints.
1144 check_constraints (tree cinfo
)
1146 // No constraints? Satisfied.
1149 return check_requirements (CI_REQUIREMENTS (cinfo
));
1152 // Check the constraints in CINFO against the given ARGS, returning
1153 // true when the constraints are satisfied and false otherwise.
1155 check_constraints (tree cinfo
, tree args
)
1157 // No constraints? Satisfied.
1161 // Dependent arguments? Satisfied. They won't reduce to true or false.
1162 if (uses_template_parms (args
))
1165 return check_requirements (CI_REQUIREMENTS (cinfo
), args
);
1168 // Check the constraints of the declaration or type T, against
1169 // the specified arguments. Returns true if the constraints are
1170 // satisfied and false otherwise.
1172 check_template_constraints (tree t
, tree args
)
1174 return check_constraints (DECL_CONSTRAINTS (t
), args
);
1177 // -------------------------------------------------------------------------- //
1178 // Constraint Relations
1180 // Interfaces for determining equivalency and ordering of constraints.
1182 // Returns true when A and B are equivalent constraints.
1184 equivalent_constraints (tree a
, tree b
)
1189 return subsumes (a
, b
) && subsumes (b
, a
);
1192 // Returns true if the template declarations A and B have equivalent
1193 // constraints. This is the case when A's constraints subsume B's and
1194 // when B's also constrain A's.
1196 equivalently_constrained (tree a
, tree b
)
1198 gcc_assert (TREE_CODE (a
) == TREE_CODE (b
));
1199 return equivalent_constraints (DECL_CONSTRAINTS (a
), DECL_CONSTRAINTS (b
));
1202 // Returns true when the A contains more atomic properties than B.
1204 more_constraints (tree a
, tree b
)
1206 return subsumes (a
, b
);
1209 // Returns true when the template declaration A's constraints subsume
1210 // those of the template declaration B.
1212 more_constrained (tree a
, tree b
)
1214 gcc_assert (TREE_CODE (a
) == TREE_CODE (b
));
1215 return more_constraints (DECL_CONSTRAINTS (a
), DECL_CONSTRAINTS (b
));
1219 // -------------------------------------------------------------------------- //
1220 // Constraint Diagnostics
1224 void diagnose_node (location_t
, tree
, tree
);
1226 // Diagnose a constraint failure for type trait expressions.
1228 diagnose_trait (location_t loc
, tree t
, tree args
)
1230 if (check_requirements (t
, args
))
1233 ++processing_template_decl
;
1234 tree subst
= instantiate_requirements (t
, args
);
1235 --processing_template_decl
;
1237 if (subst
== error_mark_node
)
1239 inform (input_location
, " substitution failure in %qE", t
);
1243 tree t1
= TRAIT_EXPR_TYPE1 (subst
);
1244 tree t2
= TRAIT_EXPR_TYPE2 (subst
);
1245 switch (TRAIT_EXPR_KIND (t
))
1247 case CPTK_HAS_NOTHROW_ASSIGN
:
1248 inform (loc
, " %qT is not nothrow assignable", t1
);
1250 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
1251 inform (loc
, " %qT is not nothrow constructible", t1
);
1253 case CPTK_HAS_NOTHROW_COPY
:
1254 inform (loc
, " %qT is not nothrow copyable", t1
);
1256 case CPTK_HAS_TRIVIAL_ASSIGN
:
1257 inform (loc
, " %qT is not trivially assignable", t1
);
1259 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
1260 inform (loc
, " %qT is not trivially constructible", t1
);
1262 case CPTK_HAS_TRIVIAL_COPY
:
1263 inform (loc
, " %qT is not trivially copyable", t1
);
1265 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
1266 inform (loc
, " %qT is not trivially destructible", t1
);
1268 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
1269 inform (loc
, " %qT does not have a virtual destructor", t1
);
1271 case CPTK_IS_ABSTRACT
:
1272 inform (loc
, " %qT is not an abstract class", t1
);
1274 case CPTK_IS_BASE_OF
:
1275 inform (loc
, " %qT is not a base of %qT", t1
, t2
);
1278 inform (loc
, " %qT is not a class", t1
);
1280 case CPTK_IS_CONVERTIBLE_TO
:
1281 inform (loc
, " %qT is not convertible to %qT", t1
, t2
);
1284 inform (loc
, " %qT is not an empty class", t1
);
1287 inform (loc
, " %qT is not an enum", t1
);
1290 inform (loc
, " %qT is not a final class", t1
);
1292 case CPTK_IS_LITERAL_TYPE
:
1293 inform (loc
, " %qT is not a literal type", t1
);
1296 inform (loc
, " %qT is not a POD type", t1
);
1298 case CPTK_IS_POLYMORPHIC
:
1299 inform (loc
, " %qT is not a polymorphic type", t1
);
1301 case CPTK_IS_SAME_AS
:
1302 inform (loc
, " %qT is not the same as %qT", t1
, t2
);
1304 case CPTK_IS_STD_LAYOUT
:
1305 inform (loc
, " %qT is not an standard layout type", t1
);
1307 case CPTK_IS_TRIVIAL
:
1308 inform (loc
, " %qT is not a trivial type", t1
);
1311 inform (loc
, " %qT is not a union", t1
);
1318 // Diagnose a failed concept check in concept indicated by T, where
1319 // T is the result of resolve_constraint_check. Recursively analyze
1320 // the nested requiremets for details.
1322 diagnose_check (location_t loc
, tree t
, tree args
)
1324 tree fn
= TREE_VALUE (t
);
1325 tree targs
= TREE_PURPOSE (t
);
1326 tree body
= DECL_SAVED_TREE (fn
);
1330 inform (loc
, " failure in constraint %q#D", DECL_TI_TEMPLATE (fn
));
1332 // Perform a mini-reduction on the constraint.
1333 if (TREE_CODE (body
) == BIND_EXPR
)
1334 body
= BIND_EXPR_BODY (body
);
1335 if (TREE_CODE (body
) == RETURN_EXPR
)
1336 body
= TREE_OPERAND (body
, 0);
1338 // Locally instantiate the body with the call's template args,
1339 // and recursively diagnose.
1340 ++processing_template_decl
;
1341 body
= instantiate_requirements (body
, targs
);
1342 --processing_template_decl
;
1344 diagnose_node (loc
, body
, args
);
1347 // Diagnose constraint failures from the call expression T.
1349 diagnose_call (location_t loc
, tree t
, tree args
)
1351 if (check_requirements (t
, args
))
1354 // If this is a concept, we're going to recurse.
1355 // If it's just a call, then we can emit a simple message.
1356 if (tree check
= resolve_constraint_check (t
))
1357 diagnose_check (loc
, check
, args
);
1359 inform (loc
, " %qE evaluated to false", t
);
1362 // Diagnose specific constraint failures.
1364 diagnose_requires (location_t loc
, tree t
, tree args
)
1366 if (check_requirements (t
, args
))
1369 ++processing_template_decl
;
1370 tree subst
= instantiate_requirements (t
, args
);
1371 --processing_template_decl
;
1373 // Print the header for the requires expression.
1374 tree parms
= TREE_OPERAND (subst
, 0);
1375 if (!VOID_TYPE_P (TREE_VALUE (parms
)))
1376 inform (loc
, " requiring syntax with values %Z", TREE_OPERAND (subst
, 0));
1378 // Create a new local specialization binding for the arguments.
1379 // This lets us instantiate sub-expressions separately from the
1381 local_specialization_stack locals
;
1382 declare_local_parms (TREE_OPERAND (t
, 0), TREE_OPERAND (subst
, 0));
1384 // Iterate over the sub-requirements and try instantiating each.
1385 for (tree l
= TREE_OPERAND (t
, 1); l
; l
= TREE_CHAIN (l
))
1386 diagnose_node (loc
, TREE_VALUE (l
), args
);
1390 diagnose_validexpr (location_t loc
, tree t
, tree args
)
1392 if (check_requirements (t
, args
))
1394 inform (loc
, " %qE is not a valid expression", TREE_OPERAND (t
, 0));
1398 diagnose_validtype (location_t loc
, tree t
, tree args
)
1400 if (check_requirements (t
, args
))
1403 // Substitute into the qualified name.
1404 tree name
= TREE_OPERAND (t
, 0);
1405 if (tree cxt
= TYPE_CONTEXT (name
))
1407 tree id
= TYPE_IDENTIFIER (name
);
1408 cxt
= tsubst (cxt
, args
, tf_none
, NULL_TREE
);
1409 name
= build_qualified_name (NULL_TREE
, cxt
, id
, false);
1410 inform (loc
, " %qE does not name a valid type", name
);
1414 inform (loc
, " %qT does not name a valid type", name
);
1419 diagnose_constexpr (location_t loc
, tree t
, tree args
)
1421 if (check_requirements (t
, args
))
1423 inform (loc
, " %qE is not a constant expression", TREE_OPERAND (t
, 0));
1427 diagnose_noexcept (location_t loc
, tree t
, tree args
)
1429 if (check_requirements (t
, args
))
1431 inform (loc
, " %qE propagates exceptions", TREE_OPERAND (t
, 0));
1434 // Diagnose a constraint failure in the expression T.
1436 diagnose_other (location_t loc
, tree t
, tree args
)
1438 if (check_requirements (t
, args
))
1440 inform (loc
, " %qE evaluated to false", t
);
1443 // Diagnose a constraint failure in the subtree T.
1445 diagnose_node (location_t loc
, tree t
, tree args
)
1447 switch (TREE_CODE (t
))
1449 case TRUTH_ANDIF_EXPR
:
1450 diagnose_node (loc
, TREE_OPERAND (t
, 0), args
);
1451 diagnose_node (loc
, TREE_OPERAND (t
, 1), args
);
1454 case TRUTH_ORIF_EXPR
:
1455 // TODO: Design better diagnostics for dijunctions.
1456 diagnose_other (loc
, t
, args
);
1460 diagnose_trait (loc
, t
, args
);
1464 diagnose_call (loc
, t
, args
);
1468 diagnose_requires (loc
, t
, args
);
1471 case VALIDEXPR_EXPR
:
1472 diagnose_validexpr (loc
, t
, args
);
1475 case VALIDTYPE_EXPR
:
1476 diagnose_validtype (loc
, t
, args
);
1479 case CONSTEXPR_EXPR
:
1480 diagnose_constexpr (loc
, t
, args
);
1484 diagnose_noexcept (loc
, t
, args
);
1488 diagnose_other (loc
, t
, args
);
1493 // Diagnose a constraint failure in the requirements expression REQS.
1495 diagnose_requirements (location_t loc
, tree reqs
, tree args
)
1497 diagnose_node (loc
, reqs
, args
);
1500 // Create a tree node representing the substitution of ARGS into
1501 // the parameters of TMPL. The resulting structure is passed as an
1502 // for diagnosing substitutions.
1504 make_subst (tree tmpl
, tree args
)
1506 tree subst
= tree_cons (NULL_TREE
, args
, NULL_TREE
);
1507 TREE_TYPE (subst
) = DECL_TEMPLATE_PARMS (tmpl
);
1513 // Emit diagnostics detailing the failure ARGS to satisfy the constraints
1514 // of the template declaration, TMPL.
1516 diagnose_constraints (location_t loc
, tree tmpl
, tree args
)
1518 inform (loc
, " constraints not satisfied %S", make_subst (tmpl
, args
));
1520 // Diagnose the constraints by recursively decomposing and
1521 // evaluating the template requirements.
1522 tree reqs
= CI_SPELLING (DECL_CONSTRAINTS (tmpl
));
1523 diagnose_requirements (loc
, reqs
, args
);