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 a template-id expression. If
185 // it's a call to a base-link, its function(s) should be a
186 // template-id expressson. If this is not a template-id, then it
187 // cannot be a concept-check.
188 tree target
= CALL_EXPR_FN (call
);
189 if (BASELINK_P (target
))
190 target
= BASELINK_FUNCTIONS (target
);
191 if (TREE_CODE (target
) != TEMPLATE_ID_EXPR
)
194 // Get the overload set and template arguments and try to
195 // resolve the target.
196 tree ovl
= TREE_OPERAND (target
, 0);
197 tree args
= TREE_OPERAND (target
, 1);
198 return resolve_constraint_check (ovl
, args
);
201 // Given a call expression to a concept, possibly including a placeholder
202 // argument, deduce the concept being checked and the prototype paraemter.
203 // Returns true if the constraint and prototype can be deduced and false
204 // otherwise. Note that the CHECK and PROTO arguments are set to NULL_TREE
205 // if this returns false.
207 deduce_constrained_parameter (tree call
, tree
& check
, tree
& proto
)
209 // Resolve the constraint check to deduce the declared parameter.
210 if (tree info
= resolve_constraint_check (call
))
212 // Get function and argument from the resolved check expression and
213 // the prototype parameter. Note that if the first argument was a
214 // pack, we need to extract the first element ot get the prototype.
215 check
= TREE_VALUE (info
);
216 tree arg
= TREE_VEC_ELT (TREE_PURPOSE (info
), 0);
217 if (ARGUMENT_PACK_P (arg
))
218 arg
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg
), 0);
219 proto
= TREE_TYPE (arg
);
222 check
= proto
= NULL_TREE
;
226 // -------------------------------------------------------------------------- //
227 // Requirement Reduction
229 // Reduces a template requirement to a logical formula written in terms of
230 // atomic propositions, returing the new expression. If the expression cannot
231 // be reduced, a NULL_TREE is returned, indicating failure to reduce the
232 // original requirment.
237 static tree
reduce_node (tree
);
238 static tree
reduce_expr (tree
);
239 static tree
reduce_stmt (tree
);
240 static tree
reduce_decl (tree
);
241 static tree
reduce_misc (tree
);
243 static tree
reduce_logical (tree
);
244 static tree
reduce_call (tree
);
245 static tree
reduce_requires (tree
);
246 static tree
reduce_expr_req (tree
);
247 static tree
reduce_type_req (tree
);
248 static tree
reduce_nested_req (tree
);
249 static tree
reduce_template_id (tree
);
250 static tree
reduce_stmt_list (tree
);
252 // Reduce the requirement T into a logical formula written in terms of
253 // atomic propositions.
257 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
263 return reduce_expr (t
);
266 return reduce_stmt (t
);
268 case tcc_declaration
:
269 return reduce_decl (t
);
271 case tcc_exceptional
:
272 return reduce_misc (t
);
274 // These kinds of expressions are atomic.
286 // Reduction rules for the expression node T.
290 switch (TREE_CODE (t
))
292 case TRUTH_ANDIF_EXPR
:
293 case TRUTH_ORIF_EXPR
:
294 return reduce_logical (t
);
297 return reduce_call (t
);
300 return reduce_requires (t
);
303 return reduce_expr_req (t
);
306 return reduce_type_req (t
);
309 return reduce_nested_req (t
);
311 case TEMPLATE_ID_EXPR
:
312 return reduce_template_id (t
);
315 return reduce_node (TREE_VALUE (TREE_OPERAND (t
, 0)));
318 return reduce_node (BIND_EXPR_BODY (t
));
324 // Everything else is atomic.
331 // Reduction rules for the statement T.
335 switch (TREE_CODE (t
))
337 // Reduce the returned expression.
339 return reduce_node (TREE_OPERAND (t
, 0));
341 // These statements do not introduce propositions
342 // in the constraints language. Do not recurse.
353 // Reduction rules for the declaration T.
357 switch (TREE_CODE (t
))
359 // References to var decls are atomic.
369 // Reduction rules for the node T.
373 switch (TREE_CODE (t
))
375 // Errors and traits are atomic.
381 return reduce_stmt_list (t
);
389 // Reduction rules for the binary logical expression T (&& and ||).
391 // Generate a new expression from the reduced operands. If either operand
392 // cannot be reduced, then the resulting expression is null.
394 reduce_logical (tree t
)
396 tree l
= reduce_expr (TREE_OPERAND (t
, 0));
397 tree r
= reduce_expr (TREE_OPERAND (t
, 1));
401 TREE_OPERAND (t
, 0) = l
;
402 TREE_OPERAND (t
, 1) = r
;
409 // Reduction rules for the call expression T.
411 // If T is a call to a constraint instantiate its definition and
412 // recursively reduce its returned expression.
416 // Is the function call actually a constraint check?
417 tree check
= resolve_constraint_check (t
);
421 tree fn
= TREE_VALUE (check
);
422 tree args
= TREE_PURPOSE (check
);
424 // Reduce the body of the function into the constriants language.
425 tree body
= reduce_requirements (DECL_SAVED_TREE (fn
));
428 error ("could not inline requirements from %qD", fn
);
429 return error_mark_node
;
432 // Instantiate the reduced results using the deduced args.
433 tree result
= instantiate_requirements (body
, args
);
434 if (result
== error_mark_node
)
436 error ("could not instantiate requirements from %qD", fn
);
437 return error_mark_node
;
442 // Reduction rules for the template-id T.
444 // It turns out that we often get requirements being written like this:
446 // template<typename T>
450 // Where Foo<T> should actually be written as Foo<T>(). Generate an
451 // error and suggest the improved writing.
453 reduce_template_id (tree t
)
455 vec
<tree
, va_gc
>* args
= NULL
;
456 tree c
= finish_call_expr (t
, &args
, true, false, 0);
457 error_at (EXPR_LOCATION (t
), "invalid requirement");
458 inform (EXPR_LOCATION (t
), "did you mean %qE", c
);
463 // Reduce an expression requirement as a conjunction of its
464 // individual constraints.
466 reduce_expr_req (tree t
)
469 for (tree l
= TREE_OPERAND (t
, 0); l
; l
= TREE_CHAIN (l
))
470 r
= conjoin_requirements (r
, reduce_expr (TREE_VALUE (l
)));
474 // Reduce a type requirement by returing its underlying
477 reduce_type_req (tree t
)
479 return TREE_OPERAND (t
, 0);
482 // Reduce a nested requireemnt by returing its only operand.
484 reduce_nested_req (tree t
)
486 return TREE_OPERAND (t
, 0);
489 // Reduce a requires expr by reducing each requirement in turn,
490 // rewriting the list of requirements so that we end up with a
491 // list of expressions, some of which may be conjunctions.
493 reduce_requires (tree t
)
495 for (tree l
= TREE_OPERAND (t
, 1); l
; l
= TREE_CHAIN (l
))
496 TREE_VALUE (l
) = reduce_expr (TREE_VALUE (l
));
500 // Reduction rules for the statement list STMTS.
502 // Recursively reduce each statement in the list, concatenating each
503 // reduced result into a conjunction of requirements.
505 // A constexpr function may include statements other than a return
506 // statement. The primary purpose of these rules is to filter those
507 // non-return statements from the constraints language.
509 reduce_stmt_list (tree stmts
)
511 tree lhs
= NULL_TREE
;
512 tree_stmt_iterator i
= tsi_start (stmts
);
513 while (!tsi_end_p (i
))
515 if (tree rhs
= reduce_node (tsi_stmt (i
)))
516 lhs
= conjoin_requirements (lhs
, rhs
);
524 // Reduce the requirement REQS into a logical formula written in terms of
525 // atomic propositions.
527 reduce_requirements (tree reqs
)
529 return reduce_node (reqs
);
532 // -------------------------------------------------------------------------- //
533 // Constraint Semantic Processing
535 // The following functions are called by the parser and substitution rules
536 // to create and evaluate constraint-related nodes.
538 // Create a constraint-info node from the specified requirements.
540 make_constraints (tree reqs
)
542 // No requirements == no constraints
546 // Reduce the requirements into a single expression of constraints.
547 tree expr
= reduce_requirements (reqs
);
548 if (expr
== error_mark_node
)
549 return error_mark_node
;
551 // Decompose those expressions into lists of lists of atomic
553 tree assume
= decompose_assumptions (expr
);
555 // Build the constraint info.
556 tree_constraint_info
*cinfo
=
557 (tree_constraint_info
*)make_node (CONSTRAINT_INFO
);
558 cinfo
->spelling
= reqs
;
559 cinfo
->requirements
= expr
;
560 cinfo
->assumptions
= assume
;
564 // Returns the template constraints of declaration T. If T is not a
565 // template, this return NULL_TREE. Note that T must be non-null.
567 get_constraints (tree t
)
569 gcc_assert (DECL_P (t
));
570 if (TREE_CODE (t
) != TEMPLATE_DECL
)
572 if (!DECL_TEMPLATE_INFO (t
))
575 return DECL_CONSTRAINTS (DECL_TI_TEMPLATE (t
));
577 return DECL_CONSTRAINTS (t
);
580 // Returns a conjunction of shorthand requirements for the template
581 // parameter list PARMS. Note that the requirements are stored in
582 // the TYPE of each tree node.
584 get_shorthand_requirements (tree parms
)
586 tree reqs
= NULL_TREE
;
587 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
588 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
590 tree parm
= TREE_VEC_ELT (parms
, i
);
591 reqs
= conjoin_requirements(reqs
, TREE_TYPE (parm
));
596 // Finish the template requirement, EXPR, by translating it into
597 // a constraint information record.
599 finish_template_requirements (tree expr
)
601 if (expr
== error_mark_node
)
604 return make_constraints (expr
);
608 build_requires_expr (tree parms
, tree reqs
)
610 // Modify the declared parameters by removing their context (so they
611 // don't refer to the enclosing scope), and marking them constant (so
612 // we can actually check constexpr properties).
613 for (tree p
= parms
; p
&& !VOID_TYPE_P (TREE_VALUE (p
)); p
= TREE_CHAIN (p
))
615 tree parm
= TREE_VALUE (p
);
616 DECL_CONTEXT (parm
) = NULL_TREE
;
617 TREE_CONSTANT (parm
) = true;
621 tree r
= build_min (REQUIRES_EXPR
, boolean_type_node
, parms
, reqs
);
622 TREE_SIDE_EFFECTS (r
) = false;
623 TREE_CONSTANT (r
) = true;
627 // Evaluate an instantiatd requires expr, returning the truth node
628 // only when all sub-requirements have evaluated to true.
630 eval_requires_expr (tree reqs
)
632 for (tree t
= reqs
; t
; t
= TREE_CHAIN (t
)) {
633 tree r
= TREE_VALUE (t
);
634 r
= fold_non_dependent_expr (r
);
635 r
= maybe_constant_value (r
);
636 if (r
!= boolean_true_node
)
637 return boolean_false_node
;
639 return boolean_true_node
;
642 // Finish a requires expression, returning a node wrapping the parameters,
643 // PARMS, and the list of requirements REQS.
645 finish_requires_expr (tree parms
, tree reqs
)
647 if (processing_template_decl
)
648 return build_requires_expr (parms
, reqs
);
650 return eval_requires_expr (reqs
);
653 // Construct a unary expression that evaluates properties of the
654 // expression or type T, and has a boolean result type.
656 build_check_expr (tree_code c
, tree t
)
658 tree r
= build_min (c
, boolean_type_node
, t
);
659 TREE_SIDE_EFFECTS (r
) = false;
660 TREE_READONLY (r
) = true;
661 TREE_CONSTANT (r
) = true;
665 // Finish a syntax requirement, constructing a list embodying a sequence
666 // of checks for the validity of EXPR and TYPE, the convertibility of
667 // EXPR to TYPE, and the expression properties specified in SPECS.
669 finish_expr_requirement (tree expr
, tree type
, tree specs
)
671 gcc_assert (processing_template_decl
);
673 // Build a list of checks, starting with the valid expression.
674 tree result
= tree_cons (NULL_TREE
, finish_validexpr_expr (expr
), NULL_TREE
);
676 // If a type requirement was provided, build the result type checks.
679 // If the type is dependent, ensure that it can be validly
682 // NOTE: We can also disregard checks that result in the template
684 if (dependent_type_p (type
))
686 tree treq
= finish_type_requirement (type
);
687 result
= tree_cons (NULL_TREE
, treq
, result
);
690 // Ensure that the result of the expression can be converted to
692 tree decl_type
= finish_decltype_type (expr
, false, tf_none
);
693 tree creq
= finish_trait_expr (CPTK_IS_CONVERTIBLE_TO
, decl_type
, type
);
694 result
= tree_cons (NULL_TREE
, creq
, result
);
697 // If constraint specifiers are present, make them part of the
698 // list of constraints.
701 TREE_CHAIN (tree_last (specs
)) = result
;
705 // Finally, construct the syntactic requirement.
706 return build_check_expr (EXPR_REQ
, nreverse (result
));
709 // Finish a simple syntax requirement, returning a node representing
710 // a check that EXPR is a valid expression.
712 finish_expr_requirement (tree expr
)
714 gcc_assert (processing_template_decl
);
715 tree req
= finish_validexpr_expr (expr
);
716 tree reqs
= tree_cons (NULL_TREE
, req
, NULL_TREE
);
717 return build_check_expr (EXPR_REQ
, reqs
);
720 // Finish a type requirement, returning a node representing a check
721 // that TYPE will result in a valid type when instantiated.
723 finish_type_requirement (tree type
)
725 gcc_assert (processing_template_decl
);
726 tree req
= finish_validtype_expr (type
);
727 return build_check_expr (TYPE_REQ
, req
);
731 finish_nested_requirement (tree expr
)
733 gcc_assert (processing_template_decl
);
734 return build_check_expr (NESTED_REQ
, expr
);
737 // Finish a constexpr requirement, returning a node representing a
738 // check that EXPR, when instantiated, may be evaluated at compile time.
740 finish_constexpr_requirement (tree expr
)
742 gcc_assert (processing_template_decl
);
743 return finish_constexpr_expr (expr
);
746 // Finish the noexcept requirement by constructing a noexcept
747 // expression evaluating EXPR.
749 finish_noexcept_requirement (tree expr
)
751 gcc_assert (processing_template_decl
);
752 return finish_noexcept_expr (expr
, tf_none
);
755 // Returns the true or false node depending on the truth value of B.
759 return b
? boolean_true_node
: boolean_false_node
;
762 // Returns a finished validexpr-expr. Returns the true or false node
763 // depending on whether EXPR denotes a valid expression. This is the case
764 // when the expression has been successfully type checked.
766 // When processing a template declaration, the result is an expression
767 // representing the check.
769 finish_validexpr_expr (tree expr
)
771 if (processing_template_decl
)
772 return build_check_expr (VALIDEXPR_EXPR
, expr
);
773 return truth_node (expr
&& expr
!= error_mark_node
);
776 // Returns a finished validtype-expr. Returns the true or false node
777 // depending on whether T denotes a valid type name.
779 // When processing a template declaration, the result is an expression
780 // representing the check.
782 // FIXME: Semantics need to be aligned with the new version of the
783 // specificaiton (i.e., we must be able to invent a function and
784 // perform argument deduction against it).
786 finish_validtype_expr (tree type
)
790 sorry ("%<auto%< not supported in result type constraints\n");
791 return error_mark_node
;
794 if (processing_template_decl
)
795 return build_check_expr (VALIDTYPE_EXPR
, type
);
796 return truth_node (type
&& TYPE_P (type
));
799 // Returns a finished constexpr-expr. Returns the true or false node
800 // depending on whether the expression T may be evaluated at compile
803 // When processing a template declaration, the result is an expression
804 // representing the check.
806 finish_constexpr_expr (tree expr
)
808 if (processing_template_decl
)
809 return build_check_expr (CONSTEXPR_EXPR
, expr
);
811 // TODO: Actually check that the expression can be constexpr
814 // return truth_node (potential_constant_expression (expr));
815 sorry ("constexpr requirement");
819 // Check that a constrained friend declaration function declaration,
820 // FN, is admissable. This is the case only when the declaration depends
821 // on template parameters and does not declare a specialization.
823 check_constrained_friend (tree fn
, tree reqs
)
825 if (fn
== error_mark_node
)
827 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
829 // If there are not constraints, this cannot be an error.
833 // Constrained friend functions that don't depend on template
834 // arguments are effectively meaningless.
835 tree parms
= DECL_ARGUMENTS (fn
);
836 tree result
= TREE_TYPE (TREE_TYPE (fn
));
837 if (!(parms
&& uses_template_parms (parms
)) && !uses_template_parms (result
))
839 error ("constrained friend does not depend on template parameters");
845 // Build a new call expression, but don't actually generate a new
846 // function call. We just want the tree, not the semantics.
848 build_call_check (tree id
)
850 ++processing_template_decl
;
851 vec
<tree
, va_gc
> *fargs
= make_tree_vector();
852 tree call
= finish_call_expr (id
, &fargs
, false, false, tf_none
);
853 --processing_template_decl
;
858 // Construct a concept check for the given TARGET. The target may be
859 // an overload set or a baselink referring to an overload set. Template
860 // arguments to the target are given by ARG and REST. If the target is
861 // a function (overload set or baselink reffering to an overload set),
862 // then ths builds the call expression TARGET<ARG, REST>(). If REST is
863 // NULL_TREE, then the resulting check is just TARGET<ARG>().
865 // TODO: Allow TARGET to be a variable concept.
867 build_concept_check (tree target
, tree arg
, tree rest
)
869 gcc_assert (rest
? TREE_CODE (rest
) == TREE_VEC
: true);
871 // Build a template-id that acts as the call target using TARGET as
872 // the template and ARG as the only explicit argument.
873 int n
= rest
? TREE_VEC_LENGTH (rest
) : 0;
874 tree targs
= make_tree_vec (n
+ 1);
875 TREE_VEC_ELT (targs
, 0) = arg
;
877 for (int i
= 0; i
< n
; ++i
)
878 TREE_VEC_ELT (targs
, i
+ 1) = TREE_VEC_ELT (rest
, i
);
879 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, n
+ 1);
880 tree id
= lookup_template_function (target
, targs
);
881 return build_call_check (id
);
884 // Returns a TYPE_DECL that contains sufficient information to build
885 // a template parameter of the same kind as PROTO and constrained
886 // by the concept declaration FN. PROTO is saved as the initializer of
887 // the new type decl, and the constraining function is saved in
890 // If specified ARGS provides additional arguments to the constraint
891 // check. These are stored in the DECL_SIZE field.
893 build_constrained_parameter (tree fn
, tree proto
, tree args
)
895 tree name
= DECL_NAME (fn
);
896 tree type
= TREE_TYPE (proto
);
897 tree decl
= build_decl (input_location
, TYPE_DECL
, name
, type
);
898 DECL_INITIAL (decl
) = proto
; // Describing parameter
899 DECL_SIZE_UNIT (decl
) = fn
; // Constraining function declaration
900 DECL_SIZE (decl
) = args
; // Extra template arguments.
904 // Create a requirement expression for the given DECL that evaluates the
905 // requirements specified by CONSTR, a TYPE_DECL that contains all the
906 // information necessary to build the requirements (see finish_concept_name
907 // for the layout of that TYPE_DECL).
909 // Note that the constraints are neither reduced nor decomposed. That is
910 // done only after the requires clause has been parsed (or not).
912 finish_shorthand_requirement (tree decl
, tree constr
)
914 // No requirements means no constraints.
918 tree proto
= DECL_INITIAL (constr
); // The prototype declaration
919 tree con
= DECL_SIZE_UNIT (constr
); // The concept declaration
920 tree args
= DECL_SIZE (constr
); // Extra template arguments
922 // If the parameter declaration is variadic, but the concept is not
923 // then we need to apply the concept to every element in the pack.
924 bool is_proto_pack
= template_parameter_pack_p (proto
);
925 bool is_decl_pack
= template_parameter_pack_p (decl
);
926 bool apply_to_all_p
= is_decl_pack
&& !is_proto_pack
;
928 // Get the argument and overload used for the requirement. Adjust
929 // if we're going to expand later.
930 tree arg
= template_parm_to_arg (build_tree_list (NULL_TREE
, decl
));
932 arg
= PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg
), 0));
934 // Build the concept check. If it the constraint needs to be applied
935 // to all elements of the parameter pack, then expand make the constraint
937 tree ovl
= build_overload (DECL_TI_TEMPLATE (con
), NULL_TREE
);
938 tree check
= build_concept_check (ovl
, arg
, args
);
941 check
= make_pack_expansion (check
);
943 // Set the type to indicate that this expansion will get special
944 // treatment during instantiation.
946 // TODO: Maybe this should be a different kind of node... one that
947 // has all the same properties as a pack expansion, but has a definite
948 // expansion when instantiated as part of an expression.
950 // As of now, this is a hack.
951 TREE_TYPE (check
) = boolean_type_node
;
957 // -------------------------------------------------------------------------- //
958 // Substitution Rules
960 // The following functions implement substitution rules for constraints.
963 // In an unevaluated context, the substitution of parm decls are not
964 // properly chained during substitution. Do that here.
966 fix_local_parms (tree sparms
)
971 tree p
= TREE_CHAIN (sparms
);
973 while (p
&& TREE_VALUE (p
) != void_type_node
)
975 DECL_CHAIN (TREE_VALUE (q
)) = TREE_VALUE (p
);
982 // Register local specializations for each of tparm and the corresponding
983 // sparm. This is a helper function for tsubst_requires_expr.
985 declare_local_parms (tree tparms
, tree sparms
)
987 tree s
= TREE_VALUE (sparms
);
988 for (tree p
= tparms
; p
&& !VOID_TYPE_P (TREE_VALUE (p
)); p
= TREE_CHAIN (p
))
990 tree t
= TREE_VALUE (p
);
993 tree pack
= extract_fnparm_pack (t
, &s
);
994 register_local_specialization (pack
, t
);
998 register_local_specialization (s
, t
);
1004 // Substitute ARGS into the parameter list T, producing a sequence of
1005 // local parameters (variables) in the current scope.
1007 tsubst_local_parms (tree t
,
1009 tsubst_flags_t complain
,
1012 tree r
= fix_local_parms (tsubst (t
, args
, complain
, in_decl
));
1013 if (r
== error_mark_node
)
1014 return error_mark_node
;
1016 // Register the instantiated args as local parameters.
1018 declare_local_parms (t
, r
);
1023 // Substitute ARGS into the requirement body (list of requirements), T.
1025 tsubst_requirement_body (tree t
, tree args
, tree in_decl
)
1027 cp_unevaluated guard
;
1031 // If any substitutions fail, then this is equivalent to
1033 tree e
= tsubst_expr (TREE_VALUE (t
), args
, tf_none
, in_decl
, false);
1034 if (e
== error_mark_node
)
1035 e
= boolean_false_node
;
1036 r
= tree_cons (NULL_TREE
, e
, r
);
1043 // Substitute ARGS into the requires expression T.
1045 tsubst_requires_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
1047 local_specialization_stack stack
;
1048 tree p
= tsubst_local_parms (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
1049 tree r
= tsubst_requirement_body (TREE_OPERAND (t
, 1), args
, in_decl
);
1050 return finish_requires_expr (p
, r
);
1053 // Substitute ARGS into the valid-expr expression T.
1055 tsubst_validexpr_expr (tree t
, tree args
, tree in_decl
)
1057 tree r
= tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1058 return finish_validexpr_expr (r
);
1061 // Substitute ARGS into the valid-type expression T.
1063 tsubst_validtype_expr (tree t
, tree args
, tree in_decl
)
1065 tree r
= tsubst (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
);
1066 return finish_validtype_expr (r
);
1069 // Substitute ARGS into the constexpr expression T.
1071 tsubst_constexpr_expr (tree t
, tree args
, tree in_decl
)
1073 tree r
= tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1074 return finish_constexpr_expr (r
);
1077 // Substitute ARGS into the expr requirement T. Note that a requirement
1078 // node is instantiated from a non-reduced context (e.g., static_assert).
1080 tsubst_expr_req (tree t
, tree args
, tree in_decl
)
1083 for (tree l
= TREE_OPERAND (t
, 0); l
; l
= TREE_CHAIN (l
))
1085 tree e
= tsubst_expr (TREE_VALUE (l
), args
, tf_none
, in_decl
, false);
1086 r
= conjoin_requirements (r
, e
);
1091 // Substitute ARGS into the type requirement T. Note that a requirement
1092 // node is instantiated from a non-reduced context (e.g., static_assert).
1094 tsubst_type_req (tree t
, tree args
, tree in_decl
)
1096 return tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1099 // Substitute ARGS into the nested requirement T. Note that a requirement
1100 // node is instantiated from a non-reduced context (e.g., static_assert).
1102 tsubst_nested_req (tree t
, tree args
, tree in_decl
)
1104 return tsubst_expr (TREE_OPERAND (t
, 0), args
, tf_none
, in_decl
, false);
1107 // Substitute the template arguments ARGS into the requirement
1108 // expression REQS. Errors resulting from substitution are not
1111 instantiate_requirements (tree reqs
, tree args
)
1113 return tsubst_expr (reqs
, args
, tf_none
, NULL_TREE
, false);
1116 // -------------------------------------------------------------------------- //
1117 // Constraint Satisfaction
1119 // The following functions are responsible for the instantiation and
1120 // evaluation of constraints.
1123 // Returns true if the requirements expression REQS is satisfied
1124 // and false otherwise. The requirements are checked by simply
1125 // evaluating REQS as a constant expression.
1127 check_requirements (tree reqs
)
1129 // Reduce any remaining TRAIT_EXPR nodes before evaluating.
1130 reqs
= fold_non_dependent_expr (reqs
);
1132 // Requirements are satisfied when REQS evaluates to true.
1133 return cxx_constant_value (reqs
) == boolean_true_node
;
1136 // Returns true if the requirements expression REQS is satisfied
1137 // and false otherwise. The requirements are checked by first
1138 // instantiating REQS and then evaluating it as a constant expression.
1140 check_requirements (tree reqs
, tree args
)
1142 // If any arguments are dependent, then we can't check the
1143 // requirements. Just return true.
1144 if (uses_template_parms (args
))
1147 // Instantiate and evaluate the requirements.
1148 reqs
= instantiate_requirements (reqs
, args
);
1149 if (reqs
== error_mark_node
)
1151 return check_requirements (reqs
);
1155 // Check the instantiated declaration constraints.
1157 check_constraints (tree cinfo
)
1159 // No constraints? Satisfied.
1162 return check_requirements (CI_REQUIREMENTS (cinfo
));
1165 // Check the constraints in CINFO against the given ARGS, returning
1166 // true when the constraints are satisfied and false otherwise.
1168 check_constraints (tree cinfo
, tree args
)
1170 // No constraints? Satisfied.
1174 // Dependent arguments? Satisfied. They won't reduce to true or false.
1175 if (uses_template_parms (args
))
1178 return check_requirements (CI_REQUIREMENTS (cinfo
), args
);
1181 // Check the constraints of the declaration or type T, against
1182 // the specified arguments. Returns true if the constraints are
1183 // satisfied and false otherwise.
1185 check_template_constraints (tree t
, tree args
)
1187 return check_constraints (DECL_CONSTRAINTS (t
), args
);
1190 // -------------------------------------------------------------------------- //
1191 // Constraint Relations
1193 // Interfaces for determining equivalency and ordering of constraints.
1195 // Returns true when A and B are equivalent constraints.
1197 equivalent_constraints (tree a
, tree b
)
1202 return subsumes (a
, b
) && subsumes (b
, a
);
1205 // Returns true if the template declarations A and B have equivalent
1206 // constraints. This is the case when A's constraints subsume B's and
1207 // when B's also constrain A's.
1209 equivalently_constrained (tree a
, tree b
)
1211 gcc_assert (TREE_CODE (a
) == TREE_CODE (b
));
1212 return equivalent_constraints (DECL_CONSTRAINTS (a
), DECL_CONSTRAINTS (b
));
1215 // Returns true when the A contains more atomic properties than B.
1217 more_constraints (tree a
, tree b
)
1219 return subsumes (a
, b
);
1222 // Returns true when the template declaration A's constraints subsume
1223 // those of the template declaration B.
1225 more_constrained (tree a
, tree b
)
1227 gcc_assert (TREE_CODE (a
) == TREE_CODE (b
));
1228 return more_constraints (DECL_CONSTRAINTS (a
), DECL_CONSTRAINTS (b
));
1232 // -------------------------------------------------------------------------- //
1233 // Constraint Diagnostics
1237 void diagnose_node (location_t
, tree
, tree
);
1239 // Diagnose a constraint failure for type trait expressions.
1241 diagnose_trait (location_t loc
, tree t
, tree args
)
1243 if (check_requirements (t
, args
))
1246 ++processing_template_decl
;
1247 tree subst
= instantiate_requirements (t
, args
);
1248 --processing_template_decl
;
1250 if (subst
== error_mark_node
)
1252 inform (input_location
, " substitution failure in %qE", t
);
1256 tree t1
= TRAIT_EXPR_TYPE1 (subst
);
1257 tree t2
= TRAIT_EXPR_TYPE2 (subst
);
1258 switch (TRAIT_EXPR_KIND (t
))
1260 case CPTK_HAS_NOTHROW_ASSIGN
:
1261 inform (loc
, " %qT is not nothrow assignable", t1
);
1263 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
1264 inform (loc
, " %qT is not nothrow constructible", t1
);
1266 case CPTK_HAS_NOTHROW_COPY
:
1267 inform (loc
, " %qT is not nothrow copyable", t1
);
1269 case CPTK_HAS_TRIVIAL_ASSIGN
:
1270 inform (loc
, " %qT is not trivially assignable", t1
);
1272 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
1273 inform (loc
, " %qT is not trivially constructible", t1
);
1275 case CPTK_HAS_TRIVIAL_COPY
:
1276 inform (loc
, " %qT is not trivially copyable", t1
);
1278 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
1279 inform (loc
, " %qT is not trivially destructible", t1
);
1281 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
1282 inform (loc
, " %qT does not have a virtual destructor", t1
);
1284 case CPTK_IS_ABSTRACT
:
1285 inform (loc
, " %qT is not an abstract class", t1
);
1287 case CPTK_IS_BASE_OF
:
1288 inform (loc
, " %qT is not a base of %qT", t1
, t2
);
1291 inform (loc
, " %qT is not a class", t1
);
1293 case CPTK_IS_CONVERTIBLE_TO
:
1294 inform (loc
, " %qT is not convertible to %qT", t1
, t2
);
1297 inform (loc
, " %qT is not an empty class", t1
);
1300 inform (loc
, " %qT is not an enum", t1
);
1303 inform (loc
, " %qT is not a final class", t1
);
1305 case CPTK_IS_LITERAL_TYPE
:
1306 inform (loc
, " %qT is not a literal type", t1
);
1309 inform (loc
, " %qT is not a POD type", t1
);
1311 case CPTK_IS_POLYMORPHIC
:
1312 inform (loc
, " %qT is not a polymorphic type", t1
);
1314 case CPTK_IS_SAME_AS
:
1315 inform (loc
, " %qT is not the same as %qT", t1
, t2
);
1317 case CPTK_IS_STD_LAYOUT
:
1318 inform (loc
, " %qT is not an standard layout type", t1
);
1320 case CPTK_IS_TRIVIAL
:
1321 inform (loc
, " %qT is not a trivial type", t1
);
1324 inform (loc
, " %qT is not a union", t1
);
1331 // Diagnose a failed concept check in concept indicated by T, where
1332 // T is the result of resolve_constraint_check. Recursively analyze
1333 // the nested requiremets for details.
1335 diagnose_check (location_t loc
, tree t
, tree args
)
1337 tree fn
= TREE_VALUE (t
);
1338 tree targs
= TREE_PURPOSE (t
);
1339 tree body
= DECL_SAVED_TREE (fn
);
1343 inform (loc
, " failure in constraint %q#D", DECL_TI_TEMPLATE (fn
));
1345 // Perform a mini-reduction on the constraint.
1346 if (TREE_CODE (body
) == BIND_EXPR
)
1347 body
= BIND_EXPR_BODY (body
);
1348 if (TREE_CODE (body
) == RETURN_EXPR
)
1349 body
= TREE_OPERAND (body
, 0);
1351 // Locally instantiate the body with the call's template args,
1352 // and recursively diagnose.
1353 ++processing_template_decl
;
1354 body
= instantiate_requirements (body
, targs
);
1355 --processing_template_decl
;
1357 diagnose_node (loc
, body
, args
);
1360 // Diagnose constraint failures from the call expression T.
1362 diagnose_call (location_t loc
, tree t
, tree args
)
1364 if (check_requirements (t
, args
))
1367 // If this is a concept, we're going to recurse.
1368 // If it's just a call, then we can emit a simple message.
1369 if (tree check
= resolve_constraint_check (t
))
1370 diagnose_check (loc
, check
, args
);
1372 inform (loc
, " %qE evaluated to false", t
);
1375 // Diagnose specific constraint failures.
1377 diagnose_requires (location_t loc
, tree t
, tree args
)
1379 if (check_requirements (t
, args
))
1382 ++processing_template_decl
;
1383 tree subst
= instantiate_requirements (t
, args
);
1384 --processing_template_decl
;
1386 // Print the header for the requires expression.
1387 tree parms
= TREE_OPERAND (subst
, 0);
1388 if (!VOID_TYPE_P (TREE_VALUE (parms
)))
1389 inform (loc
, " requiring syntax with values %Z", TREE_OPERAND (subst
, 0));
1391 // Create a new local specialization binding for the arguments.
1392 // This lets us instantiate sub-expressions separately from the
1394 local_specialization_stack locals
;
1395 declare_local_parms (TREE_OPERAND (t
, 0), TREE_OPERAND (subst
, 0));
1397 // Iterate over the sub-requirements and try instantiating each.
1398 for (tree l
= TREE_OPERAND (t
, 1); l
; l
= TREE_CHAIN (l
))
1399 diagnose_node (loc
, TREE_VALUE (l
), args
);
1403 diagnose_validexpr (location_t loc
, tree t
, tree args
)
1405 if (check_requirements (t
, args
))
1407 inform (loc
, " %qE is not a valid expression", TREE_OPERAND (t
, 0));
1411 diagnose_validtype (location_t loc
, tree t
, tree args
)
1413 if (check_requirements (t
, args
))
1416 // Substitute into the qualified name.
1417 tree name
= TREE_OPERAND (t
, 0);
1418 if (tree cxt
= TYPE_CONTEXT (name
))
1420 tree id
= TYPE_IDENTIFIER (name
);
1421 cxt
= tsubst (cxt
, args
, tf_none
, NULL_TREE
);
1422 name
= build_qualified_name (NULL_TREE
, cxt
, id
, false);
1423 inform (loc
, " %qE does not name a valid type", name
);
1427 inform (loc
, " %qT does not name a valid type", name
);
1432 diagnose_constexpr (location_t loc
, tree t
, tree args
)
1434 if (check_requirements (t
, args
))
1436 inform (loc
, " %qE is not a constant expression", TREE_OPERAND (t
, 0));
1440 diagnose_noexcept (location_t loc
, tree t
, tree args
)
1442 if (check_requirements (t
, args
))
1444 inform (loc
, " %qE propagates exceptions", TREE_OPERAND (t
, 0));
1447 // Diagnose a constraint failure in the expression T.
1449 diagnose_other (location_t loc
, tree t
, tree args
)
1451 if (check_requirements (t
, args
))
1453 inform (loc
, " %qE evaluated to false", t
);
1456 // Diagnose a constraint failure in the subtree T.
1458 diagnose_node (location_t loc
, tree t
, tree args
)
1460 switch (TREE_CODE (t
))
1462 case TRUTH_ANDIF_EXPR
:
1463 diagnose_node (loc
, TREE_OPERAND (t
, 0), args
);
1464 diagnose_node (loc
, TREE_OPERAND (t
, 1), args
);
1467 case TRUTH_ORIF_EXPR
:
1468 // TODO: Design better diagnostics for dijunctions.
1469 diagnose_other (loc
, t
, args
);
1473 diagnose_trait (loc
, t
, args
);
1477 diagnose_call (loc
, t
, args
);
1481 diagnose_requires (loc
, t
, args
);
1484 case VALIDEXPR_EXPR
:
1485 diagnose_validexpr (loc
, t
, args
);
1488 case VALIDTYPE_EXPR
:
1489 diagnose_validtype (loc
, t
, args
);
1492 case CONSTEXPR_EXPR
:
1493 diagnose_constexpr (loc
, t
, args
);
1497 diagnose_noexcept (loc
, t
, args
);
1501 diagnose_other (loc
, t
, args
);
1506 // Diagnose a constraint failure in the requirements expression REQS.
1508 diagnose_requirements (location_t loc
, tree reqs
, tree args
)
1510 diagnose_node (loc
, reqs
, args
);
1513 // Create a tree node representing the substitution of ARGS into
1514 // the parameters of TMPL. The resulting structure is passed as an
1515 // for diagnosing substitutions.
1517 make_subst (tree tmpl
, tree args
)
1519 tree subst
= tree_cons (NULL_TREE
, args
, NULL_TREE
);
1520 TREE_TYPE (subst
) = DECL_TEMPLATE_PARMS (tmpl
);
1526 // Emit diagnostics detailing the failure ARGS to satisfy the constraints
1527 // of the template declaration, TMPL.
1529 diagnose_constraints (location_t loc
, tree tmpl
, tree args
)
1531 inform (loc
, " constraints not satisfied %S", make_subst (tmpl
, args
));
1533 // Diagnose the constraints by recursively decomposing and
1534 // evaluating the template requirements.
1535 tree reqs
= CI_SPELLING (DECL_CONSTRAINTS (tmpl
));
1536 diagnose_requirements (loc
, reqs
, args
);