1 /* Process declarations and variables for C++ compiler.
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/>. */
21 // Components for processing constraints and evaluating constraints.
25 #include "coretypes.h"
29 #include "c-family/c-common.h"
30 #include "c-family/c-objc.h"
31 #include "tree-inline.h"
36 #include "diagnostic.h"
38 #include "tree-iterator.h"
45 // -------------------------------------------------------------------------- //
46 // Requirement Construction
48 // Facilities for building and manipulating template requirements.
50 // TODO: Simply assigning boolean_type_node to the result type of the
51 // expression seems right for constraints, but in the long-term we might want
52 // to be more flexible (i.e., allow some form of overload resolution?).
54 // Create a new logical node joining the subexpressions a and b.
56 join_requirements (tree_code c
, tree a
, tree b
)
58 gcc_assert (a
!= NULL_TREE
&& b
!= NULL_TREE
);
59 gcc_assert (c
== TRUTH_ANDIF_EXPR
|| c
== TRUTH_ORIF_EXPR
);
60 return build_min (c
, boolean_type_node
, a
, b
);
63 // Returns the conjunction of two requirements A and B, where A and B are
64 // reduced terms in the constraints language. Note that conjoining a non-null
65 // expression with NULL_TREE is an identity operation. That is, for some
68 // conjoin_requirements(a, NULL_TREE) == a
70 // If both A and B are NULL_TREE, the result is also NULL_TREE.
72 conjoin_requirements (tree a
, tree b
)
75 return b
? join_requirements (TRUTH_ANDIF_EXPR
, a
, b
) : a
;
83 // -------------------------------------------------------------------------- //
84 // Constraint Resolution
86 // This facility is used to resolve constraint checks from requirement
87 // expressions. A constraint check is a call to a function template, declared
90 // The result of resolution is a pair (a list node) whose value is the
91 // matched declaration, and whose purpose contains the coerced template
92 // arguments that can be substituted into the call.
96 // Given an overload set, try to find a unique definition that can be
97 // instantiated by the template arguments.
99 // This function is not called for arbitrary call expressions. In particular,
100 // the call expression must be written with explicit template arguments
101 // and no function arguments. For example:
105 // The overload set will contain only template declarations.
107 // If a single definition is found, this returns a list node whose VALUE
108 // is the constraint function (not the template), and its PURPOSE is
109 // the complete set of arguments substituted into the parameter list.
111 resolve_constraint_check (tree ovl
, tree args
)
113 tree cands
= NULL_TREE
;
114 for (tree p
= ovl
; p
!= NULL_TREE
; p
= OVL_NEXT (p
))
116 tree tmpl
= OVL_FUNCTION (p
);
117 tree parms
= TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl
));
119 // Remember the candidate if we can deduce a substitution.
120 if (tree subst
= coerce_template_parms (parms
, args
, tmpl
))
121 if (subst
!= error_mark_node
)
122 cands
= tree_cons (subst
, DECL_TEMPLATE_RESULT (tmpl
), cands
);
125 // If we didn't find a unique candidate, then this is
126 // not a constraint check.
127 if (!cands
|| TREE_CHAIN (cands
))
130 // Constraints must be declared concepts.
131 tree decl
= TREE_VALUE (cands
);
132 if (!DECL_DECLARED_CONCEPT_P (decl
))
135 // Concept declarations must have a corresponding definition.
137 // TODO: This should be part of the up-front checking for
138 // a concept declaration.
139 if (!DECL_SAVED_TREE (decl
))
141 error_at (DECL_SOURCE_LOCATION (decl
),
142 "concept %q#D has no definition", decl
);
149 // Determine if the the call expression CALL is a constraint check, and
150 // return the concept declaration and arguments being checked. If CALL
151 // does not denote a constraint check, return NULL.
153 resolve_constraint_check (tree call
)
155 gcc_assert (TREE_CODE (call
) == CALL_EXPR
);
157 // A constraint check must be only be a template-id expression.
158 tree target
= CALL_EXPR_FN (call
);
159 if (TREE_CODE (target
) != TEMPLATE_ID_EXPR
)
162 // Get the overload set and template arguments and try to
163 // resolve the target.
164 tree ovl
= TREE_OPERAND (target
, 0);
165 tree args
= TREE_OPERAND (target
, 1);
166 return resolve_constraint_check (ovl
, args
);
172 // -------------------------------------------------------------------------- //
173 // Requirement Reduction
175 // Reduces a template requirement to a logical formula written in terms of
176 // atomic propositions, returing the new expression. If the expression cannot
177 // be reduced, a NULL_TREE is returned, indicating failure to reduce the
178 // original requirment.
183 static tree
reduce_node (tree
);
184 static tree
reduce_expr (tree
);
185 static tree
reduce_stmt (tree
);
186 static tree
reduce_decl (tree
);
187 static tree
reduce_misc (tree
);
189 static tree
reduce_logical (tree
);
190 static tree
reduce_call (tree
);
191 static tree
reduce_template_id (tree
);
192 static tree
reduce_stmt_list (tree
);
194 // Reduce the requirement T into a logical formula written in terms of
195 // atomic propositions.
199 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
205 return reduce_expr (t
);
208 return reduce_stmt (t
);
210 case tcc_declaration
:
211 return reduce_decl (t
);
213 case tcc_exceptional
:
214 return reduce_misc (t
);
216 // These kinds of expressions are atomic.
228 // Reduction rules for the expression node T.
232 switch (TREE_CODE (t
))
234 case TRUTH_ANDIF_EXPR
:
235 case TRUTH_ORIF_EXPR
:
236 return reduce_logical (t
);
239 return reduce_call (t
);
241 case TEMPLATE_ID_EXPR
:
242 return reduce_template_id (t
);
245 return reduce_node (TREE_VALUE (TREE_OPERAND (t
, 0)));
248 return reduce_node (BIND_EXPR_BODY (t
));
254 // Everything else is atomic.
261 // Reduction rules for the statement T.
265 switch (TREE_CODE (t
))
267 // Reduce the returned expression.
269 return reduce_node (TREE_OPERAND (t
, 0));
271 // These statements do not introduce propositions
272 // in the constraints language. Do not recurse.
283 // Reduction rules for the declaration T.
287 switch (TREE_CODE (t
))
289 // References to var decls are atomic.
299 // Reduction rules for the node T.
303 switch (TREE_CODE (t
))
305 // Errors and traits are atomic.
311 return reduce_stmt_list (t
);
319 // Reduction rules for the binary logical expression T (&& and ||).
321 // Generate a new expression from the reduced operands. If either operand
322 // cannot be reduced, then the resulting expression is null.
324 reduce_logical (tree t
)
326 tree l
= reduce_expr (TREE_OPERAND (t
, 0));
327 tree r
= reduce_expr (TREE_OPERAND (t
, 1));
331 TREE_OPERAND (t
, 0) = l
;
332 TREE_OPERAND (t
, 1) = r
;
339 // Reduction rules for the call expression T.
341 // If T is a call to a constraint instantiate its definition and
342 // recursively reduce its returned expression.
346 // Is the function call actually a constraint check?
347 tree check
= resolve_constraint_check (t
);
351 tree fn
= TREE_VALUE (check
);
352 tree args
= TREE_PURPOSE (check
);
354 // Reduce the body of the function into the constriants language.
355 tree body
= reduce_requirements (DECL_SAVED_TREE (fn
));
358 error ("could not inline requirements from %qD", fn
);
359 return error_mark_node
;
362 // Instantiate the reduced results using the deduced args.
363 tree result
= instantiate_requirements (body
, args
);
364 if (result
== error_mark_node
)
366 error ("could not instantiate requirements from %qD", fn
);
367 return error_mark_node
;
372 // Reduction rules for the template-id T.
374 // It turns out that we often get requirements being written like this:
376 // template<typename T>
380 // Where Foo<T> should actually be written as Foo<T>(). Generate an
381 // error and suggest the improved writing.
383 reduce_template_id (tree t
)
385 vec
<tree
, va_gc
>* args
= NULL
;
386 tree c
= finish_call_expr (t
, &args
, true, false, 0);
387 error_at (EXPR_LOC_OR_HERE (t
), "invalid requirement");
388 inform (EXPR_LOC_OR_HERE (t
), "did you mean %qE", c
);
392 // Reduction rules for the statement list STMTS.
394 // Recursively reduce each statement in the list, concatenating each
395 // reduced result into a conjunction of requirements.
397 // A constexpr function may include statements other than a return
398 // statement. The primary purpose of these rules is to filter those
399 // non-return statements from the constraints language.
401 reduce_stmt_list (tree stmts
)
403 tree lhs
= NULL_TREE
;
404 tree_stmt_iterator i
= tsi_start (stmts
);
405 while (!tsi_end_p (i
))
407 if (tree rhs
= reduce_node (tsi_stmt (i
)))
412 lhs
= conjoin_requirements (lhs
, rhs
);
422 // Reduce the requirement REQS into a logical formula written in terms of
423 // atomic propositions.
425 reduce_requirements (tree reqs
)
427 return reduce_node (reqs
);
430 // Create a constraint-info node from the specified requirements.
432 make_constraints (tree reqs
)
434 // No requirements == no constraints
438 // Reduce the requirements into atoms and break them into
439 // sets of atomic propositions.
440 tree atomic
= reduce_requirements (reqs
);
441 if (atomic
== error_mark_node
)
442 return error_mark_node
;
443 tree assume
= decompose_assumptions (atomic
);
445 tree_constraint_info
*cinfo
=
446 (tree_constraint_info
*)make_node (CONSTRAINT_INFO
);
447 cinfo
->spelling
= reqs
;
448 cinfo
->requirements
= atomic
;
449 cinfo
->assumptions
= assume
;
457 get_type_constraints (tree t
)
459 // Template template arguments may not have template info.
460 if (!TYPE_TEMPLATE_INFO (t
))
462 return TYPE_TEMPLATE_CONSTRAINT (t
);
466 get_decl_constraints (tree t
)
468 if (TREE_CODE (t
) == TEMPLATE_DECL
)
470 tree d
= DECL_TEMPLATE_RESULT (t
);
471 if (TREE_CODE (d
) == TYPE_DECL
)
472 return get_type_constraints (TREE_TYPE (t
));
474 return get_decl_constraints (d
);
476 return DECL_TEMPLATE_CONSTRAINT (t
);
481 // Return constraint info for the node T, regardless of the
484 get_constraints (tree t
)
487 return get_type_constraints (t
);
489 return get_decl_constraints (t
);
495 // Returns true if the requirements expression REQS is satisfied
496 // and false otherwise. The requirements are checked by simply
497 // evaluating REQS as a constant expression.
499 check_requirements (tree reqs
)
501 // Simplify the expression before evaluating it. This will
502 // cause TRAIT_EXPR nodes to be reduced before constexpr
504 reqs
= fold_non_dependent_expr (reqs
);
506 // Requirements are satisfied when REQS evaluates to true.
507 return cxx_constant_value (reqs
) == boolean_true_node
;
510 // Returns true if the requirements expression REQS is satisfied
511 // and false otherwise. The requirements are checked by first
512 // instantiating REQS and then evaluating it as a constant expression.
514 check_requirements (tree reqs
, tree args
)
516 reqs
= instantiate_requirements (reqs
, args
);
517 if (reqs
== error_mark_node
)
519 return check_requirements (reqs
);
522 // Check the instantiated declaration constraints.
524 check_constraints (tree cinfo
)
528 return check_requirements (CI_REQUIREMENTS (cinfo
));
531 // Check the constraints in CINFO against the given ARGS, returning
532 // true when the constraints are satisfied and false otherwise.
534 check_constraints (tree cinfo
, tree args
)
536 // No constraints? Satisfied.
539 return check_requirements (CI_REQUIREMENTS (cinfo
), args
);
543 check_type_constraints (tree t
, tree args
)
545 return check_constraints (CLASSTYPE_TEMPLATE_CONSTRAINT (t
), args
);
549 check_decl_constraints (tree t
, tree args
)
551 if (TREE_CODE (t
) == TEMPLATE_DECL
)
552 return check_decl_constraints (DECL_TEMPLATE_RESULT (t
), args
);
554 return check_constraints (DECL_TEMPLATE_CONSTRAINT (t
), args
);
557 // Check the constraints of the declaration or type T, against
558 // the specified arguments. Returns true if the constraints are
559 // satisfied and false otherwise.
561 check_template_constraints (tree t
, tree args
)
563 return check_constraints (get_constraints (t
), args
);
566 // Returns true when A and B are equivalent constraints.
568 equivalent_constraints (tree a
, tree b
)
570 return subsumes (a
, b
) && subsumes (b
, a
);
573 // Returns true if the template declarations A and B have equivalent
574 // constraints. This is the case when A's constraints subsume B's and
575 // when B's also constrain A's.
577 equivalently_constrained (tree a
, tree b
)
579 gcc_assert (TREE_CODE (a
) == TREE_CODE (b
));
580 return equivalent_constraints (get_constraints (a
), get_constraints (b
));
583 // Returns true when the A contains more atomic properties than B.
585 more_constraints (tree a
, tree b
)
587 return subsumes (a
, b
);
590 // Returns true when the template declaration A's constraints subsume
591 // those of the template declaration B.
593 more_constrained (tree a
, tree b
)
595 gcc_assert (TREE_CODE (a
) == TREE_CODE (b
));
596 return more_constraints (get_constraints (a
), get_constraints (b
));
600 // -------------------------------------------------------------------------- //
601 // Constraint Diagnostics
605 void diagnose_node (location_t
, tree
, tree
);
607 // Diagnose a constraint failure for type trait expressions.
609 diagnose_trait (location_t loc
, tree t
, tree args
)
611 if (check_requirements (t
, args
))
614 ++processing_template_decl
;
615 tree subst
= instantiate_requirements (t
, args
);
616 --processing_template_decl
;
618 if (subst
== error_mark_node
)
620 inform (input_location
, " substitution failure in %qE", t
);
624 tree t1
= TRAIT_EXPR_TYPE1 (subst
);
625 tree t2
= TRAIT_EXPR_TYPE2 (subst
);
626 switch (TRAIT_EXPR_KIND (t
))
628 case CPTK_HAS_NOTHROW_ASSIGN
:
629 inform (loc
, " %qT is not nothrow assignable", t1
);
631 case CPTK_HAS_NOTHROW_CONSTRUCTOR
:
632 inform (loc
, " %qT is not nothrow constructible", t1
);
634 case CPTK_HAS_NOTHROW_COPY
:
635 inform (loc
, " %qT is not nothrow copyable", t1
);
637 case CPTK_HAS_TRIVIAL_ASSIGN
:
638 inform (loc
, " %qT is not trivially assignable", t1
);
640 case CPTK_HAS_TRIVIAL_CONSTRUCTOR
:
641 inform (loc
, " %qT is not trivially constructible", t1
);
643 case CPTK_HAS_TRIVIAL_COPY
:
644 inform (loc
, " %qT is not trivially copyable", t1
);
646 case CPTK_HAS_TRIVIAL_DESTRUCTOR
:
647 inform (loc
, " %qT is not trivially destructible", t1
);
649 case CPTK_HAS_VIRTUAL_DESTRUCTOR
:
650 inform (loc
, " %qT does not have a virtual destructor", t1
);
652 case CPTK_IS_ABSTRACT
:
653 inform (loc
, " %qT is not an abstract class", t1
);
655 case CPTK_IS_BASE_OF
:
656 inform (loc
, " %qT is not a base of %qT", t1
, t2
);
659 inform (loc
, " %qT is not a class", t1
);
662 inform (loc
, " %qT is not an empty class", t1
);
665 inform (loc
, " %qT is not an enum", t1
);
668 inform (loc
, " %qT is not a final class", t1
);
670 case CPTK_IS_LITERAL_TYPE
:
671 inform (loc
, " %qT is not a literal type", t1
);
674 inform (loc
, " %qT is not a POD type", t1
);
676 case CPTK_IS_POLYMORPHIC
:
677 inform (loc
, " %qT is not a polymorphic type", t1
);
679 case CPTK_IS_STD_LAYOUT
:
680 inform (loc
, " %qT is not an standard layout type", t1
);
682 case CPTK_IS_TRIVIAL
:
683 inform (loc
, " %qT is not a trivial type", t1
);
686 inform (loc
, " %qT is not a union", t1
);
693 // Diagnose a failed concept check in concept indicated by T, where
694 // T is the result of resolve_constraint_check. Recursively analyze
695 // the nested requiremets for details.
697 diagnose_check (location_t loc
, tree t
, tree args
)
699 tree fn
= TREE_VALUE (t
);
700 tree targs
= TREE_PURPOSE (t
);
701 tree body
= DECL_SAVED_TREE (fn
);
705 inform (loc
, " failure in constraint %q#D", DECL_TI_TEMPLATE (fn
));
707 // Perform a mini-reduction on the constraint.
708 if (TREE_CODE (body
) == BIND_EXPR
)
709 body
= BIND_EXPR_BODY (body
);
710 if (TREE_CODE (body
) == RETURN_EXPR
)
711 body
= TREE_OPERAND (body
, 0);
713 // Locally instantiate the body with the call's template args,
714 // and recursively diagnose.
715 ++processing_template_decl
;
716 body
= instantiate_requirements (body
, targs
);
717 --processing_template_decl
;
719 diagnose_node (loc
, body
, args
);
722 // Diagnose constraint failures from the call expression T.
724 diagnose_call (location_t loc
, tree t
, tree args
)
726 if (check_requirements (t
, args
))
729 // If this is a concept, we're going to recurse.
730 // If it's just a call, then we can emit a simple message.
731 if (tree check
= resolve_constraint_check (t
))
732 diagnose_check (loc
, check
, args
);
734 inform (loc
, " %qE evaluated to false", t
);
737 // Diagnose a constraint failure in the expression T.
739 diagnose_other (location_t loc
, tree t
, tree args
)
741 if (check_requirements (t
, args
))
743 inform (loc
, " %qE evaluated to false", t
);
746 // Diagnose a constraint failure in the subtree T.
748 diagnose_node (location_t loc
, tree t
, tree args
)
750 switch (TREE_CODE (t
))
752 case TRUTH_ANDIF_EXPR
:
753 diagnose_node (loc
, TREE_OPERAND (t
, 0), args
);
754 diagnose_node (loc
, TREE_OPERAND (t
, 1), args
);
757 case TRUTH_ORIF_EXPR
:
758 sorry ("cannot diagnose disjunctions just yet");
762 diagnose_trait (loc
, t
, args
);
765 diagnose_call (loc
, t
, args
);
768 diagnose_other (loc
, t
, args
);
773 // Diagnose a constraint failure in the requirements expression REQS.
775 diagnose_requirements (location_t loc
, tree reqs
, tree args
)
777 diagnose_node (loc
, reqs
, args
);
780 // Create a tree node representing the substitution of ARGS into
781 // the parameters of TMPL. The resulting structure is passed as an
782 // for diagnosing substitutions.
784 make_subst (tree tmpl
, tree args
)
786 tree subst
= tree_cons (NULL_TREE
, args
, NULL_TREE
);
787 TREE_TYPE (subst
) = DECL_TEMPLATE_PARMS (tmpl
);
793 // Emit diagnostics detailing the failure ARGS to satisfy the constraints
794 // of the template declaration, TMPL.
796 diagnose_constraint_failure (location_t loc
, tree tmpl
, tree args
)
798 inform (loc
, " constraints not satisfied %S", make_subst (tmpl
, args
));
800 // Diagnose the constraints by recursively decomposing and
801 // evaluating the template requirements.
802 tree reqs
= CI_SPELLING (get_constraints (tmpl
));
803 diagnose_requirements (loc
, reqs
, args
);