09ae301f0deb668603ce065842943cc56c42f83e
[platform/upstream/linaro-gcc.git] / gcc / cp / constraint.cc
1 /* Processing rules for constraints.
2    Copyright (C) 2013-2016 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 /*---------------------------------------------------------------------------
50                        Operations on constraints
51 ---------------------------------------------------------------------------*/
52
53 /* Returns true if C is a constraint tree code. Note that ERROR_MARK
54    is a valid constraint.  */
55
56 static inline bool
57 constraint_p (tree_code c)
58 {
59   return ((PRED_CONSTR <= c && c <= DISJ_CONSTR)
60           || c == EXPR_PACK_EXPANSION
61           || c == ERROR_MARK);
62 }
63
64 /* Returns true if T is a constraint. Note that error_mark_node
65    is a valid constraint.  */
66
67 bool
68 constraint_p (tree t)
69 {
70   return constraint_p (TREE_CODE (t));
71 }
72
73 /* Returns the conjunction of two constraints A and B. Note that
74    conjoining a non-null constraint with NULL_TREE is an identity
75    operation. That is, for non-null A,
76
77       conjoin_constraints(a, NULL_TREE) == a
78
79    and
80
81       conjoin_constraints (NULL_TREE, a) == a
82
83    If both A and B are NULL_TREE, the result is also NULL_TREE. */
84
85 tree
86 conjoin_constraints (tree a, tree b)
87 {
88   gcc_assert (a ? constraint_p (a) : true);
89   gcc_assert (b ? constraint_p (b) : true);
90   if (a)
91     return b ? build_nt (CONJ_CONSTR, a, b) : a;
92   else if (b)
93     return b;
94   else
95     return NULL_TREE;
96 }
97
98 /* Transform the vector of expressions in the T into a conjunction
99    of requirements. T must be a TREE_VEC. */
100
101 tree
102 conjoin_constraints (tree t)
103 {
104   gcc_assert (TREE_CODE (t) == TREE_VEC);
105   tree r = NULL_TREE;
106   for (int i = 0; i < TREE_VEC_LENGTH (t); ++i)
107     r = conjoin_constraints (r, TREE_VEC_ELT (t, i));
108   return r;
109 }
110
111 /* Returns true if T is a call expression to a function
112    concept. */
113
114 bool
115 function_concept_check_p (tree t)
116 {
117   gcc_assert (TREE_CODE (t) == CALL_EXPR);
118   tree fn = CALL_EXPR_FN (t);
119   if (TREE_CODE (fn) == TEMPLATE_ID_EXPR
120       && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
121     {
122       tree f1 = get_first_fn (fn);
123       if (TREE_CODE (f1) == TEMPLATE_DECL
124           && DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (f1)))
125         return true;
126     }
127   return false;
128 }
129
130 /* Returns true if any of the arguments in the template
131    argument list is a wildcard or wildcard pack.  */
132
133 bool
134 contains_wildcard_p (tree args)
135 {
136   for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
137     {
138       tree arg = TREE_VEC_ELT (args, i);
139       if (TREE_CODE (arg) == WILDCARD_DECL)
140         return true;
141     }
142   return false;
143 }
144
145 /* Build a new call expression, but don't actually generate a
146    new function call. We just want the tree, not the semantics.  */
147
148 inline tree
149 build_call_check (tree id)
150 {
151   ++processing_template_decl;
152   vec<tree, va_gc> *fargs = make_tree_vector();
153   tree call = finish_call_expr (id, &fargs, false, false, tf_none);
154   release_tree_vector (fargs);
155   --processing_template_decl;
156   return call;
157 }
158
159 /* Build an expression that will check a variable concept. If any
160    argument contains a wildcard, don't try to finish the variable
161    template because we can't substitute into a non-existent
162    declaration.  */
163
164 tree
165 build_variable_check (tree id)
166 {
167   gcc_assert (TREE_CODE (id) == TEMPLATE_ID_EXPR);
168   if (contains_wildcard_p (TREE_OPERAND (id, 1)))
169     return id;
170
171   ++processing_template_decl;
172   tree var = finish_template_variable (id);
173   --processing_template_decl;
174   return var;
175 }
176
177 /*---------------------------------------------------------------------------
178                     Resolution of qualified concept names
179 ---------------------------------------------------------------------------*/
180
181 /* This facility is used to resolve constraint checks from
182    requirement expressions. A constraint check is a call to
183    a function template declared with the keyword 'concept'.
184
185    The result of resolution is a pair (a TREE_LIST) whose value
186    is the matched declaration, and whose purpose contains the
187    coerced template arguments that can be substituted into the
188    call.  */
189
190 // Given an overload set OVL, try to find a unique definition that can be
191 // instantiated by the template arguments ARGS.
192 //
193 // This function is not called for arbitrary call expressions. In particular,
194 // the call expression must be written with explicit template arguments
195 // and no function arguments. For example:
196 //
197 //      f<T, U>()
198 //
199 // If a single match is found, this returns a TREE_LIST whose VALUE
200 // is the constraint function (not the template), and its PURPOSE is
201 // the complete set of arguments substituted into the parameter list.
202 static tree
203 resolve_constraint_check (tree ovl, tree args)
204 {
205   int nerrs = 0;
206   tree cands = NULL_TREE;
207   for (tree p = ovl; p != NULL_TREE; p = OVL_NEXT (p))
208     {
209       // Get the next template overload.
210       tree tmpl = OVL_CURRENT (p);
211       if (TREE_CODE (tmpl) != TEMPLATE_DECL)
212         continue;
213
214       // Don't try to deduce checks for non-concepts. We often
215       // end up trying to resolve constraints in functional casts
216       // as part of a postfix-expression. We can save time and
217       // headaches by not instantiating those declarations.
218       //
219       // NOTE: This masks a potential error, caused by instantiating
220       // non-deduced contexts using placeholder arguments.
221       tree fn = DECL_TEMPLATE_RESULT (tmpl);
222       if (DECL_ARGUMENTS (fn))
223         continue;
224       if (!DECL_DECLARED_CONCEPT_P (fn))
225         continue;
226
227       // Remember the candidate if we can deduce a substitution.
228       ++processing_template_decl;
229       tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl));
230       if (tree subst = coerce_template_parms (parms, args, tmpl))
231         {
232           if (subst == error_mark_node)
233             ++nerrs;
234           else
235             cands = tree_cons (subst, fn, cands);
236         }
237       --processing_template_decl;
238     }
239
240   if (!cands)
241     /* We either had no candidates or failed deductions.  */
242     return nerrs ? error_mark_node : NULL_TREE;
243   else if (TREE_CHAIN (cands))
244     /* There are multiple candidates.  */
245     return error_mark_node;
246
247   return cands;
248 }
249
250 // Determine if the the call expression CALL is a constraint check, and
251 // return the concept declaration and arguments being checked. If CALL
252 // does not denote a constraint check, return NULL.
253 tree
254 resolve_constraint_check (tree call)
255 {
256   gcc_assert (TREE_CODE (call) == CALL_EXPR);
257
258   // A constraint check must be only a template-id expression. If
259   // it's a call to a base-link, its function(s) should be a
260   // template-id expression. If this is not a template-id, then it
261   // cannot be a concept-check.
262   tree target = CALL_EXPR_FN (call);
263   if (BASELINK_P (target))
264     target = BASELINK_FUNCTIONS (target);
265   if (TREE_CODE (target) != TEMPLATE_ID_EXPR)
266     return NULL_TREE;
267
268   // Get the overload set and template arguments and try to
269   // resolve the target.
270   tree ovl = TREE_OPERAND (target, 0);
271
272   /* This is a function call of a variable concept... ill-formed. */
273   if (TREE_CODE (ovl) == TEMPLATE_DECL)
274     {
275       error_at (location_of (call),
276                 "function call of variable concept %qE", call);
277       return error_mark_node;
278     }
279
280   tree args = TREE_OPERAND (target, 1);
281   return resolve_constraint_check (ovl, args);
282 }
283
284 /* Returns a pair containing the checked variable concept
285    and its associated prototype parameter.  The result
286    is a TREE_LIST whose TREE_VALUE is the variable concept
287    and whose TREE_PURPOSE is the prototype parameter.  */
288
289 tree
290 resolve_variable_concept_check (tree id)
291 {
292   tree tmpl = TREE_OPERAND (id, 0);
293   tree args = TREE_OPERAND (id, 1);
294
295   if (!variable_concept_p (tmpl))
296     return NULL_TREE;
297
298   /* Make sure that we have the right parameters before
299      assuming that it works.  Note that failing to deduce
300      will result in diagnostics.  */
301   tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
302   ++processing_template_decl;
303   tree result = coerce_template_parms (parms, args, tmpl);
304   --processing_template_decl;
305   if (result != error_mark_node)
306     {
307       tree decl = DECL_TEMPLATE_RESULT (tmpl);
308       return build_tree_list (result, decl);
309     }
310   else
311     return error_mark_node;
312 }
313
314
315 /* Given a call expression or template-id expression to
316   a concept EXPR possibly including a wildcard, deduce
317   the concept being checked and the prototype parameter.
318   Returns true if the constraint and prototype can be
319   deduced and false otherwise.  Note that the CHECK and
320   PROTO arguments are set to NULL_TREE if this returns
321   false.  */
322
323 bool
324 deduce_constrained_parameter (tree expr, tree& check, tree& proto)
325 {
326   tree info = NULL_TREE;
327   if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
328     info = resolve_variable_concept_check (expr);
329   else if (TREE_CODE (expr) == CALL_EXPR)
330     info = resolve_constraint_check (expr);
331   else
332     gcc_unreachable ();
333
334   if (info && info != error_mark_node)
335     {
336       check = TREE_VALUE (info);
337       tree arg = TREE_VEC_ELT (TREE_PURPOSE (info), 0);
338       if (ARGUMENT_PACK_P (arg))
339         arg = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0);
340       proto = TREE_TYPE (arg);
341       return true;
342     }
343   check = proto = NULL_TREE;
344   return false;
345 }
346
347 // Given a call expression or template-id expression to a concept, EXPR,
348 // deduce the concept being checked and return the template arguments.
349 // Returns NULL_TREE if deduction fails.
350 static tree
351 deduce_concept_introduction (tree expr)
352 {
353   tree info = NULL_TREE;
354   if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
355     info = resolve_variable_concept_check (expr);
356   else if (TREE_CODE (expr) == CALL_EXPR)
357     info = resolve_constraint_check (expr);
358   else
359     gcc_unreachable ();
360
361   if (info && info != error_mark_node)
362     return TREE_PURPOSE (info);
363   return NULL_TREE;
364 }
365
366 namespace {
367
368 /*---------------------------------------------------------------------------
369                        Constraint implication learning
370 ---------------------------------------------------------------------------*/
371
372 /* The implication context determines how we memoize concept checks.
373    Given two checks C1 and C2, the direction of implication depends
374    on whether we are learning implications of a conjunction or disjunction.
375    For example:
376
377       template<typename T> concept bool C = ...;
378       template<typenaem T> concept bool D = C<T> && true;
379
380    From this, we can learn that D<T> implies C<T>. We cannot learn,
381    without further testing, that C<T> does not imply D<T>. If, for
382    example, C<T> were defined as true, then these constraints would
383    be logically equivalent.
384
385    In rare cases, we may start with a logical equivalence. For example:
386
387       template<typename T> concept bool C = ...;
388       template<typename T> concept bool D = C<T>;
389
390    Here, we learn that C<T> implies D<T> and vice versa.   */
391
392 enum implication_context
393 {
394   conjunction_cxt, /* C1 implies C2. */
395   disjunction_cxt, /* C2 implies C1. */
396   equivalence_cxt  /* C1 implies C2, C2 implies C1. */
397 };
398
399 void learn_implications(tree, tree, implication_context);
400
401 void
402 learn_implication (tree parent, tree child, implication_context cxt)
403 {
404   switch (cxt)
405     {
406       case conjunction_cxt:
407         save_subsumption_result (parent, child, true);
408         break;
409       case disjunction_cxt:
410         save_subsumption_result (child, parent, true);
411         break;
412       case equivalence_cxt:
413         save_subsumption_result (parent, child, true);
414         save_subsumption_result (child, parent, true);
415         break;
416     }
417 }
418
419 void
420 learn_logical_operation (tree parent, tree constr, implication_context cxt)
421 {
422   learn_implications (parent, TREE_OPERAND (constr, 0), cxt);
423   learn_implications (parent, TREE_OPERAND (constr, 1), cxt);
424 }
425
426 void
427 learn_implications (tree parent, tree constr, implication_context cxt)
428 {
429   switch (TREE_CODE (constr))
430     {
431       case CHECK_CONSTR:
432         return learn_implication (parent, constr, cxt);
433
434       case CONJ_CONSTR:
435         if (cxt == disjunction_cxt)
436           return;
437         return learn_logical_operation (parent, constr, cxt);
438
439       case DISJ_CONSTR:
440         if (cxt == conjunction_cxt)
441           return;
442         return learn_logical_operation (parent, constr, cxt);
443
444       default:
445         break;
446     }
447 }
448
449 /* Quickly scan the top-level constraints of CONSTR to learn and
450    cache logical relations between concepts.  The search does not
451    include conjunctions of disjunctions or vice versa.  */
452
453 void
454 learn_implications (tree tmpl, tree args, tree constr)
455 {
456   /* Don't memoize relations between non-dependent arguemnts. It's not
457      helpful. */
458   if (!uses_template_parms (args))
459     return;
460
461   /* Build a check constraint for the purpose of caching. */
462   tree parent = build_nt (CHECK_CONSTR, tmpl, args);
463
464   /* Start learning based on the kind of the top-level contraint. */
465   if (TREE_CODE (constr) == CONJ_CONSTR)
466     return learn_logical_operation (parent, constr, conjunction_cxt);
467   else if (TREE_CODE (constr) == DISJ_CONSTR)
468     return learn_logical_operation (parent, constr, disjunction_cxt);
469   else if (TREE_CODE (constr) == CHECK_CONSTR)
470     /* This is the rare concept alias case. */
471     return learn_implication (parent, constr, equivalence_cxt);
472 }
473
474 /*---------------------------------------------------------------------------
475                        Expansion of concept definitions
476 ---------------------------------------------------------------------------*/
477
478 /* Returns the expression of a function concept. */
479
480 tree
481 get_returned_expression (tree fn)
482 {
483   /* Extract the body of the function minus the return expression.  */
484   tree body = DECL_SAVED_TREE (fn);
485   if (!body)
486     return error_mark_node;
487   if (TREE_CODE (body) == BIND_EXPR)
488     body = BIND_EXPR_BODY (body);
489   if (TREE_CODE (body) != RETURN_EXPR)
490     return error_mark_node;
491
492   return TREE_OPERAND (body, 0);
493 }
494
495 /* Returns the initializer of a variable concept. */
496
497 tree
498 get_variable_initializer (tree var)
499 {
500   tree init = DECL_INITIAL (var);
501   if (!init)
502     return error_mark_node;
503   return init;
504 }
505
506 /* Returns the definition of a variable or function concept.  */
507
508 tree
509 get_concept_definition (tree decl)
510 {
511   if (TREE_CODE (decl) == VAR_DECL)
512     return get_variable_initializer (decl);
513   else if (TREE_CODE (decl) == FUNCTION_DECL)
514     return get_returned_expression (decl);
515   gcc_unreachable ();
516 }
517
518 int expansion_level = 0;
519
520 struct expanding_concept_sentinel
521 {
522   expanding_concept_sentinel ()
523   {
524     ++expansion_level;
525   }
526
527   ~expanding_concept_sentinel()
528   {
529     --expansion_level;
530   }
531 };
532
533
534 } /* namespace */
535
536 /* Returns true when a concept is being expanded.  */
537
538 bool
539 expanding_concept()
540 {
541   return expansion_level > 0;
542 }
543
544 /* Expand a concept declaration (not a template) and its arguments to
545    a constraint defined by the concept's initializer or definition.  */
546
547 tree
548 expand_concept (tree decl, tree args)
549 {
550   expanding_concept_sentinel sentinel;
551
552   if (TREE_CODE (decl) == TEMPLATE_DECL)
553     decl = DECL_TEMPLATE_RESULT (decl);
554   tree tmpl = DECL_TI_TEMPLATE (decl);
555
556   /* Check for a previous specialization. */
557   if (tree spec = get_concept_expansion (tmpl, args))
558     return spec;
559
560   /* Substitute the arguments to form a new definition expression.  */
561   tree def = get_concept_definition (decl);
562
563   ++processing_template_decl;
564   tree result = tsubst_expr (def, args, tf_none, NULL_TREE, true);
565   --processing_template_decl;
566   if (result == error_mark_node)
567     return error_mark_node;
568
569   /* And lastly, normalize it, check for implications, and save
570      the specialization for later.  */
571   tree norm = normalize_expression (result);
572   learn_implications (tmpl, args, norm);
573   return save_concept_expansion (tmpl, args, norm);
574 }
575
576
577 /*---------------------------------------------------------------------------
578                 Stepwise normalization of expressions
579
580 This set of functions will transform an expression into a constraint
581 in a sequence of steps. Normalization does not not look into concept
582 definitions.
583 ---------------------------------------------------------------------------*/
584
585 /* Transform a logical-or or logical-and expression into either
586    a conjunction or disjunction. */
587
588 tree
589 normalize_logical_operation (tree t, tree_code c)
590 {
591   tree t0 = normalize_expression (TREE_OPERAND (t, 0));
592   tree t1 = normalize_expression (TREE_OPERAND (t, 1));
593   return build_nt (c, t0, t1);
594 }
595
596 /* A simple requirement T introduces an expression constraint
597    for its expression. */
598
599 inline tree
600 normalize_simple_requirement (tree t)
601 {
602   return build_nt (EXPR_CONSTR, TREE_OPERAND (t, 0));
603 }
604
605 /* A type requirement T introduce a type constraint for its type.  */
606
607 inline tree
608 normalize_type_requirement (tree t)
609 {
610   return build_nt (TYPE_CONSTR, TREE_OPERAND (t, 0));
611 }
612
613 /* A compound requirement T introduces a conjunction of constraints
614    depending on its form.  The conjunction always includes an
615    expression constraint for the expression of the requirement.
616    If a trailing return type was specified, the conjunction includes
617    either an implicit conversion constraint or an argument deduction
618    constraint.  If the noexcept specifier is present, the conjunction
619    includes an exception constraint.  */
620
621 tree
622 normalize_compound_requirement (tree t)
623 {
624   tree expr = TREE_OPERAND (t, 0);
625   tree constr = build_nt (EXPR_CONSTR, TREE_OPERAND (t, 0));
626
627   /* If a type is given, append an implicit conversion or
628      argument deduction constraint.  */
629   if (tree type = TREE_OPERAND (t, 1))
630     {
631       tree type_constr;
632       /* TODO: We should be extracting a list of auto nodes
633          from type_uses_auto, not a single node */
634       if (tree placeholder = type_uses_auto (type))
635         type_constr = build_nt (DEDUCT_CONSTR, expr, type, placeholder);
636       else
637         type_constr = build_nt (ICONV_CONSTR, expr, type);
638       constr = conjoin_constraints (constr, type_constr);
639     }
640
641   /* If noexcept is present, append an exception constraint. */
642   if (COMPOUND_REQ_NOEXCEPT_P (t))
643     {
644       tree except = build_nt (EXCEPT_CONSTR, expr);
645       constr = conjoin_constraints (constr, except);
646     }
647
648   return constr;
649 }
650
651 /* A nested requirement T introduces a conjunction of constraints
652    corresponding to its constraint-expression.
653
654    If the result of transforming T is error_mark_node, the resulting
655    constraint is a predicate constraint whose operand is also
656    error_mark_node. This preserves the constraint structure, but
657    will guarantee that the constraint is never satisfied.  */
658
659 inline tree
660 normalize_nested_requirement (tree t)
661 {
662   return normalize_expression (TREE_OPERAND (t, 0));
663 }
664
665 /* Transform a requirement T into one or more constraints.  */
666
667 tree
668 normalize_requirement (tree t)
669 {
670   switch (TREE_CODE (t))
671     {
672     case SIMPLE_REQ:
673       return normalize_simple_requirement (t);
674
675     case TYPE_REQ:
676       return normalize_type_requirement (t);
677
678     case COMPOUND_REQ:
679       return normalize_compound_requirement (t);
680
681     case NESTED_REQ:
682       return normalize_nested_requirement (t);
683
684     default:
685       gcc_unreachable ();
686     }
687   return error_mark_node;
688 }
689
690 /* Transform a sequence of requirements into a conjunction of
691    constraints. */
692
693 tree
694 normalize_requirements (tree t)
695 {
696   tree result = NULL_TREE;
697   for (; t; t = TREE_CHAIN (t))
698     {
699       tree constr = normalize_requirement (TREE_VALUE (t));
700       result = conjoin_constraints (result, constr);
701     }
702   return result;
703 }
704
705 /* The normal form of a requires-expression is a parameterized
706    constraint having the same parameters and a conjunction of
707    constraints representing the normal form of requirements.  */
708
709 tree
710 normalize_requires_expression (tree t)
711 {
712   tree operand = normalize_requirements (TREE_OPERAND (t, 1));
713   if (tree parms = TREE_OPERAND (t, 0))
714     return build_nt (PARM_CONSTR, parms, operand);
715   else
716     return operand;
717 }
718
719 /* For a template-id referring to a variable concept, returns
720    a check constraint. Otherwise, returns a predicate constraint. */
721
722 tree
723 normalize_template_id_expression (tree t)
724 {
725   if (tree info = resolve_variable_concept_check (t))
726     {
727       if (info == error_mark_node)
728         {
729           /* We get this when the template arguments don't match
730              the variable concept. */
731           error ("invalid reference to concept %qE", t);
732           return error_mark_node;
733         }
734
735       tree decl = TREE_VALUE (info);
736       tree args = TREE_PURPOSE (info);
737       return build_nt (CHECK_CONSTR, decl, args);
738     }
739
740   /* Check that we didn't refer to a function concept like a variable.  */
741   tree tmpl = TREE_OPERAND (t, 0);
742   if (TREE_CODE (tmpl) == OVERLOAD)
743     {
744       tree fn = OVL_FUNCTION (tmpl);
745       if (TREE_CODE (fn) == TEMPLATE_DECL
746           && DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (fn)))
747         {
748           error_at (location_of (t),
749                     "invalid reference to function concept %qD", fn);
750           return error_mark_node;
751         }
752     }
753
754   return build_nt (PRED_CONSTR, t);
755 }
756
757 /* For a call expression to a function concept, returns a check
758    constraint. Otherwise, returns a predicate constraint. */
759
760 tree
761 normalize_call_expression (tree t)
762 {
763   /* Try to resolve this function call as a concept.  If not, then
764      it can be returned as a predicate constraint.  */
765   tree check = resolve_constraint_check (t);
766   if (!check)
767     return build_nt (PRED_CONSTR, t);
768   if (check == error_mark_node)
769     {
770       /* TODO: Improve diagnostics. We could report why the reference
771          is invalid. */
772       error ("invalid reference to concept %qE", t);
773       return error_mark_node;
774     }
775
776   tree fn = TREE_VALUE (check);
777   tree args = TREE_PURPOSE (check);
778   return build_nt (CHECK_CONSTR, fn, args);
779 }
780
781 /* If T is a call to an overloaded && or || operator, diagnose that
782    as a non-SFINAEable error.  Returns true if an error is emitted.
783
784    TODO: It would be better to diagnose this at the point of definition,
785    if possible. Perhaps we should immediately do a first-pass normalization
786    of a concept definition to catch obvious non-dependent errors like
787    this.  */
788
789 bool
790 check_for_logical_overloads (tree t)
791 {
792   if (TREE_CODE (t) != CALL_EXPR)
793     return false;
794
795   tree fn = CALL_EXPR_FN (t);
796
797   /* For member calls, try extracting the function from the
798      component ref.  */
799   if (TREE_CODE (fn) == COMPONENT_REF)
800     {
801       fn = TREE_OPERAND (fn, 1);
802       if (TREE_CODE (fn) == BASELINK)
803         fn = BASELINK_FUNCTIONS (fn);
804     }
805
806   if (TREE_CODE (fn) != FUNCTION_DECL)
807     return false;
808
809   if (DECL_OVERLOADED_OPERATOR_P (fn))
810     {
811       location_t loc = EXPR_LOC_OR_LOC (t, input_location);
812       error_at (loc, "constraint %qE, uses overloaded operator", t);
813       return true;
814     }
815
816   return false;
817 }
818
819 /* The normal form of an atom depends on the expression. The normal
820    form of a function call to a function concept is a check constraint
821    for that concept. The normal form of a reference to a variable
822    concept is a check constraint for that concept. Otherwise, the
823    constraint is a predicate constraint.  */
824
825 tree
826 normalize_atom (tree t)
827 {
828   /* We can get constraints pushed down through pack expansions, so
829      just return them. */
830   if (constraint_p (t))
831     return t;
832
833   tree type = TREE_TYPE (t);
834   if (!type || type_unknown_p (t) || TREE_CODE (type) == TEMPLATE_TYPE_PARM)
835     ;
836   else if (!dependent_type_p (type))
837     {
838       if (check_for_logical_overloads (t))
839         return error_mark_node;
840
841       type = cv_unqualified (type);
842       if (!same_type_p (type, boolean_type_node))
843         {
844           error ("predicate constraint %q+E does not have type %<bool%>", t);
845           return error_mark_node;
846         }
847     }
848
849   if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
850     return normalize_template_id_expression (t);
851   if (TREE_CODE (t) == CALL_EXPR)
852     return normalize_call_expression (t);
853   return build_nt (PRED_CONSTR, t);
854 }
855
856 /* Push down the pack expansion EXP into the leaves of the constraint PAT.  */
857
858 tree
859 push_down_pack_expansion (tree exp, tree pat)
860 {
861   switch (TREE_CODE (pat))
862     {
863     case CONJ_CONSTR:
864     case DISJ_CONSTR:
865       {
866         pat = copy_node (pat);
867         TREE_OPERAND (pat, 0)
868           = push_down_pack_expansion (exp, TREE_OPERAND (pat, 0));
869         TREE_OPERAND (pat, 1)
870           = push_down_pack_expansion (exp, TREE_OPERAND (pat, 1));
871         return pat;
872       }
873     default:
874       {
875         exp = copy_node (exp);
876         SET_PACK_EXPANSION_PATTERN (exp, pat);
877         return exp;
878       }
879     }
880 }
881
882 /* Transform a pack expansion into a constraint.  First we transform the
883    pattern of the pack expansion, then we push the pack expansion down into the
884    leaves of the constraint so that partial ordering will work.  */
885
886 tree
887 normalize_pack_expansion (tree t)
888 {
889   tree pat = normalize_expression (PACK_EXPANSION_PATTERN (t));
890   return push_down_pack_expansion (t, pat);
891 }
892
893 /* Transform an expression into a constraint.  */
894
895 tree
896 normalize_any_expression (tree t)
897 {
898   switch (TREE_CODE (t))
899     {
900     case TRUTH_ANDIF_EXPR:
901       return normalize_logical_operation (t, CONJ_CONSTR);
902
903     case TRUTH_ORIF_EXPR:
904       return normalize_logical_operation (t, DISJ_CONSTR);
905
906     case REQUIRES_EXPR:
907       return normalize_requires_expression (t);
908
909     case BIND_EXPR:
910       return normalize_expression (BIND_EXPR_BODY (t));
911
912     case EXPR_PACK_EXPANSION:
913       return normalize_pack_expansion (t);
914
915     default:
916       /* All other constraints are atomic. */
917       return normalize_atom (t);
918     }
919 }
920
921 /* Transform a statement into an expression.  */
922 tree
923 normalize_any_statement (tree t)
924 {
925   switch (TREE_CODE (t))
926     {
927     case RETURN_EXPR:
928       return normalize_expression (TREE_OPERAND (t, 0));
929     default:
930       gcc_unreachable ();
931     }
932   return error_mark_node;
933 }
934
935 /* Reduction rules for the declaration T.  */
936
937 tree
938 normalize_any_declaration (tree t)
939 {
940   switch (TREE_CODE (t))
941     {
942     case VAR_DECL:
943       return normalize_atom (t);
944     default:
945       gcc_unreachable ();
946     }
947   return error_mark_node;
948 }
949
950 /* Returns the normal form of a constraint expression. */
951
952 tree
953 normalize_expression (tree t)
954 {
955   if (!t)
956     return NULL_TREE;
957
958   if (t == error_mark_node)
959     return error_mark_node;
960
961   switch (TREE_CODE_CLASS (TREE_CODE (t)))
962     {
963     case tcc_unary:
964     case tcc_binary:
965     case tcc_expression:
966     case tcc_vl_exp:
967       return normalize_any_expression (t);
968
969     case tcc_statement:
970       return normalize_any_statement (t);
971
972     case tcc_declaration:
973       return normalize_any_declaration (t);
974
975     case tcc_exceptional:
976     case tcc_constant:
977     case tcc_reference:
978     case tcc_comparison:
979       /* These are all atomic predicate constraints. */
980       return normalize_atom (t);
981
982     default:
983       /* Unhandled node kind. */
984       gcc_unreachable ();
985     }
986   return error_mark_node;
987 }
988
989
990 /*---------------------------------------------------------------------------
991                         Constraint normalization
992 ---------------------------------------------------------------------------*/
993
994 tree normalize_constraint (tree);
995
996 /* The normal form of the disjunction T0 /\ T1 is the conjunction
997    of the normal form of T0 and the normal form of T1.  */
998
999 inline tree
1000 normalize_conjunction (tree t)
1001 {
1002   tree t0 = normalize_constraint (TREE_OPERAND (t, 0));
1003   tree t1 = normalize_constraint (TREE_OPERAND (t, 1));
1004   return build_nt (CONJ_CONSTR, t0, t1);
1005 }
1006
1007 /* The normal form of the disjunction T0 \/ T1 is the disjunction
1008    of the normal form of T0 and the normal form of T1.  */
1009
1010 inline tree
1011 normalize_disjunction (tree t)
1012 {
1013   tree t0 = normalize_constraint (TREE_OPERAND (t, 0));
1014   tree t1 = normalize_constraint (TREE_OPERAND (t, 1));
1015   return build_nt (DISJ_CONSTR, t0, t1);
1016 }
1017
1018 /* A predicate constraint is normalized in two stages.  First all
1019    references specializations of concepts are replaced by their
1020    substituted definitions.  Then, the resulting expression is
1021    transformed into a constraint by transforming && expressions
1022    into conjunctions and || into disjunctions.  */
1023
1024 tree
1025 normalize_predicate_constraint (tree t)
1026 {
1027   ++processing_template_decl;
1028   tree expr = PRED_CONSTR_EXPR (t);
1029   tree constr = normalize_expression (expr);
1030   --processing_template_decl;
1031   return constr;
1032 }
1033
1034 /* The normal form of a parameterized constraint is the normal
1035    form of its operand.  */
1036
1037 tree
1038 normalize_parameterized_constraint (tree t)
1039 {
1040   tree parms = PARM_CONSTR_PARMS (t);
1041   tree operand = normalize_constraint (PARM_CONSTR_OPERAND (t));
1042   return build_nt (PARM_CONSTR, parms, operand);
1043 }
1044
1045 /* Normalize the constraint T by reducing it so that it is
1046    comprised of only conjunctions and disjunctions of atomic
1047    constraints.  */
1048
1049 tree
1050 normalize_constraint (tree t)
1051 {
1052   if (!t)
1053     return NULL_TREE;
1054
1055   if (t == error_mark_node)
1056     return t;
1057
1058   switch (TREE_CODE (t))
1059     {
1060       case CONJ_CONSTR:
1061         return normalize_conjunction (t);
1062
1063       case DISJ_CONSTR:
1064         return normalize_disjunction (t);
1065
1066       case PRED_CONSTR:
1067         return normalize_predicate_constraint (t);
1068
1069       case PARM_CONSTR:
1070         return normalize_parameterized_constraint (t);
1071
1072       case EXPR_CONSTR:
1073       case TYPE_CONSTR:
1074       case ICONV_CONSTR:
1075       case DEDUCT_CONSTR:
1076       case EXCEPT_CONSTR:
1077         /* These constraints are defined to be atomic. */
1078         return t;
1079
1080       default:
1081         /* CONSTR was not a constraint. */
1082         gcc_unreachable();
1083     }
1084   return error_mark_node;
1085 }
1086
1087
1088
1089 // -------------------------------------------------------------------------- //
1090 // Constraint Semantic Processing
1091 //
1092 // The following functions are called by the parser and substitution rules
1093 // to create and evaluate constraint-related nodes.
1094
1095 // The constraints associated with the current template parameters.
1096 tree
1097 current_template_constraints (void)
1098 {
1099   if (!current_template_parms)
1100     return NULL_TREE;
1101   tree tmpl_constr = TEMPLATE_PARM_CONSTRAINTS (current_template_parms);
1102   return build_constraints (tmpl_constr, NULL_TREE);
1103 }
1104
1105 // If the recently parsed TYPE declares or defines a template or template
1106 // specialization, get its corresponding constraints from the current
1107 // template parameters and bind them to TYPE's declaration.
1108 tree
1109 associate_classtype_constraints (tree type)
1110 {
1111   if (!type || type == error_mark_node || TREE_CODE (type) != RECORD_TYPE)
1112     return type;
1113
1114   // An explicit class template specialization has no template
1115   // parameters.
1116   if (!current_template_parms)
1117     return type;
1118
1119   if (CLASSTYPE_IS_TEMPLATE (type) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1120     {
1121       tree decl = TYPE_STUB_DECL (type);
1122       tree ci = current_template_constraints ();
1123
1124       // An implicitly instantiated member template declaration already
1125       // has associated constraints. If it is defined outside of its
1126       // class, then we need match these constraints against those of
1127       // original declaration.
1128       if (tree orig_ci = get_constraints (decl))
1129         {
1130           if (!equivalent_constraints (ci, orig_ci))
1131             {
1132               // FIXME: Improve diagnostics.
1133               error ("%qT does not match any declaration", type);
1134               return error_mark_node;
1135             }
1136           return type;
1137         }
1138       set_constraints (decl, ci);
1139     }
1140   return type;
1141 }
1142
1143 namespace {
1144
1145 // Create an empty constraint info block.
1146 inline tree_constraint_info*
1147 build_constraint_info ()
1148 {
1149   return (tree_constraint_info *)make_node (CONSTRAINT_INFO);
1150 }
1151
1152 } // namespace
1153
1154 /* Build a constraint-info object that contains the associated constraints
1155    of a declaration.  This also includes the declaration's template
1156    requirements (TREQS) and any trailing requirements for a function
1157    declarator (DREQS).  Note that both TREQS and DREQS must be constraints.
1158
1159    If the declaration has neither template nor declaration requirements
1160    this returns NULL_TREE, indicating an unconstrained declaration.  */
1161
1162 tree
1163 build_constraints (tree tmpl_reqs, tree decl_reqs)
1164 {
1165   gcc_assert (tmpl_reqs ? constraint_p (tmpl_reqs) : true);
1166   gcc_assert (decl_reqs ? constraint_p (decl_reqs) : true);
1167
1168   if (!tmpl_reqs && !decl_reqs)
1169     return NULL_TREE;
1170
1171   tree_constraint_info* ci = build_constraint_info ();
1172   ci->template_reqs = tmpl_reqs;
1173   ci->declarator_reqs = decl_reqs;
1174   ci->associated_constr = conjoin_constraints (tmpl_reqs, decl_reqs);
1175
1176   return (tree)ci;
1177 }
1178
1179 namespace {
1180
1181 /* Construct a sequence of template arguments by prepending
1182    ARG to REST. Either ARG or REST may be null. */
1183 tree
1184 build_concept_check_arguments (tree arg, tree rest)
1185 {
1186   gcc_assert (rest ? TREE_CODE (rest) == TREE_VEC : true);
1187   tree args;
1188   if (arg)
1189     {
1190       int n = rest ? TREE_VEC_LENGTH (rest) : 0;
1191       args = make_tree_vec (n + 1);
1192       TREE_VEC_ELT (args, 0) = arg;
1193       if (rest)
1194         for (int i = 0; i < n; ++i)
1195           TREE_VEC_ELT (args, i + 1) = TREE_VEC_ELT (rest, i);
1196       int def = rest ? GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (rest) : 0;
1197       SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, def + 1);
1198     }
1199   else
1200     {
1201       gcc_assert (rest != NULL_TREE);
1202       args = rest;
1203     }
1204   return args;
1205 }
1206
1207 } // namespace
1208
1209 /* Construct an expression that checks the concept given by
1210    TARGET. The TARGET must be:
1211
1212    - an OVERLOAD referring to one or more function concepts
1213    - a BASELINK referring to an overload set of the above, or
1214    - a TEMPLTATE_DECL referring to a variable concept.
1215
1216    ARG and REST are the explicit template arguments for the
1217    eventual concept check. */
1218 tree
1219 build_concept_check (tree target, tree arg, tree rest)
1220 {
1221   tree args = build_concept_check_arguments (arg, rest);
1222   if (variable_template_p (target))
1223     return build_variable_check (lookup_template_variable (target, args));
1224   else
1225     return build_call_check (lookup_template_function (target, args));
1226 }
1227
1228
1229 /* Returns a TYPE_DECL that contains sufficient information to
1230    build a template parameter of the same kind as PROTO and
1231    constrained by the concept declaration CNC.  Note that PROTO
1232    is the first template parameter of CNC.
1233
1234    If specified, ARGS provides additional arguments to the
1235    constraint check.  */
1236 tree
1237 build_constrained_parameter (tree cnc, tree proto, tree args)
1238 {
1239   tree name = DECL_NAME (cnc);
1240   tree type = TREE_TYPE (proto);
1241   tree decl = build_decl (input_location, TYPE_DECL, name, type);
1242   CONSTRAINED_PARM_PROTOTYPE (decl) = proto;
1243   CONSTRAINED_PARM_CONCEPT (decl) = cnc;
1244   CONSTRAINED_PARM_EXTRA_ARGS (decl) = args;
1245   return decl;
1246 }
1247
1248 /* Create a constraint expression for the given DECL that
1249    evaluates the requirements specified by CONSTR, a TYPE_DECL
1250    that contains all the information necessary to build the
1251    requirements (see finish_concept_name for the layout of
1252    that TYPE_DECL).
1253
1254    Note that the constraints are neither reduced nor decomposed.
1255    That is done only after the requires clause has been parsed
1256    (or not).
1257
1258    This will always return a CHECK_CONSTR. */
1259 tree
1260 finish_shorthand_constraint (tree decl, tree constr)
1261 {
1262   /* No requirements means no constraints.  */
1263   if (!constr)
1264     return NULL_TREE;
1265
1266   tree proto = CONSTRAINED_PARM_PROTOTYPE (constr);
1267   tree con = CONSTRAINED_PARM_CONCEPT (constr);
1268   tree args = CONSTRAINED_PARM_EXTRA_ARGS (constr);
1269
1270   /* If the parameter declaration is variadic, but the concept
1271      is not then we need to apply the concept to every element
1272      in the pack.  */
1273   bool is_proto_pack = template_parameter_pack_p (proto);
1274   bool is_decl_pack = template_parameter_pack_p (decl);
1275   bool apply_to_all_p = is_decl_pack && !is_proto_pack;
1276
1277   /* Get the argument and overload used for the requirement
1278      and adjust it if we're going to expand later.  */
1279   tree arg = template_parm_to_arg (build_tree_list (NULL_TREE, decl));
1280   if (apply_to_all_p)
1281     arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
1282
1283   /* Build the concept check. If it the constraint needs to be
1284      applied to all elements of the parameter pack, then make
1285      the constraint an expansion. */
1286   tree check;
1287   tree tmpl = DECL_TI_TEMPLATE (con);
1288   if (TREE_CODE (con) == VAR_DECL)
1289     {
1290       check = build_concept_check (tmpl, arg, args);
1291     }
1292   else
1293     {
1294       tree ovl = build_overload (tmpl, NULL_TREE);
1295       check = build_concept_check (ovl, arg, args);
1296     }
1297
1298   /* Make the check a pack expansion if needed.
1299
1300      FIXME: We should be making a fold expression. */
1301   if (apply_to_all_p)
1302     {
1303       check = make_pack_expansion (check);
1304       TREE_TYPE (check) = boolean_type_node;
1305     }
1306
1307   return normalize_expression (check);
1308 }
1309
1310 /* Returns a conjunction of shorthand requirements for the template
1311    parameter list PARMS. Note that the requirements are stored in
1312    the TYPE of each tree node. */
1313 tree
1314 get_shorthand_constraints (tree parms)
1315 {
1316   tree result = NULL_TREE;
1317   parms = INNERMOST_TEMPLATE_PARMS (parms);
1318   for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
1319     {
1320       tree parm = TREE_VEC_ELT (parms, i);
1321       tree constr = TEMPLATE_PARM_CONSTRAINTS (parm);
1322       result = conjoin_constraints (result, constr);
1323     }
1324   return result;
1325 }
1326
1327 // Returns and chains a new parameter for PARAMETER_LIST which will conform
1328 // to the prototype given by SRC_PARM.  The new parameter will have its
1329 // identifier and location set according to IDENT and PARM_LOC respectively.
1330 static tree
1331 process_introduction_parm (tree parameter_list, tree src_parm)
1332 {
1333   // If we have a pack, we should have a single pack argument which is the
1334   // placeholder we want to look at.
1335   bool is_parameter_pack = ARGUMENT_PACK_P (src_parm);
1336   if (is_parameter_pack)
1337     src_parm = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (src_parm), 0);
1338
1339   // At this point we should have a wildcard, but we want to
1340   // grab the associated decl from it.  Also grab the stored
1341   // identifier and location that should be chained to it in
1342   // a PARM_DECL.
1343   gcc_assert (TREE_CODE (src_parm) == WILDCARD_DECL);
1344
1345   tree ident = DECL_NAME (src_parm);
1346   location_t parm_loc = DECL_SOURCE_LOCATION (src_parm);
1347
1348   // If we expect a pack and the deduced template is not a pack, or if the
1349   // template is using a pack and we didn't declare a pack, throw an error.
1350   if (is_parameter_pack != WILDCARD_PACK_P (src_parm))
1351     {
1352       error_at (parm_loc, "cannot match pack for introduced parameter");
1353       tree err_parm = build_tree_list (error_mark_node, error_mark_node);
1354       return chainon (parameter_list, err_parm);
1355     }
1356
1357   src_parm = TREE_TYPE (src_parm);
1358
1359   tree parm;
1360   bool is_non_type;
1361   if (TREE_CODE (src_parm) == TYPE_DECL)
1362     {
1363       is_non_type = false;
1364       parm = finish_template_type_parm (class_type_node, ident);
1365     }
1366   else if (TREE_CODE (src_parm) == TEMPLATE_DECL)
1367     {
1368       is_non_type = false;
1369       begin_template_parm_list ();
1370       current_template_parms = DECL_TEMPLATE_PARMS (src_parm);
1371       end_template_parm_list ();
1372       parm = finish_template_template_parm (class_type_node, ident);
1373     }
1374   else
1375     {
1376       is_non_type = true;
1377
1378       // Since we don't have a declarator, so we can copy the source
1379       // parameter and change the name and eventually the location.
1380       parm = copy_decl (src_parm);
1381       DECL_NAME (parm) = ident;
1382     }
1383
1384   // Wrap in a TREE_LIST for process_template_parm.  Introductions do not
1385   // retain the defaults from the source template.
1386   parm = build_tree_list (NULL_TREE, parm);
1387
1388   return process_template_parm (parameter_list, parm_loc, parm,
1389                                 is_non_type, is_parameter_pack);
1390 }
1391
1392 /* Associates a constraint check to the current template based
1393    on the introduction parameters.  INTRO_LIST must be a TREE_VEC
1394    of WILDCARD_DECLs containing a chained PARM_DECL which
1395    contains the identifier as well as the source location.
1396    TMPL_DECL is the decl for the concept being used.  If we
1397    take a concept, C, this will form a check in the form of
1398    C<INTRO_LIST> filling in any extra arguments needed by the
1399    defaults deduced.
1400
1401    Returns NULL_TREE if no concept could be matched and
1402    error_mark_node if an error occurred when matching.  */
1403 tree
1404 finish_template_introduction (tree tmpl_decl, tree intro_list)
1405 {
1406   /* Deduce the concept check.  */
1407   tree expr = build_concept_check (tmpl_decl, NULL_TREE, intro_list);
1408   if (expr == error_mark_node)
1409     return NULL_TREE;
1410
1411   tree parms = deduce_concept_introduction (expr);
1412   if (!parms)
1413     return NULL_TREE;
1414
1415   /* Build template parameter scope for introduction.  */
1416   tree parm_list = NULL_TREE;
1417   begin_template_parm_list ();
1418   int nargs = MIN (TREE_VEC_LENGTH (parms), TREE_VEC_LENGTH (intro_list));
1419   for (int n = 0; n < nargs; ++n)
1420     parm_list = process_introduction_parm (parm_list, TREE_VEC_ELT (parms, n));
1421   parm_list = end_template_parm_list (parm_list);
1422   for (int i = 0; i < TREE_VEC_LENGTH (parm_list); ++i)
1423     if (TREE_VALUE (TREE_VEC_ELT (parm_list, i)) == error_mark_node)
1424       {
1425         end_template_decl ();
1426         return error_mark_node;
1427       }
1428
1429   /* Build a concept check for our constraint.  */
1430   tree check_args = make_tree_vec (TREE_VEC_LENGTH (parms));
1431   int n = 0;
1432   for (; n < TREE_VEC_LENGTH (parm_list); ++n)
1433     {
1434       tree parm = TREE_VEC_ELT (parm_list, n);
1435       TREE_VEC_ELT (check_args, n) = template_parm_to_arg (parm);
1436     }
1437   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (check_args, n);
1438
1439   /* If the template expects more parameters we should be able
1440      to use the defaults from our deduced concept.  */
1441   for (; n < TREE_VEC_LENGTH (parms); ++n)
1442     TREE_VEC_ELT (check_args, n) = TREE_VEC_ELT (parms, n);
1443
1444   /* Associate the constraint. */
1445   tree check = build_concept_check (tmpl_decl, NULL_TREE, check_args);
1446   tree constr = normalize_expression (check);
1447   TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = constr;
1448
1449   return parm_list;
1450 }
1451
1452
1453 /* Given the predicate constraint T from a constrained-type-specifier, extract
1454    its TMPL and ARGS.  FIXME why do we need two different forms of
1455    constrained-type-specifier?  */
1456
1457 void
1458 placeholder_extract_concept_and_args (tree t, tree &tmpl, tree &args)
1459 {
1460   if (TREE_CODE (t) == TYPE_DECL)
1461     {
1462       /* A constrained parameter.  Build a constraint check
1463          based on the prototype parameter and then extract the
1464          arguments from that.  */
1465       tree proto = CONSTRAINED_PARM_PROTOTYPE (t);
1466       tree check = finish_shorthand_constraint (proto, t);
1467       placeholder_extract_concept_and_args (check, tmpl, args);
1468       return;
1469     }
1470
1471   if (TREE_CODE (t) == CHECK_CONSTR)
1472     {
1473       tree decl = CHECK_CONSTR_CONCEPT (t);
1474       tmpl = DECL_TI_TEMPLATE (decl);
1475       args = CHECK_CONSTR_ARGS (t);
1476       return;
1477     }
1478
1479     gcc_unreachable ();
1480 }
1481
1482 /* Returns true iff the placeholders C1 and C2 are equivalent.  C1
1483    and C2 can be either CHECK_CONSTR or TEMPLATE_TYPE_PARM.  */
1484
1485 bool
1486 equivalent_placeholder_constraints (tree c1, tree c2)
1487 {
1488   if (c1 && TREE_CODE (c1) == TEMPLATE_TYPE_PARM)
1489     /* A constrained auto.  */
1490     c1 = PLACEHOLDER_TYPE_CONSTRAINTS (c1);
1491   if (c2 && TREE_CODE (c2) == TEMPLATE_TYPE_PARM)
1492     c2 = PLACEHOLDER_TYPE_CONSTRAINTS (c2);
1493
1494   if (c1 == c2)
1495     return true;
1496   if (!c1 || !c2)
1497     return false;
1498   if (c1 == error_mark_node || c2 == error_mark_node)
1499     /* We get here during satisfaction; when a deduction constraint
1500        fails, substitution can produce an error_mark_node for the
1501        placeholder constraints.  */
1502     return false;
1503
1504   tree t1, t2, a1, a2;
1505   placeholder_extract_concept_and_args (c1, t1, a1);
1506   placeholder_extract_concept_and_args (c2, t2, a2);
1507
1508   if (t1 != t2)
1509     return false;
1510
1511   int len1 = TREE_VEC_LENGTH (a1);
1512   int len2 = TREE_VEC_LENGTH (a2);
1513   if (len1 != len2)
1514     return false;
1515
1516   /* Skip the first argument so we don't infinitely recurse.
1517      Also, they may differ in template parameter index.  */
1518   for (int i = 1; i < len1; ++i)
1519     {
1520       tree t1 = TREE_VEC_ELT (a1, i);
1521       tree t2 = TREE_VEC_ELT (a2, i);
1522       if (!template_args_equal (t1, t2))
1523       return false;
1524     }
1525   return true;
1526 }
1527
1528 /* Return a hash value for the placeholder PRED_CONSTR C.  */
1529
1530 hashval_t
1531 hash_placeholder_constraint (tree c)
1532 {
1533   tree t, a;
1534   placeholder_extract_concept_and_args (c, t, a);
1535
1536   /* Like hash_tmpl_and_args, but skip the first argument.  */
1537   hashval_t val = iterative_hash_object (DECL_UID (t), 0);
1538
1539   for (int i = TREE_VEC_LENGTH (a)-1; i > 0; --i)
1540     val = iterative_hash_template_arg (TREE_VEC_ELT (a, i), val);
1541
1542   return val;
1543 }
1544
1545 /*---------------------------------------------------------------------------
1546                         Constraint substitution
1547 ---------------------------------------------------------------------------*/
1548
1549 /* The following functions implement substitution rules for constraints.
1550    Substitution without checking constraints happens only in the
1551    instantiation of class templates. For example:
1552
1553       template<C1 T> struct S {
1554         void f(T) requires C2<T>;
1555         void g(T) requires T::value;
1556       };
1557
1558       S<int> s; // error instantiating S<int>::g(T)
1559
1560    When we instantiate S, we substitute into its member declarations,
1561    including their constraints. However, those constraints are not
1562    checked. Substituting int into C2<T> yields C2<int>, and substituting
1563    into T::value yields a substitution failure, making the program
1564    ill-formed.
1565
1566    Note that we only ever substitute into the associated constraints
1567    of a declaration. That is, substitution is defined only for predicate
1568    constraints and conjunctions. */
1569
1570 /* Substitute into the predicate constraints. Returns error_mark_node
1571    if the substitution into the expression fails. */
1572 tree
1573 tsubst_predicate_constraint (tree t, tree args,
1574                              tsubst_flags_t complain, tree in_decl)
1575 {
1576   tree expr = PRED_CONSTR_EXPR (t);
1577   ++processing_template_decl;
1578   tree result = tsubst_expr (expr, args, complain, in_decl, false);
1579   --processing_template_decl;
1580   return build_nt (PRED_CONSTR, result);
1581 }
1582
1583 /* Substitute into a check constraint. */
1584
1585 tree
1586 tsubst_check_constraint (tree t, tree args,
1587                          tsubst_flags_t complain, tree in_decl)
1588 {
1589   tree decl = CHECK_CONSTR_CONCEPT (t);
1590   tree tmpl = DECL_TI_TEMPLATE (decl);
1591   tree targs = CHECK_CONSTR_ARGS (t);
1592
1593   /* Substitute through by building an template-id expression
1594      and then substituting into that. */
1595   tree expr = build_nt(TEMPLATE_ID_EXPR, tmpl, targs);
1596   ++processing_template_decl;
1597   tree result = tsubst_expr (expr, args, complain, in_decl, false);
1598   --processing_template_decl;
1599
1600   if (result == error_mark_node)
1601     return error_mark_node;
1602
1603   /* Extract the results and rebuild the check constraint. */
1604   decl = DECL_TEMPLATE_RESULT (TREE_OPERAND (result, 0));
1605   args = TREE_OPERAND (result, 1);
1606
1607   return build_nt (CHECK_CONSTR, decl, args);
1608 }
1609
1610 /* Substitute into the conjunction of constraints. Returns
1611    error_mark_node if substitution into either operand fails. */
1612
1613 tree
1614 tsubst_logical_operator (tree t, tree args,
1615                          tsubst_flags_t complain, tree in_decl)
1616 {
1617   tree t0 = TREE_OPERAND (t, 0);
1618   tree r0 = tsubst_constraint (t0, args, complain, in_decl);
1619   if (r0 == error_mark_node)
1620     return error_mark_node;
1621   tree t1 = TREE_OPERAND (t, 1);
1622   tree r1 = tsubst_constraint (t1, args, complain, in_decl);
1623   if (r1 == error_mark_node)
1624     return error_mark_node;
1625   return build_nt (TREE_CODE (t), r0, r1);
1626 }
1627
1628 namespace {
1629
1630 /* Substitute ARGS into the expression constraint T.  */
1631
1632 tree
1633 tsubst_expr_constr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1634 {
1635   cp_unevaluated guard;
1636   tree expr = EXPR_CONSTR_EXPR (t);
1637   tree ret = tsubst_expr (expr, args, complain, in_decl, false);
1638   if (ret == error_mark_node)
1639     return error_mark_node;
1640   return build_nt (EXPR_CONSTR, ret);
1641 }
1642
1643 /* Substitute ARGS into the type constraint T.  */
1644
1645 tree
1646 tsubst_type_constr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1647 {
1648   tree type = TYPE_CONSTR_TYPE (t);
1649   tree ret = tsubst (type, args, complain, in_decl);
1650   if (ret == error_mark_node)
1651     return error_mark_node;
1652   return build_nt (TYPE_CONSTR, ret);
1653 }
1654
1655 /* Substitute ARGS into the implicit conversion constraint T.  */
1656
1657 tree
1658 tsubst_implicit_conversion_constr (tree t, tree args, tsubst_flags_t complain,
1659                                    tree in_decl)
1660 {
1661   cp_unevaluated guard;
1662   tree expr = ICONV_CONSTR_EXPR (t);
1663   tree type = ICONV_CONSTR_TYPE (t);
1664   tree new_expr = tsubst_expr (expr, args, complain, in_decl, false);
1665   if (new_expr == error_mark_node)
1666     return error_mark_node;
1667   tree new_type = tsubst (type, args, complain, in_decl);
1668   if (new_type == error_mark_node)
1669     return error_mark_node;
1670   return build_nt (ICONV_CONSTR, new_expr, new_type);
1671 }
1672
1673 /* Substitute ARGS into the argument deduction constraint T.  */
1674
1675 tree
1676 tsubst_argument_deduction_constr (tree t, tree args, tsubst_flags_t complain,
1677                                   tree in_decl)
1678 {
1679   cp_unevaluated guard;
1680   tree expr = DEDUCT_CONSTR_EXPR (t);
1681   tree pattern = DEDUCT_CONSTR_PATTERN (t);
1682   tree autos = DEDUCT_CONSTR_PLACEHOLDER(t);
1683   tree new_expr = tsubst_expr (expr, args, complain, in_decl, false);
1684   if (new_expr == error_mark_node)
1685     return error_mark_node;
1686   /* It seems like substituting through the pattern will not affect the
1687      placeholders.  We should (?) be able to reuse the existing list
1688      without any problems.  If not, then we probably want to create a
1689      new list of placeholders and then instantiate the pattern using
1690      those.  */
1691   tree new_pattern = tsubst (pattern, args, complain, in_decl);
1692   if (new_pattern == error_mark_node)
1693     return error_mark_node;
1694   return build_nt (DEDUCT_CONSTR, new_expr, new_pattern, autos);
1695 }
1696
1697 /* Substitute ARGS into the exception constraint T.  */
1698
1699 tree
1700 tsubst_exception_constr (tree t, tree args, tsubst_flags_t complain,
1701                          tree in_decl)
1702 {
1703   cp_unevaluated guard;
1704   tree expr = EXCEPT_CONSTR_EXPR (t);
1705   tree ret = tsubst_expr (expr, args, complain, in_decl, false);
1706   if (ret == error_mark_node)
1707     return error_mark_node;
1708   return build_nt (EXCEPT_CONSTR, ret);
1709 }
1710
1711 /* A subroutine of tsubst_constraint_variables. Register local
1712    specializations for each of parameter in PARMS and its
1713    corresponding substituted constraint variable in VARS.
1714    Returns VARS. */
1715
1716 tree
1717 declare_constraint_vars (tree parms, tree vars)
1718 {
1719   tree s = vars;
1720   for (tree t = parms; t; t = DECL_CHAIN (t))
1721     {
1722       if (DECL_PACK_P (t))
1723         {
1724           tree pack = extract_fnparm_pack (t, &s);
1725           register_local_specialization (pack, t);
1726         }
1727       else
1728         {
1729           register_local_specialization (s, t);
1730           s = DECL_CHAIN (s);
1731         }
1732     }
1733   return vars;
1734 }
1735
1736 /* A subroutine of tsubst_parameterized_constraint. Substitute ARGS
1737    into the parameter list T, producing a sequence of constraint
1738    variables, declared in the current scope.
1739
1740    Note that the caller must establish a local specialization stack
1741    prior to calling this function since this substitution will
1742    declare the substituted parameters. */
1743
1744 tree
1745 tsubst_constraint_variables (tree t, tree args,
1746                              tsubst_flags_t complain, tree in_decl)
1747 {
1748   /* Clear cp_unevaluated_operand across tsubst so that we get a proper chain
1749      of PARM_DECLs.  */
1750   int saved_unevaluated_operand = cp_unevaluated_operand;
1751   cp_unevaluated_operand = 0;
1752   tree vars = tsubst (t, args, complain, in_decl);
1753   cp_unevaluated_operand = saved_unevaluated_operand;
1754   if (vars == error_mark_node)
1755     return error_mark_node;
1756   return declare_constraint_vars (t, vars);
1757 }
1758
1759 /* Substitute ARGS into the parameterized constraint T.  */
1760
1761 tree
1762 tsubst_parameterized_constraint (tree t, tree args,
1763                                  tsubst_flags_t complain, tree in_decl)
1764 {
1765   local_specialization_stack stack;
1766   tree vars = tsubst_constraint_variables (PARM_CONSTR_PARMS (t),
1767                                            args, complain, in_decl);
1768   if (vars == error_mark_node)
1769     return error_mark_node;
1770   tree expr = tsubst_constraint (PARM_CONSTR_OPERAND (t), args,
1771                                  complain, in_decl);
1772   if (expr == error_mark_node)
1773     return error_mark_node;
1774   return build_nt (PARM_CONSTR, vars, expr);
1775 }
1776
1777 /* Substitute ARGS into the simple requirement T. Note that
1778    substitution may result in an ill-formed expression without
1779    causing the program to be ill-formed. In such cases, the
1780    requirement wraps an error_mark_node. */
1781
1782 inline tree
1783 tsubst_simple_requirement (tree t, tree args,
1784                            tsubst_flags_t complain, tree in_decl)
1785 {
1786   ++processing_template_decl;
1787   tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1788   --processing_template_decl;
1789   return finish_simple_requirement (expr);
1790 }
1791
1792 /* Substitute ARGS into the type requirement T. Note that
1793    substitution may result in an ill-formed type without
1794    causing the program to be ill-formed. In such cases, the
1795    requirement wraps an error_mark_node. */
1796
1797 inline tree
1798 tsubst_type_requirement (tree t, tree args,
1799                          tsubst_flags_t complain, tree in_decl)
1800 {
1801   ++processing_template_decl;
1802   tree type = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
1803   --processing_template_decl;
1804   return finish_type_requirement (type);
1805 }
1806
1807 /* Substitute args into the compound requirement T. If substituting
1808    into either the expression or the type fails, the corresponding
1809    operands in the resulting node will be error_mark_node. This
1810    preserves a requirement for the purpose of partial ordering, but
1811    it will never be satisfied. */
1812
1813 tree
1814 tsubst_compound_requirement (tree t, tree args,
1815                              tsubst_flags_t complain, tree in_decl)
1816 {
1817   ++processing_template_decl;
1818   tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1819   tree type = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
1820   --processing_template_decl;
1821   bool noexcept_p = COMPOUND_REQ_NOEXCEPT_P (t);
1822   return finish_compound_requirement (expr, type, noexcept_p);
1823 }
1824
1825 /* Substitute ARGS into the nested requirement T. */
1826
1827 tree
1828 tsubst_nested_requirement (tree t, tree args,
1829                            tsubst_flags_t complain, tree in_decl)
1830 {
1831   ++processing_template_decl;
1832   tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1833   --processing_template_decl;
1834   return finish_nested_requirement (expr);
1835 }
1836
1837 /* Substitute ARGS into the requirement T.  */
1838
1839 inline tree
1840 tsubst_requirement (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1841 {
1842   switch (TREE_CODE (t))
1843     {
1844     case SIMPLE_REQ:
1845       return tsubst_simple_requirement (t, args, complain, in_decl);
1846     case TYPE_REQ:
1847       return tsubst_type_requirement (t, args, complain, in_decl);
1848     case COMPOUND_REQ:
1849       return tsubst_compound_requirement (t, args, complain, in_decl);
1850     case NESTED_REQ:
1851       return tsubst_nested_requirement (t, args, complain, in_decl);
1852     default:
1853       gcc_unreachable ();
1854     }
1855   return error_mark_node;
1856 }
1857
1858 /* Substitute ARGS into the list of requirements T. Note that
1859    substitution failures here result in ill-formed programs. */
1860
1861 tree
1862 tsubst_requirement_body (tree t, tree args,
1863                          tsubst_flags_t complain, tree in_decl)
1864 {
1865   tree r = NULL_TREE;
1866   while (t)
1867     {
1868       tree e = tsubst_requirement (TREE_VALUE (t), args, complain, in_decl);
1869       if (e == error_mark_node)
1870         return error_mark_node;
1871       r = tree_cons (NULL_TREE, e, r);
1872       t = TREE_CHAIN (t);
1873     }
1874   /* Ensure that the order of constraints is the same as the original.  */
1875   return nreverse (r);
1876 }
1877
1878 } /* namespace */
1879
1880 /* Substitute ARGS into the requires expression T. Note that this
1881    results in the re-declaration of local parameters when
1882    substituting through the parameter list. If either substitution
1883    fails, the program is ill-formed. */
1884
1885 tree
1886 tsubst_requires_expr (tree t, tree args,
1887                       tsubst_flags_t complain, tree in_decl)
1888 {
1889   local_specialization_stack stack;
1890
1891   tree parms = TREE_OPERAND (t, 0);
1892   if (parms)
1893     {
1894       parms = tsubst_constraint_variables (parms, args, complain, in_decl);
1895       if (parms == error_mark_node)
1896         return error_mark_node;
1897     }
1898
1899   tree reqs = TREE_OPERAND (t, 1);
1900   reqs = tsubst_requirement_body (reqs, args, complain, in_decl);
1901   if (reqs == error_mark_node)
1902     return error_mark_node;
1903
1904   return finish_requires_expr (parms, reqs);
1905 }
1906
1907 /* Substitute ARGS into the constraint information CI, producing a new
1908    constraint record. */
1909
1910 tree
1911 tsubst_constraint_info (tree t, tree args,
1912                         tsubst_flags_t complain, tree in_decl)
1913 {
1914   if (!t || t == error_mark_node || !check_constraint_info (t))
1915     return NULL_TREE;
1916
1917   tree tmpl_constr = NULL_TREE;
1918   if (tree r = CI_TEMPLATE_REQS (t))
1919     tmpl_constr = tsubst_constraint (r, args, complain, in_decl);
1920
1921   tree decl_constr = NULL_TREE;
1922   if (tree r = CI_DECLARATOR_REQS (t))
1923     decl_constr = tsubst_constraint (r, args, complain, in_decl);
1924
1925   return build_constraints (tmpl_constr, decl_constr);
1926 }
1927
1928 /* Substitute ARGS into the constraint T. */
1929
1930 tree
1931 tsubst_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1932 {
1933   if (t == NULL_TREE)
1934     return t;
1935   switch (TREE_CODE (t))
1936   {
1937   case PRED_CONSTR:
1938     return tsubst_predicate_constraint (t, args, complain, in_decl);
1939   case CHECK_CONSTR:
1940     return tsubst_check_constraint (t, args, complain, in_decl);
1941   case CONJ_CONSTR:
1942   case DISJ_CONSTR:
1943     return tsubst_logical_operator (t, args, complain, in_decl);
1944   case PARM_CONSTR:
1945     return tsubst_parameterized_constraint (t, args, complain, in_decl);
1946   case EXPR_CONSTR:
1947     return tsubst_expr_constr (t, args, complain, in_decl);
1948   case TYPE_CONSTR:
1949     return tsubst_type_constr (t, args, complain, in_decl);
1950   case ICONV_CONSTR:
1951     return tsubst_implicit_conversion_constr (t, args, complain, in_decl);
1952   case DEDUCT_CONSTR:
1953     return tsubst_argument_deduction_constr (t, args, complain, in_decl);
1954   case EXCEPT_CONSTR:
1955     return tsubst_exception_constr (t, args, complain, in_decl);
1956   default:
1957     gcc_unreachable ();
1958   }
1959   return error_mark_node;
1960 }
1961
1962 /*---------------------------------------------------------------------------
1963                         Constraint satisfaction
1964 ---------------------------------------------------------------------------*/
1965
1966 /* The following functions determine if a constraint, when
1967    substituting template arguments, is satisfied. For convenience,
1968    satisfaction reduces a constraint to either true or false (and
1969    nothing else). */
1970
1971 namespace {
1972
1973 tree satisfy_constraint_1 (tree, tree, tsubst_flags_t, tree);
1974
1975 /* Check the constraint pack expansion.  */
1976
1977 tree
1978 satisfy_pack_expansion (tree t, tree args,
1979                       tsubst_flags_t complain, tree in_decl)
1980 {
1981   /* Get the vector of satisfaction results.
1982      gen_elem_of_pack_expansion_instantiation will check that each element of
1983      the expansion is satisfied.  */
1984   tree exprs = tsubst_pack_expansion (t, args, complain, in_decl);
1985
1986   if (exprs == error_mark_node)
1987     return boolean_false_node;
1988
1989   /* TODO: It might be better to normalize each expanded term
1990      and evaluate them separately. That would provide better
1991      opportunities for diagnostics.  */
1992   for (int i = 0; i < TREE_VEC_LENGTH (exprs); ++i)
1993     if (TREE_VEC_ELT (exprs, i) != boolean_true_node)
1994       return boolean_false_node;
1995   return boolean_true_node;
1996 }
1997
1998 /* A predicate constraint is satisfied if its expression evaluates
1999    to true. If substitution into that node fails, the constraint
2000    is not satisfied ([temp.constr.pred]).
2001
2002    Note that a predicate constraint is a constraint expression
2003    of type bool. If neither of those are true, the program is
2004    ill-formed; they are not SFINAE'able errors. */
2005
2006 tree
2007 satisfy_predicate_constraint (tree t, tree args,
2008                               tsubst_flags_t complain, tree in_decl)
2009 {
2010   tree expr = TREE_OPERAND (t, 0);
2011
2012   /* We should never have a naked pack expansion in a predicate constraint.  */
2013   gcc_assert (TREE_CODE (expr) != EXPR_PACK_EXPANSION);
2014
2015   /* If substitution into the expression fails, the constraint
2016      is not satisfied.  */
2017   expr = tsubst_expr (expr, args, complain, in_decl, false);
2018   if (expr == error_mark_node)
2019     return boolean_false_node;
2020
2021   /* A predicate constraint shall have type bool. In some
2022      cases, substitution gives us const-qualified bool, which
2023      is also acceptable.  */
2024   tree type = cv_unqualified (TREE_TYPE (expr));
2025   if (!same_type_p (type, boolean_type_node))
2026     {
2027       error_at (EXPR_LOC_OR_LOC (expr, input_location),
2028                 "constraint %qE does not have type %qT",
2029                 expr, boolean_type_node);
2030       return boolean_false_node;
2031     }
2032
2033   return cxx_constant_value (expr);
2034 }
2035
2036 /* A concept check constraint like C<CARGS> is satisfied if substituting ARGS
2037    into CARGS succeeds and C is satisfied for the resulting arguments.  */
2038
2039 tree
2040 satisfy_check_constraint (tree t, tree args,
2041                           tsubst_flags_t complain, tree in_decl)
2042 {
2043   tree decl = CHECK_CONSTR_CONCEPT (t);
2044   tree tmpl = DECL_TI_TEMPLATE (decl);
2045   tree cargs = CHECK_CONSTR_ARGS (t);
2046
2047   /* Instantiate the concept check arguments.  */
2048   tree targs = tsubst (cargs, args, tf_none, NULL_TREE);
2049   if (targs == error_mark_node)
2050     return boolean_false_node;
2051
2052   /* Search for a previous value.  */
2053   if (tree prev = lookup_concept_satisfaction (tmpl, targs))
2054     return prev;
2055
2056   /* Expand the concept; failure here implies non-satisfaction.  */
2057   tree def = expand_concept (decl, targs);
2058   if (def == error_mark_node)
2059     return memoize_concept_satisfaction (tmpl, args, boolean_false_node);
2060
2061   /* Recursively satisfy the constraint.  */
2062   tree result = satisfy_constraint_1 (def, targs, complain, in_decl);
2063   return memoize_concept_satisfaction (tmpl, targs, result);
2064 }
2065
2066 /* Check an expression constraint. The constraint is satisfied if
2067    substitution succeeds ([temp.constr.expr]).
2068
2069    Note that the expression is unevaluated. */
2070
2071 tree
2072 satisfy_expression_constraint (tree t, tree args,
2073                                tsubst_flags_t complain, tree in_decl)
2074 {
2075   cp_unevaluated guard;
2076   deferring_access_check_sentinel deferring;
2077
2078   tree expr = EXPR_CONSTR_EXPR (t);
2079   tree check = tsubst_expr (expr, args, complain, in_decl, false);
2080   if (check == error_mark_node)
2081     return boolean_false_node;
2082   if (!perform_deferred_access_checks (tf_none))
2083     return boolean_false_node;
2084   return boolean_true_node;
2085 }
2086
2087 /* Check a type constraint. The constraint is satisfied if
2088    substitution succeeds. */
2089
2090 inline tree
2091 satisfy_type_constraint (tree t, tree args,
2092                          tsubst_flags_t complain, tree in_decl)
2093 {
2094   deferring_access_check_sentinel deferring;
2095   tree type = TYPE_CONSTR_TYPE (t);
2096   gcc_assert (TYPE_P (type) || type == error_mark_node);
2097   tree check = tsubst (type, args, complain, in_decl);
2098   if (error_operand_p (check))
2099     return boolean_false_node;
2100   if (!perform_deferred_access_checks (complain))
2101     return boolean_false_node;
2102   return boolean_true_node;
2103 }
2104
2105 /* Check an implicit conversion constraint.  */
2106
2107 tree
2108 satisfy_implicit_conversion_constraint (tree t, tree args,
2109                                         tsubst_flags_t complain, tree in_decl)
2110 {
2111   /* Don't tsubst as if we're processing a template. If we try
2112      to we can end up generating template-like expressions
2113      (e.g., modop-exprs) that aren't properly typed.  */
2114   tree expr =
2115     tsubst_expr (ICONV_CONSTR_EXPR (t), args, complain, in_decl, false);
2116   if (expr == error_mark_node)
2117     return boolean_false_node;
2118
2119   /* Get the transformed target type.  */
2120   tree type = tsubst (ICONV_CONSTR_TYPE (t), args, complain, in_decl);
2121   if (type == error_mark_node)
2122     return boolean_false_node;
2123
2124   /* Attempt the conversion as a direct initialization
2125      of the form TYPE <unspecified> = EXPR.  */
2126   tree conv =
2127     perform_direct_initialization_if_possible (type, expr, false, complain);
2128   if (conv == NULL_TREE || conv == error_mark_node)
2129     return boolean_false_node;
2130   else
2131     return boolean_true_node;
2132 }
2133
2134 /* Check an argument deduction constraint. */
2135
2136 tree
2137 satisfy_argument_deduction_constraint (tree t, tree args,
2138                                        tsubst_flags_t complain, tree in_decl)
2139 {
2140   /* Substitute through the expression. */
2141   tree expr = DEDUCT_CONSTR_EXPR (t);
2142   tree init = tsubst_expr (expr, args, complain, in_decl, false);
2143   if (expr == error_mark_node)
2144     return boolean_false_node;
2145
2146   /* Perform auto or decltype(auto) deduction to get the result. */
2147   tree pattern = DEDUCT_CONSTR_PATTERN (t);
2148   tree placeholder = DEDUCT_CONSTR_PLACEHOLDER (t);
2149   tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (placeholder);
2150   tree type_canonical = TYPE_CANONICAL (placeholder);
2151   PLACEHOLDER_TYPE_CONSTRAINTS (placeholder)
2152     = tsubst_constraint (constr, args, complain|tf_partial, in_decl);
2153   TYPE_CANONICAL (placeholder) = NULL_TREE;
2154   tree type = do_auto_deduction (pattern, init, placeholder,
2155                                  complain, adc_requirement);
2156   PLACEHOLDER_TYPE_CONSTRAINTS (placeholder) = constr;
2157   TYPE_CANONICAL (placeholder) = type_canonical;
2158   if (type == error_mark_node)
2159     return boolean_false_node;
2160
2161   return boolean_true_node;
2162 }
2163
2164 /* Check an exception constraint. An exception constraint for an
2165    expression e is satisfied when noexcept(e) is true. */
2166
2167 tree
2168 satisfy_exception_constraint (tree t, tree args,
2169                               tsubst_flags_t complain, tree in_decl)
2170 {
2171   tree expr = EXCEPT_CONSTR_EXPR (t);
2172   tree check = tsubst_expr (expr, args, complain, in_decl, false);
2173   if (check == error_mark_node)
2174     return boolean_false_node;
2175
2176   if (expr_noexcept_p (check, complain))
2177     return boolean_true_node;
2178   else
2179     return boolean_false_node;
2180 }
2181
2182 /* Check a parameterized constraint. */
2183
2184 tree
2185 satisfy_parameterized_constraint (tree t, tree args,
2186                                   tsubst_flags_t complain, tree in_decl)
2187 {
2188   local_specialization_stack stack;
2189   tree parms = PARM_CONSTR_PARMS (t);
2190   tree vars = tsubst_constraint_variables (parms, args, complain, in_decl);
2191   if (vars == error_mark_node)
2192     return boolean_false_node;
2193   tree constr = PARM_CONSTR_OPERAND (t);
2194   return satisfy_constraint_1 (constr, args, complain, in_decl);
2195 }
2196
2197 /* Check that the conjunction of constraints is satisfied. Note
2198    that if left operand is not satisfied, the right operand
2199    is not checked.
2200
2201    FIXME: Check that this wouldn't result in a user-defined
2202    operator. Note that this error is partially diagnosed in
2203    satisfy_predicate_constraint. It would be nice to diagnose
2204    the overload, but I don't think it's strictly necessary.  */
2205
2206 tree
2207 satisfy_conjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
2208 {
2209   tree t0 = satisfy_constraint_1 (TREE_OPERAND (t, 0), args, complain, in_decl);
2210   if (t0 == boolean_false_node)
2211     return boolean_false_node;
2212   return satisfy_constraint_1 (TREE_OPERAND (t, 1), args, complain, in_decl);
2213 }
2214
2215 /* Check that the disjunction of constraints is satisfied. Note
2216    that if the left operand is satisfied, the right operand is not
2217    checked.  */
2218
2219 tree
2220 satisfy_disjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
2221 {
2222   tree t0 = satisfy_constraint_1 (TREE_OPERAND (t, 0), args, complain, in_decl);
2223   if (t0 == boolean_true_node)
2224     return boolean_true_node;
2225   return satisfy_constraint_1 (TREE_OPERAND (t, 1), args, complain, in_decl);
2226 }
2227
2228 /* Dispatch to an appropriate satisfaction routine depending on the
2229    tree code of T.  */
2230
2231 tree
2232 satisfy_constraint_1 (tree t, tree args, tsubst_flags_t complain, tree in_decl)
2233 {
2234   gcc_assert (!processing_template_decl);
2235
2236   if (!t)
2237     return boolean_false_node;
2238
2239   if (t == error_mark_node)
2240     return boolean_false_node;
2241
2242   switch (TREE_CODE (t))
2243   {
2244   case PRED_CONSTR:
2245     return satisfy_predicate_constraint (t, args, complain, in_decl);
2246
2247   case CHECK_CONSTR:
2248     return satisfy_check_constraint (t, args, complain, in_decl);
2249
2250   case EXPR_CONSTR:
2251     return satisfy_expression_constraint (t, args, complain, in_decl);
2252
2253   case TYPE_CONSTR:
2254     return satisfy_type_constraint (t, args, complain, in_decl);
2255
2256   case ICONV_CONSTR:
2257     return satisfy_implicit_conversion_constraint (t, args, complain, in_decl);
2258
2259   case DEDUCT_CONSTR:
2260     return satisfy_argument_deduction_constraint (t, args, complain, in_decl);
2261
2262   case EXCEPT_CONSTR:
2263     return satisfy_exception_constraint (t, args, complain, in_decl);
2264
2265   case PARM_CONSTR:
2266     return satisfy_parameterized_constraint (t, args, complain, in_decl);
2267
2268   case CONJ_CONSTR:
2269     return satisfy_conjunction (t, args, complain, in_decl);
2270
2271   case DISJ_CONSTR:
2272     return satisfy_disjunction (t, args, complain, in_decl);
2273
2274   case EXPR_PACK_EXPANSION:
2275     return satisfy_pack_expansion (t, args, complain, in_decl);
2276
2277   default:
2278     gcc_unreachable ();
2279   }
2280   return boolean_false_node;
2281 }
2282
2283 /* Check that the constraint is satisfied, according to the rules
2284    for that constraint. Note that each satisfy_* function returns
2285    true or false, depending on whether it is satisfied or not.  */
2286
2287 tree
2288 satisfy_constraint (tree t, tree args)
2289 {
2290   auto_timevar time (TV_CONSTRAINT_SAT);
2291
2292   /* Turn off template processing. Constraint satisfaction only applies
2293      to non-dependent terms, so we want to ensure full checking here.  */
2294   processing_template_decl_sentinel proc (true);
2295
2296   /* Avoid early exit in tsubst and tsubst_copy from null args; since earlier
2297      substitution was done with processing_template_decl forced on, there will
2298      be expressions that still need semantic processing, possibly buried in
2299      decltype or a template argument.  */
2300   if (args == NULL_TREE)
2301     args = make_tree_vec (1);
2302
2303   return satisfy_constraint_1 (t, args, tf_none, NULL_TREE);
2304 }
2305
2306 /* Check the associated constraints in CI against the given
2307    ARGS, returning true when the constraints are satisfied
2308    and false otherwise.  */
2309
2310 tree
2311 satisfy_associated_constraints (tree ci, tree args)
2312 {
2313   /* If there are no constraints then this is trivially satisfied. */
2314   if (!ci)
2315     return boolean_true_node;
2316
2317   /* If any arguments depend on template parameters, we can't
2318      check constraints. */
2319   if (args && uses_template_parms (args))
2320     return boolean_true_node;
2321
2322   /* Check if we've seen a previous result. */
2323   if (tree prev = lookup_constraint_satisfaction (ci, args))
2324     return prev;
2325
2326   /* Actually test for satisfaction. */
2327   tree result = satisfy_constraint (CI_ASSOCIATED_CONSTRAINTS (ci), args);
2328   return memoize_constraint_satisfaction (ci, args, result);
2329 }
2330
2331 } /* namespace */
2332
2333 /* Evaluate the given constraint, returning boolean_true_node
2334    if the constraint is satisfied and boolean_false_node
2335    otherwise. */
2336
2337 tree
2338 evaluate_constraints (tree constr, tree args)
2339 {
2340   gcc_assert (constraint_p (constr));
2341   return satisfy_constraint (constr, args);
2342 }
2343
2344 /* Evaluate the function concept FN by substituting its own args
2345    into its definition and evaluating that as the result. Returns
2346    boolean_true_node if the constraints are satisfied and
2347    boolean_false_node otherwise.  */
2348
2349 tree
2350 evaluate_function_concept (tree fn, tree args)
2351 {
2352   tree constr = build_nt (CHECK_CONSTR, fn, args);
2353   return satisfy_constraint (constr, args);
2354 }
2355
2356 /* Evaluate the variable concept VAR by substituting its own args into
2357    its initializer and checking the resulting constraint. Returns
2358    boolean_true_node if the constraints are satisfied and
2359    boolean_false_node otherwise.  */
2360
2361 tree
2362 evaluate_variable_concept (tree var, tree args)
2363 {
2364   tree constr = build_nt (CHECK_CONSTR, var, args);
2365   return satisfy_constraint (constr, args);
2366 }
2367
2368 /* Evaluate the given expression as if it were a predicate
2369    constraint. Returns boolean_true_node if the constraint
2370    is satisfied and boolean_false_node otherwise. */
2371
2372 tree
2373 evaluate_constraint_expression (tree expr, tree args)
2374 {
2375   tree constr = normalize_expression (expr);
2376   return satisfy_constraint (constr, args);
2377 }
2378
2379 /* Returns true if the DECL's constraints are satisfied.
2380    This is used in cases where a declaration is formed but
2381    before it is used (e.g., overload resolution). */
2382
2383 bool
2384 constraints_satisfied_p (tree decl)
2385 {
2386   /* Get the constraints to check for satisfaction. This depends
2387      on whether we're looking at a template specialization or not. */
2388   tree ci;
2389   tree args = NULL_TREE;
2390   if (tree ti = DECL_TEMPLATE_INFO (decl))
2391     {
2392       ci = get_constraints (TI_TEMPLATE (ti));
2393       args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
2394     }
2395   else
2396     {
2397       ci = get_constraints (decl);
2398     }
2399
2400   tree eval = satisfy_associated_constraints (ci, args);
2401   return eval == boolean_true_node;
2402 }
2403
2404 /* Returns true if the constraints are satisfied by ARGS.
2405    Here, T can be either a constraint or a constrained
2406    declaration.  */
2407
2408 bool
2409 constraints_satisfied_p (tree t, tree args)
2410 {
2411   tree eval;
2412   if (constraint_p (t))
2413     eval = evaluate_constraints (t, args);
2414   else
2415     eval = satisfy_associated_constraints (get_constraints (t), args);
2416   return eval == boolean_true_node;
2417 }
2418
2419 namespace
2420 {
2421
2422 /* Normalize EXPR and determine if the resulting constraint is
2423    satisfied by ARGS. Returns true if and only if the constraint
2424    is satisfied.  This is used extensively by diagnostics to
2425    determine causes for failure.  */
2426
2427 inline bool
2428 constraint_expression_satisfied_p (tree expr, tree args)
2429 {
2430   return evaluate_constraint_expression (expr, args) == boolean_true_node;
2431 }
2432
2433 } /* namespace */
2434
2435 /*---------------------------------------------------------------------------
2436                 Semantic analysis of requires-expressions
2437 ---------------------------------------------------------------------------*/
2438
2439 /* Finish a requires expression for the given PARMS (possibly
2440    null) and the non-empty sequence of requirements. */
2441 tree
2442 finish_requires_expr (tree parms, tree reqs)
2443 {
2444   /* Modify the declared parameters by removing their context
2445      so they don't refer to the enclosing scope and explicitly
2446      indicating that they are constraint variables. */
2447   for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
2448     {
2449       DECL_CONTEXT (parm) = NULL_TREE;
2450       CONSTRAINT_VAR_P (parm) = true;
2451     }
2452
2453   /* Build the node. */
2454   tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs);
2455   TREE_SIDE_EFFECTS (r) = false;
2456   TREE_CONSTANT (r) = true;
2457   return r;
2458 }
2459
2460 /* Construct a requirement for the validity of EXPR. */
2461 tree
2462 finish_simple_requirement (tree expr)
2463 {
2464   return build_nt (SIMPLE_REQ, expr);
2465 }
2466
2467 /* Construct a requirement for the validity of TYPE. */
2468 tree
2469 finish_type_requirement (tree type)
2470 {
2471   return build_nt (TYPE_REQ, type);
2472 }
2473
2474 /* Construct a requirement for the validity of EXPR, along with
2475    its properties. if TYPE is non-null, then it specifies either
2476    an implicit conversion or argument deduction constraint,
2477    depending on whether any placeholders occur in the type name.
2478    NOEXCEPT_P is true iff the noexcept keyword was specified. */
2479 tree
2480 finish_compound_requirement (tree expr, tree type, bool noexcept_p)
2481 {
2482   tree req = build_nt (COMPOUND_REQ, expr, type);
2483   COMPOUND_REQ_NOEXCEPT_P (req) = noexcept_p;
2484   return req;
2485 }
2486
2487 /* Finish a nested requirement. */
2488 tree
2489 finish_nested_requirement (tree expr)
2490 {
2491   return build_nt (NESTED_REQ, expr);
2492 }
2493
2494 // Check that FN satisfies the structural requirements of a
2495 // function concept definition.
2496 tree
2497 check_function_concept (tree fn)
2498 {
2499   // Check that the function is comprised of only a single
2500   // return statement.
2501   tree body = DECL_SAVED_TREE (fn);
2502   if (TREE_CODE (body) == BIND_EXPR)
2503     body = BIND_EXPR_BODY (body);
2504
2505   // Sometimes a function call results in the creation of clean up
2506   // points. Allow these to be preserved in the body of the
2507   // constraint, as we might actually need them for some constexpr
2508   // evaluations.
2509   if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
2510     body = TREE_OPERAND (body, 0);
2511
2512   /* Check that the definition is written correctly. */
2513   if (TREE_CODE (body) != RETURN_EXPR)
2514     {
2515       location_t loc = DECL_SOURCE_LOCATION (fn);
2516       if (TREE_CODE (body) == STATEMENT_LIST && !STATEMENT_LIST_HEAD (body))
2517         error_at (loc, "definition of concept %qD is empty", fn);
2518       else
2519         error_at (loc, "definition of concept %qD has multiple statements", fn);
2520     }
2521
2522   return NULL_TREE;
2523 }
2524
2525
2526 // Check that a constrained friend declaration function declaration,
2527 // FN, is admissible. This is the case only when the declaration depends
2528 // on template parameters and does not declare a specialization.
2529 void
2530 check_constrained_friend (tree fn, tree reqs)
2531 {
2532   if (fn == error_mark_node)
2533     return;
2534   gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
2535
2536   // If there are not constraints, this cannot be an error.
2537   if (!reqs)
2538     return;
2539
2540   // Constrained friend functions that don't depend on template
2541   // arguments are effectively meaningless.
2542   if (!uses_template_parms (TREE_TYPE (fn)))
2543     {
2544       error_at (location_of (fn),
2545                 "constrained friend does not depend on template parameters");
2546       return;
2547     }
2548 }
2549
2550 /*---------------------------------------------------------------------------
2551                         Equivalence of constraints
2552 ---------------------------------------------------------------------------*/
2553
2554 /* Returns true when A and B are equivalent constraints.  */
2555 bool
2556 equivalent_constraints (tree a, tree b)
2557 {
2558   gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2559   gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2560   return cp_tree_equal (a, b);
2561 }
2562
2563 /* Returns true if the template declarations A and B have equivalent
2564    constraints. This is the case when A's constraints subsume B's and
2565    when B's also constrain A's.  */
2566 bool
2567 equivalently_constrained (tree d1, tree d2)
2568 {
2569   gcc_assert (TREE_CODE (d1) == TREE_CODE (d2));
2570   return equivalent_constraints (get_constraints (d1), get_constraints (d2));
2571 }
2572
2573 /*---------------------------------------------------------------------------
2574                      Partial ordering of constraints
2575 ---------------------------------------------------------------------------*/
2576
2577 /* Returns true when the the constraints in A subsume those in B.  */
2578
2579 bool
2580 subsumes_constraints (tree a, tree b)
2581 {
2582   gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2583   gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2584   return subsumes (a, b);
2585 }
2586
2587 /* Returns true when the the constraints in A subsume those in B, but
2588    the constraints in B do not subsume the constraints in A.  */
2589
2590 bool
2591 strictly_subsumes (tree a, tree b)
2592 {
2593   return subsumes (a, b) && !subsumes (b, a);
2594 }
2595
2596 /* Determines which of the declarations, A or B, is more constrained.
2597    That is, which declaration's constraints subsume but are not subsumed
2598    by the other's?
2599
2600    Returns 1 if A is more constrained than B, -1 if B is more constrained
2601    than A, and 0 otherwise. */
2602
2603 int
2604 more_constrained (tree d1, tree d2)
2605 {
2606   tree c1 = get_constraints (d1);
2607   tree c2 = get_constraints (d2);
2608   int winner = 0;
2609   if (subsumes_constraints (c1, c2))
2610     ++winner;
2611   if (subsumes_constraints (c2, c1))
2612     --winner;
2613   return winner;
2614 }
2615
2616 /* Returns true if D1 is at least as constrained as D2. That is, the
2617    associated constraints of D1 subsume those of D2, or both declarations
2618    are unconstrained. */
2619
2620 bool
2621 at_least_as_constrained (tree d1, tree d2)
2622 {
2623   tree c1 = get_constraints (d1);
2624   tree c2 = get_constraints (d2);
2625   return subsumes_constraints (c1, c2);
2626 }
2627
2628
2629 /*---------------------------------------------------------------------------
2630                         Constraint diagnostics
2631
2632 FIXME: Normalize expressions into constraints before evaluating them.
2633 This should be the general pattern for all such diagnostics.
2634 ---------------------------------------------------------------------------*/
2635
2636 /* The number of detailed constraint failures.  */
2637
2638 int constraint_errors = 0;
2639
2640 /* Do not generate errors after diagnosing this number of constraint
2641    failures.
2642
2643    FIXME: This is a really arbitrary number. Provide better control of
2644    constraint diagnostics with a command line option.  */
2645
2646 int constraint_thresh = 20;
2647
2648
2649 /* Returns true if we should elide the diagnostic for a constraint failure.
2650    This is the case when the number of errors has exceeded the pre-configured
2651    threshold.  */
2652
2653 inline bool
2654 elide_constraint_failure_p ()
2655 {
2656   bool ret = constraint_thresh <= constraint_errors;
2657   ++constraint_errors;
2658   return ret;
2659 }
2660
2661 /* Returns the number of undiagnosed errors. */
2662
2663 inline int
2664 undiagnosed_constraint_failures ()
2665 {
2666   return constraint_errors - constraint_thresh;
2667 }
2668
2669 /* The diagnosis of constraints performs a combination of normalization
2670    and satisfaction testing. We recursively walk through the conjunction or
2671    disjunction of associated constraints, testing each sub-constraint in
2672    turn.  */
2673
2674 namespace {
2675
2676 void diagnose_constraint (location_t, tree, tree, tree);
2677
2678 /* Emit a specific diagnostics for a failed trait.  */
2679
2680 void
2681 diagnose_trait_expression (location_t loc, tree, tree cur, tree args)
2682 {
2683   if (constraint_expression_satisfied_p (cur, args))
2684     return;
2685   if (elide_constraint_failure_p())
2686     return;
2687
2688   tree expr = PRED_CONSTR_EXPR (cur);
2689   ++processing_template_decl;
2690   expr = tsubst_expr (expr, args, tf_none, NULL_TREE, false);
2691   --processing_template_decl;
2692
2693   tree t1 = TRAIT_EXPR_TYPE1 (expr);
2694   tree t2 = TRAIT_EXPR_TYPE2 (expr);
2695   switch (TRAIT_EXPR_KIND (expr))
2696     {
2697     case CPTK_HAS_NOTHROW_ASSIGN:
2698       inform (loc, "  %qT is not nothrow copy assignable", t1);
2699       break;
2700     case CPTK_HAS_NOTHROW_CONSTRUCTOR:
2701       inform (loc, "  %qT is not nothrow default constructible", t1);
2702       break;
2703     case CPTK_HAS_NOTHROW_COPY:
2704       inform (loc, "  %qT is not nothrow copy constructible", t1);
2705       break;
2706     case CPTK_HAS_TRIVIAL_ASSIGN:
2707       inform (loc, "  %qT is not trivially copy assignable", t1);
2708       break;
2709     case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
2710       inform (loc, "  %qT is not trivially default constructible", t1);
2711       break;
2712     case CPTK_HAS_TRIVIAL_COPY:
2713       inform (loc, "  %qT is not trivially copy constructible", t1);
2714       break;
2715     case CPTK_HAS_TRIVIAL_DESTRUCTOR:
2716       inform (loc, "  %qT is not trivially destructible", t1);
2717       break;
2718     case CPTK_HAS_VIRTUAL_DESTRUCTOR:
2719       inform (loc, "  %qT does not have a virtual destructor", t1);
2720       break;
2721     case CPTK_IS_ABSTRACT:
2722       inform (loc, "  %qT is not an abstract class", t1);
2723       break;
2724     case CPTK_IS_BASE_OF:
2725       inform (loc, "  %qT is not a base of %qT", t1, t2);
2726       break;
2727     case CPTK_IS_CLASS:
2728       inform (loc, "  %qT is not a class", t1);
2729       break;
2730     case CPTK_IS_EMPTY:
2731       inform (loc, "  %qT is not an empty class", t1);
2732       break;
2733     case CPTK_IS_ENUM:
2734       inform (loc, "  %qT is not an enum", t1);
2735       break;
2736     case CPTK_IS_FINAL:
2737       inform (loc, "  %qT is not a final class", t1);
2738       break;
2739     case CPTK_IS_LITERAL_TYPE:
2740       inform (loc, "  %qT is not a literal type", t1);
2741       break;
2742     case CPTK_IS_POD:
2743       inform (loc, "  %qT is not a POD type", t1);
2744       break;
2745     case CPTK_IS_POLYMORPHIC:
2746       inform (loc, "  %qT is not a polymorphic type", t1);
2747       break;
2748     case CPTK_IS_SAME_AS:
2749       inform (loc, "  %qT is not the same as %qT", t1, t2);
2750       break;
2751     case CPTK_IS_STD_LAYOUT:
2752       inform (loc, "  %qT is not an standard layout type", t1);
2753       break;
2754     case CPTK_IS_TRIVIAL:
2755       inform (loc, "  %qT is not a trivial type", t1);
2756       break;
2757     case CPTK_IS_UNION:
2758       inform (loc, "  %qT is not a union", t1);
2759       break;
2760     default:
2761       gcc_unreachable ();
2762     }
2763 }
2764
2765 /* Diagnose the expression of a predicate constraint.  */
2766
2767 void
2768 diagnose_other_expression (location_t loc, tree, tree cur, tree args)
2769 {
2770   if (constraint_expression_satisfied_p (cur, args))
2771     return;
2772   if (elide_constraint_failure_p())
2773     return;
2774   inform (loc, "%qE evaluated to false", cur);
2775 }
2776
2777 /* Do our best to infer meaning from predicates.  */
2778
2779 inline void
2780 diagnose_predicate_constraint (location_t loc, tree orig, tree cur, tree args)
2781 {
2782   if (TREE_CODE (PRED_CONSTR_EXPR (cur)) == TRAIT_EXPR)
2783     diagnose_trait_expression (loc, orig, cur, args);
2784   else
2785     diagnose_other_expression (loc, orig, cur, args);
2786 }
2787
2788 /* Diagnose a failed pack expansion, possibly containing constraints.  */
2789
2790 void
2791 diagnose_pack_expansion (location_t loc, tree, tree cur, tree args)
2792 {
2793   if (constraint_expression_satisfied_p (cur, args))
2794     return;
2795   if (elide_constraint_failure_p())
2796     return;
2797
2798   /* Make sure that we don't have naked packs that we don't expect. */
2799   if (!same_type_p (TREE_TYPE (cur), boolean_type_node))
2800     {
2801       inform (loc, "invalid pack expansion in constraint %qE", cur);
2802       return;
2803     }
2804
2805   inform (loc, "in the expansion of %qE", cur);
2806
2807   /* Get the vector of expanded arguments. Note that n must not
2808      be 0 since this constraint is not satisfied.  */
2809   ++processing_template_decl;
2810   tree exprs = tsubst_pack_expansion (cur, args, tf_none, NULL_TREE);
2811   --processing_template_decl;
2812   if (exprs == error_mark_node)
2813     {
2814       /* TODO: This error message could be better. */
2815       inform (loc, "    substitution failure occurred during expansion");
2816       return;
2817     }
2818
2819   /* Check each expanded constraint separately. */
2820   int n = TREE_VEC_LENGTH (exprs);
2821   for (int i = 0; i < n; ++i)
2822     {
2823       tree expr = TREE_VEC_ELT (exprs, i);
2824       if (!constraint_expression_satisfied_p (expr, args))
2825         inform (loc, "    %qE was not satisfied", expr);
2826     }
2827 }
2828
2829 /* Diagnose a potentially unsatisfied concept check constraint DECL<CARGS>.
2830    Parameters are as for diagnose_constraint.  */
2831
2832 void
2833 diagnose_check_constraint (location_t loc, tree orig, tree cur, tree args)
2834 {
2835   if (constraints_satisfied_p (cur, args))
2836     return;
2837
2838   tree decl = CHECK_CONSTR_CONCEPT (cur);
2839   tree cargs = CHECK_CONSTR_ARGS (cur);
2840   tree tmpl = DECL_TI_TEMPLATE (decl);
2841   tree check = build_nt (CHECK_CONSTR, decl, cargs);
2842
2843   /* Instantiate the concept check arguments.  */
2844   tree targs = tsubst (cargs, args, tf_none, NULL_TREE);
2845   if (targs == error_mark_node)
2846     {
2847       if (elide_constraint_failure_p ())
2848         return;
2849       inform (loc, "invalid use of the concept %qE", check);
2850       tsubst (cargs, args, tf_warning_or_error, NULL_TREE);
2851       return;
2852     }
2853
2854   tree sub = build_tree_list (tmpl, targs);
2855   /* Update to the expanded definitions. */
2856   cur = expand_concept (decl, targs);
2857   if (cur == error_mark_node)
2858     {
2859       if (elide_constraint_failure_p ())
2860         return;
2861       inform (loc, "in the expansion of concept %qE %S", check, sub);
2862       cur = get_concept_definition (decl);
2863       tsubst_expr (cur, targs, tf_warning_or_error, NULL_TREE, false);
2864       return;
2865     }
2866
2867   orig = get_concept_definition (CHECK_CONSTR_CONCEPT (orig));
2868   orig = normalize_expression (orig);
2869
2870   location_t dloc = DECL_SOURCE_LOCATION (decl);
2871   inform (dloc, "within %qS", sub);
2872   diagnose_constraint (dloc, orig, cur, targs);
2873 }
2874
2875 /* Diagnose a potentially unsatisfied conjunction or disjunction.  Parameters
2876    are as for diagnose_constraint.  */
2877
2878 void
2879 diagnose_logical_constraint (location_t loc, tree orig, tree cur, tree args)
2880 {
2881   tree t0 = TREE_OPERAND (cur, 0);
2882   tree t1 = TREE_OPERAND (cur, 1);
2883   if (!constraints_satisfied_p (t0, args))
2884     diagnose_constraint (loc, TREE_OPERAND (orig, 0), t0, args);
2885   else if (TREE_CODE (orig) == TRUTH_ORIF_EXPR)
2886     return;
2887   if (!constraints_satisfied_p (t1, args))
2888     diagnose_constraint (loc, TREE_OPERAND (orig, 1), t1, args);
2889 }
2890
2891 /* Diagnose a potential expression constraint failure. */
2892
2893 void
2894 diagnose_expression_constraint (location_t loc, tree orig, tree cur, tree args)
2895 {
2896   if (constraints_satisfied_p (cur, args))
2897     return;
2898   if (elide_constraint_failure_p())
2899     return;
2900
2901   tree expr = EXPR_CONSTR_EXPR (orig);
2902   inform (loc, "the required expression %qE would be ill-formed", expr);
2903
2904   // TODO: We should have a flag that controls this substitution.
2905   // I'm finding it very useful for resolving concept check errors.
2906
2907   // inform (input_location, "==== BEGIN DUMP ====");
2908   // tsubst_expr (EXPR_CONSTR_EXPR (orig), args, tf_warning_or_error, NULL_TREE, false);
2909   // inform (input_location, "==== END DUMP ====");
2910 }
2911
2912 /* Diagnose a potentially failed type constraint. */
2913
2914 void
2915 diagnose_type_constraint (location_t loc, tree orig, tree cur, tree args)
2916 {
2917   if (constraints_satisfied_p (cur, args))
2918     return;
2919   if (elide_constraint_failure_p())
2920     return;
2921
2922   tree type = TYPE_CONSTR_TYPE (orig);
2923   inform (loc, "the required type %qT would be ill-formed", type);
2924 }
2925
2926 /* Diagnose a potentially unsatisfied conversion constraint. */
2927
2928 void
2929 diagnose_implicit_conversion_constraint (location_t loc, tree orig, tree cur,
2930                                          tree args)
2931 {
2932   if (constraints_satisfied_p (cur, args))
2933     return;
2934
2935   /* The expression and type will previously have been substituted into,
2936      and therefore may already be an error. Also, we will have already
2937      diagnosed substitution failures into an expression since this must be
2938      part of a compound requirement.  */
2939   tree expr = ICONV_CONSTR_EXPR (cur);
2940   if (error_operand_p (expr))
2941     return;
2942
2943   /* Don't elide a previously diagnosed failure.  */
2944   if (elide_constraint_failure_p())
2945     return;
2946
2947   tree type = ICONV_CONSTR_TYPE (cur);
2948   if (error_operand_p (type))
2949     {
2950       inform (loc, "substitution into type %qT failed",
2951               ICONV_CONSTR_TYPE (orig));
2952       return;
2953     }
2954
2955   inform(loc, "%qE is not implicitly convertible to %qT", expr, type);
2956 }
2957
2958 /* Diagnose an argument deduction constraint. */
2959
2960 void
2961 diagnose_argument_deduction_constraint (location_t loc, tree orig, tree cur,
2962                                         tree args)
2963 {
2964   if (constraints_satisfied_p (cur, args))
2965     return;
2966
2967   /* The expression and type will previously have been substituted into,
2968      and therefore may already be an error. Also, we will have already
2969      diagnosed substution failures into an expression since this must be
2970      part of a compound requirement.  */
2971   tree expr = DEDUCT_CONSTR_EXPR (cur);
2972   if (error_operand_p (expr))
2973     return;
2974
2975   /* Don't elide a previously diagnosed failure.  */
2976   if (elide_constraint_failure_p ())
2977     return;
2978
2979   tree pattern = DEDUCT_CONSTR_PATTERN (cur);
2980   if (error_operand_p (pattern))
2981     {
2982       inform (loc, "substitution into type %qT failed",
2983               DEDUCT_CONSTR_PATTERN (orig));
2984       return;
2985     }
2986
2987   inform (loc, "unable to deduce placeholder type %qT from %qE",
2988           pattern, expr);
2989 }
2990
2991 /* Diagnose an exception constraint. */
2992
2993 void
2994 diagnose_exception_constraint (location_t loc, tree orig, tree cur, tree args)
2995 {
2996   if (constraints_satisfied_p (cur, args))
2997     return;
2998   if (elide_constraint_failure_p ())
2999     return;
3000
3001   /* Rebuild a noexcept expression. */
3002   tree expr = EXCEPT_CONSTR_EXPR (cur);
3003   if (error_operand_p (expr))
3004     return;
3005
3006   inform (loc, "%qE evaluated to false", EXCEPT_CONSTR_EXPR (orig));
3007 }
3008
3009 /* Diagnose a potentially unsatisfied parameterized constraint.  */
3010
3011 void
3012 diagnose_parameterized_constraint (location_t loc, tree orig, tree cur,
3013                                    tree args)
3014 {
3015   if (constraints_satisfied_p (cur, args))
3016     return;
3017
3018   local_specialization_stack stack;
3019   tree parms = PARM_CONSTR_PARMS (cur);
3020   tree vars = tsubst_constraint_variables (parms, args, tf_warning_or_error,
3021                                            NULL_TREE);
3022   if (vars == error_mark_node)
3023     {
3024       if (elide_constraint_failure_p ())
3025         return;
3026
3027       /* TODO: Check which variable failed and use orig to diagnose
3028          that substitution error.  */
3029       inform (loc, "failed to instantiate constraint variables");
3030       return;
3031     }
3032
3033   /* TODO: It would be better write these in a list. */
3034   while (vars)
3035     {
3036       inform (loc, "    with %q#D", vars);
3037       vars = TREE_CHAIN (vars);
3038     }
3039   orig = PARM_CONSTR_OPERAND (orig);
3040   cur = PARM_CONSTR_OPERAND (cur);
3041   return diagnose_constraint (loc, orig, cur, args);
3042 }
3043
3044 /* Diagnose the constraint CUR for the given ARGS. This is only ever invoked
3045    on the associated constraints, so we can only have conjunctions of
3046    predicate constraints.  The ORIGinal (dependent) constructs follow
3047    the current constraints to enable better diagnostics.  Note that ORIG
3048    and CUR must be the same kinds of node, except when CUR is an error.  */
3049
3050 void
3051 diagnose_constraint (location_t loc, tree orig, tree cur, tree args)
3052 {
3053   switch (TREE_CODE (cur))
3054     {
3055     case EXPR_CONSTR:
3056       diagnose_expression_constraint (loc, orig, cur, args);
3057       break;
3058
3059     case TYPE_CONSTR:
3060       diagnose_type_constraint (loc, orig, cur, args);
3061       break;
3062
3063     case ICONV_CONSTR:
3064       diagnose_implicit_conversion_constraint (loc, orig, cur, args);
3065       break;
3066
3067     case DEDUCT_CONSTR:
3068       diagnose_argument_deduction_constraint (loc, orig, cur, args);
3069       break;
3070
3071     case EXCEPT_CONSTR:
3072       diagnose_exception_constraint (loc, orig, cur, args);
3073       break;
3074
3075     case CONJ_CONSTR:
3076     case DISJ_CONSTR:
3077       diagnose_logical_constraint (loc, orig, cur, args);
3078       break;
3079
3080     case PRED_CONSTR:
3081       diagnose_predicate_constraint (loc, orig, cur, args);
3082       break;
3083
3084     case PARM_CONSTR:
3085       diagnose_parameterized_constraint (loc, orig, cur, args);
3086       break;
3087
3088     case CHECK_CONSTR:
3089       diagnose_check_constraint (loc, orig, cur, args);
3090       break;
3091
3092     case EXPR_PACK_EXPANSION:
3093       diagnose_pack_expansion (loc, orig, cur, args);
3094       break;
3095
3096     case ERROR_MARK:
3097       /* TODO: Can we improve the diagnostic with the original?  */
3098       inform (input_location, "ill-formed constraint");
3099       break;
3100
3101     default:
3102       gcc_unreachable ();
3103       break;
3104     }
3105 }
3106
3107 /* Diagnose the reason(s) why ARGS do not satisfy the constraints
3108    of declaration DECL. */
3109
3110 void
3111 diagnose_declaration_constraints (location_t loc, tree decl, tree args)
3112 {
3113   inform (loc, "  constraints not satisfied");
3114
3115   /* Constraints are attached to the template.  */
3116   if (tree ti = DECL_TEMPLATE_INFO (decl))
3117     {
3118       decl = TI_TEMPLATE (ti);
3119       if (!args)
3120         args = TI_ARGS (ti);
3121     }
3122
3123   /* Recursively diagnose the associated constraints.  */
3124   tree ci = get_constraints (decl);
3125   tree t = CI_ASSOCIATED_CONSTRAINTS (ci);
3126   diagnose_constraint (loc, t, t, args);
3127 }
3128
3129 } // namespace
3130
3131 /* Emit diagnostics detailing the failure ARGS to satisfy the
3132    constraints of T. Here, T can be either a constraint
3133    or a declaration.  */
3134
3135 void
3136 diagnose_constraints (location_t loc, tree t, tree args)
3137 {
3138   constraint_errors = 0;
3139
3140   if (constraint_p (t))
3141     diagnose_constraint (loc, t, t, args);
3142   else if (DECL_P (t))
3143     diagnose_declaration_constraints (loc, t, args);
3144   else
3145     gcc_unreachable ();
3146
3147   /* Note the number of elided failures. */
3148   int n = undiagnosed_constraint_failures ();
3149   if (n > 0)
3150     inform (loc, "... and %d more constraint errors not shown", n);
3151 }