2013-06-20 Andrew Sutton <andrew.n.sutton@gmail.com>
[official-gcc.git] / gcc / cp / constraint.cc
blob2f6af2621e96821cd640d9b4e771cedeb65bd555
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)
10 any later version.
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.
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "c-family/c-common.h"
30 #include "c-family/c-objc.h"
31 #include "tree-inline.h"
32 #include "intl.h"
33 #include "toplev.h"
34 #include "flags.h"
35 #include "timevar.h"
36 #include "diagnostic.h"
37 #include "cgraph.h"
38 #include "tree-iterator.h"
39 #include "vec.h"
40 #include "target.h"
41 #include "gimple.h"
42 #include "bitmap.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.
55 static inline tree
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
66 // non-null A,
68 // conjoin_requirements(a, NULL_TREE) == a
70 // If both A and B are NULL_TREE, the result is also NULL_TREE.
71 tree
72 conjoin_requirements (tree a, tree b)
74 if (a)
75 return b ? join_requirements (TRUTH_ANDIF_EXPR, a, b) : a;
76 else if (b)
77 return b;
78 else
79 return NULL_TREE;
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
88 // concept.
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.
94 namespace {
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:
103 // f<T, U>()
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.
110 static tree
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))
128 return NULL_TREE;
130 // Constraints must be declared concepts.
131 tree decl = TREE_VALUE (cands);
132 if (!DECL_DECLARED_CONCEPT_P (decl))
133 return NULL_TREE;
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);
143 return NULL;
146 return cands;
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.
152 tree
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)
160 return NULL_TREE;
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);
169 } // end namespace
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.
180 namespace {
182 // Helper functions
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.
196 tree
197 reduce_node (tree t)
199 switch (TREE_CODE_CLASS (TREE_CODE (t)))
201 case tcc_unary:
202 case tcc_binary:
203 case tcc_expression:
204 case tcc_vl_exp:
205 return reduce_expr (t);
207 case tcc_statement:
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.
217 case tcc_constant:
218 case tcc_reference:
219 case tcc_comparison:
220 return t;
222 default:
223 gcc_unreachable ();
225 return NULL_TREE;
228 // Reduction rules for the expression node T.
229 tree
230 reduce_expr (tree t)
232 switch (TREE_CODE (t))
234 case TRUTH_ANDIF_EXPR:
235 case TRUTH_ORIF_EXPR:
236 return reduce_logical (t);
238 case CALL_EXPR:
239 return reduce_call (t);
241 case TEMPLATE_ID_EXPR:
242 return reduce_template_id (t);
244 case CAST_EXPR:
245 return reduce_node (TREE_VALUE (TREE_OPERAND (t, 0)));
247 case BIND_EXPR:
248 return reduce_node (BIND_EXPR_BODY (t));
250 // Do not recurse.
251 case TAG_DEFN:
252 return NULL_TREE;
254 // Everything else is atomic.
255 default:
256 return t;
261 // Reduction rules for the statement T.
262 tree
263 reduce_stmt (tree t)
265 switch (TREE_CODE (t))
267 // Reduce the returned expression.
268 case RETURN_EXPR:
269 return reduce_node (TREE_OPERAND (t, 0));
271 // These statements do not introduce propositions
272 // in the constraints language. Do not recurse.
273 case DECL_EXPR:
274 case USING_STMT:
275 return NULL_TREE;
277 default:
278 gcc_unreachable ();
280 return NULL_TREE;
283 // Reduction rules for the declaration T.
284 tree
285 reduce_decl (tree t)
287 switch (TREE_CODE (t))
289 // References to var decls are atomic.
290 case VAR_DECL:
291 return t;
293 default:
294 gcc_unreachable ();
296 return NULL_TREE;
299 // Reduction rules for the node T.
300 tree
301 reduce_misc (tree t)
303 switch (TREE_CODE (t))
305 // Errors and traits are atomic.
306 case ERROR_MARK:
307 case TRAIT_EXPR:
308 return t;
310 case STATEMENT_LIST:
311 return reduce_stmt_list (t);
313 default:
314 gcc_unreachable ();
316 return NULL_TREE;
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.
323 tree
324 reduce_logical (tree t)
326 tree l = reduce_expr (TREE_OPERAND (t, 0));
327 tree r = reduce_expr (TREE_OPERAND (t, 1));
328 if (l && r)
330 t = copy_node (t);
331 TREE_OPERAND (t, 0) = l;
332 TREE_OPERAND (t, 1) = r;
333 return t;
335 else
336 return NULL_TREE;
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.
343 tree
344 reduce_call (tree t)
346 // Is the function call actually a constraint check?
347 tree check = resolve_constraint_check (t);
348 if (!check)
349 return 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));
356 if (!body)
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;
369 return result;
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>
377 // requires Foo<T>
378 // void f()
380 // Where Foo<T> should actually be written as Foo<T>(). Generate an
381 // error and suggest the improved writing.
382 tree
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);
389 return NULL_TREE;
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.
400 tree
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)))
409 if (!lhs)
410 lhs = rhs;
411 else
412 lhs = conjoin_requirements (lhs, rhs);
414 tsi_next (&i);
416 return lhs;
419 } // end namespace
422 // Reduce the requirement REQS into a logical formula written in terms of
423 // atomic propositions.
424 tree
425 reduce_requirements (tree reqs)
427 return reduce_node (reqs);
430 // Create a constraint-info node from the specified requirements.
431 tree
432 make_constraints (tree reqs)
434 // No requirements == no constraints
435 if (!reqs)
436 return NULL_TREE;
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;
451 return (tree)cinfo;
454 namespace {
456 inline tree
457 get_type_constraints (tree t)
459 // Template template arguments may not have template info.
460 if (!TYPE_TEMPLATE_INFO (t))
461 return NULL_TREE;
462 return TYPE_TEMPLATE_CONSTRAINT (t);
465 inline tree
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));
473 else
474 return get_decl_constraints (d);
476 return DECL_TEMPLATE_CONSTRAINT (t);
479 } // end namespace
481 // Return constraint info for the node T, regardless of the
482 // kind of node.
483 tree
484 get_constraints (tree t)
486 if (TYPE_P (t))
487 return get_type_constraints (t);
488 else if (DECL_P (t))
489 return get_decl_constraints (t);
490 else
491 gcc_unreachable ();
492 return false;
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.
498 static inline bool
499 check_requirements (tree reqs)
501 // Simplify the expression before evaluating it. This will
502 // cause TRAIT_EXPR nodes to be reduced before constexpr
503 // evaluation.
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.
513 static inline bool
514 check_requirements (tree reqs, tree args)
516 reqs = instantiate_requirements (reqs, args);
517 if (reqs == error_mark_node)
518 return false;
519 return check_requirements (reqs);
522 // Check the instantiated declaration constraints.
523 bool
524 check_constraints (tree cinfo)
526 if (!cinfo)
527 return true;
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.
533 bool
534 check_constraints (tree cinfo, tree args)
536 // No constraints? Satisfied.
537 if (!cinfo)
538 return true;
539 return check_requirements (CI_REQUIREMENTS (cinfo), args);
542 static inline bool
543 check_type_constraints (tree t, tree args)
545 return check_constraints (CLASSTYPE_TEMPLATE_CONSTRAINT (t), args);
548 static inline bool
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);
553 else
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.
560 bool
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.
567 bool
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.
576 bool
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.
584 bool
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.
592 bool
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
603 namespace {
605 void diagnose_node (location_t, tree, tree);
607 // Diagnose a constraint failure for type trait expressions.
608 void
609 diagnose_trait (location_t loc, tree t, tree args)
611 if (check_requirements (t, args))
612 return;
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);
621 return;
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);
630 break;
631 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
632 inform (loc, " %qT is not nothrow constructible", t1);
633 break;
634 case CPTK_HAS_NOTHROW_COPY:
635 inform (loc, " %qT is not nothrow copyable", t1);
636 break;
637 case CPTK_HAS_TRIVIAL_ASSIGN:
638 inform (loc, " %qT is not trivially assignable", t1);
639 break;
640 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
641 inform (loc, " %qT is not trivially constructible", t1);
642 break;
643 case CPTK_HAS_TRIVIAL_COPY:
644 inform (loc, " %qT is not trivially copyable", t1);
645 break;
646 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
647 inform (loc, " %qT is not trivially destructible", t1);
648 break;
649 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
650 inform (loc, " %qT does not have a virtual destructor", t1);
651 break;
652 case CPTK_IS_ABSTRACT:
653 inform (loc, " %qT is not an abstract class", t1);
654 break;
655 case CPTK_IS_BASE_OF:
656 inform (loc, " %qT is not a base of %qT", t1, t2);
657 break;
658 case CPTK_IS_CLASS:
659 inform (loc, " %qT is not a class", t1);
660 break;
661 case CPTK_IS_EMPTY:
662 inform (loc, " %qT is not an empty class", t1);
663 break;
664 case CPTK_IS_ENUM:
665 inform (loc, " %qT is not an enum", t1);
666 break;
667 case CPTK_IS_FINAL:
668 inform (loc, " %qT is not a final class", t1);
669 break;
670 case CPTK_IS_LITERAL_TYPE:
671 inform (loc, " %qT is not a literal type", t1);
672 break;
673 case CPTK_IS_POD:
674 inform (loc, " %qT is not a POD type", t1);
675 break;
676 case CPTK_IS_POLYMORPHIC:
677 inform (loc, " %qT is not a polymorphic type", t1);
678 break;
679 case CPTK_IS_STD_LAYOUT:
680 inform (loc, " %qT is not an standard layout type", t1);
681 break;
682 case CPTK_IS_TRIVIAL:
683 inform (loc, " %qT is not a trivial type", t1);
684 break;
685 case CPTK_IS_UNION:
686 inform (loc, " %qT is not a union", t1);
687 break;
688 default:
689 gcc_unreachable ();
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.
696 void
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);
702 if (!body)
703 return;
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.
723 void
724 diagnose_call (location_t loc, tree t, tree args)
726 if (check_requirements (t, args))
727 return;
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);
733 else
734 inform (loc, " %qE evaluated to false", t);
737 // Diagnose a constraint failure in the expression T.
738 void
739 diagnose_other (location_t loc, tree t, tree args)
741 if (check_requirements (t, args))
742 return;
743 inform (loc, " %qE evaluated to false", t);
746 // Diagnose a constraint failure in the subtree T.
747 void
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);
755 break;
757 case TRUTH_ORIF_EXPR:
758 sorry ("cannot diagnose disjunctions just yet");
759 break;
761 case TRAIT_EXPR:
762 diagnose_trait (loc, t, args);
763 break;
764 case CALL_EXPR:
765 diagnose_call (loc, t, args);
766 break;
767 default:
768 diagnose_other (loc, t, args);
769 break;
773 // Diagnose a constraint failure in the requirements expression REQS.
774 inline void
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.
783 inline tree
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);
788 return subst;
791 } // namesapce
793 // Emit diagnostics detailing the failure ARGS to satisfy the constraints
794 // of the template declaration, TMPL.
795 void
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);