2014-06-11 Andrew Sutton <andrew.n.sutton@gmail.com>
[official-gcc.git] / gcc / cp / constraint.cc
blobb7a71c146ed84df414e01d322510b91efa37f71a
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);
197 // -------------------------------------------------------------------------- //
198 // Requirement Reduction
200 // Reduces a template requirement to a logical formula written in terms of
201 // atomic propositions, returing the new expression. If the expression cannot
202 // be reduced, a NULL_TREE is returned, indicating failure to reduce the
203 // original requirment.
205 namespace {
207 // Helper functions
208 static tree reduce_node (tree);
209 static tree reduce_expr (tree);
210 static tree reduce_stmt (tree);
211 static tree reduce_decl (tree);
212 static tree reduce_misc (tree);
214 static tree reduce_logical (tree);
215 static tree reduce_call (tree);
216 static tree reduce_requires (tree);
217 static tree reduce_expr_req (tree);
218 static tree reduce_type_req (tree);
219 static tree reduce_nested_req (tree);
220 static tree reduce_template_id (tree);
221 static tree reduce_stmt_list (tree);
223 // Reduce the requirement T into a logical formula written in terms of
224 // atomic propositions.
225 tree
226 reduce_node (tree t)
228 switch (TREE_CODE_CLASS (TREE_CODE (t)))
230 case tcc_unary:
231 case tcc_binary:
232 case tcc_expression:
233 case tcc_vl_exp:
234 return reduce_expr (t);
236 case tcc_statement:
237 return reduce_stmt (t);
239 case tcc_declaration:
240 return reduce_decl (t);
242 case tcc_exceptional:
243 return reduce_misc (t);
245 // These kinds of expressions are atomic.
246 case tcc_constant:
247 case tcc_reference:
248 case tcc_comparison:
249 return t;
251 default:
252 gcc_unreachable ();
254 return NULL_TREE;
257 // Reduction rules for the expression node T.
258 tree
259 reduce_expr (tree t)
261 switch (TREE_CODE (t))
263 case TRUTH_ANDIF_EXPR:
264 case TRUTH_ORIF_EXPR:
265 return reduce_logical (t);
267 case CALL_EXPR:
268 return reduce_call (t);
270 case REQUIRES_EXPR:
271 return reduce_requires (t);
273 case EXPR_REQ:
274 return reduce_expr_req (t);
276 case TYPE_REQ:
277 return reduce_type_req (t);
279 case NESTED_REQ:
280 return reduce_nested_req (t);
282 case TEMPLATE_ID_EXPR:
283 return reduce_template_id (t);
285 case CAST_EXPR:
286 return reduce_node (TREE_VALUE (TREE_OPERAND (t, 0)));
288 case BIND_EXPR:
289 return reduce_node (BIND_EXPR_BODY (t));
291 // Do not recurse.
292 case TAG_DEFN:
293 return NULL_TREE;
295 // Everything else is atomic.
296 default:
297 return t;
302 // Reduction rules for the statement T.
303 tree
304 reduce_stmt (tree t)
306 switch (TREE_CODE (t))
308 // Reduce the returned expression.
309 case RETURN_EXPR:
310 return reduce_node (TREE_OPERAND (t, 0));
312 // These statements do not introduce propositions
313 // in the constraints language. Do not recurse.
314 case DECL_EXPR:
315 case USING_STMT:
316 return NULL_TREE;
318 default:
319 gcc_unreachable ();
321 return NULL_TREE;
324 // Reduction rules for the declaration T.
325 tree
326 reduce_decl (tree t)
328 switch (TREE_CODE (t))
330 // References to var decls are atomic.
331 case VAR_DECL:
332 return t;
334 default:
335 gcc_unreachable ();
337 return NULL_TREE;
340 // Reduction rules for the node T.
341 tree
342 reduce_misc (tree t)
344 switch (TREE_CODE (t))
346 // Errors and traits are atomic.
347 case ERROR_MARK:
348 case TRAIT_EXPR:
349 return t;
351 case STATEMENT_LIST:
352 return reduce_stmt_list (t);
354 default:
355 gcc_unreachable ();
357 return NULL_TREE;
360 // Reduction rules for the binary logical expression T (&& and ||).
362 // Generate a new expression from the reduced operands. If either operand
363 // cannot be reduced, then the resulting expression is null.
364 tree
365 reduce_logical (tree t)
367 tree l = reduce_expr (TREE_OPERAND (t, 0));
368 tree r = reduce_expr (TREE_OPERAND (t, 1));
369 if (l && r)
371 t = copy_node (t);
372 TREE_OPERAND (t, 0) = l;
373 TREE_OPERAND (t, 1) = r;
374 return t;
376 else
377 return NULL_TREE;
380 // Reduction rules for the call expression T.
382 // If T is a call to a constraint instantiate its definition and
383 // recursively reduce its returned expression.
384 tree
385 reduce_call (tree t)
387 // Is the function call actually a constraint check?
388 tree check = resolve_constraint_check (t);
389 if (!check)
390 return t;
392 tree fn = TREE_VALUE (check);
393 tree args = TREE_PURPOSE (check);
395 // Reduce the body of the function into the constriants language.
396 tree body = reduce_requirements (DECL_SAVED_TREE (fn));
397 if (!body)
399 error ("could not inline requirements from %qD", fn);
400 return error_mark_node;
403 // Instantiate the reduced results using the deduced args.
404 tree result = instantiate_requirements (body, args);
405 if (result == error_mark_node)
407 error ("could not instantiate requirements from %qD", fn);
408 return error_mark_node;
410 return result;
413 // Reduction rules for the template-id T.
415 // It turns out that we often get requirements being written like this:
417 // template<typename T>
418 // requires Foo<T>
419 // void f()
421 // Where Foo<T> should actually be written as Foo<T>(). Generate an
422 // error and suggest the improved writing.
423 tree
424 reduce_template_id (tree t)
426 vec<tree, va_gc>* args = NULL;
427 tree c = finish_call_expr (t, &args, true, false, 0);
428 error_at (EXPR_LOCATION (t), "invalid requirement");
429 inform (EXPR_LOCATION (t), "did you mean %qE", c);
430 return c;
434 // Reduce an expression requirement as a conjunction of its
435 // individual constraints.
436 tree
437 reduce_expr_req (tree t)
439 tree r = NULL_TREE;
440 for (tree l = TREE_OPERAND (t, 0); l; l = TREE_CHAIN (l))
441 r = conjoin_requirements (r, reduce_expr (TREE_VALUE (l)));
442 return r;
445 // Reduce a type requirement by returing its underlying
446 // constraint.
447 tree
448 reduce_type_req (tree t)
450 return TREE_OPERAND (t, 0);
453 // Reduce a nested requireemnt by returing its only operand.
454 tree
455 reduce_nested_req (tree t)
457 return TREE_OPERAND (t, 0);
460 // Reduce a requires expr by reducing each requirement in turn,
461 // rewriting the list of requirements so that we end up with a
462 // list of expressions, some of which may be conjunctions.
463 tree
464 reduce_requires (tree t)
466 for (tree l = TREE_OPERAND (t, 1); l; l = TREE_CHAIN (l))
467 TREE_VALUE (l) = reduce_expr (TREE_VALUE (l));
468 return t;
471 // Reduction rules for the statement list STMTS.
473 // Recursively reduce each statement in the list, concatenating each
474 // reduced result into a conjunction of requirements.
476 // A constexpr function may include statements other than a return
477 // statement. The primary purpose of these rules is to filter those
478 // non-return statements from the constraints language.
479 tree
480 reduce_stmt_list (tree stmts)
482 tree lhs = NULL_TREE;
483 tree_stmt_iterator i = tsi_start (stmts);
484 while (!tsi_end_p (i))
486 if (tree rhs = reduce_node (tsi_stmt (i)))
487 lhs = conjoin_requirements (lhs, rhs);
488 tsi_next (&i);
490 return lhs;
493 } // end namespace
495 // Reduce the requirement REQS into a logical formula written in terms of
496 // atomic propositions.
497 tree
498 reduce_requirements (tree reqs)
500 return reduce_node (reqs);
503 // -------------------------------------------------------------------------- //
504 // Constraint Semantic Processing
506 // The following functions are called by the parser and substitution rules
507 // to create and evaluate constraint-related nodes.
509 // Create a constraint-info node from the specified requirements.
510 tree
511 make_constraints (tree reqs)
513 // No requirements == no constraints
514 if (!reqs)
515 return NULL_TREE;
517 // Reduce the requirements into a single expression of constraints.
518 tree expr = reduce_requirements (reqs);
519 if (expr == error_mark_node)
520 return error_mark_node;
522 // Decompose those expressions into lists of lists of atomic
523 // propositions.
524 tree assume = decompose_assumptions (expr);
526 // Build the constraint info.
527 tree_constraint_info *cinfo =
528 (tree_constraint_info *)make_node (CONSTRAINT_INFO);
529 cinfo->spelling = reqs;
530 cinfo->requirements = expr;
531 cinfo->assumptions = assume;
532 return (tree)cinfo;
535 // Returns the template constraints of declaration T. If T is not a
536 // template, this return NULL_TREE. Note that T must be non-null.
537 tree
538 get_constraints (tree t)
540 gcc_assert (DECL_P (t));
541 if (TREE_CODE (t) != TEMPLATE_DECL)
543 if (!DECL_TEMPLATE_INFO (t))
544 return NULL_TREE;
545 else
546 return DECL_CONSTRAINTS (DECL_TI_TEMPLATE (t));
548 return DECL_CONSTRAINTS (t);
551 // Returns a conjunction of shorthand requirements for the template
552 // parameter list PARMS. Note that the requirements are stored in
553 // the TYPE of each tree node.
554 tree
555 get_shorthand_requirements (tree parms)
557 tree reqs = NULL_TREE;
558 parms = INNERMOST_TEMPLATE_PARMS (parms);
559 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
561 tree parm = TREE_VEC_ELT (parms, i);
562 reqs = conjoin_requirements(reqs, TREE_TYPE (parm));
564 return reqs;
567 // Finish the template requirement, EXPR, by translating it into
568 // a constraint information record.
569 tree
570 finish_template_requirements (tree expr)
572 if (expr == error_mark_node)
573 return NULL_TREE;
574 else
575 return make_constraints (expr);
578 tree
579 build_requires_expr (tree parms, tree reqs)
581 // Modify the declared parameters by removing their context (so they
582 // don't refer to the enclosing scope), and marking them constant (so
583 // we can actually check constexpr properties).
584 for (tree p = parms; p && !VOID_TYPE_P (TREE_VALUE (p)); p = TREE_CHAIN (p))
586 tree parm = TREE_VALUE (p);
587 DECL_CONTEXT (parm) = NULL_TREE;
588 TREE_CONSTANT (parm) = true;
591 // Build the node.
592 tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs);
593 TREE_SIDE_EFFECTS (r) = false;
594 TREE_CONSTANT (r) = true;
595 return r;
598 // Evaluate an instantiatd requires expr, returning the truth node
599 // only when all sub-requirements have evaluated to true.
600 tree
601 eval_requires_expr (tree reqs)
603 for (tree t = reqs ; t; t = TREE_CHAIN (t)) {
604 tree r = TREE_VALUE (t);
605 r = fold_non_dependent_expr (r);
606 r = maybe_constant_value (r);
607 if (r != boolean_true_node)
608 return boolean_false_node;
610 return boolean_true_node;
613 // Finish a requires expression, returning a node wrapping the parameters,
614 // PARMS, and the list of requirements REQS.
615 tree
616 finish_requires_expr (tree parms, tree reqs)
618 if (processing_template_decl)
619 return build_requires_expr (parms, reqs);
620 else
621 return eval_requires_expr (reqs);
624 // Construct a unary expression that evaluates properties of the
625 // expression or type T, and has a boolean result type.
626 static inline tree
627 build_check_expr (tree_code c, tree t)
629 tree r = build_min (c, boolean_type_node, t);
630 TREE_SIDE_EFFECTS (r) = false;
631 TREE_READONLY (r) = true;
632 TREE_CONSTANT (r) = true;
633 return r;
636 // Finish a syntax requirement, constructing a list embodying a sequence
637 // of checks for the validity of EXPR and TYPE, the convertibility of
638 // EXPR to TYPE, and the expression properties specified in SPECS.
639 tree
640 finish_expr_requirement (tree expr, tree type, tree specs)
642 gcc_assert (processing_template_decl);
644 // Build a list of checks, starting with the valid expression.
645 tree result = tree_cons (NULL_TREE, finish_validexpr_expr (expr), NULL_TREE);
647 // If a type requirement was provided, build the result type checks.
648 if (type)
650 // If the type is dependent, ensure that it can be validly
651 // instantiated.
653 // NOTE: We can also disregard checks that result in the template
654 // parameter.
655 if (dependent_type_p (type))
657 tree treq = finish_type_requirement (type);
658 result = tree_cons (NULL_TREE, treq, result);
661 // Ensure that the result of the expression can be converted to
662 // the result type.
663 tree decl_type = finish_decltype_type (expr, false, tf_none);
664 tree creq = finish_trait_expr (CPTK_IS_CONVERTIBLE_TO, decl_type, type);
665 result = tree_cons (NULL_TREE, creq, result);
668 // If constraint specifiers are present, make them part of the
669 // list of constraints.
670 if (specs)
672 TREE_CHAIN (tree_last (specs)) = result;
673 result = specs;
676 // Finally, construct the syntactic requirement.
677 return build_check_expr (EXPR_REQ, nreverse (result));
680 // Finish a simple syntax requirement, returning a node representing
681 // a check that EXPR is a valid expression.
682 tree
683 finish_expr_requirement (tree expr)
685 gcc_assert (processing_template_decl);
686 tree req = finish_validexpr_expr (expr);
687 tree reqs = tree_cons (NULL_TREE, req, NULL_TREE);
688 return build_check_expr (EXPR_REQ, reqs);
691 // Finish a type requirement, returning a node representing a check
692 // that TYPE will result in a valid type when instantiated.
693 tree
694 finish_type_requirement (tree type)
696 gcc_assert (processing_template_decl);
697 tree req = finish_validtype_expr (type);
698 return build_check_expr (TYPE_REQ, req);
701 tree
702 finish_nested_requirement (tree expr)
704 gcc_assert (processing_template_decl);
705 return build_check_expr (NESTED_REQ, expr);
708 // Finish a constexpr requirement, returning a node representing a
709 // check that EXPR, when instantiated, may be evaluated at compile time.
710 tree
711 finish_constexpr_requirement (tree expr)
713 gcc_assert (processing_template_decl);
714 return finish_constexpr_expr (expr);
717 // Finish the noexcept requirement by constructing a noexcept
718 // expression evaluating EXPR.
719 tree
720 finish_noexcept_requirement (tree expr)
722 gcc_assert (processing_template_decl);
723 return finish_noexcept_expr (expr, tf_none);
726 // Returns the true or false node depending on the truth value of B.
727 static inline tree
728 truth_node (bool b)
730 return b ? boolean_true_node : boolean_false_node;
733 // Returns a finished validexpr-expr. Returns the true or false node
734 // depending on whether EXPR denotes a valid expression. This is the case
735 // when the expression has been successfully type checked.
737 // When processing a template declaration, the result is an expression
738 // representing the check.
739 tree
740 finish_validexpr_expr (tree expr)
742 if (processing_template_decl)
743 return build_check_expr (VALIDEXPR_EXPR, expr);
744 return truth_node (expr && expr != error_mark_node);
747 // Returns a finished validtype-expr. Returns the true or false node
748 // depending on whether T denotes a valid type name.
750 // When processing a template declaration, the result is an expression
751 // representing the check.
753 // FIXME: Semantics need to be aligned with the new version of the
754 // specificaiton (i.e., we must be able to invent a function and
755 // perform argument deduction against it).
756 tree
757 finish_validtype_expr (tree type)
759 if (is_auto (type))
761 sorry ("%<auto%< not supported in result type constraints\n");
762 return error_mark_node;
765 if (processing_template_decl)
766 return build_check_expr (VALIDTYPE_EXPR, type);
767 return truth_node (type && TYPE_P (type));
770 // Returns a finished constexpr-expr. Returns the true or false node
771 // depending on whether the expression T may be evaluated at compile
772 // time.
774 // When processing a template declaration, the result is an expression
775 // representing the check.
776 tree
777 finish_constexpr_expr (tree expr)
779 if (processing_template_decl)
780 return build_check_expr (CONSTEXPR_EXPR, expr);
782 // TODO: Actually check that the expression can be constexpr
783 // evaluatd.
785 // return truth_node (potential_constant_expression (expr));
786 sorry ("constexpr requirement");
787 return NULL_TREE;
790 // Check that a constrained friend declaration function declaration,
791 // FN, is admissable. This is the case only when the declaration depends
792 // on template parameters and does not declare a specialization.
793 void
794 check_constrained_friend (tree fn, tree reqs)
796 if (fn == error_mark_node)
797 return;
798 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
800 // If there are not constraints, this cannot be an error.
801 if (!reqs)
802 return;
804 // Constrained friend functions that don't depend on template
805 // arguments are effectively meaningless.
806 tree parms = DECL_ARGUMENTS (fn);
807 tree result = TREE_TYPE (TREE_TYPE (fn));
808 if (!(parms && uses_template_parms (parms)) && !uses_template_parms (result))
810 error ("constrained friend does not depend on template parameters");
811 return;
815 // Given an overload set, OVL, and a template argument or placeholder, ARG,
816 // synthesize a call expression that resolves to a concept check of
817 // the expression the form OVL<ARG>().
819 // TODO: Extend this to take a variable concept also.
820 tree
821 build_concept_check (tree ovl, tree arg)
823 gcc_assert (TREE_CODE (ovl) == OVERLOAD);
825 // Build a template-id that acts as the call target using OVL as
826 // the template and ARG as the only explicit argument.
827 tree targs = make_tree_vec (1);
828 TREE_VEC_ELT (targs, 0) = arg;
829 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, 1);
830 tree id = lookup_template_function (ovl, targs);
832 // Build a new call expression, but don't actually generate a new
833 // function call. We just want the tree, not the semantics.
834 ++processing_template_decl;
835 vec<tree, va_gc> *fargs = make_tree_vector();
836 tree call = finish_call_expr (id, &fargs, false, false, tf_none);
837 --processing_template_decl;
839 return call;
842 // Returns a TYPE_DECL that contains sufficient information to build
843 // a template parameter of the same kind as PROTO and constrained
844 // by the concept declaration FN. PROTO is saved as the initializer of
845 // the new type decl, and the constraining function is saved in
846 // DECL_SIZE_UNIT.
847 tree
848 build_constrained_parameter (tree proto, tree fn)
850 tree name = DECL_NAME (fn);
851 tree type = TREE_TYPE (proto);
852 tree decl = build_decl (input_location, TYPE_DECL, name, type);
853 DECL_INITIAL (decl) = proto; // Describing parameter
854 DECL_SIZE_UNIT (decl) = fn; // Constraining function declaration
855 return decl;
858 // Create a requirement expression for the given DECL that evaluates the
859 // requirements specified by CONSTR, a TYPE_DECL that contains all the
860 // information necessary to build the requirements (see finish_concept_name
861 // for the layout of that TYPE_DECL).
863 // Note that the constraints are neither reduced nor decomposed. That is
864 // done only after the requires clause has been parsed (or not).
865 tree
866 finish_shorthand_requirement (tree decl, tree constr)
868 // No requirements means no constraints.
869 if (!constr)
870 return NULL_TREE;
872 tree proto = DECL_INITIAL (constr); // The prototype declaration
873 tree con = DECL_SIZE_UNIT (constr); // The concept declaration
875 // If the parameter declaration is variadic, but the concept is not
876 // then we need to apply the concept to every element in the pack.
877 bool is_proto_pack = template_parameter_pack_p (proto);
878 bool is_decl_pack = template_parameter_pack_p (decl);
879 bool apply_to_all_p = is_decl_pack && !is_proto_pack;
881 // Get the argument and overload used for the requirement. Adjust
882 // if we're going to expand later.
883 tree arg = template_parm_to_arg (build_tree_list (NULL_TREE, decl));
884 if (apply_to_all_p)
885 arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
887 // Build the concept check. If it the constraint needs to be applied
888 // to all elements of the parameter pack, then expand make the constraint
889 // an expansion.
890 tree ovl = build_overload (DECL_TI_TEMPLATE (con), NULL_TREE);
891 tree check = build_concept_check (ovl, arg);
892 if (apply_to_all_p)
894 check = make_pack_expansion (check);
896 // Set the type to indicate that this expansion will get special
897 // treatment during instantiation.
899 // TODO: Maybe this should be a different kind of node... one that
900 // has all the same properties as a pack expansion, but has a definite
901 // expansion when instantiated as part of an expression.
903 // As of now, this is a hack.
904 TREE_TYPE (check) = boolean_type_node;
907 return check;
910 // -------------------------------------------------------------------------- //
911 // Substitution Rules
913 // The following functions implement substitution rules for constraints.
915 namespace {
916 // In an unevaluated context, the substitution of parm decls are not
917 // properly chained during substitution. Do that here.
918 tree
919 fix_local_parms (tree sparms)
921 if (!sparms)
922 return sparms;
924 tree p = TREE_CHAIN (sparms);
925 tree q = sparms;
926 while (p && TREE_VALUE (p) != void_type_node)
928 DECL_CHAIN (TREE_VALUE (q)) = TREE_VALUE (p);
929 q = p;
930 p = TREE_CHAIN (p);
932 return sparms;
935 // Register local specializations for each of tparm and the corresponding
936 // sparm. This is a helper function for tsubst_requires_expr.
937 void
938 declare_local_parms (tree tparms, tree sparms)
940 tree s = TREE_VALUE (sparms);
941 for (tree p = tparms; p && !VOID_TYPE_P (TREE_VALUE (p)); p = TREE_CHAIN (p))
943 tree t = TREE_VALUE (p);
944 if (DECL_PACK_P (t))
946 tree pack = extract_fnparm_pack (t, &s);
947 register_local_specialization (pack, t);
949 else
951 register_local_specialization (s, t);
952 s = TREE_CHAIN (s);
957 // Substitute ARGS into the parameter list T, producing a sequence of
958 // local parameters (variables) in the current scope.
959 tree
960 tsubst_local_parms (tree t,
961 tree args,
962 tsubst_flags_t complain,
963 tree in_decl)
965 tree r = fix_local_parms (tsubst (t, args, complain, in_decl));
966 if (r == error_mark_node)
967 return error_mark_node;
969 // Register the instantiated args as local parameters.
970 if (t)
971 declare_local_parms (t, r);
973 return r;
976 // Substitute ARGS into the requirement body (list of requirements), T.
977 tree
978 tsubst_requirement_body (tree t, tree args, tree in_decl)
980 cp_unevaluated guard;
981 tree r = NULL_TREE;
982 while (t)
984 // If any substitutions fail, then this is equivalent to
985 // returning false.
986 tree e = tsubst_expr (TREE_VALUE (t), args, tf_none, in_decl, false);
987 if (e == error_mark_node)
988 e = boolean_false_node;
989 r = tree_cons (NULL_TREE, e, r);
990 t = TREE_CHAIN (t);
992 return r;
994 } // namespace
996 // Substitute ARGS into the requires expression T.
997 tree
998 tsubst_requires_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1000 local_specialization_stack stack;
1001 tree p = tsubst_local_parms (TREE_OPERAND (t, 0), args, complain, in_decl);
1002 tree r = tsubst_requirement_body (TREE_OPERAND (t, 1), args, in_decl);
1003 return finish_requires_expr (p, r);
1006 // Substitute ARGS into the valid-expr expression T.
1007 tree
1008 tsubst_validexpr_expr (tree t, tree args, tree in_decl)
1010 tree r = tsubst_expr (TREE_OPERAND (t, 0), args, tf_none, in_decl, false);
1011 return finish_validexpr_expr (r);
1014 // Substitute ARGS into the valid-type expression T.
1015 tree
1016 tsubst_validtype_expr (tree t, tree args, tree in_decl)
1018 tree r = tsubst (TREE_OPERAND (t, 0), args, tf_none, in_decl);
1019 return finish_validtype_expr (r);
1022 // Substitute ARGS into the constexpr expression T.
1023 tree
1024 tsubst_constexpr_expr (tree t, tree args, tree in_decl)
1026 tree r = tsubst_expr (TREE_OPERAND (t, 0), args, tf_none, in_decl, false);
1027 return finish_constexpr_expr (r);
1030 // Substitute ARGS into the expr requirement T. Note that a requirement
1031 // node is instantiated from a non-reduced context (e.g., static_assert).
1032 tree
1033 tsubst_expr_req (tree t, tree args, tree in_decl)
1035 tree r = NULL_TREE;
1036 for (tree l = TREE_OPERAND (t, 0); l; l = TREE_CHAIN (l))
1038 tree e = tsubst_expr (TREE_VALUE (l), args, tf_none, in_decl, false);
1039 r = conjoin_requirements (r, e);
1041 return r;
1044 // Substitute ARGS into the type requirement T. Note that a requirement
1045 // node is instantiated from a non-reduced context (e.g., static_assert).
1046 tree
1047 tsubst_type_req (tree t, tree args, tree in_decl)
1049 return tsubst_expr (TREE_OPERAND (t, 0), args, tf_none, in_decl, false);
1052 // Substitute ARGS into the nested requirement T. Note that a requirement
1053 // node is instantiated from a non-reduced context (e.g., static_assert).
1054 tree
1055 tsubst_nested_req (tree t, tree args, tree in_decl)
1057 return tsubst_expr (TREE_OPERAND (t, 0), args, tf_none, in_decl, false);
1060 // Substitute the template arguments ARGS into the requirement
1061 // expression REQS. Errors resulting from substitution are not
1062 // diagnosed.
1063 tree
1064 instantiate_requirements (tree reqs, tree args)
1066 return tsubst_expr (reqs, args, tf_none, NULL_TREE, false);
1069 // -------------------------------------------------------------------------- //
1070 // Constraint Satisfaction
1072 // The following functions are responsible for the instantiation and
1073 // evaluation of constraints.
1075 namespace {
1076 // Returns true if the requirements expression REQS is satisfied
1077 // and false otherwise. The requirements are checked by simply
1078 // evaluating REQS as a constant expression.
1079 static inline bool
1080 check_requirements (tree reqs)
1082 // Reduce any remaining TRAIT_EXPR nodes before evaluating.
1083 reqs = fold_non_dependent_expr (reqs);
1085 // Requirements are satisfied when REQS evaluates to true.
1086 return cxx_constant_value (reqs) == boolean_true_node;
1089 // Returns true if the requirements expression REQS is satisfied
1090 // and false otherwise. The requirements are checked by first
1091 // instantiating REQS and then evaluating it as a constant expression.
1092 static inline bool
1093 check_requirements (tree reqs, tree args)
1095 // If any arguments are dependent, then we can't check the
1096 // requirements. Just return true.
1097 if (uses_template_parms (args))
1098 return true;
1100 // Instantiate and evaluate the requirements.
1101 reqs = instantiate_requirements (reqs, args);
1102 if (reqs == error_mark_node)
1103 return false;
1104 return check_requirements (reqs);
1106 } // namespace
1108 // Check the instantiated declaration constraints.
1109 bool
1110 check_constraints (tree cinfo)
1112 // No constraints? Satisfied.
1113 if (!cinfo)
1114 return true;
1115 return check_requirements (CI_REQUIREMENTS (cinfo));
1118 // Check the constraints in CINFO against the given ARGS, returning
1119 // true when the constraints are satisfied and false otherwise.
1120 bool
1121 check_constraints (tree cinfo, tree args)
1123 // No constraints? Satisfied.
1124 if (!cinfo)
1125 return true;
1127 // Dependent arguments? Satisfied. They won't reduce to true or false.
1128 if (uses_template_parms (args))
1129 return true;
1131 return check_requirements (CI_REQUIREMENTS (cinfo), args);
1134 // Check the constraints of the declaration or type T, against
1135 // the specified arguments. Returns true if the constraints are
1136 // satisfied and false otherwise.
1137 bool
1138 check_template_constraints (tree t, tree args)
1140 return check_constraints (DECL_CONSTRAINTS (t), args);
1143 // -------------------------------------------------------------------------- //
1144 // Constraint Relations
1146 // Interfaces for determining equivalency and ordering of constraints.
1148 // Returns true when A and B are equivalent constraints.
1149 bool
1150 equivalent_constraints (tree a, tree b)
1152 if (a == b)
1153 return true;
1154 else
1155 return subsumes (a, b) && subsumes (b, a);
1158 // Returns true if the template declarations A and B have equivalent
1159 // constraints. This is the case when A's constraints subsume B's and
1160 // when B's also constrain A's.
1161 bool
1162 equivalently_constrained (tree a, tree b)
1164 gcc_assert (TREE_CODE (a) == TREE_CODE (b));
1165 return equivalent_constraints (DECL_CONSTRAINTS (a), DECL_CONSTRAINTS (b));
1168 // Returns true when the A contains more atomic properties than B.
1169 bool
1170 more_constraints (tree a, tree b)
1172 return subsumes (a, b);
1175 // Returns true when the template declaration A's constraints subsume
1176 // those of the template declaration B.
1177 bool
1178 more_constrained (tree a, tree b)
1180 gcc_assert (TREE_CODE (a) == TREE_CODE (b));
1181 return more_constraints (DECL_CONSTRAINTS (a), DECL_CONSTRAINTS (b));
1185 // -------------------------------------------------------------------------- //
1186 // Constraint Diagnostics
1188 namespace {
1190 void diagnose_node (location_t, tree, tree);
1192 // Diagnose a constraint failure for type trait expressions.
1193 void
1194 diagnose_trait (location_t loc, tree t, tree args)
1196 if (check_requirements (t, args))
1197 return;
1199 ++processing_template_decl;
1200 tree subst = instantiate_requirements (t, args);
1201 --processing_template_decl;
1203 if (subst == error_mark_node)
1205 inform (input_location, " substitution failure in %qE", t);
1206 return;
1209 tree t1 = TRAIT_EXPR_TYPE1 (subst);
1210 tree t2 = TRAIT_EXPR_TYPE2 (subst);
1211 switch (TRAIT_EXPR_KIND (t))
1213 case CPTK_HAS_NOTHROW_ASSIGN:
1214 inform (loc, " %qT is not nothrow assignable", t1);
1215 break;
1216 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
1217 inform (loc, " %qT is not nothrow constructible", t1);
1218 break;
1219 case CPTK_HAS_NOTHROW_COPY:
1220 inform (loc, " %qT is not nothrow copyable", t1);
1221 break;
1222 case CPTK_HAS_TRIVIAL_ASSIGN:
1223 inform (loc, " %qT is not trivially assignable", t1);
1224 break;
1225 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
1226 inform (loc, " %qT is not trivially constructible", t1);
1227 break;
1228 case CPTK_HAS_TRIVIAL_COPY:
1229 inform (loc, " %qT is not trivially copyable", t1);
1230 break;
1231 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
1232 inform (loc, " %qT is not trivially destructible", t1);
1233 break;
1234 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
1235 inform (loc, " %qT does not have a virtual destructor", t1);
1236 break;
1237 case CPTK_IS_ABSTRACT:
1238 inform (loc, " %qT is not an abstract class", t1);
1239 break;
1240 case CPTK_IS_BASE_OF:
1241 inform (loc, " %qT is not a base of %qT", t1, t2);
1242 break;
1243 case CPTK_IS_CLASS:
1244 inform (loc, " %qT is not a class", t1);
1245 break;
1246 case CPTK_IS_CONVERTIBLE_TO:
1247 inform (loc, " %qT is not convertible to %qT", t1, t2);
1248 break;
1249 case CPTK_IS_EMPTY:
1250 inform (loc, " %qT is not an empty class", t1);
1251 break;
1252 case CPTK_IS_ENUM:
1253 inform (loc, " %qT is not an enum", t1);
1254 break;
1255 case CPTK_IS_FINAL:
1256 inform (loc, " %qT is not a final class", t1);
1257 break;
1258 case CPTK_IS_LITERAL_TYPE:
1259 inform (loc, " %qT is not a literal type", t1);
1260 break;
1261 case CPTK_IS_POD:
1262 inform (loc, " %qT is not a POD type", t1);
1263 break;
1264 case CPTK_IS_POLYMORPHIC:
1265 inform (loc, " %qT is not a polymorphic type", t1);
1266 break;
1267 case CPTK_IS_SAME_AS:
1268 inform (loc, " %qT is not the same as %qT", t1, t2);
1269 break;
1270 case CPTK_IS_STD_LAYOUT:
1271 inform (loc, " %qT is not an standard layout type", t1);
1272 break;
1273 case CPTK_IS_TRIVIAL:
1274 inform (loc, " %qT is not a trivial type", t1);
1275 break;
1276 case CPTK_IS_UNION:
1277 inform (loc, " %qT is not a union", t1);
1278 break;
1279 default:
1280 gcc_unreachable ();
1284 // Diagnose a failed concept check in concept indicated by T, where
1285 // T is the result of resolve_constraint_check. Recursively analyze
1286 // the nested requiremets for details.
1287 void
1288 diagnose_check (location_t loc, tree t, tree args)
1290 tree fn = TREE_VALUE (t);
1291 tree targs = TREE_PURPOSE (t);
1292 tree body = DECL_SAVED_TREE (fn);
1293 if (!body)
1294 return;
1296 inform (loc, " failure in constraint %q#D", DECL_TI_TEMPLATE (fn));
1298 // Perform a mini-reduction on the constraint.
1299 if (TREE_CODE (body) == BIND_EXPR)
1300 body = BIND_EXPR_BODY (body);
1301 if (TREE_CODE (body) == RETURN_EXPR)
1302 body = TREE_OPERAND (body, 0);
1304 // Locally instantiate the body with the call's template args,
1305 // and recursively diagnose.
1306 ++processing_template_decl;
1307 body = instantiate_requirements (body, targs);
1308 --processing_template_decl;
1310 diagnose_node (loc, body, args);
1313 // Diagnose constraint failures from the call expression T.
1314 void
1315 diagnose_call (location_t loc, tree t, tree args)
1317 if (check_requirements (t, args))
1318 return;
1320 // If this is a concept, we're going to recurse.
1321 // If it's just a call, then we can emit a simple message.
1322 if (tree check = resolve_constraint_check (t))
1323 diagnose_check (loc, check, args);
1324 else
1325 inform (loc, " %qE evaluated to false", t);
1328 // Diagnose specific constraint failures.
1329 void
1330 diagnose_requires (location_t loc, tree t, tree args)
1332 if (check_requirements (t, args))
1333 return;
1335 ++processing_template_decl;
1336 tree subst = instantiate_requirements (t, args);
1337 --processing_template_decl;
1339 // Print the header for the requires expression.
1340 tree parms = TREE_OPERAND (subst, 0);
1341 if (!VOID_TYPE_P (TREE_VALUE (parms)))
1342 inform (loc, " requiring syntax with values %Z", TREE_OPERAND (subst, 0));
1344 // Create a new local specialization binding for the arguments.
1345 // This lets us instantiate sub-expressions separately from the
1346 // requires clause.
1347 local_specialization_stack locals;
1348 declare_local_parms (TREE_OPERAND (t, 0), TREE_OPERAND (subst, 0));
1350 // Iterate over the sub-requirements and try instantiating each.
1351 for (tree l = TREE_OPERAND (t, 1); l; l = TREE_CHAIN (l))
1352 diagnose_node (loc, TREE_VALUE (l), args);
1355 static void
1356 diagnose_validexpr (location_t loc, tree t, tree args)
1358 if (check_requirements (t, args))
1359 return;
1360 inform (loc, " %qE is not a valid expression", TREE_OPERAND (t, 0));
1363 static void
1364 diagnose_validtype (location_t loc, tree t, tree args)
1366 if (check_requirements (t, args))
1367 return;
1369 // Substitute into the qualified name.
1370 tree name = TREE_OPERAND (t, 0);
1371 if (tree cxt = TYPE_CONTEXT (name))
1373 tree id = TYPE_IDENTIFIER (name);
1374 cxt = tsubst (cxt, args, tf_none, NULL_TREE);
1375 name = build_qualified_name (NULL_TREE, cxt, id, false);
1376 inform (loc, " %qE does not name a valid type", name);
1378 else
1380 inform (loc, " %qT does not name a valid type", name);
1384 static void
1385 diagnose_constexpr (location_t loc, tree t, tree args)
1387 if (check_requirements (t, args))
1388 return;
1389 inform (loc, " %qE is not a constant expression", TREE_OPERAND (t, 0));
1392 static void
1393 diagnose_noexcept (location_t loc, tree t, tree args)
1395 if (check_requirements (t, args))
1396 return;
1397 inform (loc, " %qE propagates exceptions", TREE_OPERAND (t, 0));
1400 // Diagnose a constraint failure in the expression T.
1401 void
1402 diagnose_other (location_t loc, tree t, tree args)
1404 if (check_requirements (t, args))
1405 return;
1406 inform (loc, " %qE evaluated to false", t);
1409 // Diagnose a constraint failure in the subtree T.
1410 void
1411 diagnose_node (location_t loc, tree t, tree args)
1413 switch (TREE_CODE (t))
1415 case TRUTH_ANDIF_EXPR:
1416 diagnose_node (loc, TREE_OPERAND (t, 0), args);
1417 diagnose_node (loc, TREE_OPERAND (t, 1), args);
1418 break;
1420 case TRUTH_ORIF_EXPR:
1421 // TODO: Design better diagnostics for dijunctions.
1422 diagnose_other (loc, t, args);
1423 break;
1425 case TRAIT_EXPR:
1426 diagnose_trait (loc, t, args);
1427 break;
1429 case CALL_EXPR:
1430 diagnose_call (loc, t, args);
1431 break;
1433 case REQUIRES_EXPR:
1434 diagnose_requires (loc, t, args);
1435 break;
1437 case VALIDEXPR_EXPR:
1438 diagnose_validexpr (loc, t, args);
1439 break;
1441 case VALIDTYPE_EXPR:
1442 diagnose_validtype (loc, t, args);
1443 break;
1445 case CONSTEXPR_EXPR:
1446 diagnose_constexpr (loc, t, args);
1447 break;
1449 case NOEXCEPT_EXPR:
1450 diagnose_noexcept (loc, t, args);
1451 break;
1453 default:
1454 diagnose_other (loc, t, args);
1455 break;
1459 // Diagnose a constraint failure in the requirements expression REQS.
1460 inline void
1461 diagnose_requirements (location_t loc, tree reqs, tree args)
1463 diagnose_node (loc, reqs, args);
1466 // Create a tree node representing the substitution of ARGS into
1467 // the parameters of TMPL. The resulting structure is passed as an
1468 // for diagnosing substitutions.
1469 inline tree
1470 make_subst (tree tmpl, tree args)
1472 tree subst = tree_cons (NULL_TREE, args, NULL_TREE);
1473 TREE_TYPE (subst) = DECL_TEMPLATE_PARMS (tmpl);
1474 return subst;
1477 } // namespace
1479 // Emit diagnostics detailing the failure ARGS to satisfy the constraints
1480 // of the template declaration, TMPL.
1481 void
1482 diagnose_constraints (location_t loc, tree tmpl, tree args)
1484 inform (loc, " constraints not satisfied %S", make_subst (tmpl, args));
1486 // Diagnose the constraints by recursively decomposing and
1487 // evaluating the template requirements.
1488 tree reqs = CI_SPELLING (DECL_CONSTRAINTS (tmpl));
1489 diagnose_requirements (loc, reqs, args);