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