2014-06-12 Andrew Sutton <andrew.n.sutton@gmail.com>
[official-gcc.git] / gcc / cp / constraint.cc
blobb20f508629d564197177beda12b979cfe4354260
1 /* Processing rules for constraints.
2 Copyright (C) 2013 Free Software Foundation, Inc.
3 Contributed by Andrew Sutton (andrew.n.sutton@gmail.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
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 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "print-tree.h"
27 #include "cp-tree.h"
28 #include "c-family/c-common.h"
29 #include "c-family/c-objc.h"
30 #include "tree-inline.h"
31 #include "intl.h"
32 #include "toplev.h"
33 #include "flags.h"
34 #include "timevar.h"
35 #include "diagnostic.h"
36 #include "cgraph.h"
37 #include "tree-iterator.h"
38 #include "vec.h"
39 #include "target.h"
40 #include "bitmap.h"
43 // -------------------------------------------------------------------------- //
44 // Requirement Construction
46 // Facilities for building and manipulating template requirements.
48 // TODO: Simply assigning boolean_type_node to the result type of the
49 // expression seems right for constraints, but in the long-term we might want
50 // to be more flexible (i.e., allow some form of overload resolution?).
52 // Create a new logical node joining the subexpressions a and b.
53 static inline tree
54 join_requirements (tree_code c, tree a, tree b)
56 gcc_assert (a != NULL_TREE && b != NULL_TREE);
57 gcc_assert (c == TRUTH_ANDIF_EXPR || c == TRUTH_ORIF_EXPR);
58 return build_min (c, boolean_type_node, a, b);
61 // Returns the conjunction of two requirements A and B, where A and B are
62 // reduced terms in the constraints language. Note that conjoining a non-null
63 // expression with NULL_TREE is an identity operation. That is, for some
64 // non-null A,
66 // conjoin_requirements(a, NULL_TREE) == a
68 // If both A and B are NULL_TREE, the result is also NULL_TREE.
69 tree
70 conjoin_requirements (tree a, tree b)
72 if (a)
73 return b ? join_requirements (TRUTH_ANDIF_EXPR, a, b) : a;
74 else if (b)
75 return b;
76 else
77 return NULL_TREE;
80 // Transform the list of expressions in the T into a conjunction
81 // of requirements. T must be a TREE_VEC.
82 tree
83 conjoin_requirements (tree t)
85 gcc_assert (TREE_CODE (t) == TREE_VEC);
86 tree r = NULL_TREE;
87 for (int i = 0; i < TREE_VEC_LENGTH (t); ++i)
88 r = conjoin_requirements (r, TREE_VEC_ELT (t, i));
89 return r;
93 // -------------------------------------------------------------------------- //
94 // Constraint Resolution
96 // This facility is used to resolve constraint checks from requirement
97 // expressions. A constraint check is a call to a function template, declared
98 // concept.
100 // The result of resolution is a pair (a list node) whose value is the
101 // matched declaration, and whose purpose contains the coerced template
102 // arguments that can be substituted into the call.
105 // Given an overload set, try to find a unique definition that can be
106 // instantiated by the template arguments.
108 // This function is not called for arbitrary call expressions. In particular,
109 // the call expression must be written with explicit template arguments
110 // and no function arguments. For example:
112 // f<T, U>()
114 // The overload set will contain only template declarations.
116 // If a single definition is found, this returns a list node whose VALUE
117 // is the constraint function (not the template), and its PURPOSE is
118 // the complete set of arguments substituted into the parameter list.
119 static tree
120 resolve_constraint_check (tree ovl, tree args)
122 tree cands = NULL_TREE;
123 for (tree p = ovl; p != NULL_TREE; p = OVL_NEXT (p))
125 // Get the next template overload.
126 tree tmpl = OVL_CURRENT (p);
127 if (TREE_CODE (tmpl) != TEMPLATE_DECL)
128 continue;
130 // Don't try to deduce checks for non-concept-like. We often
131 // end up trying to resolve constraints in functional casts
132 // as part of a post-fix expression. We can save time and
133 // headaches by not instantiating those declarations.
135 // NOTE: This masks a potential error, caused by instantiating
136 // non-deduced contexts using placeholder arguments.
137 tree fn = DECL_TEMPLATE_RESULT (tmpl);
138 if (DECL_ARGUMENTS (fn))
139 continue;
140 if (!DECL_DECLARED_CONCEPT_P (fn))
141 continue;
143 // Remember the candidate if we can deduce a substitution.
144 ++processing_template_decl;
145 tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl));
146 if (tree subst = coerce_template_parms (parms, args, tmpl))
147 if (subst != error_mark_node)
148 cands = tree_cons (subst, fn, cands);
149 --processing_template_decl;
152 // If we didn't find a unique candidate, then this is
153 // not a constraint check.
154 if (!cands || TREE_CHAIN (cands))
155 return NULL_TREE;
157 // Constraints must be declared concepts.
158 tree decl = TREE_VALUE (cands);
159 if (!DECL_DECLARED_CONCEPT_P (decl))
160 return NULL_TREE;
162 // Concept declarations must have a corresponding definition.
164 // TODO: This should be part of the up-front checking for
165 // a concept declaration.
166 if (!DECL_SAVED_TREE (decl))
168 error_at (DECL_SOURCE_LOCATION (decl),
169 "concept %q#D has no definition", decl);
170 return NULL;
173 return cands;
176 // Determine if the the call expression CALL is a constraint check, and
177 // return the concept declaration and arguments being checked. If CALL
178 // does not denote a constraint check, return NULL.
179 tree
180 resolve_constraint_check (tree call)
182 gcc_assert (TREE_CODE (call) == CALL_EXPR);
184 // A constraint check must be only be a template-id expression.
185 tree target = CALL_EXPR_FN (call);
186 if (TREE_CODE (target) != TEMPLATE_ID_EXPR)
187 return NULL_TREE;
189 // Get the overload set and template arguments and try to
190 // resolve the target.
191 tree ovl = TREE_OPERAND (target, 0);
192 tree args = TREE_OPERAND (target, 1);
193 return resolve_constraint_check (ovl, args);
196 // Given a call expression to a concept, possibly including a placeholder
197 // argument, deduce the concept being checked and the prototype paraemter.
198 // Returns true if the constraint and prototype can be deduced and false
199 // otherwise. Note that the CHECK and PROTO arguments are set to NULL_TREE
200 // if this returns false.
201 bool
202 deduce_constrained_parameter (tree call, tree& check, tree& proto)
204 // Resolve the constraint check to deduce the declared parameter.
205 if (tree info = resolve_constraint_check (call))
207 // Get function and argument from the resolved check expression and
208 // the prototype parameter. Note that if the first argument was a
209 // pack, we need to extract the first element ot get the prototype.
210 check = TREE_VALUE (info);
211 tree arg = TREE_VEC_ELT (TREE_PURPOSE (info), 0);
212 if (ARGUMENT_PACK_P (arg))
213 arg = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0);
214 proto = TREE_TYPE (arg);
215 return true;
217 check = proto = NULL_TREE;
218 return false;
221 // -------------------------------------------------------------------------- //
222 // Requirement Reduction
224 // Reduces a template requirement to a logical formula written in terms of
225 // atomic propositions, returing the new expression. If the expression cannot
226 // be reduced, a NULL_TREE is returned, indicating failure to reduce the
227 // original requirment.
229 namespace {
231 // Helper functions
232 static tree reduce_node (tree);
233 static tree reduce_expr (tree);
234 static tree reduce_stmt (tree);
235 static tree reduce_decl (tree);
236 static tree reduce_misc (tree);
238 static tree reduce_logical (tree);
239 static tree reduce_call (tree);
240 static tree reduce_requires (tree);
241 static tree reduce_expr_req (tree);
242 static tree reduce_type_req (tree);
243 static tree reduce_nested_req (tree);
244 static tree reduce_template_id (tree);
245 static tree reduce_stmt_list (tree);
247 // Reduce the requirement T into a logical formula written in terms of
248 // atomic propositions.
249 tree
250 reduce_node (tree t)
252 switch (TREE_CODE_CLASS (TREE_CODE (t)))
254 case tcc_unary:
255 case tcc_binary:
256 case tcc_expression:
257 case tcc_vl_exp:
258 return reduce_expr (t);
260 case tcc_statement:
261 return reduce_stmt (t);
263 case tcc_declaration:
264 return reduce_decl (t);
266 case tcc_exceptional:
267 return reduce_misc (t);
269 // These kinds of expressions are atomic.
270 case tcc_constant:
271 case tcc_reference:
272 case tcc_comparison:
273 return t;
275 default:
276 gcc_unreachable ();
278 return NULL_TREE;
281 // Reduction rules for the expression node T.
282 tree
283 reduce_expr (tree t)
285 switch (TREE_CODE (t))
287 case TRUTH_ANDIF_EXPR:
288 case TRUTH_ORIF_EXPR:
289 return reduce_logical (t);
291 case CALL_EXPR:
292 return reduce_call (t);
294 case REQUIRES_EXPR:
295 return reduce_requires (t);
297 case EXPR_REQ:
298 return reduce_expr_req (t);
300 case TYPE_REQ:
301 return reduce_type_req (t);
303 case NESTED_REQ:
304 return reduce_nested_req (t);
306 case TEMPLATE_ID_EXPR:
307 return reduce_template_id (t);
309 case CAST_EXPR:
310 return reduce_node (TREE_VALUE (TREE_OPERAND (t, 0)));
312 case BIND_EXPR:
313 return reduce_node (BIND_EXPR_BODY (t));
315 // Do not recurse.
316 case TAG_DEFN:
317 return NULL_TREE;
319 // Everything else is atomic.
320 default:
321 return t;
326 // Reduction rules for the statement T.
327 tree
328 reduce_stmt (tree t)
330 switch (TREE_CODE (t))
332 // Reduce the returned expression.
333 case RETURN_EXPR:
334 return reduce_node (TREE_OPERAND (t, 0));
336 // These statements do not introduce propositions
337 // in the constraints language. Do not recurse.
338 case DECL_EXPR:
339 case USING_STMT:
340 return NULL_TREE;
342 default:
343 gcc_unreachable ();
345 return NULL_TREE;
348 // Reduction rules for the declaration T.
349 tree
350 reduce_decl (tree t)
352 switch (TREE_CODE (t))
354 // References to var decls are atomic.
355 case VAR_DECL:
356 return t;
358 default:
359 gcc_unreachable ();
361 return NULL_TREE;
364 // Reduction rules for the node T.
365 tree
366 reduce_misc (tree t)
368 switch (TREE_CODE (t))
370 // Errors and traits are atomic.
371 case ERROR_MARK:
372 case TRAIT_EXPR:
373 return t;
375 case STATEMENT_LIST:
376 return reduce_stmt_list (t);
378 default:
379 gcc_unreachable ();
381 return NULL_TREE;
384 // Reduction rules for the binary logical expression T (&& and ||).
386 // Generate a new expression from the reduced operands. If either operand
387 // cannot be reduced, then the resulting expression is null.
388 tree
389 reduce_logical (tree t)
391 tree l = reduce_expr (TREE_OPERAND (t, 0));
392 tree r = reduce_expr (TREE_OPERAND (t, 1));
393 if (l && r)
395 t = copy_node (t);
396 TREE_OPERAND (t, 0) = l;
397 TREE_OPERAND (t, 1) = r;
398 return t;
400 else
401 return NULL_TREE;
404 // Reduction rules for the call expression T.
406 // If T is a call to a constraint instantiate its definition and
407 // recursively reduce its returned expression.
408 tree
409 reduce_call (tree t)
411 // Is the function call actually a constraint check?
412 tree check = resolve_constraint_check (t);
413 if (!check)
414 return t;
416 tree fn = TREE_VALUE (check);
417 tree args = TREE_PURPOSE (check);
419 // Reduce the body of the function into the constriants language.
420 tree body = reduce_requirements (DECL_SAVED_TREE (fn));
421 if (!body)
423 error ("could not inline requirements from %qD", fn);
424 return error_mark_node;
427 // Instantiate the reduced results using the deduced args.
428 tree result = instantiate_requirements (body, args);
429 if (result == error_mark_node)
431 error ("could not instantiate requirements from %qD", fn);
432 return error_mark_node;
434 return result;
437 // Reduction rules for the template-id T.
439 // It turns out that we often get requirements being written like this:
441 // template<typename T>
442 // requires Foo<T>
443 // void f()
445 // Where Foo<T> should actually be written as Foo<T>(). Generate an
446 // error and suggest the improved writing.
447 tree
448 reduce_template_id (tree t)
450 vec<tree, va_gc>* args = NULL;
451 tree c = finish_call_expr (t, &args, true, false, 0);
452 error_at (EXPR_LOCATION (t), "invalid requirement");
453 inform (EXPR_LOCATION (t), "did you mean %qE", c);
454 return c;
458 // Reduce an expression requirement as a conjunction of its
459 // individual constraints.
460 tree
461 reduce_expr_req (tree t)
463 tree r = NULL_TREE;
464 for (tree l = TREE_OPERAND (t, 0); l; l = TREE_CHAIN (l))
465 r = conjoin_requirements (r, reduce_expr (TREE_VALUE (l)));
466 return r;
469 // Reduce a type requirement by returing its underlying
470 // constraint.
471 tree
472 reduce_type_req (tree t)
474 return TREE_OPERAND (t, 0);
477 // Reduce a nested requireemnt by returing its only operand.
478 tree
479 reduce_nested_req (tree t)
481 return TREE_OPERAND (t, 0);
484 // Reduce a requires expr by reducing each requirement in turn,
485 // rewriting the list of requirements so that we end up with a
486 // list of expressions, some of which may be conjunctions.
487 tree
488 reduce_requires (tree t)
490 for (tree l = TREE_OPERAND (t, 1); l; l = TREE_CHAIN (l))
491 TREE_VALUE (l) = reduce_expr (TREE_VALUE (l));
492 return t;
495 // Reduction rules for the statement list STMTS.
497 // Recursively reduce each statement in the list, concatenating each
498 // reduced result into a conjunction of requirements.
500 // A constexpr function may include statements other than a return
501 // statement. The primary purpose of these rules is to filter those
502 // non-return statements from the constraints language.
503 tree
504 reduce_stmt_list (tree stmts)
506 tree lhs = NULL_TREE;
507 tree_stmt_iterator i = tsi_start (stmts);
508 while (!tsi_end_p (i))
510 if (tree rhs = reduce_node (tsi_stmt (i)))
511 lhs = conjoin_requirements (lhs, rhs);
512 tsi_next (&i);
514 return lhs;
517 } // end namespace
519 // Reduce the requirement REQS into a logical formula written in terms of
520 // atomic propositions.
521 tree
522 reduce_requirements (tree reqs)
524 return reduce_node (reqs);
527 // -------------------------------------------------------------------------- //
528 // Constraint Semantic Processing
530 // The following functions are called by the parser and substitution rules
531 // to create and evaluate constraint-related nodes.
533 // Create a constraint-info node from the specified requirements.
534 tree
535 make_constraints (tree reqs)
537 // No requirements == no constraints
538 if (!reqs)
539 return NULL_TREE;
541 // Reduce the requirements into a single expression of constraints.
542 tree expr = reduce_requirements (reqs);
543 if (expr == error_mark_node)
544 return error_mark_node;
546 // Decompose those expressions into lists of lists of atomic
547 // propositions.
548 tree assume = decompose_assumptions (expr);
550 // Build the constraint info.
551 tree_constraint_info *cinfo =
552 (tree_constraint_info *)make_node (CONSTRAINT_INFO);
553 cinfo->spelling = reqs;
554 cinfo->requirements = expr;
555 cinfo->assumptions = assume;
556 return (tree)cinfo;
559 // Returns the template constraints of declaration T. If T is not a
560 // template, this return NULL_TREE. Note that T must be non-null.
561 tree
562 get_constraints (tree t)
564 gcc_assert (DECL_P (t));
565 if (TREE_CODE (t) != TEMPLATE_DECL)
567 if (!DECL_TEMPLATE_INFO (t))
568 return NULL_TREE;
569 else
570 return DECL_CONSTRAINTS (DECL_TI_TEMPLATE (t));
572 return DECL_CONSTRAINTS (t);
575 // Returns a conjunction of shorthand requirements for the template
576 // parameter list PARMS. Note that the requirements are stored in
577 // the TYPE of each tree node.
578 tree
579 get_shorthand_requirements (tree parms)
581 tree reqs = NULL_TREE;
582 parms = INNERMOST_TEMPLATE_PARMS (parms);
583 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
585 tree parm = TREE_VEC_ELT (parms, i);
586 reqs = conjoin_requirements(reqs, TREE_TYPE (parm));
588 return reqs;
591 // Finish the template requirement, EXPR, by translating it into
592 // a constraint information record.
593 tree
594 finish_template_requirements (tree expr)
596 if (expr == error_mark_node)
597 return NULL_TREE;
598 else
599 return make_constraints (expr);
602 tree
603 build_requires_expr (tree parms, tree reqs)
605 // Modify the declared parameters by removing their context (so they
606 // don't refer to the enclosing scope), and marking them constant (so
607 // we can actually check constexpr properties).
608 for (tree p = parms; p && !VOID_TYPE_P (TREE_VALUE (p)); p = TREE_CHAIN (p))
610 tree parm = TREE_VALUE (p);
611 DECL_CONTEXT (parm) = NULL_TREE;
612 TREE_CONSTANT (parm) = true;
615 // Build the node.
616 tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs);
617 TREE_SIDE_EFFECTS (r) = false;
618 TREE_CONSTANT (r) = true;
619 return r;
622 // Evaluate an instantiatd requires expr, returning the truth node
623 // only when all sub-requirements have evaluated to true.
624 tree
625 eval_requires_expr (tree reqs)
627 for (tree t = reqs ; t; t = TREE_CHAIN (t)) {
628 tree r = TREE_VALUE (t);
629 r = fold_non_dependent_expr (r);
630 r = maybe_constant_value (r);
631 if (r != boolean_true_node)
632 return boolean_false_node;
634 return boolean_true_node;
637 // Finish a requires expression, returning a node wrapping the parameters,
638 // PARMS, and the list of requirements REQS.
639 tree
640 finish_requires_expr (tree parms, tree reqs)
642 if (processing_template_decl)
643 return build_requires_expr (parms, reqs);
644 else
645 return eval_requires_expr (reqs);
648 // Construct a unary expression that evaluates properties of the
649 // expression or type T, and has a boolean result type.
650 static inline tree
651 build_check_expr (tree_code c, tree t)
653 tree r = build_min (c, boolean_type_node, t);
654 TREE_SIDE_EFFECTS (r) = false;
655 TREE_READONLY (r) = true;
656 TREE_CONSTANT (r) = true;
657 return r;
660 // Finish a syntax requirement, constructing a list embodying a sequence
661 // of checks for the validity of EXPR and TYPE, the convertibility of
662 // EXPR to TYPE, and the expression properties specified in SPECS.
663 tree
664 finish_expr_requirement (tree expr, tree type, tree specs)
666 gcc_assert (processing_template_decl);
668 // Build a list of checks, starting with the valid expression.
669 tree result = tree_cons (NULL_TREE, finish_validexpr_expr (expr), NULL_TREE);
671 // If a type requirement was provided, build the result type checks.
672 if (type)
674 // If the type is dependent, ensure that it can be validly
675 // instantiated.
677 // NOTE: We can also disregard checks that result in the template
678 // parameter.
679 if (dependent_type_p (type))
681 tree treq = finish_type_requirement (type);
682 result = tree_cons (NULL_TREE, treq, result);
685 // Ensure that the result of the expression can be converted to
686 // the result type.
687 tree decl_type = finish_decltype_type (expr, false, tf_none);
688 tree creq = finish_trait_expr (CPTK_IS_CONVERTIBLE_TO, decl_type, type);
689 result = tree_cons (NULL_TREE, creq, result);
692 // If constraint specifiers are present, make them part of the
693 // list of constraints.
694 if (specs)
696 TREE_CHAIN (tree_last (specs)) = result;
697 result = specs;
700 // Finally, construct the syntactic requirement.
701 return build_check_expr (EXPR_REQ, nreverse (result));
704 // Finish a simple syntax requirement, returning a node representing
705 // a check that EXPR is a valid expression.
706 tree
707 finish_expr_requirement (tree expr)
709 gcc_assert (processing_template_decl);
710 tree req = finish_validexpr_expr (expr);
711 tree reqs = tree_cons (NULL_TREE, req, NULL_TREE);
712 return build_check_expr (EXPR_REQ, reqs);
715 // Finish a type requirement, returning a node representing a check
716 // that TYPE will result in a valid type when instantiated.
717 tree
718 finish_type_requirement (tree type)
720 gcc_assert (processing_template_decl);
721 tree req = finish_validtype_expr (type);
722 return build_check_expr (TYPE_REQ, req);
725 tree
726 finish_nested_requirement (tree expr)
728 gcc_assert (processing_template_decl);
729 return build_check_expr (NESTED_REQ, expr);
732 // Finish a constexpr requirement, returning a node representing a
733 // check that EXPR, when instantiated, may be evaluated at compile time.
734 tree
735 finish_constexpr_requirement (tree expr)
737 gcc_assert (processing_template_decl);
738 return finish_constexpr_expr (expr);
741 // Finish the noexcept requirement by constructing a noexcept
742 // expression evaluating EXPR.
743 tree
744 finish_noexcept_requirement (tree expr)
746 gcc_assert (processing_template_decl);
747 return finish_noexcept_expr (expr, tf_none);
750 // Returns the true or false node depending on the truth value of B.
751 static inline tree
752 truth_node (bool b)
754 return b ? boolean_true_node : boolean_false_node;
757 // Returns a finished validexpr-expr. Returns the true or false node
758 // depending on whether EXPR denotes a valid expression. This is the case
759 // when the expression has been successfully type checked.
761 // When processing a template declaration, the result is an expression
762 // representing the check.
763 tree
764 finish_validexpr_expr (tree expr)
766 if (processing_template_decl)
767 return build_check_expr (VALIDEXPR_EXPR, expr);
768 return truth_node (expr && expr != error_mark_node);
771 // Returns a finished validtype-expr. Returns the true or false node
772 // depending on whether T denotes a valid type name.
774 // When processing a template declaration, the result is an expression
775 // representing the check.
777 // FIXME: Semantics need to be aligned with the new version of the
778 // specificaiton (i.e., we must be able to invent a function and
779 // perform argument deduction against it).
780 tree
781 finish_validtype_expr (tree type)
783 if (is_auto (type))
785 sorry ("%<auto%< not supported in result type constraints\n");
786 return error_mark_node;
789 if (processing_template_decl)
790 return build_check_expr (VALIDTYPE_EXPR, type);
791 return truth_node (type && TYPE_P (type));
794 // Returns a finished constexpr-expr. Returns the true or false node
795 // depending on whether the expression T may be evaluated at compile
796 // time.
798 // When processing a template declaration, the result is an expression
799 // representing the check.
800 tree
801 finish_constexpr_expr (tree expr)
803 if (processing_template_decl)
804 return build_check_expr (CONSTEXPR_EXPR, expr);
806 // TODO: Actually check that the expression can be constexpr
807 // evaluatd.
809 // return truth_node (potential_constant_expression (expr));
810 sorry ("constexpr requirement");
811 return NULL_TREE;
814 // Check that a constrained friend declaration function declaration,
815 // FN, is admissable. This is the case only when the declaration depends
816 // on template parameters and does not declare a specialization.
817 void
818 check_constrained_friend (tree fn, tree reqs)
820 if (fn == error_mark_node)
821 return;
822 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
824 // If there are not constraints, this cannot be an error.
825 if (!reqs)
826 return;
828 // Constrained friend functions that don't depend on template
829 // arguments are effectively meaningless.
830 tree parms = DECL_ARGUMENTS (fn);
831 tree result = TREE_TYPE (TREE_TYPE (fn));
832 if (!(parms && uses_template_parms (parms)) && !uses_template_parms (result))
834 error ("constrained friend does not depend on template parameters");
835 return;
839 namespace {
840 // Build a new call expression, but don't actually generate a new
841 // function call. We just want the tree, not the semantics.
842 inline tree
843 build_call_check (tree id)
845 ++processing_template_decl;
846 vec<tree, va_gc> *fargs = make_tree_vector();
847 tree call = finish_call_expr (id, &fargs, false, false, tf_none);
848 --processing_template_decl;
849 return call;
851 } // namespace
853 // Construct a concept check for the overloaded function, where the
854 // template arguments are the list given by ARG and REST. That is, it
855 // build the call expression OVL<ARG, REST>(). If REST is null, then
856 // the resulting constraint is OVL<ARG>().
858 // TODO: Extend this to take a variable concept also.
859 tree
860 build_concept_check (tree ovl, tree arg, tree rest)
862 gcc_assert (TREE_CODE (ovl) == OVERLOAD);
863 gcc_assert (rest ? TREE_CODE (rest) == TREE_VEC : true);
865 // Build a template-id that acts as the call target using OVL as
866 // the template and ARG as the only explicit argument.
867 int n = rest ? TREE_VEC_LENGTH (rest) : 0;
868 tree targs = make_tree_vec (n + 1);
869 TREE_VEC_ELT (targs, 0) = arg;
870 if (rest)
871 for (int i = 0; i < n; ++i)
872 TREE_VEC_ELT (targs, i + 1) = TREE_VEC_ELT (rest, i);
873 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, n + 1);
874 tree id = lookup_template_function (ovl, targs);
875 return build_call_check (id);
878 // Returns a TYPE_DECL that contains sufficient information to build
879 // a template parameter of the same kind as PROTO and constrained
880 // by the concept declaration FN. PROTO is saved as the initializer of
881 // the new type decl, and the constraining function is saved in
882 // DECL_SIZE_UNIT.
884 // If specified ARGS provides additional arguments to the constraint
885 // check. These are stored in the DECL_SIZE field.
886 tree
887 build_constrained_parameter (tree fn, tree proto, tree args)
889 tree name = DECL_NAME (fn);
890 tree type = TREE_TYPE (proto);
891 tree decl = build_decl (input_location, TYPE_DECL, name, type);
892 DECL_INITIAL (decl) = proto; // Describing parameter
893 DECL_SIZE_UNIT (decl) = fn; // Constraining function declaration
894 DECL_SIZE (decl) = args; // Extra template arguments.
895 return decl;
898 // Create a requirement expression for the given DECL that evaluates the
899 // requirements specified by CONSTR, a TYPE_DECL that contains all the
900 // information necessary to build the requirements (see finish_concept_name
901 // for the layout of that TYPE_DECL).
903 // Note that the constraints are neither reduced nor decomposed. That is
904 // done only after the requires clause has been parsed (or not).
905 tree
906 finish_shorthand_requirement (tree decl, tree constr)
908 // No requirements means no constraints.
909 if (!constr)
910 return NULL_TREE;
912 tree proto = DECL_INITIAL (constr); // The prototype declaration
913 tree con = DECL_SIZE_UNIT (constr); // The concept declaration
914 tree args = DECL_SIZE (constr); // Extra template arguments
916 // If the parameter declaration is variadic, but the concept is not
917 // then we need to apply the concept to every element in the pack.
918 bool is_proto_pack = template_parameter_pack_p (proto);
919 bool is_decl_pack = template_parameter_pack_p (decl);
920 bool apply_to_all_p = is_decl_pack && !is_proto_pack;
922 // Get the argument and overload used for the requirement. Adjust
923 // if we're going to expand later.
924 tree arg = template_parm_to_arg (build_tree_list (NULL_TREE, decl));
925 if (apply_to_all_p)
926 arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
928 // Build the concept check. If it the constraint needs to be applied
929 // to all elements of the parameter pack, then expand make the constraint
930 // an expansion.
931 tree ovl = build_overload (DECL_TI_TEMPLATE (con), NULL_TREE);
932 tree check = build_concept_check (ovl, arg, args);
933 if (apply_to_all_p)
935 check = make_pack_expansion (check);
937 // Set the type to indicate that this expansion will get special
938 // treatment during instantiation.
940 // TODO: Maybe this should be a different kind of node... one that
941 // has all the same properties as a pack expansion, but has a definite
942 // expansion when instantiated as part of an expression.
944 // As of now, this is a hack.
945 TREE_TYPE (check) = boolean_type_node;
948 return check;
951 // -------------------------------------------------------------------------- //
952 // Substitution Rules
954 // The following functions implement substitution rules for constraints.
956 namespace {
957 // In an unevaluated context, the substitution of parm decls are not
958 // properly chained during substitution. Do that here.
959 tree
960 fix_local_parms (tree sparms)
962 if (!sparms)
963 return sparms;
965 tree p = TREE_CHAIN (sparms);
966 tree q = sparms;
967 while (p && TREE_VALUE (p) != void_type_node)
969 DECL_CHAIN (TREE_VALUE (q)) = TREE_VALUE (p);
970 q = p;
971 p = TREE_CHAIN (p);
973 return sparms;
976 // Register local specializations for each of tparm and the corresponding
977 // sparm. This is a helper function for tsubst_requires_expr.
978 void
979 declare_local_parms (tree tparms, tree sparms)
981 tree s = TREE_VALUE (sparms);
982 for (tree p = tparms; p && !VOID_TYPE_P (TREE_VALUE (p)); p = TREE_CHAIN (p))
984 tree t = TREE_VALUE (p);
985 if (DECL_PACK_P (t))
987 tree pack = extract_fnparm_pack (t, &s);
988 register_local_specialization (pack, t);
990 else
992 register_local_specialization (s, t);
993 s = TREE_CHAIN (s);
998 // Substitute ARGS into the parameter list T, producing a sequence of
999 // local parameters (variables) in the current scope.
1000 tree
1001 tsubst_local_parms (tree t,
1002 tree args,
1003 tsubst_flags_t complain,
1004 tree in_decl)
1006 tree r = fix_local_parms (tsubst (t, args, complain, in_decl));
1007 if (r == error_mark_node)
1008 return error_mark_node;
1010 // Register the instantiated args as local parameters.
1011 if (t)
1012 declare_local_parms (t, r);
1014 return r;
1017 // Substitute ARGS into the requirement body (list of requirements), T.
1018 tree
1019 tsubst_requirement_body (tree t, tree args, tree in_decl)
1021 cp_unevaluated guard;
1022 tree r = NULL_TREE;
1023 while (t)
1025 // If any substitutions fail, then this is equivalent to
1026 // returning false.
1027 tree e = tsubst_expr (TREE_VALUE (t), args, tf_none, in_decl, false);
1028 if (e == error_mark_node)
1029 e = boolean_false_node;
1030 r = tree_cons (NULL_TREE, e, r);
1031 t = TREE_CHAIN (t);
1033 return r;
1035 } // namespace
1037 // Substitute ARGS into the requires expression T.
1038 tree
1039 tsubst_requires_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1041 local_specialization_stack stack;
1042 tree p = tsubst_local_parms (TREE_OPERAND (t, 0), args, complain, in_decl);
1043 tree r = tsubst_requirement_body (TREE_OPERAND (t, 1), args, in_decl);
1044 return finish_requires_expr (p, r);
1047 // Substitute ARGS into the valid-expr expression T.
1048 tree
1049 tsubst_validexpr_expr (tree t, tree args, tree in_decl)
1051 tree r = tsubst_expr (TREE_OPERAND (t, 0), args, tf_none, in_decl, false);
1052 return finish_validexpr_expr (r);
1055 // Substitute ARGS into the valid-type expression T.
1056 tree
1057 tsubst_validtype_expr (tree t, tree args, tree in_decl)
1059 tree r = tsubst (TREE_OPERAND (t, 0), args, tf_none, in_decl);
1060 return finish_validtype_expr (r);
1063 // Substitute ARGS into the constexpr expression T.
1064 tree
1065 tsubst_constexpr_expr (tree t, tree args, tree in_decl)
1067 tree r = tsubst_expr (TREE_OPERAND (t, 0), args, tf_none, in_decl, false);
1068 return finish_constexpr_expr (r);
1071 // Substitute ARGS into the expr requirement T. Note that a requirement
1072 // node is instantiated from a non-reduced context (e.g., static_assert).
1073 tree
1074 tsubst_expr_req (tree t, tree args, tree in_decl)
1076 tree r = NULL_TREE;
1077 for (tree l = TREE_OPERAND (t, 0); l; l = TREE_CHAIN (l))
1079 tree e = tsubst_expr (TREE_VALUE (l), args, tf_none, in_decl, false);
1080 r = conjoin_requirements (r, e);
1082 return r;
1085 // Substitute ARGS into the type requirement T. Note that a requirement
1086 // node is instantiated from a non-reduced context (e.g., static_assert).
1087 tree
1088 tsubst_type_req (tree t, tree args, tree in_decl)
1090 return tsubst_expr (TREE_OPERAND (t, 0), args, tf_none, in_decl, false);
1093 // Substitute ARGS into the nested requirement T. Note that a requirement
1094 // node is instantiated from a non-reduced context (e.g., static_assert).
1095 tree
1096 tsubst_nested_req (tree t, tree args, tree in_decl)
1098 return tsubst_expr (TREE_OPERAND (t, 0), args, tf_none, in_decl, false);
1101 // Substitute the template arguments ARGS into the requirement
1102 // expression REQS. Errors resulting from substitution are not
1103 // diagnosed.
1104 tree
1105 instantiate_requirements (tree reqs, tree args)
1107 return tsubst_expr (reqs, args, tf_none, NULL_TREE, false);
1110 // -------------------------------------------------------------------------- //
1111 // Constraint Satisfaction
1113 // The following functions are responsible for the instantiation and
1114 // evaluation of constraints.
1116 namespace {
1117 // Returns true if the requirements expression REQS is satisfied
1118 // and false otherwise. The requirements are checked by simply
1119 // evaluating REQS as a constant expression.
1120 static inline bool
1121 check_requirements (tree reqs)
1123 // Reduce any remaining TRAIT_EXPR nodes before evaluating.
1124 reqs = fold_non_dependent_expr (reqs);
1126 // Requirements are satisfied when REQS evaluates to true.
1127 return cxx_constant_value (reqs) == boolean_true_node;
1130 // Returns true if the requirements expression REQS is satisfied
1131 // and false otherwise. The requirements are checked by first
1132 // instantiating REQS and then evaluating it as a constant expression.
1133 static inline bool
1134 check_requirements (tree reqs, tree args)
1136 // If any arguments are dependent, then we can't check the
1137 // requirements. Just return true.
1138 if (uses_template_parms (args))
1139 return true;
1141 // Instantiate and evaluate the requirements.
1142 reqs = instantiate_requirements (reqs, args);
1143 if (reqs == error_mark_node)
1144 return false;
1145 return check_requirements (reqs);
1147 } // namespace
1149 // Check the instantiated declaration constraints.
1150 bool
1151 check_constraints (tree cinfo)
1153 // No constraints? Satisfied.
1154 if (!cinfo)
1155 return true;
1156 return check_requirements (CI_REQUIREMENTS (cinfo));
1159 // Check the constraints in CINFO against the given ARGS, returning
1160 // true when the constraints are satisfied and false otherwise.
1161 bool
1162 check_constraints (tree cinfo, tree args)
1164 // No constraints? Satisfied.
1165 if (!cinfo)
1166 return true;
1168 // Dependent arguments? Satisfied. They won't reduce to true or false.
1169 if (uses_template_parms (args))
1170 return true;
1172 return check_requirements (CI_REQUIREMENTS (cinfo), args);
1175 // Check the constraints of the declaration or type T, against
1176 // the specified arguments. Returns true if the constraints are
1177 // satisfied and false otherwise.
1178 bool
1179 check_template_constraints (tree t, tree args)
1181 return check_constraints (DECL_CONSTRAINTS (t), args);
1184 // -------------------------------------------------------------------------- //
1185 // Constraint Relations
1187 // Interfaces for determining equivalency and ordering of constraints.
1189 // Returns true when A and B are equivalent constraints.
1190 bool
1191 equivalent_constraints (tree a, tree b)
1193 if (a == b)
1194 return true;
1195 else
1196 return subsumes (a, b) && subsumes (b, a);
1199 // Returns true if the template declarations A and B have equivalent
1200 // constraints. This is the case when A's constraints subsume B's and
1201 // when B's also constrain A's.
1202 bool
1203 equivalently_constrained (tree a, tree b)
1205 gcc_assert (TREE_CODE (a) == TREE_CODE (b));
1206 return equivalent_constraints (DECL_CONSTRAINTS (a), DECL_CONSTRAINTS (b));
1209 // Returns true when the A contains more atomic properties than B.
1210 bool
1211 more_constraints (tree a, tree b)
1213 return subsumes (a, b);
1216 // Returns true when the template declaration A's constraints subsume
1217 // those of the template declaration B.
1218 bool
1219 more_constrained (tree a, tree b)
1221 gcc_assert (TREE_CODE (a) == TREE_CODE (b));
1222 return more_constraints (DECL_CONSTRAINTS (a), DECL_CONSTRAINTS (b));
1226 // -------------------------------------------------------------------------- //
1227 // Constraint Diagnostics
1229 namespace {
1231 void diagnose_node (location_t, tree, tree);
1233 // Diagnose a constraint failure for type trait expressions.
1234 void
1235 diagnose_trait (location_t loc, tree t, tree args)
1237 if (check_requirements (t, args))
1238 return;
1240 ++processing_template_decl;
1241 tree subst = instantiate_requirements (t, args);
1242 --processing_template_decl;
1244 if (subst == error_mark_node)
1246 inform (input_location, " substitution failure in %qE", t);
1247 return;
1250 tree t1 = TRAIT_EXPR_TYPE1 (subst);
1251 tree t2 = TRAIT_EXPR_TYPE2 (subst);
1252 switch (TRAIT_EXPR_KIND (t))
1254 case CPTK_HAS_NOTHROW_ASSIGN:
1255 inform (loc, " %qT is not nothrow assignable", t1);
1256 break;
1257 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
1258 inform (loc, " %qT is not nothrow constructible", t1);
1259 break;
1260 case CPTK_HAS_NOTHROW_COPY:
1261 inform (loc, " %qT is not nothrow copyable", t1);
1262 break;
1263 case CPTK_HAS_TRIVIAL_ASSIGN:
1264 inform (loc, " %qT is not trivially assignable", t1);
1265 break;
1266 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
1267 inform (loc, " %qT is not trivially constructible", t1);
1268 break;
1269 case CPTK_HAS_TRIVIAL_COPY:
1270 inform (loc, " %qT is not trivially copyable", t1);
1271 break;
1272 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
1273 inform (loc, " %qT is not trivially destructible", t1);
1274 break;
1275 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
1276 inform (loc, " %qT does not have a virtual destructor", t1);
1277 break;
1278 case CPTK_IS_ABSTRACT:
1279 inform (loc, " %qT is not an abstract class", t1);
1280 break;
1281 case CPTK_IS_BASE_OF:
1282 inform (loc, " %qT is not a base of %qT", t1, t2);
1283 break;
1284 case CPTK_IS_CLASS:
1285 inform (loc, " %qT is not a class", t1);
1286 break;
1287 case CPTK_IS_CONVERTIBLE_TO:
1288 inform (loc, " %qT is not convertible to %qT", t1, t2);
1289 break;
1290 case CPTK_IS_EMPTY:
1291 inform (loc, " %qT is not an empty class", t1);
1292 break;
1293 case CPTK_IS_ENUM:
1294 inform (loc, " %qT is not an enum", t1);
1295 break;
1296 case CPTK_IS_FINAL:
1297 inform (loc, " %qT is not a final class", t1);
1298 break;
1299 case CPTK_IS_LITERAL_TYPE:
1300 inform (loc, " %qT is not a literal type", t1);
1301 break;
1302 case CPTK_IS_POD:
1303 inform (loc, " %qT is not a POD type", t1);
1304 break;
1305 case CPTK_IS_POLYMORPHIC:
1306 inform (loc, " %qT is not a polymorphic type", t1);
1307 break;
1308 case CPTK_IS_SAME_AS:
1309 inform (loc, " %qT is not the same as %qT", t1, t2);
1310 break;
1311 case CPTK_IS_STD_LAYOUT:
1312 inform (loc, " %qT is not an standard layout type", t1);
1313 break;
1314 case CPTK_IS_TRIVIAL:
1315 inform (loc, " %qT is not a trivial type", t1);
1316 break;
1317 case CPTK_IS_UNION:
1318 inform (loc, " %qT is not a union", t1);
1319 break;
1320 default:
1321 gcc_unreachable ();
1325 // Diagnose a failed concept check in concept indicated by T, where
1326 // T is the result of resolve_constraint_check. Recursively analyze
1327 // the nested requiremets for details.
1328 void
1329 diagnose_check (location_t loc, tree t, tree args)
1331 tree fn = TREE_VALUE (t);
1332 tree targs = TREE_PURPOSE (t);
1333 tree body = DECL_SAVED_TREE (fn);
1334 if (!body)
1335 return;
1337 inform (loc, " failure in constraint %q#D", DECL_TI_TEMPLATE (fn));
1339 // Perform a mini-reduction on the constraint.
1340 if (TREE_CODE (body) == BIND_EXPR)
1341 body = BIND_EXPR_BODY (body);
1342 if (TREE_CODE (body) == RETURN_EXPR)
1343 body = TREE_OPERAND (body, 0);
1345 // Locally instantiate the body with the call's template args,
1346 // and recursively diagnose.
1347 ++processing_template_decl;
1348 body = instantiate_requirements (body, targs);
1349 --processing_template_decl;
1351 diagnose_node (loc, body, args);
1354 // Diagnose constraint failures from the call expression T.
1355 void
1356 diagnose_call (location_t loc, tree t, tree args)
1358 if (check_requirements (t, args))
1359 return;
1361 // If this is a concept, we're going to recurse.
1362 // If it's just a call, then we can emit a simple message.
1363 if (tree check = resolve_constraint_check (t))
1364 diagnose_check (loc, check, args);
1365 else
1366 inform (loc, " %qE evaluated to false", t);
1369 // Diagnose specific constraint failures.
1370 void
1371 diagnose_requires (location_t loc, tree t, tree args)
1373 if (check_requirements (t, args))
1374 return;
1376 ++processing_template_decl;
1377 tree subst = instantiate_requirements (t, args);
1378 --processing_template_decl;
1380 // Print the header for the requires expression.
1381 tree parms = TREE_OPERAND (subst, 0);
1382 if (!VOID_TYPE_P (TREE_VALUE (parms)))
1383 inform (loc, " requiring syntax with values %Z", TREE_OPERAND (subst, 0));
1385 // Create a new local specialization binding for the arguments.
1386 // This lets us instantiate sub-expressions separately from the
1387 // requires clause.
1388 local_specialization_stack locals;
1389 declare_local_parms (TREE_OPERAND (t, 0), TREE_OPERAND (subst, 0));
1391 // Iterate over the sub-requirements and try instantiating each.
1392 for (tree l = TREE_OPERAND (t, 1); l; l = TREE_CHAIN (l))
1393 diagnose_node (loc, TREE_VALUE (l), args);
1396 static void
1397 diagnose_validexpr (location_t loc, tree t, tree args)
1399 if (check_requirements (t, args))
1400 return;
1401 inform (loc, " %qE is not a valid expression", TREE_OPERAND (t, 0));
1404 static void
1405 diagnose_validtype (location_t loc, tree t, tree args)
1407 if (check_requirements (t, args))
1408 return;
1410 // Substitute into the qualified name.
1411 tree name = TREE_OPERAND (t, 0);
1412 if (tree cxt = TYPE_CONTEXT (name))
1414 tree id = TYPE_IDENTIFIER (name);
1415 cxt = tsubst (cxt, args, tf_none, NULL_TREE);
1416 name = build_qualified_name (NULL_TREE, cxt, id, false);
1417 inform (loc, " %qE does not name a valid type", name);
1419 else
1421 inform (loc, " %qT does not name a valid type", name);
1425 static void
1426 diagnose_constexpr (location_t loc, tree t, tree args)
1428 if (check_requirements (t, args))
1429 return;
1430 inform (loc, " %qE is not a constant expression", TREE_OPERAND (t, 0));
1433 static void
1434 diagnose_noexcept (location_t loc, tree t, tree args)
1436 if (check_requirements (t, args))
1437 return;
1438 inform (loc, " %qE propagates exceptions", TREE_OPERAND (t, 0));
1441 // Diagnose a constraint failure in the expression T.
1442 void
1443 diagnose_other (location_t loc, tree t, tree args)
1445 if (check_requirements (t, args))
1446 return;
1447 inform (loc, " %qE evaluated to false", t);
1450 // Diagnose a constraint failure in the subtree T.
1451 void
1452 diagnose_node (location_t loc, tree t, tree args)
1454 switch (TREE_CODE (t))
1456 case TRUTH_ANDIF_EXPR:
1457 diagnose_node (loc, TREE_OPERAND (t, 0), args);
1458 diagnose_node (loc, TREE_OPERAND (t, 1), args);
1459 break;
1461 case TRUTH_ORIF_EXPR:
1462 // TODO: Design better diagnostics for dijunctions.
1463 diagnose_other (loc, t, args);
1464 break;
1466 case TRAIT_EXPR:
1467 diagnose_trait (loc, t, args);
1468 break;
1470 case CALL_EXPR:
1471 diagnose_call (loc, t, args);
1472 break;
1474 case REQUIRES_EXPR:
1475 diagnose_requires (loc, t, args);
1476 break;
1478 case VALIDEXPR_EXPR:
1479 diagnose_validexpr (loc, t, args);
1480 break;
1482 case VALIDTYPE_EXPR:
1483 diagnose_validtype (loc, t, args);
1484 break;
1486 case CONSTEXPR_EXPR:
1487 diagnose_constexpr (loc, t, args);
1488 break;
1490 case NOEXCEPT_EXPR:
1491 diagnose_noexcept (loc, t, args);
1492 break;
1494 default:
1495 diagnose_other (loc, t, args);
1496 break;
1500 // Diagnose a constraint failure in the requirements expression REQS.
1501 inline void
1502 diagnose_requirements (location_t loc, tree reqs, tree args)
1504 diagnose_node (loc, reqs, args);
1507 // Create a tree node representing the substitution of ARGS into
1508 // the parameters of TMPL. The resulting structure is passed as an
1509 // for diagnosing substitutions.
1510 inline tree
1511 make_subst (tree tmpl, tree args)
1513 tree subst = tree_cons (NULL_TREE, args, NULL_TREE);
1514 TREE_TYPE (subst) = DECL_TEMPLATE_PARMS (tmpl);
1515 return subst;
1518 } // namespace
1520 // Emit diagnostics detailing the failure ARGS to satisfy the constraints
1521 // of the template declaration, TMPL.
1522 void
1523 diagnose_constraints (location_t loc, tree tmpl, tree args)
1525 inform (loc, " constraints not satisfied %S", make_subst (tmpl, args));
1527 // Diagnose the constraints by recursively decomposing and
1528 // evaluating the template requirements.
1529 tree reqs = CI_SPELLING (DECL_CONSTRAINTS (tmpl));
1530 diagnose_requirements (loc, reqs, args);