1 /* Processing rules for constraints.
2 Copyright (C) 2013-2020 Free Software Foundation, Inc.
3 Contributed by Andrew Sutton (andrew.n.sutton@gmail.com)
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "hash-set.h"
27 #include "machmode.h"
28 #include "vec.h"
29 #include "double-int.h"
30 #include "input.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "wide-int.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "stringpool.h"
37 #include "attribs.h"
38 #include "intl.h"
39 #include "flags.h"
40 #include "cp-tree.h"
41 #include "c-family/c-common.h"
42 #include "c-family/c-objc.h"
43 #include "cp-objcp-common.h"
44 #include "tree-inline.h"
45 #include "decl.h"
46 #include "toplev.h"
47 #include "type-utils.h"
48
49 static tree satisfaction_value (tree t);
50
51 /* When we're parsing or substuting a constraint expression, we have slightly
52 different expression semantics. In particular, we don't want to reduce a
53 concept-id to a satisfaction value. */
54
55 processing_constraint_expression_sentinel::
processing_constraint_expression_sentinel()56 processing_constraint_expression_sentinel ()
57 {
58 ++scope_chain->x_processing_constraint;
59 }
60
61 processing_constraint_expression_sentinel::
~processing_constraint_expression_sentinel()62 ~processing_constraint_expression_sentinel ()
63 {
64 --scope_chain->x_processing_constraint;
65 }
66
67 bool
processing_constraint_expression_p()68 processing_constraint_expression_p ()
69 {
70 return scope_chain->x_processing_constraint != 0;
71 }
72
73 /*---------------------------------------------------------------------------
74 Constraint expressions
75 ---------------------------------------------------------------------------*/
76
77 /* Information provided to substitution. */
78
79 struct subst_info
80 {
subst_infosubst_info81 subst_info (tsubst_flags_t cmp, tree in)
82 : complain (cmp), in_decl (in)
83 { }
84
85 /* True if we should not diagnose errors. */
quietsubst_info86 bool quiet() const
87 {
88 return complain == tf_none;
89 }
90
91 /* True if we should diagnose errors. */
noisysubst_info92 bool noisy() const
93 {
94 return !quiet ();
95 }
96
97 tsubst_flags_t complain;
98 tree in_decl;
99 };
100
101 static tree satisfy_constraint (tree, tree, subst_info);
102
103 /* True if T is known to be some type other than bool. Note that this
104 is false for dependent types and errors. */
105
106 static inline bool
known_non_bool_p(tree t)107 known_non_bool_p (tree t)
108 {
109 return (t && !WILDCARD_TYPE_P (t) && TREE_CODE (t) != BOOLEAN_TYPE);
110 }
111
112 static bool
check_constraint_atom(cp_expr expr)113 check_constraint_atom (cp_expr expr)
114 {
115 if (known_non_bool_p (TREE_TYPE (expr)))
116 {
117 error_at (expr.get_location (),
118 "constraint expression does not have type %<bool%>");
119 return false;
120 }
121
122 /* Check that we're using function concepts correctly. */
123 if (concept_check_p (expr))
124 {
125 tree id = unpack_concept_check (expr);
126 tree tmpl = TREE_OPERAND (id, 0);
127 if (OVL_P (tmpl) && TREE_CODE (expr) == TEMPLATE_ID_EXPR)
128 {
129 error_at (EXPR_LOC_OR_LOC (expr, input_location),
130 "function concept must be called");
131 return false;
132 }
133 }
134
135 return true;
136 }
137
138 static bool
check_constraint_operands(location_t,cp_expr lhs,cp_expr rhs)139 check_constraint_operands (location_t, cp_expr lhs, cp_expr rhs)
140 {
141 return check_constraint_atom (lhs) && check_constraint_atom (rhs);
142 }
143
144 /* Validate the semantic properties of the constraint expression. */
145
146 static cp_expr
finish_constraint_binary_op(location_t loc,tree_code code,cp_expr lhs,cp_expr rhs)147 finish_constraint_binary_op (location_t loc,
148 tree_code code,
149 cp_expr lhs,
150 cp_expr rhs)
151 {
152 gcc_assert (processing_constraint_expression_p ());
153 if (lhs == error_mark_node || rhs == error_mark_node)
154 return error_mark_node;
155 if (!check_constraint_operands (loc, lhs, rhs))
156 return error_mark_node;
157 tree overload;
158 cp_expr expr = build_x_binary_op (loc, code,
159 lhs, TREE_CODE (lhs),
160 rhs, TREE_CODE (rhs),
161 &overload, tf_none);
162 /* When either operand is dependent, the overload set may be non-empty. */
163 if (expr == error_mark_node)
164 return error_mark_node;
165 expr.set_location (loc);
166 expr.set_range (lhs.get_start (), rhs.get_finish ());
167 return expr;
168 }
169
170 cp_expr
finish_constraint_or_expr(location_t loc,cp_expr lhs,cp_expr rhs)171 finish_constraint_or_expr (location_t loc, cp_expr lhs, cp_expr rhs)
172 {
173 return finish_constraint_binary_op (loc, TRUTH_ORIF_EXPR, lhs, rhs);
174 }
175
176 cp_expr
finish_constraint_and_expr(location_t loc,cp_expr lhs,cp_expr rhs)177 finish_constraint_and_expr (location_t loc, cp_expr lhs, cp_expr rhs)
178 {
179 return finish_constraint_binary_op (loc, TRUTH_ANDIF_EXPR, lhs, rhs);
180 }
181
182 cp_expr
finish_constraint_primary_expr(cp_expr expr)183 finish_constraint_primary_expr (cp_expr expr)
184 {
185 if (expr == error_mark_node)
186 return error_mark_node;
187 if (!check_constraint_atom (expr))
188 return cp_expr (error_mark_node, expr.get_location ());
189 return expr;
190 }
191
192 /* Combine two constraint-expressions with a logical-and. */
193
194 tree
combine_constraint_expressions(tree lhs,tree rhs)195 combine_constraint_expressions (tree lhs, tree rhs)
196 {
197 processing_constraint_expression_sentinel pce;
198 if (!lhs)
199 return rhs;
200 if (!rhs)
201 return lhs;
202 return finish_constraint_and_expr (input_location, lhs, rhs);
203 }
204
205 /* Extract the template-id from a concept check. For standard and variable
206 checks, this is simply T. For function concept checks, this is the
207 called function. */
208
209 tree
unpack_concept_check(tree t)210 unpack_concept_check (tree t)
211 {
212 gcc_assert (concept_check_p (t));
213
214 if (TREE_CODE (t) == CALL_EXPR)
215 t = CALL_EXPR_FN (t);
216
217 gcc_assert (TREE_CODE (t) == TEMPLATE_ID_EXPR);
218 return t;
219 }
220
221 /* Extract the TEMPLATE_DECL from a concept check. */
222
223 tree
get_concept_check_template(tree t)224 get_concept_check_template (tree t)
225 {
226 tree id = unpack_concept_check (t);
227 tree tmpl = TREE_OPERAND (id, 0);
228 if (OVL_P (tmpl))
229 tmpl = OVL_FIRST (tmpl);
230 return tmpl;
231 }
232
233 /* Returns true if any of the arguments in the template argument list is
234 a wildcard or wildcard pack. */
235
236 bool
contains_wildcard_p(tree args)237 contains_wildcard_p (tree args)
238 {
239 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
240 {
241 tree arg = TREE_VEC_ELT (args, i);
242 if (TREE_CODE (arg) == WILDCARD_DECL)
243 return true;
244 }
245 return false;
246 }
247
248 /*---------------------------------------------------------------------------
249 Resolution of qualified concept names
250 ---------------------------------------------------------------------------*/
251
252 /* This facility is used to resolve constraint checks from requirement
253 expressions. A constraint check is a call to a function template declared
254 with the keyword 'concept'.
255
256 The result of resolution is a pair (a TREE_LIST) whose value is the
257 matched declaration, and whose purpose contains the coerced template
258 arguments that can be substituted into the call. */
259
260 /* Given an overload set OVL, try to find a unique definition that can be
261 instantiated by the template arguments ARGS.
262
263 This function is not called for arbitrary call expressions. In particular,
264 the call expression must be written with explicit template arguments
265 and no function arguments. For example:
266
267 f<T, U>()
268
269 If a single match is found, this returns a TREE_LIST whose VALUE
270 is the constraint function (not the template), and its PURPOSE is
271 the complete set of arguments substituted into the parameter list. */
272
273 static tree
resolve_function_concept_overload(tree ovl,tree args)274 resolve_function_concept_overload (tree ovl, tree args)
275 {
276 int nerrs = 0;
277 tree cands = NULL_TREE;
278 for (lkp_iterator iter (ovl); iter; ++iter)
279 {
280 tree tmpl = *iter;
281 if (TREE_CODE (tmpl) != TEMPLATE_DECL)
282 continue;
283
284 /* Don't try to deduce checks for non-concepts. We often end up trying
285 to resolve constraints in functional casts as part of a
286 postfix-expression. We can save time and headaches by not
287 instantiating those declarations.
288
289 NOTE: This masks a potential error, caused by instantiating
290 non-deduced contexts using placeholder arguments. */
291 tree fn = DECL_TEMPLATE_RESULT (tmpl);
292 if (DECL_ARGUMENTS (fn))
293 continue;
294 if (!DECL_DECLARED_CONCEPT_P (fn))
295 continue;
296
297 /* Remember the candidate if we can deduce a substitution. */
298 ++processing_template_decl;
299 tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl));
300 if (tree subst = coerce_template_parms (parms, args, tmpl))
301 {
302 if (subst == error_mark_node)
303 ++nerrs;
304 else
305 cands = tree_cons (subst, fn, cands);
306 }
307 --processing_template_decl;
308 }
309
310 if (!cands)
311 /* We either had no candidates or failed deductions. */
312 return nerrs ? error_mark_node : NULL_TREE;
313 else if (TREE_CHAIN (cands))
314 /* There are multiple candidates. */
315 return error_mark_node;
316
317 return cands;
318 }
319
320 /* Determine if the call expression CALL is a constraint check, and
321 return the concept declaration and arguments being checked. If CALL
322 does not denote a constraint check, return NULL. */
323
324 tree
resolve_function_concept_check(tree call)325 resolve_function_concept_check (tree call)
326 {
327 gcc_assert (TREE_CODE (call) == CALL_EXPR);
328
329 /* A constraint check must be only a template-id expression.
330 If it's a call to a base-link, its function(s) should be a
331 template-id expression. If this is not a template-id, then
332 it cannot be a concept-check. */
333 tree target = CALL_EXPR_FN (call);
334 if (BASELINK_P (target))
335 target = BASELINK_FUNCTIONS (target);
336 if (TREE_CODE (target) != TEMPLATE_ID_EXPR)
337 return NULL_TREE;
338
339 /* Get the overload set and template arguments and try to
340 resolve the target. */
341 tree ovl = TREE_OPERAND (target, 0);
342
343 /* This is a function call of a variable concept... ill-formed. */
344 if (TREE_CODE (ovl) == TEMPLATE_DECL)
345 {
346 error_at (location_of (call),
347 "function call of variable concept %qE", call);
348 return error_mark_node;
349 }
350
351 tree args = TREE_OPERAND (target, 1);
352 return resolve_function_concept_overload (ovl, args);
353 }
354
355 /* Returns a pair containing the checked concept and its associated
356 prototype parameter. The result is a TREE_LIST whose TREE_VALUE
357 is the concept (non-template) and whose TREE_PURPOSE contains
358 the converted template arguments, including the deduced prototype
359 parameter (in position 0). */
360
361 tree
resolve_concept_check(tree check)362 resolve_concept_check (tree check)
363 {
364 gcc_assert (concept_check_p (check));
365 tree id = unpack_concept_check (check);
366 tree tmpl = TREE_OPERAND (id, 0);
367
368 /* If this is an overloaded function concept, perform overload
369 resolution (this only happens when deducing prototype parameters
370 and template introductions). */
371 if (TREE_CODE (tmpl) == OVERLOAD)
372 {
373 if (OVL_CHAIN (tmpl))
374 return resolve_function_concept_check (check);
375 tmpl = OVL_FIRST (tmpl);
376 }
377
378 tree args = TREE_OPERAND (id, 1);
379 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
380 ++processing_template_decl;
381 tree result = coerce_template_parms (parms, args, tmpl);
382 --processing_template_decl;
383 if (result == error_mark_node)
384 return error_mark_node;
385 return build_tree_list (result, DECL_TEMPLATE_RESULT (tmpl));
386 }
387
388 /* Given a call expression or template-id expression to a concept EXPR
389 possibly including a wildcard, deduce the concept being checked and
390 the prototype parameter. Returns true if the constraint and prototype
391 can be deduced and false otherwise. Note that the CHECK and PROTO
392 arguments are set to NULL_TREE if this returns false. */
393
394 bool
deduce_constrained_parameter(tree expr,tree & check,tree & proto)395 deduce_constrained_parameter (tree expr, tree& check, tree& proto)
396 {
397 tree info = resolve_concept_check (expr);
398 if (info && info != error_mark_node)
399 {
400 check = TREE_VALUE (info);
401 tree arg = TREE_VEC_ELT (TREE_PURPOSE (info), 0);
402 if (ARGUMENT_PACK_P (arg))
403 arg = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0);
404 proto = TREE_TYPE (arg);
405 return true;
406 }
407
408 check = proto = NULL_TREE;
409 return false;
410 }
411
412 /* Given a call expression or template-id expression to a concept, EXPR,
413 deduce the concept being checked and return the template arguments.
414 Returns NULL_TREE if deduction fails. */
415 static tree
deduce_concept_introduction(tree check)416 deduce_concept_introduction (tree check)
417 {
418 tree info = resolve_concept_check (check);
419 if (info && info != error_mark_node)
420 return TREE_PURPOSE (info);
421 return NULL_TREE;
422 }
423
424 /* Build a constrained placeholder type where SPEC is a type-constraint.
425 SPEC can be anything were concept_definition_p is true.
426
427 If DECLTYPE_P is true, then the placeholder is decltype(auto).
428
429 Returns a pair whose FIRST is the concept being checked and whose
430 SECOND is the prototype parameter. */
431
432 tree_pair
finish_type_constraints(tree spec,tree args,tsubst_flags_t complain)433 finish_type_constraints (tree spec, tree args, tsubst_flags_t complain)
434 {
435 gcc_assert (concept_definition_p (spec));
436
437 /* Build an initial concept check. */
438 tree check = build_type_constraint (spec, args, complain);
439 if (check == error_mark_node)
440 return std::make_pair (error_mark_node, NULL_TREE);
441
442 /* Extract the concept and prototype parameter from the check. */
443 tree con;
444 tree proto;
445 if (!deduce_constrained_parameter (check, con, proto))
446 return std::make_pair (error_mark_node, NULL_TREE);
447
448 return std::make_pair (con, proto);
449 }
450
451 /*---------------------------------------------------------------------------
452 Expansion of concept definitions
453 ---------------------------------------------------------------------------*/
454
455 /* Returns the expression of a function concept. */
456
457 static tree
get_returned_expression(tree fn)458 get_returned_expression (tree fn)
459 {
460 /* Extract the body of the function minus the return expression. */
461 tree body = DECL_SAVED_TREE (fn);
462 if (!body)
463 return error_mark_node;
464 if (TREE_CODE (body) == BIND_EXPR)
465 body = BIND_EXPR_BODY (body);
466 if (TREE_CODE (body) != RETURN_EXPR)
467 return error_mark_node;
468
469 return TREE_OPERAND (body, 0);
470 }
471
472 /* Returns the initializer of a variable concept. */
473
474 static tree
get_variable_initializer(tree var)475 get_variable_initializer (tree var)
476 {
477 tree init = DECL_INITIAL (var);
478 if (!init)
479 return error_mark_node;
480 if (BRACE_ENCLOSED_INITIALIZER_P (init)
481 && CONSTRUCTOR_NELTS (init) == 1)
482 init = CONSTRUCTOR_ELT (init, 0)->value;
483 return init;
484 }
485
486 /* Returns the definition of a variable or function concept. */
487
488 static tree
get_concept_definition(tree decl)489 get_concept_definition (tree decl)
490 {
491 if (TREE_CODE (decl) == OVERLOAD)
492 decl = OVL_FIRST (decl);
493
494 if (TREE_CODE (decl) == TEMPLATE_DECL)
495 decl = DECL_TEMPLATE_RESULT (decl);
496
497 if (TREE_CODE (decl) == CONCEPT_DECL)
498 return DECL_INITIAL (decl);
499 if (VAR_P (decl))
500 return get_variable_initializer (decl);
501 if (TREE_CODE (decl) == FUNCTION_DECL)
502 return get_returned_expression (decl);
503 gcc_unreachable ();
504 }
505
506 /*---------------------------------------------------------------------------
507 Normalization of expressions
508
509 This set of functions will transform an expression into a constraint
510 in a sequence of steps.
511 ---------------------------------------------------------------------------*/
512
513 void
debug_parameter_mapping(tree map)514 debug_parameter_mapping (tree map)
515 {
516 for (tree p = map; p; p = TREE_CHAIN (p))
517 {
518 tree parm = TREE_VALUE (p);
519 tree arg = TREE_PURPOSE (p);
520 if (TYPE_P (parm))
521 verbatim ("MAP %qD TO %qT", TEMPLATE_TYPE_DECL (parm), arg);
522 else
523 verbatim ("MAP %qD TO %qE", TEMPLATE_PARM_DECL (parm), arg);
524 // debug_tree (parm);
525 // debug_tree (arg);
526 }
527 }
528
529 void
debug_argument_list(tree args)530 debug_argument_list (tree args)
531 {
532 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
533 {
534 tree arg = TREE_VEC_ELT (args, i);
535 if (TYPE_P (arg))
536 verbatim ("ARG %qT", arg);
537 else
538 verbatim ("ARG %qE", arg);
539 }
540 }
541
542 /* Associate each parameter in PARMS with its corresponding template
543 argument in ARGS. */
544
545 static tree
map_arguments(tree parms,tree args)546 map_arguments (tree parms, tree args)
547 {
548 for (tree p = parms; p; p = TREE_CHAIN (p))
549 if (args)
550 {
551 int level;
552 int index;
553 template_parm_level_and_index (TREE_VALUE (p), &level, &index);
554 TREE_PURPOSE (p) = TMPL_ARG (args, level, index);
555 }
556 else
557 TREE_PURPOSE (p) = template_parm_to_arg (p);
558
559 return parms;
560 }
561
562 /* Build the parameter mapping for EXPR using ARGS. */
563
564 static tree
build_parameter_mapping(tree expr,tree args,tree decl)565 build_parameter_mapping (tree expr, tree args, tree decl)
566 {
567 tree ctx_parms = NULL_TREE;
568 if (decl)
569 {
570 gcc_assert (TREE_CODE (decl) == TEMPLATE_DECL);
571 ctx_parms = DECL_TEMPLATE_PARMS (decl);
572 }
573 else if (current_template_parms)
574 {
575 /* TODO: This should probably be the only case, but because the
576 point of declaration of concepts is currently set after the
577 initializer, the template parameter lists are not available
578 when normalizing concept definitions, hence the case above. */
579 ctx_parms = current_template_parms;
580 }
581
582 tree parms = find_template_parameters (expr, ctx_parms);
583 tree map = map_arguments (parms, args);
584 return map;
585 }
586
587 /* True if the parameter mappings of two atomic constraints are equivalent. */
588
589 static bool
parameter_mapping_equivalent_p(tree t1,tree t2)590 parameter_mapping_equivalent_p (tree t1, tree t2)
591 {
592 tree map1 = ATOMIC_CONSTR_MAP (t1);
593 tree map2 = ATOMIC_CONSTR_MAP (t2);
594 while (map1 && map2)
595 {
596 tree arg1 = TREE_PURPOSE (map1);
597 tree arg2 = TREE_PURPOSE (map2);
598 if (!template_args_equal (arg1, arg2))
599 return false;
600 map1 = TREE_CHAIN (map1);
601 map2 = TREE_CHAIN (map2);
602 }
603 return true;
604 }
605
606 /* Provides additional context for normalization. */
607
608 struct norm_info : subst_info
609 {
norm_infonorm_info610 explicit norm_info (tsubst_flags_t complain)
611 : subst_info (tf_warning_or_error | complain, NULL_TREE),
612 context()
613 {}
614
615 /* Construct a top-level context for DECL. */
616
norm_infonorm_info617 norm_info (tree in_decl, tsubst_flags_t complain)
618 : subst_info (tf_warning_or_error | complain, in_decl),
619 context (make_context (in_decl))
620 {}
621
generate_diagnosticsnorm_info622 bool generate_diagnostics() const
623 {
624 return complain & tf_norm;
625 }
626
make_contextnorm_info627 tree make_context(tree in_decl)
628 {
629 if (generate_diagnostics ())
630 return build_tree_list (NULL_TREE, in_decl);
631 return NULL_TREE;
632 }
633
update_contextnorm_info634 void update_context(tree expr, tree args)
635 {
636 if (generate_diagnostics ())
637 {
638 tree map = build_parameter_mapping (expr, args, in_decl);
639 context = tree_cons (map, expr, context);
640 }
641 in_decl = get_concept_check_template (expr);
642 }
643
644 /* Provides information about the source of a constraint. This is a
645 TREE_LIST whose VALUE is either a concept check or a constrained
646 declaration. The PURPOSE, for concept checks is a parameter mapping
647 for that check. */
648
649 tree context;
650 };
651
652 static tree normalize_expression (tree, tree, norm_info);
653
654 /* Transform a logical-or or logical-and expression into either
655 a conjunction or disjunction. */
656
657 static tree
normalize_logical_operation(tree t,tree args,tree_code c,norm_info info)658 normalize_logical_operation (tree t, tree args, tree_code c, norm_info info)
659 {
660 tree t0 = normalize_expression (TREE_OPERAND (t, 0), args, info);
661 tree t1 = normalize_expression (TREE_OPERAND (t, 1), args, info);
662
663 /* Build a new info object for the constraint. */
664 tree ci = info.generate_diagnostics()
665 ? build_tree_list (t, info.context)
666 : NULL_TREE;
667
668 return build2 (c, ci, t0, t1);
669 }
670
671 static tree
normalize_concept_check(tree check,tree args,norm_info info)672 normalize_concept_check (tree check, tree args, norm_info info)
673 {
674 tree id = unpack_concept_check (check);
675 tree tmpl = TREE_OPERAND (id, 0);
676 tree targs = TREE_OPERAND (id, 1);
677
678 /* A function concept is wrapped in an overload. */
679 if (TREE_CODE (tmpl) == OVERLOAD)
680 {
681 /* TODO: Can we diagnose this error during parsing? */
682 if (TREE_CODE (check) == TEMPLATE_ID_EXPR)
683 error_at (EXPR_LOC_OR_LOC (check, input_location),
684 "function concept must be called");
685 tmpl = OVL_FIRST (tmpl);
686 }
687
688 /* Substitute through the arguments of the concept check. */
689 if (args)
690 targs = tsubst_template_args (targs, args, info.complain, info.in_decl);
691 if (targs == error_mark_node)
692 return error_mark_node;
693
694 /* Build the substitution for the concept definition. */
695 tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl));
696 /* Turn on template processing; coercing non-type template arguments
697 will automatically assume they're non-dependent. */
698 ++processing_template_decl;
699 tree subst = coerce_template_parms (parms, targs, tmpl);
700 --processing_template_decl;
701 if (subst == error_mark_node)
702 return error_mark_node;
703
704 /* The concept may have been ill-formed. */
705 tree def = get_concept_definition (DECL_TEMPLATE_RESULT (tmpl));
706 if (def == error_mark_node)
707 return error_mark_node;
708
709 info.update_context (check, args);
710 return normalize_expression (def, subst, info);
711 }
712
713 /* The normal form of an atom depends on the expression. The normal
714 form of a function call to a function concept is a check constraint
715 for that concept. The normal form of a reference to a variable
716 concept is a check constraint for that concept. Otherwise, the
717 constraint is a predicate constraint. */
718
719 static tree
normalize_atom(tree t,tree args,norm_info info)720 normalize_atom (tree t, tree args, norm_info info)
721 {
722 /* Concept checks are not atomic. */
723 if (concept_check_p (t))
724 return normalize_concept_check (t, args, info);
725
726 /* Build the parameter mapping for the atom. */
727 tree map = build_parameter_mapping (t, args, info.in_decl);
728
729 /* Build a new info object for the atom. */
730 tree ci = build_tree_list (t, info.context);
731
732 return build1 (ATOMIC_CONSTR, ci, map);
733 }
734
735 /* Returns the normal form of an expression. */
736
737 static tree
normalize_expression(tree t,tree args,norm_info info)738 normalize_expression (tree t, tree args, norm_info info)
739 {
740 if (!t)
741 return NULL_TREE;
742
743 if (t == error_mark_node)
744 return error_mark_node;
745
746 switch (TREE_CODE (t))
747 {
748 case TRUTH_ANDIF_EXPR:
749 return normalize_logical_operation (t, args, CONJ_CONSTR, info);
750 case TRUTH_ORIF_EXPR:
751 return normalize_logical_operation (t, args, DISJ_CONSTR, info);
752 default:
753 return normalize_atom (t, args, info);
754 }
755 }
756
757 /* Cache of the normalized form of constraints. Marked as deletable because it
758 can all be recalculated. */
759 static GTY((deletable)) hash_map<tree,tree> *normalized_map;
760
761 static tree
get_normalized_constraints(tree t,tree args,norm_info info)762 get_normalized_constraints (tree t, tree args, norm_info info)
763 {
764 auto_timevar time (TV_CONSTRAINT_NORM);
765 return normalize_expression (t, args, info);
766 }
767
768 /* Returns the normalized constraints from a constraint-info object
769 or NULL_TREE if the constraints are null. ARGS provide the initial
770 arguments for normalization and IN_DECL provides the declaration
771 to which the constraints belong. */
772
773 static tree
get_normalized_constraints_from_info(tree ci,tree args,tree in_decl,bool diag=false)774 get_normalized_constraints_from_info (tree ci, tree args, tree in_decl,
775 bool diag = false)
776 {
777 if (ci == NULL_TREE)
778 return NULL_TREE;
779
780 /* Substitution errors during normalization are fatal. */
781 ++processing_template_decl;
782 norm_info info (in_decl, diag ? tf_norm : tf_none);
783 tree t = get_normalized_constraints (CI_ASSOCIATED_CONSTRAINTS (ci),
784 args, info);
785 --processing_template_decl;
786
787 return t;
788 }
789
790 /* Returns the normalized constraints for the declaration D. */
791
792 static tree
get_normalized_constraints_from_decl(tree d,bool diag=false)793 get_normalized_constraints_from_decl (tree d, bool diag = false)
794 {
795 tree tmpl;
796 tree decl;
797
798 /* For inherited constructors, consider the original declaration;
799 it has the correct template information attached. */
800 d = strip_inheriting_ctors (d);
801
802 if (TREE_CODE (d) == TEMPLATE_DECL)
803 {
804 tmpl = d;
805 decl = DECL_TEMPLATE_RESULT (tmpl);
806 }
807 else
808 {
809 if (tree ti = DECL_TEMPLATE_INFO (d))
810 tmpl = TI_TEMPLATE (ti);
811 else
812 tmpl = NULL_TREE;
813 decl = d;
814 }
815
816 /* Get the most general template for the declaration, and compute
817 arguments from that. This ensures that the arguments used for
818 normalization are always template parameters and not arguments
819 used for outer specializations. For example:
820
821 template<typename T>
822 struct S {
823 template<typename U> requires C<T, U> void f(U);
824 };
825
826 S<int>::f(0);
827
828 When we normalize the requirements for S<int>::f, we want the
829 arguments to be {T, U}, not {int, U}. One reason for this is that
830 accepting the latter causes the template parameter level of U
831 to be reduced in a way that makes it overly difficult substitute
832 concrete arguments (i.e., eventually {int, int} during satisfaction. */
833 if (tmpl)
834 {
835 if (DECL_LANG_SPECIFIC(tmpl) && !DECL_TEMPLATE_SPECIALIZATION (tmpl))
836 tmpl = most_general_template (tmpl);
837 }
838
839 /* If we're not diagnosing errors, use cached constraints, if any. */
840 if (!diag)
841 if (tree *p = hash_map_safe_get (normalized_map, tmpl))
842 return *p;
843
844 push_nested_class_guard pncs (DECL_CONTEXT (d));
845
846 tree args = generic_targs_for (tmpl);
847 tree ci = get_constraints (decl);
848 tree norm = get_normalized_constraints_from_info (ci, args, tmpl, diag);
849
850 if (!diag)
851 hash_map_safe_put<hm_ggc> (normalized_map, tmpl, norm);
852
853 return norm;
854 }
855
856 /* Returns the normal form of TMPL's definition. */
857
858 static tree
normalize_concept_definition(tree tmpl,bool diag=false)859 normalize_concept_definition (tree tmpl, bool diag = false)
860 {
861 if (!diag)
862 if (tree *p = hash_map_safe_get (normalized_map, tmpl))
863 return *p;
864
865 gcc_assert (concept_definition_p (tmpl));
866 if (OVL_P (tmpl))
867 tmpl = OVL_FIRST (tmpl);
868 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
869 tree args = generic_targs_for (tmpl);
870 tree def = get_concept_definition (DECL_TEMPLATE_RESULT (tmpl));
871 ++processing_template_decl;
872 norm_info info (tmpl, diag ? tf_norm : tf_none);
873 tree norm = get_normalized_constraints (def, args, info);
874 --processing_template_decl;
875
876 if (!diag)
877 hash_map_safe_put<hm_ggc> (normalized_map, tmpl, norm);
878
879 return norm;
880 }
881
882 /* Returns the normal form of TMPL's requirements. */
883
884 static tree
normalize_template_requirements(tree tmpl,bool diag=false)885 normalize_template_requirements (tree tmpl, bool diag = false)
886 {
887 return get_normalized_constraints_from_decl (tmpl, diag);
888 }
889
890 /* Returns the normal form of TMPL's requirements. */
891
892 static tree
normalize_nontemplate_requirements(tree decl,bool diag=false)893 normalize_nontemplate_requirements (tree decl, bool diag = false)
894 {
895 return get_normalized_constraints_from_decl (decl, diag);
896 }
897
898 /* Normalize an EXPR as a constraint using ARGS. */
899
900 static tree
normalize_constraint_expression(tree expr,tree args,bool diag=false)901 normalize_constraint_expression (tree expr, tree args, bool diag = false)
902 {
903 if (!expr || expr == error_mark_node)
904 return expr;
905 ++processing_template_decl;
906 norm_info info (diag ? tf_norm : tf_none);
907 tree norm = get_normalized_constraints (expr, args, info);
908 --processing_template_decl;
909 return norm;
910 }
911
912 /* Normalize an EXPR as a constraint. */
913
914 static tree
normalize_constraint_expression(tree expr,bool diag=false)915 normalize_constraint_expression (tree expr, bool diag = false)
916 {
917 if (!expr || expr == error_mark_node)
918 return expr;
919
920 /* For concept checks, use the supplied template arguments as those used
921 for normalization. Otherwise, there are no template arguments. */
922 tree args;
923 if (concept_check_p (expr))
924 {
925 tree id = unpack_concept_check (expr);
926 args = TREE_OPERAND (id, 1);
927 }
928 else
929 args = NULL_TREE;
930
931 return normalize_constraint_expression (expr, args, diag);
932 }
933
934 /* 17.4.1.2p2. Two constraints are identical if they are formed
935 from the same expression and the targets of the parameter mapping
936 are equivalent. */
937
938 bool
atomic_constraints_identical_p(tree t1,tree t2)939 atomic_constraints_identical_p (tree t1, tree t2)
940 {
941 gcc_assert (TREE_CODE (t1) == ATOMIC_CONSTR);
942 gcc_assert (TREE_CODE (t2) == ATOMIC_CONSTR);
943
944 if (ATOMIC_CONSTR_EXPR (t1) != ATOMIC_CONSTR_EXPR (t2))
945 return false;
946
947 if (!parameter_mapping_equivalent_p (t1, t2))
948 return false;
949
950 return true;
951 }
952
953 /* True if T1 and T2 are equivalent, meaning they have the same syntactic
954 structure and all corresponding constraints are identical. */
955
956 bool
constraints_equivalent_p(tree t1,tree t2)957 constraints_equivalent_p (tree t1, tree t2)
958 {
959 gcc_assert (CONSTR_P (t1));
960 gcc_assert (CONSTR_P (t2));
961
962 if (TREE_CODE (t1) != TREE_CODE (t2))
963 return false;
964
965 switch (TREE_CODE (t1))
966 {
967 case CONJ_CONSTR:
968 case DISJ_CONSTR:
969 if (!constraints_equivalent_p (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
970 return false;
971 if (!constraints_equivalent_p (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)))
972 return false;
973 break;
974 case ATOMIC_CONSTR:
975 if (!atomic_constraints_identical_p(t1, t2))
976 return false;
977 break;
978 default:
979 gcc_unreachable ();
980 }
981 return true;
982 }
983
984 /* Compute the hash value for T. */
985
986 hashval_t
hash_atomic_constraint(tree t)987 hash_atomic_constraint (tree t)
988 {
989 gcc_assert (TREE_CODE (t) == ATOMIC_CONSTR);
990
991 /* Hash the identity of the expression. */
992 hashval_t val = htab_hash_pointer (ATOMIC_CONSTR_EXPR (t));
993
994 /* Hash the targets of the parameter map. */
995 tree p = ATOMIC_CONSTR_MAP (t);
996 while (p)
997 {
998 val = iterative_hash_template_arg (TREE_PURPOSE (p), val);
999 p = TREE_CHAIN (p);
1000 }
1001
1002 return val;
1003 }
1004
1005 namespace inchash
1006 {
1007
1008 static void
add_constraint(tree t,hash & h)1009 add_constraint (tree t, hash& h)
1010 {
1011 h.add_int(TREE_CODE (t));
1012 switch (TREE_CODE (t))
1013 {
1014 case CONJ_CONSTR:
1015 case DISJ_CONSTR:
1016 add_constraint (TREE_OPERAND (t, 0), h);
1017 add_constraint (TREE_OPERAND (t, 1), h);
1018 break;
1019 case ATOMIC_CONSTR:
1020 h.merge_hash (hash_atomic_constraint (t));
1021 break;
1022 default:
1023 gcc_unreachable ();
1024 }
1025 }
1026
1027 }
1028
1029 /* Computes a hash code for the constraint T. */
1030
1031 hashval_t
iterative_hash_constraint(tree t,hashval_t val)1032 iterative_hash_constraint (tree t, hashval_t val)
1033 {
1034 gcc_assert (CONSTR_P (t));
1035 inchash::hash h (val);
1036 inchash::add_constraint (t, h);
1037 return h.end ();
1038 }
1039
1040 // -------------------------------------------------------------------------- //
1041 // Constraint Semantic Processing
1042 //
1043 // The following functions are called by the parser and substitution rules
1044 // to create and evaluate constraint-related nodes.
1045
1046 // The constraints associated with the current template parameters.
1047 tree
current_template_constraints(void)1048 current_template_constraints (void)
1049 {
1050 if (!current_template_parms)
1051 return NULL_TREE;
1052 tree tmpl_constr = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
1053 return build_constraints (tmpl_constr, NULL_TREE);
1054 }
1055
1056 /* If the recently parsed TYPE declares or defines a template or
1057 template specialization, get its corresponding constraints from the
1058 current template parameters and bind them to TYPE's declaration. */
1059
1060 tree
associate_classtype_constraints(tree type)1061 associate_classtype_constraints (tree type)
1062 {
1063 if (!type || type == error_mark_node || !CLASS_TYPE_P (type))
1064 return type;
1065
1066 /* An explicit class template specialization has no template parameters. */
1067 if (!current_template_parms)
1068 return type;
1069
1070 if (CLASSTYPE_IS_TEMPLATE (type) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1071 {
1072 tree decl = TYPE_STUB_DECL (type);
1073 tree ci = current_template_constraints ();
1074
1075 /* An implicitly instantiated member template declaration already
1076 has associated constraints. If it is defined outside of its
1077 class, then we need match these constraints against those of
1078 original declaration. */
1079 if (tree orig_ci = get_constraints (decl))
1080 {
1081 if (int extra_levels = (TMPL_PARMS_DEPTH (current_template_parms)
1082 - TMPL_ARGS_DEPTH (TYPE_TI_ARGS (type))))
1083 {
1084 /* If there is a discrepancy between the current template depth
1085 and the template depth of the original declaration, then we
1086 must be redeclaring a class template as part of a friend
1087 declaration within another class template. Before matching
1088 constraints, we need to reduce the template parameter level
1089 within the current constraints via substitution. */
1090 tree outer_gtargs = template_parms_to_args (current_template_parms);
1091 TREE_VEC_LENGTH (outer_gtargs) = extra_levels;
1092 ci = tsubst_constraint_info (ci, outer_gtargs, tf_none, NULL_TREE);
1093 }
1094 if (!equivalent_constraints (ci, orig_ci))
1095 {
1096 error ("%qT does not match original declaration", type);
1097 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1098 location_t loc = DECL_SOURCE_LOCATION (tmpl);
1099 inform (loc, "original template declaration here");
1100 /* Fall through, so that we define the type anyway. */
1101 }
1102 return type;
1103 }
1104 set_constraints (decl, ci);
1105 }
1106 return type;
1107 }
1108
1109 /* Create an empty constraint info block. */
1110
1111 static inline tree_constraint_info*
build_constraint_info()1112 build_constraint_info ()
1113 {
1114 return (tree_constraint_info *)make_node (CONSTRAINT_INFO);
1115 }
1116
1117 /* Build a constraint-info object that contains the associated constraints
1118 of a declaration. This also includes the declaration's template
1119 requirements (TREQS) and any trailing requirements for a function
1120 declarator (DREQS). Note that both TREQS and DREQS must be constraints.
1121
1122 If the declaration has neither template nor declaration requirements
1123 this returns NULL_TREE, indicating an unconstrained declaration. */
1124
1125 tree
build_constraints(tree tr,tree dr)1126 build_constraints (tree tr, tree dr)
1127 {
1128 if (!tr && !dr)
1129 return NULL_TREE;
1130
1131 tree_constraint_info* ci = build_constraint_info ();
1132 ci->template_reqs = tr;
1133 ci->declarator_reqs = dr;
1134 ci->associated_constr = combine_constraint_expressions (tr, dr);
1135
1136 return (tree)ci;
1137 }
1138
1139 /* Add constraint RHS to the end of CONSTRAINT_INFO ci. */
1140
1141 tree
append_constraint(tree ci,tree rhs)1142 append_constraint (tree ci, tree rhs)
1143 {
1144 tree tr = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
1145 tree dr = ci ? CI_DECLARATOR_REQS (ci) : NULL_TREE;
1146 dr = combine_constraint_expressions (dr, rhs);
1147 if (ci)
1148 {
1149 CI_DECLARATOR_REQS (ci) = dr;
1150 tree ac = combine_constraint_expressions (tr, dr);
1151 CI_ASSOCIATED_CONSTRAINTS (ci) = ac;
1152 }
1153 else
1154 ci = build_constraints (tr, dr);
1155 return ci;
1156 }
1157
1158 /* A mapping from declarations to constraint information. */
1159
1160 static GTY ((cache)) decl_tree_cache_map *decl_constraints;
1161
1162 /* Returns the template constraints of declaration T. If T is not
1163 constrained, return NULL_TREE. Note that T must be non-null. */
1164
1165 tree
get_constraints(const_tree t)1166 get_constraints (const_tree t)
1167 {
1168 if (!flag_concepts)
1169 return NULL_TREE;
1170 if (!decl_constraints)
1171 return NULL_TREE;
1172
1173 gcc_assert (DECL_P (t));
1174 if (TREE_CODE (t) == TEMPLATE_DECL)
1175 t = DECL_TEMPLATE_RESULT (t);
1176 tree* found = decl_constraints->get (CONST_CAST_TREE (t));
1177 if (found)
1178 return *found;
1179 else
1180 return NULL_TREE;
1181 }
1182
1183 /* Associate the given constraint information CI with the declaration
1184 T. If T is a template, then the constraints are associated with
1185 its underlying declaration. Don't build associations if CI is
1186 NULL_TREE. */
1187
1188 void
set_constraints(tree t,tree ci)1189 set_constraints (tree t, tree ci)
1190 {
1191 if (!ci)
1192 return;
1193 gcc_assert (t && flag_concepts);
1194 if (TREE_CODE (t) == TEMPLATE_DECL)
1195 t = DECL_TEMPLATE_RESULT (t);
1196 bool found = hash_map_safe_put<hm_ggc> (decl_constraints, t, ci);
1197 gcc_assert (!found);
1198 }
1199
1200 /* Remove the associated constraints of the declaration T. */
1201
1202 void
remove_constraints(tree t)1203 remove_constraints (tree t)
1204 {
1205 gcc_assert (DECL_P (t));
1206 if (TREE_CODE (t) == TEMPLATE_DECL)
1207 t = DECL_TEMPLATE_RESULT (t);
1208
1209 if (decl_constraints)
1210 decl_constraints->remove (t);
1211 }
1212
1213 /* If DECL is a friend, substitute into REQS to produce requirements suitable
1214 for declaration matching. */
1215
1216 tree
maybe_substitute_reqs_for(tree reqs,const_tree decl_)1217 maybe_substitute_reqs_for (tree reqs, const_tree decl_)
1218 {
1219 if (reqs == NULL_TREE)
1220 return NULL_TREE;
1221 tree decl = CONST_CAST_TREE (decl_);
1222 tree result = STRIP_TEMPLATE (decl);
1223 if (DECL_FRIEND_P (result))
1224 {
1225 tree tmpl = decl == result ? DECL_TI_TEMPLATE (result) : decl;
1226 tree gargs = generic_targs_for (tmpl);
1227 processing_template_decl_sentinel s;
1228 if (uses_template_parms (gargs))
1229 ++processing_template_decl;
1230 reqs = tsubst_constraint (reqs, gargs,
1231 tf_warning_or_error, NULL_TREE);
1232 }
1233 return reqs;
1234 }
1235
1236 /* Returns the template-head requires clause for the template
1237 declaration T or NULL_TREE if none. */
1238
1239 tree
get_template_head_requirements(tree t)1240 get_template_head_requirements (tree t)
1241 {
1242 tree ci = get_constraints (t);
1243 if (!ci)
1244 return NULL_TREE;
1245 return CI_TEMPLATE_REQS (ci);
1246 }
1247
1248 /* Returns the trailing requires clause of the declarator of
1249 a template declaration T or NULL_TREE if none. */
1250
1251 tree
get_trailing_function_requirements(tree t)1252 get_trailing_function_requirements (tree t)
1253 {
1254 tree ci = get_constraints (t);
1255 if (!ci)
1256 return NULL_TREE;
1257 return CI_DECLARATOR_REQS (ci);
1258 }
1259
1260 /* Construct a sequence of template arguments by prepending
1261 ARG to REST. Either ARG or REST may be null. */
1262 static tree
build_concept_check_arguments(tree arg,tree rest)1263 build_concept_check_arguments (tree arg, tree rest)
1264 {
1265 gcc_assert (rest ? TREE_CODE (rest) == TREE_VEC : true);
1266 tree args;
1267 if (arg)
1268 {
1269 int n = rest ? TREE_VEC_LENGTH (rest) : 0;
1270 args = make_tree_vec (n + 1);
1271 TREE_VEC_ELT (args, 0) = arg;
1272 if (rest)
1273 for (int i = 0; i < n; ++i)
1274 TREE_VEC_ELT (args, i + 1) = TREE_VEC_ELT (rest, i);
1275 int def = rest ? GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (rest) : 0;
1276 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, def + 1);
1277 }
1278 else
1279 {
1280 args = rest;
1281 }
1282 return args;
1283 }
1284
1285 /* Builds an id-expression of the form `C<Args...>()` where C is a function
1286 concept. */
1287
1288 static tree
build_function_check(tree tmpl,tree args,tsubst_flags_t)1289 build_function_check (tree tmpl, tree args, tsubst_flags_t /*complain*/)
1290 {
1291 if (TREE_CODE (tmpl) == TEMPLATE_DECL)
1292 {
1293 /* If we just got a template, wrap it in an overload so it looks like any
1294 other template-id. */
1295 tmpl = ovl_make (tmpl);
1296 TREE_TYPE (tmpl) = boolean_type_node;
1297 }
1298
1299 /* Perform function concept resolution now so we always have a single
1300 function of the overload set (even if we started with only one; the
1301 resolution function converts template arguments). Note that we still
1302 wrap this in an overload set so we don't upset other parts of the
1303 compiler that expect template-ids referring to function concepts
1304 to have an overload set. */
1305 tree info = resolve_function_concept_overload (tmpl, args);
1306 if (info == error_mark_node)
1307 return error_mark_node;
1308 if (!info)
1309 {
1310 error ("no matching concepts for %qE", tmpl);
1311 return error_mark_node;
1312 }
1313 args = TREE_PURPOSE (info);
1314 tmpl = DECL_TI_TEMPLATE (TREE_VALUE (info));
1315
1316 /* Rebuild the singleton overload set; mark the type bool. */
1317 tmpl = ovl_make (tmpl, NULL_TREE);
1318 TREE_TYPE (tmpl) = boolean_type_node;
1319
1320 /* Build the id-expression around the overload set. */
1321 tree id = build2 (TEMPLATE_ID_EXPR, boolean_type_node, tmpl, args);
1322
1323 /* Finally, build the call expression around the overload. */
1324 ++processing_template_decl;
1325 vec<tree, va_gc> *fargs = make_tree_vector ();
1326 tree call = build_min_nt_call_vec (id, fargs);
1327 TREE_TYPE (call) = boolean_type_node;
1328 release_tree_vector (fargs);
1329 --processing_template_decl;
1330
1331 return call;
1332 }
1333
1334 /* Builds an id-expression of the form `C<Args...>` where C is a variable
1335 concept. */
1336
1337 static tree
build_variable_check(tree tmpl,tree args,tsubst_flags_t complain)1338 build_variable_check (tree tmpl, tree args, tsubst_flags_t complain)
1339 {
1340 gcc_assert (variable_concept_p (tmpl));
1341 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1342 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
1343 args = coerce_template_parms (parms, args, tmpl, complain);
1344 if (args == error_mark_node)
1345 return error_mark_node;
1346 return build2 (TEMPLATE_ID_EXPR, boolean_type_node, tmpl, args);
1347 }
1348
1349 /* Builds an id-expression of the form `C<Args...>` where C is a standard
1350 concept. */
1351
1352 static tree
build_standard_check(tree tmpl,tree args,tsubst_flags_t complain)1353 build_standard_check (tree tmpl, tree args, tsubst_flags_t complain)
1354 {
1355 gcc_assert (standard_concept_p (tmpl));
1356 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1357 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
1358 args = coerce_template_parms (parms, args, tmpl, complain);
1359 if (args == error_mark_node)
1360 return error_mark_node;
1361 return build2 (TEMPLATE_ID_EXPR, boolean_type_node, tmpl, args);
1362 }
1363
1364 /* Construct an expression that checks TARGET using ARGS. */
1365
1366 tree
build_concept_check(tree target,tree args,tsubst_flags_t complain)1367 build_concept_check (tree target, tree args, tsubst_flags_t complain)
1368 {
1369 return build_concept_check (target, NULL_TREE, args, complain);
1370 }
1371
1372 /* Construct an expression that checks the concept given by DECL. If
1373 concept_definition_p (DECL) is false, this returns null. */
1374
1375 tree
build_concept_check(tree decl,tree arg,tree rest,tsubst_flags_t complain)1376 build_concept_check (tree decl, tree arg, tree rest, tsubst_flags_t complain)
1377 {
1378 tree args = build_concept_check_arguments (arg, rest);
1379
1380 if (standard_concept_p (decl))
1381 return build_standard_check (decl, args, complain);
1382 if (variable_concept_p (decl))
1383 return build_variable_check (decl, args, complain);
1384 if (function_concept_p (decl))
1385 return build_function_check (decl, args, complain);
1386
1387 return error_mark_node;
1388 }
1389
1390 /* Build a template-id that can participate in a concept check. */
1391
1392 static tree
build_concept_id(tree decl,tree args)1393 build_concept_id (tree decl, tree args)
1394 {
1395 tree check = build_concept_check (decl, args, tf_warning_or_error);
1396 if (check == error_mark_node)
1397 return error_mark_node;
1398 return unpack_concept_check (check);
1399 }
1400
1401 /* Build a template-id that can participate in a concept check, preserving
1402 the source location of the original template-id. */
1403
1404 tree
build_concept_id(tree expr)1405 build_concept_id (tree expr)
1406 {
1407 gcc_assert (TREE_CODE (expr) == TEMPLATE_ID_EXPR);
1408 tree id = build_concept_id (TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
1409 protected_set_expr_location (id, cp_expr_location (expr));
1410 return id;
1411 }
1412
1413 /* Build as template-id with a placeholder that can be used as a
1414 type constraint.
1415
1416 Note that this will diagnose errors if the initial concept check
1417 cannot be built. */
1418
1419 tree
build_type_constraint(tree decl,tree args,tsubst_flags_t complain)1420 build_type_constraint (tree decl, tree args, tsubst_flags_t complain)
1421 {
1422 tree wildcard = build_nt (WILDCARD_DECL);
1423 tree check = build_concept_check (decl, wildcard, args, complain);
1424 if (check == error_mark_node)
1425 return error_mark_node;
1426 return unpack_concept_check (check);
1427 }
1428
1429 /* Returns a TYPE_DECL that contains sufficient information to
1430 build a template parameter of the same kind as PROTO and
1431 constrained by the concept declaration CNC. Note that PROTO
1432 is the first template parameter of CNC.
1433
1434 If specified, ARGS provides additional arguments to the
1435 constraint check. */
1436 tree
build_constrained_parameter(tree cnc,tree proto,tree args)1437 build_constrained_parameter (tree cnc, tree proto, tree args)
1438 {
1439 tree name = DECL_NAME (cnc);
1440 tree type = TREE_TYPE (proto);
1441 tree decl = build_decl (input_location, TYPE_DECL, name, type);
1442 CONSTRAINED_PARM_PROTOTYPE (decl) = proto;
1443 CONSTRAINED_PARM_CONCEPT (decl) = cnc;
1444 CONSTRAINED_PARM_EXTRA_ARGS (decl) = args;
1445 return decl;
1446 }
1447
1448 /* Create a constraint expression for the given DECL that evaluates the
1449 requirements specified by CONSTR, a TYPE_DECL that contains all the
1450 information necessary to build the requirements (see finish_concept_name
1451 for the layout of that TYPE_DECL).
1452
1453 Note that the constraints are neither reduced nor decomposed. That is
1454 done only after the requires clause has been parsed (or not). */
1455
1456 tree
finish_shorthand_constraint(tree decl,tree constr)1457 finish_shorthand_constraint (tree decl, tree constr)
1458 {
1459 /* No requirements means no constraints. */
1460 if (!constr)
1461 return NULL_TREE;
1462
1463 if (error_operand_p (constr))
1464 return NULL_TREE;
1465
1466 tree proto = CONSTRAINED_PARM_PROTOTYPE (constr);
1467 tree con = CONSTRAINED_PARM_CONCEPT (constr);
1468 tree args = CONSTRAINED_PARM_EXTRA_ARGS (constr);
1469
1470 /* The TS lets use shorthand to constrain a pack of arguments, but the
1471 standard does not.
1472
1473 For the TS, consider:
1474
1475 template<C... Ts> struct s;
1476
1477 If C is variadic (and because Ts is a pack), we associate the
1478 constraint C<Ts...>. In all other cases, we associate
1479 the constraint (C<Ts> && ...).
1480
1481 The standard behavior cannot be overridden by -fconcepts-ts. */
1482 bool variadic_concept_p = template_parameter_pack_p (proto);
1483 bool declared_pack_p = template_parameter_pack_p (decl);
1484 bool apply_to_each_p = (cxx_dialect >= cxx2a) ? true : !variadic_concept_p;
1485
1486 /* Get the argument and overload used for the requirement
1487 and adjust it if we're going to expand later. */
1488 tree arg = template_parm_to_arg (decl);
1489 if (apply_to_each_p && declared_pack_p)
1490 arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
1491
1492 /* Build the concept constraint-expression. */
1493 tree tmpl = DECL_TI_TEMPLATE (con);
1494 tree check = tmpl;
1495 if (TREE_CODE (con) == FUNCTION_DECL)
1496 check = ovl_make (tmpl);
1497 check = build_concept_check (check, arg, args, tf_warning_or_error);
1498
1499 /* Make the check a fold-expression if needed. */
1500 if (apply_to_each_p && declared_pack_p)
1501 check = finish_left_unary_fold_expr (check, TRUTH_ANDIF_EXPR);
1502
1503 return check;
1504 }
1505
1506 /* Returns a conjunction of shorthand requirements for the template
1507 parameter list PARMS. Note that the requirements are stored in
1508 the TYPE of each tree node. */
1509
1510 tree
get_shorthand_constraints(tree parms)1511 get_shorthand_constraints (tree parms)
1512 {
1513 tree result = NULL_TREE;
1514 parms = INNERMOST_TEMPLATE_PARMS (parms);
1515 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
1516 {
1517 tree parm = TREE_VEC_ELT (parms, i);
1518 tree constr = TEMPLATE_PARM_CONSTRAINTS (parm);
1519 result = combine_constraint_expressions (result, constr);
1520 }
1521 return result;
1522 }
1523
1524 /* Get the deduced wildcard from a DEDUCED placeholder. If the deduced
1525 wildcard is a pack, return the first argument of that pack. */
1526
1527 static tree
get_deduced_wildcard(tree wildcard)1528 get_deduced_wildcard (tree wildcard)
1529 {
1530 if (ARGUMENT_PACK_P (wildcard))
1531 wildcard = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (wildcard), 0);
1532 gcc_assert (TREE_CODE (wildcard) == WILDCARD_DECL);
1533 return wildcard;
1534 }
1535
1536 /* Returns the prototype parameter for the nth deduced wildcard. */
1537
1538 static tree
get_introduction_prototype(tree wildcards,int index)1539 get_introduction_prototype (tree wildcards, int index)
1540 {
1541 return TREE_TYPE (get_deduced_wildcard (TREE_VEC_ELT (wildcards, index)));
1542 }
1543
1544 /* Introduce a type template parameter. */
1545
1546 static tree
introduce_type_template_parameter(tree wildcard,bool & non_type_p)1547 introduce_type_template_parameter (tree wildcard, bool& non_type_p)
1548 {
1549 non_type_p = false;
1550 return finish_template_type_parm (class_type_node, DECL_NAME (wildcard));
1551 }
1552
1553 /* Introduce a template template parameter. */
1554
1555 static tree
introduce_template_template_parameter(tree wildcard,bool & non_type_p)1556 introduce_template_template_parameter (tree wildcard, bool& non_type_p)
1557 {
1558 non_type_p = false;
1559 begin_template_parm_list ();
1560 current_template_parms = DECL_TEMPLATE_PARMS (TREE_TYPE (wildcard));
1561 end_template_parm_list ();
1562 return finish_template_template_parm (class_type_node, DECL_NAME (wildcard));
1563 }
1564
1565 /* Introduce a template non-type parameter. */
1566
1567 static tree
introduce_nontype_template_parameter(tree wildcard,bool & non_type_p)1568 introduce_nontype_template_parameter (tree wildcard, bool& non_type_p)
1569 {
1570 non_type_p = true;
1571 tree parm = copy_decl (TREE_TYPE (wildcard));
1572 DECL_NAME (parm) = DECL_NAME (wildcard);
1573 return parm;
1574 }
1575
1576 /* Introduce a single template parameter. */
1577
1578 static tree
build_introduced_template_parameter(tree wildcard,bool & non_type_p)1579 build_introduced_template_parameter (tree wildcard, bool& non_type_p)
1580 {
1581 tree proto = TREE_TYPE (wildcard);
1582
1583 tree parm;
1584 if (TREE_CODE (proto) == TYPE_DECL)
1585 parm = introduce_type_template_parameter (wildcard, non_type_p);
1586 else if (TREE_CODE (proto) == TEMPLATE_DECL)
1587 parm = introduce_template_template_parameter (wildcard, non_type_p);
1588 else
1589 parm = introduce_nontype_template_parameter (wildcard, non_type_p);
1590
1591 /* Wrap in a TREE_LIST for process_template_parm. Note that introduced
1592 parameters do not retain the defaults from the source parameter. */
1593 return build_tree_list (NULL_TREE, parm);
1594 }
1595
1596 /* Introduce a single template parameter. */
1597
1598 static tree
introduce_template_parameter(tree parms,tree wildcard)1599 introduce_template_parameter (tree parms, tree wildcard)
1600 {
1601 gcc_assert (!ARGUMENT_PACK_P (wildcard));
1602 tree proto = TREE_TYPE (wildcard);
1603 location_t loc = DECL_SOURCE_LOCATION (wildcard);
1604
1605 /* Diagnose the case where we have C{...Args}. */
1606 if (WILDCARD_PACK_P (wildcard))
1607 {
1608 tree id = DECL_NAME (wildcard);
1609 error_at (loc, "%qE cannot be introduced with an ellipsis %<...%>", id);
1610 inform (DECL_SOURCE_LOCATION (proto), "prototype declared here");
1611 }
1612
1613 bool non_type_p;
1614 tree parm = build_introduced_template_parameter (wildcard, non_type_p);
1615 return process_template_parm (parms, loc, parm, non_type_p, false);
1616 }
1617
1618 /* Introduce a template parameter pack. */
1619
1620 static tree
introduce_template_parameter_pack(tree parms,tree wildcard)1621 introduce_template_parameter_pack (tree parms, tree wildcard)
1622 {
1623 bool non_type_p;
1624 tree parm = build_introduced_template_parameter (wildcard, non_type_p);
1625 location_t loc = DECL_SOURCE_LOCATION (wildcard);
1626 return process_template_parm (parms, loc, parm, non_type_p, true);
1627 }
1628
1629 /* Introduce the nth template parameter. */
1630
1631 static tree
introduce_template_parameter(tree parms,tree wildcards,int & index)1632 introduce_template_parameter (tree parms, tree wildcards, int& index)
1633 {
1634 tree deduced = TREE_VEC_ELT (wildcards, index++);
1635 return introduce_template_parameter (parms, deduced);
1636 }
1637
1638 /* Introduce either a template parameter pack or a list of template
1639 parameters. */
1640
1641 static tree
introduce_template_parameters(tree parms,tree wildcards,int & index)1642 introduce_template_parameters (tree parms, tree wildcards, int& index)
1643 {
1644 /* If the prototype was a parameter, we better have deduced an
1645 argument pack, and that argument must be the last deduced value
1646 in the wildcard vector. */
1647 tree deduced = TREE_VEC_ELT (wildcards, index++);
1648 gcc_assert (ARGUMENT_PACK_P (deduced));
1649 gcc_assert (index == TREE_VEC_LENGTH (wildcards));
1650
1651 /* Introduce each element in the pack. */
1652 tree args = ARGUMENT_PACK_ARGS (deduced);
1653 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1654 {
1655 tree arg = TREE_VEC_ELT (args, i);
1656 if (WILDCARD_PACK_P (arg))
1657 parms = introduce_template_parameter_pack (parms, arg);
1658 else
1659 parms = introduce_template_parameter (parms, arg);
1660 }
1661
1662 return parms;
1663 }
1664
1665 /* Builds the template parameter list PARMS by chaining introduced
1666 parameters from the WILDCARD vector. INDEX is the position of
1667 the current parameter. */
1668
1669 static tree
process_introduction_parms(tree parms,tree wildcards,int & index)1670 process_introduction_parms (tree parms, tree wildcards, int& index)
1671 {
1672 tree proto = get_introduction_prototype (wildcards, index);
1673 if (template_parameter_pack_p (proto))
1674 return introduce_template_parameters (parms, wildcards, index);
1675 else
1676 return introduce_template_parameter (parms, wildcards, index);
1677 }
1678
1679 /* Ensure that all template parameters have been introduced for the concept
1680 named in CHECK. If not, emit a diagnostic.
1681
1682 Note that implicitly introducing a parameter with a default argument
1683 creates a case where a parameter is declared, but unnamed, making
1684 it unusable in the definition. */
1685
1686 static bool
check_introduction_list(tree intros,tree check)1687 check_introduction_list (tree intros, tree check)
1688 {
1689 check = unpack_concept_check (check);
1690 tree tmpl = TREE_OPERAND (check, 0);
1691 if (OVL_P (tmpl))
1692 tmpl = OVL_FIRST (tmpl);
1693
1694 tree parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
1695 if (TREE_VEC_LENGTH (intros) < TREE_VEC_LENGTH (parms))
1696 {
1697 error_at (input_location, "all template parameters of %qD must "
1698 "be introduced", tmpl);
1699 return false;
1700 }
1701
1702 return true;
1703 }
1704
1705 /* Associates a constraint check to the current template based on the
1706 introduction parameters. INTRO_LIST must be a TREE_VEC of WILDCARD_DECLs
1707 containing a chained PARM_DECL which contains the identifier as well as
1708 the source location. TMPL_DECL is the decl for the concept being used.
1709 If we take a concept, C, this will form a check in the form of
1710 C<INTRO_LIST> filling in any extra arguments needed by the defaults
1711 deduced.
1712
1713 Returns NULL_TREE if no concept could be matched and error_mark_node if
1714 an error occurred when matching. */
1715
1716 tree
finish_template_introduction(tree tmpl_decl,tree intro_list,location_t intro_loc)1717 finish_template_introduction (tree tmpl_decl,
1718 tree intro_list,
1719 location_t intro_loc)
1720 {
1721 /* Build a concept check to deduce the actual parameters. */
1722 tree expr = build_concept_check (tmpl_decl, intro_list, tf_none);
1723 if (expr == error_mark_node)
1724 {
1725 error_at (intro_loc, "cannot deduce template parameters from "
1726 "introduction list");
1727 return error_mark_node;
1728 }
1729
1730 if (!check_introduction_list (intro_list, expr))
1731 return error_mark_node;
1732
1733 tree parms = deduce_concept_introduction (expr);
1734 if (!parms)
1735 return NULL_TREE;
1736
1737 /* Build template parameter scope for introduction. */
1738 tree parm_list = NULL_TREE;
1739 begin_template_parm_list ();
1740 int nargs = MIN (TREE_VEC_LENGTH (parms), TREE_VEC_LENGTH (intro_list));
1741 for (int n = 0; n < nargs; )
1742 parm_list = process_introduction_parms (parm_list, parms, n);
1743 parm_list = end_template_parm_list (parm_list);
1744
1745 /* Update the number of arguments to reflect the number of deduced
1746 template parameter introductions. */
1747 nargs = TREE_VEC_LENGTH (parm_list);
1748
1749 /* Determine if any errors occurred during matching. */
1750 for (int i = 0; i < TREE_VEC_LENGTH (parm_list); ++i)
1751 if (TREE_VALUE (TREE_VEC_ELT (parm_list, i)) == error_mark_node)
1752 {
1753 end_template_decl ();
1754 return error_mark_node;
1755 }
1756
1757 /* Build a concept check for our constraint. */
1758 tree check_args = make_tree_vec (nargs);
1759 int n = 0;
1760 for (; n < TREE_VEC_LENGTH (parm_list); ++n)
1761 {
1762 tree parm = TREE_VEC_ELT (parm_list, n);
1763 TREE_VEC_ELT (check_args, n) = template_parm_to_arg (parm);
1764 }
1765 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (check_args, n);
1766
1767 /* If the template expects more parameters we should be able
1768 to use the defaults from our deduced concept. */
1769 for (; n < TREE_VEC_LENGTH (parms); ++n)
1770 TREE_VEC_ELT (check_args, n) = TREE_VEC_ELT (parms, n);
1771
1772 /* Associate the constraint. */
1773 tree check = build_concept_check (tmpl_decl,
1774 check_args,
1775 tf_warning_or_error);
1776 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = check;
1777
1778 return parm_list;
1779 }
1780
1781
1782 /* Given the concept check T from a constrained-type-specifier, extract
1783 its TMPL and ARGS. FIXME why do we need two different forms of
1784 constrained-type-specifier? */
1785
1786 void
placeholder_extract_concept_and_args(tree t,tree & tmpl,tree & args)1787 placeholder_extract_concept_and_args (tree t, tree &tmpl, tree &args)
1788 {
1789 if (concept_check_p (t))
1790 {
1791 t = unpack_concept_check (t);
1792 tmpl = TREE_OPERAND (t, 0);
1793 if (TREE_CODE (tmpl) == OVERLOAD)
1794 tmpl = OVL_FIRST (tmpl);
1795 args = TREE_OPERAND (t, 1);
1796 return;
1797 }
1798
1799 if (TREE_CODE (t) == TYPE_DECL)
1800 {
1801 /* A constrained parameter. Build a constraint check
1802 based on the prototype parameter and then extract the
1803 arguments from that. */
1804 tree proto = CONSTRAINED_PARM_PROTOTYPE (t);
1805 tree check = finish_shorthand_constraint (proto, t);
1806 placeholder_extract_concept_and_args (check, tmpl, args);
1807 return;
1808 }
1809 }
1810
1811 /* Returns true iff the placeholders C1 and C2 are equivalent. C1
1812 and C2 can be either TEMPLATE_TYPE_PARM or template-ids. */
1813
1814 bool
equivalent_placeholder_constraints(tree c1,tree c2)1815 equivalent_placeholder_constraints (tree c1, tree c2)
1816 {
1817 if (c1 && TREE_CODE (c1) == TEMPLATE_TYPE_PARM)
1818 /* A constrained auto. */
1819 c1 = PLACEHOLDER_TYPE_CONSTRAINTS (c1);
1820 if (c2 && TREE_CODE (c2) == TEMPLATE_TYPE_PARM)
1821 c2 = PLACEHOLDER_TYPE_CONSTRAINTS (c2);
1822
1823 if (c1 == c2)
1824 return true;
1825 if (!c1 || !c2)
1826 return false;
1827 if (c1 == error_mark_node || c2 == error_mark_node)
1828 /* We get here during satisfaction; when a deduction constraint
1829 fails, substitution can produce an error_mark_node for the
1830 placeholder constraints. */
1831 return false;
1832
1833 tree t1, t2, a1, a2;
1834 placeholder_extract_concept_and_args (c1, t1, a1);
1835 placeholder_extract_concept_and_args (c2, t2, a2);
1836
1837 if (t1 != t2)
1838 return false;
1839
1840 int len1 = TREE_VEC_LENGTH (a1);
1841 int len2 = TREE_VEC_LENGTH (a2);
1842 if (len1 != len2)
1843 return false;
1844
1845 /* Skip the first argument so we don't infinitely recurse.
1846 Also, they may differ in template parameter index. */
1847 for (int i = 1; i < len1; ++i)
1848 {
1849 tree t1 = TREE_VEC_ELT (a1, i);
1850 tree t2 = TREE_VEC_ELT (a2, i);
1851 if (!template_args_equal (t1, t2))
1852 return false;
1853 }
1854 return true;
1855 }
1856
1857 /* Return a hash value for the placeholder ATOMIC_CONSTR C. */
1858
1859 hashval_t
hash_placeholder_constraint(tree c)1860 hash_placeholder_constraint (tree c)
1861 {
1862 tree t, a;
1863 placeholder_extract_concept_and_args (c, t, a);
1864
1865 /* Like hash_tmpl_and_args, but skip the first argument. */
1866 hashval_t val = iterative_hash_object (DECL_UID (t), 0);
1867
1868 for (int i = TREE_VEC_LENGTH (a)-1; i > 0; --i)
1869 val = iterative_hash_template_arg (TREE_VEC_ELT (a, i), val);
1870
1871 return val;
1872 }
1873
1874 /* Substitute through the simple requirement. */
1875
1876 static tree
tsubst_valid_expression_requirement(tree t,tree args,subst_info info)1877 tsubst_valid_expression_requirement (tree t, tree args, subst_info info)
1878 {
1879 tree r = tsubst_expr (t, args, info.complain, info.in_decl, false);
1880 if (convert_to_void (r, ICV_STATEMENT, info.complain) == error_mark_node)
1881 return error_mark_node;
1882 return r;
1883 }
1884
1885
1886 /* Substitute through the simple requirement. */
1887
1888 static tree
tsubst_simple_requirement(tree t,tree args,subst_info info)1889 tsubst_simple_requirement (tree t, tree args, subst_info info)
1890 {
1891 tree t0 = TREE_OPERAND (t, 0);
1892 tree expr = tsubst_valid_expression_requirement (t0, args, info);
1893 if (expr == error_mark_node)
1894 return error_mark_node;
1895 return finish_simple_requirement (EXPR_LOCATION (t), expr);
1896 }
1897
1898 /* Substitute through the type requirement. */
1899
1900 static tree
tsubst_type_requirement(tree t,tree args,subst_info info)1901 tsubst_type_requirement (tree t, tree args, subst_info info)
1902 {
1903 tree t0 = TREE_OPERAND (t, 0);
1904 tree type = tsubst (t0, args, info.complain, info.in_decl);
1905 if (type == error_mark_node)
1906 return error_mark_node;
1907 return finish_type_requirement (EXPR_LOCATION (t), type);
1908 }
1909
1910 /* True if TYPE can be deduced from EXPR. */
1911
1912 static bool
type_deducible_p(tree expr,tree type,tree placeholder,tree args,subst_info info)1913 type_deducible_p (tree expr, tree type, tree placeholder, tree args,
1914 subst_info info)
1915 {
1916 /* Make sure deduction is performed against ( EXPR ), so that
1917 references are preserved in the result. */
1918 expr = force_paren_expr_uneval (expr);
1919
1920 /* Replace the constraints with the instantiated constraints. This
1921 substitutes args into any template parameters in the trailing
1922 result type. */
1923 tree saved_constr = PLACEHOLDER_TYPE_CONSTRAINTS (placeholder);
1924 tree subst_constr
1925 = tsubst_constraint (saved_constr,
1926 args,
1927 info.complain | tf_partial,
1928 info.in_decl);
1929
1930 if (subst_constr == error_mark_node)
1931 return false;
1932
1933 PLACEHOLDER_TYPE_CONSTRAINTS (placeholder) = subst_constr;
1934
1935 /* Temporarily unlink the canonical type. */
1936 tree saved_type = TYPE_CANONICAL (placeholder);
1937 TYPE_CANONICAL (placeholder) = NULL_TREE;
1938
1939 tree deduced_type
1940 = do_auto_deduction (type,
1941 expr,
1942 placeholder,
1943 info.complain,
1944 adc_requirement);
1945
1946 PLACEHOLDER_TYPE_CONSTRAINTS (placeholder) = saved_constr;
1947 TYPE_CANONICAL (placeholder) = saved_type;
1948
1949 if (deduced_type == error_mark_node)
1950 return false;
1951
1952 return true;
1953 }
1954
1955 /* True if EXPR can not be converted to TYPE. */
1956
1957 static bool
expression_convertible_p(tree expr,tree type,subst_info info)1958 expression_convertible_p (tree expr, tree type, subst_info info)
1959 {
1960 tree conv =
1961 perform_direct_initialization_if_possible (type, expr, false,
1962 info.complain);
1963 if (conv == error_mark_node)
1964 return false;
1965 if (conv == NULL_TREE)
1966 {
1967 if (info.complain & tf_error)
1968 {
1969 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
1970 error_at (loc, "cannot convert %qE to %qT", expr, type);
1971 }
1972 return false;
1973 }
1974 return true;
1975 }
1976
1977
1978 /* Substitute through the compound requirement. */
1979
1980 static tree
tsubst_compound_requirement(tree t,tree args,subst_info info)1981 tsubst_compound_requirement (tree t, tree args, subst_info info)
1982 {
1983 tree t0 = TREE_OPERAND (t, 0);
1984 tree t1 = TREE_OPERAND (t, 1);
1985 tree expr = tsubst_valid_expression_requirement (t0, args, info);
1986 if (expr == error_mark_node)
1987 return error_mark_node;
1988
1989 /* Check the noexcept condition. */
1990 bool noexcept_p = COMPOUND_REQ_NOEXCEPT_P (t);
1991 if (noexcept_p && !expr_noexcept_p (expr, tf_none))
1992 return error_mark_node;
1993
1994 /* Substitute through the type expression, if any. */
1995 tree type = tsubst (t1, args, info.complain, info.in_decl);
1996 if (type == error_mark_node)
1997 return error_mark_node;
1998
1999 subst_info quiet (tf_none, info.in_decl);
2000
2001 /* Check expression against the result type. */
2002 if (type)
2003 {
2004 if (tree placeholder = type_uses_auto (type))
2005 {
2006 if (!type_deducible_p (expr, type, placeholder, args, quiet))
2007 return error_mark_node;
2008 }
2009 else if (!expression_convertible_p (expr, type, quiet))
2010 return error_mark_node;
2011 }
2012
2013 return finish_compound_requirement (EXPR_LOCATION (t),
2014 expr, type, noexcept_p);
2015 }
2016
2017 static tree
tsubst_nested_requirement(tree t,tree args,subst_info info)2018 tsubst_nested_requirement (tree t, tree args, subst_info info)
2019 {
2020 /* Ensure that we're in an evaluation context prior to satisfaction. */
2021 tree norm = TREE_VALUE (TREE_TYPE (t));
2022 tree result = satisfy_constraint (norm, args, info);
2023 if (result == error_mark_node && info.quiet ())
2024 {
2025 subst_info noisy (tf_warning_or_error, info.in_decl);
2026 satisfy_constraint (norm, args, noisy);
2027 }
2028 if (result != boolean_true_node)
2029 return error_mark_node;
2030 return result;
2031 }
2032
2033 /* Substitute ARGS into the requirement T. */
2034
2035 static tree
tsubst_requirement(tree t,tree args,subst_info info)2036 tsubst_requirement (tree t, tree args, subst_info info)
2037 {
2038 iloc_sentinel loc_s (cp_expr_location (t));
2039 switch (TREE_CODE (t))
2040 {
2041 case SIMPLE_REQ:
2042 return tsubst_simple_requirement (t, args, info);
2043 case TYPE_REQ:
2044 return tsubst_type_requirement (t, args, info);
2045 case COMPOUND_REQ:
2046 return tsubst_compound_requirement (t, args, info);
2047 case NESTED_REQ:
2048 return tsubst_nested_requirement (t, args, info);
2049 default:
2050 break;
2051 }
2052 gcc_unreachable ();
2053 }
2054
2055 /* Substitute ARGS into the list of requirements T. Note that
2056 substitution failures here result in ill-formed programs. */
2057
2058 static tree
tsubst_requirement_body(tree t,tree args,subst_info info)2059 tsubst_requirement_body (tree t, tree args, subst_info info)
2060 {
2061 tree result = NULL_TREE;
2062 while (t)
2063 {
2064 tree req = tsubst_requirement (TREE_VALUE (t), args, info);
2065 if (req == error_mark_node)
2066 return error_mark_node;
2067 result = tree_cons (NULL_TREE, req, result);
2068 t = TREE_CHAIN (t);
2069 }
2070 return nreverse (result);
2071 }
2072
2073 static tree
declare_constraint_vars(tree parms,tree vars)2074 declare_constraint_vars (tree parms, tree vars)
2075 {
2076 tree s = vars;
2077 for (tree t = parms; t; t = DECL_CHAIN (t))
2078 {
2079 if (DECL_PACK_P (t))
2080 {
2081 tree pack = extract_fnparm_pack (t, &s);
2082 register_local_specialization (pack, t);
2083 }
2084 else
2085 {
2086 register_local_specialization (s, t);
2087 s = DECL_CHAIN (s);
2088 }
2089 }
2090 return vars;
2091 }
2092
2093 /* Substitute through as if checking function parameter types. This
2094 will diagnose common parameter type errors. Returns error_mark_node
2095 if an error occurred. */
2096
2097 static tree
check_constaint_variables(tree t,tree args,subst_info info)2098 check_constaint_variables (tree t, tree args, subst_info info)
2099 {
2100 tree types = NULL_TREE;
2101 tree p = t;
2102 while (p && !VOID_TYPE_P (p))
2103 {
2104 types = tree_cons (NULL_TREE, TREE_TYPE (p), types);
2105 p = TREE_CHAIN (p);
2106 }
2107 types = chainon (nreverse (types), void_list_node);
2108 return tsubst_function_parms (types, args, info.complain, info.in_decl);
2109 }
2110
2111 /* A subroutine of tsubst_parameterized_constraint. Substitute ARGS
2112 into the parameter list T, producing a sequence of constraint
2113 variables, declared in the current scope.
2114
2115 Note that the caller must establish a local specialization stack
2116 prior to calling this function since this substitution will
2117 declare the substituted parameters. */
2118
2119 static tree
tsubst_constraint_variables(tree t,tree args,subst_info info)2120 tsubst_constraint_variables (tree t, tree args, subst_info info)
2121 {
2122 /* Perform a trial substitution to check for type errors. */
2123 tree parms = check_constaint_variables (t, args, info);
2124 if (parms == error_mark_node)
2125 return error_mark_node;
2126
2127 /* Clear cp_unevaluated_operand across tsubst so that we get a proper chain
2128 of PARM_DECLs. */
2129 int saved_unevaluated_operand = cp_unevaluated_operand;
2130 cp_unevaluated_operand = 0;
2131 tree vars = tsubst (t, args, info.complain, info.in_decl);
2132 cp_unevaluated_operand = saved_unevaluated_operand;
2133 if (vars == error_mark_node)
2134 return error_mark_node;
2135 return declare_constraint_vars (t, vars);
2136 }
2137
2138 /* Substitute ARGS into the requires-expression T. [8.4.7]p6. The
2139 substitution of template arguments into a requires-expression
2140 may result in the formation of invalid types or expressions
2141 in its requirements ... In such cases, the expression evaluates
2142 to false; it does not cause the program to be ill-formed.
2143
2144 However, there are cases where substitution must produce a
2145 new requires-expression, that is not a template constraint.
2146 For example:
2147
2148 template<typename T>
2149 class X {
2150 template<typename U>
2151 static constexpr bool var = requires (U u) { T::fn(u); };
2152 };
2153
2154 In the instantiation of X<Y> (assuming Y defines fn), then the
2155 instantiated requires-expression would include Y::fn(u). If any
2156 substitution in the requires-expression fails, we can immediately
2157 fold the expression to false, as would be the case e.g., when
2158 instantiation X<int>. */
2159
2160 tree
tsubst_requires_expr(tree t,tree args,tsubst_flags_t complain,tree in_decl)2161 tsubst_requires_expr (tree t, tree args,
2162 tsubst_flags_t complain, tree in_decl)
2163 {
2164 local_specialization_stack stack (lss_copy);
2165
2166 subst_info info (complain, in_decl);
2167
2168 /* A requires-expression is an unevaluated context. */
2169 cp_unevaluated u;
2170
2171 tree parms = TREE_OPERAND (t, 0);
2172 if (parms)
2173 {
2174 parms = tsubst_constraint_variables (parms, args, info);
2175 if (parms == error_mark_node)
2176 return boolean_false_node;
2177 }
2178
2179 tree reqs = TREE_OPERAND (t, 1);
2180 reqs = tsubst_requirement_body (reqs, args, info);
2181 if (reqs == error_mark_node)
2182 return boolean_false_node;
2183
2184 if (processing_template_decl)
2185 return finish_requires_expr (cp_expr_location (t), parms, reqs);
2186
2187 return boolean_true_node;
2188 }
2189
2190 /* Substitute ARGS into the constraint information CI, producing a new
2191 constraint record. */
2192
2193 tree
tsubst_constraint_info(tree t,tree args,tsubst_flags_t complain,tree in_decl)2194 tsubst_constraint_info (tree t, tree args,
2195 tsubst_flags_t complain, tree in_decl)
2196 {
2197 if (!t || t == error_mark_node || !check_constraint_info (t))
2198 return NULL_TREE;
2199
2200 tree tr = tsubst_constraint (CI_TEMPLATE_REQS (t), args, complain, in_decl);
2201 tree dr = tsubst_constraint (CI_DECLARATOR_REQS (t), args, complain, in_decl);
2202 return build_constraints (tr, dr);
2203 }
2204
2205 /* Substitute through a parameter mapping, in order to get the actual
2206 arguments used to instantiate an atomic constraint. This may fail
2207 if the substitution into arguments produces something ill-formed. */
2208
2209 static tree
tsubst_parameter_mapping(tree map,tree args,subst_info info)2210 tsubst_parameter_mapping (tree map, tree args, subst_info info)
2211 {
2212 if (!map)
2213 return NULL_TREE;
2214
2215 tsubst_flags_t complain = info.complain;
2216 tree in_decl = info.in_decl;
2217
2218 tree result = NULL_TREE;
2219 for (tree p = map; p; p = TREE_CHAIN (p))
2220 {
2221 if (p == error_mark_node)
2222 return error_mark_node;
2223 tree parm = TREE_VALUE (p);
2224 tree arg = TREE_PURPOSE (p);
2225 tree new_arg = NULL_TREE;
2226 if (TYPE_P (arg))
2227 {
2228 /* If a template parameter is declared with a placeholder, we can
2229 get those in the argument list if decltype is applied to the
2230 placeholder. For example:
2231
2232 template<auto T>
2233 requires C<decltype(T)>
2234 void f() { }
2235
2236 The normalized argument for C will be an auto type, so we'll
2237 need to deduce the actual argument from the corresponding
2238 initializer (whatever argument is provided for T), and use
2239 that result in the instantiated parameter mapping. */
2240 if (tree auto_node = type_uses_auto (arg))
2241 {
2242 int level;
2243 int index;
2244 template_parm_level_and_index (parm, &level, &index);
2245 tree init = TMPL_ARG (args, level, index);
2246 new_arg = do_auto_deduction (arg, init, auto_node,
2247 complain, adc_variable_type,
2248 make_tree_vec (0));
2249 }
2250 }
2251 else if (ARGUMENT_PACK_P (arg))
2252 new_arg = tsubst_argument_pack (arg, args, complain, in_decl);
2253 if (!new_arg)
2254 {
2255 new_arg = tsubst_template_arg (arg, args, complain, in_decl);
2256 if (TYPE_P (new_arg))
2257 new_arg = canonicalize_type_argument (new_arg, complain);
2258 }
2259 if (new_arg == error_mark_node)
2260 return error_mark_node;
2261
2262 result = tree_cons (new_arg, parm, result);
2263 }
2264 return nreverse (result);
2265 }
2266
2267 tree
tsubst_parameter_mapping(tree map,tree args,tsubst_flags_t complain,tree in_decl)2268 tsubst_parameter_mapping (tree map, tree args, tsubst_flags_t complain, tree in_decl)
2269 {
2270 return tsubst_parameter_mapping (map, args, subst_info (complain, in_decl));
2271 }
2272
2273 /*---------------------------------------------------------------------------
2274 Constraint satisfaction
2275 ---------------------------------------------------------------------------*/
2276
2277 /* Hash functions for satisfaction entries. */
2278
2279 struct GTY((for_user)) sat_entry
2280 {
2281 tree constr;
2282 tree args;
2283 tree result;
2284 };
2285
2286 struct sat_hasher : ggc_ptr_hash<sat_entry>
2287 {
hashsat_hasher2288 static hashval_t hash (sat_entry *e)
2289 {
2290 hashval_t value = hash_atomic_constraint (e->constr);
2291 return iterative_hash_template_arg (e->args, value);
2292 }
2293
equalsat_hasher2294 static bool equal (sat_entry *e1, sat_entry *e2)
2295 {
2296 if (!atomic_constraints_identical_p (e1->constr, e2->constr))
2297 return false;
2298 return template_args_equal (e1->args, e2->args);
2299 }
2300 };
2301
2302 /* Cache the result of satisfy_atom. */
2303 static GTY((deletable)) hash_table<sat_hasher> *sat_cache;
2304
2305 /* Cache the result of constraint_satisfaction_value. */
2306 static GTY((deletable)) hash_map<tree, tree> *decl_satisfied_cache;
2307
2308 static tree
get_satisfaction(tree constr,tree args)2309 get_satisfaction (tree constr, tree args)
2310 {
2311 if (!sat_cache)
2312 return NULL_TREE;
2313 sat_entry elt = { constr, args, NULL_TREE };
2314 sat_entry* found = sat_cache->find (&elt);
2315 if (found)
2316 return found->result;
2317 else
2318 return NULL_TREE;
2319 }
2320
2321 static void
save_satisfaction(tree constr,tree args,tree result)2322 save_satisfaction (tree constr, tree args, tree result)
2323 {
2324 if (!sat_cache)
2325 sat_cache = hash_table<sat_hasher>::create_ggc (31);
2326 sat_entry elt = {constr, args, result};
2327 sat_entry** slot = sat_cache->find_slot (&elt, INSERT);
2328 sat_entry* entry = ggc_alloc<sat_entry> ();
2329 *entry = elt;
2330 *slot = entry;
2331 }
2332
2333 void
clear_satisfaction_cache()2334 clear_satisfaction_cache ()
2335 {
2336 if (sat_cache)
2337 sat_cache->empty ();
2338 if (decl_satisfied_cache)
2339 decl_satisfied_cache->empty ();
2340 }
2341
2342 /* A tool to help manage satisfaction caching in satisfy_constraint_r.
2343 Note the cache is only used when not diagnosing errors. */
2344
2345 struct satisfaction_cache
2346 {
satisfaction_cachesatisfaction_cache2347 satisfaction_cache (tree constr, tree args, tsubst_flags_t complain)
2348 : constr(constr), args(args), complain(complain)
2349 { }
2350
getsatisfaction_cache2351 tree get ()
2352 {
2353 if (complain == tf_none)
2354 return get_satisfaction (constr, args);
2355 return NULL_TREE;
2356 }
2357
savesatisfaction_cache2358 tree save (tree result)
2359 {
2360 if (complain == tf_none)
2361 save_satisfaction (constr, args, result);
2362 return result;
2363 }
2364
2365 tree constr;
2366 tree args;
2367 tsubst_flags_t complain;
2368 };
2369
2370 static int satisfying_constraint = 0;
2371
2372 /* Returns true if we are currently satisfying a constraint.
2373
2374 This is used to guard against recursive calls to evaluate_concept_check
2375 during template argument substitution.
2376
2377 TODO: Do we need this now that we fully normalize prior to evaluation?
2378 I think not. */
2379
2380 bool
satisfying_constraint_p()2381 satisfying_constraint_p ()
2382 {
2383 return satisfying_constraint;
2384 }
2385
2386 /* Substitute ARGS into constraint-expression T during instantiation of
2387 a member of a class template. */
2388
2389 tree
tsubst_constraint(tree t,tree args,tsubst_flags_t complain,tree in_decl)2390 tsubst_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
2391 {
2392 /* We also don't want to evaluate concept-checks when substituting the
2393 constraint-expressions of a declaration. */
2394 processing_constraint_expression_sentinel s;
2395 tree expr = tsubst_expr (t, args, complain, in_decl, false);
2396 return expr;
2397 }
2398
2399 static tree satisfy_constraint_r (tree, tree, subst_info info);
2400
2401 /* Compute the satisfaction of a conjunction. */
2402
2403 static tree
satisfy_conjunction(tree t,tree args,subst_info info)2404 satisfy_conjunction (tree t, tree args, subst_info info)
2405 {
2406 tree lhs = satisfy_constraint_r (TREE_OPERAND (t, 0), args, info);
2407 if (lhs == error_mark_node || lhs == boolean_false_node)
2408 return lhs;
2409 return satisfy_constraint_r (TREE_OPERAND (t, 1), args, info);
2410 }
2411
2412 /* The current depth at which we're replaying an error during recursive
2413 diagnosis of a constraint satisfaction failure. */
2414
2415 static int current_constraint_diagnosis_depth;
2416
2417 /* Whether CURRENT_CONSTRAINT_DIAGNOSIS_DEPTH has ever exceeded
2418 CONCEPTS_DIAGNOSTICS_MAX_DEPTH during recursive diagnosis of a constraint
2419 satisfaction error. */
2420
2421 static bool concepts_diagnostics_max_depth_exceeded_p;
2422
2423 /* Recursive subroutine of collect_operands_of_disjunction. T is a normalized
2424 subexpression of a constraint (composed of CONJ_CONSTRs and DISJ_CONSTRs)
2425 and E is the corresponding unnormalized subexpression (composed of
2426 TRUTH_ANDIF_EXPRs and TRUTH_ORIF_EXPRs). */
2427
2428 static void
collect_operands_of_disjunction_r(tree t,tree e,auto_vec<tree_pair> * operands)2429 collect_operands_of_disjunction_r (tree t, tree e,
2430 auto_vec<tree_pair> *operands)
2431 {
2432 if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
2433 {
2434 collect_operands_of_disjunction_r (TREE_OPERAND (t, 0),
2435 TREE_OPERAND (e, 0), operands);
2436 collect_operands_of_disjunction_r (TREE_OPERAND (t, 1),
2437 TREE_OPERAND (e, 1), operands);
2438 }
2439 else
2440 {
2441 tree_pair p = std::make_pair (t, e);
2442 operands->safe_push (p);
2443 }
2444 }
2445
2446 /* Recursively collect the normalized and unnormalized operands of the
2447 disjunction T and append them to OPERANDS in order. */
2448
2449 static void
collect_operands_of_disjunction(tree t,auto_vec<tree_pair> * operands)2450 collect_operands_of_disjunction (tree t, auto_vec<tree_pair> *operands)
2451 {
2452 collect_operands_of_disjunction_r (t, CONSTR_EXPR (t), operands);
2453 }
2454
2455 /* Compute the satisfaction of a disjunction. */
2456
2457 static tree
satisfy_disjunction(tree t,tree args,subst_info info)2458 satisfy_disjunction (tree t, tree args, subst_info info)
2459 {
2460 /* Evaluate the operands quietly. */
2461 subst_info quiet (tf_none, NULL_TREE);
2462
2463 /* Register the constraint for diagnostics, if needed. */
2464 diagnosing_failed_constraint failure (t, args, info.noisy ());
2465
2466 tree lhs = satisfy_constraint_r (TREE_OPERAND (t, 0), args, quiet);
2467 if (lhs == boolean_true_node)
2468 return boolean_true_node;
2469 tree rhs = satisfy_constraint_r (TREE_OPERAND (t, 1), args, quiet);
2470 if (rhs != boolean_true_node && info.noisy ())
2471 {
2472 cp_expr disj_expr = CONSTR_EXPR (t);
2473 inform (disj_expr.get_location (),
2474 "no operand of the disjunction is satisfied");
2475 if (diagnosing_failed_constraint::replay_errors_p ())
2476 {
2477 /* Replay the error in each branch of the disjunction. */
2478 auto_vec<tree_pair> operands;
2479 collect_operands_of_disjunction (t, &operands);
2480 for (unsigned i = 0; i < operands.length (); i++)
2481 {
2482 tree norm_op = operands[i].first;
2483 tree op = operands[i].second;
2484 location_t loc = make_location (cp_expr_location (op),
2485 disj_expr.get_start (),
2486 disj_expr.get_finish ());
2487 inform (loc, "the operand %qE is unsatisfied because", op);
2488 satisfy_constraint_r (norm_op, args, info);
2489 }
2490 }
2491 }
2492 return rhs;
2493 }
2494
2495 /* Ensures that T is a truth value and not (accidentally, as sometimes
2496 happens) an integer value. */
2497
2498 tree
satisfaction_value(tree t)2499 satisfaction_value (tree t)
2500 {
2501 if (t == error_mark_node || t == boolean_true_node || t == boolean_false_node)
2502 return t;
2503 gcc_assert (TREE_CODE (t) == INTEGER_CST);
2504 if (integer_onep (t))
2505 return boolean_true_node;
2506 if (integer_zerop (t))
2507 return boolean_false_node;
2508
2509 /* Anything else should be invalid. */
2510 gcc_assert (false);
2511 }
2512
2513 /* Build a new template argument list with template arguments corresponding
2514 to the parameters used in an atomic constraint. */
2515
2516 tree
get_mapped_args(tree map)2517 get_mapped_args (tree map)
2518 {
2519 /* No map, no arguments. */
2520 if (!map)
2521 return NULL_TREE;
2522
2523 /* Find the mapped parameter with the highest level. */
2524 int count = 0;
2525 for (tree p = map; p; p = TREE_CHAIN (p))
2526 {
2527 int level;
2528 int index;
2529 template_parm_level_and_index (TREE_VALUE (p), &level, &index);
2530 if (level > count)
2531 count = level;
2532 }
2533
2534 /* Place each argument at its corresponding position in the argument
2535 list. Note that the list will be sparse (not all arguments supplied),
2536 but instantiation is guaranteed to only use the parameters in the
2537 mapping, so null arguments would never be used. */
2538 auto_vec< vec<tree> > lists (count);
2539 lists.quick_grow_cleared (count);
2540 for (tree p = map; p; p = TREE_CHAIN (p))
2541 {
2542 int level;
2543 int index;
2544 template_parm_level_and_index (TREE_VALUE (p), &level, &index);
2545
2546 /* Insert the argument into its corresponding position. */
2547 vec<tree> &list = lists[level - 1];
2548 if (index >= (int)list.length ())
2549 list.safe_grow_cleared (index + 1);
2550 list[index] = TREE_PURPOSE (p);
2551 }
2552
2553 /* Build the new argument list. */
2554 tree args = make_tree_vec (lists.length ());
2555 for (unsigned i = 0; i != lists.length (); ++i)
2556 {
2557 vec<tree> &list = lists[i];
2558 tree level = make_tree_vec (list.length ());
2559 for (unsigned j = 0; j < list.length(); ++j)
2560 TREE_VEC_ELT (level, j) = list[j];
2561 SET_TMPL_ARGS_LEVEL (args, i + 1, level);
2562 list.release ();
2563 }
2564 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, 0);
2565
2566 return args;
2567 }
2568
2569 static void diagnose_atomic_constraint (tree, tree, tree, subst_info);
2570
2571 /* Compute the satisfaction of an atomic constraint. */
2572
2573 static tree
satisfy_atom(tree t,tree args,subst_info info)2574 satisfy_atom (tree t, tree args, subst_info info)
2575 {
2576 satisfaction_cache cache (t, args, info.complain);
2577 if (tree r = cache.get ())
2578 return r;
2579
2580 /* Perform substitution quietly. */
2581 subst_info quiet (tf_none, NULL_TREE);
2582
2583 /* In case there is a diagnostic, we want to establish the context
2584 prior to printing errors. If no errors occur, this context is
2585 removed before returning. */
2586 diagnosing_failed_constraint failure (t, args, info.noisy ());
2587
2588 /* Instantiate the parameter mapping. */
2589 tree map = tsubst_parameter_mapping (ATOMIC_CONSTR_MAP (t), args, quiet);
2590 if (map == error_mark_node)
2591 {
2592 /* If instantiation of the parameter mapping fails, the program
2593 is ill-formed. */
2594 if (info.noisy())
2595 tsubst_parameter_mapping (ATOMIC_CONSTR_MAP (t), args, info);
2596 return cache.save (boolean_false_node);
2597 }
2598
2599 /* Rebuild the argument vector from the parameter mapping. */
2600 args = get_mapped_args (map);
2601
2602 /* Apply the parameter mapping (i.e., just substitute). */
2603 tree expr = ATOMIC_CONSTR_EXPR (t);
2604 tree result = tsubst_expr (expr, args, quiet.complain, quiet.in_decl, false);
2605 if (result == error_mark_node)
2606 {
2607 /* If substitution results in an invalid type or expression, the constraint
2608 is not satisfied. Replay the substitution. */
2609 if (info.noisy ())
2610 tsubst_expr (expr, args, info.complain, info.in_decl, false);
2611 return cache.save (boolean_false_node);
2612 }
2613
2614 /* [17.4.1.2] ... lvalue-to-rvalue conversion is performed as necessary,
2615 and EXPR shall be a constant expression of type bool. */
2616 result = force_rvalue (result, info.complain);
2617 if (result == error_mark_node)
2618 return cache.save (error_mark_node);
2619 if (!same_type_p (TREE_TYPE (result), boolean_type_node))
2620 {
2621 if (info.noisy ())
2622 diagnose_atomic_constraint (t, map, result, info);
2623 return cache.save (error_mark_node);
2624 }
2625
2626 /* Compute the value of the constraint. */
2627 if (info.noisy ())
2628 result = cxx_constant_value (result);
2629 else
2630 {
2631 result = maybe_constant_value (result);
2632 if (!TREE_CONSTANT (result))
2633 result = error_mark_node;
2634 }
2635 result = satisfaction_value (result);
2636 if (result == boolean_false_node && info.noisy ())
2637 diagnose_atomic_constraint (t, map, result, info);
2638
2639 return cache.save (result);
2640 }
2641
2642 /* Determine if the normalized constraint T is satisfied.
2643 Returns boolean_true_node if the expression/constraint is
2644 satisfied, boolean_false_node if not, and error_mark_node
2645 if the there was an error evaluating the constraint.
2646
2647 The parameter mapping of atomic constraints is simply the
2648 set of template arguments that will be substituted into
2649 the expression, regardless of template parameters appearing
2650 withing. Whether a template argument is used in the atomic
2651 constraint only matters for subsumption. */
2652
2653 static tree
satisfy_constraint_r(tree t,tree args,subst_info info)2654 satisfy_constraint_r (tree t, tree args, subst_info info)
2655 {
2656 if (t == error_mark_node)
2657 return error_mark_node;
2658
2659 switch (TREE_CODE (t))
2660 {
2661 case CONJ_CONSTR:
2662 return satisfy_conjunction (t, args, info);
2663 case DISJ_CONSTR:
2664 return satisfy_disjunction (t, args, info);
2665 case ATOMIC_CONSTR:
2666 return satisfy_atom (t, args, info);
2667 default:
2668 gcc_unreachable ();
2669 }
2670 }
2671
2672 /* Check that the normalized constraint T is satisfied for ARGS. */
2673
2674 static tree
satisfy_constraint(tree t,tree args,subst_info info)2675 satisfy_constraint (tree t, tree args, subst_info info)
2676 {
2677 auto_timevar time (TV_CONSTRAINT_SAT);
2678
2679 /* Turn off template processing. Constraint satisfaction only applies
2680 to non-dependent terms, so we want to ensure full checking here. */
2681 processing_template_decl_sentinel proc (true);
2682
2683 /* We need to check access during satisfaction. */
2684 deferring_access_check_sentinel acs (dk_no_deferred);
2685
2686 return satisfy_constraint_r (t, args, info);
2687 }
2688
2689 /* Check the normalized constraints T against ARGS, returning a satisfaction
2690 value (either true, false, or error). */
2691
2692 static tree
satisfy_associated_constraints(tree t,tree args,subst_info info)2693 satisfy_associated_constraints (tree t, tree args, subst_info info)
2694 {
2695 /* If there are no constraints then this is trivially satisfied. */
2696 if (!t)
2697 return boolean_true_node;
2698
2699 /* If any arguments depend on template parameters, we can't
2700 check constraints. Pretend they're satisfied for now. */
2701 if (args && uses_template_parms (args))
2702 return boolean_true_node;
2703
2704 return satisfy_constraint (t, args, info);
2705 }
2706
2707 /* Evaluate EXPR as a constraint expression using ARGS, returning a
2708 satisfaction value. */
2709
2710 static tree
satisfy_constraint_expression(tree t,tree args,subst_info info)2711 satisfy_constraint_expression (tree t, tree args, subst_info info)
2712 {
2713 if (t == error_mark_node)
2714 return error_mark_node;
2715
2716 gcc_assert (EXPR_P (t));
2717
2718 /* Get the normalized constraints. */
2719 tree norm;
2720 if (args == NULL_TREE && concept_check_p (t))
2721 {
2722 tree id = unpack_concept_check (t);
2723 args = TREE_OPERAND (id, 1);
2724 tree tmpl = get_concept_check_template (id);
2725 norm = normalize_concept_definition (tmpl, info.noisy ());
2726 }
2727 else
2728 norm = normalize_constraint_expression (t, info.noisy ());
2729
2730 /* Perform satisfaction. */
2731 return satisfy_constraint (norm, args, info);
2732 }
2733
2734 /* Used only to evaluate requires-expressions during constant expression
2735 evaluation. */
2736
2737 tree
satisfy_constraint_expression(tree expr)2738 satisfy_constraint_expression (tree expr)
2739 {
2740 subst_info info (tf_none, NULL_TREE);
2741 return satisfy_constraint_expression (expr, NULL_TREE, info);
2742 }
2743
2744 static tree
satisfy_declaration_constraints(tree t,subst_info info)2745 satisfy_declaration_constraints (tree t, subst_info info)
2746 {
2747 gcc_assert (DECL_P (t));
2748 const tree saved_t = t;
2749
2750 /* For inherited constructors, consider the original declaration;
2751 it has the correct template information attached. */
2752 t = strip_inheriting_ctors (t);
2753 tree inh_ctor_targs = NULL_TREE;
2754 if (t != saved_t)
2755 if (tree ti = DECL_TEMPLATE_INFO (saved_t))
2756 /* The inherited constructor points to an instantiation of a constructor
2757 template; remember its template arguments. */
2758 inh_ctor_targs = TI_ARGS (ti);
2759
2760 /* Update the declaration for diagnostics. */
2761 info.in_decl = t;
2762
2763 if (info.quiet ())
2764 if (tree *result = hash_map_safe_get (decl_satisfied_cache, saved_t))
2765 return *result;
2766
2767 /* Get the normalized constraints. */
2768 tree norm = NULL_TREE;
2769 tree args = NULL_TREE;
2770 if (tree ti = DECL_TEMPLATE_INFO (t))
2771 {
2772 tree tmpl = TI_TEMPLATE (ti);
2773 norm = normalize_template_requirements (tmpl, info.noisy ());
2774
2775 /* The initial parameter mapping is the complete set of
2776 template arguments substituted into the declaration. */
2777 args = TI_ARGS (ti);
2778 if (inh_ctor_targs)
2779 args = add_outermost_template_args (args, inh_ctor_targs);
2780 }
2781 else
2782 {
2783 /* These should be empty until we allow constraints on non-templates. */
2784 norm = normalize_nontemplate_requirements (t, info.noisy ());
2785 }
2786
2787 tree result = boolean_true_node;
2788 if (norm)
2789 {
2790 if (!push_tinst_level (t))
2791 return result;
2792 push_access_scope (t);
2793 result = satisfy_associated_constraints (norm, args, info);
2794 pop_access_scope (t);
2795 pop_tinst_level ();
2796 }
2797
2798 if (info.quiet ())
2799 hash_map_safe_put<hm_ggc> (decl_satisfied_cache, saved_t, result);
2800
2801 return result;
2802 }
2803
2804 static tree
satisfy_declaration_constraints(tree t,tree args,subst_info info)2805 satisfy_declaration_constraints (tree t, tree args, subst_info info)
2806 {
2807 /* Update the declaration for diagnostics. */
2808 info.in_decl = t;
2809
2810 gcc_assert (TREE_CODE (t) == TEMPLATE_DECL);
2811 if (tree norm = normalize_template_requirements (t, info.noisy ()))
2812 {
2813 tree pattern = DECL_TEMPLATE_RESULT (t);
2814 push_access_scope (pattern);
2815 tree result = satisfy_associated_constraints (norm, args, info);
2816 pop_access_scope (pattern);
2817 return result;
2818 }
2819
2820 return boolean_true_node;
2821 }
2822
2823 static tree
constraint_satisfaction_value(tree t,tsubst_flags_t complain)2824 constraint_satisfaction_value (tree t, tsubst_flags_t complain)
2825 {
2826 subst_info info (complain, NULL_TREE);
2827 tree r;
2828 if (DECL_P (t))
2829 r = satisfy_declaration_constraints (t, info);
2830 else
2831 r = satisfy_constraint_expression (t, NULL_TREE, info);
2832 if (r == error_mark_node && info.quiet ()
2833 && !(DECL_P (t) && TREE_NO_WARNING (t)))
2834 {
2835 constraint_satisfaction_value (t, tf_warning_or_error);
2836 if (DECL_P (t))
2837 /* Avoid giving these errors again. */
2838 TREE_NO_WARNING (t) = true;
2839 }
2840 return r;
2841 }
2842
2843 static tree
constraint_satisfaction_value(tree t,tree args,tsubst_flags_t complain)2844 constraint_satisfaction_value (tree t, tree args, tsubst_flags_t complain)
2845 {
2846 subst_info info (complain, NULL_TREE);
2847 tree r;
2848 if (DECL_P (t))
2849 r = satisfy_declaration_constraints (t, args, info);
2850 else
2851 r = satisfy_constraint_expression (t, args, info);
2852 if (r == error_mark_node && info.quiet ())
2853 constraint_satisfaction_value (t, args, tf_warning_or_error);
2854 return r;
2855 }
2856
2857 /* True iff the result of satisfying T is BOOLEAN_TRUE_NODE and false
2858 otherwise, even in the case of errors. */
2859
2860 bool
constraints_satisfied_p(tree t)2861 constraints_satisfied_p (tree t)
2862 {
2863 if (!flag_concepts)
2864 return true;
2865
2866 return constraint_satisfaction_value (t, tf_none) == boolean_true_node;
2867 }
2868
2869 /* True iff the result of satisfying T with ARGS is BOOLEAN_TRUE_NODE
2870 and false otherwise, even in the case of errors. */
2871
2872 bool
constraints_satisfied_p(tree t,tree args)2873 constraints_satisfied_p (tree t, tree args)
2874 {
2875 if (!flag_concepts)
2876 return true;
2877
2878 return constraint_satisfaction_value (t, args, tf_none) == boolean_true_node;
2879 }
2880
2881 /* Evaluate a concept check of the form C<ARGS>. This is only used for the
2882 evaluation of template-ids as id-expressions. */
2883
2884 tree
evaluate_concept_check(tree check,tsubst_flags_t complain)2885 evaluate_concept_check (tree check, tsubst_flags_t complain)
2886 {
2887 if (check == error_mark_node)
2888 return error_mark_node;
2889
2890 gcc_assert (concept_check_p (check));
2891
2892 /* Check for satisfaction without diagnostics. */
2893 subst_info quiet (tf_none, NULL_TREE);
2894 tree result = satisfy_constraint_expression (check, NULL_TREE, quiet);
2895 if (result == error_mark_node && (complain & tf_error))
2896 {
2897 /* Replay the error with re-normalized requirements. */
2898 subst_info noisy (tf_warning_or_error, NULL_TREE);
2899 satisfy_constraint_expression (check, NULL_TREE, noisy);
2900 }
2901 return result;
2902 }
2903
2904 /*---------------------------------------------------------------------------
2905 Semantic analysis of requires-expressions
2906 ---------------------------------------------------------------------------*/
2907
2908 /* Finish a requires expression for the given PARMS (possibly
2909 null) and the non-empty sequence of requirements. */
2910
2911 tree
finish_requires_expr(location_t loc,tree parms,tree reqs)2912 finish_requires_expr (location_t loc, tree parms, tree reqs)
2913 {
2914 /* Modify the declared parameters by removing their context
2915 so they don't refer to the enclosing scope and explicitly
2916 indicating that they are constraint variables. */
2917 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
2918 {
2919 DECL_CONTEXT (parm) = NULL_TREE;
2920 CONSTRAINT_VAR_P (parm) = true;
2921 }
2922
2923 /* Build the node. */
2924 tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs);
2925 TREE_SIDE_EFFECTS (r) = false;
2926 TREE_CONSTANT (r) = true;
2927 SET_EXPR_LOCATION (r, loc);
2928 return r;
2929 }
2930
2931 /* Construct a requirement for the validity of EXPR. */
2932
2933 tree
finish_simple_requirement(location_t loc,tree expr)2934 finish_simple_requirement (location_t loc, tree expr)
2935 {
2936 tree r = build_nt (SIMPLE_REQ, expr);
2937 SET_EXPR_LOCATION (r, loc);
2938 return r;
2939 }
2940
2941 /* Construct a requirement for the validity of TYPE. */
2942
2943 tree
finish_type_requirement(location_t loc,tree type)2944 finish_type_requirement (location_t loc, tree type)
2945 {
2946 tree r = build_nt (TYPE_REQ, type);
2947 SET_EXPR_LOCATION (r, loc);
2948 return r;
2949 }
2950
2951 /* Construct a requirement for the validity of EXPR, along with
2952 its properties. if TYPE is non-null, then it specifies either
2953 an implicit conversion or argument deduction constraint,
2954 depending on whether any placeholders occur in the type name.
2955 NOEXCEPT_P is true iff the noexcept keyword was specified. */
2956
2957 tree
finish_compound_requirement(location_t loc,tree expr,tree type,bool noexcept_p)2958 finish_compound_requirement (location_t loc, tree expr, tree type, bool noexcept_p)
2959 {
2960 tree req = build_nt (COMPOUND_REQ, expr, type);
2961 SET_EXPR_LOCATION (req, loc);
2962 COMPOUND_REQ_NOEXCEPT_P (req) = noexcept_p;
2963 return req;
2964 }
2965
2966 /* Finish a nested requirement. */
2967
2968 tree
finish_nested_requirement(location_t loc,tree expr)2969 finish_nested_requirement (location_t loc, tree expr)
2970 {
2971 /* Currently open template headers have dummy arg vectors, so don't
2972 pass into normalization. */
2973 tree norm = normalize_constraint_expression (expr, NULL_TREE, false);
2974 tree args = current_template_parms
2975 ? template_parms_to_args (current_template_parms) : NULL_TREE;
2976
2977 /* Save the normalized constraint and complete set of normalization
2978 arguments with the requirement. We keep the complete set of arguments
2979 around for re-normalization during diagnostics. */
2980 tree info = build_tree_list (args, norm);
2981
2982 /* Build the constraint, saving its normalization as its type. */
2983 tree r = build1 (NESTED_REQ, info, expr);
2984 SET_EXPR_LOCATION (r, loc);
2985 return r;
2986 }
2987
2988 /* Check that FN satisfies the structural requirements of a
2989 function concept definition. */
2990 tree
check_function_concept(tree fn)2991 check_function_concept (tree fn)
2992 {
2993 /* Check that the function is comprised of only a return statement. */
2994 tree body = DECL_SAVED_TREE (fn);
2995 if (TREE_CODE (body) == BIND_EXPR)
2996 body = BIND_EXPR_BODY (body);
2997
2998 /* Sometimes a function call results in the creation of clean up
2999 points. Allow these to be preserved in the body of the
3000 constraint, as we might actually need them for some constexpr
3001 evaluations. */
3002 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
3003 body = TREE_OPERAND (body, 0);
3004
3005 /* Check that the definition is written correctly. */
3006 if (TREE_CODE (body) != RETURN_EXPR)
3007 {
3008 location_t loc = DECL_SOURCE_LOCATION (fn);
3009 if (TREE_CODE (body) == STATEMENT_LIST && !STATEMENT_LIST_HEAD (body))
3010 {
3011 if (seen_error ())
3012 /* The definition was probably erroneous, not empty. */;
3013 else
3014 error_at (loc, "definition of concept %qD is empty", fn);
3015 }
3016 else
3017 error_at (loc, "definition of concept %qD has multiple statements", fn);
3018 }
3019
3020 return NULL_TREE;
3021 }
3022
3023
3024 // Check that a constrained friend declaration function declaration,
3025 // FN, is admissible. This is the case only when the declaration depends
3026 // on template parameters and does not declare a specialization.
3027 void
check_constrained_friend(tree fn,tree reqs)3028 check_constrained_friend (tree fn, tree reqs)
3029 {
3030 if (fn == error_mark_node)
3031 return;
3032 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
3033
3034 // If there are not constraints, this cannot be an error.
3035 if (!reqs)
3036 return;
3037
3038 // Constrained friend functions that don't depend on template
3039 // arguments are effectively meaningless.
3040 if (!uses_template_parms (TREE_TYPE (fn)))
3041 {
3042 error_at (location_of (fn),
3043 "constrained friend does not depend on template parameters");
3044 return;
3045 }
3046 }
3047
3048 /*---------------------------------------------------------------------------
3049 Equivalence of constraints
3050 ---------------------------------------------------------------------------*/
3051
3052 /* Returns true when A and B are equivalent constraints. */
3053 bool
equivalent_constraints(tree a,tree b)3054 equivalent_constraints (tree a, tree b)
3055 {
3056 gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
3057 gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
3058 return cp_tree_equal (a, b);
3059 }
3060
3061 /* Returns true if the template declarations A and B have equivalent
3062 constraints. This is the case when A's constraints subsume B's and
3063 when B's also constrain A's. */
3064 bool
equivalently_constrained(tree d1,tree d2)3065 equivalently_constrained (tree d1, tree d2)
3066 {
3067 gcc_assert (TREE_CODE (d1) == TREE_CODE (d2));
3068 return equivalent_constraints (get_constraints (d1), get_constraints (d2));
3069 }
3070
3071 /*---------------------------------------------------------------------------
3072 Partial ordering of constraints
3073 ---------------------------------------------------------------------------*/
3074
3075 /* Returns true when the constraints in A subsume those in B. */
3076
3077 bool
subsumes_constraints(tree a,tree b)3078 subsumes_constraints (tree a, tree b)
3079 {
3080 gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
3081 gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
3082 return subsumes (a, b);
3083 }
3084
3085 /* Returns true when the constraints in CI (with arguments
3086 ARGS) strictly subsume the associated constraints of TMPL. */
3087
3088 bool
strictly_subsumes(tree ci,tree args,tree tmpl)3089 strictly_subsumes (tree ci, tree args, tree tmpl)
3090 {
3091 tree n1 = get_normalized_constraints_from_info (ci, args, NULL_TREE);
3092 tree n2 = get_normalized_constraints_from_decl (tmpl);
3093
3094 return subsumes (n1, n2) && !subsumes (n2, n1);
3095 }
3096
3097 /* REturns true when the constraints in CI (with arguments ARGS) subsume
3098 the associated constraints of TMPL. */
3099
3100 bool
weakly_subsumes(tree ci,tree args,tree tmpl)3101 weakly_subsumes (tree ci, tree args, tree tmpl)
3102 {
3103 tree n1 = get_normalized_constraints_from_info (ci, args, NULL_TREE);
3104 tree n2 = get_normalized_constraints_from_decl (tmpl);
3105
3106 return subsumes (n1, n2);
3107 }
3108
3109 /* Determines which of the declarations, A or B, is more constrained.
3110 That is, which declaration's constraints subsume but are not subsumed
3111 by the other's?
3112
3113 Returns 1 if D1 is more constrained than D2, -1 if D2 is more constrained
3114 than D1, and 0 otherwise. */
3115
3116 int
more_constrained(tree d1,tree d2)3117 more_constrained (tree d1, tree d2)
3118 {
3119 tree n1 = get_normalized_constraints_from_decl (d1);
3120 tree n2 = get_normalized_constraints_from_decl (d2);
3121
3122 int winner = 0;
3123 if (subsumes (n1, n2))
3124 ++winner;
3125 if (subsumes (n2, n1))
3126 --winner;
3127 return winner;
3128 }
3129
3130 /* Return whether D1 is at least as constrained as D2. */
3131
3132 bool
at_least_as_constrained(tree d1,tree d2)3133 at_least_as_constrained (tree d1, tree d2)
3134 {
3135 tree n1 = get_normalized_constraints_from_decl (d1);
3136 tree n2 = get_normalized_constraints_from_decl (d2);
3137
3138 return subsumes (n1, n2);
3139 }
3140
3141 /*---------------------------------------------------------------------------
3142 Constraint diagnostics
3143 ---------------------------------------------------------------------------*/
3144
3145 /* Returns the best location to diagnose a constraint error. */
3146
3147 static location_t
get_constraint_error_location(tree t)3148 get_constraint_error_location (tree t)
3149 {
3150 if (location_t loc = cp_expr_location (t))
3151 return loc;
3152
3153 /* If we have a specific location give it. */
3154 tree expr = CONSTR_EXPR (t);
3155 if (location_t loc = cp_expr_location (expr))
3156 return loc;
3157
3158 /* If the constraint is normalized from a requires-clause, give
3159 the location as that of the constrained declaration. */
3160 tree cxt = CONSTR_CONTEXT (t);
3161 tree src = cxt ? TREE_VALUE (cxt) : NULL_TREE;
3162 if (!src)
3163 /* TODO: This only happens for constrained non-template declarations. */
3164 ;
3165 else if (DECL_P (src))
3166 return DECL_SOURCE_LOCATION (src);
3167 /* Otherwise, give the location as the defining concept. */
3168 else if (concept_check_p (src))
3169 {
3170 tree id = unpack_concept_check (src);
3171 tree tmpl = TREE_OPERAND (id, 0);
3172 if (OVL_P (tmpl))
3173 tmpl = OVL_FIRST (tmpl);
3174 return DECL_SOURCE_LOCATION (tmpl);
3175 }
3176
3177 return input_location;
3178 }
3179
3180 /* Emit a diagnostic for a failed trait. */
3181
3182 void
diagnose_trait_expr(tree expr,tree map)3183 diagnose_trait_expr (tree expr, tree map)
3184 {
3185 location_t loc = cp_expr_location (expr);
3186 tree args = get_mapped_args (map);
3187
3188 /* Build a "fake" version of the instantiated trait, so we can
3189 get the instantiated types from result. */
3190 ++processing_template_decl;
3191 expr = tsubst_expr (expr, args, tf_none, NULL_TREE, false);
3192 --processing_template_decl;
3193
3194 tree t1 = TRAIT_EXPR_TYPE1 (expr);
3195 tree t2 = TRAIT_EXPR_TYPE2 (expr);
3196 switch (TRAIT_EXPR_KIND (expr))
3197 {
3198 case CPTK_HAS_NOTHROW_ASSIGN:
3199 inform (loc, " %qT is not %<nothrow%> copy assignable", t1);
3200 break;
3201 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
3202 inform (loc, " %qT is not %<nothrow%> default constructible", t1);
3203 break;
3204 case CPTK_HAS_NOTHROW_COPY:
3205 inform (loc, " %qT is not %<nothrow%> copy constructible", t1);
3206 break;
3207 case CPTK_HAS_TRIVIAL_ASSIGN:
3208 inform (loc, " %qT is not trivially copy assignable", t1);
3209 break;
3210 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
3211 inform (loc, " %qT is not trivially default constructible", t1);
3212 break;
3213 case CPTK_HAS_TRIVIAL_COPY:
3214 inform (loc, " %qT is not trivially copy constructible", t1);
3215 break;
3216 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
3217 inform (loc, " %qT is not trivially destructible", t1);
3218 break;
3219 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
3220 inform (loc, " %qT does not have a virtual destructor", t1);
3221 break;
3222 case CPTK_IS_ABSTRACT:
3223 inform (loc, " %qT is not an abstract class", t1);
3224 break;
3225 case CPTK_IS_BASE_OF:
3226 inform (loc, " %qT is not a base of %qT", t1, t2);
3227 break;
3228 case CPTK_IS_CLASS:
3229 inform (loc, " %qT is not a class", t1);
3230 break;
3231 case CPTK_IS_EMPTY:
3232 inform (loc, " %qT is not an empty class", t1);
3233 break;
3234 case CPTK_IS_ENUM:
3235 inform (loc, " %qT is not an enum", t1);
3236 break;
3237 case CPTK_IS_FINAL:
3238 inform (loc, " %qT is not a final class", t1);
3239 break;
3240 case CPTK_IS_LITERAL_TYPE:
3241 inform (loc, " %qT is not a literal type", t1);
3242 break;
3243 case CPTK_IS_POD:
3244 inform (loc, " %qT is not a POD type", t1);
3245 break;
3246 case CPTK_IS_POLYMORPHIC:
3247 inform (loc, " %qT is not a polymorphic type", t1);
3248 break;
3249 case CPTK_IS_SAME_AS:
3250 inform (loc, " %qT is not the same as %qT", t1, t2);
3251 break;
3252 case CPTK_IS_STD_LAYOUT:
3253 inform (loc, " %qT is not an standard layout type", t1);
3254 break;
3255 case CPTK_IS_TRIVIAL:
3256 inform (loc, " %qT is not a trivial type", t1);
3257 break;
3258 case CPTK_IS_UNION:
3259 inform (loc, " %qT is not a union", t1);
3260 break;
3261 default:
3262 gcc_unreachable ();
3263 }
3264 }
3265
3266 static tree
diagnose_valid_expression(tree expr,tree args,tree in_decl)3267 diagnose_valid_expression (tree expr, tree args, tree in_decl)
3268 {
3269 tree result = tsubst_expr (expr, args, tf_none, in_decl, false);
3270 if (result != error_mark_node
3271 && convert_to_void (result, ICV_STATEMENT, tf_none) != error_mark_node)
3272 return result;
3273
3274 location_t loc = cp_expr_loc_or_input_loc (expr);
3275 if (diagnosing_failed_constraint::replay_errors_p ())
3276 {
3277 /* Replay the substitution error. */
3278 inform (loc, "the required expression %qE is invalid, because", expr);
3279 if (result == error_mark_node)
3280 tsubst_expr (expr, args, tf_error, in_decl, false);
3281 else
3282 convert_to_void (result, ICV_STATEMENT, tf_error);
3283 }
3284 else
3285 inform (loc, "the required expression %qE is invalid", expr);
3286
3287 return error_mark_node;
3288 }
3289
3290 static tree
diagnose_valid_type(tree type,tree args,tree in_decl)3291 diagnose_valid_type (tree type, tree args, tree in_decl)
3292 {
3293 tree result = tsubst (type, args, tf_none, in_decl);
3294 if (result != error_mark_node)
3295 return result;
3296
3297 location_t loc = cp_expr_loc_or_input_loc (type);
3298 if (diagnosing_failed_constraint::replay_errors_p ())
3299 {
3300 /* Replay the substitution error. */
3301 inform (loc, "the required type %qT is invalid, because", type);
3302 tsubst (type, args, tf_error, in_decl);
3303 }
3304 else
3305 inform (loc, "the required type %qT is invalid", type);
3306
3307 return error_mark_node;
3308 }
3309
3310 static void
diagnose_simple_requirement(tree req,tree args,tree in_decl)3311 diagnose_simple_requirement (tree req, tree args, tree in_decl)
3312 {
3313 diagnose_valid_expression (TREE_OPERAND (req, 0), args, in_decl);
3314 }
3315
3316 static void
diagnose_compound_requirement(tree req,tree args,tree in_decl)3317 diagnose_compound_requirement (tree req, tree args, tree in_decl)
3318 {
3319 tree expr = TREE_OPERAND (req, 0);
3320 expr = diagnose_valid_expression (expr, args, in_decl);
3321 if (expr == error_mark_node)
3322 return;
3323
3324 location_t loc = cp_expr_loc_or_input_loc (expr);
3325
3326 /* Check the noexcept condition. */
3327 if (COMPOUND_REQ_NOEXCEPT_P (req) && !expr_noexcept_p (expr, tf_none))
3328 inform (loc, "%qE is not %<noexcept%>", expr);
3329
3330 tree type = TREE_OPERAND (req, 1);
3331 type = diagnose_valid_type (type, args, in_decl);
3332 if (type == error_mark_node)
3333 return;
3334
3335 if (type)
3336 {
3337 subst_info quiet (tf_none, in_decl);
3338 subst_info noisy (tf_error, in_decl);
3339
3340 /* Check the expression against the result type. */
3341 if (tree placeholder = type_uses_auto (type))
3342 {
3343 if (!type_deducible_p (expr, type, placeholder, args, quiet))
3344 {
3345 tree orig_expr = TREE_OPERAND (req, 0);
3346 if (diagnosing_failed_constraint::replay_errors_p ())
3347 {
3348 inform (loc,
3349 "%qE does not satisfy return-type-requirement, "
3350 "because", orig_expr);
3351 /* Further explain the reason for the error. */
3352 type_deducible_p (expr, type, placeholder, args, noisy);
3353 }
3354 else
3355 inform (loc, "%qE does not satisfy return-type-requirement",
3356 orig_expr);
3357 }
3358 }
3359 else if (!expression_convertible_p (expr, type, quiet))
3360 {
3361 tree orig_expr = TREE_OPERAND (req, 0);
3362 if (diagnosing_failed_constraint::replay_errors_p ())
3363 {
3364 inform (loc, "cannot convert %qE to %qT because", orig_expr, type);
3365 /* Further explain the reason for the error. */
3366 expression_convertible_p (expr, type, noisy);
3367 }
3368 else
3369 inform (loc, "cannot convert %qE to %qT", orig_expr, type);
3370 }
3371 }
3372 }
3373
3374 static void
diagnose_type_requirement(tree req,tree args,tree in_decl)3375 diagnose_type_requirement (tree req, tree args, tree in_decl)
3376 {
3377 tree type = TREE_OPERAND (req, 0);
3378 diagnose_valid_type (type, args, in_decl);
3379 }
3380
3381 static void
diagnose_nested_requirement(tree req,tree args)3382 diagnose_nested_requirement (tree req, tree args)
3383 {
3384 /* Quietly check for satisfaction first. We can elaborate details
3385 later if needed. */
3386 tree norm = TREE_VALUE (TREE_TYPE (req));
3387 subst_info info (tf_none, NULL_TREE);
3388 tree result = satisfy_constraint (norm, args, info);
3389 if (result == boolean_true_node)
3390 return;
3391
3392 tree expr = TREE_OPERAND (req, 0);
3393 location_t loc = cp_expr_location (expr);
3394 if (diagnosing_failed_constraint::replay_errors_p ())
3395 {
3396 /* Replay the substitution error. */
3397 inform (loc, "nested requirement %qE is not satisfied, because", expr);
3398 subst_info noisy (tf_warning_or_error, NULL_TREE);
3399 satisfy_constraint_expression (expr, args, noisy);
3400 }
3401 else
3402 inform (loc, "nested requirement %qE is not satisfied", expr);
3403
3404 }
3405
3406 static void
diagnose_requirement(tree req,tree args,tree in_decl)3407 diagnose_requirement (tree req, tree args, tree in_decl)
3408 {
3409 iloc_sentinel loc_s (cp_expr_location (req));
3410 switch (TREE_CODE (req))
3411 {
3412 case SIMPLE_REQ:
3413 return diagnose_simple_requirement (req, args, in_decl);
3414 case COMPOUND_REQ:
3415 return diagnose_compound_requirement (req, args, in_decl);
3416 case TYPE_REQ:
3417 return diagnose_type_requirement (req, args, in_decl);
3418 case NESTED_REQ:
3419 return diagnose_nested_requirement (req, args);
3420 default:
3421 gcc_unreachable ();
3422 }
3423 }
3424
3425 static void
diagnose_requires_expr(tree expr,tree map,tree in_decl)3426 diagnose_requires_expr (tree expr, tree map, tree in_decl)
3427 {
3428 local_specialization_stack stack (lss_copy);
3429 tree parms = TREE_OPERAND (expr, 0);
3430 tree body = TREE_OPERAND (expr, 1);
3431 tree args = get_mapped_args (map);
3432
3433 cp_unevaluated u;
3434 subst_info info (tf_warning_or_error, NULL_TREE);
3435 tree vars = tsubst_constraint_variables (parms, args, info);
3436 if (vars == error_mark_node)
3437 return;
3438
3439 tree p = body;
3440 while (p)
3441 {
3442 tree req = TREE_VALUE (p);
3443 diagnose_requirement (req, args, in_decl);
3444 p = TREE_CHAIN (p);
3445 }
3446 }
3447
3448 /* Diagnose a substitution failure in the atomic constraint T when applied
3449 with the instantiated parameter mapping MAP. */
3450
3451 static void
diagnose_atomic_constraint(tree t,tree map,tree result,subst_info info)3452 diagnose_atomic_constraint (tree t, tree map, tree result, subst_info info)
3453 {
3454 /* If the constraint is already ill-formed, we've previously diagnosed
3455 the reason. We should still say why the constraints aren't satisfied. */
3456 if (t == error_mark_node)
3457 {
3458 location_t loc;
3459 if (info.in_decl)
3460 loc = DECL_SOURCE_LOCATION (info.in_decl);
3461 else
3462 loc = input_location;
3463 inform (loc, "invalid constraints");
3464 return;
3465 }
3466
3467 location_t loc = get_constraint_error_location (t);
3468 iloc_sentinel loc_s (loc);
3469
3470 /* Generate better diagnostics for certain kinds of expressions. */
3471 tree expr = ATOMIC_CONSTR_EXPR (t);
3472 STRIP_ANY_LOCATION_WRAPPER (expr);
3473 switch (TREE_CODE (expr))
3474 {
3475 case TRAIT_EXPR:
3476 diagnose_trait_expr (expr, map);
3477 break;
3478 case REQUIRES_EXPR:
3479 diagnose_requires_expr (expr, map, info.in_decl);
3480 break;
3481 default:
3482 tree a = copy_node (t);
3483 ATOMIC_CONSTR_MAP (a) = map;
3484 if (!same_type_p (TREE_TYPE (result), boolean_type_node))
3485 error_at (loc, "constraint %qE has type %qT, not %<bool%>",
3486 a, TREE_TYPE (result));
3487 else
3488 inform (loc, "the expression %qE evaluated to %<false%>", a);
3489 ggc_free (a);
3490 }
3491 }
3492
3493 GTY(()) tree current_failed_constraint;
3494
3495 diagnosing_failed_constraint::
diagnosing_failed_constraint(tree t,tree args,bool diag)3496 diagnosing_failed_constraint (tree t, tree args, bool diag)
3497 : diagnosing_error (diag)
3498 {
3499 if (diagnosing_error)
3500 {
3501 current_failed_constraint
3502 = tree_cons (args, t, current_failed_constraint);
3503 ++current_constraint_diagnosis_depth;
3504 }
3505 }
3506
3507 diagnosing_failed_constraint::
~diagnosing_failed_constraint()3508 ~diagnosing_failed_constraint ()
3509 {
3510 if (diagnosing_error)
3511 {
3512 --current_constraint_diagnosis_depth;
3513 if (current_failed_constraint)
3514 current_failed_constraint = TREE_CHAIN (current_failed_constraint);
3515 }
3516
3517 }
3518
3519 /* Whether we are allowed to replay an error that underlies a constraint failure
3520 at the current diagnosis depth. */
3521
3522 bool
replay_errors_p()3523 diagnosing_failed_constraint::replay_errors_p ()
3524 {
3525 if (current_constraint_diagnosis_depth >= concepts_diagnostics_max_depth)
3526 {
3527 concepts_diagnostics_max_depth_exceeded_p = true;
3528 return false;
3529 }
3530 else
3531 return true;
3532 }
3533
3534 /* Emit diagnostics detailing the failure ARGS to satisfy the constraints
3535 of T. Here, T can be either a constraint or a declaration. */
3536
3537 void
diagnose_constraints(location_t loc,tree t,tree args)3538 diagnose_constraints (location_t loc, tree t, tree args)
3539 {
3540 inform (loc, "constraints not satisfied");
3541
3542 if (concepts_diagnostics_max_depth == 0)
3543 return;
3544
3545 /* Replay satisfaction, but diagnose errors. */
3546 if (!args)
3547 constraint_satisfaction_value (t, tf_warning_or_error);
3548 else
3549 constraint_satisfaction_value (t, args, tf_warning_or_error);
3550
3551 static bool suggested_p;
3552 if (concepts_diagnostics_max_depth_exceeded_p
3553 && current_constraint_diagnosis_depth == 0
3554 && !suggested_p)
3555 {
3556 inform (UNKNOWN_LOCATION,
3557 "set %qs to at least %d for more detail",
3558 "-fconcepts-diagnostics-depth=",
3559 concepts_diagnostics_max_depth + 1);
3560 suggested_p = true;
3561 }
3562 }
3563
3564 #include "gt-cp-constraint.h"
3565