1 /* Processing rules for constraints.
2 Copyright (C) 2013-2016 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"
28 #include "double-int.h"
35 #include "stringpool.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "cp-objcp-common.h"
43 #include "tree-inline.h"
46 #include "type-utils.h"
48 /*---------------------------------------------------------------------------
49 Operations on constraints
50 ---------------------------------------------------------------------------*/
52 /* Returns true if C is a constraint tree code. Note that ERROR_MARK
53 is a valid constraint. */
56 constraint_p (tree_code c
)
58 return (PRED_CONSTR
<= c
&& c
<= DISJ_CONSTR
) || c
== ERROR_MARK
;
61 /* Returns true if T is a constraint. Note that error_mark_node
62 is a valid constraint. */
67 return constraint_p (TREE_CODE (t
));
70 /* Make a predicate constraint from the given expression. */
73 make_predicate_constraint (tree expr
)
75 return build_nt (PRED_CONSTR
, expr
);
78 /* Returns the conjunction of two constraints A and B. Note that
79 conjoining a non-null constraint with NULL_TREE is an identity
80 operation. That is, for non-null A,
82 conjoin_constraints(a, NULL_TREE) == a
86 conjoin_constraints (NULL_TREE, a) == a
88 If both A and B are NULL_TREE, the result is also NULL_TREE. */
91 conjoin_constraints (tree a
, tree b
)
93 gcc_assert (a
? constraint_p (a
) : true);
94 gcc_assert (b
? constraint_p (b
) : true);
96 return b
? build_nt (CONJ_CONSTR
, a
, b
) : a
;
103 /* Transform the vector of expressions in the T into a conjunction
104 of requirements. T must be a TREE_VEC. */
107 conjoin_constraints (tree t
)
109 gcc_assert (TREE_CODE (t
) == TREE_VEC
);
111 for (int i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
112 r
= conjoin_constraints (r
, TREE_VEC_ELT (t
, i
));
116 /* Returns true if T is a call expression to a function
120 function_concept_check_p (tree t
)
122 gcc_assert (TREE_CODE (t
) == CALL_EXPR
);
123 tree fn
= CALL_EXPR_FN (t
);
124 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
125 && TREE_CODE (TREE_OPERAND (fn
, 0)) == OVERLOAD
)
127 tree f1
= get_first_fn (fn
);
128 if (TREE_CODE (f1
) == TEMPLATE_DECL
129 && DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (f1
)))
135 /*---------------------------------------------------------------------------
136 Resolution of qualified concept names
137 ---------------------------------------------------------------------------*/
139 /* This facility is used to resolve constraint checks from
140 requirement expressions. A constraint check is a call to
141 a function template declared with the keyword 'concept'.
143 The result of resolution is a pair (a TREE_LIST) whose value
144 is the matched declaration, and whose purpose contains the
145 coerced template arguments that can be substituted into the
148 // Given an overload set OVL, try to find a unique definition that can be
149 // instantiated by the template arguments ARGS.
151 // This function is not called for arbitrary call expressions. In particular,
152 // the call expression must be written with explicit template arguments
153 // and no function arguments. For example:
157 // If a single match is found, this returns a TREE_LIST whose VALUE
158 // is the constraint function (not the template), and its PURPOSE is
159 // the complete set of arguments substituted into the parameter list.
161 resolve_constraint_check (tree ovl
, tree args
)
163 tree cands
= NULL_TREE
;
164 for (tree p
= ovl
; p
!= NULL_TREE
; p
= OVL_NEXT (p
))
166 // Get the next template overload.
167 tree tmpl
= OVL_CURRENT (p
);
168 if (TREE_CODE (tmpl
) != TEMPLATE_DECL
)
171 // Don't try to deduce checks for non-concepts. We often
172 // end up trying to resolve constraints in functional casts
173 // as part of a postfix-expression. We can save time and
174 // headaches by not instantiating those declarations.
176 // NOTE: This masks a potential error, caused by instantiating
177 // non-deduced contexts using placeholder arguments.
178 tree fn
= DECL_TEMPLATE_RESULT (tmpl
);
179 if (DECL_ARGUMENTS (fn
))
181 if (!DECL_DECLARED_CONCEPT_P (fn
))
184 // Remember the candidate if we can deduce a substitution.
185 ++processing_template_decl
;
186 tree parms
= TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl
));
187 if (tree subst
= coerce_template_parms (parms
, args
, tmpl
))
188 if (subst
!= error_mark_node
)
189 cands
= tree_cons (subst
, fn
, cands
);
190 --processing_template_decl
;
193 // If we didn't find a unique candidate, then this is
194 // not a constraint check.
195 if (!cands
|| TREE_CHAIN (cands
))
201 // Determine if the the call expression CALL is a constraint check, and
202 // return the concept declaration and arguments being checked. If CALL
203 // does not denote a constraint check, return NULL.
205 resolve_constraint_check (tree call
)
207 gcc_assert (TREE_CODE (call
) == CALL_EXPR
);
209 // A constraint check must be only a template-id expression. If
210 // it's a call to a base-link, its function(s) should be a
211 // template-id expression. If this is not a template-id, then it
212 // cannot be a concept-check.
213 tree target
= CALL_EXPR_FN (call
);
214 if (BASELINK_P (target
))
215 target
= BASELINK_FUNCTIONS (target
);
216 if (TREE_CODE (target
) != TEMPLATE_ID_EXPR
)
219 // Get the overload set and template arguments and try to
220 // resolve the target.
221 tree ovl
= TREE_OPERAND (target
, 0);
223 /* This is a function call of a variable concept... ill-formed. */
224 if (TREE_CODE (ovl
) == TEMPLATE_DECL
)
226 error_at (location_of (call
),
227 "function call of variable concept %qE", call
);
228 return error_mark_node
;
231 tree args
= TREE_OPERAND (target
, 1);
232 return resolve_constraint_check (ovl
, args
);
235 /* Returns a pair containing the checked variable concept
236 and its associated prototype parameter. The result
237 is a TREE_LIST whose TREE_VALUE is the variable concept
238 and whose TREE_PURPOSE is the prototype parameter. */
241 resolve_variable_concept_check (tree id
)
243 tree tmpl
= TREE_OPERAND (id
, 0);
244 tree args
= TREE_OPERAND (id
, 1);
246 if (!variable_concept_p (tmpl
))
249 /* Make sure that we have the right parameters before
250 assuming that it works. Note that failing to deduce
251 will result in diagnostics. */
252 tree parms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl
));
253 tree result
= coerce_template_parms (parms
, args
, tmpl
);
254 if (result
!= error_mark_node
)
256 tree decl
= DECL_TEMPLATE_RESULT (tmpl
);
257 return build_tree_list (result
, decl
);
264 /* Given a call expression or template-id expression to
265 a concept EXPR possibly including a wildcard, deduce
266 the concept being checked and the prototype parameter.
267 Returns true if the constraint and prototype can be
268 deduced and false otherwise. Note that the CHECK and
269 PROTO arguments are set to NULL_TREE if this returns
273 deduce_constrained_parameter (tree expr
, tree
& check
, tree
& proto
)
275 tree info
= NULL_TREE
;
276 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
277 info
= resolve_variable_concept_check (expr
);
278 else if (TREE_CODE (expr
) == CALL_EXPR
)
279 info
= resolve_constraint_check (expr
);
283 if (info
&& info
!= error_mark_node
)
285 check
= TREE_VALUE (info
);
286 tree arg
= TREE_VEC_ELT (TREE_PURPOSE (info
), 0);
287 if (ARGUMENT_PACK_P (arg
))
288 arg
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg
), 0);
289 proto
= TREE_TYPE (arg
);
292 check
= proto
= NULL_TREE
;
296 // Given a call expression or template-id expression to a concept, EXPR,
297 // deduce the concept being checked and return the template arguments.
298 // Returns NULL_TREE if deduction fails.
300 deduce_concept_introduction (tree expr
)
302 tree info
= NULL_TREE
;
303 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
304 info
= resolve_variable_concept_check (expr
);
305 else if (TREE_CODE (expr
) == CALL_EXPR
)
306 info
= resolve_constraint_check (expr
);
310 if (info
&& info
!= error_mark_node
)
311 return TREE_PURPOSE (info
);
317 /*---------------------------------------------------------------------------
318 Lifting of concept definitions
319 ---------------------------------------------------------------------------*/
321 /* Part of constraint normalization. Whenever we find a reference to
322 a variable concept or a call to a function concept, we lift or
323 inline that concept's definition into the constraint. This ensures
324 that constraints are always checked in the immediate instantiation
327 tree
lift_expression (tree
);
329 /* If the tree T has operands, then lift any concepts out of them. */
331 lift_operands (tree t
)
333 if (int n
= tree_operand_length (t
))
336 for (int i
= 0; i
< n
; ++i
)
337 TREE_OPERAND (t
, i
) = lift_expression (TREE_OPERAND (t
, i
));
342 /* Recursively lift all operands of the function call. Also, check
343 that the call target is not accidentally a variable concept
344 since that's ill-formed. */
346 lift_function_call (tree t
)
348 gcc_assert (TREE_CODE (t
) == CALL_EXPR
);
349 gcc_assert (!VAR_P (CALL_EXPR_FN (t
)));
350 return lift_operands (t
);
353 /* Inline a function (concept) definition by substituting
354 ARGS into its body. */
356 lift_function_definition (tree fn
, tree args
)
358 /* Extract the body of the function minus the return expression. */
359 tree body
= DECL_SAVED_TREE (fn
);
361 return error_mark_node
;
362 if (TREE_CODE (body
) == BIND_EXPR
)
363 body
= BIND_EXPR_BODY (body
);
364 if (TREE_CODE (body
) != RETURN_EXPR
)
365 return error_mark_node
;
367 body
= TREE_OPERAND (body
, 0);
369 /* Substitute template arguments to produce our inline expression. */
370 tree result
= tsubst_expr (body
, args
, tf_none
, NULL_TREE
, false);
371 if (result
== error_mark_node
)
372 return error_mark_node
;
374 return lift_expression (result
);
377 /* Inline a reference to a function concept. */
379 lift_call_expression (tree t
)
381 /* Try to resolve this function call as a concept. If not, then
382 it can be returned as-is. */
383 tree check
= resolve_constraint_check (t
);
385 return lift_function_call (t
);
386 if (check
== error_mark_node
)
387 return error_mark_node
;
389 tree fn
= TREE_VALUE (check
);
390 tree args
= TREE_PURPOSE (check
);
391 return lift_function_definition (fn
, args
);
395 lift_variable_initializer (tree var
, tree args
)
397 /* Extract the body from the variable initializer. */
398 tree init
= DECL_INITIAL (var
);
400 return error_mark_node
;
402 /* Substitute the arguments to form our new inline expression. */
403 tree result
= tsubst_expr (init
, args
, tf_none
, NULL_TREE
, false);
404 if (result
== error_mark_node
)
405 return error_mark_node
;
407 return lift_expression (result
);
410 /* Determine if a template-id is a variable concept and inline. */
413 lift_template_id (tree t
)
415 if (tree info
= resolve_variable_concept_check (t
))
417 tree decl
= TREE_VALUE (info
);
418 tree args
= TREE_PURPOSE (info
);
419 return lift_variable_initializer (decl
, args
);
422 /* Check that we didn't refer to a function concept like
425 TODO: Add a note on how to fix this. */
426 tree tmpl
= TREE_OPERAND (t
, 0);
427 if (TREE_CODE (tmpl
) == OVERLOAD
)
429 tree fn
= OVL_FUNCTION (tmpl
);
430 if (TREE_CODE (fn
) == TEMPLATE_DECL
431 && DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (fn
)))
433 error_at (location_of (t
),
434 "invalid reference to function concept %qD", fn
);
435 return error_mark_node
;
442 /* Lift any constraints appearing in a nested requirement of
443 a requires-expression. */
445 lift_requires_expression (tree t
)
447 tree parms
= TREE_OPERAND (t
, 0);
448 tree reqs
= TREE_OPERAND (t
, 1);
449 tree result
= NULL_TREE
;
450 for (; reqs
!= NULL_TREE
; reqs
= TREE_CHAIN (reqs
))
452 tree req
= TREE_VALUE (reqs
);
453 if (TREE_CODE (req
) == NESTED_REQ
)
455 tree expr
= lift_expression (TREE_OPERAND (req
, 0));
456 req
= finish_nested_requirement (expr
);
458 result
= tree_cons (NULL_TREE
, req
, result
);
460 return finish_requires_expr (parms
, result
);
463 /* Inline references to specializations of concepts. */
465 lift_expression (tree t
)
470 if (t
== error_mark_node
)
471 return error_mark_node
;
473 /* Concepts can be referred to by call or variable. All other
474 nodes are preserved. */
475 switch (TREE_CODE (t
))
478 return lift_call_expression (t
);
480 case TEMPLATE_ID_EXPR
:
481 return lift_template_id (t
);
484 return lift_requires_expression (t
);
486 case EXPR_PACK_EXPANSION
:
487 /* Use copy_node rather than make_pack_expansion so that
488 PACK_EXPANSION_PARAMETER_PACKS stays the same. */
490 SET_PACK_EXPANSION_PATTERN
491 (t
, lift_expression (PACK_EXPANSION_PATTERN (t
)));
497 TREE_VALUE (t
) = lift_expression (TREE_VALUE (t
));
498 TREE_CHAIN (t
) = lift_expression (TREE_CHAIN (t
));
503 return lift_operands (t
);
507 /*---------------------------------------------------------------------------
508 Transformation of expressions into constraints
509 ---------------------------------------------------------------------------*/
511 /* Part of constraint normalization. The following functions rewrite
512 expressions as constraints. */
514 tree
transform_expression (tree
);
516 /* Check that the logical-or or logical-and expression does
517 not result in a call to a user-defined user-defined operator
518 (temp.constr.op). Returns true if the logical operator is
519 admissible and false otherwise. */
522 check_logical_expr (tree t
)
524 /* We can't do much for type dependent expressions. */
525 if (type_dependent_expression_p (t
))
528 /* Resolve the logical operator. Note that template processing is
529 disabled so we get the actual call or target expression back.
530 not_processing_template_sentinel sentinel.
532 TODO: This check is actually subsumed by the requirement that
533 constraint operands have type bool. I'm not sure we need it
534 unless we allow conversions. */
535 tree arg1
= TREE_OPERAND (t
, 0);
536 tree arg2
= TREE_OPERAND (t
, 1);
537 tree ovl
= NULL_TREE
;
538 tree expr
= build_x_binary_op (EXPR_LOC_OR_LOC (arg2
, input_location
),
540 arg1
, TREE_CODE (arg1
),
541 arg2
, TREE_CODE (arg2
),
544 if (TREE_CODE (expr
) != TREE_CODE (t
))
546 error ("user-defined operator %qs in constraint %q+E",
547 operator_name_info
[TREE_CODE (t
)].name
, t
);
553 /* Transform a logical-or or logical-and expression into either
554 a conjunction or disjunction. */
557 xform_logical (tree t
, tree_code c
)
559 if (!check_logical_expr (t
))
560 return error_mark_node
;
561 tree t0
= transform_expression (TREE_OPERAND (t
, 0));
562 tree t1
= transform_expression (TREE_OPERAND (t
, 1));
563 return build_nt (c
, t0
, t1
);
566 /* A simple requirement T introduces an expression constraint
567 for its expression. */
570 xform_simple_requirement (tree t
)
572 return build_nt (EXPR_CONSTR
, TREE_OPERAND (t
, 0));
575 /* A type requirement T introduce a type constraint for its type. */
578 xform_type_requirement (tree t
)
580 return build_nt (TYPE_CONSTR
, TREE_OPERAND (t
, 0));
583 /* A compound requirement T introduces a conjunction of constraints
584 depending on its form. The conjunction always includes an
585 expression constraint for the expression of the requirement.
586 If a trailing return type was specified, the conjunction includes
587 either an implicit conversion constraint or an argument deduction
588 constraint. If the noexcept specifier is present, the conjunction
589 includes an exception constraint. */
592 xform_compound_requirement (tree t
)
594 tree expr
= TREE_OPERAND (t
, 0);
595 tree constr
= build_nt (EXPR_CONSTR
, TREE_OPERAND (t
, 0));
597 /* If a type is given, append an implicit conversion or
598 argument deduction constraint. */
599 if (tree type
= TREE_OPERAND (t
, 1))
602 /* TODO: We should be extracting a list of auto nodes
603 from type_uses_auto, not a single node */
604 if (tree placeholder
= type_uses_auto (type
))
605 type_constr
= build_nt (DEDUCT_CONSTR
, expr
, type
, placeholder
);
607 type_constr
= build_nt (ICONV_CONSTR
, expr
, type
);
608 constr
= conjoin_constraints (constr
, type_constr
);
611 /* If noexcept is present, append an exception constraint. */
612 if (COMPOUND_REQ_NOEXCEPT_P (t
))
614 tree except
= build_nt (EXCEPT_CONSTR
, expr
);
615 constr
= conjoin_constraints (constr
, except
);
621 /* A nested requirement T introduces a conjunction of constraints
622 corresponding to its constraint-expression.
624 If the result of transforming T is error_mark_node, the resulting
625 constraint is a predicate constraint whose operand is also
626 error_mark_node. This preserves the constraint structure, but
627 will guarantee that the constraint is never satisfied. */
630 xform_nested_requirement (tree t
)
632 return transform_expression (TREE_OPERAND (t
, 0));
635 /* Transform a requirement T into one or more constraints. */
638 xform_requirement (tree t
)
640 switch (TREE_CODE (t
))
643 return xform_simple_requirement (t
);
646 return xform_type_requirement (t
);
649 return xform_compound_requirement (t
);
652 return xform_nested_requirement (t
);
657 return error_mark_node
;
660 /* Transform a sequence of requirements into a conjunction of
664 xform_requirements (tree t
)
666 tree result
= NULL_TREE
;
667 for (; t
; t
= TREE_CHAIN (t
))
669 tree constr
= xform_requirement (TREE_VALUE (t
));
670 result
= conjoin_constraints (result
, constr
);
675 /* Transform a requires-expression into a parameterized constraint. */
678 xform_requires_expr (tree t
)
680 tree operand
= xform_requirements (TREE_OPERAND (t
, 1));
681 if (tree parms
= TREE_OPERAND (t
, 0))
682 return build_nt (PARM_CONSTR
, parms
, operand
);
687 /* Transform an expression into an atomic predicate constraint.
688 After substitution, the expression of a predicate constraint
689 shall have type bool (temp.constr.pred). For non-type-dependent
690 expressions, we can check that now. */
693 xform_atomic (tree t
)
695 if (TREE_TYPE (t
) && !type_dependent_expression_p (t
))
697 tree type
= cv_unqualified (TREE_TYPE (t
));
698 if (!same_type_p (type
, boolean_type_node
))
700 error ("predicate constraint %q+E does not have type %<bool%>", t
);
701 return error_mark_node
;
704 return build_nt (PRED_CONSTR
, t
);
707 /* Push down the pack expansion EXP into the leaves of the constraint PAT. */
710 push_down_pack_expansion (tree exp
, tree pat
)
712 switch (TREE_CODE (pat
))
717 pat
= copy_node (pat
);
718 TREE_OPERAND (pat
, 0)
719 = push_down_pack_expansion (exp
, TREE_OPERAND (pat
, 0));
720 TREE_OPERAND (pat
, 1)
721 = push_down_pack_expansion (exp
, TREE_OPERAND (pat
, 1));
726 exp
= copy_node (exp
);
727 SET_PACK_EXPANSION_PATTERN (exp
, pat
);
733 /* Transform a pack expansion into a constraint. First we transform the
734 pattern of the pack expansion, then we push the pack expansion down into the
735 leaves of the constraint so that partial ordering will work. */
738 xform_pack_expansion (tree t
)
740 tree pat
= transform_expression (PACK_EXPANSION_PATTERN (t
));
741 return push_down_pack_expansion (t
, pat
);
744 /* Transform an expression into a constraint. */
749 switch (TREE_CODE (t
))
751 case TRUTH_ANDIF_EXPR
:
752 return xform_logical (t
, CONJ_CONSTR
);
754 case TRUTH_ORIF_EXPR
:
755 return xform_logical (t
, DISJ_CONSTR
);
758 return xform_requires_expr (t
);
761 return transform_expression (BIND_EXPR_BODY (t
));
763 case EXPR_PACK_EXPANSION
:
764 return xform_pack_expansion (t
);
767 /* All other constraints are atomic. */
768 return xform_atomic (t
);
772 /* Transform a statement into an expression. */
777 switch (TREE_CODE (t
))
780 return transform_expression (TREE_OPERAND (t
, 0));
784 return error_mark_node
;
787 /* Reduction rules for the declaration T. */
792 switch (TREE_CODE (t
))
795 return xform_atomic (t
);
799 return error_mark_node
;
802 /* Transform a lifted expression into a constraint. This either
803 returns a constraint, or it returns error_mark_node when
804 a constraint cannot be formed. */
807 transform_expression (tree t
)
812 if (t
== error_mark_node
)
813 return error_mark_node
;
815 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
821 return xform_expr (t
);
824 return xform_stmt (t
);
826 case tcc_declaration
:
827 return xform_decl (t
);
829 case tcc_exceptional
:
833 /* These are all atomic predicate constraints. */
834 return xform_atomic (t
);
837 /* Unhandled node kind. */
840 return error_mark_node
;
843 /*---------------------------------------------------------------------------
844 Constraint normalization
845 ---------------------------------------------------------------------------*/
847 tree
normalize_constraint (tree
);
849 /* The normal form of the disjunction T0 /\ T1 is the conjunction
850 of the normal form of T0 and the normal form of T1. */
853 normalize_conjunction (tree t
)
855 tree t0
= normalize_constraint (TREE_OPERAND (t
, 0));
856 tree t1
= normalize_constraint (TREE_OPERAND (t
, 1));
857 return build_nt (CONJ_CONSTR
, t0
, t1
);
860 /* The normal form of the disjunction T0 \/ T1 is the disjunction
861 of the normal form of T0 and the normal form of T1. */
864 normalize_disjunction (tree t
)
866 tree t0
= normalize_constraint (TREE_OPERAND (t
, 0));
867 tree t1
= normalize_constraint (TREE_OPERAND (t
, 1));
868 return build_nt (DISJ_CONSTR
, t0
, t1
);
871 /* A predicate constraint is normalized in two stages. First all
872 references specializations of concepts are replaced by their
873 substituted definitions. Then, the resulting expression is
874 transformed into a constraint by transforming && expressions
875 into conjunctions and || into disjunctions. */
878 normalize_predicate_constraint (tree t
)
880 ++processing_template_decl
;
881 tree expr
= PRED_CONSTR_EXPR (t
);
882 tree lifted
= lift_expression (expr
);
883 tree constr
= transform_expression (lifted
);
884 --processing_template_decl
;
888 /* The normal form of a parameterized constraint is the normal
889 form of its operand. */
892 normalize_parameterized_constraint (tree t
)
894 tree parms
= PARM_CONSTR_PARMS (t
);
895 tree operand
= normalize_constraint (PARM_CONSTR_OPERAND (t
));
896 return build_nt (PARM_CONSTR
, parms
, operand
);
899 /* Normalize the constraint T by reducing it so that it is
900 comprised of only conjunctions and disjunctions of atomic
904 normalize_constraint (tree t
)
909 if (t
== error_mark_node
)
912 switch (TREE_CODE (t
))
915 return normalize_conjunction (t
);
918 return normalize_disjunction (t
);
921 return normalize_predicate_constraint (t
);
924 return normalize_parameterized_constraint (t
);
931 /* These constraints are defined to be atomic. */
935 /* CONSTR was not a constraint. */
938 return error_mark_node
;
944 // -------------------------------------------------------------------------- //
945 // Constraint Semantic Processing
947 // The following functions are called by the parser and substitution rules
948 // to create and evaluate constraint-related nodes.
950 // The constraints associated with the current template parameters.
952 current_template_constraints (void)
954 if (!current_template_parms
)
956 tree tmpl_constr
= TEMPLATE_PARM_CONSTRAINTS (current_template_parms
);
957 return build_constraints (tmpl_constr
, NULL_TREE
);
960 // If the recently parsed TYPE declares or defines a template or template
961 // specialization, get its corresponding constraints from the current
962 // template parameters and bind them to TYPE's declaration.
964 associate_classtype_constraints (tree type
)
966 if (!type
|| type
== error_mark_node
|| TREE_CODE (type
) != RECORD_TYPE
)
969 // An explicit class template specialization has no template
971 if (!current_template_parms
)
974 if (CLASSTYPE_IS_TEMPLATE (type
) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
976 tree decl
= TYPE_STUB_DECL (type
);
977 tree ci
= current_template_constraints ();
979 // An implicitly instantiated member template declaration already
980 // has associated constraints. If it is defined outside of its
981 // class, then we need match these constraints against those of
982 // original declaration.
983 if (tree orig_ci
= get_constraints (decl
))
985 if (!equivalent_constraints (ci
, orig_ci
))
987 // FIXME: Improve diagnostics.
988 error ("%qT does not match any declaration", type
);
989 return error_mark_node
;
993 set_constraints (decl
, ci
);
1000 // Create an empty constraint info block.
1001 inline tree_constraint_info
*
1002 build_constraint_info ()
1004 return (tree_constraint_info
*)make_node (CONSTRAINT_INFO
);
1009 /* Build a constraint-info object that contains the associated constraints
1010 of a declaration. This also includes the declaration's template
1011 requirements (TREQS) and any trailing requirements for a function
1012 declarator (DREQS). Note that both TREQS and DREQS must be constraints.
1014 If the declaration has neither template nor declaration requirements
1015 this returns NULL_TREE, indicating an unconstrained declaration. */
1018 build_constraints (tree tmpl_reqs
, tree decl_reqs
)
1020 gcc_assert (tmpl_reqs
? constraint_p (tmpl_reqs
) : true);
1021 gcc_assert (decl_reqs
? constraint_p (decl_reqs
) : true);
1023 if (!tmpl_reqs
&& !decl_reqs
)
1026 tree_constraint_info
* ci
= build_constraint_info ();
1027 ci
->template_reqs
= tmpl_reqs
;
1028 ci
->declarator_reqs
= decl_reqs
;
1029 ci
->associated_constr
= conjoin_constraints (tmpl_reqs
, decl_reqs
);
1031 ++processing_template_decl
;
1032 ci
->normalized_constr
= normalize_constraint (ci
->associated_constr
);
1033 --processing_template_decl
;
1035 ci
->assumptions
= decompose_assumptions (ci
->normalized_constr
);
1041 /* Returns true if any of the arguments in the template
1042 argument list is a wildcard or wildcard pack. */
1044 contains_wildcard_p (tree args
)
1046 for (int i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
1048 tree arg
= TREE_VEC_ELT (args
, i
);
1049 if (TREE_CODE (arg
) == WILDCARD_DECL
)
1055 /* Build a new call expression, but don't actually generate
1056 a new function call. We just want the tree, not the
1059 build_call_check (tree id
)
1061 ++processing_template_decl
;
1062 vec
<tree
, va_gc
> *fargs
= make_tree_vector();
1063 tree call
= finish_call_expr (id
, &fargs
, false, false, tf_none
);
1064 release_tree_vector (fargs
);
1065 --processing_template_decl
;
1069 /* Build an expression that will check a variable concept. If any
1070 argument contains a wildcard, don't try to finish the variable
1071 template because we can't substitute into a non-existent
1074 build_variable_check (tree id
)
1076 gcc_assert (TREE_CODE (id
) == TEMPLATE_ID_EXPR
);
1077 if (contains_wildcard_p (TREE_OPERAND (id
, 1)))
1080 ++processing_template_decl
;
1081 tree var
= finish_template_variable (id
);
1082 --processing_template_decl
;
1086 /* Construct a sequence of template arguments by prepending
1087 ARG to REST. Either ARG or REST may be null. */
1089 build_concept_check_arguments (tree arg
, tree rest
)
1091 gcc_assert (rest
? TREE_CODE (rest
) == TREE_VEC
: true);
1095 int n
= rest
? TREE_VEC_LENGTH (rest
) : 0;
1096 args
= make_tree_vec (n
+ 1);
1097 TREE_VEC_ELT (args
, 0) = arg
;
1099 for (int i
= 0; i
< n
; ++i
)
1100 TREE_VEC_ELT (args
, i
+ 1) = TREE_VEC_ELT (rest
, i
);
1101 int def
= rest
? GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (rest
) : 0;
1102 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
, def
+ 1);
1106 gcc_assert (rest
!= NULL_TREE
);
1114 /* Construct an expression that checks the concept given by
1115 TARGET. The TARGET must be:
1117 - an OVERLOAD referring to one or more function concepts
1118 - a BASELINK referring to an overload set of the above, or
1119 - a TEMPLTATE_DECL referring to a variable concept.
1121 ARG and REST are the explicit template arguments for the
1122 eventual concept check. */
1124 build_concept_check (tree target
, tree arg
, tree rest
)
1126 tree args
= build_concept_check_arguments (arg
, rest
);
1127 if (variable_template_p (target
))
1128 return build_variable_check (lookup_template_variable (target
, args
));
1130 return build_call_check (lookup_template_function (target
, args
));
1134 /* Returns a TYPE_DECL that contains sufficient information to
1135 build a template parameter of the same kind as PROTO and
1136 constrained by the concept declaration CNC. Note that PROTO
1137 is the first template parameter of CNC.
1139 If specified, ARGS provides additional arguments to the
1140 constraint check. */
1142 build_constrained_parameter (tree cnc
, tree proto
, tree args
)
1144 tree name
= DECL_NAME (cnc
);
1145 tree type
= TREE_TYPE (proto
);
1146 tree decl
= build_decl (input_location
, TYPE_DECL
, name
, type
);
1147 CONSTRAINED_PARM_PROTOTYPE (decl
) = proto
;
1148 CONSTRAINED_PARM_CONCEPT (decl
) = cnc
;
1149 CONSTRAINED_PARM_EXTRA_ARGS (decl
) = args
;
1153 /* Create a constraint expression for the given DECL that
1154 evaluates the requirements specified by CONSTR, a TYPE_DECL
1155 that contains all the information necessary to build the
1156 requirements (see finish_concept_name for the layout of
1159 Note that the constraints are neither reduced nor decomposed.
1160 That is done only after the requires clause has been parsed
1163 finish_shorthand_constraint (tree decl
, tree constr
)
1165 /* No requirements means no constraints. */
1169 tree proto
= CONSTRAINED_PARM_PROTOTYPE (constr
);
1170 tree con
= CONSTRAINED_PARM_CONCEPT (constr
);
1171 tree args
= CONSTRAINED_PARM_EXTRA_ARGS (constr
);
1173 /* If the parameter declaration is variadic, but the concept
1174 is not then we need to apply the concept to every element
1176 bool is_proto_pack
= template_parameter_pack_p (proto
);
1177 bool is_decl_pack
= template_parameter_pack_p (decl
);
1178 bool apply_to_all_p
= is_decl_pack
&& !is_proto_pack
;
1180 /* Get the argument and overload used for the requirement
1181 and adjust it if we're going to expand later. */
1182 tree arg
= template_parm_to_arg (build_tree_list (NULL_TREE
, decl
));
1184 arg
= PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg
), 0));
1186 /* Build the concept check. If it the constraint needs to be
1187 applied to all elements of the parameter pack, then make
1188 the constraint an expansion. */
1190 tree tmpl
= DECL_TI_TEMPLATE (con
);
1191 if (TREE_CODE (con
) == VAR_DECL
)
1193 check
= build_concept_check (tmpl
, arg
, args
);
1197 tree ovl
= build_overload (tmpl
, NULL_TREE
);
1198 check
= build_concept_check (ovl
, arg
, args
);
1201 /* Make the check a pack expansion if needed.
1203 FIXME: We should be making a fold expression. */
1206 check
= make_pack_expansion (check
);
1207 TREE_TYPE (check
) = boolean_type_node
;
1210 return make_predicate_constraint (check
);
1213 /* Returns a conjunction of shorthand requirements for the template
1214 parameter list PARMS. Note that the requirements are stored in
1215 the TYPE of each tree node. */
1217 get_shorthand_constraints (tree parms
)
1219 tree result
= NULL_TREE
;
1220 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
1221 for (int i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
1223 tree parm
= TREE_VEC_ELT (parms
, i
);
1224 tree constr
= TEMPLATE_PARM_CONSTRAINTS (parm
);
1225 result
= conjoin_constraints (result
, constr
);
1230 // Returns and chains a new parameter for PARAMETER_LIST which will conform
1231 // to the prototype given by SRC_PARM. The new parameter will have its
1232 // identifier and location set according to IDENT and PARM_LOC respectively.
1234 process_introduction_parm (tree parameter_list
, tree src_parm
)
1236 // If we have a pack, we should have a single pack argument which is the
1237 // placeholder we want to look at.
1238 bool is_parameter_pack
= ARGUMENT_PACK_P (src_parm
);
1239 if (is_parameter_pack
)
1240 src_parm
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (src_parm
), 0);
1242 // At this point we should have a wildcard, but we want to
1243 // grab the associated decl from it. Also grab the stored
1244 // identifier and location that should be chained to it in
1246 gcc_assert (TREE_CODE (src_parm
) == WILDCARD_DECL
);
1248 tree ident
= DECL_NAME (src_parm
);
1249 location_t parm_loc
= DECL_SOURCE_LOCATION (src_parm
);
1251 // If we expect a pack and the deduced template is not a pack, or if the
1252 // template is using a pack and we didn't declare a pack, throw an error.
1253 if (is_parameter_pack
!= WILDCARD_PACK_P (src_parm
))
1255 error_at (parm_loc
, "cannot match pack for introduced parameter");
1256 tree err_parm
= build_tree_list (error_mark_node
, error_mark_node
);
1257 return chainon (parameter_list
, err_parm
);
1260 src_parm
= TREE_TYPE (src_parm
);
1264 if (TREE_CODE (src_parm
) == TYPE_DECL
)
1266 is_non_type
= false;
1267 parm
= finish_template_type_parm (class_type_node
, ident
);
1269 else if (TREE_CODE (src_parm
) == TEMPLATE_DECL
)
1271 is_non_type
= false;
1272 begin_template_parm_list ();
1273 current_template_parms
= DECL_TEMPLATE_PARMS (src_parm
);
1274 end_template_parm_list ();
1275 parm
= finish_template_template_parm (class_type_node
, ident
);
1281 // Since we don't have a declarator, so we can copy the source
1282 // parameter and change the name and eventually the location.
1283 parm
= copy_decl (src_parm
);
1284 DECL_NAME (parm
) = ident
;
1287 // Wrap in a TREE_LIST for process_template_parm. Introductions do not
1288 // retain the defaults from the source template.
1289 parm
= build_tree_list (NULL_TREE
, parm
);
1291 return process_template_parm (parameter_list
, parm_loc
, parm
,
1292 is_non_type
, is_parameter_pack
);
1295 /* Associates a constraint check to the current template based
1296 on the introduction parameters. INTRO_LIST must be a TREE_VEC
1297 of WILDCARD_DECLs containing a chained PARM_DECL which
1298 contains the identifier as well as the source location.
1299 TMPL_DECL is the decl for the concept being used. If we
1300 take a concept, C, this will form a check in the form of
1301 C<INTRO_LIST> filling in any extra arguments needed by the
1304 Returns NULL_TREE if no concept could be matched and
1305 error_mark_node if an error occurred when matching. */
1307 finish_template_introduction (tree tmpl_decl
, tree intro_list
)
1309 /* Deduce the concept check. */
1310 tree expr
= build_concept_check (tmpl_decl
, NULL_TREE
, intro_list
);
1311 if (expr
== error_mark_node
)
1314 tree parms
= deduce_concept_introduction (expr
);
1318 /* Build template parameter scope for introduction. */
1319 tree parm_list
= NULL_TREE
;
1320 begin_template_parm_list ();
1321 int nargs
= MIN (TREE_VEC_LENGTH (parms
), TREE_VEC_LENGTH (intro_list
));
1322 for (int n
= 0; n
< nargs
; ++n
)
1323 parm_list
= process_introduction_parm (parm_list
, TREE_VEC_ELT (parms
, n
));
1324 parm_list
= end_template_parm_list (parm_list
);
1325 for (int i
= 0; i
< TREE_VEC_LENGTH (parm_list
); ++i
)
1326 if (TREE_VALUE (TREE_VEC_ELT (parm_list
, i
)) == error_mark_node
)
1328 end_template_decl ();
1329 return error_mark_node
;
1332 /* Build a concept check for our constraint. */
1333 tree check_args
= make_tree_vec (TREE_VEC_LENGTH (parms
));
1335 for (; n
< TREE_VEC_LENGTH (parm_list
); ++n
)
1337 tree parm
= TREE_VEC_ELT (parm_list
, n
);
1338 TREE_VEC_ELT (check_args
, n
) = template_parm_to_arg (parm
);
1340 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (check_args
, n
);
1342 /* If the template expects more parameters we should be able
1343 to use the defaults from our deduced concept. */
1344 for (; n
< TREE_VEC_LENGTH (parms
); ++n
)
1345 TREE_VEC_ELT (check_args
, n
) = TREE_VEC_ELT (parms
, n
);
1347 /* Associate the constraint. */
1348 tree check
= build_concept_check (tmpl_decl
, NULL_TREE
, check_args
);
1349 tree constr
= make_predicate_constraint (check
);
1350 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = constr
;
1356 /* Given the predicate constraint T from a constrained-type-specifier, extract
1357 its TMPL and ARGS. FIXME why do we need two different forms of
1358 constrained-type-specifier? */
1361 placeholder_extract_concept_and_args (tree t
, tree
&tmpl
, tree
&args
)
1363 if (TREE_CODE (t
) == TYPE_DECL
)
1365 /* A constrained parameter. */
1366 tmpl
= DECL_TI_TEMPLATE (CONSTRAINED_PARM_CONCEPT (t
));
1367 args
= CONSTRAINED_PARM_EXTRA_ARGS (t
);
1371 gcc_assert (TREE_CODE (t
) == PRED_CONSTR
);
1372 t
= PRED_CONSTR_EXPR (t
);
1373 gcc_assert (TREE_CODE (t
) == CALL_EXPR
1374 || TREE_CODE (t
) == TEMPLATE_ID_EXPR
1377 if (TREE_CODE (t
) == CALL_EXPR
)
1378 t
= CALL_EXPR_FN (t
);
1379 if (TREE_CODE (t
) == TEMPLATE_ID_EXPR
)
1381 tmpl
= TREE_OPERAND (t
, 0);
1382 if (TREE_CODE (tmpl
) == OVERLOAD
)
1384 gcc_assert (OVL_CHAIN (tmpl
) == NULL_TREE
);
1385 tmpl
= OVL_FUNCTION (tmpl
);
1387 args
= TREE_OPERAND (t
, 1);
1389 else if (DECL_P (t
))
1391 tmpl
= DECL_TI_TEMPLATE (t
);
1392 args
= DECL_TI_ARGS (t
);
1398 /* Returns true iff the placeholders C1 and C2 are equivalent. C1
1399 and C2 can be either PRED_CONSTR_EXPR or TEMPLATE_TYPE_PARM. */
1402 equivalent_placeholder_constraints (tree c1
, tree c2
)
1404 if (c1
&& TREE_CODE (c1
) == TEMPLATE_TYPE_PARM
)
1405 /* A constrained auto. */
1406 c1
= PLACEHOLDER_TYPE_CONSTRAINTS (c1
);
1407 if (c2
&& TREE_CODE (c2
) == TEMPLATE_TYPE_PARM
)
1408 c2
= PLACEHOLDER_TYPE_CONSTRAINTS (c2
);
1415 tree t1
, t2
, a1
, a2
;
1416 placeholder_extract_concept_and_args (c1
, t1
, a1
);
1417 placeholder_extract_concept_and_args (c2
, t2
, a2
);
1422 /* Skip the first argument to avoid infinite recursion on the
1423 placeholder auto itself. */
1424 bool skip1
= (TREE_CODE (c1
) == PRED_CONSTR
);
1425 bool skip2
= (TREE_CODE (c2
) == PRED_CONSTR
);
1427 int len1
= (a1
? TREE_VEC_LENGTH (a1
) : 0) - skip1
;
1428 int len2
= (a2
? TREE_VEC_LENGTH (a2
) : 0) - skip2
;
1433 for (int i
= 0; i
< len1
; ++i
)
1434 if (!cp_tree_equal (TREE_VEC_ELT (a1
, i
+ skip1
),
1435 TREE_VEC_ELT (a2
, i
+ skip2
)))
1440 /* Return a hash value for the placeholder PRED_CONSTR C. */
1443 hash_placeholder_constraint (tree c
)
1446 placeholder_extract_concept_and_args (c
, t
, a
);
1448 /* Like hash_tmpl_and_args, but skip the first argument. */
1449 hashval_t val
= iterative_hash_object (DECL_UID (t
), 0);
1451 for (int i
= TREE_VEC_LENGTH (a
)-1; i
> 0; --i
)
1452 val
= iterative_hash_template_arg (TREE_VEC_ELT (a
, i
), val
);
1457 /*---------------------------------------------------------------------------
1458 Constraint substitution
1459 ---------------------------------------------------------------------------*/
1461 /* The following functions implement substitution rules for constraints.
1462 Substitution without checking constraints happens only in the
1463 instantiation of class templates. For example:
1465 template<C1 T> struct S {
1466 void f(T) requires C2<T>;
1467 void g(T) requires T::value;
1470 S<int> s; // error instantiating S<int>::g(T)
1472 When we instantiate S, we substitute into its member declarations,
1473 including their constraints. However, those constraints are not
1474 checked. Substituting int into C2<T> yields C2<int>, and substituting
1475 into T::value yields a substitution failure, making the program
1478 Note that we only ever substitute into the associated constraints
1479 of a declaration. That is, substitution is defined only for predicate
1480 constraints and conjunctions. */
1482 /* Substitute into the predicate constraints. Returns error_mark_node
1483 if the substitution into the expression fails. */
1485 tsubst_predicate_constraint (tree t
, tree args
,
1486 tsubst_flags_t complain
, tree in_decl
)
1488 tree expr
= PRED_CONSTR_EXPR (t
);
1489 ++processing_template_decl
;
1490 tree result
= tsubst_expr (expr
, args
, complain
, in_decl
, false);
1491 --processing_template_decl
;
1492 return build_nt (PRED_CONSTR
, result
);
1495 /* Substitute into the conjunction of constraints. Returns
1496 error_mark_node if substitution into either operand fails. */
1498 tsubst_conjunction (tree t
, tree args
,
1499 tsubst_flags_t complain
, tree in_decl
)
1501 tree t0
= TREE_OPERAND (t
, 0);
1502 tree r0
= tsubst_constraint (t0
, args
, complain
, in_decl
);
1503 tree t1
= TREE_OPERAND (t
, 1);
1504 tree r1
= tsubst_constraint (t1
, args
, complain
, in_decl
);
1505 return build_nt (CONJ_CONSTR
, r0
, r1
);
1508 /* Substitute ARGS into the constraint T. */
1510 tsubst_constraint (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
1514 if (TREE_CODE (t
) == CONJ_CONSTR
)
1515 return tsubst_conjunction (t
, args
, complain
, in_decl
);
1516 else if (TREE_CODE (t
) == PRED_CONSTR
)
1517 return tsubst_predicate_constraint (t
, args
, complain
, in_decl
);
1520 return error_mark_node
;
1525 /* A subroutine of tsubst_constraint_variables. Register local
1526 specializations for each of parameter in PARMS and its
1527 corresponding substituted constraint variable in VARS.
1530 declare_constraint_vars (tree parms
, tree vars
)
1533 for (tree t
= parms
; t
; t
= DECL_CHAIN (t
))
1535 if (DECL_PACK_P (t
))
1537 tree pack
= extract_fnparm_pack (t
, &s
);
1538 register_local_specialization (pack
, t
);
1542 register_local_specialization (s
, t
);
1549 /* A subroutine of tsubst_parameterized_constraint. Substitute ARGS
1550 into the parameter list T, producing a sequence of constraint
1551 variables, declared in the current scope.
1553 Note that the caller must establish a local specialization stack
1554 prior to calling this function since this substitution will
1555 declare the substituted parameters. */
1557 tsubst_constraint_variables (tree t
, tree args
,
1558 tsubst_flags_t complain
, tree in_decl
)
1560 /* Clear cp_unevaluated_operand across tsubst so that we get a proper chain
1562 int saved_unevaluated_operand
= cp_unevaluated_operand
;
1563 cp_unevaluated_operand
= 0;
1564 tree vars
= tsubst (t
, args
, complain
, in_decl
);
1565 cp_unevaluated_operand
= saved_unevaluated_operand
;
1566 if (vars
== error_mark_node
)
1567 return error_mark_node
;
1568 return declare_constraint_vars (t
, vars
);
1571 /* Substitute ARGS into the simple requirement T. Note that
1572 substitution may result in an ill-formed expression without
1573 causing the program to be ill-formed. In such cases, the
1574 requirement wraps an error_mark_node. */
1576 tsubst_simple_requirement (tree t
, tree args
,
1577 tsubst_flags_t complain
, tree in_decl
)
1579 ++processing_template_decl
;
1580 tree expr
= tsubst_expr (TREE_OPERAND (t
, 0), args
, complain
, in_decl
, false);
1581 --processing_template_decl
;
1582 return finish_simple_requirement (expr
);
1585 /* Substitute ARGS into the type requirement T. Note that
1586 substitution may result in an ill-formed type without
1587 causing the program to be ill-formed. In such cases, the
1588 requirement wraps an error_mark_node. */
1591 tsubst_type_requirement (tree t
, tree args
,
1592 tsubst_flags_t complain
, tree in_decl
)
1594 ++processing_template_decl
;
1595 tree type
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
1596 --processing_template_decl
;
1597 return finish_type_requirement (type
);
1600 /* Substitute args into the compound requirement T. If substituting
1601 into either the expression or the type fails, the corresponding
1602 operands in the resulting node will be error_mark_node. This
1603 preserves a requirement for the purpose of partial ordering, but
1604 it will never be satisfied. */
1607 tsubst_compound_requirement (tree t
, tree args
,
1608 tsubst_flags_t complain
, tree in_decl
)
1610 ++processing_template_decl
;
1611 tree expr
= tsubst_expr (TREE_OPERAND (t
, 0), args
, complain
, in_decl
, false);
1612 tree type
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
1613 --processing_template_decl
;
1614 bool noexcept_p
= COMPOUND_REQ_NOEXCEPT_P (t
);
1615 return finish_compound_requirement (expr
, type
, noexcept_p
);
1618 /* Substitute ARGS into the nested requirement T. */
1621 tsubst_nested_requirement (tree t
, tree args
,
1622 tsubst_flags_t complain
, tree in_decl
)
1624 ++processing_template_decl
;
1625 tree expr
= tsubst_expr (TREE_OPERAND (t
, 0), args
, complain
, in_decl
, false);
1626 --processing_template_decl
;
1627 return finish_nested_requirement (expr
);
1631 tsubst_requirement (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
1633 switch (TREE_CODE (t
))
1636 return tsubst_simple_requirement (t
, args
, complain
, in_decl
);
1638 return tsubst_type_requirement (t
, args
, complain
, in_decl
);
1640 return tsubst_compound_requirement (t
, args
, complain
, in_decl
);
1642 return tsubst_nested_requirement (t
, args
, complain
, in_decl
);
1646 return error_mark_node
;
1649 /* Substitute ARGS into the list of requirements T. Note that
1650 substitution failures here result in ill-formed programs. */
1653 tsubst_requirement_body (tree t
, tree args
,
1654 tsubst_flags_t complain
, tree in_decl
)
1659 tree e
= tsubst_requirement (TREE_VALUE (t
), args
, complain
, in_decl
);
1660 if (e
== error_mark_node
)
1661 return error_mark_node
;
1662 r
= tree_cons (NULL_TREE
, e
, r
);
1670 /* Substitute ARGS into the requires expression T. Note that this
1671 results in the re-declaration of local parameters when
1672 substituting through the parameter list. If either substitution
1673 fails, the program is ill-formed. */
1676 tsubst_requires_expr (tree t
, tree args
,
1677 tsubst_flags_t complain
, tree in_decl
)
1679 local_specialization_stack stack
;
1681 tree parms
= TREE_OPERAND (t
, 0);
1684 parms
= tsubst_constraint_variables (parms
, args
, complain
, in_decl
);
1685 if (parms
== error_mark_node
)
1686 return error_mark_node
;
1689 tree reqs
= TREE_OPERAND (t
, 1);
1690 reqs
= tsubst_requirement_body (reqs
, args
, complain
, in_decl
);
1691 if (reqs
== error_mark_node
)
1692 return error_mark_node
;
1694 return finish_requires_expr (parms
, reqs
);
1697 /* Substitute ARGS into the constraint information CI, producing a new
1698 constraint record. */
1700 tsubst_constraint_info (tree t
, tree args
,
1701 tsubst_flags_t complain
, tree in_decl
)
1703 if (!t
|| t
== error_mark_node
|| !check_constraint_info (t
))
1706 tree tmpl_constr
= NULL_TREE
;
1707 if (tree r
= CI_TEMPLATE_REQS (t
))
1708 tmpl_constr
= tsubst_constraint (r
, args
, complain
, in_decl
);
1710 tree decl_constr
= NULL_TREE
;
1711 if (tree r
= CI_DECLARATOR_REQS (t
))
1712 decl_constr
= tsubst_constraint (r
, args
, complain
, in_decl
);
1714 return build_constraints (tmpl_constr
, decl_constr
);
1718 /*---------------------------------------------------------------------------
1719 Constraint satisfaction
1720 ---------------------------------------------------------------------------*/
1722 /* The following functions determine if a constraint, when
1723 substituting template arguments, is satisfied. For convenience,
1724 satisfaction reduces a constraint to either true or false (and
1729 tree
satisfy_constraint_1 (tree
, tree
, tsubst_flags_t
, tree
);
1731 /* Check the constraint pack expansion. */
1734 satisfy_pack_expansion (tree t
, tree args
,
1735 tsubst_flags_t complain
, tree in_decl
)
1737 /* Get the vector of satisfaction results.
1738 gen_elem_of_pack_expansion_instantiation will check that each element of
1739 the expansion is satisfied. */
1740 tree exprs
= tsubst_pack_expansion (t
, args
, complain
, in_decl
);
1741 if (exprs
== error_mark_node
)
1742 return boolean_false_node
;
1743 int n
= TREE_VEC_LENGTH (exprs
);
1745 for (int i
= 0; i
< n
; ++i
)
1746 if (TREE_VEC_ELT (exprs
, i
) != boolean_true_node
)
1747 return boolean_false_node
;
1748 return boolean_true_node
;
1751 /* A predicate constraint is satisfied if its expression evaluates
1752 to true. If substitution into that node fails, the constraint
1753 is not satisfied ([temp.constr.pred]).
1755 Note that a predicate constraint is a constraint expression
1756 of type bool. If neither of those are true, the program is
1757 ill-formed; they are not SFINAE'able errors. */
1760 satisfy_predicate_constraint (tree t
, tree args
,
1761 tsubst_flags_t complain
, tree in_decl
)
1763 tree original
= TREE_OPERAND (t
, 0);
1765 /* We should never have a naked pack expansion in a predicate constraint. */
1766 gcc_assert (TREE_CODE (original
) != EXPR_PACK_EXPANSION
);
1768 tree expr
= tsubst_expr (original
, args
, complain
, in_decl
, false);
1769 if (expr
== error_mark_node
)
1770 return boolean_false_node
;
1772 /* A predicate constraint shall have type bool. In some
1773 cases, substitution gives us const-qualified bool, which
1774 is also acceptable. */
1775 tree type
= cv_unqualified (TREE_TYPE (expr
));
1776 if (!same_type_p (type
, boolean_type_node
))
1778 error_at (EXPR_LOC_OR_LOC (expr
, input_location
),
1779 "constraint %qE does not have type %qT",
1780 expr
, boolean_type_node
);
1781 return boolean_false_node
;
1784 tree value
= cxx_constant_value (expr
);
1788 /* Check an expression constraint. The constraint is satisfied if
1789 substitution succeeds ([temp.constr.expr]).
1791 Note that the expression is unevaluated. */
1794 satisfy_expression_constraint (tree t
, tree args
,
1795 tsubst_flags_t complain
, tree in_decl
)
1797 cp_unevaluated guard
;
1798 deferring_access_check_sentinel deferring
;
1800 tree expr
= EXPR_CONSTR_EXPR (t
);
1801 tree check
= tsubst_expr (expr
, args
, complain
, in_decl
, false);
1802 if (check
== error_mark_node
)
1803 return boolean_false_node
;
1804 if (!perform_deferred_access_checks (tf_none
))
1805 return boolean_false_node
;
1807 return boolean_true_node
;
1810 /* Check a type constraint. The constraint is satisfied if
1811 substitution succeeds. */
1814 satisfy_type_constraint (tree t
, tree args
,
1815 tsubst_flags_t complain
, tree in_decl
)
1817 deferring_access_check_sentinel deferring
;
1818 tree type
= TYPE_CONSTR_TYPE (t
);
1819 gcc_assert (TYPE_P (type
) || type
== error_mark_node
);
1820 tree check
= tsubst (type
, args
, complain
, in_decl
);
1821 if (error_operand_p (check
))
1822 return boolean_false_node
;
1823 if (!perform_deferred_access_checks (complain
))
1824 return boolean_false_node
;
1826 return boolean_true_node
;
1829 /* Check an implicit conversion constraint. */
1832 satisfy_implicit_conversion_constraint (tree t
, tree args
,
1833 tsubst_flags_t complain
, tree in_decl
)
1835 /* Don't tsubst as if we're processing a template. If we try
1836 to we can end up generating template-like expressions
1837 (e.g., modop-exprs) that aren't properly typed. */
1839 tsubst_expr (ICONV_CONSTR_EXPR (t
), args
, complain
, in_decl
, false);
1840 if (expr
== error_mark_node
)
1841 return boolean_false_node
;
1843 /* Get the transformed target type. */
1844 tree type
= tsubst (ICONV_CONSTR_TYPE (t
), args
, complain
, in_decl
);
1845 if (type
== error_mark_node
)
1846 return boolean_false_node
;
1848 /* Attempt the conversion as a direct initialization
1849 of the form TYPE <unspecified> = EXPR. */
1851 perform_direct_initialization_if_possible (type
, expr
, false, complain
);
1852 if (conv
== NULL_TREE
|| conv
== error_mark_node
)
1853 return boolean_false_node
;
1855 return boolean_true_node
;
1858 /* Check an argument deduction constraint. */
1861 satisfy_argument_deduction_constraint (tree t
, tree args
,
1862 tsubst_flags_t complain
, tree in_decl
)
1864 /* Substitute through the expression. */
1865 tree expr
= DEDUCT_CONSTR_EXPR (t
);
1866 tree init
= tsubst_expr (expr
, args
, complain
, in_decl
, false);
1867 if (expr
== error_mark_node
)
1868 return boolean_false_node
;
1870 /* Perform auto or decltype(auto) deduction to get the result. */
1871 tree pattern
= DEDUCT_CONSTR_PATTERN (t
);
1872 tree placeholder
= DEDUCT_CONSTR_PLACEHOLDER (t
);
1873 tree constr
= PLACEHOLDER_TYPE_CONSTRAINTS (placeholder
);
1874 tree type_canonical
= TYPE_CANONICAL (placeholder
);
1875 PLACEHOLDER_TYPE_CONSTRAINTS (placeholder
)
1876 = tsubst_constraint (constr
, args
, complain
|tf_partial
, in_decl
);
1877 TYPE_CANONICAL (placeholder
) = NULL_TREE
;
1878 tree type
= do_auto_deduction (pattern
, init
, placeholder
,
1879 complain
, adc_requirement
);
1880 PLACEHOLDER_TYPE_CONSTRAINTS (placeholder
) = constr
;
1881 TYPE_CANONICAL (placeholder
) = type_canonical
;
1882 if (type
== error_mark_node
)
1883 return boolean_false_node
;
1885 return boolean_true_node
;
1888 /* Check an exception constraint. An exception constraint for an
1889 expression e is satisfied when noexcept(e) is true. */
1892 satisfy_exception_constraint (tree t
, tree args
,
1893 tsubst_flags_t complain
, tree in_decl
)
1895 tree expr
= EXCEPT_CONSTR_EXPR (t
);
1896 tree check
= tsubst_expr (expr
, args
, complain
, in_decl
, false);
1897 if (check
== error_mark_node
)
1898 return boolean_false_node
;
1900 if (expr_noexcept_p (check
, complain
))
1901 return boolean_true_node
;
1903 return boolean_false_node
;
1906 /* Check a parameterized constraint. */
1909 satisfy_parameterized_constraint (tree t
, tree args
,
1910 tsubst_flags_t complain
, tree in_decl
)
1912 local_specialization_stack stack
;
1913 tree parms
= PARM_CONSTR_PARMS (t
);
1914 tree vars
= tsubst_constraint_variables (parms
, args
, complain
, in_decl
);
1915 if (vars
== error_mark_node
)
1916 return boolean_false_node
;
1917 tree constr
= PARM_CONSTR_OPERAND (t
);
1918 return satisfy_constraint_1 (constr
, args
, complain
, in_decl
);
1921 /* Check that the conjunction of constraints is satisfied. Note
1922 that if left operand is not satisfied, the right operand
1925 FIXME: Check that this wouldn't result in a user-defined
1926 operator. Note that this error is partially diagnosed in
1927 satisfy_predicate_constraint. It would be nice to diagnose
1928 the overload, but I don't think it's strictly necessary. */
1931 satisfy_conjunction (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
1933 tree t0
= satisfy_constraint_1 (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
1934 if (t0
== boolean_false_node
)
1936 tree t1
= satisfy_constraint_1 (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
1937 if (t1
== boolean_false_node
)
1939 return boolean_true_node
;
1942 /* Check that the disjunction of constraints is satisfied. Note
1943 that if the left operand is satisfied, the right operand is not
1947 satisfy_disjunction (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
1949 tree t0
= satisfy_constraint_1 (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
1950 if (t0
== boolean_true_node
)
1951 return boolean_true_node
;
1952 tree t1
= satisfy_constraint_1 (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
1953 if (t1
== boolean_true_node
)
1954 return boolean_true_node
;
1955 return boolean_false_node
;
1958 /* Dispatch to an appropriate satisfaction routine depending on the
1962 satisfy_constraint_1 (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
1964 gcc_assert (!processing_template_decl
);
1967 return boolean_false_node
;
1969 if (t
== error_mark_node
)
1970 return boolean_false_node
;
1972 switch (TREE_CODE (t
))
1975 return satisfy_predicate_constraint (t
, args
, complain
, in_decl
);
1978 return satisfy_expression_constraint (t
, args
, complain
, in_decl
);
1981 return satisfy_type_constraint (t
, args
, complain
, in_decl
);
1984 return satisfy_implicit_conversion_constraint (t
, args
, complain
, in_decl
);
1987 return satisfy_argument_deduction_constraint (t
, args
, complain
, in_decl
);
1990 return satisfy_exception_constraint (t
, args
, complain
, in_decl
);
1993 return satisfy_parameterized_constraint (t
, args
, complain
, in_decl
);
1996 return satisfy_conjunction (t
, args
, complain
, in_decl
);
1999 return satisfy_disjunction (t
, args
, complain
, in_decl
);
2001 case EXPR_PACK_EXPANSION
:
2002 return satisfy_pack_expansion (t
, args
, complain
, in_decl
);
2007 return boolean_false_node
;
2010 /* Check that the constraint is satisfied, according to the rules
2011 for that constraint. Note that each satisfy_* function returns
2012 true or false, depending on whether it is satisfied or not. */
2015 satisfy_constraint (tree t
, tree args
)
2017 /* Turn off template processing. Constraint satisfaction only applies
2018 to non-dependent terms, so we want full checking here. */
2019 processing_template_decl_sentinel
sentinel (true);
2020 /* Avoid early exit in tsubst and tsubst_copy from null args; since earlier
2021 substitution was done with processing_template_decl forced on, there will
2022 be expressions that still need semantic processing, possibly buried in
2023 decltype or a template argument. */
2024 if (args
== NULL_TREE
)
2025 args
= make_tree_vec (1);
2026 return satisfy_constraint_1 (t
, args
, tf_none
, NULL_TREE
);
2029 /* Check the associated constraints in CI against the given
2030 ARGS, returning true when the constraints are satisfied
2031 and false otherwise. */
2034 satisfy_associated_constraints (tree ci
, tree args
)
2036 /* If there are no constraints then this is trivially satisfied. */
2038 return boolean_true_node
;
2040 /* If any arguments depend on template parameters, we can't
2041 check constraints. */
2042 if (args
&& uses_template_parms (args
))
2043 return boolean_true_node
;
2045 /* Invalid requirements cannot be satisfied. */
2046 if (!valid_constraints_p (ci
))
2047 return boolean_false_node
;
2049 return satisfy_constraint (CI_NORMALIZED_CONSTRAINTS (ci
), args
);
2054 /* Evaluate the given constraint, returning boolean_true_node
2055 if the constraint is satisfied and boolean_false_node
2059 evaluate_constraints (tree constr
, tree args
)
2061 gcc_assert (constraint_p (constr
));
2062 return satisfy_constraint (normalize_constraint (constr
), args
);
2065 /* Evaluate the function concept FN by substituting its own args
2066 into its definition and evaluating that as the result. Returns
2067 boolean_true_node if the constraints are satisfied and
2068 boolean_false_node otherwise. */
2071 evaluate_function_concept (tree fn
, tree args
)
2073 ++processing_template_decl
;
2074 /* We lift using DECL_TI_ARGS because we want to delay producing
2075 non-dependent expressions until we're doing satisfaction. We can't just
2076 go without any substitution because we need to lower the level of 'auto's
2077 in type deduction constraints. */
2078 tree constr
= transform_expression (lift_function_definition
2079 (fn
, DECL_TI_ARGS (fn
)));
2080 --processing_template_decl
;
2081 return satisfy_constraint (constr
, args
);
2084 /* Evaluate the variable concept VAR by substituting its own args into
2085 its initializer and checking the resulting constraint. Returns
2086 boolean_true_node if the constraints are satisfied and
2087 boolean_false_node otherwise. */
2090 evaluate_variable_concept (tree decl
, tree args
)
2092 ++processing_template_decl
;
2093 tree constr
= transform_expression (lift_variable_initializer
2094 (decl
, DECL_TI_ARGS (decl
)));
2095 --processing_template_decl
;
2096 return satisfy_constraint (constr
, args
);
2099 /* Evaluate the given expression as if it were a predicate
2100 constraint. Returns boolean_true_node if the constraint
2101 is satisfied and boolean_false_node otherwise. */
2104 evaluate_constraint_expression (tree expr
, tree args
)
2106 ++processing_template_decl
;
2107 tree constr
= transform_expression (lift_expression (expr
));
2108 --processing_template_decl
;
2109 return satisfy_constraint (constr
, args
);
2112 /* Returns true if the DECL's constraints are satisfied.
2113 This is used in cases where a declaration is formed but
2114 before it is used (e.g., overload resolution). */
2117 constraints_satisfied_p (tree decl
)
2119 /* Get the constraints to check for satisfaction. This depends
2120 on whether we're looking at a template specialization or not. */
2122 tree args
= NULL_TREE
;
2123 if (tree ti
= DECL_TEMPLATE_INFO (decl
))
2125 ci
= get_constraints (TI_TEMPLATE (ti
));
2126 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti
));
2130 ci
= get_constraints (decl
);
2133 tree eval
= satisfy_associated_constraints (ci
, args
);
2134 return eval
== boolean_true_node
;
2137 /* Returns true if the constraints are satisfied by ARGS.
2138 Here, T can be either a constraint or a constrained
2142 constraints_satisfied_p (tree t
, tree args
)
2145 if (constraint_p (t
))
2146 eval
= evaluate_constraints (t
, args
);
2148 eval
= satisfy_associated_constraints (get_constraints (t
), args
);
2149 return eval
== boolean_true_node
;
2155 /* Normalize EXPR and determine if the resulting constraint is
2156 satisfied by ARGS. Returns true if and only if the constraint
2157 is satisfied. This is used extensively by diagnostics to
2158 determine causes for failure. */
2161 constraint_expression_satisfied_p (tree expr
, tree args
)
2163 return evaluate_constraint_expression (expr
, args
) == boolean_true_node
;
2169 /*---------------------------------------------------------------------------
2170 Semantic analysis of requires-expressions
2171 ---------------------------------------------------------------------------*/
2173 /* Finish a requires expression for the given PARMS (possibly
2174 null) and the non-empty sequence of requirements. */
2176 finish_requires_expr (tree parms
, tree reqs
)
2178 /* Modify the declared parameters by removing their context
2179 so they don't refer to the enclosing scope and explicitly
2180 indicating that they are constraint variables. */
2181 for (tree parm
= parms
; parm
; parm
= DECL_CHAIN (parm
))
2183 DECL_CONTEXT (parm
) = NULL_TREE
;
2184 CONSTRAINT_VAR_P (parm
) = true;
2187 /* Build the node. */
2188 tree r
= build_min (REQUIRES_EXPR
, boolean_type_node
, parms
, reqs
);
2189 TREE_SIDE_EFFECTS (r
) = false;
2190 TREE_CONSTANT (r
) = true;
2194 /* Construct a requirement for the validity of EXPR. */
2196 finish_simple_requirement (tree expr
)
2198 return build_nt (SIMPLE_REQ
, expr
);
2201 /* Construct a requirement for the validity of TYPE. */
2203 finish_type_requirement (tree type
)
2205 return build_nt (TYPE_REQ
, type
);
2208 /* Construct a requirement for the validity of EXPR, along with
2209 its properties. if TYPE is non-null, then it specifies either
2210 an implicit conversion or argument deduction constraint,
2211 depending on whether any placeholders occur in the type name.
2212 NOEXCEPT_P is true iff the noexcept keyword was specified. */
2214 finish_compound_requirement (tree expr
, tree type
, bool noexcept_p
)
2216 tree req
= build_nt (COMPOUND_REQ
, expr
, type
);
2217 COMPOUND_REQ_NOEXCEPT_P (req
) = noexcept_p
;
2221 /* Finish a nested requirement. */
2223 finish_nested_requirement (tree expr
)
2225 return build_nt (NESTED_REQ
, expr
);
2228 // Check that FN satisfies the structural requirements of a
2229 // function concept definition.
2231 check_function_concept (tree fn
)
2233 // Check that the function is comprised of only a single
2234 // return statement.
2235 tree body
= DECL_SAVED_TREE (fn
);
2236 if (TREE_CODE (body
) == BIND_EXPR
)
2237 body
= BIND_EXPR_BODY (body
);
2239 // Sometimes a function call results in the creation of clean up
2240 // points. Allow these to be preserved in the body of the
2241 // constraint, as we might actually need them for some constexpr
2243 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
2244 body
= TREE_OPERAND (body
, 0);
2246 /* Check that the definition is written correctly. */
2247 if (TREE_CODE (body
) != RETURN_EXPR
)
2249 location_t loc
= DECL_SOURCE_LOCATION (fn
);
2250 if (TREE_CODE (body
) == STATEMENT_LIST
&& !STATEMENT_LIST_HEAD (body
))
2251 error_at (loc
, "definition of concept %qD is empty", fn
);
2253 error_at (loc
, "definition of concept %qD has multiple statements", fn
);
2260 // Check that a constrained friend declaration function declaration,
2261 // FN, is admissible. This is the case only when the declaration depends
2262 // on template parameters and does not declare a specialization.
2264 check_constrained_friend (tree fn
, tree reqs
)
2266 if (fn
== error_mark_node
)
2268 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
2270 // If there are not constraints, this cannot be an error.
2274 // Constrained friend functions that don't depend on template
2275 // arguments are effectively meaningless.
2276 if (!uses_template_parms (TREE_TYPE (fn
)))
2278 error_at (location_of (fn
),
2279 "constrained friend does not depend on template parameters");
2284 /*---------------------------------------------------------------------------
2285 Equivalence of constraints
2286 ---------------------------------------------------------------------------*/
2288 /* Returns true when A and B are equivalent constraints. */
2290 equivalent_constraints (tree a
, tree b
)
2292 gcc_assert (!a
|| TREE_CODE (a
) == CONSTRAINT_INFO
);
2293 gcc_assert (!b
|| TREE_CODE (b
) == CONSTRAINT_INFO
);
2294 return cp_tree_equal (a
, b
);
2297 /* Returns true if the template declarations A and B have equivalent
2298 constraints. This is the case when A's constraints subsume B's and
2299 when B's also constrain A's. */
2301 equivalently_constrained (tree d1
, tree d2
)
2303 gcc_assert (TREE_CODE (d1
) == TREE_CODE (d2
));
2304 return equivalent_constraints (get_constraints (d1
), get_constraints (d2
));
2307 /*---------------------------------------------------------------------------
2308 Partial ordering of constraints
2309 ---------------------------------------------------------------------------*/
2311 /* Returns true when the the constraints in A subsume those in B. */
2313 subsumes_constraints (tree a
, tree b
)
2315 gcc_assert (!a
|| TREE_CODE (a
) == CONSTRAINT_INFO
);
2316 gcc_assert (!b
|| TREE_CODE (b
) == CONSTRAINT_INFO
);
2317 return subsumes (a
, b
);
2320 /* Returns true when the the constraints in A subsume those in B, but
2321 the constraints in B do not subsume the constraints in A. */
2324 strictly_subsumes (tree a
, tree b
)
2326 return subsumes (a
, b
) && !subsumes (b
, a
);
2329 /* Determines which of the declarations, A or B, is more constrained.
2330 That is, which declaration's constraints subsume but are not subsumed
2333 Returns 1 if A is more constrained than B, -1 if B is more constrained
2334 than A, and 0 otherwise. */
2336 more_constrained (tree d1
, tree d2
)
2338 tree c1
= get_constraints (d1
);
2339 tree c2
= get_constraints (d2
);
2341 if (subsumes_constraints (c1
, c2
))
2343 if (subsumes_constraints (c2
, c1
))
2348 /* Returns true if D1 is at least as constrained as D2. That is, the
2349 associated constraints of D1 subsume those of D2, or both declarations
2350 are unconstrained. */
2352 at_least_as_constrained (tree d1
, tree d2
)
2354 tree c1
= get_constraints (d1
);
2355 tree c2
= get_constraints (d2
);
2356 return subsumes_constraints (c1
, c2
);
2360 /*---------------------------------------------------------------------------
2361 Constraint diagnostics
2362 ---------------------------------------------------------------------------*/
2364 /* The diagnosis of constraints performs a combination of
2365 normalization and satisfaction testing. We recursively
2366 walk through the conjunction (or disjunctions) of associated
2367 constraints, testing each sub-expression in turn.
2369 We currently restrict diagnostics to just the top-level
2370 conjunctions within the associated constraints. A fully
2371 recursive walk is possible, but it can generate a lot
2377 void diagnose_expression (location_t
, tree
, tree
);
2378 void diagnose_constraint (location_t
, tree
, tree
);
2380 /* Diagnose a conjunction of constraints. */
2382 diagnose_logical_operation (location_t loc
, tree t
, tree args
)
2384 diagnose_expression (loc
, TREE_OPERAND (t
, 0), args
);
2385 diagnose_expression (loc
, TREE_OPERAND (t
, 0), args
);
2388 /* Determine if the trait expression T is satisfied by ARGS.
2389 Emit a precise diagnostic if it is not. */
2391 diagnose_trait_expression (location_t loc
, tree t
, tree args
)
2393 if (constraint_expression_satisfied_p (t
, args
))
2396 /* Rebuild the trait expression so we can diagnose the
2397 specific failure. */
2398 ++processing_template_decl
;
2399 tree expr
= tsubst_expr (t
, args
, tf_none
, NULL_TREE
, false);
2400 --processing_template_decl
;
2402 tree t1
= TRAIT_EXPR_TYPE1 (expr
);
2403 tree t2
= TRAIT_EXPR_TYPE2 (expr
);
2404 switch (TRAIT_EXPR_KIND (t
))
2406 case CPTK_HAS_NOTHROW_ASSIGN
:
2407 inform (loc
, " %qT is not nothrow copy assignable", t1
);
2409 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
2410 inform (loc
, " %qT is not nothrow default constructible", t1
);
2412 case CPTK_HAS_NOTHROW_COPY
:
2413 inform (loc
, " %qT is not nothrow copy constructible", t1
);
2415 case CPTK_HAS_TRIVIAL_ASSIGN
:
2416 inform (loc
, " %qT is not trivially copy assignable", t1
);
2418 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
2419 inform (loc
, " %qT is not trivially default constructible", t1
);
2421 case CPTK_HAS_TRIVIAL_COPY
:
2422 inform (loc
, " %qT is not trivially copy constructible", t1
);
2424 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
2425 inform (loc
, " %qT is not trivially destructible", t1
);
2427 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
2428 inform (loc
, " %qT does not have a virtual destructor", t1
);
2430 case CPTK_IS_ABSTRACT
:
2431 inform (loc
, " %qT is not an abstract class", t1
);
2433 case CPTK_IS_BASE_OF
:
2434 inform (loc
, " %qT is not a base of %qT", t1
, t2
);
2437 inform (loc
, " %qT is not a class", t1
);
2440 inform (loc
, " %qT is not an empty class", t1
);
2443 inform (loc
, " %qT is not an enum", t1
);
2446 inform (loc
, " %qT is not a final class", t1
);
2448 case CPTK_IS_LITERAL_TYPE
:
2449 inform (loc
, " %qT is not a literal type", t1
);
2452 inform (loc
, " %qT is not a POD type", t1
);
2454 case CPTK_IS_POLYMORPHIC
:
2455 inform (loc
, " %qT is not a polymorphic type", t1
);
2457 case CPTK_IS_SAME_AS
:
2458 inform (loc
, " %qT is not the same as %qT", t1
, t2
);
2460 case CPTK_IS_STD_LAYOUT
:
2461 inform (loc
, " %qT is not an standard layout type", t1
);
2463 case CPTK_IS_TRIVIAL
:
2464 inform (loc
, " %qT is not a trivial type", t1
);
2467 inform (loc
, " %qT is not a union", t1
);
2474 /* Determine if the call expression T, when normalized as a constraint,
2475 is satisfied by ARGS.
2477 TODO: If T is refers to a concept, We could recursively analyze
2478 its definition to identify the exact failure, but that could
2479 emit a *lot* of error messages (defeating the purpose of
2480 improved diagnostics). Consider adding a flag to control the
2481 depth of diagnostics. */
2483 diagnose_call_expression (location_t loc
, tree t
, tree args
)
2485 if (constraint_expression_satisfied_p (t
, args
))
2488 /* Rebuild the expression for the purpose of diagnostics. */
2489 ++processing_template_decl
;
2490 tree expr
= tsubst_expr (t
, args
, tf_none
, NULL_TREE
, false);
2491 --processing_template_decl
;
2493 /* If the function call is known to be a concept check, then
2494 diagnose it differently (i.e., we may recurse). */
2495 if (resolve_constraint_check (t
))
2496 inform (loc
, " concept %qE was not satisfied", expr
);
2498 inform (loc
, " %qE evaluated to false", expr
);
2501 /* Determine if the template-id T, when normalized as a constraint
2502 is satisfied by ARGS. */
2504 diagnose_template_id (location_t loc
, tree t
, tree args
)
2506 /* Check for invalid template-ids. */
2507 if (!variable_template_p (TREE_OPERAND (t
, 0)))
2509 inform (loc
, " invalid constraint %qE", t
);
2513 if (constraint_expression_satisfied_p (t
, args
))
2516 /* Rebuild the expression for the purpose of diagnostics. */
2517 ++processing_template_decl
;
2518 tree expr
= tsubst_expr (t
, args
, tf_none
, NULL_TREE
, false);
2519 --processing_template_decl
;
2521 tree var
= DECL_TEMPLATE_RESULT (TREE_OPERAND (t
, 0));
2522 if (DECL_DECLARED_CONCEPT_P (var
))
2523 inform (loc
, " concept %qE was not satisfied", expr
);
2525 inform (loc
, " %qE evaluated to false", expr
);
2528 /* Determine if the requires-expression, when normalized as a
2529 constraint is satisfied by ARGS.
2531 TODO: Build sets of expressions, types, and constraints
2532 based on the requirements in T and emit specific diagnostics
2535 diagnose_requires_expression (location_t loc
, tree t
, tree args
)
2537 if (constraint_expression_satisfied_p (t
, args
))
2539 inform (loc
, "requirements not satisfied");
2543 diagnose_pack_expansion (location_t loc
, tree t
, tree args
)
2545 if (constraint_expression_satisfied_p (t
, args
))
2548 /* Make sure that we don't have naked packs that we don't expect. */
2549 if (!same_type_p (TREE_TYPE (t
), boolean_type_node
))
2551 inform (loc
, "invalid pack expansion in constraint %qE", t
);
2555 inform (loc
, " in the expansion of %qE", t
);
2557 /* Get the vector of expanded arguments. Note that n must not
2558 be 0 since this constraint is not satisfied. */
2559 ++processing_template_decl
;
2560 tree exprs
= tsubst_pack_expansion (t
, args
, tf_none
, NULL_TREE
);
2561 --processing_template_decl
;
2562 if (exprs
== error_mark_node
)
2564 /* TODO: This error message could be better. */
2565 inform (loc
, " substitution failure occurred during expansion");
2569 /* Check each expanded constraint separately. */
2570 int n
= TREE_VEC_LENGTH (exprs
);
2571 for (int i
= 0; i
< n
; ++i
)
2573 tree expr
= TREE_VEC_ELT (exprs
, i
);
2574 if (!constraint_expression_satisfied_p (expr
, args
))
2575 inform (loc
, " %qE was not satisfied", expr
);
2579 /* Diagnose an expression that would be characterized as
2580 a predicate constraint. */
2582 diagnose_other_expression (location_t loc
, tree t
, tree args
)
2584 if (constraint_expression_satisfied_p (t
, args
))
2586 inform (loc
, " %qE evaluated to false", t
);
2590 diagnose_expression (location_t loc
, tree t
, tree args
)
2592 switch (TREE_CODE (t
))
2594 case TRUTH_ANDIF_EXPR
:
2595 diagnose_logical_operation (loc
, t
, args
);
2598 case TRUTH_ORIF_EXPR
:
2599 diagnose_logical_operation (loc
, t
, args
);
2603 diagnose_call_expression (loc
, t
, args
);
2606 case TEMPLATE_ID_EXPR
:
2607 diagnose_template_id (loc
, t
, args
);
2611 diagnose_requires_expression (loc
, t
, args
);
2615 diagnose_trait_expression (loc
, t
, args
);
2618 case EXPR_PACK_EXPANSION
:
2619 diagnose_pack_expansion (loc
, t
, args
);
2623 diagnose_other_expression (loc
, t
, args
);
2629 diagnose_predicate_constraint (location_t loc
, tree t
, tree args
)
2631 diagnose_expression (loc
, PRED_CONSTR_EXPR (t
), args
);
2635 diagnose_conjunction (location_t loc
, tree t
, tree args
)
2637 diagnose_constraint (loc
, TREE_OPERAND (t
, 0), args
);
2638 diagnose_constraint (loc
, TREE_OPERAND (t
, 1), args
);
2641 /* Diagnose the constraint T for the given ARGS. This is only
2642 ever invoked on the associated constraints, so we can
2643 only have conjunctions of predicate constraints. */
2645 diagnose_constraint (location_t loc
, tree t
, tree args
)
2647 switch (TREE_CODE (t
))
2650 diagnose_conjunction (loc
, t
, args
);
2654 diagnose_predicate_constraint (loc
, t
, args
);
2663 /* Diagnose the reason(s) why ARGS do not satisfy the constraints
2664 of declaration DECL. */
2667 diagnose_declaration_constraints (location_t loc
, tree decl
, tree args
)
2669 inform (loc
, " constraints not satisfied");
2671 /* Constraints are attached to the template. */
2672 if (tree ti
= DECL_TEMPLATE_INFO (decl
))
2674 decl
= TI_TEMPLATE (ti
);
2676 args
= TI_ARGS (ti
);
2679 /* Check that the constraints are actually valid. */
2680 tree ci
= get_constraints (decl
);
2681 if (!valid_constraints_p (ci
))
2683 inform (loc
, " invalid constraints");
2687 /* Recursively diagnose the associated constraints. */
2688 diagnose_constraint (loc
, CI_ASSOCIATED_CONSTRAINTS (ci
), args
);
2693 /* Emit diagnostics detailing the failure ARGS to satisfy the
2694 constraints of T. Here, T can be either a constraint
2695 or a declaration. */
2698 diagnose_constraints (location_t loc
, tree t
, tree args
)
2700 if (constraint_p (t
))
2701 diagnose_constraint (loc
, t
, args
);
2703 diagnose_declaration_constraints (loc
, t
, args
);