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