2014-09-01 Andrew Sutton <andrew.n.sutton@gmail.com>
[official-gcc.git] / gcc / cp / constraint.cc
blobc09d212354759521ccf070aa6e7c9caa307b7d1f
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"
41 #include "hash-map.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_constraints(a, NULL_TREE) == a
68 // If both A and B are NULL_TREE, the result is also NULL_TREE.
69 tree
70 conjoin_constraints (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_constraints (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_constraints (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 or template-id expression to a concept, EXPR,
202 // possibly including a placeholder argument, deduce the concept being checked
203 // and the prototype paraemter. Returns true if the constraint and prototype
204 // can be deduced and false otherwise. Note that the CHECK and PROTO arguments
205 // are set to NULL_TREE if this returns false.
206 bool
207 deduce_constrained_parameter (tree expr, tree& check, tree& proto)
209 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
211 // Get the check and prototype parameter from the variable template.
212 tree decl = TREE_OPERAND (expr, 0);
213 tree parms = DECL_TEMPLATE_PARMS (decl);
215 check = DECL_TEMPLATE_RESULT (decl);
216 proto = TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), 0));
217 return true;
219 else if (TREE_CODE (expr) == CALL_EXPR)
221 // Resolve the constraint check to deduce the prototype parameter.
222 if (tree info = resolve_constraint_check (expr))
224 // Get function and argument from the resolved check expression and
225 // the prototype parameter. Note that if the first argument was a
226 // pack, we need to extract the first element ot get the prototype.
227 check = TREE_VALUE (info);
228 tree arg = TREE_VEC_ELT (TREE_PURPOSE (info), 0);
229 if (ARGUMENT_PACK_P (arg))
230 arg = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0);
231 proto = TREE_TYPE (arg);
232 return true;
234 check = proto = NULL_TREE;
235 return false;
237 else
238 gcc_unreachable ();
241 // -------------------------------------------------------------------------- //
242 // Requirement Reduction
244 // Reduces a template requirement to a logical formula written in terms of
245 // atomic propositions, returing the new expression. If the expression cannot
246 // be reduced, a NULL_TREE is returned, indicating failure to reduce the
247 // original requirment.
249 namespace {
251 // Helper functions
252 tree normalize_constraints (tree);
253 tree normalize_node (tree);
254 tree normalize_expr (tree);
255 tree normalize_stmt (tree);
256 tree normalize_decl (tree);
257 tree normalize_misc (tree);
258 tree normalize_logical (tree);
259 tree normalize_call (tree);
260 tree normalize_requires (tree);
261 tree normalize_expr_req (tree);
262 tree normalize_type_req (tree);
263 tree normalize_nested_req (tree);
264 tree normalize_var (tree);
265 tree normalize_template_id (tree);
266 tree normalize_stmt_list (tree);
267 tree normalize_cast (tree);
268 tree normalize_atom (tree);
270 // Reduce the requirement T into a logical formula written in terms of
271 // atomic propositions.
272 tree
273 normalize_node (tree t)
275 switch (TREE_CODE_CLASS (TREE_CODE (t)))
277 case tcc_unary:
278 case tcc_binary:
279 case tcc_expression:
280 case tcc_vl_exp:
281 return normalize_expr (t);
283 case tcc_statement:
284 return normalize_stmt (t);
286 case tcc_declaration:
287 return normalize_decl (t);
289 case tcc_exceptional:
290 return normalize_misc (t);
292 // These kinds of expressions are atomic.
293 case tcc_constant:
294 case tcc_reference:
295 case tcc_comparison:
296 return t;
298 default:
299 gcc_unreachable ();
301 return NULL_TREE;
304 // Reduction rules for the expression node T.
305 tree
306 normalize_expr (tree t)
308 switch (TREE_CODE (t))
310 case TRUTH_ANDIF_EXPR:
311 case TRUTH_ORIF_EXPR:
312 return normalize_logical (t);
314 case CALL_EXPR:
315 return normalize_call (t);
317 case REQUIRES_EXPR:
318 return normalize_requires (t);
320 case EXPR_REQ:
321 return normalize_expr_req (t);
323 case TYPE_REQ:
324 return normalize_type_req (t);
326 case NESTED_REQ:
327 return normalize_nested_req (t);
329 case TEMPLATE_ID_EXPR:
330 return normalize_template_id (t);
332 case CAST_EXPR:
333 return normalize_cast (t);
335 case BIND_EXPR:
336 return normalize_node (BIND_EXPR_BODY (t));
338 // Do not recurse.
339 case TAG_DEFN:
340 return NULL_TREE;
342 // Everything else is atomic.
343 default:
344 return normalize_atom (t);
349 // Reduction rules for the statement T.
350 tree
351 normalize_stmt (tree t)
353 switch (TREE_CODE (t))
355 // Reduce the returned expression.
356 case RETURN_EXPR:
357 return normalize_node (TREE_OPERAND (t, 0));
359 // These statements do not introduce propositions
360 // in the constraints language. Do not recurse.
361 case DECL_EXPR:
362 case USING_STMT:
363 return NULL_TREE;
365 default:
366 gcc_unreachable ();
368 return NULL_TREE;
371 // Reduction rules for the declaration T.
372 tree
373 normalize_decl (tree t)
375 switch (TREE_CODE (t))
377 // References to var decls are atomic.
378 case VAR_DECL:
379 return t;
381 default:
382 gcc_unreachable ();
384 return NULL_TREE;
387 // Reduction rules for the node T.
388 tree
389 normalize_misc (tree t)
391 switch (TREE_CODE (t))
393 // All of these are atomic.
394 case ERROR_MARK:
395 case TRAIT_EXPR:
396 case CONSTRUCTOR:
397 return t;
399 case STATEMENT_LIST:
400 return normalize_stmt_list (t);
402 default:
403 gcc_unreachable ();
405 return NULL_TREE;
408 // Check that the logical expression is not a user-defined operator.
409 bool
410 check_logical (tree t)
412 // We can't do much for type dependent expressions.
413 if (type_dependent_expression_p (t) || value_dependent_expression_p (t))
414 return true;
416 // Resolve the logical operator. Note that template processing is
417 // disabled so we get the actual call or target expression back.
418 // not_processing_template_sentinel sentinel;
419 tree arg1 = TREE_OPERAND (t, 0);
420 tree arg2 = TREE_OPERAND (t, 1);
422 tree ovl = NULL_TREE;
423 tree expr = build_new_op (input_location, TREE_CODE (t), LOOKUP_NORMAL,
424 arg1, arg2, /*arg3*/NULL_TREE,
425 &ovl, tf_none);
426 if (TREE_CODE (expr) != TREE_CODE (t))
428 error ("user-defined operator %qs in constraint %qE",
429 operator_name_info[TREE_CODE (t)].name, t);
431 return false;
433 return true;
436 // Reduction rules for the binary logical expression T (&& and ||).
438 // Generate a new expression from the reduced operands. If either operand
439 // cannot be reduced, then the resulting expression is null.
440 tree
441 normalize_logical (tree t)
443 if (!check_logical (t))
444 return NULL_TREE;
446 tree l = normalize_expr (TREE_OPERAND (t, 0));
447 tree r = normalize_expr (TREE_OPERAND (t, 1));
448 if (l && r)
450 tree result = copy_node (t);
451 SET_EXPR_LOCATION (result, EXPR_LOCATION (t));
452 TREE_OPERAND (result, 0) = l;
453 TREE_OPERAND (result, 1) = r;
454 return result;
456 else
457 return NULL_TREE;
460 // Do a cursory investigation of the target in the call expression
461 // with the aim of early diagnosis of ill-formed constraints.
462 inline bool
463 check_call (tree t)
465 tree target = CALL_EXPR_FN (t);
466 if (TREE_CODE (target) != TEMPLATE_ID_EXPR)
467 return true;
468 tree tmpl = TREE_OPERAND (target, 0);
469 if (TREE_CODE (tmpl) != TEMPLATE_DECL)
470 return true;
471 tree decl = DECL_TEMPLATE_RESULT (tmpl);
472 if (TREE_CODE (decl) == VAR_DECL && DECL_DECLARED_CONCEPT_P (decl))
474 error ("invalid constraint %qE", t);
475 inform (input_location, "did you mean %qE", target);
476 return false;
478 return true;
481 // Reduction rules for the call expression T.
483 // If T is a call to a constraint instantiate its definition and
484 // recursively reduce its returned expression.
485 tree
486 normalize_call (tree t)
488 if (!check_call (t))
489 return NULL_TREE;
491 // Is the function call actually a constraint check? If not, then it's
492 // an atom, and needs to be treated as such.
493 tree check = resolve_constraint_check (t);
494 if (!check)
495 return normalize_atom (t);
497 tree fn = TREE_VALUE (check);
498 tree args = TREE_PURPOSE (check);
500 // Reduce the body of the function into the constriants language.
501 tree body = normalize_constraints (DECL_SAVED_TREE (fn));
502 if (!body)
503 return error_mark_node;
505 // Instantiate the reduced results using the deduced args.
506 tree result = tsubst_constraint_expr (body, args, false);
507 if (result == error_mark_node)
508 return error_mark_node;
510 return result;
513 // Reduction rules for a variable template-id T.
515 // If T is a constraint, instantiate its initializer and recursively reduce its
516 // expression.
517 tree
518 normalize_var (tree t)
520 tree decl = DECL_TEMPLATE_RESULT (TREE_OPERAND (t, 0));
521 if (!DECL_DECLARED_CONCEPT_P (decl))
522 return t;
524 // Reduce the initializer of the variable into the constriants language.
525 tree body = normalize_constraints (DECL_INITIAL (decl));
526 if (!body)
527 return error_mark_node;
529 // Instantiate the reduced results.
530 tree result = tsubst_constraint_expr (body, TREE_OPERAND (t, 1), false);
531 if (result == error_mark_node)
532 return error_mark_node;
534 return result;
537 // Reduction rules for the template-id T.
539 // It turns out that we often get requirements being written like this:
541 // template<typename T>
542 // requires Foo<T>
543 // void f()
545 // Where Foo<T> should actually be written as Foo<T>(). Generate an
546 // error and suggest the improved writing.
547 tree
548 normalize_template_id (tree t)
550 tree tmpl = TREE_OPERAND (t, 0);
551 if (variable_concept_p (tmpl))
552 return normalize_var (t);
553 else
555 // FIXME: input_location is probably wrong, but there's not necessarly
556 // an expr location with the tree.
557 error_at (input_location, "invalid constraint %qE", t);
559 vec<tree, va_gc>* args = NULL;
560 tree c = finish_call_expr (t, &args, true, false, 0);
561 inform (input_location, "did you mean %qE", c);
563 return error_mark_node;
567 // Reduce an expression requirement as a conjunction of its
568 // individual constraints.
569 tree
570 normalize_expr_req (tree t)
572 tree r = NULL_TREE;
573 for (tree l = TREE_OPERAND (t, 0); l; l = TREE_CHAIN (l))
574 r = conjoin_constraints (r, normalize_expr (TREE_VALUE (l)));
575 return r;
578 // Reduce a type requirement by returing its underlying
579 // constraint.
580 tree
581 normalize_type_req (tree t)
583 return TREE_OPERAND (t, 0);
586 // Reduce a nested requireemnt by returing its only operand.
587 tree
588 normalize_nested_req (tree t)
590 return TREE_OPERAND (t, 0);
593 // Reduce a requires expr by reducing each requirement in turn,
594 // rewriting the list of requirements so that we end up with a
595 // list of expressions, some of which may be conjunctions.
596 tree
597 normalize_requires (tree t)
599 for (tree l = TREE_OPERAND (t, 1); l; l = TREE_CHAIN (l))
600 TREE_VALUE (l) = normalize_expr (TREE_VALUE (l));
601 return t;
604 // Reduction rules for the statement list STMTS.
606 // Recursively reduce each statement in the list, concatenating each
607 // reduced result into a conjunction of requirements.
609 // A constexpr function may include statements other than a return
610 // statement. The primary purpose of these rules is to filter those
611 // non-return statements from the constraints language.
612 tree
613 normalize_stmt_list (tree stmts)
615 tree lhs = NULL_TREE;
616 tree_stmt_iterator i = tsi_start (stmts);
617 while (!tsi_end_p (i))
619 if (tree rhs = normalize_node (tsi_stmt (i)))
620 lhs = conjoin_constraints (lhs, rhs);
621 tsi_next (&i);
623 return lhs;
626 // Normalize a cast expression.
627 tree
628 normalize_cast (tree t)
630 // return normalize_node (TREE_VALUE (TREE_OPERAND (t, 0)));
631 return normalize_atom (t);
634 // Normalize an atomic expression by performing some basic checks.
635 // In particular, if the type is known, it must be convertible to
636 // bool.
637 tree
638 normalize_atom (tree t)
640 if (!type_dependent_expression_p (t))
641 if (!can_convert (boolean_type_node, TREE_TYPE (t), tf_none))
643 error ("atomic constraint %qE is not convertible to %<bool%>", t);
644 return NULL_TREE;
646 return t;
649 // Reduce the requirement REQS into a logical formula written in terms of
650 // atomic propositions.
651 tree
652 normalize_constraints (tree reqs)
654 if (!reqs)
655 return NULL_TREE;
657 ++processing_template_decl;
658 tree expr = normalize_node (reqs);
659 --processing_template_decl;
660 return expr;
663 } // end namespace
666 // -------------------------------------------------------------------------- //
667 // Constraint Semantic Processing
669 // The following functions are called by the parser and substitution rules
670 // to create and evaluate constraint-related nodes.
672 // Returns the template constraints of declaration T. Note that
673 // T must be non-null.
674 tree
675 get_constraints (tree t)
677 gcc_assert (DECL_P (t));
678 return LANG_DECL_MIN_CHECK (t)->constraint_info;
681 // Associate the given constraint information with the declaration. Don't
682 // build associations if ci is NULL_TREE.
683 void
684 set_constraints (tree t, tree ci)
686 gcc_assert (DECL_P (t));
687 LANG_DECL_MIN_CHECK (t)->constraint_info = ci;
690 // Returns a conjunction of shorthand requirements for the template
691 // parameter list PARMS. Note that the requirements are stored in
692 // the TYPE of each tree node.
693 tree
694 get_shorthand_constraints (tree parms)
696 tree reqs = NULL_TREE;
697 parms = INNERMOST_TEMPLATE_PARMS (parms);
698 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
700 tree parm = TREE_VEC_ELT (parms, i);
701 reqs = conjoin_constraints(reqs, TEMPLATE_PARM_CONSTRAINTS (parm));
703 return reqs;
706 namespace {
707 // Create an empty constraint into block.
708 inline tree_constraint_info*
709 build_constraint_info ()
711 return (tree_constraint_info *)make_node (CONSTRAINT_INFO);
714 // Create a constraint info object, initialized with the given template
715 // requirements.
716 inline tree
717 init_leading_constraints (tree reqs)
719 tree_constraint_info* ci = build_constraint_info ();
720 ci->leading_reqs = reqs;
721 return (tree)ci;
724 // Initialize a constraint info object, initialized with the given
725 // trailing requirements.
726 inline tree
727 init_trailing_constraints (tree reqs)
729 tree_constraint_info* ci = build_constraint_info ();
730 ci->trailing_reqs = reqs;
731 return (tree)ci;
734 // Upodate the template requiremnets.
735 inline tree
736 update_leading_constraints (tree ci, tree reqs) {
737 tree& current = CI_LEADING_REQS (ci);
738 current = conjoin_constraints (current, reqs);
739 return ci;
742 // Set the trailing requiremnts to the given expression. Note that
743 // traling requirements cannot be updated once set: no other reqiurements
744 // can be found after parsing a trailing requires-clause.
745 inline tree
746 update_trailing_constraints (tree ci, tree reqs) {
747 gcc_assert(CI_TRAILING_REQS (ci) == NULL_TREE);
748 CI_TRAILING_REQS (ci) = reqs;
749 return ci;
751 } // namespace
753 // Return a constraint-info object containing the current template
754 // requirements. If constraints have already been assigned, then these
755 // are appended to the current constraints.
757 // Note that template constraints can be updated by the appearance of
758 // constrained type specifiers in a parameter list. These update the
759 // template requirements after the template header has been parsed.
760 tree
761 save_leading_constraints (tree reqs)
763 if (!reqs || reqs == error_mark_node)
764 return NULL_TREE;
765 else if (!current_template_reqs)
766 return init_leading_constraints (reqs);
767 else
768 return update_leading_constraints (current_template_reqs, reqs);
771 // Return a constraint info object containing saved trailing requirements.
772 // If there are already template requirements, these are added to the
773 // existing requirements. Otherwise, an empty constraint-info object
774 // holding only these trailing requirements is returned.
775 tree
776 save_trailing_constraints (tree reqs)
778 if (!reqs || reqs == error_mark_node)
779 return NULL_TREE;
780 else if (!current_template_reqs)
781 return init_trailing_constraints (reqs);
782 else
783 return update_trailing_constraints (current_template_reqs, reqs);
786 // Finish the template requirements, by computing the associated
787 // constrains (the conjunction of template and trailing requirements),
788 // and then decomposing that into sets of atomic propositions.
789 tree
790 finish_template_constraints (tree ci)
792 if (!ci || ci == error_mark_node)
793 return NULL_TREE;
795 // If these constraints have already been analyzed, don't do it
796 // a second time. This happens when groking a function decl.
797 // before creating its corresponding template.
798 if (CI_ASSUMPTIONS (ci))
799 return ci;
801 // Build and normalize th associated constraints. If any constraints
802 // are ill-formed, this is a hard error.
803 tree r1 = CI_LEADING_REQS (ci);
804 tree r2 = CI_TRAILING_REQS (ci);
805 tree reqs = normalize_constraints (conjoin_constraints (r1, r2));
806 CI_ASSOCIATED_REQS (ci) = reqs;
808 // If normalization succeeds, decompose those expressions into sets
809 // of atomic constraints. Otherwise, mark this as an error.
810 if (reqs)
811 CI_ASSUMPTIONS (ci) = decompose_assumptions (reqs);
812 else
813 CI_ASSUMPTIONS (ci) = error_mark_node;
814 return ci;
817 // Returns true iff cinfo contains a valid constraint expression.
818 // This is the case when the associated requirements can be successfully
819 // decomposed into lists of atomic constraints.
820 bool
821 valid_requirements_p (tree cinfo)
823 gcc_assert(cinfo);
824 return CI_ASSUMPTIONS (cinfo) != error_mark_node;
827 tree
828 build_requires_expr (tree parms, tree reqs)
830 // Modify the declared parameters by removing their context (so they
831 // don't refer to the enclosing scope), and marking them constant (so
832 // we can actually check constexpr properties).
833 for (tree p = parms; p && !VOID_TYPE_P (TREE_VALUE (p)); p = TREE_CHAIN (p))
835 tree parm = TREE_VALUE (p);
836 DECL_CONTEXT (parm) = NULL_TREE;
837 TREE_CONSTANT (parm) = true;
840 // Build the node.
841 tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs);
842 TREE_SIDE_EFFECTS (r) = false;
843 TREE_CONSTANT (r) = true;
844 return r;
847 // Evaluate an instantiatd requires expr, returning the truth node
848 // only when all sub-requirements have evaluated to true.
849 tree
850 eval_requires_expr (tree reqs)
852 for (tree t = reqs ; t; t = TREE_CHAIN (t)) {
853 tree r = TREE_VALUE (t);
854 r = fold_non_dependent_expr (r);
855 r = maybe_constant_value (r);
856 if (r != boolean_true_node)
857 return boolean_false_node;
859 return boolean_true_node;
862 // Finish a requires expression, returning a node wrapping the parameters,
863 // PARMS, and the list of requirements REQS.
864 tree
865 finish_requires_expr (tree parms, tree reqs)
867 if (processing_template_decl)
868 return build_requires_expr (parms, reqs);
869 else
870 return eval_requires_expr (reqs);
873 // Construct a unary expression that evaluates properties of the
874 // expression or type T, and has a boolean result type.
875 static inline tree
876 build_check_expr (tree_code c, tree t)
878 tree r = build_min (c, boolean_type_node, t);
879 TREE_SIDE_EFFECTS (r) = false;
880 TREE_READONLY (r) = true;
881 TREE_CONSTANT (r) = true;
882 return r;
885 // Finish a syntax requirement, constructing a list embodying a sequence
886 // of checks for the validity of EXPR and TYPE, the convertibility of
887 // EXPR to TYPE, and the expression properties specified in SPECS.
888 tree
889 finish_expr_requirement (tree expr, tree type, tree specs)
891 gcc_assert (processing_template_decl);
893 // Build a list of checks, starting with the valid expression.
894 tree result = tree_cons (NULL_TREE, finish_validexpr_expr (expr), NULL_TREE);
896 // If a type requirement was provided, build the result type checks.
897 if (type)
899 // If the type is dependent, ensure that it can be validly
900 // instantiated.
902 // NOTE: We can also disregard checks that result in the template
903 // parameter.
904 if (dependent_type_p (type))
906 tree treq = finish_type_requirement (type);
907 result = tree_cons (NULL_TREE, treq, result);
910 // Ensure that the result of the expression can be converted to
911 // the result type.
912 tree decl_type = finish_decltype_type (expr, false, tf_none);
913 tree creq = finish_trait_expr (CPTK_IS_CONVERTIBLE_TO, decl_type, type);
914 result = tree_cons (NULL_TREE, creq, result);
917 // If constraint specifiers are present, make them part of the
918 // list of constraints.
919 if (specs)
921 TREE_CHAIN (tree_last (specs)) = result;
922 result = specs;
925 // Finally, construct the syntactic requirement.
926 return build_check_expr (EXPR_REQ, nreverse (result));
929 // Finish a simple syntax requirement, returning a node representing
930 // a check that EXPR is a valid expression.
931 tree
932 finish_expr_requirement (tree expr)
934 gcc_assert (processing_template_decl);
935 tree req = finish_validexpr_expr (expr);
936 tree reqs = tree_cons (NULL_TREE, req, NULL_TREE);
937 return build_check_expr (EXPR_REQ, reqs);
940 // Finish a type requirement, returning a node representing a check
941 // that TYPE will result in a valid type when instantiated.
942 tree
943 finish_type_requirement (tree type)
945 gcc_assert (processing_template_decl);
946 tree req = finish_validtype_expr (type);
947 return build_check_expr (TYPE_REQ, req);
950 tree
951 finish_nested_requirement (tree expr)
953 gcc_assert (processing_template_decl);
954 return build_check_expr (NESTED_REQ, expr);
957 // Finish a constexpr requirement, returning a node representing a
958 // check that EXPR, when instantiated, may be evaluated at compile time.
959 tree
960 finish_constexpr_requirement (tree expr)
962 gcc_assert (processing_template_decl);
963 return finish_constexpr_expr (expr);
966 // Finish the noexcept requirement by constructing a noexcept
967 // expression evaluating EXPR.
968 tree
969 finish_noexcept_requirement (tree expr)
971 gcc_assert (processing_template_decl);
972 return finish_noexcept_expr (expr, tf_none);
975 // Returns the true or false node depending on the truth value of B.
976 static inline tree
977 truth_node (bool b)
979 return b ? boolean_true_node : boolean_false_node;
982 // Returns a finished validexpr-expr. Returns the true or false node
983 // depending on whether EXPR denotes a valid expression. This is the case
984 // when the expression has been successfully type checked.
986 // When processing a template declaration, the result is an expression
987 // representing the check.
988 tree
989 finish_validexpr_expr (tree expr)
991 if (processing_template_decl)
992 return build_check_expr (VALIDEXPR_EXPR, expr);
993 return truth_node (expr && expr != error_mark_node);
996 // Returns a finished validtype-expr. Returns the true or false node
997 // depending on whether T denotes a valid type name.
999 // When processing a template declaration, the result is an expression
1000 // representing the check.
1002 // FIXME: Semantics need to be aligned with the new version of the
1003 // specificaiton (i.e., we must be able to invent a function and
1004 // perform argument deduction against it).
1005 tree
1006 finish_validtype_expr (tree type)
1008 if (is_auto (type))
1010 sorry ("%<auto%< not supported in result type constraints\n");
1011 return error_mark_node;
1014 if (processing_template_decl)
1015 return build_check_expr (VALIDTYPE_EXPR, type);
1016 return truth_node (type && TYPE_P (type));
1019 // Returns a finished constexpr-expr. Returns the true or false node
1020 // depending on whether the expression T may be evaluated at compile
1021 // time.
1023 // When processing a template declaration, the result is an expression
1024 // representing the check.
1025 tree
1026 finish_constexpr_expr (tree expr)
1028 if (processing_template_decl)
1029 return build_check_expr (CONSTEXPR_EXPR, expr);
1031 // TODO: Actually check that the expression can be constexpr
1032 // evaluatd.
1034 // return truth_node (potential_constant_expression (expr));
1035 sorry ("constexpr requirement");
1036 return NULL_TREE;
1039 // Check that a constrained friend declaration function declaration,
1040 // FN, is admissable. This is the case only when the declaration depends
1041 // on template parameters and does not declare a specialization.
1042 void
1043 check_constrained_friend (tree fn, tree reqs)
1045 if (fn == error_mark_node)
1046 return;
1047 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
1049 // If there are not constraints, this cannot be an error.
1050 if (!reqs)
1051 return;
1053 // Constrained friend functions that don't depend on template
1054 // arguments are effectively meaningless.
1055 tree parms = DECL_ARGUMENTS (fn);
1056 tree result = TREE_TYPE (TREE_TYPE (fn));
1057 if (!(parms && uses_template_parms (parms)) && !uses_template_parms (result))
1059 error ("constrained friend does not depend on template parameters");
1060 return;
1064 namespace {
1065 // Build a new call expression, but don't actually generate a new
1066 // function call. We just want the tree, not the semantics.
1067 inline tree
1068 build_call_check (tree id)
1070 ++processing_template_decl;
1071 vec<tree, va_gc> *fargs = make_tree_vector();
1072 tree call = finish_call_expr (id, &fargs, false, false, tf_none);
1073 --processing_template_decl;
1074 return call;
1076 } // namespace
1078 // Construct a concept check for the given TARGET. The target may be
1079 // an overload set or a baselink referring to an overload set. Template
1080 // arguments to the target are given by ARG and REST. If the target is
1081 // a function (overload set or baselink reffering to an overload set),
1082 // then ths builds the call expression TARGET<ARG, REST>(). If REST is
1083 // NULL_TREE, then the resulting check is just TARGET<ARG>().
1084 tree
1085 build_concept_check (tree target, tree arg, tree rest)
1087 gcc_assert (rest ? TREE_CODE (rest) == TREE_VEC : true);
1089 // Build a template-id that acts as the call target using TARGET as
1090 // the template and ARG as the only explicit argument.
1091 int n = rest ? TREE_VEC_LENGTH (rest) : 0;
1092 tree targs = make_tree_vec (n + 1);
1093 TREE_VEC_ELT (targs, 0) = arg;
1094 if (rest)
1095 for (int i = 0; i < n; ++i)
1096 TREE_VEC_ELT (targs, i + 1) = TREE_VEC_ELT (rest, i);
1097 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, n + 1);
1098 if (variable_template_p (target))
1099 return lookup_template_variable (target, targs);
1100 else
1102 tree id = lookup_template_function (target, targs);
1103 return build_call_check (id);
1107 // Returns a TYPE_DECL that contains sufficient information to build
1108 // a template parameter of the same kind as PROTO and constrained
1109 // by the concept declaration FN. PROTO is saved as the initializer of
1110 // the new type decl, and the constraining function is saved in
1111 // DECL_SIZE_UNIT.
1113 // If specified ARGS provides additional arguments to the constraint
1114 // check. These are stored in the DECL_SIZE field.
1115 tree
1116 build_constrained_parameter (tree fn, tree proto, tree args)
1118 tree name = DECL_NAME (fn);
1119 tree type = TREE_TYPE (proto);
1120 tree decl = build_decl (input_location, TYPE_DECL, name, type);
1121 DECL_INITIAL (decl) = proto; // Describing parameter
1122 DECL_SIZE_UNIT (decl) = fn; // Constraining function declaration
1123 DECL_SIZE (decl) = args; // Extra template arguments.
1124 return decl;
1127 // Create a constraint expression for the given DECL that evaluates the
1128 // requirements specified by CONSTR, a TYPE_DECL that contains all the
1129 // information necessary to build the requirements (see finish_concept_name
1130 // for the layout of that TYPE_DECL).
1132 // Note that the constraints are neither reduced nor decomposed. That is
1133 // done only after the requires clause has been parsed (or not).
1134 tree
1135 finish_shorthand_constraint (tree decl, tree constr)
1137 // No requirements means no constraints.
1138 if (!constr)
1139 return NULL_TREE;
1141 tree proto = DECL_INITIAL (constr); // The prototype declaration
1142 tree con = DECL_SIZE_UNIT (constr); // The concept declaration
1143 tree args = DECL_SIZE (constr); // Extra template arguments
1145 // If the parameter declaration is variadic, but the concept is not
1146 // then we need to apply the concept to every element in the pack.
1147 bool is_proto_pack = template_parameter_pack_p (proto);
1148 bool is_decl_pack = template_parameter_pack_p (decl);
1149 bool apply_to_all_p = is_decl_pack && !is_proto_pack;
1151 // Get the argument and overload used for the requirement. Adjust
1152 // if we're going to expand later.
1153 tree arg = template_parm_to_arg (build_tree_list (NULL_TREE, decl));
1154 if (apply_to_all_p)
1155 arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
1157 // Build the concept check. If it the constraint needs to be applied
1158 // to all elements of the parameter pack, then expand make the constraint
1159 // an expansion.
1160 tree check;
1161 if (TREE_CODE (con) == VAR_DECL)
1163 check = build_concept_check (DECL_TI_TEMPLATE (con), arg, args);
1165 else
1167 tree ovl = build_overload (DECL_TI_TEMPLATE (con), NULL_TREE);
1168 check = build_concept_check (ovl, arg, args);
1170 if (apply_to_all_p)
1172 check = make_pack_expansion (check);
1174 // Set the type to indicate that this expansion will get special
1175 // treatment during instantiation.
1177 // TODO: Maybe this should be a different kind of node... one that
1178 // has all the same properties as a pack expansion, but has a definite
1179 // expansion when instantiated as part of an expression.
1181 // As of now, this is a hack.
1182 TREE_TYPE (check) = boolean_type_node;
1185 return check;
1188 // -------------------------------------------------------------------------- //
1189 // Substitution Rules
1191 // The following functions implement substitution rules for constraints.
1193 namespace {
1194 // In an unevaluated context, the substitution of parm decls are not
1195 // properly chained during substitution. Do that here.
1196 tree
1197 fix_local_parms (tree sparms)
1199 if (!sparms)
1200 return sparms;
1202 tree p = TREE_CHAIN (sparms);
1203 tree q = sparms;
1204 while (p && TREE_VALUE (p) != void_type_node)
1206 DECL_CHAIN (TREE_VALUE (q)) = TREE_VALUE (p);
1207 q = p;
1208 p = TREE_CHAIN (p);
1210 return sparms;
1213 // Register local specializations for each of tparm and the corresponding
1214 // sparm. This is a helper function for tsubst_requires_expr.
1215 void
1216 declare_local_parms (tree tparms, tree sparms)
1218 tree s = TREE_VALUE (sparms);
1219 for (tree p = tparms; p && !VOID_TYPE_P (TREE_VALUE (p)); p = TREE_CHAIN (p))
1221 tree t = TREE_VALUE (p);
1222 if (DECL_PACK_P (t))
1224 tree pack = extract_fnparm_pack (t, &s);
1225 register_local_specialization (pack, t);
1227 else
1229 register_local_specialization (s, t);
1230 s = TREE_CHAIN (s);
1235 // Substitute ARGS into the parameter list T, producing a sequence of
1236 // local parameters (variables) in the current scope.
1237 tree
1238 tsubst_local_parms (tree t,
1239 tree args,
1240 tsubst_flags_t complain,
1241 tree in_decl)
1243 tree r = fix_local_parms (tsubst (t, args, complain, in_decl));
1244 if (r == error_mark_node)
1245 return error_mark_node;
1247 // Register the instantiated args as local parameters.
1248 if (t)
1249 declare_local_parms (t, r);
1251 return r;
1254 // Substitute ARGS into the requirement body (list of requirements), T.
1255 // Note that if any substitutions fail, then this is equivalent to
1256 // returning false.
1257 tree
1258 tsubst_requirement_body (tree t, tree args, tree in_decl)
1260 tree r = NULL_TREE;
1261 while (t)
1263 tree e = tsubst_expr (TREE_VALUE (t), args, tf_none, in_decl, false);
1264 if (e == error_mark_node)
1265 e = boolean_false_node;
1266 r = tree_cons (NULL_TREE, e, r);
1267 t = TREE_CHAIN (t);
1269 return r;
1271 } // namespace
1273 // Substitute ARGS into the requires expression T.
1274 tree
1275 tsubst_requires_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1277 local_specialization_stack stack;
1278 tree p = tsubst_local_parms (TREE_OPERAND (t, 0), args, complain, in_decl);
1279 tree r = tsubst_requirement_body (TREE_OPERAND (t, 1), args, in_decl);
1280 return finish_requires_expr (p, r);
1283 // Substitute ARGS into the valid-expr expression T.
1284 tree
1285 tsubst_validexpr_expr (tree t, tree args, tree in_decl)
1287 tree r = tsubst_expr (TREE_OPERAND (t, 0), args, tf_none, in_decl, false);
1288 return finish_validexpr_expr (r);
1291 // Substitute ARGS into the valid-type expression T.
1292 tree
1293 tsubst_validtype_expr (tree t, tree args, tree in_decl)
1295 tree r = tsubst (TREE_OPERAND (t, 0), args, tf_none, in_decl);
1296 return finish_validtype_expr (r);
1299 // Substitute ARGS into the constexpr expression T.
1300 tree
1301 tsubst_constexpr_expr (tree t, tree args, tree in_decl)
1303 tree r = tsubst_expr (TREE_OPERAND (t, 0), args, tf_none, in_decl, false);
1304 return finish_constexpr_expr (r);
1307 // Substitute ARGS into the expr requirement T. Note that a requirement
1308 // node is instantiated from a non-reduced context (e.g., static_assert).
1309 tree
1310 tsubst_expr_req (tree t, tree args, tree in_decl)
1312 tree r = NULL_TREE;
1313 for (tree l = TREE_OPERAND (t, 0); l; l = TREE_CHAIN (l))
1315 tree e = tsubst_expr (TREE_VALUE (l), args, tf_none, in_decl, false);
1316 r = conjoin_constraints (r, e);
1318 return r;
1321 // Substitute ARGS into the type requirement T. Note that a requirement
1322 // node is instantiated from a non-reduced context (e.g., static_assert).
1323 tree
1324 tsubst_type_req (tree t, tree args, tree in_decl)
1326 return tsubst_expr (TREE_OPERAND (t, 0), args, tf_none, in_decl, false);
1329 // Substitute ARGS into the nested requirement T. Note that a requirement
1330 // node is instantiated from a non-reduced context (e.g., static_assert).
1331 tree
1332 tsubst_nested_req (tree t, tree args, tree in_decl)
1334 return tsubst_expr (TREE_OPERAND (t, 0), args, tf_none, in_decl, false);
1337 // Used in various contexts to control substitution. In particular, when
1338 // non-zero, the substitution of NULL arguments into a type will still
1339 // process the type as if passing non-NULL arguments, allowing type
1340 // expressions to be fully elaborated during substitution.
1341 int processing_constraint;
1343 // Substitute the template arguments ARGS into the requirement
1344 // expression REQS. Errors resulting from substitution are not
1345 // diagnosed.
1347 // If DO_NOT_FOLD is true, then the requirements are substituted as
1348 // if parsing a template declaration, which causes the resulting expression
1349 // to not be folded.
1350 tree
1351 tsubst_constraint_expr (tree reqs, tree args, bool do_not_fold)
1353 cp_unevaluated guard;
1354 ++processing_constraint;
1355 if (do_not_fold)
1356 ++processing_template_decl;
1357 tree r = tsubst_expr (reqs, args, tf_none, NULL_TREE, false);
1358 if (do_not_fold)
1359 --processing_template_decl;
1360 --processing_constraint;
1361 return r;
1364 // Substitute into the constraint information, producing a new constraint
1365 // record.
1366 tree
1367 tsubst_constraint_info (tree ci, tree args)
1369 if (!ci || ci == error_mark_node)
1370 return NULL_TREE;
1372 // Substitute into the various constraint fields.
1373 tree_constraint_info* result = build_constraint_info ();
1374 if (tree r = CI_LEADING_REQS (ci))
1375 result->leading_reqs = tsubst_constraint_expr (r, args, true);
1376 if (tree r = CI_TRAILING_REQS (ci))
1377 result->trailing_reqs = tsubst_constraint_expr (r, args, true);
1378 if (tree r = CI_ASSOCIATED_REQS (ci))
1379 result->associated_reqs = tsubst_constraint_expr (r, args, true);
1381 // Re-normalize the constraints to ensure that we haven't picked
1382 // any fatal errors when substituting.
1383 if (!normalize_constraints (result->associated_reqs))
1385 result->associated_reqs = error_mark_node;
1386 result->assumptions = error_mark_node;
1388 else
1390 // Analyze the resulting constraints.
1391 result->assumptions = decompose_assumptions (result->associated_reqs);
1394 return (tree)result;
1397 // -------------------------------------------------------------------------- //
1398 // Constraint Satisfaction
1400 // The following functions are responsible for the instantiation and
1401 // evaluation of constraints.
1403 namespace {
1404 // Returns true iff the atomic constraint, REQ, is satisfied. This
1405 // is the case when substitution succeeds and the resulting expression
1406 // evaluates to true.
1407 static bool
1408 check_satisfied (tree req, tree args)
1410 // If any arguments are dependent, then we can't check the
1411 // requirements. Just return true.
1412 if (args && uses_template_parms (args))
1413 return true;
1415 // Instantiate and evaluate the requirements.
1416 req = tsubst_constraint_expr (req, args, false);
1417 if (req == error_mark_node)
1418 return false;
1420 // Reduce any remaining TRAIT_EXPR nodes before evaluating.
1421 req = fold_non_dependent_expr (req);
1423 // Requirements are satisfied when REQS evaluates to true.
1424 tree result = cxx_constant_value (req);
1425 return result == boolean_true_node;
1428 } // namespace
1430 // Check the instantiated declaration constraints.
1431 bool
1432 check_constraints (tree cinfo)
1434 // No constraints? Satisfied.
1435 if (!cinfo)
1436 return true;
1437 // Invalid constraints, not satisfied.
1438 else if (!valid_requirements_p (cinfo))
1439 return false;
1440 // Funnel back into the dependent checking branch. This forces
1441 // one more substitution through the constraints, which removes
1442 // all remaining expressions that are not constant expressions
1443 // (e.g., template-id expressions).
1444 else
1445 return check_satisfied (CI_ASSOCIATED_REQS (cinfo), NULL_TREE);
1448 // Check the constraints in CINFO against the given ARGS, returning
1449 // true when the constraints are satisfied and false otherwise.
1450 bool
1451 check_constraints (tree cinfo, tree args)
1453 // If there are no constraints then this is trivally satisfied.
1454 if (!cinfo)
1455 return true;
1456 // Invlaid requirements cannot be satisfied.
1457 else if (!valid_requirements_p (cinfo))
1458 return false;
1459 else {
1460 return check_satisfied (CI_ASSOCIATED_REQS (cinfo), args);
1464 // Check the constraints of the declaration or type T, against
1465 // the specified arguments. Returns true if the constraints are
1466 // satisfied and false otherwise.
1467 bool
1468 check_template_constraints (tree t, tree args)
1470 return check_constraints (get_constraints (t), args);
1473 // -------------------------------------------------------------------------- //
1474 // Constraint Relations
1476 // Interfaces for determining equivalency and ordering of constraints.
1478 // Returns true when A and B are equivalent constraints.
1479 bool
1480 equivalent_constraints (tree a, tree b)
1482 return cp_tree_equal (a, b);
1485 // Returns true if the template declarations A and B have equivalent
1486 // constraints. This is the case when A's constraints subsume B's and
1487 // when B's also constrain A's.
1488 bool
1489 equivalently_constrained (tree a, tree b)
1491 gcc_assert (TREE_CODE (a) == TREE_CODE (b));
1492 return equivalent_constraints (get_constraints (a), get_constraints (b));
1495 // Returns true when the template declaration A's constraints subsume
1496 // those of the template declaration B.
1497 bool
1498 subsumes_constraints (tree a, tree b)
1500 gcc_assert (TREE_CODE (a) == TREE_CODE (b));
1501 return subsumes (get_constraints (a), get_constraints (b));
1504 // Determines which of the templates, A or B, is more constrained.
1505 // That is, which template's constraints subsume but are not subsumed
1506 // by the other's?
1508 // Returns 1 if A is more constrained than B, -1 if B is more constrained
1509 // than A, and 0 otherwise.
1511 more_constrained (tree a, tree b) {
1512 int winner = 0;
1513 if (subsumes_constraints (a, b))
1514 ++winner;
1515 if (subsumes_constraints (b, a))
1516 --winner;
1517 return winner;
1521 // -------------------------------------------------------------------------- //
1522 // Constraint Diagnostics
1524 namespace {
1526 // Given an arbitrary constraint expression, normalize it and
1527 // then check it. We have to normalize so we don't accidentally
1528 // instantiate concept declarations.
1529 inline bool
1530 check_diagnostic_constraints (tree reqs, tree args)
1532 return check_satisfied (normalize_constraints (reqs), args);
1535 void diagnose_node (location_t, tree, tree);
1537 // Diagnose a constraint failure for type trait expressions.
1538 void
1539 diagnose_trait (location_t loc, tree t, tree args)
1541 if (check_diagnostic_constraints (t, args))
1542 return;
1544 tree subst = tsubst_constraint_expr (t, args, true);
1546 if (subst == error_mark_node)
1548 inform (input_location, " substitution failure in %qE", t);
1549 return;
1552 tree t1 = TRAIT_EXPR_TYPE1 (subst);
1553 tree t2 = TRAIT_EXPR_TYPE2 (subst);
1554 switch (TRAIT_EXPR_KIND (t))
1556 case CPTK_HAS_NOTHROW_ASSIGN:
1557 inform (loc, " %qT is not nothrow copy assignable", t1);
1558 break;
1559 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
1560 inform (loc, " %qT is not nothrow default constructible", t1);
1561 break;
1562 case CPTK_HAS_NOTHROW_COPY:
1563 inform (loc, " %qT is not nothrow copy constructible", t1);
1564 break;
1565 case CPTK_HAS_TRIVIAL_ASSIGN:
1566 inform (loc, " %qT is not trivially copy assignable", t1);
1567 break;
1568 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
1569 inform (loc, " %qT is not trivially default constructible", t1);
1570 break;
1571 case CPTK_HAS_TRIVIAL_COPY:
1572 inform (loc, " %qT is not trivially copy constructible", t1);
1573 break;
1574 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
1575 inform (loc, " %qT is not trivially destructible", t1);
1576 break;
1577 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
1578 inform (loc, " %qT does not have a virtual destructor", t1);
1579 break;
1580 case CPTK_IS_ABSTRACT:
1581 inform (loc, " %qT is not an abstract class", t1);
1582 break;
1583 case CPTK_IS_BASE_OF:
1584 inform (loc, " %qT is not a base of %qT", t1, t2);
1585 break;
1586 case CPTK_IS_CLASS:
1587 inform (loc, " %qT is not a class", t1);
1588 break;
1589 case CPTK_IS_CONVERTIBLE_TO:
1590 inform (loc, " %qT is not convertible to %qT", t1, t2);
1591 break;
1592 case CPTK_IS_EMPTY:
1593 inform (loc, " %qT is not an empty class", t1);
1594 break;
1595 case CPTK_IS_ENUM:
1596 inform (loc, " %qT is not an enum", t1);
1597 break;
1598 case CPTK_IS_FINAL:
1599 inform (loc, " %qT is not a final class", t1);
1600 break;
1601 case CPTK_IS_LITERAL_TYPE:
1602 inform (loc, " %qT is not a literal type", t1);
1603 break;
1604 case CPTK_IS_POD:
1605 inform (loc, " %qT is not a POD type", t1);
1606 break;
1607 case CPTK_IS_POLYMORPHIC:
1608 inform (loc, " %qT is not a polymorphic type", t1);
1609 break;
1610 case CPTK_IS_SAME_AS:
1611 inform (loc, " %qT is not the same as %qT", t1, t2);
1612 break;
1613 case CPTK_IS_STD_LAYOUT:
1614 inform (loc, " %qT is not an standard layout type", t1);
1615 break;
1616 case CPTK_IS_TRIVIAL:
1617 inform (loc, " %qT is not a trivial type", t1);
1618 break;
1619 case CPTK_IS_UNION:
1620 inform (loc, " %qT is not a union", t1);
1621 break;
1622 default:
1623 gcc_unreachable ();
1627 // Diagnose a failed concept check in concept indicated by T, where
1628 // T is the result of resolve_constraint_check. Recursively analyze
1629 // the nested requiremets for details.
1630 void
1631 diagnose_check (location_t loc, tree t, tree args)
1633 tree fn = TREE_VALUE (t);
1634 tree targs = TREE_PURPOSE (t);
1635 tree body = DECL_SAVED_TREE (fn);
1636 if (!body)
1637 return;
1639 inform (loc, " failure in constraint %q#D", DECL_TI_TEMPLATE (fn));
1641 // Perform a mini-reduction on the constraint.
1642 if (TREE_CODE (body) == BIND_EXPR)
1643 body = BIND_EXPR_BODY (body);
1644 if (TREE_CODE (body) == RETURN_EXPR)
1645 body = TREE_OPERAND (body, 0);
1647 // Locally instantiate the body with the call's template args,
1648 // and recursively diagnose.
1649 body = tsubst_constraint_expr (body, targs, true);
1651 diagnose_node (loc, body, args);
1654 // Diagnose constraint failures from the call expression T.
1655 void
1656 diagnose_call (location_t loc, tree t, tree args)
1658 if (check_diagnostic_constraints (t, args))
1659 return;
1661 // If this is a concept, we're going to recurse.
1662 // If it's just a call, then we can emit a simple message.
1663 if (tree check = resolve_constraint_check (t))
1664 diagnose_check (loc, check, args);
1665 else
1666 inform (loc, " %qE evaluated to false", t);
1669 // Diagnose constraint failures in a variable concept.
1670 void
1671 diagnose_var (location_t loc, tree t, tree args)
1673 // If the template-id isn't a variable template, it can't be a
1674 // valid constraint.
1675 if (!variable_template_p (TREE_OPERAND (t, 0)))
1677 inform (loc, " invalid constraint %qE", t);
1678 return;
1681 if (check_diagnostic_constraints (t, args))
1682 return;
1684 tree var = DECL_TEMPLATE_RESULT (TREE_OPERAND (t, 0));
1685 tree body = DECL_INITIAL (var);
1686 tree targs = TREE_OPERAND (t, 1);
1687 tree subst = tsubst_constraint_expr (body, targs, true);
1689 inform (loc, " failure in constraint %q#D", DECL_TI_TEMPLATE (var));
1691 diagnose_node (loc, subst, args);
1694 // Diagnose specific constraint failures.
1695 void
1696 diagnose_requires (location_t loc, tree t, tree args)
1698 if (check_diagnostic_constraints (t, args))
1699 return;
1701 tree subst = tsubst_constraint_expr (t, args, true);
1703 // Print the header for the requires expression.
1704 tree parms = TREE_OPERAND (subst, 0);
1705 if (!VOID_TYPE_P (TREE_VALUE (parms)))
1706 inform (loc, " requiring syntax with values %Z", TREE_OPERAND (subst, 0));
1708 // Create a new local specialization binding for the arguments.
1709 // This lets us instantiate sub-expressions separately from the
1710 // requires clause.
1711 local_specialization_stack locals;
1712 declare_local_parms (TREE_OPERAND (t, 0), TREE_OPERAND (subst, 0));
1714 // Iterate over the sub-requirements and try instantiating each.
1715 for (tree l = TREE_OPERAND (t, 1); l; l = TREE_CHAIN (l))
1716 diagnose_node (loc, TREE_VALUE (l), args);
1719 static void
1720 diagnose_validexpr (location_t loc, tree t, tree args)
1722 if (check_diagnostic_constraints (t, args))
1723 return;
1724 inform (loc, " %qE is not a valid expression", TREE_OPERAND (t, 0));
1727 static void
1728 diagnose_validtype (location_t loc, tree t, tree args)
1730 if (check_diagnostic_constraints (t, args))
1731 return;
1733 // Substitute into the qualified name.
1734 tree name = TREE_OPERAND (t, 0);
1735 if (tree cxt = TYPE_CONTEXT (name))
1737 tree id = TYPE_IDENTIFIER (name);
1738 cxt = tsubst (cxt, args, tf_none, NULL_TREE);
1739 name = build_qualified_name (NULL_TREE, cxt, id, false);
1740 inform (loc, " %qE does not name a valid type", name);
1742 else
1744 inform (loc, " %qT does not name a valid type", name);
1748 static void
1749 diagnose_constexpr (location_t loc, tree t, tree args)
1751 if (check_diagnostic_constraints (t, args))
1752 return;
1753 inform (loc, " %qE is not a constant expression", TREE_OPERAND (t, 0));
1756 static void
1757 diagnose_noexcept (location_t loc, tree t, tree args)
1759 if (check_diagnostic_constraints (t, args))
1760 return;
1761 inform (loc, " %qE propagates exceptions", TREE_OPERAND (t, 0));
1764 // Diagnose a constraint failure in the expression T.
1765 void
1766 diagnose_other (location_t loc, tree t, tree args)
1768 if (check_diagnostic_constraints (t, args))
1769 return;
1770 inform (loc, " %qE evaluated to false", t);
1773 // Diagnose a constraint failure in the subtree T.
1774 void
1775 diagnose_node (location_t loc, tree t, tree args)
1777 switch (TREE_CODE (t))
1779 case TRUTH_ANDIF_EXPR:
1780 diagnose_node (loc, TREE_OPERAND (t, 0), args);
1781 diagnose_node (loc, TREE_OPERAND (t, 1), args);
1782 break;
1784 case TRUTH_ORIF_EXPR:
1785 // TODO: Design better diagnostics for dijunctions.
1786 diagnose_other (loc, t, args);
1787 break;
1789 case TRAIT_EXPR:
1790 diagnose_trait (loc, t, args);
1791 break;
1793 case CALL_EXPR:
1794 diagnose_call (loc, t, args);
1795 break;
1797 case REQUIRES_EXPR:
1798 diagnose_requires (loc, t, args);
1799 break;
1801 case VALIDEXPR_EXPR:
1802 diagnose_validexpr (loc, t, args);
1803 break;
1805 case VALIDTYPE_EXPR:
1806 diagnose_validtype (loc, t, args);
1807 break;
1809 case CONSTEXPR_EXPR:
1810 diagnose_constexpr (loc, t, args);
1811 break;
1813 case NOEXCEPT_EXPR:
1814 diagnose_noexcept (loc, t, args);
1815 break;
1817 case TEMPLATE_ID_EXPR:
1818 diagnose_var (loc, t, args);
1819 break;
1821 default:
1822 diagnose_other (loc, t, args);
1823 break;
1827 // Diagnose a constraint failure in the requirements expression REQS.
1828 inline void
1829 diagnose_requirements (location_t loc, tree reqs, tree args)
1831 diagnose_node (loc, reqs, args);
1834 // Create a tree node representing the substitution of ARGS into
1835 // the parameters of TMPL. The resulting structure is passed as an
1836 // for diagnosing substitutions.
1837 inline tree
1838 make_subst (tree tmpl, tree args)
1840 tree subst = tree_cons (NULL_TREE, args, NULL_TREE);
1841 TREE_TYPE (subst) = DECL_TEMPLATE_PARMS (tmpl);
1842 return subst;
1845 } // namespace
1847 // Emit diagnostics detailing the failure ARGS to satisfy the constraints
1848 // of the template declaration, TMPL.
1849 void
1850 diagnose_constraints (location_t loc, tree decl, tree args)
1852 tree ci = get_constraints (decl);
1854 // If the constraints could not be reduced, then we can't diagnose them.
1855 if (!valid_requirements_p (ci))
1857 inform (loc, " invalid constraints");
1858 return;
1861 // If this is a specialization of a template, we want to diagnose
1862 // the dependent constraints. Also update the template arguments.
1863 if (DECL_USE_TEMPLATE (decl)) {
1864 args = DECL_TI_ARGS (decl);
1865 decl = DECL_TI_TEMPLATE (decl);
1868 // Otherwise, diagnose the actual failed constraints.
1869 if (TREE_CODE (decl) == TEMPLATE_DECL)
1870 inform (loc, " constraints not satisfied %S", make_subst (decl, args));
1871 else
1872 inform (loc, " constraints not satisfied");
1874 // Diagnose the constraints by recursively decomposing and
1875 // evaluating the template requirements.
1876 tree reqs = CI_ASSOCIATED_REQS (get_constraints (decl));
1877 diagnose_requirements (loc, reqs, args);