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
);
196 // Given a call expression to a concept, possibly including a placeholder
197 // argument, deduce the concept being checked and the prototype paraemter.
198 // Returns true if the constraint and prototype can be deduced and false
199 // otherwise. Note that the CHECK and PROTO arguments are set to NULL_TREE
200 // if this returns false.
202 deduce_constrained_parameter (tree call
, tree
& check
, tree
& proto
)
204 // Resolve the constraint check to deduce the declared parameter.
205 if (tree info
= resolve_constraint_check (call
))
207 // Get function and argument from the resolved check expression and
208 // the prototype parameter. Note that if the first argument was a
209 // pack, we need to extract the first element ot get the prototype.
210 check
= TREE_VALUE (info
);
211 tree arg
= TREE_VEC_ELT (TREE_PURPOSE (info
), 0);
212 if (ARGUMENT_PACK_P (arg
))
213 arg
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg
), 0);
214 proto
= TREE_TYPE (arg
);
217 check
= proto
= NULL_TREE
;
221 // -------------------------------------------------------------------------- //
222 // Requirement Reduction
224 // Reduces a template requirement to a logical formula written in terms of
225 // atomic propositions, returing the new expression. If the expression cannot
226 // be reduced, a NULL_TREE is returned, indicating failure to reduce the
227 // original requirment.
232 static tree
reduce_node (tree
);
233 static tree
reduce_expr (tree
);
234 static tree
reduce_stmt (tree
);
235 static tree
reduce_decl (tree
);
236 static tree
reduce_misc (tree
);
238 static tree
reduce_logical (tree
);
239 static tree
reduce_call (tree
);
240 static tree
reduce_requires (tree
);
241 static tree
reduce_expr_req (tree
);
242 static tree
reduce_type_req (tree
);
243 static tree
reduce_nested_req (tree
);
244 static tree
reduce_template_id (tree
);
245 static tree
reduce_stmt_list (tree
);
247 // Reduce the requirement T into a logical formula written in terms of
248 // atomic propositions.
252 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
258 return reduce_expr (t
);
261 return reduce_stmt (t
);
263 case tcc_declaration
:
264 return reduce_decl (t
);
266 case tcc_exceptional
:
267 return reduce_misc (t
);
269 // These kinds of expressions are atomic.
281 // Reduction rules for the expression node T.
285 switch (TREE_CODE (t
))
287 case TRUTH_ANDIF_EXPR
:
288 case TRUTH_ORIF_EXPR
:
289 return reduce_logical (t
);
292 return reduce_call (t
);
295 return reduce_requires (t
);
298 return reduce_expr_req (t
);
301 return reduce_type_req (t
);
304 return reduce_nested_req (t
);
306 case TEMPLATE_ID_EXPR
:
307 return reduce_template_id (t
);
310 return reduce_node (TREE_VALUE (TREE_OPERAND (t
, 0)));
313 return reduce_node (BIND_EXPR_BODY (t
));
319 // Everything else is atomic.
326 // Reduction rules for the statement T.
330 switch (TREE_CODE (t
))
332 // Reduce the returned expression.
334 return reduce_node (TREE_OPERAND (t
, 0));
336 // These statements do not introduce propositions
337 // in the constraints language. Do not recurse.
348 // Reduction rules for the declaration T.
352 switch (TREE_CODE (t
))
354 // References to var decls are atomic.
364 // Reduction rules for the node T.
368 switch (TREE_CODE (t
))
370 // Errors and traits are atomic.
376 return reduce_stmt_list (t
);
384 // Reduction rules for the binary logical expression T (&& and ||).
386 // Generate a new expression from the reduced operands. If either operand
387 // cannot be reduced, then the resulting expression is null.
389 reduce_logical (tree t
)
391 tree l
= reduce_expr (TREE_OPERAND (t
, 0));
392 tree r
= reduce_expr (TREE_OPERAND (t
, 1));
396 TREE_OPERAND (t
, 0) = l
;
397 TREE_OPERAND (t
, 1) = r
;
404 // Reduction rules for the call expression T.
406 // If T is a call to a constraint instantiate its definition and
407 // recursively reduce its returned expression.
411 // Is the function call actually a constraint check?
412 tree check
= resolve_constraint_check (t
);
416 tree fn
= TREE_VALUE (check
);
417 tree args
= TREE_PURPOSE (check
);
419 // Reduce the body of the function into the constriants language.
420 tree body
= reduce_requirements (DECL_SAVED_TREE (fn
));
423 error ("could not inline requirements from %qD", fn
);
424 return error_mark_node
;
427 // Instantiate the reduced results using the deduced args.
428 tree result
= instantiate_requirements (body
, args
);
429 if (result
== error_mark_node
)
431 error ("could not instantiate requirements from %qD", fn
);
432 return error_mark_node
;
437 // Reduction rules for the template-id T.
439 // It turns out that we often get requirements being written like this:
441 // template<typename T>
445 // Where Foo<T> should actually be written as Foo<T>(). Generate an
446 // error and suggest the improved writing.
448 reduce_template_id (tree t
)
450 vec
<tree
, va_gc
>* args
= NULL
;
451 tree c
= finish_call_expr (t
, &args
, true, false, 0);
452 error_at (EXPR_LOCATION (t
), "invalid requirement");
453 inform (EXPR_LOCATION (t
), "did you mean %qE", c
);
458 // Reduce an expression requirement as a conjunction of its
459 // individual constraints.
461 reduce_expr_req (tree t
)
464 for (tree l
= TREE_OPERAND (t
, 0); l
; l
= TREE_CHAIN (l
))
465 r
= conjoin_requirements (r
, reduce_expr (TREE_VALUE (l
)));
469 // Reduce a type requirement by returing its underlying
472 reduce_type_req (tree t
)
474 return TREE_OPERAND (t
, 0);
477 // Reduce a nested requireemnt by returing its only operand.
479 reduce_nested_req (tree t
)
481 return TREE_OPERAND (t
, 0);
484 // Reduce a requires expr by reducing each requirement in turn,
485 // rewriting the list of requirements so that we end up with a
486 // list of expressions, some of which may be conjunctions.
488 reduce_requires (tree t
)
490 for (tree l
= TREE_OPERAND (t
, 1); l
; l
= TREE_CHAIN (l
))
491 TREE_VALUE (l
) = reduce_expr (TREE_VALUE (l
));
495 // Reduction rules for the statement list STMTS.
497 // Recursively reduce each statement in the list, concatenating each
498 // reduced result into a conjunction of requirements.
500 // A constexpr function may include statements other than a return
501 // statement. The primary purpose of these rules is to filter those
502 // non-return statements from the constraints language.
504 reduce_stmt_list (tree stmts
)
506 tree lhs
= NULL_TREE
;
507 tree_stmt_iterator i
= tsi_start (stmts
);
508 while (!tsi_end_p (i
))
510 if (tree rhs
= reduce_node (tsi_stmt (i
)))
511 lhs
= conjoin_requirements (lhs
, rhs
);
519 // Reduce the requirement REQS into a logical formula written in terms of
520 // atomic propositions.
522 reduce_requirements (tree reqs
)
524 return reduce_node (reqs
);
527 // -------------------------------------------------------------------------- //
528 // Constraint Semantic Processing
530 // The following functions are called by the parser and substitution rules
531 // to create and evaluate constraint-related nodes.
533 // Create a constraint-info node from the specified requirements.
535 make_constraints (tree reqs
)
537 // No requirements == no constraints
541 // Reduce the requirements into a single expression of constraints.
542 tree expr
= reduce_requirements (reqs
);
543 if (expr
== error_mark_node
)
544 return error_mark_node
;
546 // Decompose those expressions into lists of lists of atomic
548 tree assume
= decompose_assumptions (expr
);
550 // Build the constraint info.
551 tree_constraint_info
*cinfo
=
552 (tree_constraint_info
*)make_node (CONSTRAINT_INFO
);
553 cinfo
->spelling
= reqs
;
554 cinfo
->requirements
= expr
;
555 cinfo
->assumptions
= assume
;
559 // Returns the template constraints of declaration T. If T is not a
560 // template, this return NULL_TREE. Note that T must be non-null.
562 get_constraints (tree t
)
564 gcc_assert (DECL_P (t
));
565 if (TREE_CODE (t
) != TEMPLATE_DECL
)
567 if (!DECL_TEMPLATE_INFO (t
))
570 return DECL_CONSTRAINTS (DECL_TI_TEMPLATE (t
));
572 return DECL_CONSTRAINTS (t
);
575 // Returns a conjunction of shorthand requirements for the template
576 // parameter list PARMS. Note that the requirements are stored in
577 // the TYPE of each tree node.
579 get_shorthand_requirements (tree parms
)
581 tree reqs
= NULL_TREE
;
582 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
583 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
585 tree parm
= TREE_VEC_ELT (parms
, i
);
586 reqs
= conjoin_requirements(reqs
, TREE_TYPE (parm
));
591 // Finish the template requirement, EXPR, by translating it into
592 // a constraint information record.
594 finish_template_requirements (tree expr
)
596 if (expr
== error_mark_node
)
599 return make_constraints (expr
);
603 build_requires_expr (tree parms
, tree reqs
)
605 // Modify the declared parameters by removing their context (so they
606 // don't refer to the enclosing scope), and marking them constant (so
607 // we can actually check constexpr properties).
608 for (tree p
= parms
; p
&& !VOID_TYPE_P (TREE_VALUE (p
)); p
= TREE_CHAIN (p
))
610 tree parm
= TREE_VALUE (p
);
611 DECL_CONTEXT (parm
) = NULL_TREE
;
612 TREE_CONSTANT (parm
) = true;
616 tree r
= build_min (REQUIRES_EXPR
, boolean_type_node
, parms
, reqs
);
617 TREE_SIDE_EFFECTS (r
) = false;
618 TREE_CONSTANT (r
) = true;
622 // Evaluate an instantiatd requires expr, returning the truth node
623 // only when all sub-requirements have evaluated to true.
625 eval_requires_expr (tree reqs
)
627 for (tree t
= reqs
; t
; t
= TREE_CHAIN (t
)) {
628 tree r
= TREE_VALUE (t
);
629 r
= fold_non_dependent_expr (r
);
630 r
= maybe_constant_value (r
);
631 if (r
!= boolean_true_node
)
632 return boolean_false_node
;
634 return boolean_true_node
;
637 // Finish a requires expression, returning a node wrapping the parameters,
638 // PARMS, and the list of requirements REQS.
640 finish_requires_expr (tree parms
, tree reqs
)
642 if (processing_template_decl
)
643 return build_requires_expr (parms
, reqs
);
645 return eval_requires_expr (reqs
);
648 // Construct a unary expression that evaluates properties of the
649 // expression or type T, and has a boolean result type.
651 build_check_expr (tree_code c
, tree t
)
653 tree r
= build_min (c
, boolean_type_node
, t
);
654 TREE_SIDE_EFFECTS (r
) = false;
655 TREE_READONLY (r
) = true;
656 TREE_CONSTANT (r
) = true;
660 // Finish a syntax requirement, constructing a list embodying a sequence
661 // of checks for the validity of EXPR and TYPE, the convertibility of
662 // EXPR to TYPE, and the expression properties specified in SPECS.
664 finish_expr_requirement (tree expr
, tree type
, tree specs
)
666 gcc_assert (processing_template_decl
);
668 // Build a list of checks, starting with the valid expression.
669 tree result
= tree_cons (NULL_TREE
, finish_validexpr_expr (expr
), NULL_TREE
);
671 // If a type requirement was provided, build the result type checks.
674 // If the type is dependent, ensure that it can be validly
677 // NOTE: We can also disregard checks that result in the template
679 if (dependent_type_p (type
))
681 tree treq
= finish_type_requirement (type
);
682 result
= tree_cons (NULL_TREE
, treq
, result
);
685 // Ensure that the result of the expression can be converted to
687 tree decl_type
= finish_decltype_type (expr
, false, tf_none
);
688 tree creq
= finish_trait_expr (CPTK_IS_CONVERTIBLE_TO
, decl_type
, type
);
689 result
= tree_cons (NULL_TREE
, creq
, result
);
692 // If constraint specifiers are present, make them part of the
693 // list of constraints.
696 TREE_CHAIN (tree_last (specs
)) = result
;
700 // Finally, construct the syntactic requirement.
701 return build_check_expr (EXPR_REQ
, nreverse (result
));
704 // Finish a simple syntax requirement, returning a node representing
705 // a check that EXPR is a valid expression.
707 finish_expr_requirement (tree expr
)
709 gcc_assert (processing_template_decl
);
710 tree req
= finish_validexpr_expr (expr
);
711 tree reqs
= tree_cons (NULL_TREE
, req
, NULL_TREE
);
712 return build_check_expr (EXPR_REQ
, reqs
);
715 // Finish a type requirement, returning a node representing a check
716 // that TYPE will result in a valid type when instantiated.
718 finish_type_requirement (tree type
)
720 gcc_assert (processing_template_decl
);
721 tree req
= finish_validtype_expr (type
);
722 return build_check_expr (TYPE_REQ
, req
);
726 finish_nested_requirement (tree expr
)
728 gcc_assert (processing_template_decl
);
729 return build_check_expr (NESTED_REQ
, expr
);
732 // Finish a constexpr requirement, returning a node representing a
733 // check that EXPR, when instantiated, may be evaluated at compile time.
735 finish_constexpr_requirement (tree expr
)
737 gcc_assert (processing_template_decl
);
738 return finish_constexpr_expr (expr
);
741 // Finish the noexcept requirement by constructing a noexcept
742 // expression evaluating EXPR.
744 finish_noexcept_requirement (tree expr
)
746 gcc_assert (processing_template_decl
);
747 return finish_noexcept_expr (expr
, tf_none
);
750 // Returns the true or false node depending on the truth value of B.
754 return b
? boolean_true_node
: boolean_false_node
;
757 // Returns a finished validexpr-expr. Returns the true or false node
758 // depending on whether EXPR denotes a valid expression. This is the case
759 // when the expression has been successfully type checked.
761 // When processing a template declaration, the result is an expression
762 // representing the check.
764 finish_validexpr_expr (tree expr
)
766 if (processing_template_decl
)
767 return build_check_expr (VALIDEXPR_EXPR
, expr
);
768 return truth_node (expr
&& expr
!= error_mark_node
);
771 // Returns a finished validtype-expr. Returns the true or false node
772 // depending on whether T denotes a valid type name.
774 // When processing a template declaration, the result is an expression
775 // representing the check.
777 // FIXME: Semantics need to be aligned with the new version of the
778 // specificaiton (i.e., we must be able to invent a function and
779 // perform argument deduction against it).
781 finish_validtype_expr (tree type
)
785 sorry ("%<auto%< not supported in result type constraints\n");
786 return error_mark_node
;
789 if (processing_template_decl
)
790 return build_check_expr (VALIDTYPE_EXPR
, type
);
791 return truth_node (type
&& TYPE_P (type
));
794 // Returns a finished constexpr-expr. Returns the true or false node
795 // depending on whether the expression T may be evaluated at compile
798 // When processing a template declaration, the result is an expression
799 // representing the check.
801 finish_constexpr_expr (tree expr
)
803 if (processing_template_decl
)
804 return build_check_expr (CONSTEXPR_EXPR
, expr
);
806 // TODO: Actually check that the expression can be constexpr
809 // return truth_node (potential_constant_expression (expr));
810 sorry ("constexpr requirement");
814 // Check that a constrained friend declaration function declaration,
815 // FN, is admissable. This is the case only when the declaration depends
816 // on template parameters and does not declare a specialization.
818 check_constrained_friend (tree fn
, tree reqs
)
820 if (fn
== error_mark_node
)
822 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
824 // If there are not constraints, this cannot be an error.
828 // Constrained friend functions that don't depend on template
829 // arguments are effectively meaningless.
830 tree parms
= DECL_ARGUMENTS (fn
);
831 tree result
= TREE_TYPE (TREE_TYPE (fn
));
832 if (!(parms
&& uses_template_parms (parms
)) && !uses_template_parms (result
))
834 error ("constrained friend does not depend on template parameters");
840 // Build a new call expression, but don't actually generate a new
841 // function call. We just want the tree, not the semantics.
843 build_call_check (tree id
)
845 ++processing_template_decl
;
846 vec
<tree
, va_gc
> *fargs
= make_tree_vector();
847 tree call
= finish_call_expr (id
, &fargs
, false, false, tf_none
);
848 --processing_template_decl
;
853 // Construct a concept check for the overloaded function, where the
854 // template arguments are the list given by ARG and REST. That is, it
855 // build the call expression OVL<ARG, REST>(). If REST is null, then
856 // the resulting constraint is OVL<ARG>().
858 // TODO: Extend this to take a variable concept also.
860 build_concept_check (tree ovl
, tree arg
, tree rest
)
862 gcc_assert (TREE_CODE (ovl
) == OVERLOAD
);
863 gcc_assert (rest
? TREE_CODE (rest
) == TREE_VEC
: true);
865 // Build a template-id that acts as the call target using OVL as
866 // the template and ARG as the only explicit argument.
867 int n
= rest
? TREE_VEC_LENGTH (rest
) : 0;
868 tree targs
= make_tree_vec (n
+ 1);
869 TREE_VEC_ELT (targs
, 0) = arg
;
871 for (int i
= 0; i
< n
; ++i
)
872 TREE_VEC_ELT (targs
, i
+ 1) = TREE_VEC_ELT (rest
, i
);
873 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, n
+ 1);
874 tree id
= lookup_template_function (ovl
, targs
);
875 return build_call_check (id
);
878 // Returns a TYPE_DECL that contains sufficient information to build
879 // a template parameter of the same kind as PROTO and constrained
880 // by the concept declaration FN. PROTO is saved as the initializer of
881 // the new type decl, and the constraining function is saved in
884 // If specified ARGS provides additional arguments to the constraint
885 // check. These are stored in the DECL_SIZE field.
887 build_constrained_parameter (tree fn
, tree proto
, tree args
)
889 tree name
= DECL_NAME (fn
);
890 tree type
= TREE_TYPE (proto
);
891 tree decl
= build_decl (input_location
, TYPE_DECL
, name
, type
);
892 DECL_INITIAL (decl
) = proto
; // Describing parameter
893 DECL_SIZE_UNIT (decl
) = fn
; // Constraining function declaration
894 DECL_SIZE (decl
) = args
; // Extra template arguments.
898 // Create a requirement expression for the given DECL that evaluates the
899 // requirements specified by CONSTR, a TYPE_DECL that contains all the
900 // information necessary to build the requirements (see finish_concept_name
901 // for the layout of that TYPE_DECL).
903 // Note that the constraints are neither reduced nor decomposed. That is
904 // done only after the requires clause has been parsed (or not).
906 finish_shorthand_requirement (tree decl
, tree constr
)
908 // No requirements means no constraints.
912 tree proto
= DECL_INITIAL (constr
); // The prototype declaration
913 tree con
= DECL_SIZE_UNIT (constr
); // The concept declaration
914 tree args
= DECL_SIZE (constr
); // Extra template arguments
916 // If the parameter declaration is variadic, but the concept is not
917 // then we need to apply the concept to every element in the pack.
918 bool is_proto_pack
= template_parameter_pack_p (proto
);
919 bool is_decl_pack
= template_parameter_pack_p (decl
);
920 bool apply_to_all_p
= is_decl_pack
&& !is_proto_pack
;
922 // Get the argument and overload used for the requirement. Adjust
923 // if we're going to expand later.
924 tree arg
= template_parm_to_arg (build_tree_list (NULL_TREE
, decl
));
926 arg
= PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg
), 0));
928 // Build the concept check. If it the constraint needs to be applied
929 // to all elements of the parameter pack, then expand make the constraint
931 tree ovl
= build_overload (DECL_TI_TEMPLATE (con
), NULL_TREE
);
932 tree check
= build_concept_check (ovl
, arg
, args
);
935 check
= make_pack_expansion (check
);
937 // Set the type to indicate that this expansion will get special
938 // treatment during instantiation.
940 // TODO: Maybe this should be a different kind of node... one that
941 // has all the same properties as a pack expansion, but has a definite
942 // expansion when instantiated as part of an expression.
944 // As of now, this is a hack.
945 TREE_TYPE (check
) = boolean_type_node
;
951 // -------------------------------------------------------------------------- //
952 // Substitution Rules
954 // The following functions implement substitution rules for constraints.
957 // In an unevaluated context, the substitution of parm decls are not
958 // properly chained during substitution. Do that here.
960 fix_local_parms (tree sparms
)
965 tree p
= TREE_CHAIN (sparms
);
967 while (p
&& TREE_VALUE (p
) != void_type_node
)
969 DECL_CHAIN (TREE_VALUE (q
)) = TREE_VALUE (p
);
976 // Register local specializations for each of tparm and the corresponding
977 // sparm. This is a helper function for tsubst_requires_expr.
979 declare_local_parms (tree tparms
, tree sparms
)
981 tree s
= TREE_VALUE (sparms
);
982 for (tree p
= tparms
; p
&& !VOID_TYPE_P (TREE_VALUE (p
)); p
= TREE_CHAIN (p
))
984 tree t
= TREE_VALUE (p
);
987 tree pack
= extract_fnparm_pack (t
, &s
);
988 register_local_specialization (pack
, t
);
992 register_local_specialization (s
, t
);
998 // Substitute ARGS into the parameter list T, producing a sequence of
999 // local parameters (variables) in the current scope.
1001 tsubst_local_parms (tree t
,
1003 tsubst_flags_t complain
,
1006 tree r
= fix_local_parms (tsubst (t
, args
, complain
, in_decl
));
1007 if (r
== error_mark_node
)
1008 return error_mark_node
;
1010 // Register the instantiated args as local parameters.
1012 declare_local_parms (t
, r
);
1017 // Substitute ARGS into the requirement body (list of requirements), T.
1019 tsubst_requirement_body (tree t
, tree args
, tree in_decl
)
1021 cp_unevaluated guard
;
1025 // If any substitutions fail, then this is equivalent to
1027 tree e
= tsubst_expr (TREE_VALUE (t
), args
, tf_none
, in_decl
, false);
1028 if (e
== error_mark_node
)
1029 e
= boolean_false_node
;
1030 r
= tree_cons (NULL_TREE
, e
, r
);
1037 // Substitute ARGS into the requires expression T.
1039 tsubst_requires_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
1041 local_specialization_stack stack
;
1042 tree p
= tsubst_local_parms (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
1043 tree r
= tsubst_requirement_body (TREE_OPERAND (t
, 1), args
, in_decl
);
1044 return finish_requires_expr (p
, r
);
1047 // Substitute ARGS into the valid-expr expression T.
1049 tsubst_validexpr_expr (tree t
, tree args
, tree in_decl
)
1051 tree r
= tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1052 return finish_validexpr_expr (r
);
1055 // Substitute ARGS into the valid-type expression T.
1057 tsubst_validtype_expr (tree t
, tree args
, tree in_decl
)
1059 tree r
= tsubst (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
);
1060 return finish_validtype_expr (r
);
1063 // Substitute ARGS into the constexpr expression T.
1065 tsubst_constexpr_expr (tree t
, tree args
, tree in_decl
)
1067 tree r
= tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1068 return finish_constexpr_expr (r
);
1071 // Substitute ARGS into the expr requirement T. Note that a requirement
1072 // node is instantiated from a non-reduced context (e.g., static_assert).
1074 tsubst_expr_req (tree t
, tree args
, tree in_decl
)
1077 for (tree l
= TREE_OPERAND (t
, 0); l
; l
= TREE_CHAIN (l
))
1079 tree e
= tsubst_expr (TREE_VALUE (l
), args
, tf_none
, in_decl
, false);
1080 r
= conjoin_requirements (r
, e
);
1085 // Substitute ARGS into the type requirement T. Note that a requirement
1086 // node is instantiated from a non-reduced context (e.g., static_assert).
1088 tsubst_type_req (tree t
, tree args
, tree in_decl
)
1090 return tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1093 // Substitute ARGS into the nested requirement T. Note that a requirement
1094 // node is instantiated from a non-reduced context (e.g., static_assert).
1096 tsubst_nested_req (tree t
, tree args
, tree in_decl
)
1098 return tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1101 // Substitute the template arguments ARGS into the requirement
1102 // expression REQS. Errors resulting from substitution are not
1105 instantiate_requirements (tree reqs
, tree args
)
1107 return tsubst_expr (reqs
, args
, tf_none
, NULL_TREE
, false);
1110 // -------------------------------------------------------------------------- //
1111 // Constraint Satisfaction
1113 // The following functions are responsible for the instantiation and
1114 // evaluation of constraints.
1117 // Returns true if the requirements expression REQS is satisfied
1118 // and false otherwise. The requirements are checked by simply
1119 // evaluating REQS as a constant expression.
1121 check_requirements (tree reqs
)
1123 // Reduce any remaining TRAIT_EXPR nodes before evaluating.
1124 reqs
= fold_non_dependent_expr (reqs
);
1126 // Requirements are satisfied when REQS evaluates to true.
1127 return cxx_constant_value (reqs
) == boolean_true_node
;
1130 // Returns true if the requirements expression REQS is satisfied
1131 // and false otherwise. The requirements are checked by first
1132 // instantiating REQS and then evaluating it as a constant expression.
1134 check_requirements (tree reqs
, tree args
)
1136 // If any arguments are dependent, then we can't check the
1137 // requirements. Just return true.
1138 if (uses_template_parms (args
))
1141 // Instantiate and evaluate the requirements.
1142 reqs
= instantiate_requirements (reqs
, args
);
1143 if (reqs
== error_mark_node
)
1145 return check_requirements (reqs
);
1149 // Check the instantiated declaration constraints.
1151 check_constraints (tree cinfo
)
1153 // No constraints? Satisfied.
1156 return check_requirements (CI_REQUIREMENTS (cinfo
));
1159 // Check the constraints in CINFO against the given ARGS, returning
1160 // true when the constraints are satisfied and false otherwise.
1162 check_constraints (tree cinfo
, tree args
)
1164 // No constraints? Satisfied.
1168 // Dependent arguments? Satisfied. They won't reduce to true or false.
1169 if (uses_template_parms (args
))
1172 return check_requirements (CI_REQUIREMENTS (cinfo
), args
);
1175 // Check the constraints of the declaration or type T, against
1176 // the specified arguments. Returns true if the constraints are
1177 // satisfied and false otherwise.
1179 check_template_constraints (tree t
, tree args
)
1181 return check_constraints (DECL_CONSTRAINTS (t
), args
);
1184 // -------------------------------------------------------------------------- //
1185 // Constraint Relations
1187 // Interfaces for determining equivalency and ordering of constraints.
1189 // Returns true when A and B are equivalent constraints.
1191 equivalent_constraints (tree a
, tree b
)
1196 return subsumes (a
, b
) && subsumes (b
, a
);
1199 // Returns true if the template declarations A and B have equivalent
1200 // constraints. This is the case when A's constraints subsume B's and
1201 // when B's also constrain A's.
1203 equivalently_constrained (tree a
, tree b
)
1205 gcc_assert (TREE_CODE (a
) == TREE_CODE (b
));
1206 return equivalent_constraints (DECL_CONSTRAINTS (a
), DECL_CONSTRAINTS (b
));
1209 // Returns true when the A contains more atomic properties than B.
1211 more_constraints (tree a
, tree b
)
1213 return subsumes (a
, b
);
1216 // Returns true when the template declaration A's constraints subsume
1217 // those of the template declaration B.
1219 more_constrained (tree a
, tree b
)
1221 gcc_assert (TREE_CODE (a
) == TREE_CODE (b
));
1222 return more_constraints (DECL_CONSTRAINTS (a
), DECL_CONSTRAINTS (b
));
1226 // -------------------------------------------------------------------------- //
1227 // Constraint Diagnostics
1231 void diagnose_node (location_t
, tree
, tree
);
1233 // Diagnose a constraint failure for type trait expressions.
1235 diagnose_trait (location_t loc
, tree t
, tree args
)
1237 if (check_requirements (t
, args
))
1240 ++processing_template_decl
;
1241 tree subst
= instantiate_requirements (t
, args
);
1242 --processing_template_decl
;
1244 if (subst
== error_mark_node
)
1246 inform (input_location
, " substitution failure in %qE", t
);
1250 tree t1
= TRAIT_EXPR_TYPE1 (subst
);
1251 tree t2
= TRAIT_EXPR_TYPE2 (subst
);
1252 switch (TRAIT_EXPR_KIND (t
))
1254 case CPTK_HAS_NOTHROW_ASSIGN
:
1255 inform (loc
, " %qT is not nothrow assignable", t1
);
1257 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
1258 inform (loc
, " %qT is not nothrow constructible", t1
);
1260 case CPTK_HAS_NOTHROW_COPY
:
1261 inform (loc
, " %qT is not nothrow copyable", t1
);
1263 case CPTK_HAS_TRIVIAL_ASSIGN
:
1264 inform (loc
, " %qT is not trivially assignable", t1
);
1266 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
1267 inform (loc
, " %qT is not trivially constructible", t1
);
1269 case CPTK_HAS_TRIVIAL_COPY
:
1270 inform (loc
, " %qT is not trivially copyable", t1
);
1272 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
1273 inform (loc
, " %qT is not trivially destructible", t1
);
1275 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
1276 inform (loc
, " %qT does not have a virtual destructor", t1
);
1278 case CPTK_IS_ABSTRACT
:
1279 inform (loc
, " %qT is not an abstract class", t1
);
1281 case CPTK_IS_BASE_OF
:
1282 inform (loc
, " %qT is not a base of %qT", t1
, t2
);
1285 inform (loc
, " %qT is not a class", t1
);
1287 case CPTK_IS_CONVERTIBLE_TO
:
1288 inform (loc
, " %qT is not convertible to %qT", t1
, t2
);
1291 inform (loc
, " %qT is not an empty class", t1
);
1294 inform (loc
, " %qT is not an enum", t1
);
1297 inform (loc
, " %qT is not a final class", t1
);
1299 case CPTK_IS_LITERAL_TYPE
:
1300 inform (loc
, " %qT is not a literal type", t1
);
1303 inform (loc
, " %qT is not a POD type", t1
);
1305 case CPTK_IS_POLYMORPHIC
:
1306 inform (loc
, " %qT is not a polymorphic type", t1
);
1308 case CPTK_IS_SAME_AS
:
1309 inform (loc
, " %qT is not the same as %qT", t1
, t2
);
1311 case CPTK_IS_STD_LAYOUT
:
1312 inform (loc
, " %qT is not an standard layout type", t1
);
1314 case CPTK_IS_TRIVIAL
:
1315 inform (loc
, " %qT is not a trivial type", t1
);
1318 inform (loc
, " %qT is not a union", t1
);
1325 // Diagnose a failed concept check in concept indicated by T, where
1326 // T is the result of resolve_constraint_check. Recursively analyze
1327 // the nested requiremets for details.
1329 diagnose_check (location_t loc
, tree t
, tree args
)
1331 tree fn
= TREE_VALUE (t
);
1332 tree targs
= TREE_PURPOSE (t
);
1333 tree body
= DECL_SAVED_TREE (fn
);
1337 inform (loc
, " failure in constraint %q#D", DECL_TI_TEMPLATE (fn
));
1339 // Perform a mini-reduction on the constraint.
1340 if (TREE_CODE (body
) == BIND_EXPR
)
1341 body
= BIND_EXPR_BODY (body
);
1342 if (TREE_CODE (body
) == RETURN_EXPR
)
1343 body
= TREE_OPERAND (body
, 0);
1345 // Locally instantiate the body with the call's template args,
1346 // and recursively diagnose.
1347 ++processing_template_decl
;
1348 body
= instantiate_requirements (body
, targs
);
1349 --processing_template_decl
;
1351 diagnose_node (loc
, body
, args
);
1354 // Diagnose constraint failures from the call expression T.
1356 diagnose_call (location_t loc
, tree t
, tree args
)
1358 if (check_requirements (t
, args
))
1361 // If this is a concept, we're going to recurse.
1362 // If it's just a call, then we can emit a simple message.
1363 if (tree check
= resolve_constraint_check (t
))
1364 diagnose_check (loc
, check
, args
);
1366 inform (loc
, " %qE evaluated to false", t
);
1369 // Diagnose specific constraint failures.
1371 diagnose_requires (location_t loc
, tree t
, tree args
)
1373 if (check_requirements (t
, args
))
1376 ++processing_template_decl
;
1377 tree subst
= instantiate_requirements (t
, args
);
1378 --processing_template_decl
;
1380 // Print the header for the requires expression.
1381 tree parms
= TREE_OPERAND (subst
, 0);
1382 if (!VOID_TYPE_P (TREE_VALUE (parms
)))
1383 inform (loc
, " requiring syntax with values %Z", TREE_OPERAND (subst
, 0));
1385 // Create a new local specialization binding for the arguments.
1386 // This lets us instantiate sub-expressions separately from the
1388 local_specialization_stack locals
;
1389 declare_local_parms (TREE_OPERAND (t
, 0), TREE_OPERAND (subst
, 0));
1391 // Iterate over the sub-requirements and try instantiating each.
1392 for (tree l
= TREE_OPERAND (t
, 1); l
; l
= TREE_CHAIN (l
))
1393 diagnose_node (loc
, TREE_VALUE (l
), args
);
1397 diagnose_validexpr (location_t loc
, tree t
, tree args
)
1399 if (check_requirements (t
, args
))
1401 inform (loc
, " %qE is not a valid expression", TREE_OPERAND (t
, 0));
1405 diagnose_validtype (location_t loc
, tree t
, tree args
)
1407 if (check_requirements (t
, args
))
1410 // Substitute into the qualified name.
1411 tree name
= TREE_OPERAND (t
, 0);
1412 if (tree cxt
= TYPE_CONTEXT (name
))
1414 tree id
= TYPE_IDENTIFIER (name
);
1415 cxt
= tsubst (cxt
, args
, tf_none
, NULL_TREE
);
1416 name
= build_qualified_name (NULL_TREE
, cxt
, id
, false);
1417 inform (loc
, " %qE does not name a valid type", name
);
1421 inform (loc
, " %qT does not name a valid type", name
);
1426 diagnose_constexpr (location_t loc
, tree t
, tree args
)
1428 if (check_requirements (t
, args
))
1430 inform (loc
, " %qE is not a constant expression", TREE_OPERAND (t
, 0));
1434 diagnose_noexcept (location_t loc
, tree t
, tree args
)
1436 if (check_requirements (t
, args
))
1438 inform (loc
, " %qE propagates exceptions", TREE_OPERAND (t
, 0));
1441 // Diagnose a constraint failure in the expression T.
1443 diagnose_other (location_t loc
, tree t
, tree args
)
1445 if (check_requirements (t
, args
))
1447 inform (loc
, " %qE evaluated to false", t
);
1450 // Diagnose a constraint failure in the subtree T.
1452 diagnose_node (location_t loc
, tree t
, tree args
)
1454 switch (TREE_CODE (t
))
1456 case TRUTH_ANDIF_EXPR
:
1457 diagnose_node (loc
, TREE_OPERAND (t
, 0), args
);
1458 diagnose_node (loc
, TREE_OPERAND (t
, 1), args
);
1461 case TRUTH_ORIF_EXPR
:
1462 // TODO: Design better diagnostics for dijunctions.
1463 diagnose_other (loc
, t
, args
);
1467 diagnose_trait (loc
, t
, args
);
1471 diagnose_call (loc
, t
, args
);
1475 diagnose_requires (loc
, t
, args
);
1478 case VALIDEXPR_EXPR
:
1479 diagnose_validexpr (loc
, t
, args
);
1482 case VALIDTYPE_EXPR
:
1483 diagnose_validtype (loc
, t
, args
);
1486 case CONSTEXPR_EXPR
:
1487 diagnose_constexpr (loc
, t
, args
);
1491 diagnose_noexcept (loc
, t
, args
);
1495 diagnose_other (loc
, t
, args
);
1500 // Diagnose a constraint failure in the requirements expression REQS.
1502 diagnose_requirements (location_t loc
, tree reqs
, tree args
)
1504 diagnose_node (loc
, reqs
, args
);
1507 // Create a tree node representing the substitution of ARGS into
1508 // the parameters of TMPL. The resulting structure is passed as an
1509 // for diagnosing substitutions.
1511 make_subst (tree tmpl
, tree args
)
1513 tree subst
= tree_cons (NULL_TREE
, args
, NULL_TREE
);
1514 TREE_TYPE (subst
) = DECL_TEMPLATE_PARMS (tmpl
);
1520 // Emit diagnostics detailing the failure ARGS to satisfy the constraints
1521 // of the template declaration, TMPL.
1523 diagnose_constraints (location_t loc
, tree tmpl
, tree args
)
1525 inform (loc
, " constraints not satisfied %S", make_subst (tmpl
, args
));
1527 // Diagnose the constraints by recursively decomposing and
1528 // evaluating the template requirements.
1529 tree reqs
= CI_SPELLING (DECL_CONSTRAINTS (tmpl
));
1530 diagnose_requirements (loc
, reqs
, args
);