LCOV - code coverage report
Current view: top level - gcc/cp - pt.cc (source / functions) Hit Total Coverage
Test: gcc.info Lines: 13133 13893 94.5 %
Date: 2023-07-19 08:18:47 Functions: 437 458 95.4 %

          Line data    Source code
       1             : /* Handle parameterized types (templates) for GNU -*- C++ -*-.
       2             :    Copyright (C) 1992-2023 Free Software Foundation, Inc.
       3             :    Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
       4             :    Rewritten by Jason Merrill (jason@cygnus.com).
       5             : 
       6             : This file is part of GCC.
       7             : 
       8             : GCC is free software; you can redistribute it and/or modify
       9             : it under the terms of the GNU General Public License as published by
      10             : the Free Software Foundation; either version 3, or (at your option)
      11             : any later version.
      12             : 
      13             : GCC is distributed in the hope that it will be useful,
      14             : but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             : GNU General Public License for more details.
      17             : 
      18             : You should have received a copy of the GNU General Public License
      19             : along with GCC; see the file COPYING3.  If not see
      20             : <http://www.gnu.org/licenses/>.  */
      21             : 
      22             : /* Known bugs or deficiencies include:
      23             : 
      24             :      all methods must be provided in header files; can't use a source
      25             :      file that contains only the method templates and "just win".
      26             : 
      27             :      Fixed by: C++20 modules.  */
      28             : 
      29             : #include "config.h"
      30             : #define INCLUDE_ALGORITHM // for std::equal
      31             : #include "system.h"
      32             : #include "coretypes.h"
      33             : #include "cp-tree.h"
      34             : #include "timevar.h"
      35             : #include "stringpool.h"
      36             : #include "varasm.h"
      37             : #include "attribs.h"
      38             : #include "stor-layout.h"
      39             : #include "intl.h"
      40             : #include "c-family/c-objc.h"
      41             : #include "cp-objcp-common.h"
      42             : #include "toplev.h"
      43             : #include "tree-iterator.h"
      44             : #include "type-utils.h"
      45             : #include "gimplify.h"
      46             : #include "gcc-rich-location.h"
      47             : #include "selftest.h"
      48             : #include "target.h"
      49             : 
      50             : /* The type of functions taking a tree, and some additional data, and
      51             :    returning an int.  */
      52             : typedef int (*tree_fn_t) (tree, void*);
      53             : 
      54             : /* The PENDING_TEMPLATES is a list of templates whose instantiations
      55             :    have been deferred, either because their definitions were not yet
      56             :    available, or because we were putting off doing the work.  */
      57             : struct GTY ((chain_next ("%h.next"))) pending_template
      58             : {
      59             :   struct pending_template *next;
      60             :   struct tinst_level *tinst;
      61             : };
      62             : 
      63             : static GTY(()) struct pending_template *pending_templates;
      64             : static GTY(()) struct pending_template *last_pending_template;
      65             : 
      66             : int processing_template_parmlist;
      67             : static int template_header_count;
      68             : 
      69             : static vec<int> inline_parm_levels;
      70             : 
      71             : static GTY(()) struct tinst_level *current_tinst_level;
      72             : 
      73             : static GTY(()) vec<tree, va_gc> *saved_access_scope;
      74             : 
      75             : /* Live only within one (recursive) call to tsubst_expr.  We use
      76             :    this to pass the statement expression node from the STMT_EXPR
      77             :    to the EXPR_STMT that is its result.  */
      78             : static tree cur_stmt_expr;
      79             : 
      80             : // -------------------------------------------------------------------------- //
      81             : // Local Specialization Stack
      82             : //
      83             : // Implementation of the RAII helper for creating new local
      84             : // specializations.
      85    23518554 : local_specialization_stack::local_specialization_stack (lss_policy policy)
      86    23518554 :   : saved (local_specializations)
      87             : {
      88    23518554 :   if (policy == lss_nop)
      89             :     ;
      90    10731934 :   else if (policy == lss_blank || !saved)
      91     9998273 :     local_specializations = new hash_map<tree, tree>;
      92             :   else
      93      733661 :     local_specializations = new hash_map<tree, tree>(*saved);
      94    23518554 : }
      95             : 
      96    23517643 : local_specialization_stack::~local_specialization_stack ()
      97             : {
      98    23517643 :   if (local_specializations != saved)
      99             :     {
     100    21462046 :       delete local_specializations;
     101    10731023 :       local_specializations = saved;
     102             :     }
     103    23517643 : }
     104             : 
     105             : /* True if we've recursed into fn_type_unification too many times.  */
     106             : static bool excessive_deduction_depth;
     107             : 
     108             : struct spec_hasher : ggc_ptr_hash<spec_entry>
     109             : {
     110             :   static hashval_t hash (tree, tree);
     111             :   static hashval_t hash (spec_entry *);
     112             :   static bool equal (spec_entry *, spec_entry *);
     113             : };
     114             : 
     115             : /* The general template is not in these tables.  */
     116             : typedef hash_table<spec_hasher> spec_hash_table;
     117             : static GTY (()) spec_hash_table *decl_specializations;
     118             : static GTY (()) spec_hash_table *type_specializations;
     119             : 
     120             : /* Contains canonical template parameter types. The vector is indexed by
     121             :    the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
     122             :    TREE_LIST, whose TREE_VALUEs contain the canonical template
     123             :    parameters of various types and levels.  */
     124             : static GTY(()) vec<tree, va_gc> *canonical_template_parms;
     125             : 
     126             : #define UNIFY_ALLOW_NONE 0
     127             : #define UNIFY_ALLOW_MORE_CV_QUAL 1
     128             : #define UNIFY_ALLOW_LESS_CV_QUAL 2
     129             : #define UNIFY_ALLOW_DERIVED 4
     130             : #define UNIFY_ALLOW_INTEGER 8
     131             : #define UNIFY_ALLOW_OUTER_LEVEL 16
     132             : #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
     133             : #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
     134             : 
     135             : enum template_base_result {
     136             :   tbr_incomplete_type,
     137             :   tbr_ambiguous_baseclass,
     138             :   tbr_success
     139             : };
     140             : 
     141             : static bool resolve_overloaded_unification (tree, tree, tree, tree,
     142             :                                             unification_kind_t, int,
     143             :                                             bool);
     144             : static int try_one_overload (tree, tree, tree, tree, tree,
     145             :                              unification_kind_t, int, bool, bool);
     146             : static int unify (tree, tree, tree, tree, int, bool);
     147             : static void add_pending_template (tree);
     148             : static tree reopen_tinst_level (struct tinst_level *);
     149             : static tree tsubst_initializer_list (tree, tree);
     150             : static tree get_partial_spec_bindings (tree, tree, tree);
     151             : static void tsubst_enum (tree, tree, tree);
     152             : static bool check_instantiated_args (tree, tree, tsubst_flags_t);
     153             : static int check_non_deducible_conversion (tree, tree, unification_kind_t, int,
     154             :                                            struct conversion **, bool);
     155             : static int maybe_adjust_types_for_deduction (tree, unification_kind_t,
     156             :                                              tree*, tree*, tree);
     157             : static int type_unification_real (tree, tree, tree, const tree *,
     158             :                                   unsigned int, int, unification_kind_t,
     159             :                                   vec<deferred_access_check, va_gc> **,
     160             :                                   bool);
     161             : static void note_template_header (int);
     162             : static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
     163             : static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
     164             : static tree convert_template_argument (tree, tree, tree,
     165             :                                        tsubst_flags_t, int, tree);
     166             : static tree for_each_template_parm (tree, tree_fn_t, void*,
     167             :                                     hash_set<tree> *, bool, tree_fn_t = NULL);
     168             : static tree expand_template_argument_pack (tree);
     169             : static tree build_template_parm_index (int, int, int, tree, tree);
     170             : static bool inline_needs_template_parms (tree, bool);
     171             : static void push_inline_template_parms_recursive (tree, int);
     172             : static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
     173             : static int mark_template_parm (tree, void *);
     174             : static int template_parm_this_level_p (tree, void *);
     175             : static tree tsubst_friend_function (tree, tree);
     176             : static tree tsubst_friend_class (tree, tree);
     177             : static int can_complete_type_without_circularity (tree);
     178             : static tree get_bindings (tree, tree, tree, bool);
     179             : static int template_decl_level (tree);
     180             : static int check_cv_quals_for_unify (int, tree, tree);
     181             : static int unify_pack_expansion (tree, tree, tree,
     182             :                                  tree, unification_kind_t, bool, bool);
     183             : static tree copy_template_args (tree);
     184             : static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
     185             : static void tsubst_each_template_parm_constraints (tree, tree, tsubst_flags_t);
     186             : static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
     187             : static tree tsubst_aggr_type_1 (tree, tree, tsubst_flags_t, tree, int);
     188             : static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
     189             : static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
     190             : static bool check_specialization_scope (void);
     191             : static tree process_partial_specialization (tree);
     192             : static enum template_base_result get_template_base (tree, tree, tree, tree,
     193             :                                                     bool , tree *);
     194             : static tree try_class_unification (tree, tree, tree, tree, bool);
     195             : static bool class_nttp_const_wrapper_p (tree t);
     196             : static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
     197             :                                            tree, tree);
     198             : static bool template_template_parm_bindings_ok_p (tree, tree);
     199             : static void tsubst_default_arguments (tree, tsubst_flags_t);
     200             : static tree for_each_template_parm_r (tree *, int *, void *);
     201             : static tree copy_default_args_to_explicit_spec_1 (tree, tree);
     202             : static void copy_default_args_to_explicit_spec (tree);
     203             : static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
     204             : static bool dependent_template_arg_p (tree);
     205             : static bool dependent_type_p_r (tree);
     206             : static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
     207             : static tree tsubst_decl (tree, tree, tsubst_flags_t);
     208             : static tree tsubst_scope (tree, tree, tsubst_flags_t, tree);
     209             : static void perform_instantiation_time_access_checks (tree, tree);
     210             : static tree listify (tree);
     211             : static tree listify_autos (tree, tree);
     212             : static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
     213             : static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
     214             : static bool complex_alias_template_p (const_tree tmpl);
     215             : static tree get_underlying_template (tree);
     216             : static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
     217             : static tree canonicalize_expr_argument (tree, tsubst_flags_t);
     218             : static tree make_argument_pack (tree);
     219             : static tree enclosing_instantiation_of (tree tctx);
     220             : static void instantiate_body (tree pattern, tree args, tree d, bool nested);
     221             : static tree maybe_dependent_member_ref (tree, tree, tsubst_flags_t, tree);
     222             : static void mark_template_arguments_used (tree, tree);
     223             : static bool uses_outer_template_parms (tree);
     224             : 
     225             : /* Make the current scope suitable for access checking when we are
     226             :    processing T.  T can be FUNCTION_DECL for instantiated function
     227             :    template, VAR_DECL for static member variable, or TYPE_DECL for
     228             :    for a class or alias template (needed by instantiate_decl).  */
     229             : 
     230             : void
     231    92411339 : push_access_scope (tree t)
     232             : {
     233    92411339 :   gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
     234             :               || TREE_CODE (t) == TYPE_DECL);
     235             : 
     236   145429572 :   if (DECL_FRIEND_CONTEXT (t))
     237      150783 :     push_nested_class (DECL_FRIEND_CONTEXT (t));
     238    37319072 :   else if (DECL_IMPLICIT_TYPEDEF_P (t)
     239    98554264 :            && CLASS_TYPE_P (TREE_TYPE (t)))
     240     6293708 :     push_nested_class (TREE_TYPE (t));
     241    85966848 :   else if (DECL_CLASS_SCOPE_P (t))
     242    30403537 :     push_nested_class (DECL_CONTEXT (t));
     243    55563311 :   else if (deduction_guide_p (t) && DECL_ARTIFICIAL (t))
     244             :     /* An artificial deduction guide should have the same access as
     245             :        the constructor.  */
     246        4079 :     push_nested_class (TREE_TYPE (TREE_TYPE (t)));
     247             :   else
     248    55559232 :     push_to_top_level ();
     249             : 
     250    92411339 :   if (TREE_CODE (t) == FUNCTION_DECL)
     251             :     {
     252    53149672 :       vec_safe_push (saved_access_scope, current_function_decl);
     253    53149672 :       current_function_decl = t;
     254             :     }
     255    92411339 : }
     256             : 
     257             : /* Restore the scope set up by push_access_scope.  T is the node we
     258             :    are processing.  */
     259             : 
     260             : void
     261    92410439 : pop_access_scope (tree t)
     262             : {
     263    92410439 :   if (TREE_CODE (t) == FUNCTION_DECL)
     264    53148772 :     current_function_decl = saved_access_scope->pop();
     265             : 
     266   145427772 :   if (DECL_FRIEND_CONTEXT (t)
     267    92259656 :       || (DECL_IMPLICIT_TYPEDEF_P (t)
     268     6293708 :           && CLASS_TYPE_P (TREE_TYPE (t)))
     269    85965948 :       || DECL_CLASS_SCOPE_P (t)
     270   103968121 :       || (deduction_guide_p (t) && DECL_ARTIFICIAL (t)))
     271    36852107 :     pop_nested_class ();
     272             :   else
     273    55558332 :     pop_from_top_level ();
     274    92410439 : }
     275             : 
     276             : /* Do any processing required when DECL (a member template
     277             :    declaration) is finished.  Returns the TEMPLATE_DECL corresponding
     278             :    to DECL, unless it is a specialization, in which case the DECL
     279             :    itself is returned.  */
     280             : 
     281             : tree
     282    11676022 : finish_member_template_decl (tree decl)
     283             : {
     284    11676022 :   if (decl == error_mark_node)
     285             :     return error_mark_node;
     286             : 
     287    11675929 :   gcc_assert (DECL_P (decl));
     288             : 
     289    11675929 :   if (TREE_CODE (decl) == TYPE_DECL)
     290             :     {
     291      802391 :       tree type;
     292             : 
     293      802391 :       type = TREE_TYPE (decl);
     294      802391 :       if (type == error_mark_node)
     295             :         return error_mark_node;
     296      802391 :       if (MAYBE_CLASS_TYPE_P (type)
     297      802391 :           && CLASSTYPE_TEMPLATE_INFO (type)
     298     1604782 :           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
     299             :         {
     300      450197 :           tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
     301      450197 :           check_member_template (tmpl);
     302      450197 :           return tmpl;
     303             :         }
     304             :       return NULL_TREE;
     305             :     }
     306    10873538 :   else if (TREE_CODE (decl) == FIELD_DECL)
     307           4 :     error_at (DECL_SOURCE_LOCATION (decl),
     308             :               "data member %qD cannot be a member template", decl);
     309    10873534 :   else if (DECL_TEMPLATE_INFO (decl))
     310             :     {
     311    10873534 :       if (!DECL_TEMPLATE_SPECIALIZATION (decl))
     312             :         {
     313    10873534 :           check_member_template (DECL_TI_TEMPLATE (decl));
     314    10873534 :           return DECL_TI_TEMPLATE (decl);
     315             :         }
     316             :       else
     317             :         return decl;
     318             :     }
     319             :   else
     320           0 :     error_at (DECL_SOURCE_LOCATION (decl),
     321             :               "invalid member template declaration %qD", decl);
     322             : 
     323           4 :   return error_mark_node;
     324             : }
     325             : 
     326             : /* Create a template info node.  */
     327             : 
     328             : tree
     329   394853944 : build_template_info (tree template_decl, tree template_args)
     330             : {
     331   394853944 :   tree result = make_node (TEMPLATE_INFO);
     332   394853944 :   TI_TEMPLATE (result) = template_decl;
     333   394853944 :   TI_ARGS (result) = template_args;
     334   394853944 :   return result;
     335             : }
     336             : 
     337             : /* Return the template info node corresponding to T, whatever T is.  */
     338             : 
     339             : tree
     340  1634585590 : get_template_info (const_tree t)
     341             : {
     342  1634585590 :   tree tinfo = NULL_TREE;
     343             : 
     344  1634585590 :   if (!t || t == error_mark_node)
     345             :     return NULL;
     346             : 
     347  1634585588 :   if (TREE_CODE (t) == NAMESPACE_DECL
     348  1586789953 :       || TREE_CODE (t) == PARM_DECL)
     349             :     return NULL;
     350             : 
     351  1583788701 :   if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
     352   957969883 :     tinfo = DECL_TEMPLATE_INFO (t);
     353             : 
     354  1583788701 :   if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
     355   244131011 :     t = TREE_TYPE (t);
     356             : 
     357  1583788701 :   if (OVERLOAD_TYPE_P (t))
     358   615204575 :     tinfo = TYPE_TEMPLATE_INFO (t);
     359   968584126 :   else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
     360          13 :     tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
     361             : 
     362             :   return tinfo;
     363             : }
     364             : 
     365             : /* Returns the template nesting level of the indicated class TYPE.
     366             : 
     367             :    For example, in:
     368             :      template <class T>
     369             :      struct A
     370             :      {
     371             :        template <class U>
     372             :        struct B {};
     373             :      };
     374             : 
     375             :    A<T>::B<U> has depth two, while A<T> has depth one.
     376             :    Both A<T>::B<int> and A<int>::B<U> have depth one, if
     377             :    they are instantiations, not specializations.
     378             : 
     379             :    This function is guaranteed to return 0 if passed NULL_TREE so
     380             :    that, for example, `template_class_depth (current_class_type)' is
     381             :    always safe.  */
     382             : 
     383             : int
     384   323110007 : template_class_depth (tree type)
     385             : {
     386   323110007 :   int depth;
     387             : 
     388   444619528 :   for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
     389             :     {
     390   121509521 :       tree tinfo = get_template_info (type);
     391             : 
     392   121509521 :       if (tinfo
     393   111863230 :           && TREE_CODE (TI_TEMPLATE (tinfo)) == TEMPLATE_DECL
     394   111863230 :           && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
     395   232271905 :           && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
     396    85989145 :         ++depth;
     397             : 
     398   121509521 :       if (DECL_P (type))
     399             :         {
     400       93133 :           if (tree fctx = DECL_FRIEND_CONTEXT (type))
     401             :             type = fctx;
     402             :           else
     403       46531 :             type = CP_DECL_CONTEXT (type);
     404             :         }
     405   242573287 :       else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
     406       28267 :         type = LAMBDA_TYPE_EXTRA_SCOPE (type);
     407             :       else
     408   121434491 :         type = CP_TYPE_CONTEXT (type);
     409             :     }
     410             : 
     411   323110007 :   return depth;
     412             : }
     413             : 
     414             : /* Return TRUE if NODE instantiates a template that has arguments of
     415             :    its own, be it directly a primary template or indirectly through a
     416             :    partial specializations.  */
     417             : static bool
     418   120613284 : instantiates_primary_template_p (tree node)
     419             : {
     420   120613284 :   tree tinfo = get_template_info (node);
     421   120613284 :   if (!tinfo)
     422             :     return false;
     423             : 
     424   120609744 :   tree tmpl = TI_TEMPLATE (tinfo);
     425   120609744 :   if (PRIMARY_TEMPLATE_P (tmpl))
     426             :     return true;
     427             : 
     428    79965982 :   if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
     429             :     return false;
     430             : 
     431             :   /* So now we know we have a specialization, but it could be a full
     432             :      or a partial specialization.  To tell which, compare the depth of
     433             :      its template arguments with those of its context.  */
     434             : 
     435           4 :   tree ctxt = DECL_CONTEXT (tmpl);
     436           4 :   tree ctinfo = get_template_info (ctxt);
     437           4 :   if (!ctinfo)
     438             :     return true;
     439             : 
     440           4 :   return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
     441           8 :           > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
     442             : }
     443             : 
     444             : /* Subroutine of maybe_begin_member_template_processing.
     445             :    Returns true if processing DECL needs us to push template parms.  */
     446             : 
     447             : static bool
     448   103275964 : inline_needs_template_parms (tree decl, bool nsdmi)
     449             : {
     450   103275964 :   if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
     451             :     return false;
     452             : 
     453    87471231 :   return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
     454    87471231 :           > (current_template_depth + DECL_TEMPLATE_SPECIALIZATION (decl)));
     455             : }
     456             : 
     457             : /* Subroutine of maybe_begin_member_template_processing.
     458             :    Push the template parms in PARMS, starting from LEVELS steps into the
     459             :    chain, and ending at the beginning, since template parms are listed
     460             :    innermost first.  */
     461             : 
     462             : static void
     463    22692959 : push_inline_template_parms_recursive (tree parmlist, int levels)
     464             : {
     465    22692959 :   tree parms = TREE_VALUE (parmlist);
     466    22692959 :   int i;
     467             : 
     468    22692959 :   if (levels > 1)
     469      280289 :     push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
     470             : 
     471    22692959 :   ++processing_template_decl;
     472    45385918 :   current_template_parms
     473    22692959 :     = tree_cons (size_int (current_template_depth + 1),
     474             :                  parms, current_template_parms);
     475    45385918 :   TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
     476    22692959 :     = TEMPLATE_PARMS_CONSTRAINTS (parmlist);
     477    22692959 :   TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
     478             : 
     479    22692959 :   begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
     480             :                NULL);
     481    60518409 :   for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
     482             :     {
     483    37825450 :       tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
     484             : 
     485    37825450 :       if (error_operand_p (parm))
     486          32 :         continue;
     487             : 
     488    37825418 :       gcc_assert (DECL_P (parm));
     489             : 
     490    37825418 :       switch (TREE_CODE (parm))
     491             :         {
     492    34568953 :         case TYPE_DECL:
     493    34568953 :         case TEMPLATE_DECL:
     494    34568953 :           pushdecl (parm);
     495    34568953 :           break;
     496             : 
     497     3256465 :         case PARM_DECL:
     498             :           /* Push the CONST_DECL.  */
     499     3256465 :           pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
     500     3256465 :           break;
     501             : 
     502           0 :         default:
     503           0 :           gcc_unreachable ();
     504             :         }
     505             :     }
     506    22692959 : }
     507             : 
     508             : /* Restore the template parameter context for a member template, a
     509             :    friend template defined in a class definition, or a non-template
     510             :    member of template class.  */
     511             : 
     512             : void
     513   103275964 : maybe_begin_member_template_processing (tree decl)
     514             : {
     515   103275964 :   tree parms;
     516   103275964 :   int levels = 0;
     517   103275964 :   bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
     518             : 
     519   103275964 :   if (nsdmi)
     520             :     {
     521      441472 :       tree ctx = DECL_CONTEXT (decl);
     522      441472 :       decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
     523             :               /* Disregard full specializations (c++/60999).  */
     524      220224 :               && uses_template_parms (ctx)
     525      660600 :               ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
     526             :     }
     527             : 
     528   103275964 :   if (inline_needs_template_parms (decl, nsdmi))
     529             :     {
     530    22412670 :       parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
     531    22412670 :       levels = TMPL_PARMS_DEPTH (parms) - current_template_depth;
     532             : 
     533    22412670 :       if (DECL_TEMPLATE_SPECIALIZATION (decl))
     534             :         {
     535           0 :           --levels;
     536           0 :           parms = TREE_CHAIN (parms);
     537             :         }
     538             : 
     539    22412670 :       push_inline_template_parms_recursive (parms, levels);
     540             :     }
     541             : 
     542             :   /* Remember how many levels of template parameters we pushed so that
     543             :      we can pop them later.  */
     544   103275964 :   inline_parm_levels.safe_push (levels);
     545   103275964 : }
     546             : 
     547             : /* Undo the effects of maybe_begin_member_template_processing.  */
     548             : 
     549             : void
     550   103275964 : maybe_end_member_template_processing (void)
     551             : {
     552   103275964 :   int i;
     553   103275964 :   int last;
     554             : 
     555   103275964 :   if (inline_parm_levels.length () == 0)
     556             :     return;
     557             : 
     558   103275964 :   last = inline_parm_levels.pop ();
     559   125968923 :   for (i = 0; i < last; ++i)
     560             :     {
     561    22692959 :       --processing_template_decl;
     562    22692959 :       current_template_parms = TREE_CHAIN (current_template_parms);
     563    22692959 :       poplevel (0, 0, 0);
     564             :     }
     565             : }
     566             : 
     567             : /* Return a new template argument vector which contains all of ARGS,
     568             :    but has as its innermost set of arguments the EXTRA_ARGS.  */
     569             : 
     570             : tree
     571    95957635 : add_to_template_args (tree args, tree extra_args)
     572             : {
     573    95957635 :   tree new_args;
     574    95957635 :   int extra_depth;
     575    95957635 :   int i;
     576    95957635 :   int j;
     577             : 
     578    95957635 :   if (args == NULL_TREE || extra_args == error_mark_node)
     579             :     return extra_args;
     580             : 
     581    57314182 :   extra_depth = TMPL_ARGS_DEPTH (extra_args);
     582    28657091 :   new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
     583             : 
     584    87661954 :   for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
     585    28749784 :     SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
     586             : 
     587    57314186 :   for (j = 1; j <= extra_depth; ++j, ++i)
     588    57314190 :     SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
     589             : 
     590             :   return new_args;
     591             : }
     592             : 
     593             : /* Like add_to_template_args, but only the outermost ARGS are added to
     594             :    the EXTRA_ARGS.  In particular, all but TMPL_ARGS_DEPTH
     595             :    (EXTRA_ARGS) levels are added.  This function is used to combine
     596             :    the template arguments from a partial instantiation with the
     597             :    template arguments used to attain the full instantiation from the
     598             :    partial instantiation.
     599             : 
     600             :    If ARGS is a TEMPLATE_DECL, use its parameters as args.  */
     601             : 
     602             : tree
     603   430030199 : add_outermost_template_args (tree args, tree extra_args)
     604             : {
     605   430030199 :   tree new_args;
     606             : 
     607   430030199 :   if (!args)
     608             :     return extra_args;
     609   430030199 :   if (TREE_CODE (args) == TEMPLATE_DECL)
     610             :     {
     611    19063539 :       tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
     612    19063539 :       args = TI_ARGS (ti);
     613             :     }
     614             : 
     615             :   /* If there are more levels of EXTRA_ARGS than there are ARGS,
     616             :      something very fishy is going on.  */
     617  1710405882 :   gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
     618             : 
     619             :   /* If *all* the new arguments will be the EXTRA_ARGS, just return
     620             :      them.  */
     621  2121372542 :   if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
     622             :     return extra_args;
     623             : 
     624             :   /* For the moment, we make ARGS look like it contains fewer levels.  */
     625    83120927 :   TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
     626             : 
     627    27805379 :   new_args = add_to_template_args (args, extra_args);
     628             : 
     629             :   /* Now, we restore ARGS to its full dimensions.  */
     630    83120927 :   TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
     631             : 
     632    27805379 :   return new_args;
     633             : }
     634             : 
     635             : /* Return the N levels of innermost template arguments from the ARGS.  */
     636             : 
     637             : tree
     638  2569796950 : get_innermost_template_args (tree args, int n)
     639             : {
     640  2569796950 :   tree new_args;
     641  2569796950 :   int extra_levels;
     642  2569796950 :   int i;
     643             : 
     644  2569796950 :   gcc_assert (n >= 0);
     645             : 
     646             :   /* If N is 1, just return the innermost set of template arguments.  */
     647  2569796950 :   if (n == 1)
     648  5139589408 :     return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
     649             : 
     650             :   /* If we're not removing anything, just return the arguments we were
     651             :      given.  */
     652        4492 :   extra_levels = TMPL_ARGS_DEPTH (args) - n;
     653        2246 :   gcc_assert (extra_levels >= 0);
     654        2246 :   if (extra_levels == 0)
     655             :     return args;
     656             : 
     657             :   /* Make a new set of arguments, not containing the outer arguments.  */
     658           8 :   new_args = make_tree_vec (n);
     659          32 :   for (i = 1; i <= n; ++i)
     660          32 :     SET_TMPL_ARGS_LEVEL (new_args, i,
     661             :                          TMPL_ARGS_LEVEL (args, i + extra_levels));
     662             : 
     663             :   return new_args;
     664             : }
     665             : 
     666             : /* The inverse of get_innermost_template_args: Return all but the innermost
     667             :    EXTRA_LEVELS levels of template arguments from the ARGS.  */
     668             : 
     669             : static tree
     670       77346 : strip_innermost_template_args (tree args, int extra_levels)
     671             : {
     672       77346 :   tree new_args;
     673      154692 :   int n = TMPL_ARGS_DEPTH (args) - extra_levels;
     674       77346 :   int i;
     675             : 
     676       77346 :   gcc_assert (n >= 0);
     677             : 
     678             :   /* If N is 1, just return the outermost set of template arguments.  */
     679       77346 :   if (n == 1)
     680      154278 :     return TMPL_ARGS_LEVEL (args, 1);
     681             : 
     682             :   /* If we're not removing anything, just return the arguments we were
     683             :      given.  */
     684         207 :   gcc_assert (extra_levels >= 0);
     685         207 :   if (extra_levels == 0)
     686             :     return args;
     687             : 
     688             :   /* Make a new set of arguments, not containing the inner arguments.  */
     689         207 :   new_args = make_tree_vec (n);
     690         828 :   for (i = 1; i <= n; ++i)
     691         828 :     SET_TMPL_ARGS_LEVEL (new_args, i,
     692             :                          TMPL_ARGS_LEVEL (args, i));
     693             : 
     694             :   return new_args;
     695             : }
     696             : 
     697             : /* We've got a template header coming up; push to a new level for storing
     698             :    the parms.  */
     699             : 
     700             : void
     701    55219494 : begin_template_parm_list (void)
     702             : {
     703             :   /* We use a non-tag-transparent scope here, which causes pushtag to
     704             :      put tags in this scope, rather than in the enclosing class or
     705             :      namespace scope.  This is the right thing, since we want
     706             :      TEMPLATE_DECLS, and not TYPE_DECLS for template classes.  For a
     707             :      global template class, push_template_decl handles putting the
     708             :      TEMPLATE_DECL into top-level scope.  For a nested template class,
     709             :      e.g.:
     710             : 
     711             :        template <class T> struct S1 {
     712             :          template <class T> struct S2 {};
     713             :        };
     714             : 
     715             :      pushtag contains special code to insert the TEMPLATE_DECL for S2
     716             :      at the right scope.  */
     717    55219494 :   begin_scope (sk_template_parms, NULL);
     718    55219494 :   ++processing_template_decl;
     719    55219494 :   ++processing_template_parmlist;
     720    55219494 :   note_template_header (0);
     721             : 
     722             :   /* Add a dummy parameter level while we process the parameter list.  */
     723   110438988 :   current_template_parms
     724    55219494 :     = tree_cons (size_int (current_template_depth + 1),
     725             :                  make_tree_vec (0),
     726             :                  current_template_parms);
     727    55219494 : }
     728             : 
     729             : /* This routine is called when a specialization is declared.  If it is
     730             :    invalid to declare a specialization here, an error is reported and
     731             :    false is returned, otherwise this routine will return true.  */
     732             : 
     733             : static bool
     734     3556940 : check_specialization_scope (void)
     735             : {
     736     3556940 :   tree scope = current_scope ();
     737             : 
     738             :   /* [temp.expl.spec]
     739             : 
     740             :      An explicit specialization shall be declared in the namespace of
     741             :      which the template is a member, or, for member templates, in the
     742             :      namespace of which the enclosing class or enclosing class
     743             :      template is a member.  An explicit specialization of a member
     744             :      function, member class or static data member of a class template
     745             :      shall be declared in the namespace of which the class template
     746             :      is a member.  */
     747     3556940 :   if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
     748             :     {
     749          32 :       error ("explicit specialization in non-namespace scope %qD", scope);
     750          32 :       return false;
     751             :     }
     752             : 
     753             :   /* [temp.expl.spec]
     754             : 
     755             :      In an explicit specialization declaration for a member of a class
     756             :      template or a member template that appears in namespace scope,
     757             :      the member template and some of its enclosing class templates may
     758             :      remain unspecialized, except that the declaration shall not
     759             :      explicitly specialize a class member template if its enclosing
     760             :      class templates are not explicitly specialized as well.  */
     761     3556908 :   if (current_template_parms)
     762             :     {
     763           8 :       error ("enclosing class templates are not explicitly specialized");
     764           8 :       return false;
     765             :     }
     766             : 
     767             :   return true;
     768             : }
     769             : 
     770             : /* We've just seen template <>.  */
     771             : 
     772             : bool
     773     3556940 : begin_specialization (void)
     774             : {
     775     3556940 :   begin_scope (sk_template_spec, NULL);
     776     3556940 :   note_template_header (1);
     777     7113880 :   return check_specialization_scope ();
     778             : }
     779             : 
     780             : /* Called at then end of processing a declaration preceded by
     781             :    template<>.  */
     782             : 
     783             : void
     784     3556940 : end_specialization (void)
     785             : {
     786     3556940 :   finish_scope ();
     787     3556940 :   reset_specialization ();
     788     3556940 : }
     789             : 
     790             : /* Any template <>'s that we have seen thus far are not referring to a
     791             :    function specialization.  */
     792             : 
     793             : void
     794   104163370 : reset_specialization (void)
     795             : {
     796   104163370 :   processing_specialization = 0;
     797   104163370 :   template_header_count = 0;
     798    45262559 : }
     799             : 
     800             : /* We've just seen a template header.  If SPECIALIZATION is nonzero,
     801             :    it was of the form template <>.  */
     802             : 
     803             : static void
     804    58776434 : note_template_header (int specialization)
     805             : {
     806    58776434 :   processing_specialization = specialization;
     807    58776434 :   template_header_count++;
     808           0 : }
     809             : 
     810             : /* We're beginning an explicit instantiation.  */
     811             : 
     812             : void
     813     2008118 : begin_explicit_instantiation (void)
     814             : {
     815     2008118 :   gcc_assert (!processing_explicit_instantiation);
     816     2008118 :   processing_explicit_instantiation = true;
     817     2008118 : }
     818             : 
     819             : 
     820             : void
     821     2008114 : end_explicit_instantiation (void)
     822             : {
     823     2008114 :   gcc_assert (processing_explicit_instantiation);
     824     2008114 :   processing_explicit_instantiation = false;
     825     2008114 : }
     826             : 
     827             : /* An explicit specialization or partial specialization of TMPL is being
     828             :    declared.  Check that the namespace in which the specialization is
     829             :    occurring is permissible.  Returns false iff it is invalid to
     830             :    specialize TMPL in the current namespace.  */
     831             : 
     832             : static bool
     833     8735503 : check_specialization_namespace (tree tmpl)
     834             : {
     835     8735503 :   tree tpl_ns = decl_namespace_context (tmpl);
     836             : 
     837             :   /* [tmpl.expl.spec]
     838             : 
     839             :      An explicit specialization shall be declared in a namespace enclosing the
     840             :      specialized template. An explicit specialization whose declarator-id is
     841             :      not qualified shall be declared in the nearest enclosing namespace of the
     842             :      template, or, if the namespace is inline (7.3.1), any namespace from its
     843             :      enclosing namespace set.  */
     844     8735503 :   if (current_scope() != DECL_CONTEXT (tmpl)
     845     8735503 :       && !at_namespace_scope_p ())
     846             :     {
     847          16 :       error ("specialization of %qD must appear at namespace scope", tmpl);
     848          16 :       return false;
     849             :     }
     850             : 
     851     8735487 :   if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
     852             :     /* Same or enclosing namespace.  */
     853             :     return true;
     854             :   else
     855             :     {
     856          16 :       auto_diagnostic_group d;
     857          16 :       if (permerror (input_location,
     858             :                      "specialization of %qD in different namespace", tmpl))
     859          12 :         inform (DECL_SOURCE_LOCATION (tmpl),
     860             :                 "  from definition of %q#D", tmpl);
     861          16 :       return false;
     862          16 :     }
     863             : }
     864             : 
     865             : /* SPEC is an explicit instantiation.  Check that it is valid to
     866             :    perform this explicit instantiation in the current namespace.  */
     867             : 
     868             : static void
     869     2043694 : check_explicit_instantiation_namespace (tree spec)
     870             : {
     871     2043694 :   tree ns;
     872             : 
     873             :   /* DR 275: An explicit instantiation shall appear in an enclosing
     874             :      namespace of its template.  */
     875     2043694 :   ns = decl_namespace_context (spec);
     876     2043694 :   if (!is_nested_namespace (current_namespace, ns))
     877           8 :     permerror (input_location, "explicit instantiation of %qD in namespace %qD "
     878             :                "(which does not enclose namespace %qD)",
     879             :                spec, current_namespace, ns);
     880     2043694 : }
     881             : 
     882             : /* Returns true if TYPE is a new partial specialization that needs to be
     883             :    set up.  This may also modify TYPE to point to the correct (new or
     884             :    existing) constrained partial specialization.  */
     885             : 
     886             : static bool
     887    23312869 : maybe_new_partial_specialization (tree& type)
     888             : {
     889             :   /* An implicit instantiation of an incomplete type implies
     890             :      the definition of a new class template.
     891             : 
     892             :         template<typename T>
     893             :           struct S;
     894             : 
     895             :         template<typename T>
     896             :           struct S<T*>;
     897             : 
     898             :      Here, S<T*> is an implicit instantiation of S whose type
     899             :      is incomplete.  */
     900    23312869 :   if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
     901             :     return true;
     902             : 
     903             :   /* It can also be the case that TYPE is a completed specialization.
     904             :      Continuing the previous example, suppose we also declare:
     905             : 
     906             :         template<typename T>
     907             :           requires Integral<T>
     908             :             struct S<T*>;
     909             : 
     910             :      Here, S<T*> refers to the specialization S<T*> defined
     911             :      above. However, we need to differentiate definitions because
     912             :      we intend to define a new partial specialization. In this case,
     913             :      we rely on the fact that the constraints are different for
     914             :      this declaration than that above.
     915             : 
     916             :      Note that we also get here for injected class names and
     917             :      late-parsed template definitions. We must ensure that we
     918             :      do not create new type declarations for those cases.  */
     919    15501246 :   if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
     920             :     {
     921     1522688 :       tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
     922     1522688 :       tree args = CLASSTYPE_TI_ARGS (type);
     923             : 
     924             :       /* If there are no template parameters, this cannot be a new
     925             :          partial template specialization?  */
     926     1522688 :       if (!current_template_parms)
     927             :         return false;
     928             : 
     929             :       /* The injected-class-name is not a new partial specialization.  */
     930      927630 :       if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
     931             :         return false;
     932             : 
     933             :       /* If the constraints are not the same as those of the primary
     934             :          then, we can probably create a new specialization.  */
     935      927630 :       tree type_constr = current_template_constraints ();
     936             : 
     937      927630 :       if (type == TREE_TYPE (tmpl))
     938             :         {
     939          18 :           tree main_constr = get_constraints (tmpl);
     940          18 :           if (equivalent_constraints (type_constr, main_constr))
     941             :             return false;
     942             :         }
     943             : 
     944             :       /* Also, if there's a pre-existing specialization with matching
     945             :          constraints, then this also isn't new.  */
     946      927612 :       tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
     947      971579 :       while (specs)
     948             :         {
     949      954213 :           tree spec_tmpl = TREE_VALUE (specs);
     950      954213 :           tree spec_args = TREE_PURPOSE (specs);
     951      954213 :           tree spec_constr = get_constraints (spec_tmpl);
     952      954213 :           if (comp_template_args (args, spec_args)
     953      954213 :               && equivalent_constraints (type_constr, spec_constr))
     954             :             {
     955      910246 :               type = TREE_TYPE (spec_tmpl);
     956      910246 :               return false;
     957             :             }
     958       43967 :           specs = TREE_CHAIN (specs);
     959             :         }
     960             : 
     961             :       /* Create a new type node (and corresponding type decl)
     962             :          for the newly declared specialization.  */
     963       17366 :       tree t = make_class_type (TREE_CODE (type));
     964       17366 :       CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
     965       17366 :       SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
     966             : 
     967             :       /* We only need a separate type node for storing the definition of this
     968             :          partial specialization; uses of S<T*> are unconstrained, so all are
     969             :          equivalent.  So keep TYPE_CANONICAL the same.  */
     970       17366 :       TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
     971             : 
     972             :       /* Build the corresponding type decl.  */
     973       17366 :       tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
     974       17366 :       DECL_CONTEXT (d) = TYPE_CONTEXT (t);
     975       17366 :       DECL_SOURCE_LOCATION (d) = input_location;
     976       17366 :       TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
     977       17366 :       TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
     978             : 
     979       17366 :       set_instantiating_module (d);
     980       17366 :       DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl);
     981             : 
     982       17366 :       type = t;
     983       17366 :       return true;
     984             :     }
     985             : 
     986             :   return false;
     987             : }
     988             : 
     989             : /* The TYPE is being declared.  If it is a template type, that means it
     990             :    is a partial specialization.  Do appropriate error-checking.  */
     991             : 
     992             : tree
     993    45647575 : maybe_process_partial_specialization (tree type)
     994             : {
     995    45647575 :   tree context;
     996             : 
     997    45647575 :   if (type == error_mark_node)
     998             :     return error_mark_node;
     999             : 
    1000             :   /* A lambda that appears in specialization context is not itself a
    1001             :      specialization.  */
    1002    45647513 :   if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
    1003             :     return type;
    1004             : 
    1005             :   /* An injected-class-name is not a specialization.  */
    1006    45399116 :   if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
    1007             :     return type;
    1008             : 
    1009    45399108 :   if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
    1010             :     {
    1011           4 :       error ("name of class shadows template template parameter %qD",
    1012           4 :              TYPE_NAME (type));
    1013           4 :       return error_mark_node;
    1014             :     }
    1015             : 
    1016    45399104 :   context = TYPE_CONTEXT (type);
    1017             : 
    1018    45399104 :   if (TYPE_ALIAS_P (type))
    1019             :     {
    1020           6 :       tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
    1021             : 
    1022          12 :       if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
    1023           6 :         error ("specialization of alias template %qD",
    1024           6 :                TI_TEMPLATE (tinfo));
    1025             :       else
    1026           0 :         error ("explicit specialization of non-template %qT", type);
    1027           6 :       return error_mark_node;
    1028             :     }
    1029    45399098 :   else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
    1030             :     {
    1031             :       /* This is for ordinary explicit specialization and partial
    1032             :          specialization of a template class such as:
    1033             : 
    1034             :            template <> class C<int>;
    1035             : 
    1036             :          or:
    1037             : 
    1038             :            template <class T> class C<T*>;
    1039             : 
    1040             :          Make sure that `C<int>' and `C<T*>' are implicit instantiations.  */
    1041             : 
    1042    23312869 :       if (maybe_new_partial_specialization (type))
    1043             :         {
    1044     7828989 :           if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type))
    1045     7828989 :               && !at_namespace_scope_p ())
    1046          16 :             return error_mark_node;
    1047     7828973 :           SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
    1048     7828973 :           DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
    1049     7828973 :           if (processing_template_decl)
    1050             :             {
    1051     4938966 :               tree decl = push_template_decl (TYPE_MAIN_DECL (type));
    1052     4938966 :               if (decl == error_mark_node)
    1053             :                 return error_mark_node;
    1054     4938876 :               return TREE_TYPE (decl);
    1055             :             }
    1056             :         }
    1057    15483880 :       else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
    1058           0 :         error ("specialization of %qT after instantiation", type);
    1059        6181 :       else if (errorcount && !processing_specialization
    1060        4820 :                 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
    1061    15488700 :                && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
    1062             :         /* Trying to define a specialization either without a template<> header
    1063             :            or in an inappropriate place.  We've already given an error, so just
    1064             :            bail now so we don't actually define the specialization.  */
    1065        1341 :         return error_mark_node;
    1066             :     }
    1067    21564936 :   else if (CLASS_TYPE_P (type)
    1068    21564936 :            && !CLASSTYPE_USE_TEMPLATE (type)
    1069    21564936 :            && CLASSTYPE_TEMPLATE_INFO (type)
    1070    15534722 :            && context && CLASS_TYPE_P (context)
    1071    23515582 :            && CLASSTYPE_TEMPLATE_INFO (context))
    1072             :     {
    1073             :       /* This is for an explicit specialization of member class
    1074             :          template according to [temp.expl.spec/18]:
    1075             : 
    1076             :            template <> template <class U> class C<int>::D;
    1077             : 
    1078             :          The context `C<int>' must be an implicit instantiation.
    1079             :          Otherwise this is just a member class template declared
    1080             :          earlier like:
    1081             : 
    1082             :            template <> class C<int> { template <class U> class D; };
    1083             :            template <> template <class U> class C<int>::D;
    1084             : 
    1085             :          In the first case, `C<int>::D' is a specialization of `C<T>::D'
    1086             :          while in the second case, `C<int>::D' is a primary template
    1087             :          and `C<T>::D' may not exist.  */
    1088             : 
    1089     1072149 :       if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
    1090     1072149 :           && !COMPLETE_TYPE_P (type))
    1091             :         {
    1092          34 :           tree t;
    1093          34 :           tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
    1094             : 
    1095          34 :           if (current_namespace
    1096          34 :               != decl_namespace_context (tmpl))
    1097             :             {
    1098           0 :               if (permerror (input_location,
    1099             :                              "specialization of %qD in different namespace",
    1100             :                              type))
    1101           0 :                 inform (DECL_SOURCE_LOCATION (tmpl),
    1102             :                         "from definition of %q#D", tmpl);
    1103             :             }
    1104             : 
    1105             :           /* Check for invalid specialization after instantiation:
    1106             : 
    1107             :                template <> template <> class C<int>::D<int>;
    1108             :                template <> template <class U> class C<int>::D;  */
    1109             : 
    1110          34 :           for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
    1111          49 :                t; t = TREE_CHAIN (t))
    1112             :             {
    1113          15 :               tree inst = TREE_VALUE (t);
    1114          15 :               if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
    1115          15 :                   || !COMPLETE_OR_OPEN_TYPE_P (inst))
    1116             :                 {
    1117             :                   /* We already have a full specialization of this partial
    1118             :                      instantiation, or a full specialization has been
    1119             :                      looked up but not instantiated.  Reassign it to the
    1120             :                      new member specialization template.  */
    1121          11 :                   spec_entry elt;
    1122          11 :                   spec_entry *entry;
    1123             : 
    1124          11 :                   elt.tmpl = most_general_template (tmpl);
    1125          11 :                   elt.args = CLASSTYPE_TI_ARGS (inst);
    1126          11 :                   elt.spec = inst;
    1127             : 
    1128          11 :                   type_specializations->remove_elt (&elt);
    1129             : 
    1130          11 :                   elt.tmpl = tmpl;
    1131          11 :                   CLASSTYPE_TI_ARGS (inst)
    1132          11 :                     = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
    1133             : 
    1134          11 :                   spec_entry **slot
    1135          11 :                     = type_specializations->find_slot (&elt, INSERT);
    1136          11 :                   entry = ggc_alloc<spec_entry> ();
    1137          11 :                   *entry = elt;
    1138          11 :                   *slot = entry;
    1139             :                 }
    1140             :               else
    1141             :                 /* But if we've had an implicit instantiation, that's a
    1142             :                    problem ([temp.expl.spec]/6).  */
    1143           4 :                 error ("specialization %qT after instantiation %qT",
    1144             :                        type, inst);
    1145             :             }
    1146             : 
    1147             :           /* Mark TYPE as a specialization.  And as a result, we only
    1148             :              have one level of template argument for the innermost
    1149             :              class template.  */
    1150          34 :           SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
    1151          34 :           DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
    1152          34 :           CLASSTYPE_TI_ARGS (type)
    1153          68 :             = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
    1154             :         }
    1155             :     }
    1156    21014080 :   else if (processing_specialization)
    1157             :     {
    1158             :        /* Someday C++0x may allow for enum template specialization.  */
    1159          21 :       if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
    1160          42 :           && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
    1161           9 :         pedwarn (input_location, OPT_Wpedantic, "template specialization "
    1162             :                  "of %qD not allowed by ISO C++", type);
    1163             :       else
    1164             :         {
    1165          15 :           error ("explicit specialization of non-template %qT", type);
    1166          15 :           return error_mark_node;
    1167             :         }
    1168             :     }
    1169             : 
    1170    40458760 :   return type;
    1171             : }
    1172             : 
    1173             : /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
    1174             :    gone through coerce_template_parms by now.  */
    1175             : 
    1176             : static void
    1177   363198263 : verify_unstripped_args_1 (tree inner)
    1178             : {
    1179  1166861659 :   for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
    1180             :     {
    1181   803663396 :       tree arg = TREE_VEC_ELT (inner, i);
    1182   803663396 :       if (TREE_CODE (arg) == TEMPLATE_DECL)
    1183             :         /* OK */;
    1184   803105728 :       else if (TYPE_P (arg))
    1185   742554046 :         gcc_assert (strip_typedefs (arg, NULL) == arg);
    1186    60551682 :       else if (ARGUMENT_PACK_P (arg))
    1187      376667 :         verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
    1188    60175015 :       else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
    1189             :         /* Allow typedefs on the type of a non-type argument, since a
    1190             :            parameter can have them.  */;
    1191             :       else
    1192    60174803 :         gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
    1193             :     }
    1194   363198263 : }
    1195             : 
    1196             : static void
    1197   428282496 : verify_unstripped_args (tree args)
    1198             : {
    1199   428282496 :   ++processing_template_decl;
    1200   428282496 :   if (!any_dependent_template_arguments_p (args))
    1201   362821596 :     verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
    1202   428282496 :   --processing_template_decl;
    1203   428282496 : }
    1204             : 
    1205             : /* Retrieve the specialization (in the sense of [temp.spec] - a
    1206             :    specialization is either an instantiation or an explicit
    1207             :    specialization) of TMPL for the given template ARGS.  If there is
    1208             :    no such specialization, return NULL_TREE.  The ARGS are a vector of
    1209             :    arguments, or a vector of vectors of arguments, in the case of
    1210             :    templates with more than one level of parameters.
    1211             : 
    1212             :    If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
    1213             :    then we search for a partial specialization matching ARGS.  This
    1214             :    parameter is ignored if TMPL is not a class template.
    1215             : 
    1216             :    We can also look up a FIELD_DECL, if it is a lambda capture pack; the
    1217             :    result is a NONTYPE_ARGUMENT_PACK.  */
    1218             : 
    1219             : static tree
    1220   428282496 : retrieve_specialization (tree tmpl, tree args, hashval_t hash)
    1221             : {
    1222   428282496 :   if (tmpl == NULL_TREE)
    1223             :     return NULL_TREE;
    1224             : 
    1225   428282496 :   if (args == error_mark_node)
    1226             :     return NULL_TREE;
    1227             : 
    1228   428282496 :   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
    1229             :               || TREE_CODE (tmpl) == FIELD_DECL);
    1230             : 
    1231             :   /* There should be as many levels of arguments as there are
    1232             :      levels of parameters.  */
    1233   856564992 :   gcc_assert (TMPL_ARGS_DEPTH (args)
    1234             :               == (TREE_CODE (tmpl) == TEMPLATE_DECL
    1235             :                   ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
    1236             :                   : template_class_depth (DECL_CONTEXT (tmpl))));
    1237             : 
    1238   428282496 :   if (flag_checking)
    1239   428282496 :     verify_unstripped_args (args);
    1240             : 
    1241             :   /* Lambda functions in templates aren't instantiated normally, but through
    1242             :      tsubst_lambda_expr.  */
    1243   428282496 :   if (lambda_fn_in_template_p (tmpl))
    1244             :     return NULL_TREE;
    1245             : 
    1246   428282496 :   spec_entry elt;
    1247   428282496 :   elt.tmpl = tmpl;
    1248   428282496 :   elt.args = args;
    1249   428282496 :   elt.spec = NULL_TREE;
    1250             : 
    1251   428282496 :   spec_hash_table *specializations;
    1252   428282496 :   if (DECL_CLASS_TEMPLATE_P (tmpl))
    1253     1027371 :     specializations = type_specializations;
    1254             :   else
    1255   427255125 :     specializations = decl_specializations;
    1256             : 
    1257   428282496 :   if (hash == 0)
    1258   208202462 :     hash = spec_hasher::hash (&elt);
    1259   428282496 :   if (spec_entry *found = specializations->find_with_hash (&elt, hash))
    1260   156992251 :     return found->spec;
    1261             : 
    1262             :   return NULL_TREE;
    1263             : }
    1264             : 
    1265             : /* Like retrieve_specialization, but for local declarations.  */
    1266             : 
    1267             : tree
    1268    87869020 : retrieve_local_specialization (tree tmpl)
    1269             : {
    1270    87869020 :   if (local_specializations == NULL)
    1271             :     return NULL_TREE;
    1272             : 
    1273    85657815 :   tree *slot = local_specializations->get (tmpl);
    1274    85657815 :   return slot ? *slot : NULL_TREE;
    1275             : }
    1276             : 
    1277             : /* Returns nonzero iff DECL is a specialization of TMPL.  */
    1278             : 
    1279             : int
    1280      653814 : is_specialization_of (tree decl, tree tmpl)
    1281             : {
    1282      653814 :   tree t;
    1283             : 
    1284      653814 :   if (TREE_CODE (decl) == FUNCTION_DECL)
    1285             :     {
    1286      180839 :       for (t = decl;
    1287      361036 :            t != NULL_TREE;
    1288      180839 :            t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
    1289      246311 :         if (t == tmpl)
    1290             :           return 1;
    1291             :     }
    1292             :   else
    1293             :     {
    1294      473617 :       gcc_assert (TREE_CODE (decl) == TYPE_DECL);
    1295             : 
    1296      473617 :       for (t = TREE_TYPE (decl);
    1297     1011344 :            t != NULL_TREE;
    1298      537727 :            t = CLASSTYPE_USE_TEMPLATE (t)
    1299      537727 :              ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
    1300      926083 :         if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
    1301             :           return 1;
    1302             :     }
    1303             : 
    1304             :   return 0;
    1305             : }
    1306             : 
    1307             : /* Returns nonzero iff DECL is a specialization of friend declaration
    1308             :    FRIEND_DECL according to [temp.friend].  */
    1309             : 
    1310             : bool
    1311      682760 : is_specialization_of_friend (tree decl, tree friend_decl)
    1312             : {
    1313      682760 :   bool need_template = true;
    1314      682760 :   int template_depth;
    1315             : 
    1316      682760 :   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
    1317             :               || TREE_CODE (decl) == TYPE_DECL);
    1318             : 
    1319             :   /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
    1320             :      of a template class, we want to check if DECL is a specialization
    1321             :      if this.  */
    1322      682760 :   if (TREE_CODE (friend_decl) == FUNCTION_DECL
    1323       29206 :       && DECL_CLASS_SCOPE_P (friend_decl)
    1324          74 :       && DECL_TEMPLATE_INFO (friend_decl)
    1325      682832 :       && !DECL_USE_TEMPLATE (friend_decl))
    1326             :     {
    1327             :       /* We want a TEMPLATE_DECL for `is_specialization_of'.  */
    1328          72 :       friend_decl = DECL_TI_TEMPLATE (friend_decl);
    1329          72 :       need_template = false;
    1330             :     }
    1331      682688 :   else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
    1332      682688 :            && !PRIMARY_TEMPLATE_P (friend_decl))
    1333             :     need_template = false;
    1334             : 
    1335             :   /* There is nothing to do if this is not a template friend.  */
    1336      682760 :   if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
    1337             :     return false;
    1338             : 
    1339      653626 :   if (is_specialization_of (decl, friend_decl))
    1340             :     return true;
    1341             : 
    1342             :   /* [temp.friend/6]
    1343             :      A member of a class template may be declared to be a friend of a
    1344             :      non-template class.  In this case, the corresponding member of
    1345             :      every specialization of the class template is a friend of the
    1346             :      class granting friendship.
    1347             : 
    1348             :      For example, given a template friend declaration
    1349             : 
    1350             :        template <class T> friend void A<T>::f();
    1351             : 
    1352             :      the member function below is considered a friend
    1353             : 
    1354             :        template <> struct A<int> {
    1355             :          void f();
    1356             :        };
    1357             : 
    1358             :      For this type of template friend, TEMPLATE_DEPTH below will be
    1359             :      nonzero.  To determine if DECL is a friend of FRIEND, we first
    1360             :      check if the enclosing class is a specialization of another.  */
    1361             : 
    1362      199982 :   template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
    1363      199982 :   if (template_depth
    1364         196 :       && DECL_CLASS_SCOPE_P (decl)
    1365      200170 :       && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
    1366         188 :                                CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
    1367             :     {
    1368             :       /* Next, we check the members themselves.  In order to handle
    1369             :          a few tricky cases, such as when FRIEND_DECL's are
    1370             : 
    1371             :            template <class T> friend void A<T>::g(T t);
    1372             :            template <class T> template <T t> friend void A<T>::h();
    1373             : 
    1374             :          and DECL's are
    1375             : 
    1376             :            void A<int>::g(int);
    1377             :            template <int> void A<int>::h();
    1378             : 
    1379             :          we need to figure out ARGS, the template arguments from
    1380             :          the context of DECL.  This is required for template substitution
    1381             :          of `T' in the function parameter of `g' and template parameter
    1382             :          of `h' in the above examples.  Here ARGS corresponds to `int'.  */
    1383             : 
    1384         184 :       tree context = DECL_CONTEXT (decl);
    1385         184 :       tree args = NULL_TREE;
    1386         184 :       int current_depth = 0;
    1387             : 
    1388         368 :       while (current_depth < template_depth)
    1389             :         {
    1390         184 :           if (CLASSTYPE_TEMPLATE_INFO (context))
    1391             :             {
    1392         184 :               if (current_depth == 0)
    1393         368 :                 args = TYPE_TI_ARGS (context);
    1394             :               else
    1395           0 :                 args = add_to_template_args (TYPE_TI_ARGS (context), args);
    1396         184 :               current_depth++;
    1397             :             }
    1398         184 :           context = TYPE_CONTEXT (context);
    1399             :         }
    1400             : 
    1401         184 :       if (TREE_CODE (decl) == FUNCTION_DECL)
    1402             :         {
    1403          96 :           bool is_template;
    1404          96 :           tree friend_type;
    1405          96 :           tree decl_type;
    1406          96 :           tree friend_args_type;
    1407          96 :           tree decl_args_type;
    1408             : 
    1409             :           /* Make sure that both DECL and FRIEND_DECL are templates or
    1410             :              non-templates.  */
    1411          96 :           is_template = DECL_TEMPLATE_INFO (decl)
    1412          96 :                         && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
    1413          96 :           if (need_template ^ is_template)
    1414             :             return false;
    1415          72 :           else if (is_template)
    1416             :             {
    1417             :               /* If both are templates, check template parameter list.  */
    1418          36 :               tree friend_parms
    1419          36 :                 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
    1420             :                                          args, tf_none);
    1421          36 :               if (!comp_template_parms
    1422          36 :                      (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
    1423             :                       friend_parms))
    1424             :                 return false;
    1425             : 
    1426          24 :               decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
    1427             :             }
    1428             :           else
    1429          36 :             decl_type = TREE_TYPE (decl);
    1430             : 
    1431          60 :           friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
    1432             :                                               tf_none, NULL_TREE);
    1433          60 :           if (friend_type == error_mark_node)
    1434             :             return false;
    1435             : 
    1436             :           /* Check if return types match.  */
    1437          60 :           if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
    1438             :             return false;
    1439             : 
    1440             :           /* Check if function parameter types match, ignoring the
    1441             :              `this' parameter.  */
    1442          52 :           friend_args_type = TYPE_ARG_TYPES (friend_type);
    1443          52 :           decl_args_type = TYPE_ARG_TYPES (decl_type);
    1444          52 :           if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
    1445          52 :             friend_args_type = TREE_CHAIN (friend_args_type);
    1446          52 :           if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
    1447          52 :             decl_args_type = TREE_CHAIN (decl_args_type);
    1448             : 
    1449          52 :           return compparms (decl_args_type, friend_args_type);
    1450             :         }
    1451             :       else
    1452             :         {
    1453             :           /* DECL is a TYPE_DECL */
    1454          88 :           bool is_template;
    1455          88 :           tree decl_type = TREE_TYPE (decl);
    1456             : 
    1457             :           /* Make sure that both DECL and FRIEND_DECL are templates or
    1458             :              non-templates.  */
    1459          88 :           is_template
    1460          88 :             = CLASSTYPE_TEMPLATE_INFO (decl_type)
    1461          88 :               && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
    1462             : 
    1463          88 :           if (need_template ^ is_template)
    1464             :             return false;
    1465          88 :           else if (is_template)
    1466             :             {
    1467          68 :               tree friend_parms;
    1468             :               /* If both are templates, check the name of the two
    1469             :                  TEMPLATE_DECL's first because is_friend didn't.  */
    1470          68 :               if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
    1471          68 :                   != DECL_NAME (friend_decl))
    1472             :                 return false;
    1473             : 
    1474             :               /* Now check template parameter list.  */
    1475          64 :               friend_parms
    1476          64 :                 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
    1477             :                                          args, tf_none);
    1478          64 :               return comp_template_parms
    1479          64 :                 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
    1480          64 :                  friend_parms);
    1481             :             }
    1482             :           else
    1483          20 :             return (DECL_NAME (decl)
    1484          20 :                     == DECL_NAME (friend_decl));
    1485             :         }
    1486             :     }
    1487             :   return false;
    1488             : }
    1489             : 
    1490             : /* Register the specialization SPEC as a specialization of TMPL with
    1491             :    the indicated ARGS.  IS_FRIEND indicates whether the specialization
    1492             :    is actually just a friend declaration.  ATTRLIST is the list of
    1493             :    attributes that the specialization is declared with or NULL when
    1494             :    it isn't.  Returns SPEC, or an equivalent prior declaration, if
    1495             :    available.
    1496             : 
    1497             :    We also store instantiations of field packs in the hash table, even
    1498             :    though they are not themselves templates, to make lookup easier.  */
    1499             : 
    1500             : static tree
    1501   145886403 : register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
    1502             :                          hashval_t hash)
    1503             : {
    1504   145886403 :   tree fn;
    1505             : 
    1506   145886403 :   gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
    1507             :               || (TREE_CODE (tmpl) == FIELD_DECL
    1508             :                   && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
    1509             : 
    1510   145886403 :   if (TREE_CODE (spec) == FUNCTION_DECL
    1511   145886403 :       && uses_template_parms (DECL_TI_ARGS (spec)))
    1512             :     /* This is the FUNCTION_DECL for a partial instantiation.  Don't
    1513             :        register it; we want the corresponding TEMPLATE_DECL instead.
    1514             :        We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
    1515             :        the more obvious `uses_template_parms (spec)' to avoid problems
    1516             :        with default function arguments.  In particular, given
    1517             :        something like this:
    1518             : 
    1519             :           template <class T> void f(T t1, T t = T())
    1520             : 
    1521             :        the default argument expression is not substituted for in an
    1522             :        instantiation unless and until it is actually needed.  */
    1523             :     return spec;
    1524             : 
    1525   133901071 :   spec_entry elt;
    1526   133901071 :   elt.tmpl = tmpl;
    1527   133901071 :   elt.args = args;
    1528   133901071 :   elt.spec = spec;
    1529             : 
    1530   133901071 :   if (hash == 0)
    1531      907561 :     hash = spec_hasher::hash (&elt);
    1532             : 
    1533   133901071 :   spec_entry **slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
    1534   133901071 :   if (*slot)
    1535      408908 :     fn = (*slot)->spec;
    1536             :   else
    1537             :     fn = NULL_TREE;
    1538             : 
    1539             :   /* We can sometimes try to re-register a specialization that we've
    1540             :      already got.  In particular, regenerate_decl_from_template calls
    1541             :      duplicate_decls which will update the specialization list.  But,
    1542             :      we'll still get called again here anyhow.  It's more convenient
    1543             :      to simply allow this than to try to prevent it.  */
    1544   133901071 :   if (fn == spec)
    1545             :     return spec;
    1546   133900368 :   else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
    1547             :     {
    1548      408134 :       if (DECL_TEMPLATE_INSTANTIATION (fn))
    1549             :         {
    1550      407883 :           if (DECL_ODR_USED (fn)
    1551      407883 :               || DECL_EXPLICIT_INSTANTIATION (fn))
    1552             :             {
    1553          10 :               error ("specialization of %qD after instantiation",
    1554             :                      fn);
    1555          10 :               return error_mark_node;
    1556             :             }
    1557             :           else
    1558             :             {
    1559      407873 :               tree clone;
    1560             :               /* This situation should occur only if the first
    1561             :                  specialization is an implicit instantiation, the
    1562             :                  second is an explicit specialization, and the
    1563             :                  implicit instantiation has not yet been used.  That
    1564             :                  situation can occur if we have implicitly
    1565             :                  instantiated a member function and then specialized
    1566             :                  it later.
    1567             : 
    1568             :                  We can also wind up here if a friend declaration that
    1569             :                  looked like an instantiation turns out to be a
    1570             :                  specialization:
    1571             : 
    1572             :                    template <class T> void foo(T);
    1573             :                    class S { friend void foo<>(int) };
    1574             :                    template <> void foo(int);
    1575             : 
    1576             :                  We transform the existing DECL in place so that any
    1577             :                  pointers to it become pointers to the updated
    1578             :                  declaration.
    1579             : 
    1580             :                  If there was a definition for the template, but not
    1581             :                  for the specialization, we want this to look as if
    1582             :                  there were no definition, and vice versa.  */
    1583      407873 :               DECL_INITIAL (fn) = NULL_TREE;
    1584      407873 :               duplicate_decls (spec, fn, /*hiding=*/is_friend);
    1585             : 
    1586             :               /* The call to duplicate_decls will have applied
    1587             :                  [temp.expl.spec]:
    1588             : 
    1589             :                    An explicit specialization of a function template
    1590             :                    is inline only if it is explicitly declared to be,
    1591             :                    and independently of whether its function template
    1592             :                    is.
    1593             : 
    1594             :                 to the primary function; now copy the inline bits to
    1595             :                 the various clones.  */
    1596      542183 :               FOR_EACH_CLONE (clone, fn)
    1597             :                 {
    1598      134310 :                   DECL_DECLARED_INLINE_P (clone)
    1599      134310 :                     = DECL_DECLARED_INLINE_P (fn);
    1600      268620 :                   DECL_SOURCE_LOCATION (clone)
    1601      134310 :                     = DECL_SOURCE_LOCATION (fn);
    1602      134310 :                   DECL_DELETED_FN (clone)
    1603      268620 :                     = DECL_DELETED_FN (fn);
    1604             :                 }
    1605      407873 :               check_specialization_namespace (tmpl);
    1606             : 
    1607      407873 :               return fn;
    1608             :             }
    1609             :         }
    1610         251 :       else if (DECL_TEMPLATE_SPECIALIZATION (fn))
    1611             :         {
    1612         251 :           tree dd = duplicate_decls (spec, fn, /*hiding=*/is_friend);
    1613         251 :           if (dd == error_mark_node)
    1614             :             /* We've already complained in duplicate_decls.  */
    1615             :             return error_mark_node;
    1616             : 
    1617         229 :           if (dd == NULL_TREE && DECL_INITIAL (spec))
    1618             :             /* Dup decl failed, but this is a new definition. Set the
    1619             :                line number so any errors match this new
    1620             :                definition.  */
    1621           0 :             DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
    1622             : 
    1623         229 :           return fn;
    1624             :         }
    1625             :     }
    1626   133492234 :   else if (fn)
    1627          71 :     return duplicate_decls (spec, fn, /*hiding=*/is_friend);
    1628             : 
    1629             :   /* A specialization must be declared in the same namespace as the
    1630             :      template it is specializing.  */
    1631   133492163 :   if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
    1632   133990804 :       && !check_specialization_namespace (tmpl))
    1633           0 :     DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
    1634             : 
    1635   133492163 :   spec_entry *entry = ggc_alloc<spec_entry> ();
    1636   133492163 :   gcc_assert (tmpl && args && spec);
    1637   133492163 :   *entry = elt;
    1638   133492163 :   *slot = entry;
    1639    62151472 :   if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
    1640    10939851 :        && PRIMARY_TEMPLATE_P (tmpl)
    1641    10444633 :        && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
    1642   191358501 :       || variable_template_p (tmpl))
    1643             :     /* If TMPL is a forward declaration of a template function, keep a list
    1644             :        of all specializations in case we need to reassign them to a friend
    1645             :        template later in tsubst_friend_function.
    1646             : 
    1647             :        Also keep a list of all variable template instantiations so that
    1648             :        process_partial_specialization can check whether a later partial
    1649             :        specialization would have used it.  */
    1650    10794466 :     DECL_TEMPLATE_INSTANTIATIONS (tmpl)
    1651    10794466 :       = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
    1652             : 
    1653             :   return spec;
    1654             : }
    1655             : 
    1656             : /* Restricts tree and type comparisons.  */
    1657             : int comparing_specializations;
    1658             : int comparing_dependent_aliases;
    1659             : 
    1660             : /* Returns true iff two spec_entry nodes are equivalent.  */
    1661             : 
    1662             : bool
    1663  6393966912 : spec_hasher::equal (spec_entry *e1, spec_entry *e2)
    1664             : {
    1665  6393966912 :   int equal;
    1666             : 
    1667  6393966912 :   ++comparing_specializations;
    1668  6393966912 :   ++comparing_dependent_aliases;
    1669  6393966912 :   ++processing_template_decl;
    1670  6752686006 :   equal = (e1->tmpl == e2->tmpl
    1671  6393966912 :            && comp_template_args (e1->args, e2->args));
    1672   358719094 :   if (equal && flag_concepts
    1673             :       /* tmpl could be a FIELD_DECL for a capture pack.  */
    1674    26393877 :       && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
    1675    26393877 :       && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
    1676      662623 :       && uses_template_parms (e1->args))
    1677             :     {
    1678             :       /* Partial specializations of a variable template can be distinguished by
    1679             :          constraints.  */
    1680         253 :       tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
    1681         253 :       tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
    1682         253 :       equal = equivalent_constraints (c1, c2);
    1683             :     }
    1684  6393966912 :   --processing_template_decl;
    1685  6393966912 :   --comparing_dependent_aliases;
    1686  6393966912 :   --comparing_specializations;
    1687             : 
    1688  6393966912 :   return equal;
    1689             : }
    1690             : 
    1691             : /* Returns a hash for a template TMPL and template arguments ARGS.  */
    1692             : 
    1693             : static hashval_t
    1694  5835858460 : hash_tmpl_and_args (tree tmpl, tree args)
    1695             : {
    1696  5835858460 :   hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
    1697  5835858460 :   return iterative_hash_template_arg (args, val);
    1698             : }
    1699             : 
    1700             : hashval_t
    1701  5813417537 : spec_hasher::hash (tree tmpl, tree args)
    1702             : {
    1703  5813417537 :   ++comparing_specializations;
    1704  5813417537 :   hashval_t val = hash_tmpl_and_args (tmpl, args);
    1705  5813417537 :   --comparing_specializations;
    1706  5813417537 :   return val;
    1707             : }
    1708             : 
    1709             : /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
    1710             :    ignoring SPEC.  */
    1711             : 
    1712             : hashval_t
    1713  5646829481 : spec_hasher::hash (spec_entry *e)
    1714             : {
    1715  5374579414 :   return spec_hasher::hash (e->tmpl, e->args);
    1716             : }
    1717             : 
    1718             : /* Recursively calculate a hash value for a template argument ARG, for use
    1719             :    in the hash tables of template specializations.   We must be
    1720             :    careful to (at least) skip the same entities template_args_equal
    1721             :    does.  */
    1722             : 
    1723             : hashval_t
    1724 25359993494 : iterative_hash_template_arg (tree arg, hashval_t val)
    1725             : {
    1726 25359993494 :   if (arg == NULL_TREE)
    1727   208988517 :     return iterative_hash_object (arg, val);
    1728             : 
    1729 25151004977 :   if (!TYPE_P (arg))
    1730             :     /* Strip nop-like things, but not the same as STRIP_NOPS.  */
    1731 10729193636 :     while (CONVERT_EXPR_P (arg)
    1732 10729174580 :            || TREE_CODE (arg) == NON_LVALUE_EXPR
    1733 21460500851 :            || class_nttp_const_wrapper_p (arg))
    1734     2151777 :       arg = TREE_OPERAND (arg, 0);
    1735             : 
    1736 25151004977 :   enum tree_code code = TREE_CODE (arg);
    1737             : 
    1738 25151004977 :   val = iterative_hash_object (code, val);
    1739             : 
    1740 25151004977 :   switch (code)
    1741             :     {
    1742           0 :     case ARGUMENT_PACK_SELECT:
    1743             :       /* Getting here with an ARGUMENT_PACK_SELECT means we're probably
    1744             :          preserving it in a hash table, which is bad because it will change
    1745             :          meaning when gen_elem_of_pack_expansion_instantiation changes the
    1746             :          ARGUMENT_PACK_SELECT_INDEX.  */
    1747           0 :       gcc_unreachable ();
    1748             : 
    1749             :     case ERROR_MARK:
    1750             :       return val;
    1751             : 
    1752   998875041 :     case IDENTIFIER_NODE:
    1753   998875041 :       return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
    1754             : 
    1755  7737410081 :     case TREE_VEC:
    1756 22439324706 :       for (tree elt : tree_vec_range (arg))
    1757 14701914625 :         val = iterative_hash_template_arg (elt, val);
    1758  7737410081 :       return val;
    1759             : 
    1760   204214094 :     case TYPE_PACK_EXPANSION:
    1761   204214094 :     case EXPR_PACK_EXPANSION:
    1762   204214094 :       val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
    1763   204214094 :       return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
    1764             : 
    1765   720268128 :     case TYPE_ARGUMENT_PACK:
    1766   720268128 :     case NONTYPE_ARGUMENT_PACK:
    1767   720268128 :       return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
    1768             : 
    1769             :     case TREE_LIST:
    1770    94448720 :       for (; arg; arg = TREE_CHAIN (arg))
    1771    47224374 :         val = iterative_hash_template_arg (TREE_VALUE (arg), val);
    1772             :       return val;
    1773             : 
    1774         865 :     case OVERLOAD:
    1775        2595 :       for (lkp_iterator iter (arg); iter; ++iter)
    1776         865 :         val = iterative_hash_template_arg (*iter, val);
    1777         865 :       return val;
    1778             : 
    1779     2593392 :     case CONSTRUCTOR:
    1780     2593392 :       {
    1781     2593392 :         iterative_hash_template_arg (TREE_TYPE (arg), val);
    1782     3093653 :         for (auto &e: CONSTRUCTOR_ELTS (arg))
    1783             :           {
    1784      169357 :             val = iterative_hash_template_arg (e.index, val);
    1785      169357 :             val = iterative_hash_template_arg (e.value, val);
    1786             :           }
    1787             :         return val;
    1788             :       }
    1789             : 
    1790     2189083 :     case PARM_DECL:
    1791     2189083 :       if (!DECL_ARTIFICIAL (arg))
    1792             :         {
    1793     2185928 :           val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
    1794     2185928 :           val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
    1795             :         }
    1796     2189083 :       return iterative_hash_template_arg (TREE_TYPE (arg), val);
    1797             : 
    1798           0 :     case TARGET_EXPR:
    1799           0 :       return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
    1800             : 
    1801       14341 :     case PTRMEM_CST:
    1802       14341 :       val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
    1803       14341 :       return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
    1804             : 
    1805   221622727 :     case TEMPLATE_PARM_INDEX:
    1806   221622727 :       val = iterative_hash_template_arg
    1807   221622727 :         (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
    1808   221622727 :       val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
    1809   221622727 :       return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
    1810             : 
    1811   125659711 :     case TRAIT_EXPR:
    1812   125659711 :       val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
    1813   125659711 :       val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
    1814   125659711 :       return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
    1815             : 
    1816    92036993 :     case BASELINK:
    1817    92036993 :       val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
    1818             :                                          val);
    1819    92036993 :       return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
    1820    92036993 :                                           val);
    1821             : 
    1822         663 :     case MODOP_EXPR:
    1823         663 :       val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
    1824         663 :       code = TREE_CODE (TREE_OPERAND (arg, 1));
    1825         663 :       val = iterative_hash_object (code, val);
    1826         663 :       return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
    1827             : 
    1828         124 :     case LAMBDA_EXPR:
    1829             :       /* [temp.over.link] Two lambda-expressions are never considered
    1830             :          equivalent.
    1831             : 
    1832             :          So just hash the closure type.  */
    1833         124 :       return iterative_hash_template_arg (TREE_TYPE (arg), val);
    1834             : 
    1835    47699753 :     case CAST_EXPR:
    1836    47699753 :     case IMPLICIT_CONV_EXPR:
    1837    47699753 :     case STATIC_CAST_EXPR:
    1838    47699753 :     case REINTERPRET_CAST_EXPR:
    1839    47699753 :     case CONST_CAST_EXPR:
    1840    47699753 :     case DYNAMIC_CAST_EXPR:
    1841    47699753 :     case NEW_EXPR:
    1842    47699753 :       val = iterative_hash_template_arg (TREE_TYPE (arg), val);
    1843             :       /* Now hash operands as usual.  */
    1844    47699753 :       break;
    1845             : 
    1846   260168123 :     case CALL_EXPR:
    1847   260168123 :       {
    1848   260168123 :         tree fn = CALL_EXPR_FN (arg);
    1849   260168123 :         if (tree name = call_expr_dependent_name (arg))
    1850             :           {
    1851   108398318 :             if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
    1852    96703183 :               val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
    1853             :             fn = name;
    1854             :           }
    1855   260168123 :         val = iterative_hash_template_arg (fn, val);
    1856   260168123 :         call_expr_arg_iterator ai;
    1857   320131493 :         for (tree x = first_call_expr_arg (arg, &ai); x;
    1858    59963370 :              x = next_call_expr_arg (&ai))
    1859    59963370 :           val = iterative_hash_template_arg (x, val);
    1860   260168123 :         return val;
    1861             :       }
    1862             : 
    1863             :     default:
    1864             :       break;
    1865             :     }
    1866             : 
    1867 14738727239 :   char tclass = TREE_CODE_CLASS (code);
    1868 14738727239 :   switch (tclass)
    1869             :     {
    1870 13506630588 :     case tcc_type:
    1871 13506630588 :       if (tree ats = alias_template_specialization_p (arg, nt_transparent))
    1872             :         {
    1873             :           // We want an alias specialization that survived strip_typedefs
    1874             :           // to hash differently from its TYPE_CANONICAL, to avoid hash
    1875             :           // collisions that compare as different in template_args_equal.
    1876             :           // These could be dependent specializations that strip_typedefs
    1877             :           // left alone, or untouched specializations because
    1878             :           // coerce_template_parms returns the unconverted template
    1879             :           // arguments if it sees incomplete argument packs.
    1880    22440923 :           tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
    1881    22440923 :           return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
    1882             :         }
    1883             : 
    1884 13484189665 :       switch (code)
    1885             :         {
    1886    53959161 :         case  DECLTYPE_TYPE:
    1887    53959161 :           val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
    1888    53959161 :           break;
    1889             : 
    1890   504961490 :         case TYPENAME_TYPE:
    1891   504961490 :           if (comparing_specializations)
    1892             :             {
    1893             :               /* Hash the components that are relevant to TYPENAME_TYPE
    1894             :                  equivalence as determined by structural_comptypes.  We
    1895             :                  can only coherently do this when comparing_specializations
    1896             :                  is set, because otherwise structural_comptypes tries
    1897             :                  resolving TYPENAME_TYPE via the current instantiation.  */
    1898   503317997 :               tree context = TYPE_MAIN_VARIANT (TYPE_CONTEXT (arg));
    1899   503317997 :               tree fullname = TYPENAME_TYPE_FULLNAME (arg);
    1900   503317997 :               val = iterative_hash_template_arg (context, val);
    1901   503317997 :               val = iterative_hash_template_arg (fullname, val);
    1902             :             }
    1903             :           break;
    1904             : 
    1905 12925269014 :         default:
    1906 12925269014 :           if (tree canonical = TYPE_CANONICAL (arg))
    1907 12899243475 :             val = iterative_hash_object (TYPE_HASH (canonical), val);
    1908             :           break;
    1909             :         }
    1910             : 
    1911             :       return val;
    1912             : 
    1913   632018054 :     case tcc_declaration:
    1914   632018054 :     case tcc_constant:
    1915   632018054 :       return iterative_hash_expr (arg, val);
    1916             : 
    1917   600078597 :     default:
    1918   600078597 :       gcc_assert (IS_EXPR_CODE_CLASS (tclass));
    1919  1688170571 :       for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
    1920  1088091974 :         val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
    1921             :       return val;
    1922             :     }
    1923             : }
    1924             : 
    1925             : /* Unregister the specialization SPEC as a specialization of TMPL.
    1926             :    Replace it with NEW_SPEC, if NEW_SPEC is non-NULL.  Returns true
    1927             :    if the SPEC was listed as a specialization of TMPL.
    1928             : 
    1929             :    Note that SPEC has been ggc_freed, so we can't look inside it.  */
    1930             : 
    1931             : bool
    1932      416481 : reregister_specialization (tree spec, tree tinfo, tree new_spec)
    1933             : {
    1934      416481 :   spec_entry *entry;
    1935      416481 :   spec_entry elt;
    1936             : 
    1937      416481 :   elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
    1938      416481 :   elt.args = TI_ARGS (tinfo);
    1939      416481 :   elt.spec = NULL_TREE;
    1940             : 
    1941      416481 :   entry = decl_specializations->find (&elt);
    1942      416481 :   if (entry != NULL)
    1943             :     {
    1944      416481 :       gcc_assert (entry->spec == spec || entry->spec == new_spec);
    1945      416481 :       gcc_assert (new_spec != NULL_TREE);
    1946      416481 :       entry->spec = new_spec;
    1947      416481 :       return 1;
    1948             :     }
    1949             : 
    1950             :   return 0;
    1951             : }
    1952             : 
    1953             : /* Like register_specialization, but for local declarations.  We are
    1954             :    registering SPEC, an instantiation of TMPL.  */
    1955             : 
    1956             : void
    1957    32287568 : register_local_specialization (tree spec, tree tmpl)
    1958             : {
    1959    32287568 :   gcc_assert (tmpl != spec);
    1960    32287568 :   local_specializations->put (tmpl, spec);
    1961    32287568 : }
    1962             : 
    1963             : /* Registers T as a specialization of itself.  This is used to preserve
    1964             :    the references to already-parsed parameters when instantiating
    1965             :    postconditions.  */
    1966             : 
    1967             : void
    1968          51 : register_local_identity (tree t)
    1969             : {
    1970          51 :   local_specializations->put (t, t);
    1971          51 : }
    1972             : 
    1973             : /* TYPE is a class type.  Returns true if TYPE is an explicitly
    1974             :    specialized class.  */
    1975             : 
    1976             : bool
    1977    13018873 : explicit_class_specialization_p (tree type)
    1978             : {
    1979    13018873 :   if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
    1980             :     return false;
    1981      357815 :   return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
    1982             : }
    1983             : 
    1984             : /* Print the list of functions at FNS, going through all the overloads
    1985             :    for each element of the list.  Alternatively, FNS cannot be a
    1986             :    TREE_LIST, in which case it will be printed together with all the
    1987             :    overloads.
    1988             : 
    1989             :    MORE and *STR should respectively be FALSE and NULL when the function
    1990             :    is called from the outside.  They are used internally on recursive
    1991             :    calls.  print_candidates manages the two parameters and leaves NULL
    1992             :    in *STR when it ends.  */
    1993             : 
    1994             : static void
    1995         849 : print_candidates_1 (tree fns, char **str, bool more = false)
    1996             : {
    1997         849 :   if (TREE_CODE (fns) == TREE_LIST)
    1998         443 :     for (; fns; fns = TREE_CHAIN (fns))
    1999         451 :       print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
    2000             :   else
    2001        2458 :     for (lkp_iterator iter (fns); iter;)
    2002             :       {
    2003        1050 :         tree cand = *iter;
    2004        1050 :         ++iter;
    2005             : 
    2006        1050 :         const char *pfx = *str;
    2007        1050 :         if (!pfx)
    2008             :           {
    2009         551 :             if (more || iter)
    2010         288 :               pfx = _("candidates are:");
    2011             :             else
    2012         263 :               pfx = _("candidate is:");
    2013         551 :             *str = get_spaces (pfx);
    2014             :           }
    2015        1050 :         inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
    2016             :       }
    2017         849 : }
    2018             : 
    2019             : /* Print the list of candidate FNS in an error message.  FNS can also
    2020             :    be a TREE_LIST of non-functions in the case of an ambiguous lookup.  */
    2021             : 
    2022             : void
    2023         551 : print_candidates (tree fns)
    2024             : {
    2025         551 :   char *str = NULL;
    2026         551 :   print_candidates_1 (fns, &str);
    2027         551 :   free (str);
    2028         551 : }
    2029             : 
    2030             : /* Get a (possibly) constrained template declaration for the
    2031             :    purpose of ordering candidates.  */
    2032             : static tree
    2033          16 : get_template_for_ordering (tree list)
    2034             : {
    2035          16 :   gcc_assert (TREE_CODE (list) == TREE_LIST);
    2036          16 :   tree f = TREE_VALUE (list);
    2037          16 :   if (tree ti = DECL_TEMPLATE_INFO (f))
    2038          16 :     return TI_TEMPLATE (ti);
    2039             :   return f;
    2040             : }
    2041             : 
    2042             : /* Among candidates having the same signature, return the
    2043             :    most constrained or NULL_TREE if there is no best candidate.
    2044             :    If the signatures of candidates vary (e.g., template
    2045             :    specialization vs. member function), then there can be no
    2046             :    most constrained.
    2047             : 
    2048             :    Note that we don't compare constraints on the functions
    2049             :    themselves, but rather those of their templates. */
    2050             : static tree
    2051           4 : most_constrained_function (tree candidates)
    2052             : {
    2053             :   // Try to find the best candidate in a first pass.
    2054           4 :   tree champ = candidates;
    2055           8 :   for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
    2056             :     {
    2057           4 :       int winner = more_constrained (get_template_for_ordering (champ),
    2058             :                                      get_template_for_ordering (c));
    2059           4 :       if (winner == -1)
    2060             :         champ = c; // The candidate is more constrained
    2061           0 :       else if (winner == 0)
    2062             :         return NULL_TREE; // Neither is more constrained
    2063             :     }
    2064             : 
    2065             :   // Verify that the champ is better than previous candidates.
    2066           8 :   for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
    2067           4 :     if (!more_constrained (get_template_for_ordering (champ),
    2068             :                            get_template_for_ordering (c)))
    2069             :       return NULL_TREE;
    2070             :   }
    2071             : 
    2072             :   return champ;
    2073             : }
    2074             : 
    2075             : 
    2076             : /* Returns the template (one of the functions given by TEMPLATE_ID)
    2077             :    which can be specialized to match the indicated DECL with the
    2078             :    explicit template args given in TEMPLATE_ID.  The DECL may be
    2079             :    NULL_TREE if none is available.  In that case, the functions in
    2080             :    TEMPLATE_ID are non-members.
    2081             : 
    2082             :    If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
    2083             :    specialization of a member template.
    2084             : 
    2085             :    The TEMPLATE_COUNT is the number of references to qualifying
    2086             :    template classes that appeared in the name of the function. See
    2087             :    check_explicit_specialization for a more accurate description.
    2088             : 
    2089             :    TSK indicates what kind of template declaration (if any) is being
    2090             :    declared.  TSK_TEMPLATE indicates that the declaration given by
    2091             :    DECL, though a FUNCTION_DECL, has template parameters, and is
    2092             :    therefore a template function.
    2093             : 
    2094             :    The template args (those explicitly specified and those deduced)
    2095             :    are output in a newly created vector *TARGS_OUT.
    2096             : 
    2097             :    If it is impossible to determine the result, an error message is
    2098             :    issued.  The error_mark_node is returned to indicate failure.  */
    2099             : 
    2100             : static tree
    2101     2355795 : determine_specialization (tree template_id,
    2102             :                           tree decl,
    2103             :                           tree* targs_out,
    2104             :                           int need_member_template,
    2105             :                           int template_count,
    2106             :                           tmpl_spec_kind tsk)
    2107             : {
    2108     2355795 :   tree fns;
    2109     2355795 :   tree targs;
    2110     2355795 :   tree explicit_targs;
    2111     2355795 :   tree candidates = NULL_TREE;
    2112             : 
    2113             :   /* A TREE_LIST of templates of which DECL may be a specialization.
    2114             :      The TREE_VALUE of each node is a TEMPLATE_DECL.  The
    2115             :      corresponding TREE_PURPOSE is the set of template arguments that,
    2116             :      when used to instantiate the template, would produce a function
    2117             :      with the signature of DECL.  */
    2118     2355795 :   tree templates = NULL_TREE;
    2119     2355795 :   int header_count;
    2120     2355795 :   cp_binding_level *b;
    2121             : 
    2122     2355795 :   *targs_out = NULL_TREE;
    2123             : 
    2124     2355795 :   if (template_id == error_mark_node || decl == error_mark_node)
    2125             :     return error_mark_node;
    2126             : 
    2127             :   /* We shouldn't be specializing a member template of an
    2128             :      unspecialized class template; we already gave an error in
    2129             :      check_specialization_scope, now avoid crashing.  */
    2130     2355791 :   if (!VAR_P (decl)
    2131     1857128 :       && template_count && DECL_CLASS_SCOPE_P (decl)
    2132     2994464 :       && template_class_depth (DECL_CONTEXT (decl)) > 0)
    2133             :     {
    2134           4 :       gcc_assert (errorcount);
    2135           4 :       return error_mark_node;
    2136             :     }
    2137             : 
    2138     2355787 :   fns = TREE_OPERAND (template_id, 0);
    2139     2355787 :   explicit_targs = TREE_OPERAND (template_id, 1);
    2140             : 
    2141     2355787 :   if (fns == error_mark_node)
    2142             :     return error_mark_node;
    2143             : 
    2144             :   /* Check for baselinks.  */
    2145     2355787 :   if (BASELINK_P (fns))
    2146           0 :     fns = BASELINK_FUNCTIONS (fns);
    2147             : 
    2148     2355787 :   if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
    2149             :     {
    2150           8 :       error_at (DECL_SOURCE_LOCATION (decl),
    2151             :                 "%qD is not a function template", fns);
    2152           8 :       return error_mark_node;
    2153             :     }
    2154     2355779 :   else if (VAR_P (decl) && !variable_template_p (fns))
    2155             :     {
    2156           4 :       error ("%qD is not a variable template", fns);
    2157           4 :       return error_mark_node;
    2158             :     }
    2159             : 
    2160             :   /* Count the number of template headers specified for this
    2161             :      specialization.  */
    2162     2355775 :   header_count = 0;
    2163     2355775 :   for (b = current_binding_level;
    2164     3264183 :        b->kind == sk_template_parms;
    2165      908408 :        b = b->level_chain)
    2166      908408 :     ++header_count;
    2167             : 
    2168     2355775 :   tree orig_fns = fns;
    2169     2355775 :   bool header_mismatch = false;
    2170             : 
    2171     2355775 :   if (variable_template_p (fns))
    2172             :     {
    2173      498659 :       tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
    2174      498659 :       targs = coerce_template_parms (parms, explicit_targs, fns,
    2175             :                                      tf_warning_or_error);
    2176      498659 :       if (targs != error_mark_node
    2177      498659 :           && constraints_satisfied_p (fns, targs))
    2178      498656 :         templates = tree_cons (targs, fns, templates);
    2179             :     }
    2180    12118289 :   else for (lkp_iterator iter (fns); iter; ++iter)
    2181             :     {
    2182     8404057 :       tree fn = *iter;
    2183             : 
    2184     8404057 :       if (TREE_CODE (fn) == TEMPLATE_DECL)
    2185             :         {
    2186     7974273 :           tree decl_arg_types;
    2187     7974273 :           tree fn_arg_types;
    2188             : 
    2189             :           /* In case of explicit specialization, we need to check if
    2190             :              the number of template headers appearing in the specialization
    2191             :              is correct. This is usually done in check_explicit_specialization,
    2192             :              but the check done there cannot be exhaustive when specializing
    2193             :              member functions. Consider the following code:
    2194             : 
    2195             :              template <> void A<int>::f(int);
    2196             :              template <> template <> void A<int>::f(int);
    2197             : 
    2198             :              Assuming that A<int> is not itself an explicit specialization
    2199             :              already, the first line specializes "f" which is a non-template
    2200             :              member function, whilst the second line specializes "f" which
    2201             :              is a template member function. So both lines are syntactically
    2202             :              correct, and check_explicit_specialization does not reject
    2203             :              them.
    2204             : 
    2205             :              Here, we can do better, as we are matching the specialization
    2206             :              against the declarations. We count the number of template
    2207             :              headers, and we check if they match TEMPLATE_COUNT + 1
    2208             :              (TEMPLATE_COUNT is the number of qualifying template classes,
    2209             :              plus there must be another header for the member template
    2210             :              itself).
    2211             : 
    2212             :              Notice that if header_count is zero, this is not a
    2213             :              specialization but rather a template instantiation, so there
    2214             :              is no check we can perform here.  */
    2215     7974273 :           if (header_count && header_count != template_count + 1)
    2216             :             {
    2217           8 :               header_mismatch = true;
    2218           8 :               continue;
    2219             :             }
    2220             : 
    2221             :           /* Check that the number of template arguments at the
    2222             :              innermost level for DECL is the same as for FN.  */
    2223     7974265 :           if (current_binding_level->kind == sk_template_parms
    2224     7974265 :               && !current_binding_level->explicit_spec_p
    2225     7974265 :               && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
    2226         725 :                   != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
    2227             :                                       (current_template_parms))))
    2228           4 :             continue;
    2229             : 
    2230             :           /* DECL might be a specialization of FN.  */
    2231     7974261 :           decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
    2232     7974261 :           fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
    2233             : 
    2234             :           /* For a non-static member function, we need to make sure
    2235             :              that the const qualification is the same.  Since
    2236             :              get_bindings does not try to merge the "this" parameter,
    2237             :              we must do the comparison explicitly.  */
    2238     7974261 :           if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
    2239             :             {
    2240      295941 :               if (!same_type_p (TREE_VALUE (fn_arg_types),
    2241             :                                 TREE_VALUE (decl_arg_types)))
    2242          56 :                 continue;
    2243             : 
    2244             :               /* And the ref-qualification.  */
    2245      591770 :               if (type_memfn_rqual (TREE_TYPE (decl))
    2246      295885 :                   != type_memfn_rqual (TREE_TYPE (fn)))
    2247          38 :                 continue;
    2248             :             }
    2249             : 
    2250             :           /* Skip the "this" parameter and, for constructors of
    2251             :              classes with virtual bases, the VTT parameter.  A
    2252             :              full specialization of a constructor will have a VTT
    2253             :              parameter, but a template never will.  */
    2254     7974167 :           decl_arg_types
    2255     7974167 :             = skip_artificial_parms_for (decl, decl_arg_types);
    2256     7974167 :           fn_arg_types
    2257     7974167 :             = skip_artificial_parms_for (fn, fn_arg_types);
    2258             : 
    2259             :           /* Function templates cannot be specializations; there are
    2260             :              no partial specializations of functions.  Therefore, if
    2261             :              the type of DECL does not match FN, there is no
    2262             :              match.
    2263             : 
    2264             :              Note that it should never be the case that we have both
    2265             :              candidates added here, and for regular member functions
    2266             :              below. */
    2267     7974167 :           if (tsk == tsk_template)
    2268             :             {
    2269         721 :               if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
    2270         721 :                                         current_template_parms))
    2271           4 :                 continue;
    2272         717 :               if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
    2273             :                                 TREE_TYPE (TREE_TYPE (fn))))
    2274           0 :                 continue;
    2275         717 :               if (!compparms (fn_arg_types, decl_arg_types))
    2276           4 :                 continue;
    2277             : 
    2278         713 :               tree freq = get_constraints (fn);
    2279         713 :               tree dreq = get_constraints (decl);
    2280         713 :               if (!freq != !dreq)
    2281           0 :                 continue;
    2282         713 :               if (freq)
    2283             :                 {
    2284             :                   /* C++20 CA104: Substitute directly into the
    2285             :                      constraint-expression.  */
    2286          14 :                   tree fargs = DECL_TI_ARGS (fn);
    2287          14 :                   tsubst_flags_t complain = tf_none;
    2288          14 :                   freq = tsubst_constraint_info (freq, fargs, complain, fn);
    2289          14 :                   if (!cp_tree_equal (freq, dreq))
    2290          10 :                     continue;
    2291             :                 }
    2292             : 
    2293         703 :               candidates = tree_cons (NULL_TREE, fn, candidates);
    2294         703 :               continue;
    2295         703 :             }
    2296             : 
    2297             :           /* See whether this function might be a specialization of this
    2298             :              template.  Suppress access control because we might be trying
    2299             :              to make this specialization a friend, and we have already done
    2300             :              access control for the declaration of the specialization.  */
    2301     7973446 :           push_deferring_access_checks (dk_no_check);
    2302     7973446 :           targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
    2303     7973446 :           pop_deferring_access_checks ();
    2304             : 
    2305     7973446 :           if (!targs)
    2306             :             /* We cannot deduce template arguments that when used to
    2307             :                specialize TMPL will produce DECL.  */
    2308     6428507 :             continue;
    2309             : 
    2310     1544939 :           if (uses_template_parms (targs))
    2311             :             /* We deduced something involving 'auto', which isn't a valid
    2312             :                template argument.  */
    2313           3 :             continue;
    2314             : 
    2315             :           /* Save this template, and the arguments deduced.  */
    2316     1544936 :           templates = tree_cons (targs, fn, templates);
    2317             :         }
    2318      429784 :       else if (need_member_template)
    2319             :         /* FN is an ordinary member function, and we need a
    2320             :            specialization of a member template.  */
    2321             :         ;
    2322      429724 :       else if (TREE_CODE (fn) != FUNCTION_DECL)
    2323             :         /* We can get IDENTIFIER_NODEs here in certain erroneous
    2324             :            cases.  */
    2325             :         ;
    2326      429724 :       else if (!DECL_FUNCTION_MEMBER_P (fn))
    2327             :         /* This is just an ordinary non-member function.  Nothing can
    2328             :            be a specialization of that.  */
    2329             :         ;
    2330      421744 :       else if (DECL_ARTIFICIAL (fn))
    2331             :         /* Cannot specialize functions that are created implicitly.  */
    2332             :         ;
    2333             :       else
    2334             :         {
    2335      421571 :           tree decl_arg_types;
    2336             : 
    2337             :           /* This is an ordinary member function.  However, since
    2338             :              we're here, we can assume its enclosing class is a
    2339             :              template class.  For example,
    2340             : 
    2341             :                template <typename T> struct S { void f(); };
    2342             :                template <> void S<int>::f() {}
    2343             : 
    2344             :              Here, S<int>::f is a non-template, but S<int> is a
    2345             :              template class.  If FN has the same type as DECL, we
    2346             :              might be in business.  */
    2347             : 
    2348      421571 :           if (!DECL_TEMPLATE_INFO (fn))
    2349             :             /* Its enclosing class is an explicit specialization
    2350             :                of a template class.  This is not a candidate.  */
    2351           8 :             continue;
    2352             : 
    2353      421563 :           if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
    2354             :                             TREE_TYPE (TREE_TYPE (fn))))
    2355             :             /* The return types differ.  */
    2356         266 :             continue;
    2357             : 
    2358             :           /* Adjust the type of DECL in case FN is a static member.  */
    2359      421297 :           decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
    2360      421297 :           if (DECL_STATIC_FUNCTION_P (fn)
    2361      421297 :               && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
    2362         187 :             decl_arg_types = TREE_CHAIN (decl_arg_types);
    2363             : 
    2364      421297 :           if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
    2365             :                          decl_arg_types))
    2366       78655 :             continue;
    2367             : 
    2368      342642 :           if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
    2369      685105 :               && (type_memfn_rqual (TREE_TYPE (decl))
    2370      342463 :                   != type_memfn_rqual (TREE_TYPE (fn))))
    2371           6 :             continue;
    2372             : 
    2373             :           // If the deduced arguments do not satisfy the constraints,
    2374             :           // this is not a candidate.
    2375      342636 :           if (flag_concepts && !constraints_satisfied_p (fn))
    2376           7 :             continue;
    2377             : 
    2378             :           // Add the candidate.
    2379      342629 :           candidates = tree_cons (NULL_TREE, fn, candidates);
    2380             :         }
    2381             :     }
    2382             : 
    2383     2355775 :   if (templates && TREE_CHAIN (templates))
    2384             :     {
    2385             :       /* We have:
    2386             : 
    2387             :            [temp.expl.spec]
    2388             : 
    2389             :            It is possible for a specialization with a given function
    2390             :            signature to be instantiated from more than one function
    2391             :            template.  In such cases, explicit specification of the
    2392             :            template arguments must be used to uniquely identify the
    2393             :            function template specialization being specialized.
    2394             : 
    2395             :          Note that here, there's no suggestion that we're supposed to
    2396             :          determine which of the candidate templates is most
    2397             :          specialized.  However, we, also have:
    2398             : 
    2399             :            [temp.func.order]
    2400             : 
    2401             :            Partial ordering of overloaded function template
    2402             :            declarations is used in the following contexts to select
    2403             :            the function template to which a function template
    2404             :            specialization refers:
    2405             : 
    2406             :            -- when an explicit specialization refers to a function
    2407             :               template.
    2408             : 
    2409             :          So, we do use the partial ordering rules, at least for now.
    2410             :          This extension can only serve to make invalid programs valid,
    2411             :          so it's safe.  And, there is strong anecdotal evidence that
    2412             :          the committee intended the partial ordering rules to apply;
    2413             :          the EDG front end has that behavior, and John Spicer claims
    2414             :          that the committee simply forgot to delete the wording in
    2415             :          [temp.expl.spec].  */
    2416       15721 :       tree tmpl = most_specialized_instantiation (templates);
    2417       15721 :       if (tmpl != error_mark_node)
    2418             :         {
    2419       15717 :           templates = tmpl;
    2420       15717 :           TREE_CHAIN (templates) = NULL_TREE;
    2421             :         }
    2422             :     }
    2423             : 
    2424             :   // Concepts allows multiple declarations of member functions
    2425             :   // with the same signature. Like above, we need to rely on
    2426             :   // on the partial ordering of those candidates to determine which
    2427             :   // is the best.
    2428     2355775 :   if (flag_concepts && candidates && TREE_CHAIN (candidates))
    2429             :     {
    2430           4 :       if (tree cand = most_constrained_function (candidates))
    2431             :         {
    2432           4 :           candidates = cand;
    2433           4 :           TREE_CHAIN (cand) = NULL_TREE;
    2434             :         }
    2435             :     }
    2436             : 
    2437     2355775 :   if (templates == NULL_TREE && candidates == NULL_TREE)
    2438             :     {
    2439         243 :       error ("template-id %qD for %q+D does not match any template "
    2440             :              "declaration", template_id, decl);
    2441         243 :       if (header_mismatch)
    2442           4 :         inform (DECL_SOURCE_LOCATION (decl),
    2443             :                 "saw %d %<template<>%>, need %d for "
    2444             :                 "specializing a member function template",
    2445             :                 header_count, template_count + 1);
    2446         243 :       print_candidates (orig_fns);
    2447         243 :       return error_mark_node;
    2448             :     }
    2449     2012204 :   else if ((templates && TREE_CHAIN (templates))
    2450     2355528 :            || (candidates && TREE_CHAIN (candidates))
    2451     4711060 :            || (templates && candidates))
    2452             :     {
    2453           4 :       error ("ambiguous template specialization %qD for %q+D",
    2454             :              template_id, decl);
    2455           4 :       candidates = chainon (candidates, templates);
    2456           4 :       print_candidates (candidates);
    2457           4 :       return error_mark_node;
    2458             :     }
    2459             : 
    2460             :   /* We have one, and exactly one, match.  */
    2461     2355528 :   if (candidates)
    2462             :     {
    2463      343328 :       tree fn = TREE_VALUE (candidates);
    2464      343328 :       *targs_out = copy_node (DECL_TI_ARGS (fn));
    2465             : 
    2466             :       /* Propagate the candidate's constraints to the declaration.  */
    2467      343328 :       if (tsk != tsk_template)
    2468      342625 :         set_constraints (decl, get_constraints (fn));
    2469             : 
    2470             :       /* DECL is a re-declaration or partial instantiation of a template
    2471             :          function.  */
    2472      343328 :       if (TREE_CODE (fn) == TEMPLATE_DECL)
    2473             :         return fn;
    2474             :       /* It was a specialization of an ordinary member function in a
    2475             :          template class.  */
    2476      342625 :       return DECL_TI_TEMPLATE (fn);
    2477             :     }
    2478             : 
    2479             :   /* It was a specialization of a template.  */
    2480     2012200 :   tree tmpl = TREE_VALUE (templates);
    2481     2012200 :   *targs_out = add_outermost_template_args (tmpl, TREE_PURPOSE (templates));
    2482             : 
    2483             :   /* Propagate the template's constraints to the declaration.  */
    2484     2012200 :   if (tsk != tsk_template)
    2485     1646569 :     set_constraints (decl, get_constraints (tmpl));
    2486             : 
    2487             :   return tmpl;
    2488             : }
    2489             : 
    2490             : /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
    2491             :    but with the default argument values filled in from those in the
    2492             :    TMPL_TYPES.  */
    2493             : 
    2494             : static tree
    2495      147010 : copy_default_args_to_explicit_spec_1 (tree spec_types,
    2496             :                                       tree tmpl_types)
    2497             : {
    2498      147010 :   tree new_spec_types;
    2499             : 
    2500      147010 :   if (!spec_types)
    2501             :     return NULL_TREE;
    2502             : 
    2503      147010 :   if (spec_types == void_list_node)
    2504             :     return void_list_node;
    2505             : 
    2506             :   /* Substitute into the rest of the list.  */
    2507       87883 :   new_spec_types =
    2508       87883 :     copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
    2509       87883 :                                           TREE_CHAIN (tmpl_types));
    2510             : 
    2511             :   /* Add the default argument for this parameter.  */
    2512      175766 :   return hash_tree_cons (TREE_PURPOSE (tmpl_types),
    2513       87883 :                          TREE_VALUE (spec_types),
    2514       87883 :                          new_spec_types);
    2515             : }
    2516             : 
    2517             : /* DECL is an explicit specialization.  Replicate default arguments
    2518             :    from the template it specializes.  (That way, code like:
    2519             : 
    2520             :      template <class T> void f(T = 3);
    2521             :      template <> void f(double);
    2522             :      void g () { f (); }
    2523             : 
    2524             :    works, as required.)  An alternative approach would be to look up
    2525             :    the correct default arguments at the call-site, but this approach
    2526             :    is consistent with how implicit instantiations are handled.  */
    2527             : 
    2528             : static void
    2529      408186 : copy_default_args_to_explicit_spec (tree decl)
    2530             : {
    2531      408186 :   tree tmpl;
    2532      408186 :   tree spec_types;
    2533      408186 :   tree tmpl_types;
    2534      408186 :   tree new_spec_types;
    2535      408186 :   tree old_type;
    2536      408186 :   tree new_type;
    2537      408186 :   tree t;
    2538      408186 :   tree object_type = NULL_TREE;
    2539      408186 :   tree in_charge = NULL_TREE;
    2540      408186 :   tree vtt = NULL_TREE;
    2541             : 
    2542             :   /* See if there's anything we need to do.  */
    2543      408186 :   tmpl = DECL_TI_TEMPLATE (decl);
    2544      408186 :   tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
    2545     1653419 :   for (t = tmpl_types; t; t = TREE_CHAIN (t))
    2546     1304360 :     if (TREE_PURPOSE (t))
    2547             :       break;
    2548      408186 :   if (!t)
    2549             :     return;
    2550             : 
    2551       59127 :   old_type = TREE_TYPE (decl);
    2552       59127 :   spec_types = TYPE_ARG_TYPES (old_type);
    2553             : 
    2554       59127 :   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
    2555             :     {
    2556             :       /* Remove the this pointer, but remember the object's type for
    2557             :          CV quals.  */
    2558       59127 :       object_type = TREE_TYPE (TREE_VALUE (spec_types));
    2559       59127 :       spec_types = TREE_CHAIN (spec_types);
    2560       59127 :       tmpl_types = TREE_CHAIN (tmpl_types);
    2561             : 
    2562       59127 :       if (DECL_HAS_IN_CHARGE_PARM_P (decl))
    2563             :         {
    2564             :           /* DECL may contain more parameters than TMPL due to the extra
    2565             :              in-charge parameter in constructors and destructors.  */
    2566           0 :           in_charge = spec_types;
    2567           0 :           spec_types = TREE_CHAIN (spec_types);
    2568             :         }
    2569       59127 :       if (DECL_HAS_VTT_PARM_P (decl))
    2570             :         {
    2571           0 :           vtt = spec_types;
    2572           0 :           spec_types = TREE_CHAIN (spec_types);
    2573             :         }
    2574             :     }
    2575             : 
    2576             :   /* Compute the merged default arguments.  */
    2577       59127 :   new_spec_types =
    2578       59127 :     copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
    2579             : 
    2580             :   /* Compute the new FUNCTION_TYPE.  */
    2581       59127 :   if (object_type)
    2582             :     {
    2583       59127 :       if (vtt)
    2584           0 :         new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
    2585           0 :                                          TREE_VALUE (vtt),
    2586             :                                          new_spec_types);
    2587             : 
    2588       59127 :       if (in_charge)
    2589             :         /* Put the in-charge parameter back.  */
    2590           0 :         new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
    2591           0 :                                          TREE_VALUE (in_charge),
    2592             :                                          new_spec_types);
    2593             : 
    2594       59127 :       new_type = build_method_type_directly (object_type,
    2595       59127 :                                              TREE_TYPE (old_type),
    2596             :                                              new_spec_types);
    2597             :     }
    2598             :   else
    2599           0 :     new_type = build_function_type (TREE_TYPE (old_type),
    2600             :                                     new_spec_types);
    2601       59127 :   new_type = cp_build_type_attribute_variant (new_type,
    2602       59127 :                                               TYPE_ATTRIBUTES (old_type));
    2603       59127 :   new_type = cxx_copy_lang_qualifiers (new_type, old_type);
    2604             : 
    2605       59127 :   TREE_TYPE (decl) = new_type;
    2606             : }
    2607             : 
    2608             : /* Return the number of template headers we expect to see for a definition
    2609             :    or specialization of CTYPE or one of its non-template members.  */
    2610             : 
    2611             : int
    2612    20689659 : num_template_headers_for_class (tree ctype)
    2613             : {
    2614    20689659 :   int num_templates = 0;
    2615             : 
    2616    33584593 :   while (ctype && CLASS_TYPE_P (ctype))
    2617             :     {
    2618             :       /* You're supposed to have one `template <...>' for every
    2619             :          template class, but you don't need one for a full
    2620             :          specialization.  For example:
    2621             : 
    2622             :          template <class T> struct S{};
    2623             :          template <> struct S<int> { void f(); };
    2624             :          void S<int>::f () {}
    2625             : 
    2626             :          is correct; there shouldn't be a `template <>' for the
    2627             :          definition of `S<int>::f'.  */
    2628    17341965 :       if (!CLASSTYPE_TEMPLATE_INFO (ctype))
    2629             :         /* If CTYPE does not have template information of any
    2630             :            kind,  then it is not a template, nor is it nested
    2631             :            within a template.  */
    2632             :         break;
    2633    13018873 :       if (explicit_class_specialization_p (ctype))
    2634             :         break;
    2635    12894934 :       if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
    2636    12666462 :         ++num_templates;
    2637             : 
    2638    12894934 :       ctype = TYPE_CONTEXT (ctype);
    2639             :     }
    2640             : 
    2641    20689659 :   return num_templates;
    2642             : }
    2643             : 
    2644             : /* Do a simple sanity check on the template headers that precede the
    2645             :    variable declaration DECL.  */
    2646             : 
    2647             : void
    2648     1951984 : check_template_variable (tree decl)
    2649             : {
    2650     1951984 :   tree ctx = CP_DECL_CONTEXT (decl);
    2651     1951984 :   int wanted = num_template_headers_for_class (ctx);
    2652     3903964 :   if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
    2653     3903952 :       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
    2654             :     {
    2655     1702409 :       if (cxx_dialect < cxx14)
    2656           9 :         pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__14_extensions,
    2657             :                  "variable templates only available with "
    2658             :                  "%<-std=c++14%> or %<-std=gnu++14%>");
    2659             : 
    2660             :       // Namespace-scope variable templates should have a template header.
    2661     1702409 :       ++wanted;
    2662             :     }
    2663     1951984 :   if (template_header_count > wanted)
    2664             :     {
    2665          16 :       auto_diagnostic_group d;
    2666          16 :       bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
    2667             :                              "too many template headers for %qD "
    2668             :                              "(should be %d)",
    2669             :                              decl, wanted);
    2670          16 :       if (warned && CLASS_TYPE_P (ctx)
    2671          28 :           && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
    2672           4 :         inform (DECL_SOURCE_LOCATION (decl),
    2673             :                 "members of an explicitly specialized class are defined "
    2674             :                 "without a template header");
    2675          16 :     }
    2676     1951984 : }
    2677             : 
    2678             : /* An explicit specialization whose declarator-id or class-head-name is not
    2679             :    qualified shall be declared in the nearest enclosing namespace of the
    2680             :    template, or, if the namespace is inline (7.3.1), any namespace from its
    2681             :    enclosing namespace set.
    2682             : 
    2683             :    If the name declared in the explicit instantiation is an unqualified name,
    2684             :    the explicit instantiation shall appear in the namespace where its template
    2685             :    is declared or, if that namespace is inline (7.3.1), any namespace from its
    2686             :    enclosing namespace set.  */
    2687             : 
    2688             : void
    2689    10121658 : check_unqualified_spec_or_inst (tree t, location_t loc)
    2690             : {
    2691    10121658 :   tree tmpl = most_general_template (t);
    2692    20243316 :   if (DECL_NAMESPACE_SCOPE_P (tmpl)
    2693    19891107 :       && !is_nested_namespace (current_namespace,
    2694     9769449 :                                CP_DECL_CONTEXT (tmpl), true))
    2695             :     {
    2696          21 :       if (processing_specialization)
    2697           8 :         permerror (loc, "explicit specialization of %qD outside its "
    2698             :                    "namespace must use a nested-name-specifier", tmpl);
    2699          13 :       else if (processing_explicit_instantiation
    2700          13 :                && cxx_dialect >= cxx11)
    2701             :         /* This was allowed in C++98, so only pedwarn.  */
    2702          11 :         pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
    2703             :                  "outside its namespace must use a nested-name-"
    2704             :                  "specifier", tmpl);
    2705             :     }
    2706    10121658 : }
    2707             : 
    2708             : /* Warn for a template specialization SPEC that is missing some of a set
    2709             :    of function or type attributes that the template TEMPL is declared with.
    2710             :    ATTRLIST is a list of additional attributes that SPEC should be taken
    2711             :    to ultimately be declared with.  */
    2712             : 
    2713             : static void
    2714      541206 : warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
    2715             : {
    2716      541206 :   if (DECL_FUNCTION_TEMPLATE_P (tmpl))
    2717             :     tmpl = DECL_TEMPLATE_RESULT (tmpl);
    2718             : 
    2719             :   /* Avoid warning if the difference between the primary and
    2720             :      the specialization is not in one of the attributes below.  */
    2721      541206 :   const char* const blacklist[] = {
    2722             :     "alloc_align", "alloc_size", "assume_aligned", "format",
    2723             :     "format_arg", "malloc", "nonnull", NULL
    2724             :   };
    2725             : 
    2726             :   /* Put together a list of the black listed attributes that the primary
    2727             :      template is declared with that the specialization is not, in case
    2728             :      it's not apparent from the most recent declaration of the primary.  */
    2729      541206 :   pretty_printer str;
    2730      541206 :   unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
    2731             :                                                  blacklist, &str);
    2732             : 
    2733      541206 :   if (!nattrs)
    2734      541170 :     return;
    2735             : 
    2736          36 :   auto_diagnostic_group d;
    2737          36 :   if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
    2738             :                   "explicit specialization %q#D may be missing attributes",
    2739             :                   spec))
    2740          68 :     inform (DECL_SOURCE_LOCATION (tmpl),
    2741             :             nattrs > 1
    2742             :             ? G_("missing primary template attributes %s")
    2743             :             : G_("missing primary template attribute %s"),
    2744             :             pp_formatted_text (&str));
    2745      541206 : }
    2746             : 
    2747             : /* Check to see if the function just declared, as indicated in
    2748             :    DECLARATOR, and in DECL, is a specialization of a function
    2749             :    template.  We may also discover that the declaration is an explicit
    2750             :    instantiation at this point.
    2751             : 
    2752             :    Returns DECL, or an equivalent declaration that should be used
    2753             :    instead if all goes well.  Issues an error message if something is
    2754             :    amiss.  Returns error_mark_node if the error is not easily
    2755             :    recoverable.
    2756             : 
    2757             :    FLAGS is a bitmask consisting of the following flags:
    2758             : 
    2759             :    2: The function has a definition.
    2760             :    4: The function is a friend.
    2761             : 
    2762             :    The TEMPLATE_COUNT is the number of references to qualifying
    2763             :    template classes that appeared in the name of the function.  For
    2764             :    example, in
    2765             : 
    2766             :      template <class T> struct S { void f(); };
    2767             :      void S<int>::f();
    2768             : 
    2769             :    the TEMPLATE_COUNT would be 1.  However, explicitly specialized
    2770             :    classes are not counted in the TEMPLATE_COUNT, so that in
    2771             : 
    2772             :      template <class T> struct S {};
    2773             :      template <> struct S<int> { void f(); }
    2774             :      template <> void S<int>::f();
    2775             : 
    2776             :    the TEMPLATE_COUNT would be 0.  (Note that this declaration is
    2777             :    invalid; there should be no template <>.)
    2778             : 
    2779             :    If the function is a specialization, it is marked as such via
    2780             :    DECL_TEMPLATE_SPECIALIZATION.  Furthermore, its DECL_TEMPLATE_INFO
    2781             :    is set up correctly, and it is added to the list of specializations
    2782             :    for that template.  */
    2783             : 
    2784             : tree
    2785   148297523 : check_explicit_specialization (tree declarator,
    2786             :                                tree decl,
    2787             :                                int template_count,
    2788             :                                int flags,
    2789             :                                tree attrlist)
    2790             : {
    2791   148297523 :   int have_def = flags & 2;
    2792   148297523 :   int is_friend = flags & 4;
    2793   148297523 :   bool is_concept = flags & 8;
    2794   148297523 :   int specialization = 0;
    2795   148297523 :   int explicit_instantiation = 0;
    2796   148297523 :   int member_specialization = 0;
    2797   148297523 :   tree ctype = DECL_CLASS_CONTEXT (decl);
    2798   148297523 :   tree dname = DECL_NAME (decl);
    2799   148297523 :   tmpl_spec_kind tsk;
    2800             : 
    2801   148297523 :   if (is_friend)
    2802             :     {
    2803          83 :       if (!processing_specialization)
    2804             :         tsk = tsk_none;
    2805             :       else
    2806             :         tsk = tsk_excessive_parms;
    2807             :     }
    2808             :   else
    2809   148297440 :     tsk = current_tmpl_spec_kind (template_count);
    2810             : 
    2811   148297440 :   switch (tsk)
    2812             :     {
    2813   114150582 :     case tsk_none:
    2814   114150582 :       if (processing_specialization && !VAR_P (decl))
    2815             :         {
    2816      340850 :           specialization = 1;
    2817      340850 :           SET_DECL_TEMPLATE_SPECIALIZATION (decl);
    2818             :         }
    2819   113809732 :       else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
    2820   113809732 :                || (DECL_LANG_SPECIFIC (decl)
    2821    80184976 :                    && DECL_IMPLICIT_INSTANTIATION (decl)))
    2822             :         {
    2823          99 :           if (is_friend)
    2824             :             /* This could be something like:
    2825             : 
    2826             :                template <class T> void f(T);
    2827             :                class S { friend void f<>(int); }  */
    2828             :             specialization = 1;
    2829             :           else
    2830             :             {
    2831             :               /* This case handles bogus declarations like template <>
    2832             :                  template <class T> void f<int>(); */
    2833             : 
    2834          16 :               error_at (cp_expr_loc_or_input_loc (declarator),
    2835             :                         "template-id %qE in declaration of primary template",
    2836             :                         declarator);
    2837          16 :               return decl;
    2838             :             }
    2839             :         }
    2840             :       break;
    2841             : 
    2842           0 :     case tsk_invalid_member_spec:
    2843             :       /* The error has already been reported in
    2844             :          check_specialization_scope.  */
    2845           0 :       return error_mark_node;
    2846             : 
    2847           0 :     case tsk_invalid_expl_inst:
    2848           0 :       error ("template parameter list used in explicit instantiation");
    2849             : 
    2850             :       /* Fall through.  */
    2851             : 
    2852     1430135 :     case tsk_expl_inst:
    2853     1430135 :       if (have_def)
    2854           0 :         error ("definition provided for explicit instantiation");
    2855             : 
    2856             :       explicit_instantiation = 1;
    2857             :       break;
    2858             : 
    2859           0 :     case tsk_excessive_parms:
    2860           0 :     case tsk_insufficient_parms:
    2861           0 :       if (tsk == tsk_excessive_parms)
    2862           0 :         error ("too many template parameter lists in declaration of %qD",
    2863             :                decl);
    2864           0 :       else if (template_header_count)
    2865           0 :         error("too few template parameter lists in declaration of %qD", decl);
    2866             :       else
    2867           0 :         error("explicit specialization of %qD must be introduced by "
    2868             :               "%<template <>%>", decl);
    2869             : 
    2870             :       /* Fall through.  */
    2871      200389 :     case tsk_expl_spec:
    2872      200389 :       if (is_concept)
    2873           0 :         error ("explicit specialization declared %<concept%>");
    2874             : 
    2875      200389 :       if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
    2876             :         /* In cases like template<> constexpr bool v = true;
    2877             :            We'll give an error in check_template_variable.  */
    2878             :         break;
    2879             : 
    2880      200373 :       SET_DECL_TEMPLATE_SPECIALIZATION (decl);
    2881      200373 :       if (ctype)
    2882             :         member_specialization = 1;
    2883             :       else
    2884      200129 :         specialization = 1;
    2885             :       break;
    2886             : 
    2887    32516417 :     case tsk_template:
    2888    32516417 :       if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
    2889             :         {
    2890             :           /* This case handles bogus declarations like template <>
    2891             :              template <class T> void f<int>(); */
    2892             : 
    2893      365657 :           if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
    2894          10 :             error_at (cp_expr_loc_or_input_loc (declarator),
    2895             :                       "template-id %qE in declaration of primary template",
    2896             :                       declarator);
    2897      365648 :           else if (variable_template_p (TREE_OPERAND (declarator, 0)))
    2898             :             {
    2899             :               /* Partial specialization of variable template.  */
    2900      365631 :               SET_DECL_TEMPLATE_SPECIALIZATION (decl);
    2901      365631 :               specialization = 1;
    2902      365631 :               goto ok;
    2903             :             }
    2904          17 :           else if (cxx_dialect < cxx14)
    2905           5 :             error_at (cp_expr_loc_or_input_loc (declarator),
    2906             :                       "non-type partial specialization %qE "
    2907             :                       "is not allowed", declarator);
    2908             :           else
    2909          17 :             error_at (cp_expr_loc_or_input_loc (declarator),
    2910             :                       "non-class, non-variable partial specialization %qE "
    2911             :                       "is not allowed", declarator);
    2912          26 :           return decl;
    2913      365631 :         ok:;
    2914             :         }
    2915             : 
    2916    32516391 :       if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
    2917             :         /* This is a specialization of a member template, without
    2918             :            specialization the containing class.  Something like:
    2919             : 
    2920             :              template <class T> struct S {
    2921             :                template <class U> void f (U);
    2922             :              };
    2923             :              template <> template <class U> void S<int>::f(U) {}
    2924             : 
    2925             :            That's a specialization -- but of the entire template.  */
    2926             :         specialization = 1;
    2927             :       break;
    2928             : 
    2929           0 :     default:
    2930           0 :       gcc_unreachable ();
    2931             :     }
    2932             : 
    2933   148297481 :   if ((specialization || member_specialization)
    2934             :       /* This doesn't apply to variable templates.  */
    2935   148297481 :       && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
    2936             :     {
    2937      408999 :       tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
    2938     1848490 :       for (; t; t = TREE_CHAIN (t))
    2939     1439495 :         if (TREE_PURPOSE (t))
    2940             :           {
    2941           4 :             permerror (input_location,
    2942             :                        "default argument specified in explicit specialization");
    2943           4 :             break;
    2944             :           }
    2945             :     }
    2946             : 
    2947   148297481 :   if (specialization || member_specialization || explicit_instantiation)
    2948             :     {
    2949     2337794 :       tree tmpl = NULL_TREE;
    2950     2337794 :       tree targs = NULL_TREE;
    2951     2337794 :       bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
    2952     2337794 :       bool found_hidden = false;
    2953             : 
    2954             :       /* Make sure that the declarator is a TEMPLATE_ID_EXPR.  */
    2955     2337794 :       if (!was_template_id)
    2956             :         {
    2957     1236438 :           tree fns;
    2958             : 
    2959     1236438 :           gcc_assert (identifier_p (declarator));
    2960     1236438 :           if (ctype)
    2961             :             fns = dname;
    2962             :           else
    2963             :             {
    2964             :               /* If there is no class context, the explicit instantiation
    2965             :                  must be at namespace scope.  */
    2966      597734 :               gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
    2967             : 
    2968             :               /* Find the namespace binding, using the declaration
    2969             :                  context.  */
    2970      597734 :               fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
    2971             :                                            LOOK_want::NORMAL, true);
    2972      597734 :               if (fns == error_mark_node)
    2973             :                 {
    2974             :                   /* If lookup fails, look for a friend declaration so we can
    2975             :                      give a better diagnostic.  */
    2976          23 :                   fns = (lookup_qualified_name
    2977          23 :                          (CP_DECL_CONTEXT (decl), dname,
    2978             :                           LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
    2979             :                           /*complain*/true));
    2980          23 :                   found_hidden = true;
    2981             :                 }
    2982             : 
    2983      597734 :               if (fns == error_mark_node || !is_overloaded_fn (fns))
    2984             :                 {
    2985          23 :                   error ("%qD is not a template function", dname);
    2986          23 :                   fns = error_mark_node;
    2987             :                 }
    2988             :             }
    2989             : 
    2990     1236438 :           declarator = lookup_template_function (fns, NULL_TREE);
    2991             :         }
    2992             : 
    2993     2337794 :       if (declarator == error_mark_node)
    2994     1430957 :         return error_mark_node;
    2995             : 
    2996     2337771 :       if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
    2997             :         {
    2998           0 :           if (!explicit_instantiation)
    2999             :             /* A specialization in class scope.  This is invalid,
    3000             :                but the error will already have been flagged by
    3001             :                check_specialization_scope.  */
    3002             :             return error_mark_node;
    3003             :           else
    3004             :             {
    3005             :               /* It's not valid to write an explicit instantiation in
    3006             :                  class scope, e.g.:
    3007             : 
    3008             :                    class C { template void f(); }
    3009             : 
    3010             :                    This case is caught by the parser.  However, on
    3011             :                    something like:
    3012             : 
    3013             :                    template class C { void f(); };
    3014             : 
    3015             :                    (which is invalid) we can get here.  The error will be
    3016             :                    issued later.  */
    3017           0 :               ;
    3018             :             }
    3019             : 
    3020           0 :           return decl;
    3021             :         }
    3022     2337771 :       else if (ctype != NULL_TREE
    3023     2337771 :                && (identifier_p (TREE_OPERAND (declarator, 0))))
    3024             :         {
    3025             :           // We'll match variable templates in start_decl.
    3026      638704 :           if (VAR_P (decl))
    3027             :             return decl;
    3028             : 
    3029             :           /* Find the list of functions in ctype that have the same
    3030             :              name as the declared function.  */
    3031      638651 :           tree name = TREE_OPERAND (declarator, 0);
    3032             : 
    3033      638651 :           if (constructor_name_p (name, ctype))
    3034             :             {
    3035           0 :               if (DECL_CONSTRUCTOR_P (decl)
    3036           0 :                   ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
    3037           0 :                   : !CLASSTYPE_DESTRUCTOR (ctype))
    3038             :                 {
    3039             :                   /* From [temp.expl.spec]:
    3040             : 
    3041             :                      If such an explicit specialization for the member
    3042             :                      of a class template names an implicitly-declared
    3043             :                      special member function (clause _special_), the
    3044             :                      program is ill-formed.
    3045             : 
    3046             :                      Similar language is found in [temp.explicit].  */
    3047           0 :                   error ("specialization of implicitly-declared special member function");
    3048           0 :                   return error_mark_node;
    3049             :                 }
    3050             : 
    3051           0 :               name = DECL_NAME (decl);
    3052             :             }
    3053             : 
    3054             :           /* For a type-conversion operator, We might be looking for
    3055             :              `operator int' which will be a specialization of
    3056             :              `operator T'.  Grab all the conversion operators, and
    3057             :              then select from them.  */
    3058      638651 :           tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
    3059             :                                         ? conv_op_identifier : name);
    3060             : 
    3061      638651 :           if (fns == NULL_TREE)
    3062             :             {
    3063           0 :               error ("no member function %qD declared in %qT", name, ctype);
    3064           0 :               return error_mark_node;
    3065             :             }
    3066             :           else
    3067      638651 :             TREE_OPERAND (declarator, 0) = fns;
    3068             :         }
    3069             : 
    3070             :       /* Figure out what exactly is being specialized at this point.
    3071             :          Note that for an explicit instantiation, even one for a
    3072             :          member function, we cannot tell a priori whether the
    3073             :          instantiation is for a member template, or just a member
    3074             :          function of a template class.  Even if a member template is
    3075             :          being instantiated, the member template arguments may be
    3076             :          elided if they can be deduced from the rest of the
    3077             :          declaration.  */
    3078     2337718 :       tmpl = determine_specialization (declarator, decl,
    3079             :                                        &targs,
    3080             :                                        member_specialization,
    3081             :                                        template_count,
    3082             :                                        tsk);
    3083             : 
    3084     2337718 :       if (!tmpl || tmpl == error_mark_node)
    3085             :         /* We couldn't figure out what this declaration was
    3086             :            specializing.  */
    3087         258 :         return error_mark_node;
    3088             :       else
    3089             :         {
    3090     2337460 :           if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL)
    3091             :             {
    3092          12 :               auto_diagnostic_group d;
    3093          12 :               if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
    3094             :                            "friend declaration %qD is not visible to "
    3095             :                            "explicit specialization", tmpl))
    3096          12 :                 inform (DECL_SOURCE_LOCATION (tmpl),
    3097             :                         "friend declaration here");
    3098          12 :             }
    3099             : 
    3100     2337460 :           if (!ctype && !is_friend
    3101     2337460 :               && CP_DECL_CONTEXT (decl) == current_namespace)
    3102     1698460 :             check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
    3103             : 
    3104     2337460 :           tree gen_tmpl = most_general_template (tmpl);
    3105             : 
    3106     2337460 :           if (explicit_instantiation)
    3107             :             {
    3108             :               /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
    3109             :                  is done by do_decl_instantiation later.  */
    3110             : 
    3111     1429920 :               int arg_depth = TMPL_ARGS_DEPTH (targs);
    3112     1429920 :               int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
    3113             : 
    3114     1429920 :               if (arg_depth > parm_depth)
    3115             :                 {
    3116             :                   /* If TMPL is not the most general template (for
    3117             :                      example, if TMPL is a friend template that is
    3118             :                      injected into namespace scope), then there will
    3119             :                      be too many levels of TARGS.  Remove some of them
    3120             :                      here.  */
    3121      295210 :                   int i;
    3122      295210 :                   tree new_targs;
    3123             : 
    3124      295210 :                   new_targs = make_tree_vec (parm_depth);
    3125      590420 :                   for (i = arg_depth - parm_depth; i < arg_depth; ++i)
    3126      590420 :                     TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
    3127      295210 :                       = TREE_VEC_ELT (targs, i);
    3128      295210 :                   targs = new_targs;
    3129             :                 }
    3130             : 
    3131     1429920 :               return instantiate_template (tmpl, targs, tf_error);
    3132             :             }
    3133             : 
    3134             :           /* If we thought that the DECL was a member function, but it
    3135             :              turns out to be specializing a static member function,
    3136             :              make DECL a static member function as well.  */
    3137      907540 :           if (DECL_FUNCTION_TEMPLATE_P (tmpl)
    3138      408889 :               && DECL_STATIC_FUNCTION_P (tmpl)
    3139      908343 :               && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
    3140         791 :             revert_static_member_fn (decl);
    3141             : 
    3142             :           /* If this is a specialization of a member template of a
    3143             :              template class, we want to return the TEMPLATE_DECL, not
    3144             :              the specialization of it.  */
    3145      907540 :           if (tsk == tsk_template && !was_template_id)
    3146             :             {
    3147         703 :               tree result = DECL_TEMPLATE_RESULT (tmpl);
    3148         703 :               SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
    3149         703 :               DECL_INITIAL (result) = NULL_TREE;
    3150         703 :               if (have_def)
    3151             :                 {
    3152         690 :                   tree parm;
    3153         690 :                   DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
    3154        1380 :                   DECL_SOURCE_LOCATION (result)
    3155         690 :                     = DECL_SOURCE_LOCATION (decl);
    3156             :                   /* We want to use the argument list specified in the
    3157             :                      definition, not in the original declaration.  */
    3158         690 :                   DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
    3159        1443 :                   for (parm = DECL_ARGUMENTS (result); parm;
    3160         753 :                        parm = DECL_CHAIN (parm))
    3161         753 :                     DECL_CONTEXT (parm) = result;
    3162             :                 }
    3163         703 :               decl = register_specialization (tmpl, gen_tmpl, targs,
    3164             :                                               is_friend, 0);
    3165         703 :               remove_contract_attributes (result);
    3166         703 :               return decl;
    3167             :             }
    3168             : 
    3169             :           /* Set up the DECL_TEMPLATE_INFO for DECL.  */
    3170      906837 :           DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
    3171             : 
    3172      906837 :           if (was_template_id)
    3173      499452 :             TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
    3174             : 
    3175             :           /* Inherit default function arguments from the template
    3176             :              DECL is specializing.  */
    3177      906837 :           if (DECL_FUNCTION_TEMPLATE_P (tmpl))
    3178      408186 :             copy_default_args_to_explicit_spec (decl);
    3179             : 
    3180             :           /* This specialization has the same protection as the
    3181             :              template it specializes.  */
    3182      906837 :           TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
    3183      906837 :           TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
    3184             : 
    3185             :           /* 7.1.1-1 [dcl.stc]
    3186             : 
    3187             :              A storage-class-specifier shall not be specified in an
    3188             :              explicit specialization...
    3189             : 
    3190             :              The parser rejects these, so unless action is taken here,
    3191             :              explicit function specializations will always appear with
    3192             :              global linkage.
    3193             : 
    3194             :              The action recommended by the C++ CWG in response to C++
    3195             :              defect report 605 is to make the storage class and linkage
    3196             :              of the explicit specialization match the templated function:
    3197             : 
    3198             :              http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
    3199             :            */
    3200      906837 :           if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
    3201             :             {
    3202       67274 :               tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
    3203       67274 :               gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
    3204             : 
    3205             :               /* A concept cannot be specialized.  */
    3206       67274 :               if (DECL_DECLARED_CONCEPT_P (tmpl_func))
    3207             :                 {
    3208           0 :                   error ("explicit specialization of function concept %qD",
    3209             :                          gen_tmpl);
    3210           0 :                   return error_mark_node;
    3211             :                 }
    3212             : 
    3213             :               /* This specialization has the same linkage and visibility as
    3214             :                  the function template it specializes.  */
    3215       67274 :               TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
    3216       67274 :               if (! TREE_PUBLIC (decl))
    3217             :                 {
    3218          40 :                   DECL_INTERFACE_KNOWN (decl) = 1;
    3219          40 :                   DECL_NOT_REALLY_EXTERN (decl) = 1;
    3220             :                 }
    3221       67274 :               DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
    3222       67274 :               if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
    3223             :                 {
    3224          45 :                   DECL_VISIBILITY_SPECIFIED (decl) = 1;
    3225          45 :                   DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
    3226             :                 }
    3227             :             }
    3228             : 
    3229             :           /* If DECL is a friend declaration, declared using an
    3230             :              unqualified name, the namespace associated with DECL may
    3231             :              have been set incorrectly.  For example, in:
    3232             : 
    3233             :                template <typename T> void f(T);
    3234             :                namespace N {
    3235             :                  struct S { friend void f<int>(int); }
    3236             :                }
    3237             : 
    3238             :              we will have set the DECL_CONTEXT for the friend
    3239             :              declaration to N, rather than to the global namespace.  */
    3240      906837 :           if (DECL_NAMESPACE_SCOPE_P (decl))
    3241      565719 :             DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
    3242             : 
    3243      906837 :           if (is_friend && !have_def)
    3244             :             /* This is not really a declaration of a specialization.
    3245             :                It's just the name of an instantiation.  But, it's not
    3246             :                a request for an instantiation, either.  */
    3247          71 :             SET_DECL_IMPLICIT_INSTANTIATION (decl);
    3248      906766 :           else if (TREE_CODE (decl) == FUNCTION_DECL)
    3249             :             /* A specialization is not necessarily COMDAT.  */
    3250      408115 :             DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
    3251      408115 :                                   && DECL_DECLARED_INLINE_P (decl));
    3252      498651 :           else if (VAR_P (decl))
    3253      498651 :             DECL_COMDAT (decl) = false;
    3254             : 
    3255             :           /* If this is a full specialization, register it so that we can find
    3256             :              it again.  Partial specializations will be registered in
    3257             :              process_partial_specialization.  */
    3258      906837 :           if (!processing_template_decl)
    3259             :             {
    3260      541206 :               warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
    3261             : 
    3262      541206 :               decl = register_specialization (decl, gen_tmpl, targs,
    3263             :                                               is_friend, 0);
    3264             :             }
    3265             : 
    3266             :           /* If this is a specialization, splice any contracts that may have
    3267             :              been inherited from the template, removing them.  */
    3268      906837 :           if (decl != error_mark_node && DECL_TEMPLATE_SPECIALIZATION (decl))
    3269      906737 :             remove_contract_attributes (decl);
    3270             : 
    3271             :           /* A 'structor should already have clones.  */
    3272     1314997 :           gcc_assert (decl == error_mark_node
    3273             :                       || variable_template_p (tmpl)
    3274             :                       || !(DECL_CONSTRUCTOR_P (decl)
    3275             :                            || DECL_DESTRUCTOR_P (decl))
    3276             :                       || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
    3277             :         }
    3278             :     }
    3279             : 
    3280             :   return decl;
    3281             : }
    3282             : 
    3283             : /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
    3284             :    parameters.  These are represented in the same format used for
    3285             :    DECL_TEMPLATE_PARMS.  */
    3286             : 
    3287             : int
    3288   240542436 : comp_template_parms (const_tree parms1, const_tree parms2)
    3289             : {
    3290   240542436 :   const_tree p1;
    3291   240542436 :   const_tree p2;
    3292             : 
    3293   240542436 :   if (parms1 == parms2)
    3294             :     return 1;
    3295             : 
    3296             :   for (p1 = parms1, p2 = parms2;
    3297   309814229 :        p1 != NULL_TREE && p2 != NULL_TREE;
    3298    69334088 :        p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
    3299             :     {
    3300   245428833 :       tree t1 = TREE_VALUE (p1);
    3301   245428833 :       tree t2 = TREE_VALUE (p2);
    3302   245428833 :       int i;
    3303             : 
    3304   245428833 :       gcc_assert (TREE_CODE (t1) == TREE_VEC);
    3305   245428833 :       gcc_assert (TREE_CODE (t2) == TREE_VEC);
    3306             : 
    3307   245428833 :       if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
    3308             :         return 0;
    3309             : 
    3310   259362416 :       for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
    3311             :         {
    3312   190028328 :           tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
    3313   190028328 :           tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
    3314             : 
    3315             :           /* If either of the template parameters are invalid, assume
    3316             :              they match for the sake of error recovery. */
    3317   190028328 :           if (error_operand_p (parm1) || error_operand_p (parm2))
    3318             :             return 1;
    3319             : 
    3320   190028290 :           if (TREE_CODE (parm1) != TREE_CODE (parm2))
    3321             :             return 0;
    3322             : 
    3323   182131237 :           if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
    3324   182131237 :               && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
    3325           0 :                   == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
    3326           0 :             continue;
    3327   182131237 :           else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
    3328             :             return 0;
    3329             :         }
    3330             :     }
    3331             : 
    3332    64385396 :   if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
    3333             :     /* One set of parameters has more parameters lists than the
    3334             :        other.  */
    3335             :     return 0;
    3336             : 
    3337             :   return 1;
    3338             : }
    3339             : 
    3340             : /* Returns true if two template parameters are declared with
    3341             :    equivalent constraints.  */
    3342             : 
    3343             : static bool
    3344     7510150 : template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
    3345             : {
    3346     7510150 :   tree req1 = TREE_TYPE (parm1);
    3347     7510150 :   tree req2 = TREE_TYPE (parm2);
    3348     7510150 :   if (!req1 != !req2)
    3349             :     return false;
    3350     7485742 :   if (req1)
    3351       34452 :     return cp_tree_equal (req1, req2);
    3352             :   return true;
    3353             : }
    3354             : 
    3355             : /* Returns true when two template parameters are equivalent.  */
    3356             : 
    3357             : static bool
    3358     5714950 : template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
    3359             : {
    3360     5714950 :   tree decl1 = TREE_VALUE (parm1);
    3361     5714950 :   tree decl2 = TREE_VALUE (parm2);
    3362             : 
    3363             :   /* If either of the template parameters are invalid, assume
    3364             :      they match for the sake of error recovery. */
    3365     5714950 :   if (error_operand_p (decl1) || error_operand_p (decl2))
    3366             :     return true;
    3367             : 
    3368             :   /* ... they declare parameters of the same kind.  */
    3369     5714949 :   if (TREE_CODE (decl1) != TREE_CODE (decl2))
    3370             :     return false;
    3371             : 
    3372             :   /* ... one parameter was introduced by a parameter declaration, then
    3373             :      both are. This case arises as a result of eagerly rewriting declarations
    3374             :      during parsing.  */
    3375     5437042 :   if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
    3376             :     return false;
    3377             : 
    3378             :   /* ... if either declares a pack, they both do.  */
    3379     5436935 :   if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
    3380             :     return false;
    3381             : 
    3382     5083836 :   if (TREE_CODE (decl1) == PARM_DECL)
    3383             :     {
    3384             :       /* ... if they declare non-type parameters, the types are equivalent.  */
    3385      112486 :       if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
    3386             :         return false;
    3387             :     }
    3388     4971350 :   else if (TREE_CODE (decl2) == TEMPLATE_DECL)
    3389             :     {
    3390             :       /* ... if they declare template template parameters, their template
    3391             :          parameter lists are equivalent.  */
    3392        1126 :       if (!template_heads_equivalent_p (decl1, decl2))
    3393             :         return false;
    3394             :     }
    3395             : 
    3396             :   /* ... if they are declared with a qualified-concept name, they both
    3397             :      are, and those names are equivalent.  */
    3398     5080450 :   return template_parameter_constraints_equivalent_p (parm1, parm2);
    3399             : }
    3400             : 
    3401             : /* Returns true if two template parameters lists are equivalent.
    3402             :    Two template parameter lists are equivalent if they have the
    3403             :    same length and their corresponding parameters are equivalent.
    3404             : 
    3405             :    PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
    3406             :    data structure returned by DECL_TEMPLATE_PARMS.
    3407             : 
    3408             :    This is generally the same implementation as comp_template_parms
    3409             :    except that it also the concept names and arguments used to
    3410             :    introduce parameters.  */
    3411             : 
    3412             : static bool
    3413     6687004 : template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
    3414             : {
    3415     6687004 :   if (parms1 == parms2)
    3416             :     return true;
    3417             : 
    3418             :   const_tree p1 = parms1;
    3419             :   const_tree p2 = parms2;
    3420     9221914 :   while (p1 != NULL_TREE && p2 != NULL_TREE)
    3421             :     {
    3422     6708841 :       tree list1 = TREE_VALUE (p1);
    3423     6708841 :       tree list2 = TREE_VALUE (p2);
    3424             : 
    3425     6708841 :       if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
    3426             :         return 0;
    3427             : 
    3428     8249860 :       for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
    3429             :         {
    3430     5714950 :           tree parm1 = TREE_VEC_ELT (list1, i);
    3431     5714950 :           tree parm2 = TREE_VEC_ELT (list2, i);
    3432     5714950 :           if (!template_parameters_equivalent_p (parm1, parm2))
    3433             :             return false;
    3434             :         }
    3435             : 
    3436     2534910 :       p1 = TREE_CHAIN (p1);
    3437     2534910 :       p2 = TREE_CHAIN (p2);
    3438             :     }
    3439             : 
    3440     2513073 :   if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
    3441             :     return false;
    3442             : 
    3443             :   return true;
    3444             : }
    3445             : 
    3446             : /* Return true if the requires-clause of the template parameter lists are
    3447             :    equivalent and false otherwise.  */
    3448             : static bool
    3449     4051078 : template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
    3450             : {
    3451     4051078 :   tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
    3452     4051078 :   tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
    3453     4051078 :   if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
    3454             :     return false;
    3455     4047218 :   if (!cp_tree_equal (req1, req2))
    3456             :     return false;
    3457             :   return true;
    3458             : }
    3459             : 
    3460             : /* Returns true if two template heads are equivalent. 17.6.6.1p6:
    3461             :    Two template heads are equivalent if their template parameter
    3462             :    lists are equivalent and their requires clauses are equivalent.
    3463             : 
    3464             :    In pre-C++20, this is equivalent to calling comp_template_parms
    3465             :    for the template parameters of TMPL1 and TMPL2.  */
    3466             : 
    3467             : bool
    3468   152917471 : template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
    3469             : {
    3470   152917471 :   tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
    3471   152917471 :   tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
    3472             : 
    3473             :   /* Don't change the matching rules for pre-C++20.  */
    3474   152917471 :   if (cxx_dialect < cxx20)
    3475   146230467 :     return comp_template_parms (parms1, parms2);
    3476             : 
    3477             :   /* ... have the same number of template parameters, and their
    3478             :      corresponding parameters are equivalent.  */
    3479     6687004 :   if (!template_parameter_lists_equivalent_p (parms1, parms2))
    3480             :     return false;
    3481             : 
    3482             :   /* ... if either has a requires-clause, they both do and their
    3483             :      corresponding constraint-expressions are equivalent.  */
    3484     2513073 :   return template_requirements_equivalent_p (parms1, parms2);
    3485             : }
    3486             : 
    3487             : /* Determine whether PARM is a parameter pack.  */
    3488             : 
    3489             : bool
    3490  2167130252 : template_parameter_pack_p (const_tree parm)
    3491             : {
    3492             :   /* Determine if we have a non-type template parameter pack.  */
    3493  2167130252 :   if (TREE_CODE (parm) == PARM_DECL)
    3494   332618183 :     return (DECL_TEMPLATE_PARM_P (parm)
    3495   332618183 :             && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
    3496  1834512069 :   if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
    3497      973852 :     return TEMPLATE_PARM_PARAMETER_PACK (parm);
    3498             : 
    3499             :   /* If this is a list of template parameters, we could get a
    3500             :      TYPE_DECL or a TEMPLATE_DECL.  */
    3501  1833538217 :   if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
    3502  1744208483 :     parm = TREE_TYPE (parm);
    3503             : 
    3504             :   /* Otherwise it must be a type template parameter.  */
    3505  1833538217 :   return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
    3506  1833538217 :            || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
    3507  1833538217 :           && TEMPLATE_TYPE_PARAMETER_PACK (parm));
    3508             : }
    3509             : 
    3510             : /* Determine if T is a function parameter pack.  */
    3511             : 
    3512             : bool
    3513     1003765 : function_parameter_pack_p (const_tree t)
    3514             : {
    3515     1003765 :   if (t && TREE_CODE (t) == PARM_DECL)
    3516     1003765 :     return DECL_PACK_P (t);
    3517             :   return false;
    3518             : }
    3519             : 
    3520             : /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
    3521             :    PRIMARY_FUNC_TMPL_INST is a primary function template instantiation.  */
    3522             : 
    3523             : tree
    3524     1199121 : get_function_template_decl (const_tree primary_func_tmpl_inst)
    3525             : {
    3526     1199121 :   if (! primary_func_tmpl_inst
    3527     1199121 :       || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
    3528     2398242 :       || ! primary_template_specialization_p (primary_func_tmpl_inst))
    3529             :     return NULL;
    3530             : 
    3531      286962 :   return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
    3532             : }
    3533             : 
    3534             : /* Return true iff the function parameter PARAM_DECL was expanded
    3535             :    from the function parameter pack PACK.  */
    3536             : 
    3537             : bool
    3538      278992 : function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
    3539             : {
    3540      278992 :   if (DECL_ARTIFICIAL (param_decl)
    3541      278992 :       || !function_parameter_pack_p (pack))
    3542             :     return false;
    3543             : 
    3544             :   /* The parameter pack and its pack arguments have the same
    3545             :      DECL_PARM_INDEX.  */
    3546      278992 :   return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
    3547             : }
    3548             : 
    3549             : /* Determine whether ARGS describes a variadic template args list,
    3550             :    i.e., one that is terminated by a template argument pack.  */
    3551             : 
    3552             : static bool
    3553           4 : template_args_variadic_p (tree args)
    3554             : {
    3555           4 :   int nargs;
    3556           4 :   tree last_parm;
    3557             : 
    3558           4 :   if (args == NULL_TREE)
    3559             :     return false;
    3560             : 
    3561           4 :   args = INNERMOST_TEMPLATE_ARGS (args);
    3562           4 :   nargs = TREE_VEC_LENGTH (args);
    3563             : 
    3564           4 :   if (nargs == 0)
    3565             :     return false;
    3566             : 
    3567           4 :   last_parm = TREE_VEC_ELT (args, nargs - 1);
    3568             : 
    3569           4 :   return ARGUMENT_PACK_P (last_parm);
    3570             : }
    3571             : 
    3572             : /* Generate a new name for the parameter pack name NAME (an
    3573             :    IDENTIFIER_NODE) that incorporates its */
    3574             : 
    3575             : static tree
    3576      537139 : make_ith_pack_parameter_name (tree name, int i)
    3577             : {
    3578             :   /* Munge the name to include the parameter index.  */
    3579             : #define NUMBUF_LEN 128
    3580      537139 :   char numbuf[NUMBUF_LEN];
    3581      537139 :   char* newname;
    3582      537139 :   int newname_len;
    3583             : 
    3584      537139 :   if (name == NULL_TREE)
    3585             :     return name;
    3586      534490 :   snprintf (numbuf, NUMBUF_LEN, "%i", i);
    3587      534490 :   newname_len = IDENTIFIER_LENGTH (name)
    3588      534490 :                 + strlen (numbuf) + 2;
    3589      534490 :   newname = (char*)alloca (newname_len);
    3590      534490 :   snprintf (newname, newname_len,
    3591      534490 :             "%s#%i", IDENTIFIER_POINTER (name), i);
    3592      534490 :   return get_identifier (newname);
    3593             : }
    3594             : 
    3595             : /* Return true if T is a primary function, class or alias template
    3596             :    specialization, not including the template pattern.  */
    3597             : 
    3598             : bool
    3599    86221687 : primary_template_specialization_p (const_tree t)
    3600             : {
    3601    86221687 :   if (!t)
    3602             :     return false;
    3603             : 
    3604    86221687 :   if (VAR_OR_FUNCTION_DECL_P (t))
    3605    44903236 :     return (DECL_LANG_SPECIFIC (t)
    3606    44903137 :             && DECL_USE_TEMPLATE (t)
    3607    42545936 :             && DECL_TEMPLATE_INFO (t)
    3608    87449172 :             && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
    3609    41318451 :   else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
    3610    41318451 :     return (CLASSTYPE_TEMPLATE_INFO (t)
    3611    41318451 :             && CLASSTYPE_USE_TEMPLATE (t)
    3612    82636902 :             && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
    3613           0 :   else if (alias_template_specialization_p (t, nt_transparent))
    3614             :     return true;
    3615             :   return false;
    3616             : }
    3617             : 
    3618             : /* Return true if PARM is a template template parameter.  */
    3619             : 
    3620             : bool
    3621    26688604 : template_template_parameter_p (const_tree parm)
    3622             : {
    3623    26688604 :   return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
    3624             : }
    3625             : 
    3626             : /* Return true iff PARM is a DECL representing a type template
    3627             :    parameter.  */
    3628             : 
    3629             : bool
    3630   867594622 : template_type_parameter_p (const_tree parm)
    3631             : {
    3632   867594622 :   return (parm
    3633   867594622 :           && (TREE_CODE (parm) == TYPE_DECL
    3634   867594622 :               || TREE_CODE (parm) == TEMPLATE_DECL)
    3635  1324423991 :           && DECL_TEMPLATE_PARM_P (parm));
    3636             : }
    3637             : 
    3638             : /* Return the template parameters of T if T is a
    3639             :    primary template instantiation, NULL otherwise.  */
    3640             : 
    3641             : tree
    3642    95606735 : get_primary_template_innermost_parameters (const_tree t)
    3643             : {
    3644    95606735 :   tree parms = NULL, template_info = NULL;
    3645             : 
    3646    95606735 :   if ((template_info = get_template_info (t))
    3647    95606735 :       && primary_template_specialization_p (t))
    3648    41362678 :     parms = INNERMOST_TEMPLATE_PARMS
    3649             :         (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
    3650             : 
    3651    95606735 :   return parms;
    3652             : }
    3653             : 
    3654             : /* Returns the template arguments of T if T is a template instantiation,
    3655             :    NULL otherwise.  */
    3656             : 
    3657             : tree
    3658    19730549 : get_template_innermost_arguments (const_tree t)
    3659             : {
    3660    19730549 :   tree args = NULL, template_info = NULL;
    3661             : 
    3662    19730549 :   if ((template_info = get_template_info (t))
    3663    39461098 :       && TI_ARGS (template_info))
    3664    19730549 :     args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
    3665             : 
    3666    19730549 :   return args;
    3667             : }
    3668             : 
    3669             : /* Return the argument pack elements of T if T is a template argument pack,
    3670             :    NULL otherwise.  */
    3671             : 
    3672             : tree
    3673    29540383 : get_template_argument_pack_elems (const_tree t)
    3674             : {
    3675    29540383 :   if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
    3676    29540383 :       && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
    3677             :     return NULL;
    3678             : 
    3679     3075480 :   return ARGUMENT_PACK_ARGS (t);
    3680             : }
    3681             : 
    3682             : /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
    3683             :    ARGUMENT_PACK_SELECT represents. */
    3684             : 
    3685             : static tree
    3686     8386052 : argument_pack_select_arg (tree t)
    3687             : {
    3688     8386052 :   tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
    3689     8386052 :   tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
    3690             : 
    3691             :   /* If the selected argument is an expansion E, that most likely means we were
    3692             :      called from gen_elem_of_pack_expansion_instantiation during the
    3693             :      substituting of an argument pack (of which the Ith element is a pack
    3694             :      expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
    3695             :      In this case, the Ith element resulting from this substituting is going to
    3696             :      be a pack expansion, which pattern is the pattern of E.  Let's return the
    3697             :      pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
    3698             :      resulting pack expansion from it.  */
    3699     8386052 :   if (PACK_EXPANSION_P (arg))
    3700             :     {
    3701             :       /* Make sure we aren't throwing away arg info.  */
    3702      271817 :       gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
    3703      271817 :       arg = PACK_EXPANSION_PATTERN (arg);
    3704             :     }
    3705             : 
    3706     8386052 :   return arg;
    3707             : }
    3708             : 
    3709             : /* Return a modification of ARGS that's suitable for preserving inside a hash
    3710             :    table.  In particular, this replaces each ARGUMENT_PACK_SELECT with its
    3711             :    underlying argument.  ARGS is copied (upon modification) iff COW_P.  */
    3712             : 
    3713             : static tree
    3714      110327 : preserve_args (tree args, bool cow_p = true)
    3715             : {
    3716      110327 :   if (!args)
    3717             :     return NULL_TREE;
    3718             : 
    3719      310583 :   for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
    3720             :     {
    3721      200258 :       tree t = TREE_VEC_ELT (args, i);
    3722      200258 :       tree r;
    3723      200258 :       if (!t)
    3724             :         r = NULL_TREE;
    3725      200249 :       else if (TREE_CODE (t) == ARGUMENT_PACK_SELECT)
    3726          27 :         r = argument_pack_select_arg (t);
    3727      200222 :       else if (TREE_CODE (t) == TREE_VEC)
    3728       11830 :         r = preserve_args (t, cow_p);
    3729             :       else
    3730             :         r = t;
    3731      200249 :       if (r != t)
    3732             :         {
    3733          27 :           if (cow_p)
    3734             :             {
    3735          25 :               args = copy_template_args (args);
    3736          25 :               cow_p = false;
    3737             :             }
    3738          27 :           TREE_VEC_ELT (args, i) = r;
    3739             :         }
    3740             :     }
    3741             : 
    3742             :   return args;
    3743             : }
    3744             : 
    3745             : /* True iff FN is a function representing a built-in variadic parameter
    3746             :    pack.  */
    3747             : 
    3748             : bool
    3749   170235846 : builtin_pack_fn_p (tree fn)
    3750             : {
    3751   170235846 :   if (!fn
    3752   170225849 :       || TREE_CODE (fn) != FUNCTION_DECL
    3753   212518834 :       || !DECL_IS_UNDECLARED_BUILTIN (fn))
    3754             :     return false;
    3755             : 
    3756     6444200 :   if (id_equal (DECL_NAME (fn), "__integer_pack"))
    3757             :     return true;
    3758             : 
    3759             :   return false;
    3760             : }
    3761             : 
    3762             : /* True iff CALL is a call to a function representing a built-in variadic
    3763             :    parameter pack.  */
    3764             : 
    3765             : static bool
    3766   193110772 : builtin_pack_call_p (tree call)
    3767             : {
    3768   193110772 :   if (TREE_CODE (call) != CALL_EXPR)
    3769             :     return false;
    3770   136225962 :   return builtin_pack_fn_p (CALL_EXPR_FN (call));
    3771             : }
    3772             : 
    3773             : /* Return a TREE_VEC for the expansion of __integer_pack(HI).  */
    3774             : 
    3775             : static tree
    3776       75738 : expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
    3777             :                      tree in_decl)
    3778             : {
    3779       75738 :   tree ohi = CALL_EXPR_ARG (call, 0);
    3780       75738 :   tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl);
    3781             : 
    3782       75738 :   if (instantiation_dependent_expression_p (hi))
    3783             :     {
    3784       74817 :       if (hi != ohi)
    3785             :         {
    3786       74817 :           call = copy_node (call);
    3787       74817 :           CALL_EXPR_ARG (call, 0) = hi;
    3788             :         }
    3789       74817 :       tree ex = make_pack_expansion (call, complain);
    3790       74817 :       tree vec = make_tree_vec (1);
    3791       74817 :       TREE_VEC_ELT (vec, 0) = ex;
    3792       74817 :       return vec;
    3793             :     }
    3794             :   else
    3795             :     {
    3796         921 :       hi = instantiate_non_dependent_expr (hi, complain);
    3797         921 :       hi = cxx_constant_value (hi, complain);
    3798         921 :       int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
    3799             : 
    3800             :       /* Calculate the largest value of len that won't make the size of the vec
    3801             :          overflow an int.  The compiler will exceed resource limits long before
    3802             :          this, but it seems a decent place to diagnose.  */
    3803         921 :       int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
    3804             : 
    3805         921 :       if (len < 0 || len > max)
    3806             :         {
    3807          12 :           if ((complain & tf_error)
    3808           9 :               && hi != error_mark_node)
    3809           9 :             error ("argument to %<__integer_pack%> must be between 0 and %d",
    3810             :                    max);
    3811          12 :           return error_mark_node;
    3812             :         }
    3813             : 
    3814         909 :       tree vec = make_tree_vec (len);
    3815             : 
    3816        4964 :       for (int i = 0; i < len; ++i)
    3817        4055 :         TREE_VEC_ELT (vec, i) = size_int (i);
    3818             : 
    3819             :       return vec;
    3820             :     }
    3821             : }
    3822             : 
    3823             : /* Return a TREE_VEC for the expansion of built-in template parameter pack
    3824             :    CALL.  */
    3825             : 
    3826             : static tree
    3827       75738 : expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
    3828             :                           tree in_decl)
    3829             : {
    3830       75738 :   if (!builtin_pack_call_p (call))
    3831             :     return NULL_TREE;
    3832             : 
    3833       75738 :   tree fn = CALL_EXPR_FN (call);
    3834             : 
    3835       75738 :   if (id_equal (DECL_NAME (fn), "__integer_pack"))
    3836       75738 :     return expand_integer_pack (call, args, complain, in_decl);
    3837             : 
    3838             :   return NULL_TREE;
    3839             : }
    3840             : 
    3841             : /* Return true if the tree T has the extra args mechanism for
    3842             :    avoiding partial instantiation.  */
    3843             : 
    3844             : static bool
    3845  4293306501 : has_extra_args_mechanism_p (const_tree t)
    3846             : {
    3847  4293306501 :   return (PACK_EXPANSION_P (t) /* PACK_EXPANSION_EXTRA_ARGS  */
    3848  4293306501 :           || TREE_CODE (t) == REQUIRES_EXPR /* REQUIRES_EXPR_EXTRA_ARGS  */
    3849  4293306501 :           || (TREE_CODE (t) == IF_STMT
    3850      295243 :               && IF_STMT_CONSTEXPR_P (t))); /* IF_STMT_EXTRA_ARGS  */
    3851             : }
    3852             : 
    3853             : /* Structure used to track the progress of find_parameter_packs_r.  */
    3854  1195621779 : struct find_parameter_pack_data
    3855             : {
    3856             :   /* TREE_LIST that will contain all of the parameter packs found by
    3857             :      the traversal.  */
    3858             :   tree* parameter_packs;
    3859             : 
    3860             :   /* Set of AST nodes that have been visited by the traversal.  */
    3861             :   hash_set<tree> *visited;
    3862             : 
    3863             :   /* True iff we're making a type pack expansion.  */
    3864             :   bool type_pack_expansion_p;
    3865             : 
    3866             :   /* True iff we found a subtree that has the extra args mechanism.  */
    3867             :   bool found_extra_args_tree_p = false;
    3868             : };
    3869             : 
    3870             : /* Identifies all of the argument packs that occur in a template
    3871             :    argument and appends them to the TREE_LIST inside DATA, which is a
    3872             :    find_parameter_pack_data structure. This is a subroutine of
    3873             :    make_pack_expansion and uses_parameter_packs.  */
    3874             : static tree
    3875  4416036505 : find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
    3876             : {
    3877  4416036505 :   tree t = *tp;
    3878  4416036505 :   struct find_parameter_pack_data* ppd =
    3879             :     (struct find_parameter_pack_data*)data;
    3880  4416036505 :   bool parameter_pack_p = false;
    3881             : 
    3882             : #define WALK_SUBTREE(NODE)                              \
    3883             :   cp_walk_tree (&(NODE), &find_parameter_packs_r,       \
    3884             :                 ppd, ppd->visited)                   \
    3885             : 
    3886             :   /* Don't look through typedefs; we are interested in whether a
    3887             :      parameter pack is actually written in the expression/type we're
    3888             :      looking at, not the target type.  */
    3889  4416036505 :   if (TYPE_P (t) && typedef_variant_p (t))
    3890             :     {
    3891             :       /* But do look at arguments for an alias template.  */
    3892   122730004 :       if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
    3893    92112379 :         cp_walk_tree (&TI_ARGS (tinfo),
    3894             :                       &find_parameter_packs_r,
    3895             :                       ppd, ppd->visited);
    3896   122730004 :       *walk_subtrees = 0;
    3897   122730004 :       return NULL_TREE;
    3898             :     }
    3899             : 
    3900             :   /* Identify whether this is a parameter pack or not.  */
    3901  4293306501 :   switch (TREE_CODE (t))
    3902             :     {
    3903    42137421 :     case TEMPLATE_PARM_INDEX:
    3904    42137421 :       if (TEMPLATE_PARM_PARAMETER_PACK (t))
    3905             :         parameter_pack_p = true;
    3906             :       break;
    3907             : 
    3908   753935681 :     case TEMPLATE_TYPE_PARM:
    3909   753935681 :       t = TYPE_MAIN_VARIANT (t);
    3910             :       /* FALLTHRU */
    3911   754475623 :     case TEMPLATE_TEMPLATE_PARM:
    3912             :       /* If the placeholder appears in the decl-specifier-seq of a function
    3913             :          parameter pack (14.6.3), or the type-specifier-seq of a type-id that
    3914             :          is a pack expansion, the invented template parameter is a template
    3915             :          parameter pack.  */
    3916   754475623 :       if (ppd->type_pack_expansion_p && is_auto (t))
    3917          20 :         TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
    3918   754475623 :       if (TEMPLATE_TYPE_PARAMETER_PACK (t))
    3919             :         parameter_pack_p = true;
    3920             :       break;
    3921             : 
    3922   250996539 :     case FIELD_DECL:
    3923   250996539 :     case PARM_DECL:
    3924   250996539 :       if (DECL_PACK_P (t))
    3925             :         {
    3926             :           /* We don't want to walk into the type of a PARM_DECL,
    3927             :              because we don't want to see the type parameter pack.  */
    3928     1915890 :           *walk_subtrees = 0;
    3929     1915890 :           parameter_pack_p = true;
    3930             :         }
    3931             :       break;
    3932             : 
    3933   103324210 :     case VAR_DECL:
    3934   103324210 :       if (DECL_PACK_P (t))
    3935             :         {
    3936             :           /* We don't want to walk into the type of a variadic capture proxy,
    3937             :              because we don't want to see the type parameter pack.  */
    3938          40 :           *walk_subtrees = 0;
    3939          40 :           parameter_pack_p = true;
    3940             :         }
    3941   103324170 :       else if (variable_template_specialization_p (t))
    3942             :         {
    3943       16984 :           cp_walk_tree (&DECL_TI_ARGS (t),
    3944             :                         find_parameter_packs_r,
    3945             :                         ppd, ppd->visited);
    3946       16984 :           *walk_subtrees = 0;
    3947             :         }
    3948             :       break;
    3949             : 
    3950   136074484 :     case CALL_EXPR:
    3951   136074484 :       if (builtin_pack_call_p (t))
    3952             :         parameter_pack_p = true;
    3953             :       break;
    3954             : 
    3955             :     case BASES:
    3956             :       parameter_pack_p = true;
    3957             :       break;
    3958             :     default:
    3959             :       /* Not a parameter pack.  */
    3960             :       break;
    3961             :     }
    3962             : 
    3963     1932914 :   if (parameter_pack_p)
    3964             :     {
    3965             :       /* Add this parameter pack to the list.  */
    3966    42135006 :       *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
    3967             :     }
    3968             : 
    3969  4293306501 :   if (has_extra_args_mechanism_p (t) && !PACK_EXPANSION_P (t))
    3970      283472 :     ppd->found_extra_args_tree_p = true;
    3971             : 
    3972  4293306501 :   if (TYPE_P (t))
    3973  1433659696 :     cp_walk_tree (&TYPE_CONTEXT (t),
    3974             :                   &find_parameter_packs_r, ppd, ppd->visited);
    3975             : 
    3976             :   /* This switch statement will return immediately if we don't find a
    3977             :      parameter pack.  ??? Should some of these be in cp_walk_subtrees?  */
    3978  4293306501 :   switch (TREE_CODE (t))
    3979             :     {
    3980      240250 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
    3981             :       /* Check the template itself.  */
    3982      240250 :       cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
    3983             :                     &find_parameter_packs_r, ppd, ppd->visited);
    3984      240250 :       return NULL_TREE;
    3985             : 
    3986      570434 :     case DECL_EXPR:
    3987      570434 :       {
    3988      570434 :         tree decl = DECL_EXPR_DECL (t);
    3989             :         /* Ignore the declaration of a capture proxy for a parameter pack.  */
    3990      570434 :         if (is_capture_proxy (decl))
    3991      378747 :           *walk_subtrees = 0;
    3992      570434 :         if (is_typedef_decl (decl))
    3993             :           /* Since we stop at typedefs above, we need to look through them at
    3994             :              the point of the DECL_EXPR.  */
    3995       36069 :           cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
    3996             :                         &find_parameter_packs_r, ppd, ppd->visited);
    3997             :         return NULL_TREE;
    3998             :       }
    3999             : 
    4000   115278875 :     case TEMPLATE_DECL:
    4001   115278875 :       if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
    4002             :         return NULL_TREE;
    4003         175 :       cp_walk_tree (&TREE_TYPE (t),
    4004             :                     &find_parameter_packs_r, ppd, ppd->visited);
    4005         175 :       return NULL_TREE;
    4006             : 
    4007    21855285 :     case TYPE_PACK_EXPANSION:
    4008    21855285 :     case EXPR_PACK_EXPANSION:
    4009    21855285 :       *walk_subtrees = 0;
    4010    21855285 :       return NULL_TREE;
    4011             : 
    4012    19138011 :     case INTEGER_TYPE:
    4013    19138011 :       cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
    4014             :                     ppd, ppd->visited);
    4015    19138011 :       *walk_subtrees = 0;
    4016    19138011 :       return NULL_TREE;
    4017             : 
    4018   132736398 :     case IDENTIFIER_NODE:
    4019   132736398 :       cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
    4020             :                     ppd->visited);
    4021   132736398 :       *walk_subtrees = 0;
    4022   132736398 :       return NULL_TREE;
    4023             : 
    4024      283345 :     case LAMBDA_EXPR:
    4025      283345 :       {
    4026             :         /* Since we defer implicit capture, look in the parms and body.  */
    4027      283345 :         tree fn = lambda_function (t);
    4028      283345 :         cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
    4029             :                       ppd->visited);
    4030      283345 :         cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
    4031             :                       ppd->visited);
    4032      283345 :         return NULL_TREE;
    4033             :       }
    4034             : 
    4035     2954319 :     case DECLTYPE_TYPE:
    4036     2954319 :       {
    4037             :         /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
    4038             :            type_pack_expansion_p to false so that any placeholders
    4039             :            within the expression don't get marked as parameter packs.  */
    4040     2954319 :         bool type_pack_expansion_p = ppd->type_pack_expansion_p;
    4041     2954319 :         ppd->type_pack_expansion_p = false;
    4042     2954319 :         cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
    4043             :                       ppd, ppd->visited);
    4044     2954319 :         ppd->type_pack_expansion_p = type_pack_expansion_p;
    4045     2954319 :         *walk_subtrees = 0;
    4046     2954319 :         return NULL_TREE;
    4047             :       }
    4048             : 
    4049      295243 :     case IF_STMT:
    4050      295243 :       cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
    4051             :                     ppd, ppd->visited);
    4052      295243 :       cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
    4053             :                     ppd, ppd->visited);
    4054      295243 :       cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
    4055             :                     ppd, ppd->visited);
    4056             :       /* Don't walk into IF_STMT_EXTRA_ARGS.  */
    4057      295243 :       *walk_subtrees = 0;
    4058      295243 :       return NULL_TREE;
    4059             : 
    4060          83 :     case TAG_DEFN:
    4061          83 :       t = TREE_TYPE (t);
    4062          83 :       if (CLASS_TYPE_P (t))
    4063             :         {
    4064             :           /* Local class, need to look through the whole definition.
    4065             :              TYPE_BINFO might be unset for a partial instantiation.  */
    4066          44 :           if (TYPE_BINFO (t))
    4067          50 :             for (tree bb : BINFO_BASE_BINFOS (TYPE_BINFO (t)))
    4068           6 :               cp_walk_tree (&BINFO_TYPE (bb), &find_parameter_packs_r,
    4069             :                             ppd, ppd->visited);
    4070             :         }
    4071             :       else
    4072             :         /* Enum, look at the values.  */
    4073         129 :         for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
    4074          90 :           cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l)),
    4075             :                         &find_parameter_packs_r,
    4076             :                         ppd, ppd->visited);
    4077             :       return NULL_TREE;
    4078             : 
    4079     3928688 :     case FUNCTION_TYPE:
    4080     3928688 :     case METHOD_TYPE:
    4081     3928688 :       WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t));
    4082     3928688 :       break;
    4083             : 
    4084             :     default:
    4085             :       return NULL_TREE;
    4086             :     }
    4087             : 
    4088             : #undef WALK_SUBTREE
    4089             : 
    4090     3928688 :   return NULL_TREE;
    4091             : }
    4092             : 
    4093             : /* Determines if the expression or type T uses any parameter packs.  */
    4094             : tree
    4095     9894348 : uses_parameter_packs (tree t)
    4096             : {
    4097     9894348 :   tree parameter_packs = NULL_TREE;
    4098     9894348 :   struct find_parameter_pack_data ppd;
    4099     9894348 :   ppd.parameter_packs = &parameter_packs;
    4100     9894348 :   ppd.visited = new hash_set<tree>;
    4101     9894348 :   ppd.type_pack_expansion_p = false;
    4102     9894348 :   cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
    4103    19788696 :   delete ppd.visited;
    4104     9894348 :   return parameter_packs;
    4105             : }
    4106             : 
    4107             : /* Turn ARG, which may be an expression, type, or a TREE_LIST
    4108             :    representation a base-class initializer into a parameter pack
    4109             :    expansion. If all goes well, the resulting node will be an
    4110             :    EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
    4111             :    respectively.  */
    4112             : tree
    4113    38884793 : make_pack_expansion (tree arg, tsubst_flags_t complain)
    4114             : {
    4115    38884793 :   tree result;
    4116    38884793 :   tree parameter_packs = NULL_TREE;
    4117    38884793 :   bool for_types = false;
    4118    38884793 :   struct find_parameter_pack_data ppd;
    4119             : 
    4120    38884793 :   if (!arg || arg == error_mark_node)
    4121             :     return arg;
    4122             : 
    4123    38884781 :   if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
    4124             :     {
    4125             :       /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
    4126             :          class initializer.  In this case, the TREE_PURPOSE will be a
    4127             :          _TYPE node (representing the base class expansion we're
    4128             :          initializing) and the TREE_VALUE will be a TREE_LIST
    4129             :          containing the initialization arguments. 
    4130             : 
    4131             :          The resulting expansion looks somewhat different from most
    4132             :          expansions. Rather than returning just one _EXPANSION, we
    4133             :          return a TREE_LIST whose TREE_PURPOSE is a
    4134             :          TYPE_PACK_EXPANSION containing the bases that will be
    4135             :          initialized.  The TREE_VALUE will be identical to the
    4136             :          original TREE_VALUE, which is a list of arguments that will
    4137             :          be passed to each base.  We do not introduce any new pack
    4138             :          expansion nodes into the TREE_VALUE (although it is possible
    4139             :          that some already exist), because the TREE_PURPOSE and
    4140             :          TREE_VALUE all need to be expanded together with the same
    4141             :          _EXPANSION node.  Note that the TYPE_PACK_EXPANSION in the
    4142             :          resulting TREE_PURPOSE will mention the parameter packs in
    4143             :          both the bases and the arguments to the bases.  */
    4144          30 :       tree purpose;
    4145          30 :       tree value;
    4146          30 :       tree parameter_packs = NULL_TREE;
    4147             : 
    4148             :       /* Determine which parameter packs will be used by the base
    4149             :          class expansion.  */
    4150          30 :       ppd.visited = new hash_set<tree>;
    4151          30 :       ppd.parameter_packs = &parameter_packs;
    4152          30 :       ppd.type_pack_expansion_p = false;
    4153          30 :       gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
    4154          30 :       cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
    4155             :                     &ppd, ppd.visited);
    4156             : 
    4157          30 :       if (parameter_packs == NULL_TREE)
    4158             :         {
    4159           0 :           if (complain & tf_error)
    4160           0 :             error ("base initializer expansion %qT contains no parameter packs",
    4161             :                    arg);
    4162           0 :           delete ppd.visited;
    4163           0 :           return error_mark_node;
    4164             :         }
    4165             : 
    4166          30 :       if (TREE_VALUE (arg) != void_type_node)
    4167             :         {
    4168             :           /* Collect the sets of parameter packs used in each of the
    4169             :              initialization arguments.  */
    4170          57 :           for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
    4171             :             {
    4172             :               /* Determine which parameter packs will be expanded in this
    4173             :                  argument.  */
    4174          33 :               cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r, 
    4175             :                             &ppd, ppd.visited);
    4176             :             }
    4177             :         }
    4178             : 
    4179          60 :       delete ppd.visited;
    4180             : 
    4181             :       /* Create the pack expansion type for the base type.  */
    4182          30 :       purpose = cxx_make_type (TYPE_PACK_EXPANSION);
    4183          30 :       PACK_EXPANSION_PATTERN (purpose) = TREE_PURPOSE (arg);
    4184          60 :       PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
    4185          30 :       PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
    4186             : 
    4187             :       /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
    4188             :          they will rarely be compared to anything.  */
    4189          30 :       SET_TYPE_STRUCTURAL_EQUALITY (purpose);
    4190             : 
    4191          30 :       return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
    4192             :     }
    4193             : 
    4194    38884751 :   if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
    4195    34361410 :     for_types = true;
    4196             : 
    4197             :   /* Build the PACK_EXPANSION_* node.  */
    4198    77769502 :   result = for_types
    4199    34361410 :      ? cxx_make_type (TYPE_PACK_EXPANSION)
    4200     4523341 :      : make_node (EXPR_PACK_EXPANSION);
    4201    38884751 :   PACK_EXPANSION_PATTERN (result) = arg;
    4202    38884751 :   if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
    4203             :     {
    4204             :       /* Propagate type and const-expression information.  */
    4205     4523341 :       TREE_TYPE (result) = TREE_TYPE (arg);
    4206     4523341 :       TREE_CONSTANT (result) = TREE_CONSTANT (arg);
    4207             :       /* Mark this read now, since the expansion might be length 0.  */
    4208     4523341 :       mark_exp_read (arg);
    4209             :     }
    4210             :   else
    4211             :     /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
    4212             :        they will rarely be compared to anything.  */
    4213    34361410 :     SET_TYPE_STRUCTURAL_EQUALITY (result);
    4214             : 
    4215             :   /* Determine which parameter packs will be expanded.  */
    4216    38884751 :   ppd.parameter_packs = &parameter_packs;
    4217    38884751 :   ppd.visited = new hash_set<tree>;
    4218    38884751 :   ppd.type_pack_expansion_p = TYPE_P (arg);
    4219    38884751 :   cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
    4220    77769502 :   delete ppd.visited;
    4221             : 
    4222             :   /* Make sure we found some parameter packs.  */
    4223    38884751 :   if (parameter_packs == NULL_TREE)
    4224             :     {
    4225          40 :       if (complain & tf_error)
    4226             :         {
    4227          40 :           if (TYPE_P (arg))
    4228           3 :             error ("expansion pattern %qT contains no parameter packs", arg);
    4229             :           else
    4230          37 :             error ("expansion pattern %qE contains no parameter packs", arg);
    4231             :         }
    4232          40 :       return error_mark_node;
    4233             :     }
    4234    73246118 :   PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
    4235             : 
    4236    38884711 :   PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
    4237    38884711 :   if (ppd.found_extra_args_tree_p)
    4238             :     /* If the pattern of this pack expansion contains a subtree that has
    4239             :        the extra args mechanism for avoiding partial instantiation, then
    4240             :        force this pack expansion to also use extra args.  Otherwise
    4241             :        partial instantiation of this pack expansion may not lower the
    4242             :        level of some parameter packs within the pattern, which would
    4243             :        confuse tsubst_pack_expansion later (PR101764).  */
    4244          16 :     PACK_EXPANSION_FORCE_EXTRA_ARGS_P (result) = true;
    4245             : 
    4246             :   return result;
    4247             : }
    4248             : 
    4249             : /* Checks T for any "bare" parameter packs, which have not yet been
    4250             :    expanded, and issues an error if any are found. This operation can
    4251             :    only be done on full expressions or types (e.g., an expression
    4252             :    statement, "if" condition, etc.), because we could have expressions like:
    4253             : 
    4254             :      foo(f(g(h(args)))...)
    4255             : 
    4256             :    where "args" is a parameter pack. check_for_bare_parameter_packs
    4257             :    should not be called for the subexpressions args, h(args),
    4258             :    g(h(args)), or f(g(h(args))), because we would produce erroneous
    4259             :    error messages.
    4260             : 
    4261             :    Returns TRUE and emits an error if there were bare parameter packs,
    4262             :    returns FALSE otherwise.  */
    4263             : bool
    4264  1146842110 : check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
    4265             : {
    4266  1146842110 :   tree parameter_packs = NULL_TREE;
    4267  1146842110 :   struct find_parameter_pack_data ppd;
    4268             : 
    4269  1146842110 :   if (!processing_template_decl || !t || t == error_mark_node)
    4270             :     return false;
    4271             : 
    4272   732447415 :   if (TREE_CODE (t) == TYPE_DECL)
    4273           0 :     t = TREE_TYPE (t);
    4274             : 
    4275   732447415 :   ppd.parameter_packs = &parameter_packs;
    4276   732447415 :   ppd.visited = new hash_set<tree>;
    4277   732447415 :   ppd.type_pack_expansion_p = false;
    4278   732447415 :   cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
    4279  1464894830 :   delete ppd.visited;
    4280             : 
    4281   732447415 :   if (!parameter_packs)
    4282             :     return false;
    4283             : 
    4284         312 :   if (loc == UNKNOWN_LOCATION)
    4285         306 :     loc = cp_expr_loc_or_input_loc (t);
    4286             : 
    4287             :   /* It's OK for a lambda to have an unexpanded parameter pack from the
    4288             :      containing context, but do complain about unexpanded capture packs.  */
    4289         312 :   tree lam = current_lambda_expr ();
    4290         312 :   if (lam)
    4291          61 :     lam = TREE_TYPE (lam);
    4292             : 
    4293          61 :   if (lam && lam != current_class_type)
    4294             :     {
    4295             :       /* We're in a lambda, but it isn't the innermost class.
    4296             :          This should work, but currently doesn't.  */
    4297           9 :       sorry_at (loc, "unexpanded parameter pack in local class in lambda");
    4298           9 :       return true;
    4299             :     }
    4300             : 
    4301         303 :   if (lam && CLASSTYPE_TEMPLATE_INFO (lam))
    4302          91 :     for (; parameter_packs;
    4303          42 :          parameter_packs = TREE_CHAIN (parameter_packs))
    4304             :       {
    4305          49 :         tree pack = TREE_VALUE (parameter_packs);
    4306          49 :         if (is_capture_proxy (pack)
    4307          49 :             || (TREE_CODE (pack) == PARM_DECL
    4308          16 :                 && DECL_CONTEXT (DECL_CONTEXT (pack)) == lam))
    4309             :           break;
    4310             :       }
    4311             : 
    4312         303 :   if (parameter_packs)
    4313             :     {
    4314         261 :       error_at (loc, "parameter packs not expanded with %<...%>:");
    4315         783 :       while (parameter_packs)
    4316             :         {
    4317         261 :           tree pack = TREE_VALUE (parameter_packs);
    4318         261 :           tree name = NULL_TREE;
    4319             : 
    4320         261 :           if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
    4321         261 :               || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
    4322         198 :             name = TYPE_NAME (pack);
    4323          63 :           else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
    4324          32 :             name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
    4325          31 :           else if (TREE_CODE (pack) == CALL_EXPR)
    4326           3 :             name = DECL_NAME (CALL_EXPR_FN (pack));
    4327             :           else
    4328          28 :             name = DECL_NAME (pack);
    4329             : 
    4330         261 :           if (name)
    4331         261 :             inform (loc, "        %qD", name);
    4332             :           else
    4333           0 :             inform (loc, "        %s", "<anonymous>");
    4334             : 
    4335         261 :           parameter_packs = TREE_CHAIN (parameter_packs);
    4336             :         }
    4337             : 
    4338             :       return true;
    4339             :     }
    4340             : 
    4341             :   return false;
    4342             : }
    4343             : 
    4344             : /* Expand any parameter packs that occur in the template arguments in
    4345             :    ARGS.  */
    4346             : tree
    4347   400945786 : expand_template_argument_pack (tree args)
    4348             : {
    4349   400945786 :   if (args == error_mark_node)
    4350             :     return error_mark_node;
    4351             : 
    4352   400945768 :   tree result_args = NULL_TREE;
    4353   801891536 :   int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
    4354   400945768 :   int num_result_args = -1;
    4355   400945768 :   int non_default_args_count = -1;
    4356             : 
    4357             :   /* First, determine if we need to expand anything, and the number of
    4358             :      slots we'll need.  */
    4359  1074949242 :   for (in_arg = 0; in_arg < nargs; ++in_arg)
    4360             :     {
    4361   674004545 :       tree arg = TREE_VEC_ELT (args, in_arg);
    4362   674004545 :       if (arg == NULL_TREE)
    4363             :         return args;
    4364   674003474 :       if (ARGUMENT_PACK_P (arg))
    4365             :         {
    4366    40925922 :           int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
    4367    40925922 :           if (num_result_args < 0)
    4368    40925922 :             num_result_args = in_arg + num_packed;
    4369             :           else
    4370           0 :             num_result_args += num_packed;
    4371             :         }
    4372             :       else
    4373             :         {
    4374   633077552 :           if (num_result_args >= 0)
    4375           0 :             num_result_args++;
    4376             :         }
    4377             :     }
    4378             : 
    4379             :   /* If no expansion is necessary, we're done.  */
    4380   400944697 :   if (num_result_args < 0)
    4381             :     return args;
    4382             : 
    4383             :   /* Expand arguments.  */
    4384    40925922 :   result_args = make_tree_vec (num_result_args);
    4385    40925922 :   if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
    4386    40442394 :     non_default_args_count =
    4387    40442394 :       GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
    4388   102033523 :   for (in_arg = 0; in_arg < nargs; ++in_arg)
    4389             :     {
    4390    61107601 :       tree arg = TREE_VEC_ELT (args, in_arg);
    4391    61107601 :       if (ARGUMENT_PACK_P (arg))
    4392             :         {
    4393    40925922 :           tree packed = ARGUMENT_PACK_ARGS (arg);
    4394    40925922 :           int i, num_packed = TREE_VEC_LENGTH (packed);
    4395   116552464 :           for (i = 0; i < num_packed; ++i, ++out_arg)
    4396    75626542 :             TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
    4397    40925922 :           if (non_default_args_count > 0)
    4398    40442390 :             non_default_args_count += num_packed - 1;
    4399             :         }
    4400             :       else
    4401             :         {
    4402    20181679 :           TREE_VEC_ELT (result_args, out_arg) = arg;
    4403    20181679 :           ++out_arg;
    4404             :         }
    4405             :     }
    4406    40925922 :   if (non_default_args_count >= 0)
    4407    40442394 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
    4408             :   return result_args;
    4409             : }
    4410             : 
    4411             : /* Checks if DECL shadows a template parameter.
    4412             : 
    4413             :    [temp.local]: A template-parameter shall not be redeclared within its
    4414             :    scope (including nested scopes).
    4415             : 
    4416             :    Emits an error and returns TRUE if the DECL shadows a parameter,
    4417             :    returns FALSE otherwise.  */
    4418             : 
    4419             : bool
    4420  1134676796 : check_template_shadow (tree decl)
    4421             : {
    4422  1134676796 :   tree olddecl;
    4423             : 
    4424             :   /* If we're not in a template, we can't possibly shadow a template
    4425             :      parameter.  */
    4426  1134676796 :   if (!current_template_parms)
    4427             :     return true;
    4428             : 
    4429             :   /* Figure out what we're shadowing.  */
    4430   544667903 :   decl = OVL_FIRST (decl);
    4431   544667903 :   olddecl = innermost_non_namespace_value (DECL_NAME (decl));
    4432             : 
    4433             :   /* If there's no previous binding for this name, we're not shadowing
    4434             :      anything, let alone a template parameter.  */
    4435   544667903 :   if (!olddecl)
    4436             :     return true;
    4437             : 
    4438             :   /* If we're not shadowing a template parameter, we're done.  Note
    4439             :      that OLDDECL might be an OVERLOAD (or perhaps even an
    4440             :      ERROR_MARK), so we can't just blithely assume it to be a _DECL
    4441             :      node.  */
    4442    40623039 :   if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
    4443             :     return true;
    4444             : 
    4445             :   /* We check for decl != olddecl to avoid bogus errors for using a
    4446             :      name inside a class.  We check TPFI to avoid duplicate errors for
    4447             :      inline member templates.  */
    4448         199 :   if (decl == olddecl
    4449         199 :       || (DECL_TEMPLATE_PARM_P (decl)
    4450          92 :           && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
    4451             :     return true;
    4452             : 
    4453             :   /* Don't complain about the injected class name, as we've already
    4454             :      complained about the class itself.  */
    4455         155 :   if (DECL_SELF_REFERENCE_P (decl))
    4456             :     return false;
    4457             : 
    4458         147 :   if (DECL_TEMPLATE_PARM_P (decl))
    4459          48 :     error ("declaration of template parameter %q+D shadows "
    4460             :            "template parameter", decl);
    4461             :   else
    4462          99 :     error ("declaration of %q+#D shadows template parameter", decl);
    4463         147 :   inform (DECL_SOURCE_LOCATION (olddecl),
    4464             :           "template parameter %qD declared here", olddecl);
    4465         147 :   return false;
    4466             : }
    4467             : 
    4468             : /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
    4469             :    ORIG_LEVEL, DECL, and TYPE.  */
    4470             : 
    4471             : static tree
    4472   127045176 : build_template_parm_index (int index,
    4473             :                            int level,
    4474             :                            int orig_level,
    4475             :                            tree decl,
    4476             :                            tree type)
    4477             : {
    4478   127045176 :   tree t = make_node (TEMPLATE_PARM_INDEX);
    4479   127045176 :   TEMPLATE_PARM_IDX (t) = index;
    4480   127045176 :   TEMPLATE_PARM_LEVEL (t) = level;
    4481   127045176 :   TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
    4482   127045176 :   TEMPLATE_PARM_DECL (t) = decl;
    4483   127045176 :   TREE_TYPE (t) = type;
    4484   127045176 :   TREE_CONSTANT (t) = TREE_CONSTANT (decl);
    4485   127045176 :   TREE_READONLY (t) = TREE_READONLY (decl);
    4486             : 
    4487   127045176 :   return t;
    4488             : }
    4489             : 
    4490             : struct ctp_hasher : ggc_ptr_hash<tree_node>
    4491             : {
    4492   632386855 :   static hashval_t hash (tree t)
    4493             :   {
    4494   632386855 :     ++comparing_specializations;
    4495   632386855 :     tree_code code = TREE_CODE (t);
    4496   632386855 :     hashval_t val = iterative_hash_object (code, 0);
    4497   632386855 :     val = iterative_hash_object (TEMPLATE_TYPE_LEVEL (t), val);
    4498   632386855 :     val = iterative_hash_object (TEMPLATE_TYPE_IDX (t), val);
    4499   632386855 :     if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
    4500    16147907 :       val = iterative_hash_template_arg (TYPE_TI_ARGS (t), val);
    4501   632386855 :     --comparing_specializations;
    4502   632386855 :     return val;
    4503             :   }
    4504             : 
    4505   929801016 :   static bool equal (tree t, tree u)
    4506             :   {
    4507   929801016 :     ++comparing_specializations;
    4508   929801016 :     bool eq = comptypes (t, u, COMPARE_STRUCTURAL);
    4509   929801016 :     --comparing_specializations;
    4510   929801016 :     return eq;
    4511             :   }
    4512             : };
    4513             : 
    4514             : static GTY (()) hash_table<ctp_hasher> *ctp_table;
    4515             : 
    4516             : /* Find the canonical type parameter for the given template type
    4517             :    parameter.  Returns the canonical type parameter, which may be TYPE
    4518             :    if no such parameter existed.  */
    4519             : 
    4520             : tree
    4521   117216403 : canonical_type_parameter (tree type)
    4522             : {
    4523   117216403 :   if (ctp_table == NULL)
    4524       89260 :     ctp_table = hash_table<ctp_hasher>::create_ggc (61);
    4525             : 
    4526   117216403 :   tree& slot = *ctp_table->find_slot (type, INSERT);
    4527   117216403 :   if (slot == NULL_TREE)
    4528     1155699 :     slot = type;
    4529   117216403 :   return slot;
    4530             : }
    4531             : 
    4532             : /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
    4533             :    TEMPLATE_PARM_LEVEL has been decreased by LEVELS.  If such a
    4534             :    TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
    4535             :    new one is created.  */
    4536             : 
    4537             : static tree
    4538    17803618 : reduce_template_parm_level (tree index, tree type, int levels, tree args,
    4539             :                             tsubst_flags_t complain)
    4540             : {
    4541    17803618 :   if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
    4542     8980550 :       || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
    4543     8980550 :           != TEMPLATE_PARM_LEVEL (index) - levels)
    4544    26782288 :       || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
    4545             :     {
    4546     9861760 :       tree orig_decl = TEMPLATE_PARM_DECL (index);
    4547             : 
    4548     9861760 :       tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
    4549     9861760 :                               TREE_CODE (orig_decl), DECL_NAME (orig_decl),
    4550             :                               type);
    4551     9861760 :       TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
    4552     9861760 :       TREE_READONLY (decl) = TREE_READONLY (orig_decl);
    4553     9861760 :       DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
    4554     9861760 :       DECL_ARTIFICIAL (decl) = 1;
    4555     9861760 :       SET_DECL_TEMPLATE_PARM_P (decl);
    4556             : 
    4557     9861760 :       tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
    4558     9861760 :                                             TEMPLATE_PARM_LEVEL (index) - levels,
    4559     9861760 :                                             TEMPLATE_PARM_ORIG_LEVEL (index),
    4560             :                                             decl, type);
    4561     9861760 :       TEMPLATE_PARM_DESCENDANTS (index) = tpi;
    4562    19723520 :       TEMPLATE_PARM_PARAMETER_PACK (tpi)
    4563     9861760 :         = TEMPLATE_PARM_PARAMETER_PACK (index);
    4564             : 
    4565             :       /* Template template parameters need this.  */
    4566     9861760 :       tree inner = decl;
    4567     9861760 :       if (TREE_CODE (decl) == TEMPLATE_DECL)
    4568             :         {
    4569        1517 :           inner = build_decl (DECL_SOURCE_LOCATION (decl),
    4570        1517 :                               TYPE_DECL, DECL_NAME (decl), type);
    4571        1517 :           DECL_TEMPLATE_RESULT (decl) = inner;
    4572        1517 :           DECL_ARTIFICIAL (inner) = true;
    4573        3034 :           DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
    4574        1517 :             (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
    4575             :         }
    4576             : 
    4577             :       /* Attach the TPI to the decl.  */
    4578     9861760 :       if (TREE_CODE (inner) == TYPE_DECL)
    4579     7975612 :         TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
    4580             :       else
    4581     1886148 :         DECL_INITIAL (decl) = tpi;
    4582             :     }
    4583             : 
    4584    17803618 :   return TEMPLATE_PARM_DESCENDANTS (index);
    4585             : }
    4586             : 
    4587             : /* Process information from new template parameter PARM and append it
    4588             :    to the LIST being built.  This new parameter is a non-type
    4589             :    parameter iff IS_NON_TYPE is true. This new parameter is a
    4590             :    parameter pack iff IS_PARAMETER_PACK is true.  The location of PARM
    4591             :    is in PARM_LOC.  */
    4592             : 
    4593             : tree
    4594   105745052 : process_template_parm (tree list, location_t parm_loc, tree parm,
    4595             :                        bool is_non_type, bool is_parameter_pack)
    4596             : {
    4597   105745052 :   gcc_assert (TREE_CODE (parm) == TREE_LIST);
    4598   105745052 :   tree prev = NULL_TREE;
    4599   105745052 :   int idx = 0;
    4600             : 
    4601   105745052 :   if (list)
    4602             :     {
    4603    50401310 :       prev = tree_last (list);
    4604             : 
    4605    50401310 :       tree p = TREE_VALUE (prev);
    4606    50401310 :       if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
    4607    46225105 :         idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
    4608     4176205 :       else if (TREE_CODE (p) == PARM_DECL)
    4609     4176180 :         idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
    4610             : 
    4611    50401310 :       ++idx;
    4612             :     }
    4613             : 
    4614   105745052 :   tree decl = NULL_TREE;
    4615   105745052 :   tree defval = TREE_PURPOSE (parm);
    4616   105745052 :   tree constr = TREE_TYPE (parm);
    4617             : 
    4618   105745052 :   if (is_non_type)
    4619             :     {
    4620     8588223 :       parm = TREE_VALUE (parm);
    4621             : 
    4622     8588223 :       SET_DECL_TEMPLATE_PARM_P (parm);
    4623             : 
    4624     8588223 :       if (TREE_TYPE (parm) != error_mark_node)
    4625             :         {
    4626             :           /* [temp.param]
    4627             : 
    4628             :              The top-level cv-qualifiers on the template-parameter are
    4629             :              ignored when determining its type.  */
    4630     8588149 :           TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
    4631     8588149 :           if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
    4632         127 :             TREE_TYPE (parm) = error_mark_node;
    4633     8588022 :           else if (uses_parameter_packs (TREE_TYPE (parm))
    4634          51 :                    && !is_parameter_pack
    4635             :                    /* If we're in a nested template parameter list, the template
    4636             :                       template parameter could be a parameter pack.  */
    4637     8588040 :                    && processing_template_parmlist == 1)
    4638             :             {
    4639             :               /* This template parameter is not a parameter pack, but it
    4640             :                  should be. Complain about "bare" parameter packs.  */
    4641          12 :               check_for_bare_parameter_packs (TREE_TYPE (parm));
    4642             : 
    4643             :               /* Recover by calling this a parameter pack.  */
    4644          12 :               is_parameter_pack = true;
    4645             :             }
    4646             :         }
    4647             : 
    4648             :       /* A template parameter is not modifiable.  */
    4649     8588223 :       TREE_CONSTANT (parm) = 1;
    4650     8588223 :       TREE_READONLY (parm) = 1;
    4651     8588223 :       decl = build_decl (parm_loc,
    4652     8588223 :                          CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
    4653     8588223 :       TREE_CONSTANT (decl) = 1;
    4654     8588223 :       TREE_READONLY (decl) = 1;
    4655     8588223 :       DECL_INITIAL (parm) = DECL_INITIAL (decl)
    4656    17176446 :         = build_template_parm_index (idx, current_template_depth,
    4657     8588223 :                                      current_template_depth,
    4658     8588223 :                                      decl, TREE_TYPE (parm));
    4659             : 
    4660     8588223 :       TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
    4661     8588223 :         = is_parameter_pack;
    4662             :     }
    4663             :   else
    4664             :     {
    4665    97156829 :       tree t;
    4666    97156829 :       parm = TREE_VALUE (TREE_VALUE (parm));
    4667             : 
    4668    97156829 :       if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
    4669             :         {
    4670      219086 :           t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
    4671             :           /* This is for distinguishing between real templates and template
    4672             :              template parameters */
    4673      219086 :           TREE_TYPE (parm) = t;
    4674             : 
    4675             :           /* any_template_parm_r expects to be able to get the targs of a
    4676             :              DECL_TEMPLATE_RESULT.  */
    4677      219086 :           tree result = DECL_TEMPLATE_RESULT (parm);
    4678      219086 :           TREE_TYPE (result) = t;
    4679      219086 :           tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
    4680      219086 :           tree tinfo = build_template_info (parm, args);
    4681      219086 :           retrofit_lang_decl (result);
    4682      219086 :           DECL_TEMPLATE_INFO (result) = tinfo;
    4683             : 
    4684      219086 :           decl = parm;
    4685      219086 :         }
    4686             :       else
    4687             :         {
    4688    96937743 :           t = cxx_make_type (TEMPLATE_TYPE_PARM);
    4689             :           /* parm is either IDENTIFIER_NODE or NULL_TREE.  */
    4690    96937743 :           decl = build_decl (parm_loc,
    4691             :                              TYPE_DECL, parm, t);
    4692             :         }
    4693             : 
    4694    97156829 :       TYPE_NAME (t) = decl;
    4695    97156829 :       TYPE_STUB_DECL (t) = decl;
    4696    97156829 :       parm = decl;
    4697    97156829 :       TEMPLATE_TYPE_PARM_INDEX (t)
    4698    97156829 :         = build_template_parm_index (idx, current_template_depth,
    4699    97156829 :                                      current_template_depth,
    4700    97156829 :                                      decl, TREE_TYPE (parm));
    4701    97156829 :       TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
    4702    97156829 :       TYPE_CANONICAL (t) = canonical_type_parameter (t);
    4703             :     }
    4704   105745052 :   DECL_ARTIFICIAL (decl) = 1;
    4705   105745052 :   SET_DECL_TEMPLATE_PARM_P (decl);
    4706             : 
    4707   105745052 :   if (TREE_CODE (parm) == TEMPLATE_DECL
    4708   105745052 :       && !uses_outer_template_parms (parm))
    4709      219051 :     TEMPLATE_TEMPLATE_PARM_SIMPLE_P (TREE_TYPE (parm)) = true;
    4710             : 
    4711             :   /* Build requirements for the type/template parameter.
    4712             :      This must be done after SET_DECL_TEMPLATE_PARM_P or
    4713             :      process_template_parm could fail. */
    4714   105745052 :   tree reqs = finish_shorthand_constraint (parm, constr);
    4715             : 
    4716   105745052 :   decl = pushdecl (decl);
    4717   105745052 :   if (!is_non_type)
    4718    97156829 :     parm = decl;
    4719             : 
    4720             :   /* Build the parameter node linking the parameter declaration,
    4721             :      its default argument (if any), and its constraints (if any). */
    4722   105745052 :   parm = build_tree_list (defval, parm);
    4723   105745052 :   TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
    4724             : 
    4725   105745052 :   if (prev)
    4726    50401310 :     TREE_CHAIN (prev) = parm;
    4727             :   else
    4728             :     list = parm;
    4729             : 
    4730   105745052 :   return list;
    4731             : }
    4732             : 
    4733             : /* The end of a template parameter list has been reached.  Process the
    4734             :    tree list into a parameter vector, converting each parameter into a more
    4735             :    useful form.  Type parameters are saved as IDENTIFIER_NODEs, and others
    4736             :    as PARM_DECLs.  */
    4737             : 
    4738             : tree
    4739    55219487 : end_template_parm_list (tree parms)
    4740             : {
    4741    55219487 :   tree saved_parmlist = make_tree_vec (list_length (parms));
    4742             : 
    4743             :   /* Pop the dummy parameter level and add the real one.  We do not
    4744             :      morph the dummy parameter in place, as it might have been
    4745             :      captured by a (nested) template-template-parm.  */
    4746    55219487 :   current_template_parms = TREE_CHAIN (current_template_parms);
    4747             : 
    4748   110438974 :   current_template_parms
    4749    55219487 :     = tree_cons (size_int (current_template_depth + 1),
    4750             :                  saved_parmlist, current_template_parms);
    4751             : 
    4752   160755354 :   for (unsigned ix = 0; parms; ix++)
    4753             :     {
    4754   105535867 :       tree parm = parms;
    4755   105535867 :       parms = TREE_CHAIN (parms);
    4756   105535867 :       TREE_CHAIN (parm) = NULL_TREE;
    4757             : 
    4758   105535867 :       TREE_VEC_ELT (saved_parmlist, ix) = parm;
    4759             :     }
    4760             : 
    4761    55219487 :   --processing_template_parmlist;
    4762             : 
    4763    55219487 :   return saved_parmlist;
    4764             : }
    4765             : 
    4766             : // Explicitly indicate the end of the template parameter list. We assume
    4767             : // that the current template parameters have been constructed and/or
    4768             : // managed explicitly, as when creating new template template parameters
    4769             : // from a shorthand constraint.
    4770             : void
    4771           7 : end_template_parm_list ()
    4772             : {
    4773           7 :   --processing_template_parmlist;
    4774           7 : }
    4775             : 
    4776             : /* end_template_decl is called after a template declaration is seen.  */
    4777             : 
    4778             : void
    4779    55343871 : end_template_decl (void)
    4780             : {
    4781    55343871 :   reset_specialization ();
    4782             : 
    4783    55343871 :   if (! processing_template_decl)
    4784             :     return;
    4785             : 
    4786             :   /* This matches the pushlevel in begin_template_parm_list.  */
    4787    55343871 :   finish_scope ();
    4788             : 
    4789    55343871 :   --processing_template_decl;
    4790    55343871 :   current_template_parms = TREE_CHAIN (current_template_parms);
    4791             : }
    4792             : 
    4793             : /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
    4794             :    thereof, and converts it into an argument suitable to be passed to
    4795             :    the type substitution functions.  Note that if the TREE_LIST contains
    4796             :    an error_mark node, the returned argument is error_mark_node.  */
    4797             : 
    4798             : tree
    4799   483535378 : template_parm_to_arg (tree t)
    4800             : {
    4801   483535378 :   if (!t)
    4802             :     return NULL_TREE;
    4803             : 
    4804   483535339 :   if (TREE_CODE (t) == TREE_LIST)
    4805   483231909 :     t = TREE_VALUE (t);
    4806             : 
    4807   483535339 :   if (error_operand_p (t))
    4808         583 :     return error_mark_node;
    4809             : 
    4810   483534756 :   if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
    4811             :     {
    4812   483378526 :       if (TREE_CODE (t) == TYPE_DECL
    4813    38882050 :           || TREE_CODE (t) == TEMPLATE_DECL)
    4814   445030797 :         t = TREE_TYPE (t);
    4815             :       else
    4816    38347729 :         t = DECL_INITIAL (t);
    4817             :     }
    4818             : 
    4819   483534756 :   gcc_assert (TEMPLATE_PARM_P (t));
    4820             : 
    4821   483534756 :   if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
    4822   483534756 :       || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
    4823             :     {
    4824   445183944 :       if (TEMPLATE_TYPE_PARAMETER_PACK (t))
    4825             :         {
    4826             :           /* Turn this argument into a TYPE_ARGUMENT_PACK
    4827             :              with a single element, which expands T.  */
    4828    16449004 :           tree vec = make_tree_vec (1);
    4829    16449004 :           if (CHECKING_P)
    4830    16449004 :             SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
    4831             : 
    4832    16449004 :           TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
    4833             : 
    4834    16449004 :           t = cxx_make_type (TYPE_ARGUMENT_PACK);
    4835    16449004 :           ARGUMENT_PACK_ARGS (t) = vec;
    4836             :         }
    4837             :     }
    4838             :   else
    4839             :     {
    4840    38350812 :       if (TEMPLATE_PARM_PARAMETER_PACK (t))
    4841             :         {
    4842             :           /* Turn this argument into a NONTYPE_ARGUMENT_PACK
    4843             :              with a single element, which expands T.  */
    4844      956820 :           tree vec = make_tree_vec (1);
    4845      956820 :           if (CHECKING_P)
    4846      956820 :             SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
    4847             : 
    4848      956820 :           t = convert_from_reference (t);
    4849      956820 :           TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
    4850             : 
    4851      956820 :           t  = make_node (NONTYPE_ARGUMENT_PACK);
    4852      956820 :           ARGUMENT_PACK_ARGS (t) = vec;
    4853             :         }
    4854             :       else
    4855    37393992 :         t = convert_from_reference (t);
    4856             :     }
    4857             :   return t;
    4858             : }
    4859             : 
    4860             : /* If T looks like a generic template argument produced by template_parm_to_arg,
    4861             :    return the corresponding template parameter, otherwise return NULL_TREE.  */
    4862             : 
    4863             : static tree
    4864   992953205 : template_arg_to_parm (tree t)
    4865             : {
    4866   992953205 :   if (t == NULL_TREE)
    4867             :     return NULL_TREE;
    4868             : 
    4869   992953205 :   if (ARGUMENT_PACK_P (t))
    4870             :     {
    4871   109777372 :       tree args = ARGUMENT_PACK_ARGS (t);
    4872   109777372 :       if (TREE_VEC_LENGTH (args) == 1
    4873   109777372 :           && PACK_EXPANSION_P (TREE_VEC_ELT (args, 0)))
    4874    57575497 :         t = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (args, 0));
    4875             :     }
    4876             : 
    4877   992953205 :   if (REFERENCE_REF_P (t))
    4878         427 :     t = TREE_OPERAND (t, 0);
    4879             : 
    4880   992953205 :   if (TEMPLATE_PARM_P (t))
    4881             :     return t;
    4882             :   else
    4883   223740909 :     return NULL_TREE;
    4884             : }
    4885             : 
    4886             : /* Given a single level of template parameters (a TREE_VEC), return it
    4887             :    as a set of template arguments.  */
    4888             : 
    4889             : tree
    4890   227792482 : template_parms_level_to_args (tree parms)
    4891             : {
    4892   227792482 :   parms = copy_node (parms);
    4893   227792482 :   TREE_TYPE (parms) = NULL_TREE;
    4894   710864159 :   for (tree& parm : tree_vec_range (parms))
    4895   483071677 :     parm = template_parm_to_arg (parm);
    4896             : 
    4897   227792482 :   if (CHECKING_P)
    4898   227792482 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (parms, TREE_VEC_LENGTH (parms));
    4899             : 
    4900   227792482 :   return parms;
    4901             : }
    4902             : 
    4903             : /* Given a set of template parameters, return them as a set of template
    4904             :    arguments.  The template parameters are represented as a TREE_VEC, in
    4905             :    the form documented in cp-tree.h for template arguments.  */
    4906             : 
    4907             : tree
    4908   186955752 : template_parms_to_args (tree parms)
    4909             : {
    4910   186955752 :   tree header;
    4911   186955752 :   tree args = NULL_TREE;
    4912   186955752 :   int length = TMPL_PARMS_DEPTH (parms);
    4913   186955752 :   int l = length;
    4914             : 
    4915             :   /* If there is only one level of template parameters, we do not
    4916             :      create a TREE_VEC of TREE_VECs.  Instead, we return a single
    4917             :      TREE_VEC containing the arguments.  */
    4918   186955752 :   if (length > 1)
    4919    27387070 :     args = make_tree_vec (length);
    4920             : 
    4921   401433177 :   for (header = parms; header; header = TREE_CHAIN (header))
    4922             :     {
    4923   214477425 :       tree a = template_parms_level_to_args (TREE_VALUE (header));
    4924             : 
    4925   214477425 :       if (length > 1)
    4926    54908743 :         TREE_VEC_ELT (args, --l) = a;
    4927             :       else
    4928             :         args = a;
    4929             :     }
    4930             : 
    4931   186955752 :   return args;
    4932             : }
    4933             : 
    4934             : /* Within the declaration of a template, return the currently active
    4935             :    template parameters as an argument TREE_VEC.  */
    4936             : 
    4937             : static tree
    4938   186159708 : current_template_args (void)
    4939             : {
    4940           0 :   return template_parms_to_args (current_template_parms);
    4941             : }
    4942             : 
    4943             : /* Return the fully generic arguments for of TMPL, i.e. what
    4944             :    current_template_args would be while parsing it.  */
    4945             : 
    4946             : tree
    4947     1780772 : generic_targs_for (tree tmpl)
    4948             : {
    4949     1780772 :   if (tmpl == NULL_TREE)
    4950             :     return NULL_TREE;
    4951     1780772 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
    4952     3561544 :       || DECL_TEMPLATE_SPECIALIZATION (tmpl))
    4953             :     /* DECL_TEMPLATE_RESULT doesn't have the arguments we want.  For a template
    4954             :        template parameter, it has no TEMPLATE_INFO; for a partial
    4955             :        specialization, it has the arguments for the primary template, and we
    4956             :        want the arguments for the partial specialization.  */;
    4957     1714829 :   else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
    4958     1714829 :     if (tree ti = get_template_info (result))
    4959     1313001 :       return TI_ARGS (ti);
    4960      467771 :   return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
    4961             : }
    4962             : 
    4963             : /* Return the template arguments corresponding to the template parameters of
    4964             :    TMPL's enclosing scope.  When TMPL is a member of a partial specialization,
    4965             :    this returns the arguments for the partial specialization as opposed to those
    4966             :    for the primary template, which is the main difference between this function
    4967             :    and simply using e.g. the TYPE_TI_ARGS of TMPL's DECL_CONTEXT.  */
    4968             : 
    4969             : tree
    4970        2954 : outer_template_args (tree tmpl)
    4971             : {
    4972        2954 :   tree ti = get_template_info (DECL_TEMPLATE_RESULT (tmpl));
    4973        2954 :   if (!ti)
    4974             :     return NULL_TREE;
    4975        2954 :   tree args = TI_ARGS (ti);
    4976        2954 :   if (!PRIMARY_TEMPLATE_P (tmpl))
    4977             :     return args;
    4978        5908 :   if (TMPL_ARGS_DEPTH (args) == 1)
    4979             :     return NULL_TREE;
    4980        1814 :   return strip_innermost_template_args (args, 1);
    4981             : }
    4982             : 
    4983             : /* Update the declared TYPE by doing any lookups which were thought to be
    4984             :    dependent, but are not now that we know the SCOPE of the declarator.  */
    4985             : 
    4986             : tree
    4987   109764996 : maybe_update_decl_type (tree orig_type, tree scope)
    4988             : {
    4989   109764996 :   tree type = orig_type;
    4990             : 
    4991   109764996 :   if (type == NULL_TREE)
    4992             :     return type;
    4993             : 
    4994   104583501 :   if (TREE_CODE (orig_type) == TYPE_DECL)
    4995    49609141 :     type = TREE_TYPE (type);
    4996             : 
    4997     5761901 :   if (scope && TYPE_P (scope) && dependent_type_p (scope)
    4998     4971994 :       && dependent_type_p (type)
    4999             :       /* Don't bother building up the args in this case.  */
    5000   107048277 :       && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
    5001             :     {
    5002             :       /* tsubst in the args corresponding to the template parameters,
    5003             :          including auto if present.  Most things will be unchanged, but
    5004             :          make_typename_type and tsubst_qualified_id will resolve
    5005             :          TYPENAME_TYPEs and SCOPE_REFs that were previously dependent.  */
    5006     1800414 :       tree args = current_template_args ();
    5007     1800414 :       tree auto_node = type_uses_auto (type);
    5008     1800414 :       tree pushed;
    5009     1800414 :       if (auto_node)
    5010             :         {
    5011           0 :           tree auto_vec = make_tree_vec (1);
    5012           0 :           TREE_VEC_ELT (auto_vec, 0) = auto_node;
    5013           0 :           args = add_to_template_args (args, auto_vec);
    5014             :         }
    5015     1800414 :       pushed = push_scope (scope);
    5016     1800414 :       type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
    5017     1800414 :       if (pushed)
    5018     1800410 :         pop_scope (scope);
    5019             :     }
    5020             : 
    5021   104583501 :   if (type == error_mark_node)
    5022             :     return orig_type;
    5023             : 
    5024   104582891 :   if (TREE_CODE (orig_type) == TYPE_DECL)
    5025             :     {
    5026    49609100 :       if (same_type_p (type, TREE_TYPE (orig_type)))
    5027             :         type = orig_type;
    5028             :       else
    5029       50916 :         type = TYPE_NAME (type);
    5030             :     }
    5031             :   return type;
    5032             : }
    5033             : 
    5034             : /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
    5035             :    template PARMS and constraints, CONSTR.  If MEMBER_TEMPLATE_P is true,
    5036             :    the new  template is a member template. */
    5037             : 
    5038             : static tree
    5039   108516309 : build_template_decl (tree decl, tree parms, bool member_template_p)
    5040             : {
    5041   108516309 :   gcc_checking_assert (TREE_CODE (decl) != TEMPLATE_DECL);
    5042             : 
    5043   108516309 :   tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
    5044   108516309 :   SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
    5045   108516309 :   DECL_TEMPLATE_PARMS (tmpl) = parms;
    5046   108516309 :   DECL_TEMPLATE_RESULT (tmpl) = decl;
    5047   108516309 :   DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
    5048   108516309 :   TREE_TYPE (tmpl) = TREE_TYPE (decl);
    5049   108516309 :   DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
    5050   108516309 :   DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
    5051             : 
    5052             :   /* Propagate module information from the decl.  */
    5053   108516309 :   DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl);
    5054             : 
    5055   108516309 :   return tmpl;
    5056             : }
    5057             : 
    5058             : struct template_parm_data
    5059             : {
    5060             :   /* The level of the template parameters we are currently
    5061             :      processing.  */
    5062             :   int level;
    5063             : 
    5064             :   /* The index of the specialization argument we are currently
    5065             :      processing.  */
    5066             :   int current_arg;
    5067             : 
    5068             :   /* An array whose size is the number of template parameters.  The
    5069             :      elements are nonzero if the parameter has been used in any one
    5070             :      of the arguments processed so far.  */
    5071             :   int* parms;
    5072             : 
    5073             :   /* An array whose size is the number of template arguments.  The
    5074             :      elements are nonzero if the argument makes use of template
    5075             :      parameters of this level.  */
    5076             :   int* arg_uses_template_parms;
    5077             : };
    5078             : 
    5079             : /* Subroutine of push_template_decl used to see if each template
    5080             :    parameter in a partial specialization is used in the explicit
    5081             :    argument list.  If T is of the LEVEL given in DATA (which is
    5082             :    treated as a template_parm_data*), then DATA->PARMS is marked
    5083             :    appropriately.  */
    5084             : 
    5085             : static int
    5086    10863642 : mark_template_parm (tree t, void* data)
    5087             : {
    5088    10863642 :   int level;
    5089    10863642 :   int idx;
    5090    10863642 :   struct template_parm_data* tpd = (struct template_parm_data*) data;
    5091             : 
    5092    10863642 :   template_parm_level_and_index (t, &level, &idx);
    5093             : 
    5094    10863642 :   if (level == tpd->level)
    5095             :     {
    5096    10854507 :       tpd->parms[idx] = 1;
    5097    10854507 :       tpd->arg_uses_template_parms[tpd->current_arg] = 1;
    5098             :     }
    5099             : 
    5100             :   /* In C++17 the type of a non-type argument is a deduced context.  */
    5101    10863642 :   if (cxx_dialect >= cxx17
    5102    10656361 :       && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    5103     1353355 :     for_each_template_parm (TREE_TYPE (t),
    5104             :                             &mark_template_parm,
    5105             :                             data,
    5106             :                             NULL,
    5107             :                             /*include_nondeduced_p=*/false);
    5108             : 
    5109             :   /* Return zero so that for_each_template_parm will continue the
    5110             :      traversal of the tree; we want to mark *every* template parm.  */
    5111    10863642 :   return 0;
    5112             : }
    5113             : 
    5114             : /* Process the partial specialization DECL.  */
    5115             : 
    5116             : static tree
    5117     5304561 : process_partial_specialization (tree decl)
    5118             : {
    5119     5304561 :   tree type = TREE_TYPE (decl);
    5120     5304561 :   tree tinfo = get_template_info (decl);
    5121     5304561 :   tree maintmpl = TI_TEMPLATE (tinfo);
    5122     5304561 :   tree specargs = TI_ARGS (tinfo);
    5123     5304561 :   tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
    5124     5304561 :   tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
    5125     5304561 :   tree inner_parms;
    5126     5304561 :   tree inst;
    5127     5304561 :   int nargs = TREE_VEC_LENGTH (inner_args);
    5128     5304561 :   int ntparms;
    5129     5304561 :   int  i;
    5130     5304561 :   bool did_error_intro = false;
    5131     5304561 :   struct template_parm_data tpd;
    5132     5304561 :   struct template_parm_data tpd2;
    5133             : 
    5134     5304561 :   gcc_assert (current_template_parms);
    5135             : 
    5136             :   /* A concept cannot be specialized.  */
    5137     5304561 :   if (flag_concepts && variable_concept_p (maintmpl))
    5138             :     {
    5139           1 :       error ("specialization of variable concept %q#D", maintmpl);
    5140           1 :       return error_mark_node;
    5141             :     }
    5142             : 
    5143     5304560 :   inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
    5144     5304560 :   ntparms = TREE_VEC_LENGTH (inner_parms);
    5145             : 
    5146             :   /* We check that each of the template parameters given in the
    5147             :      partial specialization is used in the argument list to the
    5148             :      specialization.  For example:
    5149             : 
    5150             :        template <class T> struct S;
    5151             :        template <class T> struct S<T*>;
    5152             : 
    5153             :      The second declaration is OK because `T*' uses the template
    5154             :      parameter T, whereas
    5155             : 
    5156             :        template <class T> struct S<int>;
    5157             : 
    5158             :      is no good.  Even trickier is:
    5159             : 
    5160             :        template <class T>
    5161             :        struct S1
    5162             :        {
    5163             :           template <class U>
    5164             :           struct S2;
    5165             :           template <class U>
    5166             :           struct S2<T>;
    5167             :        };
    5168             : 
    5169             :      The S2<T> declaration is actually invalid; it is a
    5170             :      full-specialization.  Of course,
    5171             : 
    5172             :           template <class U>
    5173             :           struct S2<T (*)(U)>;
    5174             : 
    5175             :      or some such would have been OK.  */
    5176     5304560 :   tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
    5177     5304560 :   tpd.parms = XALLOCAVEC (int, ntparms);
    5178     5304560 :   memset (tpd.parms, 0, sizeof (int) * ntparms);
    5179             : 
    5180     5304560 :   tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
    5181     5304560 :   memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
    5182    16321696 :   for (i = 0; i < nargs; ++i)
    5183             :     {
    5184    11017136 :       tpd.current_arg = i;
    5185    11017136 :       for_each_template_parm (TREE_VEC_ELT (inner_args, i),
    5186             :                               &mark_template_parm,
    5187             :                               &tpd,
    5188             :                               NULL,
    5189             :                               /*include_nondeduced_p=*/false);
    5190             :     }
    5191    15423677 :   for (i = 0; i < ntparms; ++i)
    5192    10119117 :     if (tpd.parms[i] == 0)
    5193             :       {
    5194             :         /* One of the template parms was not used in a deduced context in the
    5195             :            specialization.  */
    5196          57 :         if (!did_error_intro)
    5197             :           {
    5198          57 :             error ("template parameters not deducible in "
    5199             :                    "partial specialization:");
    5200          57 :             did_error_intro = true;
    5201             :           }
    5202             : 
    5203         114 :         inform (input_location, "        %qD",
    5204          57 :                 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
    5205             :       }
    5206             : 
    5207     5304560 :   if (did_error_intro)
    5208          57 :     return error_mark_node;
    5209             : 
    5210             :   /* [temp.class.spec]
    5211             : 
    5212             :      The argument list of the specialization shall not be identical to
    5213             :      the implicit argument list of the primary template.  */
    5214     5304503 :   tree main_args
    5215     5304503 :     = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
    5216     5304503 :   if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
    5217     5304503 :       && (!flag_concepts
    5218       42480 :           || !strictly_subsumes (current_template_constraints (), maintmpl)))
    5219             :     {
    5220          16 :       if (!flag_concepts)
    5221           9 :         error ("partial specialization %q+D does not specialize "
    5222             :                "any template arguments; to define the primary template, "
    5223             :                "remove the template argument list", decl);
    5224             :       else
    5225           7 :         error ("partial specialization %q+D does not specialize any "
    5226             :                "template arguments and is not more constrained than "
    5227             :                "the primary template; to define the primary template, "
    5228             :                "remove the template argument list", decl);
    5229          16 :       inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
    5230             :     }
    5231             : 
    5232             :   /* A partial specialization that replaces multiple parameters of the
    5233             :      primary template with a pack expansion is less specialized for those
    5234             :      parameters.  */
    5235     5304503 :   if (nargs < DECL_NTPARMS (maintmpl))
    5236             :     {
    5237           3 :       error ("partial specialization is not more specialized than the "
    5238             :              "primary template because it replaces multiple parameters "
    5239             :              "with a pack expansion");
    5240           3 :       inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
    5241             :       /* Avoid crash in process_partial_specialization.  */
    5242           3 :       return decl;
    5243             :     }
    5244             : 
    5245     5304500 :   else if (nargs > DECL_NTPARMS (maintmpl))
    5246             :     {
    5247           5 :       error ("too many arguments for partial specialization %qT", type);
    5248           5 :       inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
    5249             :       /* Avoid crash below.  */
    5250           5 :       return decl;
    5251             :     }
    5252             : 
    5253             :   /* If we aren't in a dependent class, we can actually try deduction.  */
    5254     5304495 :   else if (tpd.level == 1
    5255             :            /* FIXME we should be able to handle a partial specialization of a
    5256             :               partial instantiation, but currently we can't (c++/41727).  */
    5257    10263526 :            && TMPL_ARGS_DEPTH (specargs) == 1
    5258    10436251 :            && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
    5259             :     {
    5260          18 :       auto_diagnostic_group d;
    5261          18 :       if (pedwarn (input_location, 0,
    5262             :                    "partial specialization %qD is not more specialized than",
    5263             :                    decl))
    5264          18 :         inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
    5265             :                 maintmpl);
    5266          18 :     }
    5267             : 
    5268             :   /* [temp.spec.partial]
    5269             : 
    5270             :      The type of a template parameter corresponding to a specialized
    5271             :      non-type argument shall not be dependent on a parameter of the
    5272             :      specialization.
    5273             : 
    5274             :      Also, we verify that pack expansions only occur at the
    5275             :      end of the argument list.  */
    5276     5304495 :   tpd2.parms = 0;
    5277    16321538 :   for (i = 0; i < nargs; ++i)
    5278             :     {
    5279    11017043 :       tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
    5280    11017043 :       tree arg = TREE_VEC_ELT (inner_args, i);
    5281    11017043 :       tree packed_args = NULL_TREE;
    5282    11017043 :       int j, len = 1;
    5283             : 
    5284    11017043 :       if (ARGUMENT_PACK_P (arg))
    5285             :         {
    5286             :           /* Extract the arguments from the argument pack. We'll be
    5287             :              iterating over these in the following loop.  */
    5288      502360 :           packed_args = ARGUMENT_PACK_ARGS (arg);
    5289      502360 :           len = TREE_VEC_LENGTH (packed_args);
    5290             :         }
    5291             : 
    5292    22273598 :       for (j = 0; j < len; j++)
    5293             :         {
    5294    11256555 :           if (packed_args)
    5295             :             /* Get the Jth argument in the parameter pack.  */
    5296      741872 :             arg = TREE_VEC_ELT (packed_args, j);
    5297             : 
    5298    11256555 :           if (PACK_EXPANSION_P (arg))
    5299             :             {
    5300             :               /* Pack expansions must come at the end of the
    5301             :                  argument list.  */
    5302      309960 :               if ((packed_args && j < len - 1)
    5303           6 :                   || (!packed_args && i < nargs - 1))
    5304             :                 {
    5305          12 :                   if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
    5306           6 :                     error ("parameter pack argument %qE must be at the "
    5307             :                            "end of the template argument list", arg);
    5308             :                   else
    5309           6 :                     error ("parameter pack argument %qT must be at the "
    5310             :                            "end of the template argument list", arg);
    5311             :                 }
    5312             :             }
    5313             : 
    5314    11256555 :           if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
    5315             :             /* We only care about the pattern.  */
    5316       49946 :             arg = PACK_EXPANSION_PATTERN (arg);
    5317             : 
    5318    11256555 :           if (/* These first two lines are the `non-type' bit.  */
    5319    11256555 :               !TYPE_P (arg)
    5320     3359958 :               && TREE_CODE (arg) != TEMPLATE_DECL
    5321             :               /* This next two lines are the `argument expression is not just a
    5322             :                  simple identifier' condition and also the `specialized
    5323             :                  non-type argument' bit.  */
    5324     3169707 :               && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
    5325    13506894 :               && !((REFERENCE_REF_P (arg)
    5326     2250312 :                     || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
    5327          30 :                    && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
    5328             :             {
    5329             :               /* Look at the corresponding template parameter,
    5330             :                  marking which template parameters its type depends
    5331             :                  upon.  */
    5332     2250312 :               tree type = TREE_TYPE (parm);
    5333             : 
    5334     2250312 :               if (!tpd2.parms)
    5335             :                 {
    5336             :                   /* We haven't yet initialized TPD2.  Do so now.  */
    5337     1447399 :                   tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
    5338             :                   /* The number of parameters here is the number in the
    5339             :                      main template, which, as checked in the assertion
    5340             :                      above, is NARGS.  */
    5341     1447399 :                   tpd2.parms = XALLOCAVEC (int, nargs);
    5342     1447399 :                   tpd2.level =
    5343     1447399 :                     TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
    5344             :                 }
    5345             : 
    5346             :               /* Mark the template parameters.  But this time, we're
    5347             :                  looking for the template parameters of the main
    5348             :                  template, not in the specialization.  */
    5349     2250312 :               tpd2.current_arg = i;
    5350     2250312 :               tpd2.arg_uses_template_parms[i] = 0;
    5351     2250312 :               memset (tpd2.parms, 0, sizeof (int) * nargs);
    5352     2250312 :               for_each_template_parm (type,
    5353             :                                       &mark_template_parm,
    5354             :                                       &tpd2,
    5355             :                                       NULL,
    5356             :                                       /*include_nondeduced_p=*/false);
    5357             : 
    5358     2250312 :               if (tpd2.arg_uses_template_parms [i])
    5359             :                 {
    5360             :                   /* The type depended on some template parameters.
    5361             :                      If they are fully specialized in the
    5362             :                      specialization, that's OK.  */
    5363             :                   int j;
    5364             :                   int count = 0;
    5365         100 :                   for (j = 0; j < nargs; ++j)
    5366          69 :                     if (tpd2.parms[j] != 0
    5367          31 :                         && tpd.arg_uses_template_parms [j])
    5368          24 :                       ++count;
    5369          31 :                   if (count != 0)
    5370          24 :                     error_n (input_location, count,
    5371             :                              "type %qT of template argument %qE depends "
    5372             :                              "on a template parameter",
    5373             :                              "type %qT of template argument %qE depends "
    5374             :                              "on template parameters",
    5375             :                              type,
    5376             :                              arg);
    5377             :                 }
    5378             :             }
    5379             :         }
    5380             :     }
    5381             : 
    5382             :   /* We should only get here once.  */
    5383     5304495 :   if (TREE_CODE (decl) == TYPE_DECL)
    5384     4938868 :     gcc_assert (!COMPLETE_TYPE_P (type));
    5385             : 
    5386             :   // Build the template decl.
    5387     5304495 :   tree tmpl = build_template_decl (decl, current_template_parms,
    5388     5304495 :                                    DECL_MEMBER_TEMPLATE_P (maintmpl));
    5389     5304495 :   SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
    5390     5304495 :   DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
    5391     5304495 :   DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
    5392             : 
    5393             :   /* Give template template parms a DECL_CONTEXT of the template
    5394             :      for which they are a parameter.  */
    5395    15423533 :   for (i = 0; i < ntparms; ++i)
    5396             :     {
    5397    10119038 :       tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
    5398    10119038 :       if (TREE_CODE (parm) == TEMPLATE_DECL)
    5399       77218 :         DECL_CONTEXT (parm) = tmpl;
    5400             :     }
    5401             : 
    5402     5304495 :   if (VAR_P (decl))
    5403             :     /* We didn't register this in check_explicit_specialization so we could
    5404             :        wait until the constraints were set.  */
    5405      365627 :     decl = register_specialization (decl, maintmpl, specargs, false, 0);
    5406             :   else
    5407     4938868 :     associate_classtype_constraints (type);
    5408             : 
    5409    10608990 :   DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
    5410     5304495 :     = tree_cons (specargs, tmpl,
    5411     5304495 :                  DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
    5412     5304495 :   TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
    5413             :   /* Link the DECL_TEMPLATE_RESULT back to the partial TEMPLATE_DECL.  */
    5414     5304495 :   gcc_checking_assert (!TI_PARTIAL_INFO (tinfo));
    5415     5304495 :   TI_PARTIAL_INFO (tinfo) = build_template_info (tmpl, NULL_TREE);
    5416             : 
    5417    34254262 :   for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
    5418    28949767 :        inst = TREE_CHAIN (inst))
    5419             :     {
    5420    28949767 :       tree instance = TREE_VALUE (inst);
    5421    29708430 :       if (TYPE_P (instance)
    5422    28949767 :           ? (COMPLETE_TYPE_P (instance)
    5423    28191104 :              && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
    5424      758663 :           : DECL_TEMPLATE_INSTANTIATION (instance))
    5425             :         {
    5426       62507 :           tree partial_ti = most_specialized_partial_spec (instance, tf_none,
    5427             :                                                            /*rechecking=*/true);
    5428       62507 :           tree inst_decl = (DECL_P (instance)
    5429       62507 :                             ? instance : TYPE_NAME (instance));
    5430       62507 :           if (!partial_ti)
    5431             :             /* OK */;
    5432       33734 :           else if (partial_ti == error_mark_node)
    5433           4 :             permerror (input_location,
    5434             :                        "declaration of %qD ambiguates earlier template "
    5435             :                        "instantiation for %qD", decl, inst_decl);
    5436       33730 :           else if (TI_TEMPLATE (partial_ti) == tmpl)
    5437           7 :             permerror (input_location,
    5438             :                        "partial specialization of %qD after instantiation "
    5439             :                        "of %qD", decl, inst_decl);
    5440             :         }
    5441             :     }
    5442             : 
    5443             :   return decl;
    5444             : }
    5445             : 
    5446             : /* PARM is a template parameter of some form; return the corresponding
    5447             :    TEMPLATE_PARM_INDEX.  */
    5448             : 
    5449             : static tree
    5450   225788815 : get_template_parm_index (tree parm)
    5451             : {
    5452   225788815 :   if (TREE_CODE (parm) == PARM_DECL
    5453   225788815 :       || TREE_CODE (parm) == CONST_DECL)
    5454   114650625 :     parm = DECL_INITIAL (parm);
    5455   111138190 :   else if (TREE_CODE (parm) == TYPE_DECL
    5456     4638635 :            || TREE_CODE (parm) == TEMPLATE_DECL)
    5457   106503561 :     parm = TREE_TYPE (parm);
    5458   225788815 :   if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
    5459   225788815 :       || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
    5460   115168357 :       || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
    5461   110645358 :     parm = TEMPLATE_TYPE_PARM_INDEX (parm);
    5462   225788815 :   gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
    5463   225788815 :   return parm;
    5464             : }
    5465             : 
    5466             : /* Subroutine of fixed_parameter_pack_p below.  Look for any template
    5467             :    parameter packs used by the template parameter PARM.  */
    5468             : 
    5469             : static void
    5470         561 : fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
    5471             : {
    5472             :   /* A type parm can't refer to another parm.  */
    5473         561 :   if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
    5474             :     return;
    5475         546 :   else if (TREE_CODE (parm) == PARM_DECL)
    5476             :     {
    5477         492 :       cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
    5478             :                     ppd, ppd->visited);
    5479         492 :       return;
    5480             :     }
    5481             : 
    5482          54 :   gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
    5483             : 
    5484          54 :   tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
    5485         117 :   for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
    5486             :     {
    5487          63 :       tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
    5488          63 :       if (template_parameter_pack_p (p))
    5489             :         /* Any packs in the type are expanded by this parameter.  */;
    5490             :       else
    5491          33 :         fixed_parameter_pack_p_1 (p, ppd);
    5492             :     }
    5493             : }
    5494             : 
    5495             : /* PARM is a template parameter pack.  Return any parameter packs used in
    5496             :    its type or the type of any of its template parameters.  If there are
    5497             :    any such packs, it will be instantiated into a fixed template parameter
    5498             :    list by partial instantiation rather than be fully deduced.  */
    5499             : 
    5500             : tree
    5501   109388755 : fixed_parameter_pack_p (tree parm)
    5502             : {
    5503             :   /* This can only be true in a member template.  */
    5504   109388755 :   if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
    5505             :     return NULL_TREE;
    5506             :   /* This can only be true for a parameter pack.  */
    5507     4202907 :   if (!template_parameter_pack_p (parm))
    5508             :     return NULL_TREE;
    5509             :   /* A type parm can't refer to another parm.  */
    5510     4202907 :   if (TREE_CODE (parm) == TYPE_DECL)
    5511             :     return NULL_TREE;
    5512             : 
    5513         528 :   tree parameter_packs = NULL_TREE;
    5514         528 :   struct find_parameter_pack_data ppd;
    5515         528 :   ppd.parameter_packs = &parameter_packs;
    5516         528 :   ppd.visited = new hash_set<tree>;
    5517         528 :   ppd.type_pack_expansion_p = false;
    5518             : 
    5519         528 :   fixed_parameter_pack_p_1 (parm, &ppd);
    5520             : 
    5521        1056 :   delete ppd.visited;
    5522         528 :   return parameter_packs;
    5523             : }
    5524             : 
    5525             : /* Check that a template declaration's use of default arguments and
    5526             :    parameter packs is not invalid.  Here, PARMS are the template
    5527             :    parameters.  IS_PRIMARY is true if DECL is the thing declared by
    5528             :    a primary template.  IS_PARTIAL is true if DECL is a partial
    5529             :    specialization.
    5530             : 
    5531             :    IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
    5532             :    function template declaration or a friend class template
    5533             :    declaration.  In the function case, 1 indicates a declaration, 2
    5534             :    indicates a redeclaration.  When IS_FRIEND_DECL=2, no errors are
    5535             :    emitted for extraneous default arguments.
    5536             : 
    5537             :    Returns TRUE if there were no errors found, FALSE otherwise. */
    5538             : 
    5539             : bool
    5540   148523044 : check_default_tmpl_args (tree decl, tree parms, bool is_primary,
    5541             :                          bool is_partial, int is_friend_decl)
    5542             : {
    5543   148523044 :   const char *msg;
    5544   148523044 :   int last_level_to_check;
    5545   148523044 :   tree parm_level;
    5546   148523044 :   bool no_errors = true;
    5547             : 
    5548             :   /* [temp.param]
    5549             : 
    5550             :      A default template-argument shall not be specified in a
    5551             :      function template declaration or a function template definition, nor
    5552             :      in the template-parameter-list of the definition of a member of a
    5553             :      class template.  */
    5554             : 
    5555   148523044 :   if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
    5556   148523044 :       || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
    5557             :     /* You can't have a function template declaration in a local
    5558             :        scope, nor you can you define a member of a class template in a
    5559             :        local scope.  */
    5560             :     return true;
    5561             : 
    5562   148210630 :   if ((TREE_CODE (decl) == TYPE_DECL
    5563    40173998 :        && TREE_TYPE (decl)
    5564    69441365 :        && LAMBDA_TYPE_P (TREE_TYPE (decl)))
    5565   188179019 :       || (TREE_CODE (decl) == FUNCTION_DECL
    5566   109174214 :           && LAMBDA_FUNCTION_P (decl)))
    5567             :     /* A lambda doesn't have an explicit declaration; don't complain
    5568             :        about the parms of the enclosing class.  */
    5569             :     return true;
    5570             : 
    5571   147589159 :   if (current_class_type
    5572   114184755 :       && !TYPE_BEING_DEFINED (current_class_type)
    5573    40338286 :       && DECL_LANG_SPECIFIC (decl)
    5574    40294925 :       && DECL_DECLARES_FUNCTION_P (decl)
    5575             :       /* If this is either a friend defined in the scope of the class
    5576             :          or a member function.  */
    5577    83143335 :       && (DECL_FUNCTION_MEMBER_P (decl)
    5578    40059649 :           ? same_type_p (DECL_CONTEXT (decl), current_class_type)
    5579           0 :           : DECL_FRIEND_CONTEXT (decl)
    5580           0 :           ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
    5581             :           : false)
    5582             :       /* And, if it was a member function, it really was defined in
    5583             :          the scope of the class.  */
    5584   227708457 :       && (!DECL_FUNCTION_MEMBER_P (decl)
    5585    40059649 :           || DECL_INITIALIZED_IN_CLASS_P (decl)))
    5586             :     /* We already checked these parameters when the template was
    5587             :        declared, so there's no need to do it again now.  This function
    5588             :        was defined in class scope, but we're processing its body now
    5589             :        that the class is complete.  */
    5590             :     return true;
    5591             : 
    5592             :   /* Core issue 226 (C++0x only): the following only applies to class
    5593             :      templates.  */
    5594   112901249 :   if (is_primary
    5595    46864795 :       && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
    5596             :     {
    5597             :       /* [temp.param]
    5598             : 
    5599             :          If a template-parameter has a default template-argument, all
    5600             :          subsequent template-parameters shall have a default
    5601             :          template-argument supplied.  */
    5602    35062487 :       for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
    5603             :         {
    5604    18332371 :           tree inner_parms = TREE_VALUE (parm_level);
    5605    18332371 :           int ntparms = TREE_VEC_LENGTH (inner_parms);
    5606    18332371 :           int seen_def_arg_p = 0;
    5607    18332371 :           int i;
    5608             : 
    5609    50967740 :           for (i = 0; i < ntparms; ++i)
    5610             :             {
    5611    32635369 :               tree parm = TREE_VEC_ELT (inner_parms, i);
    5612             : 
    5613    32635369 :               if (parm == error_mark_node)
    5614           0 :                 continue;
    5615             : 
    5616    32635369 :               if (TREE_PURPOSE (parm))
    5617             :                 seen_def_arg_p = 1;
    5618    29651718 :               else if (seen_def_arg_p
    5619    29651718 :                        && !template_parameter_pack_p (TREE_VALUE (parm)))
    5620             :                 {
    5621          27 :                   error ("no default argument for %qD", TREE_VALUE (parm));
    5622             :                   /* For better subsequent error-recovery, we indicate that
    5623             :                      there should have been a default argument.  */
    5624          27 :                   TREE_PURPOSE (parm) = error_mark_node;
    5625          27 :                   no_errors = false;
    5626             :                 }
    5627    29651691 :               else if (!is_partial
    5628    29651691 :                        && !is_friend_decl
    5629             :                        /* Don't complain about an enclosing partial
    5630             :                           specialization.  */
    5631    18371512 :                        && parm_level == parms
    5632    16332359 :                        && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
    5633    15807064 :                        && i < ntparms - 1
    5634     6947183 :                        && template_parameter_pack_p (TREE_VALUE (parm))
    5635             :                        /* A fixed parameter pack will be partially
    5636             :                           instantiated into a fixed length list.  */
    5637    29651727 :                        && !fixed_parameter_pack_p (TREE_VALUE (parm)))
    5638             :                 {
    5639             :                   /* A primary class template, primary variable template
    5640             :                      (DR 2032), or alias template can only have one
    5641             :                      parameter pack, at the end of the template
    5642             :                      parameter list.  */
    5643             : 
    5644          21 :                   error ("parameter pack %q+D must be at the end of the"
    5645          21 :                          " template parameter list", TREE_VALUE (parm));
    5646             : 
    5647          21 :                   TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
    5648          21 :                     = error_mark_node;
    5649          21 :                   no_errors = false;
    5650             :                 }
    5651             :             }
    5652             :         }
    5653             :     }
    5654             : 
    5655   112901249 :   if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
    5656             :       || is_partial
    5657   112592150 :       || !is_primary
    5658    41444126 :       || is_friend_decl)
    5659             :     /* For an ordinary class template, default template arguments are
    5660             :        allowed at the innermost level, e.g.:
    5661             :          template <class T = int>
    5662             :          struct S {};
    5663             :        but, in a partial specialization, they're not allowed even
    5664             :        there, as we have in [temp.class.spec]:
    5665             : 
    5666             :          The template parameter list of a specialization shall not
    5667             :          contain default template argument values.
    5668             : 
    5669             :        So, for a partial specialization, or for a function template
    5670             :        (in C++98/C++03), we look at all of them.  */
    5671             :     ;
    5672             :   else
    5673             :     /* But, for a primary class template that is not a partial
    5674             :        specialization we look at all template parameters except the
    5675             :        innermost ones.  */
    5676    40374408 :     parms = TREE_CHAIN (parms);
    5677             : 
    5678             :   /* Figure out what error message to issue.  */
    5679   112901249 :   if (is_friend_decl == 2)
    5680             :     msg = G_("default template arguments may not be used in function template "
    5681             :              "friend re-declaration");
    5682   112748781 :   else if (is_friend_decl)
    5683             :     msg = G_("default template arguments may not be used in template "
    5684             :              "friend declarations");
    5685   111831531 :   else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
    5686             :     msg = G_("default template arguments may not be used in function templates "
    5687             :              "without %<-std=c++11%> or %<-std=gnu++11%>");
    5688   111532445 :   else if (is_partial)
    5689             :     msg = G_("default template arguments may not be used in "
    5690             :              "partial specializations");
    5691   106227848 :   else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
    5692             :     msg = G_("default argument for template parameter for class enclosing %qD");
    5693             :   else
    5694             :     /* Per [temp.param]/9, "A default template-argument shall not be
    5695             :        specified in the template-parameter-lists of the definition of
    5696             :        a member of a class template that appears outside of the member's
    5697             :        class.", thus if we aren't handling a member of a class template
    5698             :        there is no need to examine the parameters.  */
    5699             :     return true;
    5700             : 
    5701    63831460 :   if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
    5702             :     /* If we're inside a class definition, there's no need to
    5703             :        examine the parameters to the class itself.  On the one
    5704             :        hand, they will be checked when the class is defined, and,
    5705             :        on the other, default arguments are valid in things like:
    5706             :          template <class T = double>
    5707             :          struct S { template <class U> void f(U); };
    5708             :        Here the default argument for `S' has no bearing on the
    5709             :        declaration of `f'.  */
    5710    53336878 :     last_level_to_check = template_class_depth (current_class_type) + 1;
    5711             :   else
    5712             :     /* Check everything.  */
    5713             :     last_level_to_check = 0;
    5714             : 
    5715    63831460 :   for (parm_level = parms;
    5716   143166826 :        parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
    5717    13162293 :        parm_level = TREE_CHAIN (parm_level))
    5718             :     {
    5719    13162296 :       tree inner_parms = TREE_VALUE (parm_level);
    5720    13162296 :       int i;
    5721    13162296 :       int ntparms;
    5722             : 
    5723    13162296 :       ntparms = TREE_VEC_LENGTH (inner_parms);
    5724    41352892 :       for (i = 0; i < ntparms; ++i)
    5725             :         {
    5726    28190599 :           if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
    5727           0 :             continue;
    5728             : 
    5729    28190599 :           if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
    5730             :             {
    5731          36 :               if (msg)
    5732             :                 {
    5733          36 :                   no_errors = false;
    5734          36 :                   if (is_friend_decl == 2)
    5735             :                     return no_errors;
    5736             : 
    5737          33 :                   error (msg, decl);
    5738          33 :                   msg = 0;
    5739             :                 }
    5740             : 
    5741             :               /* Clear out the default argument so that we are not
    5742             :                  confused later.  */
    5743          33 :               TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
    5744             :             }
    5745             :         }
    5746             : 
    5747             :       /* At this point, if we're still interested in issuing messages,
    5748             :          they must apply to classes surrounding the object declared.  */
    5749    13162293 :       if (msg)
    5750    13162259 :         msg = G_("default argument for template parameter for class "
    5751             :                  "enclosing %qD");
    5752             :     }
    5753             : 
    5754             :   return no_errors;
    5755             : }
    5756             : 
    5757             : /* Worker for push_template_decl_real, called via
    5758             :    for_each_template_parm.  DATA is really an int, indicating the
    5759             :    level of the parameters we are interested in.  If T is a template
    5760             :    parameter of that level, return nonzero.  */
    5761             : 
    5762             : static int
    5763     2235630 : template_parm_this_level_p (tree t, void* data)
    5764             : {
    5765     2235630 :   int this_level = *(int *)data;
    5766     2235630 :   int level;
    5767             : 
    5768     2235630 :   if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    5769      273480 :     level = TEMPLATE_PARM_LEVEL (t);
    5770             :   else
    5771     1962150 :     level = TEMPLATE_TYPE_LEVEL (t);
    5772     2235630 :   return level == this_level;
    5773             : }
    5774             : 
    5775             : /* Worker for uses_outer_template_parms, called via for_each_template_parm.
    5776             :    DATA is really an int, indicating the innermost outer level of parameters.
    5777             :    If T is a template parameter of that level or further out, return
    5778             :    nonzero.  */
    5779             : 
    5780             : static int
    5781     6057704 : template_parm_outer_level (tree t, void *data)
    5782             : {
    5783     6057704 :   int this_level = *(int *)data;
    5784     6057704 :   int level;
    5785             : 
    5786     6057704 :   if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    5787      169141 :     level = TEMPLATE_PARM_LEVEL (t);
    5788             :   else
    5789     5888563 :     level = TEMPLATE_TYPE_LEVEL (t);
    5790     6057704 :   return level <= this_level;
    5791             : }
    5792             : 
    5793             : /* Creates a TEMPLATE_DECL for the indicated DECL using the template
    5794             :    parameters given by current_template_args, or reuses a
    5795             :    previously existing one, if appropriate.  Returns the DECL, or an
    5796             :    equivalent one, if it is replaced via a call to duplicate_decls.
    5797             : 
    5798             :    If IS_FRIEND is true, DECL is a friend declaration.  */
    5799             : 
    5800             : tree
    5801   189362611 : push_template_decl (tree decl, bool is_friend)
    5802             : {
    5803   189362611 :   if (decl == error_mark_node || !current_template_parms)
    5804             :     return error_mark_node;
    5805             : 
    5806             :   /* See if this is a partial specialization.  */
    5807    47724338 :   bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
    5808    13190564 :                       && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
    5809    12960213 :                       && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
    5810   232147957 :                      || (VAR_P (decl)
    5811    34334623 :                          && DECL_LANG_SPECIFIC (decl)
    5812     3353334 :                          && DECL_TEMPLATE_SPECIALIZATION (decl)
    5813      365645 :                          && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
    5814             : 
    5815             :   /* No surprising friend functions.  */
    5816   189362585 :   gcc_checking_assert (is_friend
    5817             :                        || !(TREE_CODE (decl) == FUNCTION_DECL
    5818             :                             && DECL_UNIQUE_FRIEND_P (decl)));
    5819             : 
    5820   189362585 :   tree ctx;
    5821   189362585 :   if (is_friend)
    5822             :     /* For a friend, we want the context of the friend, not
    5823             :        the type of which it is a friend.  */
    5824     3574319 :     ctx = CP_DECL_CONTEXT (decl);
    5825   185788266 :   else if (CP_DECL_CONTEXT (decl)
    5826   185788266 :            && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
    5827             :     /* In the case of a virtual function, we want the class in which
    5828             :        it is defined.  */
    5829   152725629 :     ctx = CP_DECL_CONTEXT (decl);
    5830             :   else
    5831             :     /* Otherwise, if we're currently defining some class, the DECL
    5832             :        is assumed to be a member of the class.  */
    5833    33062637 :     ctx = current_scope ();
    5834             : 
    5835   189362585 :   if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
    5836    36621377 :     ctx = NULL_TREE;
    5837             : 
    5838   189362585 :   if (!DECL_CONTEXT (decl))
    5839       17537 :     DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
    5840             : 
    5841             :   /* See if this is a primary template.  */
    5842   189362585 :   bool is_primary = false;
    5843   189362585 :   if (is_friend && ctx
    5844   189362585 :       && uses_template_parms_level (ctx, current_template_depth))
    5845             :     /* A friend template that specifies a class context, i.e.
    5846             :          template <typename T> friend void A<T>::f();
    5847             :        is not primary.  */
    5848             :     ;
    5849   222136492 :   else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
    5850             :     /* Lambdas are not primary.  */
    5851             :     ;
    5852             :   else
    5853   188951764 :     is_primary = template_parm_scope_p ();
    5854             : 
    5855             :   /* True if the template is a member template, in the sense of
    5856             :      [temp.mem].  */
    5857   188951764 :   bool member_template_p = false;
    5858             : 
    5859   188951764 :   if (is_primary)
    5860             :     {
    5861    47005831 :       warning (OPT_Wtemplates, "template %qD declared", decl);
    5862             : 
    5863    47005831 :       if (DECL_CLASS_SCOPE_P (decl))
    5864             :         member_template_p = true;
    5865             : 
    5866    47005831 :       if (TREE_CODE (decl) == TYPE_DECL
    5867    47005831 :           && IDENTIFIER_ANON_P (DECL_NAME (decl)))
    5868             :         {
    5869           8 :           error ("template class without a name");
    5870           8 :           return error_mark_node;
    5871             :         }
    5872    47005823 :       else if (TREE_CODE (decl) == FUNCTION_DECL)
    5873             :         {
    5874    30610885 :           if (member_template_p)
    5875             :             {
    5876    10948416 :               if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
    5877           6 :                 error ("member template %qD may not have virt-specifiers", decl);
    5878             :             }
    5879    61221770 :           if (DECL_DESTRUCTOR_P (decl))
    5880             :             {
    5881             :               /* [temp.mem]
    5882             : 
    5883             :                  A destructor shall not be a member template.  */
    5884           8 :               error_at (DECL_SOURCE_LOCATION (decl),
    5885             :                         "destructor %qD declared as member template", decl);
    5886           8 :               return error_mark_node;
    5887             :             }
    5888    38598302 :           if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
    5889    30610907 :               && (!prototype_p (TREE_TYPE (decl))
    5890          30 :                   || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
    5891          30 :                   || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
    5892          30 :                   || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
    5893             :                       == void_list_node)))
    5894             :             {
    5895             :               /* [basic.stc.dynamic.allocation]
    5896             : 
    5897             :                  An allocation function can be a function
    5898             :                  template. ... Template allocation functions shall
    5899             :                  have two or more parameters.  */
    5900          12 :               error ("invalid template declaration of %qD", decl);
    5901          12 :               return error_mark_node;
    5902             :             }
    5903             :         }
    5904    14625170 :       else if (DECL_IMPLICIT_TYPEDEF_P (decl)
    5905    28503171 :                && CLASS_TYPE_P (TREE_TYPE (decl)))
    5906             :         /* Class template.  */;
    5907     4286712 :       else if (TREE_CODE (decl) == TYPE_DECL
    5908     4286712 :                && TYPE_DECL_ALIAS_P (decl))
    5909             :         /* alias-declaration */
    5910     2516934 :         gcc_assert (!DECL_ARTIFICIAL (decl));
    5911     1769778 :       else if (VAR_P (decl))
    5912             :         /* C++14 variable template. */;
    5913      200435 :       else if (TREE_CODE (decl) == CONCEPT_DECL)
    5914             :         /* C++20 concept definitions.  */;
    5915             :       else
    5916             :         {
    5917          10 :           error ("template declaration of %q#D", decl);
    5918          10 :           return error_mark_node;
    5919             :         }
    5920             :     }
    5921             : 
    5922    47724320 :   bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl)
    5923   223896318 :                   && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL)
    5924   137618006 :                       || (VAR_OR_FUNCTION_DECL_P (decl)
    5925   110340804 :                           && DECL_LOCAL_DECL_P (decl))));
    5926             : 
    5927             :   /* Check to see that the rules regarding the use of default
    5928             :      arguments are not being violated.  We check args for a friend
    5929             :      functions when we know whether it's a definition, introducing
    5930             :      declaration or re-declaration.  */
    5931   150808555 :   if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL))
    5932   147455461 :     check_default_tmpl_args (decl, current_template_parms,
    5933             :                              is_primary, is_partial, is_friend);
    5934             : 
    5935             :   /* Ensure that there are no parameter packs in the type of this
    5936             :      declaration that have not been expanded.  */
    5937   189362547 :   if (TREE_CODE (decl) == FUNCTION_DECL)
    5938             :     {
    5939             :       /* Check each of the arguments individually to see if there are
    5940             :          any bare parameter packs.  */
    5941   107103179 :       tree type = TREE_TYPE (decl);
    5942   107103179 :       tree arg = DECL_ARGUMENTS (decl);
    5943   107103179 :       tree argtype = TYPE_ARG_TYPES (type);
    5944             : 
    5945   344210952 :       while (arg && argtype)
    5946             :         {
    5947   237107773 :           if (!DECL_PACK_P (arg)
    5948   471141647 :               && check_for_bare_parameter_packs (TREE_TYPE (arg)))
    5949             :             {
    5950             :             /* This is a PARM_DECL that contains unexpanded parameter
    5951             :                packs. We have already complained about this in the
    5952             :                check_for_bare_parameter_packs call, so just replace
    5953             :                these types with ERROR_MARK_NODE.  */
    5954          42 :               TREE_TYPE (arg) = error_mark_node;
    5955          42 :               TREE_VALUE (argtype) = error_mark_node;
    5956             :             }
    5957             : 
    5958   237107773 :           arg = DECL_CHAIN (arg);
    5959   237107773 :           argtype = TREE_CHAIN (argtype);
    5960             :         }
    5961             : 
    5962             :       /* Check for bare parameter packs in the return type and the
    5963             :          exception specifiers.  */
    5964   107103179 :       if (check_for_bare_parameter_packs (TREE_TYPE (type)))
    5965             :         /* Errors were already issued, set return type to int
    5966             :            as the frontend doesn't expect error_mark_node as
    5967             :            the return type.  */
    5968           3 :         TREE_TYPE (type) = integer_type_node;
    5969   107103179 :       if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
    5970           0 :         TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
    5971             :     }
    5972             :   else
    5973             :     {
    5974   164518736 :       if (check_for_bare_parameter_packs (is_typedef_decl (decl)
    5975             :                                           ? DECL_ORIGINAL_TYPE (decl)
    5976    47725675 :                                           : TREE_TYPE (decl)))
    5977             :         {
    5978          64 :           TREE_TYPE (decl) = error_mark_node;
    5979          64 :           return error_mark_node;
    5980             :         }
    5981             : 
    5982     5304564 :       if (is_partial && VAR_P (decl)
    5983    82624935 :           && check_for_bare_parameter_packs (DECL_TI_ARGS (decl)))
    5984           3 :         return error_mark_node;
    5985             :     }
    5986             : 
    5987   189362480 :   if (is_partial)
    5988     5304561 :     return process_partial_specialization (decl);
    5989             : 
    5990   184057919 :   tree args = current_template_args ();
    5991   184057919 :   tree tmpl = NULL_TREE;
    5992   184057919 :   bool new_template_p = false;
    5993   184057919 :   if (local_p)
    5994             :     {
    5995             :       /* Does not get a template head.  */
    5996    38553983 :       tmpl = NULL_TREE;
    5997    38553983 :       gcc_checking_assert (!is_primary);
    5998             :     }
    5999   145503936 :   else if (!ctx
    6000   113807728 :            || TREE_CODE (ctx) == FUNCTION_DECL
    6001   113495314 :            || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
    6002    40382001 :            || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
    6003   185842280 :            || (is_friend && !(DECL_LANG_SPECIFIC (decl)
    6004          93 :                               && DECL_TEMPLATE_INFO (decl))))
    6005             :     {
    6006   105165689 :       if (DECL_LANG_SPECIFIC (decl)
    6007    95426195 :           && DECL_TEMPLATE_INFO (decl)
    6008   107142477 :           && DECL_TI_TEMPLATE (decl))
    6009     1976788 :         tmpl = DECL_TI_TEMPLATE (decl);
    6010             :       /* If DECL is a TYPE_DECL for a class-template, then there won't
    6011             :          be DECL_LANG_SPECIFIC.  The information equivalent to
    6012             :          DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead.  */
    6013    35284855 :       else if (DECL_IMPLICIT_TYPEDEF_P (decl)
    6014     8208096 :                && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
    6015   103221711 :                && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
    6016             :         {
    6017             :           /* Since a template declaration already existed for this
    6018             :              class-type, we must be redeclaring it here.  Make sure
    6019             :              that the redeclaration is valid.  */
    6020       16405 :           redeclare_class_template (TREE_TYPE (decl),
    6021             :                                     current_template_parms,
    6022             :                                     current_template_constraints ());
    6023             :           /* We don't need to create a new TEMPLATE_DECL; just use the
    6024             :              one we already had.  */
    6025       32810 :           tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
    6026             :         }
    6027             :       else
    6028             :         {
    6029   103172496 :           tmpl = build_template_decl (decl, current_template_parms,
    6030             :                                       member_template_p);
    6031   103172496 :           new_template_p = true;
    6032             : 
    6033   103172496 :           if (DECL_LANG_SPECIFIC (decl)
    6034   103172496 :               && DECL_TEMPLATE_SPECIALIZATION (decl))
    6035             :             {
    6036             :               /* A specialization of a member template of a template
    6037             :                  class.  */
    6038           0 :               SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
    6039           0 :               DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
    6040           0 :               DECL_TEMPLATE_INFO (decl) = NULL_TREE;
    6041             :             }
    6042             :         }
    6043             :     }
    6044             :   else
    6045             :     {
    6046    40338247 :       tree a, t, current, parms;
    6047    40338247 :       int i;
    6048    40338247 :       tree tinfo = get_template_info (decl);
    6049             : 
    6050    40338247 :       if (!tinfo)
    6051             :         {
    6052          19 :           error ("template definition of non-template %q#D", decl);
    6053          19 :           return error_mark_node;
    6054             :         }
    6055             : 
    6056    40338228 :       tmpl = TI_TEMPLATE (tinfo);
    6057             : 
    6058    40338228 :       if (DECL_FUNCTION_TEMPLATE_P (tmpl)
    6059    40059649 :           && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
    6060    40059649 :           && DECL_TEMPLATE_SPECIALIZATION (decl)
    6061    40338249 :           && DECL_MEMBER_TEMPLATE_P (tmpl))
    6062             :         {
    6063             :           /* The declaration is a specialization of a member
    6064             :              template, declared outside the class.  Therefore, the
    6065             :              innermost template arguments will be NULL, so we
    6066             :              replace them with the arguments determined by the
    6067             :              earlier call to check_explicit_specialization.  */
    6068          13 :           args = DECL_TI_ARGS (decl);
    6069             : 
    6070          13 :           tree new_tmpl
    6071          13 :             = build_template_decl (decl, current_template_parms,
    6072             :                                    member_template_p);
    6073          13 :           DECL_TI_TEMPLATE (decl) = new_tmpl;
    6074          13 :           SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
    6075          13 :           DECL_TEMPLATE_INFO (new_tmpl)
    6076          13 :             = build_template_info (tmpl, args);
    6077             : 
    6078          13 :           register_specialization (new_tmpl,
    6079             :                                    most_general_template (tmpl),
    6080             :                                    args,
    6081             :                                    is_friend, 0);
    6082          13 :           return decl;
    6083             :         }
    6084             : 
    6085             :       /* Make sure the template headers we got make sense.  */
    6086             : 
    6087    40338215 :       parms = DECL_TEMPLATE_PARMS (tmpl);
    6088    40338215 :       i = TMPL_PARMS_DEPTH (parms);
    6089   112473691 :       if (TMPL_ARGS_DEPTH (args) != i)
    6090             :         {
    6091           8 :           error ("expected %d levels of template parms for %q#D, got %d",
    6092           4 :                  i, decl, TMPL_ARGS_DEPTH (args));
    6093           4 :           DECL_INTERFACE_KNOWN (decl) = 1;
    6094           4 :           return error_mark_node;
    6095             :         }
    6096             :       else
    6097    89246776 :         for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
    6098             :           {
    6099    97817170 :             a = TMPL_ARGS_LEVEL (args, i);
    6100    48908585 :             t = INNERMOST_TEMPLATE_PARMS (parms);
    6101             : 
    6102    48908585 :             if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
    6103             :               {
    6104          20 :                 if (current == decl)
    6105          20 :                   error ("got %d template parameters for %q#D",
    6106          20 :                          TREE_VEC_LENGTH (a), decl);
    6107             :                 else
    6108           0 :                   error ("got %d template parameters for %q#T",
    6109           0 :                          TREE_VEC_LENGTH (a), current);
    6110          20 :                 error ("  but %d required", TREE_VEC_LENGTH (t));
    6111             :                 /* Avoid crash in import_export_decl.  */
    6112          20 :                 DECL_INTERFACE_KNOWN (decl) = 1;
    6113          20 :                 return error_mark_node;
    6114             :               }
    6115             : 
    6116    48908565 :             if (current == decl)
    6117             :               current = ctx;
    6118     8570374 :             else if (current == NULL_TREE)
    6119             :               /* Can happen in erroneous input.  */
    6120             :               break;
    6121             :             else
    6122     8570374 :               current = get_containing_scope (current);
    6123             :           }
    6124             : 
    6125             :       /* Check that the parms are used in the appropriate qualifying scopes
    6126             :          in the declarator.  */
    6127    80676382 :       if (!comp_template_args
    6128    40338191 :           (TI_ARGS (tinfo),
    6129    40338191 :            TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
    6130             :         {
    6131           4 :           error ("template arguments to %qD do not match original "
    6132           4 :                  "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
    6133           4 :           if (!uses_template_parms (TI_ARGS (tinfo)))
    6134           4 :             inform (input_location, "use %<template<>%> for"
    6135             :                     " an explicit specialization");
    6136             :           /* Avoid crash in import_export_decl.  */
    6137           4 :           DECL_INTERFACE_KNOWN (decl) = 1;
    6138           4 :           return error_mark_node;
    6139             :         }
    6140             : 
    6141             :       /* Check that the constraints for each enclosing template scope are
    6142             :          consistent with the original declarations.  */
    6143    40338187 :       if (flag_concepts)
    6144             :         {
    6145     1763634 :           tree decl_parms = DECL_TEMPLATE_PARMS (tmpl);
    6146     1763634 :           tree scope_parms = current_template_parms;
    6147     1763634 :           if (PRIMARY_TEMPLATE_P (tmpl))
    6148             :             {
    6149      612561 :               decl_parms = TREE_CHAIN (decl_parms);
    6150      612561 :               scope_parms = TREE_CHAIN (scope_parms);
    6151             :             }
    6152     3301633 :           while (decl_parms)
    6153             :             {
    6154     1538005 :               if (!template_requirements_equivalent_p (decl_parms, scope_parms))
    6155             :                 {
    6156           6 :                   error ("redeclaration of %qD with different constraints",
    6157           6 :                          TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms)));
    6158           6 :                   break;
    6159             :                 }
    6160     1537999 :               decl_parms = TREE_CHAIN (decl_parms);
    6161     1537999 :               scope_parms = TREE_CHAIN (scope_parms);
    6162             :             }
    6163             :         }
    6164             :     }
    6165             : 
    6166   291007752 :   gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
    6167             : 
    6168   184057859 :   if (new_template_p)
    6169             :     {
    6170             :       /* Push template declarations for global functions and types.
    6171             :          Note that we do not try to push a global template friend
    6172             :          declared in a template class; such a thing may well depend on
    6173             :          the template parameters of the class and we'll push it when
    6174             :          instantiating the befriending class.  */
    6175   103172496 :       if (!ctx
    6176   103172496 :           && !(is_friend && template_class_depth (current_class_type) > 0))
    6177             :         {
    6178    28278319 :           tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
    6179    28278319 :           if (pushed == error_mark_node)
    6180             :             return error_mark_node;
    6181             : 
    6182             :           /* pushdecl may have found an existing template.  */
    6183    28278268 :           if (pushed != tmpl)
    6184             :             {
    6185     2061146 :               decl = DECL_TEMPLATE_RESULT (pushed);
    6186     2061146 :               tmpl = NULL_TREE;
    6187             :             }
    6188             :         }
    6189    74894177 :       else if (is_friend)
    6190             :         {
    6191             :           /* Record this decl as belonging to the current class.  It's
    6192             :              not chained onto anything else.  */
    6193     1638783 :           DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true;
    6194     1638783 :           gcc_checking_assert (!DECL_CHAIN (tmpl));
    6195     1638783 :           DECL_CHAIN (tmpl) = current_scope ();
    6196             :         }
    6197             :     }
    6198    80885363 :   else if (tmpl)
    6199             :     /* The type may have been completed, or (erroneously) changed.  */
    6200    42331380 :     TREE_TYPE (tmpl) = TREE_TYPE (decl);
    6201             : 
    6202   145503825 :   if (tmpl)
    6203             :     {
    6204   143442679 :       if (is_primary)
    6205             :         {
    6206    39640032 :           tree parms = DECL_TEMPLATE_PARMS (tmpl);
    6207             : 
    6208    39640032 :           DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
    6209             : 
    6210             :           /* Give template template parms a DECL_CONTEXT of the template
    6211             :              for which they are a parameter.  */
    6212    39640032 :           parms = INNERMOST_TEMPLATE_PARMS (parms);
    6213   112995485 :           for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
    6214             :             {
    6215    73355453 :               tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
    6216    73355453 :               if (TREE_CODE (parm) == TEMPLATE_DECL)
    6217      136379 :                 DECL_CONTEXT (parm) = tmpl;
    6218             :             }
    6219             : 
    6220    39640032 :           if (TREE_CODE (decl) == TYPE_DECL
    6221    39640032 :               && TYPE_DECL_ALIAS_P (decl))
    6222             :             {
    6223     2516907 :               if (tree constr
    6224     2516907 :                   = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
    6225             :                 {
    6226             :                   /* ??? Why don't we do this here for all templates?  */
    6227       18872 :                   constr = build_constraints (constr, NULL_TREE);
    6228       18872 :                   set_constraints (decl, constr);
    6229             :                 }
    6230     2516907 :               if (complex_alias_template_p (tmpl))
    6231      324605 :                 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
    6232             :             }
    6233             :         }
    6234             : 
    6235             :       /* The DECL_TI_ARGS of DECL contains full set of arguments
    6236             :          referring wback to its most general template.  If TMPL is a
    6237             :          specialization, ARGS may only have the innermost set of
    6238             :          arguments.  Add the missing argument levels if necessary.  */
    6239   143442679 :       if (DECL_TEMPLATE_INFO (tmpl))
    6240         696 :         args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
    6241             : 
    6242   143442679 :       tree info = build_template_info (tmpl, args);
    6243             : 
    6244   143442679 :       if (DECL_IMPLICIT_TYPEDEF_P (decl))
    6245     8251551 :         SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
    6246             :       else
    6247             :         {
    6248   135191128 :           retrofit_lang_decl (decl);
    6249   135191128 :           DECL_TEMPLATE_INFO (decl) = info;
    6250             :         }
    6251             :     }
    6252             : 
    6253   184057808 :   if (flag_implicit_templates
    6254   183349539 :       && !is_friend
    6255   179781962 :       && TREE_PUBLIC (decl)
    6256   105407568 :       && VAR_OR_FUNCTION_DECL_P (decl))
    6257             :     /* Set DECL_COMDAT on template instantiations; if we force
    6258             :        them to be emitted by explicit instantiation,
    6259             :        mark_needed will tell cgraph to do the right thing.  */
    6260   105347968 :     DECL_COMDAT (decl) = true;
    6261             : 
    6262   327500487 :   gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
    6263             : 
    6264             :   return decl;
    6265             : }
    6266             : 
    6267             : /* FN is an inheriting constructor that inherits from the constructor
    6268             :    template INHERITED; turn FN into a constructor template with a matching
    6269             :    template header.  */
    6270             : 
    6271             : tree
    6272       33814 : add_inherited_template_parms (tree fn, tree inherited)
    6273             : {
    6274       33814 :   tree inner_parms
    6275       33814 :     = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
    6276       33814 :   inner_parms = copy_node (inner_parms);
    6277       33814 :   tree parms
    6278       33814 :     = tree_cons (size_int (current_template_depth + 1),
    6279             :                  inner_parms, current_template_parms);
    6280       33814 :   tree tmpl = build_template_decl (fn, parms, /*member*/true);
    6281       33814 :   tree args = template_parms_to_args (parms);
    6282       33814 :   DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
    6283       33814 :   DECL_ARTIFICIAL (tmpl) = true;
    6284       33814 :   DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
    6285       33814 :   return tmpl;
    6286             : }
    6287             : 
    6288             : /* Called when a class template TYPE is redeclared with the indicated
    6289             :    template PARMS, e.g.:
    6290             : 
    6291             :      template <class T> struct S;
    6292             :      template <class T> struct S {};  */
    6293             : 
    6294             : bool
    6295     1465679 : redeclare_class_template (tree type, tree parms, tree cons)
    6296             : {
    6297     1465679 :   tree tmpl;
    6298     1465679 :   tree tmpl_parms;
    6299     1465679 :   int i;
    6300             : 
    6301     1465679 :   if (!TYPE_TEMPLATE_INFO (type))
    6302             :     {
    6303           0 :       error ("%qT is not a template type", type);
    6304           0 :       return false;
    6305             :     }
    6306             : 
    6307     1465679 :   tmpl = TYPE_TI_TEMPLATE (type);
    6308     1465679 :   if (!PRIMARY_TEMPLATE_P (tmpl))
    6309             :     /* The type is nested in some template class.  Nothing to worry
    6310             :        about here; there are no new template parameters for the nested
    6311             :        type.  */
    6312             :     return true;
    6313             : 
    6314     1465679 :   if (!parms)
    6315             :     {
    6316           4 :       error ("template specifiers not specified in declaration of %qD",
    6317             :              tmpl);
    6318           4 :       return false;
    6319             :     }
    6320             : 
    6321     1465675 :   parms = INNERMOST_TEMPLATE_PARMS (parms);
    6322     1465675 :   tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
    6323             : 
    6324     1465675 :   if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
    6325             :     {
    6326          24 :       error_n (input_location, TREE_VEC_LENGTH (parms),
    6327             :                "redeclared with %d template parameter",
    6328             :                "redeclared with %d template parameters",
    6329          24 :                TREE_VEC_LENGTH (parms));
    6330          24 :       inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
    6331             :                 "previous declaration %qD used %d template parameter",
    6332             :                 "previous declaration %qD used %d template parameters",
    6333          24 :                 tmpl, TREE_VEC_LENGTH (tmpl_parms));
    6334          24 :       return false;
    6335             :     }
    6336             : 
    6337     3895349 :   for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
    6338             :     {
    6339     2429742 :       tree tmpl_parm;
    6340     2429742 :       tree parm;
    6341             : 
    6342     2429742 :       if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
    6343     2429742 :           || TREE_VEC_ELT (parms, i) == error_mark_node)
    6344           0 :         continue;
    6345             : 
    6346     2429742 :       tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
    6347     2429742 :       if (error_operand_p (tmpl_parm))
    6348             :         return false;
    6349             : 
    6350     2429733 :       parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
    6351             : 
    6352             :       /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
    6353             :          TEMPLATE_DECL.  */
    6354     2429733 :       if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
    6355     2429717 :           || (TREE_CODE (tmpl_parm) != TYPE_DECL
    6356      241706 :               && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
    6357     2429706 :           || (TREE_CODE (tmpl_parm) != PARM_DECL
    6358     2188659 :               && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
    6359     2188659 :                   != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
    6360     4859436 :           || (TREE_CODE (tmpl_parm) == PARM_DECL
    6361      241047 :               && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
    6362      241047 :                   != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
    6363             :         {
    6364          33 :           auto_diagnostic_group d;
    6365          33 :           error ("template parameter %q+#D", tmpl_parm);
    6366          33 :           if (DECL_P (parm))
    6367          29 :             inform (DECL_SOURCE_LOCATION (parm), "redeclared here as %q#D", parm);
    6368             :           else
    6369           4 :             inform (input_location, "redeclared here");
    6370          33 :           return false;
    6371          33 :         }
    6372             : 
    6373             :       /* The parameters can be declared to introduce different
    6374             :          constraints.  */
    6375     2429700 :       tree p1 = TREE_VEC_ELT (tmpl_parms, i);
    6376     2429700 :       tree p2 = TREE_VEC_ELT (parms, i);
    6377     2429700 :       if (!template_parameter_constraints_equivalent_p (p1, p2))
    6378             :         {
    6379           2 :           auto_diagnostic_group d;
    6380           2 :           error ("declaration of template parameter %q+#D with different "
    6381             :                  "constraints", parm);
    6382           2 :           inform (DECL_SOURCE_LOCATION (tmpl_parm),
    6383             :                   "original declaration appeared here");
    6384           2 :           return false;
    6385           2 :         }
    6386             : 
    6387             :       /* Give each template template parm in this redeclaration a
    6388             :          DECL_CONTEXT of the template for which they are a parameter.  */
    6389     2429698 :       if (TREE_CODE (parm) == TEMPLATE_DECL)
    6390             :         {
    6391         648 :           gcc_checking_assert (DECL_CONTEXT (parm) == NULL_TREE
    6392             :                                || DECL_CONTEXT (parm) == tmpl);
    6393         648 :           DECL_CONTEXT (parm) = tmpl;
    6394             :         }
    6395             :     }
    6396             : 
    6397     1465607 :   if (!merge_default_template_args (parms, tmpl_parms, /*class_p=*/true))
    6398             :     return false;
    6399             : 
    6400     1465603 :   tree ci = get_constraints (tmpl);
    6401     1466936 :   tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
    6402     1466936 :   tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
    6403             : 
    6404             :   /* Two classes with different constraints declare different entities.  */
    6405     1465603 :   if (!cp_tree_equal (req1, req2))
    6406             :     {
    6407           4 :       auto_diagnostic_group d;
    6408           4 :       error_at (input_location, "redeclaration of %q#D with different "
    6409             :                                 "constraints", tmpl);
    6410           4 :       inform (DECL_SOURCE_LOCATION (tmpl),
    6411             :               "original declaration appeared here");
    6412           4 :       return false;
    6413           4 :     }
    6414             : 
    6415             :     return true;
    6416             : }
    6417             : 
    6418             : /* The actual substitution part of instantiate_non_dependent_expr,
    6419             :    to be used when the caller has already checked
    6420             :     !instantiation_dependent_uneval_expression_p (expr)
    6421             :    and cleared processing_template_decl.  */
    6422             : 
    6423             : tree
    6424    95316423 : instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
    6425             : {
    6426    58876642 :   return tsubst_copy_and_build (expr,
    6427             :                                 /*args=*/NULL_TREE,
    6428             :                                 complain,
    6429    58876642 :                                 /*in_decl=*/NULL_TREE);
    6430             : }
    6431             : 
    6432             : /* Instantiate the non-dependent expression EXPR.  */
    6433             : 
    6434             : tree
    6435    71487893 : instantiate_non_dependent_expr (tree expr,
    6436             :                                 tsubst_flags_t complain /* = tf_error */)
    6437             : {
    6438    71487893 :   if (expr == NULL_TREE)
    6439             :     return NULL_TREE;
    6440             : 
    6441    71487893 :   if (processing_template_decl)
    6442             :     {
    6443             :       /* The caller should have checked this already.  */
    6444    20247966 :       gcc_checking_assert (!instantiation_dependent_uneval_expression_p (expr));
    6445    20247966 :       processing_template_decl_sentinel s;
    6446    20247966 :       expr = instantiate_non_dependent_expr_internal (expr, complain);
    6447    20247966 :     }
    6448             :   return expr;
    6449             : }
    6450             : 
    6451             : /* Like instantiate_non_dependent_expr, but return NULL_TREE if the
    6452             :    expression is dependent or non-constant.  */
    6453             : 
    6454             : tree
    6455    70961442 : instantiate_non_dependent_or_null (tree expr)
    6456             : {
    6457    70961442 :   if (expr == NULL_TREE)
    6458             :     return NULL_TREE;
    6459    64116049 :   if (processing_template_decl)
    6460             :     {
    6461        4550 :       if (!is_nondependent_constant_expression (expr))
    6462             :         expr = NULL_TREE;
    6463             :       else
    6464             :         {
    6465        3760 :           processing_template_decl_sentinel s;
    6466        3760 :           expr = instantiate_non_dependent_expr_internal (expr, tf_error);
    6467        3760 :         }
    6468             :     }
    6469             :   return expr;
    6470             : }
    6471             : 
    6472             : /* True iff T is a specialization of a variable template.  */
    6473             : 
    6474             : bool
    6475   126902638 : variable_template_specialization_p (tree t)
    6476             : {
    6477   126902638 :   if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
    6478             :     return false;
    6479     6314604 :   tree tmpl = DECL_TI_TEMPLATE (t);
    6480     6314604 :   return variable_template_p (tmpl);
    6481             : }
    6482             : 
    6483             : /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
    6484             :    template declaration, or a TYPE_DECL for an alias declaration.  */
    6485             : 
    6486             : bool
    6487   100689150 : alias_type_or_template_p (tree t)
    6488             : {
    6489   100689150 :   if (t == NULL_TREE)
    6490             :     return false;
    6491           0 :   return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
    6492   100689150 :           || (TYPE_P (t)
    6493   100689150 :               && TYPE_NAME (t)
    6494   100689150 :               && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
    6495   198049746 :           || DECL_ALIAS_TEMPLATE_P (t));
    6496             : }
    6497             : 
    6498             : /* If T is a specialization of an alias template, return it; otherwise return
    6499             :    NULL_TREE.  If TRANSPARENT_TYPEDEFS is true, look through other aliases.  */
    6500             : 
    6501             : tree
    6502 13727602641 : alias_template_specialization_p (const_tree t,
    6503             :                                  bool transparent_typedefs)
    6504             : {
    6505 13727602641 :   if (!TYPE_P (t))
    6506             :     return NULL_TREE;
    6507             : 
    6508             :   /* It's an alias template specialization if it's an alias and its
    6509             :      TYPE_NAME is a specialization of a primary template.  */
    6510 13727602641 :   if (typedef_variant_p (t))
    6511             :     {
    6512   243196246 :       if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
    6513   204368904 :         if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
    6514             :           return CONST_CAST_TREE (t);
    6515   164565756 :       if (transparent_typedefs)
    6516       64558 :         return alias_template_specialization_p (DECL_ORIGINAL_TYPE
    6517             :                                                 (TYPE_NAME (t)),
    6518       64558 :                                                 transparent_typedefs);
    6519             :     }
    6520             : 
    6521             :   return NULL_TREE;
    6522             : }
    6523             : 
    6524             : /* Data structure for complex_alias_template_*.  */
    6525             : 
    6526             : struct uses_all_template_parms_data
    6527             : {
    6528             :   int level;
    6529             :   bool *seen;
    6530             : };
    6531             : 
    6532             : /* walk_tree callback for complex_alias_template_p.  */
    6533             : 
    6534             : static tree
    6535    23881266 : complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
    6536             : {
    6537    23881266 :   tree t = *tp;
    6538    23881266 :   auto &data = *(struct uses_all_template_parms_data*)data_;
    6539             : 
    6540    23881266 :   switch (TREE_CODE (t))
    6541             :     {
    6542     4634629 :     case TEMPLATE_TYPE_PARM:
    6543     4634629 :     case TEMPLATE_PARM_INDEX:
    6544     4634629 :     case TEMPLATE_TEMPLATE_PARM:
    6545     4634629 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
    6546     4634629 :       {
    6547     4634629 :         tree idx = get_template_parm_index (t);
    6548     4634629 :         if (TEMPLATE_PARM_LEVEL (idx) == data.level)
    6549     3626205 :           data.seen[TEMPLATE_PARM_IDX (idx)] = true;
    6550             :       }
    6551             : 
    6552    23881266 :     default:;
    6553             :     }
    6554             : 
    6555    23881266 :   if (!PACK_EXPANSION_P (t))
    6556             :     return 0;
    6557             : 
    6558             :   /* An alias template with a pack expansion that expands a pack from the
    6559             :      enclosing class needs to be considered complex, to avoid confusion with
    6560             :      the same pack being used as an argument to the alias's own template
    6561             :      parameter (91966).  */
    6562     1415546 :   for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
    6563      423790 :        pack = TREE_CHAIN (pack))
    6564             :     {
    6565      526847 :       tree parm_pack = TREE_VALUE (pack);
    6566      526847 :       if (!TEMPLATE_PARM_P (parm_pack))
    6567       10449 :         continue;
    6568      516398 :       int idx, level;
    6569      516398 :       template_parm_level_and_index (parm_pack, &level, &idx);
    6570      516398 :       if (level < data.level)
    6571      103057 :         return t;
    6572             : 
    6573             :       /* Consider the expanded packs to be used outside the expansion...  */
    6574      413341 :       data.seen[idx] = true;
    6575             :     }
    6576             : 
    6577             :   /* ...but don't walk into the pattern.  Consider PR104008:
    6578             : 
    6579             :      template <typename T, typename... Ts>
    6580             :      using IsOneOf = disjunction<is_same<T, Ts>...>;
    6581             : 
    6582             :      where IsOneOf seemingly uses all of its template parameters in its
    6583             :      expansion (and does not expand a pack from the enclosing class), so the
    6584             :      alias was not marked as complex.  However, if it is used like
    6585             :      "IsOneOf<T>", the empty pack for Ts means that T no longer appears in the
    6586             :      expansion.  So only Ts is considered used by the pack expansion.  */
    6587      415024 :   *walk_subtrees = false;
    6588             : 
    6589      415024 :   return 0;
    6590             : }
    6591             : 
    6592             : /* An alias template is complex from a SFINAE perspective if a template-id
    6593             :    using that alias can be ill-formed when the expansion is not, as with
    6594             :    the void_t template.
    6595             : 
    6596             :    Returns 1 if always complex, 0 if not complex, -1 if complex iff any of the
    6597             :    template arguments are empty packs.  */
    6598             : 
    6599             : static bool
    6600     2516907 : complex_alias_template_p (const_tree tmpl)
    6601             : {
    6602             :   /* A renaming alias isn't complex.  */
    6603     2516907 :   if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
    6604             :     return false;
    6605             : 
    6606             :   /* Any other constrained alias is complex.  */
    6607     2445448 :   if (get_constraints (tmpl))
    6608             :     return true;
    6609             : 
    6610     2426576 :   struct uses_all_template_parms_data data;
    6611     2426576 :   tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
    6612     2426576 :   tree parms = DECL_TEMPLATE_PARMS (tmpl);
    6613     2426576 :   data.level = TMPL_PARMS_DEPTH (parms);
    6614     2426576 :   int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
    6615     2426576 :   data.seen = XALLOCAVEC (bool, len);
    6616     6359729 :   for (int i = 0; i < len; ++i)
    6617     3933153 :     data.seen[i] = false;
    6618             : 
    6619     2426576 :   if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data))
    6620             :     return true;
    6621     5676839 :   for (int i = 0; i < len; ++i)
    6622     3555996 :     if (!data.seen[i])
    6623             :       return true;
    6624             :   return false;
    6625             : }
    6626             : 
    6627             : /* If T is a specialization of a complex alias template with dependent
    6628             :    template-arguments, return it; otherwise return NULL_TREE.  If T is a
    6629             :    typedef to such a specialization, return the specialization.  */
    6630             : 
    6631             : tree
    6632  1011020610 : dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
    6633             : {
    6634  1011020610 :   if (t == error_mark_node)
    6635             :     return NULL_TREE;
    6636   995043950 :   gcc_assert (TYPE_P (t));
    6637             : 
    6638   995043950 :   if (!typedef_variant_p (t))
    6639             :     return NULL_TREE;
    6640             : 
    6641    97343888 :   tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
    6642    86464083 :   if (tinfo
    6643    84352956 :       && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
    6644   103512577 :       && (any_dependent_template_arguments_p
    6645    17048494 :           (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
    6646             :     return CONST_CAST_TREE (t);
    6647             : 
    6648    90142967 :   if (transparent_typedefs)
    6649             :     {
    6650     9292304 :       tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
    6651     9292304 :       return dependent_alias_template_spec_p (utype, transparent_typedefs);
    6652             :     }
    6653             : 
    6654             :   return NULL_TREE;
    6655             : }
    6656             : 
    6657             : /* Return the number of innermost template parameters in TMPL.  */
    6658             : 
    6659             : static int
    6660     4249742 : num_innermost_template_parms (const_tree tmpl)
    6661             : {
    6662     4249742 :   tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
    6663     4249742 :   return TREE_VEC_LENGTH (parms);
    6664             : }
    6665             : 
    6666             : /* Return either TMPL or another template that it is equivalent to under DR
    6667             :    1286: An alias that just changes the name of a template is equivalent to
    6668             :    the other template.  */
    6669             : 
    6670             : static tree
    6671    14447404 : get_underlying_template (tree tmpl)
    6672             : {
    6673    14447404 :   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
    6674    14519774 :   while (DECL_ALIAS_TEMPLATE_P (tmpl))
    6675             :     {
    6676             :       /* Determine if the alias is equivalent to an underlying template.  */
    6677     3899348 :       tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
    6678             :       /* The underlying type may have been ill-formed. Don't proceed.  */
    6679     3899348 :       if (!orig_type)
    6680             :         break;
    6681     3899343 :       tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
    6682     2125020 :       if (!tinfo)
    6683             :         break;
    6684             : 
    6685     2124924 :       tree underlying = TI_TEMPLATE (tinfo);
    6686     2124924 :       if (!PRIMARY_TEMPLATE_P (underlying)
    6687     4249795 :           || (num_innermost_template_parms (tmpl)
    6688     2124871 :               != num_innermost_template_parms (underlying)))
    6689             :         break;
    6690             : 
    6691             :       /* Does the alias add cv-quals?  */
    6692      694441 :       if (TYPE_QUALS (TREE_TYPE (underlying)) != TYPE_QUALS (TREE_TYPE (tmpl)))
    6693             :         break;
    6694             : 
    6695      694423 :       tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
    6696      694423 :       if (!comp_template_args (TI_ARGS (tinfo), alias_args))
    6697             :         break;
    6698             : 
    6699             :       /* Are any default template arguments equivalent?  */
    6700       72409 :       tree aparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
    6701       72409 :       tree uparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (underlying));
    6702       72409 :       const int nparms = TREE_VEC_LENGTH (aparms);
    6703      156177 :       for (int i = 0; i < nparms; ++i)
    6704             :         {
    6705       83799 :           tree adefarg = TREE_PURPOSE (TREE_VEC_ELT (aparms, i));
    6706       83799 :           tree udefarg = TREE_PURPOSE (TREE_VEC_ELT (uparms, i));
    6707       83799 :           if (!template_args_equal (adefarg, udefarg))
    6708          31 :             goto top_break;
    6709             :         }
    6710             : 
    6711             :       /* If TMPL adds or changes any constraints, it isn't equivalent.  I think
    6712             :          it's appropriate to treat a less-constrained alias as equivalent.  */
    6713       72378 :       if (!at_least_as_constrained (underlying, tmpl))
    6714             :         break;
    6715             : 
    6716             :       /* Alias is equivalent.  Strip it and repeat.  */
    6717             :       tmpl = underlying;
    6718             :     }
    6719      622141 :   top_break:;
    6720             : 
    6721    14447404 :   return tmpl;
    6722             : }
    6723             : 
    6724             : /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
    6725             :    must be a reference-to-function or a pointer-to-function type, as specified
    6726             :    in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
    6727             :    and check that the resulting function has external linkage.  */
    6728             : 
    6729             : static tree
    6730         788 : convert_nontype_argument_function (tree type, tree expr,
    6731             :                                    tsubst_flags_t complain)
    6732             : {
    6733         788 :   tree fns = expr;
    6734         788 :   tree fn, fn_no_ptr;
    6735         788 :   linkage_kind linkage;
    6736             : 
    6737         788 :   fn = instantiate_type (type, fns, tf_none);
    6738         788 :   if (fn == error_mark_node)
    6739             :     return error_mark_node;
    6740             : 
    6741         780 :   if (value_dependent_expression_p (fn))
    6742          20 :     goto accept;
    6743             : 
    6744         760 :   fn_no_ptr = fn;
    6745         760 :   if (REFERENCE_REF_P (fn_no_ptr))
    6746          14 :     fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
    6747         760 :   fn_no_ptr = strip_fnptr_conv (fn_no_ptr);
    6748         760 :   if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
    6749         649 :     fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
    6750         760 :   if (BASELINK_P (fn_no_ptr))
    6751           0 :     fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
    6752             : 
    6753             :   /* [temp.arg.nontype]/1
    6754             : 
    6755             :      A template-argument for a non-type, non-template template-parameter
    6756             :      shall be one of:
    6757             :      [...]
    6758             :      -- the address of an object or function with external [C++11: or
    6759             :         internal] linkage.  */
    6760             : 
    6761         760 :   STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
    6762         760 :   if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
    6763             :     {
    6764          36 :       if (complain & tf_error)
    6765             :         {
    6766          21 :           location_t loc = cp_expr_loc_or_input_loc (expr);
    6767          21 :           error_at (loc, "%qE is not a valid template argument for type %qT",
    6768             :                     expr, type);
    6769          21 :           if (TYPE_PTR_P (type))
    6770          15 :             inform (loc, "it must be the address of a function "
    6771             :                     "with external linkage");
    6772             :           else
    6773           6 :             inform (loc, "it must be the name of a function with "
    6774             :                     "external linkage");
    6775             :         }
    6776          36 :       return NULL_TREE;
    6777             :     }
    6778             : 
    6779         724 :   linkage = decl_linkage (fn_no_ptr);
    6780         724 :   if ((cxx_dialect < cxx11 && linkage != lk_external)
    6781         719 :       || (cxx_dialect < cxx17 && linkage == lk_none))
    6782             :     {
    6783           7 :       if (complain & tf_error)
    6784             :         {
    6785           3 :           location_t loc = cp_expr_loc_or_input_loc (expr);
    6786           3 :           if (cxx_dialect >= cxx11)
    6787           1 :             error_at (loc, "%qE is not a valid template argument for type "
    6788             :                       "%qT because %qD has no linkage",
    6789             :                       expr, type, fn_no_ptr);
    6790             :           else
    6791           2 :             error_at (loc, "%qE is not a valid template argument for type "
    6792             :                       "%qT because %qD does not have external linkage",
    6793             :                       expr, type, fn_no_ptr);
    6794             :         }
    6795           7 :       return NULL_TREE;
    6796             :     }
    6797             : 
    6798         717 :  accept:
    6799         737 :   if (TYPE_REF_P (type))
    6800             :     {
    6801          85 :       if (REFERENCE_REF_P (fn))
    6802          10 :         fn = TREE_OPERAND (fn, 0);
    6803             :       else
    6804          75 :         fn = build_address (fn);
    6805             :     }
    6806         737 :   if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
    6807          75 :     fn = build_nop (type, fn);
    6808             : 
    6809             :   return fn;
    6810             : }
    6811             : 
    6812             : /* Subroutine of convert_nontype_argument.
    6813             :    Check if EXPR of type TYPE is a valid pointer-to-member constant.
    6814             :    Emit an error otherwise.  */
    6815             : 
    6816             : static bool
    6817         969 : check_valid_ptrmem_cst_expr (tree type, tree expr,
    6818             :                              tsubst_flags_t complain)
    6819             : {
    6820         969 :   tree orig_expr = expr;
    6821         969 :   STRIP_NOPS (expr);
    6822         969 :   if (null_ptr_cst_p (expr))
    6823             :     return true;
    6824         969 :   if (TREE_CODE (expr) == PTRMEM_CST
    6825         969 :       && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
    6826             :                       PTRMEM_CST_CLASS (expr)))
    6827             :     return true;
    6828         100 :   if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
    6829             :     return true;
    6830          55 :   if (processing_template_decl
    6831           0 :       && TREE_CODE (expr) == ADDR_EXPR
    6832          55 :       && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
    6833             :     return true;
    6834          55 :   if (complain & tf_error)
    6835             :     {
    6836          28 :       location_t loc = cp_expr_loc_or_input_loc (orig_expr);
    6837          28 :       error_at (loc, "%qE is not a valid template argument for type %qT",
    6838             :                 orig_expr, type);
    6839          28 :       if (TREE_CODE (expr) != PTRMEM_CST)
    6840          14 :         inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
    6841             :       else
    6842          14 :         inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
    6843             :     }
    6844             :   return false;
    6845             : }
    6846             : 
    6847             : /* Returns TRUE iff the address of OP is value-dependent.
    6848             : 
    6849             :    14.6.2.4 [temp.dep.temp]:
    6850             :    A non-integral non-type template-argument is dependent if its type is
    6851             :    dependent or it has either of the following forms
    6852             :      qualified-id
    6853             :      & qualified-id
    6854             :    and contains a nested-name-specifier which specifies a class-name that
    6855             :    names a dependent type.
    6856             : 
    6857             :    We generalize this to just say that the address of a member of a
    6858             :    dependent class is value-dependent; the above doesn't cover the
    6859             :    address of a static data member named with an unqualified-id.  */
    6860             : 
    6861             : static bool
    6862      198782 : has_value_dependent_address (tree op)
    6863             : {
    6864      198782 :   STRIP_ANY_LOCATION_WRAPPER (op);
    6865             : 
    6866             :   /* We could use get_inner_reference here, but there's no need;
    6867             :      this is only relevant for template non-type arguments, which
    6868             :      can only be expressed as &id-expression.  */
    6869      198782 :   if (DECL_P (op))
    6870             :     {
    6871      183714 :       tree ctx = CP_DECL_CONTEXT (op);
    6872             : 
    6873      183714 :       if (TYPE_P (ctx) && dependent_type_p (ctx))
    6874             :         return true;
    6875             : 
    6876      183176 :       if (VAR_P (op)
    6877      183176 :           && TREE_STATIC (op)
    6878        3613 :           && TREE_CODE (ctx) == FUNCTION_DECL
    6879      183808 :           && type_dependent_expression_p (ctx))
    6880             :         return true;
    6881             :     }
    6882             : 
    6883             :   return false;
    6884             : }
    6885             : 
    6886             : /* The next set of functions are used for providing helpful explanatory
    6887             :    diagnostics for failed overload resolution.  Their messages should be
    6888             :    indented by two spaces for consistency with the messages in
    6889             :    call.cc  */
    6890             : 
    6891             : static int
    6892           0 : unify_success (bool /*explain_p*/)
    6893             : {
    6894           0 :   return 0;
    6895             : }
    6896             : 
    6897             : /* Other failure functions should call this one, to provide a single function
    6898             :    for setting a breakpoint on.  */
    6899             : 
    6900             : static int
    6901           0 : unify_invalid (bool /*explain_p*/)
    6902             : {
    6903           0 :   return 1;
    6904             : }
    6905             : 
    6906             : static int
    6907      133392 : unify_parameter_deduction_failure (bool explain_p, tree parm)
    6908             : {
    6909           0 :   if (explain_p)
    6910         234 :     inform (input_location,
    6911             :             "  couldn%'t deduce template parameter %qD", parm);
    6912           0 :   return unify_invalid (explain_p);
    6913             : }
    6914             : 
    6915             : static int
    6916     1287363 : unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
    6917             : {
    6918           0 :   if (explain_p)
    6919          72 :     inform (input_location,
    6920             :             "  types %qT and %qT have incompatible cv-qualifiers",
    6921             :             parm, arg);
    6922           0 :   return unify_invalid (explain_p);
    6923             : }
    6924             : 
    6925             : static int
    6926    98201252 : unify_type_mismatch (bool explain_p, tree parm, tree arg)
    6927             : {
    6928       55208 :   if (explain_p)
    6929         219 :     inform (input_location, "  mismatched types %qT and %qT", parm, arg);
    6930       55208 :   return unify_invalid (explain_p);
    6931             : }
    6932             : 
    6933             : static int
    6934      476429 : unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
    6935             : {
    6936           0 :   if (explain_p)
    6937           0 :     inform (input_location,
    6938             :             "  template parameter %qD is not a parameter pack, but "
    6939             :             "argument %qD is",
    6940             :             parm, arg);
    6941           0 :   return unify_invalid (explain_p);
    6942             : }
    6943             : 
    6944             : static int
    6945           3 : unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
    6946             : {
    6947           0 :   if (explain_p)
    6948           0 :     inform (input_location,
    6949             :             "  template argument %qE does not match "
    6950             :             "pointer-to-member constant %qE",
    6951             :             arg, parm);
    6952           0 :   return unify_invalid (explain_p);
    6953             : }
    6954             : 
    6955             : static int
    6956          13 : unify_expression_unequal (bool explain_p, tree parm, tree arg)
    6957             : {
    6958           0 :   if (explain_p)
    6959           0 :     inform (input_location, "  %qE is not equivalent to %qE", parm, arg);
    6960           0 :   return unify_invalid (explain_p);
    6961             : }
    6962             : 
    6963             : static int
    6964          12 : unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
    6965             : {
    6966           0 :   if (explain_p)
    6967           1 :     inform (input_location,
    6968             :             "  inconsistent parameter pack deduction with %qT and %qT",
    6969             :             old_arg, new_arg);
    6970          12 :   return unify_invalid (explain_p);
    6971             : }
    6972             : 
    6973             : static int
    6974      715426 : unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
    6975             : {
    6976      715426 :   if (explain_p)
    6977             :     {
    6978          84 :       if (TYPE_P (parm))
    6979          76 :         inform (input_location,
    6980             :                 "  deduced conflicting types for parameter %qT (%qT and %qT)",
    6981             :                 parm, first, second);
    6982             :       else
    6983           8 :         inform (input_location,
    6984             :                 "  deduced conflicting values for non-type parameter "
    6985             :                 "%qE (%qE and %qE)", parm, first, second);
    6986             :     }
    6987      715426 :   return unify_invalid (explain_p);
    6988             : }
    6989             : 
    6990             : static int
    6991          28 : unify_vla_arg (bool explain_p, tree arg)
    6992             : {
    6993           0 :   if (explain_p)
    6994          12 :     inform (input_location,
    6995             :             "  variable-sized array type %qT is not "
    6996             :             "a valid template argument",
    6997             :             arg);
    6998           0 :   return unify_invalid (explain_p);
    6999             : }
    7000             : 
    7001             : static int
    7002           8 : unify_method_type_error (bool explain_p, tree arg)
    7003             : {
    7004           0 :   if (explain_p)
    7005           0 :     inform (input_location,
    7006             :             "  member function type %qT is not a valid template argument",
    7007             :             arg);
    7008           0 :   return unify_invalid (explain_p);
    7009             : }
    7010             : 
    7011             : static int
    7012    12290509 : unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
    7013             : {
    7014    12290509 :   if (explain_p)
    7015             :     {
    7016         354 :       if (least_p)
    7017          35 :         inform_n (input_location, wanted,
    7018             :                   "  candidate expects at least %d argument, %d provided",
    7019             :                   "  candidate expects at least %d arguments, %d provided",
    7020             :                   wanted, have);
    7021             :       else
    7022         319 :         inform_n (input_location, wanted,
    7023             :                   "  candidate expects %d argument, %d provided",
    7024             :                   "  candidate expects %d arguments, %d provided",
    7025             :                   wanted, have);
    7026             :     }
    7027    12290509 :   return unify_invalid (explain_p);
    7028             : }
    7029             : 
    7030             : static int
    7031      957464 : unify_too_many_arguments (bool explain_p, int have, int wanted)
    7032             : {
    7033           0 :   return unify_arity (explain_p, have, wanted);
    7034             : }
    7035             : 
    7036             : static int
    7037    10796102 : unify_too_few_arguments (bool explain_p, int have, int wanted,
    7038             :                          bool least_p = false)
    7039             : {
    7040           0 :   return unify_arity (explain_p, have, wanted, least_p);
    7041             : }
    7042             : 
    7043             : static int
    7044     1269877 : unify_arg_conversion (bool explain_p, tree to_type,
    7045             :                       tree from_type, tree arg)
    7046             : {
    7047     1269877 :   if (explain_p)
    7048         112 :     inform (cp_expr_loc_or_input_loc (arg),
    7049             :             "  cannot convert %qE (type %qT) to type %qT",
    7050             :             arg, from_type, to_type);
    7051     1269877 :   return unify_invalid (explain_p);
    7052             : }
    7053             : 
    7054             : static int
    7055    22975137 : unify_no_common_base (bool explain_p, enum template_base_result r,
    7056             :                       tree parm, tree arg)
    7057             : {
    7058    22975137 :   if (explain_p)
    7059         745 :     switch (r)
    7060             :       {
    7061           4 :       case tbr_ambiguous_baseclass:
    7062           4 :         inform (input_location, "  %qT is an ambiguous base class of %qT",
    7063             :                 parm, arg);
    7064           4 :         break;
    7065         741 :       default:
    7066         741 :         inform (input_location, "  %qT is not derived from %qT", arg, parm);
    7067         741 :         break;
    7068             :       }
    7069    22975137 :   return unify_invalid (explain_p);
    7070             : }
    7071             : 
    7072             : static int
    7073          32 : unify_inconsistent_template_template_parameters (bool explain_p)
    7074             : {
    7075          32 :   if (explain_p)
    7076          13 :     inform (input_location,
    7077             :             "  template parameters of a template template argument are "
    7078             :             "inconsistent with other deduced template arguments");
    7079          32 :   return unify_invalid (explain_p);
    7080             : }
    7081             : 
    7082             : static int
    7083         145 : unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
    7084             : {
    7085           0 :   if (explain_p)
    7086           0 :     inform (input_location,
    7087             :             "  cannot deduce a template for %qT from non-template type %qT",
    7088             :             parm, arg);
    7089           0 :   return unify_invalid (explain_p);
    7090             : }
    7091             : 
    7092             : static int
    7093     1876636 : unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
    7094             : {
    7095           0 :   if (explain_p)
    7096           4 :     inform (input_location,
    7097             :             "  template argument %qE does not match %qE", arg, parm);
    7098           0 :   return unify_invalid (explain_p);
    7099             : }
    7100             : 
    7101             : /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
    7102             :    argument for TYPE, points to an unsuitable object.
    7103             : 
    7104             :    Also adjust the type of the index in C++20 array subobject references.  */
    7105             : 
    7106             : static bool
    7107        3718 : invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
    7108             : {
    7109        3723 :   switch (TREE_CODE (expr))
    7110             :     {
    7111           5 :     CASE_CONVERT:
    7112           5 :       return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
    7113           5 :                                        complain);
    7114             : 
    7115           0 :     case TARGET_EXPR:
    7116           0 :       return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
    7117           0 :                                        complain);
    7118             : 
    7119        1359 :     case CONSTRUCTOR:
    7120        1359 :       {
    7121        6018 :         for (auto &e: CONSTRUCTOR_ELTS (expr))
    7122        2365 :           if (invalid_tparm_referent_p (TREE_TYPE (e.value), e.value, complain))
    7123             :             return true;
    7124             :       }
    7125             :       break;
    7126             : 
    7127         745 :     case ADDR_EXPR:
    7128         745 :       {
    7129         745 :         tree decl = TREE_OPERAND (expr, 0);
    7130             : 
    7131         745 :         if (cxx_dialect >= cxx20)
    7132         274 :           while (TREE_CODE (decl) == COMPONENT_REF
    7133         274 :                  || TREE_CODE (decl) == ARRAY_REF)
    7134             :             {
    7135          29 :               tree &op = TREE_OPERAND (decl, 1);
    7136          29 :               if (TREE_CODE (decl) == ARRAY_REF
    7137           8 :                   && TREE_CODE (op) == INTEGER_CST)
    7138             :                 /* Canonicalize array offsets to ptrdiff_t; how they were
    7139             :                    written doesn't matter for subobject identity.  */
    7140           8 :                 op = fold_convert (ptrdiff_type_node, op);
    7141          29 :               decl = TREE_OPERAND (decl, 0);
    7142             :             }
    7143             : 
    7144         745 :         if (!VAR_OR_FUNCTION_DECL_P (decl))
    7145             :           {
    7146          15 :             if (complain & tf_error)
    7147          19 :               error_at (cp_expr_loc_or_input_loc (expr),
    7148             :                         "%qE is not a valid template argument of type %qT "
    7149             :                         "because %qE is not a variable or function",
    7150             :                         expr, type, decl);
    7151          15 :             return true;
    7152             :           }
    7153         730 :         else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
    7154             :           {
    7155           2 :             if (complain & tf_error)
    7156           3 :               error_at (cp_expr_loc_or_input_loc (expr),
    7157             :                         "%qE is not a valid template argument of type %qT "
    7158             :                         "in C++98 because %qD does not have external linkage",
    7159             :                         expr, type, decl);
    7160           2 :             return true;
    7161             :           }
    7162         728 :         else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
    7163         728 :                  && decl_linkage (decl) == lk_none)
    7164             :           {
    7165           3 :             if (complain & tf_error)
    7166           6 :               error_at (cp_expr_loc_or_input_loc (expr),
    7167             :                         "%qE is not a valid template argument of type %qT "
    7168             :                         "because %qD has no linkage", expr, type, decl);
    7169           3 :             return true;
    7170             :           }
    7171             :         /* C++17: For a non-type template-parameter of reference or pointer
    7172             :            type, the value of the constant expression shall not refer to (or
    7173             :            for a pointer type, shall not be the address of):
    7174             :            * a subobject (4.5),
    7175             :            * a temporary object (15.2),
    7176             :            * a string literal (5.13.5),
    7177             :            * the result of a typeid expression (8.2.8), or
    7178             :            * a predefined __func__ variable (11.4.1).  */
    7179         725 :         else if (VAR_P (decl) && DECL_ARTIFICIAL (decl))
    7180             :           {
    7181           3 :             if (complain & tf_error)
    7182           3 :               error ("the address of %qD is not a valid template argument",
    7183             :                      decl);
    7184           3 :             return true;
    7185             :           }
    7186         722 :         else if (cxx_dialect < cxx20
    7187        1205 :                  && !(same_type_ignoring_top_level_qualifiers_p
    7188         483 :                       (strip_array_types (TREE_TYPE (type)),
    7189         483 :                        strip_array_types (TREE_TYPE (decl)))))
    7190             :           {
    7191           6 :             if (complain & tf_error)
    7192           4 :               error ("the address of the %qT subobject of %qD is not a "
    7193           4 :                      "valid template argument", TREE_TYPE (type), decl);
    7194           6 :             return true;
    7195             :           }
    7196         716 :         else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
    7197             :           {
    7198           2 :             if (complain & tf_error)
    7199           2 :               error ("the address of %qD is not a valid template argument "
    7200             :                      "because it does not have static storage duration",
    7201             :                      decl);
    7202           2 :             return true;
    7203             :           }
    7204             :       }
    7205             :       break;
    7206             : 
    7207        1614 :     default:
    7208        1614 :       if (!INDIRECT_TYPE_P (type))
    7209             :         /* We're only concerned about pointers and references here.  */;
    7210          72 :       else if (cxx_dialect >= cxx11 && integer_zerop (expr))
    7211             :         /* Null pointer values are OK in C++11.  */;
    7212             :       else
    7213             :         {
    7214          11 :           if (VAR_P (expr))
    7215             :             {
    7216           8 :               if (complain & tf_error)
    7217           8 :                 error ("%qD is not a valid template argument "
    7218             :                        "because %qD is a variable, not the address of "
    7219             :                        "a variable", expr, expr);
    7220           8 :               return true;
    7221             :             }
    7222             :           else
    7223             :             {
    7224           3 :               if (complain & tf_error)
    7225           1 :                 error ("%qE is not a valid template argument for %qT "
    7226             :                        "because it is not the address of a variable",
    7227             :                        expr, type);
    7228           3 :               return true;
    7229             :             }
    7230             :         }
    7231             :     }
    7232             :   return false;
    7233             : 
    7234             : }
    7235             : 
    7236             : /* Return a VAR_DECL for the C++20 template parameter object corresponding to
    7237             :    template argument EXPR.  */
    7238             : 
    7239             : static tree
    7240         601 : create_template_parm_object (tree expr, tsubst_flags_t complain)
    7241             : {
    7242         601 :   if (TREE_CODE (expr) == TARGET_EXPR)
    7243         601 :     expr = TARGET_EXPR_INITIAL (expr);
    7244             : 
    7245         601 :   if (!TREE_CONSTANT (expr))
    7246             :     {
    7247           1 :       if ((complain & tf_error)
    7248           1 :           && require_rvalue_constant_expression (expr))
    7249           1 :         cxx_constant_value (expr);
    7250           1 :       return error_mark_node;
    7251             :     }
    7252         600 :   if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
    7253           0 :     return error_mark_node;
    7254             : 
    7255             :   /* This is no longer a compound literal.  */
    7256         600 :   gcc_assert (!TREE_HAS_CONSTRUCTOR (expr));
    7257             : 
    7258         600 :   return get_template_parm_object (expr, mangle_template_parm_object (expr));
    7259             : }
    7260             : 
    7261             : /* The template arguments corresponding to template parameter objects of types
    7262             :    that contain pointers to members.  */
    7263             : 
    7264             : static GTY(()) hash_map<tree, tree> *tparm_obj_values;
    7265             : 
    7266             : /* Find or build an nttp object for (already-validated) EXPR with name
    7267             :    NAME.  */
    7268             : 
    7269             : tree
    7270         602 : get_template_parm_object (tree expr, tree name)
    7271             : {
    7272         602 :   tree decl = get_global_binding (name);
    7273         602 :   if (decl)
    7274             :     return decl;
    7275             : 
    7276         168 :   tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
    7277         168 :   decl = create_temporary_var (type);
    7278         168 :   DECL_NTTP_OBJECT_P (decl) = true;
    7279         168 :   DECL_CONTEXT (decl) = NULL_TREE;
    7280         168 :   TREE_STATIC (decl) = true;
    7281         168 :   DECL_DECLARED_CONSTEXPR_P (decl) = true;
    7282         168 :   TREE_READONLY (decl) = true;
    7283         168 :   DECL_NAME (decl) = name;
    7284         168 :   SET_DECL_ASSEMBLER_NAME (decl, name);
    7285         168 :   comdat_linkage (decl);
    7286             : 
    7287         168 :   if (!zero_init_p (type))
    7288             :     {
    7289             :       /* If EXPR contains any PTRMEM_CST, they will get clobbered by
    7290             :          lower_var_init before we're done mangling.  So store the original
    7291             :          value elsewhere.  */
    7292          14 :       tree copy = unshare_constructor (expr);
    7293          14 :       hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
    7294             :     }
    7295             : 
    7296         168 :   pushdecl_top_level_and_finish (decl, expr);
    7297             : 
    7298         168 :   return decl;
    7299             : }
    7300             : 
    7301             : /* Return the actual template argument corresponding to template parameter
    7302             :    object VAR.  */
    7303             : 
    7304             : tree
    7305        1230 : tparm_object_argument (tree var)
    7306             : {
    7307        1230 :   if (zero_init_p (TREE_TYPE (var)))
    7308        1215 :     return DECL_INITIAL (var);
    7309          15 :   return *(tparm_obj_values->get (var));
    7310             : }
    7311             : 
    7312             : /* Attempt to convert the non-type template parameter EXPR to the
    7313             :    indicated TYPE.  If the conversion is successful, return the
    7314             :    converted value.  If the conversion is unsuccessful, return
    7315             :    NULL_TREE if we issued an error message, or error_mark_node if we
    7316             :    did not.  We issue error messages for out-and-out bad template
    7317             :    parameters, but not simply because the conversion failed, since we
    7318             :    might be just trying to do argument deduction.  Both TYPE and EXPR
    7319             :    must be non-dependent.
    7320             : 
    7321             :    The conversion follows the special rules described in
    7322             :    [temp.arg.nontype], and it is much more strict than an implicit
    7323             :    conversion.
    7324             : 
    7325             :    This function is called twice for each template argument (see
    7326             :    lookup_template_class for a more accurate description of this
    7327             :    problem). This means that we need to handle expressions which
    7328             :    are not valid in a C++ source, but can be created from the
    7329             :    first call (for instance, casts to perform conversions). These
    7330             :    hacks can go away after we fix the double coercion problem.  */
    7331             : 
    7332             : static tree
    7333    84074071 : convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
    7334             : {
    7335    84074071 :   tree expr_type;
    7336    84074071 :   location_t loc = cp_expr_loc_or_input_loc (expr);
    7337             : 
    7338             :   /* Detect immediately string literals as invalid non-type argument.
    7339             :      This special-case is not needed for correctness (we would easily
    7340             :      catch this later), but only to provide better diagnostic for this
    7341             :      common user mistake. As suggested by DR 100, we do not mention
    7342             :      linkage issues in the diagnostic as this is not the point.  */
    7343    84074071 :   if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
    7344             :     {
    7345           6 :       if (complain & tf_error)
    7346           6 :         error ("%qE is not a valid template argument for type %qT "
    7347             :                "because string literals can never be used in this context",
    7348             :                expr, type);
    7349           6 :       return NULL_TREE;
    7350             :     }
    7351             : 
    7352             :   /* Add the ADDR_EXPR now for the benefit of
    7353             :      value_dependent_expression_p.  */
    7354        1410 :   if (TYPE_PTROBV_P (type)
    7355    84074750 :       && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
    7356             :     {
    7357         112 :       expr = decay_conversion (expr, complain);
    7358         112 :       if (expr == error_mark_node)
    7359             :         return error_mark_node;
    7360             :     }
    7361             : 
    7362             :   /* If we are in a template, EXPR may be non-dependent, but still
    7363             :      have a syntactic, rather than semantic, form.  For example, EXPR
    7364             :      might be a SCOPE_REF, rather than the VAR_DECL to which the
    7365             :      SCOPE_REF refers.  Preserving the qualifying scope is necessary
    7366             :      so that access checking can be performed when the template is
    7367             :      instantiated -- but here we need the resolved form so that we can
    7368             :      convert the argument.  */
    7369    84074065 :   bool non_dep = false;
    7370         421 :   if (TYPE_REF_OBJ_P (type)
    7371    84074381 :       && has_value_dependent_address (expr))
    7372             :     /* If we want the address and it's value-dependent, don't fold.  */;
    7373    84074044 :   else if (processing_template_decl
    7374    84074044 :            && !instantiation_dependent_expression_p (expr))
    7375             :     non_dep = true;
    7376    84074065 :   if (error_operand_p (expr))
    7377           0 :     return error_mark_node;
    7378    84074065 :   expr_type = TREE_TYPE (expr);
    7379             : 
    7380             :   /* If the argument is non-dependent, perform any conversions in
    7381             :      non-dependent context as well.  */
    7382    84074065 :   processing_template_decl_sentinel s (non_dep);
    7383    16188055 :   if (non_dep)
    7384    16188055 :     expr = instantiate_non_dependent_expr_internal (expr, complain);
    7385             : 
    7386    84074065 :   bool val_dep_p = value_dependent_expression_p (expr);
    7387    84074065 :   if (val_dep_p)
    7388    14745285 :     expr = canonicalize_expr_argument (expr, complain);
    7389             :   else
    7390    69328780 :     STRIP_ANY_LOCATION_WRAPPER (expr);
    7391             : 
    7392             :   /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
    7393             :      to a non-type argument of "nullptr".  */
    7394    84074065 :   if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
    7395          55 :     expr = fold_simple (convert (type, expr));
    7396             : 
    7397             :   /* In C++11, integral or enumeration non-type template arguments can be
    7398             :      arbitrary constant expressions.  Pointer and pointer to
    7399             :      member arguments can be general constant expressions that evaluate
    7400             :      to a null value, but otherwise still need to be of a specific form.  */
    7401    84074065 :   if (cxx_dialect >= cxx11)
    7402             :     {
    7403    84039417 :       if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
    7404             :         /* A PTRMEM_CST is already constant, and a valid template
    7405             :            argument for a parameter of pointer to member type, we just want
    7406             :            to leave it in that form rather than lower it to a
    7407             :            CONSTRUCTOR.  */;
    7408    84038766 :       else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
    7409        2477 :                || cxx_dialect >= cxx17)
    7410             :         {
    7411             :           /* C++17: A template-argument for a non-type template-parameter shall
    7412             :              be a converted constant expression (8.20) of the type of the
    7413             :              template-parameter.  */
    7414    84038170 :           expr = build_converted_constant_expr (type, expr, complain);
    7415    84038170 :           if (expr == error_mark_node)
    7416             :             /* Make sure we return NULL_TREE only if we have really issued
    7417             :                an error, as described above.  */
    7418         222 :             return (complain & tf_error) ? NULL_TREE : error_mark_node;
    7419    84037997 :           else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
    7420             :             {
    7421          21 :               IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
    7422          21 :               return expr;
    7423             :             }
    7424    84037976 :           expr = maybe_constant_value (expr, NULL_TREE, mce_true);
    7425    84037976 :           expr = convert_from_reference (expr);
    7426             :           /* EXPR may have become value-dependent.  */
    7427    84037976 :           val_dep_p = value_dependent_expression_p (expr);
    7428             :         }
    7429         596 :       else if (TYPE_PTR_OR_PTRMEM_P (type))
    7430             :         {
    7431         494 :           tree folded = maybe_constant_value (expr, NULL_TREE, mce_true);
    7432         575 :           if (TYPE_PTR_P (type) ? integer_zerop (folded)
    7433          81 :               : null_member_pointer_value_p (folded))
    7434    84073871 :             expr = folded;
    7435             :         }
    7436             :     }
    7437             : 
    7438    84073871 :   if (TYPE_REF_P (type))
    7439         403 :     expr = mark_lvalue_use (expr);
    7440             :   else
    7441    84073468 :     expr = mark_rvalue_use (expr);
    7442             : 
    7443             :   /* HACK: Due to double coercion, we can get a
    7444             :      NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
    7445             :      which is the tree that we built on the first call (see
    7446             :      below when coercing to reference to object or to reference to
    7447             :      function). We just strip everything and get to the arg.
    7448             :      See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
    7449             :      for examples.  */
    7450    84073871 :   if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
    7451             :     {
    7452             :       /* Check this before we strip *& to avoid redundancy.  */
    7453         403 :       if (!mark_single_function (expr, complain))
    7454           0 :         return error_mark_node;
    7455             : 
    7456         403 :       tree probe_type, probe = expr;
    7457         403 :       if (REFERENCE_REF_P (probe))
    7458         294 :         probe = TREE_OPERAND (probe, 0);
    7459         403 :       probe_type = TREE_TYPE (probe);
    7460         403 :       if (TREE_CODE (probe) == NOP_EXPR)
    7461             :         {
    7462             :           /* ??? Maybe we could use convert_from_reference here, but we
    7463             :              would need to relax its constraints because the NOP_EXPR
    7464             :              could actually change the type to something more cv-qualified,
    7465             :              and this is not folded by convert_from_reference.  */
    7466         252 :           tree addr = TREE_OPERAND (probe, 0);
    7467         252 :           if (TYPE_REF_P (probe_type)
    7468         252 :               && TREE_CODE (addr) == ADDR_EXPR
    7469         250 :               && TYPE_PTR_P (TREE_TYPE (addr))
    7470         502 :               && (same_type_ignoring_top_level_qualifiers_p
    7471         250 :                   (TREE_TYPE (probe_type),
    7472         250 :                    TREE_TYPE (TREE_TYPE (addr)))))
    7473             :             {
    7474         236 :               expr = TREE_OPERAND (addr, 0);
    7475         236 :               expr_type = TREE_TYPE (probe_type);
    7476             :             }
    7477             :         }
    7478             :     }
    7479             : 
    7480             :   /* [temp.arg.nontype]/5, bullet 1
    7481             : 
    7482             :      For a non-type template-parameter of integral or enumeration type,
    7483             :      integral promotions (_conv.prom_) and integral conversions
    7484             :      (_conv.integral_) are applied.  */
    7485    84073871 :   if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
    7486    84073871 :       || SCALAR_FLOAT_TYPE_P (type))
    7487             :     {
    7488    84070430 :       if (cxx_dialect < cxx11)
    7489             :         {
    7490       34197 :           tree t = build_converted_constant_expr (type, expr, complain);
    7491       34197 :           t = maybe_constant_value (t);
    7492       34197 :           if (t != error_mark_node)
    7493    84070430 :             expr = t;
    7494             :         }
    7495             : 
    7496    84070430 :       if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
    7497           9 :         return error_mark_node;
    7498             : 
    7499             :       /* Notice that there are constant expressions like '4 % 0' which
    7500             :          do not fold into integer constants.  */
    7501    84070421 :       if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
    7502             :         {
    7503         120 :           if (complain & tf_error)
    7504             :             {
    7505          78 :               int errs = errorcount, warns = warningcount + werrorcount;
    7506          78 :               if (!require_potential_constant_expression (expr))
    7507          23 :                 expr = error_mark_node;
    7508             :               else
    7509          55 :                 expr = cxx_constant_value (expr);
    7510          78 :               if (errorcount > errs || warningcount + werrorcount > warns)
    7511          63 :                 inform (loc, "in template argument for type %qT", type);
    7512          78 :               if (expr == error_mark_node)
    7513             :                 return NULL_TREE;
    7514             :               /* else cxx_constant_value complained but gave us
    7515             :                  a real constant, so go ahead.  */
    7516           4 :               if (!CONSTANT_CLASS_P (expr))
    7517             :                 {
    7518             :                   /* Some assemble time constant expressions like
    7519             :                      (intptr_t)&&lab1 - (intptr_t)&&lab2 or
    7520             :                      4 + (intptr_t)&&var satisfy reduced_constant_expression_p
    7521             :                      as we can emit them into .rodata initializers of
    7522             :                      variables, yet they can't fold into an INTEGER_CST at
    7523             :                      compile time.  Refuse them here.  */
    7524           0 :                   gcc_checking_assert (reduced_constant_expression_p (expr));
    7525           0 :                   error_at (loc, "template argument %qE for type %qT not "
    7526             :                                  "a compile-time constant", expr, type);
    7527           0 :                   return NULL_TREE;
    7528             :                 }
    7529             :             }
    7530             :           else
    7531             :             return NULL_TREE;
    7532             :         }
    7533             : 
    7534             :       /* Avoid typedef problems.  */
    7535    84070305 :       if (TREE_TYPE (expr) != type)
    7536      257589 :         expr = fold_convert (type, expr);
    7537             :     }
    7538             :   /* [temp.arg.nontype]/5, bullet 2
    7539             : 
    7540             :      For a non-type template-parameter of type pointer to object,
    7541             :      qualification conversions (_conv.qual_) and the array-to-pointer
    7542             :      conversion (_conv.array_) are applied.  */
    7543        3441 :   else if (TYPE_PTROBV_P (type))
    7544             :     {
    7545         639 :       tree decayed = expr;
    7546             : 
    7547             :       /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
    7548             :          decay_conversion or an explicit cast.  If it's a problematic cast,
    7549             :          we'll complain about it below.  */
    7550         639 :       if (TREE_CODE (expr) == NOP_EXPR)
    7551             :         {
    7552         181 :           tree probe = expr;
    7553         181 :           STRIP_NOPS (probe);
    7554         181 :           if (TREE_CODE (probe) == ADDR_EXPR
    7555         181 :               && TYPE_PTR_P (TREE_TYPE (probe)))
    7556             :             {
    7557         179 :               expr = probe;
    7558         179 :               expr_type = TREE_TYPE (expr);
    7559             :             }
    7560             :         }
    7561             : 
    7562             :       /* [temp.arg.nontype]/1  (TC1 version, DR 49):
    7563             : 
    7564             :          A template-argument for a non-type, non-template template-parameter
    7565             :          shall be one of: [...]
    7566             : 
    7567             :          -- the name of a non-type template-parameter;
    7568             :          -- the address of an object or function with external linkage, [...]
    7569             :             expressed as "& id-expression" where the & is optional if the name
    7570             :             refers to a function or array, or if the corresponding
    7571             :             template-parameter is a reference.
    7572             : 
    7573             :         Here, we do not care about functions, as they are invalid anyway
    7574             :         for a parameter of type pointer-to-object.  */
    7575             : 
    7576         639 :       if (val_dep_p)
    7577             :         /* Non-type template parameters are OK.  */
    7578             :         ;
    7579         569 :       else if (cxx_dialect >= cxx11 && integer_zerop (expr))
    7580             :         /* Null pointer values are OK in C++11.  */;
    7581         538 :       else if (TREE_CODE (expr) != ADDR_EXPR
    7582          54 :                && !INDIRECT_TYPE_P (expr_type))
    7583             :         /* Other values, like integer constants, might be valid
    7584             :            non-type arguments of some other type.  */
    7585          43 :         return error_mark_node;
    7586         495 :       else if (invalid_tparm_referent_p (type, expr, complain))
    7587             :         return NULL_TREE;
    7588             : 
    7589         555 :       expr = decayed;
    7590             : 
    7591         555 :       expr = perform_qualification_conversions (type, expr);
    7592         555 :       if (expr == error_mark_node)
    7593             :         return error_mark_node;
    7594             :     }
    7595             :   /* [temp.arg.nontype]/5, bullet 3
    7596             : 
    7597             :      For a non-type template-parameter of type reference to object, no
    7598             :      conversions apply. The type referred to by the reference may be more
    7599             :      cv-qualified than the (otherwise identical) type of the
    7600             :      template-argument. The template-parameter is bound directly to the
    7601             :      template-argument, which must be an lvalue.  */
    7602        2802 :   else if (TYPE_REF_OBJ_P (type))
    7603             :     {
    7604         306 :       if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
    7605             :                                                       expr_type))
    7606           0 :         return error_mark_node;
    7607             : 
    7608         306 :       if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
    7609             :         {
    7610           4 :           if (complain & tf_error)
    7611           2 :             error ("%qE is not a valid template argument for type %qT "
    7612             :                    "because of conflicts in cv-qualification", expr, type);
    7613           4 :           return NULL_TREE;
    7614             :         }
    7615             : 
    7616         302 :       if (!lvalue_p (expr))
    7617             :         {
    7618           8 :           if (complain & tf_error)
    7619           8 :             error ("%qE is not a valid template argument for type %qT "
    7620             :                    "because it is not an lvalue", expr, type);
    7621           8 :           return NULL_TREE;
    7622             :         }
    7623             : 
    7624             :       /* [temp.arg.nontype]/1
    7625             : 
    7626             :          A template-argument for a non-type, non-template template-parameter
    7627             :          shall be one of: [...]
    7628             : 
    7629             :          -- the address of an object or function with external linkage.  */
    7630         294 :       if (INDIRECT_REF_P (expr)
    7631         294 :           && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
    7632             :         {
    7633          36 :           expr = TREE_OPERAND (expr, 0);
    7634          36 :           if (DECL_P (expr))
    7635             :             {
    7636           4 :               if (complain & tf_error)
    7637           4 :                 error ("%q#D is not a valid template argument for type %qT "
    7638             :                        "because a reference variable does not have a constant "
    7639             :                        "address", expr, type);
    7640           4 :               return NULL_TREE;
    7641             :             }
    7642             :         }
    7643             : 
    7644         290 :       if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
    7645             :         /* OK, dependent reference.  We don't want to ask whether a DECL is
    7646             :            itself value-dependent, since what we want here is its address.  */;
    7647             :       else
    7648             :         {
    7649         258 :           expr = build_address (expr);
    7650             : 
    7651         258 :           if (invalid_tparm_referent_p (type, expr, complain))
    7652             :             return NULL_TREE;
    7653             :         }
    7654             : 
    7655         289 :       if (!same_type_p (type, TREE_TYPE (expr)))
    7656         257 :         expr = build_nop (type, expr);
    7657             :     }
    7658             :   /* [temp.arg.nontype]/5, bullet 4
    7659             : 
    7660             :      For a non-type template-parameter of type pointer to function, only
    7661             :      the function-to-pointer conversion (_conv.func_) is applied. If the
    7662             :      template-argument represents a set of overloaded functions (or a
    7663             :      pointer to such), the matching function is selected from the set
    7664             :      (_over.over_).  */
    7665        2496 :   else if (TYPE_PTRFN_P (type))
    7666             :     {
    7667             :       /* If the argument is a template-id, we might not have enough
    7668             :          context information to decay the pointer.  */
    7669         709 :       if (!type_unknown_p (expr_type))
    7670             :         {
    7671         428 :           expr = decay_conversion (expr, complain);
    7672         428 :           if (expr == error_mark_node)
    7673             :             return error_mark_node;
    7674             :         }
    7675             : 
    7676         701 :       if (cxx_dialect >= cxx11 && integer_zerop (expr))
    7677             :         /* Null pointer values are OK in C++11.  */
    7678          10 :         return perform_qualification_conversions (type, expr);
    7679             : 
    7680         691 :       expr = convert_nontype_argument_function (type, expr, complain);
    7681         691 :       if (!expr || expr == error_mark_node)
    7682             :         return expr;
    7683             :     }
    7684             :   /* [temp.arg.nontype]/5, bullet 5
    7685             : 
    7686             :      For a non-type template-parameter of type reference to function, no
    7687             :      conversions apply. If the template-argument represents a set of
    7688             :      overloaded functions, the matching function is selected from the set
    7689             :      (_over.over_).  */
    7690        1787 :   else if (TYPE_REFFN_P (type))
    7691             :     {
    7692          97 :       if (TREE_CODE (expr) == ADDR_EXPR)
    7693             :         {
    7694           0 :           if (complain & tf_error)
    7695             :             {
    7696           0 :               error ("%qE is not a valid template argument for type %qT "
    7697             :                      "because it is a pointer", expr, type);
    7698           0 :               inform (input_location, "try using %qE instead",
    7699           0 :                       TREE_OPERAND (expr, 0));
    7700             :             }
    7701           0 :           return NULL_TREE;
    7702             :         }
    7703             : 
    7704          97 :       expr = convert_nontype_argument_function (type, expr, complain);
    7705          97 :       if (!expr || expr == error_mark_node)
    7706             :         return expr;
    7707             :     }
    7708             :   /* [temp.arg.nontype]/5, bullet 6
    7709             : 
    7710             :      For a non-type template-parameter of type pointer to member function,
    7711             :      no conversions apply. If the template-argument represents a set of
    7712             :      overloaded member functions, the matching member function is selected
    7713             :      from the set (_over.over_).  */
    7714        1690 :   else if (TYPE_PTRMEMFUNC_P (type))
    7715             :     {
    7716         639 :       expr = instantiate_type (type, expr, tf_none);
    7717         639 :       if (expr == error_mark_node)
    7718             :         return error_mark_node;
    7719             : 
    7720             :       /* [temp.arg.nontype] bullet 1 says the pointer to member
    7721             :          expression must be a pointer-to-member constant.  */
    7722         595 :       if (!val_dep_p
    7723         595 :           && !check_valid_ptrmem_cst_expr (type, expr, complain))
    7724             :         return NULL_TREE;
    7725             : 
    7726             :       /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
    7727             :          into a CONSTRUCTOR, so build up a new PTRMEM_CST instead.  */
    7728         568 :       if (fnptr_conv_p (type, TREE_TYPE (expr)))
    7729           2 :         expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
    7730             :     }
    7731             :   /* [temp.arg.nontype]/5, bullet 7
    7732             : 
    7733             :      For a non-type template-parameter of type pointer to data member,
    7734             :      qualification conversions (_conv.qual_) are applied.  */
    7735        1051 :   else if (TYPE_PTRDATAMEM_P (type))
    7736             :     {
    7737             :       /* [temp.arg.nontype] bullet 1 says the pointer to member
    7738             :          expression must be a pointer-to-member constant.  */
    7739         419 :       if (!val_dep_p
    7740         419 :           && !check_valid_ptrmem_cst_expr (type, expr, complain))
    7741             :         return NULL_TREE;
    7742             : 
    7743         391 :       expr = perform_qualification_conversions (type, expr);
    7744         391 :       if (expr == error_mark_node)
    7745             :         return expr;
    7746             :     }
    7747         632 :   else if (NULLPTR_TYPE_P (type))
    7748             :     {
    7749          15 :       if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
    7750             :         {
    7751           0 :           if (complain & tf_error)
    7752           0 :             error ("%qE is not a valid template argument for type %qT "
    7753           0 :                    "because it is of type %qT", expr, type, TREE_TYPE (expr));
    7754           0 :           return NULL_TREE;
    7755             :         }
    7756             :       return expr;
    7757             :     }
    7758         617 :   else if (CLASS_TYPE_P (type))
    7759             :     {
    7760             :       /* Replace the argument with a reference to the corresponding template
    7761             :          parameter object.  */
    7762         617 :       if (!val_dep_p)
    7763         601 :         expr = create_template_parm_object (expr, complain);
    7764         617 :       if (expr == error_mark_node)
    7765             :         return NULL_TREE;
    7766             :     }
    7767             :   /* A template non-type parameter must be one of the above.  */
    7768             :   else
    7769           0 :     gcc_unreachable ();
    7770             : 
    7771             :   /* Sanity check: did we actually convert the argument to the
    7772             :      right type?  */
    7773    84073448 :   gcc_assert (same_type_ignoring_top_level_qualifiers_p
    7774             :               (type, TREE_TYPE (expr)));
    7775    84073448 :   return convert_from_reference (expr);
    7776    84074071 : }
    7777             : 
    7778             : /* Subroutine of coerce_template_template_parms, which returns 1 if
    7779             :    PARM_PARM and ARG_PARM match using the rule for the template
    7780             :    parameters of template template parameters. Both PARM and ARG are
    7781             :    template parameters; the rest of the arguments are the same as for
    7782             :    coerce_template_template_parms.
    7783             :  */
    7784             : static int
    7785      748182 : coerce_template_template_parm (tree parm,
    7786             :                               tree arg,
    7787             :                               tsubst_flags_t complain,
    7788             :                               tree in_decl,
    7789             :                               tree outer_args)
    7790             : {
    7791      748182 :   if (arg == NULL_TREE || error_operand_p (arg)
    7792     1496356 :       || parm == NULL_TREE || error_operand_p (parm))
    7793             :     return 0;
    7794             : 
    7795      748174 :   if (TREE_CODE (arg) != TREE_CODE (parm))
    7796             :     return 0;
    7797             : 
    7798      747891 :   switch (TREE_CODE (parm))
    7799             :     {
    7800          26 :     case TEMPLATE_DECL:
    7801             :       /* We encounter instantiations of templates like
    7802             :          template <template <template <class> class> class TT>
    7803             :          class C;  */
    7804          26 :       {
    7805          26 :         if (!coerce_template_template_parms
    7806          26 :             (parm, arg, complain, in_decl, outer_args))
    7807             :           return 0;
    7808             :       }
    7809             :       /* Fall through.  */
    7810             : 
    7811      746830 :     case TYPE_DECL:
    7812      746830 :       if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
    7813      746830 :           && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
    7814             :         /* Argument is a parameter pack but parameter is not.  */
    7815             :         return 0;
    7816             :       break;
    7817             : 
    7818        1061 :     case PARM_DECL:
    7819             :       /* The tsubst call is used to handle cases such as
    7820             : 
    7821             :            template <int> class C {};
    7822             :            template <class T, template <T> class TT> class D {};
    7823             :            D<int, C> d;
    7824             : 
    7825             :          i.e. the parameter list of TT depends on earlier parameters.  */
    7826        1061 :       if (!uses_template_parms (TREE_TYPE (arg)))
    7827             :         {
    7828        1019 :           tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
    7829        1019 :           if (!uses_template_parms (t)
    7830        1019 :               && !same_type_p (t, TREE_TYPE (arg)))
    7831             :             return 0;
    7832             :         }
    7833             : 
    7834        1029 :       if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
    7835        1029 :           && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
    7836             :         /* Argument is a parameter pack but parameter is not.  */
    7837             :         return 0;
    7838             : 
    7839             :       break;
    7840             : 
    7841           0 :     default:
    7842           0 :       gcc_unreachable ();
    7843             :     }
    7844             : 
    7845             :   return 1;
    7846             : }
    7847             : 
    7848             : /* Coerce template argument list ARGLIST for use with template
    7849             :    template-parameter TEMPL.  */
    7850             : 
    7851             : static tree
    7852      164171 : coerce_template_args_for_ttp (tree templ, tree arglist,
    7853             :                               tsubst_flags_t complain)
    7854             : {
    7855             :   /* Consider an example where a template template parameter declared as
    7856             : 
    7857             :      template <class T, class U = std::allocator<T> > class TT
    7858             : 
    7859             :      The template parameter level of T and U are one level larger than
    7860             :      of TT.  To proper process the default argument of U, say when an
    7861             :      instantiation `TT<int>' is seen, we need to build the full
    7862             :      arguments containing {int} as the innermost level.  Outer levels,
    7863             :      available when not appearing as default template argument, can be
    7864             :      obtained from the arguments of the enclosing template.
    7865             : 
    7866             :      Suppose that TT is later substituted with std::vector.  The above
    7867             :      instantiation is `TT<int, std::allocator<T> >' with TT at
    7868             :      level 1, and T at level 2, while the template arguments at level 1
    7869             :      becomes {std::vector} and the inner level 2 is {int}.  */
    7870             : 
    7871      164171 :   tree outer = DECL_CONTEXT (templ);
    7872      164171 :   if (outer)
    7873      101322 :     outer = generic_targs_for (outer);
    7874       62849 :   else if (current_template_parms)
    7875             :     {
    7876             :       /* This is an argument of the current template, so we haven't set
    7877             :          DECL_CONTEXT yet.  We can also get here when level-lowering a
    7878             :          bound ttp. */
    7879             :       tree relevant_template_parms;
    7880             : 
    7881             :       /* Parameter levels that are greater than the level of the given
    7882             :          template template parm are irrelevant.  */
    7883             :       relevant_template_parms = current_template_parms;
    7884       59197 :       while (TMPL_PARMS_DEPTH (relevant_template_parms)
    7885      118394 :              != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
    7886           0 :         relevant_template_parms = TREE_CHAIN (relevant_template_parms);
    7887             : 
    7888       59197 :       outer = template_parms_to_args (relevant_template_parms);
    7889             :     }
    7890             : 
    7891      164171 :   if (outer)
    7892      160519 :     arglist = add_to_template_args (outer, arglist);
    7893             : 
    7894      164171 :   tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
    7895      164171 :   return coerce_template_parms (parmlist, arglist, templ, complain);
    7896             : }
    7897             : 
    7898             : /* A cache of template template parameters with match-all default
    7899             :    arguments.  */
    7900             : static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
    7901             : 
    7902             : /* T is a bound template template-parameter.  Copy its arguments into default
    7903             :    arguments of the template template-parameter's template parameters.  */
    7904             : 
    7905             : static tree
    7906          48 : add_defaults_to_ttp (tree otmpl)
    7907             : {
    7908          82 :   if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
    7909          18 :     return *c;
    7910             : 
    7911          30 :   tree ntmpl = copy_node (otmpl);
    7912             : 
    7913          30 :   tree ntype = copy_node (TREE_TYPE (otmpl));
    7914          30 :   TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
    7915          30 :   TYPE_MAIN_VARIANT (ntype) = ntype;
    7916          30 :   TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
    7917          30 :   TYPE_NAME (ntype) = ntmpl;
    7918          30 :   SET_TYPE_STRUCTURAL_EQUALITY (ntype);
    7919             : 
    7920          30 :   tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
    7921          30 :     = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
    7922          30 :   TEMPLATE_PARM_DECL (idx) = ntmpl;
    7923          30 :   TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
    7924             : 
    7925          30 :   tree oparms = DECL_TEMPLATE_PARMS (otmpl);
    7926          30 :   tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
    7927          30 :   TREE_CHAIN (parms) = TREE_CHAIN (oparms);
    7928          30 :   tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
    7929          68 :   for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
    7930             :     {
    7931          38 :       tree o = TREE_VEC_ELT (vec, i);
    7932          38 :       if (!template_parameter_pack_p (TREE_VALUE (o)))
    7933             :         {
    7934          34 :           tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
    7935          34 :           TREE_PURPOSE (n) = any_targ_node;
    7936             :         }
    7937             :     }
    7938             : 
    7939          30 :   hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
    7940          30 :   return ntmpl;
    7941             : }
    7942             : 
    7943             : /* ARG is a bound potential template template-argument, and PARGS is a list
    7944             :    of arguments for the corresponding template template-parameter.  Adjust
    7945             :    PARGS as appropriate for application to ARG's template, and if ARG is a
    7946             :    BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
    7947             :    arguments to the template template parameter.  */
    7948             : 
    7949             : static tree
    7950        2719 : coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
    7951             : {
    7952        2719 :   ++processing_template_decl;
    7953        2719 :   tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
    7954        2719 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
    7955             :     {
    7956             :       /* When comparing two template template-parameters in partial ordering,
    7957             :          rewrite the one currently being used as an argument to have default
    7958             :          arguments for all parameters.  */
    7959          48 :       arg_tmpl = add_defaults_to_ttp (arg_tmpl);
    7960          48 :       pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
    7961          48 :       if (pargs != error_mark_node)
    7962          44 :         arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
    7963          88 :                                            TYPE_TI_ARGS (arg));
    7964             :     }
    7965             :   else
    7966             :     {
    7967        2671 :       tree aparms
    7968        2671 :         = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
    7969        2671 :       pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain);
    7970             :     }
    7971        2719 :   --processing_template_decl;
    7972        2719 :   return pargs;
    7973             : }
    7974             : 
    7975             : /* Subroutine of unify for the case when PARM is a
    7976             :    BOUND_TEMPLATE_TEMPLATE_PARM.  */
    7977             : 
    7978             : static int
    7979        3008 : unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
    7980             :                       bool explain_p)
    7981             : {
    7982        3008 :   tree parmvec = TYPE_TI_ARGS (parm);
    7983        6016 :   tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
    7984             : 
    7985             :   /* The template template parm might be variadic and the argument
    7986             :      not, so flatten both argument lists.  */
    7987        3008 :   parmvec = expand_template_argument_pack (parmvec);
    7988        3008 :   argvec = expand_template_argument_pack (argvec);
    7989             : 
    7990        3008 :   if (flag_new_ttp)
    7991             :     {
    7992             :       /* In keeping with P0522R0, adjust P's template arguments
    7993             :          to apply to A's template; then flatten it again.  */
    7994        2719 :       tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
    7995        2719 :       nparmvec = expand_template_argument_pack (nparmvec);
    7996             : 
    7997        2719 :       if (unify (tparms, targs, nparmvec, argvec,
    7998             :                  UNIFY_ALLOW_NONE, explain_p))
    7999             :         return 1;
    8000             : 
    8001             :       /* If the P0522 adjustment eliminated a pack expansion, deduce
    8002             :          empty packs.  */
    8003        2664 :       if (flag_new_ttp
    8004        2664 :           && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
    8005        2664 :           && unify_pack_expansion (tparms, targs, parmvec, argvec,
    8006             :                                    DEDUCE_EXACT, /*sub*/true, explain_p))
    8007             :         return 1;
    8008             :     }
    8009             :   else
    8010             :     {
    8011             :       /* Deduce arguments T, i from TT<T> or TT<i>.
    8012             :          We check each element of PARMVEC and ARGVEC individually
    8013             :          rather than the whole TREE_VEC since they can have
    8014             :          different number of elements, which is allowed under N2555.  */
    8015             : 
    8016         289 :       int len = TREE_VEC_LENGTH (parmvec);
    8017             : 
    8018             :       /* Check if the parameters end in a pack, making them
    8019             :          variadic.  */
    8020         289 :       int parm_variadic_p = 0;
    8021         289 :       if (len > 0
    8022         289 :           && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
    8023             :         parm_variadic_p = 1;
    8024             : 
    8025         597 :       for (int i = 0; i < len - parm_variadic_p; ++i)
    8026             :         /* If the template argument list of P contains a pack
    8027             :            expansion that is not the last template argument, the
    8028             :            entire template argument list is a non-deduced
    8029             :            context.  */
    8030         310 :         if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
    8031        2927 :           return unify_success (explain_p);
    8032             : 
    8033         287 :       if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
    8034           4 :         return unify_too_few_arguments (explain_p,
    8035           4 :                                         TREE_VEC_LENGTH (argvec), len);
    8036             : 
    8037         563 :       for (int i = 0; i < len - parm_variadic_p; ++i)
    8038         300 :         if (unify (tparms, targs,
    8039         300 :                    TREE_VEC_ELT (parmvec, i),
    8040         300 :                    TREE_VEC_ELT (argvec, i),
    8041             :                    UNIFY_ALLOW_NONE, explain_p))
    8042             :           return 1;
    8043             : 
    8044         263 :       if (parm_variadic_p
    8045         263 :           && unify_pack_expansion (tparms, targs,
    8046             :                                    parmvec, argvec,
    8047             :                                    DEDUCE_EXACT,
    8048             :                                    /*subr=*/true, explain_p))
    8049             :         return 1;
    8050             :     }
    8051             : 
    8052             :   return 0;
    8053             : }
    8054             : 
    8055             : /* Return 1 if PARM_TMPL and ARG_TMPL match using rule for
    8056             :    template template parameters.
    8057             : 
    8058             :    Consider the example:
    8059             :      template <class T> class A;
    8060             :      template<template <class U> class TT> class B;
    8061             : 
    8062             :    For B<A>, PARM_TMPL is TT, while ARG_TMPL is A,
    8063             :    and OUTER_ARGS contains A.  */
    8064             : 
    8065             : static int
    8066     6710068 : coerce_template_template_parms (tree parm_tmpl,
    8067             :                                 tree arg_tmpl,
    8068             :                                 tsubst_flags_t complain,
    8069             :                                 tree in_decl,
    8070             :                                 tree outer_args)
    8071             : {
    8072     6710068 :   int nparms, nargs, i;
    8073     6710068 :   tree parm, arg;
    8074     6710068 :   int variadic_p = 0;
    8075             : 
    8076     6710068 :   tree parm_parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm_tmpl));
    8077     6710068 :   tree arg_parms_full = DECL_TEMPLATE_PARMS (arg_tmpl);
    8078     6710068 :   tree arg_parms = INNERMOST_TEMPLATE_PARMS (arg_parms_full);
    8079             : 
    8080     6710068 :   gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
    8081     6710068 :   gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
    8082             : 
    8083     6710068 :   nparms = TREE_VEC_LENGTH (parm_parms);
    8084     6710068 :   nargs = TREE_VEC_LENGTH (arg_parms);
    8085             : 
    8086     6710068 :   if (flag_new_ttp)
    8087             :     {
    8088             :       /* P0522R0: A template template-parameter P is at least as specialized as
    8089             :          a template template-argument A if, given the following rewrite to two
    8090             :          function templates, the function template corresponding to P is at
    8091             :          least as specialized as the function template corresponding to A
    8092             :          according to the partial ordering rules for function templates
    8093             :          ([temp.func.order]). Given an invented class template X with the
    8094             :          template parameter list of A (including default arguments):
    8095             : 
    8096             :          * Each of the two function templates has the same template parameters,
    8097             :          respectively, as P or A.
    8098             : 
    8099             :          * Each function template has a single function parameter whose type is
    8100             :          a specialization of X with template arguments corresponding to the
    8101             :          template parameters from the respective function template where, for
    8102             :          each template parameter PP in the template parameter list of the
    8103             :          function template, a corresponding template argument AA is formed. If
    8104             :          PP declares a parameter pack, then AA is the pack expansion
    8105             :          PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
    8106             : 
    8107             :          If the rewrite produces an invalid type, then P is not at least as
    8108             :          specialized as A.  */
    8109             : 
    8110             :       /* So coerce P's args to apply to A's parms, and then deduce between A's
    8111             :          args and the converted args.  If that succeeds, A is at least as
    8112             :          specialized as P, so they match.*/
    8113     6657981 :       processing_template_decl_sentinel ptds (/*reset*/false);
    8114     6657981 :       ++processing_template_decl;
    8115             : 
    8116     6657981 :       tree pargs = template_parms_level_to_args (parm_parms);
    8117             : 
    8118             :       /* PARM and ARG might be at different template depths, and we want to
    8119             :          pass the right additional levels of args when coercing PARGS to
    8120             :          ARG_PARMS in case we need to do any substitution into non-type
    8121             :          template parameter types.
    8122             : 
    8123             :          OUTER_ARGS are not the right outer levels in this case, as they are
    8124             :          the args we're building up for PARM, and for the coercion we want the
    8125             :          args for ARG.  If DECL_CONTEXT isn't set for a template template
    8126             :          parameter, we can assume that it's in the current scope.  In that case
    8127             :          we might end up adding more levels than needed, but that shouldn't be
    8128             :          a problem; any args we need to refer to are at the right level.  */
    8129     6657981 :       tree ctx = DECL_CONTEXT (arg_tmpl);
    8130     6657981 :       if (!ctx && DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
    8131      256983 :         ctx = current_scope ();
    8132     6657981 :       tree scope_args = NULL_TREE;
    8133     6657981 :       if (tree tinfo = get_template_info (ctx))
    8134      132390 :         scope_args = TI_ARGS (tinfo);
    8135     6657981 :       pargs = add_to_template_args (scope_args, pargs);
    8136             : 
    8137     6657981 :       pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none);
    8138     6657981 :       if (pargs != error_mark_node)
    8139             :         {
    8140     6657044 :           tree targs = make_tree_vec (nargs);
    8141     6657044 :           tree aargs = template_parms_level_to_args (arg_parms);
    8142     6657044 :           if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
    8143             :                       /*explain*/false))
    8144     6087654 :             return 1;
    8145             :         }
    8146     6657981 :     }
    8147             : 
    8148             :   /* Determine whether we have a parameter pack at the end of the
    8149             :      template template parameter's template parameter list.  */
    8150      622414 :   if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
    8151             :     {
    8152      622414 :       parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
    8153             : 
    8154      622414 :       if (error_operand_p (parm))
    8155             :         return 0;
    8156             : 
    8157      622407 :       switch (TREE_CODE (parm))
    8158             :         {
    8159      620466 :         case TEMPLATE_DECL:
    8160      620466 :         case TYPE_DECL:
    8161      620466 :           if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
    8162      590529 :             variadic_p = 1;
    8163             :           break;
    8164             : 
    8165        1941 :         case PARM_DECL:
    8166        1941 :           if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
    8167      590529 :             variadic_p = 1;
    8168             :           break;
    8169             : 
    8170           0 :         default:
    8171           0 :           gcc_unreachable ();
    8172             :         }
    8173             :     }
    8174             : 
    8175      622407 :   if (nargs != nparms
    8176       96725 :       && !(variadic_p && nargs >= nparms - 1))
    8177             :     return 0;
    8178             : 
    8179             :   /* Check all of the template parameters except the parameter pack at
    8180             :      the end (if any).  */
    8181      684699 :   for (i = 0; i < nparms - variadic_p; ++i)
    8182             :     {
    8183       63313 :       if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
    8184       63313 :           || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
    8185           0 :         continue;
    8186             : 
    8187       63313 :       parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
    8188       63313 :       arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
    8189             : 
    8190       63313 :       if (!coerce_template_template_parm (parm, arg, complain, in_decl,
    8191             :                                           outer_args))
    8192             :         return 0;
    8193             : 
    8194             :     }
    8195             : 
    8196      621386 :   if (variadic_p)
    8197             :     {
    8198             :       /* Check each of the template parameters in the template
    8199             :          argument against the template parameter pack at the end of
    8200             :          the template template parameter.  */
    8201      590525 :       if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
    8202             :         return 0;
    8203             : 
    8204      590525 :       parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
    8205             : 
    8206     1275378 :       for (; i < nargs; ++i)
    8207             :         {
    8208      684869 :           if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
    8209           0 :             continue;
    8210             : 
    8211      684869 :           arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
    8212             : 
    8213      684869 :           if (!coerce_template_template_parm (parm, arg, complain, in_decl,
    8214             :                                               outer_args))
    8215             :             return 0;
    8216             :         }
    8217             :     }
    8218             : 
    8219             :   return 1;
    8220             : }
    8221             : 
    8222             : /* Verifies that the deduced template arguments (in TARGS) for the
    8223             :    template template parameters (in TPARMS) represent valid bindings,
    8224             :    by comparing the template parameter list of each template argument
    8225             :    to the template parameter list of its corresponding template
    8226             :    template parameter, in accordance with DR150. This
    8227             :    routine can only be called after all template arguments have been
    8228             :    deduced. It will return TRUE if all of the template template
    8229             :    parameter bindings are okay, FALSE otherwise.  */
    8230             : bool
    8231    29537988 : template_template_parm_bindings_ok_p (tree tparms, tree targs)
    8232             : {
    8233    29537988 :   int i, ntparms = TREE_VEC_LENGTH (tparms);
    8234    29537988 :   bool ret = true;
    8235             : 
    8236             :   /* We're dealing with template parms in this process.  */
    8237    29537988 :   ++processing_template_decl;
    8238             : 
    8239    29537988 :   targs = INNERMOST_TEMPLATE_ARGS (targs);
    8240             : 
    8241    77337101 :   for (i = 0; i < ntparms; ++i)
    8242             :     {
    8243    47799158 :       tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
    8244    47799158 :       tree targ = TREE_VEC_ELT (targs, i);
    8245             : 
    8246    47799158 :       if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
    8247             :         {
    8248      260959 :           tree packed_args = NULL_TREE;
    8249      260959 :           int idx, len = 1;
    8250             : 
    8251      260959 :           if (ARGUMENT_PACK_P (targ))
    8252             :             {
    8253             :               /* Look inside the argument pack.  */
    8254         109 :               packed_args = ARGUMENT_PACK_ARGS (targ);
    8255         109 :               len = TREE_VEC_LENGTH (packed_args);
    8256             :             }
    8257             : 
    8258      521886 :           for (idx = 0; idx < len; ++idx)
    8259             :             {
    8260      260972 :               if (packed_args)
    8261             :                 /* Extract the next argument from the argument
    8262             :                    pack.  */
    8263         122 :                 targ = TREE_VEC_ELT (packed_args, idx);
    8264             : 
    8265      260972 :               if (PACK_EXPANSION_P (targ))
    8266             :                 /* Look at the pattern of the pack expansion.  */
    8267          28 :                 targ = PACK_EXPANSION_PATTERN (targ);
    8268             : 
    8269             :               /* Extract the template parameters from the template
    8270             :                  argument.  */
    8271      260972 :               if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
    8272       48890 :                 targ = TYPE_NAME (targ);
    8273             : 
    8274             :               /* Verify that we can coerce the template template
    8275             :                  parameters from the template argument to the template
    8276             :                  parameter.  This requires an exact match.  */
    8277      260972 :               if (TREE_CODE (targ) == TEMPLATE_DECL
    8278      521932 :                   && !coerce_template_template_parms
    8279      260960 :                        (tparm,
    8280             :                         targ,
    8281             :                         tf_none,
    8282             :                         tparm,
    8283             :                         targs))
    8284             :                 {
    8285          45 :                   ret = false;
    8286          45 :                   goto out;
    8287             :                 }
    8288             :             }
    8289             :         }
    8290             :     }
    8291             : 
    8292    29537943 :  out:
    8293             : 
    8294    29537988 :   --processing_template_decl;
    8295    29537988 :   return ret;
    8296             : }
    8297             : 
    8298             : /* Since type attributes aren't mangled, we need to strip them from
    8299             :    template type arguments.  */
    8300             : 
    8301             : tree
    8302   713971751 : canonicalize_type_argument (tree arg, tsubst_flags_t complain)
    8303             : {
    8304   713971751 :   if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
    8305             :     return arg;
    8306   294883146 :   bool removed_attributes = false;
    8307   294883146 :   tree canon = strip_typedefs (arg, &removed_attributes);
    8308   294883146 :   if (removed_attributes
    8309         455 :       && (complain & tf_warning))
    8310          20 :     warning (OPT_Wignored_attributes,
    8311             :              "ignoring attributes on template argument %qT", arg);
    8312             :   return canon;
    8313             : }
    8314             : 
    8315             : /* And from inside dependent non-type arguments like sizeof(Type).  */
    8316             : 
    8317             : static tree
    8318    42432577 : canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
    8319             : {
    8320    42432577 :   if (!arg || arg == error_mark_node)
    8321             :     return arg;
    8322    42432577 :   bool removed_attributes = false;
    8323    42432577 :   tree canon = strip_typedefs_expr (arg, &removed_attributes);
    8324    42432577 :   if (removed_attributes
    8325           0 :       && (complain & tf_warning))
    8326           0 :     warning (OPT_Wignored_attributes,
    8327             :              "ignoring attributes in template argument %qE", arg);
    8328             :   return canon;
    8329             : }
    8330             : 
    8331             : /* A template declaration can be substituted for a constrained
    8332             :    template template parameter only when the argument is no more
    8333             :    constrained than the parameter.  */
    8334             : 
    8335             : static bool
    8336     6448083 : is_compatible_template_arg (tree parm, tree arg)
    8337             : {
    8338     6448083 :   tree parm_cons = get_constraints (parm);
    8339             : 
    8340             :   /* For now, allow constrained template template arguments
    8341             :      and unconstrained template template parameters.  */
    8342     6448083 :   if (parm_cons == NULL_TREE)
    8343             :     return true;
    8344             : 
    8345             :   /* If the template parameter is constrained, we need to rewrite its
    8346             :      constraints in terms of the ARG's template parameters. This ensures
    8347             :      that all of the template parameter types will have the same depth.
    8348             : 
    8349             :      Note that this is only valid when coerce_template_template_parm is
    8350             :      true for the innermost template parameters of PARM and ARG. In other
    8351             :      words, because coercion is successful, this conversion will be valid.  */
    8352          16 :   tree new_args = NULL_TREE;
    8353          16 :   if (parm_cons)
    8354             :     {
    8355          16 :       tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
    8356          16 :       new_args = template_parms_level_to_args (aparms);
    8357          16 :       ++processing_template_decl;
    8358          16 :       parm_cons = tsubst_constraint_info (parm_cons, new_args,
    8359             :                                           tf_none, NULL_TREE);
    8360          16 :       --processing_template_decl;
    8361          16 :       if (parm_cons == error_mark_node)
    8362             :         return false;
    8363             :     }
    8364             : 
    8365          16 :   return weakly_subsumes (parm_cons, arg);
    8366             : }
    8367             : 
    8368             : // Convert a placeholder argument into a binding to the original
    8369             : // parameter. The original parameter is saved as the TREE_TYPE of
    8370             : // ARG.
    8371             : static inline tree
    8372      849647 : convert_wildcard_argument (tree parm, tree arg)
    8373             : {
    8374      849647 :   TREE_TYPE (arg) = parm;
    8375      849647 :   return arg;
    8376             : }
    8377             : 
    8378             : /* We can't fully resolve ARG given as a non-type template argument to TYPE,
    8379             :    because one of them is dependent.  But we need to represent the
    8380             :    conversion for the benefit of cp_tree_equal.  */
    8381             : 
    8382             : static tree
    8383    27687292 : maybe_convert_nontype_argument (tree type, tree arg)
    8384             : {
    8385             :   /* Auto parms get no conversion.  */
    8386    27687292 :   if (type_uses_auto (type))
    8387             :     return arg;
    8388             :   /* We don't need or want to add this conversion now if we're going to use the
    8389             :      argument for deduction.  */
    8390    27687016 :   if (value_dependent_expression_p (arg))
    8391             :     return arg;
    8392             : 
    8393         132 :   type = cv_unqualified (type);
    8394         132 :   tree argtype = TREE_TYPE (arg);
    8395         132 :   if (same_type_p (type, argtype))
    8396             :     return arg;
    8397             : 
    8398         132 :   arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
    8399         132 :   IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
    8400         132 :   return arg;
    8401             : }
    8402             : 
    8403             : /* Convert the indicated template ARG as necessary to match the
    8404             :    indicated template PARM.  Returns the converted ARG, or
    8405             :    error_mark_node if the conversion was unsuccessful.  Error and
    8406             :    warning messages are issued under control of COMPLAIN.  This
    8407             :    conversion is for the Ith parameter in the parameter list.  ARGS is
    8408             :    the full set of template arguments deduced so far.  */
    8409             : 
    8410             : static tree
    8411   726299327 : convert_template_argument (tree parm,
    8412             :                            tree arg,
    8413             :                            tree args,
    8414             :                            tsubst_flags_t complain,
    8415             :                            int i,
    8416             :                            tree in_decl)
    8417             : {
    8418   726299327 :   tree orig_arg;
    8419   726299327 :   tree val;
    8420   726299327 :   int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
    8421             : 
    8422   726299327 :   if (parm == error_mark_node || error_operand_p (arg))
    8423             :     return error_mark_node;
    8424             : 
    8425             :   /* Trivially convert placeholders. */
    8426   718477967 :   if (TREE_CODE (arg) == WILDCARD_DECL)
    8427      849647 :     return convert_wildcard_argument (parm, arg);
    8428             : 
    8429   717628320 :   if (arg == any_targ_node)
    8430             :     return arg;
    8431             : 
    8432   717628308 :   if (TREE_CODE (arg) == TREE_LIST
    8433   717628308 :       && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
    8434             :     {
    8435             :       /* The template argument was the name of some
    8436             :          member function.  That's usually
    8437             :          invalid, but static members are OK.  In any
    8438             :          case, grab the underlying fields/functions
    8439             :          and issue an error later if required.  */
    8440           0 :       TREE_TYPE (arg) = unknown_type_node;
    8441             :     }
    8442             : 
    8443   717628308 :   orig_arg = arg;
    8444             : 
    8445   717628308 :   requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
    8446  1435256616 :   requires_type = (TREE_CODE (parm) == TYPE_DECL
    8447   717628308 :                    || requires_tmpl_type);
    8448             : 
    8449             :   /* When determining whether an argument pack expansion is a template,
    8450             :      look at the pattern.  */
    8451   717628308 :   if (PACK_EXPANSION_P (arg))
    8452    12577040 :     arg = PACK_EXPANSION_PATTERN (arg);
    8453             : 
    8454             :   /* Deal with an injected-class-name used as a template template arg.  */
    8455   717628308 :   if (requires_tmpl_type && CLASS_TYPE_P (arg))
    8456             :     {
    8457          46 :       tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
    8458          46 :       if (TREE_CODE (t) == TEMPLATE_DECL)
    8459             :         {
    8460          38 :           if (cxx_dialect >= cxx11)
    8461             :             /* OK under DR 1004.  */;
    8462           2 :           else if (complain & tf_warning_or_error)
    8463           2 :             pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
    8464           2 :                      " used as template template argument", TYPE_NAME (arg));
    8465           0 :           else if (flag_pedantic_errors)
    8466   717628308 :             t = arg;
    8467             : 
    8468             :           arg = t;
    8469             :         }
    8470             :     }
    8471             : 
    8472  1441712734 :   is_tmpl_type =
    8473   717628308 :     ((TREE_CODE (arg) == TEMPLATE_DECL
    8474     6330901 :       && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
    8475   711297407 :      || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
    8476   711297401 :      || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
    8477  1428807517 :      || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
    8478             : 
    8479     6456118 :   if (is_tmpl_type
    8480     6456118 :       && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
    8481     6337926 :           || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
    8482      125211 :     arg = TYPE_STUB_DECL (arg);
    8483             : 
    8484   717628308 :   is_type = TYPE_P (arg) || is_tmpl_type;
    8485             : 
    8486     1074371 :   if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
    8487   717628331 :       && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
    8488             :     {
    8489          15 :       if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
    8490             :         {
    8491           4 :           if (complain & tf_error)
    8492           4 :             error ("invalid use of destructor %qE as a type", orig_arg);
    8493           4 :           return error_mark_node;
    8494             :         }
    8495             : 
    8496          11 :       permerror (input_location,
    8497             :                  "to refer to a type member of a template parameter, "
    8498             :                  "use %<typename %E%>", orig_arg);
    8499             : 
    8500          11 :       orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
    8501          11 :                                      TREE_OPERAND (arg, 1),
    8502             :                                      typename_type,
    8503             :                                      complain);
    8504          11 :       arg = orig_arg;
    8505          11 :       is_type = 1;
    8506             :     }
    8507   717628304 :   if (is_type != requires_type)
    8508             :     {
    8509     1076164 :       if (in_decl)
    8510             :         {
    8511     1075815 :           if (complain & tf_error)
    8512             :             {
    8513         143 :               error ("type/value mismatch at argument %d in template "
    8514             :                      "parameter list for %qD",
    8515             :                      i + 1, in_decl);
    8516         143 :               if (is_type)
    8517             :                 {
    8518             :                   /* The template argument is a type, but we're expecting
    8519             :                      an expression.  */
    8520          45 :                   inform (input_location,
    8521             :                           "  expected a constant of type %qT, got %qT",
    8522          34 :                           TREE_TYPE (parm),
    8523          34 :                           (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
    8524             :                   /* [temp.arg]/2: "In a template-argument, an ambiguity
    8525             :                      between a type-id and an expression is resolved to a
    8526             :                      type-id, regardless of the form of the corresponding
    8527             :                      template-parameter."  So give the user a clue.  */
    8528          34 :                   if (TREE_CODE (arg) == FUNCTION_TYPE)
    8529           2 :                     inform (input_location, "  ambiguous template argument "
    8530             :                             "for non-type template parameter is treated as "
    8531             :                             "function type");
    8532             :                 }
    8533         109 :               else if (requires_tmpl_type)
    8534          12 :                 inform (input_location,
    8535             :                         "  expected a class template, got %qE", orig_arg);
    8536             :               else
    8537          97 :                 inform (input_location,
    8538             :                         "  expected a type, got %qE", orig_arg);
    8539             :             }
    8540             :         }
    8541     1076164 :       return error_mark_node;
    8542             :     }
    8543   716552140 :   if (is_tmpl_type ^ requires_tmpl_type)
    8544             :     {
    8545          22 :       if (in_decl && (complain & tf_error))
    8546             :         {
    8547          21 :           error ("type/value mismatch at argument %d in template "
    8548             :                  "parameter list for %qD",
    8549             :                  i + 1, in_decl);
    8550          21 :           if (is_tmpl_type)
    8551           8 :             inform (input_location,
    8552           8 :                     "  expected a type, got %qT", DECL_NAME (arg));
    8553             :           else
    8554          13 :             inform (input_location,
    8555             :                     "  expected a class template, got %qT", orig_arg);
    8556             :         }
    8557          22 :       return error_mark_node;
    8558             :     }
    8559             : 
    8560   716552118 :   if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
    8561             :     /* We already did the appropriate conversion when packing args.  */
    8562             :     val = orig_arg;
    8563   716552097 :   else if (is_type)
    8564             :     {
    8565   604790659 :       if (requires_tmpl_type)
    8566             :         {
    8567     6456093 :           if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
    8568             :             /* The number of argument required is not known yet.
    8569             :                Just accept it for now.  */
    8570             :             val = orig_arg;
    8571             :           else
    8572             :             {
    8573             :               /* Strip alias templates that are equivalent to another
    8574             :                  template.  */
    8575     6449082 :               arg = get_underlying_template (arg);
    8576             : 
    8577     6449082 :               if (coerce_template_template_parms (parm, arg,
    8578             :                                                   complain, in_decl,
    8579             :                                                   args))
    8580             :                 {
    8581     6448083 :                   val = arg;
    8582             : 
    8583             :                   /* TEMPLATE_TEMPLATE_PARM node is preferred over
    8584             :                      TEMPLATE_DECL.  */
    8585     6448083 :                   if (val != error_mark_node)
    8586             :                     {
    8587     6448083 :                       if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
    8588      222929 :                         val = TREE_TYPE (val);
    8589     6448083 :                       if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
    8590          68 :                         val = make_pack_expansion (val, complain);
    8591             :                     }
    8592             :                 }
    8593             :               else
    8594             :                 {
    8595         999 :                   if (in_decl && (complain & tf_error))
    8596             :                     {
    8597          48 :                       error ("type/value mismatch at argument %d in "
    8598             :                              "template parameter list for %qD",
    8599             :                              i + 1, in_decl);
    8600          48 :                       inform (input_location,
    8601             :                               "  expected a template of type %qD, got %qT",
    8602             :                               parm, orig_arg);
    8603             :                     }
    8604             : 
    8605         999 :                   val = error_mark_node;
    8606             :                 }
    8607             : 
    8608             :               // Check that the constraints are compatible before allowing the
    8609             :               // substitution.
    8610     6449082 :               if (val != error_mark_node)
    8611     6448083 :                 if (!is_compatible_template_arg (parm, arg))
    8612             :                   {
    8613           4 :                     if (in_decl && (complain & tf_error))
    8614             :                       {
    8615           4 :                         error ("constraint mismatch at argument %d in "
    8616             :                                "template parameter list for %qD",
    8617             :                                i + 1, in_decl);
    8618           4 :                         inform (input_location, "  expected %qD but got %qD",
    8619             :                                 parm, arg);
    8620             :                       }
    8621           4 :                     val = error_mark_node;
    8622             :                   }
    8623             :             }
    8624             :         }
    8625             :       else
    8626             :         val = orig_arg;
    8627             :       /* We only form one instance of each template specialization.
    8628             :          Therefore, if we use a non-canonical variant (i.e., a
    8629             :          typedef), any future messages referring to the type will use
    8630             :          the typedef, which is confusing if those future uses do not
    8631             :          themselves also use the typedef.  */
    8632   604790659 :       if (TYPE_P (val))
    8633   598564506 :         val = canonicalize_type_argument (val, complain);
    8634             :     }
    8635             :   else
    8636             :     {
    8637   111761438 :       tree t = TREE_TYPE (parm);
    8638             : 
    8639   223522876 :       if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
    8640   223522876 :           > TMPL_ARGS_DEPTH (args))
    8641             :         /* We don't have enough levels of args to do any substitution.  This
    8642             :            can happen in the context of -fnew-ttp-matching.  */;
    8643   111757263 :       else if (tree a = type_uses_auto (t))
    8644             :         {
    8645        1540 :           t = do_auto_deduction (t, arg, a, complain, adc_unify, args,
    8646             :                                  LOOKUP_IMPLICIT, /*tmpl=*/in_decl);
    8647        1540 :           if (t == error_mark_node)
    8648             :             return error_mark_node;
    8649             :         }
    8650             :       else
    8651   111755723 :         t = tsubst (t, args, complain, in_decl);
    8652             : 
    8653             :       /* Perform array-to-pointer and function-to-pointer conversion
    8654             :          as per [temp.param]/10.  */
    8655   111761421 :       t = type_decays_to (t);
    8656             : 
    8657   111761421 :       if (invalid_nontype_parm_type_p (t, complain))
    8658          58 :         return error_mark_node;
    8659             : 
    8660             :       /* Drop top-level cv-qualifiers on the substituted/deduced type of
    8661             :          this non-type template parameter, as per [temp.param]/6.  */
    8662   111761363 :       t = cv_unqualified (t);
    8663             : 
    8664   111761363 :       if (t != TREE_TYPE (parm))
    8665     3676833 :         t = canonicalize_type_argument (t, complain);
    8666             : 
    8667   111761363 :       if (!type_dependent_expression_p (orig_arg)
    8668   111761363 :           && !uses_template_parms (t))
    8669             :         /* We used to call digest_init here.  However, digest_init
    8670             :            will report errors, which we don't want when complain
    8671             :            is zero.  More importantly, digest_init will try too
    8672             :            hard to convert things: for example, `0' should not be
    8673             :            converted to pointer type at this point according to
    8674             :            the standard.  Accepting this is not merely an
    8675             :            extension, since deciding whether or not these
    8676             :            conversions can occur is part of determining which
    8677             :            function template to call, or whether a given explicit
    8678             :            argument specification is valid.  */
    8679    84074071 :         val = convert_nontype_argument (t, orig_arg, complain);
    8680             :       else
    8681             :         {
    8682    27687292 :           val = canonicalize_expr_argument (orig_arg, complain);
    8683    27687292 :           val = maybe_convert_nontype_argument (t, val);
    8684             :         }
    8685             : 
    8686             : 
    8687   111761363 :       if (val == NULL_TREE)
    8688         403 :         val = error_mark_node;
    8689   111760960 :       else if (val == error_mark_node && (complain & tf_error))
    8690          98 :         error_at (cp_expr_loc_or_input_loc (orig_arg),
    8691             :                   "could not convert template argument %qE from %qT to %qT",
    8692          71 :                   orig_arg, TREE_TYPE (orig_arg), t);
    8693             : 
    8694   111761363 :       if (INDIRECT_REF_P (val))
    8695             :         {
    8696             :           /* Reject template arguments that are references to built-in
    8697             :              functions with no library fallbacks.  */
    8698         440 :           const_tree inner = TREE_OPERAND (val, 0);
    8699         440 :           const_tree innertype = TREE_TYPE (inner);
    8700         440 :           if (innertype
    8701         437 :               && TYPE_REF_P (innertype)
    8702         431 :               && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
    8703         113 :               && TREE_OPERAND_LENGTH (inner) > 0
    8704         517 :               && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
    8705           8 :               return error_mark_node;
    8706             :         }
    8707             : 
    8708   111761355 :       if (TREE_CODE (val) == SCOPE_REF)
    8709             :         {
    8710             :           /* Strip typedefs from the SCOPE_REF.  */
    8711    11891104 :           tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
    8712    11891104 :           tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
    8713             :                                                    complain);
    8714    23782208 :           val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
    8715    11891104 :                                       QUALIFIED_NAME_IS_TEMPLATE (val));
    8716             :         }
    8717             :     }
    8718             : 
    8719             :   return val;
    8720             : }
    8721             : 
    8722             : /* Coerces the remaining template arguments in INNER_ARGS (from
    8723             :    ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
    8724             :    Returns the coerced argument pack. PARM_IDX is the position of this
    8725             :    parameter in the template parameter list. ARGS is the original
    8726             :    template argument list.  */
    8727             : static tree
    8728    54694371 : coerce_template_parameter_pack (tree parms,
    8729             :                                 int parm_idx,
    8730             :                                 tree args,
    8731             :                                 tree inner_args,
    8732             :                                 int arg_idx,
    8733             :                                 tree new_args,
    8734             :                                 int* lost,
    8735             :                                 tree in_decl,
    8736             :                                 tsubst_flags_t complain)
    8737             : {
    8738    54694371 :   tree parm = TREE_VEC_ELT (parms, parm_idx);
    8739   109388742 :   int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
    8740    54694371 :   tree packed_args;
    8741    54694371 :   tree argument_pack;
    8742    54694371 :   tree packed_parms = NULL_TREE;
    8743             : 
    8744    54694371 :   if (arg_idx > nargs)
    8745             :     arg_idx = nargs;
    8746             : 
    8747    54694371 :   if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
    8748             :     {
    8749             :       /* When the template parameter is a non-type template parameter pack
    8750             :          or template template parameter pack whose type or template
    8751             :          parameters use parameter packs, we know exactly how many arguments
    8752             :          we are looking for.  Build a vector of the instantiated decls for
    8753             :          these template parameters in PACKED_PARMS.  */
    8754             :       /* We can't use make_pack_expansion here because it would interpret a
    8755             :          _DECL as a use rather than a declaration.  */
    8756          65 :       tree decl = TREE_VALUE (parm);
    8757          65 :       tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
    8758          65 :       PACK_EXPANSION_PATTERN (exp) = decl;
    8759         130 :       PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
    8760          65 :       SET_TYPE_STRUCTURAL_EQUALITY (exp);
    8761             : 
    8762          65 :       TREE_VEC_LENGTH (args)--;
    8763          65 :       packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
    8764          62 :       TREE_VEC_LENGTH (args)++;
    8765             : 
    8766          62 :       if (packed_parms == error_mark_node)
    8767             :         return error_mark_node;
    8768             : 
    8769             :       /* If we're doing a partial instantiation of a member template,
    8770             :          verify that all of the types used for the non-type
    8771             :          template parameter pack are, in fact, valid for non-type
    8772             :          template parameters.  */
    8773          62 :       if (arg_idx < nargs
    8774          62 :           && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
    8775             :         {
    8776          17 :           int j, len = TREE_VEC_LENGTH (packed_parms);
    8777          38 :           for (j = 0; j < len; ++j)
    8778             :             {
    8779          23 :               tree t = TREE_VEC_ELT (packed_parms, j);
    8780          23 :               if (TREE_CODE (t) == PARM_DECL
    8781          23 :                   && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
    8782           2 :                 return error_mark_node;
    8783             :             }
    8784             :           /* We don't know how many args we have yet, just
    8785             :              use the unconverted ones for now.  */
    8786             :           return NULL_TREE;
    8787             :         }
    8788             : 
    8789          45 :       packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
    8790             :     }
    8791             :   /* Check if we have a placeholder pack, which indicates we're
    8792             :      in the context of a introduction list.  In that case we want
    8793             :      to match this pack to the single placeholder.  */
    8794    54694306 :   else if (arg_idx < nargs
    8795    53402374 :            && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
    8796    54694591 :            && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
    8797             :     {
    8798          16 :       nargs = arg_idx + 1;
    8799          16 :       packed_args = make_tree_vec (1);
    8800             :     }
    8801             :   else
    8802    54694290 :     packed_args = make_tree_vec (nargs - arg_idx);
    8803             : 
    8804             :   /* Convert the remaining arguments, which will be a part of the
    8805             :      parameter pack "parm".  */
    8806    54694351 :   int first_pack_arg = arg_idx;
    8807   156937764 :   for (; arg_idx < nargs; ++arg_idx)
    8808             :     {
    8809   102243425 :       tree arg = TREE_VEC_ELT (inner_args, arg_idx);
    8810   102243425 :       tree actual_parm = TREE_VALUE (parm);
    8811   102243425 :       int pack_idx = arg_idx - first_pack_arg;
    8812             : 
    8813   102243425 :       if (packed_parms)
    8814             :         {
    8815             :           /* Once we've packed as many args as we have types, stop.  */
    8816          81 :           if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
    8817             :             break;
    8818          69 :           else if (PACK_EXPANSION_P (arg))
    8819             :             /* We don't know how many args we have yet, just
    8820             :                use the unconverted ones for now.  */
    8821             :             return NULL_TREE;
    8822             :           else
    8823          69 :             actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
    8824             :         }
    8825             : 
    8826   102243413 :       if (arg == error_mark_node)
    8827             :         {
    8828          17 :           if (complain & tf_error)
    8829          17 :             error ("template argument %d is invalid", arg_idx + 1);
    8830             :         }
    8831             :       else
    8832   102243396 :         arg = convert_template_argument (actual_parm,
    8833             :                                          arg, new_args, complain, parm_idx,
    8834             :                                          in_decl);
    8835   102243413 :       if (arg == error_mark_node)
    8836         252 :         (*lost)++;
    8837   102243413 :       TREE_VEC_ELT (packed_args, pack_idx) = arg;
    8838             :     }
    8839             : 
    8840    54694351 :   if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
    8841    54694351 :       && TREE_VEC_LENGTH (packed_args) > 0)
    8842             :     {
    8843           3 :       if (complain & tf_error)
    8844           3 :         error ("wrong number of template arguments (%d, should be %d)",
    8845           3 :                arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
    8846           3 :       return error_mark_node;
    8847             :     }
    8848             : 
    8849    54694348 :   if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
    8850    54694348 :       || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
    8851    53250467 :     argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
    8852             :   else
    8853             :     {
    8854     1443881 :       argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
    8855     1443881 :       TREE_CONSTANT (argument_pack) = 1;
    8856             :     }
    8857             : 
    8858    54694348 :   ARGUMENT_PACK_ARGS (argument_pack) = packed_args;
    8859    54694348 :   if (CHECKING_P)
    8860    54694348 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
    8861             :                                          TREE_VEC_LENGTH (packed_args));
    8862    54694348 :   return argument_pack;
    8863             : }
    8864             : 
    8865             : /* Returns the number of pack expansions in the template argument vector
    8866             :    ARGS.  */
    8867             : 
    8868             : static int
    8869   975362567 : pack_expansion_args_count (tree args)
    8870             : {
    8871   975362567 :   int i;
    8872   975362567 :   int count = 0;
    8873   975362567 :   if (args)
    8874  2869606929 :     for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
    8875             :       {
    8876  1894244362 :         tree elt = TREE_VEC_ELT (args, i);
    8877  1894244362 :         if (elt && PACK_EXPANSION_P (elt))
    8878    22690062 :           ++count;
    8879             :       }
    8880   975362567 :   return count;
    8881             : }
    8882             : 
    8883             : /* Convert all template arguments to their appropriate types, and
    8884             :    return a vector containing the innermost resulting template
    8885             :    arguments.  If any error occurs, return error_mark_node. Error and
    8886             :    warning messages are issued under control of COMPLAIN.
    8887             : 
    8888             :    If PARMS represents all template parameters levels, this function
    8889             :    returns a vector of vectors representing all the resulting argument
    8890             :    levels.  Note that in this case, only the innermost arguments are
    8891             :    coerced because the outermost ones are supposed to have been coerced
    8892             :    already.  Otherwise, if PARMS represents only (the innermost) vector
    8893             :    of parameters, this function returns a vector containing just the
    8894             :    innermost resulting arguments.
    8895             : 
    8896             :    If REQUIRE_ALL_ARGS is false, argument deduction will be performed
    8897             :    for arguments not specified in ARGS.  If REQUIRE_ALL_ARGS is true,
    8898             :    arguments not specified in ARGS must have default arguments which
    8899             :    we'll use to fill in ARGS.  */
    8900             : 
    8901             : tree
    8902   397510830 : coerce_template_parms (tree parms,
    8903             :                        tree args,
    8904             :                        tree in_decl,
    8905             :                        tsubst_flags_t complain,
    8906             :                        bool require_all_args /* = true */)
    8907             : {
    8908   397510830 :   int nparms, nargs, parm_idx, arg_idx, lost = 0;
    8909   397510830 :   tree orig_inner_args;
    8910   397510830 :   tree inner_args;
    8911             : 
    8912             :   /* When used as a boolean value, indicates whether this is a
    8913             :      variadic template parameter list. Since it's an int, we can also
    8914             :      subtract it from nparms to get the number of non-variadic
    8915             :      parameters.  */
    8916   397510830 :   int variadic_p = 0;
    8917   397510830 :   int variadic_args_p = 0;
    8918   397510830 :   int post_variadic_parms = 0;
    8919             : 
    8920             :   /* Adjustment to nparms for fixed parameter packs.  */
    8921   397510830 :   int fixed_pack_adjust = 0;
    8922   397510830 :   int fixed_packs = 0;
    8923   397510830 :   int missing = 0;
    8924             : 
    8925             :   /* Likewise for parameters with default arguments.  */
    8926   397510830 :   int default_p = 0;
    8927             : 
    8928   397510830 :   if (args == error_mark_node)
    8929             :     return error_mark_node;
    8930             : 
    8931   397510830 :   bool return_full_args = false;
    8932   397510830 :   if (TREE_CODE (parms) == TREE_LIST)
    8933             :     {
    8934   354871475 :       if (TMPL_PARMS_DEPTH (parms) > 1)
    8935             :         {
    8936    25713748 :           gcc_assert (TMPL_PARMS_DEPTH (parms) == TMPL_ARGS_DEPTH (args));
    8937             :           return_full_args = true;
    8938             :         }
    8939   354871475 :       parms = INNERMOST_TEMPLATE_PARMS (parms);
    8940             :     }
    8941             : 
    8942   397510830 :   nparms = TREE_VEC_LENGTH (parms);
    8943             : 
    8944             :   /* Determine if there are any parameter packs or default arguments.  */
    8945  1067092358 :   for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
    8946             :     {
    8947   669581528 :       tree parm = TREE_VEC_ELT (parms, parm_idx);
    8948   669581528 :       if (variadic_p)
    8949        3872 :         ++post_variadic_parms;
    8950   669581528 :       if (template_parameter_pack_p (TREE_VALUE (parm)))
    8951    56123375 :         ++variadic_p;
    8952   669581528 :       if (TREE_PURPOSE (parm))
    8953   115514121 :         ++default_p;
    8954             :     }
    8955             : 
    8956   397510830 :   inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
    8957             :   /* If there are no parameters that follow a parameter pack, we need to
    8958             :      expand any argument packs so that we can deduce a parameter pack from
    8959             :      some non-packed args followed by an argument pack, as in variadic85.C.
    8960             :      If there are such parameters, we need to leave argument packs intact
    8961             :      so the arguments are assigned properly.  This can happen when dealing
    8962             :      with a nested class inside a partial specialization of a class
    8963             :      template, as in variadic92.C, or when deducing a template parameter pack
    8964             :      from a sub-declarator, as in variadic114.C.  */
    8965   397510830 :   if (!post_variadic_parms)
    8966   397507471 :     inner_args = expand_template_argument_pack (inner_args);
    8967             : 
    8968             :   /* Count any pack expansion args.  */
    8969   397510830 :   variadic_args_p = pack_expansion_args_count (inner_args);
    8970             : 
    8971   795021660 :   nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
    8972    26851034 :   if ((nargs - variadic_args_p > nparms && !variadic_p)
    8973   424217523 :       || (nargs < nparms - variadic_p
    8974             :           && require_all_args
    8975    10088528 :           && !variadic_args_p
    8976     7384329 :           && (TREE_VEC_ELT (parms, nargs) != error_mark_node
    8977     7384329 :               && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))
    8978             :     {
    8979      144451 :     bad_nargs:
    8980      144451 :       if (complain & tf_error)
    8981             :         {
    8982          77 :           if (variadic_p || default_p)
    8983             :             {
    8984          10 :               nparms -= variadic_p + default_p;
    8985          10 :               error ("wrong number of template arguments "
    8986             :                      "(%d, should be at least %d)", nargs, nparms);
    8987             :             }
    8988             :           else
    8989          67 :              error ("wrong number of template arguments "
    8990             :                     "(%d, should be %d)", nargs, nparms);
    8991             : 
    8992          77 :           if (in_decl)
    8993          77 :             inform (DECL_SOURCE_LOCATION (in_decl),
    8994             :                     "provided for %qD", in_decl);
    8995             :         }
    8996             : 
    8997      144451 :       return error_mark_node;
    8998             :     }
    8999             :   /* We can't pass a pack expansion to a non-pack parameter of an alias
    9000             :      template (DR 1430).  */
    9001   397366390 :   else if (in_decl
    9002   390707637 :            && (DECL_ALIAS_TEMPLATE_P (in_decl)
    9003   298980531 :                || concept_definition_p (in_decl))
    9004    94621711 :            && variadic_args_p
    9005   401497083 :            && nargs - variadic_args_p < nparms - variadic_p)
    9006             :     {
    9007          16 :       if (complain & tf_error)
    9008             :         {
    9009          18 :           for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
    9010             :             {
    9011          18 :               tree arg = TREE_VEC_ELT (inner_args, i);
    9012          18 :               tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
    9013             : 
    9014          18 :               if (PACK_EXPANSION_P (arg)
    9015          18 :                   && !template_parameter_pack_p (parm))
    9016             :                 {
    9017          15 :                   if (DECL_ALIAS_TEMPLATE_P (in_decl))
    9018          12 :                     error_at (location_of (arg),
    9019             :                               "pack expansion argument for non-pack parameter "
    9020             :                               "%qD of alias template %qD", parm, in_decl);
    9021             :                   else
    9022           3 :                     error_at (location_of (arg),
    9023             :                               "pack expansion argument for non-pack parameter "
    9024             :                               "%qD of concept %qD", parm, in_decl);
    9025          15 :                   inform (DECL_SOURCE_LOCATION (parm), "declared here");
    9026          15 :                   goto found;
    9027             :                 }
    9028             :             }
    9029           0 :           gcc_unreachable ();
    9030          15 :         found:;
    9031             :         }
    9032          16 :       return error_mark_node;
    9033             :     }
    9034             : 
    9035             :   /* We need to evaluate the template arguments, even though this
    9036             :      template-id may be nested within a "sizeof".  */
    9037   397366374 :   cp_evaluated ev;
    9038             : 
    9039   397366374 :   tree new_args = add_outermost_template_args (args, make_tree_vec (nparms));
    9040   397366374 :   tree& new_inner_args = TMPL_ARGS_LEVEL (new_args, TMPL_ARGS_DEPTH (new_args));
    9041   397366374 :   int pack_adjust = 0;
    9042  1060718939 :   for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
    9043             :     {
    9044   667635958 :       tree arg;
    9045   667635958 :       tree parm;
    9046             : 
    9047             :       /* Get the Ith template parameter.  */
    9048   667635958 :       parm = TREE_VEC_ELT (parms, parm_idx);
    9049             : 
    9050   667635958 :       if (parm == error_mark_node)
    9051             :         {
    9052           0 :           TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
    9053           0 :           continue;
    9054             :         }
    9055             : 
    9056             :       /* Calculate the next argument.  */
    9057   667635958 :       if (arg_idx < nargs)
    9058   653703093 :         arg = TREE_VEC_ELT (inner_args, arg_idx);
    9059             :       else
    9060             :         arg = NULL_TREE;
    9061             : 
    9062   667635958 :       if (template_parameter_pack_p (TREE_VALUE (parm))
    9063    55703859 :           && (arg || require_all_args || !(complain & tf_partial))
    9064   722330350 :           && !(arg && ARGUMENT_PACK_P (arg)))
    9065             :         {
    9066             :           /* Some arguments will be placed in the
    9067             :              template parameter pack PARM.  */
    9068    54694371 :           arg = coerce_template_parameter_pack (parms, parm_idx, args,
    9069             :                                                 inner_args, arg_idx,
    9070             :                                                 new_args, &lost,
    9071             :                                                 in_decl, complain);
    9072             : 
    9073    54694368 :           if (arg == NULL_TREE)
    9074             :             {
    9075             :               /* We don't know how many args we have yet, just use the
    9076             :                  unconverted (and still packed) ones for now.  */
    9077          15 :               new_inner_args = orig_inner_args;
    9078          15 :               arg_idx = nargs;
    9079          15 :               break;
    9080             :             }
    9081             : 
    9082    54694353 :           TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
    9083             : 
    9084             :           /* Store this argument.  */
    9085    54694353 :           if (arg == error_mark_node)
    9086             :             {
    9087           5 :               lost++;
    9088             :               /* We are done with all of the arguments.  */
    9089           5 :               arg_idx = nargs;
    9090           5 :               break;
    9091             :             }
    9092             :           else
    9093             :             {
    9094    54694348 :               pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
    9095    54694348 :               arg_idx += pack_adjust;
    9096    54694348 :               if (fixed_parameter_pack_p (TREE_VALUE (parm)))
    9097             :                 {
    9098          42 :                   ++fixed_packs;
    9099          42 :                   fixed_pack_adjust += pack_adjust;
    9100             :                 }
    9101             :             }
    9102             : 
    9103    54694348 :           continue;
    9104             :         }
    9105   612941587 :       else if (arg)
    9106             :         {
    9107   600299589 :           if (PACK_EXPANSION_P (arg))
    9108             :             {
    9109             :               /* "If every valid specialization of a variadic template
    9110             :                  requires an empty template parameter pack, the template is
    9111             :                  ill-formed, no diagnostic required."  So check that the
    9112             :                  pattern works with this parameter.  */
    9113      688202 :               tree pattern = PACK_EXPANSION_PATTERN (arg);
    9114      688202 :               tree conv = convert_template_argument (TREE_VALUE (parm),
    9115             :                                                      pattern, new_args,
    9116             :                                                      complain, parm_idx,
    9117             :                                                      in_decl);
    9118      688202 :               if (conv == error_mark_node)
    9119             :                 {
    9120           9 :                   if (complain & tf_error)
    9121           6 :                     inform (input_location, "so any instantiation with a "
    9122             :                             "non-empty parameter pack would be ill-formed");
    9123           9 :                   ++lost;
    9124             :                 }
    9125      688193 :               else if (TYPE_P (conv) && !TYPE_P (pattern))
    9126             :                 /* Recover from missing typename.  */
    9127           9 :                 TREE_VEC_ELT (inner_args, arg_idx)
    9128          18 :                   = make_pack_expansion (conv, complain);
    9129             : 
    9130             :               /* We don't know how many args we have yet, just
    9131             :                  use the unconverted ones for now.  */
    9132      688202 :               new_inner_args = inner_args;
    9133      688202 :               arg_idx = nargs;
    9134      688202 :               break;
    9135             :             }
    9136             :         }
    9137    12641998 :       else if (require_all_args)
    9138             :         {
    9139             :           /* There must be a default arg in this case.  */
    9140     9046830 :           arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
    9141             :                                      complain, in_decl);
    9142             :           /* The position of the first default template argument,
    9143             :              is also the number of non-defaulted arguments in NEW_INNER_ARGS.
    9144             :              Record that.  */
    9145     9046830 :           if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
    9146     7385307 :             SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
    9147             :                                                  arg_idx - pack_adjust);
    9148             :         }
    9149             :       else
    9150             :         break;
    9151             : 
    9152   608658217 :       if (arg == error_mark_node)
    9153             :         {
    9154         275 :           if (complain & tf_error)
    9155         246 :             error ("template argument %d is invalid", arg_idx + 1);
    9156             :         }
    9157   608657942 :       else if (!arg)
    9158             :         {
    9159             :           /* This can occur if there was an error in the template
    9160             :              parameter list itself (which we would already have
    9161             :              reported) that we are trying to recover from, e.g., a class
    9162             :              template with a parameter list such as
    9163             :              template<typename..., typename> (cpp0x/variadic150.C).  */
    9164          12 :           ++lost;
    9165             : 
    9166             :           /* This can also happen with a fixed parameter pack (71834).  */
    9167          12 :           if (arg_idx >= nargs)
    9168           6 :             ++missing;
    9169             :         }
    9170             :       else
    9171   608657930 :         arg = convert_template_argument (TREE_VALUE (parm),
    9172             :                                          arg, new_args, complain,
    9173             :                                          parm_idx, in_decl);
    9174             : 
    9175   608658217 :       if (arg == error_mark_node)
    9176     1077939 :         lost++;
    9177             : 
    9178   608658217 :       TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
    9179             :     }
    9180             : 
    9181   397366371 :   if (missing || arg_idx < nargs - variadic_args_p)
    9182             :     {
    9183             :       /* If we had fixed parameter packs, we didn't know how many arguments we
    9184             :          actually needed earlier; now we do.  */
    9185          11 :       nparms += fixed_pack_adjust;
    9186          11 :       variadic_p -= fixed_packs;
    9187      144451 :       goto bad_nargs;
    9188             :     }
    9189             : 
    9190   397366360 :   if (arg_idx < nargs)
    9191             :     {
    9192             :       /* We had some pack expansion arguments that will only work if the packs
    9193             :          are empty, but wait until instantiation time to complain.
    9194             :          See variadic-ttp3.C.  */
    9195             : 
    9196             :       /* Except that we can't provide empty packs to alias templates or
    9197             :          concepts when there are no corresponding parameters. Basically,
    9198             :          we can get here with this:
    9199             : 
    9200             :              template<typename T> concept C = true;
    9201             : 
    9202             :              template<typename... Args>
    9203             :                requires C<Args...>
    9204             :              void f();
    9205             : 
    9206             :          When parsing C<Args...>, we try to form a concept check of
    9207             :          C<?, Args...>. Without the extra check for substituting an empty
    9208             :          pack past the last parameter, we can accept the check as valid.
    9209             : 
    9210             :          FIXME: This may be valid for alias templates (but I doubt it).
    9211             : 
    9212             :          FIXME: The error could be better also.   */
    9213        3647 :       if (in_decl && concept_definition_p (in_decl))
    9214             :         {
    9215           0 :           if (complain & tf_error)
    9216           0 :             error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
    9217             :                       "too many arguments");
    9218           0 :           return error_mark_node;
    9219             :         }
    9220             : 
    9221        3647 :       int len = nparms + (nargs - arg_idx);
    9222        3647 :       tree args = make_tree_vec (len);
    9223        3647 :       int i = 0;
    9224       10944 :       for (; i < nparms; ++i)
    9225        3650 :         TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
    9226        7294 :       for (; i < len; ++i, ++arg_idx)
    9227        3647 :         TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
    9228             :                                                arg_idx - pack_adjust);
    9229        3647 :       new_inner_args = args;
    9230             :     }
    9231             : 
    9232   397366360 :   if (lost)
    9233             :     {
    9234     1077942 :       gcc_assert (!(complain & tf_error) || seen_error ());
    9235     1077942 :       return error_mark_node;
    9236             :     }
    9237             : 
    9238   396288418 :   if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
    9239   388214963 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
    9240             :                                          TREE_VEC_LENGTH (new_inner_args));
    9241             : 
    9242   396288418 :   return return_full_args ? new_args : new_inner_args;
    9243             : }
    9244             : 
    9245             : /* Returns true if T is a wrapper to make a C++20 template parameter
    9246             :    object const.  */
    9247             : 
    9248             : static bool
    9249 12455849656 : class_nttp_const_wrapper_p (tree t)
    9250             : {
    9251 12455849656 :   if (cxx_dialect < cxx20)
    9252             :     return false;
    9253   979139858 :   return (TREE_CODE (t) == VIEW_CONVERT_EXPR
    9254         114 :           && CP_TYPE_CONST_P (TREE_TYPE (t))
    9255   979139972 :           && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
    9256             : }
    9257             : 
    9258             : /* Returns 1 if template args OT and NT are equivalent.  */
    9259             : 
    9260             : int
    9261  1443405018 : template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
    9262             : {
    9263  1443405018 :   if (nt == ot)
    9264             :     return 1;
    9265   863352360 :   if (nt == NULL_TREE || ot == NULL_TREE)
    9266             :     return false;
    9267   863337542 :   if (nt == any_targ_node || ot == any_targ_node)
    9268             :     return true;
    9269             : 
    9270   863337538 :   if (class_nttp_const_wrapper_p (nt))
    9271          14 :     nt = TREE_OPERAND (nt, 0);
    9272   863337538 :   if (class_nttp_const_wrapper_p (ot))
    9273          10 :     ot = TREE_OPERAND (ot, 0);
    9274             : 
    9275             :   /* DR 1558: Don't treat an alias template specialization with dependent
    9276             :      arguments as equivalent to its underlying type when used as a template
    9277             :      argument; we need them to be distinct so that we substitute into the
    9278             :      specialization arguments at instantiation time.  And aliases can't be
    9279             :      equivalent without being ==, so we don't need to look any deeper.
    9280             : 
    9281             :      During partial ordering, however, we need to treat them normally so we can
    9282             :      order uses of the same alias with different cv-qualification (79960).  */
    9283   863337538 :   auto cso = make_temp_override (comparing_dependent_aliases);
    9284   863337538 :   if (!partial_order)
    9285   861920067 :     ++comparing_dependent_aliases;
    9286             : 
    9287   863337538 :   if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
    9288             :     /* For member templates */
    9289    25005147 :     return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
    9290   844314231 :   else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
    9291     9292219 :     return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
    9292     8107358 :             && template_args_equal (PACK_EXPANSION_PATTERN (ot),
    9293     8107358 :                                     PACK_EXPANSION_PATTERN (nt))
    9294    16355927 :             && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
    9295    16355927 :                                     PACK_EXPANSION_EXTRA_ARGS (nt)));
    9296   834769351 :   else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
    9297    81018631 :     return cp_tree_equal (ot, nt);
    9298   753750720 :   else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
    9299           0 :     gcc_unreachable ();
    9300   753750720 :   else if (TYPE_P (nt) || TYPE_P (ot))
    9301             :     {
    9302   577230222 :       if (!(TYPE_P (nt) && TYPE_P (ot)))
    9303             :         return false;
    9304   572360611 :       return same_type_p (ot, nt);
    9305             :     }
    9306             :   else
    9307             :     {
    9308             :       /* Try to treat a template non-type argument that has been converted
    9309             :          to the parameter type as equivalent to one that hasn't yet.  */
    9310   176520498 :       for (enum tree_code code1 = TREE_CODE (ot);
    9311   176647608 :            CONVERT_EXPR_CODE_P (code1)
    9312   176647608 :              || code1 == NON_LVALUE_EXPR;
    9313      127110 :            code1 = TREE_CODE (ot))
    9314      127110 :         ot = TREE_OPERAND (ot, 0);
    9315             : 
    9316   176520498 :       for (enum tree_code code2 = TREE_CODE (nt);
    9317   176726557 :            CONVERT_EXPR_CODE_P (code2)
    9318   176726557 :              || code2 == NON_LVALUE_EXPR;
    9319      206059 :            code2 = TREE_CODE (nt))
    9320      206059 :         nt = TREE_OPERAND (nt, 0);
    9321             : 
    9322   176520498 :       return cp_tree_equal (ot, nt);
    9323             :     }
    9324  1443405018 : }
    9325             : 
    9326             : /* Returns true iff the OLDARGS and NEWARGS are in fact identical sets of
    9327             :    template arguments.  Returns false otherwise, and updates OLDARG_PTR and
    9328             :    NEWARG_PTR with the offending arguments if they are non-NULL.  */
    9329             : 
    9330             : bool
    9331  1306003519 : comp_template_args (tree oldargs, tree newargs,
    9332             :                     tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */,
    9333             :                     bool partial_order /* = false */)
    9334             : {
    9335  1306003519 :   if (oldargs == newargs)
    9336             :     return true;
    9337             : 
    9338   887419002 :   if (!oldargs || !newargs)
    9339             :     return false;
    9340             : 
    9341   887419001 :   if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
    9342             :     return false;
    9343             : 
    9344  1572227648 :   for (int i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
    9345             :     {
    9346  1238556619 :       tree nt = TREE_VEC_ELT (newargs, i);
    9347  1238556619 :       tree ot = TREE_VEC_ELT (oldargs, i);
    9348             : 
    9349  1238556619 :       if (! template_args_equal (ot, nt, partial_order))
    9350             :         {
    9351   552966779 :           if (oldarg_ptr != NULL)
    9352           6 :             *oldarg_ptr = ot;
    9353   552966779 :           if (newarg_ptr != NULL)
    9354           6 :             *newarg_ptr = nt;
    9355   552966779 :           return false;
    9356             :         }
    9357             :     }
    9358             :   return true;
    9359             : }
    9360             : 
    9361             : inline bool
    9362     9263204 : comp_template_args_porder (tree oargs, tree nargs)
    9363             : {
    9364     9263204 :   return comp_template_args (oargs, nargs, NULL, NULL, true);
    9365             : }
    9366             : 
    9367             : /* Implement a freelist interface for objects of type T.
    9368             : 
    9369             :    Head is a separate object, rather than a regular member, so that we
    9370             :    can define it as a GTY deletable pointer, which is highly
    9371             :    desirable.  A data member could be declared that way, but then the
    9372             :    containing object would implicitly get GTY((user)), which would
    9373             :    prevent us from instantiating freelists as global objects.
    9374             :    Although this way we can create freelist global objects, they're
    9375             :    such thin wrappers that instantiating temporaries at every use
    9376             :    loses nothing and saves permanent storage for the freelist object.
    9377             : 
    9378             :    Member functions next, anew, poison and reinit have default
    9379             :    implementations that work for most of the types we're interested
    9380             :    in, but if they don't work for some type, they should be explicitly
    9381             :    specialized.  See the comments before them for requirements, and
    9382             :    the example specializations for the tree_list_freelist.  */
    9383             : template <typename T>
    9384             : class freelist
    9385             : {
    9386             :   /* Return the next object in a chain.  We could just do type
    9387             :      punning, but if we access the object with its underlying type, we
    9388             :      avoid strict-aliasing trouble.  This needs only work between
    9389             :      poison and reinit.  */
    9390   705520258 :   static T *&next (T *obj) { return obj->next; }
    9391             : 
    9392             :   /* Return a newly allocated, uninitialized or minimally-initialized
    9393             :      object of type T.  Any initialization performed by anew should
    9394             :      either remain across the life of the object and the execution of
    9395             :      poison, or be redone by reinit.  */
    9396     8668166 :   static T *anew () { return ggc_alloc<T> (); }
    9397             : 
    9398             :   /* Optionally scribble all over the bits holding the object, so that
    9399             :      they become (mostly?) uninitialized memory.  This is called while
    9400             :      preparing to make the object part of the free list.  */
    9401   356641364 :   static void poison (T *obj) {
    9402   356641364 :     T *p ATTRIBUTE_UNUSED = obj;
    9403   356641364 :     T **q ATTRIBUTE_UNUSED = &next (obj);
    9404             : 
    9405             : #ifdef ENABLE_GC_CHECKING
    9406             :     /* Poison the data, to indicate the data is garbage.  */
    9407             :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
    9408   356641364 :     memset (p, 0xa5, sizeof (*p));
    9409             : #endif
    9410             :     /* Let valgrind know the object is free.  */
    9411             :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
    9412             : 
    9413             :     /* Let valgrind know the next portion of the object is available,
    9414             :        but uninitialized.  */
    9415             :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
    9416             :   }
    9417             : 
    9418             :   /* Bring an object that underwent at least one lifecycle after anew
    9419             :      and before the most recent free and poison, back to a usable
    9420             :      state, reinitializing whatever is needed for it to be
    9421             :      functionally equivalent to an object just allocated and returned
    9422             :      by anew.  This may poison or clear the next field, used by
    9423             :      freelist housekeeping after poison was called.  */
    9424   348878894 :   static void reinit (T *obj) {
    9425   348878894 :     T **q ATTRIBUTE_UNUSED = &next (obj);
    9426             : 
    9427             : #ifdef ENABLE_GC_CHECKING
    9428   348878894 :     memset (q, 0xa5, sizeof (*q));
    9429             : #endif
    9430             :     /* Let valgrind know the entire object is available, but
    9431             :        uninitialized.  */
    9432             :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
    9433             :   }
    9434             : 
    9435             :   /* Reference a GTY-deletable pointer that points to the first object
    9436             :      in the free list proper.  */
    9437             :   T *&head;
    9438             : public:
    9439             :   /* Construct a freelist object chaining objects off of HEAD.  */
    9440   714189545 :   freelist (T *&head) : head(head) {}
    9441             : 
    9442             :   /* Add OBJ to the free object list.  The former head becomes OBJ's
    9443             :      successor.  */
    9444   356641709 :   void free (T *obj)
    9445             :   {
    9446   356641709 :     poison (obj);
    9447   356641709 :     next (obj) = head;
    9448   356641709 :     head = obj;
    9449   356641709 :   }
    9450             : 
    9451             :   /* Take an object from the free list, if one is available, or
    9452             :      allocate a new one.  Objects taken from the free list should be
    9453             :      regarded as filled with garbage, except for bits that are
    9454             :      configured to be preserved across free and alloc.  */
    9455   357547836 :   T *alloc ()
    9456             :   {
    9457   357547836 :     if (head)
    9458             :       {
    9459   348879193 :         T *obj = head;
    9460   348879193 :         head = next (head);
    9461   348879193 :         reinit (obj);
    9462   348879193 :         return obj;
    9463             :       }
    9464             :     else
    9465     8668643 :       return anew ();
    9466             :   }
    9467             : };
    9468             : 
    9469             : /* Explicitly specialize the interfaces for freelist<tree_node>: we
    9470             :    want to allocate a TREE_LIST using the usual interface, and ensure
    9471             :    TREE_CHAIN remains functional.  Alas, we have to duplicate a bit of
    9472             :    build_tree_list logic in reinit, so this could go out of sync.  */
    9473             : template <>
    9474             : inline tree &
    9475         989 : freelist<tree_node>::next (tree obj)
    9476             : {
    9477         989 :   return TREE_CHAIN (obj);
    9478             : }
    9479             : template <>
    9480             : inline tree
    9481         477 : freelist<tree_node>::anew ()
    9482             : {
    9483         477 :   return build_tree_list (NULL, NULL);
    9484             : }
    9485             : template <>
    9486             : inline void
    9487         345 : freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
    9488             : {
    9489         345 :   int size ATTRIBUTE_UNUSED = sizeof (tree_list);
    9490         345 :   tree p ATTRIBUTE_UNUSED = obj;
    9491         345 :   tree_base *b ATTRIBUTE_UNUSED = &obj->base;
    9492         345 :   tree *q ATTRIBUTE_UNUSED = &next (obj);
    9493             : 
    9494             : #ifdef ENABLE_GC_CHECKING
    9495         345 :   gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
    9496             : 
    9497             :   /* Poison the data, to indicate the data is garbage.  */
    9498         345 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
    9499         345 :   memset (p, 0xa5, size);
    9500             : #endif
    9501             :   /* Let valgrind know the object is free.  */
    9502         345 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
    9503             :   /* But we still want to use the TREE_CODE and TREE_CHAIN parts.  */
    9504         345 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
    9505         345 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
    9506             : 
    9507             : #ifdef ENABLE_GC_CHECKING
    9508         345 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
    9509             :   /* Keep TREE_CHAIN functional.  */
    9510         345 :   TREE_SET_CODE (obj, TREE_LIST);
    9511             : #else
    9512             :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
    9513             : #endif
    9514         345 : }
    9515             : template <>
    9516             : inline void
    9517         299 : freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
    9518             : {
    9519         299 :   tree_base *b ATTRIBUTE_UNUSED = &obj->base;
    9520             : 
    9521             : #ifdef ENABLE_GC_CHECKING
    9522         299 :   gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
    9523         299 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
    9524         299 :   memset (obj, 0, sizeof (tree_list));
    9525             : #endif
    9526             : 
    9527             :   /* Let valgrind know the entire object is available, but
    9528             :      uninitialized.  */
    9529         299 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
    9530             : 
    9531             : #ifdef ENABLE_GC_CHECKING
    9532         299 :   TREE_SET_CODE (obj, TREE_LIST);
    9533             : #else
    9534             :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
    9535             : #endif
    9536         299 : }
    9537             : 
    9538             : /* Point to the first object in the TREE_LIST freelist.  */
    9539             : static GTY((deletable)) tree tree_list_freelist_head;
    9540             : /* Return the/an actual TREE_LIST freelist.  */
    9541             : static inline freelist<tree_node>
    9542        1121 : tree_list_freelist ()
    9543             : {
    9544        1121 :   return tree_list_freelist_head;
    9545             : }
    9546             : 
    9547             : /* Point to the first object in the tinst_level freelist.  */
    9548             : static GTY((deletable)) tinst_level *tinst_level_freelist_head;
    9549             : /* Return the/an actual tinst_level freelist.  */
    9550             : static inline freelist<tinst_level>
    9551   695033398 : tinst_level_freelist ()
    9552             : {
    9553   695033398 :   return tinst_level_freelist_head;
    9554             : }
    9555             : 
    9556             : /* Point to the first object in the pending_template freelist.  */
    9557             : static GTY((deletable)) pending_template *pending_template_freelist_head;
    9558             : /* Return the/an actual pending_template freelist.  */
    9559             : static inline freelist<pending_template>
    9560    19155026 : pending_template_freelist ()
    9561             : {
    9562    19155026 :   return pending_template_freelist_head;
    9563             : }
    9564             : 
    9565             : /* Build the TREE_LIST object out of a split list, store it
    9566             :    permanently, and return it.  */
    9567             : tree
    9568         776 : tinst_level::to_list ()
    9569             : {
    9570         776 :   gcc_assert (split_list_p ());
    9571         776 :   tree ret = tree_list_freelist ().alloc ();
    9572         776 :   TREE_PURPOSE (ret) = tldcl;
    9573         776 :   TREE_VALUE (ret) = targs;
    9574         776 :   tldcl = ret;
    9575         776 :   targs = NULL;
    9576         776 :   gcc_assert (tree_list_p ());
    9577         776 :   return ret;
    9578             : }
    9579             : 
    9580             : const unsigned short tinst_level::refcount_infinity;
    9581             : 
    9582             : /* Increment OBJ's refcount unless it is already infinite.  */
    9583             : static tinst_level *
    9584  1075081825 : inc_refcount_use (tinst_level *obj)
    9585             : {
    9586   720595992 :   if (obj && obj->refcount != tinst_level::refcount_infinity)
    9587   720595992 :     ++obj->refcount;
    9588  1075081825 :   return obj;
    9589             : }
    9590             : 
    9591             : /* Release storage for OBJ and node, if it's a TREE_LIST.  */
    9592             : void
    9593   347201187 : tinst_level::free (tinst_level *obj)
    9594             : {
    9595   347201187 :   if (obj->tree_list_p ())
    9596         345 :     tree_list_freelist ().free (obj->get_node ());
    9597   347201187 :   tinst_level_freelist ().free (obj);
    9598   347201187 : }
    9599             : 
    9600             : /* Decrement OBJ's refcount if not infinite.  If it reaches zero, release
    9601             :    OBJ's DECL and OBJ, and start over with the tinst_level object that
    9602             :    used to be referenced by OBJ's NEXT.  */
    9603             : static void
    9604  1095471874 : dec_refcount_use (tinst_level *obj)
    9605             : {
    9606  1095471874 :   while (obj
    9607   719778857 :          && obj->refcount != tinst_level::refcount_infinity
    9608  2162451918 :          && !--obj->refcount)
    9609             :     {
    9610   347201187 :       tinst_level *next = obj->next;
    9611   347201187 :       tinst_level::free (obj);
    9612   347201187 :       obj = next;
    9613             :     }
    9614  1095471874 : }
    9615             : 
    9616             : /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
    9617             :    and of the former PTR.  Omitting the second argument is equivalent
    9618             :    to passing (T*)NULL; this is allowed because passing the
    9619             :    zero-valued integral constant NULL confuses type deduction and/or
    9620             :    overload resolution.  */
    9621             : template <typename T>
    9622             : static void
    9623  1095471874 : set_refcount_ptr (T *& ptr, T *obj = NULL)
    9624             : {
    9625  1075081825 :   T *save = ptr;
    9626  1095471874 :   ptr = inc_refcount_use (obj);
    9627  1075081825 :   dec_refcount_use (save);
    9628    10949872 : }
    9629             : 
    9630             : static void
    9631    12117907 : add_pending_template (tree d)
    9632             : {
    9633    12117907 :   tree ti = (TYPE_P (d)
    9634    12117907 :              ? CLASSTYPE_TEMPLATE_INFO (d)
    9635    12117907 :              : DECL_TEMPLATE_INFO (d));
    9636    12117907 :   struct pending_template *pt;
    9637    12117907 :   int level;
    9638             : 
    9639    12117907 :   if (TI_PENDING_TEMPLATE_FLAG (ti))
    9640             :     return;
    9641             : 
    9642             :   /* We are called both from instantiate_decl, where we've already had a
    9643             :      tinst_level pushed, and instantiate_template, where we haven't.
    9644             :      Compensate.  */
    9645     9714849 :   gcc_assert (TREE_CODE (d) != TREE_LIST);
    9646    19429698 :   level = !current_tinst_level
    9647    19429698 :     || current_tinst_level->maybe_get_node () != d;
    9648             : 
    9649           0 :   if (level)
    9650     9714849 :     push_tinst_level (d);
    9651             : 
    9652     9714849 :   pt = pending_template_freelist ().alloc ();
    9653     9714849 :   pt->next = NULL;
    9654     9714849 :   pt->tinst = NULL;
    9655     9714849 :   set_refcount_ptr (pt->tinst, current_tinst_level);
    9656     9714849 :   if (last_pending_template)
    9657     9688159 :     last_pending_template->next = pt;
    9658             :   else
    9659       26690 :     pending_templates = pt;
    9660             : 
    9661     9714849 :   last_pending_template = pt;
    9662             : 
    9663     9714849 :   TI_PENDING_TEMPLATE_FLAG (ti) = 1;
    9664             : 
    9665     9714849 :   if (level)
    9666           0 :     pop_tinst_level ();
    9667             : }
    9668             : 
    9669             : 
    9670             : /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
    9671             :    ARGLIST.  Valid choices for FNS are given in the cp-tree.def
    9672             :    documentation for TEMPLATE_ID_EXPR.  */
    9673             : 
    9674             : tree
    9675    26146717 : lookup_template_function (tree fns, tree arglist)
    9676             : {
    9677    26146717 :   if (fns == error_mark_node || arglist == error_mark_node)
    9678             :     return error_mark_node;
    9679             : 
    9680    26146644 :   gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
    9681             : 
    9682    26146644 :   if (!is_overloaded_fn (fns) && !identifier_p (fns))
    9683             :     {
    9684           4 :       error ("%q#D is not a function template", fns);
    9685           4 :       return error_mark_node;
    9686             :     }
    9687             : 
    9688    26146640 :   if (BASELINK_P (fns))
    9689             :     {
    9690     4096927 :       fns = copy_node (fns);
    9691     8193854 :       BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
    9692             :                                          unknown_type_node,
    9693     4096927 :                                          BASELINK_FUNCTIONS (fns),
    9694             :                                          arglist);
    9695     4096927 :       return fns;
    9696             :     }
    9697             : 
    9698    22049713 :   return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
    9699             : }
    9700             : 
    9701             : /* Within the scope of a template class S<T>, the name S gets bound
    9702             :    (in build_self_reference) to a TYPE_DECL for the class, not a
    9703             :    TEMPLATE_DECL.  If DECL is a TYPE_DECL for current_class_type,
    9704             :    or one of its enclosing classes, and that type is a template,
    9705             :    return the associated TEMPLATE_DECL.  Otherwise, the original
    9706             :    DECL is returned.
    9707             : 
    9708             :    Also handle the case when DECL is a TREE_LIST of ambiguous
    9709             :    injected-class-names from different bases.  */
    9710             : 
    9711             : tree
    9712   392037268 : maybe_get_template_decl_from_type_decl (tree decl)
    9713             : {
    9714   392037268 :   if (decl == NULL_TREE)
    9715             :     return decl;
    9716             : 
    9717             :   /* DR 176: A lookup that finds an injected-class-name (10.2
    9718             :      [class.member.lookup]) can result in an ambiguity in certain cases
    9719             :      (for example, if it is found in more than one base class). If all of
    9720             :      the injected-class-names that are found refer to specializations of
    9721             :      the same class template, and if the name is followed by a
    9722             :      template-argument-list, the reference refers to the class template
    9723             :      itself and not a specialization thereof, and is not ambiguous.  */
    9724   392036902 :   if (TREE_CODE (decl) == TREE_LIST)
    9725             :     {
    9726             :       tree t, tmpl = NULL_TREE;
    9727       27610 :       for (t = decl; t; t = TREE_CHAIN (t))
    9728             :         {
    9729       27526 :           tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
    9730       27526 :           if (!tmpl)
    9731             :             tmpl = elt;
    9732       13763 :           else if (tmpl != elt)
    9733             :             break;
    9734             :         }
    9735       13763 :       if (tmpl && t == NULL_TREE)
    9736             :         return tmpl;
    9737             :       else
    9738             :         return decl;
    9739             :     }
    9740             : 
    9741   392023139 :   return (decl != NULL_TREE
    9742   392023139 :           && DECL_SELF_REFERENCE_P (decl)
    9743     4123593 :           && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
    9744     4123593 :     ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
    9745             : }
    9746             : 
    9747             : /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
    9748             :    parameters, find the desired type.
    9749             : 
    9750             :    D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
    9751             : 
    9752             :    IN_DECL, if non-NULL, is the template declaration we are trying to
    9753             :    instantiate.
    9754             : 
    9755             :    If ENTERING_SCOPE is nonzero, we are about to enter the scope of
    9756             :    the class we are looking up.
    9757             : 
    9758             :    Issue error and warning messages under control of COMPLAIN.
    9759             : 
    9760             :    If the template class is really a local class in a template
    9761             :    function, then the FUNCTION_CONTEXT is the function in which it is
    9762             :    being instantiated.
    9763             : 
    9764             :    ??? Note that this function is currently called *twice* for each
    9765             :    template-id: the first time from the parser, while creating the
    9766             :    incomplete type (finish_template_type), and the second type during the
    9767             :    real instantiation (instantiate_template_class). This is surely something
    9768             :    that we want to avoid. It also causes some problems with argument
    9769             :    coercion (see convert_nontype_argument for more information on this).  */
    9770             : 
    9771             : tree
    9772   558660578 : lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
    9773             :                        int entering_scope, tsubst_flags_t complain)
    9774             : {
    9775   558660578 :   auto_timevar tv (TV_TEMPLATE_INST);
    9776             : 
    9777   558660578 :   tree templ = NULL_TREE, parmlist;
    9778   558660578 :   tree t;
    9779   558660578 :   spec_entry **slot;
    9780   558660578 :   spec_entry *entry;
    9781   558660578 :   spec_entry elt;
    9782   558660578 :   hashval_t hash;
    9783             : 
    9784   558660578 :   if (identifier_p (d1))
    9785             :     {
    9786        6788 :       tree value = innermost_non_namespace_value (d1);
    9787        6788 :       if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
    9788             :         templ = value;
    9789             :       else
    9790             :         {
    9791        6788 :           if (context)
    9792        6788 :             push_decl_namespace (context);
    9793        6788 :           templ = lookup_name (d1);
    9794        6788 :           templ = maybe_get_template_decl_from_type_decl (templ);
    9795        6788 :           if (context)
    9796        6788 :             pop_decl_namespace ();
    9797             :         }
    9798             :     }
    9799   558653790 :   else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
    9800             :     {
    9801           0 :       tree type = TREE_TYPE (d1);
    9802             : 
    9803             :       /* If we are declaring a constructor, say A<T>::A<T>, we will get
    9804             :          an implicit typename for the second A.  Deal with it.  */
    9805           0 :       if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
    9806           0 :         type = TREE_TYPE (type);
    9807             : 
    9808           0 :       if (CLASSTYPE_TEMPLATE_INFO (type))
    9809             :         {
    9810           0 :           templ = CLASSTYPE_TI_TEMPLATE (type);
    9811           0 :           d1 = DECL_NAME (templ);
    9812             :         }
    9813             :     }
    9814   558653790 :   else if (TREE_CODE (d1) == ENUMERAL_TYPE
    9815   558653790 :            || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
    9816             :     {
    9817   445032827 :       templ = TYPE_TI_TEMPLATE (d1);
    9818   445032827 :       d1 = DECL_NAME (templ);
    9819             :     }
    9820   113620963 :   else if (DECL_TYPE_TEMPLATE_P (d1))
    9821             :     {
    9822   113620963 :       templ = d1;
    9823   113620963 :       d1 = DECL_NAME (templ);
    9824             :     }
    9825           0 :   else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
    9826             :     {
    9827           0 :       templ = d1;
    9828           0 :       d1 = DECL_NAME (templ);
    9829             :     }
    9830             : 
    9831             :   /* Issue an error message if we didn't find a template.  */
    9832   558660578 :   if (! templ)
    9833             :     {
    9834         366 :       if (complain & tf_error)
    9835           0 :         error ("%qT is not a template", d1);
    9836         366 :       return error_mark_node;
    9837             :     }
    9838             : 
    9839   558660212 :   if (TREE_CODE (templ) != TEMPLATE_DECL
    9840             :          /* Make sure it's a user visible template, if it was named by
    9841             :             the user.  */
    9842   558660212 :       || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
    9843   113297054 :           && !PRIMARY_TEMPLATE_P (templ)))
    9844             :     {
    9845           0 :       if (complain & tf_error)
    9846             :         {
    9847           0 :           error ("non-template type %qT used as a template", d1);
    9848           0 :           if (in_decl)
    9849           0 :             error ("for template declaration %q+D", in_decl);
    9850             :         }
    9851           0 :       return error_mark_node;
    9852             :     }
    9853             : 
    9854   558660212 :   complain &= ~tf_user;
    9855             : 
    9856             :   /* An alias that just changes the name of a template is equivalent to the
    9857             :      other template, so if any of the arguments are pack expansions, strip
    9858             :      the alias to avoid problems with a pack expansion passed to a non-pack
    9859             :      alias template parameter (DR 1430).  */
    9860   558660212 :   if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
    9861     5478020 :     templ = get_underlying_template (templ);
    9862             : 
    9863   558660212 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
    9864             :     {
    9865      164123 :       tree parm;
    9866      164123 :       tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
    9867      164123 :       if (arglist2 == error_mark_node
    9868      164123 :           || (!uses_template_parms (arglist2)
    9869        7968 :               && check_instantiated_args (templ, arglist2, complain)))
    9870          23 :         return error_mark_node;
    9871             : 
    9872      164100 :       parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
    9873      164100 :       return parm;
    9874             :     }
    9875             :   else
    9876             :     {
    9877   558496089 :       tree template_type = TREE_TYPE (templ);
    9878   558496089 :       tree gen_tmpl;
    9879   558496089 :       tree type_decl;
    9880   558496089 :       tree found = NULL_TREE;
    9881   558496089 :       int arg_depth;
    9882   558496089 :       int parm_depth;
    9883   558496089 :       int is_dependent_type;
    9884   558496089 :       int use_partial_inst_tmpl = false;
    9885             : 
    9886   558496089 :       if (template_type == error_mark_node)
    9887             :         /* An error occurred while building the template TEMPL, and a
    9888             :            diagnostic has most certainly been emitted for that
    9889             :            already.  Let's propagate that error.  */
    9890             :         return error_mark_node;
    9891             : 
    9892   558496088 :       gen_tmpl = most_general_template (templ);
    9893   558496088 :       if (modules_p ())
    9894     2625659 :         lazy_load_pendings (gen_tmpl);
    9895             : 
    9896   558496088 :       parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
    9897   558496088 :       parm_depth = TMPL_PARMS_DEPTH (parmlist);
    9898  1116992176 :       arg_depth = TMPL_ARGS_DEPTH (arglist);
    9899             : 
    9900   558496088 :       if (arg_depth == 1 && parm_depth > 1)
    9901             :         {
    9902             :           /* We've been given an incomplete set of template arguments.
    9903             :              For example, given:
    9904             : 
    9905             :                template <class T> struct S1 {
    9906             :                  template <class U> struct S2 {};
    9907             :                  template <class U> struct S2<U*> {};
    9908             :                 };
    9909             : 
    9910             :              we will be called with an ARGLIST of `U*', but the
    9911             :              TEMPLATE will be `template <class T> template
    9912             :              <class U> struct S1<T>::S2'.  We must fill in the missing
    9913             :              arguments.  */
    9914     4715262 :           tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
    9915     4715262 :           arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
    9916     9430524 :           arg_depth = TMPL_ARGS_DEPTH (arglist);
    9917             :         }
    9918             : 
    9919             :       /* Now we should have enough arguments.  */
    9920   558496088 :       gcc_assert (parm_depth == arg_depth);
    9921             : 
    9922             :       /* From here on, we're only interested in the most general
    9923             :          template.  */
    9924             : 
    9925             :       /* Shortcut looking up the current class scope again.  */
    9926   558496088 :       for (tree cur = current_nonlambda_class_type ();
    9927   955493340 :            cur != NULL_TREE;
    9928   396997252 :            cur = get_containing_scope (cur))
    9929             :         {
    9930   704070379 :           if (!CLASS_TYPE_P (cur))
    9931   268855155 :             continue;
    9932             : 
    9933   435215224 :           tree ti = CLASSTYPE_TEMPLATE_INFO (cur);
    9934  1254856400 :           if (!ti || arg_depth > TMPL_ARGS_DEPTH (TI_ARGS (ti)))
    9935             :             break;
    9936             : 
    9937   406012009 :           if (gen_tmpl == most_general_template (TI_TEMPLATE (ti))
    9938   406012009 :               && comp_template_args (arglist, TI_ARGS (ti)))
    9939   277869912 :             return cur;
    9940             :         }
    9941             : 
    9942             :       /* Calculate the BOUND_ARGS.  These will be the args that are
    9943             :          actually tsubst'd into the definition to create the
    9944             :          instantiation.  */
    9945   280626176 :       if (PRIMARY_TEMPLATE_P (gen_tmpl))
    9946   276846668 :         arglist = coerce_template_parms (parmlist, arglist, gen_tmpl, complain);
    9947             : 
    9948   280626173 :       if (arglist == error_mark_node)
    9949             :         /* We were unable to bind the arguments.  */
    9950             :         return error_mark_node;
    9951             : 
    9952             :       /* In the scope of a template class, explicit references to the
    9953             :          template class refer to the type of the template, not any
    9954             :          instantiation of it.  For example, in:
    9955             : 
    9956             :            template <class T> class C { void f(C<T>); }
    9957             : 
    9958             :          the `C<T>' is just the same as `C'.  Outside of the
    9959             :          class, however, such a reference is an instantiation.  */
    9960   280625285 :       if (entering_scope
    9961   214071186 :           || !PRIMARY_TEMPLATE_P (gen_tmpl)
    9962   491151048 :           || currently_open_class (template_type))
    9963             :         {
    9964    72104601 :           tree tinfo = TYPE_TEMPLATE_INFO (template_type);
    9965             : 
    9966   144139482 :           if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
    9967             :             return template_type;
    9968             :         }
    9969             : 
    9970             :       /* If we already have this specialization, return it.  */
    9971   269987484 :       elt.tmpl = gen_tmpl;
    9972   269987484 :       elt.args = arglist;
    9973   269987484 :       elt.spec = NULL_TREE;
    9974   269987484 :       hash = spec_hasher::hash (&elt);
    9975   269987484 :       entry = type_specializations->find_with_hash (&elt, hash);
    9976             : 
    9977   269987484 :       if (entry)
    9978   199287460 :         return entry->spec;
    9979             : 
    9980             :       /* If the template's constraints are not satisfied,
    9981             :          then we cannot form a valid type.
    9982             : 
    9983             :          Note that the check is deferred until after the hash
    9984             :          lookup. This prevents redundant checks on previously
    9985             :          instantiated specializations. */
    9986    70700024 :       if (flag_concepts
    9987     4728620 :           && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
    9988    74750610 :           && !constraints_satisfied_p (gen_tmpl, arglist))
    9989             :         {
    9990         320 :           if (complain & tf_error)
    9991             :             {
    9992          31 :               auto_diagnostic_group d;
    9993          31 :               error ("template constraint failure for %qD", gen_tmpl);
    9994          31 :               diagnose_constraints (input_location, gen_tmpl, arglist);
    9995          31 :             }
    9996         320 :           return error_mark_node;
    9997             :         }
    9998             : 
    9999    70699704 :       is_dependent_type = uses_template_parms (arglist);
   10000             : 
   10001             :       /* If the deduced arguments are invalid, then the binding
   10002             :          failed.  */
   10003    70699704 :       if (!is_dependent_type
   10004    70699704 :           && check_instantiated_args (gen_tmpl,
   10005             :                                       INNERMOST_TEMPLATE_ARGS (arglist),
   10006             :                                       complain))
   10007          10 :         return error_mark_node;
   10008             : 
   10009    70699694 :       if (!is_dependent_type
   10010    26757069 :           && !PRIMARY_TEMPLATE_P (gen_tmpl)
   10011     1829989 :           && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
   10012    71759792 :           && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
   10013             :         /* This occurs when the user has tried to define a tagged type
   10014             :            in a scope that forbids it.  We emitted an error during the
   10015             :            parse.  We didn't complete the bail out then, so here we
   10016             :            are.  */
   10017          14 :         return error_mark_node;
   10018             : 
   10019    70699680 :       context = DECL_CONTEXT (gen_tmpl);
   10020    70699680 :       if (context && TYPE_P (context))
   10021             :         {
   10022     4958070 :           if (!uses_template_parms (DECL_CONTEXT (templ)))
   10023             :             /* If the context of the partially instantiated template is
   10024             :                already non-dependent, then we might as well use it.  */
   10025     1961297 :             context = DECL_CONTEXT (templ);
   10026             :           else
   10027             :             {
   10028     2996773 :               context = tsubst_aggr_type (context, arglist,
   10029             :                                           complain, in_decl, true);
   10030             :               /* Try completing the enclosing context if it's not already so.  */
   10031     2996773 :               if (context != error_mark_node
   10032     2996773 :                   && !COMPLETE_TYPE_P (context))
   10033             :                 {
   10034     2910204 :                   context = complete_type (context);
   10035     2910204 :                   if (COMPLETE_TYPE_P (context))
   10036             :                     {
   10037             :                       /* Completion could have caused us to register the desired
   10038             :                          specialization already, so check the table again.  */
   10039           6 :                       entry = type_specializations->find_with_hash (&elt, hash);
   10040           6 :                       if (entry)
   10041           6 :                         return entry->spec;
   10042             :                     }
   10043             :                 }
   10044             :             }
   10045             :         }
   10046             :       else
   10047    65741610 :         context = tsubst (context, arglist, complain, in_decl);
   10048             : 
   10049    70699674 :       if (context == error_mark_node)
   10050             :         return error_mark_node;
   10051             : 
   10052    70699670 :       if (!context)
   10053           0 :         context = global_namespace;
   10054             : 
   10055             :       /* Create the type.  */
   10056    70699670 :       if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
   10057             :         {
   10058             :           /* The user referred to a specialization of an alias
   10059             :             template represented by GEN_TMPL.
   10060             : 
   10061             :             [temp.alias]/2 says:
   10062             : 
   10063             :                 When a template-id refers to the specialization of an
   10064             :                 alias template, it is equivalent to the associated
   10065             :                 type obtained by substitution of its
   10066             :                 template-arguments for the template-parameters in the
   10067             :                 type-id of the alias template.  */
   10068             : 
   10069     6716646 :           t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
   10070             :           /* Note that the call above (by indirectly calling
   10071             :              register_specialization in tsubst_decl) registers the
   10072             :              TYPE_DECL representing the specialization of the alias
   10073             :              template.  So next time someone substitutes ARGLIST for
   10074             :              the template parms into the alias template (GEN_TMPL),
   10075             :              she'll get that TYPE_DECL back.  */
   10076             : 
   10077     6716646 :           if (t == error_mark_node)
   10078             :             return t;
   10079             :         }
   10080    63983024 :       else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
   10081             :         {
   10082      231276 :           if (!is_dependent_type)
   10083             :             {
   10084      231273 :               set_current_access_from_decl (TYPE_NAME (template_type));
   10085      462546 :               t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
   10086      231273 :                               tsubst (ENUM_UNDERLYING_TYPE (template_type),
   10087             :                                       arglist, complain, in_decl),
   10088      231273 :                               tsubst_attributes (TYPE_ATTRIBUTES (template_type),
   10089             :                                                  arglist, complain, in_decl),
   10090      231273 :                               SCOPED_ENUM_P (template_type), NULL);
   10091             : 
   10092      231273 :               if (t == error_mark_node)
   10093             :                 return t;
   10094             :             }
   10095             :           else
   10096             :             {
   10097             :               /* We don't want to call start_enum for this type, since
   10098             :                  the values for the enumeration constants may involve
   10099             :                  template parameters.  And, no one should be interested
   10100             :                  in the enumeration constants for such a type.  */
   10101           3 :               t = cxx_make_type (ENUMERAL_TYPE);
   10102           6 :               SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
   10103             :             }
   10104      462506 :           SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
   10105      462536 :           ENUM_FIXED_UNDERLYING_TYPE_P (t)
   10106      231268 :             = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
   10107             :         }
   10108    63751748 :       else if (CLASS_TYPE_P (template_type))
   10109             :         {
   10110             :           /* Lambda closures are regenerated in tsubst_lambda_expr, not
   10111             :              instantiated here.  */
   10112   127407784 :           gcc_assert (!LAMBDA_TYPE_P (template_type));
   10113             : 
   10114    63751748 :           t = make_class_type (TREE_CODE (template_type));
   10115   191255244 :           CLASSTYPE_DECLARED_CLASS (t)
   10116    63751748 :             = CLASSTYPE_DECLARED_CLASS (template_type);
   10117    63751748 :           SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
   10118             : 
   10119             :           /* A local class.  Make sure the decl gets registered properly.  */
   10120    63751748 :           if (context == current_function_decl)
   10121      321711 :             if (pushtag (DECL_NAME (gen_tmpl), t)
   10122      321711 :                 == error_mark_node)
   10123             :               return error_mark_node;
   10124             : 
   10125    63751732 :           if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
   10126             :             /* This instantiation is another name for the primary
   10127             :                template type. Set the TYPE_CANONICAL field
   10128             :                appropriately. */
   10129     2985306 :             TYPE_CANONICAL (t) = template_type;
   10130    60766426 :           else if (any_template_arguments_need_structural_equality_p (arglist))
   10131       27156 :             SET_TYPE_STRUCTURAL_EQUALITY (t);
   10132             :         }
   10133             :       else
   10134           0 :         gcc_unreachable ();
   10135             : 
   10136             :       /* If we called start_enum or pushtag above, this information
   10137             :          will already be set up.  */
   10138    70694607 :       type_decl = TYPE_NAME (t);
   10139    70694607 :       if (!type_decl)
   10140             :         {
   10141    63430040 :           TYPE_CONTEXT (t) = FROB_CONTEXT (context);
   10142             : 
   10143    63430040 :           type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
   10144    63430040 :           DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
   10145   126860080 :           DECL_SOURCE_LOCATION (type_decl)
   10146    63430040 :             = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
   10147             :         }
   10148             : 
   10149    70694607 :       set_instantiating_module (type_decl);
   10150             :       /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
   10151             :          of export flag.  We want to propagate this because it might
   10152             :          be a friend declaration that pushes a new hidden binding.  */
   10153    70694607 :       DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl);
   10154             : 
   10155    70694607 :       if (CLASS_TYPE_P (template_type))
   10156             :         {
   10157    65801821 :           TREE_PRIVATE (type_decl)
   10158    65801821 :             = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
   10159    65801821 :           TREE_PROTECTED (type_decl)
   10160    65801821 :             = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
   10161    65801821 :           if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
   10162             :             {
   10163    65042504 :               DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
   10164    65042504 :               DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
   10165             :             }
   10166             :         }
   10167             : 
   10168    70694607 :       if (OVERLOAD_TYPE_P (t)
   10169   136978207 :           && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
   10170             :         {
   10171             :           static const char *tags[] = {"abi_tag", "may_alias"};
   10172             : 
   10173   191949000 :           for (unsigned ix = 0; ix != 2; ix++)
   10174             :             {
   10175   127966000 :               tree attributes
   10176   127966000 :                 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
   10177             : 
   10178   127966000 :               if (attributes)
   10179          64 :                 TYPE_ATTRIBUTES (t)
   10180         128 :                   = tree_cons (TREE_PURPOSE (attributes),
   10181          64 :                                TREE_VALUE (attributes),
   10182          64 :                                TYPE_ATTRIBUTES (t));
   10183             :             }
   10184             :         }
   10185             : 
   10186             :       /* Let's consider the explicit specialization of a member
   10187             :          of a class template specialization that is implicitly instantiated,
   10188             :          e.g.:
   10189             :              template<class T>
   10190             :              struct S
   10191             :              {
   10192             :                template<class U> struct M {}; //#0
   10193             :              };
   10194             : 
   10195             :              template<>
   10196             :              template<>
   10197             :              struct S<int>::M<char> //#1
   10198             :              {
   10199             :                int i;
   10200             :              };
   10201             :         [temp.expl.spec]/4 says this is valid.
   10202             : 
   10203             :         In this case, when we write:
   10204             :         S<int>::M<char> m;
   10205             : 
   10206             :         M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
   10207             :         the one of #0.
   10208             : 
   10209             :         When we encounter #1, we want to store the partial instantiation
   10210             :         of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
   10211             : 
   10212             :         For all cases other than this "explicit specialization of member of a
   10213             :         class template", we just want to store the most general template into
   10214             :         the CLASSTYPE_TI_TEMPLATE of M.
   10215             : 
   10216             :         This case of "explicit specialization of member of a class template"
   10217             :         only happens when:
   10218             :         1/ the enclosing class is an instantiation of, and therefore not
   10219             :         the same as, the context of the most general template, and
   10220             :         2/ we aren't looking at the partial instantiation itself, i.e.
   10221             :         the innermost arguments are not the same as the innermost parms of
   10222             :         the most general template.
   10223             : 
   10224             :         So it's only when 1/ and 2/ happens that we want to use the partial
   10225             :         instantiation of the member template in lieu of its most general
   10226             :         template.  */
   10227             : 
   10228    70694607 :       if (PRIMARY_TEMPLATE_P (gen_tmpl)
   10229   139269062 :           && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
   10230             :           /* the enclosing class must be an instantiation...  */
   10231     2850396 :           && CLASS_TYPE_P (context)
   10232    73544897 :           && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
   10233             :         {
   10234     1355739 :           TREE_VEC_LENGTH (arglist)--;
   10235     1355739 :           ++processing_template_decl;
   10236     1355739 :           tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
   10237     1355739 :           tree partial_inst_args =
   10238     1355739 :             tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
   10239             :                     arglist, complain, NULL_TREE);
   10240     1355739 :           --processing_template_decl;
   10241     1355739 :           TREE_VEC_LENGTH (arglist)++;
   10242     1355739 :           if (partial_inst_args == error_mark_node)
   10243             :             return error_mark_node;
   10244     2711472 :           use_partial_inst_tmpl =
   10245             :             /*...and we must not be looking at the partial instantiation
   10246             :              itself. */
   10247     1355736 :             !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
   10248             :                                  partial_inst_args);
   10249             :         }
   10250             : 
   10251     1355736 :       if (!use_partial_inst_tmpl)
   10252             :         /* This case is easy; there are no member templates involved.  */
   10253             :         found = gen_tmpl;
   10254             :       else
   10255             :         {
   10256             :           /* This is a full instantiation of a member template.  Find
   10257             :              the partial instantiation of which this is an instance.  */
   10258             : 
   10259             :           /* Temporarily reduce by one the number of levels in the ARGLIST
   10260             :              so as to avoid comparing the last set of arguments.  */
   10261      848019 :           TREE_VEC_LENGTH (arglist)--;
   10262             :           /* We don't use COMPLAIN in the following call because this isn't
   10263             :              the immediate context of deduction.  For instance, tf_partial
   10264             :              could be set here as we might be at the beginning of template
   10265             :              argument deduction when any explicitly specified template
   10266             :              arguments are substituted into the function type.  tf_partial
   10267             :              could lead into trouble because we wouldn't find the partial
   10268             :              instantiation that might have been created outside tf_partial
   10269             :              context, because the levels of template parameters wouldn't
   10270             :              match, because in a tf_partial context, tsubst doesn't reduce
   10271             :              TEMPLATE_PARM_LEVEL.  */
   10272      848019 :           found = tsubst (gen_tmpl, arglist, tf_none, NULL_TREE);
   10273      848019 :           TREE_VEC_LENGTH (arglist)++;
   10274             :           /* FOUND is either a proper class type, or an alias
   10275             :              template specialization.  In the later case, it's a
   10276             :              TYPE_DECL, resulting from the substituting of arguments
   10277             :              for parameters in the TYPE_DECL of the alias template
   10278             :              done earlier.  So be careful while getting the template
   10279             :              of FOUND.  */
   10280     1696038 :           found = (TREE_CODE (found) == TEMPLATE_DECL
   10281      848019 :                    ? found
   10282             :                    : (TREE_CODE (found) == TYPE_DECL
   10283      399435 :                       ? DECL_TI_TEMPLATE (found)
   10284           0 :                       : CLASSTYPE_TI_TEMPLATE (found)));
   10285             : 
   10286      848019 :           if (DECL_CLASS_TEMPLATE_P (found)
   10287     1296603 :               && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
   10288             :             {
   10289             :               /* If this partial instantiation is specialized, we want to
   10290             :                  use it for hash table lookup.  */
   10291          12 :               elt.tmpl = found;
   10292          12 :               elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
   10293          12 :               hash = spec_hasher::hash (&elt);
   10294             :             }
   10295             :         }
   10296             : 
   10297             :       /* Build template info for the new specialization.  */
   10298    70694604 :       SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
   10299             : 
   10300    70694604 :       elt.spec = t;
   10301    70694604 :       slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
   10302    70694604 :       gcc_checking_assert (*slot == NULL);
   10303    70694604 :       entry = ggc_alloc<spec_entry> ();
   10304    70694604 :       *entry = elt;
   10305    70694604 :       *slot = entry;
   10306             : 
   10307             :       /* Note this use of the partial instantiation so we can check it
   10308             :          later in maybe_process_partial_specialization.  */
   10309   141389208 :       DECL_TEMPLATE_INSTANTIATIONS (found)
   10310    70694604 :         = tree_cons (arglist, t,
   10311    70694604 :                      DECL_TEMPLATE_INSTANTIATIONS (found));
   10312             : 
   10313    70694604 :       if (TREE_CODE (template_type) == ENUMERAL_TYPE
   10314      231286 :           && !uses_template_parms (current_nonlambda_scope ())
   10315    70925890 :           && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
   10316             :         /* Now that the type has been registered on the instantiations
   10317             :            list, we set up the enumerators.  Because the enumeration
   10318             :            constants may involve the enumeration type itself, we make
   10319             :            sure to register the type first, and then create the
   10320             :            constants.  That way, doing tsubst_expr for the enumeration
   10321             :            constants won't result in recursive calls here; we'll find
   10322             :            the instantiation and exit above.  */
   10323      231268 :         tsubst_enum (template_type, t, arglist);
   10324             : 
   10325    70694544 :       if (CLASS_TYPE_P (template_type) && is_dependent_type)
   10326             :         /* If the type makes use of template parameters, the
   10327             :            code that generates debugging information will crash.  */
   10328    39676915 :         DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
   10329             : 
   10330             :       /* Possibly limit visibility based on template args.  */
   10331    70694544 :       TREE_PUBLIC (type_decl) = 1;
   10332    70694544 :       determine_visibility (type_decl);
   10333             : 
   10334    70694544 :       inherit_targ_abi_tags (t);
   10335             : 
   10336    70694544 :       return t;
   10337             :     }
   10338   558660515 : }
   10339             : 
   10340             : /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.  */
   10341             : 
   10342             : tree
   10343     4554612 : lookup_template_variable (tree templ, tree arglist, tsubst_flags_t complain)
   10344             : {
   10345     4554612 :   if (flag_concepts && variable_concept_p (templ))
   10346           0 :     return build_concept_check (templ, arglist, tf_none);
   10347             : 
   10348     4554612 :   tree gen_templ = most_general_template (templ);
   10349     4554612 :   tree parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_templ);
   10350     4554612 :   arglist = add_outermost_template_args (templ, arglist);
   10351     4554612 :   arglist = coerce_template_parms (parms, arglist, templ, complain);
   10352     4554612 :   if (arglist == error_mark_node)
   10353             :     return error_mark_node;
   10354             : 
   10355             :   /* The type of the expression is NULL_TREE since the template-id could refer
   10356             :      to an explicit or partial specialization. */
   10357     4554586 :   return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
   10358             : }
   10359             : 
   10360             : /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR if it's
   10361             :    not dependent.  */
   10362             : 
   10363             : tree
   10364     4055923 : finish_template_variable (tree var, tsubst_flags_t complain)
   10365             : {
   10366     4055923 :   tree templ = TREE_OPERAND (var, 0);
   10367     4055923 :   tree arglist = TREE_OPERAND (var, 1);
   10368             : 
   10369             :   /* If the template or arguments are dependent, then we
   10370             :      can't resolve the TEMPLATE_ID_EXPR yet.  */
   10371     4055923 :   if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ)) != 1
   10372     4055923 :       || any_dependent_template_arguments_p (arglist))
   10373     2793338 :     return var;
   10374             : 
   10375     1262585 :   if (flag_concepts && !constraints_satisfied_p (templ, arglist))
   10376             :     {
   10377           6 :       if (complain & tf_error)
   10378             :         {
   10379           5 :           auto_diagnostic_group d;
   10380           5 :           error ("use of invalid variable template %qE", var);
   10381           5 :           diagnose_constraints (location_of (var), templ, arglist);
   10382           5 :         }
   10383           6 :       return error_mark_node;
   10384             :     }
   10385             : 
   10386     1262579 :   return instantiate_template (templ, arglist, complain);
   10387             : }
   10388             : 
   10389             : /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
   10390             :    TARGS template args, and instantiate it if it's not dependent.  */
   10391             : 
   10392             : tree
   10393     1789118 : lookup_and_finish_template_variable (tree templ, tree targs,
   10394             :                                      tsubst_flags_t complain)
   10395             : {
   10396     1789118 :   tree var = lookup_template_variable (templ, targs, complain);
   10397     1789118 :   if (var == error_mark_node)
   10398             :     return error_mark_node;
   10399             :   /* We may be called while doing a partial substitution, but the
   10400             :      type of the variable template may be auto, in which case we
   10401             :      will call do_auto_deduction in mark_used (which clears tf_partial)
   10402             :      and the auto must be properly reduced at that time for the
   10403             :      deduction to work.  */
   10404     1789118 :   complain &= ~tf_partial;
   10405     1789118 :   var = finish_template_variable (var, complain);
   10406     1789118 :   mark_used (var);
   10407     1789118 :   return var;
   10408             : }
   10409             : 
   10410             : /* If the set of template parameters PARMS contains a template parameter
   10411             :    at the given LEVEL and INDEX, then return this parameter.  Otherwise
   10412             :    return NULL_TREE.  */
   10413             : 
   10414             : static tree
   10415     2729825 : corresponding_template_parameter_list (tree parms, int level, int index)
   10416             : {
   10417     2866789 :   while (TMPL_PARMS_DEPTH (parms) > level)
   10418      136964 :     parms = TREE_CHAIN (parms);
   10419             : 
   10420     2729825 :   if (TMPL_PARMS_DEPTH (parms) != level
   10421     2729825 :       || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
   10422             :     return NULL_TREE;
   10423             : 
   10424     2652179 :   return TREE_VEC_ELT (TREE_VALUE (parms), index);
   10425             : }
   10426             : 
   10427             : /* Return the TREE_LIST for the template parameter from PARMS that positionally
   10428             :    corresponds to the template parameter PARM, or else return NULL_TREE.  */
   10429             : 
   10430             : static tree
   10431     2729825 : corresponding_template_parameter_list (tree parms, tree parm)
   10432             : {
   10433     2729825 :   int level, index;
   10434     2729825 :   template_parm_level_and_index (parm, &level, &index);
   10435     2729825 :   return corresponding_template_parameter_list (parms, level, index);
   10436             : }
   10437             : 
   10438             : /* As above, but pull out the actual parameter.  */
   10439             : 
   10440             : static tree
   10441     2729748 : corresponding_template_parameter (tree parms, tree parm)
   10442             : {
   10443     2729748 :   tree list = corresponding_template_parameter_list (parms, parm);
   10444     2729748 :   if (!list)
   10445             :     return NULL_TREE;
   10446             : 
   10447     2652102 :   tree t = TREE_VALUE (list);
   10448             :   /* As in template_parm_to_arg.  */
   10449     2652102 :   if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
   10450     2612585 :     t = TREE_TYPE (t);
   10451             :   else
   10452       39517 :     t = DECL_INITIAL (t);
   10453             : 
   10454     2652102 :   gcc_assert (TEMPLATE_PARM_P (t));
   10455             :   return t;
   10456             : }
   10457             : 
   10458             : struct pair_fn_data
   10459             : {
   10460             :   tree_fn_t fn;
   10461             :   tree_fn_t any_fn;
   10462             :   void *data;
   10463             :   /* True when we should also visit template parameters that occur in
   10464             :      non-deduced contexts.  */
   10465             :   bool include_nondeduced_p;
   10466             :   hash_set<tree> *visited;
   10467             : };
   10468             : 
   10469             : /* Called from for_each_template_parm via walk_tree.  */
   10470             : 
   10471             : static tree
   10472   171848010 : for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
   10473             : {
   10474   171848010 :   tree t = *tp;
   10475   171848010 :   struct pair_fn_data *pfd = (struct pair_fn_data *) d;
   10476   171848010 :   tree_fn_t fn = pfd->fn;
   10477   171848010 :   void *data = pfd->data;
   10478   171848010 :   tree result = NULL_TREE;
   10479             : 
   10480             : #define WALK_SUBTREE(NODE)                                              \
   10481             :   do                                                                    \
   10482             :     {                                                                   \
   10483             :       result = for_each_template_parm (NODE, fn, data, pfd->visited, \
   10484             :                                        pfd->include_nondeduced_p,    \
   10485             :                                        pfd->any_fn);                 \
   10486             :       if (result) goto out;                                             \
   10487             :     }                                                                   \
   10488             :   while (0)
   10489             : 
   10490   171848010 :   if (pfd->any_fn && (*pfd->any_fn)(t, data))
   10491             :     return t;
   10492             : 
   10493   171848010 :   if (TYPE_P (t)
   10494    91092490 :       && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
   10495    90731599 :     WALK_SUBTREE (TYPE_CONTEXT (t));
   10496             : 
   10497   171535478 :   switch (TREE_CODE (t))
   10498             :     {
   10499    14302426 :     case RECORD_TYPE:
   10500    14302426 :       if (TYPE_PTRMEMFUNC_P (t))
   10501             :         break;
   10502             :       /* Fall through.  */
   10503             : 
   10504    14423657 :     case UNION_TYPE:
   10505    14423657 :     case ENUMERAL_TYPE:
   10506    14423657 :       if (!TYPE_TEMPLATE_INFO (t))
   10507     2060932 :         *walk_subtrees = 0;
   10508             :       else
   10509    24725450 :         WALK_SUBTREE (TYPE_TI_ARGS (t));
   10510             :       break;
   10511             : 
   10512     5382678 :     case INTEGER_TYPE:
   10513     5382678 :       WALK_SUBTREE (TYPE_MIN_VALUE (t));
   10514     5382678 :       WALK_SUBTREE (TYPE_MAX_VALUE (t));
   10515             :       break;
   10516             : 
   10517     5865360 :     case METHOD_TYPE:
   10518             :       /* Since we're not going to walk subtrees, we have to do this
   10519             :          explicitly here.  */
   10520     5865360 :       WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
   10521             :       /* Fall through.  */
   10522             : 
   10523     1473505 :     case FUNCTION_TYPE:
   10524             :       /* Check the return type.  */
   10525     1473505 :       WALK_SUBTREE (TREE_TYPE (t));
   10526             : 
   10527             :       /* Check the parameter types.  Since default arguments are not
   10528             :          instantiated until they are needed, the TYPE_ARG_TYPES may
   10529             :          contain expressions that involve template parameters.  But,
   10530             :          no-one should be looking at them yet.  And, once they're
   10531             :          instantiated, they don't contain template parameters, so
   10532             :          there's no point in looking at them then, either.  */
   10533     1298825 :       {
   10534     1298825 :         tree parm;
   10535             : 
   10536     4317872 :         for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
   10537     3033419 :           WALK_SUBTREE (TREE_VALUE (parm));
   10538             : 
   10539             :         /* Since we've already handled the TYPE_ARG_TYPES, we don't
   10540             :            want walk_tree walking into them itself.  */
   10541     1284453 :         *walk_subtrees = 0;
   10542             :       }
   10543             : 
   10544     1284453 :       if (flag_noexcept_type)
   10545             :         {
   10546     1254058 :           tree spec = TYPE_RAISES_EXCEPTIONS (t);
   10547     1254058 :           if (spec)
   10548      398168 :             WALK_SUBTREE (TREE_PURPOSE (spec));
   10549             :         }
   10550             :       break;
   10551             : 
   10552      583115 :     case TYPEOF_TYPE:
   10553      583115 :     case DECLTYPE_TYPE:
   10554      583115 :       if (pfd->include_nondeduced_p
   10555      583115 :           && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
   10556             :                                      pfd->visited,
   10557             :                                      pfd->include_nondeduced_p,
   10558             :                                      pfd->any_fn))
   10559         110 :         return error_mark_node;
   10560      583005 :       *walk_subtrees = false;
   10561      583005 :       break;
   10562             : 
   10563          84 :     case TRAIT_TYPE:
   10564          84 :       if (pfd->include_nondeduced_p)
   10565             :         {
   10566           0 :           WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t));
   10567           0 :           WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t));
   10568             :         }
   10569          84 :       *walk_subtrees = false;
   10570          84 :       break;
   10571             : 
   10572       73274 :     case FUNCTION_DECL:
   10573       73274 :     case VAR_DECL:
   10574       73274 :       if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
   10575       23270 :         WALK_SUBTREE (DECL_TI_ARGS (t));
   10576             :       break;
   10577             : 
   10578      324002 :     case PARM_DECL:
   10579      324002 :       WALK_SUBTREE (TREE_TYPE (t));
   10580             :       break;
   10581             : 
   10582      282162 :     case CONST_DECL:
   10583      282162 :       if (DECL_TEMPLATE_PARM_P (t))
   10584           0 :         WALK_SUBTREE (DECL_INITIAL (t));
   10585      282162 :       if (DECL_CONTEXT (t)
   10586      282162 :           && pfd->include_nondeduced_p)
   10587         392 :         WALK_SUBTREE (DECL_CONTEXT (t));
   10588             :       break;
   10589             : 
   10590       40042 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
   10591             :       /* Record template parameters such as `T' inside `TT<T>'.  */
   10592       40042 :       WALK_SUBTREE (TYPE_TI_ARGS (t));
   10593             :       /* Fall through.  */
   10594             : 
   10595    33992960 :     case TEMPLATE_TEMPLATE_PARM:
   10596    33992960 :     case TEMPLATE_TYPE_PARM:
   10597    33992960 :     case TEMPLATE_PARM_INDEX:
   10598    33992960 :       if (fn && (*fn)(t, data))
   10599             :         return t;
   10600    27983150 :       else if (!fn)
   10601             :         return t;
   10602             :       break;
   10603             : 
   10604     1444144 :     case TEMPLATE_DECL:
   10605             :       /* A template template parameter is encountered.  */
   10606     1444144 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   10607          18 :         WALK_SUBTREE (TREE_TYPE (t));
   10608             : 
   10609             :       /* Already substituted template template parameter */
   10610     1444144 :       *walk_subtrees = 0;
   10611     1444144 :       break;
   10612             : 
   10613     2138594 :     case TYPENAME_TYPE:
   10614             :       /* A template-id in a TYPENAME_TYPE might be a deduced context after
   10615             :          partial instantiation.  */
   10616     2138594 :       WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
   10617     2138594 :       *walk_subtrees = 0;
   10618     2138594 :       break;
   10619             : 
   10620      855734 :     case INDIRECT_REF:
   10621      855734 :     case COMPONENT_REF:
   10622             :       /* If there's no type, then this thing must be some expression
   10623             :          involving template parameters.  */
   10624      855734 :       if (!fn && !TREE_TYPE (t))
   10625           0 :         return error_mark_node;
   10626             :       break;
   10627             : 
   10628      194646 :     case CONSTRUCTOR:
   10629      194646 :     case TRAIT_EXPR:
   10630      194646 :     case PLUS_EXPR:
   10631      194646 :     case MULT_EXPR:
   10632      194646 :     case SCOPE_REF:
   10633             :       /* These are non-deduced contexts.  */
   10634      194646 :       if (!pfd->include_nondeduced_p)
   10635       37302 :         *walk_subtrees = 0;
   10636             :       break;
   10637             : 
   10638     1544430 :     case MODOP_EXPR:
   10639     1544430 :     case CAST_EXPR:
   10640     1544430 :     case IMPLICIT_CONV_EXPR:
   10641     1544430 :     case REINTERPRET_CAST_EXPR:
   10642     1544430 :     case CONST_CAST_EXPR:
   10643     1544430 :     case STATIC_CAST_EXPR:
   10644     1544430 :     case DYNAMIC_CAST_EXPR:
   10645     1544430 :     case ARROW_EXPR:
   10646     1544430 :     case DOTSTAR_EXPR:
   10647     1544430 :     case TYPEID_EXPR:
   10648     1544430 :     case PSEUDO_DTOR_EXPR:
   10649     1544430 :       if (!fn)
   10650           0 :         return error_mark_node;
   10651             :       break;
   10652             : 
   10653             :     default:
   10654             :       break;
   10655             :     }
   10656             : 
   10657             :   #undef WALK_SUBTREE
   10658             : 
   10659             :   /* We didn't find any template parameters we liked.  */
   10660             :  out:
   10661             :   return result;
   10662             : }
   10663             : 
   10664             : /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
   10665             :    BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
   10666             :    call FN with the parameter and the DATA.
   10667             :    If FN returns nonzero, the iteration is terminated, and
   10668             :    for_each_template_parm returns 1.  Otherwise, the iteration
   10669             :    continues.  If FN never returns a nonzero value, the value
   10670             :    returned by for_each_template_parm is 0.  If FN is NULL, it is
   10671             :    considered to be the function which always returns 1.
   10672             : 
   10673             :    If INCLUDE_NONDEDUCED_P, then this routine will also visit template
   10674             :    parameters that occur in non-deduced contexts.  When false, only
   10675             :    visits those template parameters that can be deduced.  */
   10676             : 
   10677             : static tree
   10678   168571857 : for_each_template_parm (tree t, tree_fn_t fn, void* data,
   10679             :                         hash_set<tree> *visited,
   10680             :                         bool include_nondeduced_p,
   10681             :                         tree_fn_t any_fn)
   10682             : {
   10683   168571857 :   struct pair_fn_data pfd;
   10684   168571857 :   tree result;
   10685             : 
   10686             :   /* Set up.  */
   10687   168571857 :   pfd.fn = fn;
   10688   168571857 :   pfd.any_fn = any_fn;
   10689   168571857 :   pfd.data = data;
   10690   168571857 :   pfd.include_nondeduced_p = include_nondeduced_p;
   10691             : 
   10692             :   /* Walk the tree.  (Conceptually, we would like to walk without
   10693             :      duplicates, but for_each_template_parm_r recursively calls
   10694             :      for_each_template_parm, so we would need to reorganize a fair
   10695             :      bit to use walk_tree_without_duplicates, so we keep our own
   10696             :      visited list.)  */
   10697   168571857 :   if (visited)
   10698   146094504 :     pfd.visited = visited;
   10699             :   else
   10700    22477353 :     pfd.visited = new hash_set<tree>;
   10701   168571857 :   result = cp_walk_tree (&t,
   10702             :                          for_each_template_parm_r,
   10703             :                          &pfd,
   10704             :                          pfd.visited);
   10705             : 
   10706             :   /* Clean up.  */
   10707   168571857 :   if (!visited)
   10708             :     {
   10709    44954706 :       delete pfd.visited;
   10710    22477353 :       pfd.visited = 0;
   10711             :     }
   10712             : 
   10713   168571857 :   return result;
   10714             : }
   10715             : 
   10716     1625176 : struct find_template_parameter_info
   10717             : {
   10718     1625176 :   explicit find_template_parameter_info (tree ctx_parms)
   10719     3250352 :     : ctx_parms (ctx_parms),
   10720     1625176 :       max_depth (TMPL_PARMS_DEPTH (ctx_parms))
   10721     1625176 :   {}
   10722             : 
   10723             :   hash_set<tree> visited;
   10724             :   hash_set<tree> parms;
   10725             :   tree parm_list = NULL_TREE;
   10726             :   tree *parm_list_tail = &parm_list;
   10727             :   tree ctx_parms;
   10728             :   int max_depth;
   10729             : 
   10730             :   tree find_in (tree);
   10731             :   tree find_in_recursive (tree);
   10732             :   bool found (tree);
   10733          76 :   unsigned num_found () { return parms.elements (); }
   10734             : };
   10735             : 
   10736             : /* Appends the declaration of T to the list in DATA.  */
   10737             : 
   10738             : static int
   10739     2974133 : keep_template_parm (tree t, void* data)
   10740             : {
   10741     2974133 :   find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
   10742             : 
   10743             :   /* Template parameters declared within the expression are not part of
   10744             :      the parameter mapping. For example, in this concept:
   10745             : 
   10746             :        template<typename T>
   10747             :        concept C = requires { <expr> } -> same_as<int>;
   10748             : 
   10749             :      the return specifier same_as<int> declares a new decltype parameter
   10750             :      that must not be part of the parameter mapping. The same is true
   10751             :      for generic lambda parameters, lambda template parameters, etc.  */
   10752     2974133 :   int level;
   10753     2974133 :   int index;
   10754     2974133 :   template_parm_level_and_index (t, &level, &index);
   10755     2974133 :   if (level == 0 || level > ftpi->max_depth)
   10756             :     return 0;
   10757             : 
   10758     2729748 :   if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
   10759             :     /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
   10760             :        BOUND_TEMPLATE_TEMPLATE_PARM itself.  */
   10761        2100 :     t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
   10762             : 
   10763             :   /* This template parameter might be an argument to a cached dependent
   10764             :      specalization that was formed earlier inside some other template, in
   10765             :      which case the parameter is not among the ones that are in-scope.
   10766             :      Look in CTX_PARMS to find the corresponding in-scope template
   10767             :      parameter, and use it instead.  */
   10768     2729748 :   if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
   10769     2652102 :     t = in_scope;
   10770             : 
   10771             :   /* Arguments like const T yield parameters like const T. This means that
   10772             :      a template-id like X<T, const T> would yield two distinct parameters:
   10773             :      T and const T. Adjust types to their unqualified versions.  */
   10774     2729748 :   if (TYPE_P (t))
   10775     2690230 :     t = TYPE_MAIN_VARIANT (t);
   10776     2729748 :   if (!ftpi->parms.add (t))
   10777             :     {
   10778             :       /* Append T to PARM_LIST.  */
   10779     2445859 :       tree node = build_tree_list (NULL_TREE, t);
   10780     2445859 :       *ftpi->parm_list_tail = node;
   10781     2445859 :       ftpi->parm_list_tail = &TREE_CHAIN (node);
   10782             :     }
   10783             : 
   10784             :   /* Verify the parameter we found has a valid index.  */
   10785     2729748 :   if (flag_checking)
   10786             :     {
   10787     2729748 :       tree parms = ftpi->ctx_parms;
   10788     2866712 :       while (TMPL_PARMS_DEPTH (parms) > level)
   10789      136964 :         parms = TREE_CHAIN (parms);
   10790     2729748 :       if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms)))
   10791     2652102 :         gcc_assert (index < len);
   10792             :     }
   10793             : 
   10794             :   return 0;
   10795             : }
   10796             : 
   10797             : /* Ensure that we recursively examine certain terms that are not normally
   10798             :    visited in for_each_template_parm_r.  */
   10799             : 
   10800             : static int
   10801    23855116 : any_template_parm_r (tree t, void *data)
   10802             : {
   10803    23855116 :   find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
   10804             : 
   10805             : #define WALK_SUBTREE(NODE)                                              \
   10806             :   do                                                                    \
   10807             :     {                                                                   \
   10808             :       for_each_template_parm (NODE, keep_template_parm, data,           \
   10809             :                               &ftpi->visited, true,                      \
   10810             :                               any_template_parm_r);                     \
   10811             :     }                                                                   \
   10812             :   while (0)
   10813             : 
   10814             :   /* A mention of a member alias/typedef is a use of all of its template
   10815             :      arguments, including those from the enclosing class, so we don't use
   10816             :      alias_template_specialization_p here.  */
   10817    23855116 :   if (TYPE_P (t) && typedef_variant_p (t))
   10818      462782 :     if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
   10819      462775 :       WALK_SUBTREE (TI_ARGS (tinfo));
   10820             : 
   10821    23855116 :   switch (TREE_CODE (t))
   10822             :     {
   10823     2930424 :     case TEMPLATE_TYPE_PARM:
   10824             :       /* Type constraints of a placeholder type may contain parameters.  */
   10825     2930424 :       if (is_auto (t))
   10826      322018 :         if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
   10827      321566 :           WALK_SUBTREE (constr);
   10828             :       break;
   10829             : 
   10830     1355364 :     case TEMPLATE_ID_EXPR:
   10831             :       /* Search through references to variable templates.  */
   10832     1355364 :       WALK_SUBTREE (TREE_OPERAND (t, 0));
   10833     1355364 :       WALK_SUBTREE (TREE_OPERAND (t, 1));
   10834     1355364 :       break;
   10835             : 
   10836       39520 :     case TEMPLATE_PARM_INDEX:
   10837       39520 :       WALK_SUBTREE (TREE_TYPE (t));
   10838       39520 :       break;
   10839             : 
   10840     1248873 :     case TEMPLATE_DECL:
   10841             :       /* If T is a member template that shares template parameters with
   10842             :          ctx_parms, we need to mark all those parameters for mapping.
   10843             :          To that end, it should suffice to just walk the DECL_CONTEXT of
   10844             :          the template (assuming the template is not overly general).  */
   10845     1248873 :       WALK_SUBTREE (DECL_CONTEXT (t));
   10846     1248873 :       break;
   10847             : 
   10848          14 :     case LAMBDA_EXPR:
   10849          14 :       {
   10850             :         /* Look in the parms and body.  */
   10851          14 :         tree fn = lambda_function (t);
   10852          14 :         WALK_SUBTREE (TREE_TYPE (fn));
   10853          14 :         WALK_SUBTREE (DECL_SAVED_TREE (fn));
   10854             :       }
   10855          14 :       break;
   10856             : 
   10857      757999 :     case IDENTIFIER_NODE:
   10858      757999 :       if (IDENTIFIER_CONV_OP_P (t))
   10859             :         /* The conversion-type-id of a conversion operator may be dependent.  */
   10860         120 :         WALK_SUBTREE (TREE_TYPE (t));
   10861             :       break;
   10862             : 
   10863           5 :     case CONVERT_EXPR:
   10864           5 :       if (is_dummy_object (t))
   10865           4 :         WALK_SUBTREE (TREE_TYPE (t));
   10866             :       break;
   10867             : 
   10868             :     default:
   10869             :       break;
   10870             :     }
   10871             : 
   10872             :   /* Keep walking.  */
   10873    23855116 :   return 0;
   10874             : }
   10875             : 
   10876             : /* Look through T for template parameters.  */
   10877             : 
   10878             : tree
   10879     1625253 : find_template_parameter_info::find_in (tree t)
   10880             : {
   10881     1625253 :   return for_each_template_parm (t, keep_template_parm, this, &visited,
   10882             :                                  /*include_nondeduced*/true,
   10883     1625253 :                                  any_template_parm_r);
   10884             : }
   10885             : 
   10886             : /* As above, but also recursively look into the default arguments of template
   10887             :    parameters we found.  Used for alias CTAD.  */
   10888             : 
   10889             : tree
   10890          76 : find_template_parameter_info::find_in_recursive (tree t)
   10891             : {
   10892          76 :   if (tree r = find_in (t))
   10893             :     return r;
   10894             :   /* Since newly found parms are added to the end of the list, we
   10895             :      can just walk it until we reach the end.  */
   10896         153 :   for (tree pl = parm_list; pl; pl = TREE_CHAIN (pl))
   10897             :     {
   10898          77 :       tree parm = TREE_VALUE (pl);
   10899          77 :       tree list = corresponding_template_parameter_list (ctx_parms, parm);
   10900          77 :       if (tree r = find_in (TREE_PURPOSE (list)))
   10901           0 :         return r;
   10902             :     }
   10903             :   return NULL_TREE;
   10904             : }
   10905             : 
   10906             : /* True if PARM was found by a previous call to find_in.  PARM can be a
   10907             :    TREE_LIST, a DECL_TEMPLATE_PARM_P, or a TEMPLATE_PARM_P.  */
   10908             : 
   10909             : bool
   10910          88 : find_template_parameter_info::found (tree parm)
   10911             : {
   10912          88 :   if (TREE_CODE (parm) == TREE_LIST)
   10913          88 :     parm = TREE_VALUE (parm);
   10914          88 :   if (TREE_CODE (parm) == TYPE_DECL)
   10915          80 :     parm = TREE_TYPE (parm);
   10916             :   else
   10917           8 :     parm = DECL_INITIAL (parm);
   10918          88 :   gcc_checking_assert (TEMPLATE_PARM_P (parm));
   10919          88 :   return parms.contains (parm);
   10920             : }
   10921             : 
   10922             : /* Returns a list of unique template parameters found within T, where CTX_PARMS
   10923             :    are the template parameters in scope.  */
   10924             : 
   10925             : tree
   10926     1625130 : find_template_parameters (tree t, tree ctx_parms)
   10927             : {
   10928     1625130 :   if (!ctx_parms)
   10929             :     return NULL_TREE;
   10930             : 
   10931     1625100 :   find_template_parameter_info ftpi (ctx_parms);
   10932     1625100 :   ftpi.find_in (t);
   10933     1625100 :   return ftpi.parm_list;
   10934     1625130 : }
   10935             : 
   10936             : /* Returns true if T depends on any template parameter.  */
   10937             : 
   10938             : bool
   10939  1918377074 : uses_template_parms (tree t)
   10940             : {
   10941  1918377074 :   if (t == NULL_TREE || t == error_mark_node)
   10942             :     return false;
   10943             : 
   10944             :   /* Namespaces can't depend on any template parameters.  */
   10945  1682631563 :   if (TREE_CODE (t) == NAMESPACE_DECL)
   10946             :     return false;
   10947             : 
   10948  1682631542 :   processing_template_decl_sentinel ptds (/*reset*/false);
   10949  1682631542 :   ++processing_template_decl;
   10950             : 
   10951  1682631542 :   if (TYPE_P (t))
   10952  1017462252 :     return dependent_type_p (t);
   10953   665169290 :   else if (TREE_CODE (t) == TREE_VEC)
   10954   457083015 :     return any_dependent_template_arguments_p (t);
   10955   208086275 :   else if (TREE_CODE (t) == TREE_LIST)
   10956    83545785 :     return (uses_template_parms (TREE_VALUE (t))
   10957    83545785 :             || uses_template_parms (TREE_CHAIN (t)));
   10958   124540490 :   else if (TREE_CODE (t) == TYPE_DECL)
   10959        7397 :     return dependent_type_p (TREE_TYPE (t));
   10960             :   else
   10961   124533093 :     return instantiation_dependent_expression_p (t);
   10962  1918377074 : }
   10963             : 
   10964             : /* Returns true if T depends on any template parameter with level LEVEL.  */
   10965             : 
   10966             : bool
   10967     1275203 : uses_template_parms_level (tree t, int level)
   10968             : {
   10969     1275203 :   return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
   10970     1275203 :                                  /*include_nondeduced_p=*/true);
   10971             : }
   10972             : 
   10973             : /* Returns true if the signature of DECL depends on any template parameter from
   10974             :    its enclosing class.  */
   10975             : 
   10976             : static bool
   10977   176117553 : uses_outer_template_parms (tree decl)
   10978             : {
   10979   176117553 :   int depth;
   10980   176117553 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
   10981      219090 :     depth = TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)) - 1;
   10982             :   else
   10983   175898463 :     depth = template_class_depth (CP_DECL_CONTEXT (decl));
   10984   176117553 :   if (depth == 0)
   10985             :     return false;
   10986     6088407 :   if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
   10987             :                               &depth, NULL, /*include_nondeduced_p=*/true))
   10988             :     return true;
   10989      476357 :   if (PRIMARY_TEMPLATE_P (decl)
   10990      476357 :       || DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
   10991             :     {
   10992      476357 :       tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl));
   10993     1089923 :       for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
   10994             :         {
   10995      613613 :           tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
   10996      613613 :           tree defarg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
   10997      613613 :           if (TREE_CODE (parm) == PARM_DECL
   10998      613613 :               && for_each_template_parm (TREE_TYPE (parm),
   10999             :                                          template_parm_outer_level,
   11000             :                                          &depth, NULL, /*nondeduced*/true))
   11001             :             return true;
   11002      613572 :           if (TREE_CODE (parm) == TEMPLATE_DECL
   11003      613572 :               && uses_outer_template_parms (parm))
   11004             :             return true;
   11005      613572 :           if (defarg
   11006      613572 :               && for_each_template_parm (defarg, template_parm_outer_level,
   11007             :                                          &depth, NULL, /*nondeduced*/true))
   11008             :             return true;
   11009             :         }
   11010             :     }
   11011      476310 :   if (uses_outer_template_parms_in_constraints (decl))
   11012             :     return true;
   11013             :   return false;
   11014             : }
   11015             : 
   11016             : /* Returns true if the constraints of DECL depend on any template parameters
   11017             :    from its enclosing scope.  */
   11018             : 
   11019             : bool
   11020     2442559 : uses_outer_template_parms_in_constraints (tree decl)
   11021             : {
   11022     2442559 :   tree ci = get_constraints (decl);
   11023     2442559 :   if (ci)
   11024          38 :     ci = CI_ASSOCIATED_CONSTRAINTS (ci);
   11025          38 :   if (!ci)
   11026             :     return false;
   11027          38 :   int depth = template_class_depth (CP_DECL_CONTEXT (decl));
   11028          38 :   if (depth == 0)
   11029             :     return false;
   11030          37 :   return for_each_template_parm (ci, template_parm_outer_level,
   11031          37 :                                  &depth, NULL, /*nondeduced*/true);
   11032             : }
   11033             : 
   11034             : /* Returns TRUE iff INST is an instantiation we don't need to do in an
   11035             :    ill-formed translation unit, i.e. a variable or function that isn't
   11036             :    usable in a constant expression.  */
   11037             : 
   11038             : static inline bool
   11039     1950895 : neglectable_inst_p (tree d)
   11040             : {
   11041     1940626 :   return (d && DECL_P (d)
   11042     1757711 :           && !undeduced_auto_decl (d)
   11043     3707032 :           && !(TREE_CODE (d) == FUNCTION_DECL
   11044     1898235 :                ? FNDECL_MANIFESTLY_CONST_EVALUATED (d)
   11045      142098 :                : decl_maybe_constant_var_p (d)));
   11046             : }
   11047             : 
   11048             : /* Returns TRUE iff we should refuse to instantiate DECL because it's
   11049             :    neglectable and instantiated from within an erroneous instantiation.  */
   11050             : 
   11051             : static bool
   11052    94411334 : limit_bad_template_recursion (tree decl)
   11053             : {
   11054    94411334 :   struct tinst_level *lev = current_tinst_level;
   11055    94411334 :   int errs = errorcount + sorrycount;
   11056    94411334 :   if (errs == 0 || !neglectable_inst_p (decl))
   11057    93388409 :     return false;
   11058             : 
   11059             :   /* Avoid instantiating members of an ill-formed class.  */
   11060     1022925 :   bool refuse
   11061     2045847 :     = (DECL_CLASS_SCOPE_P (decl)
   11062     1754084 :        && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl)));
   11063             : 
   11064             :   if (!refuse)
   11065             :     {
   11066     1049504 :       for (; lev; lev = lev->next)
   11067     1224917 :         if (neglectable_inst_p (lev->maybe_get_node ()))
   11068             :           break;
   11069     1022086 :       refuse = (lev && errs > lev->errors);
   11070             :     }
   11071             : 
   11072             :   if (refuse)
   11073             :     {
   11074             :       /* Don't warn about it not being defined.  */
   11075        1552 :       suppress_warning (decl, OPT_Wunused);
   11076        1552 :       tree clone;
   11077        2624 :       FOR_EACH_CLONE (clone, decl)
   11078        1072 :         suppress_warning (clone, OPT_Wunused);
   11079             :     }
   11080             :   return refuse;
   11081             : }
   11082             : 
   11083             : static int tinst_depth;
   11084             : extern int max_tinst_depth;
   11085             : int depth_reached;
   11086             : 
   11087             : static GTY(()) struct tinst_level *last_error_tinst_level;
   11088             : 
   11089             : /* We're starting to instantiate D; record the template instantiation context
   11090             :    at LOC for diagnostics and to restore it later.  */
   11091             : 
   11092             : bool
   11093   347833818 : push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
   11094             : {
   11095   347833818 :   struct tinst_level *new_level;
   11096             : 
   11097   347833818 :   if (tinst_depth >= max_tinst_depth)
   11098             :     {
   11099             :       /* Tell error.cc not to try to instantiate any templates.  */
   11100          55 :       at_eof = 2;
   11101          55 :       fatal_error (input_location,
   11102             :                    "template instantiation depth exceeds maximum of %d"
   11103             :                    " (use %<-ftemplate-depth=%> to increase the maximum)",
   11104             :                    max_tinst_depth);
   11105             :       return false;
   11106             :     }
   11107             : 
   11108             :   /* If the current instantiation caused problems, don't let it instantiate
   11109             :      anything else.  Do allow deduction substitution and decls usable in
   11110             :      constant expressions.  */
   11111   347833763 :   if (!targs && limit_bad_template_recursion (tldcl))
   11112             :     {
   11113             :       /* Avoid no_linkage_errors and unused function (and all other)
   11114             :          warnings for this decl.  */
   11115        1552 :       suppress_warning (tldcl);
   11116        1552 :       return false;
   11117             :     }
   11118             : 
   11119             :   /* When not -quiet, dump template instantiations other than functions, since
   11120             :      announce_function will take care of those.  */
   11121   347832211 :   if (!quiet_flag && !targs
   11122           0 :       && TREE_CODE (tldcl) != TREE_LIST
   11123           0 :       && TREE_CODE (tldcl) != FUNCTION_DECL)
   11124           0 :     fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
   11125             : 
   11126   347832211 :   new_level = tinst_level_freelist ().alloc ();
   11127   347832211 :   new_level->tldcl = tldcl;
   11128   347832211 :   new_level->targs = targs;
   11129   347832211 :   new_level->locus = loc;
   11130   347832211 :   new_level->errors = errorcount + sorrycount;
   11131   347832211 :   new_level->next = NULL;
   11132   347832211 :   new_level->refcount = 0;
   11133   347832211 :   new_level->path = new_level->visible = nullptr;
   11134   347832211 :   set_refcount_ptr (new_level->next, current_tinst_level);
   11135   347832211 :   set_refcount_ptr (current_tinst_level, new_level);
   11136             : 
   11137   347832211 :   ++tinst_depth;
   11138   347832211 :   if (GATHER_STATISTICS && (tinst_depth > depth_reached))
   11139             :     depth_reached = tinst_depth;
   11140             : 
   11141   347832211 :   return true;
   11142             : }
   11143             : 
   11144             : /* We're starting substitution of TMPL<ARGS>; record the template
   11145             :    substitution context for diagnostics and to restore it later.  */
   11146             : 
   11147             : bool
   11148   253422449 : push_tinst_level (tree tmpl, tree args)
   11149             : {
   11150   159995940 :   return push_tinst_level_loc (tmpl, args, input_location);
   11151             : }
   11152             : 
   11153             : /* We're starting to instantiate D; record INPUT_LOCATION and the
   11154             :    template instantiation context for diagnostics and to restore it
   11155             :    later.  */
   11156             : 
   11157             : bool
   11158    94300824 : push_tinst_level (tree d)
   11159             : {
   11160    51104553 :   return push_tinst_level_loc (d, input_location);
   11161             : }
   11162             : 
   11163             : /* Likewise, but record LOC as the program location.  */
   11164             : 
   11165             : bool
   11166    94411369 : push_tinst_level_loc (tree d, location_t loc)
   11167             : {
   11168    94411369 :   gcc_assert (TREE_CODE (d) != TREE_LIST);
   11169    94411369 :   return push_tinst_level_loc (d, NULL, loc);
   11170             : }
   11171             : 
   11172             : /* We're done instantiating this template; return to the instantiation
   11173             :    context.  */
   11174             : 
   11175             : void
   11176   358748690 : pop_tinst_level (void)
   11177             : {
   11178             :   /* Restore the filename and line number stashed away when we started
   11179             :      this instantiation.  */
   11180   358748690 :   input_location = current_tinst_level->locus;
   11181   358748690 :   set_refcount_ptr (current_tinst_level, current_tinst_level->next);
   11182   358748690 :   --tinst_depth;
   11183   358748690 : }
   11184             : 
   11185             : /* We're instantiating a deferred template; restore the template
   11186             :    instantiation context in which the instantiation was requested, which
   11187             :    is one step out from LEVEL.  Return the corresponding DECL or TYPE.  */
   11188             : 
   11189             : static tree
   11190    10949883 : reopen_tinst_level (struct tinst_level *level)
   11191             : {
   11192    10949883 :   struct tinst_level *t;
   11193             : 
   11194    10949883 :   tinst_depth = 0;
   11195    53142287 :   for (t = level; t; t = t->next)
   11196    42192404 :     ++tinst_depth;
   11197             : 
   11198    10949883 :   set_refcount_ptr (current_tinst_level, level);
   11199    10949883 :   pop_tinst_level ();
   11200    10949883 :   if (current_tinst_level)
   11201     8321078 :     current_tinst_level->errors = errorcount+sorrycount;
   11202    10949883 :   return level->maybe_get_node ();
   11203             : }
   11204             : 
   11205             : /* Returns the TINST_LEVEL which gives the original instantiation
   11206             :    context.  */
   11207             : 
   11208             : struct tinst_level *
   11209         152 : outermost_tinst_level (void)
   11210             : {
   11211         152 :   struct tinst_level *level = current_tinst_level;
   11212         152 :   if (level)
   11213          21 :     while (level->next)
   11214             :       level = level->next;
   11215         152 :   return level;
   11216             : }
   11217             : 
   11218             : /* True iff T is a friend function declaration that is not itself a template
   11219             :    and is not defined in a class template.  */
   11220             : 
   11221             : bool
   11222    81773734 : non_templated_friend_p (tree t)
   11223             : {
   11224    81773734 :   if (t && TREE_CODE (t) == FUNCTION_DECL
   11225   163297872 :       && DECL_UNIQUE_FRIEND_P (t))
   11226             :     {
   11227     1198539 :       tree ti = DECL_TEMPLATE_INFO (t);
   11228     1198539 :       if (!ti)
   11229             :         return true;
   11230             :       /* DECL_FRIEND_CONTEXT is set for a friend defined in class.  */
   11231     2397078 :       if (DECL_FRIEND_CONTEXT (t))
   11232             :         return false;
   11233             :       /* Non-templated friends in a class template are still represented with a
   11234             :          TEMPLATE_DECL; check that its primary template is the befriending
   11235             :          class.  Note that DECL_PRIMARY_TEMPLATE is null for
   11236             :          template <class T> friend A<T>::f(); */
   11237      385963 :       tree tmpl = TI_TEMPLATE (ti);
   11238      385963 :       tree primary = DECL_PRIMARY_TEMPLATE (tmpl);
   11239      385963 :       return (primary && primary != tmpl);
   11240             :     }
   11241             :   else
   11242             :     return false;
   11243             : }
   11244             : 
   11245             : /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL.  ARGS is the
   11246             :    vector of template arguments, as for tsubst.
   11247             : 
   11248             :    Returns an appropriate tsubst'd friend declaration.  */
   11249             : 
   11250             : static tree
   11251     1174808 : tsubst_friend_function (tree decl, tree args)
   11252             : {
   11253     1174808 :   tree new_friend;
   11254             : 
   11255     1174808 :   if (TREE_CODE (decl) == FUNCTION_DECL
   11256      513436 :       && DECL_TEMPLATE_INSTANTIATION (decl)
   11257     1192885 :       && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
   11258             :     /* This was a friend declared with an explicit template
   11259             :        argument list, e.g.:
   11260             : 
   11261             :        friend void f<>(T);
   11262             : 
   11263             :        to indicate that f was a template instantiation, not a new
   11264             :        function declaration.  Now, we have to figure out what
   11265             :        instantiation of what template.  */
   11266             :     {
   11267       18077 :       tree template_id, arglist, fns;
   11268       18077 :       tree new_args;
   11269       18077 :       tree tmpl;
   11270       18077 :       tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
   11271             : 
   11272             :       /* Friend functions are looked up in the containing namespace scope.
   11273             :          We must enter that scope, to avoid finding member functions of the
   11274             :          current class with same name.  */
   11275       18077 :       push_nested_namespace (ns);
   11276       18077 :       fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
   11277             :                          tf_warning_or_error, NULL_TREE);
   11278       18077 :       pop_nested_namespace (ns);
   11279       18077 :       arglist = tsubst (DECL_TI_ARGS (decl), args,
   11280             :                         tf_warning_or_error, NULL_TREE);
   11281       18077 :       template_id = lookup_template_function (fns, arglist);
   11282             : 
   11283       18077 :       new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
   11284       18077 :       tmpl = determine_specialization (template_id, new_friend,
   11285             :                                        &new_args,
   11286             :                                        /*need_member_template=*/0,
   11287       18077 :                                        TREE_VEC_LENGTH (args),
   11288             :                                        tsk_none);
   11289       18077 :       return instantiate_template (tmpl, new_args, tf_error);
   11290             :     }
   11291             : 
   11292     1156731 :   new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
   11293     1156731 :   if (new_friend == error_mark_node)
   11294             :     return error_mark_node;
   11295             : 
   11296             :   /* The NEW_FRIEND will look like an instantiation, to the
   11297             :      compiler, but is not an instantiation from the point of view of
   11298             :      the language.  For example, we might have had:
   11299             : 
   11300             :      template <class T> struct S {
   11301             :        template <class U> friend void f(T, U);
   11302             :      };
   11303             : 
   11304             :      Then, in S<int>, template <class U> void f(int, U) is not an
   11305             :      instantiation of anything.  */
   11306             : 
   11307     1156731 :   DECL_USE_TEMPLATE (new_friend) = 0;
   11308     1156731 :   if (TREE_CODE (new_friend) == TEMPLATE_DECL)
   11309             :     {
   11310      661372 :       DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false;
   11311      661372 :       DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
   11312     1322744 :       DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
   11313      661372 :         = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
   11314             : 
   11315             :       /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
   11316             :          match in decls_match.  */
   11317      661372 :       tree parms = DECL_TEMPLATE_PARMS (new_friend);
   11318      661372 :       tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
   11319      661372 :       treqs = maybe_substitute_reqs_for (treqs, new_friend);
   11320      661372 :       if (treqs != TEMPLATE_PARMS_CONSTRAINTS (parms))
   11321             :         {
   11322        1780 :           TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
   11323             :           /* As well as each TEMPLATE_PARM_CONSTRAINTS.  */
   11324        1780 :           tsubst_each_template_parm_constraints (parms, args,
   11325             :                                                  tf_warning_or_error);
   11326             :         }
   11327             :     }
   11328             : 
   11329             :   /* The mangled name for the NEW_FRIEND is incorrect.  The function
   11330             :      is not a template instantiation and should not be mangled like
   11331             :      one.  Therefore, we forget the mangling here; we'll recompute it
   11332             :      later if we need it.  */
   11333     1156731 :   if (TREE_CODE (new_friend) != TEMPLATE_DECL)
   11334             :     {
   11335      495359 :       SET_DECL_RTL (new_friend, NULL);
   11336      495359 :       SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
   11337             :     }
   11338             : 
   11339     1156731 :   if (DECL_NAMESPACE_SCOPE_P (new_friend))
   11340             :     {
   11341     1156650 :       tree old_decl;
   11342     1156650 :       tree ns;
   11343             : 
   11344             :       /* We must save some information from NEW_FRIEND before calling
   11345             :          duplicate decls since that function will free NEW_FRIEND if
   11346             :          possible.  */
   11347     1156650 :       tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
   11348     1156650 :       tree new_friend_result_template_info = NULL_TREE;
   11349     1156650 :       bool new_friend_is_defn =
   11350             :         (new_friend_template_info
   11351     1156650 :          && (DECL_INITIAL (DECL_TEMPLATE_RESULT
   11352             :                            (template_for_substitution (new_friend)))
   11353     1156650 :              != NULL_TREE));
   11354     1156650 :       tree not_tmpl = new_friend;
   11355             : 
   11356     1156650 :       if (TREE_CODE (new_friend) == TEMPLATE_DECL)
   11357             :         {
   11358             :           /* This declaration is a `primary' template.  */
   11359      661320 :           DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
   11360             : 
   11361      661320 :           not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
   11362      661320 :           new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
   11363             :         }
   11364      495330 :       else if (!constraints_satisfied_p (new_friend))
   11365             :         /* Only define a constrained hidden friend when satisfied.  */
   11366        1119 :         return error_mark_node;
   11367             : 
   11368             :       /* Inside pushdecl_namespace_level, we will push into the
   11369             :          current namespace. However, the friend function should go
   11370             :          into the namespace of the template.  */
   11371     1155531 :       ns = decl_namespace_context (new_friend);
   11372     1155531 :       push_nested_namespace (ns);
   11373     1155531 :       old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
   11374     1155531 :       pop_nested_namespace (ns);
   11375             : 
   11376     1155531 :       if (old_decl == error_mark_node)
   11377             :         return error_mark_node;
   11378             : 
   11379     1155519 :       if (old_decl != new_friend)
   11380             :         {
   11381             :           /* This new friend declaration matched an existing
   11382             :              declaration.  For example, given:
   11383             : 
   11384             :                template <class T> void f(T);
   11385             :                template <class U> class C {
   11386             :                  template <class T> friend void f(T) {}
   11387             :                };
   11388             : 
   11389             :              the friend declaration actually provides the definition
   11390             :              of `f', once C has been instantiated for some type.  So,
   11391             :              old_decl will be the out-of-class template declaration,
   11392             :              while new_friend is the in-class definition.
   11393             : 
   11394             :              But, if `f' was called before this point, the
   11395             :              instantiation of `f' will have DECL_TI_ARGS corresponding
   11396             :              to `T' but not to `U', references to which might appear
   11397             :              in the definition of `f'.  Previously, the most general
   11398             :              template for an instantiation of `f' was the out-of-class
   11399             :              version; now it is the in-class version.  Therefore, we
   11400             :              run through all specialization of `f', adding to their
   11401             :              DECL_TI_ARGS appropriately.  In particular, they need a
   11402             :              new set of outer arguments, corresponding to the
   11403             :              arguments for this class instantiation.
   11404             : 
   11405             :              The same situation can arise with something like this:
   11406             : 
   11407             :                friend void f(int);
   11408             :                template <class T> class C {
   11409             :                  friend void f(T) {}
   11410             :                };
   11411             : 
   11412             :              when `C<int>' is instantiated.  Now, `f(int)' is defined
   11413             :              in the class.  */
   11414             : 
   11415      262584 :           if (!new_friend_is_defn)
   11416             :             /* On the other hand, if the in-class declaration does
   11417             :                *not* provide a definition, then we don't want to alter
   11418             :                existing definitions.  We can just leave everything
   11419             :                alone.  */
   11420             :             ;
   11421             :           else
   11422             :             {
   11423          36 :               tree new_template = TI_TEMPLATE (new_friend_template_info);
   11424          36 :               tree new_args = TI_ARGS (new_friend_template_info);
   11425             : 
   11426             :               /* Overwrite whatever template info was there before, if
   11427             :                  any, with the new template information pertaining to
   11428             :                  the declaration.  */
   11429          36 :               DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
   11430             : 
   11431          36 :               if (TREE_CODE (old_decl) != TEMPLATE_DECL)
   11432             :                 {
   11433             :                   /* We should have called reregister_specialization in
   11434             :                      duplicate_decls.  */
   11435          28 :                   gcc_assert (retrieve_specialization (new_template,
   11436             :                                                        new_args, 0)
   11437             :                               == old_decl);
   11438             : 
   11439             :                   /* Instantiate it if the global has already been used.  */
   11440          28 :                   if (DECL_ODR_USED (old_decl))
   11441           4 :                     instantiate_decl (old_decl, /*defer_ok=*/true,
   11442             :                                       /*expl_inst_class_mem_p=*/false);
   11443             :                 }
   11444             :               else
   11445             :                 {
   11446           8 :                   tree t;
   11447             : 
   11448             :                   /* Indicate that the old function template is a partial
   11449             :                      instantiation.  */
   11450           8 :                   DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
   11451           8 :                     = new_friend_result_template_info;
   11452             : 
   11453           8 :                   gcc_assert (new_template
   11454             :                               == most_general_template (new_template));
   11455           8 :                   gcc_assert (new_template != old_decl);
   11456             : 
   11457             :                   /* Reassign any specializations already in the hash table
   11458             :                      to the new more general template, and add the
   11459             :                      additional template args.  */
   11460           8 :                   for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
   11461          20 :                        t != NULL_TREE;
   11462          12 :                        t = TREE_CHAIN (t))
   11463             :                     {
   11464          12 :                       tree spec = TREE_VALUE (t);
   11465          12 :                       spec_entry elt;
   11466             : 
   11467          12 :                       elt.tmpl = old_decl;
   11468          12 :                       elt.args = DECL_TI_ARGS (spec);
   11469          12 :                       elt.spec = NULL_TREE;
   11470             : 
   11471          12 :                       decl_specializations->remove_elt (&elt);
   11472             : 
   11473          12 :                       DECL_TI_ARGS (spec)
   11474          24 :                         = add_outermost_template_args (new_args,
   11475          12 :                                                        DECL_TI_ARGS (spec));
   11476             : 
   11477          12 :                       register_specialization
   11478          12 :                         (spec, new_template, DECL_TI_ARGS (spec), true, 0);
   11479             : 
   11480             :                     }
   11481           8 :                   DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
   11482             :                 }
   11483             :             }
   11484             : 
   11485             :           /* The information from NEW_FRIEND has been merged into OLD_DECL
   11486             :              by duplicate_decls.  */
   11487             :           new_friend = old_decl;
   11488             :         }
   11489             : 
   11490             :       /* We've just introduced a namespace-scope function in the purview
   11491             :          without necessarily having opened the enclosing namespace, so
   11492             :          make sure the namespace is in the purview now too.  */
   11493     1155519 :       if (modules_p ()
   11494        7312 :           && DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (new_friend))
   11495     1157435 :           && TREE_CODE (DECL_CONTEXT (new_friend)) == NAMESPACE_DECL)
   11496        1868 :         DECL_MODULE_PURVIEW_P (DECL_CONTEXT (new_friend)) = true;
   11497             :     }
   11498             :   else
   11499             :     {
   11500          81 :       tree context = DECL_CONTEXT (new_friend);
   11501          81 :       bool dependent_p;
   11502             : 
   11503             :       /* In the code
   11504             :            template <class T> class C {
   11505             :              template <class U> friend void C1<U>::f (); // case 1
   11506             :              friend void C2<T>::f ();                      // case 2
   11507             :            };
   11508             :          we only need to make sure CONTEXT is a complete type for
   11509             :          case 2.  To distinguish between the two cases, we note that
   11510             :          CONTEXT of case 1 remains dependent type after tsubst while
   11511             :          this isn't true for case 2.  */
   11512          81 :       ++processing_template_decl;
   11513          81 :       dependent_p = dependent_type_p (context);
   11514          81 :       --processing_template_decl;
   11515             : 
   11516          81 :       if (!dependent_p
   11517          81 :           && !complete_type_or_else (context, NULL_TREE))
   11518           0 :         return error_mark_node;
   11519             : 
   11520          81 :       if (COMPLETE_TYPE_P (context))
   11521             :         {
   11522          73 :           tree fn = new_friend;
   11523             :           /* do_friend adds the TEMPLATE_DECL for any member friend
   11524             :              template even if it isn't a member template, i.e.
   11525             :                template <class T> friend A<T>::f();
   11526             :              Look through it in that case.  */
   11527          73 :           if (TREE_CODE (fn) == TEMPLATE_DECL
   11528          73 :               && !PRIMARY_TEMPLATE_P (fn))
   11529          24 :             fn = DECL_TEMPLATE_RESULT (fn);
   11530             :           /* Check to see that the declaration is really present, and,
   11531             :              possibly obtain an improved declaration.  */
   11532          73 :           fn = check_classfn (context, fn, NULL_TREE);
   11533             : 
   11534          73 :           if (fn)
   11535     1174808 :             new_friend = fn;
   11536             :         }
   11537             :     }
   11538             : 
   11539             :   return new_friend;
   11540             : }
   11541             : 
   11542             : /* FRIEND_TMPL is a friend TEMPLATE_DECL.  ARGS is the vector of
   11543             :    template arguments, as for tsubst.
   11544             : 
   11545             :    Returns an appropriate tsubst'd friend type or error_mark_node on
   11546             :    failure.  */
   11547             : 
   11548             : static tree
   11549      393731 : tsubst_friend_class (tree friend_tmpl, tree args)
   11550             : {
   11551      393731 :   tree tmpl;
   11552             : 
   11553      393731 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
   11554             :     {
   11555           3 :       tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
   11556           3 :       return TREE_TYPE (tmpl);
   11557             :     }
   11558             : 
   11559      393728 :   tree context = CP_DECL_CONTEXT (friend_tmpl);
   11560      393728 :   if (TREE_CODE (context) == NAMESPACE_DECL)
   11561      393490 :     push_nested_namespace (context);
   11562             :   else
   11563             :     {
   11564         238 :       context = tsubst (context, args, tf_error, NULL_TREE);
   11565         238 :       push_nested_class (context);
   11566             :     }
   11567             : 
   11568      393728 :   tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
   11569             :                       LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
   11570             : 
   11571      393728 :   if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
   11572             :     {
   11573             :       /* The friend template has already been declared.  Just
   11574             :          check to see that the declarations match, and install any new
   11575             :          default parameters.  We must tsubst the default parameters,
   11576             :          of course.  We only need the innermost template parameters
   11577             :          because that is all that redeclare_class_template will look
   11578             :          at.  */
   11579      787232 :       if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
   11580      787232 :           > TMPL_ARGS_DEPTH (args))
   11581             :         {
   11582      242869 :           tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
   11583             :                                               args, tf_warning_or_error);
   11584      242869 :           tsubst_each_template_parm_constraints (parms, args,
   11585             :                                                  tf_warning_or_error);
   11586      242869 :           location_t saved_input_location = input_location;
   11587      242869 :           input_location = DECL_SOURCE_LOCATION (friend_tmpl);
   11588      242869 :           tree cons = get_constraints (friend_tmpl);
   11589      242869 :           ++processing_template_decl;
   11590      242869 :           cons = tsubst_constraint_info (cons, args, tf_warning_or_error,
   11591      242869 :                                          DECL_FRIEND_CONTEXT (friend_tmpl));
   11592      242869 :           --processing_template_decl;
   11593      242869 :           redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
   11594      242869 :           input_location = saved_input_location;
   11595             :         }
   11596             :     }
   11597             :   else
   11598             :     {
   11599             :       /* The friend template has not already been declared.  In this
   11600             :          case, the instantiation of the template class will cause the
   11601             :          injection of this template into the namespace scope.  */
   11602         112 :       tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
   11603             : 
   11604         112 :       if (tmpl != error_mark_node)
   11605             :         {
   11606             :           /* The new TMPL is not an instantiation of anything, so we
   11607             :              forget its origins.  We don't reset CLASSTYPE_TI_TEMPLATE
   11608             :              for the new type because that is supposed to be the
   11609             :              corresponding template decl, i.e., TMPL.  */
   11610         106 :           DECL_USE_TEMPLATE (tmpl) = 0;
   11611         106 :           DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
   11612         106 :           CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
   11613         106 :           CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
   11614         106 :             = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
   11615             : 
   11616             :           /* Substitute into and set the constraints on the new declaration.  */
   11617         106 :           if (tree ci = get_constraints (friend_tmpl))
   11618             :             {
   11619           2 :               ++processing_template_decl;
   11620           2 :               ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
   11621           2 :                                            DECL_FRIEND_CONTEXT (friend_tmpl));
   11622           2 :               --processing_template_decl;
   11623           2 :               set_constraints (tmpl, ci);
   11624           2 :               tsubst_each_template_parm_constraints (DECL_TEMPLATE_PARMS (tmpl),
   11625             :                                                      args, tf_warning_or_error);
   11626             :             }
   11627             : 
   11628             :           /* Inject this template into the enclosing namspace scope.  */
   11629         106 :           tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
   11630             :         }
   11631             :     }
   11632             : 
   11633      393728 :   if (TREE_CODE (context) == NAMESPACE_DECL)
   11634      393490 :     pop_nested_namespace (context);
   11635             :   else
   11636         238 :     pop_nested_class ();
   11637             : 
   11638      393728 :   return TREE_TYPE (tmpl);
   11639             : }
   11640             : 
   11641             : /* Returns zero if TYPE cannot be completed later due to circularity.
   11642             :    Otherwise returns one.  */
   11643             : 
   11644             : static int
   11645     3832562 : can_complete_type_without_circularity (tree type)
   11646             : {
   11647     3832950 :   if (type == NULL_TREE || type == error_mark_node)
   11648             :     return 0;
   11649     3832950 :   else if (COMPLETE_TYPE_P (type))
   11650             :     return 1;
   11651      329029 :   else if (TREE_CODE (type) == ARRAY_TYPE)
   11652         388 :     return can_complete_type_without_circularity (TREE_TYPE (type));
   11653      328637 :   else if (CLASS_TYPE_P (type)
   11654      657278 :            && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
   11655             :     return 0;
   11656             :   else
   11657      328631 :     return 1;
   11658             : }
   11659             : 
   11660             : static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
   11661             :                                 tsubst_flags_t, tree);
   11662             : 
   11663             : /* Instantiate the contract statement.  */
   11664             : 
   11665             : static tree
   11666         240 : tsubst_contract (tree decl, tree t, tree args, tsubst_flags_t complain,
   11667             :                  tree in_decl)
   11668             : {
   11669         240 :   tree type = decl ? TREE_TYPE (TREE_TYPE (decl)) : NULL_TREE;
   11670         240 :   bool auto_p  = type_uses_auto (type);
   11671             : 
   11672         240 :   tree r = copy_node (t);
   11673             : 
   11674             :   /* Rebuild the result variable.  */
   11675         240 :   if (type && POSTCONDITION_P (t) && POSTCONDITION_IDENTIFIER (t))
   11676             :     {
   11677          27 :       tree oldvar = POSTCONDITION_IDENTIFIER (t);
   11678             : 
   11679          27 :       tree newvar = copy_node (oldvar);
   11680          27 :       TREE_TYPE (newvar) = type;
   11681          27 :       DECL_CONTEXT (newvar) = decl;
   11682          27 :       POSTCONDITION_IDENTIFIER (r) = newvar;
   11683             : 
   11684             :       /* Make sure the postcondition is valid.  */
   11685          27 :       location_t loc = DECL_SOURCE_LOCATION (oldvar);
   11686          27 :       if (!auto_p)
   11687          25 :         if (!check_postcondition_result (decl, type, loc))
   11688           0 :           return invalidate_contract (r);
   11689             : 
   11690             :       /* Make the variable available for lookup.  */
   11691          27 :       register_local_specialization (newvar, oldvar);
   11692             :     }
   11693             : 
   11694             :   /* Instantiate the condition.  If the return type is undeduced, process
   11695             :      the expression as if inside a template to avoid spurious type errors.  */
   11696         240 :   if (auto_p)
   11697           2 :     ++processing_template_decl;
   11698         240 :   ++processing_contract_condition;
   11699         240 :   CONTRACT_CONDITION (r)
   11700         240 :       = tsubst_expr (CONTRACT_CONDITION (t), args, complain, in_decl);
   11701         240 :   --processing_contract_condition;
   11702         240 :   if (auto_p)
   11703           2 :     --processing_template_decl;
   11704             : 
   11705             :   /* And the comment.  */
   11706         240 :   CONTRACT_COMMENT (r)
   11707         240 :       = tsubst_expr (CONTRACT_COMMENT (r), args, complain, in_decl);
   11708             : 
   11709         240 :   return r;
   11710             : }
   11711             : 
   11712             : /* Update T by instantiating its contract attribute.  */
   11713             : 
   11714             : static void
   11715         219 : tsubst_contract_attribute (tree decl, tree t, tree args,
   11716             :                            tsubst_flags_t complain, tree in_decl)
   11717             : {
   11718             :   /* For non-specializations, adjust the current declaration to the most general
   11719             :      version of in_decl. Because we defer the instantiation of contracts as long
   11720             :      as possible, they are still written in terms of the parameters (and return
   11721             :      type) of the most general template.  */
   11722         219 :   tree tmpl = DECL_TI_TEMPLATE (in_decl);
   11723         219 :   if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
   11724         213 :     in_decl = DECL_TEMPLATE_RESULT (most_general_template (in_decl));
   11725         219 :   local_specialization_stack specs (lss_copy);
   11726         219 :   register_parameter_specializations (in_decl, decl);
   11727             : 
   11728             :   /* Get the contract to be instantiated.  */
   11729         219 :   tree contract = CONTRACT_STATEMENT (t);
   11730             : 
   11731             :   /* Use the complete set of template arguments for instantiation. The
   11732             :      contract may not have been instantiated and still refer to outer levels
   11733             :      of template parameters.  */
   11734         219 :   args = DECL_TI_ARGS (decl);
   11735             : 
   11736             :   /* For member functions, make this available for semantic analysis.  */
   11737         219 :   tree save_ccp = current_class_ptr;
   11738         219 :   tree save_ccr = current_class_ref;
   11739         219 :   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
   11740             :     {
   11741         108 :       tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
   11742         108 :       tree this_type = TREE_TYPE (TREE_VALUE (arg_types));
   11743         108 :       inject_this_parameter (this_type, cp_type_quals (this_type));
   11744             :     }
   11745             : 
   11746         219 :   contract = tsubst_contract (decl, contract, args, complain, in_decl);
   11747             : 
   11748         219 :   current_class_ptr = save_ccp;
   11749         219 :   current_class_ref = save_ccr;
   11750             : 
   11751             :   /* Rebuild the attribute.  */
   11752         219 :   TREE_VALUE (t) = build_tree_list (NULL_TREE, contract);
   11753         219 : }
   11754             : 
   11755             : /* Rebuild the attribute list for DECL, substituting into contracts
   11756             :    as needed.  */
   11757             : 
   11758             : void
   11759         146 : tsubst_contract_attributes (tree decl, tree args, tsubst_flags_t complain, tree in_decl)
   11760             : {
   11761         146 :   tree list = copy_list (DECL_ATTRIBUTES (decl));
   11762         367 :   for (tree attr = list; attr; attr = CONTRACT_CHAIN (attr))
   11763             :     {
   11764         221 :       if (cxx_contract_attribute_p (attr))
   11765         219 :         tsubst_contract_attribute (decl, attr, args, complain, in_decl);
   11766             :     }
   11767         146 :   DECL_ATTRIBUTES (decl) = list;
   11768         146 : }
   11769             : 
   11770             : /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
   11771             :    T or a new TREE_LIST, possibly a chain in the case of a pack expansion.  */
   11772             : 
   11773             : static tree
   11774     1022705 : tsubst_attribute (tree t, tree *decl_p, tree args,
   11775             :                   tsubst_flags_t complain, tree in_decl)
   11776             : {
   11777     1022705 :   gcc_assert (ATTR_IS_DEPENDENT (t));
   11778             : 
   11779             :   /* Note that contract attributes are never substituted from this function.
   11780             :      Their instantiation is triggered by regenerate_from_template_decl when
   11781             :      we instantiate the body of the function.  */
   11782             : 
   11783     1022705 :   tree val = TREE_VALUE (t);
   11784     1022705 :   if (val == NULL_TREE)
   11785             :     /* Nothing to do.  */;
   11786      136636 :   else if ((flag_openmp || flag_openmp_simd)
   11787      136848 :            && is_attribute_p ("omp declare simd",
   11788         212 :                               get_attribute_name (t)))
   11789             :     {
   11790         140 :       tree clauses = TREE_VALUE (val);
   11791         140 :       clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
   11792             :                                     complain, in_decl);
   11793         140 :       c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
   11794         140 :       clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
   11795         140 :       tree parms = DECL_ARGUMENTS (*decl_p);
   11796         140 :       clauses
   11797         140 :         = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
   11798         140 :       if (clauses)
   11799         140 :         val = build_tree_list (NULL_TREE, clauses);
   11800             :       else
   11801             :         val = NULL_TREE;
   11802             :     }
   11803      136496 :   else if (flag_openmp
   11804      136568 :            && is_attribute_p ("omp declare variant base",
   11805          72 :                               get_attribute_name (t)))
   11806             :     {
   11807          72 :       ++cp_unevaluated_operand;
   11808          72 :       tree varid = tsubst_expr (TREE_PURPOSE (val), args, complain, in_decl);
   11809          72 :       --cp_unevaluated_operand;
   11810          72 :       tree chain = TREE_CHAIN (val);
   11811          72 :       location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
   11812          72 :       tree ctx = copy_list (TREE_VALUE (val));
   11813          72 :       tree simd = get_identifier ("simd");
   11814          72 :       tree score = get_identifier (" score");
   11815          72 :       tree condition = get_identifier ("condition");
   11816         140 :       for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
   11817             :         {
   11818          80 :           const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
   11819          80 :           TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
   11820         148 :           for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
   11821             :             {
   11822          80 :               if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
   11823             :                 {
   11824           0 :                   tree clauses = TREE_VALUE (t2);
   11825           0 :                   clauses = tsubst_omp_clauses (clauses,
   11826             :                                                 C_ORT_OMP_DECLARE_SIMD, args,
   11827             :                                                 complain, in_decl);
   11828           0 :                   c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
   11829           0 :                   clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
   11830           0 :                   TREE_VALUE (t2) = clauses;
   11831             :                 }
   11832             :               else
   11833             :                 {
   11834          80 :                   TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
   11835         196 :                   for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
   11836         128 :                     if (TREE_VALUE (t3))
   11837             :                       {
   11838         124 :                         bool allow_string
   11839         120 :                           = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
   11840         124 :                              && TREE_PURPOSE (t3) != score);
   11841         124 :                         tree v = TREE_VALUE (t3);
   11842         124 :                         if (TREE_CODE (v) == STRING_CST && allow_string)
   11843           4 :                           continue;
   11844         120 :                         v = tsubst_expr (v, args, complain, in_decl);
   11845         120 :                         v = fold_non_dependent_expr (v);
   11846         240 :                         if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
   11847         232 :                             || (TREE_PURPOSE (t3) == score
   11848          52 :                                 ? TREE_CODE (v) != INTEGER_CST
   11849          60 :                                 : !tree_fits_shwi_p (v)))
   11850             :                           {
   11851           8 :                             location_t loc
   11852           8 :                               = cp_expr_loc_or_loc (TREE_VALUE (t3),
   11853             :                                                     match_loc);
   11854           8 :                             if (TREE_PURPOSE (t3) == score)
   11855           4 :                               error_at (loc, "score argument must be "
   11856             :                                              "constant integer expression");
   11857           4 :                             else if (allow_string)
   11858           0 :                               error_at (loc, "property must be constant "
   11859             :                                              "integer expression or string "
   11860             :                                              "literal");
   11861             :                             else
   11862           4 :                               error_at (loc, "property must be constant "
   11863             :                                              "integer expression");
   11864           8 :                             return NULL_TREE;
   11865             :                           }
   11866         112 :                         else if (TREE_PURPOSE (t3) == score
   11867         112 :                                  && tree_int_cst_sgn (v) < 0)
   11868             :                           {
   11869           4 :                             location_t loc
   11870           4 :                               = cp_expr_loc_or_loc (TREE_VALUE (t3),
   11871             :                                                     match_loc);
   11872           4 :                             error_at (loc, "score argument must be "
   11873             :                                            "non-negative");
   11874           4 :                             return NULL_TREE;
   11875             :                           }
   11876         108 :                         TREE_VALUE (t3) = v;
   11877             :                       }
   11878             :                 }
   11879             :             }
   11880             :         }
   11881          60 :       val = tree_cons (varid, ctx, chain);
   11882             :     }
   11883             :   /* If the first attribute argument is an identifier, don't
   11884             :      pass it through tsubst.  Attributes like mode, format,
   11885             :      cleanup and several target specific attributes expect it
   11886             :      unmodified.  */
   11887      136424 :   else if (attribute_takes_identifier_p (get_attribute_name (t)))
   11888             :     {
   11889          78 :       tree chain
   11890          78 :         = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl);
   11891          78 :       if (chain != TREE_CHAIN (val))
   11892           0 :         val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
   11893             :     }
   11894      136346 :   else if (PACK_EXPANSION_P (val))
   11895             :     {
   11896             :       /* An attribute pack expansion.  */
   11897          15 :       tree purp = TREE_PURPOSE (t);
   11898          15 :       tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
   11899          15 :       if (pack == error_mark_node)
   11900             :         return error_mark_node;
   11901          12 :       int len = TREE_VEC_LENGTH (pack);
   11902          12 :       tree list = NULL_TREE;
   11903          12 :       tree *q = &list;
   11904          24 :       for (int i = 0; i < len; ++i)
   11905             :         {
   11906          12 :           tree elt = TREE_VEC_ELT (pack, i);
   11907          12 :           *q = build_tree_list (purp, elt);
   11908          12 :           q = &TREE_CHAIN (*q);
   11909             :         }
   11910          12 :       return list;
   11911             :     }
   11912             :   else
   11913      136331 :     val = tsubst_expr (val, args, complain, in_decl);
   11914             : 
   11915     1022678 :   if (val == error_mark_node)
   11916             :     return error_mark_node;
   11917     1022668 :   if (val != TREE_VALUE (t))
   11918      135880 :     return build_tree_list (TREE_PURPOSE (t), val);
   11919             :   return t;
   11920             : }
   11921             : 
   11922             : /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
   11923             :    unchanged or a new TREE_LIST chain.  */
   11924             : 
   11925             : static tree
   11926      231273 : tsubst_attributes (tree attributes, tree args,
   11927             :                    tsubst_flags_t complain, tree in_decl)
   11928             : {
   11929      231273 :   tree last_dep = NULL_TREE;
   11930             : 
   11931      231273 :   for (tree t = attributes; t; t = TREE_CHAIN (t))
   11932          16 :     if (ATTR_IS_DEPENDENT (t))
   11933             :       {
   11934          16 :         last_dep = t;
   11935          16 :         attributes = copy_list (attributes);
   11936          16 :         break;
   11937             :       }
   11938             : 
   11939      231273 :   if (last_dep)
   11940          32 :     for (tree *p = &attributes; *p; )
   11941             :       {
   11942          16 :         tree t = *p;
   11943          16 :         if (ATTR_IS_DEPENDENT (t))
   11944             :           {
   11945          16 :             tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
   11946          16 :             if (subst != t)
   11947             :               {
   11948           6 :                 *p = subst;
   11949           9 :                 while (*p)
   11950           3 :                   p = &TREE_CHAIN (*p);
   11951           6 :                 *p = TREE_CHAIN (t);
   11952           6 :                 continue;
   11953             :               }
   11954             :           }
   11955          10 :         p = &TREE_CHAIN (*p);
   11956             :       }
   11957             : 
   11958      231273 :   return attributes;
   11959             : }
   11960             : 
   11961             : /* Apply any attributes which had to be deferred until instantiation
   11962             :    time.  DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
   11963             :    ARGS, COMPLAIN, IN_DECL are as tsubst.  Returns true normally,
   11964             :    false on error.  */
   11965             : 
   11966             : static bool
   11967   354965649 : apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
   11968             :                                 tree args, tsubst_flags_t complain, tree in_decl)
   11969             : {
   11970   354965649 :   tree t;
   11971   354965649 :   tree *p;
   11972             : 
   11973   354965649 :   if (attributes == NULL_TREE)
   11974             :     return true;
   11975             : 
   11976    15001068 :   if (DECL_P (*decl_p))
   11977             :     {
   11978    14977801 :       if (TREE_TYPE (*decl_p) == error_mark_node)
   11979             :         return false;
   11980    14977797 :       p = &DECL_ATTRIBUTES (*decl_p);
   11981             :       /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
   11982             :          to our attributes parameter.  */
   11983    14977797 :       gcc_assert (*p == attributes);
   11984             :     }
   11985             :   else
   11986             :     {
   11987       23267 :       p = &TYPE_ATTRIBUTES (*decl_p);
   11988             :       /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
   11989             :          lookup_template_class_1, and should be preserved.  */
   11990       23267 :       gcc_assert (*p != attributes);
   11991       23631 :       while (*p)
   11992         364 :         p = &TREE_CHAIN (*p);
   11993             :     }
   11994             : 
   11995             :   /* save_template_attributes puts the dependent attributes at the beginning of
   11996             :      the list; find the non-dependent ones.  */
   11997    16023753 :   for (t = attributes; t; t = TREE_CHAIN (t))
   11998    15291634 :     if (!ATTR_IS_DEPENDENT (t))
   11999             :       break;
   12000    15001064 :   tree nondep = t;
   12001             : 
   12002             :   /* Apply any non-dependent attributes.  */
   12003    15001064 :   *p = nondep;
   12004             : 
   12005    15001064 :   if (nondep == attributes)
   12006             :     return true;
   12007             : 
   12008             :   /* And then any dependent ones.  */
   12009     1022588 :   tree late_attrs = NULL_TREE;
   12010     1022588 :   tree *q = &late_attrs;
   12011     2045264 :   for (t = attributes; t != nondep; t = TREE_CHAIN (t))
   12012             :     {
   12013     1022689 :       *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
   12014     1022689 :       if (*q == error_mark_node)
   12015             :         return false;
   12016     1022676 :       if (*q == t)
   12017             :         {
   12018      886778 :           *q = copy_node (t);
   12019      886778 :           TREE_CHAIN (*q) = NULL_TREE;
   12020             :         }
   12021     2045343 :       while (*q)
   12022     1022667 :         q = &TREE_CHAIN (*q);
   12023             :     }
   12024             : 
   12025             :   /* cplus_decl_attributes can add some attributes implicitly.  For templates,
   12026             :      those attributes should have been added already when those templates were
   12027             :      parsed, and shouldn't be added based on from which context they are
   12028             :      first time instantiated.  */
   12029     1022575 :   auto o1 = make_temp_override (current_optimize_pragma, NULL_TREE);
   12030     1022575 :   auto o2 = make_temp_override (optimization_current_node,
   12031     1022575 :                                 optimization_default_node);
   12032     1022575 :   auto o3 = make_temp_override (current_target_pragma, NULL_TREE);
   12033     1022575 :   auto o4 = make_temp_override (scope_chain->omp_declare_target_attribute,
   12034     1022575 :                                 NULL);
   12035     1022575 :   auto o5 = make_temp_override (scope_chain->omp_begin_assumes, NULL);
   12036             : 
   12037     1022575 :   cplus_decl_attributes (decl_p, late_attrs, attr_flags);
   12038             : 
   12039     1022575 :   return true;
   12040     1022575 : }
   12041             : 
   12042             : /* The template TMPL is being instantiated with the template arguments TARGS.
   12043             :    Perform the access checks that we deferred when parsing the template.  */
   12044             : 
   12045             : static void
   12046    29436522 : perform_instantiation_time_access_checks (tree tmpl, tree targs)
   12047             : {
   12048    29436522 :   unsigned i;
   12049    29436522 :   deferred_access_check *chk;
   12050             : 
   12051    29436522 :   if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
   12052    29436522 :     return;
   12053             : 
   12054    58873044 :   if (vec<deferred_access_check, va_gc> *access_checks
   12055    29436522 :       = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
   12056         156 :     FOR_EACH_VEC_ELT (*access_checks, i, chk)
   12057             :       {
   12058          84 :         tree decl = chk->decl;
   12059          84 :         tree diag_decl = chk->diag_decl;
   12060          84 :         tree type_scope = TREE_TYPE (chk->binfo);
   12061             : 
   12062          84 :         if (uses_template_parms (type_scope))
   12063          16 :           type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
   12064             : 
   12065             :         /* Make access check error messages point to the location
   12066             :            of the use of the typedef.  */
   12067          84 :         iloc_sentinel ils (chk->loc);
   12068          84 :         perform_or_defer_access_check (TYPE_BINFO (type_scope),
   12069             :                                        decl, diag_decl, tf_warning_or_error);
   12070          84 :       }
   12071             : }
   12072             : 
   12073             : tree
   12074   495795221 : instantiate_class_template (tree type)
   12075             : {
   12076   495795221 :   auto_timevar tv (TV_TEMPLATE_INST);
   12077             : 
   12078   495795221 :   tree templ, args, pattern, t, member;
   12079   495795221 :   tree typedecl;
   12080   495795221 :   tree pbinfo;
   12081   495795221 :   tree base_list;
   12082   495795221 :   unsigned int saved_maximum_field_alignment;
   12083   495795221 :   tree fn_context;
   12084             : 
   12085   495795221 :   if (type == error_mark_node)
   12086             :     return error_mark_node;
   12087             : 
   12088   989798111 :   if (COMPLETE_OR_OPEN_TYPE_P (type)
   12089   520164754 :       || (uses_template_parms (type)
   12090     5059704 :           && !TYPE_FUNCTION_SCOPE_P (type)))
   12091   476485377 :     return type;
   12092             : 
   12093             :   /* Figure out which template is being instantiated.  */
   12094    19309844 :   templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
   12095    19309844 :   gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
   12096             : 
   12097             :   /* Mark the type as in the process of being defined.  */
   12098    19309844 :   TYPE_BEING_DEFINED (type) = 1;
   12099             : 
   12100             :   /* We may be in the middle of deferred access check.  Disable
   12101             :      it now.  */
   12102    19309844 :   deferring_access_check_sentinel acs (dk_no_deferred);
   12103             : 
   12104             :   /* Determine what specialization of the original template to
   12105             :      instantiate.  */
   12106    19309844 :   t = most_specialized_partial_spec (type, tf_warning_or_error);
   12107    19309844 :   if (t == error_mark_node)
   12108             :     return error_mark_node;
   12109    19309827 :   else if (t)
   12110             :     {
   12111             :       /* This TYPE is actually an instantiation of a partial
   12112             :          specialization.  We replace the innermost set of ARGS with
   12113             :          the arguments appropriate for substitution.  For example,
   12114             :          given:
   12115             : 
   12116             :            template <class T> struct S {};
   12117             :            template <class T> struct S<T*> {};
   12118             : 
   12119             :          and supposing that we are instantiating S<int*>, ARGS will
   12120             :          presently be {int*} -- but we need {int}.  */
   12121     3286127 :       pattern = TREE_TYPE (TI_TEMPLATE (t));
   12122     3286127 :       args = TI_ARGS (t);
   12123             :     }
   12124             :   else
   12125             :     {
   12126    16023700 :       pattern = TREE_TYPE (templ);
   12127    16023700 :       args = CLASSTYPE_TI_ARGS (type);
   12128             :     }
   12129             : 
   12130             :   /* If the template we're instantiating is incomplete, then clearly
   12131             :      there's nothing we can do.  */
   12132    19309827 :   if (!COMPLETE_TYPE_P (pattern))
   12133             :     {
   12134             :       /* We can try again later.  */
   12135       74161 :       TYPE_BEING_DEFINED (type) = 0;
   12136       74161 :       return type;
   12137             :     }
   12138             : 
   12139             :   /* If we've recursively instantiated too many templates, stop.  */
   12140    19235666 :   if (! push_tinst_level (type))
   12141           0 :     return type;
   12142             : 
   12143    19235647 :   int saved_unevaluated_operand = cp_unevaluated_operand;
   12144    19235647 :   int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
   12145             : 
   12146    19235647 :   fn_context = decl_function_context (TYPE_MAIN_DECL (type));
   12147             :   /* Also avoid push_to_top_level for a lambda in an NSDMI.  */
   12148    38089209 :   if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
   12149           0 :     fn_context = error_mark_node;
   12150    19235647 :   if (!fn_context)
   12151    18913883 :     push_to_top_level ();
   12152             :   else
   12153             :     {
   12154      321764 :       cp_unevaluated_operand = 0;
   12155      321764 :       c_inhibit_evaluation_warnings = 0;
   12156             :     }
   12157             : 
   12158    19235647 :   mark_template_arguments_used (templ, CLASSTYPE_TI_ARGS (type));
   12159             : 
   12160             :   /* Use #pragma pack from the template context.  */
   12161    19235647 :   saved_maximum_field_alignment = maximum_field_alignment;
   12162    19235647 :   maximum_field_alignment = TYPE_PRECISION (pattern);
   12163             : 
   12164    19235647 :   SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
   12165             : 
   12166             :   /* Set the input location to the most specialized template definition.
   12167             :      This is needed if tsubsting causes an error.  */
   12168    19235647 :   typedecl = TYPE_MAIN_DECL (pattern);
   12169    19235647 :   input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
   12170    19235647 :     DECL_SOURCE_LOCATION (typedecl);
   12171             : 
   12172    19235647 :   set_instantiating_module (TYPE_NAME (type));
   12173             : 
   12174    19235647 :   TYPE_PACKED (type) = TYPE_PACKED (pattern);
   12175    19235647 :   SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
   12176    19235647 :   TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
   12177    19235647 :   CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
   12178    19235647 :   if (ANON_AGGR_TYPE_P (pattern))
   12179       36551 :     SET_ANON_AGGR_TYPE_P (type);
   12180    19235647 :   if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
   12181             :     {
   12182    19089056 :       CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
   12183    19089056 :       CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
   12184             :       /* Adjust visibility for template arguments.  */
   12185    19089056 :       determine_visibility (TYPE_MAIN_DECL (type));
   12186             :     }
   12187    19235647 :   if (CLASS_TYPE_P (type))
   12188    19235647 :     CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
   12189             : 
   12190    19235647 :   pbinfo = TYPE_BINFO (pattern);
   12191             : 
   12192             :   /* We should never instantiate a nested class before its enclosing
   12193             :      class; we need to look up the nested class by name before we can
   12194             :      instantiate it, and that lookup should instantiate the enclosing
   12195             :      class.  */
   12196    19235647 :   gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
   12197             :               || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
   12198             : 
   12199    19235647 :   base_list = NULL_TREE;
   12200             :   /* Defer access checking while we substitute into the types named in
   12201             :      the base-clause.  */
   12202    19235647 :   push_deferring_access_checks (dk_deferred);
   12203    19235647 :   if (BINFO_N_BASE_BINFOS (pbinfo))
   12204             :     {
   12205             :       tree pbase_binfo;
   12206             :       int i;
   12207             : 
   12208             :       /* Substitute into each of the bases to determine the actual
   12209             :          basetypes.  */
   12210    25222666 :       for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
   12211             :         {
   12212    12819969 :           tree base;
   12213    12819969 :           tree access = BINFO_BASE_ACCESS (pbinfo, i);
   12214    12819969 :           tree expanded_bases = NULL_TREE;
   12215    12819969 :           int idx, len = 1;
   12216             : 
   12217    12819969 :           if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
   12218             :             {
   12219         244 :               expanded_bases = 
   12220         244 :                 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
   12221             :                                        args, tf_error, NULL_TREE);
   12222         244 :               if (expanded_bases == error_mark_node)
   12223           0 :                 continue;
   12224             : 
   12225         244 :               len = TREE_VEC_LENGTH (expanded_bases);
   12226             :             }
   12227             : 
   12228    25640249 :           for (idx = 0; idx < len; idx++)
   12229             :             {
   12230    12820280 :               if (expanded_bases)
   12231             :                 /* Extract the already-expanded base class.  */
   12232         555 :                 base = TREE_VEC_ELT (expanded_bases, idx);
   12233             :               else
   12234             :                 /* Substitute to figure out the base class.  */
   12235    12819725 :                 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, 
   12236             :                                NULL_TREE);
   12237             : 
   12238    12820280 :               if (base == error_mark_node)
   12239          24 :                 continue;
   12240             : 
   12241    12820256 :               base_list = tree_cons (access, base, base_list);
   12242    12820256 :               if (BINFO_VIRTUAL_P (pbase_binfo))
   12243       32174 :                 TREE_TYPE (base_list) = integer_type_node;
   12244             :             }
   12245             :         }
   12246             : 
   12247             :       /* The list is now in reverse order; correct that.  */
   12248    12402697 :       base_list = nreverse (base_list);
   12249             :     }
   12250             :   /* Now call xref_basetypes to set up all the base-class
   12251             :      information.  */
   12252    19235647 :   xref_basetypes (type, base_list);
   12253             : 
   12254    19235647 :   apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
   12255             :                                   (int) ATTR_FLAG_TYPE_IN_PLACE,
   12256             :                                   args, tf_error, NULL_TREE);
   12257    19235647 :   fixup_attribute_variants (type);
   12258             : 
   12259             :   /* Now that our base classes are set up, enter the scope of the
   12260             :      class, so that name lookups into base classes, etc. will work
   12261             :      correctly.  This is precisely analogous to what we do in
   12262             :      begin_class_definition when defining an ordinary non-template
   12263             :      class, except we also need to push the enclosing classes.  */
   12264    19235647 :   push_nested_class (type);
   12265             : 
   12266             :   /* Now check accessibility of the types named in its base-clause,
   12267             :      relative to the scope of the class.  */
   12268    19235647 :   pop_to_parent_deferring_access_checks ();
   12269             : 
   12270             :   /* A vector to hold members marked with attribute used. */
   12271    19235647 :   auto_vec<tree> used;
   12272             : 
   12273             :   /* Now members are processed in the order of declaration.  */
   12274    19235647 :   for (member = CLASSTYPE_DECL_LIST (pattern);
   12275   135209533 :        member; member = TREE_CHAIN (member))
   12276             :     {
   12277   115977554 :       tree t = TREE_VALUE (member);
   12278             : 
   12279   115977554 :       if (TREE_PURPOSE (member))
   12280             :         {
   12281   114139260 :           if (TYPE_P (t))
   12282             :             {
   12283     2187003 :               if (LAMBDA_TYPE_P (t))
   12284             :                 /* A closure type for a lambda in an NSDMI or default argument.
   12285             :                    Ignore it; it will be regenerated when needed.  */
   12286          24 :                 continue;
   12287             : 
   12288             :               /* If the member is a class template, we've
   12289             :                  already substituted its type.  */
   12290     1007543 :               if (CLASS_TYPE_P (t)
   12291     2246116 :                   && CLASSTYPE_IS_TEMPLATE (t))
   12292      500430 :                 continue;
   12293             : 
   12294      738143 :               tree newtag = tsubst (t, args, tf_error, NULL_TREE);
   12295      738143 :               if (newtag == error_mark_node)
   12296           0 :                 continue;
   12297             : 
   12298      738143 :               if (TREE_CODE (newtag) != ENUMERAL_TYPE)
   12299             :                 {
   12300      507113 :                   tree name = TYPE_IDENTIFIER (t);
   12301             : 
   12302             :                   /* Now, install the tag.  We don't use pushtag
   12303             :                      because that does too much work -- creating an
   12304             :                      implicit typedef, which we've already done.  */
   12305      507113 :                   set_identifier_type_value (name, TYPE_NAME (newtag));
   12306      507113 :                   maybe_add_class_template_decl_list (type, newtag, false);
   12307      507113 :                   TREE_PUBLIC (TYPE_NAME (newtag)) = true;
   12308      507113 :                   determine_visibility (TYPE_NAME (newtag));
   12309             :                 }
   12310             :             }
   12311   112900663 :           else if (DECL_DECLARES_FUNCTION_P (t))
   12312             :             {
   12313    60163260 :               tree r;
   12314             : 
   12315    60163260 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   12316    11269033 :                 ++processing_template_decl;
   12317    60163260 :               r = tsubst (t, args, tf_error, NULL_TREE);
   12318    60163260 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   12319    11269033 :                 --processing_template_decl;
   12320             : 
   12321    60163260 :               set_current_access_from_decl (r);
   12322    60163260 :               finish_member_declaration (r);
   12323             :               /* Instantiate members marked with attribute used.  */
   12324    60163260 :               if (r != error_mark_node && DECL_PRESERVE_P (r))
   12325          26 :                 used.safe_push (r);
   12326    60163260 :               if (TREE_CODE (r) == FUNCTION_DECL
   12327    60163260 :                   && DECL_OMP_DECLARE_REDUCTION_P (r))
   12328         168 :                 cp_check_omp_declare_reduction (r);
   12329             :             }
   12330    52737403 :           else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
   12331    54924490 :                    && LAMBDA_TYPE_P (TREE_TYPE (t)))
   12332             :             /* A closure type for a lambda in an NSDMI or default argument.
   12333             :                Ignore it; it will be regenerated when needed.  */;
   12334             :           else
   12335             :             {
   12336             :               /* Build new TYPE_FIELDS.  */
   12337    52737367 :               if (TREE_CODE (t) == STATIC_ASSERT)
   12338     2454326 :                 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE);
   12339    50283041 :               else if (TREE_CODE (t) != CONST_DECL)
   12340             :                 {
   12341    50283041 :                   tree r;
   12342    50283041 :                   tree vec = NULL_TREE;
   12343    50283041 :                   int len = 1;
   12344             : 
   12345    50283041 :                   gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
   12346             :                   /* The file and line for this declaration, to
   12347             :                      assist in error message reporting.  Since we
   12348             :                      called push_tinst_level above, we don't need to
   12349             :                      restore these.  */
   12350    50283041 :                   input_location = DECL_SOURCE_LOCATION (t);
   12351             : 
   12352    50283041 :                   if (TREE_CODE (t) == TEMPLATE_DECL)
   12353     2077157 :                     ++processing_template_decl;
   12354    50283041 :                   r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
   12355    50282973 :                   if (TREE_CODE (t) == TEMPLATE_DECL)
   12356     2077157 :                     --processing_template_decl;
   12357             : 
   12358    50282973 :                   if (TREE_CODE (r) == TREE_VEC)
   12359             :                     {
   12360             :                       /* A capture pack became multiple fields.  */
   12361         198 :                       vec = r;
   12362         198 :                       len = TREE_VEC_LENGTH (vec);
   12363             :                     }
   12364             : 
   12365   100562637 :                   for (int i = 0; i < len; ++i)
   12366             :                     {
   12367    50283264 :                       if (vec)
   12368         489 :                         r = TREE_VEC_ELT (vec, i);
   12369    50283264 :                       if (VAR_P (r))
   12370             :                         {
   12371             :                           /* In [temp.inst]:
   12372             : 
   12373             :                              [t]he initialization (and any associated
   12374             :                              side-effects) of a static data member does
   12375             :                              not occur unless the static data member is
   12376             :                              itself used in a way that requires the
   12377             :                              definition of the static data member to
   12378             :                              exist.
   12379             : 
   12380             :                              Therefore, we do not substitute into the
   12381             :                              initialized for the static data member here.  */
   12382     1545918 :                           finish_static_data_member_decl
   12383     1545918 :                             (r,
   12384             :                              /*init=*/NULL_TREE,
   12385             :                              /*init_const_expr_p=*/false,
   12386             :                              /*asmspec_tree=*/NULL_TREE,
   12387             :                              /*flags=*/0);
   12388             :                           /* Instantiate members marked with attribute used. */
   12389     1545918 :                           if (r != error_mark_node && DECL_PRESERVE_P (r))
   12390           8 :                             used.safe_push (r);
   12391             :                         }
   12392    48737346 :                       else if (TREE_CODE (r) == FIELD_DECL)
   12393             :                         {
   12394             :                           /* Determine whether R has a valid type and can be
   12395             :                              completed later.  If R is invalid, then its type
   12396             :                              is replaced by error_mark_node.  */
   12397     3832562 :                           tree rtype = TREE_TYPE (r);
   12398     3832562 :                           if (can_complete_type_without_circularity (rtype))
   12399     3832552 :                             complete_type (rtype);
   12400             : 
   12401     3828962 :                           if (!complete_or_array_type_p (rtype))
   12402             :                             {
   12403             :                               /* If R's type couldn't be completed and
   12404             :                                  it isn't a flexible array member (whose
   12405             :                                  type is incomplete by definition) give
   12406             :                                  an error.  */
   12407          21 :                               cxx_incomplete_type_error (r, rtype);
   12408          21 :                               TREE_TYPE (r) = error_mark_node;
   12409             :                             }
   12410     3828941 :                           else if (TREE_CODE (rtype) == ARRAY_TYPE
   12411      106127 :                                    && TYPE_DOMAIN (rtype) == NULL_TREE
   12412     3828941 :                                    && (TREE_CODE (type) == UNION_TYPE
   12413          47 :                                        || TREE_CODE (type) == QUAL_UNION_TYPE))
   12414             :                             {
   12415           5 :                               error ("flexible array member %qD in union", r);
   12416           5 :                               TREE_TYPE (r) = error_mark_node;
   12417             :                             }
   12418     3828936 :                           else if (!verify_type_context (input_location,
   12419             :                                                          TCTX_FIELD, rtype))
   12420           0 :                             TREE_TYPE (r) = error_mark_node;
   12421             :                         }
   12422             : 
   12423             :                       /* If it is a TYPE_DECL for a class-scoped
   12424             :                          ENUMERAL_TYPE, such a thing will already have
   12425             :                          been added to the field list by tsubst_enum
   12426             :                          in finish_member_declaration case above.  */
   12427    50279664 :                       if (!(TREE_CODE (r) == TYPE_DECL
   12428    41932786 :                             && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
   12429      346860 :                             && DECL_ARTIFICIAL (r)))
   12430             :                         {
   12431    50048634 :                           set_current_access_from_decl (r);
   12432    50048634 :                           finish_member_declaration (r);
   12433             :                         }
   12434             :                     }
   12435             :                 }
   12436             :             }
   12437             :         }
   12438             :       else
   12439             :         {
   12440     1568539 :           if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
   12441     3013105 :               || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   12442             :             {
   12443             :               /* Build new CLASSTYPE_FRIEND_CLASSES.  */
   12444             : 
   12445      663486 :               tree friend_type = t;
   12446      663486 :               if (TREE_CODE (friend_type) == TEMPLATE_DECL)
   12447             :                 {
   12448             :                   /* template <class T> friend class C;  */
   12449      393731 :                   friend_type = tsubst_friend_class (friend_type, args);
   12450             :                 }
   12451      269755 :               else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
   12452             :                 {
   12453             :                   /* template <class T> friend class C::D;  */
   12454          12 :                   friend_type = tsubst (friend_type, args,
   12455             :                                         tf_warning_or_error, NULL_TREE);
   12456          12 :                   if (TREE_CODE (friend_type) == TEMPLATE_DECL)
   12457          12 :                     friend_type = TREE_TYPE (friend_type);
   12458             :                 }
   12459      269743 :               else if (TREE_CODE (friend_type) == TYPENAME_TYPE
   12460      269743 :                        || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
   12461             :                 {
   12462             :                   /* This could be either
   12463             : 
   12464             :                        friend class T::C;
   12465             : 
   12466             :                      when dependent_type_p is false or
   12467             : 
   12468             :                        template <class U> friend class T::C;
   12469             : 
   12470             :                      otherwise.  */
   12471             :                   /* Bump processing_template_decl in case this is something like
   12472             :                      template <class T> friend struct A<T>::B.  */
   12473        1990 :                   ++processing_template_decl;
   12474        1990 :                   friend_type = tsubst (friend_type, args,
   12475             :                                         tf_warning_or_error, NULL_TREE);
   12476        1990 :                   --processing_template_decl;
   12477             :                 }
   12478      267753 :               else if (uses_template_parms (friend_type))
   12479             :                 /* friend class C<T>;  */
   12480      252482 :                 friend_type = tsubst (friend_type, args,
   12481             :                                       tf_warning_or_error, NULL_TREE);
   12482             :               
   12483             :               /* Otherwise it's
   12484             : 
   12485             :                    friend class C;
   12486             : 
   12487             :                  where C is already declared or
   12488             : 
   12489             :                    friend class C<int>;
   12490             : 
   12491             :                  We don't have to do anything in these cases.  */
   12492             : 
   12493      663486 :               if (friend_type != error_mark_node)
   12494      663471 :                 make_friend_class (type, friend_type, /*complain=*/false);
   12495             :             }
   12496             :           else
   12497             :             {
   12498             :               /* Build new DECL_FRIENDLIST.  */
   12499     1174808 :               tree r;
   12500             : 
   12501             :               /* The file and line for this declaration, to
   12502             :                  assist in error message reporting.  Since we
   12503             :                  called push_tinst_level above, we don't need to
   12504             :                  restore these.  */
   12505     1174808 :               input_location = DECL_SOURCE_LOCATION (t);
   12506             : 
   12507     1174808 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   12508             :                 {
   12509      661372 :                   ++processing_template_decl;
   12510      661372 :                   push_deferring_access_checks (dk_no_check);
   12511             :                 }
   12512             : 
   12513     1174808 :               r = tsubst_friend_function (t, args);
   12514     1174808 :               add_friend (type, r, /*complain=*/false);
   12515     1174808 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   12516             :                 {
   12517      661372 :                   pop_deferring_access_checks ();
   12518      661372 :                   --processing_template_decl;
   12519             :                 }
   12520             :             }
   12521             :         }
   12522             :     }
   12523             : 
   12524    19231979 :   if (fn_context)
   12525             :     {
   12526             :       /* Restore these before substituting into the lambda capture
   12527             :          initializers.  */
   12528      321764 :       cp_unevaluated_operand = saved_unevaluated_operand;
   12529      321764 :       c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
   12530             :     }
   12531             : 
   12532             :   /* Set the file and line number information to whatever is given for
   12533             :      the class itself.  This puts error messages involving generated
   12534             :      implicit functions at a predictable point, and the same point
   12535             :      that would be used for non-template classes.  */
   12536    19231979 :   input_location = DECL_SOURCE_LOCATION (typedecl);
   12537             : 
   12538    19231979 :   unreverse_member_declarations (type);
   12539    19231979 :   finish_struct_1 (type);
   12540    19231979 :   TYPE_BEING_DEFINED (type) = 0;
   12541             : 
   12542             :   /* Remember if instantiating this class ran into errors, so we can avoid
   12543             :      instantiating member functions in limit_bad_template_recursion.  We set
   12544             :      this flag even if the problem was in another instantiation triggered by
   12545             :      this one, as that will likely also cause trouble for member functions.  */
   12546    19231979 :   if (errorcount + sorrycount > current_tinst_level->errors)
   12547         974 :     CLASSTYPE_ERRONEOUS (type) = true;
   12548             : 
   12549             :   /* We don't instantiate default arguments for member functions.  14.7.1:
   12550             : 
   12551             :      The implicit instantiation of a class template specialization causes
   12552             :      the implicit instantiation of the declarations, but not of the
   12553             :      definitions or default arguments, of the class member functions,
   12554             :      member classes, static data members and member templates....  */
   12555             : 
   12556    19231979 :   perform_instantiation_time_access_checks (pattern, args);
   12557    19231979 :   perform_deferred_access_checks (tf_warning_or_error);
   12558             : 
   12559             :   /* Now that we've gone through all the members, instantiate those
   12560             :      marked with attribute used.  We must do this in the context of
   12561             :      the class -- not the context we pushed from, as that might be
   12562             :      inside a template and change the behaviour of mark_used.  */
   12563    19232073 :   for (tree x : used)
   12564          34 :     mark_used (x);
   12565             : 
   12566    19231979 :   pop_nested_class ();
   12567    19231979 :   maximum_field_alignment = saved_maximum_field_alignment;
   12568    19231979 :   if (!fn_context)
   12569    18910215 :     pop_from_top_level ();
   12570    19231979 :   pop_tinst_level ();
   12571             : 
   12572             :   /* The vtable for a template class can be emitted in any translation
   12573             :      unit in which the class is instantiated.  When there is no key
   12574             :      method, however, finish_struct_1 will already have added TYPE to
   12575             :      the keyed_classes.  */
   12576    19231979 :   if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
   12577           0 :     vec_safe_push (keyed_classes, type);
   12578             : 
   12579    19231979 :   return type;
   12580   515097691 : }
   12581             : 
   12582             : tree
   12583  1875423852 : tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   12584             : {
   12585  1875423852 :   tree r;
   12586             : 
   12587  1875423852 :   if (!t)
   12588             :     r = t;
   12589  1856049022 :   else if (TYPE_P (t))
   12590  1685390093 :     r = tsubst (t, args, complain, in_decl);
   12591             :   else
   12592             :     {
   12593   170658929 :       if (!(complain & tf_warning))
   12594   120262373 :         ++c_inhibit_evaluation_warnings;
   12595   170658929 :       r = tsubst_expr (t, args, complain, in_decl);
   12596   170658891 :       if (!(complain & tf_warning))
   12597   120262343 :         --c_inhibit_evaluation_warnings;
   12598             :     }
   12599             : 
   12600  1875423814 :   return r;
   12601             : }
   12602             : 
   12603             : /* Given a function parameter pack TMPL_PARM and some function parameters
   12604             :    instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
   12605             :    and set *SPEC_P to point at the next point in the list.  */
   12606             : 
   12607             : tree
   12608      384165 : extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
   12609             : {
   12610             :   /* Collect all of the extra "packed" parameters into an
   12611             :      argument pack.  */
   12612      384165 :   tree argpack;
   12613      384165 :   tree spec_parm = *spec_p;
   12614      384165 :   int len;
   12615             : 
   12616      614863 :   for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
   12617      230833 :     if (tmpl_parm
   12618      230833 :         && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
   12619             :       break;
   12620             : 
   12621      384165 :   spec_parm = *spec_p;
   12622      384165 :   if (len == 1 && DECL_PACK_P (spec_parm))
   12623             :     {
   12624             :       /* The instantiation is still a parameter pack; don't wrap it in a
   12625             :          NONTYPE_ARGUMENT_PACK.  */
   12626         637 :       argpack = spec_parm;
   12627         637 :       spec_parm = DECL_CHAIN (spec_parm);
   12628             :     }
   12629             :   else
   12630             :     {
   12631             :       /* Fill in PARMVEC with all of the parameters.  */
   12632      383528 :       tree parmvec = make_tree_vec (len);
   12633      383528 :       argpack = make_node (NONTYPE_ARGUMENT_PACK);
   12634      613589 :       for (int i = 0; i < len; i++)
   12635             :         {
   12636      230061 :           tree elt = spec_parm;
   12637      230061 :           if (DECL_PACK_P (elt))
   12638           0 :             elt = make_pack_expansion (elt);
   12639      230061 :           TREE_VEC_ELT (parmvec, i) = elt;
   12640      230061 :           spec_parm = DECL_CHAIN (spec_parm);
   12641             :         }
   12642             : 
   12643             :       /* Build the argument packs.  */
   12644      383528 :       ARGUMENT_PACK_ARGS (argpack) = parmvec;
   12645             :     }
   12646      384165 :   *spec_p = spec_parm;
   12647             : 
   12648      384165 :   return argpack;
   12649             : }
   12650             : 
   12651             : /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
   12652             :    NONTYPE_ARGUMENT_PACK.  */
   12653             : 
   12654             : static tree
   12655        2465 : make_fnparm_pack (tree spec_parm)
   12656             : {
   12657           0 :   return extract_fnparm_pack (NULL_TREE, &spec_parm);
   12658             : }
   12659             : 
   12660             : /* Return 1 if the Ith element of the argument pack ARG_PACK is a
   12661             :    pack expansion with no extra args, 2 if it has extra args, or 0
   12662             :    if it is not a pack expansion.  */
   12663             : 
   12664             : static int
   12665    17690215 : argument_pack_element_is_expansion_p (tree arg_pack, int i)
   12666             : {
   12667    17690215 :   if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
   12668             :     /* We're being called before this happens in tsubst_pack_expansion.  */
   12669           0 :     arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
   12670    17690215 :   tree vec = ARGUMENT_PACK_ARGS (arg_pack);
   12671    17690215 :   if (i >= TREE_VEC_LENGTH (vec))
   12672             :     return 0;
   12673    17690215 :   tree elt = TREE_VEC_ELT (vec, i);
   12674    17690215 :   if (DECL_P (elt))
   12675             :     /* A decl pack is itself an expansion.  */
   12676      468219 :     elt = TREE_TYPE (elt);
   12677    17690215 :   if (!PACK_EXPANSION_P (elt))
   12678             :     return 0;
   12679      266241 :   if (PACK_EXPANSION_EXTRA_ARGS (elt))
   12680           3 :     return 2;
   12681             :   return 1;
   12682             : }
   12683             : 
   12684             : 
   12685             : /* Creates and return an ARGUMENT_PACK_SELECT tree node.  */
   12686             : 
   12687             : static tree
   12688     3494404 : make_argument_pack_select (tree arg_pack, unsigned index)
   12689             : {
   12690     3494404 :   tree aps = make_node (ARGUMENT_PACK_SELECT);
   12691             : 
   12692     3494404 :   ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
   12693     3494404 :   ARGUMENT_PACK_SELECT_INDEX (aps) = index;
   12694             : 
   12695     3494404 :   return aps;
   12696             : }
   12697             : 
   12698             : /*  This is a subroutine of tsubst_pack_expansion.
   12699             : 
   12700             :     It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
   12701             :     mechanism to store the (non complete list of) arguments of the
   12702             :     substitution and return a non substituted pack expansion, in order
   12703             :     to wait for when we have enough arguments to really perform the
   12704             :     substitution.  */
   12705             : 
   12706             : static bool
   12707    12849211 : use_pack_expansion_extra_args_p (tree t,
   12708             :                                  tree parm_packs,
   12709             :                                  int arg_pack_len,
   12710             :                                  bool has_empty_arg)
   12711             : {
   12712    12849211 :   if (has_empty_arg
   12713    12849211 :       && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t))
   12714             :     return true;
   12715             : 
   12716             :   /* If one pack has an expansion and another pack has a normal
   12717             :      argument or if one pack has an empty argument and an another
   12718             :      one hasn't then tsubst_pack_expansion cannot perform the
   12719             :      substitution and need to fall back on the
   12720             :      PACK_EXPANSION_EXTRA mechanism.  */
   12721    12849208 :   if (parm_packs == NULL_TREE)
   12722             :     return false;
   12723     4137007 :   else if (has_empty_arg)
   12724             :     {
   12725             :       /* If all the actual packs are pack expansions, we can still
   12726             :          subsitute directly.  */
   12727       58821 :       for (tree p = parm_packs; p; p = TREE_CHAIN (p))
   12728             :         {
   12729       58821 :           tree a = TREE_VALUE (p);
   12730       58821 :           if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
   12731           0 :             a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
   12732       58821 :           a = ARGUMENT_PACK_ARGS (a);
   12733       58821 :           if (TREE_VEC_LENGTH (a) == 1)
   12734        1552 :             a = TREE_VEC_ELT (a, 0);
   12735       58821 :           if (PACK_EXPANSION_P (a))
   12736           0 :             continue;
   12737             :           return true;
   12738             :         }
   12739             :       return false;
   12740             :     }
   12741             : 
   12742    13383173 :   for (int i = 0 ; i < arg_pack_len; ++i)
   12743             :     {
   12744             :       bool has_expansion_arg = false;
   12745             :       bool has_non_expansion_arg = false;
   12746     9537682 :       for (tree parm_pack = parm_packs;
   12747    18842672 :            parm_pack;
   12748     9537682 :            parm_pack = TREE_CHAIN (parm_pack))
   12749             :         {
   12750     9537685 :           tree arg = TREE_VALUE (parm_pack);
   12751             : 
   12752     9537685 :           int exp = argument_pack_element_is_expansion_p (arg, i);
   12753     9537685 :           if (exp == 2)
   12754             :             /* We can't substitute a pack expansion with extra args into
   12755             :                our pattern.  */
   12756             :             return true;
   12757     9537682 :           else if (exp)
   12758             :             has_expansion_arg = true;
   12759             :           else
   12760     9404563 :             has_non_expansion_arg = true;
   12761             :         }
   12762             : 
   12763     9304987 :       if (has_expansion_arg && has_non_expansion_arg)
   12764             :         {
   12765           0 :           gcc_checking_assert (false);
   12766             :           return true;
   12767             :         }
   12768             :     }
   12769             :   return false;
   12770             : }
   12771             : 
   12772             : /* [temp.variadic]/6 says that:
   12773             : 
   12774             :        The instantiation of a pack expansion [...]
   12775             :        produces a list E1,E2, ..., En, where N is the number of elements
   12776             :        in the pack expansion parameters.
   12777             : 
   12778             :    This subroutine of tsubst_pack_expansion produces one of these Ei.
   12779             : 
   12780             :    PATTERN is the pattern of the pack expansion.  PARM_PACKS is a
   12781             :    TREE_LIST in which each TREE_PURPOSE is a parameter pack of
   12782             :    PATTERN, and each TREE_VALUE is its corresponding argument pack.
   12783             :    INDEX is the index 'i' of the element Ei to produce.  ARGS,
   12784             :    COMPLAIN, and IN_DECL are the same parameters as for the
   12785             :    tsubst_pack_expansion function.
   12786             : 
   12787             :    The function returns the resulting Ei upon successful completion,
   12788             :    or error_mark_node.
   12789             : 
   12790             :    Note that this function possibly modifies the ARGS parameter, so
   12791             :    it's the responsibility of the caller to restore it.  */
   12792             : 
   12793             : static tree
   12794     7919835 : gen_elem_of_pack_expansion_instantiation (tree pattern,
   12795             :                                           tree parm_packs,
   12796             :                                           unsigned index,
   12797             :                                           tree args /* This parm gets
   12798             :                                                        modified.  */,
   12799             :                                           tsubst_flags_t complain,
   12800             :                                           tree in_decl)
   12801             : {
   12802     7919835 :   tree t;
   12803     7919835 :   bool ith_elem_is_expansion = false;
   12804             : 
   12805             :   /* For each parameter pack, change the substitution of the parameter
   12806             :      pack to the ith argument in its argument pack, then expand the
   12807             :      pattern.  */
   12808    16072365 :   for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
   12809             :     {
   12810     8152530 :       tree parm = TREE_PURPOSE (pack);
   12811     8152530 :       tree arg_pack = TREE_VALUE (pack);
   12812     8152530 :       tree aps;                 /* instance of ARGUMENT_PACK_SELECT.  */
   12813             : 
   12814    16305060 :       ith_elem_is_expansion |=
   12815     8152530 :         argument_pack_element_is_expansion_p (arg_pack, index);
   12816             : 
   12817             :       /* Select the Ith argument from the pack.  */
   12818     8152530 :       if (TREE_CODE (parm) == PARM_DECL
   12819     8152530 :           || VAR_P (parm)
   12820     7918524 :           || TREE_CODE (parm) == FIELD_DECL)
   12821             :         {
   12822      234006 :           if (index == 0)
   12823             :             {
   12824      182007 :               aps = make_argument_pack_select (arg_pack, index);
   12825      182007 :               if (!mark_used (parm, complain) && !(complain & tf_error))
   12826           0 :                 return error_mark_node;
   12827      182007 :               register_local_specialization (aps, parm);
   12828             :             }
   12829             :           else
   12830       51999 :             aps = retrieve_local_specialization (parm);
   12831             :         }
   12832             :       else
   12833             :         {
   12834     7918524 :           int idx, level;
   12835     7918524 :           template_parm_level_and_index (parm, &level, &idx);
   12836             : 
   12837     7918524 :           if (index == 0)
   12838             :             {
   12839     3312397 :               aps = make_argument_pack_select (arg_pack, index);
   12840             :               /* Update the corresponding argument.  */
   12841     6624794 :               TMPL_ARG (args, level, idx) = aps;
   12842             :             }
   12843             :           else
   12844             :             /* Re-use the ARGUMENT_PACK_SELECT.  */
   12845     9212254 :             aps = TMPL_ARG (args, level, idx);
   12846             :         }
   12847     8152530 :       ARGUMENT_PACK_SELECT_INDEX (aps) = index;
   12848             :     }
   12849             : 
   12850             :   /* Substitute into the PATTERN with the (possibly altered)
   12851             :      arguments.  */
   12852     7919835 :   if (pattern == in_decl)
   12853             :     /* Expanding a fixed parameter pack from
   12854             :        coerce_template_parameter_pack.  */
   12855          95 :     t = tsubst_decl (pattern, args, complain);
   12856     7919740 :   else if (pattern == error_mark_node)
   12857             :     t = error_mark_node;
   12858     7919737 :   else if (!TYPE_P (pattern))
   12859      488492 :     t = tsubst_expr (pattern, args, complain, in_decl);
   12860             :   else
   12861             :     {
   12862     7431245 :       t = tsubst (pattern, args, complain, in_decl);
   12863     7431245 :       if (is_auto (t) && !ith_elem_is_expansion)
   12864             :         /* When expanding the fake auto... pack expansion from add_capture, we
   12865             :            need to mark that the expansion is no longer a pack.  */
   12866          12 :         TEMPLATE_TYPE_PARAMETER_PACK (t) = false;
   12867             :     }
   12868             : 
   12869             :   /*  If the Ith argument pack element is a pack expansion, then
   12870             :       the Ith element resulting from the substituting is going to
   12871             :       be a pack expansion as well.  */
   12872     7919835 :   if (ith_elem_is_expansion)
   12873      115599 :     t = make_pack_expansion (t, complain);
   12874             : 
   12875             :   return t;
   12876             : }
   12877             : 
   12878             : /* When the unexpanded parameter pack in a fold expression expands to an empty
   12879             :    sequence, the value of the expression is as follows; the program is
   12880             :    ill-formed if the operator is not listed in this table.
   12881             : 
   12882             :    &&   true
   12883             :    ||   false
   12884             :    ,    void()  */
   12885             : 
   12886             : tree
   12887       28063 : expand_empty_fold (tree t, tsubst_flags_t complain)
   12888             : {
   12889       28063 :   tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
   12890       28063 :   if (!FOLD_EXPR_MODIFY_P (t))
   12891       28041 :     switch (code)
   12892             :       {
   12893       27995 :       case TRUTH_ANDIF_EXPR:
   12894       27995 :         return boolean_true_node;
   12895           6 :       case TRUTH_ORIF_EXPR:
   12896           6 :         return boolean_false_node;
   12897           4 :       case COMPOUND_EXPR:
   12898           4 :         return void_node;
   12899             :       default:
   12900             :         break;
   12901             :       }
   12902             : 
   12903          58 :   if (complain & tf_error)
   12904          58 :     error_at (location_of (t),
   12905             :               "fold of empty expansion over %O", code);
   12906          58 :   return error_mark_node;
   12907             : }
   12908             : 
   12909             : /* Given a fold-expression T and a current LEFT and RIGHT operand,
   12910             :    form an expression that combines the two terms using the
   12911             :    operator of T. */
   12912             : 
   12913             : static tree
   12914       79453 : fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
   12915             : {
   12916       79453 :   tree_code code = FOLD_EXPR_OP (t);
   12917             : 
   12918       79453 :   tree lookups = templated_operator_saved_lookups (t);
   12919             : 
   12920             :   // Handle compound assignment operators.
   12921       79453 :   if (FOLD_EXPR_MODIFY_P (t))
   12922         344 :     return build_x_modify_expr (input_location, left, code, right,
   12923             :                                 lookups, complain);
   12924             : 
   12925       79109 :   warning_sentinel s(warn_parentheses);
   12926       79109 :   switch (code)
   12927             :     {
   12928         756 :     case COMPOUND_EXPR:
   12929         756 :       return build_x_compound_expr (input_location, left, right,
   12930         756 :                                     lookups, complain);
   12931       78353 :     default:
   12932       78353 :       return build_x_binary_op (input_location, code,
   12933       78353 :                                 left, TREE_CODE (left),
   12934       78353 :                                 right, TREE_CODE (right),
   12935             :                                 lookups, /*overload=*/NULL,
   12936             :                                 complain);
   12937             :     }
   12938       79453 : }
   12939             : 
   12940             : /* Substitute ARGS into the pack of a fold expression T. */
   12941             : 
   12942             : static inline tree
   12943       82682 : tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   12944             : {
   12945       82682 :   return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
   12946             : }
   12947             : 
   12948             : /* Substitute ARGS into the pack of a fold expression T. */
   12949             : 
   12950             : static inline tree
   12951         913 : tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   12952             : {
   12953         913 :   return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl);
   12954             : }
   12955             : 
   12956             : /* Expand a PACK of arguments into a grouped as left fold.
   12957             :    Given a pack containing elements A0, A1, ..., An and an
   12958             :    operator @, this builds the expression:
   12959             : 
   12960             :       ((A0 @ A1) @ A2) ... @ An
   12961             : 
   12962             :    Note that PACK must not be empty.
   12963             : 
   12964             :    The operator is defined by the original fold expression T. */
   12965             : 
   12966             : static tree
   12967        1650 : expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
   12968             : {
   12969        1650 :   tree left = TREE_VEC_ELT (pack, 0);
   12970        6739 :   for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
   12971             :     {
   12972        5089 :       tree right = TREE_VEC_ELT (pack, i);
   12973        5089 :       left = fold_expression (t, left, right, complain);
   12974             :     }
   12975        1650 :   return left;
   12976             : }
   12977             : 
   12978             : /* Substitute into a unary left fold expression. */
   12979             : 
   12980             : static tree
   12981        1283 : tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
   12982             :                         tree in_decl)
   12983             : {
   12984        1283 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   12985        1283 :   if (pack == error_mark_node)
   12986             :     return error_mark_node;
   12987        1283 :   if (PACK_EXPANSION_P (pack))
   12988             :     {
   12989         104 :       tree r = copy_node (t);
   12990         104 :       FOLD_EXPR_PACK (r) = pack;
   12991         104 :       return r;
   12992             :     }
   12993        1179 :   if (TREE_VEC_LENGTH (pack) == 0)
   12994          75 :     return expand_empty_fold (t, complain);
   12995             :   else
   12996        1104 :     return expand_left_fold (t, pack, complain);
   12997             : }
   12998             : 
   12999             : /* Substitute into a binary left fold expression.
   13000             : 
   13001             :    Do ths by building a single (non-empty) vector of argumnts and
   13002             :    building the expression from those elements. */
   13003             : 
   13004             : static tree
   13005         608 : tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
   13006             :                          tree in_decl)
   13007             : {
   13008         608 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13009         608 :   if (pack == error_mark_node)
   13010             :     return error_mark_node;
   13011         608 :   tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
   13012         608 :   if (init == error_mark_node)
   13013             :     return error_mark_node;
   13014             : 
   13015         608 :   if (PACK_EXPANSION_P (pack))
   13016             :     {
   13017          62 :       tree r = copy_node (t);
   13018          62 :       FOLD_EXPR_PACK (r) = pack;
   13019          62 :       FOLD_EXPR_INIT (r) = init;
   13020          62 :       return r;
   13021             :     }
   13022             : 
   13023         546 :   tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
   13024         546 :   TREE_VEC_ELT (vec, 0) = init;
   13025        1156 :   for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
   13026         610 :     TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
   13027             : 
   13028         546 :   return expand_left_fold (t, vec, complain);
   13029             : }
   13030             : 
   13031             : /* Expand a PACK of arguments into a grouped as right fold.
   13032             :    Given a pack containing elementns A0, A1, ..., and an
   13033             :    operator @, this builds the expression:
   13034             : 
   13035             :       A0@ ... (An-2 @ (An-1 @ An))
   13036             : 
   13037             :    Note that PACK must not be empty.
   13038             : 
   13039             :    The operator is defined by the original fold expression T. */
   13040             : 
   13041             : tree
   13042       45488 : expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
   13043             : {
   13044             :   // Build the expression.
   13045       45488 :   int n = TREE_VEC_LENGTH (pack);
   13046       45488 :   tree right = TREE_VEC_ELT (pack, n - 1);
   13047      119852 :   for (--n; n != 0; --n)
   13048             :     {
   13049       74364 :       tree left = TREE_VEC_ELT (pack, n - 1);
   13050       74364 :       right = fold_expression (t, left, right, complain);
   13051             :     }
   13052       45488 :   return right;
   13053             : }
   13054             : 
   13055             : /* Substitute into a unary right fold expression. */
   13056             : 
   13057             : static tree
   13058       80486 : tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
   13059             :                          tree in_decl)
   13060             : {
   13061       80486 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13062       80486 :   if (pack == error_mark_node)
   13063             :     return error_mark_node;
   13064       80483 :   if (PACK_EXPANSION_P (pack))
   13065             :     {
   13066        7252 :       tree r = copy_node (t);
   13067        7252 :       FOLD_EXPR_PACK (r) = pack;
   13068        7252 :       return r;
   13069             :     }
   13070       73231 :   if (TREE_VEC_LENGTH (pack) == 0)
   13071       27988 :     return expand_empty_fold (t, complain);
   13072             :   else
   13073       45243 :     return expand_right_fold (t, pack, complain);
   13074             : }
   13075             : 
   13076             : /* Substitute into a binary right fold expression.
   13077             : 
   13078             :    Do ths by building a single (non-empty) vector of arguments and
   13079             :    building the expression from those elements. */
   13080             : 
   13081             : static tree
   13082         305 : tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
   13083             :                          tree in_decl)
   13084             : {
   13085         305 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13086         305 :   if (pack == error_mark_node)
   13087             :     return error_mark_node;
   13088         305 :   tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
   13089         305 :   if (init == error_mark_node)
   13090             :     return error_mark_node;
   13091             : 
   13092         305 :   if (PACK_EXPANSION_P (pack))
   13093             :     {
   13094          60 :       tree r = copy_node (t);
   13095          60 :       FOLD_EXPR_PACK (r) = pack;
   13096          60 :       FOLD_EXPR_INIT (r) = init;
   13097          60 :       return r;
   13098             :     }
   13099             : 
   13100         245 :   int n = TREE_VEC_LENGTH (pack);
   13101         245 :   tree vec = make_tree_vec (n + 1);
   13102         558 :   for (int i = 0; i < n; ++i)
   13103         313 :     TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
   13104         245 :   TREE_VEC_ELT (vec, n) = init;
   13105             : 
   13106         245 :   return expand_right_fold (t, vec, complain);
   13107             : }
   13108             : 
   13109             : /* Walk through the pattern of a pack expansion, adding everything in
   13110             :    local_specializations to a list.  */
   13111             : 
   13112             : class el_data
   13113             : {
   13114             : public:
   13115             :   /* Set of variables declared within the pattern.  */
   13116             :   hash_set<tree> internal;
   13117             :   /* Set of AST nodes that have been visited by the traversal.  */
   13118             :   hash_set<tree> visited;
   13119             :   /* List of local_specializations used within the pattern.  */
   13120             :   tree extra;
   13121             :   tsubst_flags_t complain;
   13122             :   /* True iff we don't want to walk into unevaluated contexts.  */
   13123             :   bool skip_unevaluated_operands = false;
   13124             :   /* The unevaluated contexts that we avoided walking.  */
   13125             :   auto_vec<tree> skipped_trees;
   13126             : 
   13127         833 :   el_data (tsubst_flags_t c)
   13128         833 :     : extra (NULL_TREE), complain (c) {}
   13129             : };
   13130             : static tree
   13131       70291 : extract_locals_r (tree *tp, int *walk_subtrees, void *data_)
   13132             : {
   13133       70291 :   el_data &data = *reinterpret_cast<el_data*>(data_);
   13134       70291 :   tree *extra = &data.extra;
   13135       70291 :   tsubst_flags_t complain = data.complain;
   13136             : 
   13137       70291 :   if (data.skip_unevaluated_operands
   13138       70291 :       && unevaluated_p (TREE_CODE (*tp)))
   13139             :     {
   13140        1464 :       data.skipped_trees.safe_push (*tp);
   13141        1464 :       *walk_subtrees = 0;
   13142        1464 :       return NULL_TREE;
   13143             :     }
   13144             : 
   13145       68827 :   if (TYPE_P (*tp) && typedef_variant_p (*tp))
   13146             :     /* Remember local typedefs (85214).  */
   13147        1132 :     tp = &TYPE_NAME (*tp);
   13148             : 
   13149       68827 :   if (TREE_CODE (*tp) == DECL_EXPR)
   13150             :     {
   13151        1410 :       tree decl = DECL_EXPR_DECL (*tp);
   13152        1410 :       data.internal.add (decl);
   13153        1410 :       if (VAR_P (decl)
   13154         755 :           && DECL_DECOMPOSITION_P (decl)
   13155        1413 :           && TREE_TYPE (decl) != error_mark_node)
   13156             :         {
   13157           3 :           gcc_assert (DECL_NAME (decl) == NULL_TREE);
   13158           3 :           for (tree decl2 = DECL_CHAIN (decl);
   13159           9 :                decl2
   13160           6 :                && VAR_P (decl2)
   13161           6 :                && DECL_DECOMPOSITION_P (decl2)
   13162           6 :                && DECL_NAME (decl2)
   13163          15 :                && TREE_TYPE (decl2) != error_mark_node;
   13164           6 :                decl2 = DECL_CHAIN (decl2))
   13165             :             {
   13166           6 :               gcc_assert (DECL_DECOMP_BASE (decl2) == decl);
   13167           6 :               data.internal.add (decl2);
   13168             :             }
   13169             :         }
   13170             :     }
   13171       67417 :   else if (TREE_CODE (*tp) == LAMBDA_EXPR)
   13172             :     {
   13173             :       /* Since we defer implicit capture, look in the parms and body.  */
   13174         174 :       tree fn = lambda_function (*tp);
   13175         174 :       cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data,
   13176             :                     &data.visited);
   13177         174 :       cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data,
   13178             :                     &data.visited);
   13179             :     }
   13180       67243 :   else if (tree spec = retrieve_local_specialization (*tp))
   13181             :     {
   13182        1912 :       if (data.internal.contains (*tp))
   13183             :         /* Don't mess with variables declared within the pattern.  */
   13184             :         return NULL_TREE;
   13185        1904 :       if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
   13186             :         {
   13187             :           /* Maybe pull out the PARM_DECL for a partial instantiation.  */
   13188          13 :           tree args = ARGUMENT_PACK_ARGS (spec);
   13189          13 :           if (TREE_VEC_LENGTH (args) == 1)
   13190             :             {
   13191           5 :               tree elt = TREE_VEC_ELT (args, 0);
   13192           5 :               if (PACK_EXPANSION_P (elt))
   13193           0 :                 elt = PACK_EXPANSION_PATTERN (elt);
   13194           5 :               if (DECL_PACK_P (elt))
   13195             :                 spec = elt;
   13196             :             }
   13197          13 :           if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
   13198             :             {
   13199             :               /* Handle lambda capture here, since we aren't doing any
   13200             :                  substitution now, and so tsubst_copy won't call
   13201             :                  process_outer_var_ref.  */
   13202          13 :               tree args = ARGUMENT_PACK_ARGS (spec);
   13203          13 :               int len = TREE_VEC_LENGTH (args);
   13204          28 :               for (int i = 0; i < len; ++i)
   13205             :                 {
   13206          15 :                   tree arg = TREE_VEC_ELT (args, i);
   13207          15 :                   tree carg = arg;
   13208          15 :                   if (outer_automatic_var_p (arg))
   13209          15 :                     carg = process_outer_var_ref (arg, complain);
   13210          15 :                   if (carg != arg)
   13211             :                     {
   13212             :                       /* Make a new NONTYPE_ARGUMENT_PACK of the capture
   13213             :                          proxies.  */
   13214          15 :                       if (i == 0)
   13215             :                         {
   13216          10 :                           spec = copy_node (spec);
   13217          10 :                           args = copy_node (args);
   13218          10 :                           ARGUMENT_PACK_ARGS (spec) = args;
   13219          10 :                           register_local_specialization (spec, *tp);
   13220             :                         }
   13221          15 :                       TREE_VEC_ELT (args, i) = carg;
   13222             :                     }
   13223             :                 }
   13224             :             }
   13225             :         }
   13226        1904 :       if (outer_automatic_var_p (spec))
   13227           7 :         spec = process_outer_var_ref (spec, complain);
   13228        1904 :       *extra = tree_cons (*tp, spec, *extra);
   13229             :     }
   13230             :   return NULL_TREE;
   13231             : }
   13232             : static tree
   13233         833 : extract_local_specs (tree pattern, tsubst_flags_t complain)
   13234             : {
   13235         833 :   el_data data (complain);
   13236             :   /* Walk the pattern twice, ignoring unevaluated operands the first time
   13237             :      around, so that if a local specialization appears in both an evaluated
   13238             :      and unevaluated context we prefer to process it in the evaluated context
   13239             :      (since e.g. process_outer_var_ref is a no-op inside an unevaluated
   13240             :      context).  */
   13241         833 :   data.skip_unevaluated_operands = true;
   13242         833 :   cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited);
   13243             :   /* Now walk the unevaluated contexts we skipped the first time around.  */
   13244         833 :   data.skip_unevaluated_operands = false;
   13245        3717 :   for (tree t : data.skipped_trees)
   13246             :     {
   13247        1464 :       data.visited.remove (t);
   13248        1464 :       cp_walk_tree (&t, extract_locals_r, &data, &data.visited);
   13249             :     }
   13250        1666 :   return data.extra;
   13251         833 : }
   13252             : 
   13253             : /* Extract any uses of local_specializations from PATTERN and add them to ARGS
   13254             :    for use in PACK_EXPANSION_EXTRA_ARGS.  */
   13255             : 
   13256             : tree
   13257       59634 : build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
   13258             : {
   13259             :   /* Make a copy of the extra arguments so that they won't get changed
   13260             :      out from under us.  */
   13261       59634 :   tree extra = preserve_args (copy_template_args (args), /*cow_p=*/false);
   13262       59634 :   if (local_specializations)
   13263         833 :     if (tree locals = extract_local_specs (pattern, complain))
   13264         681 :       extra = tree_cons (NULL_TREE, extra, locals);
   13265       59634 :   return extra;
   13266             : }
   13267             : 
   13268             : /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
   13269             :    normal template args to ARGS.  */
   13270             : 
   13271             : tree
   13272    57350256 : add_extra_args (tree extra, tree args, tsubst_flags_t complain, tree in_decl)
   13273             : {
   13274    57350256 :   if (extra && TREE_CODE (extra) == TREE_LIST)
   13275             :     {
   13276       16393 :       for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
   13277             :         {
   13278             :           /* The partial instantiation involved local declarations collected in
   13279             :              extract_local_specs; map from the general template to our local
   13280             :              context.  */
   13281       11481 :           tree gen = TREE_PURPOSE (elt);
   13282       11481 :           tree inst = TREE_VALUE (elt);
   13283       11481 :           if (DECL_P (inst))
   13284       11468 :             if (tree local = retrieve_local_specialization (inst))
   13285       11481 :               inst = local;
   13286             :           /* else inst is already a full instantiation of the pack.  */
   13287       11481 :           register_local_specialization (inst, gen);
   13288             :         }
   13289        4912 :       gcc_assert (!TREE_PURPOSE (extra));
   13290        4912 :       extra = TREE_VALUE (extra);
   13291             :     }
   13292    57350256 :   if (uses_template_parms (extra))
   13293             :     {
   13294             :       /* This can happen after dependent substitution into a
   13295             :          requires-expr or a lambda that uses constexpr if.  */
   13296          13 :       extra = tsubst_template_args (extra, args, complain, in_decl);
   13297          13 :       args = add_outermost_template_args (args, extra);
   13298             :     }
   13299             :   else
   13300    57350243 :     args = add_to_template_args (extra, args);
   13301    57350256 :   return args;
   13302             : }
   13303             : 
   13304             : /* Substitute ARGS into T, which is an pack expansion
   13305             :    (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
   13306             :    TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
   13307             :    (if only a partial substitution could be performed) or
   13308             :    ERROR_MARK_NODE if there was an error.  */
   13309             : tree
   13310    56598607 : tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
   13311             :                        tree in_decl)
   13312             : {
   13313    56598607 :   tree pattern;
   13314    56598607 :   tree pack, packs = NULL_TREE;
   13315    56598607 :   bool unsubstituted_packs = false;
   13316    56598607 :   int i, len = -1;
   13317    56598607 :   tree result;
   13318    56598607 :   bool need_local_specializations = false;
   13319    56598607 :   int levels;
   13320             : 
   13321    56598607 :   gcc_assert (PACK_EXPANSION_P (t));
   13322    56598607 :   pattern = PACK_EXPANSION_PATTERN (t);
   13323             : 
   13324             :   /* Add in any args remembered from an earlier partial instantiation.  */
   13325    56598607 :   args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args, complain, in_decl);
   13326             : 
   13327   113197214 :   levels = TMPL_ARGS_DEPTH (args);
   13328             : 
   13329             :   /* Determine the argument packs that will instantiate the parameter
   13330             :      packs used in the expansion expression. While we're at it,
   13331             :      compute the number of arguments to be expanded and make sure it
   13332             :      is consistent.  */
   13333   166057517 :   for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
   13334    54694655 :        pack = TREE_CHAIN (pack))
   13335             :     {
   13336    56960577 :       tree parm_pack = TREE_VALUE (pack);
   13337    56960577 :       tree arg_pack = NULL_TREE;
   13338    56960577 :       tree orig_arg = NULL_TREE;
   13339    56960577 :       int level = 0;
   13340             : 
   13341    56960577 :       if (TREE_CODE (parm_pack) == BASES)
   13342             :         {
   13343          27 :           gcc_assert (parm_pack == pattern);
   13344          27 :           if (BASES_DIRECT (parm_pack))
   13345          15 :             return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
   13346             :                                                         args, complain,
   13347             :                                                         in_decl),
   13348     2265919 :                                            complain);
   13349             :           else
   13350          12 :             return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
   13351             :                                                  args, complain, in_decl),
   13352          12 :                                     complain);
   13353             :         }
   13354    56960550 :       else if (builtin_pack_call_p (parm_pack))
   13355             :         {
   13356       75740 :           if (parm_pack != pattern)
   13357             :             {
   13358           2 :               if (complain & tf_error)
   13359           2 :                 sorry ("%qE is not the entire pattern of the pack expansion",
   13360             :                        parm_pack);
   13361           2 :               return error_mark_node;
   13362             :             }
   13363       75738 :           return expand_builtin_pack_call (parm_pack, args,
   13364       75738 :                                            complain, in_decl);
   13365             :         }
   13366    56884810 :       else if (TREE_CODE (parm_pack) == PARM_DECL)
   13367             :         {
   13368             :           /* We know we have correct local_specializations if this
   13369             :              expansion is at function scope, or if we're dealing with a
   13370             :              local parameter in a requires expression; for the latter,
   13371             :              tsubst_requires_expr set it up appropriately.  */
   13372      388969 :           if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
   13373      385195 :             arg_pack = retrieve_local_specialization (parm_pack);
   13374             :           else
   13375             :             /* We can't rely on local_specializations for a parameter
   13376             :                name used later in a function declaration (such as in a
   13377             :                late-specified return type).  Even if it exists, it might
   13378             :                have the wrong value for a recursive call.  */
   13379             :             need_local_specializations = true;
   13380             : 
   13381      385195 :           if (!arg_pack)
   13382             :             {
   13383             :               /* This parameter pack was used in an unevaluated context.  Just
   13384             :                  make a dummy decl, since it's only used for its type.  */
   13385        3774 :               ++cp_unevaluated_operand;
   13386        3774 :               arg_pack = tsubst_decl (parm_pack, args, complain);
   13387        3774 :               --cp_unevaluated_operand;
   13388        3774 :               if (arg_pack && DECL_PACK_P (arg_pack))
   13389             :                 /* Partial instantiation of the parm_pack, we can't build
   13390             :                    up an argument pack yet.  */
   13391             :                 arg_pack = NULL_TREE;
   13392             :               else
   13393        2465 :                 arg_pack = make_fnparm_pack (arg_pack);
   13394             :             }
   13395      385195 :           else if (DECL_PACK_P (arg_pack))
   13396             :             /* This argument pack isn't fully instantiated yet.  */
   13397             :             arg_pack = NULL_TREE;
   13398             :         }
   13399    56495841 :       else if (is_capture_proxy (parm_pack))
   13400             :         {
   13401          60 :           arg_pack = retrieve_local_specialization (parm_pack);
   13402          60 :           if (DECL_PACK_P (arg_pack))
   13403             :             arg_pack = NULL_TREE;
   13404             :         }
   13405             :       else
   13406             :         {
   13407    56495781 :           int idx;
   13408    56495781 :           template_parm_level_and_index (parm_pack, &level, &idx);
   13409    56495781 :           if (level <= levels)
   13410    98338328 :             arg_pack = TMPL_ARG (args, level, idx);
   13411             : 
   13412    47725634 :           if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
   13413    49169253 :               && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
   13414          89 :             arg_pack = NULL_TREE;
   13415             :         }
   13416             : 
   13417    65655046 :       orig_arg = arg_pack;
   13418    56882622 :       if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
   13419        1822 :         arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
   13420             : 
   13421    48112386 :       if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
   13422             :         /* This can only happen if we forget to expand an argument
   13423             :            pack somewhere else. Just return an error, silently.  */
   13424             :         {
   13425           0 :           result = make_tree_vec (1);
   13426           0 :           TREE_VEC_ELT (result, 0) = error_mark_node;
   13427           0 :           return result;
   13428             :         }
   13429             : 
   13430    56884810 :       if (arg_pack)
   13431             :         {
   13432    48112386 :           int my_len = 
   13433    48112386 :             TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
   13434             : 
   13435             :           /* Don't bother trying to do a partial substitution with
   13436             :              incomplete packs; we'll try again after deduction.  */
   13437    48112386 :           if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
   13438             :             return t;
   13439             : 
   13440    45922269 :           if (len < 0)
   13441             :             len = my_len;
   13442      301750 :           else if (len != my_len)
   13443             :             {
   13444          35 :               if (!(complain & tf_error))
   13445             :                 /* Fail quietly.  */;
   13446          13 :               else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
   13447           7 :                 error ("mismatched argument pack lengths while expanding %qT",
   13448             :                        pattern);
   13449             :               else
   13450           6 :                 error ("mismatched argument pack lengths while expanding %qE",
   13451             :                        pattern);
   13452          35 :               return error_mark_node;
   13453             :             }
   13454             : 
   13455             :           /* Keep track of the parameter packs and their corresponding
   13456             :              argument packs.  */
   13457    45922234 :           packs = tree_cons (parm_pack, arg_pack, packs);
   13458    45922234 :           TREE_TYPE (packs) = orig_arg;
   13459             :         }
   13460             :       else
   13461             :         {
   13462             :           /* We can't substitute for this parameter pack.  We use a flag as
   13463             :              well as the missing_level counter because function parameter
   13464             :              packs don't have a level.  */
   13465     8772424 :           gcc_assert (processing_template_decl || is_auto (parm_pack));
   13466             :           unsubstituted_packs = true;
   13467             :         }
   13468             :     }
   13469             : 
   13470             :   /* If the expansion is just T..., return the matching argument pack, unless
   13471             :      we need to call convert_from_reference on all the elements.  This is an
   13472             :      important optimization; see c++/68422.  */
   13473    54332685 :   if (!unsubstituted_packs
   13474    99894345 :       && TREE_PURPOSE (packs) == pattern)
   13475             :     {
   13476    41491156 :       tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
   13477             : 
   13478             :       /* If the argument pack is a single pack expansion, pull it out.  */
   13479    41491156 :       if (TREE_VEC_LENGTH (args) == 1
   13480    41491156 :           && pack_expansion_args_count (args))
   13481     3839765 :         return TREE_VEC_ELT (args, 0);
   13482             : 
   13483             :       /* Types need no adjustment, nor does sizeof..., and if we still have
   13484             :          some pack expansion args we won't do anything yet.  */
   13485    37651391 :       if (TREE_CODE (t) == TYPE_PACK_EXPANSION
   13486      531682 :           || PACK_EXPANSION_SIZEOF_P (t)
   13487    38182490 :           || pack_expansion_args_count (args))
   13488             :         return args;
   13489             :       /* Also optimize expression pack expansions if we can tell that the
   13490             :          elements won't have reference type.  */
   13491      437009 :       tree type = TREE_TYPE (pattern);
   13492      437009 :       if (type && !TYPE_REF_P (type)
   13493      437009 :           && !PACK_EXPANSION_P (type)
   13494             :           && !WILDCARD_TYPE_P (type))
   13495             :         return args;
   13496             :       /* Otherwise use the normal path so we get convert_from_reference.  */
   13497             :     }
   13498             : 
   13499             :   /* We cannot expand this expansion expression, because we don't have
   13500             :      all of the argument packs we need.  */
   13501    12849211 :   if (use_pack_expansion_extra_args_p (t, packs, len, unsubstituted_packs))
   13502             :     {
   13503             :       /* We got some full packs, but we can't substitute them in until we
   13504             :          have values for all the packs.  So remember these until then.  */
   13505             : 
   13506       58827 :       t = make_pack_expansion (pattern, complain);
   13507       58827 :       PACK_EXPANSION_EXTRA_ARGS (t)
   13508       58827 :         = build_extra_args (pattern, args, complain);
   13509       58827 :       return t;
   13510             :     }
   13511             : 
   13512             :   /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
   13513             :      type, so create our own local specializations map; the current map is
   13514             :      either NULL or (in the case of recursive unification) might have
   13515             :      bindings that we don't want to use or alter.  */
   13516    12790384 :   local_specialization_stack lss (need_local_specializations
   13517    25577004 :                                   ? lss_blank : lss_nop);
   13518             : 
   13519    12790384 :   if (unsubstituted_packs)
   13520             :     {
   13521             :       /* There were no real arguments, we're just replacing a parameter
   13522             :          pack with another version of itself. Substitute into the
   13523             :          pattern and return a PACK_EXPANSION_*. The caller will need to
   13524             :          deal with that.  */
   13525     8712201 :       if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
   13526      448955 :         result = tsubst_expr (pattern, args, complain, in_decl);
   13527             :       else
   13528     8263246 :         result = tsubst (pattern, args, complain, in_decl);
   13529     8712201 :       result = make_pack_expansion (result, complain);
   13530     8712201 :       PACK_EXPANSION_LOCAL_P (result) = PACK_EXPANSION_LOCAL_P (t);
   13531     8712201 :       PACK_EXPANSION_SIZEOF_P (result) = PACK_EXPANSION_SIZEOF_P (t);
   13532     8712201 :       if (PACK_EXPANSION_AUTO_P (t))
   13533             :         {
   13534             :           /* This is a fake auto... pack expansion created in add_capture with
   13535             :              _PACKS that don't appear in the pattern.  Copy one over.  */
   13536           2 :           packs = PACK_EXPANSION_PARAMETER_PACKS (t);
   13537           1 :           pack = retrieve_local_specialization (TREE_VALUE (packs));
   13538           1 :           gcc_checking_assert (DECL_PACK_P (pack));
   13539           2 :           PACK_EXPANSION_PARAMETER_PACKS (result)
   13540           1 :             = build_tree_list (NULL_TREE, pack);
   13541           1 :           PACK_EXPANSION_AUTO_P (result) = true;
   13542             :         }
   13543     8712201 :       return result;
   13544             :     }
   13545             : 
   13546     4078183 :   gcc_assert (len >= 0);
   13547             : 
   13548             :   /* For each argument in each argument pack, substitute into the
   13549             :      pattern.  */
   13550     4078183 :   result = make_tree_vec (len);
   13551     4078183 :   tree elem_args = copy_template_args (args);
   13552    15294796 :   for (i = 0; i < len; ++i)
   13553             :     {
   13554     7919835 :       t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
   13555             :                                                     i,
   13556             :                                                     elem_args, complain,
   13557             :                                                     in_decl);
   13558     7919835 :       TREE_VEC_ELT (result, i) = t;
   13559     7919835 :       if (t == error_mark_node)
   13560             :         {
   13561             :           result = error_mark_node;
   13562             :           break;
   13563             :         }
   13564             :     }
   13565             : 
   13566             :   /* Update ARGS to restore the substitution from parameter packs to
   13567             :      their argument packs.  */
   13568     8458074 :   for (pack = packs; pack; pack = TREE_CHAIN (pack))
   13569             :     {
   13570     4379891 :       tree parm = TREE_PURPOSE (pack);
   13571             : 
   13572     4379891 :       if (TREE_CODE (parm) == PARM_DECL
   13573     4379891 :           || VAR_P (parm)
   13574     3993341 :           || TREE_CODE (parm) == FIELD_DECL)
   13575      386550 :         register_local_specialization (TREE_TYPE (pack), parm);
   13576             :       else
   13577             :         {
   13578     3993341 :           int idx, level;
   13579             : 
   13580     3993341 :           if (TREE_VALUE (pack) == NULL_TREE)
   13581           0 :             continue;
   13582             : 
   13583     3993341 :           template_parm_level_and_index (parm, &level, &idx);
   13584             : 
   13585             :           /* Update the corresponding argument.  */
   13586     7986682 :           if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
   13587      542753 :             TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
   13588      542753 :               TREE_TYPE (pack);
   13589             :           else
   13590     3450588 :             TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
   13591             :         }
   13592             :     }
   13593             : 
   13594             :   /* If the dependent pack arguments were such that we end up with only a
   13595             :      single pack expansion again, there's no need to keep it in a TREE_VEC.  */
   13596      705141 :   if (len == 1 && TREE_CODE (result) == TREE_VEC
   13597     4781702 :       && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
   13598      108724 :     return TREE_VEC_ELT (result, 0);
   13599             : 
   13600             :   return result;
   13601    12790384 : }
   13602             : 
   13603             : /* Make an argument pack out of the TREE_VEC VEC.  */
   13604             : 
   13605             : static tree
   13606          18 : make_argument_pack (tree vec)
   13607             : {
   13608          18 :   tree pack;
   13609             : 
   13610          18 :   if (TYPE_P (TREE_VEC_ELT (vec, 0)))
   13611          12 :     pack = cxx_make_type (TYPE_ARGUMENT_PACK);
   13612             :   else
   13613             :     {
   13614           6 :       pack = make_node (NONTYPE_ARGUMENT_PACK);
   13615           6 :       TREE_CONSTANT (pack) = 1;
   13616             :     }
   13617          18 :   ARGUMENT_PACK_ARGS (pack) = vec;
   13618          18 :   return pack;
   13619             : }
   13620             : 
   13621             : /* Return an exact copy of template args T that can be modified
   13622             :    independently.  */
   13623             : 
   13624             : static tree
   13625     9602706 : copy_template_args (tree t)
   13626             : {
   13627     9602706 :   if (t == error_mark_node)
   13628             :     return t;
   13629             : 
   13630     9602706 :   int len = TREE_VEC_LENGTH (t);
   13631     9602706 :   tree new_vec = make_tree_vec (len);
   13632             : 
   13633    28742648 :   for (int i = 0; i < len; ++i)
   13634             :     {
   13635    19139942 :       tree elt = TREE_VEC_ELT (t, i);
   13636    19139942 :       if (elt && TREE_CODE (elt) == TREE_VEC)
   13637      983383 :         elt = copy_template_args (elt);
   13638    19139942 :       TREE_VEC_ELT (new_vec, i) = elt;
   13639             :     }
   13640             : 
   13641    19205412 :   NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
   13642     9602706 :     = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
   13643             : 
   13644     9602706 :   return new_vec;
   13645             : }
   13646             : 
   13647             : /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg.  */
   13648             : 
   13649             : tree
   13650    75763747 : tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
   13651             :                       tree in_decl)
   13652             : {
   13653             :   /* This flag is used only during deduction, and we don't expect to
   13654             :      substitute such ARGUMENT_PACKs.  */
   13655    75763747 :   gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (orig_arg));
   13656             : 
   13657             :   /* Substitute into each of the arguments.  */
   13658    75763747 :   tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
   13659             :                                          args, complain, in_decl);
   13660    75763747 :   if (pack_args == error_mark_node)
   13661             :     return error_mark_node;
   13662             : 
   13663    74847763 :   if (pack_args == ARGUMENT_PACK_ARGS (orig_arg))
   13664             :     return orig_arg;
   13665             : 
   13666             :   /* If we're substituting into a generic ARGUMENT_PACK for a variadic
   13667             :      template parameter, we might be able to avoid allocating a new
   13668             :      ARGUMENT_PACK and reuse the corresponding ARGUMENT_PACK from ARGS
   13669             :      if the substituted result is identical to it.  */
   13670    70861011 :   if (tree parm = template_arg_to_parm (orig_arg))
   13671             :     {
   13672    35932485 :       int level, index;
   13673    35932485 :       template_parm_level_and_index (parm, &level, &index);
   13674   104558049 :       if (TMPL_ARGS_DEPTH (args) >= level)
   13675    62838846 :         if (tree arg = TMPL_ARG (args, level, index))
   13676    30494828 :           if (TREE_CODE (arg) == TREE_CODE (orig_arg)
   13677    30494828 :               && ARGUMENT_PACK_ARGS (arg) == pack_args)
   13678             :             {
   13679    27832141 :               gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (arg));
   13680    27832141 :               return arg;
   13681             :             }
   13682             :     }
   13683             : 
   13684    43028870 :   tree new_arg;
   13685    43028870 :   if (TYPE_P (orig_arg))
   13686             :     {
   13687    42389140 :       new_arg = cxx_make_type (TREE_CODE (orig_arg));
   13688    42389140 :       SET_TYPE_STRUCTURAL_EQUALITY (new_arg);
   13689             :     }
   13690             :   else
   13691             :     {
   13692      639730 :       new_arg = make_node (TREE_CODE (orig_arg));
   13693      639730 :       TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
   13694             :     }
   13695    43028870 :   ARGUMENT_PACK_ARGS (new_arg) = pack_args;
   13696             :   return new_arg;
   13697             : }
   13698             : 
   13699             : /* Substitute ARGS into the vector or list of template arguments T.  */
   13700             : 
   13701             : tree
   13702   949905048 : tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   13703             : {
   13704   949905048 :   if (t == error_mark_node)
   13705             :     return error_mark_node;
   13706             : 
   13707             :   /* In "sizeof(X<I>)" we need to evaluate "I".  */
   13708   949905048 :   cp_evaluated ev;
   13709             : 
   13710   949905048 :   const int len = TREE_VEC_LENGTH (t);
   13711   949905048 :   tree *elts = XALLOCAVEC (tree, len);
   13712   949905048 :   int expanded_len_adjust = 0;
   13713             : 
   13714             :   /* True iff the substituted result is identical to T.  */
   13715   949905048 :   bool const_subst_p = true;
   13716             : 
   13717  2978175445 :   for (int i = 0; i < len; i++)
   13718             :     {
   13719  2030514353 :       tree orig_arg = TREE_VEC_ELT (t, i);
   13720  2030514353 :       tree new_arg;
   13721             : 
   13722  2030514353 :       if (!orig_arg)
   13723             :         new_arg = NULL_TREE;
   13724  2030514348 :       else if (TREE_CODE (orig_arg) == TREE_VEC)
   13725    82068523 :         new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
   13726  1948445825 :       else if (PACK_EXPANSION_P (orig_arg))
   13727             :         {
   13728             :           /* Substitute into an expansion expression.  */
   13729    49045071 :           new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
   13730             : 
   13731    49045071 :           if (TREE_CODE (new_arg) == TREE_VEC)
   13732             :             /* Add to the expanded length adjustment the number of
   13733             :                expanded arguments. We subtract one from this
   13734             :                measurement, because the argument pack expression
   13735             :                itself is already counted as 1 in
   13736             :                LEN. EXPANDED_LEN_ADJUST can actually be negative, if
   13737             :                the argument pack is empty.  */
   13738    36692440 :             expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
   13739             :         }
   13740  1899400754 :       else if (ARGUMENT_PACK_P (orig_arg))
   13741    75650970 :         new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
   13742             :       else
   13743  1823749784 :         new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
   13744             : 
   13745  2030514315 :       if (new_arg == error_mark_node)
   13746     2243918 :         return error_mark_node;
   13747             : 
   13748  2028270397 :       elts[i] = new_arg;
   13749  2028270397 :       if (new_arg != orig_arg)
   13750  1934907300 :         const_subst_p = false;
   13751             :     }
   13752             : 
   13753   947661092 :   if (const_subst_p)
   13754             :     return t;
   13755             : 
   13756   931105318 :   tree maybe_reuse = NULL_TREE;
   13757             : 
   13758             :   /* If ARGS and T are both multi-level, the substituted result may be
   13759             :      identical to ARGS.  */
   13760   931105318 :   if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (t)
   13761    81966836 :       && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
   13762     9872942 :       && TMPL_ARGS_DEPTH (t) == TMPL_ARGS_DEPTH (args))
   13763             :     maybe_reuse = args;
   13764             :   /* If T appears to be a vector of generic template arguments, the
   13765             :      substituted result may be identical to the corresponding level
   13766             :      from ARGS.  */
   13767   922092194 :   else if (tree parm = template_arg_to_parm (TREE_VEC_ELT (t, 0)))
   13768             :     {
   13769   733279811 :       int level, index;
   13770   733279811 :       template_parm_level_and_index (parm, &level, &index);
   13771  2107841830 :       if (index == 0 && TMPL_ARGS_DEPTH (args) >= level)
   13772  1314088268 :         maybe_reuse = TMPL_ARGS_LEVEL (args, level);
   13773             :     }
   13774             : 
   13775             :   /* If the substituted result is identical to MAYBE_REUSE, return
   13776             :      it and avoid allocating a new TREE_VEC, as an optimization.  */
   13777    76235677 :   if (maybe_reuse != NULL_TREE
   13778   666057258 :       && TREE_VEC_LENGTH (maybe_reuse) == len
   13779  1279955846 :       && std::equal (elts, elts+len, TREE_VEC_BEGIN (maybe_reuse)))
   13780             :     return maybe_reuse;
   13781             : 
   13782             :   /* If T consists of only a pack expansion for which substitution yielded
   13783             :      a TREE_VEC of the expanded elements, then reuse that TREE_VEC instead
   13784             :      of effectively making a copy.  */
   13785   340746444 :   if (len == 1
   13786   162946157 :       && PACK_EXPANSION_P (TREE_VEC_ELT (t, 0))
   13787   381900738 :       && TREE_CODE (elts[0]) == TREE_VEC)
   13788             :     return elts[0];
   13789             : 
   13790             :   /* Make space for the expanded arguments coming from template
   13791             :      argument packs.  */
   13792   308015928 :   tree r = make_tree_vec (len + expanded_len_adjust);
   13793             :   /* T can contain TREE_VECs. That happens if T contains the
   13794             :      arguments for a member template.
   13795             :      In that case each TREE_VEC in T represents a level of template
   13796             :      arguments, and T won't carry any non defaulted argument count.
   13797             :      It will rather be the nested TREE_VECs that will carry one.
   13798             :      In other words, T carries a non defaulted argument count only
   13799             :      if it doesn't contain any nested TREE_VEC.  */
   13800   308015928 :   if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (t))
   13801             :     {
   13802   272124794 :       int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
   13803   272124794 :       count += expanded_len_adjust;
   13804   272124794 :       SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (r, count);
   13805             :     }
   13806             : 
   13807             :   int out = 0;
   13808   851260682 :   for (int i = 0; i < len; i++)
   13809             :     {
   13810   543244754 :       tree orig_arg = TREE_VEC_ELT (t, i);
   13811   543244754 :       if (orig_arg
   13812   543244750 :           && PACK_EXPANSION_P (orig_arg)
   13813    12815776 :           && TREE_CODE (elts[i]) == TREE_VEC)
   13814             :         {
   13815             :           /* Now expand the template argument pack "in place".  */
   13816    10068792 :           for (int idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
   13817     6106868 :             TREE_VEC_ELT (r, out) = TREE_VEC_ELT (elts[i], idx);
   13818             :         }
   13819             :       else
   13820             :         {
   13821   539282830 :           TREE_VEC_ELT (r, out) = elts[i];
   13822   539282830 :           out++;
   13823             :         }
   13824             :     }
   13825   308015928 :   gcc_assert (out == TREE_VEC_LENGTH (r));
   13826             : 
   13827             :   return r;
   13828   949905010 : }
   13829             : 
   13830             : /* Substitute ARGS into one level PARMS of template parameters.  */
   13831             : 
   13832             : static tree
   13833    14335374 : tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
   13834             : {
   13835    14335374 :   if (parms == error_mark_node)
   13836             :     return error_mark_node;
   13837             : 
   13838    14335374 :   tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
   13839             : 
   13840    38328640 :   for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
   13841             :     {
   13842    23993266 :       tree tuple = TREE_VEC_ELT (parms, i);
   13843             : 
   13844    23993266 :       if (tuple == error_mark_node)
   13845           0 :         continue;
   13846             : 
   13847    23993266 :       TREE_VEC_ELT (new_vec, i) =
   13848    23993266 :         tsubst_template_parm (tuple, args, complain);
   13849             :     }
   13850             : 
   13851             :   return new_vec;
   13852             : }
   13853             : 
   13854             : /* Return the result of substituting ARGS into the template parameters
   13855             :    given by PARMS.  If there are m levels of ARGS and m + n levels of
   13856             :    PARMS, then the result will contain n levels of PARMS.  For
   13857             :    example, if PARMS is `template <class T> template <class U>
   13858             :    template <T*, U, class V>' and ARGS is {{int}, {double}} then the
   13859             :    result will be `template <int*, double, class V>'.  */
   13860             : 
   13861             : static tree
   13862    14333727 : tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
   13863             : {
   13864    14333727 :   tree r = NULL_TREE;
   13865    14333727 :   tree* new_parms;
   13866             : 
   13867             :   /* When substituting into a template, we must set
   13868             :      PROCESSING_TEMPLATE_DECL as the template parameters may be
   13869             :      dependent if they are based on one-another, and the dependency
   13870             :      predicates are short-circuit outside of templates.  */
   13871    14333727 :   ++processing_template_decl;
   13872             : 
   13873    14333727 :   for (new_parms = &r;
   13874    86007201 :        parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
   13875    28670684 :        new_parms = &(TREE_CHAIN (*new_parms)),
   13876    14335342 :          parms = TREE_CHAIN (parms))
   13877             :     {
   13878    14335342 :       tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
   13879             :                                                   args, complain);
   13880    28670684 :       *new_parms =
   13881    28670682 :         tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
   13882             :                              - TMPL_ARGS_DEPTH (args)),
   13883             :                    new_vec, NULL_TREE);
   13884    28670684 :       TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
   13885    28670684 :         = TEMPLATE_PARMS_CONSTRAINTS (parms);
   13886             :     }
   13887             : 
   13888    14333727 :   --processing_template_decl;
   13889             : 
   13890    14333727 :   return r;
   13891             : }
   13892             : 
   13893             : /* Return the result of substituting ARGS into one template parameter
   13894             :    given by T. T Must be a TREE_LIST which TREE_VALUE is the template
   13895             :    parameter and which TREE_PURPOSE is the default argument of the
   13896             :    template parameter.  */
   13897             : 
   13898             : static tree
   13899    23993266 : tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
   13900             : {
   13901    23993266 :   tree default_value, parm_decl;
   13902             : 
   13903    23993266 :   if (args == NULL_TREE
   13904    23993266 :       || t == NULL_TREE
   13905    23993264 :       || t == error_mark_node)
   13906             :     return t;
   13907             : 
   13908    23993264 :   gcc_assert (TREE_CODE (t) == TREE_LIST);
   13909             : 
   13910    23993264 :   default_value = TREE_PURPOSE (t);
   13911    23993264 :   parm_decl = TREE_VALUE (t);
   13912             : 
   13913    23993264 :   parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
   13914    23993264 :   if (TREE_CODE (parm_decl) == PARM_DECL
   13915    23993264 :       && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
   13916          40 :     parm_decl = error_mark_node;
   13917    23993264 :   default_value = tsubst_template_arg (default_value, args,
   13918             :                                        complain, NULL_TREE);
   13919             : 
   13920    23993264 :   tree r = build_tree_list (default_value, parm_decl);
   13921    23993264 :   TEMPLATE_PARM_CONSTRAINTS (r) = TEMPLATE_PARM_CONSTRAINTS (t);
   13922    23993264 :   return r;
   13923             : }
   13924             : 
   13925             : /* Substitute in-place the TEMPLATE_PARM_CONSTRAINTS of each template
   13926             :    parameter in PARMS for sake of declaration matching.  */
   13927             : 
   13928             : static void
   13929      244651 : tsubst_each_template_parm_constraints (tree parms, tree args,
   13930             :                                        tsubst_flags_t complain)
   13931             : {
   13932      244651 :   ++processing_template_decl;
   13933      489302 :   for (; parms; parms = TREE_CHAIN (parms))
   13934             :     {
   13935      244651 :       tree level = TREE_VALUE (parms);
   13936      762083 :       for (tree parm : tree_vec_range (level))
   13937     1034864 :         TEMPLATE_PARM_CONSTRAINTS (parm)
   13938     1034864 :           = tsubst_constraint (TEMPLATE_PARM_CONSTRAINTS (parm), args,
   13939             :                                complain, NULL_TREE);
   13940             :     }
   13941      244651 :   --processing_template_decl;
   13942      244651 : }
   13943             : 
   13944             : /* Substitute the ARGS into the indicated aggregate (or enumeration)
   13945             :    type T.  If T is not an aggregate or enumeration type, it is
   13946             :    handled as if by tsubst.  IN_DECL is as for tsubst.  If
   13947             :    ENTERING_SCOPE is nonzero, T is the context for a template which
   13948             :    we are presently tsubst'ing.  Return the substituted value.  */
   13949             : 
   13950             : static tree
   13951   195922433 : tsubst_aggr_type (tree t,
   13952             :                   tree args,
   13953             :                   tsubst_flags_t complain,
   13954             :                   tree in_decl,
   13955             :                   int entering_scope)
   13956             : {
   13957   195922433 :   if (t == NULL_TREE)
   13958             :     return NULL_TREE;
   13959             : 
   13960             :   /* Handle typedefs via tsubst so that they get consistently reused.  */
   13961   195922433 :   if (typedef_variant_p (t))
   13962             :     {
   13963     4040889 :       t = tsubst (t, args, complain, in_decl);
   13964     4040889 :       if (t == error_mark_node)
   13965             :         return error_mark_node;
   13966             : 
   13967             :       /* The effect of entering_scope is that for a dependent specialization
   13968             :          A<T>, lookup_template_class prefers to return A's primary template
   13969             :          type instead of the implicit instantiation.  So when entering_scope,
   13970             :          we mirror this behavior by inspecting TYPE_CANONICAL appropriately,
   13971             :          taking advantage of the fact that lookup_template_class links the two
   13972             :          types by setting TYPE_CANONICAL of the latter to the former.  */
   13973     4040776 :       if (entering_scope
   13974     4040776 :           && CLASS_TYPE_P (t)
   13975     3993224 :           && dependent_type_p (t)
   13976     4268608 :           && TYPE_CANONICAL (t) == TREE_TYPE (TYPE_TI_TEMPLATE (t)))
   13977           4 :         t = TYPE_CANONICAL (t);
   13978             : 
   13979     4040776 :       return t;
   13980             :     }
   13981             : 
   13982   191881544 :   switch (TREE_CODE (t))
   13983             :     {
   13984   179973540 :       case RECORD_TYPE:
   13985   179973540 :       case ENUMERAL_TYPE:
   13986   179973540 :       case UNION_TYPE:
   13987   179973540 :         return tsubst_aggr_type_1 (t, args, complain, in_decl, entering_scope);
   13988             : 
   13989    11908004 :       default:
   13990    11908004 :         return tsubst (t, args, complain, in_decl);
   13991             :     }
   13992             : }
   13993             : 
   13994             : /* The part of tsubst_aggr_type that's shared with the RECORD_, UNION_
   13995             :    and ENUMERAL_TYPE cases of tsubst.  */
   13996             : 
   13997             : static tree
   13998   502010075 : tsubst_aggr_type_1 (tree t,
   13999             :                     tree args,
   14000             :                     tsubst_flags_t complain,
   14001             :                     tree in_decl,
   14002             :                     int entering_scope)
   14003             : {
   14004   502010075 :   if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
   14005             :     {
   14006   445180799 :       complain &= ~tf_qualifying_scope;
   14007             : 
   14008             :       /* Figure out what arguments are appropriate for the
   14009             :          type we are trying to find.  For example, given:
   14010             : 
   14011             :            template <class T> struct S;
   14012             :            template <class T, class U> void f(T, U) { S<U> su; }
   14013             : 
   14014             :          and supposing that we are instantiating f<int, double>,
   14015             :          then our ARGS will be {int, double}, but, when looking up
   14016             :          S we only want {double}.  */
   14017   890361598 :       tree argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
   14018             :                                           complain, in_decl);
   14019   445180761 :       if (argvec == error_mark_node)
   14020             :         return error_mark_node;
   14021             : 
   14022   445032827 :       tree r = lookup_template_class (t, argvec, in_decl, NULL_TREE,
   14023             :                                       entering_scope, complain);
   14024   445032767 :       return cp_build_qualified_type (r, cp_type_quals (t), complain);
   14025             :     }
   14026             :   else
   14027             :     /* This is not a template type, so there's nothing to do.  */
   14028    56829276 :     return t;
   14029             : }
   14030             : 
   14031             : /* Map from a FUNCTION_DECL to a vec of default argument instantiations,
   14032             :    indexed in reverse order of the parameters.  */
   14033             : 
   14034             : static GTY((cache)) hash_table<tree_vec_map_cache_hasher> *defarg_inst;
   14035             : 
   14036             : /* Return a reference to the vec* of defarg insts for FN.  */
   14037             : 
   14038             : static vec<tree,va_gc> *&
   14039      526676 : defarg_insts_for (tree fn)
   14040             : {
   14041      526676 :   if (!defarg_inst)
   14042        9886 :     defarg_inst = hash_table<tree_vec_map_cache_hasher>::create_ggc (13);
   14043      526676 :   tree_vec_map in = { { fn }, nullptr };
   14044      526676 :   tree_vec_map **slot
   14045      526676 :     = defarg_inst->find_slot_with_hash (&in, DECL_UID (fn), INSERT);
   14046      526676 :   if (!*slot)
   14047             :     {
   14048      330379 :       *slot = ggc_alloc<tree_vec_map> ();
   14049      330379 :       **slot = in;
   14050             :     }
   14051      526676 :   return (*slot)->to;
   14052             : }
   14053             : 
   14054             : /* Substitute into the default argument ARG (a default argument for
   14055             :    FN), which has the indicated TYPE.  */
   14056             : 
   14057             : tree
   14058      529660 : tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
   14059             :                          tsubst_flags_t complain)
   14060             : {
   14061      529660 :   int errs = errorcount + sorrycount;
   14062             : 
   14063             :   /* This can happen in invalid code.  */
   14064      529660 :   if (TREE_CODE (arg) == DEFERRED_PARSE)
   14065             :     return arg;
   14066             : 
   14067             :   /* Shortcut {}.  */
   14068        3499 :   if (BRACE_ENCLOSED_INITIALIZER_P (arg)
   14069      532644 :       && CONSTRUCTOR_NELTS (arg) == 0)
   14070             :     return arg;
   14071             : 
   14072      526676 :   tree parm = FUNCTION_FIRST_USER_PARM (fn);
   14073      526676 :   parm = chain_index (parmnum, parm);
   14074      526676 :   tree parmtype = TREE_TYPE (parm);
   14075      526676 :   if (DECL_BY_REFERENCE (parm))
   14076           3 :     parmtype = TREE_TYPE (parmtype);
   14077      526676 :   if (parmtype == error_mark_node)
   14078             :     return error_mark_node;
   14079             : 
   14080      526676 :   gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
   14081             : 
   14082             :   /* Remember the location of the pointer to the vec rather than the location
   14083             :      of the particular element, in case the vec grows in tsubst_expr.  */
   14084      526676 :   vec<tree,va_gc> *&defs = defarg_insts_for (fn);
   14085             :   /* Index in reverse order to avoid allocating space for initial parameters
   14086             :      that don't have default arguments.  */
   14087      526676 :   unsigned ridx = list_length (parm);
   14088      722973 :   if (vec_safe_length (defs) < ridx)
   14089      330394 :     vec_safe_grow_cleared (defs, ridx);
   14090      196282 :   else if (tree inst = (*defs)[ridx - 1])
   14091             :     return inst;
   14092             : 
   14093             :   /* This default argument came from a template.  Instantiate the
   14094             :      default argument here, not in tsubst.  In the case of
   14095             :      something like:
   14096             : 
   14097             :        template <class T>
   14098             :        struct S {
   14099             :          static T t();
   14100             :          void f(T = t());
   14101             :        };
   14102             : 
   14103             :      we must be careful to do name lookup in the scope of S<T>,
   14104             :      rather than in the current class.  */
   14105      384238 :   push_to_top_level ();
   14106      384238 :   push_access_scope (fn);
   14107      384238 :   push_deferring_access_checks (dk_no_deferred);
   14108             :   /* So in_immediate_context knows this is a default argument.  */
   14109      384238 :   begin_scope (sk_function_parms, fn);
   14110      384238 :   start_lambda_scope (parm);
   14111             : 
   14112             :   /* The default argument expression may cause implicitly defined
   14113             :      member functions to be synthesized, which will result in garbage
   14114             :      collection.  We must treat this situation as if we were within
   14115             :      the body of function so as to avoid collecting live data on the
   14116             :      stack.  */
   14117      384238 :   ++function_depth;
   14118      384238 :   arg = tsubst_expr (arg, DECL_TI_ARGS (fn), complain, NULL_TREE);
   14119      384238 :   --function_depth;
   14120             : 
   14121      384238 :   finish_lambda_scope ();
   14122             : 
   14123             :   /* Make sure the default argument is reasonable.  */
   14124      384238 :   arg = check_default_argument (type, arg, complain);
   14125             : 
   14126      384238 :   if (errorcount+sorrycount > errs
   14127          18 :       && (complain & tf_warning_or_error))
   14128          18 :     inform (input_location,
   14129             :             "  when instantiating default argument for call to %qD", fn);
   14130             : 
   14131      384238 :   leave_scope ();
   14132      384238 :   pop_deferring_access_checks ();
   14133      384238 :   pop_access_scope (fn);
   14134      384238 :   pop_from_top_level ();
   14135             : 
   14136      384238 :   if (arg != error_mark_node && !cp_unevaluated_operand)
   14137      273058 :     (*defs)[ridx - 1] = arg;
   14138             : 
   14139             :   return arg;
   14140             : }
   14141             : 
   14142             : /* Substitute into all the default arguments for FN.  */
   14143             : 
   14144             : static void
   14145      495218 : tsubst_default_arguments (tree fn, tsubst_flags_t complain)
   14146             : {
   14147      495218 :   tree arg;
   14148      495218 :   tree tmpl_args;
   14149             : 
   14150      495218 :   tmpl_args = DECL_TI_ARGS (fn);
   14151             : 
   14152             :   /* If this function is not yet instantiated, we certainly don't need
   14153             :      its default arguments.  */
   14154      495218 :   if (uses_template_parms (tmpl_args))
   14155             :     return;
   14156             :   /* Don't do this again for clones.  */
   14157      495218 :   if (DECL_CLONED_FUNCTION_P (fn))
   14158             :     return;
   14159             : 
   14160      495218 :   int i = 0;
   14161      495218 :   for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
   14162     1995709 :        arg;
   14163     1500491 :        arg = TREE_CHAIN (arg), ++i)
   14164     1500491 :     if (TREE_PURPOSE (arg))
   14165          14 :       TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
   14166          14 :                                                     TREE_VALUE (arg),
   14167          14 :                                                     TREE_PURPOSE (arg),
   14168             :                                                     complain);
   14169             : }
   14170             : 
   14171             : /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier.  */
   14172             : static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
   14173             : 
   14174             : /* Store a pair to EXPLICIT_SPECIFIER_MAP.  */
   14175             : 
   14176             : void
   14177       22693 : store_explicit_specifier (tree v, tree t)
   14178             : {
   14179       22693 :   if (!explicit_specifier_map)
   14180        1425 :     explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
   14181       22693 :   DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
   14182       22693 :   explicit_specifier_map->put (v, t);
   14183       22693 : }
   14184             : 
   14185             : /* Lookup an element in EXPLICIT_SPECIFIER_MAP.  */
   14186             : 
   14187             : tree
   14188       15257 : lookup_explicit_specifier (tree v)
   14189             : {
   14190        2220 :   return *explicit_specifier_map->get (v);
   14191             : }
   14192             : 
   14193             : /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
   14194             :    FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
   14195             :    are ARG_TYPES, and exception specification is RAISES, and otherwise is
   14196             :    identical to T.  */
   14197             : 
   14198             : static tree
   14199    84391076 : rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
   14200             :                                  tree raises, tsubst_flags_t complain)
   14201             : {
   14202    84391076 :   gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
   14203             : 
   14204    84391076 :   tree new_type;
   14205    84391076 :   if (TREE_CODE (t) == FUNCTION_TYPE)
   14206             :     {
   14207    28124271 :       new_type = build_function_type (return_type, arg_types);
   14208    28124271 :       new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
   14209             :     }
   14210             :   else
   14211             :     {
   14212    56266805 :       tree r = TREE_TYPE (TREE_VALUE (arg_types));
   14213             :       /* Don't pick up extra function qualifiers from the basetype.  */
   14214    56266805 :       r = cp_build_qualified_type (r, type_memfn_quals (t), complain);
   14215   112533429 :       if (! MAYBE_CLASS_TYPE_P (r))
   14216             :         {
   14217             :           /* [temp.deduct]
   14218             : 
   14219             :              Type deduction may fail for any of the following
   14220             :              reasons:
   14221             : 
   14222             :              -- Attempting to create "pointer to member of T" when T
   14223             :              is not a class type.  */
   14224           0 :           if (complain & tf_error)
   14225           0 :             error ("creating pointer to member function of non-class type %qT",
   14226             :                    r);
   14227           0 :           return error_mark_node;
   14228             :         }
   14229             : 
   14230    56266805 :       new_type = build_method_type_directly (r, return_type,
   14231    56266805 :                                              TREE_CHAIN (arg_types));
   14232             :     }
   14233    84391076 :   new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
   14234             : 
   14235    84391076 :   cp_ref_qualifier rqual = type_memfn_rqual (t);
   14236    84391076 :   bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
   14237    84391076 :   return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
   14238             : }
   14239             : 
   14240             : /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
   14241             :    each of its formal parameters.  If there is a disagreement then rebuild
   14242             :    DECL's function type according to its formal parameter types, as part of a
   14243             :    resolution for Core issues 1001/1322.  */
   14244             : 
   14245             : static void
   14246    74194164 : maybe_rebuild_function_decl_type (tree decl)
   14247             : {
   14248    74194164 :   bool function_type_needs_rebuilding = false;
   14249    74194164 :   if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
   14250             :     {
   14251    54483082 :       tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
   14252   206609042 :       while (parm_type_list && parm_type_list != void_list_node)
   14253             :         {
   14254    97642966 :           tree parm_type = TREE_VALUE (parm_type_list);
   14255    97642966 :           tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
   14256    97642966 :           if (!same_type_p (parm_type, formal_parm_type_unqual))
   14257             :             {
   14258             :               function_type_needs_rebuilding = true;
   14259             :               break;
   14260             :             }
   14261             : 
   14262    97642878 :           parm_list = DECL_CHAIN (parm_list);
   14263    97642878 :           parm_type_list = TREE_CHAIN (parm_type_list);
   14264             :         }
   14265             :     }
   14266             : 
   14267    54483082 :   if (!function_type_needs_rebuilding)
   14268    74194076 :     return;
   14269             : 
   14270          88 :   const tree fntype = TREE_TYPE (decl);
   14271          88 :   tree parm_list = DECL_ARGUMENTS (decl);
   14272          88 :   tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
   14273          88 :   tree new_parm_type_list = NULL_TREE;
   14274          88 :   tree *q = &new_parm_type_list;
   14275         102 :   for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
   14276             :     {
   14277          14 :       *q = copy_node (old_parm_type_list);
   14278          14 :       parm_list = DECL_CHAIN (parm_list);
   14279          14 :       old_parm_type_list = TREE_CHAIN (old_parm_type_list);
   14280          14 :       q = &TREE_CHAIN (*q);
   14281             :     }
   14282         180 :   while (old_parm_type_list && old_parm_type_list != void_list_node)
   14283             :     {
   14284          92 :       *q = copy_node (old_parm_type_list);
   14285          92 :       tree *new_parm_type = &TREE_VALUE (*q);
   14286          92 :       tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
   14287          92 :       if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
   14288          88 :         *new_parm_type = formal_parm_type_unqual;
   14289             : 
   14290          92 :       parm_list = DECL_CHAIN (parm_list);
   14291          92 :       old_parm_type_list = TREE_CHAIN (old_parm_type_list);
   14292          92 :       q = &TREE_CHAIN (*q);
   14293             :     }
   14294          88 :   if (old_parm_type_list == void_list_node)
   14295          88 :     *q = void_list_node;
   14296             : 
   14297          88 :   TREE_TYPE (decl)
   14298         176 :     = rebuild_function_or_method_type (fntype,
   14299          88 :                                        TREE_TYPE (fntype), new_parm_type_list,
   14300          88 :                                        TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
   14301             : }
   14302             : 
   14303             : /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL.  */
   14304             : 
   14305             : static tree
   14306    75948484 : tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
   14307             :                       tree lambda_fntype)
   14308             : {
   14309    75948484 :   tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
   14310    75948484 :   hashval_t hash = 0;
   14311    75948484 :   tree in_decl = t;
   14312             : 
   14313             :   /* Nobody should be tsubst'ing into non-template functions.  */
   14314    75948484 :   gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
   14315             :               || DECL_LOCAL_DECL_P (t));
   14316             : 
   14317    75948484 :   if (DECL_LOCAL_DECL_P (t))
   14318             :     {
   14319         309 :       if (tree spec = retrieve_local_specialization (t))
   14320             :         return spec;
   14321             :     }
   14322    75948175 :   else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
   14323             :     {
   14324             :       /* If T is not dependent, just return it.  */
   14325    75930098 :       if (!uses_template_parms (DECL_TI_ARGS (t))
   14326    75930104 :           && !LAMBDA_FUNCTION_P (t))
   14327             :         return t;
   14328             : 
   14329             :       /* A non-templated friend doesn't get DECL_TEMPLATE_INFO.  */
   14330    75728472 :       if (non_templated_friend_p (t))
   14331         141 :         goto friend_case;
   14332             : 
   14333             :       /* Calculate the most general template of which R is a
   14334             :          specialization.  */
   14335    75728331 :       gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
   14336             : 
   14337             :       /* We're substituting a lambda function under tsubst_lambda_expr but not
   14338             :          directly from it; find the matching function we're already inside.
   14339             :          But don't do this if T is a generic lambda with a single level of
   14340             :          template parms, as in that case we're doing a normal instantiation. */
   14341    76100533 :       if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
   14342    75740900 :           && (!generic_lambda_fn_p (t)
   14343       12545 :               || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
   14344          54 :         return enclosing_instantiation_of (t);
   14345             : 
   14346             :       /* Calculate the complete set of arguments used to
   14347             :          specialize R.  */
   14348    75728277 :       argvec = tsubst_template_args (DECL_TI_ARGS
   14349             :                                      (DECL_TEMPLATE_RESULT
   14350             :                                       (DECL_TI_TEMPLATE (t))),
   14351             :                                      args, complain, in_decl);
   14352    75728277 :       if (argvec == error_mark_node)
   14353             :         return error_mark_node;
   14354             : 
   14355             :       /* Check to see if we already have this specialization.  */
   14356    75728276 :       if (!lambda_fntype)
   14357             :         {
   14358    75689432 :           hash = spec_hasher::hash (gen_tmpl, argvec);
   14359    75689432 :           if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
   14360             :             /* The spec for these args might be a partial instantiation of the
   14361             :                template, but here what we want is the FUNCTION_DECL.  */
   14362      321865 :             return STRIP_TEMPLATE (spec);
   14363             :         }
   14364             :     }
   14365             :   else
   14366             :     {
   14367             :       /* This special case arises when we have something like this:
   14368             : 
   14369             :          template <class T> struct S {
   14370             :            friend void f<int>(int, double);
   14371             :          };
   14372             : 
   14373             :          Here, the DECL_TI_TEMPLATE for the friend declaration
   14374             :          will be an IDENTIFIER_NODE.  We are being called from
   14375             :          tsubst_friend_function, and we want only to create a
   14376             :          new decl (R) with appropriate types so that we can call
   14377             :          determine_specialization.  */
   14378       18077 :     friend_case:
   14379             :       gen_tmpl = NULL_TREE;
   14380             :       argvec = NULL_TREE;
   14381             :     }
   14382             : 
   14383    75424938 :   tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
   14384    75424938 :                   : NULL_TREE);
   14385    75424938 :   tree ctx = closure ? closure : DECL_CONTEXT (t);
   14386    75424938 :   bool member = ctx && TYPE_P (ctx);
   14387             : 
   14388             :   /* If this is a static lambda, remove the 'this' pointer added in
   14389             :      tsubst_lambda_expr now that we know the closure type.  */
   14390    75424938 :   if (lambda_fntype && DECL_STATIC_FUNCTION_P (t))
   14391           2 :     lambda_fntype = static_fn_type (lambda_fntype);
   14392             : 
   14393    75424938 :   if (member && !closure)
   14394    62638694 :     ctx = tsubst_aggr_type (ctx, args,
   14395             :                             complain, t, /*entering_scope=*/1);
   14396             : 
   14397    75424938 :   tree type = (lambda_fntype ? lambda_fntype
   14398    75386094 :                : tsubst (TREE_TYPE (t), args,
   14399    75416802 :                          complain | tf_fndecl_type, in_decl));
   14400    75416802 :   if (type == error_mark_node)
   14401             :     return error_mark_node;
   14402             : 
   14403             :   /* If we hit excessive deduction depth, the type is bogus even if
   14404             :      it isn't error_mark_node, so don't build a decl.  */
   14405    74194171 :   if (excessive_deduction_depth)
   14406             :     return error_mark_node;
   14407             : 
   14408             :   /* We do NOT check for matching decls pushed separately at this
   14409             :      point, as they may not represent instantiations of this
   14410             :      template, and in any case are considered separate under the
   14411             :      discrete model.  */
   14412    74194171 :   tree r = copy_decl (t);
   14413    74194171 :   DECL_USE_TEMPLATE (r) = 0;
   14414    74194171 :   TREE_TYPE (r) = type;
   14415             :   /* Clear out the mangled name and RTL for the instantiation.  */
   14416    74194171 :   SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
   14417    74194171 :   SET_DECL_RTL (r, NULL);
   14418             :   /* Leave DECL_INITIAL set on deleted instantiations.  */
   14419    74194171 :   if (!DECL_DELETED_FN (r))
   14420    73141419 :     DECL_INITIAL (r) = NULL_TREE;
   14421    74194171 :   DECL_CONTEXT (r) = ctx;
   14422    74194171 :   set_instantiating_module (r);
   14423             : 
   14424             :   /* Handle explicit(dependent-expr).  */
   14425    74194171 :   if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
   14426             :     {
   14427       13037 :       tree spec = lookup_explicit_specifier (t);
   14428       13037 :       spec = tsubst_copy_and_build (spec, args, complain, in_decl);
   14429       13037 :       spec = build_explicit_specifier (spec, complain);
   14430       13037 :       if (spec == error_mark_node)
   14431             :         return error_mark_node;
   14432       13030 :       if (instantiation_dependent_expression_p (spec))
   14433        7719 :         store_explicit_specifier (r, spec);
   14434             :       else
   14435             :         {
   14436        5311 :           DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
   14437        5311 :           DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r) = false;
   14438             :         }
   14439             :     }
   14440             : 
   14441             :   /* OpenMP UDRs have the only argument a reference to the declared
   14442             :      type.  We want to diagnose if the declared type is a reference,
   14443             :      which is invalid, but as references to references are usually
   14444             :      quietly merged, diagnose it here.  */
   14445    74194164 :   if (DECL_OMP_DECLARE_REDUCTION_P (t))
   14446             :     {
   14447         338 :       tree argtype
   14448         338 :         = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
   14449         338 :       argtype = tsubst (argtype, args, complain, in_decl);
   14450         338 :       if (TYPE_REF_P (argtype))
   14451           8 :         error_at (DECL_SOURCE_LOCATION (t),
   14452             :                   "reference type %qT in "
   14453             :                   "%<#pragma omp declare reduction%>", argtype);
   14454         338 :       if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
   14455         270 :         DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
   14456             :                                           argtype);
   14457             :     }
   14458             : 
   14459    74194164 :   if (member && DECL_CONV_FN_P (r))
   14460             :     /* Type-conversion operator.  Reconstruct the name, in
   14461             :        case it's the name of one of the template's parameters.  */
   14462      498547 :     DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
   14463             : 
   14464    74194164 :   tree parms = DECL_ARGUMENTS (t);
   14465    74194164 :   if (closure && !DECL_STATIC_FUNCTION_P (t))
   14466       38842 :     parms = DECL_CHAIN (parms);
   14467    74194164 :   parms = tsubst (parms, args, complain, t);
   14468   228019949 :   for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
   14469   153825785 :     DECL_CONTEXT (parm) = r;
   14470    74194164 :   if (closure && !DECL_STATIC_FUNCTION_P (t))
   14471             :     {
   14472       38842 :       tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
   14473       38842 :       DECL_NAME (tparm) = closure_identifier;
   14474       38842 :       DECL_CHAIN (tparm) = parms;
   14475       38842 :       parms = tparm;
   14476             :     }
   14477    74194164 :   DECL_ARGUMENTS (r) = parms;
   14478    74194164 :   DECL_RESULT (r) = NULL_TREE;
   14479             : 
   14480    74194164 :   maybe_rebuild_function_decl_type (r);
   14481             : 
   14482    74194164 :   TREE_STATIC (r) = 0;
   14483    74194164 :   TREE_PUBLIC (r) = TREE_PUBLIC (t);
   14484    74194164 :   DECL_EXTERNAL (r) = 1;
   14485             :   /* If this is an instantiation of a function with internal
   14486             :      linkage, we already know what object file linkage will be
   14487             :      assigned to the instantiation.  */
   14488    74194164 :   DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
   14489    74194164 :   DECL_DEFER_OUTPUT (r) = 0;
   14490    74194164 :   DECL_CHAIN (r) = NULL_TREE;
   14491    74194164 :   DECL_PENDING_INLINE_INFO (r) = 0;
   14492    74194164 :   DECL_PENDING_INLINE_P (r) = 0;
   14493    74194164 :   DECL_SAVED_TREE (r) = NULL_TREE;
   14494    74194164 :   DECL_STRUCT_FUNCTION (r) = NULL;
   14495    74194164 :   TREE_USED (r) = 0;
   14496             :   /* We'll re-clone as appropriate in instantiate_template.  */
   14497    74194164 :   DECL_CLONED_FUNCTION (r) = NULL_TREE;
   14498             : 
   14499             :   /* If we aren't complaining now, return on error before we register
   14500             :      the specialization so that we'll complain eventually.  */
   14501    74194164 :   if ((complain & tf_error) == 0
   14502    12816894 :       && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
   14503    74901782 :       && !grok_op_properties (r, /*complain=*/false))
   14504           4 :     return error_mark_node;
   14505             : 
   14506             :   /* Associate the constraints directly with the instantiation. We
   14507             :      don't substitute through the constraints; that's only done when
   14508             :      they are checked.  */
   14509    74194160 :   if (tree ci = get_constraints (t))
   14510      103703 :     set_constraints (r, ci);
   14511             : 
   14512   146098572 :   if (DECL_FRIEND_CONTEXT (t))
   14513      812584 :     SET_DECL_FRIEND_CONTEXT (r,
   14514             :                              tsubst (DECL_FRIEND_CONTEXT (t),
   14515             :                                      args, complain, in_decl));
   14516             : 
   14517    74194160 :   if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
   14518             :                                        args, complain, in_decl))
   14519           4 :     return error_mark_node;
   14520             : 
   14521             :   /* Set up the DECL_TEMPLATE_INFO for R.  There's no need to do
   14522             :      this in the special friend case mentioned above where
   14523             :      GEN_TMPL is NULL.  */
   14524    74194156 :   if (gen_tmpl && !closure)
   14525             :     {
   14526    74136789 :       DECL_TEMPLATE_INFO (r)
   14527    74136789 :         = build_template_info (gen_tmpl, argvec);
   14528    74136789 :       SET_DECL_IMPLICIT_INSTANTIATION (r);
   14529             : 
   14530    74136789 :       tree new_r
   14531    74136789 :         = register_specialization (r, gen_tmpl, argvec, false, hash);
   14532    74136789 :       if (new_r != r)
   14533             :         /* We instantiated this while substituting into
   14534             :            the type earlier (template/friend54.C).  */
   14535             :         return new_r;
   14536             : 
   14537             :       /* We're not supposed to instantiate default arguments
   14538             :          until they are called, for a template.  But, for a
   14539             :          declaration like:
   14540             : 
   14541             :          template <class T> void f ()
   14542             :          { extern void g(int i = T()); }
   14543             : 
   14544             :          we should do the substitution when the template is
   14545             :          instantiated.  We handle the member function case in
   14546             :          instantiate_class_template since the default arguments
   14547             :          might refer to other members of the class.  */
   14548    74136789 :       if (!member
   14549    11632795 :           && !PRIMARY_TEMPLATE_P (gen_tmpl)
   14550    74632007 :           && !uses_template_parms (argvec))
   14551      495218 :         tsubst_default_arguments (r, complain);
   14552             :     }
   14553       57367 :   else if (DECL_LOCAL_DECL_P (r))
   14554             :     {
   14555         309 :       if (!cp_unevaluated_operand)
   14556         309 :         register_local_specialization (r, t);
   14557             :     }
   14558             :   else
   14559       57058 :     DECL_TEMPLATE_INFO (r) = NULL_TREE;
   14560             : 
   14561             :   /* Copy the list of befriending classes.  */
   14562    74194156 :   for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
   14563    74833236 :        *friends;
   14564      639080 :        friends = &TREE_CHAIN (*friends))
   14565             :     {
   14566      639080 :       *friends = copy_node (*friends);
   14567      639080 :       TREE_VALUE (*friends)
   14568     1278160 :         = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
   14569             :     }
   14570             : 
   14571   148388312 :   if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
   14572             :     {
   14573    14973103 :       maybe_retrofit_in_chrg (r);
   14574    29946206 :       if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
   14575           8 :         return error_mark_node;
   14576             :       /* If this is an instantiation of a member template, clone it.
   14577             :          If it isn't, that'll be handled by
   14578             :          clone_constructors_and_destructors.  */
   14579    29946185 :       if (gen_tmpl && PRIMARY_TEMPLATE_P (gen_tmpl))
   14580     5264850 :         clone_cdtor (r, /*update_methods=*/false);
   14581             :     }
   14582    59221053 :   else if ((complain & tf_error) != 0
   14583    47045006 :            && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
   14584    70214328 :            && !grok_op_properties (r, /*complain=*/true))
   14585           4 :     return error_mark_node;
   14586             : 
   14587             :   /* Possibly limit visibility based on template args.  */
   14588    74194144 :   DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
   14589    74194144 :   if (DECL_VISIBILITY_SPECIFIED (t))
   14590             :     {
   14591    61059140 :       DECL_VISIBILITY_SPECIFIED (r) = 0;
   14592    61059140 :       DECL_ATTRIBUTES (r)
   14593   122118280 :         = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
   14594             :     }
   14595    74194144 :   determine_visibility (r);
   14596    76452270 :   if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
   14597    74226395 :       && !processing_template_decl)
   14598       32251 :     defaulted_late_check (r);
   14599             : 
   14600    74194144 :   if (flag_openmp)
   14601       37393 :     if (tree attr = lookup_attribute ("omp declare variant base",
   14602       37393 :                                       DECL_ATTRIBUTES (r)))
   14603          60 :       omp_declare_variant_finalize (r, attr);
   14604             : 
   14605    74194144 :   return r;
   14606             : }
   14607             : 
   14608             : /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL.  */
   14609             : 
   14610             : static tree
   14611    14950271 : tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
   14612             :                       tree lambda_fntype, tree lambda_tparms)
   14613             : {
   14614             :   /* We can get here when processing a member function template,
   14615             :      member class template, or template template parameter.  */
   14616    14950271 :   tree decl = DECL_TEMPLATE_RESULT (t);
   14617    14950271 :   tree in_decl = t;
   14618    14950271 :   tree spec;
   14619    14950271 :   tree tmpl_args;
   14620    14950271 :   tree full_args;
   14621    14950271 :   tree r;
   14622    14950271 :   hashval_t hash = 0;
   14623             : 
   14624    14950271 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   14625             :     {
   14626             :       /* Template template parameter is treated here.  */
   14627       13005 :       tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   14628       13005 :       if (new_type == error_mark_node)
   14629             :         r = error_mark_node;
   14630             :       /* If we get a real template back, return it.  This can happen in
   14631             :          the context of most_specialized_partial_spec.  */
   14632       13005 :       else if (TREE_CODE (new_type) == TEMPLATE_DECL)
   14633             :         r = new_type;
   14634             :       else
   14635             :         /* The new TEMPLATE_DECL was built in
   14636             :            reduce_template_parm_level.  */
   14637       26010 :         r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
   14638       13005 :       return r;
   14639             :     }
   14640             : 
   14641    14937266 :   if (!lambda_fntype)
   14642             :     {
   14643             :       /* We might already have an instance of this template.
   14644             :          The ARGS are for the surrounding class type, so the
   14645             :          full args contain the tsubst'd args for the context,
   14646             :          plus the innermost args from the template decl.  */
   14647    14933995 :       tmpl_args = DECL_CLASS_TEMPLATE_P (t)
   14648     1027390 :         ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
   14649    13906605 :         : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
   14650             :       /* Because this is a template, the arguments will still be
   14651             :          dependent, even after substitution.  If
   14652             :          PROCESSING_TEMPLATE_DECL is not set, the dependency
   14653             :          predicates will short-circuit.  */
   14654    14933995 :       ++processing_template_decl;
   14655    14933995 :       full_args = tsubst_template_args (tmpl_args, args,
   14656             :                                         complain, in_decl);
   14657    14933995 :       --processing_template_decl;
   14658    14933995 :       if (full_args == error_mark_node)
   14659             :         return error_mark_node;
   14660             : 
   14661             :       /* If this is a default template template argument,
   14662             :          tsubst might not have changed anything.  */
   14663    14933973 :       if (full_args == tmpl_args)
   14664             :         return t;
   14665             : 
   14666    14933973 :       hash = spec_hasher::hash (t, full_args);
   14667    14933973 :       spec = retrieve_specialization (t, full_args, hash);
   14668    14933973 :       if (spec != NULL_TREE)
   14669             :         {
   14670      848015 :           if (TYPE_P (spec))
   14671             :             /* Type partial instantiations are stored as the type by
   14672             :                lookup_template_class_1, not here as the template.  */
   14673      448580 :             spec = CLASSTYPE_TI_TEMPLATE (spec);
   14674      848015 :           return spec;
   14675             :         }
   14676             :     }
   14677             : 
   14678             :   /* Make a new template decl.  It will be similar to the
   14679             :      original, but will record the current template arguments.
   14680             :      We also create a new function declaration, which is just
   14681             :      like the old one, but points to this new template, rather
   14682             :      than the old one.  */
   14683    14089229 :   r = copy_decl (t);
   14684    14089229 :   gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
   14685    14089229 :   DECL_CHAIN (r) = NULL_TREE;
   14686             : 
   14687             :   // Build new template info linking to the original template decl.
   14688    14089229 :   if (!lambda_fntype)
   14689             :     {
   14690    14085958 :       DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
   14691    14085958 :       SET_DECL_IMPLICIT_INSTANTIATION (r);
   14692             :     }
   14693             :   else
   14694        3271 :     DECL_TEMPLATE_INFO (r) = NULL_TREE;
   14695             : 
   14696             :   /* The template parameters for this new template are all the
   14697             :      template parameters for the old template, except the
   14698             :      outermost level of parameters.  */
   14699    14089229 :   auto tparm_guard = make_temp_override (current_template_parms);
   14700    28178458 :   DECL_TEMPLATE_PARMS (r)
   14701    28178458 :     = current_template_parms
   14702    14089229 :     = (lambda_tparms
   14703    14089229 :        ? lambda_tparms
   14704    14085958 :        : tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
   14705             :                                 complain));
   14706             : 
   14707    14089229 :   bool class_p = false;
   14708    14089229 :   tree inner = decl;
   14709    14089229 :   ++processing_template_decl;
   14710    14089229 :   if (TREE_CODE (inner) == FUNCTION_DECL)
   14711    11933699 :     inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
   14712             :   else
   14713             :     {
   14714     2155530 :       if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
   14715             :         {
   14716      578791 :           class_p = true;
   14717      578791 :           inner = TREE_TYPE (inner);
   14718             :         }
   14719     2155530 :       if (class_p)
   14720      578791 :         inner = tsubst_aggr_type (inner, args, complain,
   14721             :                                   in_decl, /*entering*/1);
   14722             :       else
   14723     1576739 :         inner = tsubst (inner, args, complain, in_decl);
   14724             :     }
   14725    14089229 :   --processing_template_decl;
   14726    14089229 :   if (inner == error_mark_node)
   14727             :     return error_mark_node;
   14728             : 
   14729    14089169 :   if (class_p)
   14730             :     {
   14731             :       /* For a partial specialization, we need to keep pointing to
   14732             :          the primary template.  */
   14733      578769 :       if (!DECL_TEMPLATE_SPECIALIZATION (t))
   14734             :         {
   14735      500513 :           CLASSTYPE_TI_TEMPLATE (inner) = r;
   14736      500513 :           CLASSTYPE_USE_TEMPLATE (inner) = 0;
   14737             :         }
   14738             : 
   14739      578769 :       DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
   14740      578769 :       inner = TYPE_MAIN_DECL (inner);
   14741             :     }
   14742    13510400 :   else if (lambda_fntype)
   14743             :     {
   14744        3270 :       tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
   14745        3270 :       DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
   14746             :     }
   14747             :   else
   14748             :     {
   14749    13507130 :       DECL_TI_TEMPLATE (inner) = r;
   14750    13507130 :       DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
   14751             :     }
   14752             : 
   14753    14089169 :   DECL_TEMPLATE_RESULT (r) = inner;
   14754    14089169 :   TREE_TYPE (r) = TREE_TYPE (inner);
   14755    14089169 :   DECL_CONTEXT (r) = DECL_CONTEXT (inner);
   14756             : 
   14757    14089169 :   if (modules_p ())
   14758             :     {
   14759             :       /* Propagate module information from the decl.  */
   14760       50683 :       DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
   14761       50683 :       if (DECL_LANG_SPECIFIC (inner))
   14762             :         /* If this is a constrained template, the above tsubst of
   14763             :            inner can find the unconstrained template, which may have
   14764             :            come from an import.  This is ok, because we don't
   14765             :            register this instantiation (see below).  */
   14766       50010 :         gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
   14767             :                              || (TEMPLATE_PARMS_CONSTRAINTS
   14768             :                                  (DECL_TEMPLATE_PARMS (t))));
   14769             :     }
   14770             : 
   14771    14089169 :   DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
   14772    14089169 :   DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
   14773             : 
   14774    14089169 :   if (PRIMARY_TEMPLATE_P (t))
   14775    14010855 :     DECL_PRIMARY_TEMPLATE (r) = r;
   14776             : 
   14777    14089169 :   if (TREE_CODE (decl) == FUNCTION_DECL && !lambda_fntype)
   14778             :     /* Record this non-type partial instantiation.  */
   14779    11930391 :     register_specialization (r, t,
   14780    11930391 :                              DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
   14781             :                              false, hash);
   14782             : 
   14783             :   return r;
   14784    14950271 : }
   14785             : 
   14786             : /* True if FN is the op() for a lambda in an uninstantiated template.  */
   14787             : 
   14788             : bool
   14789   594421946 : lambda_fn_in_template_p (tree fn)
   14790             : {
   14791   595797464 :   if (!fn || !LAMBDA_FUNCTION_P (fn))
   14792             :     return false;
   14793       82301 :   tree closure = DECL_CONTEXT (fn);
   14794       82301 :   return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
   14795             : }
   14796             : 
   14797             : /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
   14798             :    which the above is true.  */
   14799             : 
   14800             : bool
   14801    27560228 : regenerated_lambda_fn_p (tree fn)
   14802             : {
   14803    27800102 :   if (!fn || !LAMBDA_FUNCTION_P (fn))
   14804             :     return false;
   14805       38014 :   tree closure = DECL_CONTEXT (fn);
   14806       38014 :   tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
   14807       38014 :   return LAMBDA_EXPR_REGEN_INFO (lam) != NULL_TREE;
   14808             : }
   14809             : 
   14810             : /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
   14811             :    If T is not a regenerated LAMBDA_EXPR, return T.  */
   14812             : 
   14813             : tree
   14814       16267 : most_general_lambda (tree t)
   14815             : {
   14816       32609 :   while (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
   14817       16342 :     t = TI_TEMPLATE (ti);
   14818       16267 :   return t;
   14819             : }
   14820             : 
   14821             : /* Return the set of template arguments used to regenerate the lambda T
   14822             :    from its most general lambda.  */
   14823             : 
   14824             : tree
   14825       10471 : lambda_regenerating_args (tree t)
   14826             : {
   14827       20942 :   if (LAMBDA_FUNCTION_P (t))
   14828       10471 :     t = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t));
   14829       10471 :   gcc_assert (TREE_CODE (t) == LAMBDA_EXPR);
   14830       10471 :   if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
   14831       10471 :     return TI_ARGS (ti);
   14832             :   else
   14833             :     return NULL_TREE;
   14834             : }
   14835             : 
   14836             : /* We're instantiating a variable from template function TCTX.  Return the
   14837             :    corresponding current enclosing scope.  We can match them up using
   14838             :    DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
   14839             :    the DECL_SOURCE_LOCATION for a function instantiation is updated to match
   14840             :    the template definition in regenerate_decl_from_template.  */
   14841             : 
   14842             : static tree
   14843       72692 : enclosing_instantiation_of (tree tctx)
   14844             : {
   14845       72692 :   tree fn = current_function_decl;
   14846             : 
   14847             :   /* We shouldn't ever need to do this for other artificial functions.  */
   14848       72789 :   gcc_assert (!DECL_ARTIFICIAL (tctx) || LAMBDA_FUNCTION_P (tctx));
   14849             : 
   14850       72695 :   for (; fn; fn = decl_function_context (fn))
   14851       72695 :     if (DECL_SOURCE_LOCATION (fn) == DECL_SOURCE_LOCATION (tctx))
   14852       72692 :       return fn;
   14853           0 :   gcc_unreachable ();
   14854             : }
   14855             : 
   14856             : /* Substitute the ARGS into the T, which is a _DECL.  Return the
   14857             :    result of the substitution.  Issue error and warning messages under
   14858             :    control of COMPLAIN.  */
   14859             : 
   14860             : static tree
   14861   365902703 : tsubst_decl (tree t, tree args, tsubst_flags_t complain)
   14862             : {
   14863             : #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
   14864   365902703 :   location_t saved_loc;
   14865   365902703 :   tree r = NULL_TREE;
   14866   365902703 :   tree in_decl = t;
   14867   365902703 :   hashval_t hash = 0;
   14868             : 
   14869             :   /* Set the filename and linenumber to improve error-reporting.  */
   14870   365902703 :   saved_loc = input_location;
   14871   365902703 :   input_location = DECL_SOURCE_LOCATION (t);
   14872             : 
   14873   365902703 :   switch (TREE_CODE (t))
   14874             :     {
   14875    14947000 :     case TEMPLATE_DECL:
   14876    14947000 :       r = tsubst_template_decl (t, args, complain,
   14877             :                                 /*lambda_fntype=*/NULL_TREE,
   14878             :                                 /*lambda_tparms=*/NULL_TREE);
   14879    14947000 :       break;
   14880             : 
   14881    63979211 :     case FUNCTION_DECL:
   14882    63979211 :       r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
   14883    63971075 :       break;
   14884             : 
   14885   170219976 :     case PARM_DECL:
   14886   170219976 :       {
   14887   170219976 :         tree type = NULL_TREE;
   14888   170219976 :         int i, len = 1;
   14889   170219976 :         tree expanded_types = NULL_TREE;
   14890   170219976 :         tree prev_r = NULL_TREE;
   14891   170219976 :         tree first_r = NULL_TREE;
   14892             : 
   14893   170219976 :         if (DECL_PACK_P (t))
   14894             :           {
   14895             :             /* If there is a local specialization that isn't a
   14896             :                parameter pack, it means that we're doing a "simple"
   14897             :                substitution from inside tsubst_pack_expansion. Just
   14898             :                return the local specialization (which will be a single
   14899             :                parm).  */
   14900     2018052 :             tree spec = retrieve_local_specialization (t);
   14901     2018052 :             if (spec 
   14902          31 :                 && TREE_CODE (spec) == PARM_DECL
   14903     2018058 :                 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
   14904           0 :               RETURN (spec);
   14905             : 
   14906             :             /* Expand the TYPE_PACK_EXPANSION that provides the types for
   14907             :                the parameters in this function parameter pack.  */
   14908     2018052 :             expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
   14909             :                                                     complain, in_decl);
   14910     2018052 :             if (TREE_CODE (expanded_types) == TREE_VEC)
   14911             :               {
   14912      839666 :                 len = TREE_VEC_LENGTH (expanded_types);
   14913             : 
   14914             :                 /* Zero-length parameter packs are boring. Just substitute
   14915             :                    into the chain.  */
   14916      839666 :                 if (len == 0 && !cp_unevaluated_operand)
   14917      411150 :                   RETURN (tsubst (TREE_CHAIN (t), args, complain,
   14918             :                                   TREE_CHAIN (t)));
   14919             :               }
   14920             :             else
   14921             :               {
   14922             :                 /* All we did was update the type. Make a note of that.  */
   14923             :                 type = expanded_types;
   14924             :                 expanded_types = NULL_TREE;
   14925             :               }
   14926             :           }
   14927             : 
   14928             :         /* Loop through all of the parameters we'll build. When T is
   14929             :            a function parameter pack, LEN is the number of expanded
   14930             :            types in EXPANDED_TYPES; otherwise, LEN is 1.  */
   14931   169808826 :         r = NULL_TREE;
   14932   339726192 :         for (i = 0; i < len; ++i)
   14933             :           {
   14934   169917366 :             prev_r = r;
   14935   169917366 :             r = copy_node (t);
   14936   169917366 :             if (DECL_TEMPLATE_PARM_P (t))
   14937     2662051 :               SET_DECL_TEMPLATE_PARM_P (r);
   14938             : 
   14939   169917366 :             if (expanded_types)
   14940             :               /* We're on the Ith parameter of the function parameter
   14941             :                  pack.  */
   14942             :               {
   14943             :                 /* Get the Ith type.  */
   14944      537056 :                 type = TREE_VEC_ELT (expanded_types, i);
   14945             : 
   14946             :                 /* Rename the parameter to include the index.  */
   14947      537056 :                 DECL_NAME (r)
   14948     1074112 :                   = make_ith_pack_parameter_name (DECL_NAME (r), i);
   14949             :               }
   14950   169380310 :             else if (!type)
   14951             :               /* We're dealing with a normal parameter.  */
   14952   168201924 :               type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   14953             : 
   14954   169917366 :             type = type_decays_to (type);
   14955   169917366 :             TREE_TYPE (r) = type;
   14956   169917366 :             cp_apply_type_quals_to_decl (cp_type_quals (type), r);
   14957             : 
   14958   169917366 :             if (DECL_INITIAL (r))
   14959             :               {
   14960    27215124 :                 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
   14961    24553073 :                   DECL_INITIAL (r) = TREE_TYPE (r);
   14962             :                 else
   14963     2662051 :                   DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
   14964             :                                              complain, in_decl);
   14965             :               }
   14966             : 
   14967   169917366 :             DECL_CONTEXT (r) = NULL_TREE;
   14968             : 
   14969   169917366 :             if (!DECL_TEMPLATE_PARM_P (r))
   14970   167255315 :               DECL_ARG_TYPE (r) = type_passed_as (type);
   14971             : 
   14972   169917366 :             if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
   14973             :                                                  args, complain, in_decl))
   14974           0 :               return error_mark_node;
   14975             : 
   14976             :             /* Keep track of the first new parameter we
   14977             :                generate. That's what will be returned to the
   14978             :                caller.  */
   14979   169917366 :             if (!first_r)
   14980   169808515 :               first_r = r;
   14981             : 
   14982             :             /* Build a proper chain of parameters when substituting
   14983             :                into a function parameter pack.  */
   14984   169917366 :             if (prev_r)
   14985      108851 :               DECL_CHAIN (prev_r) = r;
   14986             :           }
   14987             : 
   14988             :         /* If cp_unevaluated_operand is set, we're just looking for a
   14989             :            single dummy parameter, so don't keep going.  */
   14990   169808826 :         if (DECL_CHAIN (t) && !cp_unevaluated_operand)
   14991    90714560 :           DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
   14992    90714560 :                                    complain, DECL_CHAIN (t));
   14993             : 
   14994             :         /* FIRST_R contains the start of the chain we've built.  */
   14995   169808826 :         r = first_r;
   14996             :       }
   14997   169808826 :       break;
   14998             : 
   14999     3866781 :     case FIELD_DECL:
   15000     3866781 :       {
   15001     3866781 :         tree type = NULL_TREE;
   15002     3866781 :         tree vec = NULL_TREE;
   15003     3866781 :         tree expanded_types = NULL_TREE;
   15004     3866781 :         int len = 1;
   15005             : 
   15006     3866781 :         if (PACK_EXPANSION_P (TREE_TYPE (t)))
   15007             :           {
   15008             :             /* This field is a lambda capture pack.  Return a TREE_VEC of
   15009             :                the expanded fields to instantiate_class_template_1.  */
   15010          58 :             expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
   15011             :                                                     complain, in_decl);
   15012          58 :             if (TREE_CODE (expanded_types) == TREE_VEC)
   15013             :               {
   15014          51 :                 len = TREE_VEC_LENGTH (expanded_types);
   15015          51 :                 vec = make_tree_vec (len);
   15016             :               }
   15017             :             else
   15018             :               {
   15019             :                 /* All we did was update the type. Make a note of that.  */
   15020             :                 type = expanded_types;
   15021             :                 expanded_types = NULL_TREE;
   15022             :               }
   15023             :           }
   15024             : 
   15025     7733398 :         for (int i = 0; i < len; ++i)
   15026             :           {
   15027     3866798 :             r = copy_decl (t);
   15028     3866798 :             if (expanded_types)
   15029             :               {
   15030          68 :                 type = TREE_VEC_ELT (expanded_types, i);
   15031          68 :                 DECL_NAME (r)
   15032         136 :                   = make_ith_pack_parameter_name (DECL_NAME (r), i);
   15033             :               }
   15034     3866730 :             else if (!type)
   15035     3866723 :               type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   15036             : 
   15037     3866790 :             if (type == error_mark_node)
   15038         173 :               RETURN (error_mark_node);
   15039     3866617 :             TREE_TYPE (r) = type;
   15040     3866617 :             cp_apply_type_quals_to_decl (cp_type_quals (type), r);
   15041             : 
   15042     3866617 :             if (DECL_C_BIT_FIELD (r))
   15043             :               /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
   15044             :                  number of bits.  */
   15045        5210 :               DECL_BIT_FIELD_REPRESENTATIVE (r)
   15046        5210 :                 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
   15047             :                                complain, in_decl);
   15048     3866617 :             if (DECL_INITIAL (t))
   15049             :               {
   15050             :                 /* Set up DECL_TEMPLATE_INFO so that we can get at the
   15051             :                    NSDMI in perform_member_init.  Still set DECL_INITIAL
   15052             :                    so that we know there is one.  */
   15053      154748 :                 DECL_INITIAL (r) = void_node;
   15054      154748 :                 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
   15055      154748 :                 retrofit_lang_decl (r);
   15056      154748 :                 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
   15057             :               }
   15058             :             /* We don't have to set DECL_CONTEXT here; it is set by
   15059             :                finish_member_declaration.  */
   15060     3866617 :             DECL_CHAIN (r) = NULL_TREE;
   15061             : 
   15062     3866617 :             if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
   15063             :                                                  args, complain, in_decl))
   15064           0 :               return error_mark_node;
   15065             : 
   15066     3866617 :             if (vec)
   15067          68 :               TREE_VEC_ELT (vec, i) = r;
   15068             :           }
   15069             : 
   15070     3866600 :         if (vec)
   15071          51 :           r = vec;
   15072             :       }
   15073             :       break;
   15074             : 
   15075      894369 :     case USING_DECL:
   15076             :       /* We reach here only for member using decls.  We also need to check
   15077             :          uses_template_parms because DECL_DEPENDENT_P is not set for a
   15078             :          using-declaration that designates a member of the current
   15079             :          instantiation (c++/53549).  */
   15080      894369 :       if (DECL_DEPENDENT_P (t)
   15081      894369 :           || uses_template_parms (USING_DECL_SCOPE (t)))
   15082             :         {
   15083             :           /* True iff this using-decl was written as a pack expansion
   15084             :              (and a pack appeared in its scope or name).  If a pack
   15085             :              appeared in both, we expand the packs separately and
   15086             :              manually merge them.  */
   15087      894199 :           bool variadic_p = false;
   15088             : 
   15089      894199 :           tree scope = USING_DECL_SCOPE (t);
   15090      894199 :           if (PACK_EXPANSION_P (scope))
   15091             :             {
   15092         194 :               scope = tsubst_pack_expansion (scope, args,
   15093             :                                              complain | tf_qualifying_scope,
   15094             :                                              in_decl);
   15095         194 :               variadic_p = true;
   15096             :             }
   15097             :           else
   15098      894005 :             scope = tsubst_scope (scope, args, complain, in_decl);
   15099             : 
   15100      894199 :           tree name = DECL_NAME (t);
   15101      894199 :           if (IDENTIFIER_CONV_OP_P (name)
   15102      894199 :               && PACK_EXPANSION_P (TREE_TYPE (name)))
   15103             :             {
   15104          22 :               name = tsubst_pack_expansion (TREE_TYPE (name), args,
   15105             :                                             complain, in_decl);
   15106          22 :               if (name == error_mark_node)
   15107             :                 {
   15108           0 :                   r = error_mark_node;
   15109           0 :                   break;
   15110             :                 }
   15111          56 :               for (tree& elt : tree_vec_range (name))
   15112          34 :                 elt = make_conv_op_name (elt);
   15113          22 :               variadic_p = true;
   15114             :             }
   15115             :           else
   15116      894177 :             name = tsubst_copy (name, args, complain, in_decl);
   15117             : 
   15118      894199 :           int len;
   15119      894199 :           if (!variadic_p)
   15120             :             len = 1;
   15121         200 :           else if (TREE_CODE (scope) == TREE_VEC
   15122         194 :                    && TREE_CODE (name) == TREE_VEC)
   15123             :             {
   15124          16 :               if (TREE_VEC_LENGTH (scope) != TREE_VEC_LENGTH (name))
   15125             :                 {
   15126           2 :                   error ("mismatched argument pack lengths (%d vs %d)",
   15127           2 :                          TREE_VEC_LENGTH (scope), TREE_VEC_LENGTH (name));
   15128           2 :                   r = error_mark_node;
   15129           2 :                   break;
   15130             :                 }
   15131             :               len = TREE_VEC_LENGTH (scope);
   15132             :             }
   15133         184 :           else if (TREE_CODE (scope) == TREE_VEC)
   15134         178 :             len = TREE_VEC_LENGTH (scope);
   15135             :           else /* TREE_CODE (name) == TREE_VEC  */
   15136           6 :             len = TREE_VEC_LENGTH (name);
   15137             : 
   15138      894197 :           r = make_tree_vec (len);
   15139     1788648 :           for (int i = 0; i < len; ++i)
   15140             :             {
   15141      894488 :               tree escope = (TREE_CODE (scope) == TREE_VEC
   15142      894488 :                              ? TREE_VEC_ELT (scope, i)
   15143      894488 :                              : scope);
   15144      894488 :               tree ename = (TREE_CODE (name) == TREE_VEC
   15145      894488 :                             ? TREE_VEC_ELT (name, i)
   15146      894488 :                             : name);
   15147      894488 :               tree elt = do_class_using_decl (escope, ename);
   15148      894488 :               if (!elt)
   15149             :                 {
   15150          37 :                   r = error_mark_node;
   15151          37 :                   break;
   15152             :                 }
   15153      894451 :               TREE_PROTECTED (elt) = TREE_PROTECTED (t);
   15154      894451 :               TREE_PRIVATE (elt) = TREE_PRIVATE (t);
   15155      894451 :               TREE_VEC_ELT (r, i) = elt;
   15156             :             }
   15157             : 
   15158      894197 :           if (!variadic_p && r != error_mark_node)
   15159      893962 :             r = TREE_VEC_ELT (r, 0);
   15160             :         }
   15161             :       else
   15162             :         {
   15163         170 :           r = copy_node (t);
   15164         170 :           DECL_CHAIN (r) = NULL_TREE;
   15165             :         }
   15166             :       break;
   15167             : 
   15168   111995366 :     case TYPE_DECL:
   15169   111995366 :     case VAR_DECL:
   15170   111995366 :       {
   15171   111995366 :         tree argvec = NULL_TREE;
   15172   111995366 :         tree gen_tmpl = NULL_TREE;
   15173   111995366 :         tree tmpl = NULL_TREE;
   15174   111995366 :         tree type = NULL_TREE;
   15175             : 
   15176   111995366 :         if (TREE_TYPE (t) == error_mark_node)
   15177           7 :           RETURN (error_mark_node);
   15178             : 
   15179   111995359 :         if (TREE_CODE (t) == TYPE_DECL
   15180   111995359 :             && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
   15181             :           {
   15182             :             /* If this is the canonical decl, we don't have to
   15183             :                mess with instantiations, and often we can't (for
   15184             :                typename, template type parms and such).  Note that
   15185             :                TYPE_NAME is not correct for the above test if
   15186             :                we've copied the type for a typedef.  */
   15187    22386172 :             type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   15188    22386112 :             if (type == error_mark_node)
   15189           8 :               RETURN (error_mark_node);
   15190    22386104 :             r = TYPE_NAME (type);
   15191    22386104 :             break;
   15192             :           }
   15193             : 
   15194             :         /* Check to see if we already have the specialization we
   15195             :            need.  */
   15196    89609187 :         tree spec = NULL_TREE;
   15197    89609187 :         bool local_p = false;
   15198    89609187 :         tree ctx = DECL_CONTEXT (t);
   15199    16216890 :         if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
   15200   105825982 :             && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
   15201             :           {
   15202    76743920 :             local_p = false;
   15203    76743920 :             if (DECL_CLASS_SCOPE_P (t))
   15204             :               {
   15205    56549539 :                 ctx = tsubst_aggr_type (ctx, args,
   15206             :                                         complain,
   15207             :                                         in_decl, /*entering_scope=*/1);
   15208    56549539 :                 if (DECL_SELF_REFERENCE_P (t))
   15209             :                   /* The context and type of an injected-class-name are
   15210             :                      the same, so we don't need to substitute both.  */
   15211             :                   type = ctx;
   15212             :                 /* If CTX is unchanged, then T is in fact the
   15213             :                    specialization we want.  That situation occurs when
   15214             :                    referencing a static data member within in its own
   15215             :                    class.  We can use pointer equality, rather than
   15216             :                    same_type_p, because DECL_CONTEXT is always
   15217             :                    canonical...  */
   15218    56549539 :                 if (ctx == DECL_CONTEXT (t)
   15219             :                     /* ... unless T is a member template; in which
   15220             :                        case our caller can be willing to create a
   15221             :                        specialization of that template represented
   15222             :                        by T.  */
   15223    56549539 :                     && !(DECL_TI_TEMPLATE (t)
   15224     9769484 :                          && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
   15225             :                   spec = t;
   15226             :               }
   15227             : 
   15228    76743920 :             if (!spec)
   15229             :               {
   15230    75964651 :                 tmpl = DECL_TI_TEMPLATE (t);
   15231    75964651 :                 gen_tmpl = most_general_template (tmpl);
   15232    75964651 :                 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
   15233    75964651 :                 if (argvec != error_mark_node
   15234    75964651 :                     && PRIMARY_TEMPLATE_P (gen_tmpl)
   15235   229910010 :                     && TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (argvec))
   15236             :                   /* We're fully specializing a template declaration, so
   15237             :                      we need to coerce the innermost arguments corresponding to
   15238             :                      the template.  */
   15239    30571020 :                   argvec = (coerce_template_parms
   15240    30571020 :                             (DECL_TEMPLATE_PARMS (gen_tmpl),
   15241             :                              argvec, tmpl, complain));
   15242    75964651 :                 if (argvec == error_mark_node)
   15243           0 :                   RETURN (error_mark_node);
   15244    75964651 :                 hash = spec_hasher::hash (gen_tmpl, argvec);
   15245    75964651 :                 spec = retrieve_specialization (gen_tmpl, argvec, hash);
   15246             :               }
   15247             :           }
   15248             :         else
   15249             :           {
   15250    12865267 :             if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
   15251             :               /* Subsequent calls to pushdecl will fill this in.  */
   15252             :               ctx = NULL_TREE;
   15253             :             /* A local variable.  */
   15254    12865267 :             local_p = true;
   15255             :             /* Unless this is a reference to a static variable from an
   15256             :                enclosing function, in which case we need to fill it in now.  */
   15257    12865267 :             if (TREE_STATIC (t))
   15258             :               {
   15259       72585 :                 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
   15260       72585 :                 if (fn != current_function_decl)
   15261    12865267 :                   ctx = fn;
   15262             :               }
   15263    12865267 :             spec = retrieve_local_specialization (t);
   15264             :           }
   15265             :         /* If we already have the specialization we need, there is
   15266             :            nothing more to do.  */
   15267    89609187 :         if (spec)
   15268             :           {
   15269     1857233 :             r = spec;
   15270     1857233 :             break;
   15271             :           }
   15272             : 
   15273             :         /* Create a new node for the specialization we need.  */
   15274    87751954 :         if (type == NULL_TREE)
   15275             :           {
   15276    68516311 :             if (is_typedef_decl (t))
   15277             :               type = DECL_ORIGINAL_TYPE (t);
   15278             :             else
   15279    14359814 :               type = TREE_TYPE (t);
   15280    68516311 :             if (VAR_P (t)
   15281    14359661 :                 && VAR_HAD_UNKNOWN_BOUND (t)
   15282    68516533 :                 && type != error_mark_node)
   15283         222 :               type = strip_array_domain (type);
   15284    68516311 :             tsubst_flags_t tcomplain = complain;
   15285    68516311 :             if (VAR_P (t))
   15286    14359661 :               tcomplain |= tf_tst_ok;
   15287    68516311 :             type = tsubst (type, args, tcomplain, in_decl);
   15288             :             /* Substituting the type might have recursively instantiated this
   15289             :                same alias (c++/86171).  */
   15290    55651357 :             if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
   15291   122008289 :                 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
   15292             :               {
   15293           9 :                 r = spec;
   15294           9 :                 break;
   15295             :               }
   15296             :           }
   15297    87751945 :         r = copy_decl (t);
   15298    87751945 :         if (VAR_P (r))
   15299             :           {
   15300    14359661 :             DECL_INITIALIZED_P (r) = 0;
   15301    14359661 :             DECL_TEMPLATE_INSTANTIATED (r) = 0;
   15302    14359661 :             if (type == error_mark_node)
   15303          78 :               RETURN (error_mark_node);
   15304    14359583 :             if (TREE_CODE (type) == FUNCTION_TYPE)
   15305             :               {
   15306             :                 /* It may seem that this case cannot occur, since:
   15307             : 
   15308             :                    typedef void f();
   15309             :                    void g() { f x; }
   15310             : 
   15311             :                    declares a function, not a variable.  However:
   15312             : 
   15313             :                    typedef void f();
   15314             :                    template <typename T> void g() { T t; }
   15315             :                    template void g<f>();
   15316             : 
   15317             :                    is an attempt to declare a variable with function
   15318             :                    type.  */
   15319           4 :                 error ("variable %qD has function type",
   15320             :                        /* R is not yet sufficiently initialized, so we
   15321             :                           just use its name.  */
   15322           4 :                        DECL_NAME (r));
   15323           4 :                 RETURN (error_mark_node);
   15324             :               }
   15325    14359579 :             type = complete_type (type);
   15326             :             /* Wait until cp_finish_decl to set this again, to handle
   15327             :                circular dependency (template/instantiate6.C). */
   15328    14359575 :             DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
   15329    14359575 :             type = check_var_type (DECL_NAME (r), type,
   15330    14359575 :                                    DECL_SOURCE_LOCATION (r));
   15331    14359575 :             if (DECL_HAS_VALUE_EXPR_P (t))
   15332             :               {
   15333       14541 :                 tree ve = DECL_VALUE_EXPR (t);
   15334             :                 /* If the DECL_VALUE_EXPR is converted to the declared type,
   15335             :                    preserve the identity so that gimplify_type_sizes works.  */
   15336       14541 :                 bool nop = (TREE_CODE (ve) == NOP_EXPR);
   15337       14541 :                 if (nop)
   15338           3 :                   ve = TREE_OPERAND (ve, 0);
   15339       14541 :                 ve = tsubst_expr (ve, args, complain, in_decl);
   15340       14541 :                 if (REFERENCE_REF_P (ve))
   15341             :                   {
   15342        9192 :                     gcc_assert (TYPE_REF_P (type));
   15343        9192 :                     ve = TREE_OPERAND (ve, 0);
   15344             :                   }
   15345       14541 :                 if (nop)
   15346           3 :                   ve = build_nop (type, ve);
   15347       14538 :                 else if (DECL_LANG_SPECIFIC (t)
   15348       14434 :                          && DECL_OMP_PRIVATIZED_MEMBER (t)
   15349         163 :                          && TREE_CODE (ve) == COMPONENT_REF
   15350         163 :                          && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
   15351       14701 :                          && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
   15352           8 :                   type = TREE_TYPE (ve);
   15353             :                 else
   15354       14530 :                   gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
   15355             :                                        == TYPE_MAIN_VARIANT (type));
   15356       14541 :                 SET_DECL_VALUE_EXPR (r, ve);
   15357             :               }
   15358    14359575 :             if (CP_DECL_THREAD_LOCAL_P (r)
   15359    14359575 :                 && !processing_template_decl)
   15360          28 :               set_decl_tls_model (r, decl_default_tls_model (r));
   15361             :           }
   15362    73392284 :         else if (DECL_SELF_REFERENCE_P (t))
   15363    19235643 :           SET_DECL_SELF_REFERENCE_P (r);
   15364    87751859 :         TREE_TYPE (r) = type;
   15365    87751859 :         cp_apply_type_quals_to_decl (cp_type_quals (type), r);
   15366    87751859 :         DECL_CONTEXT (r) = ctx;
   15367             :         /* Clear out the mangled name and RTL for the instantiation.  */
   15368    87751859 :         SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
   15369    87751859 :         if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
   15370    87751859 :           SET_DECL_RTL (r, NULL);
   15371    87751859 :         set_instantiating_module (r);
   15372             : 
   15373             :         /* The initializer must not be expanded until it is required;
   15374             :            see [temp.inst].  */
   15375    87751859 :         DECL_INITIAL (r) = NULL_TREE;
   15376    87751859 :         DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
   15377    87751859 :         if (VAR_P (r))
   15378             :           {
   15379    14359575 :             if (DECL_LANG_SPECIFIC (r))
   15380     2501167 :               SET_DECL_DEPENDENT_INIT_P (r, false);
   15381             : 
   15382    14359575 :             SET_DECL_MODE (r, VOIDmode);
   15383             : 
   15384             :             /* Possibly limit visibility based on template args.  */
   15385    14359575 :             DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
   15386    14359575 :             if (DECL_VISIBILITY_SPECIFIED (t))
   15387             :               {
   15388           0 :                 DECL_VISIBILITY_SPECIFIED (r) = 0;
   15389           0 :                 DECL_ATTRIBUTES (r)
   15390           0 :                   = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
   15391             :               }
   15392    14359575 :             determine_visibility (r);
   15393             :           }
   15394             : 
   15395    87751859 :         if (!local_p)
   15396             :           {
   15397             :             /* A static data member declaration is always marked
   15398             :                external when it is declared in-class, even if an
   15399             :                initializer is present.  We mimic the non-template
   15400             :                processing here.  */
   15401    74886991 :             DECL_EXTERNAL (r) = 1;
   15402    74886991 :             if (DECL_NAMESPACE_SCOPE_P (t))
   15403    20193060 :               DECL_NOT_REALLY_EXTERN (r) = 1;
   15404             : 
   15405    74886991 :             DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
   15406    74886991 :             SET_DECL_IMPLICIT_INSTANTIATION (r);
   15407    74886991 :             if (!error_operand_p (r) || (complain & tf_error))
   15408    58911662 :               register_specialization (r, gen_tmpl, argvec, false, hash);
   15409             :           }
   15410             :         else
   15411             :           {
   15412    12864868 :             if (DECL_LANG_SPECIFIC (r))
   15413      343485 :               DECL_TEMPLATE_INFO (r) = NULL_TREE;
   15414    12864868 :             if (!cp_unevaluated_operand)
   15415    12864868 :               register_local_specialization (r, t);
   15416             :           }
   15417             : 
   15418    87751859 :         DECL_CHAIN (r) = NULL_TREE;
   15419             : 
   15420    87751859 :         if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
   15421             :                                              /*flags=*/0,
   15422             :                                              args, complain, in_decl))
   15423           4 :           return error_mark_node;
   15424             : 
   15425             :         /* Preserve a typedef that names a type.  */
   15426   161143982 :         if (is_typedef_decl (r) && type != error_mark_node)
   15427             :           {
   15428    57416464 :             DECL_ORIGINAL_TYPE (r) = NULL_TREE;
   15429    57416464 :             set_underlying_type (r);
   15430             : 
   15431             :             /* common_handle_aligned_attribute doesn't apply the alignment
   15432             :                to DECL_ORIGINAL_TYPE.  */
   15433    57416464 :             if (TYPE_USER_ALIGN (TREE_TYPE (t)))
   15434          84 :               TREE_TYPE (r) = build_aligned_type (TREE_TYPE (r),
   15435          84 :                                                   TYPE_ALIGN (TREE_TYPE (t)));
   15436             :           }
   15437             : 
   15438    87751855 :         layout_decl (r, 0);
   15439             :       }
   15440    87751855 :       break;
   15441             : 
   15442           0 :     default:
   15443           0 :       gcc_unreachable ();
   15444             :     }
   15445             : #undef RETURN
   15446             : 
   15447   365894491 :  out:
   15448             :   /* Restore the file and line information.  */
   15449   365894491 :   input_location = saved_loc;
   15450             : 
   15451   365894491 :   return r;
   15452             : }
   15453             : 
   15454             : /* Substitute into the complete parameter type list PARMS.  */
   15455             : 
   15456             : tree
   15457      144244 : tsubst_function_parms (tree parms,
   15458             :                        tree args,
   15459             :                        tsubst_flags_t complain,
   15460             :                        tree in_decl)
   15461             : {
   15462      144244 :   return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
   15463             : }
   15464             : 
   15465             : /* Substitute into the ARG_TYPES of a function type.
   15466             :    If END is a TREE_CHAIN, leave it and any following types
   15467             :    un-substituted.  */
   15468             : 
   15469             : static tree
   15470   252756301 : tsubst_arg_types (tree arg_types,
   15471             :                   tree args,
   15472             :                   tree end,
   15473             :                   tsubst_flags_t complain,
   15474             :                   tree in_decl)
   15475             : {
   15476   252756301 :   tree type = NULL_TREE;
   15477   252756301 :   int len = 1;
   15478   252756301 :   tree expanded_args = NULL_TREE;
   15479             : 
   15480   252756301 :   if (!arg_types || arg_types == void_list_node || arg_types == end)
   15481             :     return arg_types;
   15482             : 
   15483   166144202 :   if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
   15484             :     {
   15485             :       /* For a pack expansion, perform substitution on the
   15486             :          entire expression. Later on, we'll handle the arguments
   15487             :          one-by-one.  */
   15488     4188912 :       expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
   15489             :                                             args, complain, in_decl);
   15490             : 
   15491     4188912 :       if (TREE_CODE (expanded_args) == TREE_VEC)
   15492             :         /* So that we'll spin through the parameters, one by one.  */
   15493     2443447 :         len = TREE_VEC_LENGTH (expanded_args);
   15494             :       else
   15495             :         {
   15496             :           /* We only partially substituted into the parameter
   15497             :              pack. Our type is TYPE_PACK_EXPANSION.  */
   15498             :           type = expanded_args;
   15499             :           expanded_args = NULL_TREE;
   15500             :         }
   15501             :     }
   15502             :   else
   15503   161955290 :     type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
   15504             : 
   15505             :   /* Check if a substituted type is erroneous before substituting into
   15506             :      the rest of the chain.  */
   15507   331094605 :   for (int i = 0; i < len; i++)
   15508             :     {
   15509   164955155 :       if (expanded_args)
   15510     1254400 :         type = TREE_VEC_ELT (expanded_args, i);
   15511             : 
   15512   164955155 :       if (type == error_mark_node)
   15513        4733 :         return error_mark_node;
   15514   164950422 :       if (VOID_TYPE_P (type))
   15515             :         {
   15516          19 :           if (complain & tf_error)
   15517             :             {
   15518           5 :               error ("invalid parameter type %qT", type);
   15519           5 :               if (in_decl)
   15520           4 :                 error ("in declaration %q+D", in_decl);
   15521             :             }
   15522          19 :           return error_mark_node;
   15523             :         }
   15524             :     }
   15525             : 
   15526             :   /* We do not substitute into default arguments here.  The standard
   15527             :      mandates that they be instantiated only when needed, which is
   15528             :      done in build_over_call.  */
   15529   166139450 :   tree default_arg = TREE_PURPOSE (arg_types);
   15530             : 
   15531             :   /* Except that we do substitute default arguments under tsubst_lambda_expr,
   15532             :      since the new op() won't have any associated template arguments for us
   15533             :      to refer to later.  */
   15534   166139450 :   if (lambda_fn_in_template_p (in_decl)
   15535   166139450 :       || (in_decl && TREE_CODE (in_decl) == FUNCTION_DECL
   15536   156087032 :           && DECL_LOCAL_DECL_P (in_decl)))
   15537       11720 :     default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl);
   15538             : 
   15539   166139450 :   tree remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
   15540   166139450 :                                                args, end, complain, in_decl);
   15541   166139450 :   if (remaining_arg_types == error_mark_node)
   15542             :     return error_mark_node;
   15543             : 
   15544   331076669 :   for (int i = len-1; i >= 0; i--)
   15545             :     {
   15546   164943811 :       if (expanded_args)
   15547     1254400 :         type = TREE_VEC_ELT (expanded_args, i);
   15548             : 
   15549             :       /* Do array-to-pointer, function-to-pointer conversion, and ignore
   15550             :          top-level qualifiers as required.  */
   15551   164943811 :       type = cv_unqualified (type_decays_to (type));
   15552             : 
   15553   164943811 :       if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
   15554             :         {
   15555             :           /* We've instantiated a template before its default arguments
   15556             :              have been parsed.  This can happen for a nested template
   15557             :              class, and is not an error unless we require the default
   15558             :              argument in a call of this function.  */
   15559           4 :           remaining_arg_types
   15560           4 :             = tree_cons (default_arg, type, remaining_arg_types);
   15561           4 :           vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
   15562             :                          remaining_arg_types);
   15563             :         }
   15564             :       else
   15565   164943807 :         remaining_arg_types
   15566   164943807 :           = hash_tree_cons (default_arg, type, remaining_arg_types);
   15567             :     }
   15568             : 
   15569   166132858 :   return remaining_arg_types;
   15570             : }
   15571             : 
   15572             : /* Substitute into a FUNCTION_TYPE or METHOD_TYPE.  This routine does
   15573             :    *not* handle the exception-specification for FNTYPE, because the
   15574             :    initial substitution of explicitly provided template parameters
   15575             :    during argument deduction forbids substitution into the
   15576             :    exception-specification:
   15577             : 
   15578             :      [temp.deduct]
   15579             : 
   15580             :      All references in the function type of the function template to  the
   15581             :      corresponding template parameters are replaced by the specified tem-
   15582             :      plate argument values.  If a substitution in a template parameter or
   15583             :      in  the function type of the function template results in an invalid
   15584             :      type, type deduction fails.  [Note: The equivalent  substitution  in
   15585             :      exception specifications is done only when the function is instanti-
   15586             :      ated, at which point a program is  ill-formed  if  the  substitution
   15587             :      results in an invalid type.]  */
   15588             : 
   15589             : static tree
   15590    85624960 : tsubst_function_type (tree t,
   15591             :                       tree args,
   15592             :                       tsubst_flags_t complain,
   15593             :                       tree in_decl)
   15594             : {
   15595    85624960 :   tree return_type;
   15596    85624960 :   tree arg_types = NULL_TREE;
   15597             : 
   15598             :   /* The TYPE_CONTEXT is not used for function/method types.  */
   15599    85624960 :   gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
   15600             : 
   15601             :   /* DR 1227: Mixing immediate and non-immediate contexts in deduction
   15602             :      failure.  */
   15603    85624960 :   bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
   15604             : 
   15605    85624960 :   if (late_return_type_p)
   15606             :     {
   15607             :       /* Substitute the argument types.  */
   15608     9783596 :       arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
   15609             :                                     complain, in_decl);
   15610     9783596 :       if (arg_types == error_mark_node)
   15611             :         return error_mark_node;
   15612             : 
   15613     9783584 :       tree save_ccp = current_class_ptr;
   15614     9783584 :       tree save_ccr = current_class_ref;
   15615     9783584 :       tree this_type = (TREE_CODE (t) == METHOD_TYPE
   15616    10663018 :                         ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
   15617      879434 :       bool do_inject = this_type && CLASS_TYPE_P (this_type);
   15618      879434 :       if (do_inject)
   15619             :         {
   15620             :           /* DR 1207: 'this' is in scope in the trailing return type.  */
   15621      879434 :           inject_this_parameter (this_type, cp_type_quals (this_type));
   15622             :         }
   15623             : 
   15624             :       /* Substitute the return type.  */
   15625     9783584 :       return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   15626             : 
   15627     9780884 :       if (do_inject)
   15628             :         {
   15629      879434 :           current_class_ptr = save_ccp;
   15630      879434 :           current_class_ref = save_ccr;
   15631             :         }
   15632             :     }
   15633             :   else
   15634             :     /* Substitute the return type.  */
   15635    75841364 :     return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   15636             : 
   15637    85614115 :   if (return_type == error_mark_node)
   15638             :     return error_mark_node;
   15639             :   /* DR 486 clarifies that creation of a function type with an
   15640             :      invalid return type is a deduction failure.  */
   15641    84395874 :   if (TREE_CODE (return_type) == ARRAY_TYPE
   15642    84395817 :       || TREE_CODE (return_type) == FUNCTION_TYPE)
   15643             :     {
   15644         184 :       if (complain & tf_error)
   15645             :         {
   15646          22 :           if (TREE_CODE (return_type) == ARRAY_TYPE)
   15647          11 :             error ("function returning an array");
   15648             :           else
   15649          11 :             error ("function returning a function");
   15650             :         }
   15651         184 :       return error_mark_node;
   15652             :     }
   15653             : 
   15654    84395690 :   if (!late_return_type_p)
   15655             :     {
   15656             :       /* Substitute the argument types.  */
   15657    75408148 :       arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
   15658             :                                     complain, in_decl);
   15659    75408148 :       if (arg_types == error_mark_node)
   15660             :         return error_mark_node;
   15661             :     }
   15662             : 
   15663             :   /* Construct a new type node and return it.  */
   15664    84390988 :   return rebuild_function_or_method_type (t, return_type, arg_types,
   15665    84390988 :                                           /*raises=*/NULL_TREE, complain);
   15666             : }
   15667             : 
   15668             : /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE.  Substitute the template
   15669             :    ARGS into that specification, and return the substituted
   15670             :    specification.  If there is no specification, return NULL_TREE.  */
   15671             : 
   15672             : static tree
   15673    84390952 : tsubst_exception_specification (tree fntype,
   15674             :                                 tree args,
   15675             :                                 tsubst_flags_t complain,
   15676             :                                 tree in_decl,
   15677             :                                 bool defer_ok)
   15678             : {
   15679    84390952 :   tree specs;
   15680    84390952 :   tree new_specs;
   15681             : 
   15682    84390952 :   specs = TYPE_RAISES_EXCEPTIONS (fntype);
   15683    84390952 :   new_specs = NULL_TREE;
   15684   113869117 :   if (specs && TREE_PURPOSE (specs))
   15685             :     {
   15686             :       /* A noexcept-specifier.  */
   15687    29451354 :       tree expr = TREE_PURPOSE (specs);
   15688    29451354 :       if (TREE_CODE (expr) == INTEGER_CST)
   15689    26536880 :         new_specs = expr;
   15690     2914474 :       else if (defer_ok)
   15691             :         {
   15692             :           /* Defer instantiation of noexcept-specifiers to avoid
   15693             :              excessive instantiations (c++/49107).  */
   15694     2911049 :           new_specs = make_node (DEFERRED_NOEXCEPT);
   15695     2911049 :           if (DEFERRED_NOEXCEPT_SPEC_P (specs))
   15696             :             {
   15697             :               /* We already partially instantiated this member template,
   15698             :                  so combine the new args with the old.  */
   15699           7 :               DEFERRED_NOEXCEPT_PATTERN (new_specs)
   15700           7 :                 = DEFERRED_NOEXCEPT_PATTERN (expr);
   15701          14 :               DEFERRED_NOEXCEPT_ARGS (new_specs)
   15702          14 :                 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
   15703             :             }
   15704             :           else
   15705             :             {
   15706     2911042 :               DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
   15707     2911042 :               DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
   15708             :             }
   15709             :         }
   15710             :       else
   15711             :         {
   15712        3425 :           if (DEFERRED_NOEXCEPT_SPEC_P (specs))
   15713             :             {
   15714           0 :               args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
   15715             :                                            args);
   15716           0 :               expr = DEFERRED_NOEXCEPT_PATTERN (expr);
   15717             :             }
   15718        3425 :           new_specs = tsubst_copy_and_build (expr, args, complain, in_decl);
   15719             :         }
   15720    29451354 :       new_specs = build_noexcept_spec (new_specs, complain);
   15721             :       /* We've instantiated a template before a noexcept-specifier
   15722             :          contained therein has been parsed.  This can happen for
   15723             :          a nested template class:
   15724             : 
   15725             :           struct S {
   15726             :             template<typename> struct B { B() noexcept(...); };
   15727             :             struct A : B<int> { ... use B() ... };
   15728             :           };
   15729             : 
   15730             :          where completing B<int> will trigger instantiating the
   15731             :          noexcept, even though we only parse it at the end of S.  */
   15732    29451354 :       if (UNPARSED_NOEXCEPT_SPEC_P (specs))
   15733             :         {
   15734           9 :           gcc_checking_assert (defer_ok);
   15735           9 :           vec_safe_push (DEFPARSE_INSTANTIATIONS (expr), new_specs);
   15736             :         }
   15737             :     }
   15738    54939598 :   else if (specs)
   15739             :     {
   15740       26811 :       if (! TREE_VALUE (specs))
   15741       26744 :         new_specs = specs;
   15742             :       else
   15743         130 :         while (specs)
   15744             :           {
   15745          67 :             tree spec;
   15746          67 :             int i, len = 1;
   15747          67 :             tree expanded_specs = NULL_TREE;
   15748             : 
   15749          67 :             if (PACK_EXPANSION_P (TREE_VALUE (specs)))
   15750             :               {
   15751             :                 /* Expand the pack expansion type.  */
   15752          13 :                 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
   15753             :                                                        args, complain,
   15754             :                                                        in_decl);
   15755             : 
   15756          13 :                 if (expanded_specs == error_mark_node)
   15757           2 :                   return error_mark_node;
   15758          11 :                 else if (TREE_CODE (expanded_specs) == TREE_VEC)
   15759           4 :                   len = TREE_VEC_LENGTH (expanded_specs);
   15760             :                 else
   15761             :                   {
   15762             :                     /* We're substituting into a member template, so
   15763             :                        we got a TYPE_PACK_EXPANSION back.  Add that
   15764             :                        expansion and move on.  */
   15765           7 :                     gcc_assert (TREE_CODE (expanded_specs)
   15766             :                                 == TYPE_PACK_EXPANSION);
   15767           7 :                     new_specs = add_exception_specifier (new_specs,
   15768             :                                                          expanded_specs,
   15769             :                                                          complain);
   15770           7 :                     specs = TREE_CHAIN (specs);
   15771           7 :                     continue;
   15772             :                   }
   15773             :               }
   15774             : 
   15775         122 :             for (i = 0; i < len; ++i)
   15776             :               {
   15777          66 :                 if (expanded_specs)
   15778          12 :                   spec = TREE_VEC_ELT (expanded_specs, i);
   15779             :                 else
   15780          54 :                   spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
   15781          66 :                 if (spec == error_mark_node)
   15782           2 :                   return spec;
   15783          64 :                 new_specs = add_exception_specifier (new_specs, spec, 
   15784             :                                                      complain);
   15785             :               }
   15786             : 
   15787          56 :             specs = TREE_CHAIN (specs);
   15788             :           }
   15789             :     }
   15790    84390948 :   return new_specs;
   15791             : }
   15792             : 
   15793             : /* Substitute through a TREE_LIST of types or expressions, handling pack
   15794             :    expansions.  */
   15795             : 
   15796             : tree
   15797    22017724 : tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   15798             : {
   15799    22017724 :   if (t == void_list_node)
   15800             :     return t;
   15801             : 
   15802    22017704 :   tree purpose = TREE_PURPOSE (t);
   15803    22017704 :   tree purposevec = NULL_TREE;
   15804    22017704 :   if (!purpose)
   15805             :     ;
   15806           1 :   else if (PACK_EXPANSION_P (purpose))
   15807             :     {
   15808           0 :       purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
   15809           0 :       if (TREE_CODE (purpose) == TREE_VEC)
   15810           0 :         purposevec = purpose;
   15811             :     }
   15812           1 :   else if (TYPE_P (purpose))
   15813           0 :     purpose = tsubst (purpose, args, complain, in_decl);
   15814             :   else
   15815           1 :     purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
   15816    22017704 :   if (purpose == error_mark_node || purposevec == error_mark_node)
   15817             :     return error_mark_node;
   15818             : 
   15819    22017704 :   tree value = TREE_VALUE (t);
   15820    22017704 :   tree valuevec = NULL_TREE;
   15821    22017704 :   if (!value)
   15822             :     ;
   15823    22017704 :   else if (PACK_EXPANSION_P (value))
   15824             :     {
   15825      111223 :       value = tsubst_pack_expansion (value, args, complain, in_decl);
   15826      111223 :       if (TREE_CODE (value) == TREE_VEC)
   15827      111008 :         valuevec = value;
   15828             :     }
   15829    21906481 :   else if (TYPE_P (value))
   15830           4 :     value = tsubst (value, args, complain, in_decl);
   15831             :   else
   15832    21906477 :     value = tsubst_copy_and_build (value, args, complain, in_decl);
   15833    22017704 :   if (value == error_mark_node || valuevec == error_mark_node)
   15834             :     return error_mark_node;
   15835             : 
   15836    22017305 :   tree chain = TREE_CHAIN (t);
   15837    22017305 :   if (!chain)
   15838             :     ;
   15839      380813 :   else if (TREE_CODE (chain) == TREE_LIST)
   15840      380813 :     chain = tsubst_tree_list (chain, args, complain, in_decl);
   15841           0 :   else if (TYPE_P (chain))
   15842           0 :     chain = tsubst (chain, args, complain, in_decl);
   15843             :   else
   15844           0 :     chain = tsubst_copy_and_build (chain, args, complain, in_decl);
   15845    22017305 :   if (chain == error_mark_node)
   15846             :     return error_mark_node;
   15847             : 
   15848    22017305 :   if (purpose == TREE_PURPOSE (t)
   15849    22017304 :       && value == TREE_VALUE (t)
   15850    22379050 :       && chain == TREE_CHAIN (t))
   15851             :     return t;
   15852             : 
   15853    21655746 :   int len;
   15854             :   /* Determine the number of arguments.  */
   15855    21655746 :   if (purposevec)
   15856             :     {
   15857           0 :       len = TREE_VEC_LENGTH (purposevec);
   15858           0 :       gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
   15859             :     }
   15860    21655746 :   else if (valuevec)
   15861      111008 :     len = TREE_VEC_LENGTH (valuevec);
   15862             :   else
   15863             :     len = 1;
   15864             : 
   15865    43248130 :   for (int i = len; i-- > 0; )
   15866             :     {
   15867    21592384 :       if (purposevec)
   15868           0 :         purpose = TREE_VEC_ELT (purposevec, i);
   15869    21592384 :       if (valuevec)
   15870       47646 :         value = TREE_VEC_ELT (valuevec, i);
   15871             : 
   15872    21592384 :       if (value && TYPE_P (value))
   15873           5 :         chain = hash_tree_cons (purpose, value, chain);
   15874             :       else
   15875    21592379 :         chain = tree_cons (purpose, value, chain);
   15876             :     }
   15877             : 
   15878             :   return chain;
   15879             : }
   15880             : 
   15881             : /* Take the tree structure T and replace template parameters used
   15882             :    therein with the argument vector ARGS.  IN_DECL is an associated
   15883             :    decl for diagnostics.  If an error occurs, returns ERROR_MARK_NODE.
   15884             :    Issue error and warning messages under control of COMPLAIN.  Note
   15885             :    that we must be relatively non-tolerant of extensions here, in
   15886             :    order to preserve conformance; if we allow substitutions that
   15887             :    should not be allowed, we may allow argument deductions that should
   15888             :    not succeed, and therefore report ambiguous overload situations
   15889             :    where there are none.  In theory, we could allow the substitution,
   15890             :    but indicate that it should have failed, and allow our caller to
   15891             :    make sure that the right thing happens, but we don't try to do this
   15892             :    yet.
   15893             : 
   15894             :    This function is used for dealing with types, decls and the like;
   15895             :    for expressions, use tsubst_expr or tsubst_copy.  */
   15896             : 
   15897             : tree
   15898  3697590287 : tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   15899             : {
   15900  3697616503 :   enum tree_code code;
   15901  3697616503 :   tree type, r = NULL_TREE;
   15902             : 
   15903  3697616503 :   if (t == NULL_TREE || t == error_mark_node
   15904  3677437124 :       || t == integer_type_node
   15905  3642437161 :       || t == void_type_node
   15906  3597348636 :       || t == char_type_node
   15907  3587298506 :       || t == unknown_type_node
   15908  3587298491 :       || TREE_CODE (t) == NAMESPACE_DECL
   15909  3521973617 :       || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
   15910             :     return t;
   15911             : 
   15912  3521819766 :   tsubst_flags_t tst_ok_flag = (complain & tf_tst_ok);
   15913  3521819766 :   complain &= ~tf_tst_ok;
   15914             : 
   15915  3521819766 :   tsubst_flags_t qualifying_scope_flag = (complain & tf_qualifying_scope);
   15916  3521819766 :   complain &= ~tf_qualifying_scope;
   15917             : 
   15918  3521819766 :   if (DECL_P (t))
   15919   358664321 :     return tsubst_decl (t, args, complain);
   15920             : 
   15921  3163155445 :   if (args == NULL_TREE)
   15922             :     return t;
   15923             : 
   15924  3152898697 :   code = TREE_CODE (t);
   15925             : 
   15926  3152898697 :   gcc_assert (code != IDENTIFIER_NODE);
   15927  3152898697 :   type = TREE_TYPE (t);
   15928             : 
   15929  3152898697 :   gcc_assert (type != unknown_type_node);
   15930             : 
   15931  3152898697 :   if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
   15932             :     return d;
   15933             : 
   15934             :   /* Reuse typedefs.  We need to do this to handle dependent attributes,
   15935             :      such as attribute aligned.  */
   15936  3152896859 :   if (TYPE_P (t)
   15937  3152896859 :       && typedef_variant_p (t))
   15938             :     {
   15939   174127217 :       tree decl = TYPE_NAME (t);
   15940             : 
   15941   174127217 :       if (alias_template_specialization_p (t, nt_opaque))
   15942             :         {
   15943             :           /* DECL represents an alias template and we want to
   15944             :              instantiate it.  */
   15945    48453951 :           tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
   15946    48453951 :           tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
   15947    48453951 :           r = instantiate_alias_template (tmpl, gen_args, complain);
   15948             :         }
   15949   251346532 :       else if (DECL_CLASS_SCOPE_P (decl)
   15950    99639061 :                && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
   15951   221953797 :                && uses_template_parms (DECL_CONTEXT (decl)))
   15952             :         {
   15953    96146203 :           tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
   15954    96146203 :           tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
   15955    96146203 :           r = retrieve_specialization (tmpl, gen_args, 0);
   15956             :         }
   15957    59054126 :       else if (DECL_FUNCTION_SCOPE_P (decl)
   15958      794289 :                && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
   15959    30321352 :                && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
   15960      793553 :         r = retrieve_local_specialization (decl);
   15961             :       else
   15962             :         /* The typedef is from a non-template context.  */
   15963    28733510 :         return t;
   15964             : 
   15965   145393704 :       if (r)
   15966             :         {
   15967   145373875 :           r = TREE_TYPE (r);
   15968   145373875 :           r = cp_build_qualified_type
   15969   145373875 :             (r, cp_type_quals (t) | cp_type_quals (r),
   15970             :              complain | tf_ignore_bad_quals);
   15971   145373875 :           return r;
   15972             :         }
   15973             :       else
   15974             :         {
   15975             :           /* We don't have an instantiation yet, so drop the typedef.  */
   15976       19829 :           int quals = cp_type_quals (t);
   15977       19829 :           t = DECL_ORIGINAL_TYPE (decl);
   15978       19829 :           t = cp_build_qualified_type (t, quals,
   15979             :                                        complain | tf_ignore_bad_quals);
   15980             :         }
   15981             :     }
   15982             : 
   15983  2978789471 :   bool fndecl_type = (complain & tf_fndecl_type);
   15984  2978789471 :   complain &= ~tf_fndecl_type;
   15985             : 
   15986  2978789471 :   if (type
   15987  2978789471 :       && code != TYPENAME_TYPE
   15988   483346805 :       && code != TEMPLATE_TYPE_PARM
   15989   483346805 :       && code != TEMPLATE_PARM_INDEX
   15990             :       && code != IDENTIFIER_NODE
   15991   367939037 :       && code != FUNCTION_TYPE
   15992   338698702 :       && code != METHOD_TYPE)
   15993   282314137 :     type = tsubst (type, args, complain, in_decl);
   15994  2978789471 :   if (type == error_mark_node)
   15995             :     return error_mark_node;
   15996             : 
   15997  2978763480 :   switch (code)
   15998             :     {
   15999   313552789 :     case RECORD_TYPE:
   16000   313552789 :       if (TYPE_PTRMEMFUNC_P (t))
   16001       26216 :         return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
   16002             :       /* Fall through.  */
   16003   322036535 :     case UNION_TYPE:
   16004   322036535 :     case ENUMERAL_TYPE:
   16005   322036535 :       return tsubst_aggr_type_1 (t, args, complain, in_decl,
   16006   322036437 :                                  /*entering_scope=*/0);
   16007             : 
   16008             :     case ERROR_MARK:
   16009             :     case IDENTIFIER_NODE:
   16010             :     case VOID_TYPE:
   16011             :     case OPAQUE_TYPE:
   16012             :     case REAL_TYPE:
   16013             :     case COMPLEX_TYPE:
   16014             :     case VECTOR_TYPE:
   16015             :     case BOOLEAN_TYPE:
   16016             :     case NULLPTR_TYPE:
   16017             :     case LANG_TYPE:
   16018             :       return t;
   16019             : 
   16020    42493276 :     case INTEGER_TYPE:
   16021    42493276 :       if (t == integer_type_node)
   16022             :         return t;
   16023             : 
   16024    42493262 :       if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
   16025    42493262 :           && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
   16026             :         return t;
   16027             : 
   16028       94454 :       {
   16029       94454 :         tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
   16030             : 
   16031       94454 :         max = tsubst_expr (omax, args, complain, in_decl);
   16032             : 
   16033             :         /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
   16034             :            needed.  */
   16035       94454 :         if (TREE_CODE (max) == NOP_EXPR
   16036        9940 :             && TREE_SIDE_EFFECTS (omax)
   16037       94460 :             && !TREE_TYPE (max))
   16038           0 :           TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
   16039             : 
   16040             :         /* If we're in a partial instantiation, preserve the magic NOP_EXPR
   16041             :            with TREE_SIDE_EFFECTS that indicates this is not an integral
   16042             :            constant expression.  */
   16043       94454 :         if (processing_template_decl
   16044       94454 :             && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
   16045             :           {
   16046           0 :             gcc_assert (TREE_CODE (max) == NOP_EXPR);
   16047           0 :             TREE_SIDE_EFFECTS (max) = 1;
   16048             :           }
   16049             : 
   16050       94454 :         return compute_array_index_type (NULL_TREE, max, complain);
   16051             :       }
   16052             : 
   16053  1696455245 :     case TEMPLATE_TYPE_PARM:
   16054  1696455245 :       if (template_placeholder_p (t))
   16055             :         {
   16056        1677 :           tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (t);
   16057        1677 :           tmpl = tsubst_copy (tmpl, args, complain, in_decl);
   16058        1677 :           if (TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
   16059          11 :             tmpl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl);
   16060             : 
   16061        1677 :           if (tmpl != CLASS_PLACEHOLDER_TEMPLATE (t))
   16062          71 :             return make_template_placeholder (tmpl);
   16063             :           else
   16064             :             return t;
   16065             :         }
   16066             :       /* Fall through.  */
   16067  1813740165 :     case TEMPLATE_TEMPLATE_PARM:
   16068  1813740165 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
   16069  1813740165 :     case TEMPLATE_PARM_INDEX:
   16070  1813740165 :       {
   16071  1813740165 :         int idx;
   16072  1813740165 :         int level;
   16073  1813740165 :         int levels;
   16074  1813740165 :         tree arg = NULL_TREE;
   16075             : 
   16076  1813740165 :         r = NULL_TREE;
   16077             : 
   16078  1813740165 :         gcc_assert (TREE_VEC_LENGTH (args) > 0);
   16079  1813740165 :         template_parm_level_and_index (t, &level, &idx);
   16080             : 
   16081  1813740165 :         levels = TMPL_ARGS_DEPTH (args);
   16082  1813740165 :         if (level <= levels
   16083  3488895519 :             && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
   16084             :           {
   16085  1675155327 :             arg = TMPL_ARG (args, level, idx);
   16086             : 
   16087             :             /* See through ARGUMENT_PACK_SELECT arguments. */
   16088  1675155327 :             if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
   16089     8151872 :               arg = argument_pack_select_arg (arg);
   16090             :           }
   16091             : 
   16092  1813740165 :         if (arg == error_mark_node)
   16093             :           return error_mark_node;
   16094  1813740127 :         else if (arg != NULL_TREE)
   16095             :           {
   16096  1669096700 :             if (ARGUMENT_PACK_P (arg))
   16097             :               /* If ARG is an argument pack, we don't actually want to
   16098             :                  perform a substitution here, because substitutions
   16099             :                  for argument packs are only done
   16100             :                  element-by-element. We can get to this point when
   16101             :                  substituting the type of a non-type template
   16102             :                  parameter pack, when that type actually contains
   16103             :                  template parameter packs from an outer template, e.g.,
   16104             : 
   16105             :                  template<typename... Types> struct A {
   16106             :                    template<Types... Values> struct B { };
   16107             :                  };  */
   16108             :               return t;
   16109             : 
   16110  1669096333 :             if (code == TEMPLATE_TYPE_PARM)
   16111             :               {
   16112  1562093626 :                 int quals;
   16113             : 
   16114             :                 /* When building concept checks for the purpose of
   16115             :                    deducing placeholders, we can end up with wildcards
   16116             :                    where types are expected. Adjust this to the deduced
   16117             :                    value.  */
   16118  1562093626 :                 if (TREE_CODE (arg) == WILDCARD_DECL)
   16119          10 :                   arg = TREE_TYPE (TREE_TYPE (arg));
   16120             : 
   16121  1562093626 :                 gcc_assert (TYPE_P (arg));
   16122             : 
   16123  1562093626 :                 quals = cp_type_quals (arg) | cp_type_quals (t);
   16124             : 
   16125  1562093626 :                 return cp_build_qualified_type
   16126  1562093626 :                   (arg, quals, complain | tf_ignore_bad_quals);
   16127             :               }
   16128   107002707 :             else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
   16129             :               {
   16130             :                 /* We are processing a type constructed from a
   16131             :                    template template parameter.  */
   16132      316826 :                 tree argvec = tsubst (TYPE_TI_ARGS (t),
   16133             :                                       args, complain, in_decl);
   16134      158413 :                 if (argvec == error_mark_node)
   16135             :                   return error_mark_node;
   16136             : 
   16137      158413 :                 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
   16138             :                             || TREE_CODE (arg) == TEMPLATE_DECL
   16139             :                             || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
   16140             : 
   16141      158413 :                 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
   16142             :                   /* Consider this code:
   16143             : 
   16144             :                         template <template <class> class Template>
   16145             :                         struct Internal {
   16146             :                         template <class Arg> using Bind = Template<Arg>;
   16147             :                         };
   16148             : 
   16149             :                         template <template <class> class Template, class Arg>
   16150             :                         using Instantiate = Template<Arg>; //#0
   16151             : 
   16152             :                         template <template <class> class Template,
   16153             :                                   class Argument>
   16154             :                         using Bind =
   16155             :                           Instantiate<Internal<Template>::template Bind,
   16156             :                                       Argument>; //#1
   16157             : 
   16158             :                      When #1 is parsed, the
   16159             :                      BOUND_TEMPLATE_TEMPLATE_PARM representing the
   16160             :                      parameter `Template' in #0 matches the
   16161             :                      UNBOUND_CLASS_TEMPLATE representing the argument
   16162             :                      `Internal<Template>::template Bind'; We then want
   16163             :                      to assemble the type `Bind<Argument>' that can't
   16164             :                      be fully created right now, because
   16165             :                      `Internal<Template>' not being complete, the Bind
   16166             :                      template cannot be looked up in that context.  So
   16167             :                      we need to "store" `Bind<Argument>' for later
   16168             :                      when the context of Bind becomes complete.  Let's
   16169             :                      store that in a TYPENAME_TYPE.  */
   16170           6 :                   return make_typename_type (TYPE_CONTEXT (arg),
   16171             :                                              build_nt (TEMPLATE_ID_EXPR,
   16172           6 :                                                        TYPE_IDENTIFIER (arg),
   16173             :                                                        argvec),
   16174             :                                              typename_type,
   16175           6 :                                              complain);
   16176             : 
   16177             :                 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
   16178             :                    are resolving nested-types in the signature of a
   16179             :                    member function templates.  Otherwise ARG is a
   16180             :                    TEMPLATE_DECL and is the real template to be
   16181             :                    instantiated.  */
   16182      158407 :                 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
   16183          99 :                   arg = TYPE_NAME (arg);
   16184             : 
   16185      158407 :                 r = lookup_template_class (arg,
   16186             :                                            argvec, in_decl,
   16187      158407 :                                            DECL_CONTEXT (arg),
   16188             :                                             /*entering_scope=*/0,
   16189             :                                            complain);
   16190      158407 :                 return cp_build_qualified_type
   16191      158407 :                   (r, cp_type_quals (t) | cp_type_quals (r), complain);
   16192             :               }
   16193   106844294 :             else if (code == TEMPLATE_TEMPLATE_PARM)
   16194             :               return arg;
   16195             :             else
   16196             :               /* TEMPLATE_PARM_INDEX.  */
   16197   105166116 :               return convert_from_reference (unshare_expr (arg));
   16198             :           }
   16199             : 
   16200   144643427 :         if (level == 1)
   16201             :           /* This can happen during the attempted tsubst'ing in
   16202             :              unify.  This means that we don't yet have any information
   16203             :              about the template parameter in question.  */
   16204             :           return t;
   16205             : 
   16206             :         /* Early in template argument deduction substitution, we don't
   16207             :            want to reduce the level of 'auto', or it will be confused
   16208             :            with a normal template parm in subsequent deduction.
   16209             :            Similarly, don't reduce the level of template parameters to
   16210             :            avoid mismatches when deducing their types.  */
   16211   138584815 :         if (complain & tf_partial)
   16212             :           return t;
   16213             : 
   16214             :         /* If we get here, we must have been looking at a parm for a
   16215             :            more deeply nested template.  Make a new version of this
   16216             :            template parameter, but with a lower level.  */
   16217   138570824 :         int quals;
   16218   138570824 :         switch (code)
   16219             :           {
   16220   128735062 :           case TEMPLATE_TYPE_PARM:
   16221   128735062 :           case TEMPLATE_TEMPLATE_PARM:
   16222   128735062 :             quals = cp_type_quals (t);
   16223   128735062 :             if (quals)
   16224             :               {
   16225     8304769 :                 gcc_checking_assert (code == TEMPLATE_TYPE_PARM);
   16226     8304769 :                 t = TYPE_MAIN_VARIANT (t);
   16227             :               }
   16228             : 
   16229   128735062 :             if (tree d = TEMPLATE_TYPE_DESCENDANTS (t))
   16230   120772286 :               if (TEMPLATE_PARM_LEVEL (d) == TEMPLATE_TYPE_LEVEL (t) - levels
   16231   120772286 :                   && (code == TEMPLATE_TYPE_PARM
   16232       33117 :                       || TEMPLATE_TEMPLATE_PARM_SIMPLE_P (t)))
   16233             :                 /* Cache lowering a type parameter or a simple template
   16234             :                    template parameter.  */
   16235   120770251 :                 r = TREE_TYPE (d);
   16236             : 
   16237   120770251 :             if (!r)
   16238             :               {
   16239     7964811 :                 r = copy_type (t);
   16240     7964811 :                 TEMPLATE_TYPE_PARM_INDEX (r)
   16241     7964811 :                   = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
   16242             :                                                 r, levels, args, complain);
   16243     7964811 :                 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
   16244     7964811 :                 TYPE_MAIN_VARIANT (r) = r;
   16245     7964811 :                 TYPE_POINTER_TO (r) = NULL_TREE;
   16246     7964811 :                 TYPE_REFERENCE_TO (r) = NULL_TREE;
   16247             : 
   16248     7964811 :                 if (code == TEMPLATE_TYPE_PARM)
   16249     7963294 :                   if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t))
   16250             :                     /* Propagate constraints on placeholders since they are
   16251             :                        only instantiated during satisfaction.  */
   16252       28453 :                     PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r) = ci;
   16253             : 
   16254     7964811 :                 if (TYPE_STRUCTURAL_EQUALITY_P (t))
   16255           0 :                   SET_TYPE_STRUCTURAL_EQUALITY (r);
   16256             :                 else
   16257     7964811 :                   TYPE_CANONICAL (r) = canonical_type_parameter (r);
   16258             :               }
   16259             : 
   16260   128735062 :             if (quals)
   16261     8304769 :               r = cp_build_qualified_type (r, quals,
   16262             :                                            complain | tf_ignore_bad_quals);
   16263             :             break;
   16264             : 
   16265        7749 :           case BOUND_TEMPLATE_TEMPLATE_PARM:
   16266        7749 :             {
   16267        7749 :               tree tinfo = TYPE_TEMPLATE_INFO (t);
   16268             :               /* We might need to substitute into the types of non-type
   16269             :                  template parameters.  This also lowers the level of
   16270             :                  the ttp appropriately.  */
   16271        7749 :               tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
   16272             :                                   complain, in_decl);
   16273        7749 :               if (tmpl == error_mark_node)
   16274             :                 return error_mark_node;
   16275        7749 :               tree argvec = tsubst (TI_ARGS (tinfo), args,
   16276             :                                     complain, in_decl);
   16277        7749 :               if (argvec == error_mark_node)
   16278             :                 return error_mark_node;
   16279        7749 :               r = lookup_template_class (tmpl, argvec, in_decl, NULL_TREE,
   16280             :                                          /*entering_scope=*/false, complain);
   16281        7749 :               r = cp_build_qualified_type (r, cp_type_quals (t), complain);
   16282        7749 :               break;
   16283             :             }
   16284             : 
   16285     9828013 :           case TEMPLATE_PARM_INDEX:
   16286             :             /* OK, now substitute the type of the non-type parameter.  We
   16287             :                couldn't do it earlier because it might be an auto parameter,
   16288             :                and we wouldn't need to if we had an argument.  */
   16289     9828013 :             type = tsubst (type, args, complain, in_decl);
   16290     9828013 :             if (type == error_mark_node)
   16291             :               return error_mark_node;
   16292     9828006 :             r = reduce_template_parm_level (t, type, levels, args, complain);
   16293     9828006 :             break;
   16294             : 
   16295           0 :           default:
   16296           0 :             gcc_unreachable ();
   16297             :           }
   16298             : 
   16299             :         return r;
   16300             :       }
   16301             : 
   16302           5 :     case TREE_LIST:
   16303           5 :       return tsubst_tree_list (t, args, complain, in_decl);
   16304             : 
   16305           0 :     case TREE_BINFO:
   16306             :       /* We should never be tsubsting a binfo.  */
   16307           0 :       gcc_unreachable ();
   16308             : 
   16309   232458561 :     case TREE_VEC:
   16310             :       /* A vector of template arguments.  */
   16311   232458561 :       gcc_assert (!type);
   16312   232458561 :       return tsubst_template_args (t, args, complain, in_decl);
   16313             : 
   16314   273083012 :     case POINTER_TYPE:
   16315   273083012 :     case REFERENCE_TYPE:
   16316   273083012 :       {
   16317   273083012 :         if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
   16318             :           return t;
   16319             : 
   16320             :         /* [temp.deduct]
   16321             : 
   16322             :            Type deduction may fail for any of the following
   16323             :            reasons:
   16324             : 
   16325             :            -- Attempting to create a pointer to reference type.
   16326             :            -- Attempting to create a reference to a reference type or
   16327             :               a reference to void.
   16328             : 
   16329             :           Core issue 106 says that creating a reference to a reference
   16330             :           during instantiation is no longer a cause for failure. We
   16331             :           only enforce this check in strict C++98 mode.  */
   16332   252465206 :         if ((TYPE_REF_P (type)
   16333     2746904 :              && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
   16334   252465087 :             || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
   16335             :           {
   16336         443 :             static location_t last_loc;
   16337             : 
   16338             :             /* We keep track of the last time we issued this error
   16339             :                message to avoid spewing a ton of messages during a
   16340             :                single bad template instantiation.  */
   16341         443 :             if (complain & tf_error
   16342          27 :                 && last_loc != input_location)
   16343             :               {
   16344          27 :                 if (VOID_TYPE_P (type))
   16345           8 :                   error ("forming reference to void");
   16346          19 :                else if (code == POINTER_TYPE)
   16347          18 :                  error ("forming pointer to reference type %qT", type);
   16348             :                else
   16349           1 :                   error ("forming reference to reference type %qT", type);
   16350          27 :                 last_loc = input_location;
   16351             :               }
   16352             : 
   16353         443 :             return error_mark_node;
   16354             :           }
   16355   252464763 :         else if (TREE_CODE (type) == FUNCTION_TYPE
   16356   252464763 :                  && (type_memfn_quals (type) != TYPE_UNQUALIFIED
   16357      902543 :                      || type_memfn_rqual (type) != REF_QUAL_NONE))
   16358             :           {
   16359          99 :             if (complain & tf_error)
   16360             :               {
   16361          12 :                 if (code == POINTER_TYPE)
   16362           8 :                   error ("forming pointer to qualified function type %qT",
   16363             :                          type);
   16364             :                 else
   16365           4 :                   error ("forming reference to qualified function type %qT",
   16366             :                          type);
   16367             :               }
   16368          99 :             return error_mark_node;
   16369             :           }
   16370   252464664 :         else if (code == POINTER_TYPE)
   16371             :           {
   16372   146666692 :             r = build_pointer_type (type);
   16373   146666692 :             if (TREE_CODE (type) == METHOD_TYPE)
   16374       26208 :               r = build_ptrmemfunc_type (r);
   16375             :           }
   16376   105797972 :         else if (TYPE_REF_P (type))
   16377             :           /* In C++0x, during template argument substitution, when there is an
   16378             :              attempt to create a reference to a reference type, reference
   16379             :              collapsing is applied as described in [14.3.1/4 temp.arg.type]:
   16380             : 
   16381             :              "If a template-argument for a template-parameter T names a type
   16382             :              that is a reference to a type A, an attempt to create the type
   16383             :              'lvalue reference to cv T' creates the type 'lvalue reference to
   16384             :              A,' while an attempt to create the type type rvalue reference to
   16385             :              cv T' creates the type T"
   16386             :           */
   16387     2746785 :           r = cp_build_reference_type
   16388     2746785 :               (TREE_TYPE (type),
   16389     2746785 :                TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
   16390             :         else
   16391   103051187 :           r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
   16392   252464664 :         r = cp_build_qualified_type (r, cp_type_quals (t), complain);
   16393             : 
   16394   252464664 :         if (r != error_mark_node)
   16395             :           /* Will this ever be needed for TYPE_..._TO values?  */
   16396   252464664 :           layout_type (r);
   16397             : 
   16398             :         return r;
   16399             :       }
   16400      205756 :     case OFFSET_TYPE:
   16401      205756 :       {
   16402      205756 :         r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
   16403      377452 :         if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
   16404             :           {
   16405             :             /* [temp.deduct]
   16406             : 
   16407             :                Type deduction may fail for any of the following
   16408             :                reasons:
   16409             : 
   16410             :                -- Attempting to create "pointer to member of T" when T
   16411             :                   is not a class type.  */
   16412           0 :             if (complain & tf_error)
   16413           0 :               error ("creating pointer to member of non-class type %qT", r);
   16414           0 :             return error_mark_node;
   16415             :           }
   16416      205756 :         if (TYPE_REF_P (type))
   16417             :           {
   16418           8 :             if (complain & tf_error)
   16419           4 :               error ("creating pointer to member reference type %qT", type);
   16420           8 :             return error_mark_node;
   16421             :           }
   16422      205748 :         if (VOID_TYPE_P (type))
   16423             :           {
   16424           4 :             if (complain & tf_error)
   16425           4 :               error ("creating pointer to member of type void");
   16426           4 :             return error_mark_node;
   16427             :           }
   16428      205744 :         gcc_assert (TREE_CODE (type) != METHOD_TYPE);
   16429      205744 :         if (TREE_CODE (type) == FUNCTION_TYPE)
   16430             :           {
   16431             :             /* The type of the implicit object parameter gets its
   16432             :                cv-qualifiers from the FUNCTION_TYPE. */
   16433      166980 :             tree memptr;
   16434      166980 :             tree method_type
   16435      166980 :               = build_memfn_type (type, r, type_memfn_quals (type),
   16436             :                                   type_memfn_rqual (type));
   16437      166980 :             memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
   16438      166980 :             return cp_build_qualified_type (memptr, cp_type_quals (t),
   16439      166980 :                                             complain);
   16440             :           }
   16441             :         else
   16442       38764 :           return cp_build_qualified_type (build_ptrmem_type (r, type),
   16443             :                                           cp_type_quals (t),
   16444       38764 :                                           complain);
   16445             :       }
   16446    85624900 :     case FUNCTION_TYPE:
   16447    85624900 :     case METHOD_TYPE:
   16448    85624900 :       {
   16449    85624900 :         tree fntype;
   16450    85624900 :         tree specs;
   16451    85624900 :         fntype = tsubst_function_type (t, args, complain, in_decl);
   16452    85614067 :         if (fntype == error_mark_node)
   16453             :           return error_mark_node;
   16454             : 
   16455             :         /* Substitute the exception specification.  */
   16456    84390928 :         specs = tsubst_exception_specification (t, args, complain, in_decl,
   16457             :                                                 /*defer_ok*/fndecl_type);
   16458    84390928 :         if (specs == error_mark_node)
   16459             :           return error_mark_node;
   16460    84390916 :         if (specs)
   16461    29478125 :           fntype = build_exception_variant (fntype, specs);
   16462             :         return fntype;
   16463             :       }
   16464     1304887 :     case ARRAY_TYPE:
   16465     1304887 :       {
   16466     1304887 :         tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
   16467     1304887 :         if (domain == error_mark_node)
   16468             :           return error_mark_node;
   16469             : 
   16470             :         /* As an optimization, we avoid regenerating the array type if
   16471             :            it will obviously be the same as T.  */
   16472     2428697 :         if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
   16473             :           return t;
   16474             : 
   16475             :         /* These checks should match the ones in create_array_type_for_decl.
   16476             : 
   16477             :            [temp.deduct]
   16478             : 
   16479             :            The deduction may fail for any of the following reasons:
   16480             : 
   16481             :            -- Attempting to create an array with an element type that
   16482             :               is void, a function type, or a reference type, or [DR337]
   16483             :               an abstract class type.  */
   16484      215281 :         if (VOID_TYPE_P (type)
   16485      215281 :             || TREE_CODE (type) == FUNCTION_TYPE
   16486      215271 :             || (TREE_CODE (type) == ARRAY_TYPE
   16487        1547 :                 && TYPE_DOMAIN (type) == NULL_TREE)
   16488      430542 :             || TYPE_REF_P (type))
   16489             :           {
   16490          20 :             if (complain & tf_error)
   16491           4 :               error ("creating array of %qT", type);
   16492          20 :             return error_mark_node;
   16493             :           }
   16494             : 
   16495      215261 :         if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
   16496      215261 :                                   !(complain & tf_error)))
   16497           0 :           return error_mark_node;
   16498             : 
   16499      215261 :         r = build_cplus_array_type (type, domain);
   16500             : 
   16501      215261 :         if (!valid_array_size_p (input_location, r, in_decl,
   16502             :                                  (complain & tf_error)))
   16503          20 :           return error_mark_node;
   16504             : 
   16505      215241 :         if (TYPE_USER_ALIGN (t))
   16506             :           {
   16507           8 :             SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
   16508           8 :             TYPE_USER_ALIGN (r) = 1;
   16509             :           }
   16510             : 
   16511             :         return r;
   16512             :       }
   16513             : 
   16514    66544689 :     case TYPENAME_TYPE:
   16515    66544689 :       {
   16516    66544689 :         tree ctx = TYPE_CONTEXT (t);
   16517    66544689 :         if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
   16518             :           {
   16519           6 :             ctx = tsubst_pack_expansion (ctx, args,
   16520             :                                          complain | tf_qualifying_scope,
   16521             :                                          in_decl);
   16522           6 :             if (ctx == error_mark_node
   16523          12 :                 || TREE_VEC_LENGTH (ctx) > 1)
   16524             :               return error_mark_node;
   16525           6 :             if (TREE_VEC_LENGTH (ctx) == 0)
   16526             :               {
   16527           3 :                 if (complain & tf_error)
   16528           6 :                   error ("%qD is instantiated for an empty pack",
   16529           3 :                          TYPENAME_TYPE_FULLNAME (t));
   16530           3 :                 return error_mark_node;
   16531             :               }
   16532           3 :             ctx = TREE_VEC_ELT (ctx, 0);
   16533             :           }
   16534             :         else
   16535    66544683 :           ctx = tsubst_aggr_type (ctx, args,
   16536             :                                   complain | tf_qualifying_scope,
   16537             :                                   in_decl, /*entering_scope=*/1);
   16538    66544686 :         if (ctx == error_mark_node)
   16539             :           return error_mark_node;
   16540             : 
   16541    66480988 :         tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
   16542             :                               complain, in_decl);
   16543    66480988 :         if (f == error_mark_node)
   16544             :           return error_mark_node;
   16545             : 
   16546   132240965 :         if (!MAYBE_CLASS_TYPE_P (ctx))
   16547             :           {
   16548        7822 :             if (complain & tf_error)
   16549          75 :               error ("%qT is not a class, struct, or union type", ctx);
   16550        7822 :             return error_mark_node;
   16551             :           }
   16552    66473120 :         else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
   16553             :           {
   16554             :             /* Normally, make_typename_type does not require that the CTX
   16555             :                have complete type in order to allow things like:
   16556             : 
   16557             :                  template <class T> struct S { typename S<T>::X Y; };
   16558             : 
   16559             :                But, such constructs have already been resolved by this
   16560             :                point, so here CTX really should have complete type, unless
   16561             :                it's a partial instantiation.  */
   16562    50457017 :             if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain))
   16563        2230 :               return error_mark_node;
   16564             :           }
   16565             : 
   16566             :         /* FIXME: TYPENAME_IS_CLASS_P conflates 'class' vs 'struct' vs 'union'
   16567             :            tags.  TYPENAME_TYPE should probably remember the exact tag that
   16568             :            was written.  */
   16569    66470890 :         enum tag_types tag_type
   16570    66470890 :           = TYPENAME_IS_CLASS_P (t) ? class_type
   16571    66470822 :           : TYPENAME_IS_ENUM_P (t) ? enum_type
   16572    66470890 :           : typename_type;
   16573    66470890 :         tsubst_flags_t tcomplain = complain | tf_keep_type_decl;
   16574    66470890 :         tcomplain |= tst_ok_flag | qualifying_scope_flag;
   16575    66470890 :         f = make_typename_type (ctx, f, tag_type, tcomplain);
   16576    66470890 :         if (f == error_mark_node)
   16577             :           return f;
   16578    55831626 :         if (TREE_CODE (f) == TYPE_DECL)
   16579             :           {
   16580    39130448 :             complain |= tf_ignore_bad_quals;
   16581    39130448 :             f = TREE_TYPE (f);
   16582             :           }
   16583             : 
   16584    55831626 :         if (TREE_CODE (f) != TYPENAME_TYPE)
   16585             :           {
   16586    40517862 :             if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
   16587             :               {
   16588           4 :                 if (complain & tf_error)
   16589           4 :                   error ("%qT resolves to %qT, which is not an enumeration type",
   16590             :                          t, f);
   16591             :                 else
   16592             :                   return error_mark_node;
   16593             :               }
   16594    40517858 :             else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
   16595             :               {
   16596           8 :                 if (complain & tf_error)
   16597           4 :                   error ("%qT resolves to %qT, which is not a class type",
   16598             :                          t, f);
   16599             :                 else
   16600             :                   return error_mark_node;
   16601             :               }
   16602             :           }
   16603             : 
   16604    55831622 :         return cp_build_qualified_type
   16605    55831622 :           (f, cp_type_quals (f) | cp_type_quals (t), complain);
   16606             :       }
   16607             : 
   16608        8887 :     case UNBOUND_CLASS_TEMPLATE:
   16609        8887 :       {
   16610        8887 :         tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
   16611             :                                      in_decl, /*entering_scope=*/1);
   16612        8887 :         tree name = TYPE_IDENTIFIER (t);
   16613        8887 :         tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
   16614             : 
   16615        8887 :         if (ctx == error_mark_node || name == error_mark_node)
   16616             :           return error_mark_node;
   16617             : 
   16618        8887 :         if (parm_list)
   16619          12 :           parm_list = tsubst_template_parms (parm_list, args, complain);
   16620        8887 :         return make_unbound_class_template (ctx, name, parm_list, complain);
   16621             :       }
   16622             : 
   16623         216 :     case TYPEOF_TYPE:
   16624         216 :       {
   16625         216 :         tree type;
   16626             : 
   16627         216 :         ++cp_unevaluated_operand;
   16628         216 :         ++c_inhibit_evaluation_warnings;
   16629             : 
   16630         216 :         type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args, complain, in_decl);
   16631             : 
   16632         216 :         --cp_unevaluated_operand;
   16633         216 :         --c_inhibit_evaluation_warnings;
   16634             : 
   16635         216 :         type = finish_typeof (type);
   16636         216 :         return cp_build_qualified_type (type,
   16637         216 :                                         cp_type_quals (t)
   16638         216 :                                         | cp_type_quals (type),
   16639         216 :                                         complain);
   16640             :       }
   16641             : 
   16642     4816033 :     case DECLTYPE_TYPE:
   16643     4816033 :       {
   16644     4816033 :         tree type;
   16645             : 
   16646     4816033 :         ++cp_unevaluated_operand;
   16647     4816033 :         ++c_inhibit_evaluation_warnings;
   16648             : 
   16649     4816033 :         type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
   16650             :                                       complain|tf_decltype, in_decl);
   16651             : 
   16652     4805233 :         --cp_unevaluated_operand;
   16653     4805233 :         --c_inhibit_evaluation_warnings;
   16654             : 
   16655     4805233 :         if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
   16656       43568 :           type = lambda_capture_field_type (type,
   16657             :                                             false /*explicit_init*/,
   16658       21784 :                                             DECLTYPE_FOR_REF_CAPTURE (t));
   16659     4783449 :         else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
   16660           0 :           type = lambda_proxy_type (type);
   16661             :         else
   16662             :           {
   16663     4783449 :             bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
   16664       25674 :             if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
   16665     4783449 :                 && EXPR_P (type))
   16666             :               /* In a template ~id could be either a complement expression
   16667             :                  or an unqualified-id naming a destructor; if instantiating
   16668             :                  it produces an expression, it's not an id-expression or
   16669             :                  member access.  */
   16670             :               id = false;
   16671     4783449 :             type = finish_decltype_type (type, id, complain);
   16672             :           }
   16673     4805233 :         return cp_build_qualified_type (type,
   16674     4805233 :                                         cp_type_quals (t)
   16675     4805233 :                                         | cp_type_quals (type),
   16676     4805233 :                                         complain | tf_ignore_bad_quals);
   16677             :       }
   16678             : 
   16679     1060497 :     case TRAIT_TYPE:
   16680     1060497 :       {
   16681     1060497 :         tree type1 = TRAIT_TYPE_TYPE1 (t);
   16682     1060497 :         if (TYPE_P (type1))
   16683     1002998 :           type1 = tsubst (type1, args, complain, in_decl);
   16684             :         else
   16685       57499 :           type1 = tsubst_copy_and_build (type1, args, complain, in_decl);
   16686     1060497 :         tree type2 = tsubst (TRAIT_TYPE_TYPE2 (t), args, complain, in_decl);
   16687     1060497 :         type = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2, complain);
   16688     1060497 :         return cp_build_qualified_type (type,
   16689     1060497 :                                         cp_type_quals (t) | cp_type_quals (type),
   16690     1060497 :                                         complain | tf_ignore_bad_quals);
   16691             :       }
   16692             : 
   16693           0 :     case TYPE_ARGUMENT_PACK:
   16694           0 :     case NONTYPE_ARGUMENT_PACK:
   16695           0 :       return tsubst_argument_pack (t, args, complain, in_decl);
   16696             : 
   16697           0 :     case VOID_CST:
   16698           0 :     case INTEGER_CST:
   16699           0 :     case REAL_CST:
   16700           0 :     case STRING_CST:
   16701           0 :     case PLUS_EXPR:
   16702           0 :     case MINUS_EXPR:
   16703           0 :     case NEGATE_EXPR:
   16704           0 :     case NOP_EXPR:
   16705           0 :     case INDIRECT_REF:
   16706           0 :     case ADDR_EXPR:
   16707           0 :     case CALL_EXPR:
   16708           0 :     case ARRAY_REF:
   16709           0 :     case SCOPE_REF:
   16710             :       /* We should use one of the expression tsubsts for these codes.  */
   16711           0 :       gcc_unreachable ();
   16712             : 
   16713           0 :     default:
   16714           0 :       sorry ("use of %qs in template", get_tree_code_name (code));
   16715           0 :       return error_mark_node;
   16716             :     }
   16717             : }
   16718             : 
   16719             : /* Convenience wrapper over tsubst for substituting into the LHS
   16720             :    of the :: scope resolution operator.  */
   16721             : 
   16722             : static tree
   16723    46641434 : tsubst_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   16724             : {
   16725    46641434 :   gcc_checking_assert (TYPE_P (t));
   16726    46641434 :   return tsubst (t, args, complain | tf_qualifying_scope, in_decl);
   16727             : }
   16728             : 
   16729             : /* OLDFNS is a lookup set of member functions from some class template, and
   16730             :    NEWFNS is a lookup set of member functions from NEWTYPE, a specialization
   16731             :    of that class template.  Return the subset of NEWFNS which are
   16732             :    specializations of a function from OLDFNS.  */
   16733             : 
   16734             : static tree
   16735     1465513 : filter_memfn_lookup (tree oldfns, tree newfns, tree newtype)
   16736             : {
   16737             :   /* Record all member functions from the old lookup set OLDFNS into
   16738             :      VISIBLE_SET.  */
   16739     1465513 :   hash_set<tree> visible_set;
   16740     1465513 :   bool seen_dep_using = false;
   16741     4437184 :   for (tree fn : lkp_range (oldfns))
   16742             :     {
   16743     1506158 :       if (TREE_CODE (fn) == USING_DECL)
   16744             :         {
   16745             :           /* Imprecisely handle dependent using-decl by keeping all members
   16746             :              in the new lookup set that are defined in a base class, i.e.
   16747             :              members that could plausibly have been introduced by this
   16748             :              dependent using-decl.
   16749             :              FIXME: Track which members are introduced by a dependent
   16750             :              using-decl precisely, perhaps by performing another lookup
   16751             :              from the substituted USING_DECL_SCOPE.  */
   16752           0 :           gcc_checking_assert (DECL_DEPENDENT_P (fn));
   16753             :           seen_dep_using = true;
   16754             :         }
   16755             :       else
   16756     1506158 :         visible_set.add (fn);
   16757             :     }
   16758             : 
   16759             :   /* Returns true iff (a less specialized version of) FN appeared in
   16760             :      the old lookup set OLDFNS.  */
   16761     5992900 :   auto visible_p = [newtype, seen_dep_using, &visible_set] (tree fn) {
   16762     1509117 :     if (DECL_CONTEXT (fn) != newtype)
   16763             :       /* FN is a member function from a base class, introduced via a
   16764             :          using-decl; if it might have been introduced by a dependent
   16765             :          using-decl then just conservatively keep it, otherwise look
   16766             :          in the old lookup set for FN exactly.  */
   16767          36 :       return seen_dep_using || visible_set.contains (fn);
   16768     1509081 :     else if (TREE_CODE (fn) == TEMPLATE_DECL)
   16769             :       /* FN is a member function template from the current class;
   16770             :          look in the old lookup set for the TEMPLATE_DECL from which
   16771             :          it was specialized.  */
   16772      605144 :       return visible_set.contains (DECL_TI_TEMPLATE (fn));
   16773             :     else
   16774             :       /* FN is a non-template member function from the current class;
   16775             :          look in the old lookup set for the FUNCTION_DECL from which
   16776             :          it was specialized.  */
   16777      903937 :       return visible_set.contains (DECL_TEMPLATE_RESULT
   16778      903937 :                                    (DECL_TI_TEMPLATE (fn)));
   16779     1465513 :   };
   16780             : 
   16781     1465513 :   bool lookup_changed_p = false;
   16782     4437023 :   for (tree fn : lkp_range (newfns))
   16783     1506161 :     if (!visible_p (fn))
   16784             :       {
   16785             :         lookup_changed_p = true;
   16786             :         break;
   16787             :       }
   16788     1465513 :   if (!lookup_changed_p)
   16789             :     return newfns;
   16790             : 
   16791             :   /* Filter out from NEWFNS the member functions that weren't
   16792             :      previously visible according to OLDFNS.  */
   16793         164 :   tree filtered_fns = NULL_TREE;
   16794         164 :   unsigned filtered_size = 0;
   16795        3284 :   for (tree fn : lkp_range (newfns))
   16796        2956 :     if (visible_p (fn))
   16797             :       {
   16798         164 :         filtered_fns = lookup_add (fn, filtered_fns);
   16799         164 :         filtered_size++;
   16800             :       }
   16801         164 :   gcc_checking_assert (seen_dep_using
   16802             :                        ? filtered_size >= visible_set.elements ()
   16803             :                        : filtered_size == visible_set.elements ());
   16804             : 
   16805             :   return filtered_fns;
   16806     1465513 : }
   16807             : 
   16808             : /* tsubst a BASELINK.  OBJECT_TYPE, if non-NULL, is the type of the
   16809             :    expression on the left-hand side of the "." or "->" operator.  We
   16810             :    only do the lookup if we had a dependent BASELINK.  Otherwise we
   16811             :    adjust it onto the instantiated heirarchy.  */
   16812             : 
   16813             : static tree
   16814    12349961 : tsubst_baselink (tree baselink, tree object_type,
   16815             :                  tree args, tsubst_flags_t complain, tree in_decl)
   16816             : {
   16817    12349961 :   bool qualified_p = BASELINK_QUALIFIED_P (baselink);
   16818    12349961 :   tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
   16819    12349961 :   qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
   16820             : 
   16821    12349961 :   tree optype = BASELINK_OPTYPE (baselink);
   16822    12349961 :   optype = tsubst (optype, args, complain, in_decl);
   16823             : 
   16824    12349961 :   tree template_args = NULL_TREE;
   16825    12349961 :   bool template_id_p = false;
   16826    12349961 :   tree fns = BASELINK_FUNCTIONS (baselink);
   16827    12349961 :   if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
   16828             :     {
   16829     6268180 :       template_id_p = true;
   16830     6268180 :       template_args = TREE_OPERAND (fns, 1);
   16831     6268180 :       fns = TREE_OPERAND (fns, 0);
   16832     6268180 :       if (template_args)
   16833     6268180 :         template_args = tsubst_template_args (template_args, args,
   16834             :                                               complain, in_decl);
   16835             :     }
   16836             : 
   16837    12349961 :   tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
   16838    12349961 :   binfo_type = tsubst (binfo_type, args, complain, in_decl);
   16839    12349961 :   bool dependent_p = (binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink))
   16840    20286624 :                       || optype != BASELINK_OPTYPE (baselink));
   16841             : 
   16842     6757284 :   if (dependent_p)
   16843             :     {
   16844     5592677 :       tree name = OVL_NAME (fns);
   16845     5592677 :       if (IDENTIFIER_CONV_OP_P (name))
   16846          10 :         name = make_conv_op_name (optype);
   16847             : 
   16848             :       /* See maybe_dependent_member_ref.  */
   16849     5592677 :       if ((complain & tf_dguide) && dependent_scope_p (qualifying_scope))
   16850             :         {
   16851         295 :           if (template_id_p)
   16852         277 :             name = build2 (TEMPLATE_ID_EXPR, unknown_type_node, name,
   16853             :                            template_args);
   16854         295 :           return build_qualified_name (NULL_TREE, qualifying_scope, name,
   16855         295 :                                        /* ::template */false);
   16856             :         }
   16857             : 
   16858     5592382 :       if (name == complete_dtor_identifier)
   16859             :         /* Treat as-if non-dependent below.  */
   16860           0 :         dependent_p = false;
   16861             : 
   16862     5592382 :       bool maybe_incomplete = BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink);
   16863     5592382 :       baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
   16864             :                                   complain);
   16865     5592382 :       if (maybe_incomplete)
   16866             :         {
   16867             :           /* Filter out from the new lookup set those functions which didn't
   16868             :              appear in the original lookup set (in a less specialized form).
   16869             :              This is needed to preserve the consistency of member lookup
   16870             :              performed in an incomplete-class context, within which
   16871             :              later-declared members ought to remain invisible.  */
   16872     1465513 :           BASELINK_FUNCTIONS (baselink)
   16873     1465513 :             = filter_memfn_lookup (fns, BASELINK_FUNCTIONS (baselink),
   16874             :                                    binfo_type);
   16875     1465513 :           BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink) = true;
   16876             :         }
   16877             : 
   16878     5592382 :       if (!baselink)
   16879             :         {
   16880           0 :           if ((complain & tf_error)
   16881           0 :               && constructor_name_p (name, qualifying_scope))
   16882           0 :             error ("cannot call constructor %<%T::%D%> directly",
   16883             :                    qualifying_scope, name);
   16884           0 :           return error_mark_node;
   16885             :         }
   16886             : 
   16887     5592382 :       fns = BASELINK_FUNCTIONS (baselink);
   16888             :     }
   16889             :   else
   16890             :     {
   16891             :       /* We're going to overwrite pieces below, make a duplicate.  */
   16892     6757284 :       baselink = copy_node (baselink);
   16893             : 
   16894     6757284 :       if (qualifying_scope != BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)))
   16895             :         {
   16896             :           /* The decl we found was from non-dependent scope, but we still need
   16897             :              to update the binfos for the instantiated qualifying_scope.  */
   16898      197358 :           BASELINK_ACCESS_BINFO (baselink) = TYPE_BINFO (qualifying_scope);
   16899      197358 :           BASELINK_BINFO (baselink) = lookup_base (qualifying_scope, binfo_type,
   16900             :                                                    ba_unique, nullptr, complain);
   16901             :         }
   16902             :     }
   16903             : 
   16904             :   /* If lookup found a single function, mark it as used at this point.
   16905             :      (If lookup found multiple functions the one selected later by
   16906             :      overload resolution will be marked as used at that point.)  */
   16907    12349666 :   if (!template_id_p && !really_overloaded_fn (fns))
   16908             :     {
   16909     4201970 :       tree fn = OVL_FIRST (fns);
   16910     4201970 :       bool ok = mark_used (fn, complain);
   16911     4201970 :       if (!ok && !(complain & tf_error))
   16912          34 :         return error_mark_node;
   16913     4201933 :       if (ok && BASELINK_P (baselink))
   16914             :         /* We might have instantiated an auto function.  */
   16915     4201933 :         TREE_TYPE (baselink) = TREE_TYPE (fn);
   16916             :     }
   16917             : 
   16918    12349632 :   if (BASELINK_P (baselink))
   16919             :     {
   16920             :       /* Add back the template arguments, if present.  */
   16921    12349632 :       if (template_id_p)
   16922    12535806 :         BASELINK_FUNCTIONS (baselink)
   16923     6267903 :           = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
   16924             : 
   16925             :       /* Update the conversion operator type.  */
   16926    12349632 :       BASELINK_OPTYPE (baselink) = optype;
   16927             :     }
   16928             : 
   16929    12349632 :   if (!object_type)
   16930      237193 :     object_type = current_class_type;
   16931             : 
   16932    12349632 :   if (qualified_p || !dependent_p)
   16933             :     {
   16934     6759220 :       baselink = adjust_result_of_qualified_name_lookup (baselink,
   16935             :                                                          qualifying_scope,
   16936             :                                                          object_type);
   16937     6759220 :       if (!qualified_p)
   16938             :         /* We need to call adjust_result_of_qualified_name_lookup in case the
   16939             :            destructor names a base class, but we unset BASELINK_QUALIFIED_P
   16940             :            so that we still get virtual function binding.  */
   16941     1380790 :         BASELINK_QUALIFIED_P (baselink) = false;
   16942             :     }
   16943             : 
   16944             :   return baselink;
   16945             : }
   16946             : 
   16947             : /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID.  DONE is
   16948             :    true if the qualified-id will be a postfix-expression in-and-of
   16949             :    itself; false if more of the postfix-expression follows the
   16950             :    QUALIFIED_ID.  ADDRESS_P is true if the qualified-id is the operand
   16951             :    of "&".  */
   16952             : 
   16953             : static tree
   16954    49954388 : tsubst_qualified_id (tree qualified_id, tree args,
   16955             :                      tsubst_flags_t complain, tree in_decl,
   16956             :                      bool done, bool address_p)
   16957             : {
   16958    49954388 :   tree expr;
   16959    49954388 :   tree scope;
   16960    49954388 :   tree name;
   16961    49954388 :   bool is_template;
   16962    49954388 :   tree template_args;
   16963    49954388 :   location_t loc = EXPR_LOCATION (qualified_id);
   16964             : 
   16965    49954388 :   gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
   16966             : 
   16967             :   /* Figure out what name to look up.  */
   16968    49954388 :   name = TREE_OPERAND (qualified_id, 1);
   16969    49954388 :   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
   16970             :     {
   16971     6079024 :       is_template = true;
   16972     6079024 :       template_args = TREE_OPERAND (name, 1);
   16973     6079024 :       if (template_args)
   16974     6079024 :         template_args = tsubst_template_args (template_args, args,
   16975             :                                               complain, in_decl);
   16976     6079024 :       if (template_args == error_mark_node)
   16977             :         return error_mark_node;
   16978     6079018 :       name = TREE_OPERAND (name, 0);
   16979             :     }
   16980             :   else
   16981             :     {
   16982             :       is_template = false;
   16983             :       template_args = NULL_TREE;
   16984             :     }
   16985             : 
   16986             :   /* Substitute into the qualifying scope.  When there are no ARGS, we
   16987             :      are just trying to simplify a non-dependent expression.  In that
   16988             :      case the qualifying scope may be dependent, and, in any case,
   16989             :      substituting will not help.  */
   16990    49954382 :   scope = TREE_OPERAND (qualified_id, 0);
   16991    49954382 :   if (args)
   16992             :     {
   16993    45746711 :       scope = tsubst_scope (scope, args, complain, in_decl);
   16994    45746711 :       expr = tsubst_copy (name, args, complain, in_decl);
   16995             :     }
   16996             :   else
   16997             :     expr = name;
   16998             : 
   16999    49954382 :   if (dependent_scope_p (scope))
   17000             :     {
   17001    14219691 :       if (TREE_CODE (expr) == SCOPE_REF)
   17002             :         /* We built one in tsubst_baselink.  */
   17003           8 :         gcc_checking_assert (same_type_p (scope, TREE_OPERAND (expr, 0)));
   17004             :       else
   17005             :         {
   17006    14219683 :           if (is_template)
   17007     2468785 :             expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr,
   17008             :                                      template_args);
   17009    14219683 :           expr = build_qualified_name (NULL_TREE, scope, expr,
   17010    14219683 :                                        QUALIFIED_NAME_IS_TEMPLATE
   17011             :                                        (qualified_id));
   17012             :         }
   17013    14219691 :       REF_PARENTHESIZED_P (expr) = REF_PARENTHESIZED_P (qualified_id);
   17014    14219691 :       return expr;
   17015             :     }
   17016             : 
   17017    35734691 :   if (!BASELINK_P (name) && !DECL_P (expr))
   17018             :     {
   17019    30684137 :       if (TREE_CODE (expr) == BIT_NOT_EXPR)
   17020             :         {
   17021             :           /* A BIT_NOT_EXPR is used to represent a destructor.  */
   17022           8 :           if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
   17023             :             {
   17024           0 :               error ("qualifying type %qT does not match destructor name ~%qT",
   17025           0 :                      scope, TREE_OPERAND (expr, 0));
   17026           0 :               expr = error_mark_node;
   17027             :             }
   17028             :           else
   17029           8 :             expr = lookup_qualified_name (scope, complete_dtor_identifier,
   17030             :                                           LOOK_want::NORMAL, false);
   17031             :         }
   17032             :       else
   17033    30684129 :         expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
   17034    30684073 :       if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
   17035             :                      ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
   17036             :         {
   17037          33 :           if (complain & tf_error)
   17038             :             {
   17039          27 :               error ("dependent-name %qE is parsed as a non-type, but "
   17040             :                      "instantiation yields a type", qualified_id);
   17041          27 :               inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
   17042             :             }
   17043          33 :           return error_mark_node;
   17044             :         }
   17045             :     }
   17046             : 
   17047    35734594 :   if (DECL_P (expr))
   17048             :     {
   17049    28952997 :       if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
   17050             :                                                 scope, complain))
   17051           5 :         return error_mark_node;
   17052             :       /* Remember that there was a reference to this entity.  */
   17053    28952992 :       if (!mark_used (expr, complain) && !(complain & tf_error))
   17054           0 :         return error_mark_node;
   17055             :     }
   17056             : 
   17057    35730981 :   if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
   17058             :     {
   17059       42411 :       if (complain & tf_error)
   17060         114 :         qualified_name_lookup_error (scope,
   17061         114 :                                      TREE_OPERAND (qualified_id, 1),
   17062             :                                      expr, input_location);
   17063       42411 :       return error_mark_node;
   17064             :     }
   17065             : 
   17066    35688570 :   if (is_template)
   17067             :     {
   17068             :       /* We may be repeating a check already done during parsing, but
   17069             :          if it was well-formed and passed then, it will pass again
   17070             :          now, and if it didn't, we wouldn't have got here.  The case
   17071             :          we want to catch is when we couldn't tell then, and can now,
   17072             :          namely when templ prior to substitution was an
   17073             :          identifier.  */
   17074     3609796 :       if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
   17075           0 :         return error_mark_node;
   17076             : 
   17077     3609796 :       if (variable_template_p (expr))
   17078        9579 :         expr = lookup_and_finish_template_variable (expr, template_args,
   17079             :                                                     complain);
   17080             :       else
   17081     3600217 :         expr = lookup_template_function (expr, template_args);
   17082             :     }
   17083             : 
   17084    35688570 :   if (expr == error_mark_node && complain & tf_error)
   17085           4 :     qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
   17086             :                                  expr, input_location);
   17087    35688566 :   else if (TYPE_P (scope))
   17088             :     {
   17089    35688566 :       expr = (adjust_result_of_qualified_name_lookup
   17090    35688566 :               (expr, scope, current_nonlambda_class_type ()));
   17091    35688566 :       expr = (finish_qualified_id_expr
   17092    35688577 :               (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
   17093    35688566 :                QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
   17094             :                /*template_arg_p=*/false, complain));
   17095             :     }
   17096             : 
   17097             :   /* Expressions do not generally have reference type.  */
   17098    35688570 :   if (TREE_CODE (expr) != SCOPE_REF
   17099             :       /* However, if we're about to form a pointer-to-member, we just
   17100             :          want the referenced member referenced.  */
   17101    33120490 :       && TREE_CODE (expr) != OFFSET_REF)
   17102    33097540 :     expr = convert_from_reference (expr);
   17103             : 
   17104    35688570 :   if (REF_PARENTHESIZED_P (qualified_id))
   17105         150 :     expr = force_paren_expr (expr);
   17106             : 
   17107    35688570 :   expr = maybe_wrap_with_location (expr, loc);
   17108             : 
   17109    35688570 :   return expr;
   17110             : }
   17111             : 
   17112             : /* tsubst the initializer for a VAR_DECL.  INIT is the unsubstituted
   17113             :    initializer, DECL is the substituted VAR_DECL.  Other arguments are as
   17114             :    for tsubst.  */
   17115             : 
   17116             : static tree
   17117    14335369 : tsubst_init (tree init, tree decl, tree args,
   17118             :              tsubst_flags_t complain, tree in_decl)
   17119             : {
   17120    14335369 :   if (!init)
   17121             :     return NULL_TREE;
   17122             : 
   17123    11360410 :   init = tsubst_expr (init, args, complain, in_decl);
   17124             : 
   17125    11360410 :   tree type = TREE_TYPE (decl);
   17126             : 
   17127    11360410 :   if (!init && type != error_mark_node)
   17128             :     {
   17129          41 :       if (tree auto_node = type_uses_auto (type))
   17130             :         {
   17131           6 :           if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
   17132             :             {
   17133           6 :               if (complain & tf_error)
   17134           6 :                 error ("initializer for %q#D expands to an empty list "
   17135             :                        "of expressions", decl);
   17136           6 :               return error_mark_node;
   17137             :             }
   17138             :         }
   17139          35 :       else if (!dependent_type_p (type))
   17140             :         {
   17141             :           /* If we had an initializer but it
   17142             :              instantiated to nothing,
   17143             :              value-initialize the object.  This will
   17144             :              only occur when the initializer was a
   17145             :              pack expansion where the parameter packs
   17146             :              used in that expansion were of length
   17147             :              zero.  */
   17148          35 :           init = build_value_init (type, complain);
   17149          35 :           if (TREE_CODE (init) == AGGR_INIT_EXPR)
   17150          13 :             init = get_target_expr (init, complain);
   17151          35 :           if (TREE_CODE (init) == TARGET_EXPR)
   17152          13 :             TARGET_EXPR_DIRECT_INIT_P (init) = true;
   17153             :         }
   17154             :     }
   17155             : 
   17156             :   return init;
   17157             : }
   17158             : 
   17159             : /* If T is a reference to a dependent member of the current instantiation C and
   17160             :    we are trying to refer to that member in a partial instantiation of C,
   17161             :    return a SCOPE_REF; otherwise, return NULL_TREE.
   17162             : 
   17163             :    This can happen when forming a C++17 deduction guide, as in PR96199.  */
   17164             : 
   17165             : static tree
   17166  3568535154 : maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
   17167             :                             tree in_decl)
   17168             : {
   17169  3568535154 :   if (!(complain & tf_dguide))
   17170             :     return NULL_TREE;
   17171             : 
   17172  3568675708 :   tree decl = (t && TYPE_P (t)) ? TYPE_NAME (t) : t;
   17173      144474 :   if (!decl || !DECL_P (decl))
   17174             :     return NULL_TREE;
   17175             : 
   17176      102508 :   tree ctx = context_for_name_lookup (decl);
   17177      102508 :   if (!CLASS_TYPE_P (ctx))
   17178             :     return NULL_TREE;
   17179             : 
   17180       12417 :   ctx = tsubst (ctx, args, complain, in_decl);
   17181       12417 :   if (!dependent_scope_p (ctx))
   17182             :     return NULL_TREE;
   17183             : 
   17184       12360 :   if (TYPE_P (t))
   17185             :     {
   17186        6800 :       if (typedef_variant_p (t))
   17187        3298 :         t = strip_typedefs (t);
   17188        6800 :       tree decl = TYPE_NAME (t);
   17189        6800 :       if (decl)
   17190        6548 :         decl = maybe_dependent_member_ref (decl, args, complain, in_decl);
   17191        6548 :       if (!decl)
   17192        4962 :         return NULL_TREE;
   17193        1838 :       return cp_build_qualified_type (TREE_TYPE (decl), cp_type_quals (t),
   17194        1838 :                                       complain);
   17195             :     }
   17196             : 
   17197        5560 :   tree name = DECL_NAME (t);
   17198        5560 :   tree fullname = name;
   17199        5560 :   if (instantiates_primary_template_p (t))
   17200             :     {
   17201        1459 :       tree tinfo = get_template_info (t);
   17202        1459 :       name = DECL_NAME (TI_TEMPLATE (tinfo));
   17203        1459 :       tree targs = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
   17204        1459 :       targs = tsubst_template_args (targs, args, complain, in_decl);
   17205        1459 :       fullname = build_nt (TEMPLATE_ID_EXPR, name, targs);
   17206             :     }
   17207             : 
   17208        5560 :   if (TREE_CODE (t) == TYPE_DECL)
   17209             :     {
   17210        5316 :       if (TREE_CODE (TREE_TYPE (t)) == TYPENAME_TYPE
   17211        5316 :           && TYPE_NAME (TREE_TYPE (t)) == t)
   17212             :         /* The TYPE_DECL for a typename has DECL_CONTEXT of the typename
   17213             :            scope, but it doesn't need to be rewritten again.  */
   17214             :         return NULL_TREE;
   17215        1838 :       tree type = build_typename_type (ctx, name, fullname, typename_type);
   17216        1838 :       return TYPE_NAME (type);
   17217             :     }
   17218         244 :   else if (DECL_TYPE_TEMPLATE_P (t))
   17219           8 :     return make_unbound_class_template (ctx, name,
   17220           8 :                                         NULL_TREE, complain);
   17221             :   else
   17222         236 :     return build_qualified_name (NULL_TREE, ctx, fullname,
   17223         236 :                                  TREE_CODE (t) == TEMPLATE_DECL);
   17224             : }
   17225             : 
   17226             : /* Like tsubst, but deals with expressions.  This function just replaces
   17227             :    template parms; to finish processing the resultant expression, use
   17228             :    tsubst_copy_and_build or tsubst_expr.  */
   17229             : 
   17230             : static tree
   17231   491906472 : tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   17232             : {
   17233   491906480 :   enum tree_code code;
   17234   491906480 :   tree r;
   17235             : 
   17236   491906480 :   if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
   17237             :     return t;
   17238             : 
   17239   420267380 :   if (TYPE_P (t))
   17240     4637471 :     return tsubst (t, args, complain, in_decl);
   17241             : 
   17242   415629909 :   if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
   17243             :     return d;
   17244             : 
   17245   415629665 :   code = TREE_CODE (t);
   17246             : 
   17247   415629665 :   switch (code)
   17248             :     {
   17249    34433863 :     case PARM_DECL:
   17250    34433863 :       r = retrieve_local_specialization (t);
   17251             : 
   17252    34433863 :       if (r == NULL_TREE)
   17253             :         {
   17254             :           /* We get here for a use of 'this' in an NSDMI.  */
   17255      204623 :           if (DECL_NAME (t) == this_identifier && current_class_ptr)
   17256       73738 :             return current_class_ptr;
   17257             : 
   17258             :           /* This can happen for a parameter name used later in a function
   17259             :              declaration (such as in a late-specified return type).  Just
   17260             :              make a dummy decl, since it's only used for its type.  */
   17261      130885 :           gcc_assert (cp_unevaluated_operand);
   17262      130885 :           r = tsubst_decl (t, args, complain);
   17263             :           /* Give it the template pattern as its context; its true context
   17264             :              hasn't been instantiated yet and this is good enough for
   17265             :              mangling.  */
   17266      130885 :           DECL_CONTEXT (r) = DECL_CONTEXT (t);
   17267             :         }
   17268             : 
   17269    34360125 :       if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
   17270      234094 :         r = argument_pack_select_arg (r);
   17271    34360125 :       if (!mark_used (r, complain) && !(complain & tf_error))
   17272           0 :         return error_mark_node;
   17273             :       return r;
   17274             : 
   17275      819750 :     case CONST_DECL:
   17276      819750 :       {
   17277      819750 :         tree enum_type;
   17278      819750 :         tree v;
   17279             : 
   17280      819750 :         if (DECL_TEMPLATE_PARM_P (t))
   17281           8 :           return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
   17282      819742 :         if (!uses_template_parms (DECL_CONTEXT (t)))
   17283             :           return t;
   17284             : 
   17285             :         /* Unfortunately, we cannot just call lookup_name here.
   17286             :            Consider:
   17287             : 
   17288             :              template <int I> int f() {
   17289             :              enum E { a = I };
   17290             :              struct S { void g() { E e = a; } };
   17291             :              };
   17292             : 
   17293             :            When we instantiate f<7>::S::g(), say, lookup_name is not
   17294             :            clever enough to find f<7>::a.  */
   17295      160675 :         enum_type
   17296      160675 :           = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
   17297             :                               /*entering_scope=*/0);
   17298             : 
   17299      160675 :         for (v = TYPE_VALUES (enum_type);
   17300      163188 :              v != NULL_TREE;
   17301        2513 :              v = TREE_CHAIN (v))
   17302      163188 :           if (TREE_PURPOSE (v) == DECL_NAME (t))
   17303      160675 :             return TREE_VALUE (v);
   17304             : 
   17305             :           /* We didn't find the name.  That should never happen; if
   17306             :              name-lookup found it during preliminary parsing, we
   17307             :              should find it again here during instantiation.  */
   17308           0 :         gcc_unreachable ();
   17309             :       }
   17310             :       return t;
   17311             : 
   17312     3743755 :     case FIELD_DECL:
   17313     3743755 :       if (DECL_CONTEXT (t))
   17314             :         {
   17315     3743755 :           tree ctx;
   17316             : 
   17317     3743755 :           ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
   17318             :                                   /*entering_scope=*/1);
   17319     3743755 :           if (ctx != DECL_CONTEXT (t))
   17320             :             {
   17321     3596805 :               tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
   17322     3596805 :               if (!r)
   17323             :                 {
   17324           0 :                   if (complain & tf_error)
   17325           0 :                     error ("using invalid field %qD", t);
   17326           0 :                   return error_mark_node;
   17327             :                 }
   17328             :               return r;
   17329             :             }
   17330             :         }
   17331             : 
   17332             :       return t;
   17333             : 
   17334    42206418 :     case VAR_DECL:
   17335    42206418 :     case FUNCTION_DECL:
   17336    42206418 :       if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
   17337     2058665 :         r = tsubst (t, args, complain, in_decl);
   17338    40147753 :       else if (DECL_LOCAL_DECL_P (t))
   17339             :         {
   17340             :           /* Local specialization will usually have been created when
   17341             :              we instantiated the DECL_EXPR_DECL. */
   17342         367 :           r = retrieve_local_specialization (t);
   17343         367 :           if (!r)
   17344             :             {
   17345             :               /* We're in a generic lambda referencing a local extern
   17346             :                  from an outer block-scope of a non-template.  */
   17347          18 :               gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl));
   17348             :               r = t;
   17349             :             }
   17350             :         }
   17351    40147386 :       else if (local_variable_p (t)
   17352    40147386 :                && uses_template_parms (DECL_CONTEXT (t)))
   17353             :         {
   17354    36311337 :           r = retrieve_local_specialization (t);
   17355    36311337 :           if (r == NULL_TREE)
   17356             :             {
   17357             :               /* First try name lookup to find the instantiation.  */
   17358          78 :               r = lookup_name (DECL_NAME (t));
   17359          78 :               if (r)
   17360             :                 {
   17361          61 :                   if (!VAR_P (r))
   17362             :                     {
   17363             :                       /* During error-recovery we may find a non-variable,
   17364             :                          even an OVERLOAD: just bail out and avoid ICEs and
   17365             :                          duplicate diagnostics (c++/62207).  */
   17366           8 :                       gcc_assert (seen_error ());
   17367           8 :                       return error_mark_node;
   17368             :                     }
   17369          53 :                   if (!is_capture_proxy (r))
   17370             :                     {
   17371             :                       /* Make sure the one we found is the one we want.  */
   17372          53 :                       tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
   17373          53 :                       if (ctx != DECL_CONTEXT (r))
   17374             :                         r = NULL_TREE;
   17375             :                     }
   17376             :                 }
   17377             : 
   17378             :               if (r)
   17379             :                 /* OK */;
   17380             :               else
   17381             :                 {
   17382             :                   /* This can happen for a variable used in a
   17383             :                      late-specified return type of a local lambda, or for a
   17384             :                      local static or constant.  Building a new VAR_DECL
   17385             :                      should be OK in all those cases.  */
   17386          17 :                   r = tsubst_decl (t, args, complain);
   17387          17 :                   if (local_specializations)
   17388             :                     /* Avoid infinite recursion (79640).  */
   17389          17 :                     register_local_specialization (r, t);
   17390          17 :                   if (decl_maybe_constant_var_p (r))
   17391             :                     {
   17392             :                       /* We can't call cp_finish_decl, so handle the
   17393             :                          initializer by hand.  */
   17394           0 :                       tree init = tsubst_init (DECL_INITIAL (t), r, args,
   17395             :                                                complain, in_decl);
   17396           0 :                       if (!processing_template_decl)
   17397           0 :                         init = maybe_constant_init (init);
   17398           0 :                       if (processing_template_decl
   17399           0 :                           ? potential_constant_expression (init)
   17400           0 :                           : reduced_constant_expression_p (init))
   17401           0 :                         DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
   17402           0 :                           = TREE_CONSTANT (r) = true;
   17403           0 :                       DECL_INITIAL (r) = init;
   17404           0 :                       if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
   17405           0 :                         TREE_TYPE (r)
   17406           0 :                           = do_auto_deduction (TREE_TYPE (r), init, auto_node,
   17407             :                                                complain, adc_variable_type);
   17408             :                     }
   17409          17 :                   gcc_assert (cp_unevaluated_operand
   17410             :                               || processing_contract_condition
   17411             :                               || TREE_STATIC (r)
   17412             :                               || decl_constant_var_p (r)
   17413             :                               || seen_error ());
   17414          17 :                   if (!processing_template_decl
   17415          17 :                       && !TREE_STATIC (r))
   17416          17 :                     r = process_outer_var_ref (r, complain);
   17417             :                 }
   17418             :               /* Remember this for subsequent uses.  */
   17419          70 :               if (local_specializations)
   17420          70 :                 register_local_specialization (r, t);
   17421             :             }
   17422    36311329 :           if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
   17423          59 :             r = argument_pack_select_arg (r);
   17424             :         }
   17425             :       else
   17426             :         r = t;
   17427    42206410 :       if (!mark_used (r, complain))
   17428          15 :         return error_mark_node;
   17429             :       return r;
   17430             : 
   17431             :     case NAMESPACE_DECL:
   17432             :       return t;
   17433             : 
   17434             :     case OVERLOAD:
   17435             :       return t;
   17436             : 
   17437         451 :     case BASELINK:
   17438         451 :       return tsubst_baselink (t, current_nonlambda_class_type (),
   17439         451 :                               args, complain, in_decl);
   17440             : 
   17441     2133426 :     case TEMPLATE_DECL:
   17442     2133426 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   17443         254 :         return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
   17444         254 :                        args, complain, in_decl);
   17445     2133172 :       else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
   17446           0 :         return tsubst (t, args, complain, in_decl);
   17447     4266344 :       else if (DECL_CLASS_SCOPE_P (t)
   17448     2224353 :                && uses_template_parms (DECL_CONTEXT (t)))
   17449             :         {
   17450             :           /* Template template argument like the following example need
   17451             :              special treatment:
   17452             : 
   17453             :                template <template <class> class TT> struct C {};
   17454             :                template <class T> struct D {
   17455             :                  template <class U> struct E {};
   17456             :                  C<E> c;                          // #1
   17457             :                };
   17458             :                D<int> d;                          // #2
   17459             : 
   17460             :              We are processing the template argument `E' in #1 for
   17461             :              the template instantiation #2.  Originally, `E' is a
   17462             :              TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT.  Now we
   17463             :              have to substitute this with one having context `D<int>'.  */
   17464             : 
   17465       74559 :           tree context = tsubst_aggr_type (DECL_CONTEXT (t), args, complain,
   17466             :                                            in_decl, /*entering_scope=*/true);
   17467       74559 :           return lookup_field (context, DECL_NAME(t), 0, false);
   17468             :         }
   17469             :       else
   17470             :         /* Ordinary template template argument.  */
   17471     2058613 :         return t;
   17472             : 
   17473           0 :     case NON_LVALUE_EXPR:
   17474           0 :     case VIEW_CONVERT_EXPR:
   17475           0 :         {
   17476             :           /* Handle location wrappers by substituting the wrapped node
   17477             :              first, *then* reusing the resulting type.  Doing the type
   17478             :              first ensures that we handle template parameters and
   17479             :              parameter pack expansions.  */
   17480           0 :           if (location_wrapper_p (t))
   17481             :             {
   17482           0 :               tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
   17483             :                                       complain, in_decl);
   17484           0 :               return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
   17485             :             }
   17486           0 :           tree op = TREE_OPERAND (t, 0);
   17487             :           /* force_paren_expr can also create a VIEW_CONVERT_EXPR.  */
   17488           0 :           if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
   17489             :             {
   17490           0 :               op = tsubst_copy (op, args, complain, in_decl);
   17491           0 :               op = build1 (code, TREE_TYPE (op), op);
   17492           0 :               REF_PARENTHESIZED_P (op) = true;
   17493           0 :               return op;
   17494             :             }
   17495             :           /* We shouldn't see any other uses of these in templates
   17496             :              (tsubst_copy_and_build handles C++20 tparm object wrappers).  */
   17497           0 :           gcc_unreachable ();
   17498             :         }
   17499             : 
   17500         123 :     case CAST_EXPR:
   17501         123 :     case REINTERPRET_CAST_EXPR:
   17502         123 :     case CONST_CAST_EXPR:
   17503         123 :     case STATIC_CAST_EXPR:
   17504         123 :     case DYNAMIC_CAST_EXPR:
   17505         123 :     case IMPLICIT_CONV_EXPR:
   17506         123 :     CASE_CONVERT:
   17507         123 :       {
   17508         123 :         tsubst_flags_t tcomplain = complain;
   17509         123 :         if (code == CAST_EXPR)
   17510           0 :           tcomplain |= tf_tst_ok;
   17511         123 :         tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
   17512         123 :         tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
   17513         123 :         return build1 (code, type, op0);
   17514             :       }
   17515             : 
   17516           0 :     case BIT_CAST_EXPR:
   17517           0 :       {
   17518           0 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   17519           0 :         tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
   17520           0 :         r = build_min (BIT_CAST_EXPR, type, op0);
   17521           0 :         SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
   17522           0 :         return r;
   17523             :       }
   17524             : 
   17525      674045 :     case SIZEOF_EXPR:
   17526      674862 :       if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
   17527      674057 :           || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
   17528             :         {
   17529      674045 :           tree expanded, op = TREE_OPERAND (t, 0);
   17530      674045 :           int len = 0;
   17531             : 
   17532      674045 :           if (SIZEOF_EXPR_TYPE_P (t))
   17533           0 :             op = TREE_TYPE (op);
   17534             : 
   17535      674045 :           ++cp_unevaluated_operand;
   17536      674045 :           ++c_inhibit_evaluation_warnings;
   17537             :           /* We only want to compute the number of arguments.  */
   17538      674045 :           if (PACK_EXPANSION_P (op))
   17539      674033 :             expanded = tsubst_pack_expansion (op, args, complain, in_decl);
   17540             :           else
   17541          12 :             expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
   17542             :                                              args, complain, in_decl);
   17543      674045 :           --cp_unevaluated_operand;
   17544      674045 :           --c_inhibit_evaluation_warnings;
   17545             : 
   17546      674045 :           if (TREE_CODE (expanded) == TREE_VEC)
   17547             :             {
   17548      340565 :               len = TREE_VEC_LENGTH (expanded);
   17549             :               /* Set TREE_USED for the benefit of -Wunused.  */
   17550      970082 :               for (int i = 0; i < len; i++)
   17551      629517 :                 if (DECL_P (TREE_VEC_ELT (expanded, i)))
   17552         497 :                   TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
   17553             :             }
   17554             : 
   17555      674045 :           if (expanded == error_mark_node)
   17556             :             return error_mark_node;
   17557      674045 :           else if (PACK_EXPANSION_P (expanded)
   17558      674045 :                    || (TREE_CODE (expanded) == TREE_VEC
   17559      340565 :                        && pack_expansion_args_count (expanded)))
   17560             : 
   17561             :             {
   17562      333498 :               if (PACK_EXPANSION_P (expanded))
   17563             :                 /* OK.  */;
   17564          18 :               else if (TREE_VEC_LENGTH (expanded) == 1)
   17565           0 :                 expanded = TREE_VEC_ELT (expanded, 0);
   17566             :               else
   17567          18 :                 expanded = make_argument_pack (expanded);
   17568             : 
   17569      333498 :               if (TYPE_P (expanded))
   17570      333270 :                 return cxx_sizeof_or_alignof_type (input_location,
   17571             :                                                    expanded, SIZEOF_EXPR,
   17572             :                                                    false,
   17573      333270 :                                                    complain & tf_error);
   17574             :               else
   17575         228 :                 return cxx_sizeof_or_alignof_expr (input_location,
   17576             :                                                    expanded, SIZEOF_EXPR,
   17577             :                                                    false,
   17578         228 :                                                    complain & tf_error);
   17579             :             }
   17580             :           else
   17581      340547 :             return build_int_cst (size_type_node, len);
   17582             :         }
   17583           0 :       if (SIZEOF_EXPR_TYPE_P (t))
   17584             :         {
   17585           0 :           r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
   17586             :                       args, complain, in_decl);
   17587           0 :           r = build1 (NOP_EXPR, r, error_mark_node);
   17588           0 :           r = build1 (SIZEOF_EXPR,
   17589           0 :                       tsubst (TREE_TYPE (t), args, complain, in_decl), r);
   17590           0 :           SIZEOF_EXPR_TYPE_P (r) = 1;
   17591           0 :           return r;
   17592             :         }
   17593             :       /* Fall through */
   17594             : 
   17595       91053 :     case INDIRECT_REF:
   17596       91053 :     case NEGATE_EXPR:
   17597       91053 :     case TRUTH_NOT_EXPR:
   17598       91053 :     case BIT_NOT_EXPR:
   17599       91053 :     case ADDR_EXPR:
   17600       91053 :     case UNARY_PLUS_EXPR:      /* Unary + */
   17601       91053 :     case ALIGNOF_EXPR:
   17602       91053 :     case AT_ENCODE_EXPR:
   17603       91053 :     case ARROW_EXPR:
   17604       91053 :     case THROW_EXPR:
   17605       91053 :     case TYPEID_EXPR:
   17606       91053 :     case REALPART_EXPR:
   17607       91053 :     case IMAGPART_EXPR:
   17608       91053 :     case PAREN_EXPR:
   17609       91053 :       {
   17610       91053 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   17611       91053 :         tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
   17612       91053 :         r = build1_loc (EXPR_LOCATION (t), code, type, op0);
   17613       91053 :         if (code == ALIGNOF_EXPR)
   17614           0 :           ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
   17615             :         /* For addresses of immediate functions ensure we have EXPR_LOCATION
   17616             :            set for possible later diagnostics.  */
   17617       91053 :         if (code == ADDR_EXPR
   17618           0 :             && EXPR_LOCATION (r) == UNKNOWN_LOCATION
   17619           0 :             && TREE_CODE (op0) == FUNCTION_DECL
   17620       91053 :             && DECL_IMMEDIATE_FUNCTION_P (op0))
   17621           0 :           SET_EXPR_LOCATION (r, input_location);
   17622             :         return r;
   17623             :       }
   17624             : 
   17625           0 :     case EXCESS_PRECISION_EXPR:
   17626           0 :       {
   17627           0 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   17628           0 :         tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
   17629           0 :         if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
   17630             :           {
   17631           0 :             gcc_checking_assert (same_type_p (type, TREE_TYPE (op0)));
   17632             :             return op0;
   17633             :           }
   17634           0 :         return build1_loc (EXPR_LOCATION (t), code, type, op0);
   17635             :       }
   17636             : 
   17637           0 :     case COMPONENT_REF:
   17638           0 :       {
   17639           0 :         tree object;
   17640           0 :         tree name;
   17641             : 
   17642           0 :         object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
   17643           0 :         name = TREE_OPERAND (t, 1);
   17644           0 :         if (TREE_CODE (name) == BIT_NOT_EXPR)
   17645             :           {
   17646           0 :             name = tsubst_copy (TREE_OPERAND (name, 0), args,
   17647             :                                 complain, in_decl);
   17648           0 :             name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
   17649             :           }
   17650           0 :         else if (TREE_CODE (name) == SCOPE_REF
   17651           0 :                  && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
   17652             :           {
   17653           0 :             tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
   17654             :                                      complain, in_decl);
   17655           0 :             name = TREE_OPERAND (name, 1);
   17656           0 :             name = tsubst_copy (TREE_OPERAND (name, 0), args,
   17657             :                                 complain, in_decl);
   17658           0 :             name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
   17659           0 :             name = build_qualified_name (/*type=*/NULL_TREE,
   17660             :                                          base, name,
   17661             :                                          /*template_p=*/false);
   17662             :           }
   17663           0 :         else if (BASELINK_P (name))
   17664           0 :           name = tsubst_baselink (name,
   17665           0 :                                   non_reference (TREE_TYPE (object)),
   17666             :                                   args, complain,
   17667             :                                   in_decl);
   17668             :         else
   17669           0 :           name = tsubst_copy (name, args, complain, in_decl);
   17670           0 :         return build_nt (COMPONENT_REF, object, name, NULL_TREE);
   17671             :       }
   17672             : 
   17673           0 :     case PLUS_EXPR:
   17674           0 :     case MINUS_EXPR:
   17675           0 :     case MULT_EXPR:
   17676           0 :     case TRUNC_DIV_EXPR:
   17677           0 :     case CEIL_DIV_EXPR:
   17678           0 :     case FLOOR_DIV_EXPR:
   17679           0 :     case ROUND_DIV_EXPR:
   17680           0 :     case EXACT_DIV_EXPR:
   17681           0 :     case BIT_AND_EXPR:
   17682           0 :     case BIT_IOR_EXPR:
   17683           0 :     case BIT_XOR_EXPR:
   17684           0 :     case TRUNC_MOD_EXPR:
   17685           0 :     case FLOOR_MOD_EXPR:
   17686           0 :     case TRUTH_ANDIF_EXPR:
   17687           0 :     case TRUTH_ORIF_EXPR:
   17688           0 :     case TRUTH_AND_EXPR:
   17689           0 :     case TRUTH_OR_EXPR:
   17690           0 :     case RSHIFT_EXPR:
   17691           0 :     case LSHIFT_EXPR:
   17692           0 :     case EQ_EXPR:
   17693           0 :     case NE_EXPR:
   17694           0 :     case MAX_EXPR:
   17695           0 :     case MIN_EXPR:
   17696           0 :     case LE_EXPR:
   17697           0 :     case GE_EXPR:
   17698           0 :     case LT_EXPR:
   17699           0 :     case GT_EXPR:
   17700           0 :     case COMPOUND_EXPR:
   17701           0 :     case DOTSTAR_EXPR:
   17702           0 :     case MEMBER_REF:
   17703           0 :     case PREDECREMENT_EXPR:
   17704           0 :     case PREINCREMENT_EXPR:
   17705           0 :     case POSTDECREMENT_EXPR:
   17706           0 :     case POSTINCREMENT_EXPR:
   17707           0 :       {
   17708           0 :         tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
   17709           0 :         tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
   17710           0 :         return build_nt (code, op0, op1);
   17711             :       }
   17712             : 
   17713         718 :     case SCOPE_REF:
   17714         718 :       {
   17715         718 :         tree op0 = tsubst_scope (TREE_OPERAND (t, 0), args, complain, in_decl);
   17716         718 :         tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
   17717         718 :         return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
   17718         718 :                                      QUALIFIED_NAME_IS_TEMPLATE (t));
   17719             :       }
   17720             : 
   17721           0 :     case ARRAY_REF:
   17722           0 :       {
   17723           0 :         tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
   17724           0 :         tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
   17725           0 :         return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
   17726             :       }
   17727             : 
   17728           0 :     case CALL_EXPR:
   17729           0 :       {
   17730           0 :         int n = VL_EXP_OPERAND_LENGTH (t);
   17731           0 :         tree result = build_vl_exp (CALL_EXPR, n);
   17732           0 :         int i;
   17733           0 :         for (i = 0; i < n; i++)
   17734           0 :           TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
   17735             :                                              complain, in_decl);
   17736             :         return result;
   17737             :       }
   17738             : 
   17739           0 :     case COND_EXPR:
   17740           0 :     case MODOP_EXPR:
   17741           0 :     case PSEUDO_DTOR_EXPR:
   17742           0 :     case VEC_PERM_EXPR:
   17743           0 :       {
   17744           0 :         tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
   17745           0 :         tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
   17746           0 :         tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
   17747           0 :         r = build_nt (code, op0, op1, op2);
   17748           0 :         copy_warning (r, t);
   17749           0 :         return r;
   17750             :       }
   17751             : 
   17752           0 :     case NEW_EXPR:
   17753           0 :       {
   17754           0 :         tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
   17755           0 :         tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
   17756           0 :         tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
   17757           0 :         r = build_nt (code, op0, op1, op2);
   17758           0 :         NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
   17759           0 :         return r;
   17760             :       }
   17761             : 
   17762           0 :     case DELETE_EXPR:
   17763           0 :       {
   17764           0 :         tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
   17765           0 :         tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
   17766           0 :         r = build_nt (code, op0, op1);
   17767           0 :         DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
   17768           0 :         DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
   17769           0 :         return r;
   17770             :       }
   17771             : 
   17772     2978014 :     case TEMPLATE_ID_EXPR:
   17773     2978014 :       {
   17774             :         /* Substituted template arguments */
   17775     2978014 :         tree tmpl = TREE_OPERAND (t, 0);
   17776     2978014 :         tree targs = TREE_OPERAND (t, 1);
   17777             : 
   17778     2978014 :         tmpl = tsubst_copy (tmpl, args, complain, in_decl);
   17779     2978014 :         if (targs)
   17780     2978014 :           targs = tsubst_template_args (targs, args, complain, in_decl);
   17781             : 
   17782     2978014 :         if (variable_template_p (tmpl))
   17783           9 :           return lookup_template_variable (tmpl, targs, complain);
   17784             :         else
   17785     2978005 :           return lookup_template_function (tmpl, targs);
   17786             :       }
   17787             : 
   17788           0 :     case TREE_LIST:
   17789           0 :       {
   17790           0 :         tree purpose, value, chain;
   17791             : 
   17792           0 :         if (t == void_list_node)
   17793             :           return t;
   17794             : 
   17795           0 :         purpose = TREE_PURPOSE (t);
   17796           0 :         if (purpose)
   17797           0 :           purpose = tsubst_copy (purpose, args, complain, in_decl);
   17798           0 :         value = TREE_VALUE (t);
   17799           0 :         if (value)
   17800           0 :           value = tsubst_copy (value, args, complain, in_decl);
   17801           0 :         chain = TREE_CHAIN (t);
   17802           0 :         if (chain && chain != void_type_node)
   17803           0 :           chain = tsubst_copy (chain, args, complain, in_decl);
   17804           0 :         if (purpose == TREE_PURPOSE (t)
   17805           0 :             && value == TREE_VALUE (t)
   17806           0 :             && chain == TREE_CHAIN (t))
   17807             :           return t;
   17808           0 :         return tree_cons (purpose, value, chain);
   17809             :       }
   17810             : 
   17811   112745733 :     case TEMPLATE_PARM_INDEX:
   17812   112745733 :     case TYPE_DECL:
   17813   112745733 :       return tsubst (t, args, complain, in_decl);
   17814             : 
   17815           4 :     case USING_DECL:
   17816           4 :       t = DECL_NAME (t);
   17817             :       /* Fall through.  */
   17818   115906971 :     case IDENTIFIER_NODE:
   17819   115906971 :       if (IDENTIFIER_CONV_OP_P (t))
   17820             :         {
   17821         144 :           tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   17822         144 :           return make_conv_op_name (new_type);
   17823             :         }
   17824             :       else
   17825             :         return t;
   17826             : 
   17827           0 :     case CONSTRUCTOR:
   17828             :       /* This is handled by tsubst_copy_and_build.  */
   17829           0 :       gcc_unreachable ();
   17830             : 
   17831           0 :     case VA_ARG_EXPR:
   17832           0 :       {
   17833           0 :         tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
   17834           0 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   17835           0 :         return build_x_va_arg (EXPR_LOCATION (t), op0, type);
   17836             :       }
   17837             : 
   17838           0 :     case CLEANUP_POINT_EXPR:
   17839             :       /* We shouldn't have built any of these during initial template
   17840             :          generation.  Instead, they should be built during instantiation
   17841             :          in response to the saved STMT_IS_FULL_EXPR_P setting.  */
   17842           0 :       gcc_unreachable ();
   17843             : 
   17844         123 :     case OFFSET_REF:
   17845         123 :       {
   17846         123 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   17847         123 :         tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
   17848         123 :         tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
   17849         123 :         r = build2 (code, type, op0, op1);
   17850         123 :         PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
   17851         123 :         if (!mark_used (TREE_OPERAND (r, 1), complain)
   17852         123 :             && !(complain & tf_error))
   17853           0 :           return error_mark_node;
   17854             :         return r;
   17855             :       }
   17856             : 
   17857           0 :     case EXPR_PACK_EXPANSION:
   17858           0 :       error ("invalid use of pack expansion expression");
   17859           0 :       return error_mark_node;
   17860             : 
   17861           0 :     case NONTYPE_ARGUMENT_PACK:
   17862           0 :       error ("use %<...%> to expand argument pack");
   17863           0 :       return error_mark_node;
   17864             : 
   17865        1224 :     case VOID_CST:
   17866        1224 :       gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
   17867             :       return t;
   17868             : 
   17869    41262429 :     case INTEGER_CST:
   17870    41262429 :     case REAL_CST:
   17871    41262429 :     case COMPLEX_CST:
   17872    41262429 :     case VECTOR_CST:
   17873    41262429 :       {
   17874             :         /* Instantiate any typedefs in the type.  */
   17875    41262429 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   17876    41262429 :         r = fold_convert (type, t);
   17877    41262429 :         gcc_assert (TREE_CODE (r) == code);
   17878             :         return r;
   17879             :       }
   17880             : 
   17881      901622 :     case STRING_CST:
   17882      901622 :       {
   17883      901622 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   17884      901622 :         r = t;
   17885      901622 :         if (type != TREE_TYPE (t))
   17886             :           {
   17887           3 :             r = copy_node (t);
   17888           3 :             TREE_TYPE (r) = type;
   17889             :           }
   17890             :         return r;
   17891             :       }
   17892             : 
   17893          12 :     case PTRMEM_CST:
   17894             :       /* These can sometimes show up in a partial instantiation, but never
   17895             :          involve template parms.  */
   17896          12 :       gcc_assert (!uses_template_parms (t));
   17897             :       return t;
   17898             : 
   17899        1283 :     case UNARY_LEFT_FOLD_EXPR:
   17900        1283 :       return tsubst_unary_left_fold (t, args, complain, in_decl);
   17901       80486 :     case UNARY_RIGHT_FOLD_EXPR:
   17902       80486 :       return tsubst_unary_right_fold (t, args, complain, in_decl);
   17903         608 :     case BINARY_LEFT_FOLD_EXPR:
   17904         608 :       return tsubst_binary_left_fold (t, args, complain, in_decl);
   17905         305 :     case BINARY_RIGHT_FOLD_EXPR:
   17906         305 :       return tsubst_binary_right_fold (t, args, complain, in_decl);
   17907             :     case PREDICT_EXPR:
   17908             :       return t;
   17909             : 
   17910             :     case DEBUG_BEGIN_STMT:
   17911             :       /* ??? There's no point in copying it for now, but maybe some
   17912             :          day it will contain more information, such as a pointer back
   17913             :          to the containing function, inlined copy or so.  */
   17914             :       return t;
   17915             : 
   17916          66 :     case CO_AWAIT_EXPR:
   17917          66 :       return tsubst_expr (t, args, complain, in_decl);
   17918             : 
   17919           0 :     default:
   17920             :       /* We shouldn't get here, but keep going if !flag_checking.  */
   17921           0 :       if (flag_checking)
   17922           0 :         gcc_unreachable ();
   17923             :       return t;
   17924             :     }
   17925             : }
   17926             : 
   17927             : /* Helper function for tsubst_omp_clauses, used for instantiation of
   17928             :    OMP_CLAUSE_DECL of clauses.  */
   17929             : 
   17930             : static tree
   17931        6053 : tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
   17932             :                         tree in_decl, tree *iterator_cache)
   17933             : {
   17934        6053 :   if (decl == NULL_TREE || decl == ridpointers[RID_OMP_ALL_MEMORY])
   17935             :     return decl;
   17936             : 
   17937             :   /* Handle OpenMP iterators.  */
   17938        5995 :   if (TREE_CODE (decl) == TREE_LIST
   17939        2030 :       && TREE_PURPOSE (decl)
   17940        7664 :       && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
   17941             :     {
   17942         176 :       tree ret;
   17943         176 :       if (iterator_cache[0] == TREE_PURPOSE (decl))
   17944          26 :         ret = iterator_cache[1];
   17945             :       else
   17946             :         {
   17947         150 :           tree *tp = &ret;
   17948         150 :           begin_scope (sk_omp, NULL);
   17949         330 :           for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
   17950             :             {
   17951         180 :               *tp = copy_node (it);
   17952         180 :               TREE_VEC_ELT (*tp, 0)
   17953         180 :                 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
   17954         180 :               DECL_CONTEXT (TREE_VEC_ELT (*tp, 0)) = current_function_decl;
   17955         180 :               pushdecl (TREE_VEC_ELT (*tp, 0));
   17956         180 :               TREE_VEC_ELT (*tp, 1)
   17957         180 :                 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl);
   17958         180 :               TREE_VEC_ELT (*tp, 2)
   17959         180 :                 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl);
   17960         180 :               TREE_VEC_ELT (*tp, 3)
   17961         180 :                 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl);
   17962         180 :               TREE_CHAIN (*tp) = NULL_TREE;
   17963         180 :               tp = &TREE_CHAIN (*tp);
   17964             :             }
   17965         150 :           TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
   17966         150 :           iterator_cache[0] = TREE_PURPOSE (decl);
   17967         150 :           iterator_cache[1] = ret;
   17968             :         }
   17969         176 :       return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
   17970             :                                                            args, complain,
   17971             :                                                            in_decl, NULL));
   17972             :     }
   17973             : 
   17974             :   /* Handle an OpenMP array section represented as a TREE_LIST (or
   17975             :      OMP_CLAUSE_DOACROSS_KIND).  An OMP_CLAUSE_DOACROSS (with a depend
   17976             :      kind of OMP_CLAUSE_DOACROSS_SINK) can also be represented as a
   17977             :      TREE_LIST.  We can handle it exactly the same as an array section
   17978             :      (purpose, value, and a chain), even though the nomenclature
   17979             :      (low_bound, length, etc) is different.  */
   17980        5819 :   if (TREE_CODE (decl) == TREE_LIST)
   17981             :     {
   17982        1854 :       tree low_bound
   17983        1854 :         = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl);
   17984        1854 :       tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl);
   17985        1854 :       tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
   17986             :                                            in_decl, NULL);
   17987        1854 :       if (TREE_PURPOSE (decl) == low_bound
   17988        1538 :           && TREE_VALUE (decl) == length
   17989        3329 :           && TREE_CHAIN (decl) == chain)
   17990             :         return decl;
   17991        1285 :       tree ret = tree_cons (low_bound, length, chain);
   17992        1285 :       OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (ret)
   17993        1285 :         = OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (decl);
   17994        1285 :       return ret;
   17995             :     }
   17996        3965 :   tree ret = tsubst_expr (decl, args, complain, in_decl);
   17997             :   /* Undo convert_from_reference tsubst_expr could have called.  */
   17998        3965 :   if (decl
   17999        3965 :       && REFERENCE_REF_P (ret)
   18000         300 :       && !REFERENCE_REF_P (decl))
   18001         296 :     ret = TREE_OPERAND (ret, 0);
   18002             :   return ret;
   18003             : }
   18004             : 
   18005             : /* Like tsubst_copy, but specifically for OpenMP clauses.  */
   18006             : 
   18007             : static tree
   18008        4929 : tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
   18009             :                     tree args, tsubst_flags_t complain, tree in_decl)
   18010             : {
   18011        4929 :   tree new_clauses = NULL_TREE, nc, oc;
   18012        4929 :   tree linear_no_step = NULL_TREE;
   18013        4929 :   tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
   18014             : 
   18015       10528 :   for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
   18016             :     {
   18017        5599 :       nc = copy_node (oc);
   18018        5599 :       OMP_CLAUSE_CHAIN (nc) = new_clauses;
   18019        5599 :       new_clauses = nc;
   18020             : 
   18021        5599 :       switch (OMP_CLAUSE_CODE (nc))
   18022             :         {
   18023         211 :         case OMP_CLAUSE_LASTPRIVATE:
   18024         211 :           if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
   18025             :             {
   18026          38 :               OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
   18027          38 :               tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args,
   18028             :                            complain, in_decl);
   18029          38 :               OMP_CLAUSE_LASTPRIVATE_STMT (nc)
   18030          76 :                 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
   18031             :             }
   18032             :           /* FALLTHRU */
   18033        2363 :         case OMP_CLAUSE_PRIVATE:
   18034        2363 :         case OMP_CLAUSE_SHARED:
   18035        2363 :         case OMP_CLAUSE_FIRSTPRIVATE:
   18036        2363 :         case OMP_CLAUSE_COPYIN:
   18037        2363 :         case OMP_CLAUSE_COPYPRIVATE:
   18038        2363 :         case OMP_CLAUSE_UNIFORM:
   18039        2363 :         case OMP_CLAUSE_DEPEND:
   18040        2363 :         case OMP_CLAUSE_DOACROSS:
   18041        2363 :         case OMP_CLAUSE_AFFINITY:
   18042        2363 :         case OMP_CLAUSE_FROM:
   18043        2363 :         case OMP_CLAUSE_TO:
   18044        2363 :         case OMP_CLAUSE_MAP:
   18045        2363 :         case OMP_CLAUSE__CACHE_:
   18046        2363 :         case OMP_CLAUSE_NONTEMPORAL:
   18047        2363 :         case OMP_CLAUSE_USE_DEVICE_PTR:
   18048        2363 :         case OMP_CLAUSE_USE_DEVICE_ADDR:
   18049        2363 :         case OMP_CLAUSE_IS_DEVICE_PTR:
   18050        2363 :         case OMP_CLAUSE_HAS_DEVICE_ADDR:
   18051        2363 :         case OMP_CLAUSE_INCLUSIVE:
   18052        2363 :         case OMP_CLAUSE_EXCLUSIVE:
   18053        4726 :           OMP_CLAUSE_DECL (nc)
   18054        2363 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18055             :                                       in_decl, iterator_cache);
   18056        2363 :           break;
   18057         196 :         case OMP_CLAUSE_NUM_TEAMS:
   18058         196 :           if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc))
   18059          88 :             OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc)
   18060         176 :               = tsubst_expr (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc), args,
   18061             :                              complain, in_decl);
   18062             :           /* FALLTHRU */
   18063         829 :         case OMP_CLAUSE_TILE:
   18064         829 :         case OMP_CLAUSE_IF:
   18065         829 :         case OMP_CLAUSE_NUM_THREADS:
   18066         829 :         case OMP_CLAUSE_SCHEDULE:
   18067         829 :         case OMP_CLAUSE_COLLAPSE:
   18068         829 :         case OMP_CLAUSE_FINAL:
   18069         829 :         case OMP_CLAUSE_DEVICE:
   18070         829 :         case OMP_CLAUSE_DIST_SCHEDULE:
   18071         829 :         case OMP_CLAUSE_THREAD_LIMIT:
   18072         829 :         case OMP_CLAUSE_SAFELEN:
   18073         829 :         case OMP_CLAUSE_SIMDLEN:
   18074         829 :         case OMP_CLAUSE_NUM_TASKS:
   18075         829 :         case OMP_CLAUSE_GRAINSIZE:
   18076         829 :         case OMP_CLAUSE_PRIORITY:
   18077         829 :         case OMP_CLAUSE_ORDERED:
   18078         829 :         case OMP_CLAUSE_HINT:
   18079         829 :         case OMP_CLAUSE_FILTER:
   18080         829 :         case OMP_CLAUSE_NUM_GANGS:
   18081         829 :         case OMP_CLAUSE_NUM_WORKERS:
   18082         829 :         case OMP_CLAUSE_VECTOR_LENGTH:
   18083         829 :         case OMP_CLAUSE_WORKER:
   18084         829 :         case OMP_CLAUSE_VECTOR:
   18085         829 :         case OMP_CLAUSE_ASYNC:
   18086         829 :         case OMP_CLAUSE_WAIT:
   18087         829 :         case OMP_CLAUSE_DETACH:
   18088         829 :           OMP_CLAUSE_OPERAND (nc, 0)
   18089         829 :             = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
   18090         829 :           break;
   18091        1040 :         case OMP_CLAUSE_REDUCTION:
   18092        1040 :         case OMP_CLAUSE_IN_REDUCTION:
   18093        1040 :         case OMP_CLAUSE_TASK_REDUCTION:
   18094        1040 :           if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
   18095             :             {
   18096          45 :               tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
   18097          45 :               if (TREE_CODE (placeholder) == SCOPE_REF)
   18098             :                 {
   18099           6 :                   tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
   18100             :                                        complain, in_decl);
   18101           6 :                   OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
   18102          12 :                     = build_qualified_name (NULL_TREE, scope,
   18103           6 :                                             TREE_OPERAND (placeholder, 1),
   18104             :                                             false);
   18105             :                 }
   18106             :               else
   18107          39 :                 gcc_assert (identifier_p (placeholder));
   18108             :             }
   18109        2080 :           OMP_CLAUSE_DECL (nc)
   18110        1040 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18111             :                                       in_decl, NULL);
   18112        1040 :           break;
   18113          82 :         case OMP_CLAUSE_GANG:
   18114          82 :         case OMP_CLAUSE_ALIGNED:
   18115         164 :           OMP_CLAUSE_DECL (nc)
   18116          82 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18117             :                                       in_decl, NULL);
   18118          82 :           OMP_CLAUSE_OPERAND (nc, 1)
   18119          82 :             = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
   18120          82 :           break;
   18121         372 :         case OMP_CLAUSE_ALLOCATE:
   18122         744 :           OMP_CLAUSE_DECL (nc)
   18123         372 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18124             :                                       in_decl, NULL);
   18125         372 :           OMP_CLAUSE_OPERAND (nc, 1)
   18126         372 :             = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
   18127         372 :           OMP_CLAUSE_OPERAND (nc, 2)
   18128         372 :             = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 2), args, complain, in_decl);
   18129         372 :           break;
   18130         166 :         case OMP_CLAUSE_LINEAR:
   18131         332 :           OMP_CLAUSE_DECL (nc)
   18132         166 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18133             :                                       in_decl, NULL);
   18134         166 :           if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
   18135             :             {
   18136           2 :               gcc_assert (!linear_no_step);
   18137             :               linear_no_step = nc;
   18138             :             }
   18139         164 :           else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
   18140           0 :             OMP_CLAUSE_LINEAR_STEP (nc)
   18141           0 :               = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
   18142             :                                         complain, in_decl, NULL);
   18143             :           else
   18144         164 :             OMP_CLAUSE_LINEAR_STEP (nc)
   18145         328 :               = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args,
   18146             :                              complain, in_decl);
   18147             :           break;
   18148             :         case OMP_CLAUSE_NOWAIT:
   18149             :         case OMP_CLAUSE_DEFAULT:
   18150             :         case OMP_CLAUSE_UNTIED:
   18151             :         case OMP_CLAUSE_MERGEABLE:
   18152             :         case OMP_CLAUSE_INBRANCH:
   18153             :         case OMP_CLAUSE_NOTINBRANCH:
   18154             :         case OMP_CLAUSE_PROC_BIND:
   18155             :         case OMP_CLAUSE_FOR:
   18156             :         case OMP_CLAUSE_PARALLEL:
   18157             :         case OMP_CLAUSE_SECTIONS:
   18158             :         case OMP_CLAUSE_TASKGROUP:
   18159             :         case OMP_CLAUSE_NOGROUP:
   18160             :         case OMP_CLAUSE_THREADS:
   18161             :         case OMP_CLAUSE_SIMD:
   18162             :         case OMP_CLAUSE_DEFAULTMAP:
   18163             :         case OMP_CLAUSE_ORDER:
   18164             :         case OMP_CLAUSE_BIND:
   18165             :         case OMP_CLAUSE_INDEPENDENT:
   18166             :         case OMP_CLAUSE_AUTO:
   18167             :         case OMP_CLAUSE_SEQ:
   18168             :         case OMP_CLAUSE_IF_PRESENT:
   18169             :         case OMP_CLAUSE_FINALIZE:
   18170             :         case OMP_CLAUSE_NOHOST:
   18171             :           break;
   18172           0 :         default:
   18173           0 :           gcc_unreachable ();
   18174             :         }
   18175        5599 :       if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
   18176        4549 :         switch (OMP_CLAUSE_CODE (nc))
   18177             :           {
   18178        2203 :           case OMP_CLAUSE_SHARED:
   18179        2203 :           case OMP_CLAUSE_PRIVATE:
   18180        2203 :           case OMP_CLAUSE_FIRSTPRIVATE:
   18181        2203 :           case OMP_CLAUSE_LASTPRIVATE:
   18182        2203 :           case OMP_CLAUSE_COPYPRIVATE:
   18183        2203 :           case OMP_CLAUSE_LINEAR:
   18184        2203 :           case OMP_CLAUSE_REDUCTION:
   18185        2203 :           case OMP_CLAUSE_IN_REDUCTION:
   18186        2203 :           case OMP_CLAUSE_TASK_REDUCTION:
   18187        2203 :           case OMP_CLAUSE_USE_DEVICE_PTR:
   18188        2203 :           case OMP_CLAUSE_USE_DEVICE_ADDR:
   18189        2203 :           case OMP_CLAUSE_IS_DEVICE_PTR:
   18190        2203 :           case OMP_CLAUSE_HAS_DEVICE_ADDR:
   18191        2203 :           case OMP_CLAUSE_INCLUSIVE:
   18192        2203 :           case OMP_CLAUSE_EXCLUSIVE:
   18193        2203 :           case OMP_CLAUSE_ALLOCATE:
   18194             :             /* tsubst_expr on SCOPE_REF results in returning
   18195             :                finish_non_static_data_member result.  Undo that here.  */
   18196        2203 :             if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
   18197        2203 :                 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
   18198             :                     == IDENTIFIER_NODE))
   18199             :               {
   18200          94 :                 tree t = OMP_CLAUSE_DECL (nc);
   18201          94 :                 tree v = t;
   18202         554 :                 while (v)
   18203         554 :                   switch (TREE_CODE (v))
   18204             :                     {
   18205         460 :                     case COMPONENT_REF:
   18206         460 :                     case MEM_REF:
   18207         460 :                     case INDIRECT_REF:
   18208         460 :                     CASE_CONVERT:
   18209         460 :                     case POINTER_PLUS_EXPR:
   18210         460 :                       v = TREE_OPERAND (v, 0);
   18211         460 :                       continue;
   18212          92 :                     case PARM_DECL:
   18213          92 :                       if (DECL_CONTEXT (v) == current_function_decl
   18214          92 :                           && DECL_ARTIFICIAL (v)
   18215         184 :                           && DECL_NAME (v) == this_identifier)
   18216          92 :                         OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
   18217             :                       /* FALLTHRU */
   18218             :                     default:
   18219             :                       v = NULL_TREE;
   18220             :                       break;
   18221             :                     }
   18222             :               }
   18223        2109 :             else if (VAR_P (OMP_CLAUSE_DECL (oc))
   18224        1007 :                      && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
   18225         167 :                      && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
   18226         167 :                      && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
   18227        2276 :                      && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
   18228             :               {
   18229         167 :                 tree decl = OMP_CLAUSE_DECL (nc);
   18230         167 :                 if (VAR_P (decl))
   18231             :                   {
   18232         167 :                     retrofit_lang_decl (decl);
   18233         167 :                     DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
   18234             :                   }
   18235             :               }
   18236             :             break;
   18237             :           default:
   18238             :             break;
   18239             :           }
   18240             :     }
   18241             : 
   18242        4929 :   new_clauses = nreverse (new_clauses);
   18243        4929 :   if (ort != C_ORT_OMP_DECLARE_SIMD)
   18244             :     {
   18245        4789 :       new_clauses = finish_omp_clauses (new_clauses, ort);
   18246        4789 :       if (linear_no_step)
   18247           2 :         for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
   18248           2 :           if (nc == linear_no_step)
   18249             :             {
   18250           2 :               OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
   18251           2 :               break;
   18252             :             }
   18253             :     }
   18254        4929 :   return new_clauses;
   18255             : }
   18256             : 
   18257             : /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes.  */
   18258             : 
   18259             : static tree
   18260      138927 : tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
   18261             :                           tree in_decl)
   18262             : {
   18263             : #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
   18264             : 
   18265      138927 :   tree purpose, value, chain;
   18266             : 
   18267      138927 :   if (t == NULL)
   18268             :     return t;
   18269             : 
   18270       97638 :   if (TREE_CODE (t) != TREE_LIST)
   18271       48841 :     return tsubst_copy_and_build (t, args, complain, in_decl);
   18272             : 
   18273       48797 :   if (t == void_list_node)
   18274             :     return t;
   18275             : 
   18276       48797 :   purpose = TREE_PURPOSE (t);
   18277       48797 :   if (purpose)
   18278        5934 :     purpose = RECUR (purpose);
   18279       48797 :   value = TREE_VALUE (t);
   18280       48797 :   if (value)
   18281             :     {
   18282       48797 :       if (TREE_CODE (value) != LABEL_DECL)
   18283       48793 :         value = RECUR (value);
   18284             :       else
   18285             :         {
   18286           4 :           value = lookup_label (DECL_NAME (value));
   18287           4 :           gcc_assert (TREE_CODE (value) == LABEL_DECL);
   18288           4 :           TREE_USED (value) = 1;
   18289             :         }
   18290             :     }
   18291       48797 :   chain = TREE_CHAIN (t);
   18292       48797 :   if (chain && chain != void_type_node)
   18293       21868 :     chain = RECUR (chain);
   18294       48797 :   return tree_cons (purpose, value, chain);
   18295             : #undef RECUR
   18296             : }
   18297             : 
   18298             : /* Used to temporarily communicate the list of #pragma omp parallel
   18299             :    clauses to #pragma omp for instantiation if they are combined
   18300             :    together.  */
   18301             : 
   18302             : static tree *omp_parallel_combined_clauses;
   18303             : 
   18304             : static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
   18305             :                                  tree *, unsigned int *);
   18306             : 
   18307             : /* Substitute one OMP_FOR iterator.  */
   18308             : 
   18309             : static bool
   18310        1224 : tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
   18311             :                          tree initv, tree condv, tree incrv, tree *clauses,
   18312             :                          tree args, tsubst_flags_t complain, tree in_decl)
   18313             : {
   18314             : #define RECUR(NODE)                             \
   18315             :   tsubst_expr ((NODE), args, complain, in_decl)
   18316        1224 :   tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
   18317        1224 :   bool ret = false;
   18318             : 
   18319        1224 :   init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
   18320        1224 :   gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
   18321             : 
   18322        1224 :   decl = TREE_OPERAND (init, 0);
   18323        1224 :   init = TREE_OPERAND (init, 1);
   18324        1224 :   tree decl_expr = NULL_TREE;
   18325        1224 :   bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
   18326        1224 :   if (range_for)
   18327             :     {
   18328          74 :       bool decomp = false;
   18329          74 :       if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
   18330             :         {
   18331          37 :           tree v = DECL_VALUE_EXPR (decl);
   18332          37 :           if (TREE_CODE (v) == ARRAY_REF
   18333          37 :               && VAR_P (TREE_OPERAND (v, 0))
   18334          74 :               && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
   18335             :             {
   18336          37 :               tree decomp_first = NULL_TREE;
   18337          37 :               unsigned decomp_cnt = 0;
   18338          37 :               tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
   18339          37 :               maybe_push_decl (d);
   18340          37 :               d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
   18341             :                                        in_decl, &decomp_first, &decomp_cnt);
   18342          37 :               decomp = true;
   18343          37 :               if (d == error_mark_node)
   18344           0 :                 decl = error_mark_node;
   18345             :               else
   18346         139 :                 for (unsigned int i = 0; i < decomp_cnt; i++)
   18347             :                   {
   18348         102 :                     if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
   18349             :                       {
   18350         102 :                         tree v = build_nt (ARRAY_REF, d,
   18351         102 :                                            size_int (decomp_cnt - i - 1),
   18352             :                                            NULL_TREE, NULL_TREE);
   18353         102 :                         SET_DECL_VALUE_EXPR (decomp_first, v);
   18354         102 :                         DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
   18355             :                       }
   18356         102 :                     fit_decomposition_lang_decl (decomp_first, d);
   18357         102 :                     decomp_first = DECL_CHAIN (decomp_first);
   18358             :                   }
   18359             :             }
   18360             :         }
   18361          74 :       decl = tsubst_decl (decl, args, complain);
   18362          74 :       if (!decomp)
   18363          37 :         maybe_push_decl (decl);
   18364             :     }
   18365        1150 :   else if (init && TREE_CODE (init) == DECL_EXPR)
   18366             :     {
   18367             :       /* We need to jump through some hoops to handle declarations in the
   18368             :          init-statement, since we might need to handle auto deduction,
   18369             :          but we need to keep control of initialization.  */
   18370         210 :       decl_expr = init;
   18371         210 :       init = DECL_INITIAL (DECL_EXPR_DECL (init));
   18372         210 :       decl = tsubst_decl (decl, args, complain);
   18373             :     }
   18374             :   else
   18375             :     {
   18376         940 :       if (TREE_CODE (decl) == SCOPE_REF)
   18377             :         {
   18378           8 :           decl = RECUR (decl);
   18379           8 :           if (TREE_CODE (decl) == COMPONENT_REF)
   18380             :             {
   18381             :               tree v = decl;
   18382          24 :               while (v)
   18383          24 :                 switch (TREE_CODE (v))
   18384             :                   {
   18385          20 :                   case COMPONENT_REF:
   18386          20 :                   case MEM_REF:
   18387          20 :                   case INDIRECT_REF:
   18388          20 :                   CASE_CONVERT:
   18389          20 :                   case POINTER_PLUS_EXPR:
   18390          20 :                     v = TREE_OPERAND (v, 0);
   18391          20 :                     continue;
   18392           4 :                   case PARM_DECL:
   18393           4 :                     if (DECL_CONTEXT (v) == current_function_decl
   18394           4 :                         && DECL_ARTIFICIAL (v)
   18395           8 :                         && DECL_NAME (v) == this_identifier)
   18396             :                       {
   18397           4 :                         decl = TREE_OPERAND (decl, 1);
   18398           4 :                         decl = omp_privatize_field (decl, false);
   18399             :                       }
   18400             :                     /* FALLTHRU */
   18401             :                   default:
   18402             :                     v = NULL_TREE;
   18403             :                     break;
   18404             :                   }
   18405             :             }
   18406             :         }
   18407             :       else
   18408         932 :         decl = RECUR (decl);
   18409             :     }
   18410        1224 :   if (init && TREE_CODE (init) == TREE_VEC)
   18411             :     {
   18412          16 :       init = copy_node (init);
   18413          16 :       TREE_VEC_ELT (init, 0)
   18414          16 :         = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
   18415          16 :       TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
   18416          16 :       TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
   18417             :     }
   18418             :   else
   18419        1208 :     init = RECUR (init);
   18420             : 
   18421        1224 :   if (orig_declv && OMP_FOR_ORIG_DECLS (t))
   18422             :     {
   18423         696 :       tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
   18424         696 :       if (TREE_CODE (o) == TREE_LIST)
   18425          97 :         TREE_VEC_ELT (orig_declv, i)
   18426         194 :           = tree_cons (RECUR (TREE_PURPOSE (o)),
   18427          97 :                        RECUR (TREE_VALUE (o)),
   18428             :                        NULL_TREE);
   18429             :       else
   18430         599 :         TREE_VEC_ELT (orig_declv, i) = RECUR (o);
   18431             :     }
   18432             : 
   18433        1224 :   if (range_for)
   18434             :     {
   18435          74 :       tree this_pre_body = NULL_TREE;
   18436          74 :       tree orig_init = NULL_TREE;
   18437          74 :       tree orig_decl = NULL_TREE;
   18438          74 :       cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
   18439             :                                 orig_init, cond, incr);
   18440          74 :       if (orig_decl)
   18441             :         {
   18442          68 :           if (orig_declv == NULL_TREE)
   18443          63 :             orig_declv = copy_node (declv);
   18444          68 :           TREE_VEC_ELT (orig_declv, i) = orig_decl;
   18445          68 :           ret = true;
   18446             :         }
   18447           6 :       else if (orig_declv)
   18448           0 :         TREE_VEC_ELT (orig_declv, i) = decl;
   18449             :     }
   18450             : 
   18451        1224 :   tree auto_node = type_uses_auto (TREE_TYPE (decl));
   18452        1224 :   if (!range_for && auto_node && init)
   18453           2 :     TREE_TYPE (decl)
   18454           4 :       = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
   18455             : 
   18456        1224 :   gcc_assert (!type_dependent_expression_p (decl));
   18457             : 
   18458        1224 :   if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
   18459             :     {
   18460        1026 :       if (decl_expr)
   18461             :         {
   18462             :           /* Declare the variable, but don't let that initialize it.  */
   18463         155 :           tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
   18464         155 :           DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
   18465         155 :           RECUR (decl_expr);
   18466         155 :           DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
   18467             :         }
   18468             : 
   18469        1026 :       if (!range_for)
   18470             :         {
   18471         952 :           cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
   18472         952 :           if (COMPARISON_CLASS_P (cond)
   18473         952 :               && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
   18474             :             {
   18475          16 :               tree lhs = RECUR (TREE_OPERAND (cond, 0));
   18476          16 :               tree rhs = copy_node (TREE_OPERAND (cond, 1));
   18477          16 :               TREE_VEC_ELT (rhs, 0)
   18478          16 :                 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
   18479          16 :               TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
   18480          16 :               TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
   18481          16 :               cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
   18482             :                              lhs, rhs);       
   18483             :             }
   18484             :           else
   18485         936 :             cond = RECUR (cond);
   18486         952 :           incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
   18487         952 :           if (TREE_CODE (incr) == MODIFY_EXPR)
   18488             :             {
   18489         238 :               tree lhs = RECUR (TREE_OPERAND (incr, 0));
   18490         238 :               tree rhs = RECUR (TREE_OPERAND (incr, 1));
   18491         238 :               incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
   18492             :                                           NOP_EXPR, rhs, NULL_TREE, complain);
   18493             :             }
   18494             :           else
   18495         714 :             incr = RECUR (incr);
   18496         952 :           if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
   18497          12 :             TREE_VEC_ELT (orig_declv, i) = decl;
   18498             :         }
   18499        1026 :       TREE_VEC_ELT (declv, i) = decl;
   18500        1026 :       TREE_VEC_ELT (initv, i) = init;
   18501        1026 :       TREE_VEC_ELT (condv, i) = cond;
   18502        1026 :       TREE_VEC_ELT (incrv, i) = incr;
   18503        1026 :       return ret;
   18504             :     }
   18505             : 
   18506         198 :   if (decl_expr)
   18507             :     {
   18508             :       /* Declare and initialize the variable.  */
   18509          55 :       RECUR (decl_expr);
   18510          55 :       init = NULL_TREE;
   18511             :     }
   18512         143 :   else if (init)
   18513             :     {
   18514          62 :       tree *pc;
   18515          62 :       int j;
   18516          50 :       for (j = ((omp_parallel_combined_clauses == NULL
   18517         174 :                 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
   18518             :         {
   18519         216 :           for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
   18520             :             {
   18521          82 :               if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
   18522          82 :                   && OMP_CLAUSE_DECL (*pc) == decl)
   18523             :                 break;
   18524          78 :               else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
   18525          78 :                        && OMP_CLAUSE_DECL (*pc) == decl)
   18526             :                 {
   18527          31 :                   if (j)
   18528             :                     break;
   18529             :                   /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
   18530           1 :                   tree c = *pc;
   18531           1 :                   *pc = OMP_CLAUSE_CHAIN (c);
   18532           1 :                   OMP_CLAUSE_CHAIN (c) = *clauses;
   18533           1 :                   *clauses = c;
   18534             :                 }
   18535          47 :               else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
   18536          47 :                        && OMP_CLAUSE_DECL (*pc) == decl)
   18537             :                 {
   18538           0 :                   error ("iteration variable %qD should not be firstprivate",
   18539             :                          decl);
   18540           0 :                   *pc = OMP_CLAUSE_CHAIN (*pc);
   18541             :                 }
   18542          47 :               else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
   18543          47 :                        && OMP_CLAUSE_DECL (*pc) == decl)
   18544             :                 {
   18545           0 :                   error ("iteration variable %qD should not be reduction",
   18546             :                          decl);
   18547           0 :                   *pc = OMP_CLAUSE_CHAIN (*pc);
   18548             :                 }
   18549             :               else
   18550          47 :                 pc = &OMP_CLAUSE_CHAIN (*pc);
   18551             :             }
   18552          84 :           if (*pc)
   18553             :             break;
   18554             :         }
   18555          62 :       if (*pc == NULL_TREE)
   18556             :         {
   18557          28 :           tree c = build_omp_clause (input_location,
   18558          28 :                                      TREE_CODE (t) == OMP_LOOP
   18559             :                                      ? OMP_CLAUSE_LASTPRIVATE
   18560             :                                      : OMP_CLAUSE_PRIVATE);
   18561          28 :           OMP_CLAUSE_DECL (c) = decl;
   18562          28 :           c = finish_omp_clauses (c, C_ORT_OMP);
   18563          28 :           if (c)
   18564             :             {
   18565          28 :               OMP_CLAUSE_CHAIN (c) = *clauses;
   18566          28 :               *clauses = c;
   18567             :             }
   18568             :         }
   18569             :     }
   18570         198 :   cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
   18571         198 :   if (COMPARISON_CLASS_P (cond))
   18572             :     {
   18573         198 :       tree op0 = RECUR (TREE_OPERAND (cond, 0));
   18574         198 :       tree op1 = RECUR (TREE_OPERAND (cond, 1));
   18575         198 :       cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
   18576             :     }
   18577             :   else
   18578           0 :     cond = RECUR (cond);
   18579         198 :   incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
   18580         198 :   switch (TREE_CODE (incr))
   18581             :     {
   18582          88 :     case PREINCREMENT_EXPR:
   18583          88 :     case PREDECREMENT_EXPR:
   18584          88 :     case POSTINCREMENT_EXPR:
   18585          88 :     case POSTDECREMENT_EXPR:
   18586          88 :       incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
   18587          88 :                      RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
   18588          88 :       break;
   18589         110 :     case MODIFY_EXPR:
   18590         110 :       if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
   18591         110 :           || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
   18592             :         {
   18593         110 :           tree rhs = TREE_OPERAND (incr, 1);
   18594         110 :           tree lhs = RECUR (TREE_OPERAND (incr, 0));
   18595         110 :           tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
   18596         110 :           tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
   18597         110 :           incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
   18598         110 :                          build2 (TREE_CODE (rhs), TREE_TYPE (decl),
   18599             :                                  rhs0, rhs1));
   18600             :         }
   18601             :       else
   18602           0 :         incr = RECUR (incr);
   18603             :       break;
   18604           0 :     case MODOP_EXPR:
   18605           0 :       if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
   18606           0 :           || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
   18607             :         {
   18608           0 :           tree lhs = RECUR (TREE_OPERAND (incr, 0));
   18609           0 :           incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
   18610           0 :                          build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
   18611           0 :                                  TREE_TYPE (decl), lhs,
   18612           0 :                                  RECUR (TREE_OPERAND (incr, 2))));
   18613             :         }
   18614           0 :       else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
   18615           0 :                && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
   18616           0 :                    || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
   18617             :         {
   18618           0 :           tree rhs = TREE_OPERAND (incr, 2);
   18619           0 :           tree lhs = RECUR (TREE_OPERAND (incr, 0));
   18620           0 :           tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
   18621           0 :           tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
   18622           0 :           incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
   18623           0 :                          build2 (TREE_CODE (rhs), TREE_TYPE (decl),
   18624             :                                  rhs0, rhs1));
   18625             :         }
   18626             :       else
   18627           0 :         incr = RECUR (incr);
   18628             :       break;
   18629           0 :     default:
   18630           0 :       incr = RECUR (incr);
   18631           0 :       break;
   18632             :     }
   18633             : 
   18634         198 :   if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
   18635           5 :     TREE_VEC_ELT (orig_declv, i) = decl;
   18636         198 :   TREE_VEC_ELT (declv, i) = decl;
   18637         198 :   TREE_VEC_ELT (initv, i) = init;
   18638         198 :   TREE_VEC_ELT (condv, i) = cond;
   18639         198 :   TREE_VEC_ELT (incrv, i) = incr;
   18640         198 :   return false;
   18641             : #undef RECUR
   18642             : }
   18643             : 
   18644             : /* Helper function of tsubst_expr, find OMP_TEAMS inside
   18645             :    of OMP_TARGET's body.  */
   18646             : 
   18647             : static tree
   18648         212 : tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
   18649             : {
   18650         212 :   *walk_subtrees = 0;
   18651         212 :   switch (TREE_CODE (*tp))
   18652             :     {
   18653             :     case OMP_TEAMS:
   18654             :       return *tp;
   18655         106 :     case BIND_EXPR:
   18656         106 :     case STATEMENT_LIST:
   18657         106 :       *walk_subtrees = 1;
   18658         106 :       break;
   18659             :     default:
   18660             :       break;
   18661             :     }
   18662             :   return NULL_TREE;
   18663             : }
   18664             : 
   18665             : /* Helper function for tsubst_expr.  For decomposition declaration
   18666             :    artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
   18667             :    also the corresponding decls representing the identifiers
   18668             :    of the decomposition declaration.  Return DECL if successful
   18669             :    or error_mark_node otherwise, set *FIRST to the first decl
   18670             :    in the list chained through DECL_CHAIN and *CNT to the number
   18671             :    of such decls.  */
   18672             : 
   18673             : static tree
   18674        1536 : tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
   18675             :                      tsubst_flags_t complain, tree in_decl, tree *first,
   18676             :                      unsigned int *cnt)
   18677             : {
   18678        1536 :   tree decl2, decl3, prev = decl;
   18679        1536 :   *cnt = 0;
   18680        1536 :   gcc_assert (DECL_NAME (decl) == NULL_TREE);
   18681        1536 :   for (decl2 = DECL_CHAIN (pattern_decl);
   18682             :        decl2
   18683        3417 :        && VAR_P (decl2)
   18684        3411 :        && DECL_DECOMPOSITION_P (decl2)
   18685        7798 :        && DECL_NAME (decl2);
   18686        3113 :        decl2 = DECL_CHAIN (decl2))
   18687             :     {
   18688        3116 :       if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
   18689             :         {
   18690           3 :           gcc_assert (errorcount);
   18691             :           return error_mark_node;
   18692             :         }
   18693        3113 :       (*cnt)++;
   18694        3113 :       gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
   18695        3113 :       gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
   18696        3113 :       tree v = DECL_VALUE_EXPR (decl2);
   18697        3113 :       DECL_HAS_VALUE_EXPR_P (decl2) = 0;
   18698        3113 :       SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
   18699        3113 :       decl3 = tsubst (decl2, args, complain, in_decl);
   18700        3113 :       SET_DECL_VALUE_EXPR (decl2, v);
   18701        3113 :       DECL_HAS_VALUE_EXPR_P (decl2) = 1;
   18702        3113 :       if (VAR_P (decl3))
   18703        3113 :         DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
   18704             :       else
   18705             :         {
   18706           0 :           gcc_assert (errorcount);
   18707           0 :           decl = error_mark_node;
   18708           0 :           continue;
   18709             :         }
   18710        3113 :       maybe_push_decl (decl3);
   18711        3113 :       if (error_operand_p (decl3))
   18712           0 :         decl = error_mark_node;
   18713        3113 :       else if (decl != error_mark_node
   18714        3113 :                && DECL_CHAIN (decl3) != prev
   18715        3113 :                && decl != prev)
   18716             :         {
   18717           0 :           gcc_assert (errorcount);
   18718             :           decl = error_mark_node;
   18719             :         }
   18720             :       else
   18721             :         prev = decl3;
   18722             :     }
   18723        1533 :   *first = prev;
   18724        1533 :   return decl;
   18725             : }
   18726             : 
   18727             : /* Return the proper local_specialization for init-capture pack DECL.  */
   18728             : 
   18729             : static tree
   18730          12 : lookup_init_capture_pack (tree decl)
   18731             : {
   18732             :   /* We handle normal pack captures by forwarding to the specialization of the
   18733             :      captured parameter.  We can't do that for pack init-captures; we need them
   18734             :      to have their own local_specialization.  We created the individual
   18735             :      VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
   18736             :      when we process the DECL_EXPR for the pack init-capture in the template.
   18737             :      So, how do we find them?  We don't know the capture proxy pack when
   18738             :      building the individual resulting proxies, and we don't know the
   18739             :      individual proxies when instantiating the pack.  What we have in common is
   18740             :      the FIELD_DECL.
   18741             : 
   18742             :      So...when we instantiate the FIELD_DECL, we stick the result in
   18743             :      local_specializations.  Then at the DECL_EXPR we look up that result, see
   18744             :      how many elements it has, synthesize the names, and look them up.  */
   18745             : 
   18746          12 :   tree cname = DECL_NAME (decl);
   18747          12 :   tree val = DECL_VALUE_EXPR (decl);
   18748          12 :   tree field = TREE_OPERAND (val, 1);
   18749          12 :   gcc_assert (TREE_CODE (field) == FIELD_DECL);
   18750          12 :   tree fpack = retrieve_local_specialization (field);
   18751          12 :   if (fpack == error_mark_node)
   18752             :     return error_mark_node;
   18753             : 
   18754          12 :   int len = 1;
   18755          12 :   tree vec = NULL_TREE;
   18756          12 :   tree r = NULL_TREE;
   18757          12 :   if (TREE_CODE (fpack) == TREE_VEC)
   18758             :     {
   18759          11 :       len = TREE_VEC_LENGTH (fpack);
   18760          11 :       vec = make_tree_vec (len);
   18761          11 :       r = make_node (NONTYPE_ARGUMENT_PACK);
   18762          11 :       ARGUMENT_PACK_ARGS (r) = vec;
   18763             :     }
   18764          28 :   for (int i = 0; i < len; ++i)
   18765             :     {
   18766          16 :       tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
   18767          16 :       tree elt = lookup_name (ename);
   18768          16 :       if (vec)
   18769          15 :         TREE_VEC_ELT (vec, i) = elt;
   18770             :       else
   18771             :         r = elt;
   18772             :     }
   18773             :   return r;
   18774             : }
   18775             : 
   18776             : /* T is an operand of a template tree being substituted.  Return whether
   18777             :    T is dependent such that we should suppress some warnings that would
   18778             :    make sense if the substituted expression were written directly, like
   18779             :      template <int I> bool f() { return I == 2; }
   18780             :    We don't want to warn when instantiating f that comparing two constants
   18781             :    always has the same value.
   18782             : 
   18783             :    This is a more limited concept of dependence than instantiation-dependent;
   18784             :    here we don't care whether substitution could fail.  */
   18785             : 
   18786             : static bool
   18787    61041327 : dependent_operand_p (tree t)
   18788             : {
   18789    61041327 :   while (TREE_CODE (t) == IMPLICIT_CONV_EXPR)
   18790           0 :     t = TREE_OPERAND (t, 0);
   18791    61041327 :   ++processing_template_decl;
   18792    61041327 :   bool r = (potential_constant_expression (t)
   18793    61041327 :             ? value_dependent_expression_p (t)
   18794     2957309 :             : type_dependent_expression_p (t));
   18795    61041327 :   --processing_template_decl;
   18796    61041327 :   return r;
   18797             : }
   18798             : 
   18799             : /* Like tsubst_copy for expressions, etc. but also does semantic
   18800             :    processing.  */
   18801             : 
   18802             : tree
   18803   355003516 : tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   18804             : {
   18805             : #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
   18806             : #define RECUR(NODE)                             \
   18807             :   tsubst_expr ((NODE), args, complain, in_decl)
   18808             : 
   18809   355003516 :   tree stmt, tmp;
   18810   355003516 :   tree r;
   18811   355003516 :   location_t loc;
   18812             : 
   18813   355003516 :   if (t == NULL_TREE || t == error_mark_node)
   18814             :     return t;
   18815             : 
   18816   353472899 :   loc = input_location;
   18817   353472899 :   if (location_t eloc = cp_expr_location (t))
   18818   183228722 :     input_location = eloc;
   18819   353472899 :   if (STATEMENT_CODE_P (TREE_CODE (t)))
   18820    23560541 :     current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
   18821             : 
   18822   353472899 :   switch (TREE_CODE (t))
   18823             :     {
   18824    19870673 :     case STATEMENT_LIST:
   18825    19870673 :       {
   18826   107971080 :         for (tree stmt : tsi_range (t))
   18827    88100411 :           RECUR (stmt);
   18828             :         break;
   18829             :       }
   18830             : 
   18831     1589138 :     case CTOR_INITIALIZER:
   18832     1589138 :       finish_mem_initializers (tsubst_initializer_list
   18833     1589138 :                                (TREE_OPERAND (t, 0), args));
   18834     1589138 :       break;
   18835             : 
   18836     9353401 :     case RETURN_EXPR:
   18837     9353401 :       finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
   18838     9353398 :       break;
   18839             : 
   18840         145 :     case CO_RETURN_EXPR:
   18841         145 :       finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
   18842         145 :       break;
   18843             : 
   18844          43 :     case CO_YIELD_EXPR:
   18845          43 :       stmt = finish_co_yield_expr (input_location,
   18846          43 :                                    RECUR (TREE_OPERAND (t, 0)));
   18847          43 :       RETURN (stmt);
   18848             : 
   18849          86 :     case CO_AWAIT_EXPR:
   18850          86 :       stmt = finish_co_await_expr (input_location,
   18851          86 :                                    RECUR (TREE_OPERAND (t, 0)));
   18852          86 :       RETURN (stmt);
   18853             : 
   18854    13074263 :     case EXPR_STMT:
   18855    13074263 :       tmp = RECUR (EXPR_STMT_EXPR (t));
   18856    13074259 :       if (EXPR_STMT_STMT_EXPR_RESULT (t))
   18857         156 :         finish_stmt_expr_expr (tmp, cur_stmt_expr);
   18858             :       else
   18859    13074103 :         finish_expr_stmt (tmp);
   18860             :       break;
   18861             : 
   18862        1616 :     case USING_STMT:
   18863        1616 :       finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
   18864        1616 :       break;
   18865             : 
   18866           0 :     case PRECONDITION_STMT:
   18867           0 :     case POSTCONDITION_STMT:
   18868           0 :       gcc_unreachable ();
   18869             : 
   18870          21 :     case ASSERTION_STMT:
   18871          21 :       {
   18872          21 :         r = tsubst_contract (NULL_TREE, t, args, complain, in_decl);
   18873          21 :         if (r != error_mark_node)
   18874          21 :           add_stmt (r);
   18875          21 :         RETURN (r);
   18876             :       }
   18877             :       break;
   18878             : 
   18879    13288992 :     case DECL_EXPR:
   18880    13288992 :       {
   18881    13288992 :         tree decl, pattern_decl;
   18882    13288992 :         tree init;
   18883             : 
   18884    13288992 :         pattern_decl = decl = DECL_EXPR_DECL (t);
   18885    13288992 :         if (TREE_CODE (decl) == LABEL_DECL)
   18886           4 :           finish_label_decl (DECL_NAME (decl));
   18887    13288988 :         else if (TREE_CODE (decl) == USING_DECL)
   18888             :           {
   18889       59067 :             tree scope = USING_DECL_SCOPE (decl);
   18890       59067 :             if (DECL_DEPENDENT_P (decl))
   18891             :               {
   18892           3 :                 scope = tsubst (scope, args, complain, in_decl);
   18893           1 :                 if (!MAYBE_CLASS_TYPE_P (scope)
   18894           5 :                     && TREE_CODE (scope) != ENUMERAL_TYPE)
   18895             :                   {
   18896           1 :                     if (complain & tf_error)
   18897           1 :                       error_at (DECL_SOURCE_LOCATION (decl), "%qT is not a "
   18898             :                                 "class, namespace, or enumeration", scope);
   18899           1 :                     return error_mark_node;
   18900             :                   }
   18901           2 :                 finish_nonmember_using_decl (scope, DECL_NAME (decl));
   18902             :               }
   18903             :             else
   18904             :               {
   18905             :                 /* This is a non-dependent using-decl, and we'll have
   18906             :                    used the names it found during template parsing.  We do
   18907             :                    not want to do the lookup again, because we might not
   18908             :                    find the things we found then.  */
   18909       59064 :                 gcc_checking_assert (scope == tsubst (scope, args,
   18910             :                                                       complain, in_decl));
   18911             :                 /* We still need to push the bindings so that we can look up
   18912             :                    this name later.  */
   18913      118128 :                 push_using_decl_bindings (DECL_NAME (decl),
   18914       59064 :                                           USING_DECL_DECLS (decl));
   18915             :               }
   18916             :           }
   18917    13229921 :         else if (is_capture_proxy (decl)
   18918    13229921 :                  && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
   18919             :           {
   18920             :             /* We're in tsubst_lambda_expr, we've already inserted a new
   18921             :                capture proxy, so look it up and register it.  */
   18922       34047 :             tree inst;
   18923       34047 :             if (!DECL_PACK_P (decl))
   18924             :               {
   18925       33989 :                 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
   18926             :                                     LOOK_want::HIDDEN_LAMBDA);
   18927       33989 :                 gcc_assert (inst != decl && is_capture_proxy (inst));
   18928             :               }
   18929          58 :             else if (is_normal_capture_proxy (decl))
   18930             :               {
   18931          46 :                 inst = (retrieve_local_specialization
   18932          46 :                         (DECL_CAPTURED_VARIABLE (decl)));
   18933          46 :                 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
   18934             :                             || DECL_PACK_P (inst));
   18935             :               }
   18936             :             else
   18937          12 :               inst = lookup_init_capture_pack (decl);
   18938             : 
   18939       34047 :             register_local_specialization (inst, decl);
   18940       34047 :             break;
   18941             :           }
   18942    13195874 :         else if (DECL_PRETTY_FUNCTION_P (decl))
   18943        5582 :           decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
   18944        5582 :                                   DECL_NAME (decl),
   18945             :                                   true/*DECL_PRETTY_FUNCTION_P (decl)*/);
   18946     1025146 :         else if (DECL_IMPLICIT_TYPEDEF_P (decl)
   18947    13910968 :                  && LAMBDA_TYPE_P (TREE_TYPE (decl)))
   18948             :           /* Don't copy the old closure; we'll create a new one in
   18949             :              tsubst_lambda_expr.  */
   18950             :           break;
   18951             :         else
   18952             :           {
   18953    13151715 :             init = DECL_INITIAL (decl);
   18954    13151715 :             decl = tsubst (decl, args, complain, in_decl);
   18955    13151711 :             if (decl != error_mark_node)
   18956             :               {
   18957             :                 /* By marking the declaration as instantiated, we avoid
   18958             :                    trying to instantiate it.  Since instantiate_decl can't
   18959             :                    handle local variables, and since we've already done
   18960             :                    all that needs to be done, that's the right thing to
   18961             :                    do.  */
   18962    13151646 :                 if (VAR_P (decl))
   18963    12164776 :                   DECL_TEMPLATE_INSTANTIATED (decl) = 1;
   18964    12164776 :                 if (VAR_P (decl) && !DECL_NAME (decl)
   18965    13153522 :                     && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
   18966             :                   /* Anonymous aggregates are a special case.  */
   18967          37 :                   finish_anon_union (decl);
   18968    13151609 :                 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
   18969             :                   {
   18970       14301 :                     DECL_CONTEXT (decl) = current_function_decl;
   18971       14301 :                     if (DECL_NAME (decl) == this_identifier)
   18972             :                       {
   18973        4651 :                         tree lam = DECL_CONTEXT (current_function_decl);
   18974        4651 :                         lam = CLASSTYPE_LAMBDA_EXPR (lam);
   18975        4651 :                         LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
   18976             :                       }
   18977       14301 :                     insert_capture_proxy (decl);
   18978             :                   }
   18979    13137308 :                 else if (DECL_IMPLICIT_TYPEDEF_P (t))
   18980             :                   /* We already did a pushtag.  */;
   18981      986870 :                 else if (VAR_OR_FUNCTION_DECL_P (decl)
   18982    13137617 :                          && DECL_LOCAL_DECL_P (decl))
   18983             :                   {
   18984         404 :                     if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
   18985         170 :                       DECL_CONTEXT (decl) = NULL_TREE;
   18986         404 :                     decl = pushdecl (decl);
   18987         404 :                     if (TREE_CODE (decl) == FUNCTION_DECL
   18988         297 :                         && DECL_OMP_DECLARE_REDUCTION_P (decl)
   18989         562 :                         && cp_check_omp_declare_reduction (decl))
   18990         110 :                       instantiate_body (pattern_decl, args, decl, true);
   18991             :                   }
   18992             :                 else
   18993             :                   {
   18994    13136904 :                     bool const_init = false;
   18995    13136904 :                     unsigned int cnt = 0;
   18996    13136904 :                     tree first = NULL_TREE, ndecl = error_mark_node;
   18997    13136904 :                     tree asmspec_tree = NULL_TREE;
   18998    13136904 :                     maybe_push_decl (decl);
   18999             : 
   19000    13136904 :                     if (VAR_P (decl)
   19001    12150343 :                         && DECL_LANG_SPECIFIC (decl)
   19002    13460736 :                         && DECL_OMP_PRIVATIZED_MEMBER (decl))
   19003             :                       break;
   19004             : 
   19005    13136741 :                     if (VAR_P (decl)
   19006    12150180 :                         && DECL_DECOMPOSITION_P (decl)
   19007    13138185 :                         && TREE_TYPE (pattern_decl) != error_mark_node)
   19008        1444 :                       ndecl = tsubst_decomp_names (decl, pattern_decl, args,
   19009             :                                                    complain, in_decl, &first,
   19010             :                                                    &cnt);
   19011             : 
   19012    13136741 :                     init = tsubst_init (init, decl, args, complain, in_decl);
   19013             : 
   19014    13136741 :                     if (VAR_P (decl))
   19015    12150180 :                       const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
   19016             :                                     (pattern_decl));
   19017             : 
   19018    13136741 :                     if (ndecl != error_mark_node)
   19019        1441 :                       cp_maybe_mangle_decomp (ndecl, first, cnt);
   19020             : 
   19021             :                     /* In a non-template function, VLA type declarations are
   19022             :                        handled in grokdeclarator; for templates, handle them
   19023             :                        now.  */
   19024    13136741 :                     predeclare_vla (decl);
   19025             : 
   19026    25286921 :                     if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl))
   19027             :                       {
   19028           4 :                         tree id = DECL_ASSEMBLER_NAME (pattern_decl);
   19029           4 :                         const char *asmspec = IDENTIFIER_POINTER (id);
   19030           4 :                         gcc_assert (asmspec[0] == '*');
   19031           4 :                         asmspec_tree
   19032           4 :                           = build_string (IDENTIFIER_LENGTH (id) - 1,
   19033             :                                           asmspec + 1);
   19034           4 :                         TREE_TYPE (asmspec_tree) = char_array_type_node;
   19035             :                       }
   19036             : 
   19037    13136741 :                     cp_finish_decl (decl, init, const_init, asmspec_tree, 0);
   19038             : 
   19039    13136741 :                     if (ndecl != error_mark_node)
   19040        1441 :                       cp_finish_decomp (ndecl, first, cnt);
   19041             :                   }
   19042             :               }
   19043             :           }
   19044             : 
   19045             :         break;
   19046             :       }
   19047             : 
   19048      701341 :     case FOR_STMT:
   19049      701341 :       stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
   19050      701341 :       RECUR (FOR_INIT_STMT (t));
   19051      701341 :       finish_init_stmt (stmt);
   19052      701341 :       tmp = RECUR (FOR_COND (t));
   19053      701341 :       finish_for_cond (tmp, stmt, false, 0);
   19054      701341 :       tmp = RECUR (FOR_EXPR (t));
   19055      701341 :       finish_for_expr (tmp, stmt);
   19056      701341 :       {
   19057      701341 :         bool prev = note_iteration_stmt_body_start ();
   19058      701341 :         RECUR (FOR_BODY (t));
   19059      701341 :         note_iteration_stmt_body_end (prev);
   19060             :       }
   19061      701341 :       finish_for_stmt (stmt);
   19062      701341 :       break;
   19063             : 
   19064       30027 :     case RANGE_FOR_STMT:
   19065       30027 :       {
   19066             :         /* Construct another range_for, if this is not a final
   19067             :            substitution (for inside a generic lambda of a
   19068             :            template).  Otherwise convert to a regular for.  */
   19069       30027 :         tree decl, expr;
   19070       60054 :         stmt = (processing_template_decl
   19071       30027 :                 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
   19072       30021 :                 : begin_for_stmt (NULL_TREE, NULL_TREE));
   19073       30027 :         RECUR (RANGE_FOR_INIT_STMT (t));
   19074       30027 :         decl = RANGE_FOR_DECL (t);
   19075       30027 :         decl = tsubst (decl, args, complain, in_decl);
   19076       30027 :         maybe_push_decl (decl);
   19077       30027 :         expr = RECUR (RANGE_FOR_EXPR (t));
   19078             : 
   19079       30027 :         tree decomp_first = NULL_TREE;
   19080       30027 :         unsigned decomp_cnt = 0;
   19081       30027 :         if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
   19082          55 :           decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
   19083             :                                       complain, in_decl,
   19084             :                                       &decomp_first, &decomp_cnt);
   19085             : 
   19086       30027 :         if (processing_template_decl)
   19087             :           {
   19088           6 :             RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
   19089           6 :             RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
   19090           6 :             finish_range_for_decl (stmt, decl, expr);
   19091           6 :             if (decomp_first && decl != error_mark_node)
   19092           3 :               cp_finish_decomp (decl, decomp_first, decomp_cnt);
   19093             :           }
   19094             :         else
   19095             :           {
   19096       30021 :             unsigned short unroll = (RANGE_FOR_UNROLL (t)
   19097       30021 :                                      ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
   19098       30021 :             stmt = cp_convert_range_for (stmt, decl, expr,
   19099             :                                          decomp_first, decomp_cnt,
   19100       30021 :                                          RANGE_FOR_IVDEP (t), unroll);
   19101             :           }
   19102             : 
   19103       30027 :         bool prev = note_iteration_stmt_body_start ();
   19104       30027 :         RECUR (RANGE_FOR_BODY (t));
   19105       30027 :         note_iteration_stmt_body_end (prev);
   19106       30027 :         finish_for_stmt (stmt);
   19107             :       }
   19108       30027 :       break;
   19109             : 
   19110      184727 :     case WHILE_STMT:
   19111      184727 :       stmt = begin_while_stmt ();
   19112      184727 :       tmp = RECUR (WHILE_COND (t));
   19113      184727 :       finish_while_stmt_cond (tmp, stmt, false, 0);
   19114      184727 :       {
   19115      184727 :         bool prev = note_iteration_stmt_body_start ();
   19116      184727 :         RECUR (WHILE_BODY (t));
   19117      184727 :         note_iteration_stmt_body_end (prev);
   19118             :       }
   19119      184727 :       finish_while_stmt (stmt);
   19120      184727 :       break;
   19121             : 
   19122      393260 :     case DO_STMT:
   19123      393260 :       stmt = begin_do_stmt ();
   19124      393260 :       {
   19125      393260 :         bool prev = note_iteration_stmt_body_start ();
   19126      393260 :         RECUR (DO_BODY (t));
   19127      393260 :         note_iteration_stmt_body_end (prev);
   19128             :       }
   19129      393260 :       finish_do_body (stmt);
   19130      393260 :       tmp = RECUR (DO_COND (t));
   19131      393260 :       finish_do_stmt (tmp, stmt, false, 0);
   19132      393260 :       break;
   19133             : 
   19134     6436958 :     case IF_STMT:
   19135     6436958 :       stmt = begin_if_stmt ();
   19136     6436958 :       IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
   19137     6436958 :       IF_STMT_CONSTEVAL_P (stmt) = IF_STMT_CONSTEVAL_P (t);
   19138     6436958 :       if (IF_STMT_CONSTEXPR_P (t))
   19139      476760 :         args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args, complain, in_decl);
   19140     6436958 :       {
   19141     6436958 :         tree cond = IF_COND (t);
   19142     6436958 :         bool was_dep = dependent_operand_p (cond);
   19143     6436958 :         cond = RECUR (cond);
   19144     6436958 :         warning_sentinel s1(warn_address, was_dep);
   19145     6436958 :         tmp = finish_if_stmt_cond (cond, stmt);
   19146     6436958 :       }
   19147     6436958 :       if (IF_STMT_CONSTEXPR_P (t)
   19148     6436958 :           && instantiation_dependent_expression_p (tmp))
   19149             :         {
   19150             :           /* We're partially instantiating a generic lambda, but the condition
   19151             :              of the constexpr if is still dependent.  Don't substitute into the
   19152             :              branches now, just remember the template arguments.  */
   19153         654 :           do_poplevel (IF_SCOPE (stmt));
   19154         654 :           IF_COND (stmt) = IF_COND (t);
   19155         654 :           THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
   19156         654 :           ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
   19157         654 :           IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
   19158         654 :           add_stmt (stmt);
   19159         654 :           break;
   19160             :         }
   19161     6436304 :       if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
   19162             :         /* Don't instantiate the THEN_CLAUSE. */;
   19163     6132543 :       else if (IF_STMT_CONSTEVAL_P (t))
   19164             :         {
   19165          25 :           bool save_in_consteval_if_p = in_consteval_if_p;
   19166          25 :           in_consteval_if_p = true;
   19167          25 :           RECUR (THEN_CLAUSE (t));
   19168          25 :           in_consteval_if_p = save_in_consteval_if_p;
   19169             :         }
   19170             :       else
   19171             :         {
   19172     6132518 :           tree folded = fold_non_dependent_expr (tmp, complain);
   19173     6132518 :           bool inhibit = integer_zerop (folded);
   19174     6132518 :           if (inhibit)
   19175      155372 :             ++c_inhibit_evaluation_warnings;
   19176     6132518 :           RECUR (THEN_CLAUSE (t));
   19177     6132518 :           if (inhibit)
   19178      155372 :             --c_inhibit_evaluation_warnings;
   19179             :         }
   19180     6436304 :       finish_then_clause (stmt);
   19181             : 
   19182     6436304 :       if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
   19183             :         /* Don't instantiate the ELSE_CLAUSE. */;
   19184     6263969 :       else if (ELSE_CLAUSE (t))
   19185             :         {
   19186     2798672 :           tree folded = fold_non_dependent_expr (tmp, complain);
   19187     2798672 :           bool inhibit = integer_nonzerop (folded);
   19188     2798672 :           begin_else_clause (stmt);
   19189     2798672 :           if (inhibit)
   19190       17649 :             ++c_inhibit_evaluation_warnings;
   19191     2798672 :           RECUR (ELSE_CLAUSE (t));
   19192     2798672 :           if (inhibit)
   19193       17649 :             --c_inhibit_evaluation_warnings;
   19194     2798672 :           finish_else_clause (stmt);
   19195             :         }
   19196             : 
   19197     6436304 :       finish_if_stmt (stmt);
   19198     6436304 :       break;
   19199             : 
   19200    20708398 :     case BIND_EXPR:
   19201    20708398 :       if (BIND_EXPR_BODY_BLOCK (t))
   19202     2143933 :         stmt = begin_function_body ();
   19203             :       else
   19204    37054740 :         stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
   19205             :                                     ? BCS_TRY_BLOCK : 0);
   19206             : 
   19207    20708398 :       RECUR (BIND_EXPR_BODY (t));
   19208             : 
   19209    20708387 :       if (BIND_EXPR_BODY_BLOCK (t))
   19210     2143933 :         finish_function_body (stmt);
   19211             :       else
   19212    18564454 :         finish_compound_stmt (stmt);
   19213             :       break;
   19214             : 
   19215      550374 :     case BREAK_STMT:
   19216      550374 :       finish_break_stmt ();
   19217      550374 :       break;
   19218             : 
   19219         827 :     case CONTINUE_STMT:
   19220         827 :       finish_continue_stmt ();
   19221         827 :       break;
   19222             : 
   19223      126897 :     case SWITCH_STMT:
   19224      126897 :       stmt = begin_switch_stmt ();
   19225      126897 :       tmp = RECUR (SWITCH_STMT_COND (t));
   19226      126897 :       finish_switch_cond (tmp, stmt);
   19227      126897 :       RECUR (SWITCH_STMT_BODY (t));
   19228      126897 :       finish_switch_stmt (stmt);
   19229      126897 :       break;
   19230             : 
   19231      604698 :     case CASE_LABEL_EXPR:
   19232      604698 :       {
   19233      604698 :         tree decl = CASE_LABEL (t);
   19234      604698 :         tree low = RECUR (CASE_LOW (t));
   19235      604698 :         tree high = RECUR (CASE_HIGH (t));
   19236      604698 :         tree l = finish_case_label (EXPR_LOCATION (t), low, high);
   19237      604698 :         if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
   19238             :           {
   19239      604688 :             tree label = CASE_LABEL (l);
   19240      604688 :             FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
   19241      604688 :             if (DECL_ATTRIBUTES (decl) != NULL_TREE)
   19242           2 :               cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
   19243             :           }
   19244             :       }
   19245             :       break;
   19246             : 
   19247         148 :     case LABEL_EXPR:
   19248         148 :       {
   19249         148 :         tree decl = LABEL_EXPR_LABEL (t);
   19250         148 :         tree label;
   19251             : 
   19252         148 :         label = finish_label_stmt (DECL_NAME (decl));
   19253         148 :         if (TREE_CODE (label) == LABEL_DECL)
   19254         148 :           FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
   19255         148 :         if (DECL_ATTRIBUTES (decl) != NULL_TREE)
   19256          18 :           cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
   19257             :       }
   19258         148 :       break;
   19259             : 
   19260         116 :     case GOTO_EXPR:
   19261         116 :       tmp = GOTO_DESTINATION (t);
   19262         116 :       if (TREE_CODE (tmp) != LABEL_DECL)
   19263             :         /* Computed goto's must be tsubst'd into.  On the other hand,
   19264             :            non-computed gotos must not be; the identifier in question
   19265             :            will have no binding.  */
   19266          28 :         tmp = RECUR (tmp);
   19267             :       else
   19268          88 :         tmp = DECL_NAME (tmp);
   19269         116 :       finish_goto_stmt (tmp);
   19270         116 :       break;
   19271             : 
   19272       15583 :     case ASM_EXPR:
   19273       15583 :       {
   19274       15583 :         tree string = RECUR (ASM_STRING (t));
   19275       15583 :         tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
   19276             :                                                  complain, in_decl);
   19277       15583 :         tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
   19278             :                                                 complain, in_decl);
   19279       15583 :         tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
   19280             :                                                   complain, in_decl);
   19281       15583 :         tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
   19282             :                                                 complain, in_decl);
   19283       31166 :         tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
   19284             :                                outputs, inputs, clobbers, labels,
   19285       15583 :                                ASM_INLINE_P (t));
   19286       15583 :         tree asm_expr = tmp;
   19287       15583 :         if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
   19288       15583 :           asm_expr = TREE_OPERAND (asm_expr, 0);
   19289       15583 :         ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
   19290             :       }
   19291       15583 :       break;
   19292             : 
   19293       74190 :     case TRY_BLOCK:
   19294       74190 :       if (CLEANUP_P (t))
   19295             :         {
   19296           0 :           stmt = begin_try_block ();
   19297           0 :           RECUR (TRY_STMTS (t));
   19298           0 :           finish_cleanup_try_block (stmt);
   19299           0 :           finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
   19300             :         }
   19301             :       else
   19302             :         {
   19303       74190 :           tree compound_stmt = NULL_TREE;
   19304             : 
   19305       74190 :           if (FN_TRY_BLOCK_P (t))
   19306          45 :             stmt = begin_function_try_block (&compound_stmt);
   19307             :           else
   19308       74145 :             stmt = begin_try_block ();
   19309             : 
   19310       74190 :           RECUR (TRY_STMTS (t));
   19311             : 
   19312       74190 :           if (FN_TRY_BLOCK_P (t))
   19313          45 :             finish_function_try_block (stmt);
   19314             :           else
   19315       74145 :             finish_try_block (stmt);
   19316             : 
   19317       74190 :           RECUR (TRY_HANDLERS (t));
   19318       74190 :           if (FN_TRY_BLOCK_P (t))
   19319          45 :             finish_function_handler_sequence (stmt, compound_stmt);
   19320             :           else
   19321       74145 :             finish_handler_sequence (stmt);
   19322             :         }
   19323             :       break;
   19324             : 
   19325       74912 :     case HANDLER:
   19326       74912 :       {
   19327       74912 :         tree decl = HANDLER_PARMS (t);
   19328             : 
   19329       74912 :         if (decl)
   19330             :           {
   19331        1690 :             decl = tsubst (decl, args, complain, in_decl);
   19332             :             /* Prevent instantiate_decl from trying to instantiate
   19333             :                this variable.  We've already done all that needs to be
   19334             :                done.  */
   19335        1690 :             if (decl != error_mark_node)
   19336        1682 :               DECL_TEMPLATE_INSTANTIATED (decl) = 1;
   19337             :           }
   19338       74912 :         stmt = begin_handler ();
   19339       74912 :         finish_handler_parms (decl, stmt);
   19340       74912 :         RECUR (HANDLER_BODY (t));
   19341       74912 :         finish_handler (stmt);
   19342             :       }
   19343       74912 :       break;
   19344             : 
   19345      321862 :     case TAG_DEFN:
   19346      321862 :       tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
   19347      321862 :       if (CLASS_TYPE_P (tmp))
   19348             :         {
   19349             :           /* Local classes are not independent templates; they are
   19350             :              instantiated along with their containing function.  And this
   19351             :              way we don't have to deal with pushing out of one local class
   19352             :              to instantiate a member of another local class.  */
   19353             :           /* Closures are handled by the LAMBDA_EXPR.  */
   19354      643270 :           gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
   19355      321676 :           complete_type (tmp);
   19356      321676 :           if (dependent_type_p (tmp))
   19357             :             {
   19358             :               /* This is a partial instantiation, try again when full.  */
   19359          15 :               add_stmt (build_min (TAG_DEFN, tmp));
   19360          15 :               break;
   19361             :             }
   19362      321661 :           tree save_ccp = current_class_ptr;
   19363      321661 :           tree save_ccr = current_class_ref;
   19364     2227571 :           for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
   19365     1905910 :             if ((VAR_P (fld)
   19366     1905906 :                  || (TREE_CODE (fld) == FUNCTION_DECL
   19367     1393641 :                      && !DECL_ARTIFICIAL (fld)))
   19368     3299551 :                 && DECL_TEMPLATE_INSTANTIATION (fld))
   19369     1393645 :               instantiate_decl (fld, /*defer_ok=*/false,
   19370             :                                 /*expl_inst_class=*/false);
   19371      512265 :             else if (TREE_CODE (fld) == FIELD_DECL)
   19372      190455 :               maybe_instantiate_nsdmi_init (fld, tf_warning_or_error);
   19373      321661 :           current_class_ptr = save_ccp;
   19374      321661 :           current_class_ref = save_ccr;
   19375             :         }
   19376             :       break;
   19377             : 
   19378     2841470 :     case STATIC_ASSERT:
   19379     2841470 :       {
   19380     2841470 :         tree condition;
   19381             : 
   19382     2841470 :         ++c_inhibit_evaluation_warnings;
   19383     2841470 :         condition = tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
   19384             :                                  complain, in_decl);
   19385     2841470 :         --c_inhibit_evaluation_warnings;
   19386             : 
   19387     2841470 :         finish_static_assert (condition,
   19388     2841470 :                               STATIC_ASSERT_MESSAGE (t),
   19389     2841470 :                               STATIC_ASSERT_SOURCE_LOCATION (t),
   19390             :                               /*member_p=*/false, /*show_expr_p=*/true);
   19391             :       }
   19392     2841470 :       break;
   19393             : 
   19394         320 :     case OACC_KERNELS:
   19395         320 :     case OACC_PARALLEL:
   19396         320 :     case OACC_SERIAL:
   19397         320 :       tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
   19398             :                                 in_decl);
   19399         320 :       stmt = begin_omp_parallel ();
   19400         320 :       RECUR (OMP_BODY (t));
   19401         320 :       finish_omp_construct (TREE_CODE (t), stmt, tmp);
   19402         320 :       break;
   19403             : 
   19404         949 :     case OMP_PARALLEL:
   19405         949 :       r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
   19406         949 :       tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
   19407             :                                 complain, in_decl);
   19408         949 :       if (OMP_PARALLEL_COMBINED (t))
   19409         508 :         omp_parallel_combined_clauses = &tmp;
   19410         949 :       stmt = begin_omp_parallel ();
   19411         949 :       RECUR (OMP_PARALLEL_BODY (t));
   19412         949 :       gcc_assert (omp_parallel_combined_clauses == NULL);
   19413        1898 :       OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
   19414         949 :         = OMP_PARALLEL_COMBINED (t);
   19415         949 :       pop_omp_privatization_clauses (r);
   19416         949 :       break;
   19417             : 
   19418         449 :     case OMP_TASK:
   19419         449 :       if (OMP_TASK_BODY (t) == NULL_TREE)
   19420             :         {
   19421           0 :           tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
   19422             :                                     complain, in_decl);
   19423           0 :           t = copy_node (t);
   19424           0 :           OMP_TASK_CLAUSES (t) = tmp;
   19425           0 :           add_stmt (t);
   19426           0 :           break;
   19427             :         }
   19428         449 :       r = push_omp_privatization_clauses (false);
   19429         449 :       tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
   19430             :                                 complain, in_decl);
   19431         449 :       stmt = begin_omp_task ();
   19432         449 :       RECUR (OMP_TASK_BODY (t));
   19433         449 :       finish_omp_task (tmp, stmt);
   19434         449 :       pop_omp_privatization_clauses (r);
   19435         449 :       break;
   19436             : 
   19437        1259 :     case OMP_FOR:
   19438        1259 :     case OMP_LOOP:
   19439        1259 :     case OMP_SIMD:
   19440        1259 :     case OMP_DISTRIBUTE:
   19441        1259 :     case OMP_TASKLOOP:
   19442        1259 :     case OACC_LOOP:
   19443        1259 :       {
   19444        1259 :         tree clauses, body, pre_body;
   19445        1259 :         tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
   19446        1259 :         tree orig_declv = NULL_TREE;
   19447        1259 :         tree incrv = NULL_TREE;
   19448        1259 :         enum c_omp_region_type ort = C_ORT_OMP;
   19449        1259 :         bool any_range_for = false;
   19450        1259 :         int i;
   19451             : 
   19452        1259 :         if (TREE_CODE (t) == OACC_LOOP)
   19453          42 :           ort = C_ORT_ACC;
   19454             : 
   19455        1259 :         r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
   19456        1259 :         clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
   19457             :                                       in_decl);
   19458        1259 :         if (OMP_FOR_INIT (t) != NULL_TREE)
   19459             :           {
   19460        1041 :             declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   19461        1041 :             if (OMP_FOR_ORIG_DECLS (t))
   19462         599 :               orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   19463        1041 :             initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   19464        1041 :             condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   19465        1041 :             incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   19466             :           }
   19467             : 
   19468        1259 :         keep_next_level (true);
   19469        1259 :         stmt = begin_omp_structured_block ();
   19470             : 
   19471        1259 :         pre_body = push_stmt_list ();
   19472        1259 :         RECUR (OMP_FOR_PRE_BODY (t));
   19473        1259 :         pre_body = pop_stmt_list (pre_body);
   19474             : 
   19475        1259 :         if (OMP_FOR_INIT (t) != NULL_TREE)
   19476        2265 :           for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
   19477        1224 :             any_range_for
   19478        1224 :               |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
   19479             :                                           condv, incrv, &clauses, args,
   19480             :                                           complain, in_decl);
   19481        1259 :         omp_parallel_combined_clauses = NULL;
   19482             : 
   19483        1259 :         if (any_range_for)
   19484             :           {
   19485          63 :             gcc_assert (orig_declv);
   19486          63 :             body = begin_omp_structured_block ();
   19487         211 :             for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
   19488          85 :               if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
   19489          68 :                   && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
   19490         153 :                   && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
   19491          68 :                 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
   19492          68 :                                          TREE_VEC_ELT (declv, i));
   19493             :           }
   19494             :         else
   19495        1196 :           body = push_stmt_list ();
   19496        1259 :         RECUR (OMP_FOR_BODY (t));
   19497        1259 :         if (any_range_for)
   19498          63 :           body = finish_omp_structured_block (body);
   19499             :         else
   19500        1196 :           body = pop_stmt_list (body);
   19501             : 
   19502        1259 :         if (OMP_FOR_INIT (t) != NULL_TREE)
   19503        1041 :           t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
   19504             :                               orig_declv, initv, condv, incrv, body, pre_body,
   19505             :                               NULL, clauses);
   19506             :         else
   19507             :           {
   19508         218 :             t = make_node (TREE_CODE (t));
   19509         218 :             TREE_TYPE (t) = void_type_node;
   19510         218 :             OMP_FOR_BODY (t) = body;
   19511         218 :             OMP_FOR_PRE_BODY (t) = pre_body;
   19512         218 :             OMP_FOR_CLAUSES (t) = clauses;
   19513         218 :             SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
   19514         218 :             add_stmt (t);
   19515             :           }
   19516             : 
   19517        1259 :         add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
   19518             :                                         t));
   19519        1259 :         pop_omp_privatization_clauses (r);
   19520             :       }
   19521        1259 :       break;
   19522             : 
   19523          25 :     case OMP_SECTIONS:
   19524          25 :     case OMP_MASKED:
   19525          25 :       omp_parallel_combined_clauses = NULL;
   19526             :       /* FALLTHRU */
   19527         589 :     case OMP_SINGLE:
   19528         589 :     case OMP_SCOPE:
   19529         589 :     case OMP_TEAMS:
   19530         589 :     case OMP_CRITICAL:
   19531         589 :     case OMP_TASKGROUP:
   19532         589 :     case OMP_SCAN:
   19533         589 :       r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
   19534         589 :                                           && OMP_TEAMS_COMBINED (t));
   19535         589 :       tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
   19536             :                                 in_decl);
   19537         589 :       if (TREE_CODE (t) == OMP_TEAMS)
   19538             :         {
   19539         330 :           keep_next_level (true);
   19540         330 :           stmt = begin_omp_structured_block ();
   19541         330 :           RECUR (OMP_BODY (t));
   19542         330 :           stmt = finish_omp_structured_block (stmt);
   19543             :         }
   19544             :       else
   19545             :         {
   19546         259 :           stmt = push_stmt_list ();
   19547         259 :           RECUR (OMP_BODY (t));
   19548         259 :           stmt = pop_stmt_list (stmt);
   19549             :         }
   19550             : 
   19551         589 :       if (TREE_CODE (t) == OMP_CRITICAL
   19552          24 :           && tmp != NULL_TREE
   19553         605 :           && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
   19554             :         {
   19555           8 :           error_at (OMP_CLAUSE_LOCATION (tmp),
   19556             :                     "%<#pragma omp critical%> with %<hint%> clause requires "
   19557             :                     "a name, except when %<omp_sync_hint_none%> is used");
   19558           8 :           RETURN (error_mark_node);
   19559             :         }
   19560         581 :       t = copy_node (t);
   19561         581 :       OMP_BODY (t) = stmt;
   19562         581 :       OMP_CLAUSES (t) = tmp;
   19563         581 :       add_stmt (t);
   19564         581 :       pop_omp_privatization_clauses (r);
   19565         581 :       break;
   19566             : 
   19567         149 :     case OMP_DEPOBJ:
   19568         149 :       r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
   19569         149 :       if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
   19570             :         {
   19571         137 :           enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
   19572         137 :           if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
   19573             :             {
   19574          76 :               tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
   19575             :                                         args, complain, in_decl);
   19576          76 :               if (tmp == NULL_TREE)
   19577           8 :                 tmp = error_mark_node;
   19578             :             }
   19579             :           else
   19580             :             {
   19581         122 :               kind = (enum omp_clause_depend_kind)
   19582          61 :                      tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
   19583          61 :               tmp = NULL_TREE;
   19584             :             }
   19585         137 :           finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
   19586             :         }
   19587             :       else
   19588          12 :         finish_omp_depobj (EXPR_LOCATION (t), r,
   19589             :                            OMP_CLAUSE_DEPEND_INVALID,
   19590          12 :                            OMP_DEPOBJ_CLAUSES (t));
   19591             :       break;
   19592             : 
   19593         536 :     case OACC_DATA:
   19594         536 :     case OMP_TARGET_DATA:
   19595         536 :     case OMP_TARGET:
   19596        1056 :       tmp = tsubst_omp_clauses (OMP_CLAUSES (t),
   19597             :                                 TREE_CODE (t) == OACC_DATA
   19598             :                                 ? C_ORT_ACC
   19599             :                                 : TREE_CODE (t) == OMP_TARGET
   19600         520 :                                 ? C_ORT_OMP_TARGET : C_ORT_OMP,
   19601             :                                 args, complain, in_decl);
   19602         536 :       keep_next_level (true);
   19603         536 :       stmt = begin_omp_structured_block ();
   19604             : 
   19605         536 :       RECUR (OMP_BODY (t));
   19606         536 :       stmt = finish_omp_structured_block (stmt);
   19607             : 
   19608         536 :       t = copy_node (t);
   19609         536 :       OMP_BODY (t) = stmt;
   19610         536 :       OMP_CLAUSES (t) = tmp;
   19611             : 
   19612         536 :       if (TREE_CODE (t) == OMP_TARGET)
   19613         508 :         finish_omp_target_clauses (EXPR_LOCATION (t), OMP_BODY (t),
   19614             :                                    &OMP_CLAUSES (t));
   19615             : 
   19616         536 :       if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
   19617             :         {
   19618         106 :           tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
   19619         106 :           if (teams)
   19620             :             /* For combined target teams, ensure the num_teams and
   19621             :                thread_limit clause expressions are evaluated on the host,
   19622             :                before entering the target construct.  */
   19623         210 :             for (tree c = OMP_TEAMS_CLAUSES (teams);
   19624         210 :                  c; c = OMP_CLAUSE_CHAIN (c))
   19625         104 :               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
   19626         104 :                   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
   19627         188 :                 for (int i = 0;
   19628         292 :                      i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
   19629         188 :                   if (OMP_CLAUSE_OPERAND (c, i)
   19630         188 :                       && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
   19631             :                     {
   19632          56 :                       tree expr = OMP_CLAUSE_OPERAND (c, i);
   19633          56 :                       expr = force_target_expr (TREE_TYPE (expr), expr,
   19634             :                                                 tf_none);
   19635          56 :                       if (expr == error_mark_node)
   19636           0 :                         continue;
   19637          56 :                       tmp = TARGET_EXPR_SLOT (expr);
   19638          56 :                       add_stmt (expr);
   19639          56 :                       OMP_CLAUSE_OPERAND (c, i) = expr;
   19640          56 :                       tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
   19641             :                                                   OMP_CLAUSE_FIRSTPRIVATE);
   19642          56 :                       OMP_CLAUSE_DECL (tc) = tmp;
   19643          56 :                       OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
   19644          56 :                       OMP_TARGET_CLAUSES (t) = tc;
   19645             :                     }
   19646             :         }
   19647         536 :       add_stmt (t);
   19648         536 :       break;
   19649             : 
   19650           1 :     case OACC_DECLARE:
   19651           1 :       t = copy_node (t);
   19652           1 :       tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
   19653             :                                 complain, in_decl);
   19654           1 :       OACC_DECLARE_CLAUSES (t) = tmp;
   19655           1 :       add_stmt (t);
   19656           1 :       break;
   19657             : 
   19658          40 :     case OMP_TARGET_UPDATE:
   19659          40 :     case OMP_TARGET_ENTER_DATA:
   19660          40 :     case OMP_TARGET_EXIT_DATA:
   19661          40 :       tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
   19662             :                                 complain, in_decl);
   19663          40 :       t = copy_node (t);
   19664          40 :       OMP_STANDALONE_CLAUSES (t) = tmp;
   19665          40 :       add_stmt (t);
   19666          40 :       break;
   19667             : 
   19668         486 :     case OACC_CACHE:
   19669         486 :     case OACC_ENTER_DATA:
   19670         486 :     case OACC_EXIT_DATA:
   19671         486 :     case OACC_UPDATE:
   19672         486 :       tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
   19673             :                                 complain, in_decl);
   19674         486 :       t = copy_node (t);
   19675         486 :       OMP_STANDALONE_CLAUSES (t) = tmp;
   19676         486 :       add_stmt (t);
   19677         486 :       break;
   19678             : 
   19679          44 :     case OMP_ORDERED:
   19680          44 :       tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
   19681             :                                 complain, in_decl);
   19682          44 :       if (OMP_BODY (t))
   19683             :         {
   19684           4 :           stmt = push_stmt_list ();
   19685           4 :           RECUR (OMP_BODY (t));
   19686           4 :           stmt = pop_stmt_list (stmt);
   19687             :         }
   19688             :       else
   19689          40 :         stmt = NULL_TREE;
   19690             : 
   19691          44 :       t = copy_node (t);
   19692          44 :       OMP_BODY (t) = stmt;
   19693          44 :       OMP_ORDERED_CLAUSES (t) = tmp;
   19694          44 :       add_stmt (t);
   19695          44 :       break;
   19696             : 
   19697          16 :     case OMP_MASTER:
   19698          16 :       omp_parallel_combined_clauses = NULL;
   19699             :       /* FALLTHRU */
   19700          41 :     case OMP_SECTION:
   19701          41 :       stmt = push_stmt_list ();
   19702          41 :       RECUR (OMP_BODY (t));
   19703          41 :       stmt = pop_stmt_list (stmt);
   19704             : 
   19705          41 :       t = copy_node (t);
   19706          41 :       OMP_BODY (t) = stmt;
   19707          41 :       add_stmt (t);
   19708          41 :       break;
   19709             : 
   19710         665 :     case OMP_ATOMIC:
   19711         665 :       gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
   19712         665 :       tmp = NULL_TREE;
   19713         665 :       if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
   19714          40 :         tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
   19715             :                                   complain, in_decl);
   19716         665 :       if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
   19717             :         {
   19718         220 :           tree op1 = TREE_OPERAND (t, 1);
   19719         220 :           tree rhs1 = NULL_TREE;
   19720         220 :           tree r = NULL_TREE;
   19721         220 :           tree lhs, rhs;
   19722         220 :           if (TREE_CODE (op1) == COMPOUND_EXPR)
   19723             :             {
   19724         119 :               rhs1 = RECUR (TREE_OPERAND (op1, 0));
   19725         119 :               op1 = TREE_OPERAND (op1, 1);
   19726             :             }
   19727         220 :           if (TREE_CODE (op1) == COND_EXPR)
   19728             :             {
   19729          56 :               gcc_assert (rhs1 == NULL_TREE);
   19730          56 :               tree c = TREE_OPERAND (op1, 0);
   19731          56 :               if (TREE_CODE (c) == MODIFY_EXPR)
   19732             :                 {
   19733          16 :                   r = RECUR (TREE_OPERAND (c, 0));
   19734          16 :                   c = TREE_OPERAND (c, 1);
   19735             :                 }
   19736          56 :               gcc_assert (TREE_CODE (c) == EQ_EXPR);
   19737          56 :               rhs = RECUR (TREE_OPERAND (c, 1));
   19738          56 :               lhs = RECUR (TREE_OPERAND (op1, 2));
   19739          56 :               rhs1 = RECUR (TREE_OPERAND (op1, 1));
   19740             :             }
   19741             :           else
   19742             :             {
   19743         164 :               lhs = RECUR (TREE_OPERAND (op1, 0));
   19744         164 :               rhs = RECUR (TREE_OPERAND (op1, 1));
   19745             :             }
   19746         220 :           finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
   19747             :                              lhs, rhs, NULL_TREE, NULL_TREE, rhs1, r,
   19748         220 :                              tmp, OMP_ATOMIC_MEMORY_ORDER (t),
   19749         220 :                              OMP_ATOMIC_WEAK (t));
   19750             :         }
   19751             :       else
   19752             :         {
   19753         445 :           tree op1 = TREE_OPERAND (t, 1);
   19754         445 :           tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
   19755         445 :           tree rhs1 = NULL_TREE, r = NULL_TREE;
   19756         445 :           enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
   19757         445 :           enum tree_code opcode = NOP_EXPR;
   19758         445 :           if (code == OMP_ATOMIC_READ)
   19759             :             {
   19760         165 :               v = RECUR (TREE_OPERAND (op1, 0));
   19761         165 :               lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
   19762             :             }
   19763         280 :           else if (code == OMP_ATOMIC_CAPTURE_OLD
   19764         280 :                    || code == OMP_ATOMIC_CAPTURE_NEW)
   19765             :             {
   19766         255 :               tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
   19767         255 :               v = RECUR (TREE_OPERAND (op1, 0));
   19768         255 :               lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
   19769         255 :               if (TREE_CODE (op11) == COMPOUND_EXPR)
   19770             :                 {
   19771         103 :                   rhs1 = RECUR (TREE_OPERAND (op11, 0));
   19772         103 :                   op11 = TREE_OPERAND (op11, 1);
   19773             :                 }
   19774         255 :               if (TREE_CODE (op11) == COND_EXPR)
   19775             :                 {
   19776          88 :                   gcc_assert (rhs1 == NULL_TREE);
   19777          88 :                   tree c = TREE_OPERAND (op11, 0);
   19778          88 :                   if (TREE_CODE (c) == MODIFY_EXPR)
   19779             :                     {
   19780          40 :                       r = RECUR (TREE_OPERAND (c, 0));
   19781          40 :                       c = TREE_OPERAND (c, 1);
   19782             :                     }
   19783          88 :                   gcc_assert (TREE_CODE (c) == EQ_EXPR);
   19784          88 :                   rhs = RECUR (TREE_OPERAND (c, 1));
   19785          88 :                   lhs = RECUR (TREE_OPERAND (op11, 2));
   19786          88 :                   rhs1 = RECUR (TREE_OPERAND (op11, 1));
   19787             :                 }
   19788             :               else
   19789             :                 {
   19790         167 :                   lhs = RECUR (TREE_OPERAND (op11, 0));
   19791         167 :                   rhs = RECUR (TREE_OPERAND (op11, 1));
   19792             :                 }
   19793         255 :               opcode = TREE_CODE (op11);
   19794         255 :               if (opcode == MODIFY_EXPR)
   19795          11 :                 opcode = NOP_EXPR;
   19796             :             }
   19797             :           else
   19798             :             {
   19799          25 :               code = OMP_ATOMIC;
   19800          25 :               lhs = RECUR (TREE_OPERAND (op1, 0));
   19801          25 :               rhs = RECUR (TREE_OPERAND (op1, 1));
   19802             :             }
   19803         445 :           finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
   19804             :                              lhs1, rhs1, r, tmp,
   19805         445 :                              OMP_ATOMIC_MEMORY_ORDER (t), OMP_ATOMIC_WEAK (t));
   19806             :         }
   19807             :       break;
   19808             : 
   19809          84 :     case TRANSACTION_EXPR:
   19810          84 :       {
   19811          84 :         int flags = 0;
   19812          84 :         flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
   19813          84 :         flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
   19814             : 
   19815          84 :         if (TRANSACTION_EXPR_IS_STMT (t))
   19816             :           {
   19817          50 :             tree body = TRANSACTION_EXPR_BODY (t);
   19818          50 :             tree noex = NULL_TREE;
   19819          50 :             if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
   19820             :               {
   19821          12 :                 noex = MUST_NOT_THROW_COND (body);
   19822          12 :                 if (noex == NULL_TREE)
   19823           3 :                   noex = boolean_true_node;
   19824          12 :                 body = TREE_OPERAND (body, 0);
   19825             :               }
   19826          50 :             stmt = begin_transaction_stmt (input_location, NULL, flags);
   19827          50 :             RECUR (body);
   19828          50 :             finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
   19829             :           }
   19830             :         else
   19831             :           {
   19832          34 :             stmt = build_transaction_expr (EXPR_LOCATION (t),
   19833          34 :                                            RECUR (TRANSACTION_EXPR_BODY (t)),
   19834             :                                            flags, NULL_TREE);
   19835          34 :             RETURN (stmt);
   19836             :           }
   19837             :       }
   19838          50 :       break;
   19839             : 
   19840          21 :     case MUST_NOT_THROW_EXPR:
   19841          21 :       {
   19842          21 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   19843          21 :         tree cond = RECUR (MUST_NOT_THROW_COND (t));
   19844          21 :         RETURN (build_must_not_throw_expr (op0, cond));
   19845             :       }
   19846             : 
   19847           0 :     case EXPR_PACK_EXPANSION:
   19848           0 :       error ("invalid use of pack expansion expression");
   19849           0 :       RETURN (error_mark_node);
   19850             : 
   19851           0 :     case NONTYPE_ARGUMENT_PACK:
   19852           0 :       error ("use %<...%> to expand argument pack");
   19853           0 :       RETURN (error_mark_node);
   19854             : 
   19855       76633 :     case COMPOUND_EXPR:
   19856       76633 :       tmp = RECUR (TREE_OPERAND (t, 0));
   19857       76633 :       if (tmp == NULL_TREE)
   19858             :         /* If the first operand was a statement, we're done with it.  */
   19859       41163 :         RETURN (RECUR (TREE_OPERAND (t, 1)));
   19860       35470 :       RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
   19861             :                                     RECUR (TREE_OPERAND (t, 1)),
   19862             :                                     templated_operator_saved_lookups (t),
   19863             :                                     complain));
   19864             : 
   19865          11 :     case ANNOTATE_EXPR:
   19866          11 :       tmp = RECUR (TREE_OPERAND (t, 0));
   19867          11 :       RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
   19868             :                           TREE_TYPE (tmp), tmp,
   19869             :                           RECUR (TREE_OPERAND (t, 1)),
   19870             :                           RECUR (TREE_OPERAND (t, 2))));
   19871             : 
   19872       90406 :     case PREDICT_EXPR:
   19873       90406 :       RETURN (add_stmt (copy_node (t)));
   19874             : 
   19875   263056050 :     default:
   19876   263056050 :       gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
   19877             : 
   19878   263056050 :       RETURN (tsubst_copy_and_build (t, args, complain, in_decl));
   19879             :     }
   19880             : 
   19881    90249559 :   RETURN (NULL_TREE);
   19882   353467359 :  out:
   19883   353467359 :   input_location = loc;
   19884   353467359 :   return r;
   19885             : #undef RECUR
   19886             : #undef RETURN
   19887             : }
   19888             : 
   19889             : /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
   19890             :    function.  For description of the body see comment above
   19891             :    cp_parser_omp_declare_reduction_exprs.  */
   19892             : 
   19893             : static void
   19894         126 : tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   19895             : {
   19896         126 :   if (t == NULL_TREE || t == error_mark_node)
   19897           0 :     return;
   19898             : 
   19899         126 :   gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
   19900             : 
   19901         126 :   tree_stmt_iterator tsi;
   19902         126 :   int i;
   19903         126 :   tree stmts[7];
   19904         126 :   memset (stmts, 0, sizeof stmts);
   19905         126 :   for (i = 0, tsi = tsi_start (t);
   19906         702 :        i < 7 && !tsi_end_p (tsi);
   19907         736 :        i++, tsi_next (&tsi))
   19908         610 :     stmts[i] = tsi_stmt (tsi);
   19909         126 :   gcc_assert (tsi_end_p (tsi));
   19910             : 
   19911         126 :   if (i >= 3)
   19912             :     {
   19913         126 :       gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
   19914             :                   && TREE_CODE (stmts[1]) == DECL_EXPR);
   19915         126 :       tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
   19916             :                              args, complain, in_decl);
   19917         126 :       tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
   19918             :                             args, complain, in_decl);
   19919             :       /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
   19920             :          expect to be pushing it.  */
   19921         126 :       DECL_CONTEXT (omp_out) = current_function_decl;
   19922         126 :       DECL_CONTEXT (omp_in) = current_function_decl;
   19923         126 :       keep_next_level (true);
   19924         126 :       tree block = begin_omp_structured_block ();
   19925         126 :       tsubst_expr (stmts[2], args, complain, in_decl);
   19926         126 :       block = finish_omp_structured_block (block);
   19927         126 :       block = maybe_cleanup_point_expr_void (block);
   19928         126 :       add_decl_expr (omp_out);
   19929         126 :       copy_warning (omp_out, DECL_EXPR_DECL (stmts[0]));
   19930         126 :       add_decl_expr (omp_in);
   19931         126 :       finish_expr_stmt (block);
   19932             :     }
   19933         126 :   if (i >= 6)
   19934             :     {
   19935          66 :       gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
   19936             :                   && TREE_CODE (stmts[4]) == DECL_EXPR);
   19937          66 :       tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
   19938             :                               args, complain, in_decl);
   19939          66 :       tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
   19940             :                               args, complain, in_decl);
   19941          66 :       DECL_CONTEXT (omp_priv) = current_function_decl;
   19942          66 :       DECL_CONTEXT (omp_orig) = current_function_decl;
   19943          66 :       keep_next_level (true);
   19944          66 :       tree block = begin_omp_structured_block ();
   19945          66 :       tsubst_expr (stmts[5], args, complain, in_decl);
   19946          66 :       block = finish_omp_structured_block (block);
   19947          66 :       block = maybe_cleanup_point_expr_void (block);
   19948          66 :       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
   19949          66 :       add_decl_expr (omp_priv);
   19950          66 :       add_decl_expr (omp_orig);
   19951          66 :       finish_expr_stmt (block);
   19952          66 :       if (i == 7)
   19953          34 :         add_decl_expr (omp_orig);
   19954             :     }
   19955             : }
   19956             : 
   19957             : /* T is a postfix-expression that is not being used in a function
   19958             :    call.  Return the substituted version of T.  */
   19959             : 
   19960             : static tree
   19961    14560421 : tsubst_non_call_postfix_expression (tree t, tree args,
   19962             :                                     tsubst_flags_t complain,
   19963             :                                     tree in_decl)
   19964             : {
   19965    14560421 :   if (TREE_CODE (t) == SCOPE_REF)
   19966        2051 :     t = tsubst_qualified_id (t, args, complain, in_decl,
   19967             :                              /*done=*/false, /*address_p=*/false);
   19968             :   else
   19969    14558370 :     t = tsubst_copy_and_build (t, args, complain, in_decl);
   19970             : 
   19971    14560421 :   return t;
   19972             : }
   19973             : 
   19974             : /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
   19975             :    LAMBDA_EXPR_CAPTURE_LIST passed in LIST.  Do deduction for a previously
   19976             :    dependent init-capture.  EXPLICIT_P is true if the original list had
   19977             :    explicit captures.  */
   19978             : 
   19979             : static void
   19980       34055 : prepend_one_capture (tree field, tree init, tree &list, bool explicit_p,
   19981             :                      tsubst_flags_t complain)
   19982             : {
   19983       34055 :   if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
   19984             :     {
   19985          56 :       tree type = NULL_TREE;
   19986          56 :       if (!init)
   19987             :         {
   19988           3 :           if (complain & tf_error)
   19989           3 :             error ("empty initializer in lambda init-capture");
   19990           3 :           init = error_mark_node;
   19991             :         }
   19992          53 :       else if (TREE_CODE (init) == TREE_LIST)
   19993           0 :         init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
   19994          56 :       if (!type)
   19995          56 :         type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
   19996          56 :       TREE_TYPE (field) = type;
   19997          56 :       cp_apply_type_quals_to_decl (cp_type_quals (type), field);
   19998             :     }
   19999       34055 :   list = tree_cons (field, init, list);
   20000       34055 :   LAMBDA_CAPTURE_EXPLICIT_P (list) = explicit_p;
   20001       34055 : }
   20002             : 
   20003             : /* T is a LAMBDA_EXPR.  Generate a new LAMBDA_EXPR for the current
   20004             :    instantiation context.  Instantiating a pack expansion containing a lambda
   20005             :    might result in multiple lambdas all based on the same lambda in the
   20006             :    template.  */
   20007             : 
   20008             : tree
   20009       38863 : tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   20010             : {
   20011       38863 :   tree oldfn = lambda_function (t);
   20012       38863 :   in_decl = oldfn;
   20013             : 
   20014       38863 :   tree r = build_lambda_expr ();
   20015             : 
   20016       38863 :   LAMBDA_EXPR_LOCATION (r)
   20017       38863 :     = LAMBDA_EXPR_LOCATION (t);
   20018       38863 :   LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
   20019       38863 :     = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
   20020       38863 :   LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
   20021       38863 :   if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
   20022         306 :     LAMBDA_EXPR_REGEN_INFO (r)
   20023         153 :       = build_template_info (t, add_to_template_args (TI_ARGS (ti),
   20024             :                                                       preserve_args (args)));
   20025             :   else
   20026       77420 :     LAMBDA_EXPR_REGEN_INFO (r)
   20027       38710 :       = build_template_info (t, preserve_args (args));
   20028             : 
   20029       38863 :   gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
   20030             :               && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
   20031             : 
   20032       38863 :   vec<tree,va_gc>* field_packs = NULL;
   20033             : 
   20034       72910 :   for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
   20035       34047 :        cap = TREE_CHAIN (cap))
   20036             :     {
   20037       34051 :       tree ofield = TREE_PURPOSE (cap);
   20038       34051 :       tree init = TREE_VALUE (cap);
   20039       34051 :       if (PACK_EXPANSION_P (init))
   20040          58 :         init = tsubst_pack_expansion (init, args, complain, in_decl);
   20041             :       else
   20042       33993 :         init = tsubst_copy_and_build (init, args, complain, in_decl);
   20043             : 
   20044       34051 :       if (init == error_mark_node)
   20045           4 :         return error_mark_node;
   20046             : 
   20047       34047 :       if (init && TREE_CODE (init) == TREE_LIST)
   20048           6 :         init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
   20049             : 
   20050       34047 :       if (!processing_template_decl
   20051       33923 :           && init && TREE_CODE (init) != TREE_VEC
   20052       67916 :           && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
   20053             :         {
   20054             :           /* For a VLA, simply tsubsting the field type won't work, we need to
   20055             :              go through add_capture again.  XXX do we want to do this for all
   20056             :              captures?  */
   20057           9 :           tree name = (get_identifier
   20058             :                        (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
   20059           9 :           tree ftype = TREE_TYPE (ofield);
   20060           9 :           bool by_ref = (TYPE_REF_P (ftype)
   20061           9 :                          || (TREE_CODE (ftype) == DECLTYPE_TYPE
   20062           0 :                              && DECLTYPE_FOR_REF_CAPTURE (ftype)));
   20063           9 :           add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
   20064           9 :           continue;
   20065           9 :         }
   20066             : 
   20067       34038 :       if (PACK_EXPANSION_P (ofield))
   20068          48 :         ofield = PACK_EXPANSION_PATTERN (ofield);
   20069       34038 :       tree field = tsubst_decl (ofield, args, complain);
   20070             : 
   20071       34038 :       if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
   20072             :         {
   20073             :           /* Remember these for when we've pushed local_specializations.  */
   20074          12 :           vec_safe_push (field_packs, ofield);
   20075          12 :           vec_safe_push (field_packs, field);
   20076             :         }
   20077             : 
   20078       34038 :       if (field == error_mark_node)
   20079           0 :         return error_mark_node;
   20080             : 
   20081       34038 :       if (TREE_CODE (field) == TREE_VEC)
   20082             :         {
   20083          51 :           int len = TREE_VEC_LENGTH (field);
   20084          51 :           gcc_assert (TREE_CODE (init) == TREE_VEC
   20085             :                       && TREE_VEC_LENGTH (init) == len);
   20086         119 :           for (int i = 0; i < len; ++i)
   20087         136 :             prepend_one_capture (TREE_VEC_ELT (field, i),
   20088          68 :                                  TREE_VEC_ELT (init, i),
   20089          68 :                                  LAMBDA_EXPR_CAPTURE_LIST (r),
   20090          68 :                                  LAMBDA_CAPTURE_EXPLICIT_P (cap),
   20091             :                                  complain);
   20092             :         }
   20093             :       else
   20094             :         {
   20095       67974 :           prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
   20096       33987 :                                LAMBDA_CAPTURE_EXPLICIT_P (cap), complain);
   20097             : 
   20098       33987 :           if (id_equal (DECL_NAME (field), "__this"))
   20099        2396 :             LAMBDA_EXPR_THIS_CAPTURE (r) = field;
   20100             :         }
   20101             :     }
   20102             : 
   20103       38859 :   tree type = begin_lambda_type (r);
   20104       38859 :   if (type == error_mark_node)
   20105             :     return error_mark_node;
   20106             : 
   20107       38855 :   if (LAMBDA_EXPR_EXTRA_SCOPE (t))
   20108       38706 :     record_lambda_scope (r);
   20109         149 :   else if (TYPE_NAMESPACE_SCOPE_P (TREE_TYPE (t)))
   20110             :     /* If we're pushed into another scope (PR105652), fix it.  */
   20111         222 :     TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_NAME (type))
   20112         222 :       = TYPE_CONTEXT (TREE_TYPE (t));
   20113       38855 :   record_lambda_scope_discriminator (r);
   20114             : 
   20115             :   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
   20116       38855 :   determine_visibility (TYPE_NAME (type));
   20117             : 
   20118       38855 :   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
   20119             : 
   20120       38855 :   tree oldtmpl = (generic_lambda_fn_p (oldfn)
   20121       38855 :                   ? DECL_TI_TEMPLATE (oldfn)
   20122       42126 :                   : NULL_TREE);
   20123             : 
   20124        3271 :   tree tparms = NULL_TREE;
   20125        3271 :   if (oldtmpl)
   20126        3271 :     tparms = tsubst_template_parms (DECL_TEMPLATE_PARMS (oldtmpl), args, complain);
   20127             : 
   20128       38855 :   tree fntype = static_fn_type (oldfn);
   20129             : 
   20130       38855 :   tree saved_ctp = current_template_parms;
   20131       38855 :   if (oldtmpl)
   20132             :     {
   20133        3271 :       ++processing_template_decl;
   20134        3271 :       current_template_parms = tparms;
   20135             :     }
   20136       38855 :   fntype = tsubst (fntype, args, complain, in_decl);
   20137       38855 :   if (oldtmpl)
   20138             :     {
   20139        3271 :       current_template_parms = saved_ctp;
   20140        3271 :       --processing_template_decl;
   20141             :     }
   20142             : 
   20143       38855 :   if (fntype == error_mark_node)
   20144             :     r = error_mark_node;
   20145             :   else
   20146             :     {
   20147             :       /* The body of a lambda-expression is not a subexpression of the
   20148             :          enclosing expression.  Parms are to have DECL_CHAIN tsubsted,
   20149             :          which would be skipped if cp_unevaluated_operand.  */
   20150       38845 :       cp_evaluated ev;
   20151             : 
   20152             :       /* Fix the type of 'this'.  */
   20153       38845 :       fntype = build_memfn_type (fntype, type,
   20154             :                                  type_memfn_quals (fntype),
   20155             :                                  type_memfn_rqual (fntype));
   20156       38845 :       tree inst = (oldtmpl
   20157       38845 :                    ? tsubst_template_decl (oldtmpl, args, complain,
   20158             :                                            fntype, tparms)
   20159       35574 :                    : tsubst_function_decl (oldfn, args, complain, fntype));
   20160       38845 :       if (inst == error_mark_node)
   20161             :         {
   20162           1 :           r = error_mark_node;
   20163           1 :           goto out;
   20164             :         }
   20165       38844 :       finish_member_declaration (inst);
   20166       38844 :       record_lambda_scope_sig_discriminator (r, inst);
   20167             : 
   20168       42114 :       tree fn = oldtmpl ? DECL_TEMPLATE_RESULT (inst) : inst;
   20169             : 
   20170             :       /* Let finish_function set this.  */
   20171       38844 :       DECL_DECLARED_CONSTEXPR_P (fn) = false;
   20172             : 
   20173       38844 :       bool nested = cfun;
   20174       38844 :       if (nested)
   20175       38563 :         push_function_context ();
   20176             :       else
   20177             :         /* Still increment function_depth so that we don't GC in the
   20178             :            middle of an expression.  */
   20179         281 :         ++function_depth;
   20180             : 
   20181       38844 :       local_specialization_stack s (lss_copy);
   20182             : 
   20183       38844 :       bool save_in_consteval_if_p = in_consteval_if_p;
   20184       38844 :       in_consteval_if_p = false;
   20185             : 
   20186       38844 :       tree body = start_lambda_function (fn, r);
   20187             : 
   20188             :       /* Now record them for lookup_init_capture_pack.  */
   20189       38844 :       int fplen = vec_safe_length (field_packs);
   20190       38856 :       for (int i = 0; i < fplen; )
   20191             :         {
   20192          12 :           tree pack = (*field_packs)[i++];
   20193          12 :           tree inst = (*field_packs)[i++];
   20194          12 :           register_local_specialization (inst, pack);
   20195             :         }
   20196       38844 :       release_tree_vector (field_packs);
   20197             : 
   20198       38844 :       register_parameter_specializations (oldfn, fn);
   20199             : 
   20200       38844 :       if (oldtmpl)
   20201             :         {
   20202             :           /* We might not partially instantiate some parts of the function, so
   20203             :              copy these flags from the original template.  */
   20204        3270 :           language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
   20205        3270 :           current_function_returns_value = ol->returns_value;
   20206        3270 :           current_function_returns_null = ol->returns_null;
   20207        3270 :           current_function_returns_abnormally = ol->returns_abnormally;
   20208        3270 :           current_function_infinite_loop = ol->infinite_loop;
   20209             :         }
   20210             : 
   20211             :       /* [temp.deduct] A lambda-expression appearing in a function type or a
   20212             :          template parameter is not considered part of the immediate context for
   20213             :          the purposes of template argument deduction. */
   20214       38844 :       complain = tf_warning_or_error;
   20215             : 
   20216       38844 :       tree saved = DECL_SAVED_TREE (oldfn);
   20217       38844 :       if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved))
   20218             :         /* We already have a body block from start_lambda_function, we don't
   20219             :            need another to confuse NRV (91217).  */
   20220       38844 :         saved = BIND_EXPR_BODY (saved);
   20221             : 
   20222       38844 :       tsubst_expr (saved, args, complain, r);
   20223             : 
   20224       38844 :       finish_lambda_function (body);
   20225             : 
   20226       38844 :       in_consteval_if_p = save_in_consteval_if_p;
   20227             : 
   20228       38844 :       if (nested)
   20229       38563 :         pop_function_context ();
   20230             :       else
   20231         281 :         --function_depth;
   20232             : 
   20233             :       /* The capture list was built up in reverse order; fix that now.  */
   20234       38844 :       LAMBDA_EXPR_CAPTURE_LIST (r)
   20235       38844 :         = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
   20236             : 
   20237       38844 :       LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
   20238             : 
   20239       38844 :       maybe_add_lambda_conv_op (type);
   20240       38845 :     }
   20241             : 
   20242       38855 : out:
   20243       38855 :   finish_struct (type, /*attr*/NULL_TREE);
   20244             : 
   20245       38855 :   insert_pending_capture_proxies ();
   20246             : 
   20247       38855 :   return r;
   20248             : }
   20249             : 
   20250             : /* Subroutine of maybe_fold_fn_template_args.  */
   20251             : 
   20252             : static bool
   20253     9023992 : fold_targs_r (tree targs, tsubst_flags_t complain)
   20254             : {
   20255     9023992 :   int len = TREE_VEC_LENGTH (targs);
   20256    26235431 :   for (int i = 0; i < len; ++i)
   20257             :     {
   20258    17211442 :       tree &elt = TREE_VEC_ELT (targs, i);
   20259    17211442 :       if (!elt || TYPE_P (elt)
   20260      236669 :           || TREE_CODE (elt) == TEMPLATE_DECL)
   20261    16974954 :         continue;
   20262      236488 :       if (TREE_CODE (elt) == NONTYPE_ARGUMENT_PACK)
   20263             :         {
   20264           0 :           if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt), complain))
   20265             :             return false;
   20266             :         }
   20267      236488 :       else if (/* We can only safely preevaluate scalar prvalues.  */
   20268      472968 :                SCALAR_TYPE_P (TREE_TYPE (elt))
   20269      236333 :                && !glvalue_p (elt)
   20270      470886 :                && !TREE_CONSTANT (elt))
   20271             :         {
   20272        2756 :           elt = cxx_constant_value (elt, complain);
   20273        2756 :           if (elt == error_mark_node)
   20274             :             return false;
   20275             :         }
   20276             :     }
   20277             : 
   20278             :   return true;
   20279             : }
   20280             : 
   20281             : /* Try to do constant evaluation of any explicit template arguments in FN
   20282             :    before overload resolution, to get any errors only once.  Return true iff
   20283             :    we didn't have any problems folding.  */
   20284             : 
   20285             : static bool
   20286    45957037 : maybe_fold_fn_template_args (tree fn, tsubst_flags_t complain)
   20287             : {
   20288    45957037 :   if (processing_template_decl || fn == NULL_TREE)
   20289             :     return true;
   20290    34340088 :   if (fn == error_mark_node)
   20291             :     return false;
   20292    34331147 :   if (TREE_CODE (fn) == OFFSET_REF
   20293    34268497 :       || TREE_CODE (fn) == COMPONENT_REF)
   20294     5854177 :     fn = TREE_OPERAND (fn, 1);
   20295    34331147 :   if (BASELINK_P (fn))
   20296    13591655 :     fn = BASELINK_FUNCTIONS (fn);
   20297    34331147 :   if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
   20298             :     return true;
   20299     9024017 :   tree targs = TREE_OPERAND (fn, 1);
   20300     9024017 :   if (targs == NULL_TREE)
   20301             :     return true;
   20302     9024017 :   if (targs == error_mark_node)
   20303             :     return false;
   20304     9023992 :   return fold_targs_r (targs, complain);
   20305             : }
   20306             : 
   20307             : /* Helper function for tsubst_copy_and_build CALL_EXPR and ARRAY_REF
   20308             :    handling.  */
   20309             : 
   20310             : static void
   20311    45971508 : tsubst_copy_and_build_call_args (tree t, tree args, tsubst_flags_t complain,
   20312             :                                  tree in_decl, releasing_vec &call_args)
   20313             : {
   20314    45971508 :   unsigned int nargs = call_expr_nargs (t);
   20315    78126212 :   for (unsigned int i = 0; i < nargs; ++i)
   20316             :     {
   20317    32154707 :       tree arg = CALL_EXPR_ARG (t, i);
   20318             : 
   20319    32154707 :       if (!PACK_EXPANSION_P (arg))
   20320    63357447 :         vec_safe_push (call_args,
   20321    31678725 :                        tsubst_copy_and_build (arg, args, complain, in_decl));
   20322             :       else
   20323             :         {
   20324             :           /* Expand the pack expansion and push each entry onto CALL_ARGS.  */
   20325      475982 :           arg = tsubst_pack_expansion (arg, args, complain, in_decl);
   20326      475982 :           if (TREE_CODE (arg) == TREE_VEC)
   20327             :             {
   20328      402647 :               unsigned int len, j;
   20329             : 
   20330      402647 :               len = TREE_VEC_LENGTH (arg);
   20331      691662 :               for (j = 0; j < len; ++j)
   20332             :                 {
   20333      289015 :                   tree value = TREE_VEC_ELT (arg, j);
   20334      289015 :                   if (value != NULL_TREE)
   20335      289015 :                     value = convert_from_reference (value);
   20336      289015 :                   vec_safe_push (call_args, value);
   20337             :                 }
   20338             :             }
   20339             :           else
   20340             :             /* A partial substitution.  Add one entry.  */
   20341    32154704 :             vec_safe_push (call_args, arg);
   20342             :         }
   20343             :     }
   20344    45971505 : }
   20345             : 
   20346             : /* Like tsubst but deals with expressions and performs semantic
   20347             :    analysis.  */
   20348             : 
   20349             : tree
   20350   771708711 : tsubst_copy_and_build (tree t,
   20351             :                        tree args,
   20352             :                        tsubst_flags_t complain,
   20353             :                        tree in_decl)
   20354             : {
   20355             : #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
   20356             : #define RECUR(NODE)                                             \
   20357             :   tsubst_copy_and_build (NODE, args, complain, in_decl)
   20358             : 
   20359   771708711 :   tree retval, op1;
   20360   771708711 :   location_t save_loc;
   20361             : 
   20362   771708711 :   if (t == NULL_TREE || t == error_mark_node)
   20363             :     return t;
   20364             : 
   20365   770943266 :   save_loc = input_location;
   20366   770943266 :   if (location_t eloc = cp_expr_location (t))
   20367   357855607 :     input_location = eloc;
   20368             : 
   20369             :   /* N3276 decltype magic only applies to calls at the top level or on the
   20370             :      right side of a comma.  */
   20371   770943266 :   tsubst_flags_t decltype_flag = (complain & tf_decltype);
   20372   770943266 :   complain &= ~tf_decltype;
   20373             : 
   20374   770943266 :   switch (TREE_CODE (t))
   20375             :     {
   20376      111495 :     case USING_DECL:
   20377      111495 :       t = DECL_NAME (t);
   20378             :       /* Fall through.  */
   20379      126291 :     case IDENTIFIER_NODE:
   20380      126291 :       {
   20381      126291 :         tree decl;
   20382      126291 :         cp_id_kind idk;
   20383      126291 :         const char *error_msg;
   20384             : 
   20385      126291 :         if (IDENTIFIER_CONV_OP_P (t))
   20386             :           {
   20387           0 :             tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   20388           0 :             t = make_conv_op_name (new_type);
   20389             :           }
   20390             : 
   20391             :         /* Look up the name.  */
   20392      126291 :         decl = lookup_name (t);
   20393             : 
   20394             :         /* By convention, expressions use ERROR_MARK_NODE to indicate
   20395             :            failure, not NULL_TREE.  */
   20396      126291 :         if (decl == NULL_TREE)
   20397          36 :           decl = error_mark_node;
   20398             : 
   20399      126291 :         decl = finish_id_expression (t, decl, NULL_TREE,
   20400             :                                      &idk,
   20401             :                                      /*i_c_e_p=*/false,
   20402             :                                      /*allow_i_c_e_p=*/true,
   20403             :                                      /*non_i_c_e_p=*/nullptr,
   20404             :                                      /*template_p=*/false,
   20405             :                                      /*done=*/true,
   20406             :                                      /*address_p=*/false,
   20407             :                                      /*template_arg_p=*/false,
   20408             :                                      &error_msg,
   20409             :                                      input_location);
   20410      126291 :         if (error_msg)
   20411           0 :           error (error_msg);
   20412      126291 :         if (identifier_p (decl))
   20413             :           {
   20414          36 :             if (complain & tf_error)
   20415          36 :               unqualified_name_lookup_error (decl);
   20416          36 :             decl = error_mark_node;
   20417             :           }
   20418      126291 :         RETURN (decl);
   20419             :       }
   20420             : 
   20421     7754264 :     case TEMPLATE_ID_EXPR:
   20422     7754264 :       {
   20423     7754264 :         tree object;
   20424     7754264 :         tree templ = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
   20425             :                                             complain, in_decl);
   20426     7754264 :         tree targs = TREE_OPERAND (t, 1);
   20427             : 
   20428     7754264 :         if (targs)
   20429     7754264 :           targs = tsubst_template_args (targs, args, complain, in_decl);
   20430     7754264 :         if (targs == error_mark_node)
   20431         159 :           RETURN (error_mark_node);
   20432             : 
   20433     7754105 :         if (TREE_CODE (templ) == SCOPE_REF)
   20434             :           {
   20435          45 :             tree name = TREE_OPERAND (templ, 1);
   20436          45 :             tree tid = lookup_template_function (name, targs);
   20437          45 :             TREE_OPERAND (templ, 1) = tid;
   20438          45 :             RETURN (templ);
   20439             :           }
   20440             : 
   20441     7754060 :         if (concept_definition_p (templ))
   20442             :           {
   20443       70533 :             tree check = build_concept_check (templ, targs, complain);
   20444       70533 :             if (check == error_mark_node)
   20445           0 :               RETURN (error_mark_node);
   20446             : 
   20447       70533 :             tree id = unpack_concept_check (check);
   20448             : 
   20449             :             /* If we built a function concept check, return the underlying
   20450             :                template-id. So we can evaluate it as a function call.  */
   20451       70533 :             if (function_concept_p (TREE_OPERAND (id, 0)))
   20452          27 :               RETURN (id);
   20453             : 
   20454       70506 :             RETURN (check);
   20455             :           }
   20456             : 
   20457     7683527 :         if (variable_template_p (templ))
   20458             :           {
   20459     1779515 :             tree r = lookup_and_finish_template_variable (templ, targs,
   20460             :                                                           complain);
   20461     1779515 :             r = convert_from_reference (r);
   20462     1779515 :             r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
   20463     1779515 :             RETURN (r);
   20464             :           }
   20465             : 
   20466     5904012 :         if (TREE_CODE (templ) == COMPONENT_REF)
   20467             :           {
   20468           0 :             object = TREE_OPERAND (templ, 0);
   20469           0 :             templ = TREE_OPERAND (templ, 1);
   20470             :           }
   20471             :         else
   20472             :           object = NULL_TREE;
   20473             : 
   20474     5904012 :         tree tid = lookup_template_function (templ, targs);
   20475     5904012 :         protected_set_expr_location (tid, EXPR_LOCATION (t));
   20476             : 
   20477     5904012 :         if (object)
   20478           0 :           RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
   20479             :                          object, tid, NULL_TREE));
   20480     5904012 :         else if (identifier_p (templ))
   20481             :           {
   20482             :             /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
   20483             :                name lookup found nothing when parsing the template name.  */
   20484           0 :             gcc_assert (cxx_dialect >= cxx20 || seen_error ());
   20485           0 :             RETURN (tid);
   20486             :           }
   20487             :         else
   20488     5904012 :           RETURN (baselink_for_fns (tid));
   20489             :       }
   20490             : 
   20491    15463916 :     case INDIRECT_REF:
   20492    15463916 :       {
   20493    15463916 :         tree r = RECUR (TREE_OPERAND (t, 0));
   20494             : 
   20495    15463916 :         if (REFERENCE_REF_P (t))
   20496             :           {
   20497             :             /* A type conversion to reference type will be enclosed in
   20498             :                such an indirect ref, but the substitution of the cast
   20499             :                will have also added such an indirect ref.  */
   20500     7795816 :             r = convert_from_reference (r);
   20501             :           }
   20502             :         else
   20503     7668100 :           r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
   20504             :                                     templated_operator_saved_lookups (t),
   20505             :                                     complain|decltype_flag);
   20506             : 
   20507    15463916 :         if (REF_PARENTHESIZED_P (t))
   20508         113 :           r = force_paren_expr (r);
   20509             : 
   20510    15463916 :         RETURN (r);
   20511             :       }
   20512             : 
   20513     6694260 :     case NOP_EXPR:
   20514     6694260 :       {
   20515     6694260 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   20516     6694260 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   20517     6694256 :         RETURN (build_nop (type, op0));
   20518             :       }
   20519             : 
   20520      678162 :     case IMPLICIT_CONV_EXPR:
   20521      678162 :       {
   20522      678162 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   20523      678162 :         tree expr = RECUR (TREE_OPERAND (t, 0));
   20524      678162 :         if (dependent_type_p (type) || type_dependent_expression_p (expr))
   20525             :           {
   20526          53 :             retval = copy_node (t);
   20527          53 :             TREE_TYPE (retval) = type;
   20528          53 :             TREE_OPERAND (retval, 0) = expr;
   20529          53 :             RETURN (retval);
   20530             :           }
   20531      678109 :         if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
   20532             :           /* We'll pass this to convert_nontype_argument again, we don't need
   20533             :              to actually perform any conversion here.  */
   20534         199 :           RETURN (expr);
   20535      677910 :         int flags = LOOKUP_IMPLICIT;
   20536      677910 :         if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
   20537      119507 :           flags = LOOKUP_NORMAL;
   20538      677910 :         if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
   20539        1540 :           flags |= LOOKUP_NO_NARROWING;
   20540      677910 :         RETURN (perform_implicit_conversion_flags (type, expr, complain,
   20541             :                                                   flags));
   20542             :       }
   20543             : 
   20544      929041 :     case CONVERT_EXPR:
   20545      929041 :       {
   20546      929041 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   20547      929041 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   20548      929041 :         if (op0 == error_mark_node)
   20549          29 :           RETURN (error_mark_node);
   20550      929012 :         RETURN (build1 (CONVERT_EXPR, type, op0));
   20551             :       }
   20552             : 
   20553    21995202 :     case CAST_EXPR:
   20554    21995202 :     case REINTERPRET_CAST_EXPR:
   20555    21995202 :     case CONST_CAST_EXPR:
   20556    21995202 :     case DYNAMIC_CAST_EXPR:
   20557    21995202 :     case STATIC_CAST_EXPR:
   20558    21995202 :       {
   20559    21995202 :         tree type;
   20560    21995202 :         tree op, r = NULL_TREE;
   20561             : 
   20562    21995202 :         tsubst_flags_t tcomplain = complain;
   20563    21995202 :         if (TREE_CODE (t) == CAST_EXPR)
   20564    20291133 :           tcomplain |= tf_tst_ok;
   20565    21995202 :         type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
   20566             : 
   20567    21995202 :         op = RECUR (TREE_OPERAND (t, 0));
   20568             : 
   20569    21995202 :         warning_sentinel s(warn_useless_cast);
   20570    21995202 :         warning_sentinel s2(warn_ignored_qualifiers);
   20571    21995202 :         warning_sentinel s3(warn_int_in_bool_context);
   20572    21995202 :         switch (TREE_CODE (t))
   20573             :           {
   20574    20291133 :           case CAST_EXPR:
   20575    20291133 :             r = build_functional_cast (input_location, type, op, complain);
   20576    20291133 :             break;
   20577       21699 :           case REINTERPRET_CAST_EXPR:
   20578       21699 :             r = build_reinterpret_cast (input_location, type, op, complain);
   20579       21699 :             break;
   20580       59205 :           case CONST_CAST_EXPR:
   20581       59205 :             r = build_const_cast (input_location, type, op, complain);
   20582       59205 :             break;
   20583        2489 :           case DYNAMIC_CAST_EXPR:
   20584        2489 :             r = build_dynamic_cast (input_location, type, op, complain);
   20585        2489 :             break;
   20586     1620676 :           case STATIC_CAST_EXPR:
   20587     1620676 :             r = build_static_cast (input_location, type, op, complain);
   20588     1620676 :             if (IMPLICIT_RVALUE_P (t))
   20589           0 :               set_implicit_rvalue_p (r);
   20590             :             break;
   20591           0 :           default:
   20592           0 :             gcc_unreachable ();
   20593             :           }
   20594             : 
   20595    21995199 :         RETURN (r);
   20596    21995199 :       }
   20597             : 
   20598         216 :     case BIT_CAST_EXPR:
   20599         216 :       {
   20600         216 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   20601         216 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   20602         216 :         RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain));
   20603             :       }
   20604             : 
   20605      279042 :     case POSTDECREMENT_EXPR:
   20606      279042 :     case POSTINCREMENT_EXPR:
   20607      279042 :       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   20608             :                                                 args, complain, in_decl);
   20609      279042 :       RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
   20610             :                                 templated_operator_saved_lookups (t),
   20611             :                                 complain|decltype_flag));
   20612             : 
   20613    13226684 :     case PREDECREMENT_EXPR:
   20614    13226684 :     case PREINCREMENT_EXPR:
   20615    13226684 :     case NEGATE_EXPR:
   20616    13226684 :     case BIT_NOT_EXPR:
   20617    13226684 :     case ABS_EXPR:
   20618    13226684 :     case TRUTH_NOT_EXPR:
   20619    13226684 :     case UNARY_PLUS_EXPR:  /* Unary + */
   20620    13226684 :     case REALPART_EXPR:
   20621    13226684 :     case IMAGPART_EXPR:
   20622    13226684 :       RETURN (build_x_unary_op (input_location, TREE_CODE (t),
   20623             :                                 RECUR (TREE_OPERAND (t, 0)),
   20624             :                                 templated_operator_saved_lookups (t),
   20625             :                                 complain|decltype_flag));
   20626             : 
   20627         343 :     case EXCESS_PRECISION_EXPR:
   20628         343 :       {
   20629         343 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   20630         343 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   20631         343 :         if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
   20632           0 :           RETURN (op0);
   20633         343 :         RETURN (build1_loc (EXPR_LOCATION (t), EXCESS_PRECISION_EXPR,
   20634             :                             type, op0));
   20635             :       }
   20636             : 
   20637           0 :     case FIX_TRUNC_EXPR:
   20638             :       /* convert_like should have created an IMPLICIT_CONV_EXPR.  */
   20639           0 :       gcc_unreachable ();
   20640             : 
   20641      760703 :     case ADDR_EXPR:
   20642      760703 :       op1 = TREE_OPERAND (t, 0);
   20643      760703 :       if (TREE_CODE (op1) == LABEL_DECL)
   20644          42 :         RETURN (finish_label_address_expr (DECL_NAME (op1),
   20645             :                                           EXPR_LOCATION (op1)));
   20646      760661 :       if (TREE_CODE (op1) == SCOPE_REF)
   20647       56360 :         op1 = tsubst_qualified_id (op1, args, complain, in_decl,
   20648             :                                    /*done=*/true, /*address_p=*/true);
   20649             :       else
   20650      704301 :         op1 = tsubst_non_call_postfix_expression (op1, args, complain,
   20651             :                                                   in_decl);
   20652      760661 :       RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
   20653             :                                 templated_operator_saved_lookups (t),
   20654             :                                 complain|decltype_flag));
   20655             : 
   20656    45680414 :     case PLUS_EXPR:
   20657    45680414 :     case MINUS_EXPR:
   20658    45680414 :     case MULT_EXPR:
   20659    45680414 :     case TRUNC_DIV_EXPR:
   20660    45680414 :     case CEIL_DIV_EXPR:
   20661    45680414 :     case FLOOR_DIV_EXPR:
   20662    45680414 :     case ROUND_DIV_EXPR:
   20663    45680414 :     case EXACT_DIV_EXPR:
   20664    45680414 :     case BIT_AND_EXPR:
   20665    45680414 :     case BIT_IOR_EXPR:
   20666    45680414 :     case BIT_XOR_EXPR:
   20667    45680414 :     case TRUNC_MOD_EXPR:
   20668    45680414 :     case FLOOR_MOD_EXPR:
   20669    45680414 :     case TRUTH_ANDIF_EXPR:
   20670    45680414 :     case TRUTH_ORIF_EXPR:
   20671    45680414 :     case TRUTH_AND_EXPR:
   20672    45680414 :     case TRUTH_OR_EXPR:
   20673    45680414 :     case RSHIFT_EXPR:
   20674    45680414 :     case LSHIFT_EXPR:
   20675    45680414 :     case EQ_EXPR:
   20676    45680414 :     case NE_EXPR:
   20677    45680414 :     case MAX_EXPR:
   20678    45680414 :     case MIN_EXPR:
   20679    45680414 :     case LE_EXPR:
   20680    45680414 :     case GE_EXPR:
   20681    45680414 :     case LT_EXPR:
   20682    45680414 :     case GT_EXPR:
   20683    45680414 :     case SPACESHIP_EXPR:
   20684    45680414 :     case MEMBER_REF:
   20685    45680414 :     case DOTSTAR_EXPR:
   20686    45680414 :       {
   20687             :         /* If either OP0 or OP1 was value- or type-dependent, suppress
   20688             :            warnings that depend on the range of the types involved.  */
   20689    45680414 :         tree op0 = TREE_OPERAND (t, 0);
   20690    45680414 :         tree op1 = TREE_OPERAND (t, 1);
   20691    45680414 :         const bool was_dep = (dependent_operand_p (op0)
   20692    45680414 :                               || dependent_operand_p (op1));
   20693    45680414 :         op0 = RECUR (op0);
   20694    45680414 :         op1 = RECUR (op1);
   20695             : 
   20696    45680354 :         warning_sentinel s1(warn_type_limits, was_dep);
   20697    45680354 :         warning_sentinel s2(warn_div_by_zero, was_dep);
   20698    45680354 :         warning_sentinel s3(warn_logical_op, was_dep);
   20699    45680354 :         warning_sentinel s4(warn_tautological_compare, was_dep);
   20700    45680354 :         warning_sentinel s5(warn_address, was_dep);
   20701             : 
   20702    45680354 :         tree r = build_x_binary_op
   20703   174533440 :           (input_location, TREE_CODE (t),
   20704             :            op0,
   20705    45680354 :            (warning_suppressed_p (TREE_OPERAND (t, 0))
   20706             :             ? ERROR_MARK
   20707    37492378 :             : TREE_CODE (TREE_OPERAND (t, 0))),
   20708             :            op1,
   20709    45680354 :            (warning_suppressed_p (TREE_OPERAND (t, 1))
   20710             :             ? ERROR_MARK
   20711    37182052 :             : TREE_CODE (TREE_OPERAND (t, 1))),
   20712             :            templated_operator_saved_lookups (t),
   20713             :            /*overload=*/NULL,
   20714             :            complain|decltype_flag);
   20715    45680324 :         if (EXPR_P (r))
   20716    45676401 :           copy_warning (r, t);
   20717             : 
   20718    45680324 :         RETURN (r);
   20719    45680324 :       }
   20720             : 
   20721           6 :     case POINTER_PLUS_EXPR:
   20722           6 :       {
   20723           6 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   20724           6 :         if (op0 == error_mark_node)
   20725           0 :           RETURN (error_mark_node);
   20726           6 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   20727           6 :         if (op1 == error_mark_node)
   20728           0 :           RETURN (error_mark_node);
   20729           6 :         RETURN (fold_build_pointer_plus (op0, op1));
   20730             :       }
   20731             : 
   20732    40270051 :     case SCOPE_REF:
   20733    40270051 :       RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
   20734             :                                   /*address_p=*/false));
   20735             : 
   20736     8050587 :     case BASELINK:
   20737     8050587 :       RETURN (tsubst_baselink (t, current_nonlambda_class_type (),
   20738             :                                args, complain, in_decl));
   20739             : 
   20740     1236726 :     case ARRAY_REF:
   20741     1236726 :       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   20742             :                                                 args, complain, in_decl);
   20743     1236726 :       if (TREE_CODE (TREE_OPERAND (t, 1)) == CALL_EXPR
   20744     1236726 :           && (CALL_EXPR_FN (TREE_OPERAND (t, 1))
   20745        1414 :               == ovl_op_identifier (ARRAY_REF)))
   20746             :         {
   20747          23 :           tree c = TREE_OPERAND (t, 1);
   20748          23 :           releasing_vec index_exp_list;
   20749          23 :           tsubst_copy_and_build_call_args (c, args, complain, in_decl,
   20750             :                                            index_exp_list);
   20751             : 
   20752          23 :           tree r;
   20753          23 :           if (vec_safe_length (index_exp_list) == 1
   20754          26 :               && !PACK_EXPANSION_P (index_exp_list[0]))
   20755           3 :             r = grok_array_decl (EXPR_LOCATION (t), op1,
   20756           3 :                                  index_exp_list[0], NULL,
   20757             :                                  complain | decltype_flag);
   20758             :           else
   20759          20 :             r = grok_array_decl (EXPR_LOCATION (t), op1,
   20760             :                                  NULL_TREE, &index_exp_list,
   20761             :                                  complain | decltype_flag);
   20762          23 :           RETURN (r);
   20763          23 :         }
   20764     1236703 :       RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
   20765             :                                  RECUR (TREE_OPERAND (t, 1)),
   20766             :                                  complain|decltype_flag));
   20767             : 
   20768     4007321 :     case SIZEOF_EXPR:
   20769     7341414 :       if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
   20770     7340609 :           || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
   20771      674045 :         RETURN (tsubst_copy (t, args, complain, in_decl));
   20772             :       /* Fall through */
   20773             : 
   20774     3884651 :     case ALIGNOF_EXPR:
   20775     3884651 :       {
   20776     3884651 :         tree r;
   20777             : 
   20778     3884651 :         op1 = TREE_OPERAND (t, 0);
   20779     3884651 :         if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
   20780           0 :           op1 = TREE_TYPE (op1);
   20781     3884651 :         bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
   20782     3884651 :                             && ALIGNOF_EXPR_STD_P (t));
   20783     3884651 :         if (!args)
   20784             :           {
   20785             :             /* When there are no ARGS, we are trying to evaluate a
   20786             :                non-dependent expression from the parser.  Trying to do
   20787             :                the substitutions may not work.  */
   20788      558064 :             if (!TYPE_P (op1))
   20789       65229 :               op1 = TREE_TYPE (op1);
   20790             :           }
   20791             :         else
   20792             :           {
   20793     3326587 :             ++cp_unevaluated_operand;
   20794     3326587 :             ++c_inhibit_evaluation_warnings;
   20795     3326587 :             if (TYPE_P (op1))
   20796     3235488 :               op1 = tsubst (op1, args, complain, in_decl);
   20797             :             else
   20798       91099 :               op1 = tsubst_copy_and_build (op1, args, complain, in_decl);
   20799     3326587 :             --cp_unevaluated_operand;
   20800     3326587 :             --c_inhibit_evaluation_warnings;
   20801             :           }
   20802     3884651 :         if (TYPE_P (op1))
   20803     3793543 :           r = cxx_sizeof_or_alignof_type (input_location,
   20804     3793543 :                                           op1, TREE_CODE (t), std_alignof,
   20805             :                                           complain & tf_error);
   20806             :         else
   20807       91108 :           r = cxx_sizeof_or_alignof_expr (input_location,
   20808       91108 :                                           op1, TREE_CODE (t), std_alignof,
   20809             :                                           complain & tf_error);
   20810     3884651 :         if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
   20811             :           {
   20812     3330366 :             if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
   20813             :               {
   20814     3330243 :                 if (!processing_template_decl && TYPE_P (op1))
   20815             :                   {
   20816     3081328 :                     r = build_min (SIZEOF_EXPR, size_type_node,
   20817             :                                    build1 (NOP_EXPR, op1, error_mark_node));
   20818     3081328 :                     SIZEOF_EXPR_TYPE_P (r) = 1;
   20819             :                   }
   20820             :                 else
   20821      248915 :                   r = build_min (SIZEOF_EXPR, size_type_node, op1);
   20822     3330243 :                 TREE_SIDE_EFFECTS (r) = 0;
   20823     3330243 :                 TREE_READONLY (r) = 1;
   20824             :               }
   20825     3330366 :             SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
   20826             :           }
   20827     3884651 :         RETURN (r);
   20828             :       }
   20829             : 
   20830           0 :     case AT_ENCODE_EXPR:
   20831           0 :       {
   20832           0 :         op1 = TREE_OPERAND (t, 0);
   20833           0 :         ++cp_unevaluated_operand;
   20834           0 :         ++c_inhibit_evaluation_warnings;
   20835           0 :         op1 = tsubst_copy_and_build (op1, args, complain, in_decl);
   20836           0 :         --cp_unevaluated_operand;
   20837           0 :         --c_inhibit_evaluation_warnings;
   20838           0 :         RETURN (objc_build_encode_expr (op1));
   20839             :       }
   20840             : 
   20841      184364 :     case NOEXCEPT_EXPR:
   20842      184364 :       op1 = TREE_OPERAND (t, 0);
   20843      184364 :       ++cp_unevaluated_operand;
   20844      184364 :       ++c_inhibit_evaluation_warnings;
   20845      184364 :       ++cp_noexcept_operand;
   20846      184364 :       op1 = tsubst_copy_and_build (op1, args, complain, in_decl);
   20847      184364 :       --cp_unevaluated_operand;
   20848      184364 :       --c_inhibit_evaluation_warnings;
   20849      184364 :       --cp_noexcept_operand;
   20850      184364 :       RETURN (finish_noexcept_expr (op1, complain));
   20851             : 
   20852     8383203 :     case MODOP_EXPR:
   20853     8383203 :       {
   20854     8383203 :         warning_sentinel s(warn_div_by_zero);
   20855     8383203 :         tree lhs = RECUR (TREE_OPERAND (t, 0));
   20856     8383203 :         tree rhs = RECUR (TREE_OPERAND (t, 2));
   20857             : 
   20858     8383203 :         tree r = build_x_modify_expr
   20859     8383203 :           (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
   20860             :            templated_operator_saved_lookups (t),
   20861             :            complain|decltype_flag);
   20862             :         /* TREE_NO_WARNING must be set if either the expression was
   20863             :            parenthesized or it uses an operator such as >>= rather
   20864             :            than plain assignment.  In the former case, it was already
   20865             :            set and must be copied.  In the latter case,
   20866             :            build_x_modify_expr sets it and it must not be reset
   20867             :            here.  */
   20868     8383203 :         if (warning_suppressed_p (t, OPT_Wparentheses))
   20869         400 :           suppress_warning (r, OPT_Wparentheses);
   20870             : 
   20871     8383203 :         RETURN (r);
   20872     8383203 :       }
   20873             : 
   20874     1944158 :     case ARROW_EXPR:
   20875     1944158 :       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   20876             :                                                 args, complain, in_decl);
   20877             :       /* Remember that there was a reference to this entity.  */
   20878     1944158 :       if (DECL_P (op1)
   20879     1944158 :           && !mark_used (op1, complain) && !(complain & tf_error))
   20880           0 :         RETURN (error_mark_node);
   20881     1944158 :       RETURN (build_x_arrow (input_location, op1, complain));
   20882             : 
   20883      105862 :     case NEW_EXPR:
   20884      105862 :       {
   20885      105862 :         tree placement = RECUR (TREE_OPERAND (t, 0));
   20886      105862 :         tree init = RECUR (TREE_OPERAND (t, 3));
   20887      105862 :         vec<tree, va_gc> *placement_vec;
   20888      105862 :         vec<tree, va_gc> *init_vec;
   20889      105862 :         tree ret;
   20890      105862 :         location_t loc = EXPR_LOCATION (t);
   20891             : 
   20892      105862 :         if (placement == NULL_TREE)
   20893       10112 :           placement_vec = NULL;
   20894       95750 :         else if (placement == error_mark_node)
   20895           4 :           RETURN (error_mark_node);
   20896             :         else
   20897             :           {
   20898       95746 :             placement_vec = make_tree_vector ();
   20899      191492 :             for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
   20900       95746 :               vec_safe_push (placement_vec, TREE_VALUE (placement));
   20901             :           }
   20902             : 
   20903             :         /* If there was an initializer in the original tree, but it
   20904             :            instantiated to an empty list, then we should pass a
   20905             :            non-NULL empty vector to tell build_new that it was an
   20906             :            empty initializer() rather than no initializer.  This can
   20907             :            only happen when the initializer is a pack expansion whose
   20908             :            parameter packs are of length zero.  */
   20909      105858 :         if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
   20910        9066 :           init_vec = NULL;
   20911       96792 :         else if (init == error_mark_node)
   20912           6 :           RETURN (error_mark_node);
   20913             :         else
   20914             :           {
   20915       96786 :             init_vec = make_tree_vector ();
   20916       96786 :             if (init == void_node)
   20917         599 :               gcc_assert (init_vec != NULL);
   20918             :             else
   20919             :               {
   20920      180591 :                 for (; init != NULL_TREE; init = TREE_CHAIN (init))
   20921       84404 :                   vec_safe_push (init_vec, TREE_VALUE (init));
   20922             :               }
   20923             :           }
   20924             : 
   20925             :         /* Avoid passing an enclosing decl to valid_array_size_p.  */
   20926      105852 :         in_decl = NULL_TREE;
   20927             : 
   20928      105852 :         tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
   20929      105852 :         tree op2 = RECUR (TREE_OPERAND (t, 2));
   20930      105852 :         ret = build_new (loc, &placement_vec, op1, op2,
   20931      105852 :                          &init_vec, NEW_EXPR_USE_GLOBAL (t),
   20932             :                          complain);
   20933             : 
   20934      105852 :         if (placement_vec != NULL)
   20935      105611 :           release_tree_vector (placement_vec);
   20936      105852 :         if (init_vec != NULL)
   20937       81477 :           release_tree_vector (init_vec);
   20938             : 
   20939      105862 :         RETURN (ret);
   20940             :       }
   20941             : 
   20942       32060 :     case DELETE_EXPR:
   20943       32060 :       {
   20944       32060 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   20945       32060 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   20946       32060 :         RETURN (delete_sanity (input_location, op0, op1,
   20947             :                                DELETE_EXPR_USE_VEC (t),
   20948             :                                DELETE_EXPR_USE_GLOBAL (t),
   20949             :                                complain));
   20950             :       }
   20951             : 
   20952       14836 :     case COMPOUND_EXPR:
   20953       14836 :       {
   20954       14836 :         tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
   20955             :                                           complain & ~tf_decltype, in_decl);
   20956       14836 :         RETURN (build_x_compound_expr (EXPR_LOCATION (t),
   20957             :                                        op0,
   20958             :                                        RECUR (TREE_OPERAND (t, 1)),
   20959             :                                        templated_operator_saved_lookups (t),
   20960             :                                        complain|decltype_flag));
   20961             :       }
   20962             : 
   20963    45976183 :     case CALL_EXPR:
   20964    45976183 :       {
   20965    45976183 :         tree function;
   20966    45976183 :         unsigned int nargs;
   20967    45976183 :         bool qualified_p;
   20968    45976183 :         bool koenig_p;
   20969    45976183 :         tree ret;
   20970             : 
   20971    45976183 :         function = CALL_EXPR_FN (t);
   20972             :         /* Internal function with no arguments.  */
   20973    45976183 :         if (function == NULL_TREE && call_expr_nargs (t) == 0)
   20974        4698 :           RETURN (t);
   20975             : 
   20976             :         /* When we parsed the expression, we determined whether or
   20977             :            not Koenig lookup should be performed.  */
   20978    45971485 :         koenig_p = KOENIG_LOOKUP_P (t);
   20979    45971485 :         if (function == NULL_TREE)
   20980             :           {
   20981             :             koenig_p = false;
   20982             :             qualified_p = false;
   20983             :           }
   20984    45971323 :         else if (TREE_CODE (function) == SCOPE_REF)
   20985             :           {
   20986     9625926 :             qualified_p = true;
   20987     9625926 :             function = tsubst_qualified_id (function, args, complain, in_decl,
   20988             :                                             /*done=*/false,
   20989             :                                             /*address_p=*/false);
   20990             :           }
   20991    36345397 :         else if (koenig_p
   20992    36345397 :                  && (identifier_p (function)
   20993     3736177 :                      || (TREE_CODE (function) == TEMPLATE_ID_EXPR
   20994      433770 :                          && identifier_p (TREE_OPERAND (function, 0)))))
   20995             :           {
   20996             :             /* Do nothing; calling tsubst_copy_and_build on an identifier
   20997             :                would incorrectly perform unqualified lookup again.
   20998             : 
   20999             :                Note that we can also have an IDENTIFIER_NODE if the earlier
   21000             :                unqualified lookup found a dependent local extern declaration
   21001             :                (as per finish_call_expr); in that case koenig_p will be false
   21002             :                and we do want to do the lookup again to find the substituted
   21003             :                declaration.  */
   21004       91513 :             qualified_p = false;
   21005             : 
   21006       91513 :             if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
   21007             :               /* Use tsubst_copy to substitute through the template arguments
   21008             :                  of the template-id without performing unqualified lookup of
   21009             :                  the template name.  */
   21010         908 :               function = tsubst_copy (function, args, complain, in_decl);
   21011             :           }
   21012             :         else
   21013             :           {
   21014    36253884 :             if (TREE_CODE (function) == COMPONENT_REF)
   21015             :               {
   21016     6194722 :                 tree op = TREE_OPERAND (function, 1);
   21017             : 
   21018     6194722 :                 qualified_p = (TREE_CODE (op) == SCOPE_REF
   21019     6194722 :                                || (BASELINK_P (op)
   21020     4298856 :                                    && BASELINK_QUALIFIED_P (op)));
   21021             :               }
   21022             :             else
   21023             :               qualified_p = false;
   21024             : 
   21025    36253884 :             if (TREE_CODE (function) == ADDR_EXPR
   21026    36253884 :                 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
   21027             :               /* Avoid error about taking the address of a constructor.  */
   21028          37 :               function = TREE_OPERAND (function, 0);
   21029             : 
   21030    36253884 :             tsubst_flags_t subcomplain = complain;
   21031    36253884 :             if (koenig_p && TREE_CODE (function) == FUNCTION_DECL)
   21032             :               /* When KOENIG_P, we don't want to mark_used the callee before
   21033             :                  augmenting the overload set via ADL, so during this initial
   21034             :                  substitution we disable mark_used by setting tf_conv (68942).  */
   21035     1208292 :               subcomplain |= tf_conv;
   21036    36253884 :             function = tsubst_copy_and_build (function, args, subcomplain, in_decl);
   21037             : 
   21038    36253884 :             if (BASELINK_P (function))
   21039     8043565 :               qualified_p = true;
   21040             :           }
   21041             : 
   21042    45971485 :         nargs = call_expr_nargs (t);
   21043    45971485 :         releasing_vec call_args;
   21044    45971485 :         tsubst_copy_and_build_call_args (t, args, complain, in_decl,
   21045             :                                          call_args);
   21046             : 
   21047             :         /* Stripped-down processing for a call in a thunk.  Specifically, in
   21048             :            the thunk template for a generic lambda.  */
   21049    45971482 :         if (call_from_lambda_thunk_p (t))
   21050             :           {
   21051             :             /* Now that we've expanded any packs, the number of call args
   21052             :                might be different.  */
   21053         103 :             unsigned int cargs = call_args->length ();
   21054         103 :             tree thisarg = NULL_TREE;
   21055         103 :             if (TREE_CODE (function) == COMPONENT_REF)
   21056             :               {
   21057         103 :                 thisarg = TREE_OPERAND (function, 0);
   21058         103 :                 if (TREE_CODE (thisarg) == INDIRECT_REF)
   21059         103 :                   thisarg = TREE_OPERAND (thisarg, 0);
   21060         103 :                 function = TREE_OPERAND (function, 1);
   21061         103 :                 if (TREE_CODE (function) == BASELINK)
   21062         103 :                   function = BASELINK_FUNCTIONS (function);
   21063             :               }
   21064             :             /* We aren't going to do normal overload resolution, so force the
   21065             :                template-id to resolve.  */
   21066         103 :             function = resolve_nondeduced_context (function, complain);
   21067         323 :             for (unsigned i = 0; i < cargs; ++i)
   21068             :               {
   21069             :                 /* In a thunk, pass through args directly, without any
   21070             :                    conversions.  */
   21071         220 :                 tree arg = (*call_args)[i];
   21072         220 :                 while (TREE_CODE (arg) != PARM_DECL)
   21073           0 :                   arg = TREE_OPERAND (arg, 0);
   21074         220 :                 (*call_args)[i] = arg;
   21075             :               }
   21076         103 :             if (thisarg)
   21077             :               {
   21078             :                 /* If there are no other args, just push 'this'.  */
   21079         103 :                 if (cargs == 0)
   21080           9 :                   vec_safe_push (call_args, thisarg);
   21081             :                 else
   21082             :                   {
   21083             :                     /* Otherwise, shift the other args over to make room.  */
   21084          94 :                     tree last = (*call_args)[cargs - 1];
   21085          94 :                     vec_safe_push (call_args, last);
   21086         220 :                     for (int i = cargs - 1; i > 0; --i)
   21087         126 :                       (*call_args)[i] = (*call_args)[i - 1];
   21088          94 :                     (*call_args)[0] = thisarg;
   21089             :                   }
   21090             :               }
   21091         103 :             ret = build_call_a (function, call_args->length (),
   21092             :                                 call_args->address ());
   21093             :             /* The thunk location is not interesting.  */
   21094         103 :             SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
   21095         103 :             CALL_FROM_THUNK_P (ret) = true;
   21096         103 :             if (CLASS_TYPE_P (TREE_TYPE (ret)))
   21097           9 :               CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
   21098             : 
   21099         103 :             RETURN (ret);
   21100             :           }
   21101             : 
   21102             :         /* We do not perform argument-dependent lookup if normal
   21103             :            lookup finds a non-function, in accordance with the
   21104             :            resolution of DR 218.  */
   21105    45971379 :         if (koenig_p
   21106     3826779 :             && ((is_overloaded_fn (function)
   21107             :                  /* If lookup found a member function, the Koenig lookup is
   21108             :                     not appropriate, even if an unqualified-name was used
   21109             :                     to denote the function.  */
   21110     3735266 :                  && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
   21111     3827687 :                 || identifier_p (function)
   21112             :                 /* C++20 P0846: Lookup found nothing.  */
   21113         908 :                 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
   21114         908 :                     && identifier_p (TREE_OPERAND (function, 0))))
   21115             :             /* Only do this when substitution turns a dependent call
   21116             :                into a non-dependent call.  */
   21117     3826779 :             && type_dependent_expression_p_push (t)
   21118    48853317 :             && !any_type_dependent_arguments_p (call_args))
   21119     2810820 :           function = perform_koenig_lookup (function, call_args, tf_none);
   21120             : 
   21121    45971379 :         if (function != NULL_TREE
   21122       84731 :             && (identifier_p (function)
   21123    45886488 :                 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
   21124     5897991 :                     && identifier_p (TREE_OPERAND (function, 0))
   21125           3 :                     && !any_dependent_template_arguments_p (TREE_OPERAND
   21126             :                                                             (function, 1))))
   21127    46056110 :             && !any_type_dependent_arguments_p (call_args))
   21128             :           {
   21129       14351 :             bool template_id_p = (TREE_CODE (function) == TEMPLATE_ID_EXPR);
   21130       14351 :             if (template_id_p)
   21131           2 :               function = TREE_OPERAND (function, 0);
   21132       14351 :             if (koenig_p && (complain & tf_warning_or_error))
   21133             :               {
   21134             :                 /* For backwards compatibility and good diagnostics, try
   21135             :                    the unqualified lookup again if we aren't in SFINAE
   21136             :                    context.  */
   21137          42 :                 tree unq = tsubst_copy_and_build (function, args,
   21138             :                                                   complain, in_decl);
   21139          42 :                 if (unq == error_mark_node)
   21140          15 :                   RETURN (error_mark_node);
   21141             : 
   21142          27 :                 if (unq != function)
   21143             :                   {
   21144          27 :                     char const *const msg
   21145             :                       = G_("%qD was not declared in this scope, "
   21146             :                            "and no declarations were found by "
   21147             :                            "argument-dependent lookup at the point "
   21148             :                            "of instantiation");
   21149             : 
   21150          27 :                     bool in_lambda = (current_class_type
   21151          43 :                                       && LAMBDA_TYPE_P (current_class_type));
   21152             :                     /* In a lambda fn, we have to be careful to not
   21153             :                        introduce new this captures.  Legacy code can't
   21154             :                        be using lambdas anyway, so it's ok to be
   21155             :                        stricter.  Be strict with C++20 template-id ADL too.
   21156             :                        And be strict if we're already failing anyway.  */
   21157          27 :                     bool strict = in_lambda || template_id_p || seen_error();
   21158          27 :                     bool diag = true;
   21159          27 :                     if (strict)
   21160          15 :                       error_at (cp_expr_loc_or_input_loc (t),
   21161             :                                 msg, function);
   21162             :                     else
   21163          12 :                       diag = permerror (cp_expr_loc_or_input_loc (t),
   21164             :                                         msg, function);
   21165          27 :                     if (diag)
   21166             :                       {
   21167          27 :                         tree fn = unq;
   21168             : 
   21169          27 :                         if (INDIRECT_REF_P (fn))
   21170           4 :                           fn = TREE_OPERAND (fn, 0);
   21171          27 :                         if (is_overloaded_fn (fn))
   21172          23 :                           fn = get_first_fn (fn);
   21173             : 
   21174          27 :                         if (!DECL_P (fn))
   21175             :                           /* Can't say anything more.  */;
   21176          23 :                         else if (DECL_CLASS_SCOPE_P (fn))
   21177             :                           {
   21178          12 :                             location_t loc = cp_expr_loc_or_input_loc (t);
   21179          12 :                             inform (loc,
   21180             :                                     "declarations in dependent base %qT are "
   21181             :                                     "not found by unqualified lookup",
   21182          12 :                                     DECL_CLASS_CONTEXT (fn));
   21183          12 :                             if (current_class_ptr)
   21184           8 :                               inform (loc,
   21185             :                                       "use %<this->%D%> instead", function);
   21186             :                             else
   21187           4 :                               inform (loc,
   21188             :                                       "use %<%T::%D%> instead",
   21189             :                                       current_class_name, function);
   21190             :                           }
   21191             :                         else
   21192          11 :                           inform (DECL_SOURCE_LOCATION (fn),
   21193             :                                   "%qD declared here, later in the "
   21194             :                                   "translation unit", fn);
   21195          27 :                         if (strict)
   21196          15 :                           RETURN (error_mark_node);
   21197             :                       }
   21198             : 
   21199             :                     function = unq;
   21200             :                   }
   21201             :               }
   21202       14321 :             if (identifier_p (function))
   21203             :               {
   21204       14309 :                 if (complain & tf_error)
   21205           0 :                   unqualified_name_lookup_error (function);
   21206       14309 :                 RETURN (error_mark_node);
   21207             :               }
   21208             :           }
   21209             : 
   21210             :         /* Remember that there was a reference to this entity.  */
   21211    45957040 :         if (function != NULL_TREE
   21212    45956878 :             && DECL_P (function)
   21213    52156967 :             && !mark_used (function, complain) && !(complain & tf_error))
   21214           3 :           RETURN (error_mark_node);
   21215             : 
   21216    45957037 :         if (!maybe_fold_fn_template_args (function, complain))
   21217        8969 :           return error_mark_node;
   21218             : 
   21219             :         /* Put back tf_decltype for the actual call.  */
   21220    45948068 :         complain |= decltype_flag;
   21221             : 
   21222    45948068 :         if (function == NULL_TREE)
   21223         162 :           switch (CALL_EXPR_IFN (t))
   21224             :             {
   21225          45 :             case IFN_LAUNDER:
   21226          45 :               gcc_assert (nargs == 1);
   21227          45 :               if (vec_safe_length (call_args) != 1)
   21228             :                 {
   21229           6 :                   error_at (cp_expr_loc_or_input_loc (t),
   21230             :                             "wrong number of arguments to "
   21231             :                             "%<__builtin_launder%>");
   21232           6 :                   ret = error_mark_node;
   21233             :                 }
   21234             :               else
   21235          39 :                 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
   21236          39 :                                               (*call_args)[0], complain);
   21237             :               break;
   21238             : 
   21239          36 :             case IFN_VEC_CONVERT:
   21240          36 :               gcc_assert (nargs == 1);
   21241          36 :               if (vec_safe_length (call_args) != 1)
   21242             :                 {
   21243           0 :                   error_at (cp_expr_loc_or_input_loc (t),
   21244             :                             "wrong number of arguments to "
   21245             :                             "%<__builtin_convertvector%>");
   21246           0 :                   ret = error_mark_node;
   21247           0 :                   break;
   21248             :                 }
   21249          36 :               ret = cp_build_vec_convert ((*call_args)[0], input_location,
   21250          36 :                                           tsubst (TREE_TYPE (t), args,
   21251             :                                                   complain, in_decl),
   21252             :                                           complain);
   21253          36 :               if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
   21254           4 :                 RETURN (ret);
   21255             :               break;
   21256             : 
   21257          16 :             case IFN_SHUFFLEVECTOR:
   21258          16 :               {
   21259          16 :                 ret = build_x_shufflevector (input_location, call_args,
   21260             :                                              complain);
   21261          16 :                 if (ret != error_mark_node)
   21262          12 :                   RETURN (ret);
   21263             :                 break;
   21264             :               }
   21265             : 
   21266          65 :             case IFN_ASSUME:
   21267          65 :               gcc_assert (nargs == 1);
   21268          65 :               if (vec_safe_length (call_args) != 1)
   21269             :                 {
   21270           0 :                   error_at (cp_expr_loc_or_input_loc (t),
   21271             :                             "wrong number of arguments to "
   21272             :                             "%<assume%> attribute");
   21273           0 :                   ret = error_mark_node;
   21274             :                 }
   21275             :               else
   21276             :                 {
   21277          65 :                   tree &arg = (*call_args)[0];
   21278          65 :                   if (!type_dependent_expression_p (arg))
   21279          65 :                     arg = contextual_conv_bool (arg, tf_warning_or_error);
   21280          65 :                   if (error_operand_p (arg))
   21281             :                     {
   21282           6 :                       ret = error_mark_node;
   21283           6 :                       break;
   21284             :                     }
   21285          59 :                   ret = build_assume_call (EXPR_LOCATION (t), arg);
   21286          59 :                   RETURN (ret);
   21287             :                 }
   21288           0 :               break;
   21289             : 
   21290           0 :             default:
   21291             :               /* Unsupported internal function with arguments.  */
   21292           0 :               gcc_unreachable ();
   21293             :             }
   21294    45947906 :         else if (TREE_CODE (function) == OFFSET_REF
   21295    45885256 :                  || TREE_CODE (function) == DOTSTAR_EXPR
   21296    45871315 :                  || TREE_CODE (function) == MEMBER_REF)
   21297       76591 :           ret = build_offset_ref_call_from_tree (function, &call_args,
   21298             :                                                  complain);
   21299    45871315 :         else if (TREE_CODE (function) == COMPONENT_REF)
   21300             :           {
   21301     6232503 :             tree instance = TREE_OPERAND (function, 0);
   21302     6232503 :             tree fn = TREE_OPERAND (function, 1);
   21303             : 
   21304     6232503 :             if (processing_template_decl
   21305     6232503 :                 && (type_dependent_expression_p (instance)
   21306       70190 :                     || (!BASELINK_P (fn)
   21307         162 :                         && TREE_CODE (fn) != FIELD_DECL)
   21308       70031 :                     || type_dependent_expression_p (fn)
   21309       70031 :                     || any_type_dependent_arguments_p (call_args)))
   21310      440960 :               ret = build_min_nt_call_vec (function, call_args);
   21311     5791543 :             else if (!BASELINK_P (fn))
   21312       17901 :               ret = finish_call_expr (function, &call_args,
   21313             :                                        /*disallow_virtual=*/false,
   21314             :                                        /*koenig_p=*/false,
   21315             :                                        complain);
   21316             :             else
   21317     5773642 :               ret = (build_new_method_call
   21318    11525209 :                       (instance, fn,
   21319             :                        &call_args, NULL_TREE,
   21320             :                        qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
   21321             :                        /*fn_p=*/NULL,
   21322             :                        complain));
   21323             :           }
   21324    39638812 :         else if (concept_check_p (function))
   21325             :           {
   21326             :             /* FUNCTION is a template-id referring to a concept definition.  */
   21327          27 :             tree id = unpack_concept_check (function);
   21328          27 :             tree tmpl = TREE_OPERAND (id, 0);
   21329          27 :             tree args = TREE_OPERAND (id, 1);
   21330             : 
   21331             :             /* Calls to standard and variable concepts should have been
   21332             :                previously diagnosed.  */
   21333          27 :             gcc_assert (function_concept_p (tmpl));
   21334             : 
   21335             :             /* Ensure the result is wrapped as a call expression.  */
   21336          27 :             ret = build_concept_check (tmpl, args, tf_warning_or_error);
   21337             :           }
   21338             :         else
   21339    39638785 :           ret = finish_call_expr (function, &call_args,
   21340             :                                   /*disallow_virtual=*/qualified_p,
   21341             :                                   koenig_p,
   21342             :                                   complain);
   21343             : 
   21344    45936289 :         if (ret != error_mark_node)
   21345             :           {
   21346    45912568 :             bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
   21347    45912568 :             bool ord = CALL_EXPR_ORDERED_ARGS (t);
   21348    45912568 :             bool rev = CALL_EXPR_REVERSE_ARGS (t);
   21349    45912568 :             if (op || ord || rev)
   21350      489438 :               if (tree call = extract_call_expr (ret))
   21351             :                 {
   21352      489432 :                   CALL_EXPR_OPERATOR_SYNTAX (call) = op;
   21353      489432 :                   CALL_EXPR_ORDERED_ARGS (call) = ord;
   21354      489432 :                   CALL_EXPR_REVERSE_ARGS (call) = rev;
   21355             :                 }
   21356    45912568 :             if (warning_suppressed_p (t, OPT_Wpessimizing_move))
   21357             :               /* This also suppresses -Wredundant-move.  */
   21358    33775662 :               suppress_warning (ret, OPT_Wpessimizing_move);
   21359             :           }
   21360             : 
   21361    45950809 :         RETURN (ret);
   21362    45964476 :       }
   21363             : 
   21364      812545 :     case COND_EXPR:
   21365      812545 :       {
   21366      812545 :         tree cond = RECUR (TREE_OPERAND (t, 0));
   21367      812545 :         cond = mark_rvalue_use (cond);
   21368      812545 :         tree folded_cond = fold_non_dependent_expr (cond, complain);
   21369      812545 :         tree exp1, exp2;
   21370             : 
   21371      812545 :         if (TREE_CODE (folded_cond) == INTEGER_CST)
   21372             :           {
   21373      486933 :             if (integer_zerop (folded_cond))
   21374             :               {
   21375      301506 :                 ++c_inhibit_evaluation_warnings;
   21376      301506 :                 exp1 = RECUR (TREE_OPERAND (t, 1));
   21377      301506 :                 --c_inhibit_evaluation_warnings;
   21378      301506 :                 exp2 = RECUR (TREE_OPERAND (t, 2));
   21379             :               }
   21380             :             else
   21381             :               {
   21382      185427 :                 exp1 = RECUR (TREE_OPERAND (t, 1));
   21383      185427 :                 ++c_inhibit_evaluation_warnings;
   21384      185427 :                 exp2 = RECUR (TREE_OPERAND (t, 2));
   21385      185427 :                 --c_inhibit_evaluation_warnings;
   21386             :               }
   21387             :             cond = folded_cond;
   21388             :           }
   21389             :         else
   21390             :           {
   21391      325612 :             exp1 = RECUR (TREE_OPERAND (t, 1));
   21392      325612 :             exp2 = RECUR (TREE_OPERAND (t, 2));
   21393             :           }
   21394             : 
   21395      812545 :         warning_sentinel s(warn_duplicated_branches);
   21396      812545 :         RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
   21397             :                                          cond, exp1, exp2, complain));
   21398      812545 :       }
   21399             : 
   21400          24 :     case PSEUDO_DTOR_EXPR:
   21401          24 :       {
   21402          24 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21403          24 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   21404          24 :         tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
   21405          24 :         RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
   21406             :                                                input_location));
   21407             :       }
   21408             : 
   21409    21636906 :     case TREE_LIST:
   21410    21636906 :       RETURN (tsubst_tree_list (t, args, complain, in_decl));
   21411             : 
   21412    10396194 :     case COMPONENT_REF:
   21413    10396194 :       {
   21414    10396194 :         tree object;
   21415    10396194 :         tree object_type;
   21416    10396194 :         tree member;
   21417    10396194 :         tree r;
   21418             : 
   21419    10396194 :         object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   21420             :                                                      args, complain, in_decl);
   21421             :         /* Remember that there was a reference to this entity.  */
   21422    10396194 :         if (DECL_P (object)
   21423    10396194 :             && !mark_used (object, complain) && !(complain & tf_error))
   21424           0 :           RETURN (error_mark_node);
   21425    10396194 :         object_type = TREE_TYPE (object);
   21426             : 
   21427    10396194 :         member = TREE_OPERAND (t, 1);
   21428    10396194 :         if (BASELINK_P (member))
   21429     4298923 :           member = tsubst_baselink (member,
   21430     4298923 :                                     non_reference (TREE_TYPE (object)),
   21431             :                                     args, complain, in_decl);
   21432             :         else
   21433     6097271 :           member = tsubst_copy (member, args, complain, in_decl);
   21434    10396194 :         if (member == error_mark_node)
   21435          38 :           RETURN (error_mark_node);
   21436             : 
   21437    10031369 :         if (object_type && TYPE_PTRMEMFUNC_P (object_type)
   21438    10396178 :             && TREE_CODE (member) == FIELD_DECL)
   21439             :           {
   21440           4 :             r = build_ptrmemfunc_access_expr (object, DECL_NAME (member));
   21441           4 :             RETURN (r);
   21442             :           }
   21443    10396152 :         else if (TREE_CODE (member) == FIELD_DECL)
   21444             :           {
   21445     2377879 :             r = finish_non_static_data_member (member, object, NULL_TREE,
   21446             :                                                complain);
   21447     2377879 :             if (TREE_CODE (r) == COMPONENT_REF)
   21448     2327251 :               REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
   21449     2377879 :             RETURN (r);
   21450             :           }
   21451     8018273 :         else if (type_dependent_expression_p (object))
   21452             :           /* We can't do much here.  */;
   21453     7647257 :         else if (!CLASS_TYPE_P (object_type))
   21454             :           {
   21455       14026 :             if (scalarish_type_p (object_type))
   21456             :               {
   21457       12193 :                 tree s = NULL_TREE;
   21458       12193 :                 tree dtor = member;
   21459             : 
   21460       12193 :                 if (TREE_CODE (dtor) == SCOPE_REF)
   21461             :                   {
   21462          20 :                     s = TREE_OPERAND (dtor, 0);
   21463          20 :                     dtor = TREE_OPERAND (dtor, 1);
   21464             :                   }
   21465       12193 :                 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
   21466             :                   {
   21467       10208 :                     dtor = TREE_OPERAND (dtor, 0);
   21468       10208 :                     if (TYPE_P (dtor))
   21469       10208 :                       RETURN (finish_pseudo_destructor_expr
   21470             :                               (object, s, dtor, input_location));
   21471             :                   }
   21472             :               }
   21473             :           }
   21474     7633231 :         else if (TREE_CODE (member) == SCOPE_REF
   21475     7633231 :                  && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
   21476             :           {
   21477             :             /* Lookup the template functions now that we know what the
   21478             :                scope is.  */
   21479          19 :             tree scope = TREE_OPERAND (member, 0);
   21480          19 :             tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
   21481          19 :             tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
   21482          19 :             member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
   21483             :                                             /*complain=*/false);
   21484          19 :             if (BASELINK_P (member))
   21485             :               {
   21486          38 :                 BASELINK_FUNCTIONS (member)
   21487          19 :                   = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
   21488             :                               args);
   21489          19 :                 member = (adjust_result_of_qualified_name_lookup
   21490          19 :                           (member, BINFO_TYPE (BASELINK_BINFO (member)),
   21491             :                            object_type));
   21492             :               }
   21493             :             else
   21494             :               {
   21495           0 :                 qualified_name_lookup_error (scope, tmpl, member,
   21496             :                                              input_location);
   21497          19 :                 RETURN (error_mark_node);
   21498             :               }
   21499             :           }
   21500     7633212 :         else if (TREE_CODE (member) == SCOPE_REF
   21501         679 :                  && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
   21502     7633216 :                  && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
   21503             :           {
   21504           4 :             if (complain & tf_error)
   21505             :               {
   21506           4 :                 if (TYPE_P (TREE_OPERAND (member, 0)))
   21507           4 :                   error ("%qT is not a class or namespace",
   21508           4 :                          TREE_OPERAND (member, 0));
   21509             :                 else
   21510           0 :                   error ("%qD is not a class or namespace",
   21511           0 :                          TREE_OPERAND (member, 0));
   21512             :               }
   21513           4 :             RETURN (error_mark_node);
   21514             :           }
   21515             : 
   21516     8008061 :         r = finish_class_member_access_expr (object, member,
   21517             :                                              /*template_p=*/false,
   21518             :                                              complain);
   21519     8008061 :         if (REF_PARENTHESIZED_P (t))
   21520         762 :           r = force_paren_expr (r);
   21521     8008061 :         RETURN (r);
   21522             :       }
   21523             : 
   21524       11871 :     case THROW_EXPR:
   21525       11871 :       RETURN (build_throw
   21526             :        (input_location, RECUR (TREE_OPERAND (t, 0))));
   21527             : 
   21528     2822045 :     case CONSTRUCTOR:
   21529     2822045 :       {
   21530     2822045 :         vec<constructor_elt, va_gc> *n;
   21531     2822045 :         constructor_elt *ce;
   21532     2822045 :         unsigned HOST_WIDE_INT idx;
   21533     2822045 :         bool process_index_p;
   21534     2822045 :         int newlen;
   21535     2822045 :         bool need_copy_p = false;
   21536     2822045 :         tree r;
   21537             : 
   21538     2822045 :         tsubst_flags_t tcomplain = complain;
   21539     2822045 :         if (COMPOUND_LITERAL_P (t))
   21540     1781758 :           tcomplain |= tf_tst_ok;
   21541     2822045 :         tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
   21542     2822045 :         if (type == error_mark_node)
   21543           3 :           RETURN (error_mark_node);
   21544             : 
   21545             :         /* We do not want to process the index of aggregate
   21546             :            initializers as they are identifier nodes which will be
   21547             :            looked up by digest_init.  */
   21548     4441440 :         process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
   21549             : 
   21550     2822042 :         if (null_member_pointer_value_p (t))
   21551             :           {
   21552          18 :             gcc_assert (same_type_p (type, TREE_TYPE (t)));
   21553          18 :             RETURN (t);
   21554             :           }
   21555             : 
   21556     2822024 :         n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
   21557     2822024 :         newlen = vec_safe_length (n);
   21558     4383153 :         FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
   21559             :           {
   21560     1561129 :             if (ce->index && process_index_p
   21561             :                 /* An identifier index is looked up in the type
   21562             :                    being initialized, not the current scope.  */
   21563         255 :                 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
   21564         237 :               ce->index = RECUR (ce->index);
   21565             : 
   21566     1561129 :             if (PACK_EXPANSION_P (ce->value))
   21567             :               {
   21568             :                 /* Substitute into the pack expansion.  */
   21569        1918 :                 ce->value = tsubst_pack_expansion (ce->value, args, complain,
   21570             :                                                   in_decl);
   21571             : 
   21572        1918 :                 if (ce->value == error_mark_node
   21573        1915 :                     || PACK_EXPANSION_P (ce->value))
   21574             :                   ;
   21575        1852 :                 else if (TREE_VEC_LENGTH (ce->value) == 1)
   21576             :                   /* Just move the argument into place.  */
   21577         865 :                   ce->value = TREE_VEC_ELT (ce->value, 0);
   21578             :                 else
   21579             :                   {
   21580             :                     /* Update the length of the final CONSTRUCTOR
   21581             :                        arguments vector, and note that we will need to
   21582             :                        copy.*/
   21583         987 :                     newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
   21584         987 :                     need_copy_p = true;
   21585             :                   }
   21586             :               }
   21587             :             else
   21588     1559211 :               ce->value = RECUR (ce->value);
   21589             :           }
   21590             : 
   21591     2822024 :         if (need_copy_p)
   21592             :           {
   21593         965 :             vec<constructor_elt, va_gc> *old_n = n;
   21594             : 
   21595         965 :             vec_alloc (n, newlen);
   21596        2069 :             FOR_EACH_VEC_ELT (*old_n, idx, ce)
   21597             :               {
   21598        1104 :                 if (TREE_CODE (ce->value) == TREE_VEC)
   21599             :                   {
   21600         987 :                     int i, len = TREE_VEC_LENGTH (ce->value);
   21601        5113 :                     for (i = 0; i < len; ++i)
   21602        4126 :                       CONSTRUCTOR_APPEND_ELT (n, 0,
   21603             :                                               TREE_VEC_ELT (ce->value, i));
   21604             :                   }
   21605             :                 else
   21606        1221 :                   CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
   21607             :               }
   21608             :           }
   21609             : 
   21610     2822024 :         r = build_constructor (init_list_type_node, n);
   21611     2822024 :         CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
   21612     5644048 :         CONSTRUCTOR_IS_DESIGNATED_INIT (r)
   21613     2822024 :           = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
   21614             : 
   21615     2822024 :         if (TREE_HAS_CONSTRUCTOR (t))
   21616             :           {
   21617     1781755 :             fcl_t cl = fcl_functional;
   21618     1781755 :             if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
   21619          85 :               cl = fcl_c99;
   21620     1781755 :             RETURN (finish_compound_literal (type, r, complain, cl));
   21621             :           }
   21622             : 
   21623     1040269 :         TREE_TYPE (r) = type;
   21624     2822045 :         RETURN (r);
   21625             :       }
   21626             : 
   21627       70413 :     case TYPEID_EXPR:
   21628       70413 :       {
   21629       70413 :         tree operand_0 = TREE_OPERAND (t, 0);
   21630       70413 :         if (TYPE_P (operand_0))
   21631             :           {
   21632       70269 :             operand_0 = tsubst (operand_0, args, complain, in_decl);
   21633       70269 :             RETURN (get_typeid (operand_0, complain));
   21634             :           }
   21635             :         else
   21636             :           {
   21637         144 :             operand_0 = RECUR (operand_0);
   21638         144 :             RETURN (build_typeid (operand_0, complain));
   21639             :           }
   21640             :       }
   21641             : 
   21642    58546556 :     case VAR_DECL:
   21643    58546556 :       if (!args)
   21644    20654863 :         RETURN (t);
   21645             :       /* Fall through */
   21646             : 
   21647    80555472 :     case PARM_DECL:
   21648    80555472 :       {
   21649    80555472 :         tree r = tsubst_copy (t, args, complain, in_decl);
   21650             :         /* ??? We're doing a subset of finish_id_expression here.  */
   21651    80555468 :         if (tree wrap = maybe_get_tls_wrapper_call (r))
   21652             :           /* Replace an evaluated use of the thread_local variable with
   21653             :              a call to its wrapper.  */
   21654             :           r = wrap;
   21655    80555310 :         else if (outer_automatic_var_p (r))
   21656      151096 :           r = process_outer_var_ref (r, complain);
   21657             : 
   21658    80555468 :         if (!TYPE_REF_P (TREE_TYPE (t)))
   21659             :           /* If the original type was a reference, we'll be wrapped in
   21660             :              the appropriate INDIRECT_REF.  */
   21661    73591960 :           r = convert_from_reference (r);
   21662    80555468 :         RETURN (r);
   21663             :       }
   21664             : 
   21665          56 :     case VA_ARG_EXPR:
   21666          56 :       {
   21667          56 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21668          56 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21669          56 :         RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
   21670             :       }
   21671             : 
   21672          60 :     case OFFSETOF_EXPR:
   21673          60 :       {
   21674          60 :         tree object_ptr
   21675          60 :           = tsubst_copy_and_build (TREE_OPERAND (t, 1), args,
   21676             :                                    complain, in_decl);
   21677          60 :         RETURN (finish_offsetof (object_ptr,
   21678             :                                  RECUR (TREE_OPERAND (t, 0)),
   21679             :                                  EXPR_LOCATION (t)));
   21680             :       }
   21681             : 
   21682      150765 :     case ADDRESSOF_EXPR:
   21683      150765 :       RETURN (cp_build_addressof (EXPR_LOCATION (t),
   21684             :                                   RECUR (TREE_OPERAND (t, 0)), complain));
   21685             : 
   21686     4179251 :     case TRAIT_EXPR:
   21687     4179251 :       {
   21688     4179251 :         tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
   21689             :                                   complain, in_decl);
   21690     4179251 :         tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
   21691             :                              complain, in_decl);
   21692     4179251 :         RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
   21693             :                                    TRAIT_EXPR_KIND (t), type1, type2));
   21694             :       }
   21695             : 
   21696         171 :     case STMT_EXPR:
   21697         171 :       {
   21698         171 :         tree old_stmt_expr = cur_stmt_expr;
   21699         171 :         tree stmt_expr = begin_stmt_expr ();
   21700             : 
   21701         171 :         cur_stmt_expr = stmt_expr;
   21702         171 :         tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
   21703         171 :         stmt_expr = finish_stmt_expr (stmt_expr, false);
   21704         171 :         cur_stmt_expr = old_stmt_expr;
   21705             : 
   21706             :         /* If the resulting list of expression statement is empty,
   21707             :            fold it further into void_node.  */
   21708         171 :         if (empty_expr_stmt_p (stmt_expr))
   21709          12 :           stmt_expr = void_node;
   21710             : 
   21711         171 :         RETURN (stmt_expr);
   21712             :       }
   21713             : 
   21714       38863 :     case LAMBDA_EXPR:
   21715       38863 :       {
   21716       38863 :         if (complain & tf_partial)
   21717             :           {
   21718             :             /* We don't have a full set of template arguments yet; don't touch
   21719             :                the lambda at all.  */
   21720           0 :             gcc_assert (processing_template_decl);
   21721             :             return t;
   21722             :           }
   21723       38863 :         tree r = tsubst_lambda_expr (t, args, complain, in_decl);
   21724             : 
   21725       38863 :         RETURN (build_lambda_object (r));
   21726             :       }
   21727             : 
   21728          12 :     case TRANSACTION_EXPR:
   21729          12 :       RETURN (tsubst_expr (t, args, complain, in_decl));
   21730             : 
   21731      335934 :     case PAREN_EXPR:
   21732      335934 :       if (REF_PARENTHESIZED_P (t))
   21733      335933 :         RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
   21734             :       else
   21735             :         /* Recreate the PAREN_EXPR from __builtin_assoc_barrier.  */
   21736             :         {
   21737           1 :           tree op0 = RECUR (TREE_OPERAND (t, 0));
   21738           1 :           RETURN (build1_loc (input_location, PAREN_EXPR,
   21739             :                               TREE_TYPE (op0), op0));
   21740             :         }
   21741             : 
   21742          13 :     case VEC_PERM_EXPR:
   21743          13 :       {
   21744          13 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21745          13 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   21746          13 :         tree op2 = RECUR (TREE_OPERAND (t, 2));
   21747          13 :         RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
   21748             :                                        complain));
   21749             :       }
   21750             : 
   21751      274663 :     case REQUIRES_EXPR:
   21752      274663 :       {
   21753      274663 :         tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
   21754      273763 :         RETURN (r);
   21755             :       }
   21756             : 
   21757           6 :     case RANGE_EXPR:
   21758             :       /* No need to substitute further, a RANGE_EXPR will always be built
   21759             :          with constant operands.  */
   21760           6 :       RETURN (t);
   21761             : 
   21762   122191382 :     case NON_LVALUE_EXPR:
   21763   122191382 :     case VIEW_CONVERT_EXPR:
   21764   122191382 :       {
   21765   122191382 :         tree op = RECUR (TREE_OPERAND (t, 0));
   21766             : 
   21767   122191382 :         if (location_wrapper_p (t))
   21768             :           /* We need to do this here as well as in tsubst_copy so we get the
   21769             :              other tsubst_copy_and_build semantics for a PARM_DECL operand.  */
   21770   122190950 :           RETURN (maybe_wrap_with_location (op, EXPR_LOCATION (t)));
   21771             : 
   21772         432 :         gcc_checking_assert (TREE_CODE (t) == VIEW_CONVERT_EXPR);
   21773         432 :         if (REF_PARENTHESIZED_P (t))
   21774             :           /* force_paren_expr can also create a VIEW_CONVERT_EXPR.  */
   21775           0 :           RETURN (finish_parenthesized_expr (op));
   21776             : 
   21777             :         /* Otherwise, we're dealing with a wrapper to make a C++20 template
   21778             :            parameter object const.  */
   21779         432 :         if (TREE_TYPE (op) == NULL_TREE
   21780         432 :             || !CP_TYPE_CONST_P (TREE_TYPE (op)))
   21781             :           {
   21782             :             /* The template argument is not const, presumably because
   21783             :                it is still dependent, and so not the const template parm
   21784             :                object.  */
   21785         394 :             tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21786         394 :             if (TREE_CODE (op) == CONSTRUCTOR
   21787         393 :                 || TREE_CODE (op) == IMPLICIT_CONV_EXPR)
   21788             :               {
   21789             :                 /* Don't add a wrapper to these.  */
   21790           2 :                 op = copy_node (op);
   21791           2 :                 TREE_TYPE (op) = type;
   21792             :               }
   21793             :             else
   21794             :               /* Do add a wrapper otherwise (in particular, if op is
   21795             :                  another TEMPLATE_PARM_INDEX).  */
   21796         392 :               op = build1 (VIEW_CONVERT_EXPR, type, op);
   21797             :           }
   21798         432 :         RETURN (op);
   21799             :       }
   21800             : 
   21801   282466487 :     default:
   21802             :       /* Handle Objective-C++ constructs, if appropriate.  */
   21803   282466487 :       {
   21804   282466487 :         tree subst
   21805   282466487 :           = objcp_tsubst_copy_and_build (t, args, complain, in_decl);
   21806   282466487 :         if (subst)
   21807           0 :           RETURN (subst);
   21808             :       }
   21809   282466487 :       RETURN (tsubst_copy (t, args, complain, in_decl));
   21810             :     }
   21811             : 
   21812             : #undef RECUR
   21813             : #undef RETURN
   21814   770917917 :  out:
   21815   770917917 :   input_location = save_loc;
   21816   770917917 :   return retval;
   21817             : }
   21818             : 
   21819             : /* Verify that the instantiated ARGS are valid. For type arguments,
   21820             :    make sure that the type's linkage is ok. For non-type arguments,
   21821             :    make sure they are constants if they are integral or enumerations.
   21822             :    Emit an error under control of COMPLAIN, and return TRUE on error.  */
   21823             : 
   21824             : static bool
   21825   137989940 : check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
   21826             : {
   21827   137989940 :   if (dependent_template_arg_p (t))
   21828             :     return false;
   21829   124573735 :   if (ARGUMENT_PACK_P (t))
   21830             :     {
   21831    11198182 :       tree vec = ARGUMENT_PACK_ARGS (t);
   21832    11198182 :       int len = TREE_VEC_LENGTH (vec);
   21833    11198182 :       bool result = false;
   21834    11198182 :       int i;
   21835             : 
   21836    38119123 :       for (i = 0; i < len; ++i)
   21837    26920941 :         if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
   21838           0 :           result = true;
   21839    11198182 :       return result;
   21840             :     }
   21841   113375553 :   else if (TYPE_P (t))
   21842             :     {
   21843             :       /* [basic.link]: A name with no linkage (notably, the name
   21844             :          of a class or enumeration declared in a local scope)
   21845             :          shall not be used to declare an entity with linkage.
   21846             :          This implies that names with no linkage cannot be used as
   21847             :          template arguments
   21848             : 
   21849             :          DR 757 relaxes this restriction for C++0x.  */
   21850    98310877 :       tree nt = (cxx_dialect > cxx98 ? NULL_TREE
   21851      116835 :                  : no_linkage_check (t, /*relaxed_p=*/false));
   21852             : 
   21853      116835 :       if (nt)
   21854             :         {
   21855             :           /* DR 488 makes use of a type with no linkage cause
   21856             :              type deduction to fail.  */
   21857           8 :           if (complain & tf_error)
   21858             :             {
   21859          10 :               if (TYPE_UNNAMED_P (nt))
   21860           0 :                 error ("%qT is/uses unnamed type", t);
   21861             :               else
   21862           5 :                 error ("template argument for %qD uses local type %qT",
   21863             :                        tmpl, t);
   21864             :             }
   21865           8 :           return true;
   21866             :         }
   21867             :       /* In order to avoid all sorts of complications, we do not
   21868             :          allow variably-modified types as template arguments.  */
   21869    98310869 :       else if (variably_modified_type_p (t, NULL_TREE))
   21870             :         {
   21871           8 :           if (complain & tf_error)
   21872           4 :             error ("%qT is a variably modified type", t);
   21873           8 :           return true;
   21874             :         }
   21875             :     }
   21876             :   /* Class template and alias template arguments should be OK.  */
   21877    15064676 :   else if (DECL_TYPE_TEMPLATE_P (t))
   21878             :     ;
   21879             :   /* A non-type argument of integral or enumerated type must be a
   21880             :      constant.  */
   21881    14770882 :   else if (TREE_TYPE (t)
   21882    14770882 :            && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
   21883    14769047 :            && !REFERENCE_REF_P (t)
   21884    29539843 :            && !TREE_CONSTANT (t))
   21885             :     {
   21886           0 :       if (complain & tf_error)
   21887           0 :         error ("integral expression %qE is not constant", t);
   21888           0 :       return true;
   21889             :     }
   21890             :   return false;
   21891             : }
   21892             : 
   21893             : static bool
   21894    71382273 : check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
   21895             : {
   21896    71382273 :   int ix, len = DECL_NTPARMS (tmpl);
   21897    71382273 :   bool result = false;
   21898             : 
   21899   182451272 :   for (ix = 0; ix != len; ix++)
   21900             :     {
   21901   111068999 :       if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
   21902          16 :         result = true;
   21903             :     }
   21904    71382273 :   if (result && (complain & tf_error))
   21905           9 :     error ("  trying to instantiate %qD", tmpl);
   21906    71382273 :   return result;
   21907             : }
   21908             : 
   21909             : /* Call mark_used on each entity within the non-type template arguments in
   21910             :    ARGS for an instantiation of TMPL, to ensure that each such entity is
   21911             :    considered odr-used (and therefore marked for instantiation) regardless of
   21912             :    whether the specialization was first formed in a template context (which
   21913             :    inhibits mark_used).
   21914             : 
   21915             :    This function assumes push_to_top_level has been called beforehand.  */
   21916             : 
   21917             : static void
   21918    30638987 : mark_template_arguments_used (tree tmpl, tree args)
   21919             : {
   21920             :   /* It suffices to do this only when instantiating a primary template.  */
   21921    30638987 :   if (TREE_CODE (tmpl) != TEMPLATE_DECL || !PRIMARY_TEMPLATE_P (tmpl))
   21922             :     return;
   21923             : 
   21924             :   /* We already marked outer arguments when specializing the context.  */
   21925    23817200 :   args = INNERMOST_TEMPLATE_ARGS (args);
   21926             : 
   21927    60972531 :   for (tree arg : tree_vec_range (args))
   21928             :     {
   21929             :       /* A (pointer/reference to) function or variable NTTP argument.  */
   21930    37155331 :       if (TREE_CODE (arg) == ADDR_EXPR
   21931    37154745 :           || TREE_CODE (arg) == INDIRECT_REF)
   21932             :         {
   21933        1908 :           while (TREE_CODE (arg) == ADDR_EXPR
   21934        1138 :                  || REFERENCE_REF_P (arg)
   21935        2862 :                  || CONVERT_EXPR_P (arg))
   21936        1138 :             arg = TREE_OPERAND (arg, 0);
   21937         770 :           if (VAR_OR_FUNCTION_DECL_P (arg))
   21938             :             {
   21939             :               /* Pass tf_none to avoid duplicate diagnostics: if this call
   21940             :                  fails then an earlier call to mark_used for this argument
   21941             :                  must have also failed and emitted a diagnostic.  */
   21942         764 :               bool ok = mark_used (arg, tf_none);
   21943         764 :               gcc_checking_assert (ok || seen_error ());
   21944             :             }
   21945             :         }
   21946             :       /* A class NTTP argument.  */
   21947    37154561 :       else if (VAR_P (arg)
   21948    37154561 :                && DECL_NTTP_OBJECT_P (arg))
   21949             :         {
   21950         506 :           auto mark_used_r = [](tree *tp, int *, void *) {
   21951         376 :             if (VAR_OR_FUNCTION_DECL_P (*tp))
   21952             :               {
   21953           3 :                 bool ok = mark_used (*tp, tf_none);
   21954           3 :                 gcc_checking_assert (ok || seen_error ());
   21955             :               }
   21956         376 :             return NULL_TREE;
   21957             :           };
   21958         130 :           cp_walk_tree_without_duplicates (&DECL_INITIAL (arg),
   21959             :                                            mark_used_r, nullptr);
   21960             :         }
   21961             :     }
   21962             : }
   21963             : 
   21964             : /* We're out of SFINAE context now, so generate diagnostics for the access
   21965             :    errors we saw earlier when instantiating D from TMPL and ARGS.  */
   21966             : 
   21967             : static void
   21968          10 : recheck_decl_substitution (tree d, tree tmpl, tree args)
   21969             : {
   21970          10 :   tree pattern = DECL_TEMPLATE_RESULT (tmpl);
   21971          10 :   tree type = TREE_TYPE (pattern);
   21972          10 :   location_t loc = input_location;
   21973             : 
   21974          10 :   push_access_scope (d);
   21975          10 :   push_deferring_access_checks (dk_no_deferred);
   21976          10 :   input_location = DECL_SOURCE_LOCATION (pattern);
   21977          10 :   tsubst (type, args, tf_warning_or_error, d);
   21978          10 :   input_location = loc;
   21979          10 :   pop_deferring_access_checks ();
   21980          10 :   pop_access_scope (d);
   21981          10 : }
   21982             : 
   21983             : /* Instantiate the indicated variable, function, or alias template TMPL with
   21984             :    the template arguments in TARG_PTR.  */
   21985             : 
   21986             : tree
   21987    81734637 : instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
   21988             : {
   21989    81734637 :   auto_timevar tv (TV_TEMPLATE_INST);
   21990             : 
   21991    81734637 :   tree targ_ptr = orig_args;
   21992    81734637 :   tree fndecl;
   21993    81734637 :   tree gen_tmpl;
   21994    81734637 :   tree spec;
   21995    81734637 :   bool access_ok = true;
   21996             : 
   21997    81734637 :   if (tmpl == error_mark_node)
   21998             :     return error_mark_node;
   21999             : 
   22000    81734628 :   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
   22001             : 
   22002    81734628 :   if (modules_p ())
   22003      383116 :     lazy_load_pendings (tmpl);
   22004             : 
   22005             :   /* If this function is a clone, handle it specially.  */
   22006    81734628 :   if (DECL_CLONED_FUNCTION_P (tmpl))
   22007             :     {
   22008     2087669 :       tree spec;
   22009     2087669 :       tree clone;
   22010             : 
   22011             :       /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
   22012             :          DECL_CLONED_FUNCTION.  */
   22013     2087669 :       spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
   22014             :                                    targ_ptr, complain);
   22015     2087669 :       if (spec == error_mark_node)
   22016             :         return error_mark_node;
   22017             : 
   22018             :       /* Look for the clone.  */
   22019     4021006 :       FOR_EACH_CLONE (clone, spec)
   22020     4021006 :         if (DECL_NAME (clone) == DECL_NAME (tmpl))
   22021     2083605 :           return clone;
   22022             :       /* We should always have found the clone by now.  */
   22023           0 :       gcc_unreachable ();
   22024             :       return NULL_TREE;
   22025             :     }
   22026             : 
   22027    79646959 :   if (targ_ptr == error_mark_node)
   22028             :     return error_mark_node;
   22029             : 
   22030             :   /* Check to see if we already have this specialization.  */
   22031    79646959 :   gen_tmpl = most_general_template (tmpl);
   22032   159293918 :   if (TMPL_ARGS_DEPTH (targ_ptr)
   22033    79646959 :       < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
   22034             :     /* targ_ptr only has the innermost template args, so add the outer ones
   22035             :        from tmpl, which could be either a partial instantiation or gen_tmpl (in
   22036             :        the case of a non-dependent call within a template definition).  */
   22037     8855129 :     targ_ptr = (add_outermost_template_args
   22038     8855129 :                 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
   22039             :                  targ_ptr));
   22040             : 
   22041             :   /* It would be nice to avoid hashing here and then again in tsubst_decl,
   22042             :      but it doesn't seem to be on the hot path.  */
   22043    79646959 :   spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
   22044             : 
   22045    79646959 :   gcc_checking_assert (tmpl == gen_tmpl
   22046             :                        || ((fndecl
   22047             :                             = retrieve_specialization (tmpl, orig_args, 0))
   22048             :                            == spec)
   22049             :                        || fndecl == NULL_TREE);
   22050             : 
   22051    79646959 :   if (spec != NULL_TREE)
   22052             :     {
   22053    35029733 :       if (FNDECL_HAS_ACCESS_ERRORS (spec))
   22054             :         {
   22055          16 :           if (complain & tf_error)
   22056          10 :             recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
   22057          16 :           return error_mark_node;
   22058             :         }
   22059             :       return spec;
   22060             :     }
   22061             : 
   22062    44617226 :   if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
   22063             :                                complain))
   22064           6 :     return error_mark_node;
   22065             : 
   22066             :   /* We are building a FUNCTION_DECL, during which the access of its
   22067             :      parameters and return types have to be checked.  However this
   22068             :      FUNCTION_DECL which is the desired context for access checking
   22069             :      is not built yet.  We solve this chicken-and-egg problem by
   22070             :      deferring all checks until we have the FUNCTION_DECL.  */
   22071    44617220 :   push_deferring_access_checks (dk_deferred);
   22072             : 
   22073             :   /* Instantiation of the function happens in the context of the function
   22074             :      template, not the context of the overload resolution we're doing.  */
   22075    44617220 :   push_to_top_level ();
   22076             :   /* If there are dependent arguments, e.g. because we're doing partial
   22077             :      ordering, make sure processing_template_decl stays set.  */
   22078    44617220 :   if (uses_template_parms (targ_ptr))
   22079     9695030 :     ++processing_template_decl;
   22080    44617220 :   if (DECL_CLASS_SCOPE_P (gen_tmpl))
   22081             :     {
   22082    12851845 :       tree ctx;
   22083    12851845 :       if (!uses_template_parms (DECL_CONTEXT (tmpl)))
   22084             :         /* If the context of the partially instantiated template is
   22085             :            already non-dependent, then we might as well use it.  */
   22086    10225768 :         ctx = DECL_CONTEXT (tmpl);
   22087             :       else
   22088     2626077 :         ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
   22089             :                                 complain, gen_tmpl, true);
   22090    12851845 :       push_nested_class (ctx);
   22091             :     }
   22092             : 
   22093    44617220 :   tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
   22094             : 
   22095    44617220 :   tree partial_ti = NULL_TREE;
   22096    44617220 :   fndecl = NULL_TREE;
   22097    44617220 :   if (VAR_P (pattern))
   22098             :     {
   22099             :       /* We need to determine if we're using a partial or explicit
   22100             :          specialization now, because the type of the variable could be
   22101             :          different.  */
   22102      601389 :       tree tid = build2 (TEMPLATE_ID_EXPR, NULL_TREE, tmpl, targ_ptr);
   22103      601389 :       partial_ti = most_specialized_partial_spec (tid, complain);
   22104      601389 :       if (partial_ti == error_mark_node)
   22105             :         pattern = error_mark_node;
   22106      601386 :       else if (partial_ti)
   22107             :         {
   22108       42521 :           tree partial_tmpl = TI_TEMPLATE (partial_ti);
   22109       42521 :           tree partial_args = TI_ARGS (partial_ti);
   22110       42521 :           tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
   22111       42521 :           fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
   22112             :         }
   22113             :     }
   22114             : 
   22115             :   /* Substitute template parameters to obtain the specialization.  */
   22116    44617220 :   if (fndecl == NULL_TREE)
   22117    44574699 :     fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
   22118    44609084 :   if (DECL_CLASS_SCOPE_P (gen_tmpl))
   22119    12851845 :     pop_nested_class ();
   22120    44609084 :   pop_from_top_level ();
   22121             : 
   22122    44609084 :   if (fndecl == error_mark_node)
   22123             :     {
   22124     1222540 :       pop_deferring_access_checks ();
   22125     1222540 :       return error_mark_node;
   22126             :     }
   22127             : 
   22128             :   /* The DECL_TI_TEMPLATE should always be the immediate parent
   22129             :      template, not the most general template.  */
   22130    43386544 :   DECL_TI_TEMPLATE (fndecl) = tmpl;
   22131    43386544 :   DECL_TI_ARGS (fndecl) = targ_ptr;
   22132    43386544 :   if (VAR_P (pattern))
   22133             :     /* Now that we we've formed this variable template specialization,
   22134             :        remember the result of most_specialized_partial_spec for it.  */
   22135      601386 :     TI_PARTIAL_INFO (DECL_TEMPLATE_INFO (fndecl)) = partial_ti;
   22136             : 
   22137    43386544 :   set_instantiating_module (fndecl);
   22138             : 
   22139             :   /* Now we know the specialization, compute access previously
   22140             :      deferred.  Do no access control for inheriting constructors,
   22141             :      as we already checked access for the inherited constructor.  */
   22142    43386544 :   if (!(flag_new_inheriting_ctors
   22143    56202252 :         && DECL_INHERITED_CTOR (fndecl)))
   22144             :     {
   22145    43378767 :       push_access_scope (fndecl);
   22146    43378767 :       if (!perform_deferred_access_checks (complain))
   22147             :         access_ok = false;
   22148    43378767 :       pop_access_scope (fndecl);
   22149             :     }
   22150    43386544 :   pop_deferring_access_checks ();
   22151             : 
   22152             :   /* If we've just instantiated the main entry point for a function,
   22153             :      instantiate all the alternate entry points as well.  We do this
   22154             :      by cloning the instantiation of the main entry point, not by
   22155             :      instantiating the template clones.  */
   22156    43386544 :   if (tree chain = DECL_CHAIN (gen_tmpl))
   22157    43208538 :     if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
   22158       58000 :       clone_cdtor (fndecl, /*update_methods=*/false);
   22159             : 
   22160    43386544 :   if (!access_ok)
   22161             :     {
   22162          98 :       if (!(complain & tf_error))
   22163             :         {
   22164             :           /* Remember to reinstantiate when we're out of SFINAE so the user
   22165             :              can see the errors.  */
   22166          98 :           FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
   22167             :         }
   22168          98 :       return error_mark_node;
   22169             :     }
   22170             : 
   22171             :   return fndecl;
   22172    81726501 : }
   22173             : 
   22174             : /* Instantiate the alias template TMPL with ARGS.  Also push a template
   22175             :    instantiation level, which instantiate_template doesn't do because
   22176             :    functions and variables have sufficient context established by the
   22177             :    callers.  */
   22178             : 
   22179             : static tree
   22180    48453951 : instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
   22181             : {
   22182    48453951 :   if (tmpl == error_mark_node || args == error_mark_node)
   22183             :     return error_mark_node;
   22184             : 
   22185    47453787 :   args = coerce_template_parms (DECL_TEMPLATE_PARMS (tmpl),
   22186             :                                 args, tmpl, complain);
   22187    47453787 :   if (args == error_mark_node)
   22188             :     return error_mark_node;
   22189             : 
   22190             :   /* FIXME check for satisfaction in check_instantiated_args.  */
   22191    47453771 :   if (!constraints_satisfied_p (tmpl, args))
   22192             :     {
   22193        2861 :       if (complain & tf_error)
   22194             :         {
   22195          13 :           auto_diagnostic_group d;
   22196          13 :           error ("template constraint failure for %qD", tmpl);
   22197          13 :           diagnose_constraints (input_location, tmpl, args);
   22198          13 :         }
   22199        2861 :       return error_mark_node;
   22200             :     }
   22201             : 
   22202    47450910 :   if (!push_tinst_level (tmpl, args))
   22203           0 :     return error_mark_node;
   22204    47450907 :   tree r = instantiate_template (tmpl, args, complain);
   22205    47450907 :   pop_tinst_level ();
   22206             : 
   22207    47450907 :   if (tree d = dependent_alias_template_spec_p (TREE_TYPE (r), nt_opaque))
   22208             :     {
   22209             :       /* An alias template specialization can be dependent
   22210             :          even if its underlying type is not.  */
   22211     3668683 :       TYPE_DEPENDENT_P (d) = true;
   22212     3668683 :       TYPE_DEPENDENT_P_VALID (d) = true;
   22213             :       /* Sometimes a dependent alias spec is equivalent to its expansion,
   22214             :          sometimes not.  So always use structural_comptypes.  */
   22215     3668683 :       SET_TYPE_STRUCTURAL_EQUALITY (d);
   22216             :     }
   22217             : 
   22218             :   return r;
   22219             : }
   22220             : 
   22221             : /* PARM is a template parameter pack for FN.  Returns true iff
   22222             :    PARM is used in a deducible way in the argument list of FN.  */
   22223             : 
   22224             : static bool
   22225     4585062 : pack_deducible_p (tree parm, tree fn)
   22226             : {
   22227     4585062 :   tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
   22228     9192146 :   for (; t; t = TREE_CHAIN (t))
   22229             :     {
   22230     4607319 :       tree type = TREE_VALUE (t);
   22231     4607319 :       tree packs;
   22232     4607319 :       if (!PACK_EXPANSION_P (type))
   22233     4607051 :         continue;
   22234         536 :       for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
   22235         304 :            packs; packs = TREE_CHAIN (packs))
   22236         271 :         if (template_args_equal (TREE_VALUE (packs), parm))
   22237             :           {
   22238             :             /* The template parameter pack is used in a function parameter
   22239             :                pack.  If this is the end of the parameter list, the
   22240             :                template parameter pack is deducible.  */
   22241         235 :             if (TREE_CHAIN (t) == void_list_node)
   22242             :               return true;
   22243             :             else
   22244             :               /* Otherwise, not.  Well, it could be deduced from
   22245             :                  a non-pack parameter, but doing so would end up with
   22246             :                  a deduction mismatch, so don't bother.  */
   22247             :               return false;
   22248             :           }
   22249             :     }
   22250             :   /* The template parameter pack isn't used in any function parameter
   22251             :      packs, but it might be used deeper, e.g. tuple<Args...>.  */
   22252             :   return true;
   22253             : }
   22254             : 
   22255             : /* Subroutine of fn_type_unification: check non-dependent parms for
   22256             :    convertibility.  */
   22257             : 
   22258             : static int
   22259    20466321 : check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
   22260             :                                  tree fn, unification_kind_t strict, int flags,
   22261             :                                  struct conversion **convs, bool explain_p)
   22262             : {
   22263             :   /* Non-constructor methods need to leave a conversion for 'this', which
   22264             :      isn't included in nargs here.  */
   22265    20466321 :   unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
   22266    24296809 :                      && !DECL_CONSTRUCTOR_P (fn));
   22267             : 
   22268    20466321 :   for (unsigned ia = 0;
   22269    47276940 :        parms && parms != void_list_node && ia < nargs; )
   22270             :     {
   22271    28298118 :       tree parm = TREE_VALUE (parms);
   22272             : 
   22273    28298118 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
   22274    28298118 :           && (!TREE_CHAIN (parms)
   22275      217661 :               || TREE_CHAIN (parms) == void_list_node))
   22276             :         /* For a function parameter pack that occurs at the end of the
   22277             :            parameter-declaration-list, the type A of each remaining
   22278             :            argument of the call is compared with the type P of the
   22279             :            declarator-id of the function parameter pack.  */
   22280             :         break;
   22281             : 
   22282    28080496 :       parms = TREE_CHAIN (parms);
   22283             : 
   22284    28080496 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
   22285             :         /* For a function parameter pack that does not occur at the
   22286             :            end of the parameter-declaration-list, the type of the
   22287             :            parameter pack is a non-deduced context.  */
   22288          40 :         continue;
   22289             : 
   22290    28080456 :       if (!uses_template_parms (parm))
   22291             :         {
   22292     8395332 :           tree arg = args[ia];
   22293     8395332 :           conversion **conv_p = convs ? &convs[ia+offset] : NULL;
   22294     8395332 :           int lflags = conv_flags (ia, nargs, fn, arg, flags);
   22295             : 
   22296     8395332 :           if (check_non_deducible_conversion (parm, arg, strict, lflags,
   22297             :                                               conv_p, explain_p))
   22298             :             return 1;
   22299             :         }
   22300             : 
   22301    26810579 :       ++ia;
   22302             :     }
   22303             : 
   22304             :   return 0;
   22305             : }
   22306             : 
   22307             : /* The FN is a TEMPLATE_DECL for a function.  ARGS is an array with
   22308             :    NARGS elements of the arguments that are being used when calling
   22309             :    it.  TARGS is a vector into which the deduced template arguments
   22310             :    are placed.
   22311             : 
   22312             :    Returns either a FUNCTION_DECL for the matching specialization of FN or
   22313             :    NULL_TREE if no suitable specialization can be found.  If EXPLAIN_P is
   22314             :    true, diagnostics will be printed to explain why it failed.
   22315             : 
   22316             :    If FN is a conversion operator, or we are trying to produce a specific
   22317             :    specialization, RETURN_TYPE is the return type desired.
   22318             : 
   22319             :    The EXPLICIT_TARGS are explicit template arguments provided via a
   22320             :    template-id.
   22321             : 
   22322             :    The parameter STRICT is one of:
   22323             : 
   22324             :    DEDUCE_CALL:
   22325             :      We are deducing arguments for a function call, as in
   22326             :      [temp.deduct.call].  If RETURN_TYPE is non-null, we are
   22327             :      deducing arguments for a call to the result of a conversion
   22328             :      function template, as in [over.call.object].
   22329             : 
   22330             :    DEDUCE_CONV:
   22331             :      We are deducing arguments for a conversion function, as in
   22332             :      [temp.deduct.conv].
   22333             : 
   22334             :    DEDUCE_EXACT:
   22335             :      We are deducing arguments when doing an explicit instantiation
   22336             :      as in [temp.explicit], when determining an explicit specialization
   22337             :      as in [temp.expl.spec], or when taking the address of a function
   22338             :      template, as in [temp.deduct.funcaddr].  */
   22339             : 
   22340             : tree
   22341   169180027 : fn_type_unification (tree fn,
   22342             :                      tree explicit_targs,
   22343             :                      tree targs,
   22344             :                      const tree *args,
   22345             :                      unsigned int nargs,
   22346             :                      tree return_type,
   22347             :                      unification_kind_t strict,
   22348             :                      int flags,
   22349             :                      struct conversion **convs,
   22350             :                      bool explain_p,
   22351             :                      bool decltype_p)
   22352             : {
   22353   169180027 :   tree parms;
   22354   169180027 :   tree fntype;
   22355   169180027 :   tree decl = NULL_TREE;
   22356   169180027 :   tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
   22357   169180027 :   bool ok;
   22358   169180027 :   static int deduction_depth;
   22359             :   /* type_unification_real will pass back any access checks from default
   22360             :      template argument substitution.  */
   22361   169180027 :   vec<deferred_access_check, va_gc> *checks = NULL;
   22362             :   /* We don't have all the template args yet.  */
   22363   169180027 :   bool incomplete = true;
   22364             : 
   22365   169180027 :   tree orig_fn = fn;
   22366   169180027 :   if (flag_new_inheriting_ctors)
   22367   169178175 :     fn = strip_inheriting_ctors (fn);
   22368             : 
   22369   169180027 :   tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
   22370   169180027 :   tree r = error_mark_node;
   22371             : 
   22372   169180027 :   tree full_targs = targs;
   22373   338360054 :   if (TMPL_ARGS_DEPTH (targs)
   22374   169180027 :       < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
   22375       25026 :     full_targs = (add_outermost_template_args
   22376       25026 :                   (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
   22377             :                    targs));
   22378             : 
   22379   169180027 :   if (decltype_p)
   22380     6856321 :     complain |= tf_decltype;
   22381             : 
   22382             :   /* In C++0x, it's possible to have a function template whose type depends
   22383             :      on itself recursively.  This is most obvious with decltype, but can also
   22384             :      occur with enumeration scope (c++/48969).  So we need to catch infinite
   22385             :      recursion and reject the substitution at deduction time; this function
   22386             :      will return error_mark_node for any repeated substitution.
   22387             : 
   22388             :      This also catches excessive recursion such as when f<N> depends on
   22389             :      f<N-1> across all integers, and returns error_mark_node for all the
   22390             :      substitutions back up to the initial one.
   22391             : 
   22392             :      This is, of course, not reentrant.  */
   22393   169180027 :   if (excessive_deduction_depth)
   22394           0 :     return error_mark_node;
   22395   169180027 :   ++deduction_depth;
   22396             : 
   22397   169180027 :   gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
   22398             : 
   22399   169180027 :   fntype = TREE_TYPE (fn);
   22400   169180027 :   if (explicit_targs)
   22401             :     {
   22402             :       /* [temp.deduct]
   22403             : 
   22404             :          The specified template arguments must match the template
   22405             :          parameters in kind (i.e., type, nontype, template), and there
   22406             :          must not be more arguments than there are parameters;
   22407             :          otherwise type deduction fails.
   22408             : 
   22409             :          Nontype arguments must match the types of the corresponding
   22410             :          nontype template parameters, or must be convertible to the
   22411             :          types of the corresponding nontype parameters as specified in
   22412             :          _temp.arg.nontype_, otherwise type deduction fails.
   22413             : 
   22414             :          All references in the function type of the function template
   22415             :          to the corresponding template parameters are replaced by the
   22416             :          specified template argument values.  If a substitution in a
   22417             :          template parameter or in the function type of the function
   22418             :          template results in an invalid type, type deduction fails.  */
   22419    18476110 :       int i, len = TREE_VEC_LENGTH (tparms);
   22420    18476110 :       location_t loc = input_location;
   22421    18476110 :       incomplete = false;
   22422             : 
   22423    18476110 :       if (explicit_targs == error_mark_node)
   22424          16 :         goto fail;
   22425             : 
   22426    36952188 :       if (TMPL_ARGS_DEPTH (explicit_targs)
   22427    36952188 :           < TMPL_ARGS_DEPTH (full_targs))
   22428        3798 :         explicit_targs = add_outermost_template_args (full_targs,
   22429             :                                                       explicit_targs);
   22430             : 
   22431             :       /* Adjust any explicit template arguments before entering the
   22432             :          substitution context.  */
   22433    18476094 :       explicit_targs
   22434    18476094 :         = (coerce_template_parms (tparms, explicit_targs, fn,
   22435             :                                   complain|tf_partial,
   22436             :                                   /*require_all_args=*/false));
   22437    18476094 :       if (explicit_targs == error_mark_node)
   22438     1094362 :         goto fail;
   22439             : 
   22440             :       /* Substitute the explicit args into the function type.  This is
   22441             :          necessary so that, for instance, explicitly declared function
   22442             :          arguments can match null pointed constants.  If we were given
   22443             :          an incomplete set of explicit args, we must not do semantic
   22444             :          processing during substitution as we could create partial
   22445             :          instantiations.  */
   22446    44320847 :       for (i = 0; i < len; i++)
   22447             :         {
   22448    26939115 :           tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
   22449    26939115 :           bool parameter_pack = false;
   22450    26939115 :           tree targ = TREE_VEC_ELT (explicit_targs, i);
   22451             : 
   22452             :           /* Dig out the actual parm.  */
   22453    26939115 :           if (TREE_CODE (parm) == TYPE_DECL
   22454     2006876 :               || TREE_CODE (parm) == TEMPLATE_DECL)
   22455             :             {
   22456    24934896 :               parm = TREE_TYPE (parm);
   22457    24934896 :               parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
   22458             :             }
   22459     2004219 :           else if (TREE_CODE (parm) == PARM_DECL)
   22460             :             {
   22461     2004219 :               parm = DECL_INITIAL (parm);
   22462     2004219 :               parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
   22463             :             }
   22464             : 
   22465    26939115 :           if (targ == NULL_TREE)
   22466             :             /* No explicit argument for this template parameter.  */
   22467             :             incomplete = true;
   22468    23048136 :           else if (parameter_pack && pack_deducible_p (parm, fn))
   22469             :             {
   22470             :               /* Mark the argument pack as "incomplete". We could
   22471             :                  still deduce more arguments during unification.
   22472             :                  We remove this mark in type_unification_real.  */
   22473     4585049 :               ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
   22474     4585049 :               ARGUMENT_PACK_EXPLICIT_ARGS (targ)
   22475     4585049 :                 = ARGUMENT_PACK_ARGS (targ);
   22476             : 
   22477             :               /* We have some incomplete argument packs.  */
   22478     4585049 :               incomplete = true;
   22479             :             }
   22480             :         }
   22481             : 
   22482    17381732 :       if (incomplete)
   22483             :         {
   22484     7103943 :           if (!push_tinst_level (fn, explicit_targs))
   22485             :             {
   22486           0 :               excessive_deduction_depth = true;
   22487           0 :               goto fail;
   22488             :             }
   22489     7103939 :           ++processing_template_decl;
   22490     7103939 :           input_location = DECL_SOURCE_LOCATION (fn);
   22491             :           /* Ignore any access checks; we'll see them again in
   22492             :              instantiate_template and they might have the wrong
   22493             :              access path at this point.  */
   22494     7103939 :           push_deferring_access_checks (dk_deferred);
   22495     7103939 :           tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
   22496     7103939 :           fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
   22497     7101242 :           pop_deferring_access_checks ();
   22498     7101242 :           input_location = loc;
   22499     7101242 :           --processing_template_decl;
   22500     7101242 :           pop_tinst_level ();
   22501             : 
   22502     7101242 :           if (fntype == error_mark_node)
   22503         456 :             goto fail;
   22504             :         }
   22505             : 
   22506             :       /* Place the explicitly specified arguments in TARGS.  */
   22507    17378575 :       explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
   22508    44311242 :       for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
   22509    26932667 :         TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
   22510    17378575 :       if (!incomplete && CHECKING_P
   22511    27656364 :           && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
   22512    10277789 :         SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
   22513             :           (targs, NUM_TMPL_ARGS (explicit_targs));
   22514             :     }
   22515             : 
   22516   168082492 :   if (return_type && strict != DEDUCE_CALL)
   22517             :     {
   22518     8126096 :       tree *new_args = XALLOCAVEC (tree, nargs + 1);
   22519     8126096 :       new_args[0] = return_type;
   22520     8126096 :       memcpy (new_args + 1, args, nargs * sizeof (tree));
   22521     8126096 :       args = new_args;
   22522     8126096 :       ++nargs;
   22523             :     }
   22524             : 
   22525   168082492 :   if (!incomplete)
   22526    10277789 :     goto deduced;
   22527             : 
   22528             :   /* Never do unification on the 'this' parameter.  */
   22529   157804703 :   parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
   22530             : 
   22531   157804703 :   if (return_type && strict == DEDUCE_CALL)
   22532             :     {
   22533             :       /* We're deducing for a call to the result of a template conversion
   22534             :          function.  The parms we really want are in return_type.  */
   22535          17 :       if (INDIRECT_TYPE_P (return_type))
   22536          17 :         return_type = TREE_TYPE (return_type);
   22537          17 :       parms = TYPE_ARG_TYPES (return_type);
   22538             :     }
   22539   157804686 :   else if (return_type)
   22540             :     {
   22541     7501555 :       parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
   22542             :     }
   22543             : 
   22544             :   /* We allow incomplete unification without an error message here
   22545             :      because the standard doesn't seem to explicitly prohibit it.  Our
   22546             :      callers must be ready to deal with unification failures in any
   22547             :      event.  */
   22548             : 
   22549             :   /* If we aren't explaining yet, push tinst context so we can see where
   22550             :      any errors (e.g. from class instantiations triggered by instantiation
   22551             :      of default template arguments) come from.  If we are explaining, this
   22552             :      context is redundant.  */
   22553   315607167 :   if (!explain_p && !push_tinst_level (fn, targs))
   22554             :     {
   22555           0 :       excessive_deduction_depth = true;
   22556           0 :       goto fail;
   22557             :     }
   22558             : 
   22559   157804697 :   ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
   22560             :                                full_targs, parms, args, nargs, /*subr=*/0,
   22561             :                                strict, &checks, explain_p);
   22562   157804697 :   if (!explain_p)
   22563   157802464 :     pop_tinst_level ();
   22564   157804697 :   if (!ok)
   22565   137221742 :     goto fail;
   22566             : 
   22567             :   /* Now that we have bindings for all of the template arguments,
   22568             :      ensure that the arguments deduced for the template template
   22569             :      parameters have compatible template parameter lists.  We cannot
   22570             :      check this property before we have deduced all template
   22571             :      arguments, because the template parameter types of a template
   22572             :      template parameter might depend on prior template parameters
   22573             :      deduced after the template template parameter.  The following
   22574             :      ill-formed example illustrates this issue:
   22575             : 
   22576             :        template<typename T, template<T> class C> void f(C<5>, T);
   22577             : 
   22578             :        template<int N> struct X {};
   22579             : 
   22580             :        void g() {
   22581             :          f(X<5>(), 5l); // error: template argument deduction fails
   22582             :        }
   22583             : 
   22584             :      The template parameter list of 'C' depends on the template type
   22585             :      parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
   22586             :      'long'.  Thus, we can't check that 'C' cannot bind to 'X' at the
   22587             :      time that we deduce 'C'.  */
   22588    41165910 :   if (!template_template_parm_bindings_ok_p
   22589    20582955 :            (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
   22590             :     {
   22591          32 :       unify_inconsistent_template_template_parameters (explain_p);
   22592          32 :       goto fail;
   22593             :     }
   22594             : 
   22595    20582923 :  deduced:
   22596             : 
   22597             :   /* CWG2369: Check satisfaction before non-deducible conversions.  */
   22598    30860712 :   if (!constraints_satisfied_p (fn, targs))
   22599             :     {
   22600      115906 :       if (explain_p)
   22601         250 :         diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
   22602      115906 :       goto fail;
   22603             :     }
   22604             : 
   22605             :   /* DR 1391: All parameters have args, now check non-dependent parms for
   22606             :      convertibility.  We don't do this if all args were explicitly specified,
   22607             :      as the standard says that we substitute explicit args immediately.  */
   22608    30743906 :   if (incomplete
   22609    30743906 :       && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
   22610             :                                           convs, explain_p))
   22611     1269877 :     goto fail;
   22612             : 
   22613             :   /* All is well so far.  Now, check:
   22614             : 
   22615             :      [temp.deduct]
   22616             : 
   22617             :      When all template arguments have been deduced, all uses of
   22618             :      template parameters in nondeduced contexts are replaced with
   22619             :      the corresponding deduced argument values.  If the
   22620             :      substitution results in an invalid type, as described above,
   22621             :      type deduction fails.  */
   22622    29474029 :   if (!push_tinst_level (fn, targs))
   22623             :     {
   22624           0 :       excessive_deduction_depth = true;
   22625           0 :       goto fail;
   22626             :     }
   22627             : 
   22628             :   /* Also collect access checks from the instantiation.  */
   22629    29474022 :   reopen_deferring_access_checks (checks);
   22630             : 
   22631    29474022 :   decl = instantiate_template (fn, targs, complain);
   22632             : 
   22633    29465886 :   checks = get_deferred_access_checks ();
   22634    29465886 :   pop_deferring_access_checks ();
   22635             : 
   22636    29465886 :   pop_tinst_level ();
   22637             : 
   22638    29465886 :   if (decl == error_mark_node)
   22639     1222605 :     goto fail;
   22640             : 
   22641             :   /* Now perform any access checks encountered during substitution.  */
   22642    28243281 :   push_access_scope (decl);
   22643    28243281 :   ok = perform_access_checks (checks, complain);
   22644    28243281 :   pop_access_scope (decl);
   22645    28243281 :   if (!ok)
   22646          22 :     goto fail;
   22647             : 
   22648             :   /* If we're looking for an exact match, check that what we got
   22649             :      is indeed an exact match.  It might not be if some template
   22650             :      parameters are used in non-deduced contexts.  But don't check
   22651             :      for an exact match if we have dependent template arguments;
   22652             :      in that case we're doing partial ordering, and we already know
   22653             :      that we have two candidates that will provide the actual type.  */
   22654    28243259 :   if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
   22655             :     {
   22656     1620785 :       tree substed = TREE_TYPE (decl);
   22657     1620785 :       unsigned int i;
   22658             : 
   22659     1620785 :       tree sarg
   22660     1620785 :         = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
   22661     1620785 :       if (return_type)
   22662     1620758 :         sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
   22663     7093069 :       for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
   22664     5527492 :         if (!same_type_p (args[i], TREE_VALUE (sarg)))
   22665             :           {
   22666       55208 :             unify_type_mismatch (explain_p, args[i],
   22667       55208 :                                  TREE_VALUE (sarg));
   22668       55208 :             goto fail;
   22669             :           }
   22670     1565577 :       if ((i < nargs || sarg)
   22671             :           /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args
   22672             :              doesn't contain the trailing void, and conv fns are always ().  */
   22673     1565577 :           && !DECL_CONV_FN_P (decl))
   22674             :         {
   22675           8 :           unsigned nsargs = i + list_length (sarg);
   22676           8 :           unify_arity (explain_p, nargs, nsargs);
   22677           8 :           goto fail;
   22678             :         }
   22679             :     }
   22680             : 
   22681             :   /* After doing deduction with the inherited constructor, actually return an
   22682             :      instantiation of the inheriting constructor.  */
   22683    28188043 :   if (orig_fn != fn)
   22684       10222 :     decl = instantiate_template (orig_fn, targs, complain);
   22685             : 
   22686             :   r = decl;
   22687             : 
   22688   169168277 :  fail:
   22689   169168277 :   --deduction_depth;
   22690   169168277 :   if (excessive_deduction_depth)
   22691             :     {
   22692           0 :       if (deduction_depth == 0)
   22693             :         /* Reset once we're all the way out.  */
   22694           0 :         excessive_deduction_depth = false;
   22695             :     }
   22696             : 
   22697             :   return r;
   22698             : }
   22699             : 
   22700             : /* Returns true iff PARM is a forwarding reference in the context of
   22701             :    template argument deduction for TMPL.  */
   22702             : 
   22703             : static bool
   22704   167178755 : forwarding_reference_p (tree parm, tree tmpl)
   22705             : {
   22706             :   /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a
   22707             :      cv-unqualified template parameter ..."  */
   22708   167178755 :   if (TYPE_REF_P (parm)
   22709   132545184 :       && TYPE_REF_IS_RVALUE (parm)
   22710     8438158 :       && TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM
   22711   172664559 :       && cp_type_quals (TREE_TYPE (parm)) == TYPE_UNQUALIFIED)
   22712             :     {
   22713     5413312 :       parm = TREE_TYPE (parm);
   22714             :       /* [temp.deduct.call], "... that does not represent a template parameter
   22715             :          of a class template (during class template argument deduction)."  */
   22716     5413312 :       if (tmpl
   22717     5345286 :           && deduction_guide_p (tmpl)
   22718     5417071 :           && DECL_ARTIFICIAL (tmpl))
   22719             :         {
   22720             :           /* Since the template parameters of a synthesized guide consist of
   22721             :              the template parameters of the class template followed by those of
   22722             :              the constructor (if any), we can tell if PARM represents a template
   22723             :              parameter of the class template by comparing its index with the
   22724             :              arity of the class template.  */
   22725        2371 :           tree ctmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl)));
   22726        2371 :           if (TEMPLATE_TYPE_IDX (parm)
   22727        2371 :               < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl)))
   22728             :             return false;
   22729             :         }
   22730             :       return true;
   22731             :     }
   22732             :   return false;
   22733             : }
   22734             : 
   22735             : /* Adjust types before performing type deduction, as described in
   22736             :    [temp.deduct.call] and [temp.deduct.conv].  The rules in these two
   22737             :    sections are symmetric.  PARM is the type of a function parameter
   22738             :    or the return type of the conversion function.  ARG is the type of
   22739             :    the argument passed to the call, or the type of the value
   22740             :    initialized with the result of the conversion function.
   22741             :    ARG_EXPR is the original argument expression, which may be null.  */
   22742             : 
   22743             : static int
   22744   167178755 : maybe_adjust_types_for_deduction (tree tparms,
   22745             :                                   unification_kind_t strict,
   22746             :                                   tree* parm,
   22747             :                                   tree* arg,
   22748             :                                   tree arg_expr)
   22749             : {
   22750   167178755 :   int result = 0;
   22751             : 
   22752   167178755 :   switch (strict)
   22753             :     {
   22754             :     case DEDUCE_CALL:
   22755             :       break;
   22756             : 
   22757       33347 :     case DEDUCE_CONV:
   22758             :       /* [temp.deduct.conv] First remove a reference type on parm.
   22759             :          DRs 322 & 976 affected this.  */
   22760       33347 :       if (TYPE_REF_P (*parm))
   22761          94 :         *parm = TREE_TYPE (*parm);
   22762             : 
   22763             :       /* Swap PARM and ARG throughout the remainder of this
   22764             :          function; the handling is precisely symmetric since PARM
   22765             :          will initialize ARG rather than vice versa.  */
   22766             :       std::swap (parm, arg);
   22767             : 
   22768             :       break;
   22769             : 
   22770    20000197 :     case DEDUCE_EXACT:
   22771             :       /* Core issue #873: Do the DR606 thing (see below) for these cases,
   22772             :          too, but here handle it by stripping the reference from PARM
   22773             :          rather than by adding it to ARG.  */
   22774    20000197 :       if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
   22775      866063 :           && TYPE_REF_P (*arg)
   22776    20782973 :           && !TYPE_REF_IS_RVALUE (*arg))
   22777      782765 :         *parm = TREE_TYPE (*parm);
   22778             :       /* Nothing else to do in this case.  */
   22779             :       return 0;
   22780             : 
   22781           0 :     default:
   22782           0 :       gcc_unreachable ();
   22783             :     }
   22784             : 
   22785   147178558 :   if (!TYPE_REF_P (*parm))
   22786             :     {
   22787             :       /* [temp.deduct.call]
   22788             : 
   22789             :          If P is not a reference type:
   22790             : 
   22791             :          --If A is an array type, the pointer type produced by the
   22792             :          array-to-pointer standard conversion (_conv.array_) is
   22793             :          used in place of A for type deduction; otherwise,
   22794             : 
   22795             :          --If A is a function type, the pointer type produced by
   22796             :          the function-to-pointer standard conversion
   22797             :          (_conv.func_) is used in place of A for type deduction;
   22798             :          otherwise,
   22799             : 
   22800             :          --If A is a cv-qualified type, the top level
   22801             :          cv-qualifiers of A's type are ignored for type
   22802             :          deduction.  */
   22803    29970479 :       if (TREE_CODE (*arg) == ARRAY_TYPE)
   22804      288292 :         *arg = build_pointer_type (TREE_TYPE (*arg));
   22805    29682187 :       else if (TREE_CODE (*arg) == FUNCTION_TYPE)
   22806        3661 :         *arg = build_pointer_type (*arg);
   22807             :       else
   22808    29678526 :         *arg = TYPE_MAIN_VARIANT (*arg);
   22809             :     }
   22810             : 
   22811             :   /* [temp.deduct.call], "If P is a forwarding reference and the argument is
   22812             :      an lvalue, the type 'lvalue reference to A' is used in place of A for
   22813             :      type deduction."  */
   22814   147178558 :   if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
   22815   147178558 :       && (arg_expr ? lvalue_p (arg_expr)
   22816             :           /* try_one_overload doesn't provide an arg_expr, but
   22817             :              functions are always lvalues.  */
   22818          47 :           : TREE_CODE (*arg) == FUNCTION_TYPE))
   22819     1558654 :     *arg = build_reference_type (*arg);
   22820             : 
   22821             :   /* [temp.deduct.call]
   22822             : 
   22823             :      If P is a cv-qualified type, the top level cv-qualifiers
   22824             :      of P's type are ignored for type deduction.  If P is a
   22825             :      reference type, the type referred to by P is used for
   22826             :      type deduction.  */
   22827   147178558 :   *parm = TYPE_MAIN_VARIANT (*parm);
   22828   147178558 :   if (TYPE_REF_P (*parm))
   22829             :     {
   22830   117208079 :       *parm = TREE_TYPE (*parm);
   22831   117208079 :       result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
   22832             :     }
   22833             : 
   22834             :   return result;
   22835             : }
   22836             : 
   22837             : /* Subroutine of fn_type_unification.  PARM is a function parameter of a
   22838             :    template which doesn't contain any deducible template parameters; check if
   22839             :    ARG is a suitable match for it.  STRICT, FLAGS and EXPLAIN_P are as in
   22840             :    unify_one_argument.  */
   22841             : 
   22842             : static int
   22843     8395332 : check_non_deducible_conversion (tree parm, tree arg, unification_kind_t strict,
   22844             :                                 int flags, struct conversion **conv_p,
   22845             :                                 bool explain_p)
   22846             : {
   22847     8395332 :   tree type;
   22848             : 
   22849     8395332 :   if (!TYPE_P (arg))
   22850     7638730 :     type = TREE_TYPE (arg);
   22851             :   else
   22852             :     type = arg;
   22853             : 
   22854     8395332 :   if (same_type_p (parm, type))
   22855     7125455 :     return unify_success (explain_p);
   22856             : 
   22857     2894605 :   tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
   22858     2894605 :   if (strict == DEDUCE_CONV)
   22859             :     {
   22860          19 :       if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
   22861     7125455 :         return unify_success (explain_p);
   22862             :     }
   22863     2894586 :   else if (strict == DEDUCE_CALL)
   22864             :     {
   22865     2894586 :       bool ok = false;
   22866     2894586 :       tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
   22867     2894586 :       if (conv_p)
   22868             :         /* Avoid recalculating this in add_function_candidate.  */
   22869     5788948 :         ok = (*conv_p
   22870     2894474 :               = good_conversion (parm, type, conv_arg, flags, complain));
   22871             :       else
   22872         112 :         ok = can_convert_arg (parm, type, conv_arg, flags, complain);
   22873     2894586 :       if (ok)
   22874     7125455 :         return unify_success (explain_p);
   22875             :     }
   22876             : 
   22877     1269877 :   if (strict == DEDUCE_EXACT)
   22878           0 :     return unify_type_mismatch (explain_p, parm, arg);
   22879             :   else
   22880     1269877 :     return unify_arg_conversion (explain_p, parm, type, arg);
   22881             : }
   22882             : 
   22883             : static bool uses_deducible_template_parms (tree type);
   22884             : 
   22885             : /* Returns true iff the expression EXPR is one from which a template
   22886             :    argument can be deduced.  In other words, if it's an undecorated
   22887             :    use of a template non-type parameter.  */
   22888             : 
   22889             : static bool
   22890     1445576 : deducible_expression (tree expr)
   22891             : {
   22892             :   /* Strip implicit conversions and implicit INDIRECT_REFs.  */
   22893     1445576 :   while (CONVERT_EXPR_P (expr)
   22894     1445585 :          || TREE_CODE (expr) == VIEW_CONVERT_EXPR
   22895     2891403 :          || REFERENCE_REF_P (expr))
   22896         245 :     expr = TREE_OPERAND (expr, 0);
   22897     1445576 :   return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
   22898             : }
   22899             : 
   22900             : /* Returns true iff the array domain DOMAIN uses a template parameter in a
   22901             :    deducible way; that is, if it has a max value of <PARM> - 1.  */
   22902             : 
   22903             : static bool
   22904        4689 : deducible_array_bound (tree domain)
   22905             : {
   22906        4689 :   if (domain == NULL_TREE)
   22907             :     return false;
   22908             : 
   22909        4613 :   tree max = TYPE_MAX_VALUE (domain);
   22910        4613 :   if (TREE_CODE (max) != MINUS_EXPR)
   22911             :     return false;
   22912             : 
   22913        4300 :   return deducible_expression (TREE_OPERAND (max, 0));
   22914             : }
   22915             : 
   22916             : /* Returns true iff the template arguments ARGS use a template parameter
   22917             :    in a deducible way.  */
   22918             : 
   22919             : static bool
   22920   119160491 : deducible_template_args (tree args)
   22921             : {
   22922   123469824 :   for (tree elt : tree_vec_range (args))
   22923             :     {
   22924   120708502 :       bool deducible;
   22925   120708502 :       if (ARGUMENT_PACK_P (elt))
   22926     5237915 :         deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
   22927             :       else
   22928             :         {
   22929   115470587 :           if (PACK_EXPANSION_P (elt))
   22930     4969509 :             elt = PACK_EXPANSION_PATTERN (elt);
   22931   115470587 :           if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
   22932             :             deducible = true;
   22933   115470553 :           else if (TYPE_P (elt))
   22934   114029299 :             deducible = uses_deducible_template_parms (elt);
   22935             :           else
   22936     1441254 :             deducible = deducible_expression (elt);
   22937             :         }
   22938   120708468 :       if (deducible)
   22939   116399169 :         return true;
   22940             :     }
   22941     2761322 :   return false;
   22942             : }
   22943             : 
   22944             : /* Returns true iff TYPE contains any deducible references to template
   22945             :    parameters, as per 14.8.2.5.  */
   22946             : 
   22947             : static bool
   22948   293525000 : uses_deducible_template_parms (tree type)
   22949             : {
   22950   425504066 :   if (PACK_EXPANSION_P (type))
   22951          89 :     type = PACK_EXPANSION_PATTERN (type);
   22952             : 
   22953             :   /* T
   22954             :      cv-list T
   22955             :      TT<T>
   22956             :      TT<i>
   22957             :      TT<> */
   22958   425504066 :   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
   22959   279302919 :       || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
   22960             :     return true;
   22961             : 
   22962             :   /* T*
   22963             :      T&
   22964             :      T&&  */
   22965   279302611 :   if (INDIRECT_TYPE_P (type))
   22966   131979066 :     return uses_deducible_template_parms (TREE_TYPE (type));
   22967             : 
   22968             :   /* T[integer-constant ]
   22969             :      type [i]  */
   22970   147323545 :   if (TREE_CODE (type) == ARRAY_TYPE)
   22971      268892 :     return (uses_deducible_template_parms (TREE_TYPE (type))
   22972      273518 :             || deducible_array_bound (TYPE_DOMAIN (type)));
   22973             : 
   22974             :   /* T type ::*
   22975             :      type T::*
   22976             :      T T::*
   22977             :      T (type ::*)()
   22978             :      type (T::*)()
   22979             :      type (type ::*)(T)
   22980             :      type (T::*)(T)
   22981             :      T (type ::*)(T)
   22982             :      T (T::*)()
   22983             :      T (T::*)(T) */
   22984   147054653 :   if (TYPE_PTRMEM_P (type))
   22985       17775 :     return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
   22986       18153 :             || (uses_deducible_template_parms
   22987         378 :                 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
   22988             : 
   22989             :   /* template-name <T> (where template-name refers to a class template)
   22990             :      template-name <i> (where template-name refers to a class template) */
   22991   118272251 :   if (CLASS_TYPE_P (type)
   22992   118272235 :       && CLASSTYPE_TEMPLATE_INFO (type)
   22993   261909109 :       && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
   22994   113922576 :     return deducible_template_args (INNERMOST_TEMPLATE_ARGS
   22995   113922576 :                                     (CLASSTYPE_TI_ARGS (type)));
   22996             : 
   22997             :   /* type (T)
   22998             :      T()
   22999             :      T(T)  */
   23000    33114302 :   if (FUNC_OR_METHOD_TYPE_P (type))
   23001             :     {
   23002     3752628 :       if (uses_deducible_template_parms (TREE_TYPE (type)))
   23003             :         return true;
   23004      123445 :       tree parm = TYPE_ARG_TYPES (type);
   23005      123445 :       if (TREE_CODE (type) == METHOD_TYPE)
   23006         288 :         parm = TREE_CHAIN (parm);
   23007      126560 :       for (; parm; parm = TREE_CHAIN (parm))
   23008      124671 :         if (uses_deducible_template_parms (TREE_VALUE (parm)))
   23009             :           return true;
   23010        1889 :       if (flag_noexcept_type
   23011        1861 :           && TYPE_RAISES_EXCEPTIONS (type)
   23012          22 :           && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))
   23013        1911 :           && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))))
   23014             :         return true;
   23015             :     }
   23016             : 
   23017             :   return false;
   23018             : }
   23019             : 
   23020             : /* Subroutine of type_unification_real and unify_pack_expansion to
   23021             :    handle unification of a single P/A pair.  Parameters are as
   23022             :    for those functions.  */
   23023             : 
   23024             : static int
   23025   200459236 : unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
   23026             :                     int subr, unification_kind_t strict,
   23027             :                     bool explain_p)
   23028             : {
   23029   200459236 :   tree arg_expr = NULL_TREE;
   23030   200459236 :   int arg_strict;
   23031             : 
   23032   200459236 :   if (arg == error_mark_node || parm == error_mark_node)
   23033          13 :     return unify_invalid (explain_p);
   23034   200459227 :   if (arg == unknown_type_node)
   23035             :     /* We can't deduce anything from this, but we might get all the
   23036             :        template args from other function args.  */
   23037    28156047 :     return unify_success (explain_p);
   23038             : 
   23039             :   /* Implicit conversions (Clause 4) will be performed on a function
   23040             :      argument to convert it to the type of the corresponding function
   23041             :      parameter if the parameter type contains no template-parameters that
   23042             :      participate in template argument deduction.  */
   23043   200459227 :   if (strict != DEDUCE_EXACT
   23044   200459227 :       && TYPE_P (parm) && !uses_deducible_template_parms (parm))
   23045             :     /* For function parameters with no deducible template parameters,
   23046             :        just return.  We'll check non-dependent conversions later.  */
   23047    28156047 :     return unify_success (explain_p);
   23048             : 
   23049   172363411 :   switch (strict)
   23050             :     {
   23051             :     case DEDUCE_CALL:
   23052             :       arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
   23053             :                     | UNIFY_ALLOW_MORE_CV_QUAL
   23054             :                     | UNIFY_ALLOW_DERIVED);
   23055             :       break;
   23056             : 
   23057             :     case DEDUCE_CONV:
   23058             :       arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
   23059             :       break;
   23060             : 
   23061             :     case DEDUCE_EXACT:
   23062             :       arg_strict = UNIFY_ALLOW_NONE;
   23063             :       break;
   23064             : 
   23065           0 :     default:
   23066           0 :       gcc_unreachable ();
   23067             :     }
   23068             : 
   23069             :   /* We only do these transformations if this is the top-level
   23070             :      parameter_type_list in a call or declaration matching; in other
   23071             :      situations (nested function declarators, template argument lists) we
   23072             :      won't be comparing a type to an expression, and we don't do any type
   23073             :      adjustments.  */
   23074   172363411 :   if (!subr)
   23075             :     {
   23076   167234205 :       if (!TYPE_P (arg))
   23077             :         {
   23078   147200661 :           gcc_assert (TREE_TYPE (arg) != NULL_TREE);
   23079   147200661 :           if (type_unknown_p (arg))
   23080             :             {
   23081             :               /* [temp.deduct.type] A template-argument can be
   23082             :                  deduced from a pointer to function or pointer
   23083             :                  to member function argument if the set of
   23084             :                  overloaded functions does not contain function
   23085             :                  templates and at most one of a set of
   23086             :                  overloaded functions provides a unique
   23087             :                  match.  */
   23088       60231 :               resolve_overloaded_unification (tparms, targs, parm,
   23089             :                                               arg, strict,
   23090             :                                               arg_strict, explain_p);
   23091             :               /* If a unique match was not found, this is a
   23092             :                  non-deduced context, so we still succeed. */
   23093       60231 :               return unify_success (explain_p);
   23094             :             }
   23095             : 
   23096   147140430 :           arg_expr = arg;
   23097   147140430 :           arg = unlowered_expr_type (arg);
   23098   147140430 :           if (arg == error_mark_node)
   23099          13 :             return unify_invalid (explain_p);
   23100             :         }
   23101             : 
   23102   167173974 :       arg_strict |= maybe_adjust_types_for_deduction (tparms, strict,
   23103             :                                                       &parm, &arg, arg_expr);
   23104             :     }
   23105             :   else
   23106    10258412 :     if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
   23107     5129206 :         != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
   23108           4 :       return unify_template_argument_mismatch (explain_p, parm, arg);
   23109             : 
   23110             :   /* For deduction from an init-list we need the actual list.  */
   23111   167173974 :   if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
   23112        7378 :     arg = arg_expr;
   23113   172303176 :   return unify (tparms, targs, parm, arg, arg_strict, explain_p);
   23114             : }
   23115             : 
   23116             : /* for_each_template_parm callback that always returns 0.  */
   23117             : 
   23118             : static int
   23119    11791062 : zero_r (tree, void *)
   23120             : {
   23121    11791062 :   return 0;
   23122             : }
   23123             : 
   23124             : /* for_each_template_parm any_fn callback to handle deduction of a template
   23125             :    type argument from the type of an array bound.  */
   23126             : 
   23127             : static int
   23128    73726971 : array_deduction_r (tree t, void *data)
   23129             : {
   23130    73726971 :   tree_pair_p d = (tree_pair_p)data;
   23131    73726971 :   tree &tparms = d->purpose;
   23132    73726971 :   tree &targs = d->value;
   23133             : 
   23134    73726971 :   if (TREE_CODE (t) == ARRAY_TYPE)
   23135         118 :     if (tree dom = TYPE_DOMAIN (t))
   23136          36 :       if (tree max = TYPE_MAX_VALUE (dom))
   23137             :         {
   23138          36 :           if (TREE_CODE (max) == MINUS_EXPR)
   23139          11 :             max = TREE_OPERAND (max, 0);
   23140          36 :           if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
   23141          11 :             unify (tparms, targs, TREE_TYPE (max), size_type_node,
   23142             :                    UNIFY_ALLOW_NONE, /*explain*/false);
   23143             :         }
   23144             : 
   23145             :   /* Keep walking.  */
   23146    73726971 :   return 0;
   23147             : }
   23148             : 
   23149             : /* Try to deduce any not-yet-deduced template type arguments from the type of
   23150             :    an array bound.  This is handled separately from unify because 14.8.2.5 says
   23151             :    "The type of a type parameter is only deduced from an array bound if it is
   23152             :    not otherwise deduced."  */
   23153             : 
   23154             : static void
   23155    12084028 : try_array_deduction (tree tparms, tree targs, tree parm)
   23156             : {
   23157    12084028 :   tree_pair_s data = { tparms, targs };
   23158    12084028 :   hash_set<tree> visited;
   23159    12084028 :   for_each_template_parm (parm, zero_r, &data, &visited,
   23160             :                           /*nondeduced*/false, array_deduction_r);
   23161    12084028 : }
   23162             : 
   23163             : /* Most parms like fn_type_unification.
   23164             : 
   23165             :    If SUBR is 1, we're being called recursively (to unify the
   23166             :    arguments of a function or method parameter of a function
   23167             :    template).
   23168             : 
   23169             :    CHECKS is a pointer to a vector of access checks encountered while
   23170             :    substituting default template arguments.  */
   23171             : 
   23172             : static int
   23173   159168346 : type_unification_real (tree tparms,
   23174             :                        tree full_targs,
   23175             :                        tree xparms,
   23176             :                        const tree *xargs,
   23177             :                        unsigned int xnargs,
   23178             :                        int subr,
   23179             :                        unification_kind_t strict,
   23180             :                        vec<deferred_access_check, va_gc> **checks,
   23181             :                        bool explain_p)
   23182             : {
   23183   159168346 :   tree parm, arg;
   23184   159168346 :   int i;
   23185   159168346 :   int ntparms = TREE_VEC_LENGTH (tparms);
   23186   159168346 :   int saw_undeduced = 0;
   23187   159168346 :   tree parms;
   23188   159168346 :   const tree *args;
   23189   159168346 :   unsigned int nargs;
   23190   159168346 :   unsigned int ia;
   23191             : 
   23192   159168346 :   gcc_assert (TREE_CODE (tparms) == TREE_VEC);
   23193   159168346 :   gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
   23194   159168346 :   gcc_assert (ntparms > 0);
   23195             : 
   23196   159168346 :   tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
   23197             : 
   23198             :   /* Reset the number of non-defaulted template arguments contained
   23199             :      in TARGS.  */
   23200   159168346 :   NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
   23201             : 
   23202   159726893 :  again:
   23203   159726893 :   parms = xparms;
   23204   159726893 :   args = xargs;
   23205   159726893 :   nargs = xnargs;
   23206             : 
   23207             :   /* Only fn_type_unification cares about terminal void.  */
   23208   159726893 :   if (nargs && args[nargs-1] == void_type_node)
   23209     7468091 :     --nargs;
   23210             : 
   23211   159726893 :   ia = 0;
   23212   394772222 :   while (parms && parms != void_list_node
   23213   448180810 :          && ia < nargs)
   23214             :     {
   23215   196027786 :       parm = TREE_VALUE (parms);
   23216             : 
   23217   196027786 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
   23218   196027786 :           && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
   23219             :         /* For a function parameter pack that occurs at the end of the
   23220             :            parameter-declaration-list, the type A of each remaining
   23221             :            argument of the call is compared with the type P of the
   23222             :            declarator-id of the function parameter pack.  */
   23223             :         break;
   23224             : 
   23225   195672985 :       parms = TREE_CHAIN (parms);
   23226             : 
   23227   195672985 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
   23228             :         /* For a function parameter pack that does not occur at the
   23229             :            end of the parameter-declaration-list, the type of the
   23230             :            parameter pack is a non-deduced context.  */
   23231          46 :         continue;
   23232             : 
   23233   195672939 :       arg = args[ia];
   23234   195672939 :       ++ia;
   23235             : 
   23236   195672939 :       if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
   23237             :                               explain_p))
   23238             :         return 1;
   23239             :     }
   23240             : 
   23241    43716544 :   if (parms
   23242    39372344 :       && parms != void_list_node
   23243    56834840 :       && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
   23244             :     {
   23245             :       /* Unify the remaining arguments with the pack expansion type.  */
   23246      721438 :       tree argvec;
   23247      721438 :       tree parmvec = make_tree_vec (1);
   23248             : 
   23249             :       /* Allocate a TREE_VEC and copy in all of the arguments */
   23250      721438 :       argvec = make_tree_vec (nargs - ia);
   23251     1894688 :       for (i = 0; ia < nargs; ++ia, ++i)
   23252      451812 :         TREE_VEC_ELT (argvec, i) = args[ia];
   23253             : 
   23254             :       /* Copy the parameter into parmvec.  */
   23255      721438 :       TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
   23256      721438 :       if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
   23257             :                                 /*subr=*/subr, explain_p))
   23258             :         return 1;
   23259             : 
   23260             :       /* Advance to the end of the list of parameters.  */
   23261      721295 :       parms = TREE_CHAIN (parms);
   23262             :     }
   23263             : 
   23264             :   /* Fail if we've reached the end of the parm list, and more args
   23265             :      are present, and the parm list isn't variadic.  */
   23266    43716401 :   if (ia < nargs && parms == void_list_node)
   23267      957464 :     return unify_too_many_arguments (explain_p, nargs, ia);
   23268             :   /* Fail if parms are left and they don't have default values and
   23269             :      they aren't all deduced as empty packs (c++/57397).  This is
   23270             :      consistent with sufficient_parms_p.  */
   23271    38414569 :   if (parms && parms != void_list_node
   23272    55155801 :       && TREE_PURPOSE (parms) == NULL_TREE)
   23273             :     {
   23274             :       unsigned int count = nargs;
   23275             :       tree p = parms;
   23276    19614380 :       bool type_pack_p;
   23277    19614380 :       do
   23278             :         {
   23279    19614380 :           type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
   23280    19614380 :           if (!type_pack_p)
   23281    19594076 :             count++;
   23282    19614380 :           p = TREE_CHAIN (p);
   23283             :         }
   23284    19614380 :       while (p && p != void_list_node);
   23285    10796104 :       if (count != nargs)
   23286    10796098 :         return unify_too_few_arguments (explain_p, ia, count,
   23287    10796098 :                                         type_pack_p);
   23288             :     }
   23289             : 
   23290    31962839 :   if (!subr)
   23291             :     {
   23292    63371172 :       tsubst_flags_t complain = (explain_p
   23293    31685586 :                                  ? tf_warning_or_error
   23294             :                                  : tf_none);
   23295    31685586 :       bool tried_array_deduction = (cxx_dialect < cxx17);
   23296             : 
   23297    84151707 :       for (i = 0; i < ntparms; i++)
   23298             :         {
   23299    52599513 :           tree targ = TREE_VEC_ELT (targs, i);
   23300    52599513 :           tree tparm = TREE_VEC_ELT (tparms, i);
   23301             : 
   23302             :           /* Clear the "incomplete" flags on all argument packs now so that
   23303             :              substituting them into later default arguments works.  */
   23304    52599513 :           if (targ && ARGUMENT_PACK_P (targ))
   23305             :             {
   23306     5763077 :               ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
   23307     5763077 :               ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
   23308             :             }
   23309             : 
   23310    22753155 :           if (targ || tparm == error_mark_node)
   23311    35609435 :             continue;
   23312    16990078 :           tparm = TREE_VALUE (tparm);
   23313             : 
   23314    16990078 :           if (TREE_CODE (tparm) == TYPE_DECL
   23315    12835971 :               && !tried_array_deduction)
   23316             :             {
   23317    12084006 :               try_array_deduction (tparms, targs, xparms);
   23318    12084006 :               tried_array_deduction = true;
   23319    12084006 :               if (TREE_VEC_ELT (targs, i))
   23320           2 :                 continue;
   23321             :             }
   23322             : 
   23323             :           /* If this is an undeduced nontype parameter that depends on
   23324             :              a type parameter, try another pass; its type may have been
   23325             :              deduced from a later argument than the one from which
   23326             :              this parameter can be deduced.  */
   23327    19060632 :           if (TREE_CODE (tparm) == PARM_DECL
   23328     4154090 :               && !is_auto (TREE_TYPE (tparm))
   23329     4154064 :               && uses_template_parms (TREE_TYPE (tparm))
   23330    19626877 :               && saw_undeduced < 2)
   23331             :             {
   23332     2070556 :               saw_undeduced = 1;
   23333     2070556 :               continue;
   23334             :             }
   23335             : 
   23336             :           /* Core issue #226 (C++0x) [temp.deduct]:
   23337             : 
   23338             :              If a template argument has not been deduced, its
   23339             :              default template argument, if any, is used.
   23340             : 
   23341             :              When we are in C++98 mode, TREE_PURPOSE will either
   23342             :              be NULL_TREE or ERROR_MARK_NODE, so we do not need
   23343             :              to explicitly check cxx_dialect here.  */
   23344    14919520 :           if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
   23345             :             /* OK, there is a default argument.  Wait until after the
   23346             :                conversion check to do substitution.  */
   23347    14711410 :             continue;
   23348             : 
   23349             :           /* If the type parameter is a parameter pack, then it will
   23350             :              be deduced to an empty parameter pack.  */
   23351      208110 :           if (template_parameter_pack_p (tparm))
   23352             :             {
   23353       74718 :               tree arg;
   23354             : 
   23355       74718 :               if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
   23356             :                 {
   23357           0 :                   arg = make_node (NONTYPE_ARGUMENT_PACK);
   23358           0 :                   TREE_CONSTANT (arg) = 1;
   23359             :                 }
   23360             :               else
   23361       74718 :                 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
   23362             : 
   23363       74718 :               ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
   23364             : 
   23365       74718 :               TREE_VEC_ELT (targs, i) = arg;
   23366       74718 :               continue;
   23367       74718 :             }
   23368             : 
   23369      133392 :           return unify_parameter_deduction_failure (explain_p, tparm);
   23370             :         }
   23371             : 
   23372             :       /* During partial ordering, we deduce dependent template args.  */
   23373             :       bool any_dependent_targs = false;
   23374             : 
   23375             :       /* Now substitute into the default template arguments.  */
   23376    74599126 :       for (i = 0; i < ntparms; i++)
   23377             :         {
   23378    52371556 :           tree targ = TREE_VEC_ELT (targs, i);
   23379    52371556 :           tree tparm = TREE_VEC_ELT (tparms, i);
   23380             : 
   23381    52371556 :           if (targ)
   23382             :             {
   23383    35592161 :               if (!any_dependent_targs && dependent_template_arg_p (targ))
   23384             :                 any_dependent_targs = true;
   23385    35592161 :               continue;
   23386             :             }
   23387    16779395 :           if (tparm == error_mark_node)
   23388           0 :             continue;
   23389             : 
   23390    16779395 :           tree parm = TREE_VALUE (tparm);
   23391    16779395 :           tree arg = TREE_PURPOSE (tparm);
   23392    16779395 :           reopen_deferring_access_checks (*checks);
   23393    16779395 :           location_t save_loc = input_location;
   23394    16779395 :           if (DECL_P (parm))
   23395    16779389 :             input_location = DECL_SOURCE_LOCATION (parm);
   23396             : 
   23397    16779395 :           if (saw_undeduced == 1
   23398     3150732 :               && TREE_CODE (parm) == PARM_DECL
   23399     2220807 :               && !is_auto (TREE_TYPE (parm))
   23400    19000202 :               && uses_template_parms (TREE_TYPE (parm)))
   23401             :             {
   23402             :               /* The type of this non-type parameter depends on undeduced
   23403             :                  parameters.  Don't try to use its default argument yet,
   23404             :                  since we might deduce an argument for it on the next pass,
   23405             :                  but do check whether the arguments we already have cause
   23406             :                  substitution failure, so that that happens before we try
   23407             :                  later default arguments (78489).  */
   23408     2069581 :               ++processing_template_decl;
   23409     2069581 :               tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
   23410             :                                   NULL_TREE);
   23411     2069581 :               --processing_template_decl;
   23412     2069581 :               if (type == error_mark_node)
   23413     1503319 :                 arg = error_mark_node;
   23414             :               else
   23415             :                 arg = NULL_TREE;
   23416             :             }
   23417             :           else
   23418             :             {
   23419             :               /* Even if the call is happening in template context, getting
   23420             :                  here means it's non-dependent, and a default argument is
   23421             :                  considered a separate definition under [temp.decls], so we can
   23422             :                  do this substitution without processing_template_decl.  This
   23423             :                  is important if the default argument contains something that
   23424             :                  might be instantiation-dependent like access (87480).  */
   23425    14709814 :               processing_template_decl_sentinel s (!any_dependent_targs);
   23426    14709814 :               tree substed = NULL_TREE;
   23427    14709814 :               if (saw_undeduced == 1 && !any_dependent_targs)
   23428             :                 {
   23429             :                   /* First instatiate in template context, in case we still
   23430             :                      depend on undeduced template parameters.  */
   23431     1081151 :                   ++processing_template_decl;
   23432     1081151 :                   substed = tsubst_template_arg (arg, full_targs, complain,
   23433             :                                                  NULL_TREE);
   23434     1081151 :                   --processing_template_decl;
   23435     1081151 :                   if (substed != error_mark_node
   23436     1081151 :                       && !uses_template_parms (substed))
   23437             :                     /* We replaced all the tparms, substitute again out of
   23438             :                        template context.  */
   23439             :                     substed = NULL_TREE;
   23440             :                 }
   23441         981 :               if (!substed)
   23442    14708833 :                 substed = tsubst_template_arg (arg, full_targs, complain,
   23443             :                                                NULL_TREE);
   23444             : 
   23445    14709814 :               if (!uses_template_parms (substed))
   23446    14709799 :                 arg = convert_template_argument (parm, substed, full_targs,
   23447             :                                                  complain, i, NULL_TREE);
   23448          15 :               else if (saw_undeduced == 1)
   23449             :                 arg = NULL_TREE;
   23450           9 :               else if (!any_dependent_targs)
   23451           0 :                 arg = error_mark_node;
   23452    14709814 :             }
   23453             : 
   23454    16779395 :           input_location = save_loc;
   23455    16779395 :           *checks = get_deferred_access_checks ();
   23456    16779395 :           pop_deferring_access_checks ();
   23457             : 
   23458    16779395 :           if (arg == error_mark_node)
   23459             :             return 1;
   23460     7454771 :           else if (arg)
   23461             :             {
   23462     6888503 :               TREE_VEC_ELT (targs, i) = arg;
   23463             :               /* The position of the first default template argument,
   23464             :                  is also the number of non-defaulted arguments in TARGS.
   23465             :                  Record that.  */
   23466     6888503 :               if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
   23467     5724948 :                 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
   23468             :             }
   23469             :         }
   23470             : 
   23471    22227570 :       if (saw_undeduced++ == 1)
   23472      558547 :         goto again;
   23473             :     }
   23474             : 
   23475    21946276 :   if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
   23476    16291042 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
   23477             : 
   23478    21946276 :   return unify_success (explain_p);
   23479             : }
   23480             : 
   23481             : /* Subroutine of type_unification_real.  Args are like the variables
   23482             :    at the call site.  ARG is an overloaded function (or template-id);
   23483             :    we try deducing template args from each of the overloads, and if
   23484             :    only one succeeds, we go with that.  Modifies TARGS and returns
   23485             :    true on success.  */
   23486             : 
   23487             : static bool
   23488       60231 : resolve_overloaded_unification (tree tparms,
   23489             :                                 tree targs,
   23490             :                                 tree parm,
   23491             :                                 tree arg,
   23492             :                                 unification_kind_t strict,
   23493             :                                 int sub_strict,
   23494             :                                 bool explain_p)
   23495             : {
   23496       60231 :   tree tempargs = copy_node (targs);
   23497       60231 :   int good = 0;
   23498       60231 :   tree goodfn = NULL_TREE;
   23499       60231 :   bool addr_p;
   23500             : 
   23501       60231 :   if (TREE_CODE (arg) == ADDR_EXPR)
   23502             :     {
   23503         784 :       arg = TREE_OPERAND (arg, 0);
   23504         784 :       addr_p = true;
   23505             :     }
   23506             :   else
   23507             :     addr_p = false;
   23508             : 
   23509       60231 :   if (TREE_CODE (arg) == COMPONENT_REF)
   23510             :     /* Handle `&x' where `x' is some static or non-static member
   23511             :        function name.  */
   23512         208 :     arg = TREE_OPERAND (arg, 1);
   23513             : 
   23514       60231 :   if (TREE_CODE (arg) == OFFSET_REF)
   23515         512 :     arg = TREE_OPERAND (arg, 1);
   23516             : 
   23517             :   /* Strip baselink information.  */
   23518       60231 :   if (BASELINK_P (arg))
   23519         728 :     arg = BASELINK_FUNCTIONS (arg);
   23520             : 
   23521       60231 :   if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
   23522             :     {
   23523             :       /* If we got some explicit template args, we need to plug them into
   23524             :          the affected templates before we try to unify, in case the
   23525             :          explicit args will completely resolve the templates in question.  */
   23526             : 
   23527         483 :       int ok = 0;
   23528         483 :       tree expl_subargs = TREE_OPERAND (arg, 1);
   23529         483 :       arg = TREE_OPERAND (arg, 0);
   23530             : 
   23531        1505 :       for (lkp_iterator iter (arg); iter; ++iter)
   23532             :         {
   23533         539 :           tree fn = *iter;
   23534         539 :           tree subargs, elem;
   23535             : 
   23536         539 :           if (TREE_CODE (fn) != TEMPLATE_DECL)
   23537           0 :             continue;
   23538             : 
   23539         539 :           subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
   23540             :                                            expl_subargs, NULL_TREE, tf_none);
   23541         539 :           if (subargs != error_mark_node
   23542         539 :               && !any_dependent_template_arguments_p (subargs))
   23543             :             {
   23544         509 :               fn = instantiate_template (fn, subargs, tf_none);
   23545         509 :               if (!constraints_satisfied_p (fn))
   23546          26 :                 continue;
   23547         483 :               if (undeduced_auto_decl (fn))
   23548             :                 {
   23549             :                   /* Instantiate the function to deduce its return type.  */
   23550           6 :                   ++function_depth;
   23551           6 :                   instantiate_decl (fn, /*defer*/false, /*class*/false);
   23552           6 :                   --function_depth;
   23553             :                 }
   23554             : 
   23555         483 :               if (flag_noexcept_type)
   23556         423 :                 maybe_instantiate_noexcept (fn, tf_none);
   23557             : 
   23558         483 :               elem = TREE_TYPE (fn);
   23559         483 :               if (try_one_overload (tparms, targs, tempargs, parm,
   23560             :                                     elem, strict, sub_strict, addr_p, explain_p)
   23561         483 :                   && (!goodfn || !same_type_p (goodfn, elem)))
   23562             :                 {
   23563         453 :                   goodfn = elem;
   23564         453 :                   ++good;
   23565             :                 }
   23566             :             }
   23567          30 :           else if (subargs)
   23568          30 :             ++ok;
   23569             :         }
   23570             :       /* If no templates (or more than one) are fully resolved by the
   23571             :          explicit arguments, this template-id is a non-deduced context; it
   23572             :          could still be OK if we deduce all template arguments for the
   23573             :          enclosing call through other arguments.  */
   23574         483 :       if (good != 1)
   23575             :         good = ok;
   23576             :     }
   23577       59748 :   else if (!OVL_P (arg))
   23578             :     /* If ARG is, for example, "(0, &f)" then its type will be unknown
   23579             :        -- but the deduction does not succeed because the expression is
   23580             :        not just the function on its own.  */
   23581             :     return false;
   23582             :   else
   23583      179749 :     for (lkp_iterator iter (arg); iter; ++iter)
   23584             :       {
   23585       60253 :         tree fn = *iter;
   23586       60253 :         if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
   23587             :                               strict, sub_strict, addr_p, explain_p)
   23588       60253 :             && (!goodfn || !decls_match (goodfn, fn)))
   23589             :           {
   23590       59969 :             goodfn = fn;
   23591       59969 :             ++good;
   23592             :           }
   23593             :       }
   23594             : 
   23595             :   /* [temp.deduct.type] A template-argument can be deduced from a pointer
   23596             :      to function or pointer to member function argument if the set of
   23597             :      overloaded functions does not contain function templates and at most
   23598             :      one of a set of overloaded functions provides a unique match.
   23599             : 
   23600             :      So if we found multiple possibilities, we return success but don't
   23601             :      deduce anything.  */
   23602             : 
   23603       59810 :   if (good == 1)
   23604             :     {
   23605       59924 :       int i = TREE_VEC_LENGTH (targs);
   23606      249011 :       for (; i--; )
   23607      189087 :         if (TREE_VEC_ELT (tempargs, i))
   23608             :           {
   23609      102854 :             tree old = TREE_VEC_ELT (targs, i);
   23610      102854 :             tree new_ = TREE_VEC_ELT (tempargs, i);
   23611      102081 :             if (new_ && old && ARGUMENT_PACK_P (old)
   23612      102863 :                 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
   23613             :               /* Don't forget explicit template arguments in a pack.  */
   23614           6 :               ARGUMENT_PACK_EXPLICIT_ARGS (new_)
   23615          12 :                 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
   23616      102854 :             TREE_VEC_ELT (targs, i) = new_;
   23617             :           }
   23618             :     }
   23619       60231 :   if (good)
   23620             :     return true;
   23621             : 
   23622             :   return false;
   23623             : }
   23624             : 
   23625             : /* Core DR 115: In contexts where deduction is done and fails, or in
   23626             :    contexts where deduction is not done, if a template argument list is
   23627             :    specified and it, along with any default template arguments, identifies
   23628             :    a single function template specialization, then the template-id is an
   23629             :    lvalue for the function template specialization.  */
   23630             : 
   23631             : tree
   23632   762771202 : resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
   23633             : {
   23634   762771202 :   tree expr, offset, baselink;
   23635   762771202 :   bool addr;
   23636             : 
   23637   762771202 :   if (!type_unknown_p (orig_expr))
   23638             :     return orig_expr;
   23639             : 
   23640        3002 :   expr = orig_expr;
   23641        3002 :   addr = false;
   23642        3002 :   offset = NULL_TREE;
   23643        3002 :   baselink = NULL_TREE;
   23644             : 
   23645        3002 :   if (TREE_CODE (expr) == ADDR_EXPR)
   23646             :     {
   23647         247 :       expr = TREE_OPERAND (expr, 0);
   23648         247 :       addr = true;
   23649             :     }
   23650        3002 :   if (TREE_CODE (expr) == OFFSET_REF)
   23651             :     {
   23652          66 :       offset = expr;
   23653          66 :       expr = TREE_OPERAND (expr, 1);
   23654             :     }
   23655        3002 :   if (BASELINK_P (expr))
   23656             :     {
   23657         262 :       baselink = expr;
   23658         262 :       expr = BASELINK_FUNCTIONS (expr);
   23659             :     }
   23660             : 
   23661        3002 :   if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
   23662             :     {
   23663         843 :       int good = 0;
   23664         843 :       tree goodfn = NULL_TREE;
   23665             : 
   23666             :       /* If we got some explicit template args, we need to plug them into
   23667             :          the affected templates before we try to unify, in case the
   23668             :          explicit args will completely resolve the templates in question.  */
   23669             : 
   23670         843 :       tree expl_subargs = TREE_OPERAND (expr, 1);
   23671         843 :       tree arg = TREE_OPERAND (expr, 0);
   23672         843 :       tree badfn = NULL_TREE;
   23673         843 :       tree badargs = NULL_TREE;
   23674             : 
   23675        2620 :       for (lkp_iterator iter (arg); iter; ++iter)
   23676             :         {
   23677         934 :           tree fn = *iter;
   23678         934 :           tree subargs, elem;
   23679             : 
   23680         934 :           if (TREE_CODE (fn) != TEMPLATE_DECL)
   23681           0 :             continue;
   23682             : 
   23683         934 :           subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
   23684             :                                            expl_subargs, NULL_TREE, tf_none);
   23685         934 :           if (subargs != error_mark_node
   23686         934 :               && !any_dependent_template_arguments_p (subargs))
   23687             :             {
   23688         728 :               elem = instantiate_template (fn, subargs, tf_none);
   23689         728 :               if (elem == error_mark_node)
   23690             :                 {
   23691             :                   badfn = fn;
   23692             :                   badargs = subargs;
   23693             :                 }
   23694         688 :               else if (elem && (!goodfn || !decls_match (goodfn, elem))
   23695        1376 :                        && constraints_satisfied_p (elem))
   23696             :                 {
   23697         662 :                   goodfn = elem;
   23698         662 :                   ++good;
   23699             :                 }
   23700             :             }
   23701             :         }
   23702         843 :       if (good == 1)
   23703             :         {
   23704         608 :           mark_used (goodfn);
   23705         608 :           expr = goodfn;
   23706         608 :           if (baselink)
   23707         124 :             expr = build_baselink (BASELINK_BINFO (baselink),
   23708         124 :                                    BASELINK_ACCESS_BINFO (baselink),
   23709         124 :                                    expr, BASELINK_OPTYPE (baselink));
   23710         608 :           if (offset)
   23711             :             {
   23712          43 :               tree base
   23713          43 :                 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
   23714          43 :               expr = build_offset_ref (base, expr, addr, complain);
   23715             :             }
   23716         608 :           if (addr)
   23717         158 :             expr = cp_build_addr_expr (expr, complain);
   23718         608 :           return expr;
   23719             :         }
   23720         235 :       else if (good == 0 && badargs && (complain & tf_error))
   23721             :         /* There were no good options and at least one bad one, so let the
   23722             :            user know what the problem is.  */
   23723           4 :         instantiate_template (badfn, badargs, complain);
   23724             :     }
   23725             :   return orig_expr;
   23726             : }
   23727             : 
   23728             : /* As above, but error out if the expression remains overloaded.  */
   23729             : 
   23730             : tree
   23731   554764649 : resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
   23732             : {
   23733   554764649 :   exp = resolve_nondeduced_context (exp, complain);
   23734   554764649 :   if (type_unknown_p (exp))
   23735             :     {
   23736         117 :       if (complain & tf_error)
   23737         117 :         cxx_incomplete_type_error (exp, TREE_TYPE (exp));
   23738         117 :       return error_mark_node;
   23739             :     }
   23740             :   return exp;
   23741             : }
   23742             : 
   23743             : /* Subroutine of resolve_overloaded_unification; does deduction for a single
   23744             :    overload.  Fills TARGS with any deduced arguments, or error_mark_node if
   23745             :    different overloads deduce different arguments for a given parm.
   23746             :    ADDR_P is true if the expression for which deduction is being
   23747             :    performed was of the form "& fn" rather than simply "fn".
   23748             : 
   23749             :    Returns 1 on success.  */
   23750             : 
   23751             : static int
   23752       60736 : try_one_overload (tree tparms,
   23753             :                   tree orig_targs,
   23754             :                   tree targs,
   23755             :                   tree parm,
   23756             :                   tree arg,
   23757             :                   unification_kind_t strict,
   23758             :                   int sub_strict,
   23759             :                   bool addr_p,
   23760             :                   bool explain_p)
   23761             : {
   23762       60736 :   int nargs;
   23763       60736 :   tree tempargs;
   23764       60736 :   int i;
   23765             : 
   23766       60736 :   if (arg == error_mark_node)
   23767             :     return 0;
   23768             : 
   23769             :   /* [temp.deduct.type] A template-argument can be deduced from a pointer
   23770             :      to function or pointer to member function argument if the set of
   23771             :      overloaded functions does not contain function templates and at most
   23772             :      one of a set of overloaded functions provides a unique match.
   23773             : 
   23774             :      So if this is a template, just return success.  */
   23775             : 
   23776       60733 :   if (uses_template_parms (arg))
   23777             :     return 1;
   23778             : 
   23779        1449 :   if (TREE_CODE (arg) == METHOD_TYPE)
   23780         440 :     arg = build_ptrmemfunc_type (build_pointer_type (arg));
   23781        1009 :   else if (addr_p)
   23782         772 :     arg = build_pointer_type (arg);
   23783             : 
   23784        1449 :   sub_strict |= maybe_adjust_types_for_deduction (tparms, strict,
   23785             :                                                   &parm, &arg, NULL_TREE);
   23786             : 
   23787             :   /* We don't copy orig_targs for this because if we have already deduced
   23788             :      some template args from previous args, unify would complain when we
   23789             :      try to deduce a template parameter for the same argument, even though
   23790             :      there isn't really a conflict.  */
   23791        1449 :   nargs = TREE_VEC_LENGTH (targs);
   23792        1449 :   tempargs = make_tree_vec (nargs);
   23793             : 
   23794        1449 :   if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
   23795             :     return 0;
   23796             : 
   23797             :   /* First make sure we didn't deduce anything that conflicts with
   23798             :      explicitly specified args.  */
   23799        3386 :   for (i = nargs; i--; )
   23800             :     {
   23801        2236 :       tree elt = TREE_VEC_ELT (tempargs, i);
   23802        2236 :       tree oldelt = TREE_VEC_ELT (orig_targs, i);
   23803             : 
   23804        2236 :       if (!elt)
   23805             :         /*NOP*/;
   23806        1343 :       else if (uses_template_parms (elt))
   23807             :         /* Since we're unifying against ourselves, we will fill in
   23808             :            template args used in the function parm list with our own
   23809             :            template parms.  Discard them.  */
   23810           0 :         TREE_VEC_ELT (tempargs, i) = NULL_TREE;
   23811        1343 :       else if (oldelt && ARGUMENT_PACK_P (oldelt))
   23812             :         {
   23813             :           /* Check that the argument at each index of the deduced argument pack
   23814             :              is equivalent to the corresponding explicitly specified argument.
   23815             :              We may have deduced more arguments than were explicitly specified,
   23816             :              and that's OK.  */
   23817             : 
   23818             :           /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
   23819             :              that's wrong if we deduce the same argument pack from multiple
   23820             :              function arguments: it's only incomplete the first time.  */
   23821             : 
   23822          24 :           tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
   23823          24 :           tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
   23824             : 
   23825          24 :           if (TREE_VEC_LENGTH (deduced_pack)
   23826          24 :               < TREE_VEC_LENGTH (explicit_pack))
   23827             :             return 0;
   23828             : 
   23829          30 :           for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
   23830          21 :             if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
   23831          21 :                                       TREE_VEC_ELT (deduced_pack, j)))
   23832             :               return 0;
   23833             :         }
   23834          26 :       else if (oldelt && !template_args_equal (oldelt, elt))
   23835             :         return 0;
   23836             :     }
   23837             : 
   23838        3329 :   for (i = nargs; i--; )
   23839             :     {
   23840        2179 :       tree elt = TREE_VEC_ELT (tempargs, i);
   23841             : 
   23842        2179 :       if (elt)
   23843        1306 :         TREE_VEC_ELT (targs, i) = elt;
   23844             :     }
   23845             : 
   23846             :   return 1;
   23847             : }
   23848             : 
   23849             : /* PARM is a template class (perhaps with unbound template
   23850             :    parameters).  ARG is a fully instantiated type.  If ARG can be
   23851             :    bound to PARM, return ARG, otherwise return NULL_TREE.  TPARMS and
   23852             :    TARGS are as for unify.  */
   23853             : 
   23854             : static tree
   23855    37932841 : try_class_unification (tree tparms, tree targs, tree parm, tree arg,
   23856             :                        bool explain_p)
   23857             : {
   23858    37932841 :   if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
   23859             :     return NULL_TREE;
   23860    26384600 :   else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   23861             :     /* Matches anything.  */;
   23862    52768672 :   else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
   23863    26384336 :            != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
   23864             :     return NULL_TREE;
   23865             : 
   23866             :   /* We need to make a new template argument vector for the call to
   23867             :      unify.  If we used TARGS, we'd clutter it up with the result of
   23868             :      the attempted unification, even if this class didn't work out.
   23869             :      We also don't want to commit ourselves to all the unifications
   23870             :      we've already done, since unification is supposed to be done on
   23871             :      an argument-by-argument basis.  In other words, consider the
   23872             :      following pathological case:
   23873             : 
   23874             :        template <int I, int J, int K>
   23875             :        struct S {};
   23876             : 
   23877             :        template <int I, int J>
   23878             :        struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
   23879             : 
   23880             :        template <int I, int J, int K>
   23881             :        void f(S<I, J, K>, S<I, I, I>);
   23882             : 
   23883             :        void g() {
   23884             :          S<0, 0, 0> s0;
   23885             :          S<0, 1, 2> s2;
   23886             : 
   23887             :          f(s0, s2);
   23888             :        }
   23889             : 
   23890             :      Now, by the time we consider the unification involving `s2', we
   23891             :      already know that we must have `f<0, 0, 0>'.  But, even though
   23892             :      `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
   23893             :      because there are two ways to unify base classes of S<0, 1, 2>
   23894             :      with S<I, I, I>.  If we kept the already deduced knowledge, we
   23895             :      would reject the possibility I=1.  */
   23896     4481481 :   targs = copy_template_args (targs);
   23897    14859347 :   for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs)))
   23898    10377866 :     targ = NULL_TREE;
   23899             : 
   23900     4481481 :   int err;
   23901     4481481 :   if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   23902         264 :     err = unify_bound_ttp_args (tparms, targs, parm, arg, explain_p);
   23903             :   else
   23904     4481217 :     err = unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
   23905     4481217 :                  CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p);
   23906             : 
   23907     4481481 :   return err ? NULL_TREE : arg;
   23908             : }
   23909             : 
   23910             : /* Given a template type PARM and a class type ARG, find the unique
   23911             :    base type in ARG that is an instance of PARM.  We do not examine
   23912             :    ARG itself; only its base-classes.  If there is not exactly one
   23913             :    appropriate base class, return NULL_TREE.  PARM may be the type of
   23914             :    a partial specialization, as well as a plain template type.  Used
   23915             :    by unify.  */
   23916             : 
   23917             : static enum template_base_result
   23918    23360589 : get_template_base (tree tparms, tree targs, tree parm, tree arg,
   23919             :                    bool explain_p, tree *result)
   23920             : {
   23921    23360589 :   tree rval = NULL_TREE;
   23922    23360589 :   tree binfo;
   23923             : 
   23924    23360589 :   gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
   23925             : 
   23926    23360589 :   binfo = TYPE_BINFO (complete_type (arg));
   23927    23360589 :   if (!binfo)
   23928             :     {
   23929             :       /* The type could not be completed.  */
   23930           6 :       *result = NULL_TREE;
   23931           6 :       return tbr_incomplete_type;
   23932             :     }
   23933             : 
   23934             :   /* Walk in inheritance graph order.  The search order is not
   23935             :      important, and this avoids multiple walks of virtual bases.  */
   23936    33997274 :   for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
   23937             :     {
   23938    21273416 :       tree r = try_class_unification (tparms, targs, parm,
   23939    10636708 :                                       BINFO_TYPE (binfo), explain_p);
   23940             : 
   23941    10636708 :       if (r)
   23942             :         {
   23943             :           /* If there is more than one satisfactory baseclass, then:
   23944             : 
   23945             :                [temp.deduct.call]
   23946             : 
   23947             :               If they yield more than one possible deduced A, the type
   23948             :               deduction fails.
   23949             : 
   23950             :              applies.  */
   23951      385516 :           if (rval && !same_type_p (r, rval))
   23952             :             {
   23953             :               /* [temp.deduct.call]/4.3: If there is a class C that is a
   23954             :                  (direct or indirect) base class of D and derived (directly or
   23955             :                  indirectly) from a class B and that would be a valid deduced
   23956             :                  A, the deduced A cannot be B or pointer to B, respectively. */
   23957          47 :               if (DERIVED_FROM_P (r, rval))
   23958             :                 /* Ignore r.  */
   23959          27 :                 continue;
   23960          20 :               else if (DERIVED_FROM_P (rval, r))
   23961             :                 /* Ignore rval.  */;
   23962             :               else
   23963             :                 {
   23964          17 :                   *result = NULL_TREE;
   23965          17 :                   return tbr_ambiguous_baseclass;
   23966             :                 }
   23967             :             }
   23968             : 
   23969             :           rval = r;
   23970             :         }
   23971             :     }
   23972             : 
   23973    23360566 :   *result = rval;
   23974    23360566 :   return tbr_success;
   23975             : }
   23976             : 
   23977             : /* Returns the level of DECL, which declares a template parameter.  */
   23978             : 
   23979             : static int
   23980   107694171 : template_decl_level (tree decl)
   23981             : {
   23982   107694171 :   switch (TREE_CODE (decl))
   23983             :     {
   23984   100130844 :     case TYPE_DECL:
   23985   100130844 :     case TEMPLATE_DECL:
   23986   100130844 :       return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
   23987             : 
   23988     7563327 :     case PARM_DECL:
   23989     7563327 :       return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
   23990             : 
   23991           0 :     default:
   23992           0 :       gcc_unreachable ();
   23993             :     }
   23994             :   return 0;
   23995             : }
   23996             : 
   23997             : /* Decide whether ARG can be unified with PARM, considering only the
   23998             :    cv-qualifiers of each type, given STRICT as documented for unify.
   23999             :    Returns nonzero iff the unification is OK on that basis.  */
   24000             : 
   24001             : static int
   24002   169325485 : check_cv_quals_for_unify (int strict, tree arg, tree parm)
   24003             : {
   24004   169325485 :   int arg_quals = cp_type_quals (arg);
   24005   169325485 :   int parm_quals = cp_type_quals (parm);
   24006             : 
   24007   169325485 :   if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
   24008   102916217 :       && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
   24009             :     {
   24010             :       /*  Although a CVR qualifier is ignored when being applied to a
   24011             :           substituted template parameter ([8.3.2]/1 for example), that
   24012             :           does not allow us to unify "const T" with "int&" because both
   24013             :           types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
   24014             :           It is ok when we're allowing additional CV qualifiers
   24015             :           at the outer level [14.8.2.1]/3,1st bullet.  */
   24016    88980392 :       if ((TYPE_REF_P (arg)
   24017    88980392 :            || FUNC_OR_METHOD_TYPE_P (arg))
   24018     2097559 :           && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
   24019             :         return 0;
   24020             : 
   24021    88966275 :       if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
   24022    64559195 :           && (parm_quals & TYPE_QUAL_RESTRICT))
   24023             :         return 0;
   24024             :     }
   24025             : 
   24026   169311368 :   if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
   24027   107261361 :       && (arg_quals & parm_quals) != parm_quals)
   24028             :     return 0;
   24029             : 
   24030   168408376 :   if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
   24031    66389365 :       && (parm_quals & arg_quals) != arg_quals)
   24032      369954 :     return 0;
   24033             : 
   24034             :   return 1;
   24035             : }
   24036             : 
   24037             : /* Determines the LEVEL and INDEX for the template parameter PARM.  */
   24038             : void
   24039  2956397904 : template_parm_level_and_index (tree parm, int* level, int* index)
   24040             : {
   24041  2956397904 :   if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
   24042  2956397904 :       || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
   24043   183177016 :       || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   24044             :     {
   24045  2773427051 :       *index = TEMPLATE_TYPE_IDX (parm);
   24046  2773427051 :       *level = TEMPLATE_TYPE_LEVEL (parm);
   24047             :     }
   24048             :   else
   24049             :     {
   24050   182970853 :       *index = TEMPLATE_PARM_IDX (parm);
   24051   182970853 :       *level = TEMPLATE_PARM_LEVEL (parm);
   24052             :     }
   24053  2956397904 : }
   24054             : 
   24055             : #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP)                    \
   24056             :   do {                                                                  \
   24057             :     if (unify (TP, TA, P, A, S, EP))                                    \
   24058             :       return 1;                                                         \
   24059             :   } while (0)
   24060             : 
   24061             : /* Unifies the remaining arguments in PACKED_ARGS with the pack
   24062             :    expansion at the end of PACKED_PARMS. Returns 0 if the type
   24063             :    deduction succeeds, 1 otherwise. STRICT is the same as in
   24064             :    fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
   24065             :    function call argument list. We'll need to adjust the arguments to make them
   24066             :    types. SUBR tells us if this is from a recursive call to
   24067             :    type_unification_real, or for comparing two template argument
   24068             :    lists. */
   24069             : 
   24070             : static int
   24071     3429574 : unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
   24072             :                       tree packed_args, unification_kind_t strict,
   24073             :                       bool subr, bool explain_p)
   24074             : {
   24075     3429574 :   tree parm
   24076     3429574 :     = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
   24077     3429574 :   tree pattern = PACK_EXPANSION_PATTERN (parm);
   24078     3429574 :   tree pack, packs = NULL_TREE;
   24079     3429574 :   int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
   24080             : 
   24081             :   /* Add in any args remembered from an earlier partial instantiation.  */
   24082     3429574 :   targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
   24083     6859148 :   int levels = TMPL_ARGS_DEPTH (targs);
   24084             : 
   24085     3429574 :   packed_args = expand_template_argument_pack (packed_args);
   24086             : 
   24087     3429574 :   int len = TREE_VEC_LENGTH (packed_args);
   24088             : 
   24089             :   /* Determine the parameter packs we will be deducing from the
   24090             :      pattern, and record their current deductions.  */
   24091     6764319 :   for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
   24092     6859169 :        pack; pack = TREE_CHAIN (pack))
   24093             :     {
   24094     3429595 :       tree parm_pack = TREE_VALUE (pack);
   24095     3429595 :       int idx, level;
   24096             : 
   24097             :       /* Only template parameter packs can be deduced, not e.g. function
   24098             :          parameter packs or __bases or __integer_pack.  */
   24099     3429595 :       if (!TEMPLATE_PARM_P (parm_pack))
   24100      218856 :         continue;
   24101             : 
   24102             :       /* Determine the index and level of this parameter pack.  */
   24103     3429589 :       template_parm_level_and_index (parm_pack, &level, &idx);
   24104     3429589 :       if (level > levels)
   24105      218850 :         continue;
   24106             : 
   24107             :       /* Keep track of the parameter packs and their corresponding
   24108             :          argument packs.  */
   24109     6421478 :       packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
   24110     3210739 :       TREE_TYPE (packs) = make_tree_vec (len - start);
   24111             :     }
   24112             : 
   24113             :   /* Loop through all of the arguments that have not yet been
   24114             :      unified and unify each with the pattern.  */
   24115     8215549 :   for (i = start; i < len; i++)
   24116             :     {
   24117     4786297 :       tree parm;
   24118     4786297 :       bool any_explicit = false;
   24119     4786297 :       tree arg = TREE_VEC_ELT (packed_args, i);
   24120             : 
   24121             :       /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
   24122             :          or the element of its argument pack at the current index if
   24123             :          this argument was explicitly specified.  */
   24124     9353777 :       for (pack = packs; pack; pack = TREE_CHAIN (pack))
   24125             :         {
   24126     4567480 :           int idx, level;
   24127     4567480 :           tree arg, pargs;
   24128     4567480 :           template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
   24129             : 
   24130     4567480 :           arg = NULL_TREE;
   24131     4567480 :           if (TREE_VALUE (pack)
   24132       99222 :               && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
   24133     4568035 :               && (i - start < TREE_VEC_LENGTH (pargs)))
   24134             :             {
   24135         492 :               any_explicit = true;
   24136         492 :               arg = TREE_VEC_ELT (pargs, i - start);
   24137             :             }
   24138     9134960 :           TMPL_ARG (targs, level, idx) = arg;
   24139             :         }
   24140             : 
   24141             :       /* If we had explicit template arguments, substitute them into the
   24142             :          pattern before deduction.  */
   24143     4786297 :       if (any_explicit)
   24144             :         {
   24145             :           /* Some arguments might still be unspecified or dependent.  */
   24146         492 :           bool dependent;
   24147         492 :           ++processing_template_decl;
   24148         492 :           dependent = any_dependent_template_arguments_p (targs);
   24149         492 :           if (!dependent)
   24150         477 :             --processing_template_decl;
   24151         984 :           parm = tsubst (pattern, targs,
   24152             :                          explain_p ? tf_warning_or_error : tf_none,
   24153             :                          NULL_TREE);
   24154         492 :           if (dependent)
   24155          15 :             --processing_template_decl;
   24156         492 :           if (parm == error_mark_node)
   24157             :             return 1;
   24158             :         }
   24159             :       else
   24160             :         parm = pattern;
   24161             : 
   24162             :       /* Unify the pattern with the current argument.  */
   24163     4786297 :       if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
   24164             :                               explain_p))
   24165             :         return 1;
   24166             : 
   24167             :       /* For each parameter pack, collect the deduced value.  */
   24168     9353130 :       for (pack = packs; pack; pack = TREE_CHAIN (pack))
   24169             :         {
   24170     4567155 :           int idx, level;
   24171     4567155 :           template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
   24172             : 
   24173     4567155 :           TREE_VEC_ELT (TREE_TYPE (pack), i - start) = 
   24174     9134310 :             TMPL_ARG (targs, level, idx);
   24175             :         }
   24176             :     }
   24177             : 
   24178             :   /* Verify that the results of unification with the parameter packs
   24179             :      produce results consistent with what we've seen before, and make
   24180             :      the deduced argument packs available.  */
   24181     6639654 :   for (pack = packs; pack; pack = TREE_CHAIN (pack))
   24182             :     {
   24183     3210414 :       tree old_pack = TREE_VALUE (pack);
   24184     3210414 :       tree new_args = TREE_TYPE (pack);
   24185     3210414 :       int i, len = TREE_VEC_LENGTH (new_args);
   24186     3210414 :       int idx, level;
   24187     3210414 :       bool nondeduced_p = false;
   24188             : 
   24189             :       /* By default keep the original deduced argument pack.
   24190             :          If necessary, more specific code is going to update the
   24191             :          resulting deduced argument later down in this function.  */
   24192     3210414 :       template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
   24193     6420828 :       TMPL_ARG (targs, level, idx) = old_pack;
   24194             : 
   24195             :       /* If NEW_ARGS contains any NULL_TREE entries, we didn't
   24196             :          actually deduce anything.  */
   24197     7777276 :       for (i = 0; i < len && !nondeduced_p; ++i)
   24198     4566862 :         if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
   24199         190 :           nondeduced_p = true;
   24200     3210414 :       if (nondeduced_p)
   24201         190 :         continue;
   24202             : 
   24203     3210224 :       if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
   24204             :         {
   24205             :           /* If we had fewer function args than explicit template args,
   24206             :              just use the explicits.  */
   24207         216 :           tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
   24208         216 :           int explicit_len = TREE_VEC_LENGTH (explicit_args);
   24209         216 :           if (len < explicit_len)
   24210     3210224 :             new_args = explicit_args;
   24211             :         }
   24212             : 
   24213     3210224 :       if (!old_pack)
   24214             :         {
   24215     3065802 :           tree result;
   24216             :           /* Build the deduced *_ARGUMENT_PACK.  */
   24217     3065802 :           if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
   24218             :             {
   24219       94815 :               result = make_node (NONTYPE_ARGUMENT_PACK);
   24220       94815 :               TREE_CONSTANT (result) = 1;
   24221             :             }
   24222             :           else
   24223     2970987 :             result = cxx_make_type (TYPE_ARGUMENT_PACK);
   24224             : 
   24225     3065802 :           ARGUMENT_PACK_ARGS (result) = new_args;
   24226             : 
   24227             :           /* Note the deduced argument packs for this parameter
   24228             :              pack.  */
   24229     6131604 :           TMPL_ARG (targs, level, idx) = result;
   24230             :         }
   24231      144422 :       else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
   24232      144422 :                && (ARGUMENT_PACK_ARGS (old_pack) 
   24233         216 :                    == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
   24234             :         {
   24235             :           /* We only had the explicitly-provided arguments before, but
   24236             :              now we have a complete set of arguments.  */
   24237         216 :           tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
   24238             : 
   24239         216 :           ARGUMENT_PACK_ARGS (old_pack) = new_args;
   24240         216 :           ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
   24241         216 :           ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
   24242             :         }
   24243             :       else
   24244             :         {
   24245      144206 :           tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
   24246      144206 :           tree old_args = ARGUMENT_PACK_ARGS (old_pack);
   24247      144206 :           temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
   24248             :           /* During template argument deduction for the aggregate deduction
   24249             :              candidate, the number of elements in a trailing parameter pack
   24250             :              is only deduced from the number of remaining function
   24251             :              arguments if it is not otherwise deduced.  */
   24252      144206 :           if (cxx_dialect >= cxx20
   24253       13919 :               && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
   24254             :               /* FIXME This isn't set properly for partial instantiations.  */
   24255           5 :               && TPARMS_PRIMARY_TEMPLATE (tparms)
   24256      144210 :               && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
   24257           4 :             TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
   24258      144206 :           if (!comp_template_args (old_args, new_args,
   24259             :                                    &bad_old_arg, &bad_new_arg))
   24260             :             /* Inconsistent unification of this parameter pack.  */
   24261          13 :             return unify_parameter_pack_inconsistent (explain_p,
   24262             :                                                       bad_old_arg,
   24263             :                                                       bad_new_arg);
   24264      144206 :         }
   24265             :     }
   24266             : 
   24267     3429574 :   return unify_success (explain_p);
   24268             : }
   24269             : 
   24270             : /* Handle unification of the domain of an array.  PARM_DOM and ARG_DOM are
   24271             :    INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs.  The other
   24272             :    parameters and return value are as for unify.  */
   24273             : 
   24274             : static int
   24275       10814 : unify_array_domain (tree tparms, tree targs,
   24276             :                     tree parm_dom, tree arg_dom,
   24277             :                     bool explain_p)
   24278             : {
   24279       10814 :   tree parm_max;
   24280       10814 :   tree arg_max;
   24281       10814 :   bool parm_cst;
   24282       10814 :   bool arg_cst;
   24283             : 
   24284             :   /* Our representation of array types uses "N - 1" as the
   24285             :      TYPE_MAX_VALUE for an array with "N" elements, if "N" is
   24286             :      not an integer constant.  We cannot unify arbitrarily
   24287             :      complex expressions, so we eliminate the MINUS_EXPRs
   24288             :      here.  */
   24289       10814 :   parm_max = TYPE_MAX_VALUE (parm_dom);
   24290       10814 :   parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
   24291       10814 :   if (!parm_cst)
   24292             :     {
   24293       10733 :       gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
   24294       10733 :       parm_max = TREE_OPERAND (parm_max, 0);
   24295             :     }
   24296       10814 :   arg_max = TYPE_MAX_VALUE (arg_dom);
   24297       10814 :   arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
   24298       10814 :   if (!arg_cst)
   24299             :     {
   24300             :       /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
   24301             :          trying to unify the type of a variable with the type
   24302             :          of a template parameter.  For example:
   24303             : 
   24304             :            template <unsigned int N>
   24305             :            void f (char (&) [N]);
   24306             :            int g();
   24307             :            void h(int i) {
   24308             :              char a[g(i)];
   24309             :              f(a);
   24310             :            }
   24311             : 
   24312             :          Here, the type of the ARG will be "int [g(i)]", and
   24313             :          may be a SAVE_EXPR, etc.  */
   24314         952 :       if (TREE_CODE (arg_max) != MINUS_EXPR)
   24315           8 :         return unify_vla_arg (explain_p, arg_dom);
   24316         944 :       arg_max = TREE_OPERAND (arg_max, 0);
   24317             :     }
   24318             : 
   24319             :   /* If only one of the bounds used a MINUS_EXPR, compensate
   24320             :      by adding one to the other bound.  */
   24321       10806 :   if (parm_cst && !arg_cst)
   24322           0 :     parm_max = fold_build2_loc (input_location, PLUS_EXPR,
   24323             :                                 integer_type_node,
   24324             :                                 parm_max,
   24325             :                                 integer_one_node);
   24326       10806 :   else if (arg_cst && !parm_cst)
   24327        9781 :     arg_max = fold_build2_loc (input_location, PLUS_EXPR,
   24328             :                                integer_type_node,
   24329             :                                arg_max,
   24330             :                                integer_one_node);
   24331             : 
   24332       10806 :   return unify (tparms, targs, parm_max, arg_max,
   24333       10806 :                 UNIFY_ALLOW_INTEGER, explain_p);
   24334             : }
   24335             : 
   24336             : /* Returns whether T, a P or A in unify, is a type, template or expression.  */
   24337             : 
   24338             : enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
   24339             : 
   24340             : static pa_kind_t
   24341   641721826 : pa_kind (tree t)
   24342             : {
   24343   641721826 :   if (PACK_EXPANSION_P (t))
   24344     1009747 :     t = PACK_EXPANSION_PATTERN (t);
   24345   641721826 :   if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
   24346   641721826 :       || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
   24347   641721826 :       || DECL_TYPE_TEMPLATE_P (t))
   24348             :     return pa_tmpl;
   24349   641187521 :   else if (TYPE_P (t))
   24350             :     return pa_type;
   24351             :   else
   24352   112393845 :     return pa_expr;
   24353             : }
   24354             : 
   24355             : /* Deduce the value of template parameters.  TPARMS is the (innermost)
   24356             :    set of template parameters to a template.  TARGS is the bindings
   24357             :    for those template parameters, as determined thus far; TARGS may
   24358             :    include template arguments for outer levels of template parameters
   24359             :    as well.  PARM is a parameter to a template function, or a
   24360             :    subcomponent of that parameter; ARG is the corresponding argument.
   24361             :    This function attempts to match PARM with ARG in a manner
   24362             :    consistent with the existing assignments in TARGS.  If more values
   24363             :    are deduced, then TARGS is updated.
   24364             : 
   24365             :    Returns 0 if the type deduction succeeds, 1 otherwise.  The
   24366             :    parameter STRICT is a bitwise or of the following flags:
   24367             : 
   24368             :      UNIFY_ALLOW_NONE:
   24369             :        Require an exact match between PARM and ARG.
   24370             :      UNIFY_ALLOW_MORE_CV_QUAL:
   24371             :        Allow the deduced ARG to be more cv-qualified (by qualification
   24372             :        conversion) than ARG.
   24373             :      UNIFY_ALLOW_LESS_CV_QUAL:
   24374             :        Allow the deduced ARG to be less cv-qualified than ARG.
   24375             :      UNIFY_ALLOW_DERIVED:
   24376             :        Allow the deduced ARG to be a template base class of ARG,
   24377             :        or a pointer to a template base class of the type pointed to by
   24378             :        ARG.
   24379             :      UNIFY_ALLOW_INTEGER:
   24380             :        Allow any integral type to be deduced.  See the TEMPLATE_PARM_INDEX
   24381             :        case for more information.
   24382             :      UNIFY_ALLOW_OUTER_LEVEL:
   24383             :        This is the outermost level of a deduction. Used to determine validity
   24384             :        of qualification conversions. A valid qualification conversion must
   24385             :        have const qualified pointers leading up to the inner type which
   24386             :        requires additional CV quals, except at the outer level, where const
   24387             :        is not required [conv.qual]. It would be normal to set this flag in
   24388             :        addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
   24389             :      UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
   24390             :        This is the outermost level of a deduction, and PARM can be more CV
   24391             :        qualified at this point.
   24392             :      UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
   24393             :        This is the outermost level of a deduction, and PARM can be less CV
   24394             :        qualified at this point.  */
   24395             : 
   24396             : static int
   24397   326846664 : unify (tree tparms, tree targs, tree parm, tree arg, int strict,
   24398             :        bool explain_p)
   24399             : {
   24400   326846664 :   int idx;
   24401   326846664 :   tree targ;
   24402   326846664 :   tree tparm;
   24403   326846664 :   int strict_in = strict;
   24404   653693328 :   tsubst_flags_t complain = (explain_p
   24405   326846664 :                              ? tf_warning_or_error
   24406             :                              : tf_none);
   24407             : 
   24408             :   /* I don't think this will do the right thing with respect to types.
   24409             :      But the only case I've seen it in so far has been array bounds, where
   24410             :      signedness is the only information lost, and I think that will be
   24411             :      okay.  VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
   24412             :      finish_id_expression_1, and are also OK.  */
   24413   326848816 :   while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
   24414        2152 :     parm = TREE_OPERAND (parm, 0);
   24415             : 
   24416   326846664 :   if (arg == error_mark_node)
   24417   110371883 :     return unify_invalid (explain_p);
   24418   326846664 :   if (arg == unknown_type_node
   24419   326846664 :       || arg == init_list_type_node)
   24420             :     /* We can't deduce anything from this, but we might get all the
   24421             :        template args from other function args.  */
   24422   150035134 :     return unify_success (explain_p);
   24423             : 
   24424   326846664 :   if (parm == any_targ_node || arg == any_targ_node)
   24425   150035134 :     return unify_success (explain_p);
   24426             : 
   24427             :   /* If PARM uses template parameters, then we can't bail out here,
   24428             :      even if ARG == PARM, since we won't record unifications for the
   24429             :      template parameters.  We might need them if we're trying to
   24430             :      figure out which of two things is more specialized.  */
   24431   326846656 :   if (arg == parm && !uses_template_parms (parm))
   24432   150035134 :     return unify_success (explain_p);
   24433             : 
   24434             :   /* Handle init lists early, so the rest of the function can assume
   24435             :      we're dealing with a type. */
   24436   320869010 :   if (BRACE_ENCLOSED_INITIALIZER_P (arg))
   24437             :     {
   24438        8097 :       tree elttype;
   24439        8097 :       tree orig_parm = parm;
   24440             : 
   24441        8097 :       if (!is_std_init_list (parm)
   24442        8097 :           && TREE_CODE (parm) != ARRAY_TYPE)
   24443             :         /* We can only deduce from an initializer list argument if the
   24444             :            parameter is std::initializer_list or an array; otherwise this
   24445             :            is a non-deduced context. */
   24446        7599 :         return unify_success (explain_p);
   24447             : 
   24448        1536 :       if (TREE_CODE (parm) == ARRAY_TYPE)
   24449          78 :         elttype = TREE_TYPE (parm);
   24450             :       else
   24451             :         {
   24452        1458 :           elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
   24453             :           /* Deduction is defined in terms of a single type, so just punt
   24454             :              on the (bizarre) std::initializer_list<T...>.  */
   24455        1458 :           if (PACK_EXPANSION_P (elttype))
   24456        7599 :             return unify_success (explain_p);
   24457             :         }
   24458             : 
   24459        1533 :       if (strict != DEDUCE_EXACT
   24460        1533 :           && TYPE_P (elttype)
   24461        3066 :           && !uses_deducible_template_parms (elttype))
   24462             :         /* If ELTTYPE has no deducible template parms, skip deduction from
   24463             :            the list elements.  */;
   24464             :       else
   24465        8059 :         for (auto &e: CONSTRUCTOR_ELTS (arg))
   24466             :           {
   24467        4063 :             tree elt = e.value;
   24468        4063 :             int elt_strict = strict;
   24469             : 
   24470        4063 :             if (elt == error_mark_node)
   24471         451 :               return unify_invalid (explain_p);
   24472             : 
   24473        4057 :             if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
   24474             :               {
   24475        3338 :                 tree type = TREE_TYPE (elt);
   24476        3338 :                 if (type == error_mark_node)
   24477           6 :                   return unify_invalid (explain_p);
   24478             :                 /* It should only be possible to get here for a call.  */
   24479        3332 :                 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
   24480        6664 :                 elt_strict |= maybe_adjust_types_for_deduction
   24481        3332 :                   (tparms, DEDUCE_CALL, &elttype, &type, elt);
   24482        3332 :                 elt = type;
   24483             :               }
   24484             : 
   24485        4051 :           RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
   24486             :                                    explain_p);
   24487             :         }
   24488             : 
   24489        1082 :       if (TREE_CODE (parm) == ARRAY_TYPE
   24490        1082 :           && deducible_array_bound (TYPE_DOMAIN (parm)))
   24491             :         {
   24492             :           /* Also deduce from the length of the initializer list.  */
   24493          94 :           tree max = size_int (CONSTRUCTOR_NELTS (arg));
   24494          47 :           tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
   24495          47 :           if (idx == error_mark_node)
   24496         451 :             return unify_invalid (explain_p);
   24497          47 :           return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
   24498          47 :                                      idx, explain_p);
   24499             :         }
   24500             : 
   24501             :       /* If the std::initializer_list<T> deduction worked, replace the
   24502             :          deduced A with std::initializer_list<A>.  */
   24503             :       if (orig_parm != parm)
   24504             :         {
   24505             :           idx = TEMPLATE_TYPE_IDX (orig_parm);
   24506             :           targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
   24507             :           targ = listify (targ);
   24508             :           TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
   24509             :         }
   24510        7599 :       return unify_success (explain_p);
   24511             :     }
   24512             : 
   24513             :   /* If parm and arg aren't the same kind of thing (template, type, or
   24514             :      expression), fail early.  */
   24515   320860913 :   if (pa_kind (parm) != pa_kind (arg))
   24516   110371883 :     return unify_invalid (explain_p);
   24517             : 
   24518             :   /* Immediately reject some pairs that won't unify because of
   24519             :      cv-qualification mismatches.  */
   24520   320860897 :   if (TREE_CODE (arg) == TREE_CODE (parm)
   24521   135762064 :       && TYPE_P (arg)
   24522             :       /* It is the elements of the array which hold the cv quals of an array
   24523             :          type, and the elements might be template type parms. We'll check
   24524             :          when we recurse.  */
   24525    83104393 :       && TREE_CODE (arg) != ARRAY_TYPE
   24526             :       /* We check the cv-qualifiers when unifying with template type
   24527             :          parameters below.  We want to allow ARG `const T' to unify with
   24528             :          PARM `T' for example, when computing which of two templates
   24529             :          is more specialized, for example.  */
   24530    83088757 :       && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
   24531   387228262 :       && !check_cv_quals_for_unify (strict_in, arg, parm))
   24532      380677 :     return unify_cv_qual_mismatch (explain_p, parm, arg);
   24533             : 
   24534   320480220 :   if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
   24535   173709019 :       && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)
   24536   435744877 :       && !FUNC_OR_METHOD_TYPE_P (parm))
   24537   114981508 :     strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
   24538             :   /* PMFs recurse at the same level, so don't strip this yet.  */
   24539   320480220 :   if (!TYPE_PTRMEMFUNC_P (parm))
   24540   320476459 :     strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
   24541   320480220 :   strict &= ~UNIFY_ALLOW_DERIVED;
   24542   320480220 :   strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
   24543   320480220 :   strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
   24544             : 
   24545   320480220 :   switch (TREE_CODE (parm))
   24546             :     {
   24547             :     case TYPENAME_TYPE:
   24548             :     case SCOPE_REF:
   24549             :     case UNBOUND_CLASS_TEMPLATE:
   24550             :       /* In a type which contains a nested-name-specifier, template
   24551             :          argument values cannot be deduced for template parameters used
   24552             :          within the nested-name-specifier.  */
   24553   150035134 :       return unify_success (explain_p);
   24554             : 
   24555   103181044 :     case TEMPLATE_TYPE_PARM:
   24556   103181044 :     case TEMPLATE_TEMPLATE_PARM:
   24557   103181044 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
   24558   103181044 :       tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
   24559   103181044 :       if (error_operand_p (tparm))
   24560   110371883 :         return unify_invalid (explain_p);
   24561             : 
   24562   206362076 :       if (TEMPLATE_TYPE_LEVEL (parm)
   24563   103181038 :           != template_decl_level (tparm))
   24564             :         /* The PARM is not one we're trying to unify.  Just check
   24565             :            to see if it matches ARG.  */
   24566             :         {
   24567          29 :           if (TREE_CODE (arg) == TREE_CODE (parm)
   24568          31 :               && (is_auto (parm) ? is_auto (arg)
   24569           2 :                   : same_type_p (parm, arg)))
   24570          18 :             return unify_success (explain_p);
   24571             :           else
   24572          11 :             return unify_type_mismatch (explain_p, parm, arg);
   24573             :         }
   24574   103181009 :       idx = TEMPLATE_TYPE_IDX (parm);
   24575   103181009 :       targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
   24576   103181009 :       tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
   24577   103181009 :       if (error_operand_p (tparm))
   24578   110371883 :         return unify_invalid (explain_p);
   24579             : 
   24580             :       /* Check for mixed types and values.  */
   24581   103181009 :       if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
   24582   102913180 :            && TREE_CODE (tparm) != TYPE_DECL)
   24583   103181009 :           || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
   24584      264906 :               && TREE_CODE (tparm) != TEMPLATE_DECL))
   24585           0 :         gcc_unreachable ();
   24586             : 
   24587   103181009 :       if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   24588             :         {
   24589        2923 :           if ((strict_in & UNIFY_ALLOW_DERIVED)
   24590        2923 :               && CLASS_TYPE_P (arg))
   24591             :             {
   24592             :               /* First try to match ARG directly.  */
   24593         264 :               tree t = try_class_unification (tparms, targs, parm, arg,
   24594         264 :                                               explain_p);
   24595         264 :               if (!t)
   24596             :                 {
   24597             :                   /* Otherwise, look for a suitable base of ARG, as below.  */
   24598          42 :                   enum template_base_result r;
   24599          42 :                   r = get_template_base (tparms, targs, parm, arg,
   24600             :                                          explain_p, &t);
   24601          42 :                   if (!t)
   24602          34 :                     return unify_no_common_base (explain_p, r, parm, arg);
   24603           8 :                   arg = t;
   24604             :                 }
   24605             :             }
   24606             :           /* ARG must be constructed from a template class or a template
   24607             :              template parameter.  */
   24608        2659 :           else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
   24609        2659 :                    && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
   24610         145 :             return unify_template_deduction_failure (explain_p, parm, arg);
   24611             : 
   24612             :           /* Deduce arguments T, i from TT<T> or TT<i>.  */
   24613        2744 :           if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
   24614             :             return 1;
   24615             : 
   24616        2697 :           arg = TYPE_TI_TEMPLATE (arg);
   24617        2697 :           if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
   24618             :             /* If the template is a template template parameter, use the
   24619             :                TEMPLATE_TEMPLATE_PARM for matching.  */
   24620          50 :             arg = TREE_TYPE (arg);
   24621             : 
   24622             :           /* Fall through to deduce template name.  */
   24623             :         }
   24624             : 
   24625   103180783 :       if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
   24626   102915877 :           || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   24627             :         {
   24628             :           /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>.  */
   24629             : 
   24630             :           /* Simple cases: Value already set, does match or doesn't.  */
   24631      267603 :           if (targ != NULL_TREE && template_args_equal (targ, arg))
   24632   150035134 :             return unify_success (explain_p);
   24633      267178 :           else if (targ)
   24634         180 :             return unify_inconsistency (explain_p, parm, targ, arg);
   24635             :         }
   24636             :       else
   24637             :         {
   24638             :           /* If PARM is `const T' and ARG is only `int', we don't have
   24639             :              a match unless we are allowing additional qualification.
   24640             :              If ARG is `const int' and PARM is just `T' that's OK;
   24641             :              that binds `const int' to `T'.  */
   24642   102913180 :           if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
   24643             :                                          arg, parm))
   24644      904296 :             return unify_cv_qual_mismatch (explain_p, parm, arg);
   24645             : 
   24646             :           /* Consider the case where ARG is `const volatile int' and
   24647             :              PARM is `const T'.  Then, T should be `volatile int'.  */
   24648   204017768 :           arg = cp_build_qualified_type
   24649   102008884 :             (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
   24650   102008884 :           if (arg == error_mark_node)
   24651   110371883 :             return unify_invalid (explain_p);
   24652             : 
   24653             :           /* Simple cases: Value already set, does match or doesn't.  */
   24654   102008884 :           if (targ != NULL_TREE && same_type_p (targ, arg))
   24655   150035134 :             return unify_success (explain_p);
   24656    85502374 :           else if (targ)
   24657      714813 :             return unify_inconsistency (explain_p, parm, targ, arg);
   24658             : 
   24659             :           /* Make sure that ARG is not a variable-sized array.  (Note
   24660             :              that were talking about variable-sized arrays (like
   24661             :              `int[n]'), rather than arrays of unknown size (like
   24662             :              `int[]').)  We'll get very confused by such a type since
   24663             :              the bound of the array is not constant, and therefore
   24664             :              not mangleable.  Besides, such types are not allowed in
   24665             :              ISO C++, so we can do as we please here.  We do allow
   24666             :              them for 'auto' deduction, since that isn't ABI-exposed.  */
   24667    84787561 :           if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
   24668          20 :             return unify_vla_arg (explain_p, arg);
   24669             : 
   24670             :           /* Strip typedefs as in convert_template_argument.  */
   24671    84787541 :           arg = canonicalize_type_argument (arg, tf_none);
   24672             :         }
   24673             : 
   24674             :       /* If ARG is a parameter pack or an expansion, we cannot unify
   24675             :          against it unless PARM is also a parameter pack.  */
   24676   170108666 :       if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
   24677    86013597 :           && !template_parameter_pack_p (parm))
   24678      476424 :         return unify_parameter_pack_mismatch (explain_p, parm, arg);
   24679             : 
   24680             :       /* If the argument deduction results is a METHOD_TYPE,
   24681             :          then there is a problem.
   24682             :          METHOD_TYPE doesn't map to any real C++ type the result of
   24683             :          the deduction cannot be of that type.  */
   24684    84578115 :       if (TREE_CODE (arg) == METHOD_TYPE)
   24685           8 :         return unify_method_type_error (explain_p, arg);
   24686             : 
   24687    84578107 :       TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
   24688    84578107 :       return unify_success (explain_p);
   24689             : 
   24690     4513133 :     case TEMPLATE_PARM_INDEX:
   24691     4513133 :       tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
   24692     4513133 :       if (error_operand_p (tparm))
   24693   110371883 :         return unify_invalid (explain_p);
   24694             : 
   24695     4513133 :       if (TEMPLATE_PARM_LEVEL (parm)
   24696     4513133 :           != template_decl_level (tparm))
   24697             :         {
   24698             :           /* The PARM is not one we're trying to unify.  Just check
   24699             :              to see if it matches ARG.  */
   24700           0 :           int result = !(TREE_CODE (arg) == TREE_CODE (parm)
   24701           0 :                          && cp_tree_equal (parm, arg));
   24702           0 :           if (result)
   24703           0 :             unify_expression_unequal (explain_p, parm, arg);
   24704           0 :           return result;
   24705             :         }
   24706             : 
   24707     4513133 :       idx = TEMPLATE_PARM_IDX (parm);
   24708     4513133 :       targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
   24709             : 
   24710     4513133 :       if (targ)
   24711             :         {
   24712        5489 :           if ((strict & UNIFY_ALLOW_INTEGER)
   24713         370 :               && TREE_TYPE (targ) && TREE_TYPE (arg)
   24714        5859 :               && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
   24715             :             /* We're deducing from an array bound, the type doesn't matter.
   24716             :                This conversion should match the one below.  */
   24717         370 :             arg = fold (build_nop (TREE_TYPE (targ), arg));
   24718        5489 :           int x = !cp_tree_equal (targ, arg);
   24719        5489 :           if (x)
   24720         433 :             unify_inconsistency (explain_p, parm, targ, arg);
   24721        5489 :           return x;
   24722             :         }
   24723             : 
   24724             :       /* [temp.deduct.type] If, in the declaration of a function template
   24725             :          with a non-type template-parameter, the non-type
   24726             :          template-parameter is used in an expression in the function
   24727             :          parameter-list and, if the corresponding template-argument is
   24728             :          deduced, the template-argument type shall match the type of the
   24729             :          template-parameter exactly, except that a template-argument
   24730             :          deduced from an array bound may be of any integral type.
   24731             :          The non-type parameter might use already deduced type parameters.  */
   24732     4507644 :       tparm = TREE_TYPE (parm);
   24733    13522904 :       if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
   24734             :         /* We don't have enough levels of args to do any substitution.  This
   24735             :            can happen in the context of -fnew-ttp-matching.  */;
   24736             :       else
   24737             :         {
   24738     4501779 :           ++processing_template_decl;
   24739     4501779 :           tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
   24740     4501779 :           --processing_template_decl;
   24741             : 
   24742     4501779 :           if (tree a = type_uses_auto (tparm))
   24743             :             {
   24744         732 :               tparm = do_auto_deduction (tparm, arg, a,
   24745             :                                          complain, adc_unify, targs,
   24746             :                                          LOOKUP_NORMAL,
   24747         366 :                                          TPARMS_PRIMARY_TEMPLATE (tparms));
   24748         366 :               if (tparm == error_mark_node)
   24749             :                 return 1;
   24750             :             }
   24751             :         }
   24752             : 
   24753     4507636 :       if (!TREE_TYPE (arg)
   24754     4507636 :           || TREE_CODE (TREE_TYPE (arg)) == DEPENDENT_OPERATOR_TYPE)
   24755             :         /* Template-parameter dependent expression.  Just accept it for now.
   24756             :            It will later be processed in convert_template_argument.  */
   24757             :         ;
   24758     4507622 :       else if (same_type_ignoring_top_level_qualifiers_p
   24759     4507622 :                (non_reference (TREE_TYPE (arg)),
   24760             :                 non_reference (tparm)))
   24761             :         /* OK.  Ignore top-level quals here because a class-type template
   24762             :            parameter object is const.  */;
   24763        9383 :       else if ((strict & UNIFY_ALLOW_INTEGER)
   24764        9183 :                && CP_INTEGRAL_TYPE_P (tparm))
   24765             :         /* Convert the ARG to the type of PARM; the deduced non-type
   24766             :            template argument must exactly match the types of the
   24767             :            corresponding parameter.  This conversion should match the
   24768             :            one above.  */
   24769        9171 :         arg = fold (build_nop (tparm, arg));
   24770         212 :       else if (uses_template_parms (tparm))
   24771             :         {
   24772             :           /* We haven't deduced the type of this parameter yet.  */
   24773         139 :           if (cxx_dialect >= cxx17
   24774             :               /* We deduce from array bounds in try_array_deduction.  */
   24775         107 :               && !(strict & UNIFY_ALLOW_INTEGER)
   24776         426 :               && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
   24777             :             {
   24778             :               /* Deduce it from the non-type argument.  As above, ignore
   24779             :                  top-level quals here too.  */
   24780          77 :               tree atype = cv_unqualified (TREE_TYPE (arg));
   24781          77 :               RECUR_AND_CHECK_FAILURE (tparms, targs,
   24782             :                                        tparm, atype,
   24783             :                                        UNIFY_ALLOW_NONE, explain_p);
   24784             :               /* Now check whether the type of this parameter is still
   24785             :                  dependent, and give up if so.  */
   24786          71 :               ++processing_template_decl;
   24787          71 :               tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
   24788          71 :               --processing_template_decl;
   24789          71 :               if (uses_template_parms (tparm))
   24790             :                 return unify_success (explain_p);
   24791             :             }
   24792             :           else
   24793             :             /* Try again later.  */
   24794   150035134 :             return unify_success (explain_p);
   24795             :         }
   24796             :       else
   24797          73 :         return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
   24798             : 
   24799             :       /* If ARG is a parameter pack or an expansion, we cannot unify
   24800             :          against it unless PARM is also a parameter pack.  */
   24801     9014898 :       if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
   24802     4558104 :           && !TEMPLATE_PARM_PARAMETER_PACK (parm))
   24803           5 :         return unify_parameter_pack_mismatch (explain_p, parm, arg);
   24804             : 
   24805     4507444 :       {
   24806     4507444 :         bool removed_attr = false;
   24807     4507444 :         arg = strip_typedefs_expr (arg, &removed_attr);
   24808             :       }
   24809     4507444 :       TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
   24810     4507444 :       return unify_success (explain_p);
   24811             : 
   24812          13 :     case PTRMEM_CST:
   24813          13 :      {
   24814             :         /* A pointer-to-member constant can be unified only with
   24815             :          another constant.  */
   24816          13 :       if (TREE_CODE (arg) != PTRMEM_CST)
   24817           3 :         return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
   24818             : 
   24819             :       /* Just unify the class member. It would be useless (and possibly
   24820             :          wrong, depending on the strict flags) to unify also
   24821             :          PTRMEM_CST_CLASS, because we want to be sure that both parm and
   24822             :          arg refer to the same variable, even if through different
   24823             :          classes. For instance:
   24824             : 
   24825             :          struct A { int x; };
   24826             :          struct B : A { };
   24827             : 
   24828             :          Unification of &A::x and &B::x must succeed.  */
   24829          10 :       return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
   24830          10 :                     PTRMEM_CST_MEMBER (arg), strict, explain_p);
   24831             :      }
   24832             : 
   24833     6299295 :     case POINTER_TYPE:
   24834     6299295 :       {
   24835     6299295 :         if (!TYPE_PTR_P (arg))
   24836     3693360 :           return unify_type_mismatch (explain_p, parm, arg);
   24837             : 
   24838             :         /* [temp.deduct.call]
   24839             : 
   24840             :            A can be another pointer or pointer to member type that can
   24841             :            be converted to the deduced A via a qualification
   24842             :            conversion (_conv.qual_).
   24843             : 
   24844             :            We pass down STRICT here rather than UNIFY_ALLOW_NONE.
   24845             :            This will allow for additional cv-qualification of the
   24846             :            pointed-to types if appropriate.  */
   24847             : 
   24848     2605935 :         if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
   24849             :           /* The derived-to-base conversion only persists through one
   24850             :              level of pointers.  */
   24851      453988 :           strict |= (strict_in & UNIFY_ALLOW_DERIVED);
   24852             : 
   24853     2605935 :         return unify (tparms, targs, TREE_TYPE (parm),
   24854     5211870 :                       TREE_TYPE (arg), strict, explain_p);
   24855             :       }
   24856             : 
   24857    17018636 :     case REFERENCE_TYPE:
   24858    17018636 :       if (!TYPE_REF_P (arg))
   24859     3027879 :         return unify_type_mismatch (explain_p, parm, arg);
   24860    13990757 :       return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
   24861    13990757 :                     strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
   24862             : 
   24863     1113498 :     case ARRAY_TYPE:
   24864     1113498 :       if (TREE_CODE (arg) != ARRAY_TYPE)
   24865     1097862 :         return unify_type_mismatch (explain_p, parm, arg);
   24866       15636 :       if ((TYPE_DOMAIN (parm) == NULL_TREE)
   24867       15636 :           != (TYPE_DOMAIN (arg) == NULL_TREE))
   24868        3645 :         return unify_type_mismatch (explain_p, parm, arg);
   24869       11991 :       RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
   24870             :                                strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
   24871       11899 :       if (TYPE_DOMAIN (parm) != NULL_TREE)
   24872       21534 :         return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
   24873       10767 :                                    TYPE_DOMAIN (arg), explain_p);
   24874             :       return unify_success (explain_p);
   24875             : 
   24876     3082314 :     case REAL_TYPE:
   24877     3082314 :     case COMPLEX_TYPE:
   24878     3082314 :     case VECTOR_TYPE:
   24879     3082314 :     case INTEGER_TYPE:
   24880     3082314 :     case BOOLEAN_TYPE:
   24881     3082314 :     case ENUMERAL_TYPE:
   24882     3082314 :     case VOID_TYPE:
   24883     3082314 :     case OPAQUE_TYPE:
   24884     3082314 :     case NULLPTR_TYPE:
   24885     3082314 :       if (TREE_CODE (arg) != TREE_CODE (parm))
   24886      970381 :         return unify_type_mismatch (explain_p, parm, arg);
   24887             : 
   24888             :       /* We have already checked cv-qualification at the top of the
   24889             :          function.  */
   24890     2111933 :       if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
   24891     1091646 :         return unify_type_mismatch (explain_p, parm, arg);
   24892             : 
   24893             :       /* As far as unification is concerned, this wins.  Later checks
   24894             :          will invalidate it if necessary.  */
   24895   150035134 :       return unify_success (explain_p);
   24896             : 
   24897             :       /* Types INTEGER_CST and MINUS_EXPR can come from array bounds.  */
   24898             :       /* Type INTEGER_CST can come from ordinary constant template args.  */
   24899             :     case INTEGER_CST:
   24900     1874402 :       while (CONVERT_EXPR_P (arg))
   24901           0 :         arg = TREE_OPERAND (arg, 0);
   24902             : 
   24903     1874402 :       if (TREE_CODE (arg) != INTEGER_CST)
   24904        1921 :         return unify_template_argument_mismatch (explain_p, parm, arg);
   24905     1872481 :       return (tree_int_cst_equal (parm, arg)
   24906     1872481 :               ? unify_success (explain_p)
   24907     1872481 :               : unify_template_argument_mismatch (explain_p, parm, arg));
   24908             : 
   24909    49700852 :     case TREE_VEC:
   24910    49700852 :       {
   24911    49700852 :         int i, len, argslen;
   24912    49700852 :         int parm_variadic_p = 0;
   24913             : 
   24914    49700852 :         if (TREE_CODE (arg) != TREE_VEC)
   24915           0 :           return unify_template_argument_mismatch (explain_p, parm, arg);
   24916             : 
   24917    49700852 :         len = TREE_VEC_LENGTH (parm);
   24918    49700852 :         argslen = TREE_VEC_LENGTH (arg);
   24919             : 
   24920             :         /* Check for pack expansions in the parameters.  */
   24921   141680936 :         for (i = 0; i < len; ++i)
   24922             :           {
   24923    91980097 :             if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
   24924             :               {
   24925     2740424 :                 if (i == len - 1)
   24926             :                   /* We can unify against something with a trailing
   24927             :                      parameter pack.  */
   24928             :                   parm_variadic_p = 1;
   24929             :                 else
   24930             :                   /* [temp.deduct.type]/9: If the template argument list of
   24931             :                      P contains a pack expansion that is not the last
   24932             :                      template argument, the entire template argument list
   24933             :                      is a non-deduced context.  */
   24934   150035134 :                   return unify_success (explain_p);
   24935             :               }
   24936             :           }
   24937             : 
   24938             :         /* If we don't have enough arguments to satisfy the parameters
   24939             :            (not counting the pack expression at the end), or we have
   24940             :            too many arguments for a parameter list that doesn't end in
   24941             :            a pack expression, we can't unify.  */
   24942    49700839 :         if (parm_variadic_p
   24943    49700839 :             ? argslen < len - parm_variadic_p
   24944             :             : argslen != len)
   24945      536935 :           return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
   24946             : 
   24947             :         /* Unify all of the parameters that precede the (optional)
   24948             :            pack expression.  */
   24949   123719439 :         for (i = 0; i < len - parm_variadic_p; ++i)
   24950             :           {
   24951    84386952 :             RECUR_AND_CHECK_FAILURE (tparms, targs,
   24952             :                                      TREE_VEC_ELT (parm, i),
   24953             :                                      TREE_VEC_ELT (arg, i),
   24954             :                                      UNIFY_ALLOW_NONE, explain_p);
   24955             :           }
   24956    39332487 :         if (parm_variadic_p)
   24957     2706777 :           return unify_pack_expansion (tparms, targs, parm, arg,
   24958             :                                        DEDUCE_EXACT,
   24959     2706777 :                                        /*subr=*/true, explain_p);
   24960   150035134 :         return unify_success (explain_p);
   24961             :       }
   24962             : 
   24963   129162644 :     case RECORD_TYPE:
   24964   129162644 :     case UNION_TYPE:
   24965   129162644 :       if (TREE_CODE (arg) != TREE_CODE (parm))
   24966    85643109 :         return unify_type_mismatch (explain_p, parm, arg);
   24967             : 
   24968    43519535 :       if (TYPE_PTRMEMFUNC_P (parm))
   24969             :         {
   24970        3415 :           if (!TYPE_PTRMEMFUNC_P (arg))
   24971         270 :             return unify_type_mismatch (explain_p, parm, arg);
   24972             : 
   24973        9435 :           return unify (tparms, targs,
   24974        3145 :                         TYPE_PTRMEMFUNC_FN_TYPE (parm),
   24975        3145 :                         TYPE_PTRMEMFUNC_FN_TYPE (arg),
   24976        3145 :                         strict, explain_p);
   24977             :         }
   24978    43516120 :       else if (TYPE_PTRMEMFUNC_P (arg))
   24979        1258 :         return unify_type_mismatch (explain_p, parm, arg);
   24980             : 
   24981    43514862 :       if (CLASSTYPE_TEMPLATE_INFO (parm))
   24982             :         {
   24983    42278888 :           tree t = NULL_TREE;
   24984             : 
   24985    42278888 :           if (strict_in & UNIFY_ALLOW_DERIVED)
   24986             :             {
   24987             :               /* First, we try to unify the PARM and ARG directly.  */
   24988    27295869 :               t = try_class_unification (tparms, targs,
   24989             :                                          parm, arg, explain_p);
   24990             : 
   24991    27295869 :               if (!t)
   24992             :                 {
   24993             :                   /* Fallback to the special case allowed in
   24994             :                      [temp.deduct.call]:
   24995             : 
   24996             :                        If P is a class, and P has the form
   24997             :                        template-id, then A can be a derived class of
   24998             :                        the deduced A.  Likewise, if P is a pointer to
   24999             :                        a class of the form template-id, A can be a
   25000             :                        pointer to a derived class pointed to by the
   25001             :                        deduced A.  */
   25002    23360547 :                   enum template_base_result r;
   25003    23360547 :                   r = get_template_base (tparms, targs, parm, arg,
   25004             :                                          explain_p, &t);
   25005             : 
   25006    23360547 :                   if (!t)
   25007             :                     {
   25008             :                       /* Don't give the derived diagnostic if we're
   25009             :                          already dealing with the same template.  */
   25010    22975103 :                       bool same_template
   25011    22975103 :                         = (CLASSTYPE_TEMPLATE_INFO (arg)
   25012    22975103 :                            && (CLASSTYPE_TI_TEMPLATE (parm)
   25013    12583274 :                                == CLASSTYPE_TI_TEMPLATE (arg)));
   25014    22975103 :                       return unify_no_common_base (explain_p && !same_template,
   25015    22975103 :                                                    r, parm, arg);
   25016             :                     }
   25017             :                 }
   25018             :             }
   25019    14983019 :           else if (CLASSTYPE_TEMPLATE_INFO (arg)
   25020    14983019 :                    && (CLASSTYPE_TI_TEMPLATE (parm)
   25021    14358270 :                        == CLASSTYPE_TI_TEMPLATE (arg)))
   25022             :             /* Perhaps PARM is something like S<U> and ARG is S<int>.
   25023             :                Then, we should unify `int' and `U'.  */
   25024    13735886 :             t = arg;
   25025             :           else
   25026             :             /* There's no chance of unification succeeding.  */
   25027    43526021 :             return unify_type_mismatch (explain_p, parm, arg);
   25028             : 
   25029    18056652 :           return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
   25030    36113304 :                         CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
   25031             :         }
   25032     1235974 :       else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
   25033     1235970 :         return unify_type_mismatch (explain_p, parm, arg);
   25034   150035134 :       return unify_success (explain_p);
   25035             : 
   25036      283153 :     case METHOD_TYPE:
   25037      283153 :     case FUNCTION_TYPE:
   25038      283153 :       {
   25039      283153 :         unsigned int nargs;
   25040      283153 :         tree *args;
   25041      283153 :         tree a;
   25042      283153 :         unsigned int i;
   25043             : 
   25044      283153 :         if (TREE_CODE (arg) != TREE_CODE (parm))
   25045        2345 :           return unify_type_mismatch (explain_p, parm, arg);
   25046             : 
   25047             :         /* CV qualifications for methods can never be deduced, they must
   25048             :            match exactly.  We need to check them explicitly here,
   25049             :            because type_unification_real treats them as any other
   25050             :            cv-qualified parameter.  */
   25051      280808 :         if (TREE_CODE (parm) == METHOD_TYPE
   25052      283953 :             && (!check_cv_quals_for_unify
   25053        3145 :                 (UNIFY_ALLOW_NONE,
   25054             :                  class_of_this_parm (arg),
   25055             :                  class_of_this_parm (parm))))
   25056        2066 :           return unify_cv_qual_mismatch (explain_p, parm, arg);
   25057      278742 :         if (TREE_CODE (arg) == FUNCTION_TYPE
   25058      278742 :             && type_memfn_quals (parm) != type_memfn_quals (arg))
   25059         300 :           return unify_cv_qual_mismatch (explain_p, parm, arg);
   25060      278442 :         if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
   25061         440 :           return unify_type_mismatch (explain_p, parm, arg);
   25062             : 
   25063      278002 :         RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
   25064             :                                  TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
   25065             : 
   25066      277458 :         nargs = list_length (TYPE_ARG_TYPES (arg));
   25067      277458 :         args = XALLOCAVEC (tree, nargs);
   25068      277458 :         for (a = TYPE_ARG_TYPES (arg), i = 0;
   25069     1072159 :              a != NULL_TREE && a != void_list_node;
   25070      794701 :              a = TREE_CHAIN (a), ++i)
   25071      794701 :           args[i] = TREE_VALUE (a);
   25072      277458 :         nargs = i;
   25073             : 
   25074      277458 :         if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
   25075             :                                    args, nargs, 1, DEDUCE_EXACT,
   25076             :                                    NULL, explain_p))
   25077             :           return 1;
   25078             : 
   25079      277253 :         if (flag_noexcept_type)
   25080             :           {
   25081      268067 :             tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
   25082      268067 :             tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
   25083      268067 :             if (pspec == NULL_TREE) pspec = noexcept_false_spec;
   25084      268067 :             if (aspec == NULL_TREE) aspec = noexcept_false_spec;
   25085      536134 :             if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
   25086      536134 :                 && uses_template_parms (TREE_PURPOSE (pspec)))
   25087         121 :               RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
   25088             :                                        TREE_PURPOSE (aspec),
   25089             :                                        UNIFY_ALLOW_NONE, explain_p);
   25090             :             else
   25091             :               {
   25092      267946 :                 bool pn = nothrow_spec_p (pspec);
   25093      267946 :                 bool an = nothrow_spec_p (aspec);
   25094             :                 /* Here "less cv-qual" means the deduced arg (i.e. parm) has
   25095             :                    /more/ noexcept, since function pointer conversions are the
   25096             :                    reverse of qualification conversions.  */
   25097      267946 :                 if (an == pn
   25098      232516 :                     || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL))
   25099      232510 :                     || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL)))
   25100             :                   /* OK.  */;
   25101             :                 else
   25102          98 :                   return unify_type_mismatch (explain_p, parm, arg);
   25103             :               }
   25104             :           }
   25105      277155 :         if (flag_tm)
   25106             :           {
   25107             :             /* As for noexcept.  */
   25108          90 :             bool pn = tx_safe_fn_type_p (parm);
   25109          90 :             bool an = tx_safe_fn_type_p (arg);
   25110          90 :             if (an == pn
   25111           5 :                 || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL))
   25112           5 :                 || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL)))
   25113             :               /* OK.  */;
   25114             :             else
   25115           1 :               return unify_type_mismatch (explain_p, parm, arg);
   25116             :           }
   25117             : 
   25118             :         return 0;
   25119             :       }
   25120             : 
   25121      173419 :     case OFFSET_TYPE:
   25122             :       /* Unify a pointer to member with a pointer to member function, which
   25123             :          deduces the type of the member as a function type. */
   25124      173419 :       if (TYPE_PTRMEMFUNC_P (arg))
   25125             :         {
   25126             :           /* Check top-level cv qualifiers */
   25127       41795 :           if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
   25128          24 :             return unify_cv_qual_mismatch (explain_p, parm, arg);
   25129             : 
   25130       41771 :           RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
   25131             :                                    TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
   25132             :                                    UNIFY_ALLOW_NONE, explain_p);
   25133             : 
   25134             :           /* Determine the type of the function we are unifying against. */
   25135       41771 :           tree fntype = static_fn_type (arg);
   25136             : 
   25137       41771 :           return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
   25138             :         }
   25139             : 
   25140      131624 :       if (TREE_CODE (arg) != OFFSET_TYPE)
   25141      130563 :         return unify_type_mismatch (explain_p, parm, arg);
   25142        1061 :       RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
   25143             :                                TYPE_OFFSET_BASETYPE (arg),
   25144             :                                UNIFY_ALLOW_NONE, explain_p);
   25145        1061 :       return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
   25146        1061 :                     strict, explain_p);
   25147             : 
   25148           0 :     case CONST_DECL:
   25149           0 :       if (DECL_TEMPLATE_PARM_P (parm))
   25150           0 :         return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
   25151           0 :       if (arg != scalar_constant_value (parm))
   25152           0 :         return unify_template_argument_mismatch (explain_p, parm, arg);
   25153   150035134 :       return unify_success (explain_p);
   25154             : 
   25155        2230 :     case FIELD_DECL:
   25156        2230 :     case TEMPLATE_DECL:
   25157             :       /* Matched cases are handled by the ARG == PARM test above.  */
   25158        2230 :       return unify_template_argument_mismatch (explain_p, parm, arg);
   25159             : 
   25160           8 :     case VAR_DECL:
   25161             :       /* We might get a variable as a non-type template argument in parm if the
   25162             :          corresponding parameter is type-dependent.  Make any necessary
   25163             :          adjustments based on whether arg is a reference.  */
   25164           8 :       if (CONSTANT_CLASS_P (arg))
   25165           0 :         parm = fold_non_dependent_expr (parm, complain);
   25166           8 :       else if (REFERENCE_REF_P (arg))
   25167             :         {
   25168           0 :           tree sub = TREE_OPERAND (arg, 0);
   25169           0 :           STRIP_NOPS (sub);
   25170           0 :           if (TREE_CODE (sub) == ADDR_EXPR)
   25171           0 :             arg = TREE_OPERAND (sub, 0);
   25172             :         }
   25173             :       /* Now use the normal expression code to check whether they match.  */
   25174           8 :       goto expr;
   25175             : 
   25176     3534924 :     case TYPE_ARGUMENT_PACK:
   25177     3534924 :     case NONTYPE_ARGUMENT_PACK:
   25178     3534924 :       return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
   25179     7069848 :                     ARGUMENT_PACK_ARGS (arg), strict, explain_p);
   25180             : 
   25181             :     case TYPEOF_TYPE:
   25182             :     case DECLTYPE_TYPE:
   25183             :     case TRAIT_TYPE:
   25184             :       /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
   25185             :          or TRAIT_TYPE nodes.  */
   25186   150035134 :       return unify_success (explain_p);
   25187             : 
   25188             :     case ERROR_MARK:
   25189             :       /* Unification fails if we hit an error node.  */
   25190   110371883 :       return unify_invalid (explain_p);
   25191             : 
   25192          64 :     case INDIRECT_REF:
   25193          64 :       if (REFERENCE_REF_P (parm))
   25194             :         {
   25195          64 :           bool pexp = PACK_EXPANSION_P (arg);
   25196          64 :           if (pexp)
   25197           3 :             arg = PACK_EXPANSION_PATTERN (arg);
   25198          64 :           if (REFERENCE_REF_P (arg))
   25199          64 :             arg = TREE_OPERAND (arg, 0);
   25200          64 :           if (pexp)
   25201           3 :             arg = make_pack_expansion (arg, complain);
   25202          64 :           return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
   25203          64 :                         strict, explain_p);
   25204             :         }
   25205             :       /* FALLTHRU */
   25206             : 
   25207         398 :     default:
   25208             :       /* An unresolved overload is a nondeduced context.  */
   25209         398 :       if (is_overloaded_fn (parm) || type_unknown_p (parm))
   25210   150035134 :         return unify_success (explain_p);
   25211         395 :       gcc_assert (EXPR_P (parm)
   25212             :                   || TREE_CODE (parm) == CONSTRUCTOR
   25213             :                   || TREE_CODE (parm) == TRAIT_EXPR);
   25214         395 :     expr:
   25215             :       /* We must be looking at an expression.  This can happen with
   25216             :          something like:
   25217             : 
   25218             :            template <int I>
   25219             :            void foo(S<I>, S<I + 2>);
   25220             : 
   25221             :          or
   25222             : 
   25223             :            template<typename T>
   25224             :            void foo(A<T, T{}>);
   25225             : 
   25226             :          This is a "non-deduced context":
   25227             : 
   25228             :            [deduct.type]
   25229             : 
   25230             :            The non-deduced contexts are:
   25231             : 
   25232             :            --A non-type template argument or an array bound in which
   25233             :              a subexpression references a template parameter.
   25234             : 
   25235             :          In these cases, we assume deduction succeeded, but don't
   25236             :          actually infer any unifications.  */
   25237             : 
   25238         403 :       if (!uses_template_parms (parm)
   25239         403 :           && !template_args_equal (parm, arg))
   25240          13 :         return unify_expression_unequal (explain_p, parm, arg);
   25241             :       else
   25242         390 :         return unify_success (explain_p);
   25243             :     }
   25244             : }
   25245             : #undef RECUR_AND_CHECK_FAILURE
   25246             : 
   25247             : /* Note that DECL can be defined in this translation unit, if
   25248             :    required.  */
   25249             : 
   25250             : static void
   25251     9507724 : mark_definable (tree decl)
   25252             : {
   25253     9507724 :   tree clone;
   25254     9507724 :   DECL_NOT_REALLY_EXTERN (decl) = 1;
   25255    12799361 :   FOR_EACH_CLONE (clone, decl)
   25256     3291637 :     DECL_NOT_REALLY_EXTERN (clone) = 1;
   25257     9507724 : }
   25258             : 
   25259             : /* Called if RESULT is explicitly instantiated, or is a member of an
   25260             :    explicitly instantiated class.  */
   25261             : 
   25262             : void
   25263    16635178 : mark_decl_instantiated (tree result, int extern_p)
   25264             : {
   25265    16635178 :   SET_DECL_EXPLICIT_INSTANTIATION (result);
   25266             : 
   25267             :   /* If this entity has already been written out, it's too late to
   25268             :      make any modifications.  */
   25269    16635178 :   if (TREE_ASM_WRITTEN (result))
   25270             :     return;
   25271             : 
   25272             :   /* consteval functions are never emitted.  */
   25273    16635178 :   if (TREE_CODE (result) == FUNCTION_DECL
   25274    33016880 :       && DECL_IMMEDIATE_FUNCTION_P (result))
   25275             :     return;
   25276             : 
   25277             :   /* For anonymous namespace we don't need to do anything.  */
   25278    16635176 :   if (decl_internal_context_p (result))
   25279             :     {
   25280         958 :       gcc_assert (!TREE_PUBLIC (result));
   25281             :       return;
   25282             :     }
   25283             : 
   25284    16634218 :   if (TREE_CODE (result) != FUNCTION_DECL)
   25285             :     /* The TREE_PUBLIC flag for function declarations will have been
   25286             :        set correctly by tsubst.  */
   25287      253466 :     TREE_PUBLIC (result) = 1;
   25288             : 
   25289    16634218 :   if (extern_p)
   25290             :     {
   25291    16603544 :       DECL_EXTERNAL (result) = 1;
   25292    16603544 :       DECL_NOT_REALLY_EXTERN (result) = 0;
   25293             :     }
   25294             :   else
   25295             :     {
   25296       30674 :       mark_definable (result);
   25297       30674 :       mark_needed (result);
   25298             :       /* Always make artificials weak.  */
   25299       30674 :       if (DECL_ARTIFICIAL (result) && flag_weak)
   25300           0 :         comdat_linkage (result);
   25301             :       /* For WIN32 we also want to put explicit instantiations in
   25302             :          linkonce sections.  */
   25303       30674 :       else if (TREE_PUBLIC (result))
   25304       30654 :         maybe_make_one_only (result);
   25305       30674 :       if (TREE_CODE (result) == FUNCTION_DECL
   25306       30674 :           && DECL_TEMPLATE_INSTANTIATED (result))
   25307             :         /* If the function has already been instantiated, clear DECL_EXTERNAL,
   25308             :            since start_preparsed_function wouldn't have if we had an earlier
   25309             :            extern explicit instantiation.  */
   25310           1 :         DECL_EXTERNAL (result) = 0;
   25311             :     }
   25312             : 
   25313             :   /* If EXTERN_P, then this function will not be emitted -- unless
   25314             :      followed by an explicit instantiation, at which point its linkage
   25315             :      will be adjusted.  If !EXTERN_P, then this function will be
   25316             :      emitted here.  In neither circumstance do we want
   25317             :      import_export_decl to adjust the linkage.  */
   25318    16634218 :   DECL_INTERFACE_KNOWN (result) = 1;
   25319             : }
   25320             : 
   25321             : /* Subroutine of more_specialized_fn: check whether TARGS is missing any
   25322             :    important template arguments.  If any are missing, we check whether
   25323             :    they're important by using error_mark_node for substituting into any
   25324             :    args that were used for partial ordering (the ones between ARGS and END)
   25325             :    and seeing if it bubbles up.  */
   25326             : 
   25327             : static bool
   25328     1455312 : check_undeduced_parms (tree targs, tree args, tree end)
   25329             : {
   25330     1455312 :   bool found = false;
   25331     4472970 :   for (tree& targ : tree_vec_range (targs))
   25332     3017658 :     if (targ == NULL_TREE)
   25333             :       {
   25334     1279264 :         found = true;
   25335     1279264 :         targ = error_mark_node;
   25336             :       }
   25337     1455312 :   if (found)
   25338             :     {
   25339     1276930 :       tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
   25340     1276930 :       if (substed == error_mark_node)
   25341             :         return true;
   25342             :     }
   25343             :   return false;
   25344             : }
   25345             : 
   25346             : /* Given two function templates PAT1 and PAT2, return:
   25347             : 
   25348             :    1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
   25349             :    -1 if PAT2 is more specialized than PAT1.
   25350             :    0 if neither is more specialized.
   25351             : 
   25352             :    LEN indicates the number of parameters we should consider
   25353             :    (defaulted parameters should not be considered).
   25354             : 
   25355             :    The 1998 std underspecified function template partial ordering, and
   25356             :    DR214 addresses the issue.  We take pairs of arguments, one from
   25357             :    each of the templates, and deduce them against each other.  One of
   25358             :    the templates will be more specialized if all the *other*
   25359             :    template's arguments deduce against its arguments and at least one
   25360             :    of its arguments *does* *not* deduce against the other template's
   25361             :    corresponding argument.  Deduction is done as for class templates.
   25362             :    The arguments used in deduction have reference and top level cv
   25363             :    qualifiers removed.  Iff both arguments were originally reference
   25364             :    types *and* deduction succeeds in both directions, an lvalue reference
   25365             :    wins against an rvalue reference and otherwise the template
   25366             :    with the more cv-qualified argument wins for that pairing (if
   25367             :    neither is more cv-qualified, they both are equal).  Unlike regular
   25368             :    deduction, after all the arguments have been deduced in this way,
   25369             :    we do *not* verify the deduced template argument values can be
   25370             :    substituted into non-deduced contexts.
   25371             : 
   25372             :    The logic can be a bit confusing here, because we look at deduce1 and
   25373             :    targs1 to see if pat2 is at least as specialized, and vice versa; if we
   25374             :    can find template arguments for pat1 to make arg1 look like arg2, that
   25375             :    means that arg2 is at least as specialized as arg1.  */
   25376             : 
   25377             : int
   25378     1429036 : more_specialized_fn (tree pat1, tree pat2, int len)
   25379             : {
   25380     1429036 :   tree decl1 = DECL_TEMPLATE_RESULT (pat1);
   25381     1429036 :   tree decl2 = DECL_TEMPLATE_RESULT (pat2);
   25382     1429036 :   tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
   25383     1429036 :   tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
   25384     1429036 :   tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
   25385     1429036 :   tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
   25386     1429036 :   tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
   25387     1429036 :   tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
   25388     1429036 :   tree origs1, origs2;
   25389     1429036 :   bool lose1 = false;
   25390     1429036 :   bool lose2 = false;
   25391             : 
   25392             :   /* Remove the this parameter from non-static member functions.  If
   25393             :      one is a non-static member function and the other is not a static
   25394             :      member function, remove the first parameter from that function
   25395             :      also.  This situation occurs for operator functions where we
   25396             :      locate both a member function (with this pointer) and non-member
   25397             :      operator (with explicit first operand).  */
   25398     1429036 :   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
   25399             :     {
   25400        4016 :       len--; /* LEN is the number of significant arguments for DECL1 */
   25401        4016 :       args1 = TREE_CHAIN (args1);
   25402        4016 :       if (!DECL_STATIC_FUNCTION_P (decl2))
   25403        4006 :         args2 = TREE_CHAIN (args2);
   25404             :     }
   25405     1425020 :   else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
   25406             :     {
   25407           0 :       args2 = TREE_CHAIN (args2);
   25408           0 :       if (!DECL_STATIC_FUNCTION_P (decl1))
   25409             :         {
   25410           0 :           len--;
   25411           0 :           args1 = TREE_CHAIN (args1);
   25412             :         }
   25413             :     }
   25414             : 
   25415             :   /* If only one is a conversion operator, they are unordered.  */
   25416     1429036 :   if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
   25417             :     return 0;
   25418             : 
   25419             :   /* Consider the return type for a conversion function */
   25420     1429020 :   if (DECL_CONV_FN_P (decl1))
   25421             :     {
   25422          12 :       args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
   25423          12 :       args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
   25424          12 :       len++;
   25425             :     }
   25426             : 
   25427     1429020 :   processing_template_decl++;
   25428             : 
   25429     1429020 :   origs1 = args1;
   25430     1429020 :   origs2 = args2;
   25431             : 
   25432     1429020 :   while (len--
   25433             :          /* Stop when an ellipsis is seen.  */
   25434     3010451 :          && args1 != NULL_TREE && args2 != NULL_TREE)
   25435             :     {
   25436     1585681 :       tree arg1 = TREE_VALUE (args1);
   25437     1585681 :       tree arg2 = TREE_VALUE (args2);
   25438     1585681 :       int deduce1, deduce2;
   25439     1585681 :       int quals1 = -1;
   25440     1585681 :       int quals2 = -1;
   25441     1585681 :       int ref1 = 0;
   25442     1585681 :       int ref2 = 0;
   25443             : 
   25444     1585681 :       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
   25445        1262 :           && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
   25446             :         {
   25447             :           /* When both arguments are pack expansions, we need only
   25448             :              unify the patterns themselves.  */
   25449         209 :           arg1 = PACK_EXPANSION_PATTERN (arg1);
   25450         209 :           arg2 = PACK_EXPANSION_PATTERN (arg2);
   25451             : 
   25452             :           /* This is the last comparison we need to do.  */
   25453             :           len = 0;
   25454             :         }
   25455             : 
   25456     1585681 :       if (TYPE_REF_P (arg1))
   25457             :         {
   25458      205763 :           ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
   25459      205763 :           arg1 = TREE_TYPE (arg1);
   25460      205763 :           quals1 = cp_type_quals (arg1);
   25461             :         }
   25462             : 
   25463     1585681 :       if (TYPE_REF_P (arg2))
   25464             :         {
   25465      229111 :           ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
   25466      229111 :           arg2 = TREE_TYPE (arg2);
   25467      229111 :           quals2 = cp_type_quals (arg2);
   25468             :         }
   25469             : 
   25470     1585681 :       arg1 = TYPE_MAIN_VARIANT (arg1);
   25471     1585681 :       arg2 = TYPE_MAIN_VARIANT (arg2);
   25472             : 
   25473     1585681 :       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
   25474             :         {
   25475        1053 :           int i, len2 = remaining_arguments (args2);
   25476        1053 :           tree parmvec = make_tree_vec (1);
   25477        1053 :           tree argvec = make_tree_vec (len2);
   25478        1053 :           tree ta = args2;
   25479             : 
   25480             :           /* Setup the parameter vector, which contains only ARG1.  */
   25481        1053 :           TREE_VEC_ELT (parmvec, 0) = arg1;
   25482             : 
   25483             :           /* Setup the argument vector, which contains the remaining
   25484             :              arguments.  */
   25485        2436 :           for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
   25486        1383 :             TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
   25487             : 
   25488        1053 :           deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
   25489             :                                            argvec, DEDUCE_EXACT,
   25490             :                                            /*subr=*/true, /*explain_p=*/false)
   25491        1053 :                      == 0);
   25492             : 
   25493             :           /* We cannot deduce in the other direction, because ARG1 is
   25494             :              a pack expansion but ARG2 is not.  */
   25495        1053 :           deduce2 = 0;
   25496             :         }
   25497     1584628 :       else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
   25498             :         {
   25499         270 :           int i, len1 = remaining_arguments (args1);
   25500         270 :           tree parmvec = make_tree_vec (1);
   25501         270 :           tree argvec = make_tree_vec (len1);
   25502         270 :           tree ta = args1;
   25503             : 
   25504             :           /* Setup the parameter vector, which contains only ARG1.  */
   25505         270 :           TREE_VEC_ELT (parmvec, 0) = arg2;
   25506             : 
   25507             :           /* Setup the argument vector, which contains the remaining
   25508             :              arguments.  */
   25509         634 :           for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
   25510         364 :             TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
   25511             : 
   25512         270 :           deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
   25513             :                                            argvec, DEDUCE_EXACT,
   25514             :                                            /*subr=*/true, /*explain_p=*/false)
   25515         270 :                      == 0);
   25516             : 
   25517             :           /* We cannot deduce in the other direction, because ARG2 is
   25518             :              a pack expansion but ARG1 is not.*/
   25519         270 :           deduce1 = 0;
   25520             :         }
   25521             : 
   25522             :       else
   25523             :         {
   25524             :           /* The normal case, where neither argument is a pack
   25525             :              expansion.  */
   25526     1584358 :           deduce1 = (unify (tparms1, targs1, arg1, arg2,
   25527             :                             UNIFY_ALLOW_NONE, /*explain_p=*/false)
   25528     1584358 :                      == 0);
   25529     1584358 :           deduce2 = (unify (tparms2, targs2, arg2, arg1,
   25530             :                             UNIFY_ALLOW_NONE, /*explain_p=*/false)
   25531     1584358 :                      == 0);
   25532             :         }
   25533             : 
   25534             :       /* If we couldn't deduce arguments for tparms1 to make arg1 match
   25535             :          arg2, then arg2 is not as specialized as arg1.  */
   25536     1585681 :       if (!deduce1)
   25537             :         lose2 = true;
   25538     1585681 :       if (!deduce2)
   25539       85663 :         lose1 = true;
   25540             : 
   25541             :       /* "If, for a given type, deduction succeeds in both directions
   25542             :          (i.e., the types are identical after the transformations above)
   25543             :          and both P and A were reference types (before being replaced with
   25544             :          the type referred to above):
   25545             :          - if the type from the argument template was an lvalue reference and
   25546             :          the type from the parameter template was not, the argument type is
   25547             :          considered to be more specialized than the other; otherwise,
   25548             :          - if the type from the argument template is more cv-qualified
   25549             :          than the type from the parameter template (as described above),
   25550             :          the argument type is considered to be more specialized than the other;
   25551             :          otherwise,
   25552             :          - neither type is more specialized than the other."  */
   25553             : 
   25554     1585681 :       if (deduce1 && deduce2)
   25555             :         {
   25556      179658 :           if (ref1 && ref2 && ref1 != ref2)
   25557             :             {
   25558           3 :               if (ref1 > ref2)
   25559             :                 lose1 = true;
   25560             :               else
   25561           3 :                 lose2 = true;
   25562             :             }
   25563      179655 :           else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
   25564             :             {
   25565        1146 :               if ((quals1 & quals2) == quals2)
   25566           8 :                 lose2 = true;
   25567        1146 :               if ((quals1 & quals2) == quals1)
   25568        1138 :                 lose1 = true;
   25569             :             }
   25570             :         }
   25571             : 
   25572     1585681 :       if (lose1 && lose2)
   25573             :         /* We've failed to deduce something in either direction.
   25574             :            These must be unordered.  */
   25575             :         break;
   25576             : 
   25577     1581431 :       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
   25578     1580471 :           || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
   25579             :         /* We have already processed all of the arguments in our
   25580             :            handing of the pack expansion type.  */
   25581        1174 :         len = 0;
   25582             : 
   25583     1581431 :       args1 = TREE_CHAIN (args1);
   25584     1581431 :       args2 = TREE_CHAIN (args2);
   25585             :     }
   25586             : 
   25587             :   /* "In most cases, all template parameters must have values in order for
   25588             :      deduction to succeed, but for partial ordering purposes a template
   25589             :      parameter may remain without a value provided it is not used in the
   25590             :      types being used for partial ordering."
   25591             : 
   25592             :      Thus, if we are missing any of the targs1 we need to substitute into
   25593             :      origs1, then pat2 is not as specialized as pat1.  This can happen when
   25594             :      there is a nondeduced context.  */
   25595     1429020 :   if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
   25596             :     lose2 = true;
   25597     1429020 :   if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
   25598             :     lose1 = true;
   25599             : 
   25600     1429020 :   processing_template_decl--;
   25601             : 
   25602             :   /* If both deductions succeed, the partial ordering selects the more
   25603             :      constrained template.  */
   25604             :   /* P2113: If the corresponding template-parameters of the
   25605             :      template-parameter-lists are not equivalent ([temp.over.link]) or if
   25606             :      the function parameters that positionally correspond between the two
   25607             :      templates are not of the same type, neither template is more
   25608             :      specialized than the other.  */
   25609     1429020 :   if (!lose1 && !lose2
   25610       30530 :       && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
   25611       30530 :                               DECL_TEMPLATE_PARMS (pat2))
   25612     1459308 :       && compparms (origs1, origs2))
   25613             :     {
   25614       29587 :       int winner = more_constrained (decl1, decl2);
   25615       29587 :       if (winner > 0)
   25616             :         lose2 = true;
   25617       22656 :       else if (winner < 0)
   25618          43 :         lose1 = true;
   25619             :     }
   25620             : 
   25621             :   /* All things being equal, if the next argument is a pack expansion
   25622             :      for one function but not for the other, prefer the
   25623             :      non-variadic function.  FIXME this is bogus; see c++/41958.  */
   25624     1429020 :   if (lose1 == lose2
   25625       27803 :       && args1 && TREE_VALUE (args1)
   25626     1484621 :       && args2 && TREE_VALUE (args2))
   25627             :     {
   25628       27798 :       lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
   25629       27798 :       lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
   25630             :     }
   25631             : 
   25632     1429020 :   if (lose1 == lose2)
   25633             :     return 0;
   25634     1401419 :   else if (!lose1)
   25635             :     return 1;
   25636             :   else
   25637       78478 :     return -1;
   25638             : }
   25639             : 
   25640             : /* Determine which of two partial specializations of TMPL is more
   25641             :    specialized.
   25642             : 
   25643             :    PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
   25644             :    to the first partial specialization.  The TREE_PURPOSE is the
   25645             :    innermost set of template parameters for the partial
   25646             :    specialization.  PAT2 is similar, but for the second template.
   25647             : 
   25648             :    Return 1 if the first partial specialization is more specialized;
   25649             :    -1 if the second is more specialized; 0 if neither is more
   25650             :    specialized.
   25651             : 
   25652             :    See [temp.class.order] for information about determining which of
   25653             :    two templates is more specialized.  */
   25654             : 
   25655             : static int
   25656      246396 : more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
   25657             : {
   25658      246396 :   tree targs;
   25659      246396 :   int winner = 0;
   25660      246396 :   bool any_deductions = false;
   25661             : 
   25662      246396 :   tree tmpl1 = TREE_VALUE (pat1);
   25663      246396 :   tree tmpl2 = TREE_VALUE (pat2);
   25664      246396 :   tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
   25665      246396 :   tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
   25666             : 
   25667             :   /* Just like what happens for functions, if we are ordering between
   25668             :      different template specializations, we may encounter dependent
   25669             :      types in the arguments, and we need our dependency check functions
   25670             :      to behave correctly.  */
   25671      246396 :   ++processing_template_decl;
   25672      246396 :   targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
   25673      246396 :   if (targs)
   25674             :     {
   25675       87016 :       --winner;
   25676       87016 :       any_deductions = true;
   25677             :     }
   25678             : 
   25679      246396 :   targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
   25680      246396 :   if (targs)
   25681             :     {
   25682      160030 :       ++winner;
   25683      160030 :       any_deductions = true;
   25684             :     }
   25685      246396 :   --processing_template_decl;
   25686             : 
   25687             :   /* If both deductions succeed, the partial ordering selects the more
   25688             :      constrained template.  */
   25689      246396 :   if (!winner && any_deductions)
   25690         731 :     winner = more_constrained (tmpl1, tmpl2);
   25691             : 
   25692             :   /* In the case of a tie where at least one of the templates
   25693             :      has a parameter pack at the end, the template with the most
   25694             :      non-packed parameters wins.  */
   25695      246396 :   if (winner == 0
   25696      246396 :       && any_deductions
   25697      246398 :       && (template_args_variadic_p (TREE_PURPOSE (pat1))
   25698           2 :           || template_args_variadic_p (TREE_PURPOSE (pat2))))
   25699             :     {
   25700           0 :       tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
   25701           0 :       tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
   25702           0 :       int len1 = TREE_VEC_LENGTH (args1);
   25703           0 :       int len2 = TREE_VEC_LENGTH (args2);
   25704             : 
   25705             :       /* We don't count the pack expansion at the end.  */
   25706           0 :       if (template_args_variadic_p (TREE_PURPOSE (pat1)))
   25707           0 :         --len1;
   25708           0 :       if (template_args_variadic_p (TREE_PURPOSE (pat2)))
   25709           0 :         --len2;
   25710             : 
   25711           0 :       if (len1 > len2)
   25712             :         return 1;
   25713           0 :       else if (len1 < len2)
   25714             :         return -1;
   25715             :     }
   25716             : 
   25717             :   return winner;
   25718             : }
   25719             : 
   25720             : /* Return the template arguments that will produce the function signature
   25721             :    DECL from the function template FN, with the explicit template
   25722             :    arguments EXPLICIT_ARGS.  If CHECK_RETTYPE is true, the return type must
   25723             :    also match.  Return NULL_TREE if no satisfactory arguments could be
   25724             :    found.  */
   25725             : 
   25726             : static tree
   25727     8067932 : get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
   25728             : {
   25729     8067932 :   int ntparms = DECL_NTPARMS (fn);
   25730     8067932 :   tree targs = make_tree_vec (ntparms);
   25731     8067932 :   tree decl_type = TREE_TYPE (decl);
   25732     8067932 :   tree decl_arg_types;
   25733     8067932 :   tree *args;
   25734     8067932 :   unsigned int nargs, ix;
   25735     8067932 :   tree arg;
   25736             : 
   25737     8067932 :   gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
   25738             : 
   25739             :   /* Never do unification on the 'this' parameter.  */
   25740     8067932 :   decl_arg_types = skip_artificial_parms_for (decl,
   25741     8067932 :                                               TYPE_ARG_TYPES (decl_type));
   25742             : 
   25743     8067932 :   nargs = list_length (decl_arg_types);
   25744     8067932 :   args = XALLOCAVEC (tree, nargs);
   25745     8067932 :   for (arg = decl_arg_types, ix = 0;
   25746    31547787 :        arg != NULL_TREE;
   25747    23479855 :        arg = TREE_CHAIN (arg), ++ix)
   25748    23479855 :     args[ix] = TREE_VALUE (arg);
   25749             : 
   25750    16135864 :   if (fn_type_unification (fn, explicit_args, targs,
   25751             :                            args, ix,
   25752           0 :                            (check_rettype || DECL_CONV_FN_P (fn)
   25753     8067932 :                             ? TREE_TYPE (decl_type) : NULL_TREE),
   25754             :                            DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
   25755             :                            /*explain_p=*/false,
   25756             :                            /*decltype*/false)
   25757     8067932 :       == error_mark_node)
   25758     6491360 :     return NULL_TREE;
   25759             : 
   25760             :   return targs;
   25761             : }
   25762             : 
   25763             : /* Return the innermost template arguments that, when applied to a partial
   25764             :    specialization SPEC_TMPL of TMPL, yield the ARGS.
   25765             : 
   25766             :    For example, suppose we have:
   25767             : 
   25768             :      template <class T, class U> struct S {};
   25769             :      template <class T> struct S<T*, int> {};
   25770             : 
   25771             :    Then, suppose we want to get `S<double*, int>'.  SPEC_TMPL will be the
   25772             :    partial specialization and the ARGS will be {double*, int}.  The resulting
   25773             :    vector will be {double}, indicating that `T' is bound to `double'.  */
   25774             : 
   25775             : static tree
   25776    17262259 : get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
   25777             : {
   25778    17262259 :   tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
   25779    17262259 :   tree spec_args
   25780    17262259 :     = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
   25781    17262259 :   int i, ntparms = TREE_VEC_LENGTH (tparms);
   25782    17262259 :   tree deduced_args;
   25783    17262259 :   tree innermost_deduced_args;
   25784             : 
   25785    17262259 :   innermost_deduced_args = make_tree_vec (ntparms);
   25786    34524518 :   if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
   25787             :     {
   25788         274 :       deduced_args = copy_node (args);
   25789         548 :       SET_TMPL_ARGS_LEVEL (deduced_args,
   25790             :                            TMPL_ARGS_DEPTH (deduced_args),
   25791             :                            innermost_deduced_args);
   25792             :     }
   25793             :   else
   25794             :     deduced_args = innermost_deduced_args;
   25795             : 
   25796    17262259 :   bool tried_array_deduction = (cxx_dialect < cxx17);
   25797    17262261 :  again:
   25798    17262261 :   if (unify (tparms, deduced_args,
   25799             :              INNERMOST_TEMPLATE_ARGS (spec_args),
   25800             :              INNERMOST_TEMPLATE_ARGS (args),
   25801             :              UNIFY_ALLOW_NONE, /*explain_p=*/false))
   25802             :     return NULL_TREE;
   25803             : 
   25804    27404241 :   for (i =  0; i < ntparms; ++i)
   25805    18006614 :     if (! TREE_VEC_ELT (innermost_deduced_args, i))
   25806             :       {
   25807          27 :         if (!tried_array_deduction)
   25808             :           {
   25809          18 :             try_array_deduction (tparms, innermost_deduced_args,
   25810             :                                  INNERMOST_TEMPLATE_ARGS (spec_args));
   25811          18 :             tried_array_deduction = true;
   25812          18 :             if (TREE_VEC_ELT (innermost_deduced_args, i))
   25813           2 :               goto again;
   25814             :           }
   25815          25 :         return NULL_TREE;
   25816             :       }
   25817             : 
   25818     9397627 :   if (!push_tinst_level (spec_tmpl, deduced_args))
   25819             :     {
   25820           0 :       excessive_deduction_depth = true;
   25821           0 :       return NULL_TREE;
   25822             :     }
   25823             : 
   25824             :   /* Verify that nondeduced template arguments agree with the type
   25825             :      obtained from argument deduction.
   25826             : 
   25827             :      For example:
   25828             : 
   25829             :        struct A { typedef int X; };
   25830             :        template <class T, class U> struct C {};
   25831             :        template <class T> struct C<T, typename T::X> {};
   25832             : 
   25833             :      Then with the instantiation `C<A, int>', we can deduce that
   25834             :      `T' is `A' but unify () does not check whether `typename T::X'
   25835             :      is `int'.  */
   25836     9397627 :   spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
   25837             : 
   25838     9397627 :   if (spec_args != error_mark_node)
   25839     9263204 :     spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
   25840             :                                        INNERMOST_TEMPLATE_ARGS (spec_args),
   25841             :                                        tmpl, tf_none, false);
   25842             : 
   25843     9397627 :   pop_tinst_level ();
   25844             : 
   25845     9397627 :   if (spec_args == error_mark_node
   25846             :       /* We only need to check the innermost arguments; the other
   25847             :          arguments will always agree.  */
   25848     9397627 :       || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
   25849             :                                      INNERMOST_TEMPLATE_ARGS (args)))
   25850      442594 :     return NULL_TREE;
   25851             : 
   25852             :   /* Now that we have bindings for all of the template arguments,
   25853             :      ensure that the arguments deduced for the template template
   25854             :      parameters have compatible template parameter lists.  See the use
   25855             :      of template_template_parm_bindings_ok_p in fn_type_unification
   25856             :      for more information.  */
   25857     8955033 :   if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
   25858             :     return NULL_TREE;
   25859             : 
   25860             :   return deduced_args;
   25861             : }
   25862             : 
   25863             : // Compare two function templates T1 and T2 by deducing bindings
   25864             : // from one against the other. If both deductions succeed, compare
   25865             : // constraints to see which is more constrained.
   25866             : static int
   25867       47243 : more_specialized_inst (tree t1, tree t2)
   25868             : {
   25869       47243 :   int fate = 0;
   25870       47243 :   int count = 0;
   25871             : 
   25872       47243 :   if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
   25873             :     {
   25874         142 :       --fate;
   25875         142 :       ++count;
   25876             :     }
   25877             : 
   25878       47243 :   if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
   25879             :     {
   25880       31491 :       ++fate;
   25881       31491 :       ++count;
   25882             :     }
   25883             : 
   25884             :   // If both deductions succeed, then one may be more constrained.
   25885       47243 :   if (count == 2 && fate == 0)
   25886          60 :     fate = more_constrained (t1, t2);
   25887             : 
   25888       47243 :   return fate;
   25889             : }
   25890             : 
   25891             : /* TEMPLATES is a TREE_LIST.  Each TREE_VALUE is a TEMPLATE_DECL.
   25892             :    Return the TREE_LIST node with the most specialized template, if
   25893             :    any.  If there is no most specialized template, the error_mark_node
   25894             :    is returned.
   25895             : 
   25896             :    Note that this function does not look at, or modify, the
   25897             :    TREE_PURPOSE or TREE_TYPE of any of the nodes.  Since the node
   25898             :    returned is one of the elements of INSTANTIATIONS, callers may
   25899             :    store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
   25900             :    and retrieve it from the value returned.  */
   25901             : 
   25902             : tree
   25903       36163 : most_specialized_instantiation (tree templates)
   25904             : {
   25905       36163 :   tree fn, champ;
   25906             : 
   25907       36163 :   ++processing_template_decl;
   25908             : 
   25909       36163 :   champ = templates;
   25910       51966 :   for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
   25911             :     {
   25912       15815 :       gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
   25913       15815 :       int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
   25914       15815 :       if (fate == -1)
   25915             :         champ = fn;
   25916       15703 :       else if (!fate)
   25917             :         {
   25918             :           /* Equally specialized, move to next function.  If there
   25919             :              is no next function, nothing's most specialized.  */
   25920       15670 :           fn = TREE_CHAIN (fn);
   25921       15670 :           champ = fn;
   25922       15670 :           if (!fn)
   25923             :             break;
   25924             :         }
   25925             :     }
   25926             : 
   25927       36163 :   if (champ)
   25928             :     /* Now verify that champ is better than everything earlier in the
   25929             :        instantiation list.  */
   25930       67579 :     for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
   25931       31428 :       if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
   25932             :       {
   25933             :         champ = NULL_TREE;
   25934             :         break;
   25935             :       }
   25936             :     }
   25937             : 
   25938       36163 :   processing_template_decl--;
   25939             : 
   25940       36163 :   if (!champ)
   25941          12 :     return error_mark_node;
   25942             : 
   25943             :   return champ;
   25944             : }
   25945             : 
   25946             : /* If DECL is a specialization of some template, return the most
   25947             :    general such template.  Otherwise, returns NULL_TREE.
   25948             : 
   25949             :    For example, given:
   25950             : 
   25951             :      template <class T> struct S { template <class U> void f(U); };
   25952             : 
   25953             :    if TMPL is `template <class U> void S<int>::f(U)' this will return
   25954             :    the full template.  This function will not trace past partial
   25955             :    specializations, however.  For example, given in addition:
   25956             : 
   25957             :      template <class T> struct S<T*> { template <class U> void f(U); };
   25958             : 
   25959             :    if TMPL is `template <class U> void S<int*>::f(U)' this will return
   25960             :    `template <class T> template <class U> S<T*>::f(U)'.  */
   25961             : 
   25962             : tree
   25963  1884912379 : most_general_template (tree decl)
   25964             : {
   25965  1884912379 :   if (TREE_CODE (decl) != TEMPLATE_DECL)
   25966             :     {
   25967   366248618 :       if (tree tinfo = get_template_info (decl))
   25968   236562223 :         decl = TI_TEMPLATE (tinfo);
   25969             :       /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
   25970             :          template friend, or a FIELD_DECL for a capture pack.  */
   25971   366248618 :       if (TREE_CODE (decl) != TEMPLATE_DECL)
   25972             :         return NULL_TREE;
   25973             :     }
   25974             : 
   25975             :   /* Look for more and more general templates.  */
   25976  1773877718 :   while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
   25977             :     {
   25978             :       /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
   25979             :          (See cp-tree.h for details.)  */
   25980    18652346 :       if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
   25981             :         break;
   25982             : 
   25983    37304692 :       if (CLASS_TYPE_P (TREE_TYPE (decl))
   25984     4672705 :           && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
   25985    23113192 :           && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
   25986             :         break;
   25987             : 
   25988             :       /* Stop if we run into an explicitly specialized class template.  */
   25989    37303880 :       if (!DECL_NAMESPACE_SCOPE_P (decl)
   25990    18606288 :           && DECL_CONTEXT (decl)
   25991    37258228 :           && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
   25992             :         break;
   25993             : 
   25994    18651940 :       decl = DECL_TI_TEMPLATE (decl);
   25995             :     }
   25996             : 
   25997             :   return decl;
   25998             : }
   25999             : 
   26000             : /* Return the most specialized of the template partial specializations
   26001             :    which can produce TARGET, a specialization of some class or variable
   26002             :    template.  The value returned is a TEMPLATE_INFO; the TI_TEMPLATE is a
   26003             :    TEMPLATE_DECL node corresponding to the partial specialization, while
   26004             :    the TI_ARGS is the set of template arguments that must be substituted
   26005             :    into the template pattern in order to generate TARGET.  The result is
   26006             :    cached in the TI_PARTIAL_INFO of the corresponding TEMPLATE_INFO unless
   26007             :    RECHECKING is true.
   26008             : 
   26009             :    If the choice of partial specialization is ambiguous, a diagnostic
   26010             :    is issued, and the error_mark_node is returned.  If there are no
   26011             :    partial specializations matching TARGET, then NULL_TREE is
   26012             :    returned, indicating that the primary template should be used.  */
   26013             : 
   26014             : tree
   26015    20568264 : most_specialized_partial_spec (tree target, tsubst_flags_t complain,
   26016             :                                bool rechecking /* = false */)
   26017             : {
   26018    20568264 :   tree tinfo = NULL_TREE;
   26019    20568264 :   tree tmpl, args, decl;
   26020    20568264 :   if (TYPE_P (target))
   26021             :     {
   26022    19372438 :       tinfo = CLASSTYPE_TEMPLATE_INFO (target);
   26023    19372438 :       tmpl = TI_TEMPLATE (tinfo);
   26024    19372438 :       args = TI_ARGS (tinfo);
   26025    19372438 :       decl = TYPE_NAME (target);
   26026             :     }
   26027     1195826 :   else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
   26028             :     {
   26029      601389 :       tmpl = TREE_OPERAND (target, 0);
   26030      601389 :       args = TREE_OPERAND (target, 1);
   26031      601389 :       decl = DECL_TEMPLATE_RESULT (tmpl);
   26032             :     }
   26033      594437 :   else if (VAR_P (target))
   26034             :     {
   26035      594437 :       tinfo = DECL_TEMPLATE_INFO (target);
   26036      594437 :       tmpl = TI_TEMPLATE (tinfo);
   26037      594437 :       args = TI_ARGS (tinfo);
   26038      594437 :       decl = target;
   26039             :     }
   26040             :   else
   26041           0 :     gcc_unreachable ();
   26042             : 
   26043    20568264 :   if (!PRIMARY_TEMPLATE_P (tmpl))
   26044             :     return NULL_TREE;
   26045             : 
   26046    19909870 :   if (!rechecking
   26047    19909870 :       && tinfo
   26048    19909870 :       && (VAR_P (target) || COMPLETE_TYPE_P (target)))
   26049      594442 :     return TI_PARTIAL_INFO (tinfo);
   26050             : 
   26051    19315428 :   tree main_tmpl = most_general_template (tmpl);
   26052    19315428 :   tree specs = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl);
   26053    19315428 :   if (!specs)
   26054             :     /* There are no partial specializations of this template.  */
   26055             :     return NULL_TREE;
   26056             : 
   26057     6279594 :   push_access_scope_guard pas (decl);
   26058     6279594 :   deferring_access_check_sentinel acs (dk_no_deferred);
   26059             : 
   26060             :   /* For determining which partial specialization to use, only the
   26061             :      innermost args are interesting.  */
   26062     6279594 :   tree outer_args = NULL_TREE;
   26063    12559188 :   if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
   26064             :     {
   26065       75532 :       outer_args = strip_innermost_template_args (args, 1);
   26066       75532 :       args = INNERMOST_TEMPLATE_ARGS (args);
   26067             :     }
   26068             : 
   26069             :   /* The caller hasn't called push_to_top_level yet, but we need
   26070             :      get_partial_spec_bindings to be done in non-template context so that we'll
   26071             :      fully resolve everything.  */
   26072     6279594 :   processing_template_decl_sentinel ptds;
   26073             : 
   26074     6279594 :   tree list = NULL_TREE;
   26075    17917305 :   for (tree t = specs; t; t = TREE_CHAIN (t))
   26076             :     {
   26077    11637719 :       const tree ospec_tmpl = TREE_VALUE (t);
   26078             : 
   26079    11637719 :       tree spec_tmpl;
   26080    11637719 :       if (outer_args)
   26081             :         {
   26082             :           /* Substitute in the template args from the enclosing class.  */
   26083       78276 :           ++processing_template_decl;
   26084       78276 :           spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
   26085       78276 :           --processing_template_decl;
   26086       78276 :           if (spec_tmpl == error_mark_node)
   26087           8 :             return error_mark_node;
   26088             :         }
   26089             :       else
   26090             :         spec_tmpl = ospec_tmpl;
   26091             : 
   26092    11637711 :       tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
   26093    11637711 :       if (spec_args)
   26094             :         {
   26095     3576236 :           if (outer_args)
   26096       30372 :             spec_args = add_to_template_args (outer_args, spec_args);
   26097             : 
   26098             :           /* Keep the candidate only if its constraints are satisfied.  */
   26099     3576236 :           if (constraints_satisfied_p (ospec_tmpl, spec_args))
   26100     3506546 :             list = tree_cons (spec_args, ospec_tmpl, list);
   26101             :         }
   26102             :     }
   26103             : 
   26104     6279586 :   if (! list)
   26105             :     return NULL_TREE;
   26106             : 
   26107     3362441 :   tree champ = list;
   26108     3362441 :   bool ambiguous_p = false;
   26109     3506463 :   for (tree t = TREE_CHAIN (list); t; t = TREE_CHAIN (t))
   26110             :     {
   26111      144035 :       int fate = more_specialized_partial_spec (tmpl, champ, t);
   26112      144035 :       if (fate == 1)
   26113             :         ;
   26114             :       else
   26115             :         {
   26116       86571 :           if (fate == 0)
   26117             :             {
   26118          83 :               t = TREE_CHAIN (t);
   26119          83 :               if (! t)
   26120             :                 {
   26121             :                   ambiguous_p = true;
   26122             :                   break;
   26123             :                 }
   26124             :             }
   26125             :           champ = t;
   26126             :         }
   26127             :     }
   26128             : 
   26129     3362441 :   if (!ambiguous_p)
   26130     3464786 :     for (tree t = list; t && t != champ; t = TREE_CHAIN (t))
   26131             :       {
   26132      102361 :         int fate = more_specialized_partial_spec (tmpl, champ, t);
   26133      102361 :         if (fate != 1)
   26134             :           {
   26135             :             ambiguous_p = true;
   26136             :             break;
   26137             :           }
   26138             :       }
   26139             : 
   26140     3362428 :   if (ambiguous_p)
   26141             :     {
   26142          16 :       const char *str;
   26143          16 :       char *spaces = NULL;
   26144          16 :       if (!(complain & tf_error))
   26145           4 :         return error_mark_node;
   26146          12 :       if (TYPE_P (target))
   26147           9 :         error ("ambiguous template instantiation for %q#T", target);
   26148             :       else
   26149           3 :         error ("ambiguous template instantiation for %q#D", target);
   26150          12 :       str = ngettext ("candidate is:", "candidates are:", list_length (list));
   26151          45 :       for (tree t = list; t; t = TREE_CHAIN (t))
   26152             :         {
   26153          33 :           tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
   26154          66 :           inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
   26155             :                   "%s %#qS", spaces ? spaces : str, subst);
   26156          33 :           spaces = spaces ? spaces : get_spaces (str);
   26157             :         }
   26158          12 :       free (spaces);
   26159          12 :       return error_mark_node;
   26160             :     }
   26161             : 
   26162     3362425 :   tree result = build_template_info (TREE_VALUE (champ), TREE_PURPOSE (champ));
   26163     3362425 :   if (!rechecking && tinfo)
   26164     3286174 :     TI_PARTIAL_INFO (tinfo) = result;
   26165             :   return result;
   26166     6279594 : }
   26167             : 
   26168             : /* Explicitly instantiate DECL.  */
   26169             : 
   26170             : void
   26171     1430143 : do_decl_instantiation (tree decl, tree storage)
   26172             : {
   26173     1430143 :   tree result = NULL_TREE;
   26174     1430143 :   int extern_p = 0;
   26175             : 
   26176     1430143 :   if (!decl || decl == error_mark_node)
   26177             :     /* An error occurred, for which grokdeclarator has already issued
   26178             :        an appropriate message.  */
   26179             :     return;
   26180     1429967 :   else if (! DECL_LANG_SPECIFIC (decl))
   26181             :     {
   26182           0 :       error ("explicit instantiation of non-template %q#D", decl);
   26183           0 :       return;
   26184             :     }
   26185     1429967 :   else if (DECL_DECLARED_CONCEPT_P (decl))
   26186             :     {
   26187           2 :       if (VAR_P (decl))
   26188           1 :         error ("explicit instantiation of variable concept %q#D", decl);
   26189             :       else
   26190           1 :         error ("explicit instantiation of function concept %q#D", decl);
   26191           2 :       return;
   26192             :     }
   26193             : 
   26194     1429965 :   bool var_templ = (DECL_TEMPLATE_INFO (decl)
   26195     1429965 :                     && variable_template_p (DECL_TI_TEMPLATE (decl)));
   26196             : 
   26197     1429965 :   if (VAR_P (decl) && !var_templ)
   26198             :     {
   26199             :       /* There is an asymmetry here in the way VAR_DECLs and
   26200             :          FUNCTION_DECLs are handled by grokdeclarator.  In the case of
   26201             :          the latter, the DECL we get back will be marked as a
   26202             :          template instantiation, and the appropriate
   26203             :          DECL_TEMPLATE_INFO will be set up.  This does not happen for
   26204             :          VAR_DECLs so we do the lookup here.  Probably, grokdeclarator
   26205             :          should handle VAR_DECLs as it currently handles
   26206             :          FUNCTION_DECLs.  */
   26207          47 :       if (!DECL_CLASS_SCOPE_P (decl))
   26208             :         {
   26209           0 :           error ("%qD is not a static data member of a class template", decl);
   26210           0 :           return;
   26211             :         }
   26212          47 :       result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
   26213          47 :       if (!result || !VAR_P (result))
   26214             :         {
   26215           0 :           error ("no matching template for %qD found", decl);
   26216           0 :           return;
   26217             :         }
   26218          47 :       if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
   26219             :         {
   26220          12 :           error ("type %qT for explicit instantiation %qD does not match "
   26221           4 :                  "declared type %qT", TREE_TYPE (result), decl,
   26222           4 :                  TREE_TYPE (decl));
   26223           4 :           return;
   26224             :         }
   26225             :     }
   26226     1429918 :   else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
   26227             :     {
   26228           0 :       error ("explicit instantiation of %q#D", decl);
   26229           0 :       return;
   26230             :     }
   26231             :   else
   26232             :     result = decl;
   26233             : 
   26234             :   /* Check for various error cases.  Note that if the explicit
   26235             :      instantiation is valid the RESULT will currently be marked as an
   26236             :      *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
   26237             :      until we get here.  */
   26238             : 
   26239     1429961 :   if (DECL_TEMPLATE_SPECIALIZATION (result))
   26240             :     {
   26241             :       /* DR 259 [temp.spec].
   26242             : 
   26243             :          Both an explicit instantiation and a declaration of an explicit
   26244             :          specialization shall not appear in a program unless the explicit
   26245             :          instantiation follows a declaration of the explicit specialization.
   26246             : 
   26247             :          For a given set of template parameters, if an explicit
   26248             :          instantiation of a template appears after a declaration of an
   26249             :          explicit specialization for that template, the explicit
   26250             :          instantiation has no effect.  */
   26251             :       return;
   26252             :     }
   26253     1404128 :   else if (DECL_EXPLICIT_INSTANTIATION (result))
   26254             :     {
   26255             :       /* [temp.spec]
   26256             : 
   26257             :          No program shall explicitly instantiate any template more
   26258             :          than once.
   26259             : 
   26260             :          We check DECL_NOT_REALLY_EXTERN so as not to complain when
   26261             :          the first instantiation was `extern' and the second is not,
   26262             :          and EXTERN_P for the opposite case.  */
   26263         772 :       if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
   26264           8 :         permerror (input_location, "duplicate explicit instantiation of %q#D", result);
   26265             :       /* If an "extern" explicit instantiation follows an ordinary
   26266             :          explicit instantiation, the template is instantiated.  */
   26267             :       if (extern_p)
   26268             :         return;
   26269             :     }
   26270     1403356 :   else if (!DECL_IMPLICIT_INSTANTIATION (result))
   26271             :     {
   26272           0 :       error ("no matching template for %qD found", result);
   26273           0 :       return;
   26274             :     }
   26275     1403356 :   else if (!DECL_TEMPLATE_INFO (result))
   26276             :     {
   26277           0 :       permerror (input_location, "explicit instantiation of non-template %q#D", result);
   26278           0 :       return;
   26279             :     }
   26280             : 
   26281     1404128 :   if (storage == NULL_TREE)
   26282             :     ;
   26283     1401094 :   else if (storage == ridpointers[(int) RID_EXTERN])
   26284             :     {
   26285     1401094 :       if (cxx_dialect == cxx98)
   26286       16705 :         pedwarn (input_location, OPT_Wpedantic,
   26287             :                  "ISO C++ 1998 forbids the use of %<extern%> on explicit "
   26288             :                  "instantiations");
   26289             :       extern_p = 1;
   26290             :     }
   26291             :   else
   26292           0 :     error ("storage class %qD applied to template instantiation", storage);
   26293             : 
   26294     1404128 :   check_explicit_instantiation_namespace (result);
   26295     1404128 :   mark_decl_instantiated (result, extern_p);
   26296     1404128 :   if (! extern_p)
   26297        3034 :     instantiate_decl (result, /*defer_ok=*/true,
   26298             :                       /*expl_inst_class_mem_p=*/false);
   26299             : }
   26300             : 
   26301             : static void
   26302      639566 : mark_class_instantiated (tree t, int extern_p)
   26303             : {
   26304      639566 :   SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
   26305      639566 :   SET_CLASSTYPE_INTERFACE_KNOWN (t);
   26306      639566 :   CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
   26307      639566 :   TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
   26308      639566 :   if (! extern_p)
   26309             :     {
   26310        2678 :       CLASSTYPE_DEBUG_REQUESTED (t) = 1;
   26311        2678 :       rest_of_type_compilation (t, 1);
   26312             :     }
   26313      639566 : }
   26314             : 
   26315             : /* Perform an explicit instantiation of template class T.  STORAGE, if
   26316             :    non-null, is the RID for extern, inline or static.  COMPLAIN is
   26317             :    nonzero if this is called from the parser, zero if called recursively,
   26318             :    since the standard is unclear (as detailed below).  */
   26319             : 
   26320             : void
   26321      670378 : do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
   26322             : {
   26323      670378 :   if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
   26324             :     {
   26325           4 :       if (tree ti = TYPE_TEMPLATE_INFO (t))
   26326           0 :         error ("explicit instantiation of non-class template %qD",
   26327           0 :                TI_TEMPLATE (ti));
   26328             :       else
   26329           4 :         error ("explicit instantiation of non-template type %qT", t);
   26330           4 :       return;
   26331             :     }
   26332             : 
   26333      670374 :   complete_type (t);
   26334             : 
   26335      670374 :   if (!COMPLETE_TYPE_P (t))
   26336             :     {
   26337          82 :       if (complain & tf_error)
   26338           0 :         error ("explicit instantiation of %q#T before definition of template",
   26339             :                t);
   26340          82 :       return;
   26341             :     }
   26342             : 
   26343             :   /* At most one of these will be true.  */
   26344      670292 :   bool extern_p = false;
   26345      670292 :   bool nomem_p = false;
   26346      670292 :   bool static_p = false;
   26347             : 
   26348      670292 :   if (storage != NULL_TREE)
   26349             :     {
   26350      667580 :       if (storage == ridpointers[(int) RID_EXTERN])
   26351             :         {
   26352      667552 :           if (cxx_dialect == cxx98)
   26353        7438 :             pedwarn (input_location, OPT_Wpedantic,
   26354             :                      "ISO C++ 1998 forbids the use of %<extern%> on "
   26355             :                      "explicit instantiations");
   26356             :         }
   26357             :       else
   26358          28 :         pedwarn (input_location, OPT_Wpedantic,
   26359             :                  "ISO C++ forbids the use of %qE"
   26360             :                  " on explicit instantiations", storage);
   26361             : 
   26362      667580 :       if (storage == ridpointers[(int) RID_INLINE])
   26363             :         nomem_p = true;
   26364      667556 :       else if (storage == ridpointers[(int) RID_EXTERN])
   26365             :         extern_p = true;
   26366           4 :       else if (storage == ridpointers[(int) RID_STATIC])
   26367             :         static_p = true;
   26368             :       else
   26369           0 :         error ("storage class %qD applied to template instantiation",
   26370             :                storage);
   26371             :     }
   26372             : 
   26373      670292 :   if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
   26374             :     /* DR 259 [temp.spec].
   26375             : 
   26376             :        Both an explicit instantiation and a declaration of an explicit
   26377             :        specialization shall not appear in a program unless the
   26378             :        explicit instantiation follows a declaration of the explicit
   26379             :        specialization.
   26380             : 
   26381             :        For a given set of template parameters, if an explicit
   26382             :        instantiation of a template appears after a declaration of an
   26383             :        explicit specialization for that template, the explicit
   26384             :        instantiation has no effect.  */
   26385             :     return;
   26386             : 
   26387      639570 :   if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
   26388             :     {
   26389             :       /* We've already instantiated the template.  */
   26390             : 
   26391             :       /* [temp.spec]
   26392             : 
   26393             :          No program shall explicitly instantiate any template more
   26394             :          than once.
   26395             : 
   26396             :          If EXTERN_P then this is ok.  */
   26397           4 :       if (!extern_p && (complain & tf_error))
   26398           4 :         permerror (input_location,
   26399             :                    "duplicate explicit instantiation of %q#T", t);
   26400             : 
   26401           4 :       return;
   26402             :     }
   26403             : 
   26404      639566 :   check_explicit_instantiation_namespace (TYPE_NAME (t));
   26405      639566 :   mark_class_instantiated (t, extern_p);
   26406             : 
   26407      639566 :   if (nomem_p)
   26408             :     return;
   26409             : 
   26410             :   /* In contrast to implicit instantiation, where only the
   26411             :      declarations, and not the definitions, of members are
   26412             :      instantiated, we have here:
   26413             : 
   26414             :          [temp.explicit]
   26415             : 
   26416             :          An explicit instantiation that names a class template
   26417             :          specialization is also an explicit instantiation of the same
   26418             :          kind (declaration or definition) of each of its members (not
   26419             :          including members inherited from base classes and members
   26420             :          that are templates) that has not been previously explicitly
   26421             :          specialized in the translation unit containing the explicit
   26422             :          instantiation, provided that the associated constraints, if
   26423             :          any, of that member are satisfied by the template arguments
   26424             :          of the explicit instantiation.  */
   26425    24778717 :   for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
   26426    24139179 :     if ((VAR_P (fld)
   26427    23885746 :          || (TREE_CODE (fld) == FUNCTION_DECL
   26428    16271592 :              && !static_p
   26429    16271592 :              && user_provided_p (fld)))
   26430    15472080 :         && DECL_TEMPLATE_INSTANTIATION (fld)
   26431    39370230 :         && constraints_satisfied_p (fld))
   26432             :       {
   26433    15231050 :         mark_decl_instantiated (fld, extern_p);
   26434    15231050 :         if (! extern_p)
   26435       28600 :           instantiate_decl (fld, /*defer_ok=*/true,
   26436             :                             /*expl_inst_class_mem_p=*/true);
   26437             :       }
   26438     8908129 :     else if (DECL_IMPLICIT_TYPEDEF_P (fld))
   26439             :       {
   26440      108212 :         tree type = TREE_TYPE (fld);
   26441             : 
   26442       92466 :         if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
   26443      200676 :             && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
   26444       92464 :           do_type_instantiation (type, storage, 0);
   26445             :       }
   26446             : }
   26447             : 
   26448             : /* Given a function DECL, which is a specialization of TMPL, modify
   26449             :    DECL to be a re-instantiation of TMPL with the same template
   26450             :    arguments.  TMPL should be the template into which tsubst'ing
   26451             :    should occur for DECL, not the most general template.
   26452             : 
   26453             :    One reason for doing this is a scenario like this:
   26454             : 
   26455             :      template <class T>
   26456             :      void f(const T&, int i);
   26457             : 
   26458             :      void g() { f(3, 7); }
   26459             : 
   26460             :      template <class T>
   26461             :      void f(const T& t, const int i) { }
   26462             : 
   26463             :    Note that when the template is first instantiated, with
   26464             :    instantiate_template, the resulting DECL will have no name for the
   26465             :    first parameter, and the wrong type for the second.  So, when we go
   26466             :    to instantiate the DECL, we regenerate it.  */
   26467             : 
   26468             : static void
   26469    11403230 : regenerate_decl_from_template (tree decl, tree tmpl, tree args)
   26470             : {
   26471             :   /* The arguments used to instantiate DECL, from the most general
   26472             :      template.  */
   26473    11403230 :   tree code_pattern = DECL_TEMPLATE_RESULT (tmpl);
   26474             : 
   26475             :   /* Make sure that we can see identifiers, and compute access correctly.  */
   26476    11403230 :   push_access_scope (decl);
   26477             : 
   26478    11403230 :   if (TREE_CODE (decl) == FUNCTION_DECL)
   26479             :     {
   26480    10204602 :       tree specs;
   26481    10204602 :       int args_depth;
   26482    10204602 :       int parms_depth;
   26483             : 
   26484             :       /* Don't bother with this for unique friends that can't be redeclared and
   26485             :          might change type if regenerated (PR69836).  */
   26486    10204602 :       if (DECL_UNIQUE_FRIEND_P (decl))
   26487       23476 :         goto done;
   26488             : 
   26489             :       /* Use the source location of the definition.  */
   26490    10181126 :       DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl);
   26491             : 
   26492    20362252 :       args_depth = TMPL_ARGS_DEPTH (args);
   26493    10181126 :       parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
   26494    10181126 :       if (args_depth > parms_depth)
   26495           0 :         args = get_innermost_template_args (args, parms_depth);
   26496             : 
   26497             :       /* Instantiate a dynamic exception-specification.  noexcept will be
   26498             :          handled below.  */
   26499    10181126 :       if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
   26500     4371819 :         if (TREE_VALUE (raises))
   26501             :           {
   26502          24 :             specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
   26503             :                                                     args, tf_error, NULL_TREE,
   26504             :                                                     /*defer_ok*/false);
   26505          24 :             if (specs && specs != error_mark_node)
   26506          22 :               TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
   26507             :                                                           specs);
   26508             :           }
   26509             : 
   26510             :       /* Merge parameter declarations.  */
   26511    10181126 :       if (tree pattern_parm
   26512    10181126 :           = skip_artificial_parms_for (code_pattern,
   26513    10181126 :                                        DECL_ARGUMENTS (code_pattern)))
   26514             :         {
   26515     7068964 :           tree *p = &DECL_ARGUMENTS (decl);
   26516     9444514 :           for (int skip = num_artificial_parms_for (decl); skip; --skip)
   26517     2375550 :             p = &DECL_CHAIN (*p);
   26518     7068964 :           *p = tsubst_decl (pattern_parm, args, tf_error);
   26519    20136276 :           for (tree t = *p; t; t = DECL_CHAIN (t))
   26520    13067312 :             DECL_CONTEXT (t) = decl;
   26521             :         }
   26522             : 
   26523    10181126 :       if (DECL_CONTRACTS (decl))
   26524             :         {
   26525             :           /* If we're regenerating a specialization, the contracts will have
   26526             :              been copied from the most general template. Replace those with
   26527             :              the ones from the actual specialization.  */
   26528         146 :           tree tmpl = DECL_TI_TEMPLATE (decl);
   26529         146 :           if (DECL_TEMPLATE_SPECIALIZATION (tmpl))
   26530             :             {
   26531           4 :               remove_contract_attributes (decl);
   26532           4 :               copy_contract_attributes (decl, code_pattern);
   26533             :             }
   26534             : 
   26535         146 :           tsubst_contract_attributes (decl, args, tf_warning_or_error, code_pattern);
   26536             :         }
   26537             : 
   26538             :       /* Merge additional specifiers from the CODE_PATTERN.  */
   26539    10181126 :       if (DECL_DECLARED_INLINE_P (code_pattern)
   26540    19107591 :           && !DECL_DECLARED_INLINE_P (decl))
   26541       40865 :         DECL_DECLARED_INLINE_P (decl) = 1;
   26542             : 
   26543    10181126 :       maybe_instantiate_noexcept (decl, tf_error);
   26544             :     }
   26545     1198628 :   else if (VAR_P (decl))
   26546             :     {
   26547     1198628 :       start_lambda_scope (decl);
   26548     1198628 :       DECL_INITIAL (decl) =
   26549     1198628 :         tsubst_init (DECL_INITIAL (code_pattern), decl, args,
   26550     1198628 :                      tf_error, DECL_TI_TEMPLATE (decl));
   26551     1198628 :       finish_lambda_scope ();
   26552     1198628 :       if (VAR_HAD_UNKNOWN_BOUND (decl))
   26553          64 :         TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
   26554          64 :                                    tf_error, DECL_TI_TEMPLATE (decl));
   26555             :     }
   26556             :   else
   26557           0 :     gcc_unreachable ();
   26558             : 
   26559    11403230 :  done:
   26560    11403230 :   pop_access_scope (decl);
   26561    11403230 : }
   26562             : 
   26563             : /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
   26564             :    substituted to get DECL.  */
   26565             : 
   26566             : tree
   26567    42354834 : template_for_substitution (tree decl)
   26568             : {
   26569    42354834 :   tree tmpl = DECL_TI_TEMPLATE (decl);
   26570             : 
   26571             :   /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
   26572             :      for the instantiation.  This is not always the most general
   26573             :      template.  Consider, for example:
   26574             : 
   26575             :         template <class T>
   26576             :         struct S { template <class U> void f();
   26577             :                    template <> void f<int>(); };
   26578             : 
   26579             :      and an instantiation of S<double>::f<int>.  We want TD to be the
   26580             :      specialization S<T>::f<int>, not the more general S<T>::f<U>.  */
   26581    42354834 :   while (/* An instantiation cannot have a definition, so we need a
   26582             :             more general template.  */
   26583    92405360 :          DECL_TEMPLATE_INSTANTIATION (tmpl)
   26584             :            /* We must also deal with friend templates.  Given:
   26585             : 
   26586             :                 template <class T> struct S {
   26587             :                   template <class U> friend void f() {};
   26588             :                 };
   26589             : 
   26590             :               S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
   26591             :               so far as the language is concerned, but that's still
   26592             :               where we get the pattern for the instantiation from.  On
   26593             :               other hand, if the definition comes outside the class, say:
   26594             : 
   26595             :                 template <class T> struct S {
   26596             :                   template <class U> friend void f();
   26597             :                 };
   26598             :                 template <class U> friend void f() {}
   26599             : 
   26600             :               we don't need to look any further.  That's what the check for
   26601             :               DECL_INITIAL is for.  */
   26602    46202680 :           || (TREE_CODE (decl) == FUNCTION_DECL
   26603    38678224 :               && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
   26604      368088 :               && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
   26605             :     {
   26606             :       /* The present template, TD, should not be a definition.  If it
   26607             :          were a definition, we should be using it!  Note that we
   26608             :          cannot restructure the loop to just keep going until we find
   26609             :          a template with a definition, since that might go too far if
   26610             :          a specialization was declared, but not defined.  */
   26611             : 
   26612             :       /* Fetch the more general template.  */
   26613     3847846 :       tmpl = DECL_TI_TEMPLATE (tmpl);
   26614             :     }
   26615             : 
   26616    42354834 :   return tmpl;
   26617             : }
   26618             : 
   26619             : /* Returns true if we need to instantiate this template instance even if we
   26620             :    know we aren't going to emit it.  */
   26621             : 
   26622             : bool
   26623     2226747 : always_instantiate_p (tree decl)
   26624             : {
   26625             :   /* We always instantiate inline functions so that we can inline them.  An
   26626             :      explicit instantiation declaration prohibits implicit instantiation of
   26627             :      non-inline functions.  With high levels of optimization, we would
   26628             :      normally inline non-inline functions -- but we're not allowed to do
   26629             :      that for "extern template" functions.  Therefore, we check
   26630             :      DECL_DECLARED_INLINE_P, rather than possibly_inlined_p.  */
   26631     2226747 :   return ((TREE_CODE (decl) == FUNCTION_DECL
   26632     2175581 :            && (DECL_DECLARED_INLINE_P (decl)
   26633       94421 :                || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
   26634             :           /* And we need to instantiate static data members so that
   26635             :              their initializers are available in integral constant
   26636             :              expressions.  */
   26637     2321168 :           || (VAR_P (decl)
   26638       51166 :               && decl_maybe_constant_var_p (decl)));
   26639             : }
   26640             : 
   26641             : /* If FN has a noexcept-specifier that hasn't been instantiated yet,
   26642             :    instantiate it now, modifying TREE_TYPE (fn).  Returns false on
   26643             :    error, true otherwise.  */
   26644             : 
   26645             : bool
   26646   193093556 : maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
   26647             : {
   26648   193093556 :   if (fn == error_mark_node)
   26649             :     return false;
   26650             : 
   26651             :   /* Don't instantiate a noexcept-specification from template context.  */
   26652   193093555 :   if (processing_template_decl
   26653   193093555 :       && (!flag_noexcept_type || type_dependent_expression_p (fn)))
   26654      297337 :     return true;
   26655             : 
   26656   192796218 :   tree fntype = TREE_TYPE (fn);
   26657   192796218 :   tree spec = TYPE_RAISES_EXCEPTIONS (fntype);
   26658             : 
   26659    87936951 :   if ((!spec || UNEVALUATED_NOEXCEPT_SPEC_P (spec))
   26660   196886982 :       && DECL_MAYBE_DELETED (fn))
   26661             :     {
   26662        3251 :       if (fn == current_function_decl)
   26663             :         /* We're in start_preparsed_function, keep going.  */
   26664             :         return true;
   26665             : 
   26666           5 :       ++function_depth;
   26667           5 :       maybe_synthesize_method (fn);
   26668           5 :       --function_depth;
   26669           5 :       return !DECL_DELETED_FN (fn);
   26670             :     }
   26671             : 
   26672   280729918 :   if (!spec || !TREE_PURPOSE (spec))
   26673             :     return true;
   26674             : 
   26675    87236562 :   tree noex = TREE_PURPOSE (spec);
   26676    87236562 :   if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
   26677    87236562 :       && TREE_CODE (noex) != DEFERRED_PARSE)
   26678             :     return true;
   26679             : 
   26680     4654270 :   tree orig_fn = NULL_TREE;
   26681             :   /* For a member friend template we can get a TEMPLATE_DECL.  Let's use
   26682             :      its FUNCTION_DECL for the rest of this function -- push_access_scope
   26683             :      doesn't accept TEMPLATE_DECLs.  */
   26684     4654270 :   if (DECL_FUNCTION_TEMPLATE_P (fn))
   26685             :     {
   26686          45 :       orig_fn = fn;
   26687          45 :       fn = DECL_TEMPLATE_RESULT (fn);
   26688             :     }
   26689             : 
   26690     4654270 :   if (DECL_CLONED_FUNCTION_P (fn))
   26691             :     {
   26692     2224154 :       tree prime = DECL_CLONED_FUNCTION (fn);
   26693     2224154 :       if (!maybe_instantiate_noexcept (prime, complain))
   26694             :         return false;
   26695     2224053 :       spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
   26696             :     }
   26697     2430116 :   else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
   26698             :     {
   26699     2430116 :       static hash_set<tree>* fns = new hash_set<tree>;
   26700     2430116 :       bool added = false;
   26701     2430116 :       if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
   26702             :         {
   26703     2048673 :           spec = get_defaulted_eh_spec (fn, complain);
   26704     2048673 :           if (spec == error_mark_node)
   26705             :             /* This might have failed because of an unparsed DMI, so
   26706             :                let's try again later.  */
   26707             :             return false;
   26708             :         }
   26709      381443 :       else if (!(added = !fns->add (fn)))
   26710             :         {
   26711             :           /* If hash_set::add returns true, the element was already there.  */
   26712           3 :           location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
   26713           3 :                                             DECL_SOURCE_LOCATION (fn));
   26714           3 :           error_at (loc,
   26715             :                     "exception specification of %qD depends on itself",
   26716             :                     fn);
   26717           3 :           spec = noexcept_false_spec;
   26718             :         }
   26719      381440 :       else if (push_tinst_level (fn))
   26720             :         {
   26721      381414 :           push_to_top_level ();
   26722      381414 :           push_access_scope (fn);
   26723      381414 :           push_deferring_access_checks (dk_no_deferred);
   26724      381414 :           input_location = DECL_SOURCE_LOCATION (fn);
   26725             : 
   26726      381414 :           if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
   26727      381414 :               && !DECL_LOCAL_DECL_P (fn))
   26728             :             {
   26729             :               /* If needed, set current_class_ptr for the benefit of
   26730             :                  tsubst_copy/PARM_DECL.  */
   26731      132707 :               tree this_parm = DECL_ARGUMENTS (fn);
   26732      132707 :               current_class_ptr = NULL_TREE;
   26733      132707 :               current_class_ref = cp_build_fold_indirect_ref (this_parm);
   26734      132707 :               current_class_ptr = this_parm;
   26735             :             }
   26736             : 
   26737             :           /* If this function is represented by a TEMPLATE_DECL, then
   26738             :              the deferred noexcept-specification might still contain
   26739             :              dependent types, even after substitution.  And we need the
   26740             :              dependency check functions to work in build_noexcept_spec.  */
   26741      381414 :           if (orig_fn)
   26742          45 :             ++processing_template_decl;
   26743             : 
   26744             :           /* Do deferred instantiation of the noexcept-specifier.  */
   26745      381414 :           noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
   26746      381414 :                                         DEFERRED_NOEXCEPT_ARGS (noex),
   26747             :                                         tf_warning_or_error, fn);
   26748             : 
   26749             :           /* Build up the noexcept-specification.  */
   26750      381414 :           spec = build_noexcept_spec (noex, tf_warning_or_error);
   26751             : 
   26752      381414 :           if (orig_fn)
   26753          45 :             --processing_template_decl;
   26754             : 
   26755      381414 :           pop_deferring_access_checks ();
   26756      381414 :           pop_access_scope (fn);
   26757      381414 :           pop_tinst_level ();
   26758      381414 :           pop_from_top_level ();
   26759             :         }
   26760             :       else
   26761          26 :         spec = noexcept_false_spec;
   26762             : 
   26763      381443 :       if (added)
   26764      381440 :         fns->remove (fn);
   26765             :     }
   26766             : 
   26767     4654068 :   if (spec == error_mark_node)
   26768             :     {
   26769             :       /* This failed with a hard error, so let's go with false.  */
   26770          27 :       gcc_assert (seen_error ());
   26771          27 :       spec = noexcept_false_spec;
   26772             :     }
   26773             : 
   26774     4654068 :   TREE_TYPE (fn) = build_exception_variant (fntype, spec);
   26775     4654068 :   if (orig_fn)
   26776          45 :     TREE_TYPE (orig_fn) = TREE_TYPE (fn);
   26777             : 
   26778             :   return true;
   26779             : }
   26780             : 
   26781             : /* We're starting to process the function INST, an instantiation of PATTERN;
   26782             :    add their parameters to local_specializations.  */
   26783             : 
   26784             : void
   26785    10243716 : register_parameter_specializations (tree pattern, tree inst)
   26786             : {
   26787    10243716 :   tree tmpl_parm = DECL_ARGUMENTS (pattern);
   26788    10243716 :   tree spec_parm = DECL_ARGUMENTS (inst);
   26789    10243716 :   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
   26790             :     {
   26791     4948850 :       register_local_specialization (spec_parm, tmpl_parm);
   26792     4948850 :       spec_parm = skip_artificial_parms_for (inst, spec_parm);
   26793     4948850 :       tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
   26794             :     }
   26795    23524585 :   for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
   26796             :     {
   26797    13280869 :       if (!DECL_PACK_P (tmpl_parm))
   26798             :         {
   26799    12899210 :           register_local_specialization (spec_parm, tmpl_parm);
   26800    12899210 :           spec_parm = DECL_CHAIN (spec_parm);
   26801             :         }
   26802             :       else
   26803             :         {
   26804             :           /* Register the (value) argument pack as a specialization of
   26805             :              TMPL_PARM, then move on.  */
   26806      381659 :           tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
   26807      381659 :           register_local_specialization (argpack, tmpl_parm);
   26808             :         }
   26809             :     }
   26810    10243716 :   gcc_assert (!spec_parm);
   26811    10243716 : }
   26812             : 
   26813             : /* Instantiate the body of D using PATTERN with ARGS.  We have
   26814             :    already determined PATTERN is the correct template to use.
   26815             :    NESTED_P is true if this is a nested function, in which case
   26816             :    PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL.  */
   26817             : 
   26818             : static void
   26819    11403340 : instantiate_body (tree pattern, tree args, tree d, bool nested_p)
   26820             : {
   26821    11403340 :   tree td = NULL_TREE;
   26822    11403340 :   tree code_pattern = pattern;
   26823             : 
   26824    11403340 :   if (!nested_p)
   26825             :     {
   26826    11403230 :       td = pattern;
   26827    11403230 :       code_pattern = DECL_TEMPLATE_RESULT (td);
   26828             :     }
   26829             :   else
   26830             :     /* Only OMP reductions are nested.  */
   26831         110 :     gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
   26832             : 
   26833    11403340 :   vec<tree> omp_privatization_save;
   26834    11403340 :   if (current_function_decl)
   26835     1573499 :     save_omp_privatization_clauses (omp_privatization_save);
   26836             : 
   26837    11403340 :   bool push_to_top = maybe_push_to_top_level (d);
   26838             : 
   26839    11403340 :   mark_template_arguments_used (pattern, args);
   26840             : 
   26841    11403340 :   if (VAR_P (d))
   26842             :     {
   26843             :       /* The variable might be a lambda's extra scope, and that
   26844             :          lambda's visibility depends on D's.  */
   26845     1198628 :       maybe_commonize_var (d);
   26846     1198628 :       determine_visibility (d);
   26847             :     }
   26848             : 
   26849             :   /* Mark D as instantiated so that recursive calls to
   26850             :      instantiate_decl do not try to instantiate it again.  */
   26851    11403340 :   DECL_TEMPLATE_INSTANTIATED (d) = 1;
   26852             : 
   26853    11403340 :   if (td)
   26854             :     /* Regenerate the declaration in case the template has been modified
   26855             :        by a subsequent redeclaration.  */
   26856    11403230 :     regenerate_decl_from_template (d, td, args);
   26857             : 
   26858             :   /* We already set the file and line above.  Reset them now in case
   26859             :      they changed as a result of calling regenerate_decl_from_template.  */
   26860    11403340 :   input_location = DECL_SOURCE_LOCATION (d);
   26861             : 
   26862    11403340 :   if (VAR_P (d))
   26863             :     {
   26864             :       /* Clear out DECL_RTL; whatever was there before may not be right
   26865             :          since we've reset the type of the declaration.  */
   26866     1198628 :       SET_DECL_RTL (d, NULL);
   26867     1198628 :       DECL_IN_AGGR_P (d) = 0;
   26868             : 
   26869             :       /* The initializer is placed in DECL_INITIAL by
   26870             :          regenerate_decl_from_template so we don't need to
   26871             :          push/pop_access_scope again here.  Pull it out so that
   26872             :          cp_finish_decl can process it.  */
   26873     1198628 :       bool const_init = false;
   26874     1198628 :       tree init = DECL_INITIAL (d);
   26875     1198628 :       DECL_INITIAL (d) = NULL_TREE;
   26876     1198628 :       DECL_INITIALIZED_P (d) = 0;
   26877             : 
   26878             :       /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
   26879             :          initializer.  That function will defer actual emission until
   26880             :          we have a chance to determine linkage.  */
   26881     1198628 :       DECL_EXTERNAL (d) = 0;
   26882             : 
   26883             :       /* Enter the scope of D so that access-checking works correctly.  */
   26884     1198628 :       bool enter_context = DECL_CLASS_SCOPE_P (d);
   26885      611778 :       if (enter_context)
   26886      611778 :         push_nested_class (DECL_CONTEXT (d));
   26887             : 
   26888     1198628 :       const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
   26889     1198628 :       cp_finish_decl (d, init, const_init, NULL_TREE, 0);
   26890             : 
   26891     1198628 :       if (enter_context)
   26892      611778 :         pop_nested_class ();
   26893             :     }
   26894    10204712 :   else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
   26895          59 :     synthesize_method (d);
   26896    10204653 :   else if (TREE_CODE (d) == FUNCTION_DECL)
   26897             :     {
   26898             :       /* Set up the list of local specializations.  */
   26899    10846929 :       local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
   26900    10204653 :       tree block = NULL_TREE;
   26901             : 
   26902             :       /* Set up context.  */
   26903    10204653 :       if (nested_p)
   26904         110 :         block = push_stmt_list ();
   26905             :       else
   26906             :         {
   26907    10204543 :           start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
   26908             : 
   26909    10204543 :           perform_instantiation_time_access_checks (code_pattern, args);
   26910             :         }
   26911             : 
   26912             :       /* Create substitution entries for the parameters.  */
   26913    10204653 :       register_parameter_specializations (code_pattern, d);
   26914             : 
   26915             :       /* Substitute into the body of the function.  */
   26916    10204653 :       if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
   26917         126 :         tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
   26918             :                         tf_warning_or_error, d);
   26919             :       else
   26920             :         {
   26921    10204527 :           tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
   26922    10204527 :                        tf_warning_or_error, DECL_TI_TEMPLATE (d));
   26923             : 
   26924             :           /* Set the current input_location to the end of the function
   26925             :              so that finish_function knows where we are.  */
   26926    10204516 :           input_location
   26927    10204516 :             = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
   26928             : 
   26929             :           /* Remember if we saw an infinite loop in the template.  */
   26930    10204516 :           current_function_infinite_loop
   26931    10204516 :             = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
   26932             :         }
   26933             : 
   26934             :       /* Finish the function.  */
   26935    10204642 :       if (nested_p)
   26936         110 :         DECL_SAVED_TREE (d) = pop_stmt_list (block);
   26937             :       else
   26938             :         {
   26939    10204532 :           d = finish_function (/*inline_p=*/false);
   26940    10204532 :           expand_or_defer_fn (d);
   26941             :         }
   26942             : 
   26943    10204642 :       if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
   26944         126 :         cp_check_omp_declare_reduction (d);
   26945    10204642 :     }
   26946             : 
   26947             :   /* We're not deferring instantiation any more.  */
   26948    11403329 :   if (!nested_p)
   26949    11403219 :     TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
   26950             : 
   26951    11403329 :   maybe_pop_from_top_level (push_to_top);
   26952             : 
   26953    11403329 :   if (current_function_decl)
   26954     1573499 :     restore_omp_privatization_clauses (omp_privatization_save);
   26955    11403329 : }
   26956             : 
   26957             : /* Produce the definition of D, a _DECL generated from a template.  If
   26958             :    DEFER_OK is true, then we don't have to actually do the
   26959             :    instantiation now; we just have to do it sometime.  Normally it is
   26960             :    an error if this is an explicit instantiation but D is undefined.
   26961             :    EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
   26962             :    instantiated class template.  */
   26963             : 
   26964             : tree
   26965    58923269 : instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
   26966             : {
   26967    58923269 :   tree tmpl = DECL_TI_TEMPLATE (d);
   26968    58923269 :   tree gen_args;
   26969    58923269 :   tree args;
   26970    58923269 :   tree td;
   26971    58923269 :   tree code_pattern;
   26972    58923269 :   tree spec;
   26973    58923269 :   tree gen_tmpl;
   26974    58923269 :   bool pattern_defined;
   26975    58923269 :   location_t saved_loc = input_location;
   26976    58923269 :   int saved_unevaluated_operand = cp_unevaluated_operand;
   26977    58923269 :   int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
   26978    58923269 :   bool external_p;
   26979    58923269 :   bool deleted_p;
   26980             : 
   26981             :   /* This function should only be used to instantiate templates for
   26982             :      functions and static member variables.  */
   26983    58923269 :   gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
   26984             : 
   26985             :   /* A concept is never instantiated. */
   26986    58923269 :   gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
   26987             : 
   26988    58923269 :   gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
   26989             : 
   26990    58923269 :   if (modules_p ())
   26991             :     /* We may have a pending instantiation of D itself.  */
   26992      263959 :     lazy_load_pendings (d);
   26993             : 
   26994             :   /* Variables are never deferred; if instantiation is required, they
   26995             :      are instantiated right away.  That allows for better code in the
   26996             :      case that an expression refers to the value of the variable --
   26997             :      if the variable has a constant value the referring expression can
   26998             :      take advantage of that fact.  */
   26999    58923269 :   if (VAR_P (d))
   27000    37323537 :     defer_ok = false;
   27001             : 
   27002             :   /* Don't instantiate cloned functions.  Instead, instantiate the
   27003             :      functions they cloned.  */
   27004    58923269 :   if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
   27005     3020046 :     d = DECL_CLONED_FUNCTION (d);
   27006             : 
   27007    58923269 :   if (DECL_TEMPLATE_INSTANTIATED (d)
   27008    23628625 :       || TREE_TYPE (d) == error_mark_node
   27009    23628620 :       || (TREE_CODE (d) == FUNCTION_DECL
   27010    20220379 :           && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
   27011    82537862 :       || DECL_TEMPLATE_SPECIALIZATION (d))
   27012             :     /* D has already been instantiated or explicitly specialized, so
   27013             :        there's nothing for us to do here.
   27014             : 
   27015             :        It might seem reasonable to check whether or not D is an explicit
   27016             :        instantiation, and, if so, stop here.  But when an explicit
   27017             :        instantiation is deferred until the end of the compilation,
   27018             :        DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
   27019             :        the instantiation.  */
   27020             :     return d;
   27021             : 
   27022             :   /* Check to see whether we know that this template will be
   27023             :      instantiated in some other file, as with "extern template"
   27024             :      extension.  */
   27025    23579165 :   external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
   27026             : 
   27027             :   /* In general, we do not instantiate such templates.  */
   27028     1443924 :   if (external_p && !always_instantiate_p (d))
   27029             :     return d;
   27030             : 
   27031    23579165 :   gen_tmpl = most_general_template (tmpl);
   27032    23579165 :   gen_args = DECL_TI_ARGS (d);
   27033             : 
   27034             :   /* We should already have the extra args.  */
   27035    27207739 :   gcc_checking_assert (tmpl == gen_tmpl
   27036             :                        || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
   27037             :                            == TMPL_ARGS_DEPTH (gen_args)));
   27038             :   /* And what's in the hash table should match D.  */
   27039    23579165 :   gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
   27040             :                        == d
   27041             :                        || spec == NULL_TREE);
   27042             : 
   27043             :   /* This needs to happen before any tsubsting.  */
   27044    23579165 :   if (! push_tinst_level (d))
   27045             :     return d;
   27046             : 
   27047    23578468 :   auto_timevar tv (TV_TEMPLATE_INST);
   27048             : 
   27049             :   /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
   27050             :      for the instantiation.  */
   27051    23578468 :   td = template_for_substitution (d);
   27052    23578468 :   args = gen_args;
   27053             : 
   27054    23578468 :   if (variable_template_specialization_p (d))
   27055             :     {
   27056             :       /* Look up an explicit specialization, if any.  */
   27057      594434 :       tree partial_ti = most_specialized_partial_spec (d, tf_warning_or_error);
   27058      594434 :       if (partial_ti && partial_ti != error_mark_node)
   27059             :         {
   27060       42518 :           td = TI_TEMPLATE (partial_ti);
   27061       42518 :           args = TI_ARGS (partial_ti);
   27062             :         }
   27063             :     }
   27064             : 
   27065    23578468 :   code_pattern = DECL_TEMPLATE_RESULT (td);
   27066             : 
   27067             :   /* We should never be trying to instantiate a member of a class
   27068             :      template or partial specialization.  */
   27069    23578468 :   gcc_assert (d != code_pattern);
   27070             : 
   27071    47156936 :   if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
   27072    39714666 :       || DECL_TEMPLATE_SPECIALIZATION (td))
   27073             :     /* In the case of a friend template whose definition is provided
   27074             :        outside the class, we may have too many arguments.  Drop the
   27075             :        ones we don't need.  The same is true for specializations.  */
   27076     7442794 :     args = get_innermost_template_args
   27077     7442794 :       (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
   27078             : 
   27079    23578468 :   if (TREE_CODE (d) == FUNCTION_DECL)
   27080             :     {
   27081    20196921 :       deleted_p = DECL_DELETED_FN (code_pattern);
   27082    20196921 :       pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
   27083    19651740 :                           && DECL_INITIAL (code_pattern) != error_mark_node)
   27084      545186 :                          || DECL_DEFAULTED_FN (code_pattern)
   27085    20741967 :                          || deleted_p);
   27086             :     }
   27087             :   else
   27088             :     {
   27089     3381547 :       deleted_p = false;
   27090     3381547 :       if (DECL_CLASS_SCOPE_P (code_pattern))
   27091     2794691 :         pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
   27092             :       else
   27093      586856 :         pattern_defined = ! DECL_EXTERNAL (code_pattern);
   27094             :     }
   27095             : 
   27096             :   /* We may be in the middle of deferred access check.  Disable it now.  */
   27097    23578468 :   push_deferring_access_checks (dk_no_deferred);
   27098             : 
   27099             :   /* Unless an explicit instantiation directive has already determined
   27100             :      the linkage of D, remember that a definition is available for
   27101             :      this entity.  */
   27102    23578468 :   if (pattern_defined
   27103    20893474 :       && !DECL_INTERFACE_KNOWN (d)
   27104    42215574 :       && !DECL_NOT_REALLY_EXTERN (d))
   27105     9477050 :     mark_definable (d);
   27106             : 
   27107    23578468 :   DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
   27108    23578468 :   DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
   27109    23578468 :   input_location = DECL_SOURCE_LOCATION (d);
   27110             : 
   27111             :   /* If D is a member of an explicitly instantiated class template,
   27112             :      and no definition is available, treat it like an implicit
   27113             :      instantiation.  */
   27114    23578468 :   if (!pattern_defined && expl_inst_class_mem_p
   27115    23578468 :       && DECL_EXPLICIT_INSTANTIATION (d))
   27116             :     {
   27117             :       /* Leave linkage flags alone on instantiations with anonymous
   27118             :          visibility.  */
   27119         402 :       if (TREE_PUBLIC (d))
   27120             :         {
   27121         390 :           DECL_NOT_REALLY_EXTERN (d) = 0;
   27122         390 :           DECL_INTERFACE_KNOWN (d) = 0;
   27123             :         }
   27124         402 :       SET_DECL_IMPLICIT_INSTANTIATION (d);
   27125             :     }
   27126             : 
   27127             :   /* Defer all other templates, unless we have been explicitly
   27128             :      forbidden from doing so.  */
   27129    23578468 :   if (/* If there is no definition, we cannot instantiate the
   27130             :          template.  */
   27131             :       ! pattern_defined
   27132             :       /* If it's OK to postpone instantiation, do so.  */
   27133    23578468 :       || defer_ok
   27134             :       /* If this is a static data member that will be defined
   27135             :          elsewhere, we don't want to instantiate the entire data
   27136             :          member, but we do want to instantiate the initializer so that
   27137             :          we can substitute that elsewhere.  */
   27138    11446192 :       || (external_p && VAR_P (d))
   27139             :       /* Handle here a deleted function too, avoid generating
   27140             :          its body (c++/61080).  */
   27141    11403269 :       || deleted_p)
   27142             :     {
   27143             :       /* The definition of the static data member is now required so
   27144             :          we must substitute the initializer.  */
   27145    12175238 :       if (VAR_P (d)
   27146     2182919 :           && !DECL_INITIAL (d)
   27147    12388460 :           && DECL_INITIAL (code_pattern))
   27148             :         {
   27149      212718 :           tree ns;
   27150      212718 :           tree init;
   27151      212718 :           bool const_init = false;
   27152      212718 :           bool enter_context = DECL_CLASS_SCOPE_P (d);
   27153             : 
   27154      212718 :           ns = decl_namespace_context (d);
   27155      212718 :           push_nested_namespace (ns);
   27156      212718 :           if (enter_context)
   27157      212718 :             push_nested_class (DECL_CONTEXT (d));
   27158      212718 :           init = tsubst_expr (DECL_INITIAL (code_pattern),
   27159             :                               args,
   27160             :                               tf_warning_or_error, NULL_TREE);
   27161             :           /* If instantiating the initializer involved instantiating this
   27162             :              again, don't call cp_finish_decl twice.  */
   27163      209110 :           if (!DECL_INITIAL (d))
   27164             :             {
   27165             :               /* Make sure the initializer is still constant, in case of
   27166             :                  circular dependency (template/instantiate6.C). */
   27167      209106 :               const_init
   27168      209106 :                 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
   27169      209106 :               cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
   27170             :                               /*asmspec_tree=*/NULL_TREE, 0);
   27171             :             }
   27172      198326 :           if (enter_context)
   27173      198326 :             pop_nested_class ();
   27174      198326 :           pop_nested_namespace (ns);
   27175             :         }
   27176             : 
   27177             :       /* We restore the source position here because it's used by
   27178             :          add_pending_template.  */
   27179    12160846 :       input_location = saved_loc;
   27180             : 
   27181     8823248 :       if (at_eof && !pattern_defined
   27182     2517285 :           && DECL_EXPLICIT_INSTANTIATION (d)
   27183    12160866 :           && DECL_NOT_REALLY_EXTERN (d))
   27184             :         /* [temp.explicit]
   27185             : 
   27186             :            The definition of a non-exported function template, a
   27187             :            non-exported member function template, or a non-exported
   27188             :            member function or static data member of a class template
   27189             :            shall be present in every translation unit in which it is
   27190             :            explicitly instantiated.  */
   27191          20 :         permerror (input_location,  "explicit instantiation of %qD "
   27192             :                    "but no definition available", d);
   27193             : 
   27194             :       /* If we're in unevaluated context, we just wanted to get the
   27195             :          constant value; this isn't an odr use, so don't queue
   27196             :          a full instantiation.  */
   27197    12160846 :       if (!cp_unevaluated_operand
   27198             :           /* ??? Historically, we have instantiated inline functions, even
   27199             :              when marked as "extern template".  */
   27200    12160830 :           && !(external_p && VAR_P (d)))
   27201    12117907 :         add_pending_template (d);
   27202             :     }
   27203             :   else
   27204             :     {
   27205    11403230 :       set_instantiating_module (d);
   27206    11403230 :       if (variable_template_p (gen_tmpl))
   27207      594344 :         note_variable_template_instantiation (d);
   27208    11403230 :       instantiate_body (td, args, d, false);
   27209             :     }
   27210             : 
   27211    23564065 :   pop_deferring_access_checks ();
   27212    23564065 :   pop_tinst_level ();
   27213    23564065 :   input_location = saved_loc;
   27214    23564065 :   cp_unevaluated_operand = saved_unevaluated_operand;
   27215    23564065 :   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
   27216             : 
   27217    23564065 :   return d;
   27218    23564065 : }
   27219             : 
   27220             : /* Run through the list of templates that we wish we could
   27221             :    instantiate, and instantiate any we can.  RETRIES is the
   27222             :    number of times we retry pending template instantiation.  */
   27223             : 
   27224             : void
   27225      125772 : instantiate_pending_templates (int retries)
   27226             : {
   27227      125772 :   int reconsider;
   27228      125772 :   location_t saved_loc = input_location;
   27229             : 
   27230             :   /* Instantiating templates may trigger vtable generation.  This in turn
   27231             :      may require further template instantiations.  We place a limit here
   27232             :      to avoid infinite loop.  */
   27233      125772 :   if (pending_templates && retries >= max_tinst_depth)
   27234             :     {
   27235           4 :       tree decl = pending_templates->tinst->maybe_get_node ();
   27236             : 
   27237           4 :       fatal_error (input_location,
   27238             :                    "template instantiation depth exceeds maximum of %d"
   27239             :                    " instantiating %q+D, possibly from virtual table generation"
   27240             :                    " (use %<-ftemplate-depth=%> to increase the maximum)",
   27241             :                    max_tinst_depth, decl);
   27242             :       if (TREE_CODE (decl) == FUNCTION_DECL)
   27243             :         /* Pretend that we defined it.  */
   27244             :         DECL_INITIAL (decl) = error_mark_node;
   27245             :       return;
   27246             :     }
   27247             : 
   27248      156086 :   do
   27249             :     {
   27250      156086 :       struct pending_template **t = &pending_templates;
   27251      156086 :       struct pending_template *last = NULL;
   27252      156086 :       reconsider = 0;
   27253      156086 :       while (*t)
   27254             :         {
   27255    10949883 :           tree instantiation = reopen_tinst_level ((*t)->tinst);
   27256    10949883 :           bool complete = false;
   27257             : 
   27258    10949883 :           if (TYPE_P (instantiation))
   27259             :             {
   27260           0 :               if (!COMPLETE_TYPE_P (instantiation))
   27261             :                 {
   27262           0 :                   instantiate_class_template (instantiation);
   27263           0 :                   if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
   27264           0 :                     for (tree fld = TYPE_FIELDS (instantiation);
   27265           0 :                          fld; fld = TREE_CHAIN (fld))
   27266           0 :                       if ((VAR_P (fld)
   27267           0 :                            || (TREE_CODE (fld) == FUNCTION_DECL
   27268           0 :                                && !DECL_ARTIFICIAL (fld)))
   27269           0 :                           && DECL_TEMPLATE_INSTANTIATION (fld))
   27270           0 :                         instantiate_decl (fld,
   27271             :                                           /*defer_ok=*/false,
   27272             :                                           /*expl_inst_class_mem_p=*/false);
   27273             : 
   27274           0 :                   if (COMPLETE_TYPE_P (instantiation))
   27275           0 :                     reconsider = 1;
   27276             :                 }
   27277             : 
   27278           0 :               complete = COMPLETE_TYPE_P (instantiation);
   27279             :             }
   27280             :           else
   27281             :             {
   27282    10949883 :               if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
   27283    10949883 :                   && !DECL_TEMPLATE_INSTANTIATED (instantiation))
   27284             :                 {
   27285     9837521 :                   instantiation
   27286     9837521 :                     = instantiate_decl (instantiation,
   27287             :                                         /*defer_ok=*/false,
   27288             :                                         /*expl_inst_class_mem_p=*/false);
   27289     9837510 :                   if (DECL_TEMPLATE_INSTANTIATED (instantiation))
   27290     8327815 :                     reconsider = 1;
   27291             :                 }
   27292             : 
   27293    10949872 :               complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
   27294    10949872 :                           || DECL_TEMPLATE_INSTANTIATED (instantiation));
   27295             :             }
   27296             : 
   27297           0 :           if (complete)
   27298             :             {
   27299             :               /* If INSTANTIATION has been instantiated, then we don't
   27300             :                  need to consider it again in the future.  */
   27301     9440177 :               struct pending_template *drop = *t;
   27302     9440177 :               *t = (*t)->next;
   27303     9440177 :               set_refcount_ptr (drop->tinst);
   27304     9440177 :               pending_template_freelist ().free (drop);
   27305             :             }
   27306             :           else
   27307             :             {
   27308     1509695 :               last = *t;
   27309     1509695 :               t = &(*t)->next;
   27310             :             }
   27311    10949872 :           tinst_depth = 0;
   27312    22055830 :           set_refcount_ptr (current_tinst_level);
   27313             :         }
   27314      156075 :       last_pending_template = last;
   27315             :     }
   27316      156075 :   while (reconsider);
   27317             : 
   27318      125757 :   input_location = saved_loc;
   27319             : }
   27320             : 
   27321             : /* Substitute ARGVEC into T, which is a list of initializers for
   27322             :    either base class or a non-static data member.  The TREE_PURPOSEs
   27323             :    are DECLs, and the TREE_VALUEs are the initializer values.  Used by
   27324             :    instantiate_decl.  */
   27325             : 
   27326             : static tree
   27327     1589138 : tsubst_initializer_list (tree t, tree argvec)
   27328             : {
   27329     1589138 :   tree inits = NULL_TREE;
   27330     1589138 :   tree target_ctor = error_mark_node;
   27331             : 
   27332     3328494 :   for (; t; t = TREE_CHAIN (t))
   27333             :     {
   27334     1739359 :       tree decl;
   27335     1739359 :       tree init;
   27336     1739359 :       tree expanded_bases = NULL_TREE;
   27337     1739359 :       tree expanded_arguments = NULL_TREE;
   27338     1739359 :       int i, len = 1;
   27339             : 
   27340     1739359 :       if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
   27341             :         {
   27342          28 :           tree expr;
   27343          28 :           tree arg;
   27344             : 
   27345             :           /* Expand the base class expansion type into separate base
   27346             :              classes.  */
   27347          28 :           expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
   27348             :                                                  tf_warning_or_error,
   27349             :                                                  NULL_TREE);
   27350          28 :           if (expanded_bases == error_mark_node)
   27351           0 :             continue;
   27352             : 
   27353             :           /* We'll be building separate TREE_LISTs of arguments for
   27354             :              each base.  */
   27355          28 :           len = TREE_VEC_LENGTH (expanded_bases);
   27356          28 :           expanded_arguments = make_tree_vec (len);
   27357          97 :           for (i = 0; i < len; i++)
   27358          41 :             TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
   27359             : 
   27360             :           /* Build a dummy EXPR_PACK_EXPANSION that will be used to
   27361             :              expand each argument in the TREE_VALUE of t.  */
   27362          28 :           expr = make_node (EXPR_PACK_EXPANSION);
   27363          28 :           PACK_EXPANSION_LOCAL_P (expr) = true;
   27364          56 :           PACK_EXPANSION_PARAMETER_PACKS (expr) =
   27365          28 :             PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
   27366             : 
   27367          28 :           if (TREE_VALUE (t) == void_type_node)
   27368             :             /* VOID_TYPE_NODE is used to indicate
   27369             :                value-initialization.  */
   27370             :             {
   27371           9 :               for (i = 0; i < len; i++)
   27372           3 :                 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
   27373             :             }
   27374             :           else
   27375             :             {
   27376             :               /* Substitute parameter packs into each argument in the
   27377             :                  TREE_LIST.  */
   27378          22 :               in_base_initializer = 1;
   27379          53 :               for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
   27380             :                 {
   27381          31 :                   tree expanded_exprs;
   27382             : 
   27383             :                   /* Expand the argument.  */
   27384          31 :                   tree value;
   27385          31 :                   if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
   27386             :                     value = TREE_VALUE (arg);
   27387             :                   else
   27388             :                     {
   27389          28 :                       value = expr;
   27390          28 :                       PACK_EXPANSION_PATTERN (value) = TREE_VALUE (arg);
   27391             :                     }
   27392          31 :                   expanded_exprs
   27393          31 :                     = tsubst_pack_expansion (value, argvec,
   27394             :                                              tf_warning_or_error,
   27395             :                                              NULL_TREE);
   27396          31 :                   if (expanded_exprs == error_mark_node)
   27397           3 :                     continue;
   27398             : 
   27399             :                   /* Prepend each of the expanded expressions to the
   27400             :                      corresponding TREE_LIST in EXPANDED_ARGUMENTS.  */
   27401          87 :                   for (i = 0; i < len; i++)
   27402          59 :                     if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
   27403          36 :                       for (int j = 0; j < TREE_VEC_LENGTH (expanded_exprs); j++)
   27404          27 :                         TREE_VEC_ELT (expanded_arguments, i)
   27405          54 :                           = tree_cons (NULL_TREE,
   27406          27 :                                        TREE_VEC_ELT (expanded_exprs, j),
   27407          27 :                                        TREE_VEC_ELT (expanded_arguments, i));
   27408             :                     else
   27409          50 :                       TREE_VEC_ELT (expanded_arguments, i)
   27410         100 :                         = tree_cons (NULL_TREE,
   27411          50 :                                      TREE_VEC_ELT (expanded_exprs, i),
   27412          50 :                                      TREE_VEC_ELT (expanded_arguments, i));
   27413             :                 }
   27414          22 :               in_base_initializer = 0;
   27415             : 
   27416             :               /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
   27417             :                  since we built them backwards.  */
   27418          60 :               for (i = 0; i < len; i++)
   27419             :                 {
   27420          38 :                   TREE_VEC_ELT (expanded_arguments, i) =
   27421          38 :                     nreverse (TREE_VEC_ELT (expanded_arguments, i));
   27422             :                 }
   27423             :             }
   27424             :         }
   27425             : 
   27426     3478728 :       for (i = 0; i < len; ++i)
   27427             :         {
   27428     1739372 :           if (expanded_bases)
   27429             :             {
   27430          41 :               decl = TREE_VEC_ELT (expanded_bases, i);
   27431          41 :               decl = expand_member_init (decl);
   27432          41 :               init = TREE_VEC_ELT (expanded_arguments, i);
   27433             :             }
   27434             :           else
   27435             :             {
   27436     1739331 :               tree tmp;
   27437     1739331 :               decl = tsubst_copy (TREE_PURPOSE (t), argvec, 
   27438             :                                   tf_warning_or_error, NULL_TREE);
   27439             : 
   27440     1739331 :               decl = expand_member_init (decl);
   27441     1739331 :               if (decl && !DECL_P (decl))
   27442      367359 :                 in_base_initializer = 1;
   27443             : 
   27444     1739331 :               init = TREE_VALUE (t);
   27445     1739331 :               tmp = init;
   27446     1739331 :               if (init != void_type_node)
   27447     1432041 :                 init = tsubst_expr (init, argvec,
   27448             :                                     tf_warning_or_error, NULL_TREE);
   27449     1739331 :               if (init == NULL_TREE && tmp != NULL_TREE)
   27450             :                 /* If we had an initializer but it instantiated to nothing,
   27451             :                    value-initialize the object.  This will only occur when
   27452             :                    the initializer was a pack expansion where the parameter
   27453             :                    packs used in that expansion were of length zero.  */
   27454         422 :                 init = void_type_node;
   27455     1739331 :               in_base_initializer = 0;
   27456             :             }
   27457             : 
   27458     1739372 :           if (target_ctor != error_mark_node
   27459           3 :               && init != error_mark_node)
   27460             :             {
   27461           3 :               error ("mem-initializer for %qD follows constructor delegation",
   27462             :                      decl);
   27463           3 :               return inits;
   27464             :             }
   27465             :           /* Look for a target constructor. */
   27466     1739369 :           if (init != error_mark_node
   27467     1739355 :               && decl && CLASS_TYPE_P (decl)
   27468     1743281 :               && same_type_p (decl, current_class_type))
   27469             :             {
   27470        3912 :               maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
   27471        3912 :               if (inits)
   27472             :                 {
   27473           0 :                   error ("constructor delegation follows mem-initializer for %qD",
   27474           0 :                          TREE_PURPOSE (inits));
   27475           0 :                   continue;
   27476             :                 }
   27477             :               target_ctor = init;
   27478             :             }
   27479             : 
   27480     1739369 :           if (decl)
   27481             :             {
   27482     1739362 :               init = build_tree_list (decl, init);
   27483             :               /* Carry over the dummy TREE_TYPE node containing the source
   27484             :                  location.  */
   27485     1739362 :               TREE_TYPE (init) = TREE_TYPE (t);
   27486     1739362 :               TREE_CHAIN (init) = inits;
   27487     1739362 :               inits = init;
   27488             :             }
   27489             :         }
   27490             :     }
   27491             :   return inits;
   27492             : }
   27493             : 
   27494             : /* Instantiate an enumerated type.  TAG is the template type, NEWTAG
   27495             :    is the instantiation (which should have been created with
   27496             :    start_enum) and ARGS are the template arguments to use.  */
   27497             : 
   27498             : static void
   27499      231268 : tsubst_enum (tree tag, tree newtag, tree args)
   27500             : {
   27501      231268 :   tree e;
   27502             : 
   27503      231268 :   if (SCOPED_ENUM_P (newtag))
   27504         334 :     begin_scope (sk_scoped_enum, newtag);
   27505             : 
   27506      471611 :   for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
   27507             :     {
   27508      240403 :       tree value;
   27509      240403 :       tree decl = TREE_VALUE (e);
   27510             : 
   27511             :       /* Note that in a template enum, the TREE_VALUE is the
   27512             :          CONST_DECL, not the corresponding INTEGER_CST.  */
   27513      240403 :       value = tsubst_expr (DECL_INITIAL (decl),
   27514             :                            args, tf_warning_or_error, NULL_TREE);
   27515             : 
   27516             :       /* Give this enumeration constant the correct access.  */
   27517      240343 :       set_current_access_from_decl (decl);
   27518             : 
   27519             :       /* Actually build the enumerator itself.  Here we're assuming that
   27520             :          enumerators can't have dependent attributes.  */
   27521      240343 :       tree newdecl = build_enumerator (DECL_NAME (decl), value, newtag,
   27522      240343 :                                        DECL_ATTRIBUTES (decl),
   27523      240343 :                                        DECL_SOURCE_LOCATION (decl));
   27524             :       /* Attribute deprecated without an argument isn't sticky: it'll
   27525             :          melt into a tree flag, so we need to propagate the flag here,
   27526             :          since we just created a new enumerator.  */
   27527      240343 :       TREE_DEPRECATED (newdecl) = TREE_DEPRECATED (decl);
   27528      240343 :       TREE_UNAVAILABLE (newdecl) = TREE_UNAVAILABLE (decl);
   27529             :     }
   27530             : 
   27531      231208 :   if (SCOPED_ENUM_P (newtag))
   27532         334 :     finish_scope ();
   27533             : 
   27534      231208 :   finish_enum_value_list (newtag);
   27535      231208 :   finish_enum (newtag);
   27536             : 
   27537      231208 :   DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
   27538      231208 :     = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
   27539      231208 :   TREE_DEPRECATED (newtag) = TREE_DEPRECATED (tag);
   27540      231208 :   TREE_UNAVAILABLE (newtag) = TREE_UNAVAILABLE (tag);
   27541      231208 : }
   27542             : 
   27543             : /* DECL is a FUNCTION_DECL that is a template specialization.  Return
   27544             :    its type -- but without substituting the innermost set of template
   27545             :    arguments.  So, innermost set of template parameters will appear in
   27546             :    the type.  */
   27547             : 
   27548             : tree
   27549     7194897 : get_mostly_instantiated_function_type (tree decl)
   27550             : {
   27551             :   /* For a function, DECL_TI_TEMPLATE is partially instantiated.  */
   27552     7194897 :   return TREE_TYPE (DECL_TI_TEMPLATE (decl));
   27553             : }
   27554             : 
   27555             : /* Return truthvalue if we're processing a template different from
   27556             :    the last one involved in diagnostics.  */
   27557             : bool
   27558      177594 : problematic_instantiation_changed (void)
   27559             : {
   27560      177594 :   return current_tinst_level != last_error_tinst_level;
   27561             : }
   27562             : 
   27563             : /* Remember current template involved in diagnostics.  */
   27564             : void
   27565        3981 : record_last_problematic_instantiation (void)
   27566             : {
   27567        3981 :   set_refcount_ptr (last_error_tinst_level, current_tinst_level);
   27568        3981 : }
   27569             : 
   27570             : struct tinst_level *
   27571   186489639 : current_instantiation (void)
   27572             : {
   27573   186489639 :   return current_tinst_level;
   27574             : }
   27575             : 
   27576             : /* Return TRUE if current_function_decl is being instantiated, false
   27577             :    otherwise.  */
   27578             : 
   27579             : bool
   27580   136007729 : instantiating_current_function_p (void)
   27581             : {
   27582   136007729 :   return (current_instantiation ()
   27583   136007729 :           && (current_instantiation ()->maybe_get_node ()
   27584      996419 :               == current_function_decl));
   27585             : }
   27586             : 
   27587             : /* [temp.param] Check that template non-type parm TYPE is of an allowable
   27588             :    type.  Return false for ok, true for disallowed.  Issue error and
   27589             :    inform messages under control of COMPLAIN.  */
   27590             : 
   27591             : static bool
   27592   123011561 : invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
   27593             : {
   27594   123011561 :   if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
   27595             :     return false;
   27596     3903812 :   else if (TYPE_PTR_P (type))
   27597             :     return false;
   27598     3901319 :   else if (TYPE_REF_P (type)
   27599     3901319 :            && !TYPE_REF_IS_RVALUE (type))
   27600             :     return false;
   27601     3900463 :   else if (TYPE_PTRMEM_P (type))
   27602             :     return false;
   27603     3896962 :   else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
   27604             :     {
   27605     1813828 :       if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
   27606             :         {
   27607           1 :           if (complain & tf_error)
   27608           1 :             error ("non-type template parameters of deduced class type only "
   27609             :                    "available with %<-std=c++20%> or %<-std=gnu++20%>");
   27610           1 :           return true;
   27611             :         }
   27612             :       return false;
   27613             :     }
   27614     2083134 :   else if (TREE_CODE (type) == NULLPTR_TYPE)
   27615             :     return false;
   27616     2083101 :   else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
   27617          15 :            && cxx_dialect < cxx11)
   27618             :     /* Fall through; before C++11 alias templates, a bound ttp
   27619             :        always instantiates into a class type.  */;
   27620     2083101 :   else if (WILDCARD_TYPE_P (type))
   27621             :     /* Any other wildcard type not already handled above is allowed.  */
   27622             :     return false;
   27623             :   else if (TREE_CODE (type) == COMPLEX_TYPE)
   27624             :     /* Fall through.  */;
   27625             :   else if (VOID_TYPE_P (type))
   27626             :     /* Fall through.  */;
   27627         883 :   else if (cxx_dialect >= cxx20)
   27628             :     {
   27629         780 :       if (dependent_type_p (type))
   27630             :         return false;
   27631         767 :       if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
   27632             :         return true;
   27633         753 :       if (structural_type_p (type))
   27634             :         return false;
   27635           5 :       if (complain & tf_error)
   27636             :         {
   27637           3 :           auto_diagnostic_group d;
   27638           3 :           error ("%qT is not a valid type for a template non-type "
   27639             :                  "parameter because it is not structural", type);
   27640           3 :           structural_type_p (type, true);
   27641           3 :         }
   27642           5 :       return true;
   27643             :     }
   27644         103 :   else if (CLASS_TYPE_P (type))
   27645             :     {
   27646          23 :       if (complain & tf_error)
   27647          18 :         error ("non-type template parameters of class type only available "
   27648             :                "with %<-std=c++20%> or %<-std=gnu++20%>");
   27649          23 :       return true;
   27650             :     }
   27651             : 
   27652         184 :   if (complain & tf_error)
   27653             :     {
   27654         174 :       if (type == error_mark_node)
   27655          16 :         inform (input_location, "invalid template non-type parameter");
   27656             :       else
   27657         158 :         error ("%q#T is not a valid type for a template non-type parameter",
   27658             :                type);
   27659             :     }
   27660             :   return true;
   27661             : }
   27662             : 
   27663             : /* Returns true iff the noexcept-specifier for TYPE is value-dependent.  */
   27664             : 
   27665             : static bool
   27666     3772257 : value_dependent_noexcept_spec_p (tree type)
   27667             : {
   27668     3772257 :   if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
   27669     1389775 :     if (tree noex = TREE_PURPOSE (spec))
   27670             :       /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
   27671             :          affect overload resolution and treating it as dependent breaks
   27672             :          things.  Same for an unparsed noexcept expression.  */
   27673     1388394 :       if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
   27674     1388394 :           && TREE_CODE (noex) != DEFERRED_PARSE
   27675     1388394 :           && value_dependent_expression_p (noex))
   27676             :         return true;
   27677             : 
   27678             :   return false;
   27679             : }
   27680             : 
   27681             : /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
   27682             :    Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
   27683             : 
   27684             : static bool
   27685   313522590 : dependent_type_p_r (tree type)
   27686             : {
   27687   313522590 :   tree scope;
   27688             : 
   27689             :   /* [temp.dep.type]
   27690             : 
   27691             :      A type is dependent if it is:
   27692             : 
   27693             :      -- a template parameter. Template template parameters are types
   27694             :         for us (since TYPE_P holds true for them) so we handle
   27695             :         them here.  */
   27696   313522590 :   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
   27697   313522590 :       || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
   27698             :     return true;
   27699             :   /* -- a qualified-id with a nested-name-specifier which contains a
   27700             :         class-name that names a dependent type or whose unqualified-id
   27701             :         names a dependent type.  */
   27702   244217065 :   if (TREE_CODE (type) == TYPENAME_TYPE)
   27703             :     return true;
   27704             : 
   27705             :   /* An alias template specialization can be dependent even if the
   27706             :      resulting type is not.  */
   27707   229980565 :   if (dependent_alias_template_spec_p (type, nt_transparent))
   27708             :     return true;
   27709             : 
   27710             :   /* -- a cv-qualified type where the cv-unqualified type is
   27711             :         dependent.
   27712             :      No code is necessary for this bullet; the code below handles
   27713             :      cv-qualified types, and we don't want to strip aliases with
   27714             :      TYPE_MAIN_VARIANT because of DR 1558.  */
   27715             :   /* -- a compound type constructed from any dependent type.  */
   27716   229900101 :   if (TYPE_PTRMEM_P (type))
   27717      782561 :     return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
   27718      782561 :             || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
   27719             :                                            (type)));
   27720   229117540 :   else if (INDIRECT_TYPE_P (type))
   27721    43637466 :     return dependent_type_p (TREE_TYPE (type));
   27722   185480074 :   else if (FUNC_OR_METHOD_TYPE_P (type))
   27723             :     {
   27724    44573502 :       tree arg_type;
   27725             : 
   27726    44573502 :       if (dependent_type_p (TREE_TYPE (type)))
   27727             :         return true;
   27728    25058287 :       for (arg_type = TYPE_ARG_TYPES (type);
   27729    34737749 :            arg_type;
   27730     9679462 :            arg_type = TREE_CHAIN (arg_type))
   27731    31265040 :         if (dependent_type_p (TREE_VALUE (arg_type)))
   27732             :           return true;
   27733     3472709 :       if (cxx_dialect >= cxx17
   27734     3472709 :           && value_dependent_noexcept_spec_p (type))
   27735             :         /* A value-dependent noexcept-specifier makes the type dependent.  */
   27736             :         return true;
   27737     3472683 :       return false;
   27738             :     }
   27739             :   /* -- an array type constructed from any dependent type or whose
   27740             :         size is specified by a constant expression that is
   27741             :         value-dependent.
   27742             : 
   27743             :         We checked for type- and value-dependence of the bounds in
   27744             :         compute_array_index_type, so TYPE_DEPENDENT_P is already set.  */
   27745   140906572 :   if (TREE_CODE (type) == ARRAY_TYPE)
   27746             :     {
   27747        8400 :       if (TYPE_DOMAIN (type)
   27748        8400 :           && dependent_type_p (TYPE_DOMAIN (type)))
   27749             :         return true;
   27750        8400 :       return dependent_type_p (TREE_TYPE (type));
   27751             :     }
   27752             : 
   27753             :   /* -- a template-id in which either the template name is a template
   27754             :      parameter ...  */
   27755   140898172 :   if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
   27756             :     return true;
   27757             :   /* ... or any of the template arguments is a dependent type or
   27758             :         an expression that is type-dependent or value-dependent.  */
   27759    72397444 :   else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
   27760   210291137 :            && (any_dependent_template_arguments_p
   27761    69553102 :                (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
   27762             :     return true;
   27763             : 
   27764             :   /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and TRAIT_TYPEs are
   27765             :      dependent; if the argument of the `typeof' expression is not
   27766             :      type-dependent, then it should already been have resolved.  */
   27767    94633169 :   if (TREE_CODE (type) == TYPEOF_TYPE
   27768    94633169 :       || TREE_CODE (type) == DECLTYPE_TYPE
   27769    82284356 :       || TREE_CODE (type) == TRAIT_TYPE)
   27770             :     return true;
   27771             : 
   27772             :   /* A template argument pack is dependent if any of its packed
   27773             :      arguments are.  */
   27774    82238498 :   if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
   27775             :     {
   27776         159 :       tree args = ARGUMENT_PACK_ARGS (type);
   27777         207 :       for (tree arg : tree_vec_range (args))
   27778         151 :         if (dependent_template_arg_p (arg))
   27779         103 :           return true;
   27780             :     }
   27781             : 
   27782             :   /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
   27783             :      be template parameters.  */
   27784    82238395 :   if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
   27785             :     return true;
   27786             : 
   27787    74303677 :   if (TREE_CODE (type) == DEPENDENT_OPERATOR_TYPE)
   27788             :     return true;
   27789             : 
   27790    28573387 :   if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
   27791             :     return true;
   27792             : 
   27793             :   /* The standard does not specifically mention types that are local
   27794             :      to template functions or local classes, but they should be
   27795             :      considered dependent too.  For example:
   27796             : 
   27797             :        template <int I> void f() {
   27798             :          enum E { a = I };
   27799             :          S<sizeof (E)> s;
   27800             :        }
   27801             : 
   27802             :      The size of `E' cannot be known until the value of `I' has been
   27803             :      determined.  Therefore, `E' must be considered dependent.  */
   27804    28573387 :   scope = TYPE_CONTEXT (type);
   27805    28573387 :   if (scope && TYPE_P (scope))
   27806     1600783 :     return dependent_type_p (scope);
   27807             :   /* Don't use type_dependent_expression_p here, as it can lead
   27808             :      to infinite recursion trying to determine whether a lambda
   27809             :      nested in a lambda is dependent (c++/47687).  */
   27810    25223287 :   else if (scope && TREE_CODE (scope) == FUNCTION_DECL
   27811      355968 :            && DECL_LANG_SPECIFIC (scope)
   27812      355968 :            && DECL_TEMPLATE_INFO (scope)
   27813      349541 :            && (any_dependent_template_arguments_p
   27814      349541 :                (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
   27815             :     return true;
   27816             : 
   27817             :   /* Other types are non-dependent.  */
   27818             :   return false;
   27819             : }
   27820             : 
   27821             : /* Returns TRUE if TYPE is dependent, in the sense of
   27822             :    [temp.dep.type].  Note that a NULL type is considered dependent.  */
   27823             : 
   27824             : bool
   27825 10321688486 : dependent_type_p (tree type)
   27826             : {
   27827             :   /* If there are no template parameters in scope, then there can't be
   27828             :      any dependent types.  */
   27829 10321688486 :   if (!processing_template_decl)
   27830             :     {
   27831             :       /* If we are not processing a template, then nobody should be
   27832             :          providing us with a dependent type.  */
   27833  1403866662 :       gcc_assert (type);
   27834  1403866662 :       gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
   27835             :       return false;
   27836             :     }
   27837             : 
   27838             :   /* If the type is NULL, we have not computed a type for the entity
   27839             :      in question; in that case, the type is dependent.  */
   27840  8917821824 :   if (!type)
   27841             :     return true;
   27842             : 
   27843             :   /* Erroneous types can be considered non-dependent.  */
   27844  8786990967 :   if (type == error_mark_node)
   27845             :     return false;
   27846             : 
   27847             :   /* If we have not already computed the appropriate value for TYPE,
   27848             :      do so now.  */
   27849  8786990843 :   if (!TYPE_DEPENDENT_P_VALID (type))
   27850             :     {
   27851   313522590 :       TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
   27852   313522590 :       TYPE_DEPENDENT_P_VALID (type) = 1;
   27853             :     }
   27854             : 
   27855  8786990843 :   return TYPE_DEPENDENT_P (type);
   27856             : }
   27857             : 
   27858             : /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
   27859             :    lookup.  In other words, a dependent type that is not the current
   27860             :    instantiation.  */
   27861             : 
   27862             : bool
   27863   651225460 : dependent_scope_p (tree scope)
   27864             : {
   27865   651225460 :   return (scope && TYPE_P (scope) && dependent_type_p (scope)
   27866  1077692792 :           && !currently_open_class (scope));
   27867             : }
   27868             : 
   27869             : /* True if we might find more declarations in SCOPE during instantiation than
   27870             :    we can when parsing the template.  */
   27871             : 
   27872             : bool
   27873   121852872 : dependentish_scope_p (tree scope)
   27874             : {
   27875   121852872 :   return dependent_scope_p (scope) || any_dependent_bases_p (scope);
   27876             : }
   27877             : 
   27878             : /* T is a SCOPE_REF.  Return whether it represents a non-static member of
   27879             :    an unknown base of 'this' (and is therefore instantiation-dependent).  */
   27880             : 
   27881             : static bool
   27882     9100394 : unknown_base_ref_p (tree t)
   27883             : {
   27884     9100394 :   if (!current_class_ptr)
   27885             :     return false;
   27886             : 
   27887     6246410 :   tree mem = TREE_OPERAND (t, 1);
   27888     6246410 :   if (shared_member_p (mem))
   27889             :     return false;
   27890             : 
   27891        9668 :   tree cur = current_nonlambda_class_type ();
   27892        9668 :   if (!any_dependent_bases_p (cur))
   27893             :     return false;
   27894             : 
   27895          25 :   tree ctx = TREE_OPERAND (t, 0);
   27896          25 :   if (DERIVED_FROM_P (ctx, cur))
   27897             :     return false;
   27898             : 
   27899             :   return true;
   27900             : }
   27901             : 
   27902             : /* T is a SCOPE_REF; return whether we need to consider it
   27903             :     instantiation-dependent so that we can check access at instantiation
   27904             :     time even though we know which member it resolves to.  */
   27905             : 
   27906             : static bool
   27907    11814881 : instantiation_dependent_scope_ref_p (tree t)
   27908             : {
   27909    11814881 :   if (DECL_P (TREE_OPERAND (t, 1))
   27910     9100398 :       && CLASS_TYPE_P (TREE_OPERAND (t, 0))
   27911     9100398 :       && !dependent_scope_p (TREE_OPERAND (t, 0))
   27912     9100394 :       && !unknown_base_ref_p (t)
   27913    20915260 :       && accessible_in_template_p (TREE_OPERAND (t, 0),
   27914     9100379 :                                    TREE_OPERAND (t, 1)))
   27915             :     return false;
   27916             :   else
   27917     2721546 :     return true;
   27918             : }
   27919             : 
   27920             : /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
   27921             :    [temp.dep.constexpr].  EXPRESSION is already known to be a constant
   27922             :    expression.  */
   27923             : 
   27924             : /* Note that this predicate is not appropriate for general expressions;
   27925             :    only constant expressions (that satisfy potential_constant_expression)
   27926             :    can be tested for value dependence.  */
   27927             : 
   27928             : bool
   27929   854174065 : value_dependent_expression_p (tree expression)
   27930             : {
   27931   856911135 :   if (!processing_template_decl || expression == NULL_TREE)
   27932             :     return false;
   27933             : 
   27934             :   /* A type-dependent expression is also value-dependent.  */
   27935   716922285 :   if (type_dependent_expression_p (expression))
   27936             :     return true;
   27937             : 
   27938   605257006 :   switch (TREE_CODE (expression))
   27939             :     {
   27940     4896307 :     case BASELINK:
   27941             :       /* A dependent member function of the current instantiation.  */
   27942     4896307 :       return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
   27943             : 
   27944     9243765 :     case FUNCTION_DECL:
   27945             :       /* A dependent member function of the current instantiation.  */
   27946    18487530 :       if (DECL_CLASS_SCOPE_P (expression)
   27947     9279980 :           && dependent_type_p (DECL_CONTEXT (expression)))
   27948             :         return true;
   27949             :       break;
   27950             : 
   27951             :     case IDENTIFIER_NODE:
   27952             :       /* A name that has not been looked up -- must be dependent.  */
   27953             :       return true;
   27954             : 
   27955             :     case TEMPLATE_PARM_INDEX:
   27956             :       /* A non-type template parm.  */
   27957             :       return true;
   27958             : 
   27959     1673366 :     case CONST_DECL:
   27960             :       /* A non-type template parm.  */
   27961     1673366 :       if (DECL_TEMPLATE_PARM_P (expression))
   27962             :         return true;
   27963     1673366 :       return value_dependent_expression_p (DECL_INITIAL (expression));
   27964             : 
   27965    35074092 :     case VAR_DECL:
   27966             :        /* A constant with literal type and is initialized
   27967             :           with an expression that is value-dependent.  */
   27968    35074092 :       if (DECL_DEPENDENT_INIT_P (expression))
   27969             :         return true;
   27970    25753003 :       if (DECL_HAS_VALUE_EXPR_P (expression))
   27971             :         {
   27972        7121 :           tree value_expr = DECL_VALUE_EXPR (expression);
   27973        7121 :           if (value_dependent_expression_p (value_expr)
   27974             :               /* __PRETTY_FUNCTION__ inside a template function is dependent
   27975             :                  on the name of the function.  */
   27976        7121 :               || (DECL_PRETTY_FUNCTION_P (expression)
   27977             :                   /* It might be used in a template, but not a template
   27978             :                      function, in which case its DECL_VALUE_EXPR will be
   27979             :                      "top level".  */
   27980           6 :                   && value_expr == error_mark_node))
   27981             :             return true;
   27982             :         }
   27983    25745882 :       else if (TYPE_REF_P (TREE_TYPE (expression)))
   27984             :         /* FIXME cp_finish_decl doesn't fold reference initializers.  */
   27985             :         return true;
   27986             :       /* We have a constexpr variable and we're processing a template.  When
   27987             :          there's lifetime extension involved (for which finish_compound_literal
   27988             :          used to create a temporary), we'll not be able to evaluate the
   27989             :          variable until instantiating, so pretend it's value-dependent.  */
   27990    25518384 :       else if (DECL_DECLARED_CONSTEXPR_P (expression)
   27991    25518384 :                && !TREE_CONSTANT (expression))
   27992             :         return true;
   27993             :       return false;
   27994             : 
   27995     4503020 :     case DYNAMIC_CAST_EXPR:
   27996     4503020 :     case STATIC_CAST_EXPR:
   27997     4503020 :     case CONST_CAST_EXPR:
   27998     4503020 :     case REINTERPRET_CAST_EXPR:
   27999     4503020 :     case CAST_EXPR:
   28000     4503020 :     case IMPLICIT_CONV_EXPR:
   28001             :       /* These expressions are value-dependent if the type to which
   28002             :          the cast occurs is dependent or the expression being casted
   28003             :          is value-dependent.  */
   28004     4503020 :       {
   28005     4503020 :         tree type = TREE_TYPE (expression);
   28006             : 
   28007     4503020 :         if (dependent_type_p (type))
   28008             :           return true;
   28009             : 
   28010             :         /* A functional cast has a list of operands.  */
   28011     4503020 :         expression = TREE_OPERAND (expression, 0);
   28012     4503020 :         if (!expression)
   28013             :           {
   28014             :             /* If there are no operands, it must be an expression such
   28015             :                as "int()". This should not happen for aggregate types
   28016             :                because it would form non-constant expressions.  */
   28017       18860 :             gcc_assert (cxx_dialect >= cxx11
   28018             :                         || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
   28019             : 
   28020             :             return false;
   28021             :           }
   28022             : 
   28023     4484160 :         if (TREE_CODE (expression) == TREE_LIST)
   28024     3420437 :           return any_value_dependent_elements_p (expression);
   28025             : 
   28026     1063723 :         if (TREE_CODE (type) == REFERENCE_TYPE
   28027     1063723 :             && has_value_dependent_address (expression))
   28028             :           return true;
   28029             : 
   28030     1063704 :         return value_dependent_expression_p (expression);
   28031             :       }
   28032             : 
   28033     6895650 :     case SIZEOF_EXPR:
   28034     6895650 :       if (SIZEOF_EXPR_TYPE_P (expression))
   28035     1274464 :         return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
   28036             :       /* FALLTHRU */
   28037     6103177 :     case ALIGNOF_EXPR:
   28038     6103177 :     case TYPEID_EXPR:
   28039             :       /* A `sizeof' expression is value-dependent if the operand is
   28040             :          type-dependent or is a pack expansion.  */
   28041     6103177 :       expression = TREE_OPERAND (expression, 0);
   28042     6103177 :       if (PACK_EXPANSION_P (expression))
   28043             :         return true;
   28044     3254175 :       else if (TYPE_P (expression))
   28045     3094851 :         return dependent_type_p (expression);
   28046      159324 :       return instantiation_dependent_uneval_expression_p (expression);
   28047             : 
   28048           0 :     case AT_ENCODE_EXPR:
   28049             :       /* An 'encode' expression is value-dependent if the operand is
   28050             :          type-dependent.  */
   28051           0 :       expression = TREE_OPERAND (expression, 0);
   28052           0 :       return dependent_type_p (expression);
   28053             : 
   28054      579668 :     case NOEXCEPT_EXPR:
   28055      579668 :       expression = TREE_OPERAND (expression, 0);
   28056      579668 :       return instantiation_dependent_uneval_expression_p (expression);
   28057             : 
   28058     4763920 :     case SCOPE_REF:
   28059             :       /* All instantiation-dependent expressions should also be considered
   28060             :          value-dependent.  */
   28061     4763920 :       return instantiation_dependent_scope_ref_p (expression);
   28062             : 
   28063      202656 :     case COMPONENT_REF:
   28064      202656 :       return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
   28065      202656 :               || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
   28066             : 
   28067           0 :     case NONTYPE_ARGUMENT_PACK:
   28068             :       /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
   28069             :          is value-dependent.  */
   28070           0 :       for (tree arg : tree_vec_range (ARGUMENT_PACK_ARGS (expression)))
   28071           0 :         if (value_dependent_expression_p (arg))
   28072           0 :           return true;
   28073           0 :       return false;
   28074             : 
   28075    13854949 :     case TRAIT_EXPR:
   28076    13854949 :       {
   28077    13854949 :         if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
   28078             :           return true;
   28079             : 
   28080       16438 :         tree type2 = TRAIT_EXPR_TYPE2 (expression);
   28081       16438 :         if (!type2)
   28082             :           return false;
   28083             : 
   28084       16389 :         if (TREE_CODE (type2) != TREE_VEC)
   28085       16316 :           return dependent_type_p (type2);
   28086             : 
   28087          85 :         for (tree arg : tree_vec_range (type2))
   28088          70 :           if (dependent_type_p (arg))
   28089          58 :             return true;
   28090             : 
   28091          15 :         return false;
   28092             :       }
   28093             : 
   28094           0 :     case MODOP_EXPR:
   28095           0 :       return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
   28096           0 :               || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
   28097             : 
   28098      265800 :     case ARRAY_REF:
   28099      265800 :       return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
   28100      265800 :               || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
   28101             : 
   28102      183654 :     case ADDR_EXPR:
   28103      183654 :       {
   28104      183654 :         tree op = TREE_OPERAND (expression, 0);
   28105      183654 :         return (value_dependent_expression_p (op)
   28106      183654 :                 || has_value_dependent_address (op));
   28107             :       }
   28108             : 
   28109             :     case REQUIRES_EXPR:
   28110             :       /* Treat all requires-expressions as value-dependent so
   28111             :          we don't try to fold them.  */
   28112             :       return true;
   28113             : 
   28114           0 :     case TYPE_REQ:
   28115           0 :       return dependent_type_p (TREE_OPERAND (expression, 0));
   28116             : 
   28117     8700188 :     case CALL_EXPR:
   28118     8700188 :       {
   28119     8700188 :         if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
   28120             :           return true;
   28121     8472625 :         tree fn = get_callee_fndecl (expression);
   28122     8472625 :         int i, nargs;
   28123     8472625 :         nargs = call_expr_nargs (expression);
   28124    10802320 :         for (i = 0; i < nargs; ++i)
   28125             :           {
   28126     2580858 :             tree op = CALL_EXPR_ARG (expression, i);
   28127             :             /* In a call to a constexpr member function, look through the
   28128             :                implicit ADDR_EXPR on the object argument so that it doesn't
   28129             :                cause the call to be considered value-dependent.  We also
   28130             :                look through it in potential_constant_expression.  */
   28131         125 :             if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
   28132         123 :                 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
   28133     2580860 :                 && TREE_CODE (op) == ADDR_EXPR)
   28134           2 :               op = TREE_OPERAND (op, 0);
   28135     2580858 :             if (value_dependent_expression_p (op))
   28136             :               return true;
   28137             :           }
   28138             :         return false;
   28139             :       }
   28140             : 
   28141      511176 :     case TEMPLATE_ID_EXPR:
   28142      511176 :       return concept_definition_p (TREE_OPERAND (expression, 0))
   28143      511176 :         && any_dependent_template_arguments_p (TREE_OPERAND (expression, 1));
   28144             : 
   28145     1605440 :     case CONSTRUCTOR:
   28146     1605440 :       {
   28147     1605440 :         unsigned ix;
   28148     1605440 :         tree val;
   28149     1605440 :         if (dependent_type_p (TREE_TYPE (expression)))
   28150             :           return true;
   28151     1637880 :         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
   28152       32674 :           if (value_dependent_expression_p (val))
   28153             :             return true;
   28154             :         return false;
   28155             :       }
   28156             : 
   28157             :     case STMT_EXPR:
   28158             :       /* Treat a GNU statement expression as dependent to avoid crashing
   28159             :          under instantiate_non_dependent_expr; it can't be constant.  */
   28160             :       return true;
   28161             : 
   28162          27 :     case NEW_EXPR:
   28163          27 :     case VEC_NEW_EXPR:
   28164             :       /* The second operand is a type, which type_dependent_expression_p
   28165             :          (and therefore value_dependent_expression_p) doesn't want to see.  */
   28166          27 :       return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
   28167          26 :               || value_dependent_expression_p (TREE_OPERAND (expression, 2))
   28168          52 :               || value_dependent_expression_p (TREE_OPERAND (expression, 3)));
   28169             : 
   28170   434912884 :     default:
   28171             :       /* A constant expression is value-dependent if any subexpression is
   28172             :          value-dependent.  */
   28173   434912884 :       switch (TREE_CODE_CLASS (TREE_CODE (expression)))
   28174             :         {
   28175    90341960 :         case tcc_reference:
   28176    90341960 :         case tcc_unary:
   28177    90341960 :         case tcc_comparison:
   28178    90341960 :         case tcc_binary:
   28179    90341960 :         case tcc_expression:
   28180    90341960 :         case tcc_vl_exp:
   28181    90341960 :           {
   28182    90341960 :             int i, len = cp_tree_operand_length (expression);
   28183             : 
   28184   166643205 :             for (i = 0; i < len; i++)
   28185             :               {
   28186    97437645 :                 tree t = TREE_OPERAND (expression, i);
   28187             : 
   28188             :                 /* In some cases, some of the operands may be missing.
   28189             :                    (For example, in the case of PREDECREMENT_EXPR, the
   28190             :                    amount to increment by may be missing.)  That doesn't
   28191             :                    make the expression dependent.  */
   28192    97437645 :                 if (t && value_dependent_expression_p (t))
   28193             :                   return true;
   28194             :               }
   28195             :           }
   28196             :           break;
   28197             :         default:
   28198             :           break;
   28199             :         }
   28200             :       break;
   28201             :     }
   28202             : 
   28203             :   /* The expression is not value-dependent.  */
   28204             :   return false;
   28205             : }
   28206             : 
   28207             : /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
   28208             :    [temp.dep.expr].  Note that an expression with no type is
   28209             :    considered dependent.  Other parts of the compiler arrange for an
   28210             :    expression with type-dependent subexpressions to have no type, so
   28211             :    this function doesn't have to be fully recursive.  */
   28212             : 
   28213             : bool
   28214  2621878287 : type_dependent_expression_p (tree expression)
   28215             : {
   28216  2621878465 :   if (!processing_template_decl)
   28217             :     return false;
   28218             : 
   28219  2334771467 :   if (expression == NULL_TREE || expression == error_mark_node)
   28220             :     return false;
   28221             : 
   28222  2334128276 :   gcc_checking_assert (!TYPE_P (expression));
   28223             : 
   28224  2334128276 :   STRIP_ANY_LOCATION_WRAPPER (expression);
   28225             : 
   28226             :   /* An unresolved name is always dependent.  */
   28227  2334128276 :   if (identifier_p (expression)
   28228             :       || TREE_CODE (expression) == USING_DECL
   28229             :       || TREE_CODE (expression) == WILDCARD_DECL)
   28230             :     return true;
   28231             : 
   28232             :   /* A lambda-expression in template context is dependent.  dependent_type_p is
   28233             :      true for a lambda in the scope of a class or function template, but that
   28234             :      doesn't cover all template contexts, like a default template argument.  */
   28235             :   if (TREE_CODE (expression) == LAMBDA_EXPR)
   28236             :     return true;
   28237             : 
   28238             :   /* A fold expression is type-dependent. */
   28239             :   if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
   28240             :       || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
   28241             :       || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
   28242             :       || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
   28243             :     return true;
   28244             : 
   28245             :   /* Some expression forms are never type-dependent.  */
   28246             :   if (TREE_CODE (expression) == SIZEOF_EXPR
   28247             :       || TREE_CODE (expression) == ALIGNOF_EXPR
   28248             :       || TREE_CODE (expression) == AT_ENCODE_EXPR
   28249             :       || TREE_CODE (expression) == NOEXCEPT_EXPR
   28250             :       || TREE_CODE (expression) == TRAIT_EXPR
   28251             :       || TREE_CODE (expression) == TYPEID_EXPR
   28252             :       || TREE_CODE (expression) == DELETE_EXPR
   28253             :       || TREE_CODE (expression) == VEC_DELETE_EXPR
   28254             :       || TREE_CODE (expression) == THROW_EXPR
   28255             :       || TREE_CODE (expression) == REQUIRES_EXPR)
   28256             :     return false;
   28257             : 
   28258             :   /* The types of these expressions depends only on the type to which
   28259             :      the cast occurs.  */
   28260             :   if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
   28261             :       || TREE_CODE (expression) == STATIC_CAST_EXPR
   28262             :       || TREE_CODE (expression) == CONST_CAST_EXPR
   28263             :       || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
   28264             :       || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
   28265             :       || TREE_CODE (expression) == CAST_EXPR)
   28266    25035840 :     return dependent_type_p (TREE_TYPE (expression));
   28267             : 
   28268             :   /* The types of these expressions depends only on the type created
   28269             :      by the expression.  */
   28270             :   if (TREE_CODE (expression) == NEW_EXPR
   28271             :       || TREE_CODE (expression) == VEC_NEW_EXPR)
   28272             :     {
   28273             :       /* For NEW_EXPR tree nodes created inside a template, either
   28274             :          the object type itself or a TREE_LIST may appear as the
   28275             :          operand 1.  */
   28276      301841 :       tree type = TREE_OPERAND (expression, 1);
   28277      301841 :       if (TREE_CODE (type) == TREE_LIST)
   28278             :         /* This is an array type.  We need to check array dimensions
   28279             :            as well.  */
   28280           0 :         return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
   28281           0 :                || value_dependent_expression_p
   28282           0 :                     (TREE_OPERAND (TREE_VALUE (type), 1));
   28283             :       /* Array type whose dimension has to be deduced.  */
   28284      301841 :       else if (TREE_CODE (type) == ARRAY_TYPE
   28285      301841 :                && TREE_OPERAND (expression, 2) == NULL_TREE)
   28286             :         return true;
   28287             :       else
   28288      301828 :         return dependent_type_p (type);
   28289             :     }
   28290             : 
   28291             :   if (TREE_CODE (expression) == SCOPE_REF)
   28292             :     {
   28293    86514438 :       tree scope = TREE_OPERAND (expression, 0);
   28294    86514438 :       tree name = TREE_OPERAND (expression, 1);
   28295             : 
   28296             :       /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
   28297             :          contains an identifier associated by name lookup with one or more
   28298             :          declarations declared with a dependent type, or...a
   28299             :          nested-name-specifier or qualified-id that names a member of an
   28300             :          unknown specialization.  */
   28301    86514438 :       return (type_dependent_expression_p (name)
   28302    86514438 :               || dependent_scope_p (scope));
   28303             :     }
   28304             : 
   28305             :   if (TREE_CODE (expression) == TEMPLATE_DECL
   28306   175898463 :       && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
   28307   175898463 :     return uses_outer_template_parms (expression);
   28308             : 
   28309  1941460762 :   if (TREE_CODE (expression) == STMT_EXPR)
   28310         265 :     expression = stmt_expr_value_expr (expression);
   28311             : 
   28312  1941460762 :   if (BRACE_ENCLOSED_INITIALIZER_P (expression))
   28313             :     {
   28314     4388830 :       for (auto &elt : CONSTRUCTOR_ELTS (expression))
   28315      440702 :         if (type_dependent_expression_p (elt.value))
   28316             :           return true;
   28317             :       return false;
   28318             :     }
   28319             : 
   28320             :   /* A static data member of the current instantiation with incomplete
   28321             :      array type is type-dependent, as the definition and specializations
   28322             :      can have different bounds.  */
   28323  1937976571 :   if (VAR_P (expression)
   28324   348884042 :       && DECL_CLASS_SCOPE_P (expression)
   28325    50387927 :       && dependent_type_p (DECL_CONTEXT (expression))
   28326  1961073758 :       && VAR_HAD_UNKNOWN_BOUND (expression))
   28327             :     return true;
   28328             : 
   28329             :   /* An array of unknown bound depending on a variadic parameter, eg:
   28330             : 
   28331             :      template<typename... Args>
   28332             :        void foo (Args... args)
   28333             :        {
   28334             :          int arr[] = { args... };
   28335             :        }
   28336             : 
   28337             :      template<int... vals>
   28338             :        void bar ()
   28339             :        {
   28340             :          int arr[] = { vals... };
   28341             :        }
   28342             : 
   28343             :      If the array has no length and has an initializer, it must be that
   28344             :      we couldn't determine its length in cp_complete_array_type because
   28345             :      it is dependent.  */
   28346   348883825 :   if (((VAR_P (expression) && DECL_INITIAL (expression))
   28347  1624980375 :        || COMPOUND_LITERAL_P (expression))
   28348   314234779 :       && TREE_TYPE (expression) != NULL_TREE
   28349   314234779 :       && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
   28350  1938699121 :       && !TYPE_DOMAIN (TREE_TYPE (expression)))
   28351             :    return true;
   28352             : 
   28353             :   /* Pull a FUNCTION_DECL out of a BASELINK if we can.  */
   28354  1937837067 :   if (BASELINK_P (expression))
   28355             :     {
   28356    45125490 :       if (BASELINK_OPTYPE (expression)
   28357    45125490 :           && dependent_type_p (BASELINK_OPTYPE (expression)))
   28358             :         return true;
   28359    45125436 :       expression = BASELINK_FUNCTIONS (expression);
   28360             :     }
   28361             : 
   28362             :   /* A function or variable template-id is type-dependent if it has any
   28363             :      dependent template arguments.  */
   28364  1937837013 :   if (VAR_OR_FUNCTION_DECL_P (expression)
   28365   511648945 :       && DECL_LANG_SPECIFIC (expression)
   28366  2186573149 :       && DECL_TEMPLATE_INFO (expression))
   28367             :     {
   28368             :       /* Consider the innermost template arguments, since those are the ones
   28369             :          that come from the template-id; the template arguments for the
   28370             :          enclosing class do not make it type-dependent unless they are used in
   28371             :          the type of the decl.  */
   28372   120607724 :       if (instantiates_primary_template_p (expression)
   28373   161250031 :           && (any_dependent_template_arguments_p
   28374    40642307 :               (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
   28375             :         return true;
   28376             :     }
   28377             : 
   28378             :   /* Otherwise, if the function decl isn't from a dependent scope, it can't be
   28379             :      type-dependent.  Checking this is important for functions with auto return
   28380             :      type, which looks like a dependent type.  */
   28381  1901597279 :   if (TREE_CODE (expression) == FUNCTION_DECL
   28382   179800979 :       && !(DECL_CLASS_SCOPE_P (expression)
   28383    53136309 :            && dependent_type_p (DECL_CONTEXT (expression)))
   28384    84421617 :       && !(DECL_LANG_SPECIFIC (expression)
   28385    84421617 :            && DECL_UNIQUE_FRIEND_P (expression)
   28386      377290 :            && (!DECL_FRIEND_CONTEXT (expression)
   28387      158452 :                || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
   28388  1985988342 :       && !DECL_LOCAL_DECL_P (expression))
   28389             :     {
   28390    84347839 :       gcc_assert (!dependent_type_p (TREE_TYPE (expression))
   28391             :                   || undeduced_auto_decl (expression));
   28392    84347839 :       return false;
   28393             :     }
   28394             : 
   28395             :   /* Otherwise, its constraints could still depend on outer template parameters
   28396             :      from its (dependent) scope.  */
   28397  1817249440 :   if (TREE_CODE (expression) == FUNCTION_DECL
   28398             :       /* As an optimization, check this cheaper sufficient condition first.
   28399             :          (At this point we've established that we're looking at a member of
   28400             :          a dependent class, so it makes sense to start treating say undeduced
   28401             :          auto as dependent.)  */
   28402    42316831 :       && !dependent_type_p (TREE_TYPE (expression))
   28403  1819202200 :       && uses_outer_template_parms_in_constraints (expression))
   28404             :     return true;
   28405             : 
   28406             :   /* Always dependent, on the number of arguments if nothing else.  */
   28407  1817249438 :   if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
   28408             :     return true;
   28409             : 
   28410  1813733597 :   if (TREE_TYPE (expression) == unknown_type_node)
   28411             :     {
   28412   111631472 :       if (TREE_CODE (expression) == ADDR_EXPR)
   28413         178 :         return type_dependent_expression_p (TREE_OPERAND (expression, 0));
   28414   111631294 :       if (TREE_CODE (expression) == COMPONENT_REF
   28415   111576502 :           || TREE_CODE (expression) == OFFSET_REF)
   28416             :         {
   28417       54952 :           if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
   28418             :             return true;
   28419       54802 :           expression = TREE_OPERAND (expression, 1);
   28420   493274385 :           if (identifier_p (expression))
   28421             :             return false;
   28422             :         }
   28423             :       /* SCOPE_REF with non-null TREE_TYPE is always non-dependent.  */
   28424   111631144 :       if (TREE_CODE (expression) == SCOPE_REF)
   28425             :         return false;
   28426             : 
   28427             :       /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent.  */
   28428   111631144 :       if (TREE_CODE (expression) == CO_AWAIT_EXPR
   28429   111631144 :           || TREE_CODE (expression) == CO_YIELD_EXPR)
   28430             :         return true;
   28431             : 
   28432   111630969 :       if (BASELINK_P (expression))
   28433             :         {
   28434       54802 :           if (BASELINK_OPTYPE (expression)
   28435       54802 :               && dependent_type_p (BASELINK_OPTYPE (expression)))
   28436             :             return true;
   28437       54802 :           expression = BASELINK_FUNCTIONS (expression);
   28438             :         }
   28439             : 
   28440   111630969 :       if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
   28441             :         {
   28442    39408518 :           if (any_dependent_template_arguments_p
   28443    39408518 :               (TREE_OPERAND (expression, 1)))
   28444             :             return true;
   28445    12919019 :           expression = TREE_OPERAND (expression, 0);
   28446   238240811 :           if (identifier_p (expression))
   28447             :             return true;
   28448             :         }
   28449             : 
   28450    85141470 :       gcc_assert (OVL_P (expression));
   28451             : 
   28452   367193841 :       for (lkp_iterator iter (expression); iter; ++iter)
   28453   218364492 :         if (type_dependent_expression_p (*iter))
   28454    21453591 :           return true;
   28455             : 
   28456    63687879 :       return false;
   28457             :     }
   28458             : 
   28459             :   /* The type of a non-type template parm declared with a placeholder type
   28460             :      depends on the corresponding template argument, even though
   28461             :      placeholders are not normally considered dependent.  */
   28462  1702102125 :   if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
   28463  1702102125 :       && is_auto (TREE_TYPE (expression)))
   28464             :     return true;
   28465             : 
   28466  1702100853 :   gcc_assert (TREE_CODE (expression) != TYPE_DECL);
   28467             : 
   28468             :   /* Dependent type attributes might not have made it from the decl to
   28469             :      the type yet.  */
   28470  1702100853 :   if (DECL_P (expression)
   28471  1702100853 :       && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
   28472             :     return true;
   28473             : 
   28474  1702100781 :   return (dependent_type_p (TREE_TYPE (expression)));
   28475             : }
   28476             : 
   28477             : /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
   28478             :    type-dependent if the expression refers to a member of the current
   28479             :    instantiation and the type of the referenced member is dependent, or the
   28480             :    class member access expression refers to a member of an unknown
   28481             :    specialization.
   28482             : 
   28483             :    This function returns true if the OBJECT in such a class member access
   28484             :    expression is of an unknown specialization.  */
   28485             : 
   28486             : bool
   28487   226849048 : type_dependent_object_expression_p (tree object)
   28488             : {
   28489             :   /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
   28490             :      dependent.  */
   28491   226849048 :   if (TREE_CODE (object) == IDENTIFIER_NODE)
   28492             :     return true;
   28493   226849048 :   tree scope = TREE_TYPE (object);
   28494   226849048 :   return (!scope || dependent_scope_p (scope));
   28495             : }
   28496             : 
   28497             : /* walk_tree callback function for instantiation_dependent_expression_p,
   28498             :    below.  Returns non-zero if a dependent subexpression is found.  */
   28499             : 
   28500             : static tree
   28501   480611524 : instantiation_dependent_r (tree *tp, int *walk_subtrees,
   28502             :                            void * /*data*/)
   28503             : {
   28504   480611524 :   if (TYPE_P (*tp))
   28505             :     {
   28506             :       /* We don't have to worry about decltype currently because decltype
   28507             :          of an instantiation-dependent expr is a dependent type.  This
   28508             :          might change depending on the resolution of DR 1172.  */
   28509    22241641 :       *walk_subtrees = false;
   28510    22241641 :       return NULL_TREE;
   28511             :     }
   28512   458369883 :   enum tree_code code = TREE_CODE (*tp);
   28513   458369883 :   switch (code)
   28514             :     {
   28515             :       /* Don't treat an argument list as dependent just because it has no
   28516             :          TREE_TYPE.  */
   28517             :     case TREE_LIST:
   28518             :     case TREE_VEC:
   28519             :     case NONTYPE_ARGUMENT_PACK:
   28520             :       return NULL_TREE;
   28521             : 
   28522    26662376 :     case TEMPLATE_PARM_INDEX:
   28523    26662376 :       if (dependent_type_p (TREE_TYPE (*tp)))
   28524         237 :         return *tp;
   28525    26662139 :       if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
   28526             :         return *tp;
   28527             :       /* We'll check value-dependence separately.  */
   28528             :       return NULL_TREE;
   28529             : 
   28530             :       /* Handle expressions with type operands.  */
   28531     6686715 :     case SIZEOF_EXPR:
   28532     6686715 :     case ALIGNOF_EXPR:
   28533     6686715 :     case TYPEID_EXPR:
   28534     6686715 :     case AT_ENCODE_EXPR:
   28535     6686715 :       {
   28536     6686715 :         tree op = TREE_OPERAND (*tp, 0);
   28537     6686715 :         if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
   28538     1274464 :           op = TREE_TYPE (op);
   28539     6686715 :         if (TYPE_P (op))
   28540             :           {
   28541     6424968 :             if (dependent_type_p (op))
   28542     4532758 :               return *tp;
   28543             :             else
   28544             :               {
   28545     1892210 :                 *walk_subtrees = false;
   28546     1892210 :                 return NULL_TREE;
   28547             :               }
   28548             :           }
   28549             :         break;
   28550             :       }
   28551             : 
   28552     2995060 :     case COMPONENT_REF:
   28553     2995060 :       if (identifier_p (TREE_OPERAND (*tp, 1)))
   28554             :         /* In a template, finish_class_member_access_expr creates a
   28555             :            COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
   28556             :            type-dependent, so that we can check access control at
   28557             :            instantiation time (PR 42277).  See also Core issue 1273.  */
   28558             :         return *tp;
   28559             :       break;
   28560             : 
   28561     7050961 :     case SCOPE_REF:
   28562     7050961 :       if (instantiation_dependent_scope_ref_p (*tp))
   28563     2721520 :         return *tp;
   28564             :       else
   28565             :         break;
   28566             : 
   28567             :       /* Treat statement-expressions as dependent.  */
   28568             :     case BIND_EXPR:
   28569             :       return *tp;
   28570             : 
   28571             :       /* Treat requires-expressions as dependent. */
   28572             :     case REQUIRES_EXPR:
   28573             :       return *tp;
   28574             : 
   28575     2097101 :     case CONSTRUCTOR:
   28576     2097101 :       if (CONSTRUCTOR_IS_DEPENDENT (*tp))
   28577             :         return *tp;
   28578             :       break;
   28579             : 
   28580    47474672 :     case TEMPLATE_DECL:
   28581    47474672 :     case FUNCTION_DECL:
   28582             :       /* Before C++17, a noexcept-specifier isn't part of the function type
   28583             :          so it doesn't affect type dependence, but we still want to consider it
   28584             :          for instantiation dependence.  */
   28585    47474672 :       if (cxx_dialect < cxx17
   28586      406994 :           && DECL_DECLARES_FUNCTION_P (*tp)
   28587    47881642 :           && value_dependent_noexcept_spec_p (TREE_TYPE (*tp)))
   28588         585 :         return *tp;
   28589             :       break;
   28590             : 
   28591             :     default:
   28592             :       break;
   28593             :     }
   28594             : 
   28595   411333845 :   if (type_dependent_expression_p (*tp))
   28596    89108683 :     return *tp;
   28597             :   else
   28598             :     return NULL_TREE;
   28599             : }
   28600             : 
   28601             : /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
   28602             :    sense defined by the ABI:
   28603             : 
   28604             :    "An expression is instantiation-dependent if it is type-dependent
   28605             :    or value-dependent, or it has a subexpression that is type-dependent
   28606             :    or value-dependent."
   28607             : 
   28608             :    Except don't actually check value-dependence for unevaluated expressions,
   28609             :    because in sizeof(i) we don't care about the value of i.  Checking
   28610             :    type-dependence will in turn check value-dependence of array bounds/template
   28611             :    arguments as needed.  */
   28612             : 
   28613             : bool
   28614   628028096 : instantiation_dependent_uneval_expression_p (tree expression)
   28615             : {
   28616   628028096 :   tree result;
   28617             : 
   28618   628028096 :   if (!processing_template_decl)
   28619             :     return false;
   28620             : 
   28621   339702428 :   if (expression == error_mark_node)
   28622             :     return false;
   28623             : 
   28624   339702414 :   result = cp_walk_tree_without_duplicates (&expression,
   28625             :                                             instantiation_dependent_r, NULL);
   28626   339702414 :   return result != NULL_TREE;
   28627             : }
   28628             : 
   28629             : /* As above, but also check value-dependence of the expression as a whole.  */
   28630             : 
   28631             : bool
   28632   599503430 : instantiation_dependent_expression_p (tree expression)
   28633             : {
   28634   599503430 :   return (instantiation_dependent_uneval_expression_p (expression)
   28635   599503430 :           || (processing_template_decl
   28636   220366178 :               && potential_constant_expression (expression)
   28637   220366102 :               && value_dependent_expression_p (expression)));
   28638             : }
   28639             : 
   28640             : /* Like type_dependent_expression_p, but it also works while not processing
   28641             :    a template definition, i.e. during substitution or mangling.  */
   28642             : 
   28643             : bool
   28644     3829842 : type_dependent_expression_p_push (tree expr)
   28645             : {
   28646     3829842 :   bool b;
   28647     3829842 :   ++processing_template_decl;
   28648     3829842 :   b = type_dependent_expression_p (expr);
   28649     3829842 :   --processing_template_decl;
   28650     3829842 :   return b;
   28651             : }
   28652             : 
   28653             : /* Returns TRUE if ARGS contains a type-dependent expression.  */
   28654             : 
   28655             : bool
   28656    65990023 : any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
   28657             : {
   28658    65990023 :   if (!processing_template_decl || !args)
   28659             :     return false;
   28660             : 
   28661    63744090 :   for (tree arg : *args)
   28662    46562313 :     if (type_dependent_expression_p (arg))
   28663             :       return true;
   28664             : 
   28665             :   return false;
   28666             : }
   28667             : 
   28668             : /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
   28669             :    expressions) contains any type-dependent expressions.  */
   28670             : 
   28671             : bool
   28672           0 : any_type_dependent_elements_p (const_tree list)
   28673             : {
   28674           0 :   for (; list; list = TREE_CHAIN (list))
   28675           0 :     if (type_dependent_expression_p (TREE_VALUE (list)))
   28676             :       return true;
   28677             : 
   28678             :   return false;
   28679             : }
   28680             : 
   28681             : /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
   28682             :    expressions) contains any value-dependent expressions.  */
   28683             : 
   28684             : bool
   28685     3420484 : any_value_dependent_elements_p (const_tree list)
   28686             : {
   28687     5487390 :   for (; list; list = TREE_CHAIN (list))
   28688     3468962 :     if (value_dependent_expression_p (TREE_VALUE (list)))
   28689             :       return true;
   28690             : 
   28691             :   return false;
   28692             : }
   28693             : 
   28694             : /* Returns TRUE if the ARG (a template argument) is dependent.  */
   28695             : 
   28696             : bool
   28697  2743751537 : dependent_template_arg_p (tree arg)
   28698             : {
   28699  2743751537 :   if (!processing_template_decl)
   28700             :     return false;
   28701             : 
   28702             :   /* Assume a template argument that was wrongly written by the user
   28703             :      is dependent. This is consistent with what
   28704             :      any_dependent_template_arguments_p [that calls this function]
   28705             :      does.  */
   28706  2557304869 :   if (!arg || arg == error_mark_node)
   28707             :     return true;
   28708             : 
   28709  2557304452 :   if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
   28710           0 :     arg = argument_pack_select_arg (arg);
   28711             : 
   28712  2557304452 :   if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
   28713             :     return true;
   28714  2557122275 :   if (TREE_CODE (arg) == TEMPLATE_DECL)
   28715             :     {
   28716     6966384 :       if (DECL_TEMPLATE_PARM_P (arg))
   28717             :         return true;
   28718             :       /* A member template of a dependent class is not necessarily
   28719             :          type-dependent, but it is a dependent template argument because it
   28720             :          will be a member of an unknown specialization to that template.  */
   28721     6966264 :       tree scope = CP_DECL_CONTEXT (arg);
   28722     6966264 :       return TYPE_P (scope) && dependent_type_p (scope);
   28723             :     }
   28724  2550155891 :   else if (ARGUMENT_PACK_P (arg))
   28725             :     {
   28726   102661850 :       tree args = ARGUMENT_PACK_ARGS (arg);
   28727   245148397 :       for (tree arg : tree_vec_range (args))
   28728   177299292 :         if (dependent_template_arg_p (arg))
   28729    34812745 :           return true;
   28730    67849105 :       return false;
   28731             :     }
   28732  2447494041 :   else if (TYPE_P (arg))
   28733  2189113381 :     return dependent_type_p (arg);
   28734             :   else
   28735   258380660 :     return value_dependent_expression_p (arg);
   28736             : }
   28737             : 
   28738             : /* Identify any expressions that use function parms.  */
   28739             : 
   28740             : static tree
   28741   322340575 : find_parm_usage_r (tree *tp, int *walk_subtrees, void*)
   28742             : {
   28743   322340575 :   tree t = *tp;
   28744   322340575 :   if (TREE_CODE (t) == PARM_DECL)
   28745             :     {
   28746       27162 :       *walk_subtrees = 0;
   28747       27162 :       return t;
   28748             :     }
   28749             :   return NULL_TREE;
   28750             : }
   28751             : 
   28752             : /* Returns true if a type specialization formed using the template
   28753             :    arguments ARGS needs to use structural equality.  */
   28754             : 
   28755             : bool
   28756    60930570 : any_template_arguments_need_structural_equality_p (tree args)
   28757             : {
   28758    60930570 :   int i;
   28759    60930570 :   int j;
   28760             : 
   28761    60930570 :   if (!args)
   28762             :     return false;
   28763    60930570 :   if (args == error_mark_node)
   28764             :     return true;
   28765             : 
   28766   242331318 :   for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
   28767             :     {
   28768    62267424 :       tree level = TMPL_ARGS_LEVEL (args, i + 1);
   28769   175609909 :       for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
   28770             :         {
   28771   113369651 :           tree arg = TREE_VEC_ELT (level, j);
   28772   113369651 :           tree packed_args = NULL_TREE;
   28773   113369651 :           int k, len = 1;
   28774             : 
   28775   113369651 :           if (ARGUMENT_PACK_P (arg))
   28776             :             {
   28777             :               /* Look inside the argument pack.  */
   28778     9517143 :               packed_args = ARGUMENT_PACK_ARGS (arg);
   28779     9517143 :               len = TREE_VEC_LENGTH (packed_args);
   28780             :             }
   28781             : 
   28782   236227197 :           for (k = 0; k < len; ++k)
   28783             :             {
   28784   122884712 :               if (packed_args)
   28785    19032204 :                 arg = TREE_VEC_ELT (packed_args, k);
   28786             : 
   28787   122884712 :               if (error_operand_p (arg))
   28788       27166 :                 return true;
   28789   122884712 :               else if (TREE_CODE (arg) == TEMPLATE_DECL)
   28790     2895153 :                 continue;
   28791   119989559 :               else if (arg == any_targ_node)
   28792             :                 /* An any_targ_node argument (added by add_defaults_to_ttp)
   28793             :                    makes the corresponding specialization not canonicalizable,
   28794             :                    since template_args_equal always return true for it.  We
   28795             :                    may see this when called from bind_template_template_parm.  */
   28796             :                 return true;
   28797             :               /* Checking current_function_decl because this structural
   28798             :                  comparison is only necessary for redeclaration.  */
   28799   119989555 :               else if (!current_function_decl
   28800   109077697 :                        && dependent_template_arg_p (arg)
   28801   173144134 :                        && (cp_walk_tree_without_duplicates
   28802             :                            (&arg, find_parm_usage_r, NULL)))
   28803             :                 /* The identity of a class template specialization that uses
   28804             :                    a function parameter depends on the identity of the function.
   28805             :                    And if this specialization appeared in the trailing return
   28806             :                    type thereof, we don't know the identity of the function
   28807             :                    (e.g. if it's a redeclaration or a new function) until we
   28808             :                    form its signature and go through duplicate_decls.  Thus
   28809             :                    it's unsafe to decide on a canonical type now (which depends
   28810             :                    on the DECL_CONTEXT of the function parameter, which can get
   28811             :                    mutated after the fact by duplicate_decls), so just require
   28812             :                    structural equality in this case (PR52830).  */
   28813             :                 return true;
   28814             :             }
   28815             :         }
   28816             :     }
   28817             : 
   28818             :   return false;
   28819             : }
   28820             : 
   28821             : /* Returns true if ARGS (a collection of template arguments) contains
   28822             :    any dependent arguments.  */
   28823             : 
   28824             : bool
   28825  1450770667 : any_dependent_template_arguments_p (const_tree args)
   28826             : {
   28827  1450770667 :   if (args == error_mark_node)
   28828             :     return true;
   28829  1450770667 :   if (!processing_template_decl || !args)
   28830             :     return false;
   28831             : 
   28832  3282454566 :   for (int i = 0, depth = TMPL_ARGS_DEPTH (args); i < depth; ++i)
   28833             :     {
   28834  1405078529 :       const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
   28835  3036176236 :       for (tree arg : tree_vec_range (CONST_CAST_TREE (level)))
   28836  2283792413 :         if (dependent_template_arg_p (arg))
   28837   652694706 :           return true;
   28838             :     }
   28839             : 
   28840             :   return false;
   28841             : }
   28842             : 
   28843             : /* Returns true if ARGS contains any errors.  */
   28844             : 
   28845             : bool
   28846    16176895 : any_erroneous_template_args_p (const_tree args)
   28847             : {
   28848    16176895 :   int i;
   28849    16176895 :   int j;
   28850             : 
   28851    16176895 :   if (args == error_mark_node)
   28852             :     return true;
   28853             : 
   28854    16176853 :   if (args && TREE_CODE (args) != TREE_VEC)
   28855             :     {
   28856    16176853 :       if (tree ti = get_template_info (args))
   28857    13374072 :         args = TI_ARGS (ti);
   28858             :       else
   28859             :         args = NULL_TREE;
   28860             :     }
   28861             : 
   28862    13374072 :   if (!args)
   28863             :     return false;
   28864             : 
   28865    53462770 :   for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
   28866             :     {
   28867    13407350 :       const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
   28868    36778467 :       for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
   28869    23371245 :         if (error_operand_p (TREE_VEC_ELT (level, j)))
   28870             :           return true;
   28871             :     }
   28872             : 
   28873             :   return false;
   28874             : }
   28875             : 
   28876             : /* Returns TRUE if the template TMPL is type-dependent.  */
   28877             : 
   28878             : bool
   28879     1055882 : dependent_template_p (tree tmpl)
   28880             : {
   28881     1055882 :   if (TREE_CODE (tmpl) == OVERLOAD)
   28882             :     {
   28883     1374681 :       for (lkp_iterator iter (tmpl); iter; ++iter)
   28884      723087 :         if (dependent_template_p (*iter))
   28885           0 :           return true;
   28886      325797 :       return false;
   28887             :     }
   28888             : 
   28889             :   /* Template template parameters are dependent.  */
   28890      592713 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
   28891     1322798 :       || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
   28892             :     return true;
   28893             :   /* So are names that have not been looked up.  */
   28894      737071 :   if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
   28895             :     return true;
   28896             :   return false;
   28897             : }
   28898             : 
   28899             : /* Returns TRUE if the specialization TMPL<ARGS> is dependent.  */
   28900             : 
   28901             : bool
   28902      332795 : dependent_template_id_p (tree tmpl, tree args)
   28903             : {
   28904      332795 :   return (dependent_template_p (tmpl)
   28905      332795 :           || any_dependent_template_arguments_p (args));
   28906             : }
   28907             : 
   28908             : /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
   28909             :    are dependent.  */
   28910             : 
   28911             : bool
   28912       20820 : dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
   28913             : {
   28914       20820 :   int i;
   28915             : 
   28916       20820 :   if (!processing_template_decl)
   28917             :     return false;
   28918             : 
   28919        2091 :   for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
   28920             :     {
   28921        1314 :       tree decl = TREE_VEC_ELT (declv, i);
   28922        1314 :       tree init = TREE_VEC_ELT (initv, i);
   28923        1314 :       tree cond = TREE_VEC_ELT (condv, i);
   28924        1314 :       tree incr = TREE_VEC_ELT (incrv, i);
   28925             : 
   28926        1314 :       if (type_dependent_expression_p (decl)
   28927        1314 :           || TREE_CODE (decl) == SCOPE_REF)
   28928             :         return true;
   28929             : 
   28930        1047 :       if (init && type_dependent_expression_p (init))
   28931             :         return true;
   28932             : 
   28933        1047 :       if (cond == global_namespace)
   28934             :         return true;
   28935             : 
   28936        1005 :       if (type_dependent_expression_p (cond))
   28937             :         return true;
   28938             : 
   28939         985 :       if (COMPARISON_CLASS_P (cond)
   28940         985 :           && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
   28941         981 :               || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
   28942          13 :         return true;
   28943             : 
   28944         972 :       if (TREE_CODE (incr) == MODOP_EXPR)
   28945             :         {
   28946           1 :           if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
   28947           1 :               || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
   28948           0 :             return true;
   28949             :         }
   28950         971 :       else if (type_dependent_expression_p (incr))
   28951             :         return true;
   28952         971 :       else if (TREE_CODE (incr) == MODIFY_EXPR)
   28953             :         {
   28954         284 :           if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
   28955             :             return true;
   28956         284 :           else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
   28957             :             {
   28958         284 :               tree t = TREE_OPERAND (incr, 1);
   28959         284 :               if (type_dependent_expression_p (TREE_OPERAND (t, 0))
   28960         284 :                   || type_dependent_expression_p (TREE_OPERAND (t, 1)))
   28961           5 :                 return true;
   28962             : 
   28963             :               /* If this loop has a class iterator with != comparison
   28964             :                  with increment other than i++/++i/i--/--i, make sure the
   28965             :                  increment is constant.  */
   28966         558 :               if (CLASS_TYPE_P (TREE_TYPE (decl))
   28967         340 :                   && TREE_CODE (cond) == NE_EXPR)
   28968             :                 {
   28969           8 :                   if (TREE_OPERAND (t, 0) == decl)
   28970           8 :                     t = TREE_OPERAND (t, 1);
   28971             :                   else
   28972           0 :                     t = TREE_OPERAND (t, 0);
   28973           8 :                   if (TREE_CODE (t) != INTEGER_CST)
   28974             :                     return true;
   28975             :                 }
   28976             :             }
   28977             :         }
   28978             :     }
   28979             : 
   28980             :   return false;
   28981             : }
   28982             : 
   28983             : /* TYPE is a TYPENAME_TYPE.  Returns the ordinary TYPE to which the
   28984             :    TYPENAME_TYPE corresponds.  Returns the original TYPENAME_TYPE if
   28985             :    no such TYPE can be found.  Note that this function peers inside
   28986             :    uninstantiated templates and therefore should be used only in
   28987             :    extremely limited situations.  ONLY_CURRENT_P restricts this
   28988             :    peering to the currently open classes hierarchy (which is required
   28989             :    when comparing types).  */
   28990             : 
   28991             : tree
   28992   129878767 : resolve_typename_type (tree type, bool only_current_p)
   28993             : {
   28994   129878767 :   tree scope;
   28995   129878767 :   tree name;
   28996   129878767 :   tree decl;
   28997   129878767 :   int quals;
   28998   129878767 :   tree pushed_scope;
   28999   129878767 :   tree result;
   29000             : 
   29001   129878767 :   gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
   29002             : 
   29003   129878767 :   scope = TYPE_CONTEXT (type);
   29004             :   /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope.  */
   29005   129878767 :   gcc_checking_assert (uses_template_parms (scope));
   29006             : 
   29007             :   /* Usually the non-qualified identifier of a TYPENAME_TYPE is
   29008             :      TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
   29009             :      TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
   29010             :      representing the typedef. In that case TYPE_IDENTIFIER (type) is
   29011             :      not the non-qualified identifier of the TYPENAME_TYPE anymore.
   29012             :      So by getting the TYPE_IDENTIFIER of the _main declaration_ of
   29013             :      the TYPENAME_TYPE instead, we avoid messing up with a possible
   29014             :      typedef variant case.  */
   29015   129878767 :   name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
   29016             : 
   29017             :   /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
   29018             :      it first before we can figure out what NAME refers to.  */
   29019   129878767 :   if (TREE_CODE (scope) == TYPENAME_TYPE)
   29020             :     {
   29021     1492219 :       if (TYPENAME_IS_RESOLVING_P (scope))
   29022             :         /* Given a class template A with a dependent base with nested type C,
   29023             :            typedef typename A::C::C C will land us here, as trying to resolve
   29024             :            the initial A::C leads to the local C typedef, which leads back to
   29025             :            A::C::C.  So we break the recursion now.  */
   29026             :         return type;
   29027             :       else
   29028     1492219 :         scope = resolve_typename_type (scope, only_current_p);
   29029             :     }
   29030             :   /* If we don't know what SCOPE refers to, then we cannot resolve the
   29031             :      TYPENAME_TYPE.  */
   29032   129878767 :   if (!CLASS_TYPE_P (scope))
   29033             :     return type;
   29034             :   /* If this is a typedef, we don't want to look inside (c++/11987).  */
   29035   119832534 :   if (typedef_variant_p (type))
   29036             :     return type;
   29037             :   /* If SCOPE isn't the template itself, it will not have a valid
   29038             :      TYPE_FIELDS list.  */
   29039    87055454 :   if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
   29040             :     /* scope is either the template itself or a compatible instantiation
   29041             :        like X<T>, so look up the name in the original template.  */
   29042    38422667 :     scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
   29043             :   /* If scope has no fields, it can't be a current instantiation.  Check this
   29044             :      before currently_open_class to avoid infinite recursion (71515).  */
   29045    87055454 :   if (!TYPE_FIELDS (scope))
   29046             :     return type;
   29047             :   /* If the SCOPE is not the current instantiation, there's no reason
   29048             :      to look inside it.  */
   29049    38087547 :   if (only_current_p && !currently_open_class (scope))
   29050             :     return type;
   29051             :   /* Enter the SCOPE so that name lookup will be resolved as if we
   29052             :      were in the class definition.  In particular, SCOPE will no
   29053             :      longer be considered a dependent type.  */
   29054       44512 :   pushed_scope = push_scope (scope);
   29055             :   /* Look up the declaration.  */
   29056       44512 :   decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
   29057             :                         tf_warning_or_error);
   29058             : 
   29059       44512 :   result = NULL_TREE;
   29060             : 
   29061             :   /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
   29062             :      find a TEMPLATE_DECL.  Otherwise, we want to find a TYPE_DECL.  */
   29063       44512 :   tree fullname = TYPENAME_TYPE_FULLNAME (type);
   29064       44512 :   if (!decl)
   29065             :     /*nop*/;
   29066       44482 :   else if (identifier_p (fullname)
   29067       44475 :            && TREE_CODE (decl) == TYPE_DECL)
   29068             :     {
   29069       44475 :       result = TREE_TYPE (decl);
   29070       44475 :       if (result == error_mark_node)
   29071          33 :         result = NULL_TREE;
   29072             :     }
   29073           7 :   else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
   29074           7 :            && DECL_CLASS_TEMPLATE_P (decl))
   29075             :     {
   29076             :       /* Obtain the template and the arguments.  */
   29077           4 :       tree tmpl = TREE_OPERAND (fullname, 0);
   29078           4 :       if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
   29079             :         {
   29080             :           /* We get here with a plain identifier because a previous tentative
   29081             :              parse of the nested-name-specifier as part of a ptr-operator saw
   29082             :              ::template X<A>.  The use of ::template is necessary in a
   29083             :              ptr-operator, but wrong in a declarator-id.
   29084             : 
   29085             :              [temp.names]: In a qualified-id of a declarator-id, the keyword
   29086             :              template shall not appear at the top level.  */
   29087           4 :           pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
   29088             :                    "keyword %<template%> not allowed in declarator-id");
   29089           4 :           tmpl = decl;
   29090             :         }
   29091           4 :       tree args = TREE_OPERAND (fullname, 1);
   29092             :       /* Instantiate the template.  */
   29093           4 :       result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
   29094             :                                       /*entering_scope=*/true,
   29095             :                                       tf_error | tf_user);
   29096           4 :       if (result == error_mark_node)
   29097          33 :         result = NULL_TREE;
   29098             :     }
   29099             : 
   29100             :   /* Leave the SCOPE.  */
   29101       44512 :   if (pushed_scope)
   29102       44496 :     pop_scope (pushed_scope);
   29103             : 
   29104             :   /* If we failed to resolve it, return the original typename.  */
   29105       44512 :   if (!result)
   29106             :     return type;
   29107             : 
   29108             :   /* If lookup found a typename type, resolve that too.  */
   29109       44479 :   if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
   29110             :     {
   29111             :       /* Ill-formed programs can cause infinite recursion here, so we
   29112             :          must catch that.  */
   29113           6 :       TYPENAME_IS_RESOLVING_P (result) = 1;
   29114           6 :       result = resolve_typename_type (result, only_current_p);
   29115           6 :       TYPENAME_IS_RESOLVING_P (result) = 0;
   29116             :     }
   29117             : 
   29118             :   /* Qualify the resulting type.  */
   29119       44479 :   quals = cp_type_quals (type);
   29120       44479 :   if (quals)
   29121           0 :     result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
   29122             : 
   29123             :   return result;
   29124             : }
   29125             : 
   29126             : /* EXPR is an expression which is not type-dependent.  Return a proxy
   29127             :    for EXPR that can be used to compute the types of larger
   29128             :    expressions containing EXPR.  */
   29129             : 
   29130             : tree
   29131   146763748 : build_non_dependent_expr (tree expr)
   29132             : {
   29133   146763748 :   tree orig_expr = expr;
   29134   146763748 :   tree inner_expr;
   29135             : 
   29136             :   /* When checking, try to get a constant value for all non-dependent
   29137             :      expressions in order to expose bugs in *_dependent_expression_p
   29138             :      and constexpr.  This can affect code generation, see PR70704, so
   29139             :      only do this for -fchecking=2.  */
   29140   146763748 :   if (flag_checking > 1
   29141   146763708 :       && cxx_dialect >= cxx11
   29142             :       /* Don't do this during nsdmi parsing as it can lead to
   29143             :          unexpected recursive instantiations.  */
   29144   146110111 :       && !parsing_nsdmi ()
   29145             :       /* Don't do this during concept processing either and for
   29146             :          the same reason.  */
   29147   289363280 :       && !processing_constraint_expression_p ())
   29148   142224926 :     fold_non_dependent_expr (expr, tf_none);
   29149             : 
   29150   146763748 :   STRIP_ANY_LOCATION_WRAPPER (expr);
   29151             : 
   29152             :   /* Preserve OVERLOADs; the functions must be available to resolve
   29153             :      types.  */
   29154   146763748 :   inner_expr = expr;
   29155   146763748 :   if (TREE_CODE (inner_expr) == STMT_EXPR)
   29156         207 :     inner_expr = stmt_expr_value_expr (inner_expr);
   29157   146763748 :   if (TREE_CODE (inner_expr) == ADDR_EXPR)
   29158      171415 :     inner_expr = TREE_OPERAND (inner_expr, 0);
   29159   146763748 :   if (TREE_CODE (inner_expr) == COMPONENT_REF)
   29160     6697510 :     inner_expr = TREE_OPERAND (inner_expr, 1);
   29161   146763748 :   if (is_overloaded_fn (inner_expr)
   29162   141334978 :       || TREE_CODE (inner_expr) == OFFSET_REF)
   29163             :     return orig_expr;
   29164             :   /* There is no need to return a proxy for a variable, parameter
   29165             :      or enumerator.  */
   29166   141334874 :   if (VAR_P (expr) || TREE_CODE (expr) == PARM_DECL
   29167             :       || TREE_CODE (expr) == CONST_DECL)
   29168             :     return orig_expr;
   29169             :   /* Preserve string constants; conversions from string constants to
   29170             :      "char *" are allowed, even though normally a "const char *"
   29171             :      cannot be used to initialize a "char *".  */
   29172             :   if (TREE_CODE (expr) == STRING_CST)
   29173             :     return orig_expr;
   29174             :   /* Preserve void and arithmetic constants, as an optimization -- there is no
   29175             :      reason to create a new node.  */
   29176             :   if (TREE_CODE (expr) == VOID_CST
   29177             :       || TREE_CODE (expr) == INTEGER_CST
   29178             :       || TREE_CODE (expr) == REAL_CST)
   29179             :     return orig_expr;
   29180             :   /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
   29181             :      There is at least one place where we want to know that a
   29182             :      particular expression is a throw-expression: when checking a ?:
   29183             :      expression, there are special rules if the second or third
   29184             :      argument is a throw-expression.  */
   29185             :   if (TREE_CODE (expr) == THROW_EXPR)
   29186             :     return orig_expr;
   29187             : 
   29188             :   /* Don't wrap an initializer list, we need to be able to look inside.  */
   29189       63907 :   if (BRACE_ENCLOSED_INITIALIZER_P (expr))
   29190             :     return orig_expr;
   29191             : 
   29192             :   /* Don't wrap a dummy object, we need to be able to test for it.  */
   29193   100535083 :   if (is_dummy_object (expr))
   29194             :     return orig_expr;
   29195             : 
   29196    98044578 :   if (TREE_CODE (expr) == COND_EXPR)
   29197      779850 :     return build3 (COND_EXPR,
   29198      259950 :                    TREE_TYPE (expr),
   29199      259950 :                    build_non_dependent_expr (TREE_OPERAND (expr, 0)),
   29200      259950 :                    (TREE_OPERAND (expr, 1)
   29201      259932 :                     ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
   29202          18 :                     : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
   29203      519900 :                    build_non_dependent_expr (TREE_OPERAND (expr, 2)));
   29204    97784628 :   if (TREE_CODE (expr) == COMPOUND_EXPR)
   29205      224648 :     return build2 (COMPOUND_EXPR,
   29206      112324 :                    TREE_TYPE (expr),
   29207      112324 :                    TREE_OPERAND (expr, 0),
   29208      224648 :                    build_non_dependent_expr (TREE_OPERAND (expr, 1)));
   29209             : 
   29210             :   /* If the type is unknown, it can't really be non-dependent */
   29211    97672304 :   gcc_assert (TREE_TYPE (expr) != unknown_type_node);
   29212             : 
   29213             :   /* Otherwise, build a NON_DEPENDENT_EXPR.  */
   29214    97672304 :   return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
   29215   195344608 :                      TREE_TYPE (expr), expr);
   29216             : }
   29217             : 
   29218             : /* ARGS is a vector of expressions as arguments to a function call.
   29219             :    Replace the arguments with equivalent non-dependent expressions.
   29220             :    This modifies ARGS in place.  */
   29221             : 
   29222             : void
   29223    15916901 : make_args_non_dependent (vec<tree, va_gc> *args)
   29224             : {
   29225    15916901 :   unsigned int ix;
   29226    15916901 :   tree arg;
   29227             : 
   29228    22830421 :   FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
   29229             :     {
   29230     6913520 :       tree newarg = build_non_dependent_expr (arg);
   29231     6913520 :       if (newarg != arg)
   29232     3010013 :         (*args)[ix] = newarg;
   29233             :     }
   29234    15916901 : }
   29235             : 
   29236             : /* Returns a type which represents 'auto' or 'decltype(auto)'.  We use a
   29237             :    TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms,
   29238             :    by default.  If set_canonical is true, we set TYPE_CANONICAL on it.  */
   29239             : 
   29240             : static tree
   29241    11434371 : make_auto_1 (tree name, bool set_canonical, int level = -1)
   29242             : {
   29243    11434371 :   if (level == -1)
   29244     5568162 :     level = current_template_depth + 1;
   29245    11434371 :   tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
   29246    11434371 :   TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
   29247    11434371 :   TYPE_STUB_DECL (au) = TYPE_NAME (au);
   29248    11434371 :   TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
   29249    11434371 :     (0, level, level, TYPE_NAME (au), NULL_TREE);
   29250    11434371 :   if (set_canonical)
   29251     5450031 :     TYPE_CANONICAL (au) = canonical_type_parameter (au);
   29252    11434371 :   DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
   29253    11434371 :   SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
   29254    11434371 :   if (name == decltype_auto_identifier)
   29255      272371 :     AUTO_IS_DECLTYPE (au) = true;
   29256             : 
   29257    11434371 :   return au;
   29258             : }
   29259             : 
   29260             : tree
   29261      156572 : make_decltype_auto (void)
   29262             : {
   29263      156572 :   return make_auto_1 (decltype_auto_identifier, true);
   29264             : }
   29265             : 
   29266             : tree
   29267     5293459 : make_auto (void)
   29268             : {
   29269     5293459 :   return make_auto_1 (auto_identifier, true);
   29270             : }
   29271             : 
   29272             : /* Return a C++17 deduction placeholder for class template TMPL.
   29273             :    There are represented as an 'auto' with the special level 0 and
   29274             :    CLASS_PLACEHOLDER_TEMPLATE set.  */
   29275             : 
   29276             : tree
   29277     5866209 : make_template_placeholder (tree tmpl)
   29278             : {
   29279     5866209 :   tree t = make_auto_1 (auto_identifier, false, /*level=*/0);
   29280     5866209 :   CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
   29281             :   /* Our canonical type depends on the placeholder.  */
   29282     5866209 :   TYPE_CANONICAL (t) = canonical_type_parameter (t);
   29283     5866209 :   return t;
   29284             : }
   29285             : 
   29286             : /* True iff T is a C++17 class template deduction placeholder.  */
   29287             : 
   29288             : bool
   29289  2714490295 : template_placeholder_p (tree t)
   29290             : {
   29291  2722642607 :   return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
   29292             : }
   29293             : 
   29294             : /* Make a "constrained auto" type-specifier. This is an auto or
   29295             :   decltype(auto) type with constraints that must be associated after
   29296             :   deduction.  The constraint is formed from the given concept CON
   29297             :   and its optional sequence of template arguments ARGS.
   29298             : 
   29299             :   TYPE must be the result of make_auto_type or make_decltype_auto_type. */
   29300             : 
   29301             : static tree
   29302      118099 : make_constrained_placeholder_type (tree type, tree con, tree args)
   29303             : {
   29304             :   /* Build the constraint. */
   29305      118099 :   tree tmpl = DECL_TI_TEMPLATE (con);
   29306      118099 :   tree expr = tmpl;
   29307      118099 :   if (TREE_CODE (con) == FUNCTION_DECL)
   29308          10 :     expr = ovl_make (tmpl);
   29309      118099 :   ++processing_template_decl;
   29310      118099 :   expr = build_concept_check (expr, type, args, tf_warning_or_error);
   29311      118099 :   --processing_template_decl;
   29312             : 
   29313      118099 :   PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type)
   29314      118099 :     = build_tree_list (current_template_parms, expr);
   29315             : 
   29316             :   /* Our canonical type depends on the constraint.  */
   29317      118099 :   TYPE_CANONICAL (type) = canonical_type_parameter (type);
   29318             : 
   29319             :   /* Attach the constraint to the type declaration. */
   29320      118099 :   return TYPE_NAME (type);
   29321             : }
   29322             : 
   29323             : /* Make a "constrained auto" type-specifier.  */
   29324             : 
   29325             : tree
   29326        2307 : make_constrained_auto (tree con, tree args)
   29327             : {
   29328        2307 :   tree type = make_auto_1 (auto_identifier, false);
   29329        2307 :   return make_constrained_placeholder_type (type, con, args);
   29330             : }
   29331             : 
   29332             : /* Make a "constrained decltype(auto)" type-specifier.  */
   29333             : 
   29334             : tree
   29335      115792 : make_constrained_decltype_auto (tree con, tree args)
   29336             : {
   29337      115792 :   tree type = make_auto_1 (decltype_auto_identifier, false);
   29338      115792 :   return make_constrained_placeholder_type (type, con, args);
   29339             : }
   29340             : 
   29341             : /* Returns true if the placeholder type constraint T has any dependent
   29342             :    (explicit) template arguments.  */
   29343             : 
   29344             : static bool
   29345          20 : placeholder_type_constraint_dependent_p (tree t)
   29346             : {
   29347          20 :   tree id = unpack_concept_check (t);
   29348          20 :   tree args = TREE_OPERAND (id, 1);
   29349          20 :   tree first = TREE_VEC_ELT (args, 0);
   29350          20 :   if (ARGUMENT_PACK_P (first))
   29351             :     {
   29352           6 :       args = expand_template_argument_pack (args);
   29353           6 :       first = TREE_VEC_ELT (args, 0);
   29354             :     }
   29355          20 :   gcc_checking_assert (TREE_CODE (first) == WILDCARD_DECL
   29356             :                        || is_auto (first));
   29357          26 :   for (int i = 1; i < TREE_VEC_LENGTH (args); ++i)
   29358          14 :     if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
   29359             :       return true;
   29360             :   return false;
   29361             : }
   29362             : 
   29363             : /* Build and return a concept definition. Like other templates, the
   29364             :    CONCEPT_DECL node is wrapped by a TEMPLATE_DECL.  This returns the
   29365             :    the TEMPLATE_DECL. */
   29366             : 
   29367             : tree
   29368      200428 : finish_concept_definition (cp_expr id, tree init, tree attrs)
   29369             : {
   29370      200428 :   gcc_assert (identifier_p (id));
   29371      200428 :   gcc_assert (processing_template_decl);
   29372             : 
   29373      200428 :   location_t loc = id.get_location();
   29374             : 
   29375             :   /* A concept-definition shall not have associated constraints.  */
   29376      200428 :   if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
   29377             :     {
   29378           1 :       error_at (loc, "a concept cannot be constrained");
   29379           1 :       TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
   29380             :     }
   29381             : 
   29382             :   /* A concept-definition shall appear in namespace scope.  Templates
   29383             :      aren't allowed in block scope, so we only need to check for class
   29384             :      scope.  */
   29385      200428 :   if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
   29386             :     {
   29387           2 :       error_at (loc, "concept %qE not in namespace scope", *id);
   29388           2 :       return error_mark_node;
   29389             :     }
   29390             : 
   29391      200426 :   if (current_template_depth > 1)
   29392             :     {
   29393           1 :       error_at (loc, "concept %qE has multiple template parameter lists", *id);
   29394           1 :       return error_mark_node;
   29395             :     }
   29396             : 
   29397             :   /* Initially build the concept declaration; its type is bool.  */
   29398      200425 :   tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
   29399      200425 :   DECL_CONTEXT (decl) = current_scope ();
   29400      200425 :   DECL_INITIAL (decl) = init;
   29401             : 
   29402      200425 :   if (attrs)
   29403           4 :     cplus_decl_attributes (&decl, attrs, 0);
   29404             : 
   29405      200425 :   set_originating_module (decl, false);
   29406             : 
   29407             :   /* Push the enclosing template.  */
   29408      200425 :   return push_template_decl (decl);
   29409             : }
   29410             : 
   29411             : /* Given type ARG, return std::initializer_list<ARG>.  */
   29412             : 
   29413             : static tree
   29414         294 : listify (tree arg)
   29415             : {
   29416         294 :   tree std_init_list = lookup_qualified_name (std_node, init_list_identifier);
   29417             : 
   29418         294 :   if (std_init_list == error_mark_node
   29419         294 :       || !DECL_CLASS_TEMPLATE_P (std_init_list))
   29420             :     {
   29421          15 :       gcc_rich_location richloc (input_location);
   29422          15 :       maybe_add_include_fixit (&richloc, "<initializer_list>", false);
   29423          15 :       error_at (&richloc,
   29424             :                 "deducing from brace-enclosed initializer list"
   29425             :                 " requires %<#include <initializer_list>%>");
   29426             : 
   29427          15 :       return error_mark_node;
   29428          15 :     }
   29429         279 :   tree argvec = make_tree_vec (1);
   29430         279 :   TREE_VEC_ELT (argvec, 0) = arg;
   29431             : 
   29432         279 :   return lookup_template_class (std_init_list, argvec, NULL_TREE,
   29433         279 :                                 NULL_TREE, 0, tf_warning_or_error);
   29434             : }
   29435             : 
   29436             : /* Replace auto in TYPE with std::initializer_list<auto>.  */
   29437             : 
   29438             : static tree
   29439         294 : listify_autos (tree type, tree auto_node)
   29440             : {
   29441         294 :   tree init_auto = listify (strip_top_quals (auto_node));
   29442         294 :   tree argvec = make_tree_vec (1);
   29443         294 :   TREE_VEC_ELT (argvec, 0) = init_auto;
   29444         294 :   if (processing_template_decl)
   29445           3 :     argvec = add_to_template_args (current_template_args (), argvec);
   29446         294 :   return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
   29447             : }
   29448             : 
   29449             : /* Hash traits for hashing possibly constrained 'auto'
   29450             :    TEMPLATE_TYPE_PARMs for use by do_auto_deduction.  */
   29451             : 
   29452             : struct auto_hash : default_hash_traits<tree>
   29453             : {
   29454             :   static inline hashval_t hash (tree);
   29455             :   static inline bool equal (tree, tree);
   29456             : };
   29457             : 
   29458             : /* Hash the 'auto' T.  */
   29459             : 
   29460             : inline hashval_t
   29461         214 : auto_hash::hash (tree t)
   29462             : {
   29463         214 :   if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
   29464             :     /* Matching constrained-type-specifiers denote the same template
   29465             :        parameter, so hash the constraint.  */
   29466          30 :     return hash_placeholder_constraint (c);
   29467             :   else
   29468             :     /* But unconstrained autos are all separate, so just hash the pointer.  */
   29469         184 :     return iterative_hash_object (t, 0);
   29470             : }
   29471             : 
   29472             : /* Compare two 'auto's.  */
   29473             : 
   29474             : inline bool
   29475          12 : auto_hash::equal (tree t1, tree t2)
   29476             : {
   29477          12 :   if (t1 == t2)
   29478             :     return true;
   29479             : 
   29480          12 :   tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
   29481          12 :   tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
   29482             : 
   29483             :   /* Two unconstrained autos are distinct.  */
   29484          12 :   if (!c1 || !c2)
   29485             :     return false;
   29486             : 
   29487           5 :   return equivalent_placeholder_constraints (c1, c2);
   29488             : }
   29489             : 
   29490             : /* for_each_template_parm callback for extract_autos: if t is a (possibly
   29491             :    constrained) auto, add it to the vector.  */
   29492             : 
   29493             : static int
   29494         203 : extract_autos_r (tree t, void *data)
   29495             : {
   29496         203 :   hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
   29497         203 :   if (is_auto (t) && !template_placeholder_p (t))
   29498             :     {
   29499             :       /* All the autos were built with index 0; fix that up now.  */
   29500         203 :       tree *p = hash.find_slot (t, INSERT);
   29501         203 :       int idx;
   29502         203 :       if (*p)
   29503             :         /* If this is a repeated constrained-type-specifier, use the index we
   29504             :            chose before.  */
   29505           5 :         idx = TEMPLATE_TYPE_IDX (*p);
   29506             :       else
   29507             :         {
   29508             :           /* Otherwise this is new, so use the current count.  */
   29509         198 :           *p = t;
   29510         198 :           idx = hash.elements () - 1;
   29511             :         }
   29512         203 :       if (idx != TEMPLATE_TYPE_IDX (t))
   29513             :         {
   29514           6 :           gcc_checking_assert (TEMPLATE_TYPE_IDX (t) == 0);
   29515           6 :           gcc_checking_assert (TYPE_CANONICAL (t) != t);
   29516           6 :           TEMPLATE_TYPE_IDX (t) = idx;
   29517           6 :           TYPE_CANONICAL (t) = canonical_type_parameter (t);
   29518             :         }
   29519             :     }
   29520             : 
   29521             :   /* Always keep walking.  */
   29522         203 :   return 0;
   29523             : }
   29524             : 
   29525             : /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
   29526             :    says they can appear anywhere in the type.  */
   29527             : 
   29528             : static tree
   29529         192 : extract_autos (tree type)
   29530             : {
   29531         192 :   hash_set<tree> visited;
   29532         192 :   hash_table<auto_hash> hash (2);
   29533             : 
   29534         192 :   for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
   29535             : 
   29536         192 :   tree tree_vec = make_tree_vec (hash.elements());
   29537         390 :   for (tree elt : hash)
   29538             :     {
   29539         198 :       unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
   29540         198 :       TREE_VEC_ELT (tree_vec, i)
   29541         396 :         = build_tree_list (NULL_TREE, TYPE_NAME (elt));
   29542             :     }
   29543             : 
   29544         384 :   return tree_vec;
   29545         192 : }
   29546             : 
   29547             : /* The stem for deduction guide names.  */
   29548             : const char *const dguide_base = "__dguide_";
   29549             : 
   29550             : /* Return the name for a deduction guide for class template TMPL.  */
   29551             : 
   29552             : tree
   29553      560858 : dguide_name (tree tmpl)
   29554             : {
   29555      560858 :   tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
   29556      560858 :   tree tname = TYPE_IDENTIFIER (type);
   29557      560858 :   char *buf = (char *) alloca (1 + strlen (dguide_base)
   29558             :                                + IDENTIFIER_LENGTH (tname));
   29559      560858 :   memcpy (buf, dguide_base, strlen (dguide_base));
   29560      560858 :   memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
   29561      560858 :           IDENTIFIER_LENGTH (tname) + 1);
   29562      560858 :   tree dname = get_identifier (buf);
   29563      560858 :   TREE_TYPE (dname) = type;
   29564      560858 :   return dname;
   29565             : }
   29566             : 
   29567             : /* True if NAME is the name of a deduction guide.  */
   29568             : 
   29569             : bool
   29570   459074889 : dguide_name_p (tree name)
   29571             : {
   29572   459074889 :   return (TREE_CODE (name) == IDENTIFIER_NODE
   29573   459074887 :           && TREE_TYPE (name)
   29574   491512795 :           && startswith (IDENTIFIER_POINTER (name), dguide_base));
   29575             : }
   29576             : 
   29577             : /* True if FN is a deduction guide.  */
   29578             : 
   29579             : bool
   29580   258219930 : deduction_guide_p (const_tree fn)
   29581             : {
   29582   258219930 :   if (DECL_P (fn))
   29583   258219892 :     if (tree name = DECL_NAME (fn))
   29584   258219892 :       return dguide_name_p (name);
   29585             :   return false;
   29586             : }
   29587             : 
   29588             : /* True if FN is the copy deduction guide, i.e. A(A)->A.  */
   29589             : 
   29590             : bool
   29591         589 : copy_guide_p (const_tree fn)
   29592             : {
   29593         589 :   gcc_assert (deduction_guide_p (fn));
   29594         589 :   if (!DECL_ARTIFICIAL (fn))
   29595             :     return false;
   29596         589 :   tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
   29597         589 :   return (TREE_CHAIN (parms) == void_list_node
   29598         589 :           && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
   29599             : }
   29600             : 
   29601             : /* True if FN is a guide generated from a constructor template.  */
   29602             : 
   29603             : bool
   29604          32 : template_guide_p (const_tree fn)
   29605             : {
   29606          32 :   gcc_assert (deduction_guide_p (fn));
   29607          32 :   if (!DECL_ARTIFICIAL (fn))
   29608             :     return false;
   29609          32 :   tree tmpl = DECL_TI_TEMPLATE (fn);
   29610          32 :   if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
   29611          32 :     return PRIMARY_TEMPLATE_P (org);
   29612             :   return false;
   29613             : }
   29614             : 
   29615             : /* True if FN is an aggregate initialization guide or the copy deduction
   29616             :    guide.  */
   29617             : 
   29618             : bool
   29619           4 : builtin_guide_p (const_tree fn)
   29620             : {
   29621           4 :   if (!deduction_guide_p (fn))
   29622             :     return false;
   29623           4 :   if (!DECL_ARTIFICIAL (fn))
   29624             :     /* Explicitly declared.  */
   29625             :     return false;
   29626           4 :   if (DECL_ABSTRACT_ORIGIN (fn))
   29627             :     /* Derived from a constructor.  */
   29628           0 :     return false;
   29629             :   return true;
   29630             : }
   29631             : 
   29632             : /* OLDDECL is a _DECL for a template parameter.  Return a similar parameter at
   29633             :    LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
   29634             :    template parameter types.  Note that the handling of template template
   29635             :    parameters relies on current_template_parms being set appropriately for the
   29636             :    new template.  */
   29637             : 
   29638             : static tree
   29639        3995 : rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
   29640             :                        tree tsubst_args, tsubst_flags_t complain)
   29641             : {
   29642        3995 :   if (olddecl == error_mark_node)
   29643             :     return error_mark_node;
   29644             : 
   29645        3993 :   tree oldidx = get_template_parm_index (olddecl);
   29646             : 
   29647        3993 :   tree newtype;
   29648        3993 :   if (TREE_CODE (olddecl) == TYPE_DECL
   29649        1409 :       || TREE_CODE (olddecl) == TEMPLATE_DECL)
   29650             :     {
   29651        2600 :       tree oldtype = TREE_TYPE (olddecl);
   29652        2600 :       newtype = cxx_make_type (TREE_CODE (oldtype));
   29653        2600 :       TYPE_MAIN_VARIANT (newtype) = newtype;
   29654        2600 :     }
   29655             :   else
   29656             :     {
   29657        1393 :       newtype = TREE_TYPE (olddecl);
   29658        1393 :       if (type_uses_auto (newtype))
   29659             :         {
   29660             :           // Substitute once to fix references to other template parameters.
   29661           3 :           newtype = tsubst (newtype, tsubst_args,
   29662             :                             complain|tf_partial, NULL_TREE);
   29663             :           // Now substitute again to reduce the level of the auto.
   29664           3 :           newtype = tsubst (newtype, current_template_args (),
   29665             :                             complain, NULL_TREE);
   29666             :         }
   29667             :       else
   29668        1390 :         newtype = tsubst (newtype, tsubst_args,
   29669             :                           complain, NULL_TREE);
   29670             :     }
   29671             : 
   29672        3993 :   tree newdecl
   29673        3993 :     = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
   29674        3993 :                   DECL_NAME (olddecl), newtype);
   29675        3993 :   SET_DECL_TEMPLATE_PARM_P (newdecl);
   29676             : 
   29677        3993 :   tree newidx;
   29678        3993 :   if (TREE_CODE (olddecl) == TYPE_DECL
   29679        1409 :       || TREE_CODE (olddecl) == TEMPLATE_DECL)
   29680             :     {
   29681        2600 :       newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
   29682        2600 :         = build_template_parm_index (index, level, level,
   29683             :                                      newdecl, newtype);
   29684        5200 :       TEMPLATE_PARM_PARAMETER_PACK (newidx)
   29685        2600 :         = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
   29686        2600 :       TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
   29687             : 
   29688        2600 :       if (TREE_CODE (olddecl) == TEMPLATE_DECL)
   29689             :         {
   29690          32 :           DECL_TEMPLATE_RESULT (newdecl)
   29691          16 :             = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
   29692          16 :                           DECL_NAME (olddecl), newtype);
   29693          16 :           DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
   29694             :           // First create a copy (ttargs) of tsubst_args with an
   29695             :           // additional level for the template template parameter's own
   29696             :           // template parameters (ttparms).
   29697          16 :           tree ttparms = (INNERMOST_TEMPLATE_PARMS
   29698          16 :                           (DECL_TEMPLATE_PARMS (olddecl)));
   29699          32 :           const int depth = TMPL_ARGS_DEPTH (tsubst_args);
   29700          16 :           tree ttargs = make_tree_vec (depth + 1);
   29701          44 :           for (int i = 0; i < depth; ++i)
   29702          56 :             TREE_VEC_ELT (ttargs, i) = TMPL_ARGS_LEVEL (tsubst_args, i + 1);
   29703          16 :           TREE_VEC_ELT (ttargs, depth)
   29704          16 :             = template_parms_level_to_args (ttparms);
   29705             :           // Substitute ttargs into ttparms to fix references to
   29706             :           // other template parameters.
   29707          16 :           ttparms = tsubst_template_parms_level (ttparms, ttargs,
   29708             :                                                  complain|tf_partial);
   29709             :           // Now substitute again with args based on tparms, to reduce
   29710             :           // the level of the ttparms.
   29711          16 :           ttargs = current_template_args ();
   29712          16 :           ttparms = tsubst_template_parms_level (ttparms, ttargs,
   29713             :                                                  complain);
   29714             :           // Finally, tack the adjusted parms onto tparms.
   29715          16 :           ttparms = tree_cons (size_int (level + 1), ttparms,
   29716             :                                copy_node (current_template_parms));
   29717             :           // As with all template template parms, the parameter list captured
   29718             :           // by this template template parm that corresponds to its own level
   29719             :           // should be empty.  This avoids infinite recursion when structurally
   29720             :           // comparing two such rewritten template template parms (PR102479).
   29721          16 :           gcc_assert (!TREE_VEC_LENGTH
   29722             :                       (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl)))));
   29723          16 :           gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms)) == level);
   29724          16 :           TREE_VALUE (TREE_CHAIN (ttparms)) = make_tree_vec (0);
   29725             :           // All done.
   29726          16 :           DECL_TEMPLATE_PARMS (newdecl) = ttparms;
   29727             :         }
   29728             : 
   29729        2600 :       if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
   29730           0 :         SET_TYPE_STRUCTURAL_EQUALITY (newtype);
   29731             :       else
   29732        2600 :         TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
   29733             :     }
   29734             :   else
   29735             :     {
   29736        1393 :       tree oldconst = TEMPLATE_PARM_DECL (oldidx);
   29737        1393 :       tree newconst
   29738        1393 :         = build_decl (DECL_SOURCE_LOCATION (oldconst),
   29739        1393 :                       TREE_CODE (oldconst),
   29740        1393 :                       DECL_NAME (oldconst), newtype);
   29741        5572 :       TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
   29742        1393 :         = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
   29743        1393 :       SET_DECL_TEMPLATE_PARM_P (newconst);
   29744        1393 :       newidx = build_template_parm_index (index, level, level,
   29745             :                                           newconst, newtype);
   29746        2786 :       TEMPLATE_PARM_PARAMETER_PACK (newidx)
   29747        1393 :         = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
   29748        1393 :       DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
   29749             :     }
   29750             : 
   29751             :   return newdecl;
   29752             : }
   29753             : 
   29754             : /* As rewrite_template_parm, but for the whole TREE_LIST representing a
   29755             :    template parameter.  */
   29756             : 
   29757             : static tree
   29758        3995 : rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
   29759             :                     tree targs, unsigned targs_index, tsubst_flags_t complain)
   29760             : {
   29761        3995 :   tree olddecl = TREE_VALUE (oldelt);
   29762        3995 :   tree newdecl = rewrite_template_parm (olddecl, index, level,
   29763             :                                         targs, complain);
   29764        3995 :   if (newdecl == error_mark_node)
   29765             :     return error_mark_node;
   29766        3993 :   tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
   29767             :                                      targs, complain, NULL_TREE);
   29768        3993 :   tree list = build_tree_list (newdef, newdecl);
   29769        7986 :   TEMPLATE_PARM_CONSTRAINTS (list)
   29770        3993 :     = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
   29771             :                               targs, complain, NULL_TREE);
   29772        7986 :   int depth = TMPL_ARGS_DEPTH (targs);
   29773        7986 :   TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
   29774        3993 :   return list;
   29775             : }
   29776             : 
   29777             : /* Returns a C++17 class deduction guide template based on the constructor
   29778             :    CTOR.  As a special case, CTOR can be a RECORD_TYPE for an implicit default
   29779             :    guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
   29780             :    aggregate initialization guide.  OUTER_ARGS are the template arguments
   29781             :    for the enclosing scope of the class.  */
   29782             : 
   29783             : static tree
   29784        5421 : build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
   29785             : {
   29786        5421 :   tree tparms, targs, fparms, fargs, ci;
   29787        5421 :   bool memtmpl = false;
   29788        5421 :   bool explicit_p;
   29789        5421 :   location_t loc;
   29790        5421 :   tree fn_tmpl = NULL_TREE;
   29791             : 
   29792        5421 :   if (outer_args)
   29793             :     {
   29794          68 :       ++processing_template_decl;
   29795          68 :       type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
   29796          68 :       --processing_template_decl;
   29797             :     }
   29798             : 
   29799        5421 :   if (!DECL_DECLARES_FUNCTION_P (ctor))
   29800             :     {
   29801        1488 :       if (TYPE_P (ctor))
   29802             :         {
   29803        1393 :           bool copy_p = TYPE_REF_P (ctor);
   29804        1393 :           if (copy_p)
   29805        1170 :             fparms = tree_cons (NULL_TREE, type, void_list_node);
   29806             :           else
   29807         223 :             fparms = void_list_node;
   29808             :         }
   29809          95 :       else if (TREE_CODE (ctor) == TREE_LIST)
   29810             :         fparms = ctor;
   29811             :       else
   29812           0 :         gcc_unreachable ();
   29813             : 
   29814        1488 :       tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
   29815        1488 :       tparms = DECL_TEMPLATE_PARMS (ctmpl);
   29816        1488 :       targs = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
   29817        1488 :       ci = NULL_TREE;
   29818        1488 :       fargs = NULL_TREE;
   29819        1488 :       loc = DECL_SOURCE_LOCATION (ctmpl);
   29820        1488 :       explicit_p = false;
   29821             :     }
   29822             :   else
   29823             :     {
   29824        3933 :       ++processing_template_decl;
   29825        3933 :       bool ok = true;
   29826             : 
   29827        3933 :       complain |= tf_dguide;
   29828             : 
   29829        3933 :       fn_tmpl
   29830        3933 :         = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
   29831        1761 :            : DECL_TI_TEMPLATE (ctor));
   29832        3933 :       if (outer_args)
   29833          26 :         fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
   29834        3933 :       ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
   29835             : 
   29836        3933 :       tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
   29837             :       /* If type is a member class template, DECL_TI_ARGS (ctor) will have
   29838             :          fully specialized args for the enclosing class.  Strip those off, as
   29839             :          the deduction guide won't have those template parameters.  */
   29840        7866 :       targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
   29841        3933 :                                                 TMPL_PARMS_DEPTH (tparms));
   29842             :       /* Discard the 'this' parameter.  */
   29843        3933 :       fparms = FUNCTION_ARG_CHAIN (ctor);
   29844        3933 :       fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
   29845        3933 :       ci = get_constraints (ctor);
   29846        3933 :       loc = DECL_SOURCE_LOCATION (ctor);
   29847        3933 :       explicit_p = DECL_NONCONVERTING_P (ctor);
   29848             : 
   29849        3933 :       if (PRIMARY_TEMPLATE_P (fn_tmpl))
   29850             :         {
   29851        2172 :           memtmpl = true;
   29852             : 
   29853             :           /* For a member template constructor, we need to flatten the two
   29854             :              template parameter lists into one, and then adjust the function
   29855             :              signature accordingly.  This gets...complicated.  */
   29856        2172 :           tree save_parms = current_template_parms;
   29857             : 
   29858             :           /* For a member template we should have two levels of parms/args, one
   29859             :              for the class and one for the constructor.  We stripped
   29860             :              specialized args for further enclosing classes above.  */
   29861        2172 :           const int depth = 2;
   29862        4344 :           gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
   29863             : 
   29864             :           /* Template args for translating references to the two-level template
   29865             :              parameters into references to the one-level template parameters we
   29866             :              are creating.  */
   29867        2172 :           tree tsubst_args = copy_node (targs);
   29868        2172 :           TMPL_ARGS_LEVEL (tsubst_args, depth)
   29869        4344 :             = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
   29870             : 
   29871             :           /* Template parms for the constructor template.  */
   29872        2172 :           tree ftparms = TREE_VALUE (tparms);
   29873        2172 :           unsigned flen = TREE_VEC_LENGTH (ftparms);
   29874             :           /* Template parms for the class template.  */
   29875        2172 :           tparms = TREE_CHAIN (tparms);
   29876        2172 :           tree ctparms = TREE_VALUE (tparms);
   29877        2172 :           unsigned clen = TREE_VEC_LENGTH (ctparms);
   29878             :           /* Template parms for the deduction guide start as a copy of the
   29879             :              template parms for the class.  We set current_template_parms for
   29880             :              lookup_template_class_1.  */
   29881        2172 :           current_template_parms = tparms = copy_node (tparms);
   29882        2172 :           tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
   29883        6136 :           for (unsigned i = 0; i < clen; ++i)
   29884        3964 :             TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
   29885             : 
   29886             :           /* Now we need to rewrite the constructor parms to append them to the
   29887             :              class parms.  */
   29888        6066 :           for (unsigned i = 0; i < flen; ++i)
   29889             :             {
   29890        3894 :               unsigned index = i + clen;
   29891        3894 :               unsigned level = 1;
   29892        3894 :               tree oldelt = TREE_VEC_ELT (ftparms, i);
   29893        3894 :               tree newelt
   29894        3894 :                 = rewrite_tparm_list (oldelt, index, level,
   29895             :                                       tsubst_args, i, complain);
   29896        3894 :               if (newelt == error_mark_node)
   29897           2 :                 ok = false;
   29898        3894 :               TREE_VEC_ELT (new_vec, index) = newelt;
   29899             :             }
   29900             : 
   29901             :           /* Now we have a final set of template parms to substitute into the
   29902             :              function signature.  */
   29903        2172 :           targs = template_parms_to_args (tparms);
   29904        2172 :           fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
   29905             :                                      complain, ctor);
   29906        2172 :           if (fparms == error_mark_node)
   29907           0 :             ok = false;
   29908        2172 :           if (ci)
   29909             :             {
   29910        1381 :               if (outer_args)
   29911             :                 /* FIXME: We'd like to avoid substituting outer template
   29912             :                    arguments into the constraint ahead of time, but the
   29913             :                    construction of tsubst_args assumes that outer arguments
   29914             :                    are already substituted in.  */
   29915           2 :                 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
   29916        1381 :               ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
   29917             :             }
   29918             : 
   29919             :           /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
   29920             :              cp_unevaluated_operand.  */
   29921        2172 :           cp_evaluated ev;
   29922        2172 :           fargs = tsubst (fargs, tsubst_args, complain, ctor);
   29923        2172 :           current_template_parms = save_parms;
   29924        2172 :         }
   29925             :       else
   29926             :         {
   29927             :           /* Substitute in the same arguments to rewrite class members into
   29928             :              references to members of an unknown specialization.  */
   29929        1761 :           cp_evaluated ev;
   29930        1761 :           fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
   29931        1761 :           fargs = tsubst (fargs, targs, complain, ctor);
   29932        1761 :           if (ci)
   29933             :             {
   29934         558 :               if (outer_args)
   29935             :                 /* FIXME: As above.  */
   29936           2 :                 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
   29937         558 :               ci = tsubst_constraint_info (ci, targs, complain, ctor);
   29938             :             }
   29939        1761 :         }
   29940             : 
   29941        3933 :       --processing_template_decl;
   29942        3933 :       if (!ok)
   29943           2 :         return error_mark_node;
   29944             :     }
   29945             : 
   29946        5419 :   if (!memtmpl)
   29947             :     {
   29948             :       /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE.  */
   29949        3249 :       tparms = copy_node (tparms);
   29950        3249 :       INNERMOST_TEMPLATE_PARMS (tparms)
   29951        3249 :         = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
   29952             :     }
   29953             : 
   29954        5419 :   tree fntype = build_function_type (type, fparms);
   29955        5419 :   tree ded_fn = build_lang_decl_loc (loc,
   29956             :                                      FUNCTION_DECL,
   29957             :                                      dguide_name (type), fntype);
   29958        5419 :   DECL_ARGUMENTS (ded_fn) = fargs;
   29959        5419 :   DECL_ARTIFICIAL (ded_fn) = true;
   29960        5419 :   DECL_NONCONVERTING_P (ded_fn) = explicit_p;
   29961        5419 :   tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
   29962        5419 :   DECL_ARTIFICIAL (ded_tmpl) = true;
   29963        5419 :   DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
   29964        5419 :   DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
   29965        5419 :   if (DECL_P (ctor))
   29966        3931 :     DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
   29967        5419 :   if (ci)
   29968        1939 :     set_constraints (ded_tmpl, ci);
   29969             : 
   29970             :   return ded_tmpl;
   29971             : }
   29972             : 
   29973             : /* Add to LIST the member types for the reshaped initializer CTOR.  */
   29974             : 
   29975             : static tree
   29976         144 : collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
   29977             : {
   29978         144 :   vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
   29979         144 :   tree idx, val; unsigned i;
   29980         284 :   FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
   29981             :     {
   29982         140 :       tree ftype = elt ? elt : TREE_TYPE (idx);
   29983          23 :       if (BRACE_ENCLOSED_INITIALIZER_P (val)
   29984         163 :           && CONSTRUCTOR_BRACES_ELIDED_P (val))
   29985             :         {
   29986           9 :           tree subelt = NULL_TREE;
   29987           9 :           if (TREE_CODE (ftype) == ARRAY_TYPE)
   29988           7 :             subelt = TREE_TYPE (ftype);
   29989           9 :           list = collect_ctor_idx_types (val, list, subelt);
   29990           9 :           continue;
   29991           9 :         }
   29992         131 :       tree arg = NULL_TREE;
   29993         131 :       if (i == v->length() - 1
   29994         131 :           && PACK_EXPANSION_P (ftype))
   29995             :         /* Give the trailing pack expansion parameter a default argument to
   29996             :            match aggregate initialization behavior, even if we deduce the
   29997             :            length of the pack separately to more than we have initializers. */
   29998           5 :         arg = build_constructor (init_list_type_node, NULL);
   29999             :       /* if ei is of array type and xi is a braced-init-list or string literal,
   30000             :          Ti is an rvalue reference to the declared type of ei */
   30001         131 :       STRIP_ANY_LOCATION_WRAPPER (val);
   30002         131 :       if (TREE_CODE (ftype) == ARRAY_TYPE
   30003         131 :           && (BRACE_ENCLOSED_INITIALIZER_P (val)
   30004           2 :               || TREE_CODE (val) == STRING_CST))
   30005             :         {
   30006           8 :           if (TREE_CODE (val) == STRING_CST)
   30007           2 :             ftype = cp_build_qualified_type
   30008           2 :               (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
   30009           8 :           ftype = (cp_build_reference_type
   30010          10 :                    (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
   30011             :         }
   30012         131 :       list = tree_cons (arg, ftype, list);
   30013             :     }
   30014             : 
   30015         144 :   return list;
   30016             : }
   30017             : 
   30018             : /* Return whether ETYPE is, or is derived from, a specialization of TMPL.  */
   30019             : 
   30020             : static bool
   30021         979 : is_spec_or_derived (tree etype, tree tmpl)
   30022             : {
   30023         979 :   if (!etype || !CLASS_TYPE_P (etype))
   30024             :     return false;
   30025             : 
   30026         521 :   etype = cv_unqualified (etype);
   30027         521 :   tree type = TREE_TYPE (tmpl);
   30028         521 :   tree tparms = (INNERMOST_TEMPLATE_PARMS
   30029         521 :                  (DECL_TEMPLATE_PARMS (tmpl)));
   30030         521 :   tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
   30031         521 :   int err = unify (tparms, targs, type, etype,
   30032             :                    UNIFY_ALLOW_DERIVED, /*explain*/false);
   30033         521 :   ggc_free (targs);
   30034         521 :   return !err;
   30035             : }
   30036             : 
   30037             : static tree alias_ctad_tweaks (tree, tree);
   30038             : 
   30039             : /* Return a C++20 aggregate deduction candidate for TYPE initialized from
   30040             :    INIT.  */
   30041             : 
   30042             : static tree
   30043         923 : maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
   30044             : {
   30045         923 :   if (cxx_dialect < cxx20)
   30046             :     return NULL_TREE;
   30047             : 
   30048         715 :   if (init == NULL_TREE)
   30049             :     return NULL_TREE;
   30050             : 
   30051         686 :   if (DECL_ALIAS_TEMPLATE_P (tmpl))
   30052             :     {
   30053          28 :       tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   30054          28 :       tree tinfo = get_template_info (under);
   30055          28 :       if (tree guide = maybe_aggr_guide (TI_TEMPLATE (tinfo), init, args))
   30056           1 :         return alias_ctad_tweaks (tmpl, guide);
   30057             :       return NULL_TREE;
   30058             :     }
   30059             : 
   30060             :   /* We might be creating a guide for a class member template, e.g.,
   30061             : 
   30062             :        template<typename U> struct A {
   30063             :          template<typename T> struct B { T t; };
   30064             :        };
   30065             : 
   30066             :      At this point, A will have been instantiated.  Below, we need to
   30067             :      use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types.  */
   30068         658 :   const bool member_template_p
   30069         658 :     = (DECL_TEMPLATE_INFO (tmpl)
   30070         658 :        && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl)));
   30071         658 :   tree type = TREE_TYPE (tmpl);
   30072         658 :   tree template_type = (member_template_p
   30073         658 :                         ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl))
   30074         658 :                         : type);
   30075         658 :   if (!CP_AGGREGATE_TYPE_P (template_type))
   30076             :     return NULL_TREE;
   30077             : 
   30078             :   /* No aggregate candidate for copy-initialization.  */
   30079         176 :   if (args->length() == 1)
   30080             :     {
   30081         176 :       tree val = (*args)[0];
   30082         176 :       if (is_spec_or_derived (TREE_TYPE (val), tmpl))
   30083             :         return NULL_TREE;
   30084             :     }
   30085             : 
   30086             :   /* If we encounter a problem, we just won't add the candidate.  */
   30087         140 :   tsubst_flags_t complain = tf_none;
   30088             : 
   30089         140 :   tree parms = NULL_TREE;
   30090         140 :   if (BRACE_ENCLOSED_INITIALIZER_P (init))
   30091             :     {
   30092         139 :       init = reshape_init (template_type, init, complain);
   30093         139 :       if (init == error_mark_node)
   30094             :         return NULL_TREE;
   30095         135 :       parms = collect_ctor_idx_types (init, parms);
   30096             :       /* If we're creating a deduction guide for a member class template,
   30097             :          we've used the original template pattern type for the reshape_init
   30098             :          above; this is done because we want PARMS to be a template parameter
   30099             :          type, something that can be deduced when used as a function template
   30100             :          parameter.  At this point the outer class template has already been
   30101             :          partially instantiated (we deferred the deduction until the enclosing
   30102             :          scope is non-dependent).  Therefore we have to partially instantiate
   30103             :          PARMS, so that its template level is properly reduced and we don't get
   30104             :          mismatches when deducing types using the guide with PARMS.  */
   30105         135 :       if (member_template_p)
   30106             :         {
   30107           5 :           ++processing_template_decl;
   30108           5 :           parms = tsubst (parms, DECL_TI_ARGS (tmpl), complain, init);
   30109           5 :           --processing_template_decl;
   30110             :         }
   30111             :     }
   30112           1 :   else if (TREE_CODE (init) == TREE_LIST)
   30113             :     {
   30114           0 :       int len = list_length (init);
   30115           0 :       for (tree field = TYPE_FIELDS (type);
   30116           0 :            len;
   30117           0 :            --len, field = DECL_CHAIN (field))
   30118             :         {
   30119           0 :           field = next_aggregate_field (field);
   30120           0 :           if (!field)
   30121             :             return NULL_TREE;
   30122           0 :           tree ftype = finish_decltype_type (field, true, complain);
   30123           0 :           parms = tree_cons (NULL_TREE, ftype, parms);
   30124             :         }
   30125             :     }
   30126             :   else
   30127             :     /* Aggregate initialization doesn't apply to an initializer expression.  */
   30128             :     return NULL_TREE;
   30129             : 
   30130         135 :   if (parms)
   30131             :     {
   30132          95 :       tree last = parms;
   30133          95 :       parms = nreverse (parms);
   30134          95 :       TREE_CHAIN (last) = void_list_node;
   30135          95 :       tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
   30136          95 :       return guide;
   30137             :     }
   30138             : 
   30139             :   return NULL_TREE;
   30140             : }
   30141             : 
   30142             : /* UGUIDES are the deduction guides for the underlying template of alias
   30143             :    template TMPL; adjust them to be deduction guides for TMPL.  */
   30144             : 
   30145             : static tree
   30146          28 : alias_ctad_tweaks (tree tmpl, tree uguides)
   30147             : {
   30148             :   /* [over.match.class.deduct]: When resolving a placeholder for a deduced
   30149             :      class type (9.2.8.2) where the template-name names an alias template A,
   30150             :      the defining-type-id of A must be of the form
   30151             : 
   30152             :      typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
   30153             : 
   30154             :      as specified in 9.2.8.2. The guides of A are the set of functions or
   30155             :      function templates formed as follows. For each function or function
   30156             :      template f in the guides of the template named by the simple-template-id
   30157             :      of the defining-type-id, the template arguments of the return type of f
   30158             :      are deduced from the defining-type-id of A according to the process in
   30159             :      13.10.2.5 with the exception that deduction does not fail if not all
   30160             :      template arguments are deduced. Let g denote the result of substituting
   30161             :      these deductions into f. If substitution succeeds, form a function or
   30162             :      function template f' with the following properties and add it to the set
   30163             :      of guides of A:
   30164             : 
   30165             :      * The function type of f' is the function type of g.
   30166             : 
   30167             :      * If f is a function template, f' is a function template whose template
   30168             :      parameter list consists of all the template parameters of A (including
   30169             :      their default template arguments) that appear in the above deductions or
   30170             :      (recursively) in their default template arguments, followed by the
   30171             :      template parameters of f that were not deduced (including their default
   30172             :      template arguments), otherwise f' is not a function template.
   30173             : 
   30174             :      * The associated constraints (13.5.2) are the conjunction of the
   30175             :      associated constraints of g and a constraint that is satisfied if and only
   30176             :      if the arguments of A are deducible (see below) from the return type.
   30177             : 
   30178             :      * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
   30179             :      be so as well.
   30180             : 
   30181             :      * If f was generated from a deduction-guide (12.4.1.8), then f' is
   30182             :      considered to be so as well.
   30183             : 
   30184             :      * The explicit-specifier of f' is the explicit-specifier of g (if
   30185             :      any).  */
   30186             : 
   30187          28 :   tsubst_flags_t complain = tf_warning_or_error;
   30188          28 :   tree atype = TREE_TYPE (tmpl);
   30189          28 :   tree aguides = NULL_TREE;
   30190          28 :   tree fullatparms = DECL_TEMPLATE_PARMS (tmpl);
   30191          28 :   tree atparms = INNERMOST_TEMPLATE_PARMS (fullatparms);
   30192          28 :   unsigned natparms = TREE_VEC_LENGTH (atparms);
   30193          28 :   tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   30194         182 :   for (ovl_iterator iter (uguides); iter; ++iter)
   30195             :     {
   30196          77 :       tree f = *iter;
   30197          77 :       tree in_decl = f;
   30198          77 :       location_t loc = DECL_SOURCE_LOCATION (f);
   30199          77 :       tree ret = TREE_TYPE (TREE_TYPE (f));
   30200          77 :       tree fprime = f;
   30201          77 :       if (TREE_CODE (f) == TEMPLATE_DECL)
   30202             :         {
   30203          76 :           processing_template_decl_sentinel ptds (/*reset*/false);
   30204          76 :           ++processing_template_decl;
   30205             : 
   30206             :           /* Deduce template arguments for f from the type-id of A.  */
   30207          76 :           tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
   30208          76 :           unsigned len = TREE_VEC_LENGTH (ftparms);
   30209          76 :           tree targs = make_tree_vec (len);
   30210          76 :           int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
   30211          76 :           if (err)
   30212             :             /* CWG2664: Discard any deductions, still build the guide.  */
   30213           8 :             for (unsigned i = 0; i < len; ++i)
   30214           4 :               TREE_VEC_ELT (targs, i) = NULL_TREE;
   30215             : 
   30216             :           /* The number of parms for f' is the number of parms of A used in
   30217             :              the deduced arguments plus non-deduced parms of f.  */
   30218             :           unsigned ndlen = 0;
   30219             :           unsigned j;
   30220         224 :           for (unsigned i = 0; i < len; ++i)
   30221         148 :             if (TREE_VEC_ELT (targs, i) == NULL_TREE)
   30222          24 :               ++ndlen;
   30223          76 :           find_template_parameter_info ftpi (fullatparms);
   30224          76 :           ftpi.find_in_recursive (targs);
   30225          76 :           unsigned nusedatparms = ftpi.num_found ();
   30226          76 :           unsigned nfparms = nusedatparms + ndlen;
   30227          76 :           tree gtparms = make_tree_vec (nfparms);
   30228             : 
   30229             :           /* Set current_template_parms as in build_deduction_guide.  */
   30230          76 :           auto ctp = make_temp_override (current_template_parms);
   30231          76 :           current_template_parms = copy_node (DECL_TEMPLATE_PARMS (tmpl));
   30232          76 :           TREE_VALUE (current_template_parms) = gtparms;
   30233             : 
   30234          76 :           j = 0;
   30235          76 :           unsigned level = 1;
   30236             : 
   30237             :           /* First copy over the used parms of A.  */
   30238          76 :           tree atargs = make_tree_vec (natparms);
   30239         164 :           for (unsigned i = 0; i < natparms; ++i)
   30240             :             {
   30241          88 :               tree elt = TREE_VEC_ELT (atparms, i);
   30242          88 :               if (ftpi.found (elt))
   30243             :                 {
   30244          77 :                   unsigned index = j++;
   30245          77 :                   tree nelt = rewrite_tparm_list (elt, index, level,
   30246             :                                                   atargs, i, complain);
   30247          77 :                   TREE_VEC_ELT (gtparms, index) = nelt;
   30248             :                 }
   30249             :             }
   30250          76 :           gcc_checking_assert (j == nusedatparms);
   30251             : 
   30252             :           /* Adjust the deduced template args for f to refer to the A parms
   30253             :              with their new indexes.  */
   30254          76 :           if (nusedatparms && nusedatparms != natparms)
   30255           3 :             targs = tsubst_template_args (targs, atargs, complain, in_decl);
   30256             : 
   30257             :           /* Now rewrite the non-deduced parms of f.  */
   30258         119 :           for (unsigned i = 0; ndlen && i < len; ++i)
   30259          43 :             if (TREE_VEC_ELT (targs, i) == NULL_TREE)
   30260             :               {
   30261          24 :                 --ndlen;
   30262          24 :                 unsigned index = j++;
   30263          24 :                 tree oldlist = TREE_VEC_ELT (ftparms, i);
   30264          24 :                 tree list = rewrite_tparm_list (oldlist, index, level,
   30265             :                                                 targs, i, complain);
   30266          24 :                 TREE_VEC_ELT (gtparms, index) = list;
   30267             :               }
   30268          76 :           gtparms = build_tree_list (size_one_node, gtparms);
   30269             : 
   30270             :           /* Substitute the deduced arguments plus the rewritten template
   30271             :              parameters into f to get g.  This covers the type, copyness,
   30272             :              guideness, and explicit-specifier.  */
   30273          76 :           tree g;
   30274          76 :             {
   30275             :               /* Parms are to have DECL_CHAIN tsubsted, which would be skipped
   30276             :                  if cp_unevaluated_operand.  */
   30277          76 :               cp_evaluated ev;
   30278          76 :               g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
   30279          76 :             }
   30280          76 :           if (g == error_mark_node)
   30281           0 :             continue;
   30282          76 :           if (nfparms == 0)
   30283             :             {
   30284             :               /* The targs are all non-dependent, so g isn't a template.  */
   30285           4 :               fprime = g;
   30286           4 :               ret = TREE_TYPE (TREE_TYPE (fprime));
   30287           4 :               goto non_template;
   30288             :             }
   30289          72 :           DECL_USE_TEMPLATE (g) = 0;
   30290          72 :           fprime = build_template_decl (g, gtparms, false);
   30291          72 :           DECL_TEMPLATE_RESULT (fprime) = g;
   30292          72 :           TREE_TYPE (fprime) = TREE_TYPE (g);
   30293          72 :           tree gtargs = template_parms_to_args (gtparms);
   30294          72 :           DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
   30295          72 :           DECL_PRIMARY_TEMPLATE (fprime) = fprime;
   30296             : 
   30297             :           /* Substitute the associated constraints.  */
   30298          72 :           tree ci = get_constraints (f);
   30299          72 :           if (ci)
   30300           2 :             ci = tsubst_constraint_info (ci, targs, complain, in_decl);
   30301          72 :           if (ci == error_mark_node)
   30302           0 :             continue;
   30303             : 
   30304             :           /* Add a constraint that the return type matches the instantiation of
   30305             :              A with the same template arguments.  */
   30306          72 :           ret = TREE_TYPE (TREE_TYPE (fprime));
   30307          72 :           if (!same_type_p (atype, ret)
   30308             :               /* FIXME this should mean they don't compare as equivalent.  */
   30309          72 :               || dependent_alias_template_spec_p (atype, nt_opaque))
   30310             :             {
   30311          13 :               tree same = finish_trait_expr (loc, CPTK_IS_DEDUCIBLE, tmpl, ret);
   30312          13 :               ci = append_constraint (ci, same);
   30313             :             }
   30314             : 
   30315          72 :           if (ci)
   30316             :             {
   30317          15 :               remove_constraints (fprime);
   30318          15 :               set_constraints (fprime, ci);
   30319             :             }
   30320         152 :         }
   30321             :       else
   30322             :         {
   30323             :           /* For a non-template deduction guide, if the arguments of A aren't
   30324             :              deducible from the return type, don't add the candidate.  */
   30325           1 :         non_template:
   30326           5 :           if (!type_targs_deducible_from (tmpl, ret))
   30327           1 :             continue;
   30328             :         }
   30329             : 
   30330          76 :       aguides = lookup_add (fprime, aguides);
   30331             :     }
   30332             : 
   30333          28 :   return aguides;
   30334             : }
   30335             : 
   30336             : /* True iff template arguments for TMPL can be deduced from TYPE.
   30337             :    Used to implement CPTK_IS_DEDUCIBLE for alias CTAD according to
   30338             :    [over.match.class.deduct].
   30339             : 
   30340             :    This check is specified in terms of partial specialization, so the behavior
   30341             :    should be parallel to that of get_partial_spec_bindings.  */
   30342             : 
   30343             : bool
   30344          23 : type_targs_deducible_from (tree tmpl, tree type)
   30345             : {
   30346          23 :   tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
   30347          23 :   int len = TREE_VEC_LENGTH (tparms);
   30348          23 :   tree targs = make_tree_vec (len);
   30349          23 :   bool tried_array_deduction = (cxx_dialect < cxx17);
   30350             : 
   30351             :   /* If tmpl is a class template, this is trivial: it's deducible if TYPE is a
   30352             :      specialization of TMPL.  */
   30353          23 :   if (DECL_CLASS_TEMPLATE_P (tmpl))
   30354           0 :     return (CLASS_TYPE_P (type)
   30355           0 :             && CLASSTYPE_TEMPLATE_INFO (type)
   30356           0 :             && CLASSTYPE_TI_TEMPLATE (type) == tmpl);
   30357             : 
   30358             :   /* Otherwise it's an alias template.  */
   30359          23 :  again:
   30360          23 :   if (unify (tparms, targs, TREE_TYPE (tmpl), type,
   30361             :              UNIFY_ALLOW_NONE, false))
   30362             :     return false;
   30363             : 
   30364             :   /* We don't fail on an undeduced targ the second time through (like
   30365             :      get_partial_spec_bindings) because we're going to try defaults.  */
   30366          33 :   for (int i =  0; i < len; ++i)
   30367          18 :     if (! TREE_VEC_ELT (targs, i))
   30368             :       {
   30369           6 :         tree tparm = TREE_VEC_ELT (tparms, i);
   30370           6 :         tparm = TREE_VALUE (tparm);
   30371             : 
   30372           6 :         if (!tried_array_deduction
   30373           6 :             && TREE_CODE (tparm) == TYPE_DECL)
   30374             :           {
   30375           4 :             try_array_deduction (tparms, targs, TREE_TYPE (tmpl));
   30376           4 :             tried_array_deduction = true;
   30377           4 :             if (TREE_VEC_ELT (targs, i))
   30378           0 :               goto again;
   30379             :           }
   30380             :         /* If the type parameter is a parameter pack, then it will be deduced
   30381             :            to an empty parameter pack.  This is another case that doesn't model
   30382             :            well as partial specialization.  */
   30383           6 :         if (template_parameter_pack_p (tparm))
   30384             :           {
   30385           2 :             tree arg;
   30386           2 :             if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
   30387             :               {
   30388           0 :                 arg = make_node (NONTYPE_ARGUMENT_PACK);
   30389           0 :                 TREE_CONSTANT (arg) = 1;
   30390             :               }
   30391             :             else
   30392           2 :               arg = cxx_make_type (TYPE_ARGUMENT_PACK);
   30393           2 :             ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
   30394           2 :             TREE_VEC_ELT (targs, i) = arg;
   30395             :           }
   30396             :       }
   30397             : 
   30398             :   /* Maybe add in default template args.  This seems like a flaw in the
   30399             :      specification in terms of partial specialization, since it says the
   30400             :      partial specialization has the the template parameter list of A, but a
   30401             :      partial specialization can't have default targs.  */
   30402          15 :   targs = coerce_template_parms (tparms, targs, tmpl, tf_none);
   30403          15 :   if (targs == error_mark_node)
   30404             :     return false;
   30405             : 
   30406             :   /* I believe we don't need the template_template_parm_bindings_ok_p call
   30407             :      because coerce_template_parms did coerce_template_template_parms.  */
   30408             : 
   30409          13 :   return constraints_satisfied_p (tmpl, targs);
   30410             : }
   30411             : 
   30412             : /* Return artificial deduction guides built from the constructors of class
   30413             :    template TMPL.  */
   30414             : 
   30415             : static tree
   30416        1170 : ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
   30417             : {
   30418        1170 :   tree outer_args = outer_template_args (tmpl);
   30419        1170 :   tree type = TREE_TYPE (most_general_template (tmpl));
   30420             : 
   30421        1170 :   tree cands = NULL_TREE;
   30422             : 
   30423        9056 :   for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
   30424             :     {
   30425             :       /* Skip inherited constructors.  */
   30426        3943 :       if (iter.using_p ())
   30427          10 :         continue;
   30428             : 
   30429        3933 :       tree guide = build_deduction_guide (type, *iter, outer_args, complain);
   30430        3933 :       cands = lookup_add (guide, cands);
   30431             :     }
   30432             : 
   30433             :   /* Add implicit default constructor deduction guide.  */
   30434        1170 :   if (!TYPE_HAS_USER_CONSTRUCTOR (type))
   30435             :     {
   30436         223 :       tree guide = build_deduction_guide (type, type, outer_args,
   30437             :                                           complain);
   30438         223 :       cands = lookup_add (guide, cands);
   30439             :     }
   30440             : 
   30441             :   /* Add copy guide.  */
   30442        1170 :   {
   30443        1170 :     tree gtype = build_reference_type (type);
   30444        1170 :     tree guide = build_deduction_guide (type, gtype, outer_args,
   30445             :                                         complain);
   30446        1170 :     cands = lookup_add (guide, cands);
   30447             :   }
   30448             : 
   30449        1170 :   return cands;
   30450             : }
   30451             : 
   30452             : static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
   30453             : 
   30454             : /* Return the non-aggregate deduction guides for deducible template TMPL.  The
   30455             :    aggregate candidate is added separately because it depends on the
   30456             :    initializer.  Set ANY_DGUIDES_P if we find a non-implicit deduction
   30457             :    guide.  */
   30458             : 
   30459             : static tree
   30460        3426 : deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
   30461             : {
   30462        3426 :   tree guides = NULL_TREE;
   30463        3426 :   if (DECL_ALIAS_TEMPLATE_P (tmpl))
   30464             :     {
   30465          44 :       tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   30466          44 :       tree tinfo = get_template_info (under);
   30467          44 :       guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
   30468             :                                      complain);
   30469             :     }
   30470             :   else
   30471             :     {
   30472        3382 :       guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
   30473             :                                       dguide_name (tmpl),
   30474             :                                       LOOK_want::NORMAL, /*complain*/false);
   30475        3382 :       if (guides == error_mark_node)
   30476             :         guides = NULL_TREE;
   30477             :       else
   30478        2485 :         any_dguides_p = true;
   30479             :     }
   30480             : 
   30481             :   /* Cache the deduction guides for a template.  We also remember the result of
   30482             :      lookup, and rebuild everything if it changes; should be very rare.  */
   30483        3426 :   tree_pair_p cache = NULL;
   30484        6852 :   if (tree_pair_p &r
   30485        3426 :       = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
   30486             :     {
   30487        2241 :       cache = r;
   30488        2241 :       if (cache->purpose == guides)
   30489        2229 :         return cache->value;
   30490             :     }
   30491             :   else
   30492             :     {
   30493        1185 :       r = cache = ggc_cleared_alloc<tree_pair_s> ();
   30494        1185 :       cache->purpose = guides;
   30495             :     }
   30496             : 
   30497        1197 :   tree cands = NULL_TREE;
   30498        1197 :   if (DECL_ALIAS_TEMPLATE_P (tmpl))
   30499          27 :     cands = alias_ctad_tweaks (tmpl, guides);
   30500             :   else
   30501             :     {
   30502        1170 :       cands = ctor_deduction_guides_for (tmpl, complain);
   30503        2725 :       for (ovl_iterator it (guides); it; ++it)
   30504        1555 :         cands = lookup_add (*it, cands);
   30505             :     }
   30506             : 
   30507        1197 :   cache->value = cands;
   30508        1197 :   return cands;
   30509             : }
   30510             : 
   30511             : /* Return whether TMPL is a (class template argument-) deducible template.  */
   30512             : 
   30513             : bool
   30514   474240446 : ctad_template_p (tree tmpl)
   30515             : {
   30516             :   /* A deducible template is either a class template or is an alias template
   30517             :      whose defining-type-id is of the form
   30518             : 
   30519             :       typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
   30520             : 
   30521             :      where the nested-name-specifier (if any) is non-dependent and the
   30522             :      template-name of the simple-template-id names a deducible template.  */
   30523             : 
   30524     5638814 :   if (DECL_CLASS_TEMPLATE_P (tmpl)
   30525   474680127 :       || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
   30526             :     return true;
   30527   468941267 :   if (!DECL_ALIAS_TEMPLATE_P (tmpl))
   30528             :     return false;
   30529       95127 :   tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   30530       95127 :   if (tree tinfo = get_template_info (orig))
   30531       18326 :     return ctad_template_p (TI_TEMPLATE (tinfo));
   30532             :   return false;
   30533             : }
   30534             : 
   30535             : /* Deduce template arguments for the class template placeholder PTYPE for
   30536             :    template TMPL based on the initializer INIT, and return the resulting
   30537             :    type.  */
   30538             : 
   30539             : static tree
   30540        3508 : do_class_deduction (tree ptype, tree tmpl, tree init,
   30541             :                     int flags, tsubst_flags_t complain)
   30542             : {
   30543             :   /* We should have handled this in the caller.  */
   30544        3508 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
   30545             :     return ptype;
   30546             : 
   30547             :   /* If the class was erroneous, don't try to deduce, because that
   30548             :      can generate a lot of diagnostic.  */
   30549        3460 :   if (TREE_TYPE (tmpl)
   30550        3460 :       && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl))
   30551        6918 :       && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl)))
   30552             :     return ptype;
   30553             : 
   30554             :   /* Wait until the enclosing scope is non-dependent.  */
   30555        6912 :   if (DECL_CLASS_SCOPE_P (tmpl)
   30556        3533 :       && dependent_type_p (DECL_CONTEXT (tmpl)))
   30557             :     return ptype;
   30558             : 
   30559             :   /* Initializing one placeholder from another.  */
   30560        3447 :   if (init
   30561        3362 :       && (TREE_CODE (init) == TEMPLATE_PARM_INDEX
   30562        3322 :           || (TREE_CODE (init) == EXPR_PACK_EXPANSION
   30563           1 :               && (TREE_CODE (PACK_EXPANSION_PATTERN (init))
   30564             :                   == TEMPLATE_PARM_INDEX)))
   30565          41 :       && is_auto (TREE_TYPE (init))
   30566        3488 :       && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
   30567          15 :     return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
   30568             : 
   30569        3432 :   if (!ctad_template_p (tmpl))
   30570             :     {
   30571           2 :       if (complain & tf_error)
   30572           2 :         error ("non-deducible template %qT used without template arguments", tmpl);
   30573           2 :       return error_mark_node;
   30574             :     }
   30575        3430 :   else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
   30576             :     {
   30577           7 :       if (complain & tf_error)
   30578             :         {
   30579             :           /* Be permissive with equivalent alias templates.  */
   30580           7 :           tree u = get_underlying_template (tmpl);
   30581           7 :           diagnostic_t dk = (u == tmpl) ? DK_ERROR : DK_PEDWARN;
   30582           7 :           bool complained
   30583           7 :             = emit_diagnostic (dk, input_location, 0,
   30584             :                                "alias template deduction only available "
   30585             :                                "with %<-std=c++20%> or %<-std=gnu++20%>");
   30586           7 :           if (u == tmpl)
   30587           6 :             return error_mark_node;
   30588           1 :           else if (complained)
   30589             :             {
   30590           1 :               inform (input_location, "use %qD directly instead", u);
   30591           1 :               tmpl = u;
   30592             :             }
   30593             :         }
   30594             :       else
   30595           0 :         return error_mark_node;
   30596             :     }
   30597             : 
   30598             :   /* Wait until the initializer is non-dependent.  */
   30599        3424 :   if (type_dependent_expression_p (init))
   30600             :     return ptype;
   30601             : 
   30602             :   /* Don't bother with the alias rules for an equivalent template.  */
   30603        3388 :   tmpl = get_underlying_template (tmpl);
   30604             : 
   30605        3388 :   tree type = TREE_TYPE (tmpl);
   30606             : 
   30607        3388 :   bool try_list_cand = false;
   30608        3388 :   bool list_init_p = false;
   30609             : 
   30610        3388 :   releasing_vec rv_args = NULL;
   30611        3388 :   vec<tree,va_gc> *&args = *&rv_args;
   30612        3388 :   if (init == NULL_TREE)
   30613          84 :     args = make_tree_vector ();
   30614        3304 :   else if (BRACE_ENCLOSED_INITIALIZER_P (init))
   30615             :     {
   30616        1476 :       list_init_p = true;
   30617        1476 :       try_list_cand = true;
   30618        1476 :       if (CONSTRUCTOR_NELTS (init) == 1
   30619        1411 :           && !CONSTRUCTOR_IS_DESIGNATED_INIT (init))
   30620             :         {
   30621             :           /* As an exception, the first phase in 16.3.1.7 (considering the
   30622             :              initializer list as a single argument) is omitted if the
   30623             :              initializer list consists of a single expression of type cv U,
   30624             :              where U is a specialization of C or a class derived from a
   30625             :              specialization of C.  */
   30626         803 :           tree elt = CONSTRUCTOR_ELT (init, 0)->value;
   30627         803 :           if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
   30628          24 :             try_list_cand = false;
   30629             :         }
   30630          24 :       if (try_list_cand || is_std_init_list (type))
   30631        1452 :         args = make_tree_vector_single (init);
   30632             :       else
   30633          24 :         args = make_tree_vector_from_ctor (init);
   30634             :     }
   30635        1828 :   else if (TREE_CODE (init) == TREE_LIST)
   30636        1352 :     args = make_tree_vector_from_list (init);
   30637             :   else
   30638         476 :     args = make_tree_vector_single (init);
   30639             : 
   30640             :   /* Do this now to avoid problems with erroneous args later on.  */
   30641        3388 :   args = resolve_args (args, complain);
   30642        3388 :   if (args == NULL)
   30643           6 :     return error_mark_node;
   30644             : 
   30645        3382 :   bool any_dguides_p = false;
   30646        3382 :   tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
   30647        3382 :   if (cands == error_mark_node)
   30648             :     return error_mark_node;
   30649             : 
   30650             :   /* Prune explicit deduction guides in copy-initialization context (but
   30651             :      not copy-list-initialization).  */
   30652        3380 :   bool elided = false;
   30653        3380 :   if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
   30654             :     {
   30655        5092 :       for (lkp_iterator iter (cands); !elided && iter; ++iter)
   30656        4156 :         if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
   30657         105 :           elided = true;
   30658             : 
   30659         468 :       if (elided)
   30660             :         {
   30661             :           /* Found a nonconverting guide, prune the candidates.  */
   30662         105 :           tree pruned = NULL_TREE;
   30663        1729 :           for (lkp_iterator iter (cands); iter; ++iter)
   30664        1519 :             if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
   30665        1168 :               pruned = lookup_add (*iter, pruned);
   30666             : 
   30667         105 :           cands = pruned;
   30668             :         }
   30669             :     }
   30670             : 
   30671        3380 :   if (!any_dguides_p)
   30672         895 :     if (tree guide = maybe_aggr_guide (tmpl, init, args))
   30673          95 :       cands = lookup_add (guide, cands);
   30674             : 
   30675        3380 :   tree fndecl = error_mark_node;
   30676             : 
   30677             :   /* If this is list-initialization and the class has a list guide, first
   30678             :      try deducing from the list as a single argument, as [over.match.list].  */
   30679        3380 :   if (try_list_cand)
   30680             :     {
   30681        1452 :       tree list_cands = NULL_TREE;
   30682       15562 :       for (tree dg : lkp_range (cands))
   30683       12658 :         if (is_list_ctor (dg))
   30684         662 :           list_cands = lookup_add (dg, list_cands);
   30685        1452 :       if (list_cands)
   30686         344 :         fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
   30687        1452 :       if (fndecl == error_mark_node)
   30688             :         {
   30689             :           /* That didn't work, now try treating the list as a sequence of
   30690             :              arguments.  */
   30691        1405 :           release_tree_vector (args);
   30692        1405 :           args = make_tree_vector_from_ctor (init);
   30693        1405 :           args = resolve_args (args, complain);
   30694        1405 :           if (args == NULL)
   30695           4 :             return error_mark_node;
   30696             :         }
   30697             :     }
   30698             : 
   30699        3376 :   if (elided && !cands)
   30700             :     {
   30701           0 :       error ("cannot deduce template arguments for copy-initialization"
   30702             :              " of %qT, as it has no non-explicit deduction guides or "
   30703             :              "user-declared constructors", type);
   30704           0 :       return error_mark_node;
   30705             :     }
   30706        3376 :   else if (!cands && fndecl == error_mark_node)
   30707             :     {
   30708           0 :       error ("cannot deduce template arguments of %qT, as it has no viable "
   30709             :              "deduction guides", type);
   30710           0 :       return error_mark_node;
   30711             :     }
   30712             : 
   30713        3376 :   if (fndecl == error_mark_node)
   30714        3329 :     fndecl = perform_dguide_overload_resolution (cands, args, tf_none);
   30715             : 
   30716        3376 :   if (fndecl == error_mark_node)
   30717             :     {
   30718         165 :       if (complain & tf_warning_or_error)
   30719             :         {
   30720         104 :           error ("class template argument deduction failed:");
   30721         104 :           perform_dguide_overload_resolution (cands, args, complain);
   30722         104 :           if (elided)
   30723          17 :             inform (input_location, "explicit deduction guides not considered "
   30724             :                     "for copy-initialization");
   30725             :         }
   30726         165 :       return error_mark_node;
   30727             :     }
   30728             :   /* [over.match.list]/1: In copy-list-initialization, if an explicit
   30729             :      constructor is chosen, the initialization is ill-formed.  */
   30730        3211 :   else if (flags & LOOKUP_ONLYCONVERTING)
   30731             :     {
   30732         535 :       if (DECL_NONCONVERTING_P (fndecl))
   30733             :         {
   30734          23 :           if (complain & tf_warning_or_error)
   30735             :             {
   30736             :               // TODO: Pass down location from cp_finish_decl.
   30737          23 :               error ("class template argument deduction for %qT failed: "
   30738             :                      "explicit deduction guide selected in "
   30739             :                      "copy-list-initialization", type);
   30740          23 :               inform (DECL_SOURCE_LOCATION (fndecl),
   30741             :                       "explicit deduction guide declared here");
   30742             : 
   30743             :             }
   30744          23 :           return error_mark_node;
   30745             :         }
   30746             :     }
   30747             : 
   30748             :   /* If CTAD succeeded but the type doesn't have any explicit deduction
   30749             :      guides, this deduction might not be what the user intended.  */
   30750        3188 :   if (fndecl != error_mark_node && !any_dguides_p && (complain & tf_warning))
   30751             :     {
   30752         761 :       if ((!DECL_IN_SYSTEM_HEADER (fndecl)
   30753         372 :            || global_dc->dc_warn_system_headers)
   30754         763 :           && warning (OPT_Wctad_maybe_unsupported,
   30755             :                       "%qT may not intend to support class template argument "
   30756             :                       "deduction", type))
   30757          16 :         inform (input_location, "add a deduction guide to suppress this "
   30758             :                 "warning");
   30759             :     }
   30760             : 
   30761        3188 :   return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl)),
   30762        3188 :                                   cp_type_quals (ptype));
   30763        3508 : }
   30764             : 
   30765             : /* Return true if INIT is an unparenthesized id-expression or an
   30766             :    unparenthesized class member access.  Used for the argument of
   30767             :    decltype(auto).  */
   30768             : 
   30769             : bool
   30770      211292 : unparenthesized_id_or_class_member_access_p (tree init)
   30771             : {
   30772      211292 :   STRIP_ANY_LOCATION_WRAPPER (init);
   30773             : 
   30774             :   /* We need to be able to tell '(r)' and 'r' apart (when it's of
   30775             :      reference type).  Only the latter is an id-expression.  */
   30776       32812 :   if (REFERENCE_REF_P (init)
   30777      234437 :       && !REF_PARENTHESIZED_P (init))
   30778        1235 :     init = TREE_OPERAND (init, 0);
   30779      211292 :   return (DECL_P (init)
   30780      211292 :           || ((TREE_CODE (init) == COMPONENT_REF
   30781      211117 :                || TREE_CODE (init) == SCOPE_REF)
   30782         218 :               && !REF_PARENTHESIZED_P (init)));
   30783             : }
   30784             : 
   30785             : /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
   30786             :    from INIT.  AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
   30787             :    The CONTEXT determines the context in which auto deduction is performed
   30788             :    and is used to control error diagnostics.  FLAGS are the LOOKUP_* flags.
   30789             : 
   30790             :    OUTER_TARGS is used during template argument deduction (context == adc_unify)
   30791             :    to properly substitute the result.  It's also used in the adc_unify and
   30792             :    adc_requirement contexts to communicate the necessary template arguments
   30793             :    to satisfaction.  OUTER_TARGS is ignored in other contexts.
   30794             : 
   30795             :    Additionally for adc_unify contexts TMPL is the template for which TYPE
   30796             :    is a template parameter type.
   30797             : 
   30798             :    For partial-concept-ids, extra args from OUTER_TARGS, TMPL and the current
   30799             :    scope may be appended to the list of deduced template arguments prior to
   30800             :    determining constraint satisfaction as appropriate.  */
   30801             : 
   30802             : tree
   30803     4208486 : do_auto_deduction (tree type, tree init, tree auto_node,
   30804             :                    tsubst_flags_t complain /* = tf_warning_or_error */,
   30805             :                    auto_deduction_context context /* = adc_unspecified */,
   30806             :                    tree outer_targs /* = NULL_TREE */,
   30807             :                    int flags /* = LOOKUP_NORMAL */,
   30808             :                    tree tmpl /* = NULL_TREE */)
   30809             : {
   30810     4208486 :   if (type == error_mark_node || init == error_mark_node)
   30811             :     return error_mark_node;
   30812             : 
   30813     4207918 :   if (init && type_dependent_expression_p (init)
   30814     7116196 :       && context != adc_unify)
   30815             :     /* Defining a subset of type-dependent expressions that we can deduce
   30816             :        from ahead of time isn't worth the trouble.  */
   30817             :     return type;
   30818             : 
   30819             :   /* Similarly, we can't deduce from another undeduced decl.  */
   30820     1300187 :   if (init && undeduced_auto_decl (init))
   30821             :     return type;
   30822             : 
   30823             :   /* We may be doing a partial substitution, but we still want to replace
   30824             :      auto_node.  */
   30825     1300180 :   complain &= ~tf_partial;
   30826             : 
   30827     1300180 :   if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
   30828             :     {
   30829             :       /* We don't recurse here because we can't deduce from a nested
   30830             :          initializer_list.  */
   30831        1935 :       if (CONSTRUCTOR_ELTS (init))
   30832        5502 :         for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
   30833        3655 :           elt.value = resolve_nondeduced_context (elt.value, complain);
   30834             :     }
   30835     1298245 :   else if (init)
   30836     1298135 :     init = resolve_nondeduced_context (init, complain);
   30837             : 
   30838             :   /* In C++23, we must deduce the type to int&& for code like
   30839             :        decltype(auto) f(int&& x) { return (x); }
   30840             :      or
   30841             :        auto&& f(int x) { return x; }
   30842             :      so we use treat_lvalue_as_rvalue_p.  But don't do it for
   30843             :        decltype(auto) f(int x) { return x; }
   30844             :      where we should deduce 'int' rather than 'int&&'; transmogrifying
   30845             :      INIT to an rvalue would break that.  */
   30846     1300180 :   tree r;
   30847     1300180 :   if (cxx_dialect >= cxx23
   30848       39239 :       && context == adc_return_type
   30849        6308 :       && (!AUTO_IS_DECLTYPE (auto_node)
   30850         988 :           || !unparenthesized_id_or_class_member_access_p (init))
   30851     1306464 :       && (r = treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init),
   30852             :                                         /*return*/true)))
   30853          74 :     init = r;
   30854             : 
   30855     1300180 :   if (tree ctmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
   30856             :     /* C++17 class template argument deduction.  */
   30857        3508 :     return do_class_deduction (type, ctmpl, init, flags, complain);
   30858             : 
   30859     1296672 :   if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
   30860             :     /* Nothing we can do with this, even in deduction context.  */
   30861             :     return type;
   30862             : 
   30863     1296551 :   location_t loc = cp_expr_loc_or_input_loc (init);
   30864             : 
   30865             :   /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
   30866             :      with either a new invented type template parameter U or, if the
   30867             :      initializer is a braced-init-list (8.5.4), with
   30868             :      std::initializer_list<U>.  */
   30869     1296551 :   if (BRACE_ENCLOSED_INITIALIZER_P (init))
   30870             :     {
   30871         427 :       if (!DIRECT_LIST_INIT_P (init))
   30872         288 :         type = listify_autos (type, auto_node);
   30873         139 :       else if (CONSTRUCTOR_NELTS (init) == 1)
   30874         133 :         init = CONSTRUCTOR_ELT (init, 0)->value;
   30875             :       else
   30876             :         {
   30877           6 :           if (complain & tf_warning_or_error)
   30878             :             {
   30879           6 :               if (permerror (loc, "direct-list-initialization of "
   30880             :                              "%<auto%> requires exactly one element"))
   30881           6 :                 inform (loc,
   30882             :                         "for deduction to %<std::initializer_list%>, use copy-"
   30883             :                         "list-initialization (i.e. add %<=%> before the %<{%>)");
   30884             :             }
   30885           6 :           type = listify_autos (type, auto_node);
   30886             :         }
   30887             :     }
   30888             : 
   30889     1296551 :   if (type == error_mark_node || init == error_mark_node)
   30890             :     return error_mark_node;
   30891             : 
   30892     1296536 :   tree targs;
   30893     1296536 :   if (context == adc_decomp_type
   30894     1296536 :       && auto_node == type
   30895     1296536 :       && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
   30896             :     {
   30897             :       /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
   30898             :          and initializer has array type, deduce cv-qualified array type.  */
   30899          88 :       targs = make_tree_vec (1);
   30900          88 :       TREE_VEC_ELT (targs, 0) = TREE_TYPE (init);
   30901             :     }
   30902     1296448 :   else if (AUTO_IS_DECLTYPE (auto_node))
   30903             :     {
   30904      210304 :       const bool id = unparenthesized_id_or_class_member_access_p (init);
   30905      210304 :       tree deduced = finish_decltype_type (init, id, complain);
   30906      210304 :       deduced = canonicalize_type_argument (deduced, complain);
   30907      210304 :       if (deduced == error_mark_node)
   30908             :         return error_mark_node;
   30909      210302 :       targs = make_tree_vec (1);
   30910      210302 :       TREE_VEC_ELT (targs, 0) = deduced;
   30911             :     }
   30912             :   else
   30913             :     {
   30914     1086144 :       if (error_operand_p (init))
   30915             :         return error_mark_node;
   30916             : 
   30917     1086134 :       tree parms = build_tree_list (NULL_TREE, type);
   30918     1086134 :       tree tparms;
   30919             : 
   30920     1086134 :       if (flag_concepts_ts)
   30921         192 :         tparms = extract_autos (type);
   30922             :       else
   30923             :         {
   30924     1085942 :           tparms = make_tree_vec (1);
   30925     1085942 :           TREE_VEC_ELT (tparms, 0)
   30926     2171884 :             = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
   30927             :         }
   30928             : 
   30929     1086134 :       targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
   30930     1086134 :       int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
   30931             :                                        DEDUCE_CALL,
   30932             :                                        NULL, /*explain_p=*/false);
   30933     1086134 :       if (val > 0)
   30934             :         {
   30935          66 :           if (processing_template_decl)
   30936             :             /* Try again at instantiation time.  */
   30937             :             return type;
   30938          59 :           if (type && type != error_mark_node
   30939          59 :               && (complain & tf_error))
   30940             :             /* If type is error_mark_node a diagnostic must have been
   30941             :                emitted by now.  Also, having a mention to '<type error>'
   30942             :                in the diagnostic is not really useful to the user.  */
   30943             :             {
   30944          57 :               if (cfun
   30945          28 :                   && FNDECL_USED_AUTO (current_function_decl)
   30946          12 :                   && (auto_node
   30947          12 :                       == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
   30948          69 :                   && LAMBDA_FUNCTION_P (current_function_decl))
   30949           6 :                 error_at (loc, "unable to deduce lambda return type from %qE",
   30950             :                           init);
   30951             :               else
   30952          51 :                 error_at (loc, "unable to deduce %qT from %qE", type, init);
   30953          57 :               type_unification_real (tparms, targs, parms, &init, 1, 0,
   30954             :                                      DEDUCE_CALL,
   30955             :                                      NULL, /*explain_p=*/true);
   30956             :             }
   30957          59 :           return error_mark_node;
   30958             :         }
   30959             :     }
   30960             : 
   30961             :   /* Check any placeholder constraints against the deduced type. */
   30962     1296458 :   if (processing_template_decl && context == adc_unify)
   30963             :     /* Constraints will be checked after deduction.  */;
   30964     1296272 :   else if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
   30965             :     {
   30966      199412 :       if (processing_template_decl)
   30967             :         {
   30968          20 :           gcc_checking_assert (context == adc_variable_type
   30969             :                                || context == adc_return_type
   30970             :                                || context == adc_decomp_type);
   30971          20 :           gcc_checking_assert (!type_dependent_expression_p (init));
   30972             :           /* If the constraint is dependent, we need to wait until
   30973             :              instantiation time to resolve the placeholder.  */
   30974          20 :           if (placeholder_type_constraint_dependent_p (constr))
   30975             :             return type;
   30976             :         }
   30977             : 
   30978      199404 :       if (context == adc_return_type
   30979      199404 :           || context == adc_variable_type
   30980      199404 :           || context == adc_decomp_type)
   30981         161 :         if (tree fn = current_function_decl)
   30982         125 :           if (DECL_TEMPLATE_INFO (fn) || LAMBDA_FUNCTION_P (fn))
   30983             :             {
   30984          42 :               outer_targs = DECL_TEMPLATE_INFO (fn)
   30985          42 :                 ? DECL_TI_ARGS (fn) : NULL_TREE;
   30986          46 :               if (LAMBDA_FUNCTION_P (fn))
   30987             :                 {
   30988             :                   /* As in satisfy_declaration_constraints.  */
   30989           4 :                   tree regen_args = lambda_regenerating_args (fn);
   30990           4 :                   if (outer_targs)
   30991           0 :                     outer_targs = add_to_template_args (regen_args, outer_targs);
   30992             :                   else
   30993             :                     outer_targs = regen_args;
   30994             :                 }
   30995             :             }
   30996             : 
   30997      199404 :       tree full_targs = outer_targs;
   30998      199404 :       if (context == adc_unify && tmpl)
   30999          64 :         full_targs = add_outermost_template_args (tmpl, full_targs);
   31000      199404 :       full_targs = add_to_template_args (full_targs, targs);
   31001             : 
   31002             :       /* HACK: Compensate for callers not always communicating all levels of
   31003             :          outer template arguments by filling in the outermost missing levels
   31004             :          with dummy levels before checking satisfaction.  We'll still crash
   31005             :          if the constraint depends on a template argument belonging to one of
   31006             :          these missing levels, but this hack otherwise allows us to handle a
   31007             :          large subset of possible constraints (including all non-dependent
   31008             :          constraints).  */
   31009      199404 :       if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node)
   31010      398927 :                                 - TMPL_ARGS_DEPTH (full_targs)))
   31011             :         {
   31012           4 :           tree dummy_levels = make_tree_vec (missing_levels);
   31013           8 :           for (int i = 0; i < missing_levels; ++i)
   31014           4 :             TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0);
   31015           4 :           full_targs = add_to_template_args (dummy_levels, full_targs);
   31016             :         }
   31017             : 
   31018      199404 :       if (!constraints_satisfied_p (auto_node, full_targs))
   31019             :         {
   31020         301 :           if (complain & tf_warning_or_error)
   31021             :             {
   31022          56 :               auto_diagnostic_group d;
   31023          56 :               switch (context)
   31024             :                 {
   31025           9 :                 case adc_unspecified:
   31026           9 :                 case adc_unify:
   31027           9 :                   error_at (loc, "placeholder constraints not satisfied");
   31028           9 :                   break;
   31029          26 :                 case adc_variable_type:
   31030          26 :                 case adc_decomp_type:
   31031          26 :                   error_at (loc, "deduced initializer does not satisfy "
   31032             :                             "placeholder constraints");
   31033          26 :                   break;
   31034          17 :                 case adc_return_type:
   31035          17 :                   error_at (loc, "deduced return type does not satisfy "
   31036             :                             "placeholder constraints");
   31037          17 :                   break;
   31038           4 :                 case adc_requirement:
   31039           4 :                   error_at (loc, "deduced expression type does not satisfy "
   31040             :                             "placeholder constraints");
   31041           4 :                   break;
   31042             :                 }
   31043          56 :               diagnose_constraints (loc, auto_node, full_targs);
   31044          56 :             }
   31045         301 :           return error_mark_node;
   31046             :         }
   31047             :     }
   31048             : 
   31049     1296149 :   if (TEMPLATE_TYPE_LEVEL (auto_node) == 1)
   31050             :     /* The outer template arguments are already substituted into type
   31051             :        (but we still may have used them for constraint checking above).  */;
   31052      302657 :   else if (context == adc_unify)
   31053        1336 :     targs = add_to_template_args (outer_targs, targs);
   31054      301321 :   else if (processing_template_decl)
   31055      301321 :     targs = add_to_template_args (current_template_args (), targs);
   31056     1296149 :   return tsubst (type, targs, complain, NULL_TREE);
   31057             : }
   31058             : 
   31059             : /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
   31060             :    result.  */
   31061             : 
   31062             : tree
   31063   116383211 : splice_late_return_type (tree type, tree late_return_type)
   31064             : {
   31065   116383211 :   if (late_return_type)
   31066             :     {
   31067     1844736 :       gcc_assert (is_auto (type) || seen_error ());
   31068     1844736 :       return late_return_type;
   31069             :     }
   31070             : 
   31071   114538475 :   if (tree auto_node = find_type_usage (type, is_auto))
   31072      617295 :     if (TEMPLATE_TYPE_LEVEL (auto_node) <= current_template_depth)
   31073             :       {
   31074             :         /* In an abbreviated function template we didn't know we were dealing
   31075             :            with a function template when we saw the auto return type, so rebuild
   31076             :            the return type using an auto with the correct level.  */
   31077          32 :         tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), false);
   31078          32 :         tree auto_vec = make_tree_vec (1);
   31079          32 :         TREE_VEC_ELT (auto_vec, 0) = new_auto;
   31080          32 :         tree targs = add_outermost_template_args (current_template_args (),
   31081             :                                                   auto_vec);
   31082             :         /* Also rebuild the constraint info in terms of the new auto.  */
   31083          32 :         if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node))
   31084           4 :           PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto)
   31085           4 :             = build_tree_list (current_template_parms,
   31086           2 :                                tsubst_constraint (TREE_VALUE (ci), targs,
   31087             :                                                   tf_none, NULL_TREE));
   31088          32 :         TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
   31089          32 :         return tsubst (type, targs, tf_none, NULL_TREE);
   31090             :       }
   31091             :   return type;
   31092             : }
   31093             : 
   31094             : /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
   31095             :    'decltype(auto)' or a deduced class template.  */
   31096             : 
   31097             : bool
   31098  6914941861 : is_auto (const_tree type)
   31099             : {
   31100  6914941861 :   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
   31101  6914941861 :       && (TYPE_IDENTIFIER (type) == auto_identifier
   31102  4150911082 :           || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
   31103             :     return true;
   31104             :   else
   31105  6606788424 :     return false;
   31106             : }
   31107             : 
   31108             : /* for_each_template_parm callback for type_uses_auto.  */
   31109             : 
   31110             : int
   31111       70586 : is_auto_r (tree tp, void */*data*/)
   31112             : {
   31113       70586 :   return is_auto (tp);
   31114             : }
   31115             : 
   31116             : /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
   31117             :    a use of `auto'.  Returns NULL_TREE otherwise.  */
   31118             : 
   31119             : tree
   31120  1634747075 : type_uses_auto (tree type)
   31121             : {
   31122  1634747075 :   if (type == NULL_TREE)
   31123             :     return NULL_TREE;
   31124  1634074440 :   else if (flag_concepts_ts)
   31125             :     {
   31126             :       /* The Concepts TS allows multiple autos in one type-specifier; just
   31127             :          return the first one we find, do_auto_deduction will collect all of
   31128             :          them.  */
   31129      176640 :       if (uses_template_parms (type))
   31130       66041 :         return for_each_template_parm (type, is_auto_r, /*data*/NULL,
   31131       66041 :                                        /*visited*/NULL, /*nondeduced*/false);
   31132             :       else
   31133             :         return NULL_TREE;
   31134             :     }
   31135             :   else
   31136  1633897800 :     return find_type_usage (type, is_auto);
   31137             : }
   31138             : 
   31139             : /* Report ill-formed occurrences of auto types in ARGUMENTS.  If
   31140             :    concepts are enabled, auto is acceptable in template arguments, but
   31141             :    only when TEMPL identifies a template class.  Return TRUE if any
   31142             :    such errors were reported.  */
   31143             : 
   31144             : bool
   31145     8536139 : check_auto_in_tmpl_args (tree tmpl, tree args)
   31146             : {
   31147     8536139 :   if (!flag_concepts_ts)
   31148             :     /* Only the concepts TS allows 'auto' as a type-id; it'd otherwise
   31149             :        have already been rejected by the parser more generally.  */
   31150             :     return false;
   31151             : 
   31152             :   /* If there were previous errors, nevermind.  */
   31153       25341 :   if (!args || TREE_CODE (args) != TREE_VEC)
   31154             :     return false;
   31155             : 
   31156             :   /* If TMPL is an identifier, we're parsing and we can't tell yet
   31157             :      whether TMPL is supposed to be a type, a function or a variable.
   31158             :      We'll only be able to tell during template substitution, so we
   31159             :      expect to be called again then.  If concepts are enabled and we
   31160             :      know we have a type, we're ok.  */
   31161     8561398 :   if (identifier_p (tmpl)
   31162       25259 :       || (DECL_P (tmpl)
   31163       22162 :           &&  (DECL_TYPE_TEMPLATE_P (tmpl)
   31164        3249 :                || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))))
   31165             :     return false;
   31166             : 
   31167             :   /* Quickly search for any occurrences of auto; usually there won't
   31168             :      be any, and then we'll avoid allocating the vector.  */
   31169        6346 :   if (!type_uses_auto (args))
   31170             :     return false;
   31171             : 
   31172           0 :   bool errors = false;
   31173             : 
   31174           0 :   tree vec = extract_autos (args);
   31175           0 :   for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
   31176             :     {
   31177           0 :       tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
   31178           0 :       error_at (DECL_SOURCE_LOCATION (xauto),
   31179             :                 "invalid use of %qT in template argument", xauto);
   31180           0 :       errors = true;
   31181             :     }
   31182             : 
   31183             :   return errors;
   31184             : }
   31185             : 
   31186             : /* Recursively walk over && expressions searching for EXPR. Return a reference
   31187             :    to that expression.  */
   31188             : 
   31189          16 : static tree *find_template_requirement (tree *t, tree key)
   31190             : {
   31191          16 :   if (*t == key)
   31192             :     return t;
   31193           8 :   if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
   31194             :     {
   31195           4 :       if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
   31196             :         return p;
   31197           4 :       if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
   31198             :         return p;
   31199             :     }
   31200             :   return 0;
   31201             : }
   31202             : 
   31203             : /* Convert the generic type parameters in PARM that match the types given in the
   31204             :    range [START_IDX, END_IDX) from the current_template_parms into generic type
   31205             :    packs.  */
   31206             : 
   31207             : tree
   31208       10795 : convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
   31209             : {
   31210       10795 :   tree current = current_template_parms;
   31211       10795 :   int depth = TMPL_PARMS_DEPTH (current);
   31212       10795 :   current = INNERMOST_TEMPLATE_PARMS (current);
   31213       10795 :   tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
   31214             : 
   31215       11694 :   for (int i = 0; i < start_idx; ++i)
   31216        1798 :     TREE_VEC_ELT (replacement, i)
   31217         899 :       = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
   31218             : 
   31219       21596 :   for (int i = start_idx; i < end_idx; ++i)
   31220             :     {
   31221             :       /* Create a distinct parameter pack type from the current parm and add it
   31222             :          to the replacement args to tsubst below into the generic function
   31223             :          parameter.  */
   31224       10801 :       tree node = TREE_VEC_ELT (current, i);
   31225       10801 :       tree o = TREE_TYPE (TREE_VALUE (node));
   31226       10801 :       tree t = copy_type (o);
   31227       10801 :       TEMPLATE_TYPE_PARM_INDEX (t)
   31228       10801 :         = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
   31229             :                                       t, 0, 0, tf_none);
   31230       10801 :       TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
   31231       10801 :       TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
   31232       10801 :       TYPE_MAIN_VARIANT (t) = t;
   31233       10801 :       TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
   31234       10801 :       TYPE_CANONICAL (t) = canonical_type_parameter (t);
   31235       10801 :       TREE_VEC_ELT (replacement, i) = t;
   31236             : 
   31237             :       /* Replace the current template parameter with new pack.  */
   31238       10801 :       TREE_VALUE (node) = TREE_CHAIN (t);
   31239             : 
   31240             :       /* Surgically adjust the associated constraint of adjusted parameter
   31241             :          and it's corresponding contribution to the current template
   31242             :          requirements.  */
   31243       10801 :       if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
   31244             :         {
   31245           8 :           tree id = unpack_concept_check (constr);
   31246           8 :           TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
   31247           8 :           tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
   31248           8 :           TEMPLATE_PARM_CONSTRAINTS (node) = fold;
   31249             : 
   31250             :           /* If there was a constraint, we also need to replace that in
   31251             :              the template requirements, which we've already built.  */
   31252           8 :           tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
   31253           8 :           reqs = find_template_requirement (reqs, constr);
   31254           8 :           *reqs = fold;
   31255             :         }
   31256             :     }
   31257             : 
   31258       10795 :   for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
   31259           0 :     TREE_VEC_ELT (replacement, i)
   31260           0 :       = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
   31261             : 
   31262             :   /* If there are more levels then build up the replacement with the outer
   31263             :      template parms.  */
   31264       10795 :   if (depth > 1)
   31265       10659 :     replacement = add_to_template_args (template_parms_to_args
   31266       10659 :                                         (TREE_CHAIN (current_template_parms)),
   31267             :                                         replacement);
   31268             : 
   31269       10795 :   return tsubst (parm, replacement, tf_none, NULL_TREE);
   31270             : }
   31271             : 
   31272             : /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
   31273             :    0..N-1.  */
   31274             : 
   31275             : void
   31276       76591 : declare_integer_pack (void)
   31277             : {
   31278       76591 :   tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
   31279             :                                build_function_type_list (integer_type_node,
   31280             :                                                          integer_type_node,
   31281             :                                                          NULL_TREE),
   31282             :                                NULL_TREE, ECF_CONST);
   31283       76591 :   DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
   31284       76591 :   set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
   31285             :                               CP_BUILT_IN_INTEGER_PACK);
   31286       76591 : }
   31287             : 
   31288             : /* Walk the decl or type specialization table calling FN on each
   31289             :    entry.  */
   31290             : 
   31291             : void
   31292        3320 : walk_specializations (bool decls_p,
   31293             :                       void (*fn) (bool decls_p, spec_entry *entry, void *data),
   31294             :                       void *data)
   31295             : {
   31296        3320 :   spec_hash_table *table = decls_p ? decl_specializations
   31297             :     : type_specializations;
   31298        3320 :   spec_hash_table::iterator end (table->end ());
   31299     1263710 :   for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter)
   31300      630195 :     fn (decls_p, *iter, data);
   31301        3320 : }
   31302             : 
   31303             : /* Lookup the specialization of *ELT, in the decl or type
   31304             :    specialization table.  Return the SPEC that's already there, or
   31305             :    NULL if nothing.  */
   31306             : 
   31307             : tree
   31308     2034786 : match_mergeable_specialization (bool decl_p, spec_entry *elt)
   31309             : {
   31310     4069572 :   hash_table<spec_hasher> *specializations
   31311     2034786 :     = decl_p ? decl_specializations : type_specializations;
   31312     2034786 :   hashval_t hash = spec_hasher::hash (elt);
   31313     2034786 :   auto *slot = specializations->find_slot_with_hash (elt, hash, NO_INSERT);
   31314             : 
   31315     2034786 :   if (slot)
   31316     1613725 :     return (*slot)->spec;
   31317             : 
   31318             :   return NULL_TREE;
   31319             : }
   31320             : 
   31321             : /* Return flags encoding whether SPEC is on the instantiation and/or
   31322             :    specialization lists of TMPL.  */
   31323             : 
   31324             : unsigned
   31325      418488 : get_mergeable_specialization_flags (tree tmpl, tree decl)
   31326             : {
   31327      418488 :   unsigned flags = 0;
   31328             : 
   31329      418488 :   for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
   31330     9589158 :        inst; inst = TREE_CHAIN (inst))
   31331     9172184 :     if (TREE_VALUE (inst) == decl)
   31332             :       {
   31333             :         flags |= 1;
   31334             :         break;
   31335             :       }
   31336             : 
   31337      836976 :   if (CLASS_TYPE_P (TREE_TYPE (decl))
   31338      182938 :       && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))
   31339      596700 :       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
   31340             :     /* Only need to search if DECL is a partial specialization.  */
   31341       14816 :     for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
   31342       31895 :          part; part = TREE_CHAIN (part))
   31343       17079 :       if (TREE_VALUE (part) == decl)
   31344             :         {
   31345           0 :           flags |= 2;
   31346           0 :           break;
   31347             :         }
   31348             : 
   31349      418488 :   return flags;
   31350             : }
   31351             : 
   31352             : /* Add a new specialization described by SPEC.  DECL is the
   31353             :    maybe-template decl and FLAGS is as returned from
   31354             :    get_mergeable_specialization_flags.  */
   31355             : 
   31356             : void
   31357      227785 : add_mergeable_specialization (bool decl_p, bool alias_p, spec_entry *elt,
   31358             :                               tree decl, unsigned flags)
   31359             : {
   31360      227785 :   hashval_t hash = spec_hasher::hash (elt);
   31361      227785 :   if (decl_p)
   31362             :     {
   31363      158732 :       auto *slot = decl_specializations->find_slot_with_hash (elt, hash, INSERT);
   31364             : 
   31365      158732 :       gcc_checking_assert (!*slot);
   31366      158732 :       auto entry = ggc_alloc<spec_entry> ();
   31367      158732 :       *entry = *elt;
   31368      158732 :       *slot = entry;
   31369             : 
   31370      158732 :       if (alias_p)
   31371             :         {
   31372        9571 :           elt->spec = TREE_TYPE (elt->spec);
   31373        9571 :           gcc_checking_assert (elt->spec);
   31374             :         }
   31375             :     }
   31376             : 
   31377      227785 :   if (!decl_p || alias_p)
   31378             :     {
   31379       78624 :       auto *slot = type_specializations->find_slot_with_hash (elt, hash, INSERT);
   31380             : 
   31381             :       /* We don't distinguish different constrained partial type
   31382             :          specializations, so there could be duplicates.  Everything else
   31383             :          must be new.   */
   31384       78624 :       if (!(flags & 2 && *slot))
   31385             :         {
   31386       78444 :           gcc_checking_assert (!*slot);
   31387             : 
   31388       78444 :           auto entry = ggc_alloc<spec_entry> ();
   31389       78444 :           *entry = *elt;
   31390       78444 :           *slot = entry;
   31391             :         }
   31392             :     }
   31393             : 
   31394      227785 :   if (flags & 1)
   31395         620 :     DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl)
   31396        1240 :       = tree_cons (elt->args, decl, DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl));
   31397             : 
   31398      227785 :   if (flags & 2)
   31399             :     {
   31400             :       /* A partial specialization.  */
   31401        8006 :       tree cons = tree_cons (elt->args, decl,
   31402        8006 :                              DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl));
   31403        8006 :       TREE_TYPE (cons) = decl_p ? TREE_TYPE (elt->spec) : elt->spec;
   31404        8006 :       DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl) = cons;
   31405             :     }
   31406      227785 : }
   31407             : 
   31408             : /* Set up the hash tables for template instantiations.  */
   31409             : 
   31410             : void
   31411       89260 : init_template_processing (void)
   31412             : {
   31413       89260 :   decl_specializations = hash_table<spec_hasher>::create_ggc (37);
   31414       89260 :   type_specializations = hash_table<spec_hasher>::create_ggc (37);
   31415             : 
   31416       89260 :   if (cxx_dialect >= cxx11)
   31417       76591 :     declare_integer_pack ();
   31418       89260 : }
   31419             : 
   31420             : /* Print stats about the template hash tables for -fstats.  */
   31421             : 
   31422             : void
   31423           0 : print_template_statistics (void)
   31424             : {
   31425           0 :   fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
   31426           0 :            "%f collisions\n", (long) decl_specializations->size (),
   31427           0 :            (long) decl_specializations->elements (),
   31428             :            decl_specializations->collisions ());
   31429           0 :   fprintf (stderr, "type_specializations: size %ld, %ld elements, "
   31430           0 :            "%f collisions\n", (long) type_specializations->size (),
   31431           0 :            (long) type_specializations->elements (),
   31432             :            type_specializations->collisions ());
   31433           0 : }
   31434             : 
   31435             : #if CHECKING_P
   31436             : 
   31437             : namespace selftest {
   31438             : 
   31439             : /* Verify that build_non_dependent_expr () works, for various expressions,
   31440             :    and that location wrappers don't affect the results.  */
   31441             : 
   31442             : static void
   31443           1 : test_build_non_dependent_expr ()
   31444             : {
   31445           1 :   location_t loc = BUILTINS_LOCATION;
   31446             : 
   31447             :   /* Verify constants, without and with location wrappers.  */
   31448           1 :   tree int_cst = build_int_cst (integer_type_node, 42);
   31449           1 :   ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
   31450             : 
   31451           1 :   tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
   31452           1 :   ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
   31453           1 :   ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
   31454             : 
   31455           1 :   tree string_lit = build_string (4, "foo");
   31456           1 :   TREE_TYPE (string_lit) = char_array_type_node;
   31457           1 :   string_lit = fix_string_type (string_lit);
   31458           1 :   ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
   31459             : 
   31460           1 :   tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
   31461           1 :   ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
   31462           1 :   ASSERT_EQ (wrapped_string_lit,
   31463             :              build_non_dependent_expr (wrapped_string_lit));
   31464           1 : }
   31465             : 
   31466             : /* Verify that type_dependent_expression_p () works correctly, even
   31467             :    in the presence of location wrapper nodes.  */
   31468             : 
   31469             : static void
   31470           1 : test_type_dependent_expression_p ()
   31471             : {
   31472           1 :   location_t loc = BUILTINS_LOCATION;
   31473             : 
   31474           1 :   tree name = get_identifier ("foo");
   31475             : 
   31476             :   /* If no templates are involved, nothing is type-dependent.  */
   31477           1 :   gcc_assert (!processing_template_decl);
   31478           1 :   ASSERT_FALSE (type_dependent_expression_p (name));
   31479             : 
   31480           1 :   ++processing_template_decl;
   31481             : 
   31482             :   /* Within a template, an unresolved name is always type-dependent.  */
   31483           1 :   ASSERT_TRUE (type_dependent_expression_p (name));
   31484             : 
   31485             :   /* Ensure it copes with NULL_TREE and errors.  */
   31486           1 :   ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
   31487           1 :   ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
   31488             : 
   31489             :   /* A USING_DECL in a template should be type-dependent, even if wrapped
   31490             :      with a location wrapper (PR c++/83799).  */
   31491           1 :   tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
   31492           1 :   TREE_TYPE (using_decl) = integer_type_node;
   31493           1 :   ASSERT_TRUE (type_dependent_expression_p (using_decl));
   31494           1 :   tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
   31495           1 :   ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
   31496           1 :   ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
   31497             : 
   31498           1 :   --processing_template_decl;
   31499           1 : }
   31500             : 
   31501             : /* Run all of the selftests within this file.  */
   31502             : 
   31503             : void
   31504           1 : cp_pt_cc_tests ()
   31505             : {
   31506           1 :   test_build_non_dependent_expr ();
   31507           1 :   test_type_dependent_expression_p ();
   31508           1 : }
   31509             : 
   31510             : } // namespace selftest
   31511             : 
   31512             : #endif /* #if CHECKING_P */
   31513             : 
   31514             : #include "gt-cp-pt.h"

Generated by: LCOV version 1.16