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