Merge from trunk
[official-gcc.git] / gcc / cp / constraint.cc
blobaaa0a32286e5562ef7cc02a64fb9bfa65c431e3f
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 "cp-tree.h"
27 #include "c-family/c-common.h"
28 #include "c-family/c-objc.h"
29 #include "tree-inline.h"
30 #include "intl.h"
31 #include "toplev.h"
32 #include "flags.h"
33 #include "timevar.h"
34 #include "diagnostic.h"
35 #include "cgraph.h"
36 #include "tree-iterator.h"
37 #include "vec.h"
38 #include "target.h"
39 #include "bitmap.h"
42 // -------------------------------------------------------------------------- //
43 // Requirement Construction
45 // Facilities for building and manipulating template requirements.
47 // TODO: Simply assigning boolean_type_node to the result type of the
48 // expression seems right for constraints, but in the long-term we might want
49 // to be more flexible (i.e., allow some form of overload resolution?).
51 // Create a new logical node joining the subexpressions a and b.
52 static inline tree
53 join_requirements (tree_code c, tree a, tree b)
55 gcc_assert (a != NULL_TREE && b != NULL_TREE);
56 gcc_assert (c == TRUTH_ANDIF_EXPR || c == TRUTH_ORIF_EXPR);
57 return build_min (c, boolean_type_node, a, b);
60 // Returns the conjunction of two requirements A and B, where A and B are
61 // reduced terms in the constraints language. Note that conjoining a non-null
62 // expression with NULL_TREE is an identity operation. That is, for some
63 // non-null A,
65 // conjoin_requirements(a, NULL_TREE) == a
67 // If both A and B are NULL_TREE, the result is also NULL_TREE.
68 tree
69 conjoin_requirements (tree a, tree b)
71 if (a)
72 return b ? join_requirements (TRUTH_ANDIF_EXPR, a, b) : a;
73 else if (b)
74 return b;
75 else
76 return NULL_TREE;
79 // Transform the list of expressions in the T into a conjunction
80 // of requirements. T must be a TREE_VEC.
81 tree
82 conjoin_requirements (tree t)
84 gcc_assert (TREE_CODE (t) == TREE_VEC);
85 tree r = NULL_TREE;
86 for (int i = 0; i < TREE_VEC_LENGTH (t); ++i)
87 r = conjoin_requirements (r, TREE_VEC_ELT (t, i));
88 return r;
92 // -------------------------------------------------------------------------- //
93 // Constraint Resolution
95 // This facility is used to resolve constraint checks from requirement
96 // expressions. A constraint check is a call to a function template, declared
97 // concept.
99 // The result of resolution is a pair (a list node) whose value is the
100 // matched declaration, and whose purpose contains the coerced template
101 // arguments that can be substituted into the call.
103 // Given an overload set, try to find a unique definition that can be
104 // instantiated by the template arguments.
106 // This function is not called for arbitrary call expressions. In particular,
107 // the call expression must be written with explicit template arguments
108 // and no function arguments. For example:
110 // f<T, U>()
112 // The overload set will contain only template declarations.
114 // If a single definition is found, this returns a list node whose VALUE
115 // is the constraint function (not the template), and its PURPOSE is
116 // the complete set of arguments substituted into the parameter list.
117 static tree
118 resolve_constraint_check (tree ovl, tree args)
120 tree cands = NULL_TREE;
121 for (tree p = ovl; p != NULL_TREE; p = OVL_NEXT (p))
123 // Get the next template overload.
124 tree tmpl = OVL_CURRENT (p);
125 if (TREE_CODE (tmpl) != TEMPLATE_DECL)
126 continue;
128 // Don't try to deduce checks for non-concept-like. We often
129 // end up trying to resolve constraints in functional casts
130 // as part of a post-fix expression. We can save time and
131 // headaches by not instantiating those declarations.
133 // NOTE: This masks a potential error, caused by instantiating
134 // non-deduced contexts using placeholder arguments.
135 tree fn = DECL_TEMPLATE_RESULT (tmpl);
136 if (DECL_ARGUMENTS (fn))
137 continue;
138 if (!DECL_DECLARED_CONCEPT_P (fn))
139 continue;
141 // Remember the candidate if we can deduce a substitution.
142 ++processing_template_decl;
143 tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl));
144 if (tree subst = coerce_template_parms (parms, args, tmpl))
145 if (subst != error_mark_node)
146 cands = tree_cons (subst, fn, cands);
147 --processing_template_decl;
150 // If we didn't find a unique candidate, then this is
151 // not a constraint check.
152 if (!cands || TREE_CHAIN (cands))
153 return NULL_TREE;
155 // Constraints must be declared concepts.
156 tree decl = TREE_VALUE (cands);
157 if (!DECL_DECLARED_CONCEPT_P (decl))
158 return NULL_TREE;
160 // Concept declarations must have a corresponding definition.
162 // TODO: This should be part of the up-front checking for
163 // a concept declaration.
164 if (!DECL_SAVED_TREE (decl))
166 error_at (DECL_SOURCE_LOCATION (decl),
167 "concept %q#D has no definition", decl);
168 return NULL;
171 return cands;
174 // Determine if the the call expression CALL is a constraint check, and
175 // return the concept declaration and arguments being checked. If CALL
176 // does not denote a constraint check, return NULL.
177 tree
178 resolve_constraint_check (tree call)
180 gcc_assert (TREE_CODE (call) == CALL_EXPR);
182 // A constraint check must be only be a template-id expression.
183 tree target = CALL_EXPR_FN (call);
184 if (TREE_CODE (target) != TEMPLATE_ID_EXPR)
185 return NULL_TREE;
187 // Get the overload set and template arguments and try to
188 // resolve the target.
189 tree ovl = TREE_OPERAND (target, 0);
190 tree args = TREE_OPERAND (target, 1);
191 return resolve_constraint_check (ovl, args);
195 // -------------------------------------------------------------------------- //
196 // Requirement Reduction
198 // Reduces a template requirement to a logical formula written in terms of
199 // atomic propositions, returing the new expression. If the expression cannot
200 // be reduced, a NULL_TREE is returned, indicating failure to reduce the
201 // original requirment.
203 namespace {
205 // Helper functions
206 static tree reduce_node (tree);
207 static tree reduce_expr (tree);
208 static tree reduce_stmt (tree);
209 static tree reduce_decl (tree);
210 static tree reduce_misc (tree);
212 static tree reduce_logical (tree);
213 static tree reduce_call (tree);
214 static tree reduce_requires (tree);
215 static tree reduce_expr_req (tree);
216 static tree reduce_type_req (tree);
217 static tree reduce_nested_req (tree);
218 static tree reduce_template_id (tree);
219 static tree reduce_stmt_list (tree);
221 // Reduce the requirement T into a logical formula written in terms of
222 // atomic propositions.
223 tree
224 reduce_node (tree t)
226 switch (TREE_CODE_CLASS (TREE_CODE (t)))
228 case tcc_unary:
229 case tcc_binary:
230 case tcc_expression:
231 case tcc_vl_exp:
232 return reduce_expr (t);
234 case tcc_statement:
235 return reduce_stmt (t);
237 case tcc_declaration:
238 return reduce_decl (t);
240 case tcc_exceptional:
241 return reduce_misc (t);
243 // These kinds of expressions are atomic.
244 case tcc_constant:
245 case tcc_reference:
246 case tcc_comparison:
247 return t;
249 default:
250 gcc_unreachable ();
252 return NULL_TREE;
255 // Reduction rules for the expression node T.
256 tree
257 reduce_expr (tree t)
259 switch (TREE_CODE (t))
261 case TRUTH_ANDIF_EXPR:
262 case TRUTH_ORIF_EXPR:
263 return reduce_logical (t);
265 case CALL_EXPR:
266 return reduce_call (t);
268 case REQUIRES_EXPR:
269 return reduce_requires (t);
271 case EXPR_REQ:
272 return reduce_expr_req (t);
274 case TYPE_REQ:
275 return reduce_type_req (t);
277 case NESTED_REQ:
278 return reduce_nested_req (t);
280 case TEMPLATE_ID_EXPR:
281 return reduce_template_id (t);
283 case CAST_EXPR:
284 return reduce_node (TREE_VALUE (TREE_OPERAND (t, 0)));
286 case BIND_EXPR:
287 return reduce_node (BIND_EXPR_BODY (t));
289 // Do not recurse.
290 case TAG_DEFN:
291 return NULL_TREE;
293 // Everything else is atomic.
294 default:
295 return t;
300 // Reduction rules for the statement T.
301 tree
302 reduce_stmt (tree t)
304 switch (TREE_CODE (t))
306 // Reduce the returned expression.
307 case RETURN_EXPR:
308 return reduce_node (TREE_OPERAND (t, 0));
310 // These statements do not introduce propositions
311 // in the constraints language. Do not recurse.
312 case DECL_EXPR:
313 case USING_STMT:
314 return NULL_TREE;
316 default:
317 gcc_unreachable ();
319 return NULL_TREE;
322 // Reduction rules for the declaration T.
323 tree
324 reduce_decl (tree t)
326 switch (TREE_CODE (t))
328 // References to var decls are atomic.
329 case VAR_DECL:
330 return t;
332 default:
333 gcc_unreachable ();
335 return NULL_TREE;
338 // Reduction rules for the node T.
339 tree
340 reduce_misc (tree t)
342 switch (TREE_CODE (t))
344 // Errors and traits are atomic.
345 case ERROR_MARK:
346 case TRAIT_EXPR:
347 return t;
349 case STATEMENT_LIST:
350 return reduce_stmt_list (t);
352 default:
353 gcc_unreachable ();
355 return NULL_TREE;
358 // Reduction rules for the binary logical expression T (&& and ||).
360 // Generate a new expression from the reduced operands. If either operand
361 // cannot be reduced, then the resulting expression is null.
362 tree
363 reduce_logical (tree t)
365 tree l = reduce_expr (TREE_OPERAND (t, 0));
366 tree r = reduce_expr (TREE_OPERAND (t, 1));
367 if (l && r)
369 t = copy_node (t);
370 TREE_OPERAND (t, 0) = l;
371 TREE_OPERAND (t, 1) = r;
372 return t;
374 else
375 return NULL_TREE;
378 // Reduction rules for the call expression T.
380 // If T is a call to a constraint instantiate its definition and
381 // recursively reduce its returned expression.
382 tree
383 reduce_call (tree t)
385 // Is the function call actually a constraint check?
386 tree check = resolve_constraint_check (t);
387 if (!check)
388 return t;
390 tree fn = TREE_VALUE (check);
391 tree args = TREE_PURPOSE (check);
393 // Reduce the body of the function into the constriants language.
394 tree body = reduce_requirements (DECL_SAVED_TREE (fn));
395 if (!body)
397 error ("could not inline requirements from %qD", fn);
398 return error_mark_node;
401 // Instantiate the reduced results using the deduced args.
402 tree result = instantiate_requirements (body, args);
403 if (result == error_mark_node)
405 error ("could not instantiate requirements from %qD", fn);
406 return error_mark_node;
408 return result;
411 // Reduction rules for the template-id T.
413 // It turns out that we often get requirements being written like this:
415 // template<typename T>
416 // requires Foo<T>
417 // void f()
419 // Where Foo<T> should actually be written as Foo<T>(). Generate an
420 // error and suggest the improved writing.
421 tree
422 reduce_template_id (tree t)
424 vec<tree, va_gc>* args = NULL;
425 tree c = finish_call_expr (t, &args, true, false, 0);
426 error_at (EXPR_LOCATION (t), "invalid requirement");
427 inform (EXPR_LOCATION (t), "did you mean %qE", c);
428 return c;
432 // Reduce an expression requirement as a conjunction of its
433 // individual constraints.
434 tree
435 reduce_expr_req (tree t)
437 tree r = NULL_TREE;
438 for (tree l = TREE_OPERAND (t, 0); l; l = TREE_CHAIN (l))
439 r = conjoin_requirements (r, reduce_expr (TREE_VALUE (l)));
440 return r;
443 // Reduce a type requirement by returing its underlying
444 // constraint.
445 tree
446 reduce_type_req (tree t)
448 return TREE_OPERAND (t, 0);
451 // Reduce a nested requireemnt by returing its only operand.
452 tree
453 reduce_nested_req (tree t)
455 return TREE_OPERAND (t, 0);
458 // Reduce a requires expr by reducing each requirement in turn,
459 // rewriting the list of requirements so that we end up with a
460 // list of expressions, some of which may be conjunctions.
461 tree
462 reduce_requires (tree t)
464 for (tree l = TREE_OPERAND (t, 1); l; l = TREE_CHAIN (l))
465 TREE_VALUE (l) = reduce_expr (TREE_VALUE (l));
466 return t;
469 // Reduction rules for the statement list STMTS.
471 // Recursively reduce each statement in the list, concatenating each
472 // reduced result into a conjunction of requirements.
474 // A constexpr function may include statements other than a return
475 // statement. The primary purpose of these rules is to filter those
476 // non-return statements from the constraints language.
477 tree
478 reduce_stmt_list (tree stmts)
480 tree lhs = NULL_TREE;
481 tree_stmt_iterator i = tsi_start (stmts);
482 while (!tsi_end_p (i))
484 if (tree rhs = reduce_node (tsi_stmt (i)))
485 lhs = conjoin_requirements (lhs, rhs);
486 tsi_next (&i);
488 return lhs;
491 } // end namespace
493 // Reduce the requirement REQS into a logical formula written in terms of
494 // atomic propositions.
495 tree
496 reduce_requirements (tree reqs)
498 return reduce_node (reqs);
501 // -------------------------------------------------------------------------- //
502 // Constraint Semantic Processing
504 // The following functions are called by the parser and substitution rules
505 // to create and evaluate constraint-related nodes.
507 // Create a constraint-info node from the specified requirements.
508 tree
509 make_constraints (tree reqs)
511 // No requirements == no constraints
512 if (!reqs)
513 return NULL_TREE;
515 // Reduce the requirements into a single expression of constraints.
516 tree expr = reduce_requirements (reqs);
517 if (expr == error_mark_node)
518 return error_mark_node;
520 // Decompose those expressions into lists of lists of atomic
521 // propositions.
522 tree assume = decompose_assumptions (expr);
524 // Build the constraint info.
525 tree_constraint_info *cinfo =
526 (tree_constraint_info *)make_node (CONSTRAINT_INFO);
527 cinfo->spelling = reqs;
528 cinfo->requirements = expr;
529 cinfo->assumptions = assume;
530 return (tree)cinfo;
533 // Returns the template constraints of declaration T. If T is not a
534 // template, this return NULL_TREE. Note that T must be non-null.
535 tree
536 get_constraints (tree t)
538 gcc_assert (DECL_P (t));
539 if (TREE_CODE (t) != TEMPLATE_DECL)
541 if (!DECL_TEMPLATE_INFO (t))
542 return NULL_TREE;
543 else
544 return DECL_CONSTRAINTS (DECL_TI_TEMPLATE (t));
546 return DECL_CONSTRAINTS (t);
549 // Returns a conjunction of shorthand requirements for the template
550 // parameter list PARMS. Note that the requirements are stored in
551 // the TYPE of each tree node.
552 tree
553 get_shorthand_requirements (tree parms)
555 tree reqs = NULL_TREE;
556 parms = INNERMOST_TEMPLATE_PARMS (parms);
557 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
559 tree parm = TREE_VEC_ELT (parms, i);
560 reqs = conjoin_requirements(reqs, TREE_TYPE (parm));
562 return reqs;
565 // Finish the template requirement, EXPR, by translating it into
566 // a constraint information record.
567 tree
568 finish_template_requirements (tree expr)
570 if (expr == error_mark_node)
571 return NULL_TREE;
572 else
573 return make_constraints (expr);
576 tree
577 build_requires_expr (tree parms, tree reqs)
579 // Modify the declared parameters by removing their context (so they
580 // don't refer to the enclosing scope), and marking them constant (so
581 // we can actually check constexpr properties).
582 for (tree p = parms; p && !VOID_TYPE_P (TREE_VALUE (p)); p = TREE_CHAIN (p))
584 tree parm = TREE_VALUE (p);
585 DECL_CONTEXT (parm) = NULL_TREE;
586 TREE_CONSTANT (parm) = true;
589 // Build the node.
590 tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs);
591 TREE_SIDE_EFFECTS (r) = false;
592 TREE_CONSTANT (r) = true;
593 return r;
596 // Evaluate an instantiatd requires expr, returning the truth node
597 // only when all sub-requirements have evaluated to true.
598 tree
599 eval_requires_expr (tree reqs)
601 for (tree t = reqs ; t; t = TREE_CHAIN (t)) {
602 tree r = TREE_VALUE (t);
603 r = fold_non_dependent_expr (r);
604 r = maybe_constant_value (r);
605 if (r != boolean_true_node)
606 return boolean_false_node;
608 return boolean_true_node;
611 // Finish a requires expression, returning a node wrapping the parameters,
612 // PARMS, and the list of requirements REQS.
613 tree
614 finish_requires_expr (tree parms, tree reqs)
616 if (processing_template_decl)
617 return build_requires_expr (parms, reqs);
618 else
619 return eval_requires_expr (reqs);
622 // Construct a unary expression that evaluates properties of the
623 // expression or type T, and has a boolean result type.
624 static inline tree
625 build_check_expr (tree_code c, tree t)
627 tree r = build_min (c, boolean_type_node, t);
628 TREE_SIDE_EFFECTS (r) = false;
629 TREE_READONLY (r) = true;
630 TREE_CONSTANT (r) = true;
631 return r;
634 // Finish a syntax requirement, constructing a list embodying a sequence
635 // of checks for the validity of EXPR and TYPE, the convertibility of
636 // EXPR to TYPE, and the expression properties specified in SPECS.
637 tree
638 finish_expr_requirement (tree expr, tree type, tree specs)
640 gcc_assert (processing_template_decl);
642 // Build a list of checks, starting with the valid expression.
643 tree result = tree_cons (NULL_TREE, finish_validexpr_expr (expr), NULL_TREE);
645 // If a type requirement was provided, build the result type checks.
646 if (type)
648 // If the type is dependent, ensure that it can be validly
649 // instantiated.
651 // NOTE: We can also disregard checks that result in the template
652 // parameter.
653 if (dependent_type_p (type))
655 tree treq = finish_type_requirement (type);
656 result = tree_cons (NULL_TREE, treq, result);
659 // Ensure that the result of the expression can be converted to
660 // the result type.
661 tree decl_type = finish_decltype_type (expr, false, tf_none);
662 tree creq = finish_trait_expr (CPTK_IS_CONVERTIBLE_TO, decl_type, type);
663 result = tree_cons (NULL_TREE, creq, result);
666 // If constraint specifiers are present, make them part of the
667 // list of constraints.
668 if (specs)
670 TREE_CHAIN (tree_last (specs)) = result;
671 result = specs;
674 // Finally, construct the syntactic requirement.
675 return build_check_expr (EXPR_REQ, nreverse (result));
678 // Finish a simple syntax requirement, returning a node representing
679 // a check that EXPR is a valid expression.
680 tree
681 finish_expr_requirement (tree expr)
683 gcc_assert (processing_template_decl);
684 tree req = finish_validexpr_expr (expr);
685 tree reqs = tree_cons (NULL_TREE, req, NULL_TREE);
686 return build_check_expr (EXPR_REQ, reqs);
689 // Finish a type requirement, returning a node representing a check
690 // that TYPE will result in a valid type when instantiated.
691 tree
692 finish_type_requirement (tree type)
694 gcc_assert (processing_template_decl);
695 tree req = finish_validtype_expr (type);
696 return build_check_expr (TYPE_REQ, req);
699 tree
700 finish_nested_requirement (tree expr)
702 gcc_assert (processing_template_decl);
703 return build_check_expr (NESTED_REQ, expr);
706 // Finish a constexpr requirement, returning a node representing a
707 // check that EXPR, when instantiated, may be evaluated at compile time.
708 tree
709 finish_constexpr_requirement (tree expr)
711 gcc_assert (processing_template_decl);
712 return finish_constexpr_expr (expr);
715 // Finish the noexcept requirement by constructing a noexcept
716 // expression evaluating EXPR.
717 tree
718 finish_noexcept_requirement (tree expr)
720 gcc_assert (processing_template_decl);
721 return finish_noexcept_expr (expr, tf_none);
724 // Returns the true or false node depending on the truth value of B.
725 static inline tree
726 truth_node (bool b)
728 return b ? boolean_true_node : boolean_false_node;
731 // Returns a finished validexpr-expr. Returns the true or false node
732 // depending on whether EXPR denotes a valid expression. This is the case
733 // when the expression has been successfully type checked.
735 // When processing a template declaration, the result is an expression
736 // representing the check.
737 tree
738 finish_validexpr_expr (tree expr)
740 if (processing_template_decl)
741 return build_check_expr (VALIDEXPR_EXPR, expr);
742 return truth_node (expr && expr != error_mark_node);
745 // Returns a finished validtype-expr. Returns the true or false node
746 // depending on whether T denotes a valid type name.
748 // When processing a template declaration, the result is an expression
749 // representing the check.
750 tree
751 finish_validtype_expr (tree type)
753 if (processing_template_decl)
754 return build_check_expr (VALIDTYPE_EXPR, type);
755 return truth_node (type && TYPE_P (type));
758 // Returns a finished constexpr-expr. Returns the true or false node
759 // depending on whether the expression T may be evaluated at compile
760 // time.
762 // When processing a template declaration, the result is an expression
763 // representing the check.
764 tree
765 finish_constexpr_expr (tree expr)
767 if (processing_template_decl)
768 return build_check_expr (CONSTEXPR_EXPR, expr);
770 // TODO: Actually check that the expression can be constexpr
771 // evaluatd.
773 // return truth_node (potential_constant_expression (expr));
774 sorry ("constexpr requirement");
775 return NULL_TREE;
778 // Check that a constrained friend declaration function declaration,
779 // FN, is admissable. This is the case only when the declaration depends
780 // on template parameters and does not declare a specialization.
781 void
782 check_constrained_friend (tree fn, tree reqs)
784 if (fn == error_mark_node)
785 return;
786 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
788 // If there are not constraints, this cannot be an error.
789 if (!reqs)
790 return;
792 // Constrained friend functions that don't depend on template
793 // arguments are effectively meaningless.
794 tree parms = DECL_ARGUMENTS (fn);
795 tree result = TREE_TYPE (TREE_TYPE (fn));
796 if (!(parms && uses_template_parms (parms)) && !uses_template_parms (result))
798 error ("constrained friend does not depend on template parameters");
799 return;
803 // Given an overload set, OVL, and a template argument or placeholder, ARG,
804 // synthesize a call expression that resolves to a concept check of
805 // the expression the form OVL<ARG>().
806 tree
807 build_concept_check (tree ovl, tree arg)
809 // Build a template-id that acts as the call target using OVL as
810 // the template and ARG as the only explicit argument.
811 tree targs = make_tree_vec (1);
812 TREE_VEC_ELT (targs, 0) = arg;
813 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, 1);
814 tree id = lookup_template_function (ovl, targs);
816 // Build a new call expression, but don't actually generate a new
817 // function call. We just want the tree, not the semantics.
818 ++processing_template_decl;
819 vec<tree, va_gc> *fargs = make_tree_vector();
820 tree call = finish_call_expr (id, &fargs, false, false, tf_none);
821 --processing_template_decl;
823 return call;
826 // Returns a TYPE_DECL that contains sufficient information to build
827 // a template parameter of the same kind as PROTO and constrained
828 // by the concept declaration FN. PROTO is saved as the initializer of
829 // the new type decl, and the constraining function is saved in
830 // DECL_SIZE_UNIT.
831 tree
832 describe_template_parm (tree proto, tree fn)
834 tree name = DECL_NAME (fn);
835 tree type = TREE_TYPE (proto);
836 tree decl = build_decl (input_location, TYPE_DECL, name, type);
837 DECL_INITIAL (decl) = proto; // Describing parameter
838 DECL_SIZE_UNIT (decl) = fn; // Constraining function declaration
839 return decl;
842 // If the result is a TYPE_DECL, its DECL_NAME is the name of the
843 // concept (without arguments), its TREE_TYPE refers to the type of the
844 // first template parameter of concept definition (the prototype parameter),
845 // its DECL_INITIAL is the declaration of the prototype parameter, and
846 // its DECL_SIZE_UNIT is the constraining concept declaration.
848 // TODO: A variable template may refer to a concept. The concept-name
849 // could introduce a constrained placeholder type in the terse template
850 // syntax.
851 tree
852 finish_concept_name (tree decl)
854 gcc_assert (TREE_CODE (decl) == OVERLOAD);
856 // Try to build a call expression that evaluates the concept. This
857 // can fail if the overload set refers only to non-templates.
858 tree call = build_concept_check (decl, build_nt(PLACEHOLDER_EXPR));
859 if (call == error_mark_node)
860 return NULL_TREE;
862 // Resolve the constraint check to deduce the declared parameter.
863 tree check = resolve_constraint_check (call);
864 if (!check)
865 return NULL_TREE;
867 // Get function and argument from the resolved check expression. If
868 // the argument was a pack expansion, then get the first element
869 // of that pack.
870 tree fn = TREE_VALUE (check);
871 tree arg = TREE_VEC_ELT (TREE_PURPOSE (check), 0);
872 if (ARGUMENT_PACK_P (arg))
873 arg = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0);
875 // Get the protyping parameter bound to the placeholder.
876 tree proto = TREE_TYPE (arg);
878 // How we process the constrained declaration depends on the scope.
879 // In template scope, we return a "description" that will later be
880 // transformed into a real template parameter by process_template_parm.
881 if (template_parm_scope_p ())
882 return describe_template_parm (proto, fn);
884 // NOTE: We may need to be smarter about this since there are lots of
885 // places outside of a template parameter scope we'll want to use
886 // concept names (function arguments, function return types, result
887 // constraints, variable declarations, etc.).
889 return NULL_TREE;
892 // Create a requirement expression for the given DECL that evaluates the
893 // requirements specified by CONSTR, a TYPE_DECL that contains all the
894 // information necessary to build the requirements (see finish_concept_name
895 // for the layout of that TYPE_DECL).
897 // Note that the constraints are neither reduced nor decomposed. That is
898 // done only after the requires clause has been parsed (or not).
899 tree
900 finish_shorthand_requirement (tree decl, tree constr)
902 // No requirements means no constraints.
903 if (!constr)
904 return NULL_TREE;
906 tree proto = DECL_INITIAL (constr); // The prototype declaration
907 tree con = DECL_SIZE_UNIT (constr); // The concept declaration
909 // If the parameter declaration is variadic, but the concept is not
910 // then we need to apply the concept to every element in the pack.
911 bool is_proto_pack = template_parameter_pack_p (proto);
912 bool is_decl_pack = template_parameter_pack_p (decl);
913 bool apply_to_all_p = is_decl_pack && !is_proto_pack;
915 // Get the argument and overload used for the requirement. Adjust
916 // if we're going to expand later.
917 tree arg = template_parm_to_arg (build_tree_list (NULL_TREE, decl));
918 if (apply_to_all_p)
919 arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
921 // Build the concept check. If it the constraint needs to be applied
922 // to all elements of the parameter pack, then expand make the constraint
923 // an expansion.
924 tree ovl = build_overload (DECL_TI_TEMPLATE (con), NULL_TREE);
925 tree check = build_concept_check (ovl, arg);
926 if (apply_to_all_p)
928 check = make_pack_expansion (check);
930 // Set the type to indicate that this expansion will get special
931 // treatment during instantiation.
933 // TODO: Maybe this should be a different kind of node... one that
934 // has all the same properties as a pack expansion, but has a definite
935 // expansion when instantiated as part of an expression.
937 // As of now, this is a hack.
938 TREE_TYPE (check) = boolean_type_node;
941 return check;
944 // -------------------------------------------------------------------------- //
945 // Substitution Rules
947 // The following functions implement substitution rules for constraints.
949 namespace {
950 // In an unevaluated context, the substitution of parm decls are not
951 // properly chained during substitution. Do that here.
952 tree
953 fix_local_parms (tree sparms)
955 if (!sparms)
956 return sparms;
958 tree p = TREE_CHAIN (sparms);
959 tree q = sparms;
960 while (p && TREE_VALUE (p) != void_type_node)
962 DECL_CHAIN (TREE_VALUE (q)) = TREE_VALUE (p);
963 q = p;
964 p = TREE_CHAIN (p);
966 return sparms;
969 // Register local specializations for each of tparm and the corresponding
970 // sparm. This is a helper function for tsubst_requires_expr.
971 void
972 declare_local_parms (tree tparms, tree sparms)
974 tree s = TREE_VALUE (sparms);
975 for (tree p = tparms; p && !VOID_TYPE_P (TREE_VALUE (p)); p = TREE_CHAIN (p))
977 tree t = TREE_VALUE (p);
978 if (DECL_PACK_P (t))
980 tree pack = extract_fnparm_pack (t, &s);
981 register_local_specialization (pack, t);
983 else
985 register_local_specialization (s, t);
986 s = TREE_CHAIN (s);
991 // Substitute ARGS into the parameter list T, producing a sequence of
992 // local parameters (variables) in the current scope.
993 tree
994 tsubst_local_parms (tree t,
995 tree args,
996 tsubst_flags_t complain,
997 tree in_decl)
999 tree r = fix_local_parms (tsubst (t, args, complain, in_decl));
1000 if (r == error_mark_node)
1001 return error_mark_node;
1003 // Register the instantiated args as local parameters.
1004 if (t)
1005 declare_local_parms (t, r);
1007 return r;
1010 // Substitute ARGS into the requirement body (list of requirements), T.
1011 tree
1012 tsubst_requirement_body (tree t, tree args, tree in_decl)
1014 cp_unevaluated guard;
1015 tree r = NULL_TREE;
1016 while (t)
1018 // If any substitutions fail, then this is equivalent to
1019 // returning false.
1020 tree e = tsubst_expr (TREE_VALUE (t), args, tf_none, in_decl, false);
1021 if (e == error_mark_node)
1022 e = boolean_false_node;
1023 r = tree_cons (NULL_TREE, e, r);
1024 t = TREE_CHAIN (t);
1026 return r;
1028 } // namespace
1030 // Substitute ARGS into the requires expression T.
1031 tree
1032 tsubst_requires_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1034 local_specialization_stack stack;
1035 tree p = tsubst_local_parms (TREE_OPERAND (t, 0), args, complain, in_decl);
1036 tree r = tsubst_requirement_body (TREE_OPERAND (t, 1), args, in_decl);
1037 return finish_requires_expr (p, r);
1040 // Substitute ARGS into the valid-expr expression T.
1041 tree
1042 tsubst_validexpr_expr (tree t, tree args, tree in_decl)
1044 tree r = tsubst_expr (TREE_OPERAND (t, 0), args, tf_none, in_decl, false);
1045 return finish_validexpr_expr (r);
1048 // Substitute ARGS into the valid-type expression T.
1049 tree
1050 tsubst_validtype_expr (tree t, tree args, tree in_decl)
1052 tree r = tsubst (TREE_OPERAND (t, 0), args, tf_none, in_decl);
1053 return finish_validtype_expr (r);
1056 // Substitute ARGS into the constexpr expression T.
1057 tree
1058 tsubst_constexpr_expr (tree t, tree args, tree in_decl)
1060 tree r = tsubst_expr (TREE_OPERAND (t, 0), args, tf_none, in_decl, false);
1061 return finish_constexpr_expr (r);
1064 // Substitute ARGS into the expr requirement T. Note that a requirement
1065 // node is instantiated from a non-reduced context (e.g., static_assert).
1066 tree
1067 tsubst_expr_req (tree t, tree args, tree in_decl)
1069 tree r = NULL_TREE;
1070 for (tree l = TREE_OPERAND (t, 0); l; l = TREE_CHAIN (l))
1072 tree e = tsubst_expr (TREE_VALUE (l), args, tf_none, in_decl, false);
1073 r = conjoin_requirements (r, e);
1075 return r;
1078 // Substitute ARGS into the type requirement T. Note that a requirement
1079 // node is instantiated from a non-reduced context (e.g., static_assert).
1080 tree
1081 tsubst_type_req (tree t, tree args, tree in_decl)
1083 return tsubst_expr (TREE_OPERAND (t, 0), args, tf_none, in_decl, false);
1086 // Substitute ARGS into the nested requirement T. Note that a requirement
1087 // node is instantiated from a non-reduced context (e.g., static_assert).
1088 tree
1089 tsubst_nested_req (tree t, tree args, tree in_decl)
1091 return tsubst_expr (TREE_OPERAND (t, 0), args, tf_none, in_decl, false);
1094 // Substitute the template arguments ARGS into the requirement
1095 // expression REQS. Errors resulting from substitution are not
1096 // diagnosed.
1097 tree
1098 instantiate_requirements (tree reqs, tree args)
1100 return tsubst_expr (reqs, args, tf_none, NULL_TREE, false);
1103 // -------------------------------------------------------------------------- //
1104 // Constraint Satisfaction
1106 // The following functions are responsible for the instantiation and
1107 // evaluation of constraints.
1109 namespace {
1110 // Returns true if the requirements expression REQS is satisfied
1111 // and false otherwise. The requirements are checked by simply
1112 // evaluating REQS as a constant expression.
1113 static inline bool
1114 check_requirements (tree reqs)
1116 // Reduce any remaining TRAIT_EXPR nodes before evaluating.
1117 reqs = fold_non_dependent_expr (reqs);
1119 // Requirements are satisfied when REQS evaluates to true.
1120 return cxx_constant_value (reqs) == boolean_true_node;
1123 // Returns true if the requirements expression REQS is satisfied
1124 // and false otherwise. The requirements are checked by first
1125 // instantiating REQS and then evaluating it as a constant expression.
1126 static inline bool
1127 check_requirements (tree reqs, tree args)
1129 // If any arguments are dependent, then we can't check the
1130 // requirements. Just return true.
1131 if (uses_template_parms (args))
1132 return true;
1134 // Instantiate and evaluate the requirements.
1135 reqs = instantiate_requirements (reqs, args);
1136 if (reqs == error_mark_node)
1137 return false;
1138 return check_requirements (reqs);
1140 } // namespace
1142 // Check the instantiated declaration constraints.
1143 bool
1144 check_constraints (tree cinfo)
1146 // No constraints? Satisfied.
1147 if (!cinfo)
1148 return true;
1149 return check_requirements (CI_REQUIREMENTS (cinfo));
1152 // Check the constraints in CINFO against the given ARGS, returning
1153 // true when the constraints are satisfied and false otherwise.
1154 bool
1155 check_constraints (tree cinfo, tree args)
1157 // No constraints? Satisfied.
1158 if (!cinfo)
1159 return true;
1161 // Dependent arguments? Satisfied. They won't reduce to true or false.
1162 if (uses_template_parms (args))
1163 return true;
1165 return check_requirements (CI_REQUIREMENTS (cinfo), args);
1168 // Check the constraints of the declaration or type T, against
1169 // the specified arguments. Returns true if the constraints are
1170 // satisfied and false otherwise.
1171 bool
1172 check_template_constraints (tree t, tree args)
1174 return check_constraints (DECL_CONSTRAINTS (t), args);
1177 // -------------------------------------------------------------------------- //
1178 // Constraint Relations
1180 // Interfaces for determining equivalency and ordering of constraints.
1182 // Returns true when A and B are equivalent constraints.
1183 bool
1184 equivalent_constraints (tree a, tree b)
1186 if (a == b)
1187 return true;
1188 else
1189 return subsumes (a, b) && subsumes (b, a);
1192 // Returns true if the template declarations A and B have equivalent
1193 // constraints. This is the case when A's constraints subsume B's and
1194 // when B's also constrain A's.
1195 bool
1196 equivalently_constrained (tree a, tree b)
1198 gcc_assert (TREE_CODE (a) == TREE_CODE (b));
1199 return equivalent_constraints (DECL_CONSTRAINTS (a), DECL_CONSTRAINTS (b));
1202 // Returns true when the A contains more atomic properties than B.
1203 bool
1204 more_constraints (tree a, tree b)
1206 return subsumes (a, b);
1209 // Returns true when the template declaration A's constraints subsume
1210 // those of the template declaration B.
1211 bool
1212 more_constrained (tree a, tree b)
1214 gcc_assert (TREE_CODE (a) == TREE_CODE (b));
1215 return more_constraints (DECL_CONSTRAINTS (a), DECL_CONSTRAINTS (b));
1219 // -------------------------------------------------------------------------- //
1220 // Constraint Diagnostics
1222 namespace {
1224 void diagnose_node (location_t, tree, tree);
1226 // Diagnose a constraint failure for type trait expressions.
1227 void
1228 diagnose_trait (location_t loc, tree t, tree args)
1230 if (check_requirements (t, args))
1231 return;
1233 ++processing_template_decl;
1234 tree subst = instantiate_requirements (t, args);
1235 --processing_template_decl;
1237 if (subst == error_mark_node)
1239 inform (input_location, " substitution failure in %qE", t);
1240 return;
1243 tree t1 = TRAIT_EXPR_TYPE1 (subst);
1244 tree t2 = TRAIT_EXPR_TYPE2 (subst);
1245 switch (TRAIT_EXPR_KIND (t))
1247 case CPTK_HAS_NOTHROW_ASSIGN:
1248 inform (loc, " %qT is not nothrow assignable", t1);
1249 break;
1250 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
1251 inform (loc, " %qT is not nothrow constructible", t1);
1252 break;
1253 case CPTK_HAS_NOTHROW_COPY:
1254 inform (loc, " %qT is not nothrow copyable", t1);
1255 break;
1256 case CPTK_HAS_TRIVIAL_ASSIGN:
1257 inform (loc, " %qT is not trivially assignable", t1);
1258 break;
1259 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
1260 inform (loc, " %qT is not trivially constructible", t1);
1261 break;
1262 case CPTK_HAS_TRIVIAL_COPY:
1263 inform (loc, " %qT is not trivially copyable", t1);
1264 break;
1265 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
1266 inform (loc, " %qT is not trivially destructible", t1);
1267 break;
1268 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
1269 inform (loc, " %qT does not have a virtual destructor", t1);
1270 break;
1271 case CPTK_IS_ABSTRACT:
1272 inform (loc, " %qT is not an abstract class", t1);
1273 break;
1274 case CPTK_IS_BASE_OF:
1275 inform (loc, " %qT is not a base of %qT", t1, t2);
1276 break;
1277 case CPTK_IS_CLASS:
1278 inform (loc, " %qT is not a class", t1);
1279 break;
1280 case CPTK_IS_CONVERTIBLE_TO:
1281 inform (loc, " %qT is not convertible to %qT", t1, t2);
1282 break;
1283 case CPTK_IS_EMPTY:
1284 inform (loc, " %qT is not an empty class", t1);
1285 break;
1286 case CPTK_IS_ENUM:
1287 inform (loc, " %qT is not an enum", t1);
1288 break;
1289 case CPTK_IS_FINAL:
1290 inform (loc, " %qT is not a final class", t1);
1291 break;
1292 case CPTK_IS_LITERAL_TYPE:
1293 inform (loc, " %qT is not a literal type", t1);
1294 break;
1295 case CPTK_IS_POD:
1296 inform (loc, " %qT is not a POD type", t1);
1297 break;
1298 case CPTK_IS_POLYMORPHIC:
1299 inform (loc, " %qT is not a polymorphic type", t1);
1300 break;
1301 case CPTK_IS_SAME_AS:
1302 inform (loc, " %qT is not the same as %qT", t1, t2);
1303 break;
1304 case CPTK_IS_STD_LAYOUT:
1305 inform (loc, " %qT is not an standard layout type", t1);
1306 break;
1307 case CPTK_IS_TRIVIAL:
1308 inform (loc, " %qT is not a trivial type", t1);
1309 break;
1310 case CPTK_IS_UNION:
1311 inform (loc, " %qT is not a union", t1);
1312 break;
1313 default:
1314 gcc_unreachable ();
1318 // Diagnose a failed concept check in concept indicated by T, where
1319 // T is the result of resolve_constraint_check. Recursively analyze
1320 // the nested requiremets for details.
1321 void
1322 diagnose_check (location_t loc, tree t, tree args)
1324 tree fn = TREE_VALUE (t);
1325 tree targs = TREE_PURPOSE (t);
1326 tree body = DECL_SAVED_TREE (fn);
1327 if (!body)
1328 return;
1330 inform (loc, " failure in constraint %q#D", DECL_TI_TEMPLATE (fn));
1332 // Perform a mini-reduction on the constraint.
1333 if (TREE_CODE (body) == BIND_EXPR)
1334 body = BIND_EXPR_BODY (body);
1335 if (TREE_CODE (body) == RETURN_EXPR)
1336 body = TREE_OPERAND (body, 0);
1338 // Locally instantiate the body with the call's template args,
1339 // and recursively diagnose.
1340 ++processing_template_decl;
1341 body = instantiate_requirements (body, targs);
1342 --processing_template_decl;
1344 diagnose_node (loc, body, args);
1347 // Diagnose constraint failures from the call expression T.
1348 void
1349 diagnose_call (location_t loc, tree t, tree args)
1351 if (check_requirements (t, args))
1352 return;
1354 // If this is a concept, we're going to recurse.
1355 // If it's just a call, then we can emit a simple message.
1356 if (tree check = resolve_constraint_check (t))
1357 diagnose_check (loc, check, args);
1358 else
1359 inform (loc, " %qE evaluated to false", t);
1362 // Diagnose specific constraint failures.
1363 void
1364 diagnose_requires (location_t loc, tree t, tree args)
1366 if (check_requirements (t, args))
1367 return;
1369 ++processing_template_decl;
1370 tree subst = instantiate_requirements (t, args);
1371 --processing_template_decl;
1373 // Print the header for the requires expression.
1374 tree parms = TREE_OPERAND (subst, 0);
1375 if (!VOID_TYPE_P (TREE_VALUE (parms)))
1376 inform (loc, " requiring syntax with values %Z", TREE_OPERAND (subst, 0));
1378 // Create a new local specialization binding for the arguments.
1379 // This lets us instantiate sub-expressions separately from the
1380 // requires clause.
1381 local_specialization_stack locals;
1382 declare_local_parms (TREE_OPERAND (t, 0), TREE_OPERAND (subst, 0));
1384 // Iterate over the sub-requirements and try instantiating each.
1385 for (tree l = TREE_OPERAND (t, 1); l; l = TREE_CHAIN (l))
1386 diagnose_node (loc, TREE_VALUE (l), args);
1389 static void
1390 diagnose_validexpr (location_t loc, tree t, tree args)
1392 if (check_requirements (t, args))
1393 return;
1394 inform (loc, " %qE is not a valid expression", TREE_OPERAND (t, 0));
1397 static void
1398 diagnose_validtype (location_t loc, tree t, tree args)
1400 if (check_requirements (t, args))
1401 return;
1403 // Substitute into the qualified name.
1404 tree name = TREE_OPERAND (t, 0);
1405 if (tree cxt = TYPE_CONTEXT (name))
1407 tree id = TYPE_IDENTIFIER (name);
1408 cxt = tsubst (cxt, args, tf_none, NULL_TREE);
1409 name = build_qualified_name (NULL_TREE, cxt, id, false);
1410 inform (loc, " %qE does not name a valid type", name);
1412 else
1414 inform (loc, " %qT does not name a valid type", name);
1418 static void
1419 diagnose_constexpr (location_t loc, tree t, tree args)
1421 if (check_requirements (t, args))
1422 return;
1423 inform (loc, " %qE is not a constant expression", TREE_OPERAND (t, 0));
1426 static void
1427 diagnose_noexcept (location_t loc, tree t, tree args)
1429 if (check_requirements (t, args))
1430 return;
1431 inform (loc, " %qE propagates exceptions", TREE_OPERAND (t, 0));
1434 // Diagnose a constraint failure in the expression T.
1435 void
1436 diagnose_other (location_t loc, tree t, tree args)
1438 if (check_requirements (t, args))
1439 return;
1440 inform (loc, " %qE evaluated to false", t);
1443 // Diagnose a constraint failure in the subtree T.
1444 void
1445 diagnose_node (location_t loc, tree t, tree args)
1447 switch (TREE_CODE (t))
1449 case TRUTH_ANDIF_EXPR:
1450 diagnose_node (loc, TREE_OPERAND (t, 0), args);
1451 diagnose_node (loc, TREE_OPERAND (t, 1), args);
1452 break;
1454 case TRUTH_ORIF_EXPR:
1455 // TODO: Design better diagnostics for dijunctions.
1456 diagnose_other (loc, t, args);
1457 break;
1459 case TRAIT_EXPR:
1460 diagnose_trait (loc, t, args);
1461 break;
1463 case CALL_EXPR:
1464 diagnose_call (loc, t, args);
1465 break;
1467 case REQUIRES_EXPR:
1468 diagnose_requires (loc, t, args);
1469 break;
1471 case VALIDEXPR_EXPR:
1472 diagnose_validexpr (loc, t, args);
1473 break;
1475 case VALIDTYPE_EXPR:
1476 diagnose_validtype (loc, t, args);
1477 break;
1479 case CONSTEXPR_EXPR:
1480 diagnose_constexpr (loc, t, args);
1481 break;
1483 case NOEXCEPT_EXPR:
1484 diagnose_noexcept (loc, t, args);
1485 break;
1487 default:
1488 diagnose_other (loc, t, args);
1489 break;
1493 // Diagnose a constraint failure in the requirements expression REQS.
1494 inline void
1495 diagnose_requirements (location_t loc, tree reqs, tree args)
1497 diagnose_node (loc, reqs, args);
1500 // Create a tree node representing the substitution of ARGS into
1501 // the parameters of TMPL. The resulting structure is passed as an
1502 // for diagnosing substitutions.
1503 inline tree
1504 make_subst (tree tmpl, tree args)
1506 tree subst = tree_cons (NULL_TREE, args, NULL_TREE);
1507 TREE_TYPE (subst) = DECL_TEMPLATE_PARMS (tmpl);
1508 return subst;
1511 } // namespace
1513 // Emit diagnostics detailing the failure ARGS to satisfy the constraints
1514 // of the template declaration, TMPL.
1515 void
1516 diagnose_constraints (location_t loc, tree tmpl, tree args)
1518 inform (loc, " constraints not satisfied %S", make_subst (tmpl, args));
1520 // Diagnose the constraints by recursively decomposing and
1521 // evaluating the template requirements.
1522 tree reqs = CI_SPELLING (DECL_CONSTRAINTS (tmpl));
1523 diagnose_requirements (loc, reqs, args);